'Java Execution timeout
I'm trying to solve BaekJoon 12865 problem by Java.
But it evaluates Execution timeout.
There isn't any error in eclipse compiler.
Someone know about problem in my codes.
Please tell me what did i do wrong.
This problem is find Maximum value in K weight by N objects that has V(value) and W(weight).
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
class Object implements Comparable<Object>{
private int W; //object's weight
private int V; //object's value
private int VbyW;
public Object(int w, int v) {
this.W = w;
this.V = v;
this.VbyW = v/w; // value by weight
}
@Override
public int compareTo(Object object)
{
if (object.VbyW > VbyW)
{
return 1;
}else if(object.VbyW < VbyW){
return -1;
}else
return 0;
}
}
int N; // number of object
int K; // Maximum of weight that I can have
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
K = sc.nextInt();
//first I got number of objects and Maximum weight
int W[] = new int[N];
int V[] = new int[N];
Object[] obj = new Object[N];
ArrayList<Object> objs = new ArrayList<>();
for (int i=0;i<N;i++)
{
W[i] = sc.nextInt();
V[i] = sc.nextInt();
obj[i] = new Object(W[i],V[i]);
objs.add(obj[i]);
} // I'll get all object's value and weight
Collections.sort(objs, Collections.reverseOrder());
int totalV=0;
for(int j=0;j<N;j++)
{
while(K>objs.get(j).W)
{
totalV +=objs.get(j).V;
}
} // I'll find Maximum value in K weight by objects
System.out.println(totalV);
sc.close();
}
}
problem site : https://www.acmicpc.net/problem/12865
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|