Submission #532833

# Submission time Handle Problem Language Result Execution time Memory
532833 2022-03-04T03:28:10 Z fhvirus Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
373 ms 96396 KB
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#define debug(...) ((void)0)
#endif

const int kN = 200002;
int N, A[kN], H[kN], C[kN];

map<int, ll> dp[kN];

void add(map<int, ll>& mp, int pos, ll val) {
	mp[pos] += val;
	while (val > 0) {
		auto it = mp.lower_bound(pos);
		if (it == begin(mp)) break;
		it = prev(it);
		ll d = min(val, it->ss);
		val -= d;
		it->ss -= d;
		if (it->ss == 0) mp.erase(it);
	}
}
void merge(map<int, ll>& a, map<int, ll>& b) {
	if (a.size() < b.size()) swap(a, b);
	for (const auto& [pos, val]: b)
		a[pos] += val;
}

int indeg[kN];
vector<vector<int>> cycles;

int vis[kN], tot;
void dfs(int u) {
	vis[u] = tot;
	int v = A[u];
	if (vis[v] == 0) dfs(v);
	else if (vis[v] == tot) {
		vector<int> cycle;
		v = u;
		do {
			cycle.emplace_back(v);
			v = A[v];
		} while (v != u);
		cycles.push_back(cycle);
	}
}

signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> N;
	for (int i = 1; i <= N; ++i) {
		cin >> A[i] >> H[i] >> C[i];
		++indeg[A[i]];
	}

	for (int i = 1; i <= N; ++i)
		if (vis[i] == 0) {
			++tot;
			dfs(i);
		}

	queue<int> q;
	for (int i = 1; i <= N; ++i)
		if (indeg[i] == 0) q.push(i);

	while (!q.empty()) {
		int i = q.front(); q.pop();
		add(dp[i], H[i], C[i]);
		merge(dp[A[i]], dp[i]);
		if ((--indeg[A[i]]) == 0)
			q.push(A[i]);
	}

	ll ans = accumulate(C + 1, C + N + 1, 0ll);
	debug(ans);
	for (const vector<int>& cycle: cycles) {
		debug(cycle);
		map<int, ll> mp, cst;
		for (const int& u: cycle) {
			merge(mp, dp[u]);
			cst[H[u]] += C[u];
		}
		cst[0] = 0;
		debug(cst, mp);
		ll cur = 0, tmp = 0;
		auto i = rbegin(cst);
		auto j = rbegin(mp);
		for (; i != rend(cst); i = next(i)) {
			while (j != rend(mp) and j->ff >= i->ff) {
				cur += j->ss;
				j = next(j);
			}
			tmp = max(tmp, cur + i->ss);
		}
		ans -= tmp;
	}

	cout << ans << '\n';

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 7 ms 9676 KB Output is correct
5 Correct 13 ms 11148 KB Output is correct
6 Correct 9 ms 10572 KB Output is correct
7 Correct 8 ms 10212 KB Output is correct
8 Correct 11 ms 11148 KB Output is correct
9 Correct 8 ms 10572 KB Output is correct
10 Correct 8 ms 10316 KB Output is correct
11 Correct 7 ms 10124 KB Output is correct
12 Correct 8 ms 9932 KB Output is correct
13 Correct 7 ms 9932 KB Output is correct
14 Correct 10 ms 9932 KB Output is correct
15 Correct 9 ms 9852 KB Output is correct
16 Correct 11 ms 11212 KB Output is correct
17 Correct 9 ms 10640 KB Output is correct
18 Correct 9 ms 10060 KB Output is correct
19 Correct 9 ms 10316 KB Output is correct
20 Correct 8 ms 10060 KB Output is correct
21 Correct 7 ms 10060 KB Output is correct
22 Correct 8 ms 10504 KB Output is correct
23 Correct 7 ms 10188 KB Output is correct
24 Correct 8 ms 10188 KB Output is correct
25 Correct 8 ms 10000 KB Output is correct
26 Correct 7 ms 9932 KB Output is correct
27 Correct 9 ms 10444 KB Output is correct
28 Correct 9 ms 10252 KB Output is correct
29 Correct 8 ms 10252 KB Output is correct
30 Correct 9 ms 10380 KB Output is correct
31 Correct 9 ms 10356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 7 ms 9676 KB Output is correct
5 Correct 13 ms 11148 KB Output is correct
6 Correct 9 ms 10572 KB Output is correct
7 Correct 8 ms 10212 KB Output is correct
8 Correct 11 ms 11148 KB Output is correct
9 Correct 8 ms 10572 KB Output is correct
10 Correct 8 ms 10316 KB Output is correct
11 Correct 7 ms 10124 KB Output is correct
12 Correct 8 ms 9932 KB Output is correct
13 Correct 7 ms 9932 KB Output is correct
14 Correct 10 ms 9932 KB Output is correct
15 Correct 9 ms 9852 KB Output is correct
16 Correct 11 ms 11212 KB Output is correct
17 Correct 9 ms 10640 KB Output is correct
18 Correct 9 ms 10060 KB Output is correct
19 Correct 9 ms 10316 KB Output is correct
20 Correct 8 ms 10060 KB Output is correct
21 Correct 7 ms 10060 KB Output is correct
22 Correct 8 ms 10504 KB Output is correct
23 Correct 7 ms 10188 KB Output is correct
24 Correct 8 ms 10188 KB Output is correct
25 Correct 8 ms 10000 KB Output is correct
26 Correct 7 ms 9932 KB Output is correct
27 Correct 9 ms 10444 KB Output is correct
28 Correct 9 ms 10252 KB Output is correct
29 Correct 8 ms 10252 KB Output is correct
30 Correct 9 ms 10380 KB Output is correct
31 Correct 9 ms 10356 KB Output is correct
32 Correct 10 ms 11084 KB Output is correct
33 Correct 356 ms 85916 KB Output is correct
34 Correct 282 ms 55384 KB Output is correct
35 Correct 373 ms 85324 KB Output is correct
36 Correct 282 ms 55196 KB Output is correct
37 Correct 152 ms 28964 KB Output is correct
38 Correct 105 ms 23624 KB Output is correct
39 Correct 155 ms 17092 KB Output is correct
40 Correct 115 ms 17092 KB Output is correct
41 Correct 78 ms 16996 KB Output is correct
42 Correct 123 ms 17160 KB Output is correct
43 Correct 110 ms 17172 KB Output is correct
44 Correct 314 ms 96396 KB Output is correct
45 Correct 211 ms 60592 KB Output is correct
46 Correct 87 ms 23132 KB Output is correct
47 Correct 180 ms 29944 KB Output is correct
48 Correct 97 ms 23028 KB Output is correct
49 Correct 94 ms 23044 KB Output is correct
50 Correct 228 ms 41040 KB Output is correct
51 Correct 102 ms 28612 KB Output is correct
52 Correct 222 ms 30088 KB Output is correct
53 Correct 102 ms 23104 KB Output is correct
54 Correct 85 ms 17068 KB Output is correct
55 Correct 173 ms 34672 KB Output is correct
56 Correct 126 ms 29768 KB Output is correct
57 Correct 120 ms 29304 KB Output is correct
58 Correct 187 ms 35796 KB Output is correct
59 Correct 182 ms 35872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 7 ms 9676 KB Output is correct
5 Correct 13 ms 11148 KB Output is correct
6 Correct 9 ms 10572 KB Output is correct
7 Correct 8 ms 10212 KB Output is correct
8 Correct 11 ms 11148 KB Output is correct
9 Correct 8 ms 10572 KB Output is correct
10 Correct 8 ms 10316 KB Output is correct
11 Correct 7 ms 10124 KB Output is correct
12 Correct 8 ms 9932 KB Output is correct
13 Correct 7 ms 9932 KB Output is correct
14 Correct 10 ms 9932 KB Output is correct
15 Correct 9 ms 9852 KB Output is correct
16 Correct 11 ms 11212 KB Output is correct
17 Correct 9 ms 10640 KB Output is correct
18 Correct 9 ms 10060 KB Output is correct
19 Correct 9 ms 10316 KB Output is correct
20 Correct 8 ms 10060 KB Output is correct
21 Correct 7 ms 10060 KB Output is correct
22 Correct 8 ms 10504 KB Output is correct
23 Correct 7 ms 10188 KB Output is correct
24 Correct 8 ms 10188 KB Output is correct
25 Correct 8 ms 10000 KB Output is correct
26 Correct 7 ms 9932 KB Output is correct
27 Correct 9 ms 10444 KB Output is correct
28 Correct 9 ms 10252 KB Output is correct
29 Correct 8 ms 10252 KB Output is correct
30 Correct 9 ms 10380 KB Output is correct
31 Correct 9 ms 10356 KB Output is correct
32 Correct 10 ms 11084 KB Output is correct
33 Correct 356 ms 85916 KB Output is correct
34 Correct 282 ms 55384 KB Output is correct
35 Correct 373 ms 85324 KB Output is correct
36 Correct 282 ms 55196 KB Output is correct
37 Correct 152 ms 28964 KB Output is correct
38 Correct 105 ms 23624 KB Output is correct
39 Correct 155 ms 17092 KB Output is correct
40 Correct 115 ms 17092 KB Output is correct
41 Correct 78 ms 16996 KB Output is correct
42 Correct 123 ms 17160 KB Output is correct
43 Correct 110 ms 17172 KB Output is correct
44 Correct 314 ms 96396 KB Output is correct
45 Correct 211 ms 60592 KB Output is correct
46 Correct 87 ms 23132 KB Output is correct
47 Correct 180 ms 29944 KB Output is correct
48 Correct 97 ms 23028 KB Output is correct
49 Correct 94 ms 23044 KB Output is correct
50 Correct 228 ms 41040 KB Output is correct
51 Correct 102 ms 28612 KB Output is correct
52 Correct 222 ms 30088 KB Output is correct
53 Correct 102 ms 23104 KB Output is correct
54 Correct 85 ms 17068 KB Output is correct
55 Correct 173 ms 34672 KB Output is correct
56 Correct 126 ms 29768 KB Output is correct
57 Correct 120 ms 29304 KB Output is correct
58 Correct 187 ms 35796 KB Output is correct
59 Correct 182 ms 35872 KB Output is correct
60 Correct 6 ms 9676 KB Output is correct
61 Correct 6 ms 9676 KB Output is correct
62 Correct 5 ms 9728 KB Output is correct
63 Correct 4 ms 9644 KB Output is correct
64 Correct 288 ms 57288 KB Output is correct
65 Correct 264 ms 41880 KB Output is correct
66 Correct 298 ms 58412 KB Output is correct
67 Correct 255 ms 41864 KB Output is correct
68 Correct 150 ms 27120 KB Output is correct
69 Correct 119 ms 23504 KB Output is correct
70 Correct 191 ms 41028 KB Output is correct
71 Correct 126 ms 29536 KB Output is correct
72 Correct 215 ms 50804 KB Output is correct
73 Correct 115 ms 39232 KB Output is correct
74 Correct 264 ms 47308 KB Output is correct
75 Correct 150 ms 34832 KB Output is correct
76 Correct 110 ms 34748 KB Output is correct
77 Correct 201 ms 47380 KB Output is correct
78 Correct 107 ms 34840 KB Output is correct
79 Correct 257 ms 66648 KB Output is correct
80 Correct 233 ms 47740 KB Output is correct
81 Correct 123 ms 34368 KB Output is correct
82 Correct 177 ms 51024 KB Output is correct
83 Correct 114 ms 29688 KB Output is correct
84 Correct 193 ms 43684 KB Output is correct
85 Correct 188 ms 43648 KB Output is correct
86 Correct 193 ms 40428 KB Output is correct
87 Correct 231 ms 44728 KB Output is correct
88 Correct 200 ms 46896 KB Output is correct