Submission #3768729


Source Code Expand

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

#define MAX_N (100)

using namespace std;

int dp[MAX_N + 1][MAX_N + 1][3 * MAX_N + 1] = {};

int main(int argc, char *argv[]) {
  // read inputs
  int N, W, ws[MAX_N], vs[MAX_N];
  scanf("%d %d", &N, &W);
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &ws[i], &vs[i]);
  }

  // subtract w1 from all wis
  const int w0 = ws[0];
  for (int i = 0; i < N; i++) {
    ws[i] -= w0;
  }

  // solve dp
  for (int n = 1; n <= N; n++) {
    for (int i = 0; i < N; i++) {
      const int w_i = ws[i], v_i = vs[i];
      for (int j = 0; j < w_i; j++) {
        dp[n][i + 1][j] = dp[n][i][j];
      }
      for (int j = w_i; j <= 3 * N; j++) {
        dp[n][i + 1][j] = max(dp[n][i][j], dp[n - 1][i][j - w_i] + v_i);
      }
    }
  }

  // print answer
  int ans = 0;
  for (int n = 1; n <= N && W - n * w0 >= 0; n++) {
    ans = max(ans, dp[n][N][W - n * w0]);
  }
  printf("%d\n", ans);

  return 0;
}

Submission Info

Submission Time
Task D - Simple Knapsack
User khir
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1006 Byte
Status RE
Exec Time 108 ms
Memory 12032 KB

Compile Error

./Main.cpp: In function ‘int main(int, char**)’:
./Main.cpp:14:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &N, &W);
                         ^
./Main.cpp:16:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &ws[i], &vs[i]);
                                   ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 4
AC × 11
WA × 1
RE × 4
Set Name Test Cases
Sample example0, example1, example2, example3
All antigreedy0, antigreedy1, antigreedy2, example0, example1, example2, example3, quarter0, quarter1, quarter2, rand0, rand1, rand2, smallw0, smallw1, smallw2
Case Name Status Exec Time Memory
antigreedy0 RE 107 ms 12032 KB
antigreedy1 RE 108 ms 12032 KB
antigreedy2 RE 107 ms 12032 KB
example0 AC 1 ms 128 KB
example1 AC 1 ms 128 KB
example2 AC 1 ms 128 KB
example3 AC 1 ms 128 KB
quarter0 AC 8 ms 12032 KB
quarter1 AC 8 ms 12032 KB
quarter2 AC 8 ms 12032 KB
rand0 AC 1 ms 256 KB
rand1 AC 2 ms 5248 KB
rand2 RE 100 ms 2816 KB
smallw0 AC 8 ms 12032 KB
smallw1 WA 8 ms 12032 KB
smallw2 AC 8 ms 12032 KB