#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <iterator>
#include <random>
#define ll long long
#define ld long double
#define inf (ll)(2*1e18)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define endl "\n"
using namespace std;
struct Line{
ll k, b;
};
void solve(){
ll n, i, x, res=0;
cin>>n;
vector<pair<ll, ll>> a;
cin>>x;
a.pb({x, 1});
for(i=1;i<n;++i){
cin>>x;
if(a.back().first >= x){
++a.back().second;
}else{
a.pb({x, 1});
}
}
Line cur, next;
cur.k = n;
cur.b = 0;
vector<ll> dp(a.size());
queue<Line> q;
for(i=0;i<a.size();++i){
while(!q.empty()){
if((cur.k - q.front().k)*a[i].first >= q.front().b - cur.b){
cur = q.front();
q.pop();
}else{
break;
}
}
n -= a[i].second;
dp[i] = cur.k*a[i].first + cur.b;
next.k = n;
next.b = dp[i];
q.push(next);
}
cout<<dp.back()<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
srand(time(nullptr));
ll t=1;
// cin>>t;
for(;t>0;--t){
solve();
}
return 0;
}
# | 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... |