Submission #1093844

#TimeUsernameProblemLanguageResultExecution timeMemory
1093844akim9905Telegraph (JOI16_telegraph)C++17
100 / 100
29 ms12112 KiB
#include <bits/stdc++.h> using namespace std; #define fileio() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout) #define fio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define all(x) (x).begin(), (x).end() #define allr(x) x.rbegin(), x.rend() #define cmprs(x) sort(all(x)),x.erase(unique(all(x)),x.end()) #define endl "\n" #define sp " " #define pb push_back #define lb lower_bound #define ub upper_bound #define rz resize #define sz(a) (int)(a.size()) #define clr(a) a.clear() typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, ll> pil; typedef tuple<int, int, int> tpi; typedef tuple<ll, ll, ll> tpl; typedef pair<double, ll> pdl; typedef pair<double, int> pdi; const int dx[] = {1,-1,0,0,1,1,-1,-1}; const int dy[] = {0,0,1,-1,1,-1,1,-1}; const ll MOD = 1e9+7; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3f; const int MAX = 101010; // PLZ CHK! int N,G[MAX],C[MAX],V[MAX],F[MAX],inc[MAX]; vector<int> IN[MAX]; vector<vector<int>> CY; void dfs(int cur, pii &c) { V[cur]=1; int nxt=G[cur]; if (V[nxt]) { if (!F[nxt]) c={nxt,cur}; } else dfs(nxt,c); F[cur]=1; } int main() { fio(); cin>>N; for (int i=1; i<=N; i++) { cin>>G[i]>>C[i]; IN[G[i]].pb(C[i]); } for (int i=1; i<=N; i++) { if (V[i]) continue; pii c={-1,-1}; dfs(i,c); if (c==pii(-1,-1)) continue; vector<int> cy; for (int x=c.first;; x=G[x]) { cy.pb(x); if (x==c.second) break; } for (int x:cy) inc[x]=1; CY.pb(cy); if (sz(cy)==N) { cout<<0; return 0; } } for (int i=1; i<=N; i++) sort(all(IN[i]),greater<>()); ll sum=0; for (int i=1; i<=N; i++) sum+=C[i]; for (int i=1; i<=N; i++) { if (inc[i]||IN[i].empty()) continue; sum-=IN[i][0]; } ll ans=0; for (auto cy:CY) { ll sum=0; for (int u:cy) sum+=IN[u][0]; ll mx=0; for (int u:cy) { int v=G[u]; ll t=sum; if (IN[v][0]==C[u]) { t-=C[u]; if (1<sz(IN[v])) t+=IN[v][1]; } mx=max(mx,t); } ans+=mx; } cout<<sum-ans<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...