Submission

Status:

PPPP-PPPPP

Subtask/Task Score:

90/100

Score: 90

User: Neozaawwman1

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

Language: cpp

Time: 0.003 second

Submitted On: 2025-10-10 10:56:26

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;cin>>n;
	int sum=0;
	int max=0;
	int count=0;
	int pre=0;
	vector<int> arr;
	for(int i=0; i<n; i++){
		int x;cin>>x;
		if(max<x){
			max=x;
			count=0;
		}else{
			sum+=max-x;
			arr.push_back(max-x);
			count++;
		}
		//cout<<count<<" "<<max<<" "<<sum<<endl;
		if(i==n-1 && x<max && x<pre){
			for(int j=0; j<count; j++){
				sum=sum-arr[arr.size()-1-j];
			}
		}else if(i==n-1 && x<max && x>=pre){
			for(int j=0; j<count; j++){
				sum=sum-(max-x);
			}
		}
		pre = x;
	}
	cout<<sum;
	return 0;
}