#include <bits/stdc++.h>
#define se second
#define fs first
#define mp make_pair
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 2e5 + 7;
const int Mod = 1e9 + 7;
const int szBL = 320;
const int INF = 1e9;
const int BASE = 137;
struct Data {
int A, H, C;
};
int n;
Data a[N];
int szC[N];
vector<int> ke[N];
map<int,ll> dp[N];///dp[u]: decreasing function
int dd[N];
void dfs (int u) {
dd[u] = 1;
szC[u] = 1;
int mxV = -1;
// cout << u <<"\n";
iter (&v, ke[u]) {
dfs (v);
szC[u] += szC[v];
if (mxV == -1 || szC[v] > szC[mxV]) {
mxV = v;
}
}
if (mxV != -1) swap(dp[u], dp[mxV]); dp[u][1] += 0;
iter (&v, ke[u]) {
if (v == mxV) continue;
iter (&id, dp[v]) {
if (id.fs != 1) {
dp[u][id.fs] += id.se;
dp[u][1] -= id.se;
// cout << "upd: "<<u<<" "<<v<<" "<<id.fs<<" "<<id.se <<"\n";
}
}
map<int,ll> ().swap(dp[v]);
}
dp[u][a[u].H + 1] -= a[u].C;
ll val = a[u].C;
auto it = dp[u].lower_bound(a[u].H + 1);
// cout << a[u].H<<"\n";
// cout << u<<" "<<dp[u][1]<<" aa\n";
while (1) {
--it;
// cout << it->fs<<" "<<it->se<<"\n";
if (it != dp[u].begin() && val >= -it->se) {
val += it->se;
it = dp[u].erase(it);
}
else {
it->se += val;
val = 0;
break;
}
}
// dp[u][1] += val;
// cout << u <<" "<<dp[u][1] <<" "<<val<<"\n";
}
vector<int> zip;
void compress () {
rep (i, 1, n) zip.push_back(a[i].H);
sort (ALL(zip));
zip.resize(unique(ALL(zip)) - zip.begin());
rep (i, 1, n) {
a[i].H = lower_bound(ALL(zip), a[i].H) - zip.begin() + 1;
}
}
void solution () {
cin >> n;
ll tot = 0;
rep (i, 1, n) {
cin >> a[i].A >> a[i].H >> a[i].C;
tot += a[i].C;
if (a[i].A != i)
ke[a[i].A].push_back(i);
}
compress();
rep (i, 1, n) {
if (a[i].A != i) continue;
dfs(i);
cout << tot - dp[i][1] <<"\n";
}
}
#define file(name) freopen(name".inp", "r", stdin); \
freopen(name".out", "w", stdout);
int main () {
// file("c");
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
solution();
}
/*
no bug challenge
*/
Compilation message
worst_reporter2.cpp: In function 'void dfs(int)':
worst_reporter2.cpp:49:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
49 | if (mxV != -1) swap(dp[u], dp[mxV]); dp[u][1] += 0;
| ^~
worst_reporter2.cpp:49:42: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
49 | if (mxV != -1) swap(dp[u], dp[mxV]); dp[u][1] += 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14428 KB |
Output is correct |
2 |
Correct |
6 ms |
14424 KB |
Output is correct |
3 |
Correct |
6 ms |
14428 KB |
Output is correct |
4 |
Correct |
7 ms |
14428 KB |
Output is correct |
5 |
Correct |
11 ms |
15016 KB |
Output is correct |
6 |
Correct |
11 ms |
14684 KB |
Output is correct |
7 |
Correct |
10 ms |
14684 KB |
Output is correct |
8 |
Correct |
12 ms |
14936 KB |
Output is correct |
9 |
Correct |
10 ms |
14684 KB |
Output is correct |
10 |
Correct |
10 ms |
14780 KB |
Output is correct |
11 |
Correct |
10 ms |
14788 KB |
Output is correct |
12 |
Correct |
11 ms |
15708 KB |
Output is correct |
13 |
Correct |
10 ms |
15760 KB |
Output is correct |
14 |
Correct |
11 ms |
15392 KB |
Output is correct |
15 |
Correct |
10 ms |
15196 KB |
Output is correct |
16 |
Correct |
12 ms |
15076 KB |
Output is correct |
17 |
Correct |
11 ms |
14680 KB |
Output is correct |
18 |
Correct |
9 ms |
14684 KB |
Output is correct |
19 |
Correct |
11 ms |
15456 KB |
Output is correct |
20 |
Correct |
10 ms |
15196 KB |
Output is correct |
21 |
Correct |
10 ms |
15452 KB |
Output is correct |
22 |
Correct |
10 ms |
15432 KB |
Output is correct |
23 |
Correct |
10 ms |
15312 KB |
Output is correct |
24 |
Correct |
11 ms |
15452 KB |
Output is correct |
25 |
Correct |
11 ms |
15452 KB |
Output is correct |
26 |
Correct |
10 ms |
15748 KB |
Output is correct |
27 |
Correct |
11 ms |
15452 KB |
Output is correct |
28 |
Correct |
10 ms |
15664 KB |
Output is correct |
29 |
Correct |
13 ms |
15704 KB |
Output is correct |
30 |
Correct |
10 ms |
15708 KB |
Output is correct |
31 |
Correct |
11 ms |
15704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14428 KB |
Output is correct |
2 |
Correct |
6 ms |
14424 KB |
Output is correct |
3 |
Correct |
6 ms |
14428 KB |
Output is correct |
4 |
Correct |
7 ms |
14428 KB |
Output is correct |
5 |
Correct |
11 ms |
15016 KB |
Output is correct |
6 |
Correct |
11 ms |
14684 KB |
Output is correct |
7 |
Correct |
10 ms |
14684 KB |
Output is correct |
8 |
Correct |
12 ms |
14936 KB |
Output is correct |
9 |
Correct |
10 ms |
14684 KB |
Output is correct |
10 |
Correct |
10 ms |
14780 KB |
Output is correct |
11 |
Correct |
10 ms |
14788 KB |
Output is correct |
12 |
Correct |
11 ms |
15708 KB |
Output is correct |
13 |
Correct |
10 ms |
15760 KB |
Output is correct |
14 |
Correct |
11 ms |
15392 KB |
Output is correct |
15 |
Correct |
10 ms |
15196 KB |
Output is correct |
16 |
Correct |
12 ms |
15076 KB |
Output is correct |
17 |
Correct |
11 ms |
14680 KB |
Output is correct |
18 |
Correct |
9 ms |
14684 KB |
Output is correct |
19 |
Correct |
11 ms |
15456 KB |
Output is correct |
20 |
Correct |
10 ms |
15196 KB |
Output is correct |
21 |
Correct |
10 ms |
15452 KB |
Output is correct |
22 |
Correct |
10 ms |
15432 KB |
Output is correct |
23 |
Correct |
10 ms |
15312 KB |
Output is correct |
24 |
Correct |
11 ms |
15452 KB |
Output is correct |
25 |
Correct |
11 ms |
15452 KB |
Output is correct |
26 |
Correct |
10 ms |
15748 KB |
Output is correct |
27 |
Correct |
11 ms |
15452 KB |
Output is correct |
28 |
Correct |
10 ms |
15664 KB |
Output is correct |
29 |
Correct |
13 ms |
15704 KB |
Output is correct |
30 |
Correct |
10 ms |
15708 KB |
Output is correct |
31 |
Correct |
11 ms |
15704 KB |
Output is correct |
32 |
Correct |
12 ms |
14940 KB |
Output is correct |
33 |
Correct |
324 ms |
39996 KB |
Output is correct |
34 |
Correct |
261 ms |
28032 KB |
Output is correct |
35 |
Correct |
355 ms |
38620 KB |
Output is correct |
36 |
Correct |
260 ms |
27852 KB |
Output is correct |
37 |
Correct |
193 ms |
27592 KB |
Output is correct |
38 |
Correct |
185 ms |
27200 KB |
Output is correct |
39 |
Correct |
237 ms |
68296 KB |
Output is correct |
40 |
Correct |
206 ms |
68256 KB |
Output is correct |
41 |
Correct |
198 ms |
68288 KB |
Output is correct |
42 |
Correct |
224 ms |
49548 KB |
Output is correct |
43 |
Correct |
208 ms |
49576 KB |
Output is correct |
44 |
Correct |
309 ms |
39624 KB |
Output is correct |
45 |
Correct |
240 ms |
27616 KB |
Output is correct |
46 |
Correct |
156 ms |
27600 KB |
Output is correct |
47 |
Correct |
265 ms |
52936 KB |
Output is correct |
48 |
Correct |
187 ms |
46288 KB |
Output is correct |
49 |
Correct |
169 ms |
46248 KB |
Output is correct |
50 |
Correct |
267 ms |
49080 KB |
Output is correct |
51 |
Correct |
175 ms |
49312 KB |
Output is correct |
52 |
Correct |
267 ms |
58960 KB |
Output is correct |
53 |
Correct |
251 ms |
58964 KB |
Output is correct |
54 |
Correct |
184 ms |
68272 KB |
Output is correct |
55 |
Correct |
232 ms |
54728 KB |
Output is correct |
56 |
Correct |
219 ms |
65872 KB |
Output is correct |
57 |
Correct |
215 ms |
71108 KB |
Output is correct |
58 |
Correct |
230 ms |
67268 KB |
Output is correct |
59 |
Correct |
229 ms |
67428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14428 KB |
Output is correct |
2 |
Correct |
6 ms |
14424 KB |
Output is correct |
3 |
Correct |
6 ms |
14428 KB |
Output is correct |
4 |
Correct |
7 ms |
14428 KB |
Output is correct |
5 |
Correct |
11 ms |
15016 KB |
Output is correct |
6 |
Correct |
11 ms |
14684 KB |
Output is correct |
7 |
Correct |
10 ms |
14684 KB |
Output is correct |
8 |
Correct |
12 ms |
14936 KB |
Output is correct |
9 |
Correct |
10 ms |
14684 KB |
Output is correct |
10 |
Correct |
10 ms |
14780 KB |
Output is correct |
11 |
Correct |
10 ms |
14788 KB |
Output is correct |
12 |
Correct |
11 ms |
15708 KB |
Output is correct |
13 |
Correct |
10 ms |
15760 KB |
Output is correct |
14 |
Correct |
11 ms |
15392 KB |
Output is correct |
15 |
Correct |
10 ms |
15196 KB |
Output is correct |
16 |
Correct |
12 ms |
15076 KB |
Output is correct |
17 |
Correct |
11 ms |
14680 KB |
Output is correct |
18 |
Correct |
9 ms |
14684 KB |
Output is correct |
19 |
Correct |
11 ms |
15456 KB |
Output is correct |
20 |
Correct |
10 ms |
15196 KB |
Output is correct |
21 |
Correct |
10 ms |
15452 KB |
Output is correct |
22 |
Correct |
10 ms |
15432 KB |
Output is correct |
23 |
Correct |
10 ms |
15312 KB |
Output is correct |
24 |
Correct |
11 ms |
15452 KB |
Output is correct |
25 |
Correct |
11 ms |
15452 KB |
Output is correct |
26 |
Correct |
10 ms |
15748 KB |
Output is correct |
27 |
Correct |
11 ms |
15452 KB |
Output is correct |
28 |
Correct |
10 ms |
15664 KB |
Output is correct |
29 |
Correct |
13 ms |
15704 KB |
Output is correct |
30 |
Correct |
10 ms |
15708 KB |
Output is correct |
31 |
Correct |
11 ms |
15704 KB |
Output is correct |
32 |
Correct |
12 ms |
14940 KB |
Output is correct |
33 |
Correct |
324 ms |
39996 KB |
Output is correct |
34 |
Correct |
261 ms |
28032 KB |
Output is correct |
35 |
Correct |
355 ms |
38620 KB |
Output is correct |
36 |
Correct |
260 ms |
27852 KB |
Output is correct |
37 |
Correct |
193 ms |
27592 KB |
Output is correct |
38 |
Correct |
185 ms |
27200 KB |
Output is correct |
39 |
Correct |
237 ms |
68296 KB |
Output is correct |
40 |
Correct |
206 ms |
68256 KB |
Output is correct |
41 |
Correct |
198 ms |
68288 KB |
Output is correct |
42 |
Correct |
224 ms |
49548 KB |
Output is correct |
43 |
Correct |
208 ms |
49576 KB |
Output is correct |
44 |
Correct |
309 ms |
39624 KB |
Output is correct |
45 |
Correct |
240 ms |
27616 KB |
Output is correct |
46 |
Correct |
156 ms |
27600 KB |
Output is correct |
47 |
Correct |
265 ms |
52936 KB |
Output is correct |
48 |
Correct |
187 ms |
46288 KB |
Output is correct |
49 |
Correct |
169 ms |
46248 KB |
Output is correct |
50 |
Correct |
267 ms |
49080 KB |
Output is correct |
51 |
Correct |
175 ms |
49312 KB |
Output is correct |
52 |
Correct |
267 ms |
58960 KB |
Output is correct |
53 |
Correct |
251 ms |
58964 KB |
Output is correct |
54 |
Correct |
184 ms |
68272 KB |
Output is correct |
55 |
Correct |
232 ms |
54728 KB |
Output is correct |
56 |
Correct |
219 ms |
65872 KB |
Output is correct |
57 |
Correct |
215 ms |
71108 KB |
Output is correct |
58 |
Correct |
230 ms |
67268 KB |
Output is correct |
59 |
Correct |
229 ms |
67428 KB |
Output is correct |
60 |
Incorrect |
5 ms |
14424 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |