#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
using namespace std;
///~~~~~~~~~~~~~~~~~~~~~~~~~~
template <typename H, typename T>
ostream& operator<<(ostream& os, pair<H, T> m){
return os <<"("<< m.st<<", "<<m.nd<<")";
}
template <typename H>
ostream& operator<<(ostream& os, vector<H> V){
os<<"{";
for(int i=0; i<V.size(); i++){
if(i)os<<" ";
os<<V[i];
}
os<<"}";
return os;
}
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);
//#define deb(x...) ;
///~~~~~~~~~~~~~~~~~~~~~~~~~
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1<<19, INF=1e9+5, mod=1e9+7, mod2=998244353;
struct modint{
int n=0;
modint(){}
modint(ll x){
n=x%mod;
if(n<0)n+=mod;
}
operator int(){
return n;
}
modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;}
modint operator+=(modint a){return (*this)=(*this)+a;}
modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;}
modint operator-=(modint a){return (*this)=(*this)-a;}
modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;}
modint operator*=(modint a){return (*this)=(*this)*a;}
modint operator^(const ll &m)const{
modint a(1);
if(m==0)return a;
if(m==1)return (*this);
a=(*this)^(m/2);
a*=a;
return a*((*this)^(m&1));
}
modint odw(){
return (*this)^((ll)mod-2);
}
modint operator/(modint a){return (*this)*a.odw();}
modint operator/=(modint a){return (*this)=(*this)/a;}
bool operator==(modint a){return a.n==n;}
friend ostream& operator<<(ostream& os, modint m) {
return os << m.n;
}
};
modint fact[N], fact2[N];
typedef vector<modint> vm;
void factinit(){
fact[0]=1;
for(int i=1; i<N; i++){
fact[i]=(fact[i-1]*modint(i))%mod;
}
fact2[N-1]=fact[N-1].odw();
for(int i=N-2; i>=0; i--){
fact2[i]=(fact2[i+1]*modint(i+1))%mod;
}
}
modint npok(int _n, int _k){
return fact[_n]*fact2[_k]*fact2[_n-_k];
}
int deg[N], A[N], H[N], C[N], czy[N];
vi E[N];
struct node{
node *l=nullptr,*r=nullptr;
ll val=0, lazy=0;
node(){};
void prop(){
//deb(val, lazy, l, r);
if(l){
assert(l);
l->val=max(l->val+lazy, val);
l->lazy+=lazy;
}
if(r){
r->val=max(r->val+lazy, val);
r->lazy+=lazy;
}
lazy=0;
}
};
node* merge(node*u, node*v){
if(!u)return v;
if(!v)return u;
if(!u->l){
v->val+=u->val;
v->lazy+=u->val;
delete u;
return v;
}
if(!v->l){
u->val+=v->val;
u->lazy+=v->val;
delete v;
return u;
}
u->prop();
v->prop();
u->l=merge(u->l, v->l);
u->r=merge(u->r, v->r);
delete v;
return u;
}
void add(node* v, int L, int R, int l, int r, ll c){
if(l==L && r==R){
v->val=max(c, v->val);
return;
}
if(!v->l){
v->l=(new node);
v->r=(new node);
}
v->prop();
int M=(L+R)>>1;
if(l<=M)add(v->l, L, M, l, min(M,r), c);
if(r>M)add(v->r, M+1, R, max(l, M+1), r, c);
return;
}
ll get_val(node* v, int L, int R, int t){
if(!v->l)return v->val;
v->prop();
int M=(L+R)>>1;
if(t<=M)return max(v->val, get_val(v->l, L, M, t));
else return max(v->val, get_val(v->r, M+1, R, t));
}
node* dfs(int v){
//deb(v);
node* root=0;
ll sum=C[v];
for(int u:E[v]){
node *w=dfs(u);
assert(w);
sum+=get_val(w, 0, N-1, H[v]);
root=merge(root, w);
}
if(!root)root=new node;
add(root, 0, N-1, 0, H[v], sum);
return root;
}
int main(){
//factinit();
//BOOST;
int n;
cin>>n;
vector<int> ska;
ll sum=0;
for(int i=1; i<=n; i++){
cin>>A[i]>>H[i]>>C[i];
deg[A[i]]++;
ska.pb(H[i]);
sum+=C[i];
}
sor(ska);
ska.resize(unique(all(ska))- ska.begin());
for(int i=1; i<=n; i++){
H[i]=lower_bound(all(ska), H[i])-ska.begin();
}
vi V;
for(int i=1; i<=n; i++){
if(deg[i]==0){
V.pb(i);
}
}
for(int i=0; i<V.size(); i++){
int v=V[i];
if(!--deg[A[v]]){
V.pb(A[v]);
}
E[A[v]].pb(v);
}
vector<vi> cykle, co;
vector<map<int, ll> > M;
for(int i=0; i<n; i++){
if(deg[i]){
cykle.pb({});
co.pb({});
M.resize(M.size()+1);
for(int j=A[i]; deg[j]; j=A[j]){
deg[j]=0;
M[M.size()-1][H[j]]+=C[j];
cykle.back().pb(j);
for(int u:E[j]){
co.back().pb(u);
}
}
}
}
//deb(V);
ll ans=0;
for(int i=0; i<cykle.size(); i++){
node* root=0;
for(int j:co[i]){
root=merge(root, dfs(j));
}
ll part=0;
for(auto j:M[i]){
if(root)part=max(part, get_val(root, 0, N-1, j.st)+j.nd);
else part=max(part, j.nd);
}
if(root)part=max(part, get_val(root, 0, N-1, 0));
ans+=part;
}
cout<<sum-ans;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:220:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
220 | for(int i=0; i<V.size(); i++){
| ~^~~~~~~~~
worst_reporter2.cpp:246:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
246 | for(int i=0; i<cykle.size(); i++){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16680 KB |
Output is correct |
3 |
Correct |
10 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
22 ms |
18136 KB |
Output is correct |
6 |
Correct |
19 ms |
17124 KB |
Output is correct |
7 |
Correct |
17 ms |
17120 KB |
Output is correct |
8 |
Correct |
20 ms |
18012 KB |
Output is correct |
9 |
Correct |
18 ms |
17080 KB |
Output is correct |
10 |
Correct |
19 ms |
17120 KB |
Output is correct |
11 |
Correct |
17 ms |
17004 KB |
Output is correct |
12 |
Correct |
16 ms |
17884 KB |
Output is correct |
13 |
Correct |
15 ms |
17412 KB |
Output is correct |
14 |
Correct |
19 ms |
17924 KB |
Output is correct |
15 |
Correct |
15 ms |
17236 KB |
Output is correct |
16 |
Correct |
20 ms |
18008 KB |
Output is correct |
17 |
Correct |
18 ms |
17108 KB |
Output is correct |
18 |
Correct |
17 ms |
17048 KB |
Output is correct |
19 |
Correct |
22 ms |
21472 KB |
Output is correct |
20 |
Correct |
28 ms |
21452 KB |
Output is correct |
21 |
Correct |
28 ms |
21640 KB |
Output is correct |
22 |
Correct |
21 ms |
17492 KB |
Output is correct |
23 |
Correct |
20 ms |
16980 KB |
Output is correct |
24 |
Correct |
21 ms |
17672 KB |
Output is correct |
25 |
Correct |
17 ms |
17248 KB |
Output is correct |
26 |
Correct |
16 ms |
17860 KB |
Output is correct |
27 |
Correct |
21 ms |
20544 KB |
Output is correct |
28 |
Correct |
19 ms |
20168 KB |
Output is correct |
29 |
Correct |
17 ms |
19028 KB |
Output is correct |
30 |
Correct |
17 ms |
17732 KB |
Output is correct |
31 |
Correct |
15 ms |
17748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16680 KB |
Output is correct |
3 |
Correct |
10 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
22 ms |
18136 KB |
Output is correct |
6 |
Correct |
19 ms |
17124 KB |
Output is correct |
7 |
Correct |
17 ms |
17120 KB |
Output is correct |
8 |
Correct |
20 ms |
18012 KB |
Output is correct |
9 |
Correct |
18 ms |
17080 KB |
Output is correct |
10 |
Correct |
19 ms |
17120 KB |
Output is correct |
11 |
Correct |
17 ms |
17004 KB |
Output is correct |
12 |
Correct |
16 ms |
17884 KB |
Output is correct |
13 |
Correct |
15 ms |
17412 KB |
Output is correct |
14 |
Correct |
19 ms |
17924 KB |
Output is correct |
15 |
Correct |
15 ms |
17236 KB |
Output is correct |
16 |
Correct |
20 ms |
18008 KB |
Output is correct |
17 |
Correct |
18 ms |
17108 KB |
Output is correct |
18 |
Correct |
17 ms |
17048 KB |
Output is correct |
19 |
Correct |
22 ms |
21472 KB |
Output is correct |
20 |
Correct |
28 ms |
21452 KB |
Output is correct |
21 |
Correct |
28 ms |
21640 KB |
Output is correct |
22 |
Correct |
21 ms |
17492 KB |
Output is correct |
23 |
Correct |
20 ms |
16980 KB |
Output is correct |
24 |
Correct |
21 ms |
17672 KB |
Output is correct |
25 |
Correct |
17 ms |
17248 KB |
Output is correct |
26 |
Correct |
16 ms |
17860 KB |
Output is correct |
27 |
Correct |
21 ms |
20544 KB |
Output is correct |
28 |
Correct |
19 ms |
20168 KB |
Output is correct |
29 |
Correct |
17 ms |
19028 KB |
Output is correct |
30 |
Correct |
17 ms |
17732 KB |
Output is correct |
31 |
Correct |
15 ms |
17748 KB |
Output is correct |
32 |
Correct |
22 ms |
18004 KB |
Output is correct |
33 |
Correct |
852 ms |
66740 KB |
Output is correct |
34 |
Correct |
493 ms |
27076 KB |
Output is correct |
35 |
Correct |
840 ms |
67936 KB |
Output is correct |
36 |
Correct |
493 ms |
27068 KB |
Output is correct |
37 |
Correct |
435 ms |
26036 KB |
Output is correct |
38 |
Correct |
407 ms |
26016 KB |
Output is correct |
39 |
Correct |
393 ms |
60092 KB |
Output is correct |
40 |
Correct |
298 ms |
41452 KB |
Output is correct |
41 |
Correct |
285 ms |
41464 KB |
Output is correct |
42 |
Correct |
401 ms |
62040 KB |
Output is correct |
43 |
Correct |
305 ms |
35300 KB |
Output is correct |
44 |
Correct |
782 ms |
62160 KB |
Output is correct |
45 |
Correct |
444 ms |
26156 KB |
Output is correct |
46 |
Correct |
362 ms |
25360 KB |
Output is correct |
47 |
Correct |
784 ms |
205348 KB |
Output is correct |
48 |
Correct |
596 ms |
205476 KB |
Output is correct |
49 |
Correct |
591 ms |
209140 KB |
Output is correct |
50 |
Correct |
508 ms |
42044 KB |
Output is correct |
51 |
Correct |
439 ms |
24160 KB |
Output is correct |
52 |
Correct |
465 ms |
50752 KB |
Output is correct |
53 |
Correct |
386 ms |
32064 KB |
Output is correct |
54 |
Correct |
316 ms |
60104 KB |
Output is correct |
55 |
Correct |
615 ms |
181864 KB |
Output is correct |
56 |
Correct |
451 ms |
121316 KB |
Output is correct |
57 |
Correct |
383 ms |
90156 KB |
Output is correct |
58 |
Correct |
324 ms |
53824 KB |
Output is correct |
59 |
Correct |
341 ms |
53892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16680 KB |
Output is correct |
3 |
Correct |
10 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
22 ms |
18136 KB |
Output is correct |
6 |
Correct |
19 ms |
17124 KB |
Output is correct |
7 |
Correct |
17 ms |
17120 KB |
Output is correct |
8 |
Correct |
20 ms |
18012 KB |
Output is correct |
9 |
Correct |
18 ms |
17080 KB |
Output is correct |
10 |
Correct |
19 ms |
17120 KB |
Output is correct |
11 |
Correct |
17 ms |
17004 KB |
Output is correct |
12 |
Correct |
16 ms |
17884 KB |
Output is correct |
13 |
Correct |
15 ms |
17412 KB |
Output is correct |
14 |
Correct |
19 ms |
17924 KB |
Output is correct |
15 |
Correct |
15 ms |
17236 KB |
Output is correct |
16 |
Correct |
20 ms |
18008 KB |
Output is correct |
17 |
Correct |
18 ms |
17108 KB |
Output is correct |
18 |
Correct |
17 ms |
17048 KB |
Output is correct |
19 |
Correct |
22 ms |
21472 KB |
Output is correct |
20 |
Correct |
28 ms |
21452 KB |
Output is correct |
21 |
Correct |
28 ms |
21640 KB |
Output is correct |
22 |
Correct |
21 ms |
17492 KB |
Output is correct |
23 |
Correct |
20 ms |
16980 KB |
Output is correct |
24 |
Correct |
21 ms |
17672 KB |
Output is correct |
25 |
Correct |
17 ms |
17248 KB |
Output is correct |
26 |
Correct |
16 ms |
17860 KB |
Output is correct |
27 |
Correct |
21 ms |
20544 KB |
Output is correct |
28 |
Correct |
19 ms |
20168 KB |
Output is correct |
29 |
Correct |
17 ms |
19028 KB |
Output is correct |
30 |
Correct |
17 ms |
17732 KB |
Output is correct |
31 |
Correct |
15 ms |
17748 KB |
Output is correct |
32 |
Correct |
22 ms |
18004 KB |
Output is correct |
33 |
Correct |
852 ms |
66740 KB |
Output is correct |
34 |
Correct |
493 ms |
27076 KB |
Output is correct |
35 |
Correct |
840 ms |
67936 KB |
Output is correct |
36 |
Correct |
493 ms |
27068 KB |
Output is correct |
37 |
Correct |
435 ms |
26036 KB |
Output is correct |
38 |
Correct |
407 ms |
26016 KB |
Output is correct |
39 |
Correct |
393 ms |
60092 KB |
Output is correct |
40 |
Correct |
298 ms |
41452 KB |
Output is correct |
41 |
Correct |
285 ms |
41464 KB |
Output is correct |
42 |
Correct |
401 ms |
62040 KB |
Output is correct |
43 |
Correct |
305 ms |
35300 KB |
Output is correct |
44 |
Correct |
782 ms |
62160 KB |
Output is correct |
45 |
Correct |
444 ms |
26156 KB |
Output is correct |
46 |
Correct |
362 ms |
25360 KB |
Output is correct |
47 |
Correct |
784 ms |
205348 KB |
Output is correct |
48 |
Correct |
596 ms |
205476 KB |
Output is correct |
49 |
Correct |
591 ms |
209140 KB |
Output is correct |
50 |
Correct |
508 ms |
42044 KB |
Output is correct |
51 |
Correct |
439 ms |
24160 KB |
Output is correct |
52 |
Correct |
465 ms |
50752 KB |
Output is correct |
53 |
Correct |
386 ms |
32064 KB |
Output is correct |
54 |
Correct |
316 ms |
60104 KB |
Output is correct |
55 |
Correct |
615 ms |
181864 KB |
Output is correct |
56 |
Correct |
451 ms |
121316 KB |
Output is correct |
57 |
Correct |
383 ms |
90156 KB |
Output is correct |
58 |
Correct |
324 ms |
53824 KB |
Output is correct |
59 |
Correct |
341 ms |
53892 KB |
Output is correct |
60 |
Correct |
8 ms |
16724 KB |
Output is correct |
61 |
Correct |
10 ms |
16724 KB |
Output is correct |
62 |
Incorrect |
9 ms |
16724 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |