Problem: 2063B - Subsequence Update
Language: java

Solution 1 (runtime: 359 ms):


import java.util.*;

import java.io.*;

public class Main {



	public static class cmp implements Comparator<Integer>{

		public int compare(Integer a,Integer b) {

			return a-b;

		}

	}

	public static BufferedReader rd=new BufferedReader(new InputStreamReader(System.in));

	public static BufferedWriter wd=new BufferedWriter(new OutputStreamWriter(System.out));

	public static void solve()throws Exception{

		String dr[]=rd.readLine().split(" ");

		int n=Integer.parseInt(dr[0]),l=Integer.parseInt(dr[1]),r=Integer.parseInt(dr[2]);

		long a[]=new long [n+10],b[]=new long [n+10],c[]=new long [n+10];

		int t1=0,t2=0,t3=0;

		dr=rd.readLine().split(" ");

		long res=0;

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

			int x=Integer.parseInt(dr[i-1]);

			if(i<l) a[++t1]=x;

			if(i>r) {

				c[++t3]=x;

			//	res+=x;

			}

			if(i>=l&&i<=r){

				b[++t2]=x;

				res+=x;

			//	wd.write(res+" "+x+"\n");

			}

		}

	//	wd.write(res+"\n");

	//	for(int i=1;i<=t2;i++) wd.write(b[i]+"\n");

		Arrays.sort(a,1,1+t1);Arrays.sort(b,1,1+t2);Arrays.sort(c,1,1+t3);

	//	wd.write(res+"\n");

		//for(int i=1;i<=t3;i++) wd.write(c[i]+"\n");

		for(int i=1;i<=t1;i++) a[i]+=a[i-1];

		for(int i=1;i<=t2;i++) b[i]+=b[i-1];

		for(int i=1;i<=t3;i++) c[i]+=c[i-1];

		long ans=res;

		int k=Math.min(t1,t2);

		//wd.write(res+" "+ans+"\n");

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

			long tem=b[t2]-b[t2-i];

			//wd.write(tem+" "+i+"\n");

			//wd.write(tem-a[i]+"\n");

			ans=Math.min(ans, res-(tem-a[i]));

		}

		int k2=Math.min(t2, t3);

		for(int i=1;i<=k2;i++) {

			long tem=b[t2]-b[t2-i];

		//wd.write(i+" "+tem+" "+(tem-c[i])+"\n");

			ans=Math.min(ans,res-(tem-c[i]));

		}

		wd.write(ans+"\n");

		//long res=0;

		//for(int i=1;i<=t3;i++) wd.write(c[i]+"\n");

	}

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

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

		while(t-->0) {

			solve();

			wd.flush();

		}

	}



}



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

        }

    }

}