이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
ll n;
cin>>n;
ll prev = 0;
ll tot = 0;
stack<pair<ll,ll>> s; //index, time
ll val;
for (ll i = 0; i<n; i++){
cin>>val;
if (val<=prev){
continue;
} else {
prev=val;
}
ll ind = i;
tot+=(n-ind)*val;
while (s.size()>0){
auto f = s.top();
ll tind = f.first;
ll 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... |