#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define double long double
#define pb push_back
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) (x) * (x)
#define fi first
#define se second
mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l,int r){return l+((hdp()%(r-l+1))+r-l+1)%(r-l+1);}
const int N = 2e5 + 5;
const int mod = 998244353;
const int SQ = 450;
const int inf = 1e9;
const int bound = 1e7;
int A[N], H[N], C[N], deg[N];
map<int, int> cnt[N];
void merge(map<int, int>& a, map<int, int>& b)
{
if (a.size() < b.size()) swap(a, b);
for (auto t : b) a[t.fi] += t.se;
}
void fix(map<int, int>& a, int b, int c)
{
a[b] += c; auto it = a.find(b);
while (it != a.begin())
{
it--;
if ((*it).se > c) {(*it).se -= c; break;}
c -= (*it).se; it = a.erase(it);
}
}
signed main()
{
if (fopen("in.txt", "r")) {
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
}
fastIO
int n, sum = 0;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> A[i] >> H[i] >> C[i], sum += C[i], deg[A[i]]++;
queue<int> q;
for (int i = 1; i <= n; i++)
if (!deg[i]) q.push(i);
while (!q.empty())
{
int u = q.front(), crr = C[u]; q.pop();
deg[A[u]]--;
fix(cnt[u], H[u], C[u]);
merge(cnt[A[u]], cnt[u]);
if (!deg[A[u]]) q.push(A[u]);
}
for (int i = 1; i <= n; i++)
{
if (!deg[i]) continue;
deg[i] = 0;
map<int, int> all = cnt[i], cn;
cn[H[i]] += C[i];
for (int crr = A[i]; crr != i; crr = A[crr])
merge(all, cnt[crr]), deg[crr] = 0, cn[H[crr]] += C[crr];
for (auto t : cn) fix(all, t.fi, t.se);
for (auto t : all) sum -= t.se;
}
cout << sum;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:62:28: warning: unused variable 'crr' [-Wunused-variable]
62 | int u = q.front(), crr = C[u]; q.pop();
| ^~~
worst_reporter2.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen("in.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
worst_reporter2.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen("out.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
5 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
8 ms |
11356 KB |
Output is correct |
6 |
Correct |
7 ms |
10588 KB |
Output is correct |
7 |
Correct |
7 ms |
10332 KB |
Output is correct |
8 |
Correct |
10 ms |
11352 KB |
Output is correct |
9 |
Correct |
8 ms |
10840 KB |
Output is correct |
10 |
Correct |
7 ms |
10328 KB |
Output is correct |
11 |
Correct |
6 ms |
10076 KB |
Output is correct |
12 |
Correct |
6 ms |
9820 KB |
Output is correct |
13 |
Correct |
6 ms |
9816 KB |
Output is correct |
14 |
Correct |
6 ms |
10076 KB |
Output is correct |
15 |
Correct |
7 ms |
9816 KB |
Output is correct |
16 |
Correct |
9 ms |
11608 KB |
Output is correct |
17 |
Correct |
7 ms |
10600 KB |
Output is correct |
18 |
Correct |
8 ms |
10076 KB |
Output is correct |
19 |
Correct |
7 ms |
10408 KB |
Output is correct |
20 |
Correct |
6 ms |
10076 KB |
Output is correct |
21 |
Correct |
7 ms |
10076 KB |
Output is correct |
22 |
Correct |
7 ms |
10772 KB |
Output is correct |
23 |
Correct |
6 ms |
10212 KB |
Output is correct |
24 |
Correct |
7 ms |
10332 KB |
Output is correct |
25 |
Correct |
6 ms |
10076 KB |
Output is correct |
26 |
Correct |
5 ms |
9820 KB |
Output is correct |
27 |
Correct |
7 ms |
10556 KB |
Output is correct |
28 |
Correct |
6 ms |
10468 KB |
Output is correct |
29 |
Correct |
6 ms |
10588 KB |
Output is correct |
30 |
Correct |
6 ms |
10588 KB |
Output is correct |
31 |
Correct |
7 ms |
10584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
5 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
8 ms |
11356 KB |
Output is correct |
6 |
Correct |
7 ms |
10588 KB |
Output is correct |
7 |
Correct |
7 ms |
10332 KB |
Output is correct |
8 |
Correct |
10 ms |
11352 KB |
Output is correct |
9 |
Correct |
8 ms |
10840 KB |
Output is correct |
10 |
Correct |
7 ms |
10328 KB |
Output is correct |
11 |
Correct |
6 ms |
10076 KB |
Output is correct |
12 |
Correct |
6 ms |
9820 KB |
Output is correct |
13 |
Correct |
6 ms |
9816 KB |
Output is correct |
14 |
Correct |
6 ms |
10076 KB |
Output is correct |
15 |
Correct |
7 ms |
9816 KB |
Output is correct |
16 |
Correct |
9 ms |
11608 KB |
Output is correct |
17 |
Correct |
7 ms |
10600 KB |
Output is correct |
18 |
Correct |
8 ms |
10076 KB |
Output is correct |
19 |
Correct |
7 ms |
10408 KB |
Output is correct |
20 |
Correct |
6 ms |
10076 KB |
Output is correct |
21 |
Correct |
7 ms |
10076 KB |
Output is correct |
22 |
Correct |
7 ms |
10772 KB |
Output is correct |
23 |
Correct |
6 ms |
10212 KB |
Output is correct |
24 |
Correct |
7 ms |
10332 KB |
Output is correct |
25 |
Correct |
6 ms |
10076 KB |
Output is correct |
26 |
Correct |
5 ms |
9820 KB |
Output is correct |
27 |
Correct |
7 ms |
10556 KB |
Output is correct |
28 |
Correct |
6 ms |
10468 KB |
Output is correct |
29 |
Correct |
6 ms |
10588 KB |
Output is correct |
30 |
Correct |
6 ms |
10588 KB |
Output is correct |
31 |
Correct |
7 ms |
10584 KB |
Output is correct |
32 |
Correct |
9 ms |
11356 KB |
Output is correct |
33 |
Correct |
292 ms |
94552 KB |
Output is correct |
34 |
Correct |
188 ms |
54316 KB |
Output is correct |
35 |
Correct |
284 ms |
94036 KB |
Output is correct |
36 |
Correct |
195 ms |
54096 KB |
Output is correct |
37 |
Correct |
91 ms |
27988 KB |
Output is correct |
38 |
Correct |
72 ms |
22608 KB |
Output is correct |
39 |
Correct |
76 ms |
16068 KB |
Output is correct |
40 |
Correct |
78 ms |
16000 KB |
Output is correct |
41 |
Correct |
50 ms |
16004 KB |
Output is correct |
42 |
Correct |
81 ms |
16224 KB |
Output is correct |
43 |
Correct |
77 ms |
16212 KB |
Output is correct |
44 |
Correct |
261 ms |
103892 KB |
Output is correct |
45 |
Correct |
173 ms |
59220 KB |
Output is correct |
46 |
Correct |
51 ms |
21808 KB |
Output is correct |
47 |
Correct |
142 ms |
35412 KB |
Output is correct |
48 |
Correct |
85 ms |
21584 KB |
Output is correct |
49 |
Correct |
55 ms |
21584 KB |
Output is correct |
50 |
Correct |
192 ms |
52048 KB |
Output is correct |
51 |
Correct |
71 ms |
27220 KB |
Output is correct |
52 |
Correct |
166 ms |
35860 KB |
Output is correct |
53 |
Correct |
68 ms |
21588 KB |
Output is correct |
54 |
Correct |
52 ms |
15952 KB |
Output is correct |
55 |
Correct |
117 ms |
42576 KB |
Output is correct |
56 |
Correct |
95 ms |
38740 KB |
Output is correct |
57 |
Correct |
99 ms |
39088 KB |
Output is correct |
58 |
Correct |
157 ms |
47184 KB |
Output is correct |
59 |
Correct |
148 ms |
47192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
5 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
8 ms |
11356 KB |
Output is correct |
6 |
Correct |
7 ms |
10588 KB |
Output is correct |
7 |
Correct |
7 ms |
10332 KB |
Output is correct |
8 |
Correct |
10 ms |
11352 KB |
Output is correct |
9 |
Correct |
8 ms |
10840 KB |
Output is correct |
10 |
Correct |
7 ms |
10328 KB |
Output is correct |
11 |
Correct |
6 ms |
10076 KB |
Output is correct |
12 |
Correct |
6 ms |
9820 KB |
Output is correct |
13 |
Correct |
6 ms |
9816 KB |
Output is correct |
14 |
Correct |
6 ms |
10076 KB |
Output is correct |
15 |
Correct |
7 ms |
9816 KB |
Output is correct |
16 |
Correct |
9 ms |
11608 KB |
Output is correct |
17 |
Correct |
7 ms |
10600 KB |
Output is correct |
18 |
Correct |
8 ms |
10076 KB |
Output is correct |
19 |
Correct |
7 ms |
10408 KB |
Output is correct |
20 |
Correct |
6 ms |
10076 KB |
Output is correct |
21 |
Correct |
7 ms |
10076 KB |
Output is correct |
22 |
Correct |
7 ms |
10772 KB |
Output is correct |
23 |
Correct |
6 ms |
10212 KB |
Output is correct |
24 |
Correct |
7 ms |
10332 KB |
Output is correct |
25 |
Correct |
6 ms |
10076 KB |
Output is correct |
26 |
Correct |
5 ms |
9820 KB |
Output is correct |
27 |
Correct |
7 ms |
10556 KB |
Output is correct |
28 |
Correct |
6 ms |
10468 KB |
Output is correct |
29 |
Correct |
6 ms |
10588 KB |
Output is correct |
30 |
Correct |
6 ms |
10588 KB |
Output is correct |
31 |
Correct |
7 ms |
10584 KB |
Output is correct |
32 |
Correct |
9 ms |
11356 KB |
Output is correct |
33 |
Correct |
292 ms |
94552 KB |
Output is correct |
34 |
Correct |
188 ms |
54316 KB |
Output is correct |
35 |
Correct |
284 ms |
94036 KB |
Output is correct |
36 |
Correct |
195 ms |
54096 KB |
Output is correct |
37 |
Correct |
91 ms |
27988 KB |
Output is correct |
38 |
Correct |
72 ms |
22608 KB |
Output is correct |
39 |
Correct |
76 ms |
16068 KB |
Output is correct |
40 |
Correct |
78 ms |
16000 KB |
Output is correct |
41 |
Correct |
50 ms |
16004 KB |
Output is correct |
42 |
Correct |
81 ms |
16224 KB |
Output is correct |
43 |
Correct |
77 ms |
16212 KB |
Output is correct |
44 |
Correct |
261 ms |
103892 KB |
Output is correct |
45 |
Correct |
173 ms |
59220 KB |
Output is correct |
46 |
Correct |
51 ms |
21808 KB |
Output is correct |
47 |
Correct |
142 ms |
35412 KB |
Output is correct |
48 |
Correct |
85 ms |
21584 KB |
Output is correct |
49 |
Correct |
55 ms |
21584 KB |
Output is correct |
50 |
Correct |
192 ms |
52048 KB |
Output is correct |
51 |
Correct |
71 ms |
27220 KB |
Output is correct |
52 |
Correct |
166 ms |
35860 KB |
Output is correct |
53 |
Correct |
68 ms |
21588 KB |
Output is correct |
54 |
Correct |
52 ms |
15952 KB |
Output is correct |
55 |
Correct |
117 ms |
42576 KB |
Output is correct |
56 |
Correct |
95 ms |
38740 KB |
Output is correct |
57 |
Correct |
99 ms |
39088 KB |
Output is correct |
58 |
Correct |
157 ms |
47184 KB |
Output is correct |
59 |
Correct |
148 ms |
47192 KB |
Output is correct |
60 |
Correct |
4 ms |
9820 KB |
Output is correct |
61 |
Correct |
5 ms |
9820 KB |
Output is correct |
62 |
Correct |
5 ms |
9820 KB |
Output is correct |
63 |
Correct |
5 ms |
9816 KB |
Output is correct |
64 |
Correct |
220 ms |
55860 KB |
Output is correct |
65 |
Correct |
178 ms |
38992 KB |
Output is correct |
66 |
Correct |
233 ms |
54348 KB |
Output is correct |
67 |
Correct |
169 ms |
38996 KB |
Output is correct |
68 |
Correct |
93 ms |
24404 KB |
Output is correct |
69 |
Correct |
76 ms |
20992 KB |
Output is correct |
70 |
Correct |
156 ms |
24636 KB |
Output is correct |
71 |
Correct |
67 ms |
15952 KB |
Output is correct |
72 |
Correct |
163 ms |
28544 KB |
Output is correct |
73 |
Correct |
66 ms |
16156 KB |
Output is correct |
74 |
Correct |
198 ms |
36432 KB |
Output is correct |
75 |
Correct |
77 ms |
22356 KB |
Output is correct |
76 |
Correct |
67 ms |
22240 KB |
Output is correct |
77 |
Correct |
170 ms |
36116 KB |
Output is correct |
78 |
Correct |
76 ms |
22340 KB |
Output is correct |
79 |
Correct |
210 ms |
55120 KB |
Output is correct |
80 |
Correct |
132 ms |
35084 KB |
Output is correct |
81 |
Correct |
74 ms |
21840 KB |
Output is correct |
82 |
Correct |
106 ms |
28496 KB |
Output is correct |
83 |
Correct |
61 ms |
15996 KB |
Output is correct |
84 |
Correct |
148 ms |
31664 KB |
Output is correct |
85 |
Correct |
137 ms |
31572 KB |
Output is correct |
86 |
Correct |
122 ms |
28496 KB |
Output is correct |
87 |
Correct |
143 ms |
31572 KB |
Output is correct |
88 |
Correct |
170 ms |
31708 KB |
Output is correct |