Submission

Status:

-PP--P----

Subtask/Task Score:

30/100

Score: 30

User: Bestzu

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

Language: cpp

Time: 0.003 second

Submitted On: 2025-10-15 11:32:23

#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(lmax < rmax) {
			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;
}