Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: Kidmaiok

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

Language: cpp

Time: 0.002 second

Submitted On: 2026-10-04 17:55:08

#include<bits/stdc++.h>
using namespace std;
int main(){
    int lmax=0,rmax=0,n,water = 0;
    vector<int> wall;
    cin >> n;
    for(int i =0; i <n; i ++){
        int x;
        cin >> x;
        wall.push_back(x);
    }
    lmax = wall[0];
    rmax = wall[n-1];
    int r = n-1;
    int l = 0;
    while(l < r){
        if(rmax <= lmax){
            r--;
            if(wall[r] < rmax){
                water += rmax-wall[r];
            }else{
                rmax = wall[r];
            }
        }else{
            l++;
            if(wall[l] < lmax){
                water += lmax-wall[l];
            }else{
                lmax = wall[l];
            }

        }
    }
    cout << water;
}