Advertisement
Ruhan_DIU

Problem E - Typ B

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