#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
#include <time.h>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9;
const ll inf = 1e18;
const ll mod = 1000000007;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 2e5+5;
const ll A = 911382323;
const ll B = 972663749;
void solve(){
int n;
cin >> n;
ll a[n];
fr(i, 0, n){
cin >> a[i];
}
stack<int> S;
pq<pair<ll, int> > Q;
bool active[n];
memset(active, false, sizeof(active));
ll dp[n+1];
dp[n] = 0;
for(int i = n-1; i >= 0; i --){
while(!S.empty() && a[S.top()] <= a[i]){
active[S.top()] = false;
S.pop();
}
while(!Q.empty() && !active[Q.top().nd]){
Q.pop();
}
dp[i] = a[i]*(n-i);
if(!Q.empty()){
dp[i] += -(int)Q.top().st;
}
Q.push({-dp[i], i});
active[i] = true;
S.push(i);
}
ll mx = 0;
ll ans = inf;
fr(i, 0, n){
mx = max(mx, a[i]);
ans = min(ans, dp[i+1]+mx*n);
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
//cin >> tc;
while(tc--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
133 ms |
16920 KB |
Output is correct |
2 |
Correct |
132 ms |
17016 KB |
Output is correct |
3 |
Correct |
134 ms |
17016 KB |
Output is correct |
4 |
Correct |
132 ms |
17016 KB |
Output is correct |
5 |
Correct |
136 ms |
17040 KB |
Output is correct |
6 |
Correct |
131 ms |
17016 KB |
Output is correct |
7 |
Correct |
135 ms |
17016 KB |
Output is correct |
8 |
Correct |
133 ms |
16888 KB |
Output is correct |
9 |
Correct |
132 ms |
17016 KB |
Output is correct |
10 |
Correct |
137 ms |
17016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |