TLE in Educational DP Contest: D

Revision en2, by sibindon, 2020-07-07 06:51:39

I don't understand why my code is giving TLE. Not able to catch the bug. Any help would be appreciated !!!

# define    all(a)              a.begin(), a.end()
#define MOD (long long int )1e9+7
using namespace std;

typedef long long int ll;

ll dp[110][100010];
ll solve(int index, ll currweight, ll weight, vector<vector<int>> nums, int n) {
    if (index == n)
        return 0;
    if (dp[index][currweight] != -1)
        return dp[index][currweight];
    ll res = solve(index+1, currweight, weight, nums, n);
    if (currweight+nums[index][0] <= weight) {
        res = max(res, nums[index][1] + solve(index+1, currweight+nums[index][0], weight, nums, n));
    dp[index][currweight] = res;
    return res;

int main() {
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
    int n, k;
    cin >> n >> k;
    vector<vector<int>> nums(n, vector<int>(2));
    for (int i=0; i<n; i++) cin >> nums[i][0] >> nums[i][1];
    memset(dp, -1, sizeof(dp));
    ll res = solve(0, 0, k, nums, n); 
    cout << res << endl;
    return 0;


  Rev. Lang. By When Δ Comment
en3 English sibindon 2020-07-07 09:42:42 63
en2 English sibindon 2020-07-07 06:51:39 1 Tiny change: 'n\n~~~~~\n #include<b' -> 'n\n~~~~~\n#include<b'
en1 English sibindon 2020-07-07 06:46:32 1298 Initial revision (published)