#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using pll = pair<ll, ll>;
#define sz(x) int(x.size())
const ll INF = 1'000'000'000'000'000'000LL;
struct line
{
double a;
double b;
};
double intersect(line A, line B)
{
return (B.b-A.b)/(A.a-B.a);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
int TS = N;
vector<pll> T;
T.push_back({0, 0});
for(int i = 1; i <= N; i++)
{
ll t;
cin >> t;
if(t <= T.back().first)
T.back().second++;
else
T.push_back({t, 1});
}
N = sz(T)-1;
vll suff(1+N, 0);
suff[0] = TS;
for(int i = 1; i <= N; i++) suff[i] = suff[i-1] - T[i].second;
vll dp(1+N, INF);
dp[0] = 0;
vector<line> L;
L.push_back(line{double(suff[0]), double(dp[0])});
for(int i = 1; i <= N; i++)
{
int lo = 0;
int hi = sz(L) - 1;
double x = T[i].first;
while(lo != hi)
{
int mid = (lo+hi)/2;
if(intersect(L[mid], L[mid+1]) <= x)
lo = mid+1;
else
hi = mid;
}
dp[i] = ll(L[lo].a) * T[i].first + ll(L[lo].b);
if(i < N)
{
line new_line{double(suff[i]), double(dp[i])};
while(sz(L) >= 2 && intersect(L[sz(L)-2], L[sz(L)-1]) >= intersect(L[sz(L)-1], new_line))
L.pop_back();
L.push_back(new_line);
}
}
cout << dp[N] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
312 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
121 ms |
14324 KB |
Output is correct |
17 |
Correct |
127 ms |
15420 KB |
Output is correct |
18 |
Correct |
94 ms |
10308 KB |
Output is correct |
19 |
Correct |
125 ms |
15400 KB |
Output is correct |
20 |
Correct |
121 ms |
16804 KB |
Output is correct |
21 |
Correct |
120 ms |
15508 KB |
Output is correct |
22 |
Correct |
110 ms |
14116 KB |
Output is correct |
23 |
Correct |
139 ms |
20260 KB |
Output is correct |
24 |
Correct |
133 ms |
18176 KB |
Output is correct |
25 |
Correct |
136 ms |
18064 KB |
Output is correct |
26 |
Correct |
155 ms |
21828 KB |
Output is correct |
27 |
Correct |
102 ms |
12472 KB |
Output is correct |
28 |
Correct |
119 ms |
16148 KB |
Output is correct |
29 |
Correct |
133 ms |
18680 KB |
Output is correct |
30 |
Correct |
142 ms |
21024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
1968 KB |
Output is correct |
2 |
Correct |
95 ms |
2324 KB |
Output is correct |
3 |
Correct |
94 ms |
2328 KB |
Output is correct |
4 |
Correct |
95 ms |
2364 KB |
Output is correct |
5 |
Correct |
93 ms |
2372 KB |
Output is correct |
6 |
Correct |
91 ms |
2388 KB |
Output is correct |
7 |
Correct |
97 ms |
2524 KB |
Output is correct |
8 |
Correct |
105 ms |
2336 KB |
Output is correct |
9 |
Correct |
94 ms |
2372 KB |
Output is correct |
10 |
Correct |
97 ms |
2420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
312 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
316 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
1 ms |
320 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
312 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
121 ms |
14324 KB |
Output is correct |
31 |
Correct |
127 ms |
15420 KB |
Output is correct |
32 |
Correct |
94 ms |
10308 KB |
Output is correct |
33 |
Correct |
125 ms |
15400 KB |
Output is correct |
34 |
Correct |
121 ms |
16804 KB |
Output is correct |
35 |
Correct |
120 ms |
15508 KB |
Output is correct |
36 |
Correct |
110 ms |
14116 KB |
Output is correct |
37 |
Correct |
139 ms |
20260 KB |
Output is correct |
38 |
Correct |
133 ms |
18176 KB |
Output is correct |
39 |
Correct |
136 ms |
18064 KB |
Output is correct |
40 |
Correct |
155 ms |
21828 KB |
Output is correct |
41 |
Correct |
102 ms |
12472 KB |
Output is correct |
42 |
Correct |
119 ms |
16148 KB |
Output is correct |
43 |
Correct |
133 ms |
18680 KB |
Output is correct |
44 |
Correct |
142 ms |
21024 KB |
Output is correct |
45 |
Correct |
100 ms |
1968 KB |
Output is correct |
46 |
Correct |
95 ms |
2324 KB |
Output is correct |
47 |
Correct |
94 ms |
2328 KB |
Output is correct |
48 |
Correct |
95 ms |
2364 KB |
Output is correct |
49 |
Correct |
93 ms |
2372 KB |
Output is correct |
50 |
Correct |
91 ms |
2388 KB |
Output is correct |
51 |
Correct |
97 ms |
2524 KB |
Output is correct |
52 |
Correct |
105 ms |
2336 KB |
Output is correct |
53 |
Correct |
94 ms |
2372 KB |
Output is correct |
54 |
Correct |
97 ms |
2420 KB |
Output is correct |
55 |
Correct |
1 ms |
204 KB |
Output is correct |
56 |
Correct |
1 ms |
204 KB |
Output is correct |
57 |
Correct |
1 ms |
204 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
316 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
1 ms |
332 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
0 ms |
204 KB |
Output is correct |
64 |
Correct |
0 ms |
204 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
1 ms |
204 KB |
Output is correct |
67 |
Correct |
1 ms |
332 KB |
Output is correct |
68 |
Correct |
1 ms |
332 KB |
Output is correct |
69 |
Correct |
1 ms |
204 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |
71 |
Correct |
1 ms |
204 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
1 ms |
204 KB |
Output is correct |
74 |
Correct |
1 ms |
320 KB |
Output is correct |
75 |
Correct |
1 ms |
204 KB |
Output is correct |
76 |
Correct |
76 ms |
1660 KB |
Output is correct |
77 |
Correct |
74 ms |
1712 KB |
Output is correct |
78 |
Correct |
69 ms |
1604 KB |
Output is correct |
79 |
Correct |
71 ms |
1720 KB |
Output is correct |
80 |
Correct |
75 ms |
1688 KB |
Output is correct |
81 |
Correct |
72 ms |
1736 KB |
Output is correct |
82 |
Correct |
67 ms |
1808 KB |
Output is correct |
83 |
Correct |
70 ms |
1704 KB |
Output is correct |
84 |
Correct |
73 ms |
1788 KB |
Output is correct |
85 |
Correct |
78 ms |
1696 KB |
Output is correct |
86 |
Correct |
68 ms |
1568 KB |
Output is correct |
87 |
Correct |
74 ms |
1572 KB |
Output is correct |
88 |
Correct |
73 ms |
1568 KB |
Output is correct |
89 |
Correct |
73 ms |
1668 KB |
Output is correct |
90 |
Correct |
69 ms |
1476 KB |
Output is correct |
91 |
Correct |
68 ms |
1604 KB |
Output is correct |
92 |
Correct |
135 ms |
17700 KB |
Output is correct |
93 |
Correct |
126 ms |
15780 KB |
Output is correct |
94 |
Correct |
66 ms |
1300 KB |
Output is correct |
95 |
Correct |
75 ms |
1316 KB |
Output is correct |
96 |
Correct |
67 ms |
1348 KB |
Output is correct |
97 |
Correct |
69 ms |
1352 KB |
Output is correct |
98 |
Correct |
78 ms |
1464 KB |
Output is correct |
99 |
Correct |
74 ms |
1304 KB |
Output is correct |
100 |
Correct |
70 ms |
1152 KB |
Output is correct |