ec1117

Untitled

Jul 4th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. import java.io.*;
  5. import java.io.BufferedReader;
  6. import java.awt.Point;
  7.  
  8. public class tallbarn{
  9. public static final String TASKNAME = "tallbarn";
  10. public static long mod= 1000000007;
  11. public static Debug db;
  12.  
  13. public static void main(String[] args) throws IOException {
  14. // InputReader in = new InputReader(System.in);
  15. // PrintWriter out = new PrintWriter(System.out);
  16. InputReader in = new InputReader(new FileInputStream(TASKNAME+".in"));
  17. PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(TASKNAME+".out")));
  18. Autocompletion solver = new Autocompletion();
  19. db=new Debug(System.getSecurityManager()==null);
  20. solver.solve(1, in, out);
  21. out.close();
  22. }
  23. static class Autocompletion {
  24.  
  25. public void solve(int testNumber, InputReader in, PrintWriter out) {
  26. int n=in.nextInt();
  27. long k=in.nextLong();
  28. long arr[]=new long[n];
  29. long sum=0;
  30. for(int i=0;i<n;i++) {
  31. arr[i]=in.nextLong();
  32. sum+=arr[i];
  33. }
  34. double ans[]=new double[n];//how many workers should be here
  35. double temp=(double)((double)k/sum);
  36. for(int i=0;i<n;i++) {
  37. ans[i]=temp*arr[i];
  38. }
  39. TreeSet<Pair> take=new TreeSet<Pair>();
  40. for(int i=0;i<n;i++) {
  41. if((long)ans[i]!=0 && (long)ans[i]!=1) {
  42. 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
  43. }
  44. }
  45. long sum2=0;//workers used if lower bound
  46. for(int i=0;i<n;i++) {
  47. if((long)ans[i]==0) {//have to give it one
  48. sum2++;
  49. ans[i]=1.0;
  50. Pair y=take.pollFirst();
  51. if(y!=null) {
  52. ans[y.y]--;
  53. if((long)ans[y.y]!=1) {
  54. take.add(new Pair(( (double)arr[i]/(long)(ans[i]-1)-(double)arr[i]/((long)(ans[i])) ),y.y));
  55. }
  56. }
  57. }
  58. else {
  59. sum2+=ans[i];
  60. }
  61. }
  62. Pair arr2[]=new Pair[n];//time saved by giving one here
  63. for(int i=0;i<n;i++) {
  64. arr2[i]=new Pair(((double)arr[i]/(long)ans[i]-(double)arr[i]/((long)(ans[i]+1))),i);//time saved by giving one here
  65. if(ans[i]==0) {
  66. int test[]=new int[8];
  67. timeout();
  68. System.out.println(test[8]);
  69.  
  70. }
  71. }
  72. Arrays.sort(arr2, Collections.reverseOrder());
  73. double ret=0;
  74. for(int i=0;i<k-sum2;i++) {
  75. ans[arr2[i].y]++;
  76. }
  77. for(int i=0;i<n;i++) {
  78. ret+=(double)arr[i]/(long)ans[i];
  79. }
  80. // db.debug("ans", ans);
  81. // db.debug("arr2", arr2);
  82. // db.debug("ret", ret);
  83. out.println(Math.round(ret));
  84. }
  85.  
  86. private void timeout() {
  87. ArrayList<Integer> list=new ArrayList<Integer>();
  88. for(int i=0;i<Integer.MAX_VALUE;i++) {
  89. list.add(i);
  90. }
  91.  
  92. }
  93. }
  94. static class Pair implements Comparable<Pair>{
  95. double x;
  96. int y;
  97. Pair(double d, int b){
  98. x=d;
  99. y=b;
  100. }
  101. @Override
  102. public int compareTo(Pair arg0) {
  103. if(arg0.x!=x)return (int) (x-arg0.x);
  104. return y-arg0.y;
  105. }
  106. }
  107. static class Triple implements Comparable<Triple>{
  108. int x;
  109. int y;
  110. int z;
  111. Triple(int a, int b, int c){
  112. x=a;
  113. y=b;
  114. z=c;
  115. }
  116. @Override
  117. public int compareTo(Triple arg0) {
  118. if(arg0.x!=x)return x-arg0.x;
  119. return y-arg0.y;
  120. }
  121. }
  122.  
  123. static class InputReader {
  124. public BufferedReader reader;
  125. public StringTokenizer tokenizer;
  126.  
  127. public InputReader(InputStream stream) {
  128. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  129. tokenizer = null;
  130. }
  131.  
  132. public String next() {
  133. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  134. try {
  135. tokenizer = new StringTokenizer(reader.readLine());
  136. } catch (IOException e) {
  137. throw new RuntimeException(e);
  138. }
  139. }
  140. return tokenizer.nextToken();
  141. }
  142.  
  143. public int nextInt() {
  144. return Integer.parseInt(next());
  145. }
  146. public long nextLong() {
  147. return Long.parseLong(next());
  148. }
  149. public int[] nextIntArr(int n) {
  150. int arr[]=new int[n];
  151. for(int i=0;i<n;i++) {
  152. arr[i]=this.nextInt();
  153. }
  154. return arr;
  155. }
  156. }
  157. public static class Debug {
  158. private boolean allowDebug;
  159.  
  160. public Debug(boolean allowDebug) {
  161. this.allowDebug = allowDebug;
  162. }
  163.  
  164. private void outputName(String name) {
  165. System.out.print(name + " = ");
  166. }
  167.  
  168. public void debug(String name, int x) {
  169. if (!allowDebug) {
  170. return;
  171. }
  172.  
  173. outputName(name);
  174. System.out.println("" + x);
  175. }
  176.  
  177. public void debug(String name, long x) {
  178. if (!allowDebug) {
  179. return;
  180. }
  181. outputName(name);
  182. System.out.println("" + x);
  183. }
  184.  
  185. public void debug(String name, double x) {
  186. if (!allowDebug) {
  187. return;
  188. }
  189. outputName(name);
  190. System.out.println("" + x);
  191. }
  192.  
  193. public void debug(String name, int[] x) {
  194. if (!allowDebug) {
  195. return;
  196. }
  197. outputName(name);
  198. System.out.println(Arrays.toString(x));
  199. }
  200.  
  201. public void debug(String name, long[] x) {
  202. if (!allowDebug) {
  203. return;
  204. }
  205. outputName(name);
  206. System.out.println(Arrays.toString(x));
  207. }
  208.  
  209. public void debug(String name, double[] x) {
  210. if (!allowDebug) {
  211. return;
  212. }
  213. outputName(name);
  214. System.out.println(Arrays.toString(x));
  215. }
  216.  
  217. public void debug(String name, Pair[] x) {
  218. if (!allowDebug) {
  219. return;
  220. }
  221. outputName(name);
  222. StringBuilder sb = new StringBuilder("[");
  223. int cnt=0;
  224. for(Pair y:x) {
  225. sb.append("("+y.x+","+y.y+')');
  226. if (cnt != x.length-1)sb.append(", ");
  227. cnt++;
  228. }
  229. System.out.println(sb.append("]").toString());
  230. }
  231.  
  232. public void debug(String name, Object x) {
  233. if (!allowDebug) {
  234. return;
  235. }
  236. outputName(name);
  237. System.out.println("" + x);
  238. }
  239.  
  240. public void debug(String name, Object... x) {
  241. if (!allowDebug) {
  242. return;
  243. }
  244. outputName(name);
  245. System.out.println(Arrays.deepToString(x));
  246. }
  247. }
  248. }
Add Comment
Please, Sign In to add comment