Submission #49893

# Submission time Handle Problem Language Result Execution time Memory
49893 2018-06-04T12:23:29 Z hamzqq9 Fireworks (APIO16_fireworks) C++14
0 / 100
4 ms 712 KB
#include<bits/stdc++.h>
#define lf double
#define ll long long
#define cc pair<char,char>
#define ull unsigned ll
#define ii pair<int,int>
#define li pair<ll,int>
#define iii pair<int,ii>
#define iiii pair<ii,ii>
#define iiii2 pair<int,iii>
#define lii pair<ll,ii>
#define lolo pair<ll,ll>
#define heap priority_queue
#define mp make_pair
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define all(x) x.begin(),x.end()
#define len(x) strlen(x)
#define sz(x) (int) x.size()
#define orta ((bas+son)/2)
#define min3(x,y,z) min(min(x,y),z)
#define max3(x,y,z) max(max(x,y),z)
#define umin(x,y) x=min(x,y)
#define umax(x,y) x=max(x,y)
#define dbgs(x) cerr<<(#x)<<" --> "<<(x)<<" "
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<endl;getchar()
#define MOD 1000000007
#define inf 100000000
#define M 10000002
#define N 5005
#define LOG 20
#define magic 100
#define KOK 250
#define EPS 0.0025
#define pw(x) ((1ll)<<(x))
#define PI 3.1415926535
using namespace std;

ll ans=inf,mn[N],mx[N];
int n,m,x,y,cst[N];
vector<ii> v[N];

ll solve(int node,int cost,int lazy=0) {

	ll res=0;

	if(node>1) {

		if(mx[node]+lazy<cost) {

			res+=cost-mx[node]-lazy;

			lazy+=cost-mx[node]-lazy;

		}
		else if(mn[node]+lazy>cost) {

			res+=mn[node]+lazy-cost;

			lazy-=mn[node]+lazy-cost;

		}
	
	}

	for(ii i:v[node]) {

		res+=solve(i.st,cost,lazy);

	}

	return res;

}

void dfs1(int node,int sum=0) {

	if(node>n) mn[node]=mx[node]=sum;

	for(ii i:v[node]) {

		dfs1(i.st,sum+i.nd);

		umin(mn[node],mn[i.st]);
		umax(mx[node],mx[i.st]);

	}

}

int main() {
 
 	#if 0
	freopen("input.txt","r",stdin);
 	#endif

 	fill(mn,mn+N,inf);
 	fill(mx,mx+N,-inf);

	scanf("%d %d",&n,&m);

	for(int i=2;i<=n;i++) {

		scanf("%d %d",&x,&y);

		v[x].pb({i,y});

	}

	for(int i=1;i<=m;i++) {

		scanf("%d %d",&x,&y);

		cst[i+n]=y;

		v[x].pb({i+n,y});

	}

	dfs1(1);

	for(int i=n+1;i<=n+m;i++) {

		umin(ans,solve(1,mn[i]));

	}

	printf("%lld",ans);

}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:104:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
  ~~~~~^~~~~~~~~~~~~~~
fireworks.cpp:108:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&x,&y);
   ~~~~~^~~~~~~~~~~~~~~
fireworks.cpp:116:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&x,&y);
   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 504 KB Output is correct
2 Incorrect 4 ms 624 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 680 KB Output is correct
2 Incorrect 3 ms 712 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 504 KB Output is correct
2 Incorrect 4 ms 624 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 504 KB Output is correct
2 Incorrect 4 ms 624 KB Output isn't correct
3 Halted 0 ms 0 KB -