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

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2510

#include <iostream>
// #include<fstream>

#define N 1001 // データの最大個数
#define ami 0
#define mami 1

int n = 0;
int wTime[N] = {};

int sum(int a[]) {
  int ans = 0;
  for (int i = 0; a[i] != 0; i++)
    ans += a[i];
  return ans;
}

int timeCalc(int differ) {
  bool dp[1000000] = {
      true,
  };

  // wTime[0 ? n-2]はsort済み
  int time = 0;

  for (int i = 0; i <= n - 2; i++) {           // 各wTimeごとに
    for (int j = differ; j >= wTime[i]; j--) { // wTime[i] < j < differ
      if (dp[j - wTime[i]]) {
        dp[j] = true; // DP的な。
        if (time < j)
          time = j; // 大きい方を選択
      }
    }
  }
  return time;
}

int main() {
  using namespace std;
  /*
          ifstream fin;

          fin.open("input.txt");
          if (!fin)cout << "file read error!" << endl;
  */
  int rTime[N] = {};
  int allTime[2] = {0, 0};

  while (true) {

    cin >> n;
    if (n == 0)
      break; // 終了条件

    for (int i = 0; i < N; i++) { // 初期化
      rTime[i] = wTime[i] = 0;
    }

    for (int i = 0; i < n; i++) {
      cin >> rTime[i] >> wTime[i];
      //			cout << rTime[i] << wTime[i] << endl;
    }

    if (n == 1) {
      cout << 2 * rTime[0] + wTime[0] << endl;
      continue;
    }

    // 読書時間でbubble sort
    for (int i = n - 1; i >= 0; i--) {
      for (int j = 0; j < i; j++) {
        if (rTime[j] > rTime[j + 1]) {
          int tmp[2] = {rTime[j], wTime[j]};

          rTime[j] = rTime[j + 1];
          wTime[j] = wTime[j + 1];

          rTime[j + 1] = tmp[0];
          wTime[j + 1] = tmp[1];
        }
      }
    }
    // 計算開始

    // read
    // 最も読む時間のかかる一つと、その他の全ての和を比較

    // max <= sum
    if (sum(rTime) >= 2 * rTime[n - 1]) {
      cout << sum(rTime) + sum(wTime) << endl;
    }

    // max > sum
    else if (sum(rTime) < 2 * rTime[n - 1]) {

      allTime[ami] = rTime[n - 1];
      allTime[mami] = sum(rTime) - rTime[n - 1];

      int differ = allTime[ami] - allTime[mami];

      for (int i = n - 2; i >= 0; i--) {
        for (int j = 0; j < i; j++) {
          if (wTime[j] > wTime[j + 1]) {
            int tmp = wTime[j];
            wTime[j] = wTime[j + 1];
            wTime[j + 1] = tmp;
          }
        }
      }

      int writingTime = timeCalc(differ);

      cout << sum(rTime) + sum(wTime) + differ - writingTime << endl;
    }
  }
  return 0;
}

Error: -11

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2510

#include <fstream>
#include <iostream>

#define N 1000 // データの最大個数
#define ami 0
#define mami 1

int n = 0;
int wTime[N] = {};

int sum(int a[]) {
  int ans = 0;
  for (int i = 0; a[i] != 0; i++)
    ans += a[i];
  return ans;
}

int timeCalc(int differ, int sum) {
  bool dp[10000000] = {
      true,
  };

  // wTime[0 ? n-2]はsort済み
  int time = 0;
  int max = 0;

  for (int i = 0; i < n - 2; i++) {
    for (int j = differ; j >= wTime[i]; j--) {
      if (dp[j - wTime[i]])
        dp[j] = true;
      if (dp[j] && time < j)
        time = j;
    }
  }
  return time;
}

int main() {
  using namespace std;

  int rTime[N] = {};
  int allTime[2] = {0, 0};

  while (true) {

    cin >> n;
    if (n == 0)
      break; // 終了条件

    for (int i = 0; i < N; i++) { // 初期化
      rTime[i] = wTime[i] = 0;
    }

    for (int i = 0; i < n; i++) {
      cin >> rTime[i] >> wTime[i];
      //			cout << rTime[i] << wTime[i] << endl;
    }

    if (n == 1) {
      cout << 2 * rTime[0] + wTime[0] << endl;
      continue;
    }

    // 読書時間でbubble sort
    for (int i = n - 1; i >= 0; i--) {
      for (int j = 0; j < i; j++) {
        if (rTime[j] > rTime[j + 1]) {
          int tmp = rTime[j];
          rTime[j] = rTime[j + 1];
          rTime[j + 1] = tmp;
        }
      }
    }
    // 計算開始

    // read
    // 最も読む時間のかかる一つと、その他の全ての和を比較

    // max <= sum
    if (sum(rTime) >= 2 * rTime[n - 1]) {
      cout << sum(rTime) + sum(wTime) << endl;
    }

    // max > sum
    else if (sum(rTime) < 2 * rTime[n - 1]) {

      allTime[ami] = rTime[n - 1];
      allTime[mami] = sum(rTime) - rTime[n - 1];

      int differ = allTime[ami] - allTime[mami];

      for (int i = n - 2; i >= 0; i--) {
        for (int j = 0; j < i; j++) {
          if (wTime[j] > wTime[j + 1]) {
            int tmp = wTime[j];
            wTime[j] = wTime[j + 1];
            wTime[j + 1] = tmp;
          }
        }
      }

      int writingTime = timeCalc(differ, allTime[mami]);

      cout << sum(rTime) + sum(wTime) + differ - writingTime << endl;
    }
  }
  return 0;
}