Submission #417070

# Submission time Handle Problem Language Result Execution time Memory
417070 2021-06-03T11:24:30 Z errorgorn Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
739 ms 194280 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
#define endl '\n'

#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound

#define rep(x,s,e) for (auto x=s-(s>e);x!=e-(s>e);(s<e?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n;
int arr[200005];
int h[200005];
int c[200005];

vector<int> al[200005];

map<int,ll> s[200005];

void dfs(int i,int p){
	for (auto &it:al[i]){
		if (it==p) continue;
		
		dfs(it,i);
		if (sz(s[i])<sz(s[it])) swap(s[i],s[it]);
		for (auto &it2:s[it]) s[i][it2.fi]+=it2.se;
	}
	
	s[i][0]+=c[i];
	s[i][h[i]+1]+=c[i];
	s[i][h[i]]-=c[i];
	
	auto iter=s[i].find(h[i]);
	
	while ((*iter).se<0){
		ll temp=min((*prev(iter)).se,-(*iter).se);
		(*iter).se+=temp;
		if ((*prev(iter)).se==temp) s[i].erase(prev(iter));
		else (*prev(iter)).se-=temp;
	}
	
	//cout<<i<<": "<<endl;
	//for (auto &it:s[i]) cout<<it.fi<<" "<<it.se<<endl;
	//cout<<endl;
}

bool vis[200005];
bool onstk[200005];

ll ans=0;

void dfs_cyc(int i){
	vis[i]=true;
	onstk[i]=true;
	
	if (!vis[arr[i]]) dfs_cyc(arr[i]);
	else if (onstk[arr[i]]){
		vector<int> v;
		int curr=i;
		do{
			v.pub(curr);
			curr=arr[curr];
		} while (curr!=i);
		
		//for (auto &it:v) cout<<it<<" "; cout<<endl;
		
		map<int,ll> m;
		rep(x,0,sz(v)){
			for (auto &it:al[v[(x+1)%sz(v)]]){
				if (it==v[x]) continue;
				dfs(it,v[(x+1)%sz(v)]);
				for (auto &it2:s[it]) m[it2.fi]+=it2.se;
			}
		}
		
		ll tot=0;
		
		for (auto &it:v) tot+=c[it];
		sort(all(v),[](int i,int j){
			return h[i]>h[j];
		});
		
		ll best=tot+m[0];
		
		while (!v.empty()){
			int hh=h[v.back()];
			
			ll save=0;
			while (!v.empty() && h[v.back()]==hh){
				save+=c[v.back()];
				v.pob();
			}
			
			while (!m.empty() && (*m.begin()).fi<=hh){
				tot+=(*m.begin()).se;
				m.erase(m.begin());
			}
			
			best=min(best,tot-save);
		}
		
		ans+=best;
	}
	
	onstk[i]=false;
}

int main(){
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(false);
	
	cin>>n;
	rep(x,1,n+1) cin>>arr[x]>>h[x]>>c[x];
	rep(x,1,n+1) al[arr[x]].pub(x);
	
	rep(x,1,n+1) if (!vis[x]) dfs_cyc(x);
	
	cout<<ans<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 9 ms 14360 KB Output is correct
4 Correct 8 ms 14416 KB Output is correct
5 Correct 19 ms 17112 KB Output is correct
6 Correct 15 ms 15820 KB Output is correct
7 Correct 13 ms 15368 KB Output is correct
8 Correct 18 ms 17028 KB Output is correct
9 Correct 15 ms 15684 KB Output is correct
10 Correct 13 ms 15344 KB Output is correct
11 Correct 14 ms 14928 KB Output is correct
12 Correct 12 ms 15388 KB Output is correct
13 Correct 11 ms 15304 KB Output is correct
14 Correct 13 ms 15152 KB Output is correct
15 Correct 13 ms 15052 KB Output is correct
16 Correct 23 ms 17604 KB Output is correct
17 Correct 19 ms 16204 KB Output is correct
18 Correct 11 ms 14944 KB Output is correct
19 Correct 15 ms 15956 KB Output is correct
20 Correct 13 ms 15316 KB Output is correct
21 Correct 12 ms 15312 KB Output is correct
22 Correct 14 ms 15864 KB Output is correct
23 Correct 12 ms 15168 KB Output is correct
24 Correct 15 ms 15948 KB Output is correct
25 Correct 12 ms 15308 KB Output is correct
26 Correct 14 ms 15948 KB Output is correct
27 Correct 16 ms 16152 KB Output is correct
28 Correct 17 ms 16104 KB Output is correct
29 Correct 15 ms 16332 KB Output is correct
30 Correct 16 ms 16204 KB Output is correct
31 Correct 19 ms 16204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 9 ms 14360 KB Output is correct
4 Correct 8 ms 14416 KB Output is correct
5 Correct 19 ms 17112 KB Output is correct
6 Correct 15 ms 15820 KB Output is correct
7 Correct 13 ms 15368 KB Output is correct
8 Correct 18 ms 17028 KB Output is correct
9 Correct 15 ms 15684 KB Output is correct
10 Correct 13 ms 15344 KB Output is correct
11 Correct 14 ms 14928 KB Output is correct
12 Correct 12 ms 15388 KB Output is correct
13 Correct 11 ms 15304 KB Output is correct
14 Correct 13 ms 15152 KB Output is correct
15 Correct 13 ms 15052 KB Output is correct
16 Correct 23 ms 17604 KB Output is correct
17 Correct 19 ms 16204 KB Output is correct
18 Correct 11 ms 14944 KB Output is correct
19 Correct 15 ms 15956 KB Output is correct
20 Correct 13 ms 15316 KB Output is correct
21 Correct 12 ms 15312 KB Output is correct
22 Correct 14 ms 15864 KB Output is correct
23 Correct 12 ms 15168 KB Output is correct
24 Correct 15 ms 15948 KB Output is correct
25 Correct 12 ms 15308 KB Output is correct
26 Correct 14 ms 15948 KB Output is correct
27 Correct 16 ms 16152 KB Output is correct
28 Correct 17 ms 16104 KB Output is correct
29 Correct 15 ms 16332 KB Output is correct
30 Correct 16 ms 16204 KB Output is correct
31 Correct 19 ms 16204 KB Output is correct
32 Correct 23 ms 17160 KB Output is correct
33 Correct 642 ms 140192 KB Output is correct
34 Correct 442 ms 82004 KB Output is correct
35 Correct 612 ms 136724 KB Output is correct
36 Correct 422 ms 81996 KB Output is correct
37 Correct 245 ms 43840 KB Output is correct
38 Correct 243 ms 33100 KB Output is correct
39 Correct 208 ms 48964 KB Output is correct
40 Correct 183 ms 48800 KB Output is correct
41 Correct 118 ms 48772 KB Output is correct
42 Correct 196 ms 36684 KB Output is correct
43 Correct 180 ms 36352 KB Output is correct
44 Correct 739 ms 194280 KB Output is correct
45 Correct 432 ms 111172 KB Output is correct
46 Correct 136 ms 32968 KB Output is correct
47 Correct 463 ms 73536 KB Output is correct
48 Correct 174 ms 45808 KB Output is correct
49 Correct 141 ms 45524 KB Output is correct
50 Correct 384 ms 68392 KB Output is correct
51 Correct 171 ms 43312 KB Output is correct
52 Correct 478 ms 74676 KB Output is correct
53 Correct 172 ms 46268 KB Output is correct
54 Correct 380 ms 73764 KB Output is correct
55 Correct 403 ms 82244 KB Output is correct
56 Correct 379 ms 86488 KB Output is correct
57 Correct 392 ms 90460 KB Output is correct
58 Correct 399 ms 86192 KB Output is correct
59 Correct 434 ms 86268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 9 ms 14360 KB Output is correct
4 Correct 8 ms 14416 KB Output is correct
5 Correct 19 ms 17112 KB Output is correct
6 Correct 15 ms 15820 KB Output is correct
7 Correct 13 ms 15368 KB Output is correct
8 Correct 18 ms 17028 KB Output is correct
9 Correct 15 ms 15684 KB Output is correct
10 Correct 13 ms 15344 KB Output is correct
11 Correct 14 ms 14928 KB Output is correct
12 Correct 12 ms 15388 KB Output is correct
13 Correct 11 ms 15304 KB Output is correct
14 Correct 13 ms 15152 KB Output is correct
15 Correct 13 ms 15052 KB Output is correct
16 Correct 23 ms 17604 KB Output is correct
17 Correct 19 ms 16204 KB Output is correct
18 Correct 11 ms 14944 KB Output is correct
19 Correct 15 ms 15956 KB Output is correct
20 Correct 13 ms 15316 KB Output is correct
21 Correct 12 ms 15312 KB Output is correct
22 Correct 14 ms 15864 KB Output is correct
23 Correct 12 ms 15168 KB Output is correct
24 Correct 15 ms 15948 KB Output is correct
25 Correct 12 ms 15308 KB Output is correct
26 Correct 14 ms 15948 KB Output is correct
27 Correct 16 ms 16152 KB Output is correct
28 Correct 17 ms 16104 KB Output is correct
29 Correct 15 ms 16332 KB Output is correct
30 Correct 16 ms 16204 KB Output is correct
31 Correct 19 ms 16204 KB Output is correct
32 Correct 23 ms 17160 KB Output is correct
33 Correct 642 ms 140192 KB Output is correct
34 Correct 442 ms 82004 KB Output is correct
35 Correct 612 ms 136724 KB Output is correct
36 Correct 422 ms 81996 KB Output is correct
37 Correct 245 ms 43840 KB Output is correct
38 Correct 243 ms 33100 KB Output is correct
39 Correct 208 ms 48964 KB Output is correct
40 Correct 183 ms 48800 KB Output is correct
41 Correct 118 ms 48772 KB Output is correct
42 Correct 196 ms 36684 KB Output is correct
43 Correct 180 ms 36352 KB Output is correct
44 Correct 739 ms 194280 KB Output is correct
45 Correct 432 ms 111172 KB Output is correct
46 Correct 136 ms 32968 KB Output is correct
47 Correct 463 ms 73536 KB Output is correct
48 Correct 174 ms 45808 KB Output is correct
49 Correct 141 ms 45524 KB Output is correct
50 Correct 384 ms 68392 KB Output is correct
51 Correct 171 ms 43312 KB Output is correct
52 Correct 478 ms 74676 KB Output is correct
53 Correct 172 ms 46268 KB Output is correct
54 Correct 380 ms 73764 KB Output is correct
55 Correct 403 ms 82244 KB Output is correct
56 Correct 379 ms 86488 KB Output is correct
57 Correct 392 ms 90460 KB Output is correct
58 Correct 399 ms 86192 KB Output is correct
59 Correct 434 ms 86268 KB Output is correct
60 Correct 8 ms 14412 KB Output is correct
61 Correct 8 ms 14424 KB Output is correct
62 Correct 9 ms 14424 KB Output is correct
63 Correct 9 ms 14416 KB Output is correct
64 Correct 614 ms 114084 KB Output is correct
65 Correct 387 ms 66588 KB Output is correct
66 Correct 545 ms 96840 KB Output is correct
67 Correct 420 ms 66536 KB Output is correct
68 Correct 275 ms 38948 KB Output is correct
69 Correct 234 ms 30740 KB Output is correct
70 Correct 183 ms 57124 KB Output is correct
71 Correct 180 ms 48392 KB Output is correct
72 Correct 241 ms 71760 KB Output is correct
73 Correct 194 ms 71676 KB Output is correct
74 Correct 353 ms 69360 KB Output is correct
75 Correct 272 ms 56948 KB Output is correct
76 Correct 211 ms 56884 KB Output is correct
77 Correct 296 ms 69860 KB Output is correct
78 Correct 178 ms 57524 KB Output is correct
79 Correct 447 ms 106096 KB Output is correct
80 Correct 325 ms 79344 KB Output is correct
81 Correct 210 ms 57772 KB Output is correct
82 Correct 237 ms 71680 KB Output is correct
83 Correct 116 ms 23628 KB Output is correct
84 Correct 429 ms 83456 KB Output is correct
85 Correct 442 ms 83448 KB Output is correct
86 Correct 422 ms 83584 KB Output is correct
87 Correct 419 ms 86140 KB Output is correct
88 Correct 451 ms 91668 KB Output is correct