#include <bits/stdc++.h>
#define ll long long
#define db long double
#define N 1000005
#define II pair <int,int>
#define III pair <ll,II>
#define IV pair <vector <int>,vector <int> >
#define fst first
#define snd second
#define BIT(x,i) ((x>>i)&1)
#define Log(x) (31-__builtin_clz((int)x))
#define LogLL(x) (63-__builtin_clzll((ll)x))
#define pi acos(-1)
#define to_radian(x) (x*pi/180.0)
#define to_degree(x) (x*180.0/pi)
using namespace std;
vector <int> a[N];
int nxt[N],w[N],deg[N];
bool visited[N];
ll n,i,u,v,k,sum,res;
ll f[N],ma0[N],ma1[N];
const ll oo=round(1e18);
int main()
{
// freopen("islands.inp","r",stdin);
//freopen("islands.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
for(u=1;u<=n;u++)
{
cin>>v>>k;
nxt[u]=v; a[v].push_back(u); deg[v]++;
w[u]=k;
}
for(u=1;u<=n;u++)
if(visited[u]==0)
{
queue <int> q;
vector <int> node;
q.push(u); visited[u]=1;
while(q.size()>0)
{
int u=q.front(); q.pop();
node.push_back(u);
for(int v:a[u]) if(visited[v]==0) q.push(v),visited[v]=1;
if(visited[nxt[u]]==0) q.push(nxt[u]),visited[nxt[u]]=1;
}
for(int u:node)
if(deg[u]==0) q.push(u);
while(q.size()>0)
{
int u=q.front(),v=nxt[u]; q.pop();
ll k=f[u]+w[u];
f[v]=max(f[v],k);
if(ma0[v]<k) ma0[v]=k;
if(ma1[v]<ma0[v]) swap(ma0[v],ma1[v]);
deg[v]--;
if(deg[v]==0) q.push(v);
}
int x=0,u;
for(int u:node)
if(deg[u]>0) { x=u; break; }
node.clear();
///////////////////////////////////////////////////////////////////
ll local_res=0,ncycle=0,ma=-oo;
vector <ll> c,dis; c.push_back(0); dis.push_back(0);
u=x;
ll sum=0;
ncycle++;
c.push_back(u); dis.push_back(sum);
sum+=w[u]; u=nxt[u];
while(u!=x)
{
ncycle++;
c.push_back(u); dis.push_back(sum);
sum+=w[u]; u=nxt[u];
}
for(int u:c) local_res=max(local_res,ma0[u]+ma1[u]);
////////////////////////////////////////////////////////////////////////////////
for(i=1;i<=ncycle;i++)
{
u=c[i];
c[i]=f[u];
}
deque <int> dq;
int j=1;
for(i=1;i<ncycle;i++)
{
while(j<ncycle && sum-dis[j+1]+dis[i]>=dis[j+1]-dis[i])
{
j++;
while(dq.size()>0 && c[dq.back()]-dis[dq.back()]<c[j]-dis[j])
dq.pop_back();
dq.push_back(j);
}
if(dq.size()>0 && dq.front()==i) dq.pop_front();
if(dq.size()>0) local_res=max(local_res,c[i]+c[dq.front()]+sum+dis[i]-dis[dq.front()]);
}
j=ncycle+1;
for(i=ncycle-1;i>=1;i--)
{
while(j-1>i && sum-dis[j-1]+dis[i]<dis[j-1]-dis[i])
{
j--;
ma=max(ma,c[j]+dis[j]);
}
local_res=max(local_res,c[i]-dis[i]+ma);
}
res+=local_res;
}
cout<<res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31064 KB |
Output is correct |
2 |
Incorrect |
6 ms |
31068 KB |
Output isn't correct |
3 |
Correct |
6 ms |
31068 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
6 ms |
31268 KB |
Output is correct |
6 |
Correct |
7 ms |
31068 KB |
Output is correct |
7 |
Correct |
6 ms |
31068 KB |
Output is correct |
8 |
Correct |
6 ms |
31192 KB |
Output is correct |
9 |
Correct |
6 ms |
31068 KB |
Output is correct |
10 |
Correct |
6 ms |
31068 KB |
Output is correct |
11 |
Correct |
6 ms |
31260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31324 KB |
Output is correct |
2 |
Correct |
6 ms |
31308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31320 KB |
Output is correct |
2 |
Correct |
7 ms |
31500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
31836 KB |
Output is correct |
2 |
Correct |
15 ms |
33196 KB |
Output is correct |
3 |
Correct |
12 ms |
32348 KB |
Output is correct |
4 |
Incorrect |
9 ms |
31580 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
34232 KB |
Output is correct |
2 |
Correct |
27 ms |
37428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
47000 KB |
Output is correct |
2 |
Correct |
59 ms |
49664 KB |
Output is correct |
3 |
Correct |
65 ms |
53188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
53832 KB |
Output is correct |
2 |
Correct |
113 ms |
64600 KB |
Output is correct |
3 |
Correct |
128 ms |
67684 KB |
Output is correct |
4 |
Correct |
148 ms |
79464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
51220 KB |
Output is correct |
2 |
Correct |
372 ms |
81456 KB |
Output is correct |
3 |
Correct |
174 ms |
69660 KB |
Output is correct |
4 |
Correct |
225 ms |
84136 KB |
Output is correct |
5 |
Correct |
212 ms |
82876 KB |
Output is correct |
6 |
Incorrect |
556 ms |
83072 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
216 ms |
110780 KB |
Output is correct |
2 |
Incorrect |
208 ms |
110872 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |