This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
int n;
cin>>n;
ll prev = 0;
ll tot = 0;
stack<pair<int,int>> s; //index, time
for (int i = 0; i<n; i++){
ll val;
cin>>val;
if (val<=prev){prev=val;continue;}
prev=val;
int ind = i;
tot+=(n-ind)*val;
while (s.size()>0){
auto f = s.top();
int tind = f.first;
int th = f.second;
//cout<<tind<<' '<<th<<'\n';
//can we do better by popping?
if ((n-ind)*val+(n-tind)*th>=(n-tind)*val){
//cout<<"subtract\n";
tot-=(n-ind)*val+(n-tind)*th;
//cout<<tot<<'\n';
tot+=(n-tind)*val;
//cout<<tot<<'\n';
ind=tind;
s.pop();
} else {
break;
}
}
//cout<<i<<": "<<tot<<'\n';
s.push({ind,val});
}
cout<<tot<<'\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |