Submission #912488

# Submission time Handle Problem Language Result Execution time Memory
912488 2024-01-19T14:34:54 Z mickey080929 Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
365 ms 145276 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll inf = 2e18;

ll nxt[200010];
vector<ll> rev[200010];
ll H[200010], C[200010];
ll vis[200010];
ll st = -1;
ll cy[200010];
vector<ll> v[200010];

void dfs(ll x, ll num) {
	vis[x] = num;
	if (vis[nxt[x]] == num) {
		st = x;
		return;
	}
	if (vis[nxt[x]]) return;
	dfs(nxt[x], num);
}

void push_cycle(ll x, ll num) {
	cy[x] = num;
	v[num].push_back(x);
	if (cy[nxt[x]]) return;
	push_cycle(nxt[x], num);
}

ll par[200010];
multiset<pair<ll,ll>> ms[200010];

ll Find(ll x) {
	if (x == par[x]) return x;
	return par[x] = Find(par[x]);
}

void Union(ll x, ll y) {
	x = Find(x);
	y = Find(y);
	if (x == y) return;
	if (ms[x].size() < ms[y].size()) swap(x, y);
	for (auto &[i, add] : ms[y]) {
		ms[x].insert({i, add});
	}
	par[y] = x;
}

void dfs2(ll x) {
	for (auto &y : rev[x]) {
		dfs2(y);
		Union(x, y);
	}
	ll X = Find(x);
	ll cur = C[x];
	for (auto it = ms[X].upper_bound({H[x], inf}); it != ms[X].end(); ) {
		if (cur < it->second) {
			auto t = *it;
			ms[X].erase(it);
			ms[X].insert({t.first, t.second - cur});
			break;
		}
		cur -= it->second;
		it = ms[X].erase(it);
	}
	ms[X].insert({H[x], C[x]});
}

int main() {
	ios_base :: sync_with_stdio(false); cin.tie(NULL); 
	ll n;
	cin >> n;
	ll ans = 0;
	for (ll i=1; i<=n; i++) {
		cin >> nxt[i] >> H[i] >> C[i];
		H[i] *= -1;
		rev[nxt[i]].push_back(i);
		ans += C[i];
		par[i] = i;
	}
	ll num = 0, num2 = 0;
	for (ll i=1; i<=n; i++) {
		if (vis[i]) continue;
		st = -1;
		dfs(i, ++num);
		if (st != -1) {
			push_cycle(st, ++num2);
		}
	}
	for (ll gr=1; gr<=num2; gr++) {
		map<ll,ll> mp;
		ll pv = -1;
		for (auto &x : v[gr]) {
			for (auto &y : rev[x]) {
				if (cy[y]) continue;
				dfs2(y);
				if (pv == -1) pv = y;
				else Union(pv, y);
			}
			if (mp.find(H[x]) != mp.end()) {
				mp[H[x]] += C[x];
			}
			else {
				mp[H[x]] = C[x];
			}
		}
		ll mx = 0;
		if (pv == -1) {
			for (auto &[i, add] : mp)
				mx = max(mx, add);
		}
		else {
			pv = Find(pv);
			vector<pair<ll,ll>> func;
			for (auto &[i, add] : ms[pv]) {
				func.push_back({i, add});
			}
			for (ll i=0; i+1<func.size(); i++) {
				func[i+1].second += func[i].second;
			}
			for (auto &[i, add] : mp) {
				ll idx = upper_bound(func.begin(), func.end(), make_pair(i, inf)) - func.begin() - 1;
				if (idx == -1) mx = max(mx, add);
				else mx = max(mx, func[idx].second + add);
			}
			mx = max(mx, func.back().second);
		}
		ans -= mx;
	}
	cout << ans << '\n';
}

Compilation message

worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:121:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |    for (ll i=0; i+1<func.size(); i++) {
      |                 ~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 25176 KB Output is correct
2 Correct 7 ms 25184 KB Output is correct
3 Correct 7 ms 25264 KB Output is correct
4 Correct 6 ms 25260 KB Output is correct
5 Correct 12 ms 26464 KB Output is correct
6 Correct 11 ms 26460 KB Output is correct
7 Correct 11 ms 26412 KB Output is correct
8 Correct 11 ms 26460 KB Output is correct
9 Correct 11 ms 26536 KB Output is correct
10 Correct 11 ms 26460 KB Output is correct
11 Correct 15 ms 26968 KB Output is correct
12 Correct 10 ms 26100 KB Output is correct
13 Correct 10 ms 25948 KB Output is correct
14 Correct 9 ms 25692 KB Output is correct
15 Correct 10 ms 25688 KB Output is correct
16 Correct 11 ms 26716 KB Output is correct
17 Correct 12 ms 26808 KB Output is correct
18 Correct 12 ms 27224 KB Output is correct
19 Correct 9 ms 25948 KB Output is correct
20 Correct 9 ms 25948 KB Output is correct
21 Correct 10 ms 25948 KB Output is correct
22 Correct 9 ms 25948 KB Output is correct
23 Correct 9 ms 25948 KB Output is correct
24 Correct 10 ms 25948 KB Output is correct
25 Correct 12 ms 25948 KB Output is correct
26 Correct 8 ms 25944 KB Output is correct
27 Correct 11 ms 25944 KB Output is correct
28 Correct 9 ms 26204 KB Output is correct
29 Correct 9 ms 26208 KB Output is correct
30 Correct 10 ms 26380 KB Output is correct
31 Correct 10 ms 26404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 25176 KB Output is correct
2 Correct 7 ms 25184 KB Output is correct
3 Correct 7 ms 25264 KB Output is correct
4 Correct 6 ms 25260 KB Output is correct
5 Correct 12 ms 26464 KB Output is correct
6 Correct 11 ms 26460 KB Output is correct
7 Correct 11 ms 26412 KB Output is correct
8 Correct 11 ms 26460 KB Output is correct
9 Correct 11 ms 26536 KB Output is correct
10 Correct 11 ms 26460 KB Output is correct
11 Correct 15 ms 26968 KB Output is correct
12 Correct 10 ms 26100 KB Output is correct
13 Correct 10 ms 25948 KB Output is correct
14 Correct 9 ms 25692 KB Output is correct
15 Correct 10 ms 25688 KB Output is correct
16 Correct 11 ms 26716 KB Output is correct
17 Correct 12 ms 26808 KB Output is correct
18 Correct 12 ms 27224 KB Output is correct
19 Correct 9 ms 25948 KB Output is correct
20 Correct 9 ms 25948 KB Output is correct
21 Correct 10 ms 25948 KB Output is correct
22 Correct 9 ms 25948 KB Output is correct
23 Correct 9 ms 25948 KB Output is correct
24 Correct 10 ms 25948 KB Output is correct
25 Correct 12 ms 25948 KB Output is correct
26 Correct 8 ms 25944 KB Output is correct
27 Correct 11 ms 25944 KB Output is correct
28 Correct 9 ms 26204 KB Output is correct
29 Correct 9 ms 26208 KB Output is correct
30 Correct 10 ms 26380 KB Output is correct
31 Correct 10 ms 26404 KB Output is correct
32 Correct 11 ms 26456 KB Output is correct
33 Correct 322 ms 91684 KB Output is correct
34 Correct 322 ms 91232 KB Output is correct
35 Correct 329 ms 88756 KB Output is correct
36 Correct 312 ms 90304 KB Output is correct
37 Correct 315 ms 88256 KB Output is correct
38 Correct 359 ms 102412 KB Output is correct
39 Correct 135 ms 59312 KB Output is correct
40 Correct 135 ms 59216 KB Output is correct
41 Correct 133 ms 63032 KB Output is correct
42 Correct 125 ms 46808 KB Output is correct
43 Correct 124 ms 46928 KB Output is correct
44 Correct 272 ms 116512 KB Output is correct
45 Correct 266 ms 115268 KB Output is correct
46 Correct 314 ms 145276 KB Output is correct
47 Correct 194 ms 59056 KB Output is correct
48 Correct 186 ms 54776 KB Output is correct
49 Correct 160 ms 57512 KB Output is correct
50 Correct 248 ms 59208 KB Output is correct
51 Correct 234 ms 60552 KB Output is correct
52 Correct 245 ms 60188 KB Output is correct
53 Correct 234 ms 58304 KB Output is correct
54 Correct 85 ms 59132 KB Output is correct
55 Correct 192 ms 64372 KB Output is correct
56 Correct 158 ms 70168 KB Output is correct
57 Correct 151 ms 71920 KB Output is correct
58 Correct 192 ms 71660 KB Output is correct
59 Correct 178 ms 70088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 25176 KB Output is correct
2 Correct 7 ms 25184 KB Output is correct
3 Correct 7 ms 25264 KB Output is correct
4 Correct 6 ms 25260 KB Output is correct
5 Correct 12 ms 26464 KB Output is correct
6 Correct 11 ms 26460 KB Output is correct
7 Correct 11 ms 26412 KB Output is correct
8 Correct 11 ms 26460 KB Output is correct
9 Correct 11 ms 26536 KB Output is correct
10 Correct 11 ms 26460 KB Output is correct
11 Correct 15 ms 26968 KB Output is correct
12 Correct 10 ms 26100 KB Output is correct
13 Correct 10 ms 25948 KB Output is correct
14 Correct 9 ms 25692 KB Output is correct
15 Correct 10 ms 25688 KB Output is correct
16 Correct 11 ms 26716 KB Output is correct
17 Correct 12 ms 26808 KB Output is correct
18 Correct 12 ms 27224 KB Output is correct
19 Correct 9 ms 25948 KB Output is correct
20 Correct 9 ms 25948 KB Output is correct
21 Correct 10 ms 25948 KB Output is correct
22 Correct 9 ms 25948 KB Output is correct
23 Correct 9 ms 25948 KB Output is correct
24 Correct 10 ms 25948 KB Output is correct
25 Correct 12 ms 25948 KB Output is correct
26 Correct 8 ms 25944 KB Output is correct
27 Correct 11 ms 25944 KB Output is correct
28 Correct 9 ms 26204 KB Output is correct
29 Correct 9 ms 26208 KB Output is correct
30 Correct 10 ms 26380 KB Output is correct
31 Correct 10 ms 26404 KB Output is correct
32 Correct 11 ms 26456 KB Output is correct
33 Correct 322 ms 91684 KB Output is correct
34 Correct 322 ms 91232 KB Output is correct
35 Correct 329 ms 88756 KB Output is correct
36 Correct 312 ms 90304 KB Output is correct
37 Correct 315 ms 88256 KB Output is correct
38 Correct 359 ms 102412 KB Output is correct
39 Correct 135 ms 59312 KB Output is correct
40 Correct 135 ms 59216 KB Output is correct
41 Correct 133 ms 63032 KB Output is correct
42 Correct 125 ms 46808 KB Output is correct
43 Correct 124 ms 46928 KB Output is correct
44 Correct 272 ms 116512 KB Output is correct
45 Correct 266 ms 115268 KB Output is correct
46 Correct 314 ms 145276 KB Output is correct
47 Correct 194 ms 59056 KB Output is correct
48 Correct 186 ms 54776 KB Output is correct
49 Correct 160 ms 57512 KB Output is correct
50 Correct 248 ms 59208 KB Output is correct
51 Correct 234 ms 60552 KB Output is correct
52 Correct 245 ms 60188 KB Output is correct
53 Correct 234 ms 58304 KB Output is correct
54 Correct 85 ms 59132 KB Output is correct
55 Correct 192 ms 64372 KB Output is correct
56 Correct 158 ms 70168 KB Output is correct
57 Correct 151 ms 71920 KB Output is correct
58 Correct 192 ms 71660 KB Output is correct
59 Correct 178 ms 70088 KB Output is correct
60 Correct 7 ms 25180 KB Output is correct
61 Correct 7 ms 25180 KB Output is correct
62 Correct 6 ms 25180 KB Output is correct
63 Correct 7 ms 25180 KB Output is correct
64 Correct 318 ms 77880 KB Output is correct
65 Correct 296 ms 74692 KB Output is correct
66 Correct 294 ms 77052 KB Output is correct
67 Correct 312 ms 76828 KB Output is correct
68 Correct 286 ms 71528 KB Output is correct
69 Correct 303 ms 87348 KB Output is correct
70 Correct 259 ms 56644 KB Output is correct
71 Correct 128 ms 47048 KB Output is correct
72 Correct 237 ms 63496 KB Output is correct
73 Correct 129 ms 51264 KB Output is correct
74 Correct 365 ms 63300 KB Output is correct
75 Correct 275 ms 56904 KB Output is correct
76 Correct 259 ms 57096 KB Output is correct
77 Correct 228 ms 63940 KB Output is correct
78 Correct 180 ms 57796 KB Output is correct
79 Correct 263 ms 79948 KB Output is correct
80 Correct 242 ms 73808 KB Output is correct
81 Correct 231 ms 73408 KB Output is correct
82 Correct 229 ms 63680 KB Output is correct
83 Correct 74 ms 46160 KB Output is correct
84 Correct 264 ms 63064 KB Output is correct
85 Correct 264 ms 62608 KB Output is correct
86 Correct 279 ms 59548 KB Output is correct
87 Correct 313 ms 62836 KB Output is correct
88 Correct 250 ms 62792 KB Output is correct