#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e6;
int N;
vector<pll> adj[MAXN+10];
pll to[MAXN+10];
ll ans;
bool vis[MAXN+10], chk[MAXN+10];
ll dfs(int now, ll dep)
{
vis[now]=true;
ll ret=dep;
for(auto nxt : adj[now])
{
if(vis[nxt.first]) continue;
ret=max(ret, dfs(nxt.first, dep+nxt.second));
}
return ret;
}
int main()
{
int i, j;
scanf("%d", &N);
for(i=1; i<=N; i++)
{
int u=i, v, w;
scanf("%d%d", &v, &w);
to[u]={v, w};
adj[v].push_back({u, w});
}
for(i=1; i<=N; i++)
{
if(vis[i]) continue;
int now=i;
vector<int> cyc;
while(!chk[now])
{
cyc.push_back(now);
chk[now]=1;
now=to[now].first;
}
reverse(cyc.begin(), cyc.end());
while(!cyc.empty() && cyc.back()!=now) cyc.pop_back();
reverse(cyc.begin(), cyc.end());
//for(auto it : cyc) printf("%d ", it); printf("\n");
vector<ll> A, D;
ll val=0, s=0;
D.push_back(0);
for(auto it : cyc) vis[it]=1;
for(auto it : cyc) A.push_back(dfs(it, 0));
for(auto it : cyc) D.push_back(D.back()+to[it].second), s+=to[it].second;
D.pop_back();
for(auto it : A) val=max(val, it);
ll t=-1e18;
for(i=0; i<cyc.size(); i++)
{
if(i) val=max(val, D[i]+A[i]+t);
t=max(t, A[i]-D[i]);
}
t=-1e18;
for(i=0; i<cyc.size(); i++)
{
if(i) val=max(val, s-D[i]+A[i]+t);
t=max(t, D[i]+A[i]);
}
ans+=val;
}
printf("%lld", ans);
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:73:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<cyc.size(); i++)
~^~~~~~~~~~~
islands.cpp:80:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<cyc.size(); i++)
~^~~~~~~~~~~
islands.cpp:31:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
islands.cpp:33:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
islands.cpp:37:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &v, &w);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23800 KB |
Output is correct |
2 |
Incorrect |
20 ms |
23800 KB |
Output isn't correct |
3 |
Correct |
19 ms |
23800 KB |
Output is correct |
4 |
Correct |
19 ms |
23744 KB |
Output is correct |
5 |
Correct |
20 ms |
23800 KB |
Output is correct |
6 |
Correct |
19 ms |
23800 KB |
Output is correct |
7 |
Correct |
19 ms |
23800 KB |
Output is correct |
8 |
Correct |
19 ms |
23800 KB |
Output is correct |
9 |
Correct |
20 ms |
23804 KB |
Output is correct |
10 |
Incorrect |
20 ms |
23800 KB |
Output isn't correct |
11 |
Correct |
19 ms |
23800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23928 KB |
Output is correct |
2 |
Correct |
20 ms |
23932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23928 KB |
Output is correct |
2 |
Correct |
22 ms |
24056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
24696 KB |
Output is correct |
2 |
Correct |
37 ms |
26232 KB |
Output is correct |
3 |
Correct |
30 ms |
25148 KB |
Output is correct |
4 |
Incorrect |
25 ms |
24440 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
27252 KB |
Output is correct |
2 |
Correct |
63 ms |
30136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
35700 KB |
Output is correct |
2 |
Correct |
107 ms |
39544 KB |
Output is correct |
3 |
Correct |
133 ms |
43112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
184 ms |
47292 KB |
Output is correct |
2 |
Correct |
211 ms |
56144 KB |
Output is correct |
3 |
Correct |
228 ms |
65608 KB |
Output is correct |
4 |
Correct |
302 ms |
71388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
348 ms |
71812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2062 ms |
131072 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |