Skip to content

Instantly share code, notes, and snippets.

@ewmson
Created October 23, 2017 20:20
Show Gist options
  • Save ewmson/b4999caaaa6c365f8c3ec4eb9f4c4342 to your computer and use it in GitHub Desktop.
Save ewmson/b4999caaaa6c365f8c3ec4eb9f4c4342 to your computer and use it in GitHub Desktop.
import java.util.*;
import java.io.*;
public class K {
public static void main(String[] args) throws Exception {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer line = new StringTokenizer(input.readLine());
int b = Integer.parseInt(line.nextToken());
int n = Integer.parseInt(line.nextToken());
int e = Integer.parseInt(line.nextToken());
int m = (b + n + e)/2;
line = new StringTokenizer(input.readLine());
long sb = Integer.parseInt(line.nextToken());
long sn = Integer.parseInt(line.nextToken());
long se = Integer.parseInt(line.nextToken());
line = new StringTokenizer(input.readLine());
long[] kayaks = new long[m];
for (int i = 0; i < m; i++) {
kayaks[i] = Integer.parseInt(line.nextToken());
}
Arrays.sort(kayaks);
long lo = 0;
long hi = 20000 * 100000;
while (lo <= hi) {
long mid = (lo + hi) >>> 1;
boolean midVal = possible(kayaks, mid, b, n, e, sb, sn, se);
if (midVal) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
System.out.println(lo - 1);
}
private static boolean possible(long[] kayaks, long speed, int b, int n, int e, long sb, long sn, long se) {
for (long c : kayaks) {
if (b > 1 && (sb + sb) * c >= speed) {
b--;
b--;
continue;
}
if (b > 0 && n > 0 && (sb + sn) * c >= speed) {
b--;
n--;
continue;
}
if ((sb + se) < (sn * 2)) {
if (b > 0 && e > 0 && (sb + se) * c >= speed) {
b--;
e--;
continue;
}
if (n > 1 && (sn + sn) * c >= speed) {
n--;
n--;
continue;
}
} else {
if (n > 1 && (sn + sn) * c >= speed) {
n--;
n--;
continue;
}
if (b > 0 && e > 0 && (sb + se) * c >= speed) {
b--;
e--;
continue;
}
}
if (n > 0 && e > 0 && (sn + se) * c >= speed) {
n--;
e--;
continue;
}
if (e > 1 && (se + se) * c >= speed) {
e--;
e--;
continue;
}
return false;
}
return true;
}
}
@stuharvey
Copy link

i forgot how beautiful programming team problems can be

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment