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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <complex>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
vector<long long int> divisor(long long int n) {
  vector<long long int> res;
  for (long long int i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      res.push_back(i);
      if (i * i != n)
        res.push_back(n / i);
    }
  }
  sort(begin(res), end(res));
  return res;
}

int main() {
  int a = 1;
  while (1) {

    int W;
    cin >> W;

    if (W) {
      int N;
      cin >> N;
      vector<int> vs, ws;
      for (int i = 0; i < N; ++i) {
        string st;
        cin >> st;
        auto it = st.find(',');
        int a = stoi(st.substr(0, it));
        int b = stoi(st.substr(it + 1, st.size() - it - 1));
        vs.push_back(a);
        ws.push_back(b);
      }
      vector<vector<int>> dp(N + 1, vector<int>(W + 1, 0));
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j <= W; ++j) {
          if (j + ws[i] <= W) {
            dp[i + 1][j + ws[i]] = max(dp[i + 1][j + ws[i]], dp[i][j] + vs[i]);
          }
          dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
        }
      }
      int amax = -1;
      int amin = 999999;
      for (int i = 0; i <= W; ++i) {
        amax = max(amax, dp[N][i]);
      }
      for (int i = 0; i <= W; ++i) {
        if (dp[N][i] == amax) {
          amin = i;
          break;
        }
      }
      cout << "Case " << a << ":" << endl;
      cout << amax << endl;
      cout << amin << endl;
      a++;
    } else {
      break;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

struct Treasure {
  int Weight;
  int Worth;
};
vector<int> TempMaxWorth;

int GetMaxWorth(const int MaxWeight, vector<Treasure> nowTreasureCollection,
                vector<Treasure>::iterator nowIterator,
                const vector<Treasure> &AllTreasureCollection) {
  vector<Treasure>::iterator rootIterator(nowIterator);
  for (; nowIterator != AllTreasureCollection.end(); ++nowIterator) {
    nowTreasureCollection.push_back(*(nowIterator));
    const int NowWorth = accumulate(
        nowTreasureCollection.cbegin(), nowTreasureCollection.cend(), 0,
        [](const int q, const Treasure &p) { return q + p.Worth; });
    const int NowWeight = accumulate(
        nowTreasureCollection.cbegin(), nowTreasureCollection.cend(), 0,
        [](const int q, const Treasure &p) { return q + p.Weight; });
    if (NowWeight <= MaxWeight && NowWorth > TempMaxWorth[NowWeight]) {
      TempMaxWorth[NowWeight] = NowWorth;
      replace_if(
          TempMaxWorth.begin() + NowWeight, TempMaxWorth.end(),
          [=](int n) { return NowWorth > n; }, NowWorth);
      // if((nowIterator+1)!=AllTreasureCollection.end())GetMaxWorth(MaxWeight,
      // nowTreasureCollection, nowIterator+1,AllTreasureCollection);
    }
    if (NowWeight <= MaxWeight) {
      if ((nowIterator + 1) != AllTreasureCollection.end())
        GetMaxWorth(MaxWeight, nowTreasureCollection, nowIterator + 1,
                    AllTreasureCollection);
    }
    nowTreasureCollection.pop_back();
  }
  nowIterator = rootIterator;
  return 0;
}

int main() {
  int MaxWeight;
  int TreasureNum;
  for (int i = 1; cin >> MaxWeight; ++i) {
    if (MaxWeight == 0)
      return 0;
    cin >> TreasureNum;
    TempMaxWorth.resize(MaxWeight + 1);
    fill(TempMaxWorth.begin(), TempMaxWorth.end(), 0);
    vector<Treasure> AllTreasureCollection;
    for (int j = 0; j < TreasureNum; ++j) {
      Treasure T;
      cin >> T.Worth;
      cin.get();
      cin >> T.Weight;
      AllTreasureCollection.push_back(T);
    }
    sort(AllTreasureCollection.begin(), AllTreasureCollection.end(),
         [](const Treasure a, const Treasure b) {
           return ((float)(a.Worth) / (float)(a.Weight)) >
                  ((float)(b.Worth) / (float)(b.Weight));
         });
    vector<Treasure> NowTreasureCollection = {};
    GetMaxWorth(MaxWeight, NowTreasureCollection, AllTreasureCollection.begin(),
                AllTreasureCollection);
    cout << "Case " << i << ":" << endl
         << TempMaxWorth[MaxWeight] << endl
         << find(TempMaxWorth.cbegin(), TempMaxWorth.cend(),
                 TempMaxWorth[MaxWeight]) -
                TempMaxWorth.cbegin()
         << endl;
  }
  return 0;
}