Submission #553914

# Submission time Handle Problem Language Result Execution time Memory
553914 2022-04-27T10:13:56 Z noob26 Knapsack (NOI18_knapsack) C++14
12 / 100
1000 ms 16084 KB
//Target Expert

// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
#include "bits/stdc++.h"
using namespace std;
using namespace chrono;

#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<int> vi;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> pll;

#define ain(a, n)                \
        for (ll i = 0; i < (n); ++i) \
            cin >> (a)[i];
#define fo(i, n) for (int i = 0; i < n; i++)
#define fo2(i, start, end) for (int i = start; i <= end; i++)
#define rfo(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define endl "\n"
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define ppb pop_back
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) (int)(x.size())

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x<<" "; _print(x); cerr << endl;
#else
#define debug(x);
#endif

void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}


template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.fi); cerr << ","; _print(p.se); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key
//void _print(pbds v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

/*---------------------------------------------------------------------------------------------------------------------------*/
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
void extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3
ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0];} //for non prime b
ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}
bool revsort(ll a, ll b) {return a > b;}
void swap(int &x, int &y) {int temp = x; x = y; y = temp;}
ll combination(ll n, ll r, ll m, ll *fact, ll *ifact) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}
void google(int t) {cout << "Case #" << t << ": ";}
vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;}  //only for prime m
ll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))
/*--------------------------------------------------------------------------------------------------------------------------*/
//int dx[] = {0, 0, 1, -1, 1, -1, -1, 1};
//int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
//bool dp[100];
//vi adj[200005];
//vector<bool> vis(200005);
//vi dp(200005);
//vi nodes;

int dp[1005][2005];

int memo(int index, int s, vi &v, vi &w, vi &num) {
    //base
    if (s == 0) {
        return 0;
    }

    if (index < 0) {
        return 0;
    }

    //if (dp[index][s] != -1) return dp[index][s];

    //pick
    int x = 0, y = 0;

    if ((s - w[index]) >= 0 && num[index] >= 1) {
        num[index]--;
        x = v[index] + memo(index, s - w[index], v, w, num);
        num[index]++;
    }

    //leave
    y = memo(index - 1, s, v, w, num);

    return dp[index][s] = max(x, y);
}

void solve(int T) {
    int s, n;
    cin >> s >> n;

    vi v, w, num;

    fo(i, n)
    {
        int x, y, z;
        cin >> x >> y >> z;

        v.pb(x);
        w.pb(y);
        num.pb(z);
    }

    memset(dp, -1, sizeof dp);

    cout << memo(n - 1, s, v, w, num) << endl;
}

int32_t main()
{
    /*freopen("lepus.in", "r", stdin);
    freopen("lepus.out", "w", stdout);*/

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int T = 1;
    //cin >> T;

    //pre();

    for (int i = 1; i <= T; ++i)
    {
        solve(i);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16084 KB Output is correct
2 Correct 7 ms 16076 KB Output is correct
3 Correct 7 ms 16084 KB Output is correct
4 Correct 7 ms 16052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16084 KB Output is correct
2 Execution timed out 1086 ms 16056 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16084 KB Output is correct
2 Execution timed out 1086 ms 16056 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16084 KB Output is correct
2 Correct 7 ms 16076 KB Output is correct
3 Correct 7 ms 16084 KB Output is correct
4 Correct 7 ms 16052 KB Output is correct
5 Correct 9 ms 16084 KB Output is correct
6 Execution timed out 1086 ms 16056 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16084 KB Output is correct
2 Correct 7 ms 16076 KB Output is correct
3 Correct 7 ms 16084 KB Output is correct
4 Correct 7 ms 16052 KB Output is correct
5 Correct 9 ms 16084 KB Output is correct
6 Execution timed out 1086 ms 16056 KB Time limit exceeded
7 Halted 0 ms 0 KB -