Submission #631469

# Submission time Handle Problem Language Result Execution time Memory
631469 2022-08-18T07:20:47 Z Soul234 Dostavljač (COCI18_dostavljac) C++14
140 / 140
219 ms 4312 KB
#include<bits/stdc++.h>
using namespace std;

void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << h; if(sizeof...(t)) cerr << ", ";
    DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL

#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;

void setIO(string NAME = "") {
    cin.tie(0)->sync_with_stdio(0);
    if(sz(NAME)) {
        freopen((NAME + ".inp").c_str(),"r",stdin);
        freopen((NAME + ".out").c_str(),"w",stdout);
    }
}

tcT> bool ckmin(T&a, const T&b) {
    return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
    return b > a ? a=b,1 : 0; }

const int MOD = 1e9 + 7;

const int MX = 5e2+5;
int N, M, A[MX];
vi adj[MX];
ll dp[2][MX][MX];

void dfs(int u, int p) {
    FOR(k,1,M+1) dp[0][u][k] = dp[1][u][k] = A[u];
    each(v, adj[u]) if(v != p) {
        dfs(v, u);
        ROF(i,1,M+1) {
            F0R(j, i+1) {
                if(i-j-1 >= 0) {
                    ckmax(dp[0][u][i], max(dp[0][v][j], dp[1][v][j]) + dp[1][u][i-j-1]);
                }
                if(i-j-2 >= 0) {
                    ckmax(dp[0][u][i], dp[1][v][j] + max(dp[1][u][i-j-2], dp[0][u][i-j-2]));
                    ckmax(dp[1][u][i], dp[1][v][j] + dp[1][u][i-j-2]);
                }
            }
        }
    }
}

void solve() {
    cin >> N >> M;
    FOR(i,1,N+1) cin >> A[i];
    F0R(i, N-1) {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1, -1);
    cout << max(dp[0][1][M], dp[1][1][M]) << nl;
}

int main() {
    setIO();

    int t=1;
    //cin>>t;
    while(t-->0) {
        solve();
    }

    return 0;
}

Compilation message

dostavljac.cpp: In function 'void setIO(std::string)':
dostavljac.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen((NAME + ".inp").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dostavljac.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen((NAME + ".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1108 KB Output is correct
2 Correct 3 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1492 KB Output is correct
2 Correct 33 ms 1516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 1896 KB Output is correct
2 Correct 22 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 2616 KB Output is correct
2 Correct 118 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 3456 KB Output is correct
2 Correct 46 ms 3440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 219 ms 4240 KB Output is correct
2 Correct 27 ms 4312 KB Output is correct