Why is my code for the Knapsack problem on CodeChef not working?

Here’s my code: TlAzrj - Online C++0x Compiler & Debugging Tool - Ideone.com
Here’s the problem statement: KNAPSACK Problem - CodeChef

This passes the example test case. However I get a Wrong Answer (WA) while submitting.

The code works on the SPOJ version of the problem as well, when modified to suit the input format of the SPOJ version.

Here’s my code for the SPOJ version: z8SuKK - Online C++0x Compiler & Debugging Tool - Ideone.com
Here’s the SPOJ problem statement: SPOJ.com - Problem KNAPSACK

Please help.