Problem: 2063B - Subsequence Update
Language: java

Solution 1 (runtime: 327 ms):
import java.io.*;

import java.util.*;



public class Main {

    public static void main(String[] args) throws IOException {



        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));



        if (System.getProperty("ONLINE_JUDGE") == null) {

            br = new BufferedReader(new FileReader("src/Input.txt"));

            bw = new BufferedWriter(new FileWriter("src/Output.txt"));

        }



        // Your code here



        int t = Integer.parseInt(br.readLine());

        while(t --> 0) {

            StringTokenizer st = new StringTokenizer(br.readLine());

            int n = Integer.parseInt(st.nextToken());

            int l = Integer.parseInt(st.nextToken())-1;

            int r = Integer.parseInt(st.nextToken())-1;



            long[] arr =new long[n];

            st = new StringTokenizer(br.readLine());

            for(int i=0; i<n; i++){

                arr[i] = Long.parseLong(st.nextToken());

            }



            // [0,r] [l,n-1]

            long[] left = Arrays.copyOfRange(arr, 0, r+1);

            long[] right = Arrays.copyOfRange(arr, l, n);



            Arrays.sort(left);

            Arrays.sort(right);



            long leftSum = 0;

            long rightSum = 0;

            int k = r-l+1;

            for(int i=0;i <k; i++) {

                leftSum += left[i];

            }

            for(int i=0;i <k; i++) {

                rightSum += right[i];

            }



            bw.write(Math.min(leftSum, rightSum) + "\n");

        }



        bw.flush();

        br.close();

        bw.close();

    }

}

Solution 2 (runtime: 343 ms):
import java.util.*;

import java.io.*;



public class B_Subsequence_Update {

    public static void main(String[] args) {

        try {

            FastReader in=new FastReader();

            FastWriter out=new FastWriter();

            int testcases=in.nextInt();

            while(testcases-- >0){

                int n=in.nextInt();

                int l=in.nextInt();

                int r=in.nextInt();

                l--;

                r--;

                long a[]=new long[n];

                for(int i=0;i<n;i++){

                    a[i]=in.nextLong();

                }

                List<Long> s=new ArrayList<>();

                List<Long> e=new ArrayList<>();

                List<Long> bh=new ArrayList<>();

                for(int i=0;i<l;i++) s.add(a[i]);

                for(int j=r+1;j<n;j++) e.add(a[j]);

                Collections.sort(s);

                Collections.sort(e);

                long curr=0;

                for(int i=l;i<=r;i++){

                    curr+=a[i];

                    bh.add(a[i]);

                }

                Collections.sort(bh,Collections.reverseOrder());

                long ans=curr;

                long x=curr;

                int i=0;

                int j=0;

                while(i<s.size() && j<bh.size()){

                    curr-=bh.get(j);

                    curr+=s.get(i);

                    ans=Math.min(ans,curr);

                    i++;j++;

                }

                i=0;

                j=0;

                curr=x;

                while(i<e.size() && j<bh.size()){

                    curr-=bh.get(j);

                    curr+=e.get(i);

                    ans=Math.min(ans,curr);

                    i++;j++;

                }

                out.println(ans);

            }



            out.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    static class FastReader{

        BufferedReader br;

        StringTokenizer st;

        public FastReader(){

            br=new BufferedReader(new InputStreamReader(System.in));

        }

        String next(){

            while(st==null || !st.hasMoreTokens()){

                try {

                    st=new StringTokenizer(br.readLine());

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            return st.nextToken();

        }

        int nextInt(){

            return Integer.parseInt(next());

        }

        long nextLong(){

            return Long.parseLong(next());

        }

        double nextDouble(){

            return Double.parseDouble(next());

        }

        char nextChar() {

            return next().charAt(0);

        }

        String nextLine(){

            String str="";

            try {

                str=br.readLine().trim();

            } catch (Exception e) {

                e.printStackTrace();

            }

            return str;

        }

    }



    static class FastWriter {

        private final BufferedWriter bw;



        public FastWriter() {

            this.bw = new BufferedWriter(new OutputStreamWriter(System.out));

        }



        public void print(Object object) throws IOException {

            bw.append("" + object);

        }



        public void println(Object object) throws IOException {

            print(object);

            bw.append("\n");

        }



        public void close() throws IOException {

            bw.close();

        }

    }

}