Skip to content

Instantly share code, notes, and snippets.

@alucky0707
Last active December 16, 2015 09:19
Show Gist options
  • Save alucky0707/5412404 to your computer and use it in GitHub Desktop.
Save alucky0707/5412404 to your computer and use it in GitHub Desktop.
POJ 3627 & POJ 3628
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
final int N = scan.nextInt();
final long B = scan.nextLong();
PriorityQueue<Integer> H = new PriorityQueue<Integer>(N, new Comparator<Integer>(){
@Override
public int compare(Integer a, Integer b) {
return b.intValue() - a.intValue();
}
});
for(int i = 0; i < N; i++) {
H.add(scan.nextInt());
}
scan.close();
int n = 0;
long h = 0;
while(h < B) {
h += H.poll().longValue();
n++;
}
System.out.println(n);
}
}
import java.util.Scanner;
class Main {
private static long[] H;
private static int N;
private static long B;
private static long ans = Long.MAX_VALUE;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
N = scan.nextInt();
B = scan.nextLong();
H = new long[N];
for(int i = 0; i < N; i++) {
H[i] = scan.nextInt();
}
scan.close();
dfs(0,0);
System.out.println(ans);
}
private static void dfs(int i, long sum) {
if(sum >= B){
ans = Math.min(ans, sum-B);
} else if(i < N) {
dfs(i+1,sum+H[i]);
dfs(i+1,sum);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment