제출 #1119778

#제출 시각아이디문제언어결과실행 시간메모리
1119778barkoloriousKnapsack (NOI18_knapsack)C++17
73 / 100
248 ms262144 KiB
// barkolorious - 27 November 2024
// in god, do we trust? 
#include <bits/stdc++.h>
using namespace std;

#define FIN(x) freopen(x ".in", "r", stdin)
#define FOUT(x) freopen(x ".out", "w", stdout)
#define int long long
#define pb  push_back
#define fr  first
#define sc  second
#define __  << " " << 

const int N = 2e5 + 5;

struct Item {
  int v, w, k;
};

void solve () {
  int n, s; cin >> s >> n;
  Item item[n + 1];
  for (int i = 1; i <= n; i++) cin >> item[i].v >> item[i].w >> item[i].k;
  int dp[n + 1][s + 1]{};
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= s; j++) {
      int count = min(j / item[i].w, item[i].k);
      for (int k = 0; k <= count; k++) {
        int weight = k * item[i].w, value = k * item[i].v;
        if (j - weight < 0) break;
        dp[i][j] = max(dp[i][j], dp[i - 1][j - weight] + value);
      }
      if (j) dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  // for (int i = 0; i <= n; i++) 
  //   for (int j = 0; j <= s; j++)  
  //     cout << setfill(' ') << setw(4) << dp[i][j] << " \n"[j == s];
  cout << dp[n][s] << endl;
}

/*
-- Sample 1 --
Input:
15 5
4 12 1
2 1 1
10 4 1
1 1 1
2 2 1
Output:
15

-- Sample 2 --
Input:
20 3
5000 15 1
100 1 3
50 1 4
Output:
5400
*/

/*
g++ -std=c++17 -O2 -Wall -DLOCAL "C:\Users\LENOVO\Desktop\BARKIN\Genel\Programming\Competitive\Questions\oj.uz\NOI18\NOI18_knapsack.cpp" -o _run
*/

int32_t main () {
  #ifndef LOCAL
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
  #endif

  #ifdef LOCAL
    clock_t __START__ = clock();
    FILE* __FILE_IN__ = FIN("C:/Users/LENOVO/Desktop/BARKIN/Genel/Programming/Competitive/_run");
    FILE* __FILE_OUT__ = FOUT("C:/Users/LENOVO/Desktop/BARKIN/Genel/Programming/Competitive/_run");
  #endif

  solve();

  #ifdef LOCAL
    fclose(__FILE_IN__);
    fclose(__FILE_OUT__);
    cerr << "Executed in: " << fixed << setprecision(3) << (double) (clock() - __START__) / CLOCKS_PER_SEC << "seconds" << endl;
  #endif

  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...