#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
int n;
const int nax = 4000;
int cnt = 1;
int vis[nax+5];
int c[nax+5]; //cost
long long val[nax+5];
vector<pii> adj[nax+5];
vector<pii> edg;
vector<int> cyc;
void gp(int u, int ls=0) {
vis[u] = cnt;
for (auto t : adj[u]) {
int v = t.fi;
int e = t.se;
if(e==ls) continue;
if(vis[v] != cnt) gp(v, e);
else {
cyc.push_back(e);
}
}
}
bool fc(int u, int go, int ls) {
if(u == go) return true;
for(auto t : adj[u]) {
int v = t.fi;
int e = t.se;
if(e == ls) continue;
bool ok = fc(v, go, e);
if(ok) {
vis[u] = cnt;
vis[v] = cnt;
if(edg[e].fi == u) swap(edg[e].fi, edg[e].se);
cyc.push_back(e);
return true;
}
}
return false;
}
long long nono = 0;
long long trav(int u) {
if(val[u] != -1) return val[u];
vis[u] = cnt;
priority_queue<long long, vector<long long>, greater<long long>> pq;
for(pii t : adj[u]) {
int v = t.fi;
int e = t.se;
if(vis[v] != cnt) {
pq.push(1ll*c[e] + trav(v));
}
while(pq.size() > 2) pq.pop();
}
if(pq.size() == 2) {
long long tmp = pq.top();
pq.pop();
nono = max(nono, tmp + pq.top());
}
return val[u] = pq.empty() ? 0 : pq.top();
}
signed main () {
scanf("%d", &n);
edg.pb({0,0});
for(int i=1; i<=n; i++) {
long long l, j;
scanf("%lld %lld", &j, &l);
c[i] = l;
val[i] = -1;
adj[j].pb({i,i});
adj[i].pb({j,i});
edg.pb({i,j});
}
long long ans = 0;
for(int i=1; i<=n; i++) {
if(!vis[i]) {
cnt++;
nono = 0;
cyc.clear();
gp(i);
cyc.pop_back();
cnt++;
fc(edg[cyc[0]].se, edg[cyc[0]].fi, cyc[0]);
swap(edg[cyc[0]].fi, edg[cyc[0]].se);
int m = cyc.size();
for(int j=0; j<m; j++) {
if(edg[cyc[j]].fi != edg[cyc[(j-1+m) % m]].se) swap(edg[cyc[j]].fi, edg[cyc[j]].se);
val[edg[cyc[j]].fi] = trav(edg[cyc[j]].fi);
}
long long opt = 0;
deque<pair<long long, int>> dq;
long long dif = 0;
long long sum = 0;
int cc = 0;
for(int j=0; j<m; j++) {
int e = cyc[j];
sum += 1ll*c[e];
if(j+1 < m) {
long long temp = sum + trav(edg[e].se);
while(!dq.empty() && dq.back().fi <= temp) dq.pop_back();
dq.push_back({temp, cc++});
}
}
for(int j=0; j<m; j++) {
int e = cyc[j];
int u = edg[e].fi;
opt = max(opt , trav(u) + dq.front().fi + dif);
dif -= 1ll*c[e];
long long temp = trav(u) + sum - 1ll*c[e] - dif;
while(!dq.empty() && dq.front().se <= cc-m) dq.pop_front();
while(!dq.empty() && dq.back().fi <= temp) dq.pop_back();
dq.push_back({temp, cc++});
}
ans += max(opt,nono);
}
}
printf("%lld", ans);
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
islands.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | scanf("%lld %lld", &j, &l);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
396 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
396 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
396 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
664 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
596 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
596 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
596 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
596 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
668 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |