#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll nx=1e6+5, inf=1e18;
ll n, t[nx], dp[nx];
struct line
{
ll m, c, p;
line(ll m, ll c, ll p): m(m), c(c), p(p){}
};
struct convexhull
{
deque<line> dq;
ll div(ll a, ll b)
{
if ((a%b)==0||(a^b)>=0) return a/b;
return a/b-1;
}
void add(ll m, ll c)
{
while (dq.size()>1)
{
line tmp=dq.back();
dq.pop_back();
if (div(c-tmp.c, tmp.m-m)>dq.back().p)
{
dq.push_back(tmp);
break;
}
}
if (!dq.empty()) dq.back().p=div(c-dq.back().c, dq.back().m-m);
dq.push_back(line(m, c, inf));
}
ll query(ll x)
{
while (dq.front().p<x) dq.pop_front();
return dq.front().m*x+dq.front().c;
}
void show()
{
for (auto tmp:dq) printf("line %d %d %lld\n", tmp.m, tmp.c, tmp.p);
}
} c;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>t[i], t[i]=max(t[i], t[i-1]);
c.add(0, 0);
//c.show();
for (int i=1; i<=n; i++) dp[i]=c.query(t[i])+t[i]*n, c.add(-i, dp[i]); //c.show();
cout<<dp[n];
}
Compilation message
Discharging.cpp: In member function 'void convexhull::show()':
Discharging.cpp:46:41: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
46 | for (auto tmp:dq) printf("line %d %d %lld\n", tmp.m, tmp.c, tmp.p);
| ~^ ~~~~~
| | |
| int long long int
| %lld
Discharging.cpp:46:44: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long long int' [-Wformat=]
46 | for (auto tmp:dq) printf("line %d %d %lld\n", tmp.m, tmp.c, tmp.p);
| ~^ ~~~~~
| | |
| int long long int
| %lld
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2516 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2508 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2400 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2400 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
94 ms |
16072 KB |
Output is correct |
17 |
Correct |
101 ms |
16088 KB |
Output is correct |
18 |
Correct |
93 ms |
16068 KB |
Output is correct |
19 |
Correct |
97 ms |
16072 KB |
Output is correct |
20 |
Correct |
99 ms |
16084 KB |
Output is correct |
21 |
Correct |
106 ms |
15948 KB |
Output is correct |
22 |
Correct |
107 ms |
16080 KB |
Output is correct |
23 |
Correct |
106 ms |
15952 KB |
Output is correct |
24 |
Correct |
98 ms |
15952 KB |
Output is correct |
25 |
Correct |
103 ms |
16076 KB |
Output is correct |
26 |
Correct |
106 ms |
16068 KB |
Output is correct |
27 |
Correct |
94 ms |
16076 KB |
Output is correct |
28 |
Correct |
101 ms |
16072 KB |
Output is correct |
29 |
Correct |
105 ms |
15868 KB |
Output is correct |
30 |
Correct |
111 ms |
16332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
16072 KB |
Output is correct |
2 |
Correct |
113 ms |
16076 KB |
Output is correct |
3 |
Correct |
102 ms |
15888 KB |
Output is correct |
4 |
Correct |
112 ms |
16076 KB |
Output is correct |
5 |
Correct |
104 ms |
16080 KB |
Output is correct |
6 |
Correct |
115 ms |
16212 KB |
Output is correct |
7 |
Correct |
109 ms |
16076 KB |
Output is correct |
8 |
Correct |
118 ms |
16072 KB |
Output is correct |
9 |
Correct |
111 ms |
15952 KB |
Output is correct |
10 |
Correct |
109 ms |
16076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2516 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2508 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2552 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2400 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2508 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
1 ms |
2396 KB |
Output is correct |
43 |
Correct |
1 ms |
2548 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
2 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2516 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
2 ms |
2552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2516 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2508 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2552 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2400 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
94 ms |
16072 KB |
Output is correct |
31 |
Correct |
101 ms |
16088 KB |
Output is correct |
32 |
Correct |
93 ms |
16068 KB |
Output is correct |
33 |
Correct |
97 ms |
16072 KB |
Output is correct |
34 |
Correct |
99 ms |
16084 KB |
Output is correct |
35 |
Correct |
106 ms |
15948 KB |
Output is correct |
36 |
Correct |
107 ms |
16080 KB |
Output is correct |
37 |
Correct |
106 ms |
15952 KB |
Output is correct |
38 |
Correct |
98 ms |
15952 KB |
Output is correct |
39 |
Correct |
103 ms |
16076 KB |
Output is correct |
40 |
Correct |
106 ms |
16068 KB |
Output is correct |
41 |
Correct |
94 ms |
16076 KB |
Output is correct |
42 |
Correct |
101 ms |
16072 KB |
Output is correct |
43 |
Correct |
105 ms |
15868 KB |
Output is correct |
44 |
Correct |
111 ms |
16332 KB |
Output is correct |
45 |
Correct |
107 ms |
16072 KB |
Output is correct |
46 |
Correct |
113 ms |
16076 KB |
Output is correct |
47 |
Correct |
102 ms |
15888 KB |
Output is correct |
48 |
Correct |
112 ms |
16076 KB |
Output is correct |
49 |
Correct |
104 ms |
16080 KB |
Output is correct |
50 |
Correct |
115 ms |
16212 KB |
Output is correct |
51 |
Correct |
109 ms |
16076 KB |
Output is correct |
52 |
Correct |
118 ms |
16072 KB |
Output is correct |
53 |
Correct |
111 ms |
15952 KB |
Output is correct |
54 |
Correct |
109 ms |
16076 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
1 ms |
2508 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
1 ms |
2396 KB |
Output is correct |
61 |
Correct |
1 ms |
2396 KB |
Output is correct |
62 |
Correct |
1 ms |
2396 KB |
Output is correct |
63 |
Correct |
1 ms |
2396 KB |
Output is correct |
64 |
Correct |
1 ms |
2396 KB |
Output is correct |
65 |
Correct |
1 ms |
2396 KB |
Output is correct |
66 |
Correct |
1 ms |
2396 KB |
Output is correct |
67 |
Correct |
1 ms |
2396 KB |
Output is correct |
68 |
Correct |
1 ms |
2548 KB |
Output is correct |
69 |
Correct |
1 ms |
2396 KB |
Output is correct |
70 |
Correct |
2 ms |
2396 KB |
Output is correct |
71 |
Correct |
1 ms |
2396 KB |
Output is correct |
72 |
Correct |
1 ms |
2396 KB |
Output is correct |
73 |
Correct |
1 ms |
2516 KB |
Output is correct |
74 |
Correct |
1 ms |
2396 KB |
Output is correct |
75 |
Correct |
2 ms |
2552 KB |
Output is correct |
76 |
Correct |
92 ms |
21960 KB |
Output is correct |
77 |
Correct |
99 ms |
21976 KB |
Output is correct |
78 |
Correct |
91 ms |
21332 KB |
Output is correct |
79 |
Correct |
107 ms |
21592 KB |
Output is correct |
80 |
Correct |
102 ms |
22424 KB |
Output is correct |
81 |
Correct |
95 ms |
21752 KB |
Output is correct |
82 |
Correct |
90 ms |
21472 KB |
Output is correct |
83 |
Correct |
110 ms |
22100 KB |
Output is correct |
84 |
Correct |
98 ms |
21704 KB |
Output is correct |
85 |
Correct |
102 ms |
22168 KB |
Output is correct |
86 |
Correct |
103 ms |
21492 KB |
Output is correct |
87 |
Correct |
113 ms |
21440 KB |
Output is correct |
88 |
Correct |
96 ms |
21652 KB |
Output is correct |
89 |
Correct |
94 ms |
21476 KB |
Output is correct |
90 |
Correct |
92 ms |
21560 KB |
Output is correct |
91 |
Correct |
98 ms |
21328 KB |
Output is correct |
92 |
Correct |
109 ms |
22612 KB |
Output is correct |
93 |
Correct |
103 ms |
21888 KB |
Output is correct |
94 |
Correct |
99 ms |
21340 KB |
Output is correct |
95 |
Correct |
97 ms |
22468 KB |
Output is correct |
96 |
Correct |
96 ms |
21188 KB |
Output is correct |
97 |
Correct |
98 ms |
21832 KB |
Output is correct |
98 |
Correct |
92 ms |
21844 KB |
Output is correct |
99 |
Correct |
110 ms |
21340 KB |
Output is correct |
100 |
Correct |
95 ms |
21568 KB |
Output is correct |