Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.StringTokenizer;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Ruhan
- */
- public class Main2 {
- //Fastest I/O Class Start
- static class FastReader{
- BufferedReader br;
- StringTokenizer st;
- public FastReader(){
- br=new BufferedReader(new InputStreamReader(System.in));
- }
- String next(){
- while(st==null || !st.hasMoreElements()){
- try{
- st=new StringTokenizer(br.readLine());
- }catch(Exception e){
- }
- }
- return st.nextToken();
- }
- int nextInt(){
- return Integer.parseInt(next());
- }
- long nextLong(){
- return Long.parseLong(next());
- }
- double nextDouble(){
- return Double.parseDouble(next());
- }
- String nextLine(){
- String str="";
- try{
- str=br.readLine();
- }catch(Exception e){
- }
- return str;
- }
- }
- //Fastest I/O Class End
- public static void main(String[] args) {
- FastReader in=new FastReader();
- for(int r=0;r<r+1;r++){
- int a=in.nextInt();
- if(a==0){
- break;
- }else{
- int b=in.nextInt();
- int[] arr=new int[b];
- for (int i = 0; i < b; i++) {
- arr[i]=in.nextInt();
- }
- ArrayList<Integer> arl=new ArrayList<>();
- boolean check=false;
- for (int i = 0; i < b; i++) {
- if(arr[i]==a){ // 1= no,
- arl.add(arr[i]);
- check=true;
- break;
- }else{
- if(arr[i]<a){// 1=yes,
- arl.add(arr[i]); //list =1,
- int res=arr[i]; //1
- for (int j = i+1; j < b; j++) { //res=1
- if(res+arr[j]==a){ // 1+2 != 5 no, 3+3!=5
- arl.add(arr[j]);
- check=true;
- break;
- }
- else if(res+arr[j]<a){ //1+2<5 yes,
- arl.add(arr[j]);
- res=res+arr[j];
- }
- else if(res+arr[j]>a){
- int res1=arr[j];
- int index=0;
- for(int m:arl){
- res1=res1+m;
- if(res1>a){
- break;
- }else{
- if(res1==a){
- arl.add(index+1, arr[j]);
- check=true;
- break;
- }
- }
- index++;
- }
- }
- if(check==true){
- break;
- }
- } //j loop
- }
- }
- if(check==true){
- break;
- }else{
- arl.clear();
- }
- }
- int sum=0;
- for(int w:arl){
- sum=sum+w;
- System.out.print(w+" ");
- if(sum==a){
- break;
- }
- }
- System.out.println(a);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement