#include <bits/stdc++.h>
#define foreach for
#define in :
using namespace std;
typedef long long ll ;
const int LOG = 18;
const long long base = 31;
const long long MODULO = 1e9+7;
const long long INF = 1e18+1;
template<class X ,class Y>
bool Minimize(X &x , Y y)
{
if(x > y)
{
x = y;
return true;
}
return false;
}
template<class X , class Y>
bool Maximize(X &x , Y y)
{
if(x < y)
{
x = y;
return true;
}
return false;
}
struct lines
{
ll a ,b;
lines(ll _a , ll _b )
{
a = _a;
b = _b;
}
ll eval(ll x)
{
return a *x + b;
}
};
deque<lines>adu;
bool bad(lines &l1 , lines &l2 ,lines &l3)
{
return (l3.b - l1.b) * (l1.a - l2.a) < (l2.b - l1.b) * (l1.a - l3.a);
}
void Add(lines x)
{
while((int)adu.size() >= 2 && bad( adu[(int)adu.size()-2] , adu[(int)adu.size()-1] , x ))
{
adu.pop_back();
}
adu.push_back(x);
}
ll Get(ll x)
{
while((int)adu.size() >= 2 && adu[0].eval(x) >= adu[1].eval(x) )adu.pop_front();
return adu[0].eval(x);
}
const int N = 1e6+1;
int n , a[N];
int _next[N];
long long dp[N];
void Input()
{
cin >> n ;
for(int i = 1; i <= n ; i++)
{
cin >> a[i];
if(a[_next[i-1]] <= a[i])_next[i] = i;
else _next[i] = _next[i-1];
}
}
void Process()
{
Add(lines(0 , 0));
for(int i =1 ; i <= n ; i++)
{
if(_next[i] != i)dp[i] = dp[i-1];
else dp[i] = Get(a[i]) + n *1LL * a[i];
Add(lines( -i * 1LL , dp[i] ));
}
cout << dp[n];
}
int main()
{
ios_base :: sync_with_stdio(0);cin.tie(0);
int test = 1;
while(test--)
{
Input();
Process();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
91 ms |
15852 KB |
Output is correct |
17 |
Correct |
92 ms |
16024 KB |
Output is correct |
18 |
Correct |
82 ms |
15932 KB |
Output is correct |
19 |
Correct |
92 ms |
15864 KB |
Output is correct |
20 |
Correct |
95 ms |
15948 KB |
Output is correct |
21 |
Correct |
91 ms |
15908 KB |
Output is correct |
22 |
Correct |
89 ms |
15972 KB |
Output is correct |
23 |
Correct |
98 ms |
15916 KB |
Output is correct |
24 |
Correct |
96 ms |
15888 KB |
Output is correct |
25 |
Correct |
110 ms |
15892 KB |
Output is correct |
26 |
Correct |
98 ms |
15892 KB |
Output is correct |
27 |
Correct |
88 ms |
15972 KB |
Output is correct |
28 |
Correct |
91 ms |
15948 KB |
Output is correct |
29 |
Correct |
114 ms |
15952 KB |
Output is correct |
30 |
Correct |
99 ms |
15828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
32148 KB |
Output is correct |
2 |
Correct |
106 ms |
32200 KB |
Output is correct |
3 |
Correct |
111 ms |
32196 KB |
Output is correct |
4 |
Correct |
114 ms |
32120 KB |
Output is correct |
5 |
Correct |
110 ms |
32128 KB |
Output is correct |
6 |
Correct |
110 ms |
32132 KB |
Output is correct |
7 |
Correct |
108 ms |
32180 KB |
Output is correct |
8 |
Correct |
107 ms |
32128 KB |
Output is correct |
9 |
Correct |
109 ms |
32140 KB |
Output is correct |
10 |
Correct |
108 ms |
32180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
0 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
91 ms |
15852 KB |
Output is correct |
31 |
Correct |
92 ms |
16024 KB |
Output is correct |
32 |
Correct |
82 ms |
15932 KB |
Output is correct |
33 |
Correct |
92 ms |
15864 KB |
Output is correct |
34 |
Correct |
95 ms |
15948 KB |
Output is correct |
35 |
Correct |
91 ms |
15908 KB |
Output is correct |
36 |
Correct |
89 ms |
15972 KB |
Output is correct |
37 |
Correct |
98 ms |
15916 KB |
Output is correct |
38 |
Correct |
96 ms |
15888 KB |
Output is correct |
39 |
Correct |
110 ms |
15892 KB |
Output is correct |
40 |
Correct |
98 ms |
15892 KB |
Output is correct |
41 |
Correct |
88 ms |
15972 KB |
Output is correct |
42 |
Correct |
91 ms |
15948 KB |
Output is correct |
43 |
Correct |
114 ms |
15952 KB |
Output is correct |
44 |
Correct |
99 ms |
15828 KB |
Output is correct |
45 |
Correct |
105 ms |
32148 KB |
Output is correct |
46 |
Correct |
106 ms |
32200 KB |
Output is correct |
47 |
Correct |
111 ms |
32196 KB |
Output is correct |
48 |
Correct |
114 ms |
32120 KB |
Output is correct |
49 |
Correct |
110 ms |
32128 KB |
Output is correct |
50 |
Correct |
110 ms |
32132 KB |
Output is correct |
51 |
Correct |
108 ms |
32180 KB |
Output is correct |
52 |
Correct |
107 ms |
32128 KB |
Output is correct |
53 |
Correct |
109 ms |
32140 KB |
Output is correct |
54 |
Correct |
108 ms |
32180 KB |
Output is correct |
55 |
Correct |
0 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
0 ms |
340 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
0 ms |
340 KB |
Output is correct |
65 |
Correct |
1 ms |
340 KB |
Output is correct |
66 |
Correct |
0 ms |
340 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Correct |
1 ms |
340 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
1 ms |
340 KB |
Output is correct |
74 |
Correct |
1 ms |
340 KB |
Output is correct |
75 |
Correct |
0 ms |
340 KB |
Output is correct |
76 |
Correct |
87 ms |
15952 KB |
Output is correct |
77 |
Incorrect |
97 ms |
15928 KB |
Output isn't correct |
78 |
Halted |
0 ms |
0 KB |
- |