Submission #526816

# Submission time Handle Problem Language Result Execution time Memory
526816 2022-02-16T08:04:28 Z XEMPLI5 Fireworks (APIO16_fireworks) C++17
7 / 100
5 ms 7372 KB
#include <bits/stdc++.h>
using namespace std;

#define gc getchar_unlocked
#define fo(i, n) for (int i = 0; i < n; i++)
#define rfo(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = a; i >= b; i--)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z) cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626

typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

int mpow(int base, int exp);

const int mod = 1'000'000'007;
const int N = 3e5, M = N;

vpl g[N];
ll head[N], depth[N];

void dfs(int cur, int par)
{
    for (auto e : g[cur])
    {
        if (e.F == par)
            continue;
        depth[e.F] = depth[cur] + e.S;
        dfs(e.F, cur);
    }
}

void solve()
{
    int n, m;
    cin >> n >> m;
    fo(i, n + m - 1)
    {
        ll x, y;
        cin >> x >> y;
        x--;
        head[i + 1] = x;
        g[x].pb({i + 1, y});
        g[i + 1].pb({x, y});
    }
    dfs(0, 0);
    ll ans = 1e18;
    vl cvs;
    fo(i, m) cvs.pb(depth[i + n]);
    sortall(cvs);
    ll cv = cvs[cvs.size() / 2];
    vl changes;
    ll temp = 0;
    fo(j, m)
    {
        if (head[j + n] == 0)
        {
            temp += abs(depth[j + n] - cv);
        }
        else
        {
            changes.pb(cv - depth[j + n]);
        }
    }
    if (changes.size())
    {
        ll mv = changes[changes.size() / 2];
        mv = max(-depth[1], mv);
        temp += mv;
        for (auto e : changes)
        {
            temp += abs(e - mv);
        }
    }
    ans = min(temp, ans);
    cout << ans << '\n';
    return;
}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    int t = 1;
    int cs = 0;
    while (t - cs)
    {
        solve();
        cs++;
    }

    return 0;
}

int mpow(int base, int exp)
{
    base %= mod;
    int result = 1;
    while (exp > 0)
    {
        if (exp & 1)
            result = ((ll)result * base) % mod;
        base = ((ll)base * base) % mod;
        exp >>= 1;
    }
    return result;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7244 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 5 ms 7360 KB Output is correct
5 Correct 4 ms 7372 KB Output is correct
6 Correct 5 ms 7260 KB Output is correct
7 Correct 4 ms 7364 KB Output is correct
8 Correct 4 ms 7372 KB Output is correct
9 Correct 4 ms 7372 KB Output is correct
10 Correct 4 ms 7372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 7244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7244 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 5 ms 7360 KB Output is correct
5 Correct 4 ms 7372 KB Output is correct
6 Correct 5 ms 7260 KB Output is correct
7 Correct 4 ms 7364 KB Output is correct
8 Correct 4 ms 7372 KB Output is correct
9 Correct 4 ms 7372 KB Output is correct
10 Correct 4 ms 7372 KB Output is correct
11 Incorrect 3 ms 7244 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7244 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 5 ms 7360 KB Output is correct
5 Correct 4 ms 7372 KB Output is correct
6 Correct 5 ms 7260 KB Output is correct
7 Correct 4 ms 7364 KB Output is correct
8 Correct 4 ms 7372 KB Output is correct
9 Correct 4 ms 7372 KB Output is correct
10 Correct 4 ms 7372 KB Output is correct
11 Incorrect 3 ms 7244 KB Output isn't correct
12 Halted 0 ms 0 KB -