You are not logged in. Please login at www.codechef.com to post your questions!

×

Code giving WA on c++(gcc 4.8.1) but getting accepted on c++(gcc 4.3.2)?

Here is the problem that I am attempting: http://www.codechef.com/problems/PPTEST

A simple knapsack problem solved using DP.

Here is the code that is getting accepted when I choose C++(gcc 4.3.2) as my language but giving WA for C++(gcc 4.8.1). Can somebody help me figure it out what could it be because of?

#include<cstdio>
#include<iostream>

using namespace std;

#define MAXN 100
int c[MAXN],p[MAXN],t[MAXN],dp[MAXN+1]={0};

int max(int a,int b)
{
    if(a>b)return a;
    else return b;
}

int main()
{
    int te;
    scanf("%d",&te);
    while(te--)
    {
        int n,w;
        scanf("%d%d",&n,&w);
        for(int i=0;i<=w;i++)dp[i]=0;
        for(int i=0;i<n;i++)scanf("%d%d%d",&c[i],&p[i],&t[i]);
        for(int i=0;i<n;i++)c[i]*=p[i];
        for(int i=0;i<n;i++)
            for(int j=w;j>=0;j--)
                dp[t[i]+j]=max(dp[j]+c[i],dp[t[i]+j]);
        int ans=0;
        for(int i=0;i<=w;i++)
            ans=max(ans,dp[i]);
        printf("%d\n",ans);
    }
}
This question is marked "community wiki".

asked 21 Oct '13, 03:25

anubhav94's gravatar image

4★anubhav94
46115
accept rate: 0%

edited 21 Oct '13, 03:54

tijoforyou's gravatar image

2★tijoforyou
4.2k52364


you have problems in setting max size of dp array. Note that t[i] + j can go greater than MAX (100 in your case) that will make problems

link

answered 21 Oct '13, 04:17

dpraveen's gravatar image

4★dpraveen ♦♦
2.5k52134169
accept rate: 20%

edited 21 Oct '13, 04:17

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×24
×8

question asked: 21 Oct '13, 03:25

question was seen: 1,212 times

last updated: 21 Oct '13, 04:17