Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import java.io.BufferedReader;
- import java.awt.Point;
- public class tallbarn{
- public static final String TASKNAME = "tallbarn";
- public static long mod= 1000000007;
- public static Debug db;
- public static void main(String[] args) throws IOException {
- // InputReader in = new InputReader(System.in);
- // PrintWriter out = new PrintWriter(System.out);
- InputReader in = new InputReader(new FileInputStream(TASKNAME+".in"));
- PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(TASKNAME+".out")));
- Autocompletion solver = new Autocompletion();
- db=new Debug(System.getSecurityManager()==null);
- solver.solve(1, in, out);
- out.close();
- }
- static class Autocompletion {
- public void solve(int testNumber, InputReader in, PrintWriter out) {
- int n=in.nextInt();
- long k=in.nextLong();
- long arr[]=new long[n];
- long sum=0;
- for(int i=0;i<n;i++) {
- arr[i]=in.nextLong();
- sum+=arr[i];
- }
- double ans[]=new double[n];//how many workers should be here
- double temp=(double)((double)k/sum);
- for(int i=0;i<n;i++) {
- ans[i]=temp*arr[i];
- }
- TreeSet<Pair> take=new TreeSet<Pair>();
- for(int i=0;i<n;i++) {
- if((long)ans[i]!=0 && (long)ans[i]!=1) {
- take.add(new Pair(( (double)arr[i]/(long)(ans[i]-1)-(double)arr[i]/((long)(ans[i])) ),i));//take out by smallest amount of time added if decreased by 1
- }
- }
- long sum2=0;//workers used if lower bound
- for(int i=0;i<n;i++) {
- if((long)ans[i]==0) {//have to give it one
- sum2++;
- ans[i]=1.0;
- Pair y=take.pollFirst();
- if(y!=null) {
- ans[y.y]--;
- if((long)ans[y.y]!=1) {
- take.add(new Pair(( (double)arr[i]/(long)(ans[i]-1)-(double)arr[i]/((long)(ans[i])) ),y.y));
- }
- }
- }
- else {
- sum2+=ans[i];
- }
- }
- Pair arr2[]=new Pair[n];//time saved by giving one here
- for(int i=0;i<n;i++) {
- arr2[i]=new Pair(((double)arr[i]/(long)ans[i]-(double)arr[i]/((long)(ans[i]+1))),i);//time saved by giving one here
- if(ans[i]==0) {
- int test[]=new int[8];
- timeout();
- System.out.println(test[8]);
- }
- }
- Arrays.sort(arr2, Collections.reverseOrder());
- double ret=0;
- for(int i=0;i<k-sum2;i++) {
- ans[arr2[i].y]++;
- }
- for(int i=0;i<n;i++) {
- ret+=(double)arr[i]/(long)ans[i];
- }
- // db.debug("ans", ans);
- // db.debug("arr2", arr2);
- // db.debug("ret", ret);
- out.println(Math.round(ret));
- }
- private void timeout() {
- ArrayList<Integer> list=new ArrayList<Integer>();
- for(int i=0;i<Integer.MAX_VALUE;i++) {
- list.add(i);
- }
- }
- }
- static class Pair implements Comparable<Pair>{
- double x;
- int y;
- Pair(double d, int b){
- x=d;
- y=b;
- }
- @Override
- public int compareTo(Pair arg0) {
- if(arg0.x!=x)return (int) (x-arg0.x);
- return y-arg0.y;
- }
- }
- static class Triple implements Comparable<Triple>{
- int x;
- int y;
- int z;
- Triple(int a, int b, int c){
- x=a;
- y=b;
- z=c;
- }
- @Override
- public int compareTo(Triple arg0) {
- if(arg0.x!=x)return x-arg0.x;
- return y-arg0.y;
- }
- }
- static class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) {
- reader = new BufferedReader(new InputStreamReader(stream), 32768);
- tokenizer = null;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public int[] nextIntArr(int n) {
- int arr[]=new int[n];
- for(int i=0;i<n;i++) {
- arr[i]=this.nextInt();
- }
- return arr;
- }
- }
- public static class Debug {
- private boolean allowDebug;
- public Debug(boolean allowDebug) {
- this.allowDebug = allowDebug;
- }
- private void outputName(String name) {
- System.out.print(name + " = ");
- }
- public void debug(String name, int x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, long x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, double x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, int[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.toString(x));
- }
- public void debug(String name, long[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.toString(x));
- }
- public void debug(String name, double[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.toString(x));
- }
- public void debug(String name, Pair[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- StringBuilder sb = new StringBuilder("[");
- int cnt=0;
- for(Pair y:x) {
- sb.append("("+y.x+","+y.y+')');
- if (cnt != x.length-1)sb.append(", ");
- cnt++;
- }
- System.out.println(sb.append("]").toString());
- }
- public void debug(String name, Object x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, Object... x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.deepToString(x));
- }
- }
- }
Add Comment
Please, Sign In to add comment