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;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
vector<pll> v;
vector<pll> st;
int n;
ll total = 0;
ll sum = 0;
inline bool check(pll a,pll b){
ll ta = a.fs*a.sc+b.sc*(a.fs+b.fs);
ll tb = b.fs*(a.sc+b.sc);
if(ta>=tb)return true;
else return false;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i = 0;i<n;i++){
int k;
cin>>k;
if(v.empty()||v.back().fs < k)v.push_back({k,1});
else v.back().sc++;
}
for(auto &i:v){
while(!st.empty()&&check(st.back(),i)){
total -= sum*st.back().sc;
sum -= st.back().fs;
i.sc+=st.back().sc;
st.pop_back();
}
sum += i.fs;
total += sum*i.sc;
st.push_back(i);
}
cout<<total;
}
# | 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... |