#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 2e5+10, MOD = 998244353;
int n, m, a[N], bro[N];
int par[N];
vector<int> child[N];
vector<pair<int, ll>> can[N];
ll dp[N];
void build_tree() {
memset(par, -1, sizeof(par));
vector<int> st;
for (int i = 0; i < n; i++) {
int last = -1;
while (sz(st) && a[st.back()] < a[i]) {
last = st.back();
st.pop_back();
}
if (sz(st)) par[i] = st.back();
if (last != -1) par[last] = i;
st.push_back(i);
}
}
ll f(int bot, int top) {
if (bot == top) return 0;
return dp[bro[bot]] + f(par[bot], top);
}
void dfs(int c) {
for (int nxt : child[c]) {
dfs(nxt);
}
dp[c] = 0;
for (int nxt : child[c]) dp[c] += dp[nxt];
for (auto [v, x] : can[c]) {
ll cur = x + f(v, c);
for (int nxt : child[v]) cur += dp[nxt];
dp[c] = max(dp[c], cur);
}
}
void solve() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
build_tree();
int root = -1;
for (int i = 0; i < n; i++) {
if (par[i] == -1) {
root = i;
}
}
for (int i = 0; i < n; i++) if (i != root) {
child[par[i]].push_back(i);
}
memset(bro, -1, sizeof(bro));
for (int i = 0; i < n; i++) if (i != root) {
for (int nxt : child[par[i]]) if (nxt != i) {
bro[i] = nxt;
}
}
cin >> m;
ll base = 0;
for (int i = 0; i < m; i++) {
int x, y; ll c; cin >> x >> y >> c, --x; base += c;
int top = x;
while (par[top] != -1 && a[par[top]] < y) top = par[top];
can[top].push_back({x, c});
}
dfs(root);
cout << base - dp[root] << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11268 KB |
Output is correct |
2 |
Correct |
6 ms |
11220 KB |
Output is correct |
3 |
Correct |
5 ms |
11220 KB |
Output is correct |
4 |
Correct |
7 ms |
11220 KB |
Output is correct |
5 |
Correct |
5 ms |
11276 KB |
Output is correct |
6 |
Correct |
5 ms |
11220 KB |
Output is correct |
7 |
Correct |
6 ms |
11220 KB |
Output is correct |
8 |
Correct |
5 ms |
11220 KB |
Output is correct |
9 |
Correct |
5 ms |
11268 KB |
Output is correct |
10 |
Correct |
5 ms |
11220 KB |
Output is correct |
11 |
Correct |
5 ms |
11272 KB |
Output is correct |
12 |
Correct |
6 ms |
11220 KB |
Output is correct |
13 |
Correct |
5 ms |
11220 KB |
Output is correct |
14 |
Correct |
5 ms |
11220 KB |
Output is correct |
15 |
Correct |
5 ms |
11220 KB |
Output is correct |
16 |
Correct |
7 ms |
11220 KB |
Output is correct |
17 |
Correct |
5 ms |
11220 KB |
Output is correct |
18 |
Correct |
6 ms |
11272 KB |
Output is correct |
19 |
Correct |
6 ms |
11220 KB |
Output is correct |
20 |
Correct |
5 ms |
11220 KB |
Output is correct |
21 |
Correct |
5 ms |
11220 KB |
Output is correct |
22 |
Correct |
5 ms |
11272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11268 KB |
Output is correct |
2 |
Correct |
6 ms |
11220 KB |
Output is correct |
3 |
Correct |
5 ms |
11220 KB |
Output is correct |
4 |
Correct |
7 ms |
11220 KB |
Output is correct |
5 |
Correct |
5 ms |
11276 KB |
Output is correct |
6 |
Correct |
5 ms |
11220 KB |
Output is correct |
7 |
Correct |
6 ms |
11220 KB |
Output is correct |
8 |
Correct |
5 ms |
11220 KB |
Output is correct |
9 |
Correct |
5 ms |
11268 KB |
Output is correct |
10 |
Correct |
5 ms |
11220 KB |
Output is correct |
11 |
Correct |
5 ms |
11272 KB |
Output is correct |
12 |
Correct |
6 ms |
11220 KB |
Output is correct |
13 |
Correct |
5 ms |
11220 KB |
Output is correct |
14 |
Correct |
5 ms |
11220 KB |
Output is correct |
15 |
Correct |
5 ms |
11220 KB |
Output is correct |
16 |
Correct |
7 ms |
11220 KB |
Output is correct |
17 |
Correct |
5 ms |
11220 KB |
Output is correct |
18 |
Correct |
6 ms |
11272 KB |
Output is correct |
19 |
Correct |
6 ms |
11220 KB |
Output is correct |
20 |
Correct |
5 ms |
11220 KB |
Output is correct |
21 |
Correct |
5 ms |
11220 KB |
Output is correct |
22 |
Correct |
5 ms |
11272 KB |
Output is correct |
23 |
Correct |
8 ms |
11392 KB |
Output is correct |
24 |
Correct |
6 ms |
11348 KB |
Output is correct |
25 |
Correct |
6 ms |
11448 KB |
Output is correct |
26 |
Correct |
7 ms |
11408 KB |
Output is correct |
27 |
Correct |
6 ms |
11348 KB |
Output is correct |
28 |
Correct |
6 ms |
11348 KB |
Output is correct |
29 |
Correct |
6 ms |
11348 KB |
Output is correct |
30 |
Correct |
6 ms |
11364 KB |
Output is correct |
31 |
Correct |
6 ms |
11408 KB |
Output is correct |
32 |
Correct |
10 ms |
11536 KB |
Output is correct |
33 |
Correct |
9 ms |
11408 KB |
Output is correct |
34 |
Correct |
8 ms |
11408 KB |
Output is correct |
35 |
Correct |
8 ms |
11448 KB |
Output is correct |
36 |
Correct |
8 ms |
11476 KB |
Output is correct |
37 |
Correct |
9 ms |
11556 KB |
Output is correct |
38 |
Correct |
6 ms |
11604 KB |
Output is correct |
39 |
Correct |
6 ms |
11348 KB |
Output is correct |
40 |
Correct |
10 ms |
11576 KB |
Output is correct |
41 |
Correct |
6 ms |
11348 KB |
Output is correct |
42 |
Correct |
6 ms |
11348 KB |
Output is correct |
43 |
Correct |
9 ms |
11476 KB |
Output is correct |
44 |
Correct |
7 ms |
11448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11268 KB |
Output is correct |
2 |
Correct |
6 ms |
11220 KB |
Output is correct |
3 |
Correct |
5 ms |
11220 KB |
Output is correct |
4 |
Correct |
7 ms |
11220 KB |
Output is correct |
5 |
Correct |
5 ms |
11276 KB |
Output is correct |
6 |
Correct |
5 ms |
11220 KB |
Output is correct |
7 |
Correct |
6 ms |
11220 KB |
Output is correct |
8 |
Correct |
5 ms |
11220 KB |
Output is correct |
9 |
Correct |
5 ms |
11268 KB |
Output is correct |
10 |
Correct |
5 ms |
11220 KB |
Output is correct |
11 |
Correct |
5 ms |
11272 KB |
Output is correct |
12 |
Correct |
6 ms |
11220 KB |
Output is correct |
13 |
Correct |
5 ms |
11220 KB |
Output is correct |
14 |
Correct |
5 ms |
11220 KB |
Output is correct |
15 |
Correct |
5 ms |
11220 KB |
Output is correct |
16 |
Correct |
7 ms |
11220 KB |
Output is correct |
17 |
Correct |
5 ms |
11220 KB |
Output is correct |
18 |
Correct |
6 ms |
11272 KB |
Output is correct |
19 |
Correct |
6 ms |
11220 KB |
Output is correct |
20 |
Correct |
5 ms |
11220 KB |
Output is correct |
21 |
Correct |
5 ms |
11220 KB |
Output is correct |
22 |
Correct |
5 ms |
11272 KB |
Output is correct |
23 |
Correct |
8 ms |
11392 KB |
Output is correct |
24 |
Correct |
6 ms |
11348 KB |
Output is correct |
25 |
Correct |
6 ms |
11448 KB |
Output is correct |
26 |
Correct |
7 ms |
11408 KB |
Output is correct |
27 |
Correct |
6 ms |
11348 KB |
Output is correct |
28 |
Correct |
6 ms |
11348 KB |
Output is correct |
29 |
Correct |
6 ms |
11348 KB |
Output is correct |
30 |
Correct |
6 ms |
11364 KB |
Output is correct |
31 |
Correct |
6 ms |
11408 KB |
Output is correct |
32 |
Correct |
10 ms |
11536 KB |
Output is correct |
33 |
Correct |
9 ms |
11408 KB |
Output is correct |
34 |
Correct |
8 ms |
11408 KB |
Output is correct |
35 |
Correct |
8 ms |
11448 KB |
Output is correct |
36 |
Correct |
8 ms |
11476 KB |
Output is correct |
37 |
Correct |
9 ms |
11556 KB |
Output is correct |
38 |
Correct |
6 ms |
11604 KB |
Output is correct |
39 |
Correct |
6 ms |
11348 KB |
Output is correct |
40 |
Correct |
10 ms |
11576 KB |
Output is correct |
41 |
Correct |
6 ms |
11348 KB |
Output is correct |
42 |
Correct |
6 ms |
11348 KB |
Output is correct |
43 |
Correct |
9 ms |
11476 KB |
Output is correct |
44 |
Correct |
7 ms |
11448 KB |
Output is correct |
45 |
Correct |
109 ms |
28268 KB |
Output is correct |
46 |
Correct |
121 ms |
28104 KB |
Output is correct |
47 |
Correct |
115 ms |
28352 KB |
Output is correct |
48 |
Correct |
110 ms |
28200 KB |
Output is correct |
49 |
Correct |
107 ms |
27996 KB |
Output is correct |
50 |
Correct |
122 ms |
27868 KB |
Output is correct |
51 |
Correct |
107 ms |
27900 KB |
Output is correct |
52 |
Correct |
132 ms |
28348 KB |
Output is correct |
53 |
Correct |
107 ms |
28104 KB |
Output is correct |
54 |
Execution timed out |
1586 ms |
18696 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |