Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: goine

Problemset: ปริมาตรน้ำท่วม

Language: cpp

Time: 0.002 second

Submitted On: 2025-10-12 17:13:27

#include<iostream>
#include<vector>
#include<cmath>

using namespace std;

int main() {
    int size;
    cin >> size;

    vector<int> heights(size);
    for (int i = 0; i < size; i++) {
        cin >> heights[i];
    }

    int water = 0;
    int left = 0;
    int right = size - 1;
    int leftMax = heights[left], rightMax = heights[right];
    
    while (left < right) {
        if (leftMax < rightMax) { 
            left++;
            leftMax = fmax(leftMax, heights[left]);
            water += leftMax - heights[left];
        } else {
            right--;
            rightMax = fmax(rightMax, heights[right]);
            water += rightMax - heights[right];
        }
    }

    cout << water;
    return 0;
}