#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000;
int a[MAXN], h[MAXN], c[MAXN], deg[MAXN], n;
map<int, long long> s[MAXN];
vector<int> q;
// dp(v, i) =
// h[v] < i: \sum{dp(u, i)}
// h[v] >= i: max(\sum{dp(u, i)}, c[v] + \sum{dp(u, h[v])})
void cdeg(int v){
if (deg[v]) return;
auto it = s[v].lower_bound(h[v]);
if (it == s[v].end() || it->first != h[v])
it = s[v].emplace_hint(it, h[v], 0);
it->second += c[v];
for (int x = c[v]; it != s[v].begin() && x;){
it--;
if (it->second <= x){
x -= it->second;
it = s[v].erase(it);
}
else{
it->second -= x;
x = 0;
}
}
q.push_back(v);
}
void merge(map<int, long long> &a, map<int, long long> &b){
if (a.size() < b.size()) swap(a, b);
for (const pair<int, long long> &x: b){
a[x.first] += x.second;
}
}
long long pfm(map<int, long long> &hs){
long long p = 0;
for (auto it = hs.rbegin(); it != hs.rend(); it++)
p = it->second = p + it->second;
hs.emplace_hint(hs.end(), INT_MAX, 0);
return p;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i=0; i<n; i++){
cin >> a[i] >> h[i] >> c[i];
a[i]--, deg[a[i]]++;
}
for (int i=0; i<n; i++)
cdeg(i);
long long result = accumulate(c, c + n, 0LL);
while (q.size()){
int v = q.back(); q.pop_back();
merge(s[a[v]], s[v]);
deg[a[v]]--, cdeg(a[v]);
}
for (int i=0; i<n; i++){
if (!deg[i]) continue;
vector<pair<int, int>> cyc;
map<int, long long> hs;
for (int pos = i; deg[pos]; deg[pos] = 0, pos = a[pos]){
merge(hs, s[pos]);
cyc.emplace_back(h[pos], c[pos]);
}
sort(cyc.begin(), cyc.end());
long long max_branch = pfm(hs);
for (int lptr=0, rptr=0; lptr<cyc.size(); lptr=rptr){
long long v = hs.lower_bound(cyc[lptr].first)->second;
for (; rptr < cyc.size() && cyc[lptr].first == cyc[rptr].first; rptr++)
v += cyc[rptr].second;
max_branch = max(max_branch, v);
}
result -= max_branch;
}
cout << result << "\n";
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:77:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int lptr=0, rptr=0; lptr<cyc.size(); lptr=rptr){
| ~~~~^~~~~~~~~~~
worst_reporter2.cpp:79:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (; rptr < cyc.size() && cyc[lptr].first == cyc[rptr].first; rptr++)
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12892 KB |
Output is correct |
5 |
Correct |
6 ms |
14112 KB |
Output is correct |
6 |
Correct |
6 ms |
13404 KB |
Output is correct |
7 |
Correct |
5 ms |
13400 KB |
Output is correct |
8 |
Correct |
7 ms |
14172 KB |
Output is correct |
9 |
Correct |
5 ms |
13660 KB |
Output is correct |
10 |
Correct |
5 ms |
13404 KB |
Output is correct |
11 |
Correct |
4 ms |
13148 KB |
Output is correct |
12 |
Correct |
5 ms |
13076 KB |
Output is correct |
13 |
Correct |
4 ms |
13064 KB |
Output is correct |
14 |
Correct |
6 ms |
12888 KB |
Output is correct |
15 |
Correct |
7 ms |
12892 KB |
Output is correct |
16 |
Correct |
8 ms |
14680 KB |
Output is correct |
17 |
Correct |
6 ms |
13660 KB |
Output is correct |
18 |
Correct |
4 ms |
13208 KB |
Output is correct |
19 |
Correct |
5 ms |
13404 KB |
Output is correct |
20 |
Correct |
4 ms |
13148 KB |
Output is correct |
21 |
Correct |
4 ms |
13236 KB |
Output is correct |
22 |
Correct |
6 ms |
13660 KB |
Output is correct |
23 |
Correct |
4 ms |
13148 KB |
Output is correct |
24 |
Correct |
5 ms |
13404 KB |
Output is correct |
25 |
Correct |
5 ms |
13060 KB |
Output is correct |
26 |
Correct |
5 ms |
12888 KB |
Output is correct |
27 |
Correct |
6 ms |
13404 KB |
Output is correct |
28 |
Correct |
5 ms |
13308 KB |
Output is correct |
29 |
Correct |
5 ms |
13148 KB |
Output is correct |
30 |
Correct |
5 ms |
13404 KB |
Output is correct |
31 |
Correct |
5 ms |
13404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12892 KB |
Output is correct |
5 |
Correct |
6 ms |
14112 KB |
Output is correct |
6 |
Correct |
6 ms |
13404 KB |
Output is correct |
7 |
Correct |
5 ms |
13400 KB |
Output is correct |
8 |
Correct |
7 ms |
14172 KB |
Output is correct |
9 |
Correct |
5 ms |
13660 KB |
Output is correct |
10 |
Correct |
5 ms |
13404 KB |
Output is correct |
11 |
Correct |
4 ms |
13148 KB |
Output is correct |
12 |
Correct |
5 ms |
13076 KB |
Output is correct |
13 |
Correct |
4 ms |
13064 KB |
Output is correct |
14 |
Correct |
6 ms |
12888 KB |
Output is correct |
15 |
Correct |
7 ms |
12892 KB |
Output is correct |
16 |
Correct |
8 ms |
14680 KB |
Output is correct |
17 |
Correct |
6 ms |
13660 KB |
Output is correct |
18 |
Correct |
4 ms |
13208 KB |
Output is correct |
19 |
Correct |
5 ms |
13404 KB |
Output is correct |
20 |
Correct |
4 ms |
13148 KB |
Output is correct |
21 |
Correct |
4 ms |
13236 KB |
Output is correct |
22 |
Correct |
6 ms |
13660 KB |
Output is correct |
23 |
Correct |
4 ms |
13148 KB |
Output is correct |
24 |
Correct |
5 ms |
13404 KB |
Output is correct |
25 |
Correct |
5 ms |
13060 KB |
Output is correct |
26 |
Correct |
5 ms |
12888 KB |
Output is correct |
27 |
Correct |
6 ms |
13404 KB |
Output is correct |
28 |
Correct |
5 ms |
13308 KB |
Output is correct |
29 |
Correct |
5 ms |
13148 KB |
Output is correct |
30 |
Correct |
5 ms |
13404 KB |
Output is correct |
31 |
Correct |
5 ms |
13404 KB |
Output is correct |
32 |
Correct |
7 ms |
14168 KB |
Output is correct |
33 |
Correct |
267 ms |
82636 KB |
Output is correct |
34 |
Correct |
267 ms |
54016 KB |
Output is correct |
35 |
Correct |
257 ms |
82632 KB |
Output is correct |
36 |
Correct |
194 ms |
53740 KB |
Output is correct |
37 |
Correct |
95 ms |
29556 KB |
Output is correct |
38 |
Correct |
68 ms |
24428 KB |
Output is correct |
39 |
Correct |
75 ms |
18132 KB |
Output is correct |
40 |
Correct |
69 ms |
18000 KB |
Output is correct |
41 |
Correct |
53 ms |
18004 KB |
Output is correct |
42 |
Correct |
74 ms |
18160 KB |
Output is correct |
43 |
Correct |
68 ms |
18004 KB |
Output is correct |
44 |
Correct |
267 ms |
97736 KB |
Output is correct |
45 |
Correct |
187 ms |
62044 KB |
Output is correct |
46 |
Correct |
56 ms |
24444 KB |
Output is correct |
47 |
Correct |
123 ms |
31576 KB |
Output is correct |
48 |
Correct |
76 ms |
24668 KB |
Output is correct |
49 |
Correct |
61 ms |
24648 KB |
Output is correct |
50 |
Correct |
146 ms |
42948 KB |
Output is correct |
51 |
Correct |
69 ms |
30408 KB |
Output is correct |
52 |
Correct |
117 ms |
31712 KB |
Output is correct |
53 |
Correct |
67 ms |
24780 KB |
Output is correct |
54 |
Correct |
54 ms |
18256 KB |
Output is correct |
55 |
Correct |
115 ms |
35532 KB |
Output is correct |
56 |
Correct |
86 ms |
30412 KB |
Output is correct |
57 |
Correct |
85 ms |
29668 KB |
Output is correct |
58 |
Correct |
121 ms |
35924 KB |
Output is correct |
59 |
Correct |
120 ms |
36084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12892 KB |
Output is correct |
5 |
Correct |
6 ms |
14112 KB |
Output is correct |
6 |
Correct |
6 ms |
13404 KB |
Output is correct |
7 |
Correct |
5 ms |
13400 KB |
Output is correct |
8 |
Correct |
7 ms |
14172 KB |
Output is correct |
9 |
Correct |
5 ms |
13660 KB |
Output is correct |
10 |
Correct |
5 ms |
13404 KB |
Output is correct |
11 |
Correct |
4 ms |
13148 KB |
Output is correct |
12 |
Correct |
5 ms |
13076 KB |
Output is correct |
13 |
Correct |
4 ms |
13064 KB |
Output is correct |
14 |
Correct |
6 ms |
12888 KB |
Output is correct |
15 |
Correct |
7 ms |
12892 KB |
Output is correct |
16 |
Correct |
8 ms |
14680 KB |
Output is correct |
17 |
Correct |
6 ms |
13660 KB |
Output is correct |
18 |
Correct |
4 ms |
13208 KB |
Output is correct |
19 |
Correct |
5 ms |
13404 KB |
Output is correct |
20 |
Correct |
4 ms |
13148 KB |
Output is correct |
21 |
Correct |
4 ms |
13236 KB |
Output is correct |
22 |
Correct |
6 ms |
13660 KB |
Output is correct |
23 |
Correct |
4 ms |
13148 KB |
Output is correct |
24 |
Correct |
5 ms |
13404 KB |
Output is correct |
25 |
Correct |
5 ms |
13060 KB |
Output is correct |
26 |
Correct |
5 ms |
12888 KB |
Output is correct |
27 |
Correct |
6 ms |
13404 KB |
Output is correct |
28 |
Correct |
5 ms |
13308 KB |
Output is correct |
29 |
Correct |
5 ms |
13148 KB |
Output is correct |
30 |
Correct |
5 ms |
13404 KB |
Output is correct |
31 |
Correct |
5 ms |
13404 KB |
Output is correct |
32 |
Correct |
7 ms |
14168 KB |
Output is correct |
33 |
Correct |
267 ms |
82636 KB |
Output is correct |
34 |
Correct |
267 ms |
54016 KB |
Output is correct |
35 |
Correct |
257 ms |
82632 KB |
Output is correct |
36 |
Correct |
194 ms |
53740 KB |
Output is correct |
37 |
Correct |
95 ms |
29556 KB |
Output is correct |
38 |
Correct |
68 ms |
24428 KB |
Output is correct |
39 |
Correct |
75 ms |
18132 KB |
Output is correct |
40 |
Correct |
69 ms |
18000 KB |
Output is correct |
41 |
Correct |
53 ms |
18004 KB |
Output is correct |
42 |
Correct |
74 ms |
18160 KB |
Output is correct |
43 |
Correct |
68 ms |
18004 KB |
Output is correct |
44 |
Correct |
267 ms |
97736 KB |
Output is correct |
45 |
Correct |
187 ms |
62044 KB |
Output is correct |
46 |
Correct |
56 ms |
24444 KB |
Output is correct |
47 |
Correct |
123 ms |
31576 KB |
Output is correct |
48 |
Correct |
76 ms |
24668 KB |
Output is correct |
49 |
Correct |
61 ms |
24648 KB |
Output is correct |
50 |
Correct |
146 ms |
42948 KB |
Output is correct |
51 |
Correct |
69 ms |
30408 KB |
Output is correct |
52 |
Correct |
117 ms |
31712 KB |
Output is correct |
53 |
Correct |
67 ms |
24780 KB |
Output is correct |
54 |
Correct |
54 ms |
18256 KB |
Output is correct |
55 |
Correct |
115 ms |
35532 KB |
Output is correct |
56 |
Correct |
86 ms |
30412 KB |
Output is correct |
57 |
Correct |
85 ms |
29668 KB |
Output is correct |
58 |
Correct |
121 ms |
35924 KB |
Output is correct |
59 |
Correct |
120 ms |
36084 KB |
Output is correct |
60 |
Correct |
3 ms |
12892 KB |
Output is correct |
61 |
Correct |
3 ms |
12892 KB |
Output is correct |
62 |
Correct |
3 ms |
12892 KB |
Output is correct |
63 |
Correct |
4 ms |
12892 KB |
Output is correct |
64 |
Correct |
219 ms |
57540 KB |
Output is correct |
65 |
Correct |
177 ms |
40900 KB |
Output is correct |
66 |
Correct |
232 ms |
56044 KB |
Output is correct |
67 |
Correct |
175 ms |
40912 KB |
Output is correct |
68 |
Correct |
101 ms |
26332 KB |
Output is correct |
69 |
Correct |
70 ms |
22876 KB |
Output is correct |
70 |
Correct |
76 ms |
20164 KB |
Output is correct |
71 |
Correct |
74 ms |
19508 KB |
Output is correct |
72 |
Correct |
71 ms |
20208 KB |
Output is correct |
73 |
Correct |
77 ms |
20152 KB |
Output is correct |
74 |
Correct |
145 ms |
31808 KB |
Output is correct |
75 |
Correct |
98 ms |
25960 KB |
Output is correct |
76 |
Correct |
92 ms |
25716 KB |
Output is correct |
77 |
Correct |
116 ms |
32076 KB |
Output is correct |
78 |
Correct |
86 ms |
25936 KB |
Output is correct |
79 |
Correct |
173 ms |
50252 KB |
Output is correct |
80 |
Correct |
133 ms |
37348 KB |
Output is correct |
81 |
Correct |
90 ms |
25056 KB |
Output is correct |
82 |
Correct |
73 ms |
20324 KB |
Output is correct |
83 |
Correct |
56 ms |
17912 KB |
Output is correct |
84 |
Correct |
127 ms |
28360 KB |
Output is correct |
85 |
Correct |
125 ms |
28364 KB |
Output is correct |
86 |
Correct |
124 ms |
28256 KB |
Output is correct |
87 |
Correct |
128 ms |
28384 KB |
Output is correct |
88 |
Correct |
129 ms |
28360 KB |
Output is correct |