Submission #404312

# Submission time Handle Problem Language Result Execution time Memory
404312 2021-05-14T07:25:03 Z jeqcho Fireworks (APIO16_fireworks) C++17
0 / 100
2000 ms 17472 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define fi first
#define se second

int const NM=3e2+3;
bitset<NM> bomb;
int v[NM];
vpi adj[NM];
int const pad=NM;
int dp[NM][NM][NM+pad];
bitset<NM+pad> vis[NM][NM];

void dfs1(int now, int w)
{
	if(bomb[now])
	{
		v[now] = w;
	}
	trav(chi,adj[now])
	{
		dfs1(chi.fi, w+chi.se);
	}
}

int dfs2(int now, int x, int chn, int w)
{
	if(vis[now][x][chn+pad])return dp[now][x][chn+pad];
	vis[now][x][chn+pad]=1;
	dp[now][x][chn+pad]=1e9;
	if(bomb[now])
	{
		int cost = x-(v[now]+chn);
		if(-cost>w)
		{
			cost=1e7;
		}
		cost=abs(cost);
		dp[now][x][chn+pad]=cost;
		return cost;
	}
	F0R(i,300+1)
	{
		int cost = abs(i-w);
		int chn2 = chn+i-w;
		if(chn2>x)break;
		int cand = cost;
		trav(chi,adj[now])
		{
			cand += dfs2(chi.fi,x,chn2,chi.se);
		}
		dp[now][x][chn+pad] = min(dp[now][x][chn+pad],cand);
	}
	return dp[now][x][chn+pad];
}

int to(int x)
{
	return dfs2(0,x,0,0);
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n,m;
	cin>>n>>m;
	FOR(i,1,n+m)
	{
		int p,c;
		cin>>p>>c;
		--p;
		adj[p].pb({i,c});
		if(i>=n)
		{
			bomb[i]=1;
		}
	}
	dfs1(0,0);
	int mn=1e9;
	int l=0, r=300+1;
	while(r-l>5)
	{
		int m1 = l+(r-l)/3;
		int m2 = r-(r-l)/3;
		int f1 = to(m1);
		int f2 = to(m2);
		if(f1<f2)
		{
			r=m2;
		}
		else
		{
			l=m1;
		}
	}
	FOR(i,l,r+1)
	{
		mn=min(to(i),mn);
	}
	cout<<mn<<'\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 460 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 47 ms 1488 KB Output is correct
2 Correct 323 ms 3856 KB Output is correct
3 Correct 431 ms 6008 KB Output is correct
4 Correct 388 ms 8892 KB Output is correct
5 Correct 1584 ms 12508 KB Output is correct
6 Correct 1940 ms 14004 KB Output is correct
7 Execution timed out 2082 ms 17472 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 460 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 460 KB Output isn't correct
2 Halted 0 ms 0 KB -