답안 #793315

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
793315 2023-07-25T17:47:14 Z ToniB Fireworks (APIO16_fireworks) C++17
7 / 100
2 ms 860 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e14;
const int MAXN = 5005;

int n, m, L[MAXN], R[MAXN], perm[MAXN];
vector<pair<int, int> > adj[MAXN];
ll walk[MAXN], dp[MAXN][MAXN];

void precalc(int node){
	for(pair<int, int> x : adj[node]){
		walk[x.first] = walk[node] + x.second;
		precalc(x.first);
	}
}

void dfs(int node){
	if(node >= n){
		for(int i = n; i < n + m; ++i) dp[node][i] = abs(walk[perm[i]] - walk[node]);
	}
	for(pair<int, int> x : adj[node]){
		dfs(x.first);

		multiset<ll> lo, hi;
		vector<ll> uk (n + m);
		for(int i = n; i < n + m; ++i){
			lo.insert(dp[x.first][i] - walk[perm[i]]);
			uk[i] = walk[perm[i]] + *lo.begin();
		}

		int p = n + m - 1;
		for(int i = n + m - 1; i >= n; --i){
			hi.insert(dp[x.first][i] + walk[perm[i]]);

			while(p > i && x.second + walk[i] - walk[perm[p]] < 0){
				assert(hi.find(dp[x.first][p] + walk[perm[p]]) != hi.end());
				hi.erase(hi.find(dp[x.first][p] + walk[perm[p]]));
				--p;
			}
			uk[i] = min(uk[i], *hi.begin() - walk[perm[i]]);
		}
		
		for(int i = n; i < n + m; ++i){
			dp[node][i] += uk[i];
		}
	}
}

int main(){
	cin >> n >> m;
	for(int i = 1; i < n + m; ++i){
		int p, c;
		cin >> p >> c, --p;
		adj[p].push_back({i, c});
	}
	precalc(0);
	vector<pair<ll, int> > walks;
	for(int i = n; i < n + m; ++i) walks.push_back({walk[i], i});
	sort(walks.begin(), walks.end());
	for(int i = n; i < n + m; ++i) perm[i] = walks[i - n].second;

	dfs(0);

	ll ans = inf;
	for(int i = n; i < n + m; ++i) ans = min(ans, dp[0][i]);

	cout << ans;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 2 ms 852 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 2 ms 852 KB Output is correct
6 Correct 2 ms 852 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 1 ms 852 KB Output is correct
10 Correct 2 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 428 KB Output is correct
2 Incorrect 1 ms 596 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 2 ms 852 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 2 ms 852 KB Output is correct
6 Correct 2 ms 852 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 1 ms 852 KB Output is correct
10 Correct 2 ms 852 KB Output is correct
11 Correct 1 ms 428 KB Output is correct
12 Incorrect 1 ms 596 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 2 ms 852 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 2 ms 852 KB Output is correct
6 Correct 2 ms 852 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 1 ms 852 KB Output is correct
10 Correct 2 ms 852 KB Output is correct
11 Correct 1 ms 428 KB Output is correct
12 Incorrect 1 ms 596 KB Output isn't correct
13 Halted 0 ms 0 KB -