Submission #35833

# Submission time Handle Problem Language Result Execution time Memory
35833 2017-12-01T09:31:02 Z funcsr Chase (CEOI17_chase) C++14
50 / 100
566 ms 487600 KB
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
inline void chmax(long long &x, long long v) { if (x < v) x = v; }

int N, K;
int A[100001];
long long T[100000];
vector<int> G[100000];

long long down[100000][101];
long long dp[100000][101][2][2];
int to[100000][101][2];

void f(int x, int p) {
  for (int t : G[x]) {
    if (t == p) continue;
    f(t, x);
    rep(k, K+1) chmax(down[x][k], down[t][k]);
    rep(k, K) chmax(down[x][k+1], down[t][k]+T[x]-A[p]);
  }
}

inline void update(int x, int k, int b, long long val, int t) {
  if (val > dp[x][k][b][0]) {
    if (t == to[x][k][b]) dp[x][k][b][0] = val;
    else {
      dp[x][k][b][1] = dp[x][k][b][0];
      dp[x][k][b][0] = val;
      to[x][k][b] = t;
    }
  }
  else if (val > dp[x][k][b][1]) {
    dp[x][k][b][1] = val;
  }
}

void g(int x, int p) {
  if (p != N) {
    rep(k, K+1) {
      long long m = 0;
      rep(t, 2) {
        int s = 0;
        if (to[p][k][t] == x) s++;
        chmax(m, dp[p][k][t][s] + (t==1?-A[x]:0));
      }
      update(x, k, 0, m, p);
      if (k < K) update(x, k+1, 1, m+T[x], p);
    }
  }
  for (int t : G[x]) {
    if (t == p) continue;
    rep(k, K+1) update(x, k, 0, down[t][k], t);
    rep(k, K)   update(x, k+1, 1, down[t][k]+T[x]-A[p], t);
  }

  for (int t : G[x]) {
    if (t == p) continue;
    g(t, x);
  }
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> N >> K;
  rep(i, N) cin >> A[i];
  rep(i, N-1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[u].pb(v);
    G[v].pb(u);
  }
  rep(i, N) for (int t : G[i]) T[i] += A[t];
  rep(i, N) rep(j, K+1) rep(k, 2) to[i][j][k] = -1;

  f(0, N);
  g(0, N);
  long long m = 0;
  rep(i, N) rep(j, 2) m = max(m, dp[i][K][j][0]);
  cout << m << "\n";
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 479128 KB Output is correct
2 Correct 0 ms 479128 KB Output is correct
3 Correct 0 ms 479128 KB Output is correct
4 Correct 0 ms 479128 KB Output is correct
5 Correct 0 ms 479128 KB Output is correct
6 Correct 0 ms 479128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 479128 KB Output is correct
2 Correct 0 ms 479128 KB Output is correct
3 Correct 0 ms 479128 KB Output is correct
4 Correct 0 ms 479128 KB Output is correct
5 Correct 0 ms 479128 KB Output is correct
6 Correct 0 ms 479128 KB Output is correct
7 Incorrect 3 ms 479128 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 416 ms 487600 KB Output is correct
2 Correct 413 ms 487568 KB Output is correct
3 Correct 219 ms 482680 KB Output is correct
4 Correct 216 ms 482296 KB Output is correct
5 Correct 483 ms 482296 KB Output is correct
6 Correct 506 ms 482296 KB Output is correct
7 Correct 566 ms 482296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 479128 KB Output is correct
2 Correct 0 ms 479128 KB Output is correct
3 Correct 0 ms 479128 KB Output is correct
4 Correct 0 ms 479128 KB Output is correct
5 Correct 0 ms 479128 KB Output is correct
6 Correct 0 ms 479128 KB Output is correct
7 Incorrect 3 ms 479128 KB Output isn't correct
8 Halted 0 ms 0 KB -