Submission #469042

# Submission time Handle Problem Language Result Execution time Memory
469042 2021-08-30T13:36:56 Z paga2004 Chase (CEOI17_chase) C++17
20 / 100
4000 ms 13356 KB
#include <bits/stdc++.h>

#ifdef LOCAL
#define dbg(x) cerr << "dgb: " << x << "\n";
#else
#define dbg(x)
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,tune=native")
#endif

using namespace std;

#define int long long

int n;
vector<int> c;
vector<int> sum;
vector<vector<int>> g;

int f(int v, int p, int k) {
  if (k == 0)
    return 0;
  int maxi = 0;
  for (int w : g[v]) {
    if (w == p)
      continue;
    maxi = max(maxi, f(w, v, k - 1) + sum[v] - ((p == -1) ? 0 : c[p]));
    maxi = max(maxi, f(w, v, k));
  }
  return maxi;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int k;
  cin >> n >> k;
  c.resize(n);
  g.resize(n);
  sum.assign(n, 0);
  for (int &x : c)
    cin >> x;

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

  if (n <= 10) {
    int best = 0;
    for (int root = 0; root < n; root++)
      best = max(best, f(root, -1, k));
    cout << best << "\n";
  } else {
    cout << f(1, -1, k) << "\n";
  }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Execution timed out 4069 ms 332 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4081 ms 13356 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Execution timed out 4069 ms 332 KB Time limit exceeded
8 Halted 0 ms 0 KB -