ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063B - Subsequence Update
Language: java

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

public class CF_1000_B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        int test_cases = sc.nextInt();
        while (test_cases-- > 0) {
            int n = sc.nextInt();
            int l = sc.nextInt();
            int r = sc.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++)
                arr[i] = sc.nextInt();
            int[] arr1 = Arrays.copyOfRange(arr, 0, r);
            int[] arr2 = Arrays.copyOfRange(arr, l - 1, arr.length);
            Arrays.sort(arr1);
            Arrays.sort(arr2);
            long min1 = 0;
            long min2 = 0;
            for (int i = 0; i < r - l + 1; i++) {
                min1 += arr1[i];
                min2 += arr2[i];
            }
            sb.append(Math.min(min1, min2)).append("\n");
        }
        sc.close();
        System.out.println(sb.toString());
    }
}

Solution 2 (runtime: 280 ms):
// Problem Page: https://codeforces.com/contest/2063/problem/B



import java.util.*;



public class SubsequenceUpdate {



    public static void main(String[] args) {



        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();

        while (t-- > 0) {

            solve(sc);

        }

    }



    //subsequence does not have to be contiguous

    static void solve(Scanner sc) {

        int n = sc.nextInt();

        int l = sc.nextInt();

        int r = sc.nextInt();

        int[] a = new int[n];

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

            a[i] = sc.nextInt();

        }

        int[] b = new int[r];

        int[] c = new int[n - l + 1];

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

            b[i] = a[i];

        }

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

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

        }

        Arrays.sort(b);

        Arrays.sort(c);

        long sum_b = 0, sum_c = 0;

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

            sum_b += b[i];

            sum_c += c[i];

        }

        System.out.println(Math.min(sum_b, sum_c));

    }

}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 484 ms):
import java.util.*;
public class Fi {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int o=0;o<t;o++){
            int n = sc.nextInt();
            int result = 1;
            int arr[] = new int[n];
            for(int i=0;i<n;i++){
                arr[i] = sc.nextInt();
                if(i>0 && arr[i-1]>arr[i]){
                    result = 0;
                }else if(i>0 && arr[i-1]<=arr[i]){
                    arr[i] -=arr[i-1];
                    arr[i-1] = 0;
                }
            }
            if(result==0){
                System.out.println("NO");
            }else{
                System.out.println("YES");
            }
          
        }
    }
}
//4
//1 4 3 2

Solution 2 (runtime: 484 ms):
import java.util.*;
public class Main{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            int n=sc.nextInt();
            int ar[]=new int[n];
            for(int i=0;i<n;i++)
            {
                ar[i]=sc.nextInt();
            }
            boolean flag=false;
            for(int i=0;i+1<n;i++)
            {
                if(ar[i]<=ar[i+1])
                {
                    ar[i+1]=ar[i+1]-ar[i];
                    ar[i]-=ar[i];
                }
                else
                {
                    flag=true;
                    break;
                }
            }
            if(flag)
            {
                System.out.println("NO");
            }
            else
            {
                System.out.println("YES");
            }
        }
    }
}
Problem: 2050D - Digital string maximization
Language: java

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

public class DigitalStringMaximization {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tokenizer = new StringTokenizer(in.readLine());

        int t = Integer.parseInt(tokenizer.nextToken());

        while (t-- > 0) {
            tokenizer = new StringTokenizer(in.readLine());
            char[] c = tokenizer.nextToken().toCharArray();

            for (int i = c.length - 1; i > 0; i--) {
                if (c[i] > c[i - 1] + 1) {
                    operation(c, i);
                    i += 9;
                    if (i > c.length) {
                        i = c.length;
                    }
                }
            }

            System.out.println(new String(c));
        }
    }

//    public static int findLast(char[] c) {
//        for (int i = c.length - 1; i > 0; i--) {
//            if (c[i] > c[i - 1] + 1) {
//                return i + 1;
//            }
//        }
//        return c.length;
//    }

    public static void operation(char[] c, int i) {
        int cur = c[i];
        int left = c[i - 1];
        c[i - 1] = (char) (cur - 1);
        c[i] = (char) left;
    }
}

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



public class Main {

public static void main(String []args) {

Scanner sc=new Scanner(System.in);

int t=sc.nextInt();

while(t-->0){

String s = sc.next();

char[] arr = s.toCharArray();



for (int i = 0; i < arr.length; i++) {

int best = arr[i] - '0', pos = i;



for (int j = i; j < Math.min(i + 10, arr.length); j++) {

if ((arr[j] - '0') - (j - i) > best) {

best = (arr[j] - '0') - (j - i);

pos = j;

}

}



while (pos > i) {

char temp = arr[pos];

arr[pos] = arr[pos - 1];

arr[pos - 1] = temp;

pos--;

}



arr[i] = (char) (best + '0');

}



System.out.println(new String(arr));

}

}

}
Problem: 2050C - Uninteresting Number
Language: java

Solution 1 (runtime: 484 ms):
import java.util.*;

public class C_Uninteresting_Number{
    static HashMap<Integer, Boolean> hm;
    public static boolean func(int num, int two, int three){
        if(num%9==0)return true;
        if(two==0&&three==0)return false;
        if(hm.containsKey(num))return hm.get(num);
        boolean t=false, th=false;
        if(two>0){
            t = func(num+2, two-1, three);
        }
        if(three>0){
            th = func(num+6, two, three-1);
        }
        hm.put(num, t|th);
        return t|th;
    }
    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);
        int t = scn.nextInt();
        while(t-->0){
            String s = scn.next();
            int n = s.length();
            int num = 0;
            int two = 0, three=0;
            for(int i = 0; i < n; i++){
                num += (s.charAt(i)-'0');
                if(s.charAt(i)=='2')two++;
                if(s.charAt(i)=='3')three++;
            }
            hm = new HashMap<>();
            boolean ans = func(num, two, three);
            if(ans)
            System.out.println("YES");
            else System.out.println("NO");
        }
    }
}

Solution 2 (runtime: 249 ms):
//package com.aakash.Contest.C991;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class C {
    static BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));

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

        StringTokenizer s1
                = new StringTokenizer(br.readLine());
        int t = Integer.parseInt(s1.nextToken());
        while (t-- > 0) {
            StringBuilder sb=new StringBuilder(br.readLine());
            int n = sb.length();
            boolean is=solve(sb,n);
            if(is) System.out.println("YES");
            else System.out.println("NO");
        }

    }

    static boolean solve(StringBuilder sb,int n) {
        int two=0,three=0;
        int sum=0;
        for(int i=0;i<n;i++) {
            int curr=sb.charAt(i)-'0';
            if(curr==2) two++;
            if(curr==3) three++;
            sum+=curr;
            sum%=9;
        }
        if((two>=5&&three>=5) ||sum==0) return true;
        sum=9-sum;
        for(int x=0;x<=two;x++) {
            for(int y=0;y<=three;y++) {
                long curr=x*2;
                curr+=y*6;
                if(curr%9==sum) return true;
            }
        }
        return false;
    }
}
Problem: 2063B - Subsequence Update
Language: java

Solution 1 (runtime: 499 ms):
import java.util.*;

public class Main
{
	public static void main(String[] args) {
		int t;
		Scanner sc = new Scanner(System.in);
		t = sc.nextInt();
		while(t > 0){
		    int n = sc.nextInt();
		    int l = sc.nextInt();
		    int r = sc.nextInt();
		    l--;
		    r--;
		    List<Integer> arr1 = new ArrayList<>();
		    List<Integer> arr2 = new ArrayList<>();
		    List<Integer> arr3 = new ArrayList<>();
		    for (int i = 0; i < l; i++){
		        arr1.add(sc.nextInt());
		    }
		    for (int i = l; i <= r; i++){
		        arr2.add(sc.nextInt());
		    }
		    for (int i = r + 1; i < n; i++){
		        arr3.add(sc.nextInt());
		    }
		    Collections.sort(arr1);
		    Collections.sort(arr2);
		    Collections.sort(arr3);
		  //  System.out.println(arr1);
		  //  System.out.println(arr2);
		  //  System.out.println(arr3);
		    long sum1 = 0, sum2 = 0;
		    int j = 0;
		    for (int i = arr2.size() - 1; i >= 0; i--){
		        int num1 = arr2.get(i);
		        if (j < arr1.size()){
		            num1 = Math.min(num1, arr1.get(j));
		            j++;
		        }
		        sum1 += num1;
		    }
		    j = 0;
		    for (int i = arr2.size() - 1; i >= 0; i--){
		        int num1 = arr2.get(i);
		        if (j < arr3.size()){
		            num1 = Math.min(num1, arr3.get(j));
		            j++;
		        }
		        sum2 += num1;
		    }
		    long ans = Math.min(sum1, sum2);
		    System.out.println(ans);
		    t--;
		}
	}
}

Solution 2 (runtime: 280 ms):
// Problem Page: https://codeforces.com/contest/2063/problem/B



import java.util.*;



public class SubsequenceUpdate {



    public static void main(String[] args) {



        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();

        while (t-- > 0) {

            solve(sc);

        }

    }



    //subsequence does not have to be contiguous

    static void solve(Scanner sc) {

        int n = sc.nextInt();

        int l = sc.nextInt();

        int r = sc.nextInt();

        int[] a = new int[n];

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

            a[i] = sc.nextInt();

        }

        int[] b = new int[r];

        int[] c = new int[n - l + 1];

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

            b[i] = a[i];

        }

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

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

        }

        Arrays.sort(b);

        Arrays.sort(c);

        long sum_b = 0, sum_c = 0;

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

            sum_b += b[i];

            sum_c += c[i];

        }

        System.out.println(Math.min(sum_b, sum_c));

    }

}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 483 ms):
import java.util.*;

public class question {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        while (t-- > 0) {
            int n = sc.nextInt();

            int[] arr = new int[n];

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

            int help = 0 ;

            for(int i = 0 ; i < n-1 ; i++){
                int a = arr[i];
                int b = arr[i+1];

                if(a > b){
                    System.out.println("NO");
                    help++;
                    break;
                }else{

                    arr[i+1] = arr[i+1] - arr[i];
                    arr[i] = 0 ;
                    //System.out.println(arr[i+1]);
                }
            }


            if(help == 0){
                System.out.println("YES");
            }

        }
    }
}


Solution 2 (runtime: 765 ms):


import java.util.Deque;

import java.util.LinkedList;

import java.util.Scanner;

import javax.print.DocPrintJob;













public class SumTo {

    private static boolean  Solution(int[]a,int n){

        Deque<Integer>dq=new LinkedList<>();

        for(int num:a){

            dq.add(num);

        }

        while(dq.size()>1){

            if(((LinkedList<Integer>) dq).get(0)>((LinkedList<Integer>) dq).get(1)){

                return false;

            }

            else if(((LinkedList<Integer>) dq).get(0)<((LinkedList<Integer>) dq).get(1)){

                int first = dq.removeFirst();

                int second = dq.removeFirst();

                dq.addFirst(second - first);



            }else{

                dq.pollFirst();

                dq.pollFirst();

            }

        }

        return true;

        

    }

    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);

        int t=sc.nextInt();

        while(t-->0){

            int n=sc.nextInt();

            int[]a=new int[n];

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

                a[i]=sc.nextInt();

            }

            System.out.println(Solution(a,n)?"Yes":"NO");

        }

    }

}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 702 ms):
import java.util.*;
public class subtractminsort {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int l=0;l<t;l++){
            int n=sc.nextInt();
            int arr[]=new int[n];
            for(int i=0;i<n;i++){
                arr[i]=sc.nextInt();
            }
            String res="YES";
            int a=Math.min(arr[0],arr[1]);
            arr[0]-=a;
            arr[1]-=a;
            if(arr[0]>arr[1]){
                res="NO";
            }
            if(res=="YES"){
                for(int i=1;i<n-1;i++){
                int min=Math.min(arr[i],arr[i+1]);
                arr[i]-=min;
                arr[i+1]-=min;
                if(arr[i-1]>arr[i]){
                    res="NO";
                    break;
                }
            }
            if(arr[n-2]>arr[n-1]){
                res="NO";
            }
        }
            System.out.println(res);
          
        }
        sc.close();
    }
}


Solution 2 (runtime: 359 ms):
import java.io.*;

import java.math.*;

import java.util.*;



public class Main {

static class IoScanner {

BufferedReader bf;

StringTokenizer st;

BufferedWriter bw;



public IoScanner() {

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

st = new StringTokenizer("");

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

}



public String nextLine() throws IOException {

return bf.readLine();

}



public String next() throws IOException {

while (!st.hasMoreTokens()) {

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

}

return st.nextToken();

}



public char nextChar() throws IOException {

return next().charAt(0);

}



public int nextInt() throws IOException {

return Integer.parseInt(next());

}



public long nextLong() throws IOException {

return Long.parseLong(next());

}



public double nextDouble() throws IOException {

return Double.parseDouble(next());

}



public float nextFloat() throws IOException {

return Float.parseFloat(next());

}



public BigInteger nextBigInteger() throws IOException {

return new BigInteger(next());

}



public BigDecimal nextDecimal() throws IOException {

return new BigDecimal(next());

}

}

static IoScanner sc = new IoScanner();

static PrintWriter out = new PrintWriter(System.out);

static final int mod=(int) (1e9+7);

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

int T = 1;

T = sc.nextInt();

while (T-- > 0) {

solve();

}

out.flush();

}

static void solve() throws IOException {

int n=sc.nextInt();

int[] arr = new int[n];

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

arr[i]=sc.nextInt();

}

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

if (arr[i - 1] > arr[i]) {

out.println("NO");

return;

}else {

arr[i] -= arr[i - 1];

arr[i-1]=0;

}

}

out.println("YES");

}

}
Problem: 2060D - Subtract Min Sort
Language: java

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

public class d {
	
	static BufferedReader br;
	static StringTokenizer st;
	static StringBuilder sb = new StringBuilder("");
	
	public static void main (String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		st = new StringTokenizer(br.readLine());
		
		int t = Integer.parseInt(st.nextToken());
		
		for (int i = 0; i < t; i++) {
			st = new StringTokenizer(br.readLine());
			solve();
		}
		
		br.close();
		System.out.print(sb.toString());
	} 
 
	
	static void solve() throws IOException {
		int n = Integer.parseInt(st.nextToken());
		int[] A = new int[n+1];
		
		st = new StringTokenizer(br.readLine());
		
		for (int i = 1; i <= n; i++) {
			A[i] = Integer.parseInt(st.nextToken());
		}
		
		for (int i = 1; i <= n-1; i++) {
			int m = Math.min(A[i], A[i+1]);
			A[i] -= m;
			A[i+1] -= m;
		}
		
		
		for (int i = 1; i <= n-1; i++) {
			if (A[i] > A[i+1]) {
				sb.append("No\n");
				return;
			}
		}
		
		sb.append("Yes\n");
		
	}
	
	
	
}

Solution 2 (runtime: 671 ms):
import java.util.*;
 
public class bit {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            int arr[] = new int[n];
            for(int i=0;i<n;i++){
                arr[i]=sc.nextInt();
            }
            String ans = "YES";
            if(arr[0]>arr[1]){
                System.out.println("NO");
                continue;
            }
            for(int i=0;i<n-1;i++){
                if(arr[i]>arr[i+1]){
                    ans = "NO";
                    break;
                }else{
                    arr[i+1]-=arr[i];
                    arr[i]-=arr[i];
                }
            }
            System.out.println(ans);
            
        } 
    }
}
Problem: 2063B - Subsequence Update
Language: java

Solution 1 (runtime: 467 ms):
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class SubsequenceUpdate {
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    int tests = Integer.parseInt(scanner.nextLine());
    while (tests > 0){
      int n = scanner.nextInt();
      int l = scanner.nextInt();
      int r = scanner.nextInt();
      long[] arr = new long[n];
      for (int i = 0; i < arr.length; i++){
        arr[i] = scanner.nextLong();
      }

      ArrayList<Long> toL = new ArrayList<>();
      ArrayList<Long> toR = new ArrayList<>();
      for (int i = 0; i < r; i++){
        toR.add(arr[i]);
      }
      for (int i = l - 1; i < n; i++){
        toL.add(arr[i]);
      }

      Collections.sort(toL);
      Collections.sort(toR);

      long sumOne = 0;
      for (int i = 0; i < r - l + 1; i++){
        sumOne += toL.get(i);
      }

      long sumTwo = 0;
      for (int i = 0; i < r - l + 1; i++){
        sumTwo += toR.get(i);
      }

      System.out.println(Math.min(sumTwo, sumOne));
      tests--;
    }
  }
}


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

		}

	}



}