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

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

static const double PI = 6 * asin(0.5);
typedef complex<double> P;
typedef long long ll;

int m;
int w, n;
pair<int, int> dp[1050][1050];
char t;
vector<pair<int, int>> pa;
pair<int, int> tpa;

pair<int, int> maxpair(pair<int, int> a, pair<int, int> b) {
  return a.second == b.second  ? a.first > b.first ? b : a
         : a.second > b.second ? a
                               : b;
}

pair<int, int> rec(int i, int j) {
  if (dp[i][j].second >= 0)
    return dp[i][j];
  if (i == pa.size()) {
    tpa.second = 0;
    tpa.first = 0;
  } else if (j < pa[i].first) {
    tpa = rec(i + 1, j);

  } else {
    pair<int, int> tpa1 = rec(i + 1, j);
    pair<int, int> tpa2 = rec(i + 1, j - pa[i].first);
    tpa2.first += pa[i].first;
    tpa2.second += pa[i].second;
    tpa = maxpair(tpa1, tpa2);
  }
  return dp[i][j] = tpa;
}

int main() {
  int e = 1;
  while (cin >> w, w != 0) {
    memset(dp, -1, sizeof(dp));
    cout << "Case " << e++ << ":\\n";
    tpa.first = 0;
    tpa.second = 0;
    cin >> n;
    rep(i, n) {
      int t1, t2;
      cin >> t1 >> t >> t2;
      pair<int, int> tm;
      tm.first = t2;
      tm.second = t1;
      pa.push_back(tm);
    }
    // rep(i,pa.size())cout<<pa[i].first<<" "<<pa[i].second<<endl;
    rec(0, w);

    cout << dp[0][w].second << endl << dp[0][w].first << endl;
    pa.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

static const double PI = 6 * asin(0.5);
typedef complex<double> P;
typedef long long ll;

int m;
int w, n;
int dp;
char t;
vector<pair<int, int>> pa;
pair<int, int> tpa;
pair<pair<int, int>, int> MAX;

int foo(int w, int v, int c, int d) {
  if (c == pa.size()) {
    tpa = MAX.first;
    if (w <= m && MAX.second >= d && tpa.second == v) {
      tpa.first = w;
      tpa.second = v;
      MAX.first = tpa;
      MAX.second = d;
    } else if (w <= m && tpa.second < v) {
      tpa.first = w;
      tpa.second = v;
      MAX.first = tpa;
      MAX.second = d;
    }
  } else {
    foo(w + pa[c].first, v + pa[c].second, c + 1, d + 1);
    foo(w, v, c + 1, d);
  }
  return 0;
}

int main() {
  int e = 1;
  while (cin >> m, m != 0) {
    cout << "Case " << e++ << ":\\n";
    tpa.first = 0;
    tpa.second = 0;
    MAX.first = tpa;
    MAX.second = 0;
    cin >> n;
    rep(i, n) {
      int t1, t2;
      cin >> t1 >> t >> t2;
      pair<int, int> tm;
      tm.first = t2;
      tm.second = t1;
      pa.push_back(tm);
    }
    // rep(i,pa.size())cout<<pa[i].first<<" "<<pa[i].second<<endl;
    foo(0, 0, 0, 0);
    tpa = MAX.first;
    cout << tpa.second << endl << tpa.first << endl;
    pa.clear();
  }
  return 0;
}