Problem: 2063B - Subsequence Update
Language: java

Solution 1 (runtime: 281 ms):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
// import java.math.BigInteger;
import java.util.*;

public class Yuv1 {

    public static boolean isPrime(int n) {
        if (n <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

    // GCD
    public static long gcd(long a, long b) {
        if (a == 0) {
            return b;
        }
        return gcd(b % a, a);
    }

    // Fast Input
    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 (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());
        }

        String nextLine() {
            String str = "";
            try {
                if (st.hasMoreTokens()) {
                    str = st.nextToken("\n");
                } else {
                    str = br.readLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static long factorial(long n) {
        long f = 1;
        for (int i = 2; i <= n; i++)
            f *= i;
        return f;
    }

    public static long countDigitsInFact(long n) {

        if (n == 0 || n == 1) {
            return 1;
        }
        double logSum = 0;
        for (int i = 2; i <= n; i++) {
            logSum += Math.log10(i);
        }

        return (long) Math.floor(logSum) + 1;
    }
    private static void reverse(long[] arr, int start, int end) {
        while (start < end) {
            long temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
    }

    private static long sumSubarray(long[] arr, int start, int end) {
        long sum = 0;
        for (int i = start; i <= end; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public static int mod = 1000000007;
    static final long NEG_INF = -1000000000000000L;
    static final long INF = 1000000000000000L;

    public static void main(String args[]) {
        FastReader x = new FastReader();
        PrintWriter out = new PrintWriter(System.out);
        int t = x.nextInt();

        while (t-- > 0) {

            int n = x.nextInt();
            int l = x.nextInt();
            int r = x.nextInt();
            l--;
            long[] a = new long[n];
            for(int i=0;i<n;i++) {
                a[i]=x.nextLong();
            }

            long[] b = Arrays.copyOf(a, n);
            Arrays.sort(b, 0, r);
            reverse(b, 0, r-1);
            Arrays.sort(a, l, n);

            System.out.println(Math.min(sumSubarray(a, l, r-1), sumSubarray(b, l, r-1)));
        }
        out.flush();
    }
}

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

		}

	}



}