I'm trying to solve Party Schedule problem and getting a Wrong Answer when I submit.
Can somebody tell me whats wrong with it ? Please help me
import java.util.Scanner;
public class PARTY
{
static // Instance-scope fields
String print ="";
int n; // Number of objects
Item[] avail;// Available objects for use
Item[] used; // objects actually used
int nUsed;
int maxWeight; // Weight limit of the knapsack.
static boolean end=true;
static Scanner inp ;
public static void main ( String[] args ) throws Exception
{
PARTY knap = new PARTY();
inp = new Scanner (System.in);
while(end==true){
boolean vratil= knap.initialize(inp);
if(vratil==true){
knap.fill();
knap.report();
}
}
System.out.println(print);
}
public void selectionSort(){
for(int i = 0; i < avail.length - 1; i++){
int maxIndex = i;
for(int j = i + 1; j < avail.length; j++){
if(avail[j].vpw > avail[maxIndex].vpw) maxIndex = j;
}
Item tmp = avail[i];
avail[i] = avail[maxIndex];
avail[maxIndex] = tmp;
}
}
public boolean initialize(Scanner inp) throws Exception
{
boolean ok = true;
String lineIn;
lineIn = inp.next();
maxWeight = Integer.parseInt(lineIn.trim());
lineIn = inp.next();
n = Integer.parseInt(lineIn.trim());
if(maxWeight==0 && n==0){
end=false;
ok= false;
}
if(maxWeight>500 || n>100){
ok= false;
for(int i=0;i<=n;i++){
inp.nextLine();
}
}
if(maxWeight<=500 && n<=100){
used = new Item[n];
avail = new Item[n];
int wt,val;
for ( int k = 0; k < n; k++ )
{
lineIn = inp.next();
wt = Integer.parseInt(lineIn.trim());
lineIn = inp.next();
val = Integer.parseInt(lineIn.trim());
if(5<=wt&&wt<=25&&0<=val&&val<=10){
avail[k] = new Item(wt, val);
}
else{
avail[k] = new Item(0, 0);
}
}
}
return ok;
}
void fill()
{ int k;
Item nxt;
double remain = maxWeight;
nUsed = 0;
selectionSort();
for ( k = 0 ; remain > 0 && k < avail.length ; k++ )
{ nxt = avail[k];
if ( nxt.w <= remain && nxt.v !=0 ){
nxt.x = 1;
remain -= nxt.x * nxt.w;
used[nUsed++] = nxt;
}
}
}
void report()
{
int fun = 0, money=0;
for ( int k = 0; k < nUsed; k++ )
{
money=money+(int)used[k].w;
fun=fun+(int)(used[k].v);
}
print=print+ money+" "+fun+ "\n";
System.out.println();
}
}
class Item implements Comparable
{ double w, // For each object, the weight of the object
v, // For each object, the value of the object
x; // For each object, the fraction of the object used
double vpw; // For each object, the value-per-weight ratio
Item(double w, double v)
{ this.x = 0; this.w = w; this.v = v; this.vpw = v / w; }
Item(Item c)
{ this.w = c.w; this.w = c.w; this.v = c.v; this.vpw = c.vpw; }
// Comparable insists on the following method signature:
public int compareTo(Object x)
{ Item rt = (Item) x;
return vpw < rt.vpw? +1 : vpw > rt.vpw ? -1 : 0;
}
}