#include <bits/stdc++.h>
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define all(v) (v).begin(), (v).end()
#define F first
#define S second
#define __builtin_popcount __builtin_popcountll
#define sz(v) ((int) (v).size())
#define uni(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define pii(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
///
const int mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll oo = 1e18 + 7;
const int N = 1e6 + 5;
int n, a[N], nxt[N];
ll dp[N];
struct line
{
ll a, b;
line (const ll &_a, const ll &_b)
{
a = _a;
b = _b;
}
ll eval(const ll &x)
{
return a * x + b;
}
};
deque <line> st;
bool bad(const line &l1, const line &l2, const line &l3)
{
return (l3.b - l1.b) * (l1.a - l2.a) < (l2.b - l1.b) * (l1.a - l3.a);
}
void add(const line &x)
{
while (st.size() >= 2 && bad(st[st.size() - 2], st[st.size() - 1], x)) st.pop_back();
st.push_back(x);
}
ll query(ll x)
{
while (st.size() >= 2 && st[0].eval(x) >= st[1].eval(x)) st.pop_front();
return st[0].eval(x);
}
void Input()
{
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
nxt[1] = 1;
for (int i = 2; i <= n; i ++)
if (a[nxt[i - 1]] < a[i]) nxt[i] = i;
else nxt[i] = nxt[i - 1];
}
void solve()
{
add(line(0, 0));
for (int i = 1; i <= n; i ++)
{
dp[i] = query(a[nxt[i]]) + 1LL * n * a[nxt[i]];
add(line(-i, dp[i]));
}
cout << dp[n];
}
int main()
{
std::ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("DIAMOND.inp", "r", stdin);
// freopen("DIAMOND.out", "w", stdout);
int test = 1;
//cin >> test;
while (test--)
{
Input();
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
276 KB |
Output is correct |
16 |
Correct |
88 ms |
15948 KB |
Output is correct |
17 |
Correct |
95 ms |
15864 KB |
Output is correct |
18 |
Correct |
83 ms |
15892 KB |
Output is correct |
19 |
Correct |
104 ms |
15848 KB |
Output is correct |
20 |
Correct |
96 ms |
15912 KB |
Output is correct |
21 |
Correct |
95 ms |
15908 KB |
Output is correct |
22 |
Correct |
98 ms |
15928 KB |
Output is correct |
23 |
Correct |
98 ms |
15944 KB |
Output is correct |
24 |
Correct |
92 ms |
15936 KB |
Output is correct |
25 |
Correct |
97 ms |
15944 KB |
Output is correct |
26 |
Correct |
103 ms |
15828 KB |
Output is correct |
27 |
Correct |
98 ms |
15852 KB |
Output is correct |
28 |
Correct |
92 ms |
15948 KB |
Output is correct |
29 |
Correct |
94 ms |
15872 KB |
Output is correct |
30 |
Correct |
103 ms |
15940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
32100 KB |
Output is correct |
2 |
Correct |
113 ms |
32148 KB |
Output is correct |
3 |
Correct |
115 ms |
32148 KB |
Output is correct |
4 |
Correct |
119 ms |
32112 KB |
Output is correct |
5 |
Correct |
118 ms |
32084 KB |
Output is correct |
6 |
Correct |
114 ms |
32108 KB |
Output is correct |
7 |
Correct |
114 ms |
32192 KB |
Output is correct |
8 |
Correct |
113 ms |
32116 KB |
Output is correct |
9 |
Correct |
121 ms |
32196 KB |
Output is correct |
10 |
Correct |
115 ms |
32108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
276 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 |
1 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 |
0 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 |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
276 KB |
Output is correct |
30 |
Correct |
88 ms |
15948 KB |
Output is correct |
31 |
Correct |
95 ms |
15864 KB |
Output is correct |
32 |
Correct |
83 ms |
15892 KB |
Output is correct |
33 |
Correct |
104 ms |
15848 KB |
Output is correct |
34 |
Correct |
96 ms |
15912 KB |
Output is correct |
35 |
Correct |
95 ms |
15908 KB |
Output is correct |
36 |
Correct |
98 ms |
15928 KB |
Output is correct |
37 |
Correct |
98 ms |
15944 KB |
Output is correct |
38 |
Correct |
92 ms |
15936 KB |
Output is correct |
39 |
Correct |
97 ms |
15944 KB |
Output is correct |
40 |
Correct |
103 ms |
15828 KB |
Output is correct |
41 |
Correct |
98 ms |
15852 KB |
Output is correct |
42 |
Correct |
92 ms |
15948 KB |
Output is correct |
43 |
Correct |
94 ms |
15872 KB |
Output is correct |
44 |
Correct |
103 ms |
15940 KB |
Output is correct |
45 |
Correct |
118 ms |
32100 KB |
Output is correct |
46 |
Correct |
113 ms |
32148 KB |
Output is correct |
47 |
Correct |
115 ms |
32148 KB |
Output is correct |
48 |
Correct |
119 ms |
32112 KB |
Output is correct |
49 |
Correct |
118 ms |
32084 KB |
Output is correct |
50 |
Correct |
114 ms |
32108 KB |
Output is correct |
51 |
Correct |
114 ms |
32192 KB |
Output is correct |
52 |
Correct |
113 ms |
32116 KB |
Output is correct |
53 |
Correct |
121 ms |
32196 KB |
Output is correct |
54 |
Correct |
115 ms |
32108 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 |
1 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 |
0 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 |
1 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 |
93 ms |
15980 KB |
Output is correct |
77 |
Incorrect |
92 ms |
15908 KB |
Output isn't correct |
78 |
Halted |
0 ms |
0 KB |
- |