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 ll long long
#define pll pair<ll, ll>
#define fi first
#define se second
using namespace std;
const ll maxn=200005;
ll A[maxn], H[maxn], C[maxn], deg[maxn];
map<ll, ll> cnt[maxn];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n, sum=0; cin >> n;
for (ll i=1; i<=n; i++)
cin>>A[i]>>H[i]>>C[i], sum+=C[i], deg[A[i]]++;
queue <ll> q;
for (ll i=1; i<=n; i++)
if (!deg[i]) q.push(i);
auto Merge=[&](map<ll, ll> &a, map<ll, ll> &b)
{
if (a.size()<b.size()) swap(a, b);
for (pll i:b) a[i.fi]+=i.se;
};
auto Fix=[&](map<ll, ll> &a, ll H, ll C)
{
a[H]+=C; auto it=a.lower_bound(H);
while (it!=a.begin())
{
it--;
if ((*it).se>C) {(*it).se-=C; break;}
C-=(*it).se; it=a.erase(it);
}
};
while (!q.empty())
{
ll u=q.front(), crr=C[u]; q.pop();
deg[A[u]]--;
Fix(cnt[u], H[u], C[u]);
Merge(cnt[A[u]], cnt[u]);
if (!deg[A[u]]) q.push(A[u]);
}
for (ll i=1; i<=n; i++)
{
if (!deg[i]) continue;
map <ll, ll> all=cnt[i], cn; cn[H[i]]+=C[i];
for (ll crr=A[i]; crr!=i; crr=A[crr])
Merge(all, cnt[crr]);
for (pll j:cn) Fix(all, j.fi, j.se);
for (pll j:all) sum-=j.se;
}
cout << sum << "\n";
}
Compilation message (stderr)
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:38:25: warning: unused variable 'crr' [-Wunused-variable]
38 | ll u=q.front(), crr=C[u]; q.pop();
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |