Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: wleo.lwo

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

Language: cpp

Time: 0.004 second

Submitted On: 2026-10-09 13:26:53

#include <iostream>
#include <vector>
using namespace std;

int main() {
    long long n , water = 0 , l , r ,temp;
    vector<long long> collect;
    cin >> n;
    while(n--) {
        cin >> temp;
        collect.push_back(temp);
    }
    l = 0 , r = collect.size()-1;
    long long leftmax = collect[l] , rightmax= collect[r];
    while(l != r) {
        if(leftmax > rightmax) {
            rightmax = max(rightmax , collect[r-1]);
            water += rightmax - collect[r-1];
            r--;
        } 
        else {
            leftmax = max(leftmax , collect[l+1]);
            water += leftmax - collect[l+1];
            l++;
        }
    }
    cout << water;
}