#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr<<"hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusive
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
using ll=long long;
using ld=long double;
#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)
using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>;
long long LLINF = 1e18;
int INF = 1e9+1e6;
#define MAXN (200006)
ll n, mx, P[MAXN], A[MAXN], C[MAXN], ans, point[MAXN];
bitset<MAXN> vis, inc;
vector<int> stk, cy;
vector<int> v[MAXN];
void cycle(int x) {
vis[x]=1;
stk.eb(x);
if(!vis[P[x]]) cycle(P[x]);
else {
while(1) {
if(stk.empty()) {
cy.clear();
break;
}
ll y=stk.back(); stk.pop_back();
cy.eb(y);
if(y==P[x]) break;
}
}
}
map<ll,ll> *mp[MAXN];
void dfs(int x) {
mp[x] = new map<ll,ll>;
for(auto i:v[x]) {
dfs(i);
if(mp[x]->size() < mp[i]->size()) mp[x] = mp[i];
}
for(auto i:v[x]) if(mp[x] != mp[i]) {
for(auto j:*mp[i]) (*mp[x])[j.f] += j.s;
}
(*mp[x])[mx] += C[x];
(*mp[x])[A[x]] -= C[x];
(*mp[x])[A[x]-1] += C[x];
auto it = mp[x]->find(A[x]);
while(it->s < 0) {
auto tmp = next(it);
assert(tmp != mp[x]->end());
tmp->s += it->s;
mp[x]->erase(it);
it = tmp;
}
if(it->s == 0) mp[x]->erase(it);
}
int main() {
FAST
cin>>n;
FOR(i,1,n) cin>>P[i]>>A[i]>>C[i];
mx = *max_element(A+1, A+n+1) + 1;
FOR(i,1,n) A[i] = mx - A[i];
FOR(i,1,n) point[i]=i;
FOR(i,1,n) if(!vis[i]) {
stk.clear(), cy.clear();
cycle(i);
if(cy.size() <= 1) continue;
sort(all(cy), [](int x,int y){return A[x]>A[y];});
FOR(i,0,siz(cy)-2) {
P[cy[i]] = cy[i+1];
}
P[cy.back()]=cy.back();
for(auto i:cy) point[i]=cy[0], inc[i]=1;
}
FOR(i,1,n) if(!inc[i]) P[i] = point[P[i]];
FOR(i,1,n) if(P[i] ^ i) v[P[i]].eb(i);
FOR(i,1,n) if(P[i] == i) {
dfs(i);
ans += (*mp[i])[mx];
}
cout<<ans<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
9 ms |
6596 KB |
Output is correct |
6 |
Correct |
8 ms |
6212 KB |
Output is correct |
7 |
Correct |
7 ms |
5964 KB |
Output is correct |
8 |
Correct |
9 ms |
6604 KB |
Output is correct |
9 |
Correct |
8 ms |
6092 KB |
Output is correct |
10 |
Correct |
7 ms |
5964 KB |
Output is correct |
11 |
Correct |
7 ms |
5708 KB |
Output is correct |
12 |
Correct |
8 ms |
6348 KB |
Output is correct |
13 |
Correct |
7 ms |
6348 KB |
Output is correct |
14 |
Correct |
9 ms |
6092 KB |
Output is correct |
15 |
Correct |
8 ms |
5996 KB |
Output is correct |
16 |
Correct |
10 ms |
6988 KB |
Output is correct |
17 |
Correct |
8 ms |
6412 KB |
Output is correct |
18 |
Correct |
7 ms |
5788 KB |
Output is correct |
19 |
Correct |
8 ms |
6220 KB |
Output is correct |
20 |
Correct |
7 ms |
6044 KB |
Output is correct |
21 |
Correct |
6 ms |
6092 KB |
Output is correct |
22 |
Correct |
7 ms |
6092 KB |
Output is correct |
23 |
Correct |
6 ms |
5836 KB |
Output is correct |
24 |
Correct |
8 ms |
6316 KB |
Output is correct |
25 |
Correct |
8 ms |
6108 KB |
Output is correct |
26 |
Correct |
7 ms |
6348 KB |
Output is correct |
27 |
Correct |
10 ms |
6220 KB |
Output is correct |
28 |
Correct |
9 ms |
6476 KB |
Output is correct |
29 |
Correct |
7 ms |
6476 KB |
Output is correct |
30 |
Correct |
8 ms |
6476 KB |
Output is correct |
31 |
Correct |
8 ms |
6476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
9 ms |
6596 KB |
Output is correct |
6 |
Correct |
8 ms |
6212 KB |
Output is correct |
7 |
Correct |
7 ms |
5964 KB |
Output is correct |
8 |
Correct |
9 ms |
6604 KB |
Output is correct |
9 |
Correct |
8 ms |
6092 KB |
Output is correct |
10 |
Correct |
7 ms |
5964 KB |
Output is correct |
11 |
Correct |
7 ms |
5708 KB |
Output is correct |
12 |
Correct |
8 ms |
6348 KB |
Output is correct |
13 |
Correct |
7 ms |
6348 KB |
Output is correct |
14 |
Correct |
9 ms |
6092 KB |
Output is correct |
15 |
Correct |
8 ms |
5996 KB |
Output is correct |
16 |
Correct |
10 ms |
6988 KB |
Output is correct |
17 |
Correct |
8 ms |
6412 KB |
Output is correct |
18 |
Correct |
7 ms |
5788 KB |
Output is correct |
19 |
Correct |
8 ms |
6220 KB |
Output is correct |
20 |
Correct |
7 ms |
6044 KB |
Output is correct |
21 |
Correct |
6 ms |
6092 KB |
Output is correct |
22 |
Correct |
7 ms |
6092 KB |
Output is correct |
23 |
Correct |
6 ms |
5836 KB |
Output is correct |
24 |
Correct |
8 ms |
6316 KB |
Output is correct |
25 |
Correct |
8 ms |
6108 KB |
Output is correct |
26 |
Correct |
7 ms |
6348 KB |
Output is correct |
27 |
Correct |
10 ms |
6220 KB |
Output is correct |
28 |
Correct |
9 ms |
6476 KB |
Output is correct |
29 |
Correct |
7 ms |
6476 KB |
Output is correct |
30 |
Correct |
8 ms |
6476 KB |
Output is correct |
31 |
Correct |
8 ms |
6476 KB |
Output is correct |
32 |
Correct |
9 ms |
6604 KB |
Output is correct |
33 |
Correct |
358 ms |
84216 KB |
Output is correct |
34 |
Correct |
275 ms |
59696 KB |
Output is correct |
35 |
Correct |
369 ms |
81204 KB |
Output is correct |
36 |
Correct |
268 ms |
59588 KB |
Output is correct |
37 |
Correct |
205 ms |
40936 KB |
Output is correct |
38 |
Correct |
241 ms |
34852 KB |
Output is correct |
39 |
Correct |
202 ms |
59868 KB |
Output is correct |
40 |
Correct |
194 ms |
59840 KB |
Output is correct |
41 |
Correct |
133 ms |
59700 KB |
Output is correct |
42 |
Correct |
197 ms |
45824 KB |
Output is correct |
43 |
Correct |
185 ms |
45768 KB |
Output is correct |
44 |
Correct |
331 ms |
110600 KB |
Output is correct |
45 |
Correct |
265 ms |
75044 KB |
Output is correct |
46 |
Correct |
137 ms |
34728 KB |
Output is correct |
47 |
Correct |
367 ms |
55756 KB |
Output is correct |
48 |
Correct |
179 ms |
48964 KB |
Output is correct |
49 |
Correct |
137 ms |
48812 KB |
Output is correct |
50 |
Correct |
224 ms |
51196 KB |
Output is correct |
51 |
Correct |
136 ms |
38692 KB |
Output is correct |
52 |
Correct |
276 ms |
56420 KB |
Output is correct |
53 |
Correct |
157 ms |
49212 KB |
Output is correct |
54 |
Correct |
147 ms |
59716 KB |
Output is correct |
55 |
Correct |
246 ms |
58224 KB |
Output is correct |
56 |
Correct |
211 ms |
64112 KB |
Output is correct |
57 |
Correct |
207 ms |
67120 KB |
Output is correct |
58 |
Correct |
232 ms |
64452 KB |
Output is correct |
59 |
Correct |
228 ms |
64428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
9 ms |
6596 KB |
Output is correct |
6 |
Correct |
8 ms |
6212 KB |
Output is correct |
7 |
Correct |
7 ms |
5964 KB |
Output is correct |
8 |
Correct |
9 ms |
6604 KB |
Output is correct |
9 |
Correct |
8 ms |
6092 KB |
Output is correct |
10 |
Correct |
7 ms |
5964 KB |
Output is correct |
11 |
Correct |
7 ms |
5708 KB |
Output is correct |
12 |
Correct |
8 ms |
6348 KB |
Output is correct |
13 |
Correct |
7 ms |
6348 KB |
Output is correct |
14 |
Correct |
9 ms |
6092 KB |
Output is correct |
15 |
Correct |
8 ms |
5996 KB |
Output is correct |
16 |
Correct |
10 ms |
6988 KB |
Output is correct |
17 |
Correct |
8 ms |
6412 KB |
Output is correct |
18 |
Correct |
7 ms |
5788 KB |
Output is correct |
19 |
Correct |
8 ms |
6220 KB |
Output is correct |
20 |
Correct |
7 ms |
6044 KB |
Output is correct |
21 |
Correct |
6 ms |
6092 KB |
Output is correct |
22 |
Correct |
7 ms |
6092 KB |
Output is correct |
23 |
Correct |
6 ms |
5836 KB |
Output is correct |
24 |
Correct |
8 ms |
6316 KB |
Output is correct |
25 |
Correct |
8 ms |
6108 KB |
Output is correct |
26 |
Correct |
7 ms |
6348 KB |
Output is correct |
27 |
Correct |
10 ms |
6220 KB |
Output is correct |
28 |
Correct |
9 ms |
6476 KB |
Output is correct |
29 |
Correct |
7 ms |
6476 KB |
Output is correct |
30 |
Correct |
8 ms |
6476 KB |
Output is correct |
31 |
Correct |
8 ms |
6476 KB |
Output is correct |
32 |
Correct |
9 ms |
6604 KB |
Output is correct |
33 |
Correct |
358 ms |
84216 KB |
Output is correct |
34 |
Correct |
275 ms |
59696 KB |
Output is correct |
35 |
Correct |
369 ms |
81204 KB |
Output is correct |
36 |
Correct |
268 ms |
59588 KB |
Output is correct |
37 |
Correct |
205 ms |
40936 KB |
Output is correct |
38 |
Correct |
241 ms |
34852 KB |
Output is correct |
39 |
Correct |
202 ms |
59868 KB |
Output is correct |
40 |
Correct |
194 ms |
59840 KB |
Output is correct |
41 |
Correct |
133 ms |
59700 KB |
Output is correct |
42 |
Correct |
197 ms |
45824 KB |
Output is correct |
43 |
Correct |
185 ms |
45768 KB |
Output is correct |
44 |
Correct |
331 ms |
110600 KB |
Output is correct |
45 |
Correct |
265 ms |
75044 KB |
Output is correct |
46 |
Correct |
137 ms |
34728 KB |
Output is correct |
47 |
Correct |
367 ms |
55756 KB |
Output is correct |
48 |
Correct |
179 ms |
48964 KB |
Output is correct |
49 |
Correct |
137 ms |
48812 KB |
Output is correct |
50 |
Correct |
224 ms |
51196 KB |
Output is correct |
51 |
Correct |
136 ms |
38692 KB |
Output is correct |
52 |
Correct |
276 ms |
56420 KB |
Output is correct |
53 |
Correct |
157 ms |
49212 KB |
Output is correct |
54 |
Correct |
147 ms |
59716 KB |
Output is correct |
55 |
Correct |
246 ms |
58224 KB |
Output is correct |
56 |
Correct |
211 ms |
64112 KB |
Output is correct |
57 |
Correct |
207 ms |
67120 KB |
Output is correct |
58 |
Correct |
232 ms |
64452 KB |
Output is correct |
59 |
Correct |
228 ms |
64428 KB |
Output is correct |
60 |
Correct |
3 ms |
4940 KB |
Output is correct |
61 |
Correct |
3 ms |
4940 KB |
Output is correct |
62 |
Correct |
4 ms |
4940 KB |
Output is correct |
63 |
Correct |
3 ms |
4940 KB |
Output is correct |
64 |
Correct |
351 ms |
68860 KB |
Output is correct |
65 |
Correct |
278 ms |
57156 KB |
Output is correct |
66 |
Correct |
346 ms |
73212 KB |
Output is correct |
67 |
Correct |
275 ms |
57100 KB |
Output is correct |
68 |
Correct |
215 ms |
43784 KB |
Output is correct |
69 |
Correct |
224 ms |
39236 KB |
Output is correct |
70 |
Correct |
266 ms |
57240 KB |
Output is correct |
71 |
Correct |
267 ms |
52060 KB |
Output is correct |
72 |
Correct |
277 ms |
66428 KB |
Output is correct |
73 |
Correct |
258 ms |
66508 KB |
Output is correct |
74 |
Correct |
283 ms |
62752 KB |
Output is correct |
75 |
Correct |
214 ms |
55236 KB |
Output is correct |
76 |
Correct |
199 ms |
55164 KB |
Output is correct |
77 |
Correct |
282 ms |
62900 KB |
Output is correct |
78 |
Correct |
227 ms |
55192 KB |
Output is correct |
79 |
Correct |
337 ms |
77368 KB |
Output is correct |
80 |
Correct |
281 ms |
65676 KB |
Output is correct |
81 |
Correct |
243 ms |
56332 KB |
Output is correct |
82 |
Correct |
319 ms |
66884 KB |
Output is correct |
83 |
Correct |
147 ms |
55492 KB |
Output is correct |
84 |
Correct |
332 ms |
65928 KB |
Output is correct |
85 |
Correct |
347 ms |
66084 KB |
Output is correct |
86 |
Correct |
325 ms |
65972 KB |
Output is correct |
87 |
Correct |
362 ms |
66120 KB |
Output is correct |
88 |
Correct |
357 ms |
66096 KB |
Output is correct |