#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
#define int long long
const int mxn=1e5,mod=1000000007,lg=20,root=1000,inf=1e18;
struct line{int m,c;};
double intersect(line a,line b){return ((b.c-a.c)*1.0/(a.m-b.m)*1.0);}
static bool cmp(pair<int,line>a,pair<int,line>b){return a.f<b.f;}
vector<pair<int,line>>con;
void add(line a){
while(con.size()>1&&(intersect(con.back().s,a)<=(intersect(con[con.size()-1].s,con[con.size()-2].s))))con.pop_back();
if(con.empty())con.pb({0,a});
else con.pb({intersect(con.back().s,a),a});
}
int qry(int x){
auto it=ub(all(con),make_pair(x,line{}),cmp);
it--;
line tmp=(*it).s;
return x*(tmp.m)+tmp.c;
}
/*
dpi =dp(j)+ti*(n-j+1)
*/
int32_t main(){
fastio
int n;cin>>n;
vector<int>v(n+1),p(n+1),dp(n+1,INT_MAX);
dp[0]=0;
for(int i=1;i<=n;i++)cin>>v[i],v[i]=max(v[i-1],v[i]);
add(line{0,0});
for(int i=1;i<=n;i++){
dp[i]=v[i]*n;
//ti(1-j)+dp(j-1)
//-tij+dp(j-1)+ti
add(line{-i,dp[i-1]});
if(i>1)dp[i]=min(dp[i],qry(v[i])+v[i]*(n+1));
}
cout<<dp[n]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
96 ms |
23944 KB |
Output is correct |
17 |
Correct |
83 ms |
23900 KB |
Output is correct |
18 |
Correct |
76 ms |
23900 KB |
Output is correct |
19 |
Correct |
83 ms |
23948 KB |
Output is correct |
20 |
Correct |
98 ms |
24152 KB |
Output is correct |
21 |
Correct |
82 ms |
23896 KB |
Output is correct |
22 |
Correct |
80 ms |
23900 KB |
Output is correct |
23 |
Correct |
90 ms |
23948 KB |
Output is correct |
24 |
Correct |
84 ms |
23900 KB |
Output is correct |
25 |
Correct |
86 ms |
23932 KB |
Output is correct |
26 |
Correct |
90 ms |
23932 KB |
Output is correct |
27 |
Correct |
88 ms |
23936 KB |
Output is correct |
28 |
Correct |
82 ms |
23900 KB |
Output is correct |
29 |
Correct |
89 ms |
23924 KB |
Output is correct |
30 |
Correct |
88 ms |
23920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
23900 KB |
Output is correct |
2 |
Correct |
92 ms |
23896 KB |
Output is correct |
3 |
Correct |
92 ms |
23896 KB |
Output is correct |
4 |
Correct |
93 ms |
23936 KB |
Output is correct |
5 |
Correct |
92 ms |
23900 KB |
Output is correct |
6 |
Correct |
93 ms |
23932 KB |
Output is correct |
7 |
Correct |
96 ms |
23900 KB |
Output is correct |
8 |
Correct |
98 ms |
23940 KB |
Output is correct |
9 |
Correct |
100 ms |
23928 KB |
Output is correct |
10 |
Correct |
95 ms |
23936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
352 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
46 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
96 ms |
23944 KB |
Output is correct |
31 |
Correct |
83 ms |
23900 KB |
Output is correct |
32 |
Correct |
76 ms |
23900 KB |
Output is correct |
33 |
Correct |
83 ms |
23948 KB |
Output is correct |
34 |
Correct |
98 ms |
24152 KB |
Output is correct |
35 |
Correct |
82 ms |
23896 KB |
Output is correct |
36 |
Correct |
80 ms |
23900 KB |
Output is correct |
37 |
Correct |
90 ms |
23948 KB |
Output is correct |
38 |
Correct |
84 ms |
23900 KB |
Output is correct |
39 |
Correct |
86 ms |
23932 KB |
Output is correct |
40 |
Correct |
90 ms |
23932 KB |
Output is correct |
41 |
Correct |
88 ms |
23936 KB |
Output is correct |
42 |
Correct |
82 ms |
23900 KB |
Output is correct |
43 |
Correct |
89 ms |
23924 KB |
Output is correct |
44 |
Correct |
88 ms |
23920 KB |
Output is correct |
45 |
Correct |
92 ms |
23900 KB |
Output is correct |
46 |
Correct |
92 ms |
23896 KB |
Output is correct |
47 |
Correct |
92 ms |
23896 KB |
Output is correct |
48 |
Correct |
93 ms |
23936 KB |
Output is correct |
49 |
Correct |
92 ms |
23900 KB |
Output is correct |
50 |
Correct |
93 ms |
23932 KB |
Output is correct |
51 |
Correct |
96 ms |
23900 KB |
Output is correct |
52 |
Correct |
98 ms |
23940 KB |
Output is correct |
53 |
Correct |
100 ms |
23928 KB |
Output is correct |
54 |
Correct |
95 ms |
23936 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
344 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
352 KB |
Output is correct |
65 |
Correct |
1 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
0 ms |
348 KB |
Output is correct |
68 |
Correct |
1 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
71 |
Halted |
0 ms |
0 KB |
- |