Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: ChanakanStudy

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

Language: cpp

Time: 0.003 second

Submitted On: 2026-10-04 16:48:21

#include <iostream>
#include <vector>
using namespace std;
#define ll long long

int main() {
    ll n , water = 0 , l , r ,temp;
    vector<ll> collect;
    cin >> n;
    while(n--) {
        cin >> temp;
        collect.push_back(temp);
    }
    l = 0 , r = collect.size()-1;
    ll 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;
}