Submission #752913

# Submission time Handle Problem Language Result Execution time Memory
752913 2023-06-04T09:05:05 Z benjaminkleyn Fireworks (APIO16_fireworks) C++17
0 / 100
2000 ms 7380 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MAX = 300010;
int n, m;
int p[2 * MAX]; 
ll c[2 * MAX];
vector<pair<int, ll>> g[MAX];

ll f(vector<ll> &t, ll c)
{
    ll ans = 0;
    for (ll x : t)
        ans += abs(c - x);
    return ans;
}

pair<ll, ll> dp(int u)
{
    if (u > n)
        return {0, 0};

    ll res = 0;
    vector<ll> times;
    ll min_val = LLONG_MAX, max_val = 0;
    for (auto [v, l] : g[u])
    {
        auto [t, d] = dp(v);
        res += t;
        times.push_back(l + d);
        min_val = min(min_val, d);
        max_val = max(max_val, l + d);
    }

    ll best = LLONG_MAX, len;
    for (int i = min_val; i <= max_val; i++)
    {
        ll cost = f(times, i);
        if (cost < best)
            best = cost, len = i;
    }

    return {res + best, len};
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);

    cin >> n >> m;
    p[1] = -1, c[1] = 0;
    for (int i = 2; i <= n + m; i++)
    {
        cin >> p[i] >> c[i];
        g[p[i]].push_back({i, c[i]});
    }

    cout << dp(1).first << '\n';

    return 0;
}

Compilation message

fireworks.cpp: In function 'std::pair<long long int, long long int> dp(int)':
fireworks.cpp:44:28: warning: 'len' may be used uninitialized in this function [-Wmaybe-uninitialized]
   44 |     return {res + best, len};
      |                            ^
# Verdict Execution time Memory Grader output
1 Correct 866 ms 7360 KB Output is correct
2 Execution timed out 2073 ms 7252 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 7380 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 866 ms 7360 KB Output is correct
2 Execution timed out 2073 ms 7252 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 866 ms 7360 KB Output is correct
2 Execution timed out 2073 ms 7252 KB Time limit exceeded
3 Halted 0 ms 0 KB -