Reputation: 43
My code works fine with small numbers when dp[105][x]
when i keep x
small. But when long long
input comes i need to change the x
to larger value greater than 10^9
then this error comes
/tmp/cc35Ue0y.o: In function `__static_initialization_and_destruction_0(int, int)': main.cpp:(.text+0x294): relocation truncated to fit: R_X86_64_32 against `.bss' main.cpp:(.text+0x2a3): relocation truncated to fit: R_X86_64_32 against `.bss'
if i dont change it gives segmentation fault.
My code is for Atcode DP contest E problem https://atcoder.jp/contests/dp/tasks/dp_e
code :
#include <bits/stdc++.h>
using namespace std;
int n, v[105];
long long int wt[105] , W, dp[105][105];
long long int fun(int n, long long int W, long long int wt[], int v[]) {
if(n == 0 || W == 0) return 0;
if(dp[n][W] != -1) return dp[n][W];
if(wt[n] <= W) {
return dp[n][W] = max(v[n] + fun(n-1, W - wt[n], wt, v), fun(n-1, W, wt, v));
}
else {
return dp[n][W] = fun(n-1, W, wt, v);
}
}
int main()
{
cin >> n >> W;
// for(int i=0;i<N;i++) {
// for(int j=0;j<2;j++) {
// cin >> a[i][j];
// }
// }
for(int i=1;i<=n;i++) {
cin >> wt[i] >> v[i];
}
memset(dp, -1, sizeof(dp));
cout << fun(n, W, wt, v);
return 0;
}
What changes do i need to make.
Upvotes: 2
Views: 2026
Reputation: 218138
Stack is generally limited, use dynamic memory to handle that, as std::vector
or auto dp = new std::array<std::array<long long int, 105>, 1'000'000'000>;
but size seems even huge for heap...
You probably have to change your algorithm.
Upvotes: 2