This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define COMPRESS(a) sort(ALL(a));a.resize(unique(ALL(a))-a.begin())
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<ld> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 1000000007;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };
Vi v[200001];
int h[200001],pt[200001];
ll c[200001];
map<int,ll,greater<int>> M[200001];
void dfs(int N){
if(v[N].empty()){
M[N][h[N]]=c[N];
return;
}
for(int next:v[N]){
dfs(next);
int &x=pt[next],&y=pt[N];
if(M[x].size()>M[y].size())swap(x,y);
for(auto [a,b]:M[x]){
M[y][a]+=b;
}
}
int nn=pt[N];
M[nn][h[N]]+=c[N];
ll t=c[N];
auto it=M[nn].upper_bound(h[N]);
while(it!=M[nn].end()){
if(it->Y<t){
t-=it->Y;
it=M[nn].erase(it);
}else{
it->Y-=t;
break;
}
}
}
int main(){
int n;
scanf("%d",&n);
iota(pt,pt+n+1,0);
Vi vv={0};
ll tot=0;
fup(i,1,n,1){
int x;
scanf("%d%d%lld",&x,&h[i],&c[i]);
tot+=c[i];
if(i!=x)v[x].pb(i);
vv.pb(h[i]);
}
COMPRESS(vv);
fup(i,1,n,1)h[i]=lower_bound(ALL(vv),h[i])-vv.begin();
dfs(1);
ll ans=0;
for(auto [a,b]:M[pt[1]])ans+=b;
printf("%lld\n",tot-ans);
}
Compilation message (stderr)
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
worst_reporter2.cpp:80:2: note: in expansion of macro 'fup'
80 | fup(i,1,n,1){
| ^~~
worst_reporter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
worst_reporter2.cpp:88:2: note: in expansion of macro 'fup'
88 | fup(i,1,n,1)h[i]=lower_bound(ALL(vv),h[i])-vv.begin();
| ^~~
worst_reporter2.cpp:76:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
worst_reporter2.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%d%d%lld",&x,&h[i],&c[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |