Submission #767275

#TimeUsernameProblemLanguageResultExecution timeMemory
767275synthesisKnapsack (NOI18_knapsack)C++17
0 / 100
30 ms31744 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>
#define all(x) x.begin(), x.end()

using namespace std;
using namespace __gnu_pbds;
using ll = long long int;
using ull = unsigned long long int;

constexpr ll mod = 1e9 + 7;
constexpr ll INF = LONG_LONG_MAX;

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
string moves = "URDL";

bool cmp(const pair<ll, ll>& c1, const pair<ll, ll>& c2) {
    return c1.fi > c2.fi;
}

int main()
{
    //tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> q;
    /*freopen("problemname.in", "r", stdin);
    freopen("problemname.out", "w", stdout);*/
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll x, ans = 0;
    int n;
    cin >> x >> n;
    vector<pair<ll, ll>> arr[x + 1];
    for(int i = 0;i<n;++i) {
        ll a, b, c;
        cin >> a >> b >> c;
        arr[b].pb({a, c});
    }
    for(ll i = 1;i<=x;++i) {
        sort(all(arr[i]), cmp);
    }
    vector<ll> rel[x + 1];
    for(ll i = 1;i<=x;++i) {
        ll cnt = 0;
        for(pair<ll, ll>& v:arr[i]) {
            if (cnt == x || v.se == 0) {
                break;
            }
            while(cnt < x && v.se != 0) {
                ++cnt;
                rel[i].pb(v.fi);
            }
        }
    }
    ll dp[x + 1][x + 1];
    memset(dp, 0, sizeof dp);
    for(ll i = 1;i<=x;++i) {
        for(ll j = 0;j<=x;++j) {
            dp[i][j] = max(dp[i][j], dp[i - 1][j]);
            ll cnt = 1, val = 0, siz = rel[i].size();
            while(j - cnt*i >= 0 && cnt <= siz) {
                val+=rel[i].at(cnt - 1);
                dp[i][j] = max(dp[i][j], dp[i - 1][j - cnt*i] + val);
                ++cnt;
            }
            ans = max(ans, dp[i][j]);
        }
    }
    cout << ans << endl;
    return 0;
}
#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...