Submission #894665

#TimeUsernameProblemLanguageResultExecution timeMemory
894665LinhLewLewKnapsack (NOI18_knapsack)C++17
100 / 100
50 ms3784 KiB
// PhuThuyRuntime <3
// A secret makes a woman woman

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define fo(i, l, r) for(int i = l; i <= r; i++)
#define foi(i, l, r) for(int i = l; i >= r; i--)
#define elif else if
#define el cout << "\n";
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define fi first
#define se second
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#define ll long long
#define ull unsigned long long
#define pob pop_back
#define bs binary_search
#define vi vector<int>
#define vii vector<pair<int, int>>
#define getbit(i, j) (i >> j) & 1
#define offbit(i, j) (1 << j) ^ i
#define onbit(i, j) (1 << j) | i
const int N = 1e5 + 2;
const ll mod = 1e9 + 7;
const int inf = INT_MAX;
const int base = 31;
const long double EPS = 1e-9;
const long double pi = acos(-1.0);
int s, n;
struct zata{
     int v, w, sl;
};
zata a[N];
void inp(){
     cin >> s >> n;
     fo(i, 1, n) cin >> a[i].v >> a[i].w >> a[i].sl;
}
vi g[2002];
bool cmp(zata a, zata b){
     return a.v > b.v;
}
ll dp[2002];
void sol(){
     sort(a + 1, a + n + 1, cmp);
     fo(i, 1, n){
          int v = a[i].v, w = a[i].w, cnt = a[i].sl;
          while((int)g[w].size() + 1 <= s / w && cnt){
               g[w].emplace_back(v);
               cnt--;
          }
     }
     fo(j, 1, s){
          for(auto v : g[j]){
               foi(i, s, j){
                    dp[i] = max(dp[i], dp[i - j] + v);
               }
          }
     }
     cout << (*max_element(dp, dp + s + 1));
}
int main(){
     ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     inp();
     sol();
     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...