Submission
Status:
PPPPPPPPPP
Subtask/Task Score:
100/100
Score: 100
User: Bestzu
Problemset: ปริมาตรน้ำท่วม
Language: cpp
Time: 0.002 second
Submitted On: 2025-10-15 11:26:31
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long int
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
vector<int> h(n);
for(int i = 0; i < n; i++) {
cin >> h[i];
}
int cnt = 0;
int lmax = 0, rmax = 0, l = 0, r = n-1;
while(l < r) {
if(h[l] < h[r]) {
lmax = max(lmax, h[l]);
cnt += lmax - h[l];
l++;
}
else {
rmax = max(rmax, h[r]);
cnt += rmax - h[r];
r--;
}
}
cout << cnt;
return 0;
}