Submission #1037069

#TimeUsernameProblemLanguageResultExecution timeMemory
1037069lmaobruhKnapsack (NOI18_knapsack)C++14
100 / 100
52 ms36480 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define fo(i, a, b) for (int i = (a), b_ = (b); i <= b_; ++i)
#define fod(i, a, b) for (int i = (a), b_ = (b); i >= b_; --i)
#define rep(i, n) fo(i, 1, n)
#define per(i, n) fod(i, n, 1)
const int N = 2005;

#define pii pair<int, int>
#define fi first
#define se second

int s, n, dp[N][N];
vector<pii> groups[N];

void sol() {
  cin >> s >> n;
  rep(i, n) {
  	int v, w, c; cin >> v >> w >> c;
  	if (w <= s) groups[w].pb({v, c});
  }
  rep(i, s) {
  	vector<pii> &v = groups[i];
  	sort(all(v), greater<pii>());
  	for (int w = 0; w <= s; ++w) {
      dp[i][w] = max(dp[i][w], dp[i-1][w]);
      int add_w = 0, add_value = 0;
      for (auto &ob : v) {
        for (int j = 1; j <= ob.se; ++j) {
          add_w += i;
          add_value += ob.fi;
          if (add_w > w) goto ex;
          dp[i][w] = max(dp[i][w], dp[i-1][w-add_w] + add_value);
        }
      }
      ex:;
    }
  }
  int ans = 0;
  for (int w = 0; w <= s; ++w)
    ans = max(ans, dp[s][w]);
  cout << ans;
}

signed main() {
  cin.tie(0) -> sync_with_stdio(0);
  if (fopen("A.inp", "r")) freopen("A.inp", "r", stdin);
  int tc = 1, test = 0; // cin >> tc;
  while (++test <= tc) sol();
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:50:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |   if (fopen("A.inp", "r")) freopen("A.inp", "r", stdin);
      |                            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#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...