# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
155690 | TadijaSebez | Telegraph (JOI16_telegraph) | C++11 | 5 ms | 2808 KiB |
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>
using namespace std;
#define ll long long
#define pb push_back
const int N=100050;
vector<pair<int,int>> E[N];
int a[N],c[N],go[N];
bool was[N];
int main()
{
int n;
scanf("%i",&n);
ll sum=0,ans=0;
for(int i=1;i<=n;i++) scanf("%i %i",&a[i],&c[i]),E[a[i]].pb({c[i],i}),sum+=c[i];
for(int i=1;i<=n;i++)
{
sort(E[i].rbegin(),E[i].rend());
if(E[i].size()) go[E[i][0].second]=i,ans+=E[i][0].first;
}
int last=-1;
for(int i=1;i<=n;i++) if(!was[i])
{
int mx=-2e9,j;
for(j=i;j && !was[j];j=go[j])
{
int tmp=E[j].size()?-E[j][0].first:0;
if(E[j].size()>=2) tmp+=E[j][1].first;
mx=max(mx,tmp);
was[j]=1;
}
last=i;
if(j) ans+=mx;
}
if(last==1) printf("0\n");
else printf("%lld\n",sum-ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |