제출 #1356732

#제출 시각아이디문제언어결과실행 시간메모리
1356732Hurryup_7735Džumbus (COCI19_dzumbus)C++20
0 / 110
14 ms24132 KiB
//In The Name Of ALLAH!
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
 
using namespace __gnu_pbds;
using namespace std;
 
#define ll long long
#define ld long double
#define endl '\n'
#define pb push_back
#define pf push_front
#define Zemur007 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define bpc __builtin_popcountll
#define btz __builtin_ctzll
#define all(x) x.begin() , x.end()
#define allr(x) x.rbegin() , x.rend()
#define F first
#define S second
#define pii pair<int , int>
#define pll pair<ll , ll>
#define turtle tuple<ll , ll , ll>
#define pss pair<string , string>
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define indexed_set tree<pll , null_type , less<pll> , rb_tree_tag , tree_order_statistics_node_update>

const ll sz = 1e3 + 5 , INF = 1e18 ,  MOD = 998244353;
ll a[sz] , b[sz] ,  dis[sz] , timer;
ll dp[sz][sz][3];
vector<ll> g[sz];

void dfs(ll n , ll p){
    ++timer;
    b[timer] = a[n];
    for(ll i : g[n]){
        if(i != p){
            dfs(i , n);
        }
    }
}

//dp[i][j][k] -> i-ə qədər gəlmişik və aldığımız cc cem j olubsa

//dp[0][0][0] = 0;
/*
dp[i][j][0] = max({dp[i][j][0] , dp[i - 1][j][0] , dp[i - 1][j][2]});
dp[i][j][1] = max({dp[i][j][1] , dp[i - 1][j - a[i]][0] , })
*/

ll mask , i , j , k;
void solve(){
    ll n , m; cin >> n >> m;
    for(i = 1 ; i <= n ; i++) cin >> a[i];
    for(i = 1 ; i <= m ; i++){
        ll u , v; cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    for(i = 1 ; i <= n ; i++){
        if(g[i].size() == 1){
            timer = 0;
            dfs(i , -1);
            break;
        }
    }
    //k = 0 -> goturmuruk
    //k = 1 -> yeni component basladiriq
    //k = 2 -> evvelkine birlesdiririk
    for(i = b[1] ; i <= 1000 ; i++){
        dp[1][i][1] = 1;
    }
    for(i = 0 ; i <= 1000 ; i++) dp[1][i][2] = -INF;
    for(i = 2 ; i <= n ; i++){
        for(j = 1000 ; j >= 0 ; j--){
            dp[i][j][0] = max({dp[i][j][0] , dp[i - 1][j][0] , dp[i - 1][j][2]});
            dp[i][j][1] = max({dp[i][j][1] , (j >= b[i] ? dp[i - 1][j - b[i]][0] : 0)});
            dp[i][j][2] = max({dp[i][j][2] , (j >= b[i] ? dp[i - 1][j - b[i]][1] + 2 : 0) , (j >= b[i] ? dp[i - 1][j - b[i]][2] + 1 : 0)});
        }
    }
    // cout << dp[2][6][2] << endl;
    ll q; cin >> q;
    while(q--){
        ll x; cin >> x;
        cout << max(dp[n][x][0] , dp[n][x][2]) - 1 << endl;
    }
}
/*
5 4
1 4 5 3 2
1 2
1 3
2 4
3 5
3
8
10
12
*/
 
signed main(){
    Zemur007;
    // open();
    #ifdef LOCAL
    freopen("output.txt","w",stdout);
    #endif
    ll t = 1;
    // cin >> t;
    while(t--){
        solve();
    }
    // for(ll testcase = 1 ; testcase <= t ; testcase++){
    //     cout << "Case " << testcase << ":" << endl;
    //     solve();
    // }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...