Hi.
I get a solution for PIGBANK problem(spoj.com/problems/PIGBANK/). I tested my solution with all test cases in this forum, all test cases in problem's comments and some others created for me. I seems a correct solution but i get runtime error(SIGSEGV). Can't anybody help me??
Here is my code.
#include <iostream>
using namespace std;
int main() {
int t;
cin >> t;
while (t--)
{
int pw, tw;
cin >> pw >> tw;
int cw = tw - pw;
int cc;
cin >> cc;
int coinsValue[cc];
int coinsWeight[cc];
for(int i = 0 ; i < cc ; ++i)
{
cin >> coinsValue[i] >> coinsWeight[i];
}
unsigned long long m[cc][cw];
for (int i = 0 ; i < cc ; ++i)
{
for (int j = 0 ; j < cw ; ++j)
{
unsigned long long up = 0;
if (i > 0)
up = m[i - 1][j];
int temp = j + 1 - coinsWeight[i];
if (temp < 0)
m[i][j] = up;
else if (temp > 0)
{
if (m[i][temp - 1] != 0 && up == 0)
m[i][j] = coinsValue[i] + m[i][temp - 1];
else if (m[i][temp - 1] == 0 && up != 0)
m[i][j] = up;
else if (m[i][temp - 1] != 0 && up != 0)
m[i][j] = min(coinsValue[i] + m[i][temp - 1], up);
else
m[i][j] = 0;
}
else
{
if (up != 0)
m[i][j] = min(up, (unsigned long long)coinsValue[i]);
else
m[i][j] = coinsValue[i];
}
}
}
if (m[cc - 1][cw - 1] != 0)
cout << "The minimum amount of money in the piggy-bank is " << m[cc - 1][cw - 1] << "." << endl;
else
cout << "This is impossible." << endl;
}
return 0;
}