Problem p00042 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

struct Treasure {

  int value;
  int weight;

  Treasure() {
    value = 0;
    weight = 0;
  }

  void operator()(Treasure t) { cout << t.value << "," << t.weight << endl; }

  Treasure &operator+=(const Treasure &t) {

    this->value += t.value;
    this->weight += t.weight;
    return *this;
  }

  Treasure operator+(const Treasure &t) {
    Treasure tmp;
    tmp.value = this->value + t.value;
    tmp.weight = this->weight + t.weight;
    return tmp;
  }

  Treasure operator-(const Treasure &t) {
    Treasure tmp;
    tmp.value = this->value - t.value;
    tmp.weight = this->weight - t.weight;
    return tmp;
  }
};

Treasure max(Treasure t1, Treasure t2) {

  if (t1.value == t2.value) {
    return (t1.weight < t2.weight) ? t1 : t2;
  } else {
    return (t1.value > t2.value) ? t1 : t2;
  }
}

int N, W;

Treasure memo[1000][1000];

Treasure tmp;

vector<Treasure> treasure(1000);

int dp[1001][1001];

Treasure Knapsack(int currentWeight, int currentCount) {

  Treasure t1, t2, f;

  if (memo[currentWeight][currentCount].weight) {
    return memo[currentWeight][currentCount];
  } else if (N == currentCount) {
    return tmp;
  } else {

    t1 = Knapsack(currentWeight, currentCount + 1);

    if (W >= currentWeight + treasure[currentCount].weight) {
      t2 = Knapsack(currentWeight + treasure[currentCount].weight,
                    currentCount + 1) +
           treasure[currentCount];
    } else {
      t2 = Knapsack(currentWeight, currentCount + 1);
    }

    memo[currentWeight][currentCount] = max(t1, t2);

    return max(t1, t2);
  }
}

int main() {

  int caseNumber = 0;

  while (cin >> W && W != 0) {

    cin >> N;

    for (int i = 0; i < N; ++i) {
      scanf("%d,%d", &treasure[i].value, &treasure[i].weight);
    }
    /*
    Treasure result = Knapsack(0,0);

    memset(memo,0,sizeof(memo));

    treasure.erase(treasure.begin(),treasure.end());

    cout << "Case " << ++caseNumber << ":" << endl;

    cout << result.value << endl << result.weight << endl;
    */
    ///////////////////////////////////////////////////////

    for (int i = 0; i < W; ++i) {
      for (int j = 0; j < N; ++j) {
        dp[i][j] = 0;
      }
    }

    for (int i = 1; i <= W; ++i) {
      for (int j = 1; j <= N; ++j) {
        if (i - treasure[j - 1].weight >= 0) {
          dp[i][j] = max(dp[i][j - 1], dp[i - treasure[j - 1].weight][j - 1] +
                                           treasure[j - 1].value);
        } else {
          dp[i][j] = dp[i][j - 1];
        }
      }
    }

    bool flag = false;

    int i;

    for (i = 0; i <= W && !flag; ++i) {
      for (int j = 1; j <= N && !flag; ++j) {
        if (dp[W][N] == dp[i][j]) {
          flag = true;
        }
      }
    }

    cout << "Case " << ++caseNumber << ":" << endl;

    cout << dp[W][N] << endl << i - 1 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

struct Treasure {

  int value;
  int weight;

  Treasure() {
    value = 0;
    weight = 0;
  }

  void operator()(Treasure t) { cout << t.value << "," << t.weight << endl; }

  Treasure &operator+=(const Treasure &t) {

    this->value += t.value;
    this->weight += t.weight;
    return *this;
  }

  Treasure operator+(const Treasure &t) {
    Treasure tmp;
    tmp.value = this->value + t.value;
    tmp.weight = this->weight + t.weight;
    return tmp;
  }

  Treasure operator-(const Treasure &t) {
    Treasure tmp;
    tmp.value = this->value - t.value;
    tmp.weight = this->weight - t.weight;
    return tmp;
  }
};

/*
Treasure max(Treasure t1,Treasure t2)
{

  if(t1.value == t2.value)
    {
      return (t1.weight < t2.weight) ? t1 : t2;
    }
  else
    {
      return (t1.value > t2.value) ? t1 : t2;
    }

}
*/
int N, W;

// Treasure memo[1000][1000];

// Treasure tmp;

vector<Treasure> treasure(1000);

int dp[1000][1000];

/*
Treasure Knapsack(int currentWeight,int currentCount)
{

  Treasure t1,t2,f;

  if(memo[currentWeight][currentCount].weight)
    {
      return memo[currentWeight][currentCount];
    }
  else if(N == currentCount)
    {
      return tmp;
    }
  else{

    t1 = Knapsack(currentWeight,currentCount + 1);

    if( W >= currentWeight + treasure[currentCount].weight)
      {
        t2 = Knapsack(currentWeight + treasure[currentCount].weight,currentCount
+ 1) + treasure[currentCount];
      }
    else
      {
        t2 = Knapsack(currentWeight,currentCount + 1);
      }

    memo[currentWeight][currentCount] = max(t1,t2);

    return max(t1,t2);

  }

}

*/

int main() {

  int caseNumber = 0;

  while (cin >> W && W != 0) {

    cin >> N;

    for (int i = 0; i < N; ++i) {
      scanf("%d,%d", &treasure[i].value, &treasure[i].weight);
    }
    /*
    Treasure result = Knapsack(0,0);

    memset(memo,0,sizeof(memo));

    treasure.erase(treasure.begin(),treasure.end());

    cout << "Case " << ++caseNumber << ":" << endl;

    cout << result.value << endl << result.weight << endl;
    */
    //////////////////////////////////////////////////////////////

    for (int i = 0; i < W; ++i) {
      for (int j = 0; j < N; ++j) {
        dp[i][j] = 0;
      }
    }

    for (int i = 1; i <= W; ++i) {
      for (int j = 1; j <= N; ++j) {
        if (i - treasure[j - 1].weight >= 0) {
          dp[i][j] = max(dp[i][j - 1], dp[i - treasure[j - 1].weight][j - 1] +
                                           treasure[j - 1].value);
        } else {
          dp[i][j] = dp[i][j - 1];
        }
      }
    }

    bool flag = false;

    int i;

    for (i = 0; i <= W && !flag; ++i) {
      for (int j = 1; j <= N && !flag; ++j) {
        if (dp[W][N] == dp[i][j]) {
          flag = true;
        }
      }
    }

    cout << "Case " << ++caseNumber << ":" << endl;

    cout << dp[W][N] << endl << i - 1 << endl;
  }

  return 0;
}