#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 |
6 ms |
9816 KB |
Output is correct |
2 |
Correct |
5 ms |
9728 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
5 ms |
9820 KB |
Output is correct |
5 |
Correct |
8 ms |
11352 KB |
Output is correct |
6 |
Correct |
7 ms |
10588 KB |
Output is correct |
7 |
Correct |
6 ms |
10332 KB |
Output is correct |
8 |
Correct |
9 ms |
11292 KB |
Output is correct |
9 |
Correct |
7 ms |
10588 KB |
Output is correct |
10 |
Correct |
6 ms |
10332 KB |
Output is correct |
11 |
Correct |
5 ms |
10076 KB |
Output is correct |
12 |
Correct |
6 ms |
9816 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
6 ms |
10076 KB |
Output is correct |
15 |
Correct |
6 ms |
9804 KB |
Output is correct |
16 |
Correct |
9 ms |
11608 KB |
Output is correct |
17 |
Correct |
7 ms |
10588 KB |
Output is correct |
18 |
Correct |
5 ms |
10076 KB |
Output is correct |
19 |
Correct |
7 ms |
10332 KB |
Output is correct |
20 |
Correct |
6 ms |
10176 KB |
Output is correct |
21 |
Correct |
6 ms |
10076 KB |
Output is correct |
22 |
Correct |
6 ms |
10844 KB |
Output is correct |
23 |
Correct |
6 ms |
10164 KB |
Output is correct |
24 |
Correct |
7 ms |
10328 KB |
Output is correct |
25 |
Correct |
6 ms |
10072 KB |
Output is correct |
26 |
Correct |
6 ms |
9816 KB |
Output is correct |
27 |
Correct |
7 ms |
10588 KB |
Output is correct |
28 |
Correct |
9 ms |
10588 KB |
Output is correct |
29 |
Correct |
7 ms |
10584 KB |
Output is correct |
30 |
Correct |
7 ms |
10588 KB |
Output is correct |
31 |
Correct |
6 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9816 KB |
Output is correct |
2 |
Correct |
5 ms |
9728 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
5 ms |
9820 KB |
Output is correct |
5 |
Correct |
8 ms |
11352 KB |
Output is correct |
6 |
Correct |
7 ms |
10588 KB |
Output is correct |
7 |
Correct |
6 ms |
10332 KB |
Output is correct |
8 |
Correct |
9 ms |
11292 KB |
Output is correct |
9 |
Correct |
7 ms |
10588 KB |
Output is correct |
10 |
Correct |
6 ms |
10332 KB |
Output is correct |
11 |
Correct |
5 ms |
10076 KB |
Output is correct |
12 |
Correct |
6 ms |
9816 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
6 ms |
10076 KB |
Output is correct |
15 |
Correct |
6 ms |
9804 KB |
Output is correct |
16 |
Correct |
9 ms |
11608 KB |
Output is correct |
17 |
Correct |
7 ms |
10588 KB |
Output is correct |
18 |
Correct |
5 ms |
10076 KB |
Output is correct |
19 |
Correct |
7 ms |
10332 KB |
Output is correct |
20 |
Correct |
6 ms |
10176 KB |
Output is correct |
21 |
Correct |
6 ms |
10076 KB |
Output is correct |
22 |
Correct |
6 ms |
10844 KB |
Output is correct |
23 |
Correct |
6 ms |
10164 KB |
Output is correct |
24 |
Correct |
7 ms |
10328 KB |
Output is correct |
25 |
Correct |
6 ms |
10072 KB |
Output is correct |
26 |
Correct |
6 ms |
9816 KB |
Output is correct |
27 |
Correct |
7 ms |
10588 KB |
Output is correct |
28 |
Correct |
9 ms |
10588 KB |
Output is correct |
29 |
Correct |
7 ms |
10584 KB |
Output is correct |
30 |
Correct |
7 ms |
10588 KB |
Output is correct |
31 |
Correct |
6 ms |
10588 KB |
Output is correct |
32 |
Correct |
9 ms |
11356 KB |
Output is correct |
33 |
Correct |
261 ms |
94432 KB |
Output is correct |
34 |
Correct |
180 ms |
54404 KB |
Output is correct |
35 |
Correct |
265 ms |
94160 KB |
Output is correct |
36 |
Correct |
226 ms |
54208 KB |
Output is correct |
37 |
Correct |
89 ms |
27988 KB |
Output is correct |
38 |
Correct |
73 ms |
22608 KB |
Output is correct |
39 |
Correct |
79 ms |
16208 KB |
Output is correct |
40 |
Correct |
77 ms |
16128 KB |
Output is correct |
41 |
Correct |
47 ms |
15904 KB |
Output is correct |
42 |
Correct |
81 ms |
16208 KB |
Output is correct |
43 |
Correct |
74 ms |
16212 KB |
Output is correct |
44 |
Correct |
250 ms |
103856 KB |
Output is correct |
45 |
Correct |
153 ms |
59220 KB |
Output is correct |
46 |
Correct |
53 ms |
21844 KB |
Output is correct |
47 |
Correct |
133 ms |
35408 KB |
Output is correct |
48 |
Correct |
86 ms |
21588 KB |
Output is correct |
49 |
Correct |
54 ms |
21540 KB |
Output is correct |
50 |
Correct |
187 ms |
52048 KB |
Output is correct |
51 |
Correct |
68 ms |
27212 KB |
Output is correct |
52 |
Correct |
178 ms |
35752 KB |
Output is correct |
53 |
Correct |
67 ms |
21536 KB |
Output is correct |
54 |
Correct |
52 ms |
15956 KB |
Output is correct |
55 |
Correct |
127 ms |
42580 KB |
Output is correct |
56 |
Correct |
96 ms |
38680 KB |
Output is correct |
57 |
Correct |
86 ms |
38996 KB |
Output is correct |
58 |
Correct |
135 ms |
47184 KB |
Output is correct |
59 |
Correct |
150 ms |
47184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9816 KB |
Output is correct |
2 |
Correct |
5 ms |
9728 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
5 ms |
9820 KB |
Output is correct |
5 |
Correct |
8 ms |
11352 KB |
Output is correct |
6 |
Correct |
7 ms |
10588 KB |
Output is correct |
7 |
Correct |
6 ms |
10332 KB |
Output is correct |
8 |
Correct |
9 ms |
11292 KB |
Output is correct |
9 |
Correct |
7 ms |
10588 KB |
Output is correct |
10 |
Correct |
6 ms |
10332 KB |
Output is correct |
11 |
Correct |
5 ms |
10076 KB |
Output is correct |
12 |
Correct |
6 ms |
9816 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
6 ms |
10076 KB |
Output is correct |
15 |
Correct |
6 ms |
9804 KB |
Output is correct |
16 |
Correct |
9 ms |
11608 KB |
Output is correct |
17 |
Correct |
7 ms |
10588 KB |
Output is correct |
18 |
Correct |
5 ms |
10076 KB |
Output is correct |
19 |
Correct |
7 ms |
10332 KB |
Output is correct |
20 |
Correct |
6 ms |
10176 KB |
Output is correct |
21 |
Correct |
6 ms |
10076 KB |
Output is correct |
22 |
Correct |
6 ms |
10844 KB |
Output is correct |
23 |
Correct |
6 ms |
10164 KB |
Output is correct |
24 |
Correct |
7 ms |
10328 KB |
Output is correct |
25 |
Correct |
6 ms |
10072 KB |
Output is correct |
26 |
Correct |
6 ms |
9816 KB |
Output is correct |
27 |
Correct |
7 ms |
10588 KB |
Output is correct |
28 |
Correct |
9 ms |
10588 KB |
Output is correct |
29 |
Correct |
7 ms |
10584 KB |
Output is correct |
30 |
Correct |
7 ms |
10588 KB |
Output is correct |
31 |
Correct |
6 ms |
10588 KB |
Output is correct |
32 |
Correct |
9 ms |
11356 KB |
Output is correct |
33 |
Correct |
261 ms |
94432 KB |
Output is correct |
34 |
Correct |
180 ms |
54404 KB |
Output is correct |
35 |
Correct |
265 ms |
94160 KB |
Output is correct |
36 |
Correct |
226 ms |
54208 KB |
Output is correct |
37 |
Correct |
89 ms |
27988 KB |
Output is correct |
38 |
Correct |
73 ms |
22608 KB |
Output is correct |
39 |
Correct |
79 ms |
16208 KB |
Output is correct |
40 |
Correct |
77 ms |
16128 KB |
Output is correct |
41 |
Correct |
47 ms |
15904 KB |
Output is correct |
42 |
Correct |
81 ms |
16208 KB |
Output is correct |
43 |
Correct |
74 ms |
16212 KB |
Output is correct |
44 |
Correct |
250 ms |
103856 KB |
Output is correct |
45 |
Correct |
153 ms |
59220 KB |
Output is correct |
46 |
Correct |
53 ms |
21844 KB |
Output is correct |
47 |
Correct |
133 ms |
35408 KB |
Output is correct |
48 |
Correct |
86 ms |
21588 KB |
Output is correct |
49 |
Correct |
54 ms |
21540 KB |
Output is correct |
50 |
Correct |
187 ms |
52048 KB |
Output is correct |
51 |
Correct |
68 ms |
27212 KB |
Output is correct |
52 |
Correct |
178 ms |
35752 KB |
Output is correct |
53 |
Correct |
67 ms |
21536 KB |
Output is correct |
54 |
Correct |
52 ms |
15956 KB |
Output is correct |
55 |
Correct |
127 ms |
42580 KB |
Output is correct |
56 |
Correct |
96 ms |
38680 KB |
Output is correct |
57 |
Correct |
86 ms |
38996 KB |
Output is correct |
58 |
Correct |
135 ms |
47184 KB |
Output is correct |
59 |
Correct |
150 ms |
47184 KB |
Output is correct |
60 |
Correct |
4 ms |
9820 KB |
Output is correct |
61 |
Correct |
4 ms |
9820 KB |
Output is correct |
62 |
Correct |
4 ms |
9820 KB |
Output is correct |
63 |
Correct |
5 ms |
9820 KB |
Output is correct |
64 |
Correct |
210 ms |
55864 KB |
Output is correct |
65 |
Correct |
160 ms |
38992 KB |
Output is correct |
66 |
Correct |
207 ms |
54352 KB |
Output is correct |
67 |
Correct |
158 ms |
38992 KB |
Output is correct |
68 |
Correct |
82 ms |
24400 KB |
Output is correct |
69 |
Correct |
58 ms |
20820 KB |
Output is correct |
70 |
Correct |
168 ms |
24496 KB |
Output is correct |
71 |
Correct |
78 ms |
15996 KB |
Output is correct |
72 |
Correct |
165 ms |
28500 KB |
Output is correct |
73 |
Correct |
62 ms |
15956 KB |
Output is correct |
74 |
Correct |
198 ms |
36120 KB |
Output is correct |
75 |
Correct |
80 ms |
22372 KB |
Output is correct |
76 |
Correct |
72 ms |
22352 KB |
Output is correct |
77 |
Correct |
178 ms |
36176 KB |
Output is correct |
78 |
Correct |
78 ms |
22416 KB |
Output is correct |
79 |
Correct |
255 ms |
55228 KB |
Output is correct |
80 |
Correct |
164 ms |
35156 KB |
Output is correct |
81 |
Correct |
86 ms |
21840 KB |
Output is correct |
82 |
Correct |
119 ms |
28500 KB |
Output is correct |
83 |
Correct |
57 ms |
15888 KB |
Output is correct |
84 |
Correct |
174 ms |
31544 KB |
Output is correct |
85 |
Correct |
167 ms |
31704 KB |
Output is correct |
86 |
Correct |
158 ms |
28540 KB |
Output is correct |
87 |
Correct |
162 ms |
31568 KB |
Output is correct |
88 |
Correct |
161 ms |
31740 KB |
Output is correct |