Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

// 0042 A Thief
// vñFibvTbNâè
// Ql http://algorithms.blog55.fc2.com/blog-category-6.html

#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(void) {

  const int MAX_WEIGHT = 1000 + 1;
  const int MAX_OBJECT = 1000 + 1;
  const int MAX_SCORE = 10000;

  int caseNum = 1;
  while (1) {
    int totalWeight, totalN;
    int i, j;
    static int c
        [MAX_OBJECT]
        [MAX_WEIGHT]; // X^bNÉæçÈ¢ÌÅÃIÌæÉu­
    for (i = 0; i < MAX_OBJECT; i++)
      for (j = 0; j < MAX_WEIGHT; j++)
        c[i][j] = 0;
    char dummy;

    cin >> totalWeight;
    if (totalWeight == 0)
      break;
    cin >> totalN;

    int score, w;
    for (i = 0; i < totalN; i++) {
      cin >> score >> dummy >> w;

      // eÊjÌibvTbNÉüé©AüéÈçAüêéÆ«üêÈ¢Æ«ÇÁ¿ª¢©
      for (j = 0; j <= totalWeight; j++) {
        // ibvTbNÉüèÜ·@©ÂAüê½Æ«ÍüêÈ¢Æ«æè¾µÜ·
        if (w <= j && c[i][j - w] + score > c[i][j]) {
          c[i + 1][j] = c[i][j - w] + score;
        }
        // »ÌiÍgíÈ¢
        else {
          c[i + 1][j] = c[i][j];
        }
      }
    }

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

    w = totalWeight;
    // ¯¶¿lÅæèy¢àÌª é©²×é
    while (1) {
      if (c[totalN][totalWeight] == c[totalN][w - 1])
        w--;
      else
        break;
    }
    cout << c[totalN][totalWeight] << endl << w << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 0042 A Thief
// vñFibvTbNâè

#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(void) {

  const int MAX_WEIGHT = 1000;
  const int MAX_OBJECT = 10000;
  const int MAX_SCORE = 1000;

  struct Obj {
    int w;
    int score;
  };
  struct Cloth {
    int index;
    int totalScore;
  };

  int caseNum = 1;
  while (1) {
    // ÇÌd³ÌÆ«àAÅãÉüê½CfbNXA»êÜÅÌ¿lð¶ÝµÈ¢lÉ
    int totalScore[MAX_WEIGHT + 1] = {0};
    int intoIndex
        [MAX_WEIGHT +
         1]; // ÔÁ¿á¯±êÍú»µÈ­ÄàâèÈµ

    // \\¢Ì¶áfobOÅl©êÈ¢æI
    // Cloth total[MAX_WEIGHT+1];

    Obj obj[MAX_OBJECT];

    int w, n;
    int i, j;
    char dummy;

    cin >> w;
    if (w == 0)
      break;
    cin >> n;

    for (i = 0; i < n; i++) {
      cin >> obj[i].score >> dummy >> obj[i].w;

      for (j = w - obj[i].w; j >= 1; j--) {
        // ·ÅÉ»Ìd³ÜÅÏÜê½àÌª é
        if (totalScore[j]) {
          // æèæ¢ÂÝûð­©
          if (totalScore[j + obj[i].w] < totalScore[j] + obj[i].score) {
            totalScore[j + obj[i].w] = totalScore[j] + obj[i].score;
            intoIndex[j + obj[i].w] = i;
          }
        }
      }
      // ÅÌÍüêÄà¢¢Ìæ
      if (totalScore[obj[i].w] < obj[i].score) {
        totalScore[obj[i].w] = obj[i].score;
        intoIndex[obj[i].w] = i;
      }
    }

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

    // zñÉüÁ½vfÌÅålÆA»ÌCfbNXðT·
    int maxScore = 0;
    int maxIndex = -1;
    for (i = 0; i <= w; i++) {
      if (totalScore[i] && maxScore < totalScore[i]) {
        maxScore = totalScore[i];
        maxIndex = i;
      }
    }
    // ½©vfÍüÁÄ½H
    if (maxIndex >= 0) {
      cout << totalScore[maxIndex] << endl;

      // d³aÌvZ
      int totalW = 0;
      int index = maxIndex;
      while (index > 0) {
        totalW += obj[intoIndex[index]].w;
        index -= obj[intoIndex[index]].w;
      }
      cout << totalW << endl;
    }
    // êÂàüçÈ©Á½H
    else
      cout << "0" << endl << "0" << endl;
  }
  return 0;
}