Submission

Status:

-P---PP-PP

Subtask/Task Score:

50/100

Score: 50

User: nga12345

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

Language: cpp

Time: 0.003 second

Submitted On: 2025-10-09 09:39:26

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

using namespace std;

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

    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) { // slowly coming to middl
        if (leftMax < rightMax) { // 
            left++;
            leftMax = fmax(leftMax, heights[left]);
            cout << leftMax;

            water += leftMax - heights[left];
        } else {
            right--;
            rightMax = fmax(rightMax, heights[right]);
            water += rightMax - heights[right];
        } // ??????????????????????????????????
    }

    cout << water;
    return 0;
}