# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1166814 | SmuggingSpun | Fireworks (APIO16_fireworks) | C++20 | 4 ms | 328 KiB |
#include<bits/stdc++.h>
#define taskname "B"
using namespace std;
typedef long long ll;
template<class T>bool minimize(T& a, T b){
if(a > b){
a = b;
return true;
}
return false;
}
int n, m;
namespace sub1{
void solve(){
vector<int>a(m);
for(int& x : a){
cin >> x >> x;
}
sort(a.begin(), a.end());
int median = a[m >> 1];
ll ans = 0;
for(int& x : a){
ans += abs(x - median);
}
cout << ans;
}
}
namespace sub2{
const int lim = 305;
const int INF = 1e9;
vector<pair<int, int>>g[lim];
int ans = 0;
vector<int>dfs(int s){
if(s > n){
return vector<int>(1, 0);
}
vector<vector<int>>a;
for(auto& [d, w] : g[s]){
vector<int>value = dfs(d);
for(int& x : value){
x += w;
}
a.emplace_back(value);
}
int opt = INF;
vector<int>ret;
for(int i = 0; i < 10001; i++){
int cur = 0;
for(auto& ve : a){
int cost = INF;
for(int& x : ve){
minimize(cost, abs(x - i));
}
cur += cost;
}
if(minimize(opt, cur)){
ret.clear();
}
if(opt == cur){
ret.emplace_back(i);
}
}
ans += opt;
return ret;
}
void solve(){
for(int i = 2; i <= n + m; i++){
int p, c;
cin >> p >> c;
g[p].emplace_back(i, c);
}
dfs(1);
cout << ans;
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n >> m;
if(n == 1){
sub1::solve();
}
else if(n + m <= 200){
sub2::solve();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |