#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
#define int ll
#define FOR(i,s,e) for(ll i = s; i <= (ll)e; ++i)
#define DEC(i,s,e) for(ll i = s; i >= (ll)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{" << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define reach cerr << "LINE: " << __LINE__ << "\n";
typedef pair <ll, ll> pi;
typedef tuple<ll,ll,ll> ti3;
string cts(char x) {string t(1,x); return t;}
ll rand(ll a, ll b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (ll)1e18 + 500;
template <typename T> void chmax(T& a, T b) { a = max(a, b); }
template <typename T> void chmin(T& a, T b) { a = min(a, b); }
const int MAXN = 1000005;
#ifndef LOCAL
#define cerr if(0)cout
#endif
struct ConvexHull {
deque<pi> dq;
int f(pi line, int x) {
return line.first*x + line.second;
}
int query(int x) {
while(dq.size()>1) {
if(f(dq[0],x)>f(dq[1],x)) {
dq.pop_front();
} else {
break;
}
}
return f(dq[0],x);
}
double intersect(int m1, int c1, int m2, int c2) {
return (double) (c2-c1)/(m1-m2);
}
double intersect(pi a, pi b) {
return intersect(a.first,a.second,b.first,b.second);
}
void insert(pi line) {
while(dq.size() > 1) {
int s = dq.size();
if(intersect(dq[s-1],line) <= intersect(dq[s-2],line)) {
dq.pop_back();
} else {
break;
}
}
dq.push_back(line);
}
} CH;
//dp(i) = dp(j) + A[i] * (N-i)
//dp(i) = dp(j) + nA[i] - iA[i]
int A[MAXN];
int32_t main()
{
IAMSPEED
int n; cin >> n;
FOR(i,1,n)cin>>A[i];
int mx=-1;
CH.insert({0,0});
int dp=0;
FOR(i,1,n) {
if(A[i]>=mx) {
mx=A[i];
dp=CH.query(A[i])+n*A[i];
CH.insert({-i,dp});
} else {
CH.insert({-i,dp});
}
dbvp(CH.dq);
}
cout << dp;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
121 ms |
13176 KB |
Output is correct |
17 |
Correct |
144 ms |
14060 KB |
Output is correct |
18 |
Correct |
119 ms |
12536 KB |
Output is correct |
19 |
Correct |
144 ms |
13944 KB |
Output is correct |
20 |
Correct |
145 ms |
13816 KB |
Output is correct |
21 |
Correct |
129 ms |
13944 KB |
Output is correct |
22 |
Correct |
129 ms |
13560 KB |
Output is correct |
23 |
Correct |
140 ms |
14584 KB |
Output is correct |
24 |
Correct |
133 ms |
14044 KB |
Output is correct |
25 |
Correct |
143 ms |
14456 KB |
Output is correct |
26 |
Correct |
143 ms |
14768 KB |
Output is correct |
27 |
Correct |
121 ms |
13304 KB |
Output is correct |
28 |
Correct |
138 ms |
14072 KB |
Output is correct |
29 |
Correct |
148 ms |
14200 KB |
Output is correct |
30 |
Correct |
163 ms |
14480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
150 ms |
17788 KB |
Output is correct |
2 |
Correct |
150 ms |
17912 KB |
Output is correct |
3 |
Correct |
141 ms |
17784 KB |
Output is correct |
4 |
Correct |
165 ms |
17784 KB |
Output is correct |
5 |
Correct |
156 ms |
17784 KB |
Output is correct |
6 |
Correct |
141 ms |
17784 KB |
Output is correct |
7 |
Correct |
156 ms |
17784 KB |
Output is correct |
8 |
Correct |
167 ms |
17912 KB |
Output is correct |
9 |
Correct |
159 ms |
17784 KB |
Output is correct |
10 |
Correct |
152 ms |
17784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
0 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
0 ms |
384 KB |
Output is correct |
38 |
Correct |
0 ms |
384 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
1 ms |
384 KB |
Output is correct |
44 |
Correct |
0 ms |
384 KB |
Output is correct |
45 |
Correct |
0 ms |
384 KB |
Output is correct |
46 |
Correct |
0 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
1 ms |
384 KB |
Output is correct |
50 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
121 ms |
13176 KB |
Output is correct |
31 |
Correct |
144 ms |
14060 KB |
Output is correct |
32 |
Correct |
119 ms |
12536 KB |
Output is correct |
33 |
Correct |
144 ms |
13944 KB |
Output is correct |
34 |
Correct |
145 ms |
13816 KB |
Output is correct |
35 |
Correct |
129 ms |
13944 KB |
Output is correct |
36 |
Correct |
129 ms |
13560 KB |
Output is correct |
37 |
Correct |
140 ms |
14584 KB |
Output is correct |
38 |
Correct |
133 ms |
14044 KB |
Output is correct |
39 |
Correct |
143 ms |
14456 KB |
Output is correct |
40 |
Correct |
143 ms |
14768 KB |
Output is correct |
41 |
Correct |
121 ms |
13304 KB |
Output is correct |
42 |
Correct |
138 ms |
14072 KB |
Output is correct |
43 |
Correct |
148 ms |
14200 KB |
Output is correct |
44 |
Correct |
163 ms |
14480 KB |
Output is correct |
45 |
Correct |
150 ms |
17788 KB |
Output is correct |
46 |
Correct |
150 ms |
17912 KB |
Output is correct |
47 |
Correct |
141 ms |
17784 KB |
Output is correct |
48 |
Correct |
165 ms |
17784 KB |
Output is correct |
49 |
Correct |
156 ms |
17784 KB |
Output is correct |
50 |
Correct |
141 ms |
17784 KB |
Output is correct |
51 |
Correct |
156 ms |
17784 KB |
Output is correct |
52 |
Correct |
167 ms |
17912 KB |
Output is correct |
53 |
Correct |
159 ms |
17784 KB |
Output is correct |
54 |
Correct |
152 ms |
17784 KB |
Output is correct |
55 |
Correct |
1 ms |
384 KB |
Output is correct |
56 |
Correct |
0 ms |
384 KB |
Output is correct |
57 |
Correct |
1 ms |
384 KB |
Output is correct |
58 |
Correct |
1 ms |
384 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
384 KB |
Output is correct |
61 |
Correct |
1 ms |
384 KB |
Output is correct |
62 |
Correct |
0 ms |
384 KB |
Output is correct |
63 |
Correct |
0 ms |
384 KB |
Output is correct |
64 |
Correct |
1 ms |
384 KB |
Output is correct |
65 |
Correct |
1 ms |
384 KB |
Output is correct |
66 |
Correct |
1 ms |
384 KB |
Output is correct |
67 |
Correct |
1 ms |
384 KB |
Output is correct |
68 |
Correct |
1 ms |
384 KB |
Output is correct |
69 |
Correct |
0 ms |
384 KB |
Output is correct |
70 |
Correct |
0 ms |
384 KB |
Output is correct |
71 |
Correct |
0 ms |
384 KB |
Output is correct |
72 |
Correct |
1 ms |
384 KB |
Output is correct |
73 |
Correct |
1 ms |
384 KB |
Output is correct |
74 |
Correct |
1 ms |
384 KB |
Output is correct |
75 |
Correct |
0 ms |
384 KB |
Output is correct |
76 |
Correct |
120 ms |
14200 KB |
Output is correct |
77 |
Correct |
117 ms |
14200 KB |
Output is correct |
78 |
Correct |
112 ms |
13560 KB |
Output is correct |
79 |
Correct |
116 ms |
14072 KB |
Output is correct |
80 |
Correct |
119 ms |
14712 KB |
Output is correct |
81 |
Correct |
112 ms |
13688 KB |
Output is correct |
82 |
Correct |
109 ms |
13688 KB |
Output is correct |
83 |
Correct |
113 ms |
14072 KB |
Output is correct |
84 |
Correct |
115 ms |
13944 KB |
Output is correct |
85 |
Correct |
123 ms |
14204 KB |
Output is correct |
86 |
Correct |
109 ms |
13688 KB |
Output is correct |
87 |
Correct |
111 ms |
13560 KB |
Output is correct |
88 |
Correct |
116 ms |
13944 KB |
Output is correct |
89 |
Correct |
108 ms |
13688 KB |
Output is correct |
90 |
Correct |
111 ms |
13688 KB |
Output is correct |
91 |
Correct |
113 ms |
13432 KB |
Output is correct |
92 |
Correct |
150 ms |
14712 KB |
Output is correct |
93 |
Correct |
138 ms |
13944 KB |
Output is correct |
94 |
Correct |
103 ms |
13048 KB |
Output is correct |
95 |
Correct |
119 ms |
14584 KB |
Output is correct |
96 |
Correct |
125 ms |
13536 KB |
Output is correct |
97 |
Correct |
116 ms |
13816 KB |
Output is correct |
98 |
Correct |
123 ms |
13944 KB |
Output is correct |
99 |
Correct |
116 ms |
13564 KB |
Output is correct |
100 |
Correct |
109 ms |
13688 KB |
Output is correct |