Advertisement
Ruhan_DIU

Problem E - Typ A

Aug 8th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. // Problem E - ICPC Regional ---- typ -"a"
  2.  
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.StringTokenizer;
  9.  
  10. /**
  11. *
  12. * @author Ruhan
  13. */
  14. public class Main2 {
  15. //Fastest I/O Class Start
  16. static class FastReader{
  17. BufferedReader br;
  18. StringTokenizer st;
  19.  
  20. public FastReader(){
  21. br=new BufferedReader(new InputStreamReader(System.in));
  22. }
  23.  
  24. String next(){
  25. while(st==null || !st.hasMoreElements()){
  26. try{
  27. st=new StringTokenizer(br.readLine());
  28. }catch(Exception e){
  29.  
  30. }
  31. }
  32. return st.nextToken();
  33. }
  34.  
  35. int nextInt(){
  36. return Integer.parseInt(next());
  37. }
  38.  
  39. long nextLong(){
  40. return Long.parseLong(next());
  41. }
  42.  
  43. double nextDouble(){
  44. return Double.parseDouble(next());
  45. }
  46.  
  47. String nextLine(){
  48. String str="";
  49. try{
  50. str=br.readLine();
  51. }catch(Exception e){
  52.  
  53. }
  54. return str;
  55. }
  56.  
  57. }
  58. //Fastest I/O Class End
  59. public static void main(String[] args) {
  60. FastReader in=new FastReader();
  61.  
  62. for(int r=0;r<r+1;r++){
  63. int a=in.nextInt();
  64. if(a==0){
  65. break;
  66. }else{
  67.  
  68. int b=in.nextInt();
  69.  
  70. int[] arr=new int[b];
  71.  
  72. for (int i = 0; i < b; i++) {
  73. arr[i]=in.nextInt();
  74. }
  75.  
  76. ArrayList<Integer> arl=new ArrayList<>();
  77.  
  78. boolean check=false;
  79. boolean stop=false;
  80. for (int i = 0; i < b; i++) {
  81. if(arr[i]==a){ // 1= no,
  82. arl.add(arr[i]);
  83. check=true;
  84. break;
  85. }else{
  86. if(arr[i]<a){// 1=yes,
  87. //Either this is sequently oky or not
  88. int tress=arr[i];
  89. arl.add(arr[i]);
  90. for (int j = i+1; j < b; j++) {
  91. if(tress+arr[j]==a){
  92. arl.add(arr[j]);
  93. stop=true;
  94. break;
  95. }
  96. else if(tress+arr[j]<a){
  97. arl.add(arr[j]);
  98. tress+=arr[j];
  99. }
  100. }
  101. //End of Either
  102. if(stop==true){
  103. check=true;
  104. }else{
  105. arl.clear();
  106. arl.add(arr[i]); //list =1,
  107. int res=arr[i]; //1
  108.  
  109. for (int j = i+1; j < b; j++) { //res=1
  110. if(res+arr[j]==a){ // 1+2 != 5 no, 3+3!=5
  111. arl.add(arr[j]);
  112. check=true;
  113. break;
  114. }
  115. else if(res+arr[j]<a){ //1+2<5 yes,
  116. arl.add(arr[j]);
  117. res=res+arr[j];
  118. }
  119. else if(res+arr[j]>a){
  120. //arl_temp=arl;
  121. int res1=arr[j];
  122. int index=0;
  123.  
  124. for(int m:arl){
  125. res1=res1+m;
  126. if(res1>a){
  127. break;
  128. }else{
  129. if(res1==a){
  130. arl.add(index+1, arr[j]);
  131. check=true;
  132. break;
  133. }
  134. }
  135. index++;
  136. }
  137.  
  138. }
  139. if(check==true){
  140. break;
  141. }
  142. } //j loop
  143. }
  144.  
  145.  
  146.  
  147. }
  148. }
  149.  
  150.  
  151.  
  152. if(check==true){
  153. break;
  154. }else{
  155. arl.clear();
  156. }
  157. }
  158.  
  159. int sum=0;
  160.  
  161. if(arl.isEmpty()==true){
  162. System.out.println("0");
  163. }else{
  164. for(int w:arl){
  165. sum=sum+w;
  166. System.out.print(w+" ");
  167. if(sum==a){
  168. break;
  169. }
  170. }
  171. System.out.println(a);
  172.  
  173. }
  174. }
  175. }
  176.  
  177.  
  178. }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement