Submission
Status:
PPPPPPPPPP
Subtask/Task Score:
100/100
Score: 100
User: rice_ot
Problemset: ปริมาตรน้ำท่วม
Language: cpp
Time: 0.002 second
Submitted On: 2025-11-17 22:39:22
#include <bits/stdc++.h>
using namespace std;
int main(){
int n; cin>>n;
vector<int> dam(n);
for(auto& i : dam) cin>>i;
int lmax = dam[0], rmax = dam[n-1];
int l = 1, r = n-2;
int total = 0;
while(l <= r){
if(lmax <= rmax){
if(dam[l] > lmax) lmax = dam[l];
else{
total+= lmax - dam[l];
l++;
}
}
else if(lmax > rmax){
if(dam[r] > rmax) rmax = dam[r];
else{
total+= rmax - dam[r];
r--;
}
}
}
cout<<total;
}