Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: Kidmaiok

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

Language: cpp

Time: 0.002 second

Submitted On: 2025-10-11 12:31:11

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n,result = 0,x;
    vector<int> height;
    cin >> n;
    for(int i =  0;i<n;i++){
        cin >> x;
        height.push_back(x);
    }
    int left = 0 , right = n-1, res = 0,lmax = 0,rmax = 0;
    while(left <= right){//ทำจนกว่าทั้งสองฝั่งเลยกัน
        if(height[left] <= height[right]){
            if(height[left] >= lmax){
                lmax = max(lmax,height[left]);
            }else{
                res += lmax - height[left];
            }
            
            left ++;
        }else{ //height[left] > height[right]
            if(height[right] >= rmax){
                rmax = max(rmax,height[right]);
            }else{
                res += rmax - height[right];
            }
            right --;
        }
    }
    cout << res;
}