Submission #583278

# Submission time Handle Problem Language Result Execution time Memory
583278 2022-06-25T07:39:27 Z 600Mihnea Chase (CEOI17_chase) C++17
20 / 100
4000 ms 141024 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = (int) 1e5 + 7;
const int B = 100 + 7;
const ll INF = (ll) 1e18 + 7;
int n, bread_have, pigs[N];
ll from_up[N][B];
ll from_down[N][B];
ll sum_vecs[N];
ll sol = -INF;
vector<int> g[N];

void dfs1(int a, int p = 0) {
  ll gain = 0;
  for (auto &b : g[a]) {
    if (b != p) {
      dfs1(b, a);
      gain += pigs[b];
      for (int i = 1; i <= bread_have; i++) {
        from_up[a][i] = max(from_up[a][i], from_up[b][i]);
      }
    }
  }

  for (int i = bread_have - 1; i >= 0; i--) {
    from_up[a][i + 1] = max(from_up[a][i + 1], from_up[a][i] + gain);
  }
}

void dfs2(int a, int p = 0) {
  for (auto &b : g[a]) {
    if (b != p) {
      dfs2(b, a);
    }
  }

  for (auto &b : g[a]) {
    if (b != p) {
      for (int i = 0; i <= bread_have; i++) {
        from_down[a][i] = max(from_down[a][i], from_down[b][i]);
        if (i >= 1) {
          from_down[a][i] = max(from_down[a][i], sum_vecs[a]);
        }
      }
      for (int i = bread_have - 1; i >= 0; i--) {
        from_down[a][i + 1] = max(from_down[a][i + 1], from_down[b][i] + sum_vecs[a] - pigs[b]);
      }
    }
  }

}

void dfs3(int a, int p = 0) {
  for (auto &b : g[a]) {
    if (b != p) {
      dfs3(b, a);
    }
  }
  sol = max(sol, from_down[a][bread_have]);
  sol = max(sol, from_up[a][bread_have]);
  for (auto &b : g[a]) {
    for (auto &c : g[a]) {
      if (b == p || c == p) {
        continue;
      }
      if (b == c) {
        continue;
      }
      /// no take here
      for (int i = 0; i <= bread_have; i++) {
        for (int j = 0; i + j <= bread_have; j++) {
          sol = max(sol, from_down[b][i] + from_up[c][j]);
        }
      }
      /// take here
      for (int i = 0; i <= bread_have; i++) {
        for (int j = 0; i + j + 1 <= bread_have; j++) {
          sol = max(sol, from_down[b][i] + sum_vecs[a] - pigs[b] + from_up[c][j]); /// sau -pigs[c]?

        }
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

 /// freopen ("input.txt", "r", stdin);

  cin >> n >> bread_have;
  for (int i = 1; i <= n; i++) {
    cin >> pigs[i];
  }

  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
    sum_vecs[a] += pigs[b];
    sum_vecs[b] += pigs[a];
  }
  dfs1(1);
  dfs2(1);

  dfs3(1);

  cout << sol << "\n";


  return 0;


  /*ll sol = -INF;
  for (int root = 1; root <= n; root++) {
    dfs_down(root);
    dfs_up(root);
   /// sol = max(sol, from_up[root][bread_have]);


    exit(0);
  }
  cout << sol << "\n";*/
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Incorrect 18 ms 4056 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1859 ms 139340 KB Output is correct
2 Correct 1868 ms 141024 KB Output is correct
3 Execution timed out 4088 ms 93380 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Incorrect 18 ms 4056 KB Output isn't correct
8 Halted 0 ms 0 KB -