#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// My bug list :
// integer overflow
// 0based, 1based forgotten
// index out of bound
// n, m, i, j typo
// some cases are missing
const int MAX_N = 200010, MAX_M = 2005;
#define int ll
int n, h[MAX_N], m;
// y, c
vector<pair<int,int>> star[MAX_N];
//ll dp[MAX_M][MAX_M];
map<int,ll> dp[MAX_N];
// merge to b
void merge_dp(int a, int b) {
int H = h[b];
ll alow = 0, blow = 0;
while (dp[a].size() && dp[a].begin()->first <= H) {
chmax(alow, dp[a].begin()->second);
dp[a].erase(dp[a].begin());
}
while (dp[b].size() && dp[b].begin()->first <= H) {
chmax(blow, dp[b].begin()->second);
dp[b].erase(dp[b].begin());
}
for (auto &[k, w] : dp[a]) w += blow;
for (auto &[k, w] : dp[b]) w += alow;
for (auto [k, w] : dp[a])
chmax(dp[b][k], w);
dp[b][0] = alow + blow;
}
int dfs(int l, int r) {
if (l > r) return n + 1;
if (l == r) {
for (auto [y, c] : star[l])
dp[l][y] = c;
return l;
}
int id = l;
for (int i = l;i <= r;++i)
if (h[i] > h[id]) id = i;
int ld = dfs(l, id-1), rd = dfs(id+1, r);
for (auto [y, c] : star[id])
dp[id][y] = c;
merge_dp(ld, id);
merge_dp(rd, id);
DE(l, r);
for (auto [k, w] : dp[id])
DE(k, w);
return id;
}
ll solve() {
int rt = dfs(1, n);
ll sum = 0;
for (int i = 1;i <= n;++i)
for (auto [y, c] : star[i])
sum += c;
ll mxg = 0;
for (auto [_, w] : dp[rt])
chmax(mxg, w);
return sum - mxg;
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1;i <= n;++i)
cin >> h[i];
cin >> m;
for (int x, y, c, i = 0;i < m;++i) {
cin >> x >> y >> c;
star[x].pb(y, c);
}
assert(n < MAX_M);
cout << solve() << '\n';
}
Compilation message
constellation3.cpp: In function 'll dfs(ll, ll)':
constellation3.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
constellation3.cpp:78:2: note: in expansion of macro 'DE'
78 | DE(l, r);
| ^~
constellation3.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
constellation3.cpp:80:3: note: in expansion of macro 'DE'
80 | DE(k, w);
| ^~
constellation3.cpp:79:12: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
79 | for (auto [k, w] : dp[id])
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
11 ms |
14412 KB |
Output is correct |
3 |
Correct |
9 ms |
14424 KB |
Output is correct |
4 |
Correct |
10 ms |
14420 KB |
Output is correct |
5 |
Correct |
9 ms |
14412 KB |
Output is correct |
6 |
Correct |
9 ms |
14416 KB |
Output is correct |
7 |
Correct |
9 ms |
14452 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
9 ms |
14412 KB |
Output is correct |
10 |
Correct |
10 ms |
15084 KB |
Output is correct |
11 |
Correct |
11 ms |
14804 KB |
Output is correct |
12 |
Correct |
10 ms |
14808 KB |
Output is correct |
13 |
Correct |
10 ms |
14660 KB |
Output is correct |
14 |
Correct |
9 ms |
14668 KB |
Output is correct |
15 |
Correct |
11 ms |
14656 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
8 ms |
14412 KB |
Output is correct |
18 |
Correct |
10 ms |
14924 KB |
Output is correct |
19 |
Correct |
9 ms |
14412 KB |
Output is correct |
20 |
Correct |
9 ms |
14412 KB |
Output is correct |
21 |
Correct |
10 ms |
14940 KB |
Output is correct |
22 |
Correct |
9 ms |
14416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
11 ms |
14412 KB |
Output is correct |
3 |
Correct |
9 ms |
14424 KB |
Output is correct |
4 |
Correct |
10 ms |
14420 KB |
Output is correct |
5 |
Correct |
9 ms |
14412 KB |
Output is correct |
6 |
Correct |
9 ms |
14416 KB |
Output is correct |
7 |
Correct |
9 ms |
14452 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
9 ms |
14412 KB |
Output is correct |
10 |
Correct |
10 ms |
15084 KB |
Output is correct |
11 |
Correct |
11 ms |
14804 KB |
Output is correct |
12 |
Correct |
10 ms |
14808 KB |
Output is correct |
13 |
Correct |
10 ms |
14660 KB |
Output is correct |
14 |
Correct |
9 ms |
14668 KB |
Output is correct |
15 |
Correct |
11 ms |
14656 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
8 ms |
14412 KB |
Output is correct |
18 |
Correct |
10 ms |
14924 KB |
Output is correct |
19 |
Correct |
9 ms |
14412 KB |
Output is correct |
20 |
Correct |
9 ms |
14412 KB |
Output is correct |
21 |
Correct |
10 ms |
14940 KB |
Output is correct |
22 |
Correct |
9 ms |
14416 KB |
Output is correct |
23 |
Correct |
10 ms |
14668 KB |
Output is correct |
24 |
Correct |
10 ms |
14684 KB |
Output is correct |
25 |
Correct |
10 ms |
14684 KB |
Output is correct |
26 |
Correct |
10 ms |
14684 KB |
Output is correct |
27 |
Correct |
11 ms |
14668 KB |
Output is correct |
28 |
Correct |
11 ms |
14688 KB |
Output is correct |
29 |
Correct |
10 ms |
14688 KB |
Output is correct |
30 |
Correct |
13 ms |
14668 KB |
Output is correct |
31 |
Correct |
10 ms |
14668 KB |
Output is correct |
32 |
Correct |
90 ms |
40004 KB |
Output is correct |
33 |
Correct |
80 ms |
34500 KB |
Output is correct |
34 |
Correct |
57 ms |
30164 KB |
Output is correct |
35 |
Correct |
53 ms |
28256 KB |
Output is correct |
36 |
Correct |
98 ms |
39392 KB |
Output is correct |
37 |
Correct |
84 ms |
41916 KB |
Output is correct |
38 |
Correct |
18 ms |
14812 KB |
Output is correct |
39 |
Correct |
11 ms |
14924 KB |
Output is correct |
40 |
Correct |
82 ms |
40072 KB |
Output is correct |
41 |
Correct |
11 ms |
14796 KB |
Output is correct |
42 |
Correct |
11 ms |
14940 KB |
Output is correct |
43 |
Correct |
101 ms |
41816 KB |
Output is correct |
44 |
Correct |
11 ms |
14796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
11 ms |
14412 KB |
Output is correct |
3 |
Correct |
9 ms |
14424 KB |
Output is correct |
4 |
Correct |
10 ms |
14420 KB |
Output is correct |
5 |
Correct |
9 ms |
14412 KB |
Output is correct |
6 |
Correct |
9 ms |
14416 KB |
Output is correct |
7 |
Correct |
9 ms |
14452 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
9 ms |
14412 KB |
Output is correct |
10 |
Correct |
10 ms |
15084 KB |
Output is correct |
11 |
Correct |
11 ms |
14804 KB |
Output is correct |
12 |
Correct |
10 ms |
14808 KB |
Output is correct |
13 |
Correct |
10 ms |
14660 KB |
Output is correct |
14 |
Correct |
9 ms |
14668 KB |
Output is correct |
15 |
Correct |
11 ms |
14656 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
8 ms |
14412 KB |
Output is correct |
18 |
Correct |
10 ms |
14924 KB |
Output is correct |
19 |
Correct |
9 ms |
14412 KB |
Output is correct |
20 |
Correct |
9 ms |
14412 KB |
Output is correct |
21 |
Correct |
10 ms |
14940 KB |
Output is correct |
22 |
Correct |
9 ms |
14416 KB |
Output is correct |
23 |
Correct |
10 ms |
14668 KB |
Output is correct |
24 |
Correct |
10 ms |
14684 KB |
Output is correct |
25 |
Correct |
10 ms |
14684 KB |
Output is correct |
26 |
Correct |
10 ms |
14684 KB |
Output is correct |
27 |
Correct |
11 ms |
14668 KB |
Output is correct |
28 |
Correct |
11 ms |
14688 KB |
Output is correct |
29 |
Correct |
10 ms |
14688 KB |
Output is correct |
30 |
Correct |
13 ms |
14668 KB |
Output is correct |
31 |
Correct |
10 ms |
14668 KB |
Output is correct |
32 |
Correct |
90 ms |
40004 KB |
Output is correct |
33 |
Correct |
80 ms |
34500 KB |
Output is correct |
34 |
Correct |
57 ms |
30164 KB |
Output is correct |
35 |
Correct |
53 ms |
28256 KB |
Output is correct |
36 |
Correct |
98 ms |
39392 KB |
Output is correct |
37 |
Correct |
84 ms |
41916 KB |
Output is correct |
38 |
Correct |
18 ms |
14812 KB |
Output is correct |
39 |
Correct |
11 ms |
14924 KB |
Output is correct |
40 |
Correct |
82 ms |
40072 KB |
Output is correct |
41 |
Correct |
11 ms |
14796 KB |
Output is correct |
42 |
Correct |
11 ms |
14940 KB |
Output is correct |
43 |
Correct |
101 ms |
41816 KB |
Output is correct |
44 |
Correct |
11 ms |
14796 KB |
Output is correct |
45 |
Runtime error |
157 ms |
48364 KB |
Execution killed with signal 6 |
46 |
Halted |
0 ms |
0 KB |
- |