Submission #786621

#TimeUsernameProblemLanguageResultExecution timeMemory
786621c2zi6Cloud Computing (CEOI18_clo)C++14
18 / 100
535 ms1996 KiB
#include <bits/stdc++.h>
#define answer ll operator()()
 
using namespace std;
using ll = long long;
 
ll n, m;
vector<ll> c, f, v;
vector<ll> C, F, V;
 
struct test1 {
    answer {
        return 0;
    }
};
 
struct test2 {
    answer {
        return 0;
    }
};
 
struct test3 {
    answer {
        return 0;
    }
};

struct test4time {
    answer {
        ll ans = 0;
        for (int s = 0; s < (1<<n); s++) {
            for (int t = 0; t < (1<<m); t++) {
                ll cores1 = 0, value1 = 0;
                ll cores2 = 0, value2 = 0;
                
                for (int i = 0; i < n; i++) {
                    if (s & (1 << i)) cores1 += c[i], value1 += v[i];
                }
                for (int i = 0; i < m; i++) {
                    if (t & (1 << i)) cores2 += C[i], value2 += V[i];
                }
                
                if (cores1 >= cores2) {
                   ans = max(ans, value2 - value1); 
                }
            }
        }
        return ans;
    }
};

struct test4 {
    //f[i] = F[i] = 1
    const ll MAXCNT = 2000 * 50 + 50;
    
    answer {
        ll dp1[MAXCNT + 1]; //maximum value
        for (ll i = 0; i <= MAXCNT; i++) dp1[i] = -1e18;
        dp1[0] = 0;
        for (ll i = 1; i <= m; i++) {
            ll weight = C[i - 1];
            ll value = V[i - 1];
            for (ll j = MAXCNT; j >= 0; j--) {
                if (j - weight < 0) break;
                if (dp1[j-weight] != -1e18) dp1[j] = max(dp1[j], dp1[j-weight]+value);
            }
        }
        
        ll dp2[MAXCNT + 1]; //minimum value
        for (ll i = 0; i <= MAXCNT; i++) dp2[i] = 1e18;
        dp2[0] = 0;
        for (ll i = 1; i <= n; i++) {
            ll weight = c[i - 1];
            ll value = v[i - 1];
            for (ll j = MAXCNT; j >= 0; j--) {
                if (j - weight < 0) break;
                if (dp2[j-weight] != 1e18) dp2[j] = min(dp2[j], dp2[j-weight]+value);
            }
        }
        
        for (ll i = 1; i <= MAXCNT; i++) dp1[i] = max(dp1[i], dp1[i-1]);
        for (ll i = MAXCNT-1; i >= 0; i--) dp2[i] = min(dp2[i], dp2[i+1]);
        
        ll ans = 0;
        for (ll cur = 0; cur <= MAXCNT; cur++) {
            if (dp1[cur] <= -1e14 || dp2[cur] >= 1e14) continue;
            ll profit = dp1[cur] - dp2[cur];
            ans = max(ans, profit);
        }
        return ans;
    }
};
 
struct test5 {
    answer {
        return 0;
    }
};

vector<ll> rvect(ll n, ll l, ll r) {
    mt19937_64 rnd(time(0));
    vector<ll> a(n);
    for (ll& x : a) {
        x = rnd() % (r - l + 1) + l;
    }
    return a;
}
 
void solve() {
    bool t1, t2, t3, t4, t5;
    t1 = t2 = t3 = t4 = false;
    t5 = true;
    
    cin >> n;
    c = f = v = vector<ll>(n);
    for (int i = 0; i < n; i++) {
        cin >> c[i] >> f[i] >> v[i];
        if (f[i] != 1) t5 = false;
    }
    cin >> m;
    C = F = V = vector<ll>(m);
    for (int i = 0; i < m; i++) {
        cin >> C[i] >> F[i] >> V[i];
        if (F[i] != 1) t5 = false;
    }
    
    cout << test4{}() << endl;
    
    // n = 10, m = 12;
    // c = rvect(n, 1, 50);
    // v = rvect(n, 1, 1e9);
    // C = rvect(m, 1, 50);
    // V = rvect(m, 1, 1e9);
    
    // cout << n << endl;
    // for (int i = 0; i < n; i++) {
    //     cout << c[i] << " " << 1 << " " << v[i] << endl;
    // }
    // cout << m << endl;
    // for (int i = 0; i < m; i++) {
    //     cout << C[i] << " " << 1 << " " << V[i] << endl;
    // }
    // cout << endl << test4{}() << " " << test4time{}() << endl;
    return;
    if (t1) cout << test1{}() << endl;
    else if (t2) cout << test2{}() << endl;
    else if (t3) cout << test3{}() << endl;
    else if (t4) cout << test4{}() << endl;
    else if (t5) cout << test5{}() << endl;
    else cout << -1 << endl;
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int t = 1;
    //cin >> t;
    while (t--) 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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...