runtime error

Revision en1, by electro177, 2021-06-07 10:04:35

Please help me with run time error, this is the cses book shop problem

with all type int ,it is accepted ,while with all long long it gives run time error,I thought of always using long long because it is helpful, can u please say when to not use long long

( run time error)//////

include<bits/stdc++.h>

include

using namespace std;

define rep(i,x,y) for(ll i=x; i<=y; i++)

define ll long long

void task() { ll n,target; cin >> n >> target; vector price(n), pages(n); for (ll&v : price) cin >> v; for (ll&v : pages) cin >> v;

vector<vector> dp(n + 1, vector(target + 1, 0));

// dp[0][0] = 0;

rep(i, 1, n) { rep(j, 0, target) {

dp[i][j] = dp[i - 1][j];
  int left = j - price[i - 1];
  if (left >= 0)
    dp[i][j] = max(dp[i][j] ,  dp[i - 1][j - price[i - 1]] + pages[i - 1] );

}

}

cout << dp[n][target] << '\n'; }

int main() { int case = 1 ;

for (int i = 0; i < case; i++) task(); }

(accepted)//////

include<bits/stdc++.h>

include

using namespace std;

define rep(i,x,y) for(int i=x; i<=y; i++)

define ll long long

void task() { int n, target; cin >> n >> target; vector price(n), pages(n); for (int&v : price) cin >> v; for (int&v : pages) cin >> v;

vector<vector> dp(n + 1, vector(target + 1, 0));

// dp[0][0] = 0;

rep(i, 1, n) { rep(j, 0, target) {

dp[i][j] = dp[i - 1][j];
  int left = j - price[i - 1];
  if (left >= 0)
    dp[i][j] = max(dp[i][j] ,  dp[i - 1][j - price[i - 1]] + pages[i - 1] );

}

}

cout << dp[n][target] << '\n'; }

int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); int case = 1 ; for (int i = 0; i < case; i++) task(); }

Tags #runtime, #help

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English electro177 2021-06-07 10:04:35 1901 Initial revision (published)