Problem: 2063B - Subsequence Update
Language: java

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

        import java.util.*;



        public class Main {

            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());

                }



                int[] nextInts(int N) {

                    int[] res = new int[N];

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

                        res[i] = nextInt();

                    }

                    return res;

                }



                long nextLong() {

                    return Long.parseLong(next());

                }



                long[] nextLongs(int N) {

                    long[] res = new long[N];

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

                        res[i] = nextLong();

                    }

                    return res;

                }



                double nextDouble() {

                    return Double.parseDouble(next());

                }



                String nextLine() {

                    String str = "";

                    try {

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

                    } catch (IOException e) {

                        e.printStackTrace();

                    }

                    return str;

                }

            }



            static class FastWriter {

                private final BufferedWriter bw;

                private final StringBuilder sb;



                public FastWriter() {

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

                    this.sb = new StringBuilder();

                }



                public void print(Object object) {

                    sb.append(object);

                }



                public void println(Object object) {

                    sb.append(object).append("\n");

                }



                public void flush() throws IOException {

                    bw.write(sb.toString());

                    bw.flush();

                    sb.setLength(0);  // Clear StringBuilder

                }



                public void close() throws IOException {

                    flush();

                    bw.close();

                }

            }



            static FastReader sc = new FastReader();

            static FastWriter out = new FastWriter();



            static void solve() {



            

            int n  = sc.nextInt();

           long a[] = new long[n];

        int l  = sc.nextInt();

        int r  = sc.nextInt();

    



           for(int i=0;i<n;i++) a[i] = sc.nextLong();



           long b[] = new long[r-l+1];

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

               b[i-l+1] = a[i];

        }

        Arrays.sort(b);



           Long ans = 0l;

        //    System.out.println(ans);



           PriorityQueue<Long> pq = new PriorityQueue<>();



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

        for(int i=b.length-1;i>=0;i--){

            if(pq.size()>0&&pq.peek()<b[i]){

                ans+=pq.poll();

            }

            else ans+=b[i];

        }

        pq.clear();

        for(int i=n-1;i>=r;i--){

            pq.add(a[i]);

        // System.out.println(a[i]);

        }

        long sm = 0;

          for(int i=b.length-1;i>=0;i--){

            if(pq.size()>0&&pq.peek()<b[i]){

                sm+=pq.poll();

            }

            else sm+=b[i];

        }







              System.out.println(Math.min(ans,sm)); 

        }







            static class pair{



                long f;

                long s;



                pair(long f,long s){

                    this.f=f;

                    this.s=s;

                    // this.t=t;

                }

            }



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

                int tc = sc.nextInt();

                // int tc=  1;

                while (tc-- > 0) {

                    solve();

                }

                out.close(); // Flush everything at the end

            }

    }



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



public class B_Subsequence_Update{

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        int t = s.nextInt();

        while (t-->0){

            int n = s.nextInt();

            int l = s.nextInt();

            int r = s.nextInt();

            int[] arr = new int[n];

            for (int i=0;i<n;++i) arr[i] = s.nextInt();

            PriorityQueue<Integer> maxpq = new PriorityQueue<>((a,b)->b-a);

            PriorityQueue<Integer> minpql = new PriorityQueue<>();

            PriorityQueue<Integer> minpqr = new PriorityQueue<>();

            // ArrayList<Integer> raw = new ArrayList<>();

            long grr = 0;

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

                maxpq.add(arr[i]);

                grr += arr[i];

            }

            for (int i=0;i<l-1;++i) minpql.add(arr[i]);

            for (int i=r;i<n;++i) minpqr.add(arr[i]);

            long sw = 0, sol = 0, sor = 0;

            long ans = grr;

            while (!maxpq.isEmpty() && !minpql.isEmpty()){

                sw += maxpq.poll();

                sol += minpql.poll();

                ans = Math.min(ans,grr-sw+sol);

                // raw.add(sol);

                if (sol >= sw) break;

            }

            maxpq.clear();

            sw = 0;

            for (int i=l-1;i<r;++i) maxpq.add(arr[i]);

            while (!maxpq.isEmpty() && !minpqr.isEmpty()){

                sw += maxpq.poll();

                sor += minpqr.poll();

                ans = Math.min(ans,grr-sw+sor);

                // raw.add(sor);

                if (sor >= sw) break;

            }   

            System.out.println(ans);

        }

    }

}