fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6.  
  7. public class Main {
  8.  
  9. public static void main(String[] args) {
  10. try {
  11. String input=br.readLine();
  12. int Number_Of_TestCases=Integer.parseInt(input);
  13. //System.out.println("no. of test cases= "+ Number_Of_TestCases);
  14. ArrayList<Integer> Prices=new ArrayList<>();
  15. int price;
  16. int Minimum_Price = 0;
  17. int t;
  18.  
  19. for (int i=0;i<Number_Of_TestCases;i++)
  20. {
  21. while(!(input=br.readLine()).equals("0") )
  22. {
  23.  
  24. price=Integer.parseInt(input);
  25. Prices.add(price);
  26. }
  27. //System.out.println("before "+Prices);
  28. Collections.sort(Prices); /// take care ,,these 2 lines should be together to sort descendingly
  29. Collections.reverse(Prices);
  30. ///System.out.println("after "+Prices);
  31. for(int j=0;j<Prices.size();j++)
  32. {
  33. t= (int) (2* Math.pow(Prices.get(j), j+1) ) ;
  34. //System.out.println("t= "+t);
  35. Minimum_Price=Minimum_Price+t;
  36. //System.out.println("2*" +Minimum_Price);
  37. if (t>500000)
  38. {
  39. System.out.print("Too expensive");
  40. break;
  41. }
  42.  
  43. else
  44. {
  45. System.out.print(Minimum_Price);
  46.  
  47. }
  48. ////////////////////////reset
  49. Minimum_Price=0;
  50. Prices.clear();
  51. } //end of for(int j=0;j<Prices.size();j++)
  52.  
  53. }
  54.  
  55.  
  56. } catch (IOException e) {
  57. // TODO Auto-generated catch block
  58. e.printStackTrace();
  59. }
  60. }
  61.  
  62. }
Success #stdin #stdout 0.08s 380224KB
stdin
3
7
2
10
0
20
29
31
0
42
41
40
37
20
0
stdout
206284