답안 #793335

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
793335 2023-07-25T17:59:41 Z ToniB Fireworks (APIO16_fireworks) C++17
26 / 100
39 ms 4016 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 5005;

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

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

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

		multiset<ll> lo, hi;
		vector<ll> uk (n + m);

		for(ll i = 0; i < n + m; ++i){
			lo.insert(dp[x.first][i] - walk[perm[i]]);
			uk[i] = walk[perm[i]] + *lo.begin();
			if(i != n + m - 1) assert(walk[perm[i]] <= walk[perm[i + 1]]);
		}

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

			while(p > i && x.second + walk[perm[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(ll i = 0; i < n + m; ++i){
			dp[node][i] += uk[i];
		}
	}
}

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

	dfs(0);

	ll ans = dp[0][0];
	for(ll i = 1; 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 0 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 1 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 852 KB Output is correct
9 Correct 1 ms 852 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 2 ms 980 KB Output is correct
6 Correct 4 ms 1044 KB Output is correct
7 Correct 5 ms 1236 KB Output is correct
8 Correct 7 ms 1308 KB Output is correct
9 Correct 7 ms 1508 KB Output is correct
10 Correct 9 ms 1668 KB Output is correct
11 Correct 11 ms 1748 KB Output is correct
12 Correct 13 ms 2020 KB Output is correct
13 Correct 14 ms 2096 KB Output is correct
14 Correct 10 ms 2380 KB Output is correct
15 Correct 18 ms 2348 KB Output is correct
16 Correct 18 ms 2376 KB Output is correct
17 Correct 17 ms 2388 KB Output is correct
18 Correct 17 ms 2360 KB Output is correct
19 Correct 20 ms 2264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 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 1 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 852 KB Output is correct
9 Correct 1 ms 852 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 852 KB Output is correct
15 Correct 2 ms 980 KB Output is correct
16 Correct 4 ms 1044 KB Output is correct
17 Correct 5 ms 1236 KB Output is correct
18 Correct 7 ms 1308 KB Output is correct
19 Correct 7 ms 1508 KB Output is correct
20 Correct 9 ms 1668 KB Output is correct
21 Correct 11 ms 1748 KB Output is correct
22 Correct 13 ms 2020 KB Output is correct
23 Correct 14 ms 2096 KB Output is correct
24 Correct 10 ms 2380 KB Output is correct
25 Correct 18 ms 2348 KB Output is correct
26 Correct 18 ms 2376 KB Output is correct
27 Correct 17 ms 2388 KB Output is correct
28 Correct 17 ms 2360 KB Output is correct
29 Correct 20 ms 2264 KB Output is correct
30 Incorrect 39 ms 4016 KB Output isn't correct
31 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 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 1 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 852 KB Output is correct
9 Correct 1 ms 852 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 852 KB Output is correct
15 Correct 2 ms 980 KB Output is correct
16 Correct 4 ms 1044 KB Output is correct
17 Correct 5 ms 1236 KB Output is correct
18 Correct 7 ms 1308 KB Output is correct
19 Correct 7 ms 1508 KB Output is correct
20 Correct 9 ms 1668 KB Output is correct
21 Correct 11 ms 1748 KB Output is correct
22 Correct 13 ms 2020 KB Output is correct
23 Correct 14 ms 2096 KB Output is correct
24 Correct 10 ms 2380 KB Output is correct
25 Correct 18 ms 2348 KB Output is correct
26 Correct 18 ms 2376 KB Output is correct
27 Correct 17 ms 2388 KB Output is correct
28 Correct 17 ms 2360 KB Output is correct
29 Correct 20 ms 2264 KB Output is correct
30 Incorrect 39 ms 4016 KB Output isn't correct
31 Halted 0 ms 0 KB -