Submission #199081

#TimeUsernameProblemLanguageResultExecution timeMemory
199081dennisstarTelegraph (JOI16_telegraph)C++17
10 / 100
7 ms764 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define ryan bear
#define em emplace
#define eb emplace_back
#define sq(X) ((X)*(X))
#define all(V) (V).begin(), (V).end()
#define chk_init memset(chk, 0, sizeof(chk))
#define unq(V) (V).erase(unique(all(V)), (V).end())
using namespace std;
typedef long long ll;
typedef vector<ll> vlm;
typedef vector<int> vim;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

int N, A[100010], chk[100010];
ll C[100010], S, ans, M1[100010], M2[100010];

int main() {
	scanf("%d", &N);
	for (int i=1; i<=N; i++) scanf("%d %lld", &A[i], &C[i]), S+=C[i];

	for (int i=0, j=1; ; chk[j]=1, i++, j=A[j]) if (chk[j]) {
		if (i>=N) { puts("0"); return 0; }
		else break;
	}chk_init;

	for (int i=1; i<=N; i++) if (!chk[i]) {
		int j; for (j=i; !chk[j]; j=A[j]) chk[j]=i;
		if (chk[j]==i) for (; chk[j]!=-1; j=A[j]) chk[j]=-1;
	}
	for (int i=1; i<=N; i++) M1[A[i]]=max(M1[A[i]], C[i]);
	for (int i=1; i<=N; i++) if (chk[i]>0) M2[A[i]]=max(M2[A[i]], C[i]);
	for (int i=1; i<=N; i++) ans+=M1[i];
	ll mx;
	for (int i=1; i<=N; i++) if (chk[i]==-1) {
		mx=-(1ll<<60);
		for (int j=i; chk[j]; j=A[j]) {
			mx=max(mx, M2[j]-M1[j]);
			chk[j]++;
		}
		ans+=mx;
	}
	printf("%lld\n", S-ans);
	return 0;
}

Compilation message (stderr)

telegraph.cpp: In function 'int main()':
telegraph.cpp:22:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
telegraph.cpp:23:57: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i=1; i<=N; i++) scanf("%d %lld", &A[i], &C[i]), S+=C[i];
                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...