#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
int a[N]; ll f[N];
struct Line {
ll A, B;
Line() : A(0), B(0) {}
Line (ll _a, ll _b) {
A = _a, B = _b;
}
};
struct ConvexHullTrick {
Line st[N]; int sz, it = 1;
long double G (Line x, Line y) {
return 1.0 * (x.B - y.B) / (y.A - x.A);
}
ll calc(Line x, ll v) {
return x.A * v + x.B;
}
void add(ll x, ll y) {
Line cur = Line(x, y);
if (sz && st[sz].A == x && st[sz].B <= y) return;
while (sz > 1 && G(cur, st[sz]) <= G(st[sz], st[sz - 1])) --sz;
st[++sz] = cur;
}
ll get(ll x) {
if (it > sz) it = sz;
while (it < sz && calc(st[it + 1], x) <= calc(st[it], x)) ++it;
return calc(st[it], x);
}
} cht;
signed main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("discharging.inp", "r")) {
freopen("discharging.inp", "r", stdin);
freopen("discharging.out", "w", stdout);
}
#ifdef LOCAL_MACHINE
if (fopen("task.inp", "r")) {
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
#endif
int n; cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
cht.add(n, 0);
int mx = 0;
for (int i = 1; i <= n; ++i) {
mx = max(mx, a[i]);
f[i] = cht.get(mx);
cht.add(n - i, f[i]);
}
cout << f[n] << "\n";
}
Compilation message
Discharging.cpp: In function 'int main()':
Discharging.cpp:45:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen("discharging.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Discharging.cpp:46:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | freopen("discharging.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15956 KB |
Output is correct |
2 |
Correct |
6 ms |
15956 KB |
Output is correct |
3 |
Correct |
6 ms |
15984 KB |
Output is correct |
4 |
Correct |
7 ms |
15956 KB |
Output is correct |
5 |
Correct |
9 ms |
15956 KB |
Output is correct |
6 |
Correct |
7 ms |
15956 KB |
Output is correct |
7 |
Correct |
10 ms |
16004 KB |
Output is correct |
8 |
Correct |
7 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15924 KB |
Output is correct |
10 |
Correct |
7 ms |
15956 KB |
Output is correct |
11 |
Correct |
7 ms |
15984 KB |
Output is correct |
12 |
Correct |
8 ms |
15956 KB |
Output is correct |
13 |
Correct |
8 ms |
15984 KB |
Output is correct |
14 |
Correct |
7 ms |
16008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15996 KB |
Output is correct |
2 |
Correct |
8 ms |
15964 KB |
Output is correct |
3 |
Correct |
7 ms |
15960 KB |
Output is correct |
4 |
Correct |
9 ms |
15956 KB |
Output is correct |
5 |
Correct |
8 ms |
16072 KB |
Output is correct |
6 |
Correct |
8 ms |
15996 KB |
Output is correct |
7 |
Correct |
8 ms |
16084 KB |
Output is correct |
8 |
Correct |
7 ms |
15900 KB |
Output is correct |
9 |
Correct |
9 ms |
15996 KB |
Output is correct |
10 |
Correct |
9 ms |
15956 KB |
Output is correct |
11 |
Correct |
7 ms |
15980 KB |
Output is correct |
12 |
Correct |
7 ms |
15992 KB |
Output is correct |
13 |
Correct |
7 ms |
15956 KB |
Output is correct |
14 |
Correct |
8 ms |
16084 KB |
Output is correct |
15 |
Correct |
7 ms |
15992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15996 KB |
Output is correct |
2 |
Correct |
8 ms |
15964 KB |
Output is correct |
3 |
Correct |
7 ms |
15960 KB |
Output is correct |
4 |
Correct |
9 ms |
15956 KB |
Output is correct |
5 |
Correct |
8 ms |
16072 KB |
Output is correct |
6 |
Correct |
8 ms |
15996 KB |
Output is correct |
7 |
Correct |
8 ms |
16084 KB |
Output is correct |
8 |
Correct |
7 ms |
15900 KB |
Output is correct |
9 |
Correct |
9 ms |
15996 KB |
Output is correct |
10 |
Correct |
9 ms |
15956 KB |
Output is correct |
11 |
Correct |
7 ms |
15980 KB |
Output is correct |
12 |
Correct |
7 ms |
15992 KB |
Output is correct |
13 |
Correct |
7 ms |
15956 KB |
Output is correct |
14 |
Correct |
8 ms |
16084 KB |
Output is correct |
15 |
Correct |
7 ms |
15992 KB |
Output is correct |
16 |
Correct |
87 ms |
32640 KB |
Output is correct |
17 |
Correct |
104 ms |
33596 KB |
Output is correct |
18 |
Correct |
79 ms |
32072 KB |
Output is correct |
19 |
Correct |
101 ms |
33612 KB |
Output is correct |
20 |
Correct |
98 ms |
33320 KB |
Output is correct |
21 |
Correct |
95 ms |
33364 KB |
Output is correct |
22 |
Correct |
90 ms |
33036 KB |
Output is correct |
23 |
Correct |
108 ms |
34092 KB |
Output is correct |
24 |
Correct |
91 ms |
33476 KB |
Output is correct |
25 |
Correct |
95 ms |
33876 KB |
Output is correct |
26 |
Correct |
119 ms |
34216 KB |
Output is correct |
27 |
Correct |
92 ms |
32828 KB |
Output is correct |
28 |
Correct |
95 ms |
33592 KB |
Output is correct |
29 |
Correct |
94 ms |
33740 KB |
Output is correct |
30 |
Correct |
106 ms |
34024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
119 ms |
36156 KB |
Output is correct |
2 |
Correct |
115 ms |
35880 KB |
Output is correct |
3 |
Correct |
111 ms |
36512 KB |
Output is correct |
4 |
Correct |
110 ms |
36084 KB |
Output is correct |
5 |
Correct |
107 ms |
36476 KB |
Output is correct |
6 |
Correct |
109 ms |
36120 KB |
Output is correct |
7 |
Correct |
110 ms |
37032 KB |
Output is correct |
8 |
Correct |
110 ms |
37040 KB |
Output is correct |
9 |
Correct |
108 ms |
37296 KB |
Output is correct |
10 |
Correct |
116 ms |
37300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15956 KB |
Output is correct |
2 |
Correct |
6 ms |
15956 KB |
Output is correct |
3 |
Correct |
6 ms |
15984 KB |
Output is correct |
4 |
Correct |
7 ms |
15956 KB |
Output is correct |
5 |
Correct |
9 ms |
15956 KB |
Output is correct |
6 |
Correct |
7 ms |
15956 KB |
Output is correct |
7 |
Correct |
10 ms |
16004 KB |
Output is correct |
8 |
Correct |
7 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15924 KB |
Output is correct |
10 |
Correct |
7 ms |
15956 KB |
Output is correct |
11 |
Correct |
7 ms |
15984 KB |
Output is correct |
12 |
Correct |
8 ms |
15956 KB |
Output is correct |
13 |
Correct |
8 ms |
15984 KB |
Output is correct |
14 |
Correct |
7 ms |
16008 KB |
Output is correct |
15 |
Correct |
7 ms |
15996 KB |
Output is correct |
16 |
Correct |
8 ms |
15964 KB |
Output is correct |
17 |
Correct |
7 ms |
15960 KB |
Output is correct |
18 |
Correct |
9 ms |
15956 KB |
Output is correct |
19 |
Correct |
8 ms |
16072 KB |
Output is correct |
20 |
Correct |
8 ms |
15996 KB |
Output is correct |
21 |
Correct |
8 ms |
16084 KB |
Output is correct |
22 |
Correct |
7 ms |
15900 KB |
Output is correct |
23 |
Correct |
9 ms |
15996 KB |
Output is correct |
24 |
Correct |
9 ms |
15956 KB |
Output is correct |
25 |
Correct |
7 ms |
15980 KB |
Output is correct |
26 |
Correct |
7 ms |
15992 KB |
Output is correct |
27 |
Correct |
7 ms |
15956 KB |
Output is correct |
28 |
Correct |
8 ms |
16084 KB |
Output is correct |
29 |
Correct |
7 ms |
15992 KB |
Output is correct |
30 |
Correct |
10 ms |
15980 KB |
Output is correct |
31 |
Correct |
7 ms |
15956 KB |
Output is correct |
32 |
Correct |
9 ms |
15992 KB |
Output is correct |
33 |
Correct |
7 ms |
15984 KB |
Output is correct |
34 |
Correct |
9 ms |
15984 KB |
Output is correct |
35 |
Correct |
8 ms |
15956 KB |
Output is correct |
36 |
Correct |
9 ms |
15960 KB |
Output is correct |
37 |
Correct |
8 ms |
16024 KB |
Output is correct |
38 |
Correct |
8 ms |
15988 KB |
Output is correct |
39 |
Correct |
8 ms |
15956 KB |
Output is correct |
40 |
Correct |
10 ms |
15956 KB |
Output is correct |
41 |
Correct |
10 ms |
16028 KB |
Output is correct |
42 |
Correct |
8 ms |
15940 KB |
Output is correct |
43 |
Correct |
7 ms |
15988 KB |
Output is correct |
44 |
Correct |
7 ms |
15968 KB |
Output is correct |
45 |
Correct |
8 ms |
15956 KB |
Output is correct |
46 |
Correct |
7 ms |
15956 KB |
Output is correct |
47 |
Correct |
7 ms |
15956 KB |
Output is correct |
48 |
Correct |
8 ms |
15956 KB |
Output is correct |
49 |
Correct |
8 ms |
15956 KB |
Output is correct |
50 |
Correct |
7 ms |
15956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15956 KB |
Output is correct |
2 |
Correct |
6 ms |
15956 KB |
Output is correct |
3 |
Correct |
6 ms |
15984 KB |
Output is correct |
4 |
Correct |
7 ms |
15956 KB |
Output is correct |
5 |
Correct |
9 ms |
15956 KB |
Output is correct |
6 |
Correct |
7 ms |
15956 KB |
Output is correct |
7 |
Correct |
10 ms |
16004 KB |
Output is correct |
8 |
Correct |
7 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15924 KB |
Output is correct |
10 |
Correct |
7 ms |
15956 KB |
Output is correct |
11 |
Correct |
7 ms |
15984 KB |
Output is correct |
12 |
Correct |
8 ms |
15956 KB |
Output is correct |
13 |
Correct |
8 ms |
15984 KB |
Output is correct |
14 |
Correct |
7 ms |
16008 KB |
Output is correct |
15 |
Correct |
7 ms |
15996 KB |
Output is correct |
16 |
Correct |
8 ms |
15964 KB |
Output is correct |
17 |
Correct |
7 ms |
15960 KB |
Output is correct |
18 |
Correct |
9 ms |
15956 KB |
Output is correct |
19 |
Correct |
8 ms |
16072 KB |
Output is correct |
20 |
Correct |
8 ms |
15996 KB |
Output is correct |
21 |
Correct |
8 ms |
16084 KB |
Output is correct |
22 |
Correct |
7 ms |
15900 KB |
Output is correct |
23 |
Correct |
9 ms |
15996 KB |
Output is correct |
24 |
Correct |
9 ms |
15956 KB |
Output is correct |
25 |
Correct |
7 ms |
15980 KB |
Output is correct |
26 |
Correct |
7 ms |
15992 KB |
Output is correct |
27 |
Correct |
7 ms |
15956 KB |
Output is correct |
28 |
Correct |
8 ms |
16084 KB |
Output is correct |
29 |
Correct |
7 ms |
15992 KB |
Output is correct |
30 |
Correct |
87 ms |
32640 KB |
Output is correct |
31 |
Correct |
104 ms |
33596 KB |
Output is correct |
32 |
Correct |
79 ms |
32072 KB |
Output is correct |
33 |
Correct |
101 ms |
33612 KB |
Output is correct |
34 |
Correct |
98 ms |
33320 KB |
Output is correct |
35 |
Correct |
95 ms |
33364 KB |
Output is correct |
36 |
Correct |
90 ms |
33036 KB |
Output is correct |
37 |
Correct |
108 ms |
34092 KB |
Output is correct |
38 |
Correct |
91 ms |
33476 KB |
Output is correct |
39 |
Correct |
95 ms |
33876 KB |
Output is correct |
40 |
Correct |
119 ms |
34216 KB |
Output is correct |
41 |
Correct |
92 ms |
32828 KB |
Output is correct |
42 |
Correct |
95 ms |
33592 KB |
Output is correct |
43 |
Correct |
94 ms |
33740 KB |
Output is correct |
44 |
Correct |
106 ms |
34024 KB |
Output is correct |
45 |
Correct |
119 ms |
36156 KB |
Output is correct |
46 |
Correct |
115 ms |
35880 KB |
Output is correct |
47 |
Correct |
111 ms |
36512 KB |
Output is correct |
48 |
Correct |
110 ms |
36084 KB |
Output is correct |
49 |
Correct |
107 ms |
36476 KB |
Output is correct |
50 |
Correct |
109 ms |
36120 KB |
Output is correct |
51 |
Correct |
110 ms |
37032 KB |
Output is correct |
52 |
Correct |
110 ms |
37040 KB |
Output is correct |
53 |
Correct |
108 ms |
37296 KB |
Output is correct |
54 |
Correct |
116 ms |
37300 KB |
Output is correct |
55 |
Correct |
10 ms |
15980 KB |
Output is correct |
56 |
Correct |
7 ms |
15956 KB |
Output is correct |
57 |
Correct |
9 ms |
15992 KB |
Output is correct |
58 |
Correct |
7 ms |
15984 KB |
Output is correct |
59 |
Correct |
9 ms |
15984 KB |
Output is correct |
60 |
Correct |
8 ms |
15956 KB |
Output is correct |
61 |
Correct |
9 ms |
15960 KB |
Output is correct |
62 |
Correct |
8 ms |
16024 KB |
Output is correct |
63 |
Correct |
8 ms |
15988 KB |
Output is correct |
64 |
Correct |
8 ms |
15956 KB |
Output is correct |
65 |
Correct |
10 ms |
15956 KB |
Output is correct |
66 |
Correct |
10 ms |
16028 KB |
Output is correct |
67 |
Correct |
8 ms |
15940 KB |
Output is correct |
68 |
Correct |
7 ms |
15988 KB |
Output is correct |
69 |
Correct |
7 ms |
15968 KB |
Output is correct |
70 |
Correct |
8 ms |
15956 KB |
Output is correct |
71 |
Correct |
7 ms |
15956 KB |
Output is correct |
72 |
Correct |
7 ms |
15956 KB |
Output is correct |
73 |
Correct |
8 ms |
15956 KB |
Output is correct |
74 |
Correct |
8 ms |
15956 KB |
Output is correct |
75 |
Correct |
7 ms |
15956 KB |
Output is correct |
76 |
Correct |
98 ms |
33684 KB |
Output is correct |
77 |
Correct |
102 ms |
33760 KB |
Output is correct |
78 |
Correct |
87 ms |
33100 KB |
Output is correct |
79 |
Correct |
91 ms |
33356 KB |
Output is correct |
80 |
Correct |
100 ms |
33952 KB |
Output is correct |
81 |
Correct |
92 ms |
33144 KB |
Output is correct |
82 |
Correct |
95 ms |
33188 KB |
Output is correct |
83 |
Correct |
104 ms |
33600 KB |
Output is correct |
84 |
Correct |
96 ms |
33484 KB |
Output is correct |
85 |
Correct |
91 ms |
33776 KB |
Output is correct |
86 |
Correct |
88 ms |
33112 KB |
Output is correct |
87 |
Correct |
103 ms |
32980 KB |
Output is correct |
88 |
Correct |
91 ms |
33412 KB |
Output is correct |
89 |
Correct |
90 ms |
33268 KB |
Output is correct |
90 |
Correct |
87 ms |
33100 KB |
Output is correct |
91 |
Correct |
92 ms |
32912 KB |
Output is correct |
92 |
Correct |
104 ms |
34212 KB |
Output is correct |
93 |
Correct |
103 ms |
33496 KB |
Output is correct |
94 |
Correct |
112 ms |
32520 KB |
Output is correct |
95 |
Correct |
99 ms |
34048 KB |
Output is correct |
96 |
Correct |
86 ms |
32960 KB |
Output is correct |
97 |
Correct |
104 ms |
33260 KB |
Output is correct |
98 |
Correct |
94 ms |
33468 KB |
Output is correct |
99 |
Correct |
90 ms |
33056 KB |
Output is correct |
100 |
Correct |
93 ms |
33204 KB |
Output is correct |