Submission #939761

# Submission time Handle Problem Language Result Execution time Memory
939761 2024-03-06T17:44:20 Z iskhakkutbilim Chase (CEOI17_chase) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
 
using i64 = long long;
 
template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}
 
template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}
 
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int n, k; cin >> n >> k;
    vector <int> a(n);
    for ( auto &u: a ) cin >> u;
    vector <vector<int>> G(n);
    for ( int i = 0; i + 1 < n; i++ ){
        int u, v; cin >> u >> v;
        --u, --v;
        G[u].pb(v), G[v].pb(u);
    }
    if ( !k ){ return cout << "0\n", 0; }
    const int inf = 1e15;
    auto opt = [&](int rt){
        vector <vector<int>> dp(n, vector <int> (k + 1));
        vector <int> s(n);
        auto dfs2 = [&](auto dfs2, int u, int p) -> void{
			for ( auto &v: G[u] ){
				if ( v != p ){
					s[u] += a[v];
					dfs2(dfs2, v, u);
				}
			}
		};
		dfs2(dfs2, rt, -1);
        auto dfs = [&](auto dfs, int u, int p) -> void{
            for ( auto &v: G[u] ){
                if ( v == p ) continue;
                for ( int j = 0; j <= k; j++ ){
					chmax(dp[v][j], dp[u][j]);
					if ( j > 0 ){
						chmax(dp[v][j], dp[u][j - 1] + s[v]);
					}
                }
                dfs(dfs, v, u);
            }
        };
        dp[rt][1][1] = s[rt];
        dfs(dfs, rt, -1);
        int ret = 0;
        for ( int i = 0; i < n; i++ ){
            for ( int j = 0; j <= k; j++ ){
				chmax(ret, dp[i][j]);
            }
        }
        return ret;
    };
    int ans = 0;
    for ( int i = 0; i < n; i++ ){
        chmax(ans, opt(i));
        if ( n > 1e3 ) break;
    }
    cout << ans;
 
    cout << '\n';
}
 
/*
12 2
2 3 3 8 1 5 6 7 8 3 5 4
2 1
2 7
3 4
4 7
7 6
5 6
6 8
6 9
7 10
10 11
10 12
*/

Compilation message

chase.cpp: In lambda function:
chase.cpp:70:18: error: invalid types '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type {aka long long int}[int]' for array subscript
   70 |         dp[rt][1][1] = s[rt];
      |                  ^
chase.cpp: In function 'int main()':
chase.cpp:45:15: warning: unused variable 'inf' [-Wunused-variable]
   45 |     const int inf = 1e15;
      |               ^~~