ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2057C - Trip to the Olympiad
Language: java

Solution 1 (runtime: 390 ms):
import java.util.Scanner;

public class C2057 {
    static boolean solve(int l, int r, int val) {
        boolean ok = false;
        for (int i = 29; i >= 0; i--) {
            int x = 1 << i;
            if (x > l && x < r) {
                System.out.printf("%d %d %d\n", val + x - 1, val + x, val + x + 1);
                ok = true;
                break;
            } else if (x > l && x == r) {
                System.out.printf("%d %d %d\n", val + x - 2, val + x - 1, val + x);
                ok = true;
                break;
            }
        }
        return ok;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int l = sc.nextInt(), r = sc.nextInt();
            if (solve(l, r, 0))
                continue;
            int val = 0;
            for (int i = 29; i >= 0; i--) {
                int x = 1 << i;
                if ((l & x) == (r & x)) {
                    val += (l & x);
                    continue;
                }
                solve(l - val, r - val, val);
                break;
            }
        }
        sc.close();
    }
}


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

public class C{
    public static boolean powerOf2(int n){
        if(n==1) return false;
        int a = 1;
        while(a<n) a = a*2;
        if(a==n) return true;
        return false;
    }
    public static void main(String...args){
        Scanner sc = new Scanner(System.in);

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

            int l = sc.nextInt();
            int r = sc.nextInt();

            int mid = 0;
            boolean smallerThanr = false, greaterThanl = false;

            int start = 30;
            int i = start;
            while(i>=0){
                int lbit = (((1<<i)&l)>>i);
                int rbit = (((1<<i)&r)>>i);
                if(lbit != rbit) break;
                mid += (lbit<<i);
                i--;
            }
            // System.out.println(mid);

            start = i;
            for(i=start; i>=0; i--){
                int lbit = (((1<<i)&l)>>i);
                int rbit = (((1<<i)&r)>>i);
                int mBit = 0;

                if(lbit==1 || rbit==1) mBit=0;
                else mBit = 1;
                mid += (mBit<<i);
            }
            // System.out.println(mid);

            
            i = start;
            while(i>=0 && mid <= l){
                int mBit = (((1<<i)&mid)>>i);
                // System.out.println(i + " " + mBit + " " + mid);
                if(mBit==0 && ((mid + (1<<i)) < r)){
                    mid += (1<<i);
                }
                i--;
            }
            System.out.println(l + " " + mid + " " + r);
        }
    }
}
Problem: 2062C - Cirno and Operations
Language: java

Solution 1 (runtime: 187 ms):
// "static void main" must be defined in a public class.
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner Sc = new Scanner(System.in);
        int t = Sc.nextInt();
        Sc.nextLine();
        for(int i=0; i<t; i++) {
            int n = Sc.nextInt();
            long[] arr = new long[n];
            for(int j=0; j<n; j++) {
                arr[j] = Sc.nextInt();
            }
            Solution s1 = new Solution();
            System.out.println(s1.maxSum(arr));
        }
    }
}

class Solution {
    public long maxSum(long[] arr) {
        if(arr.length == 1)
            return arr[0];
        int n = arr.length;
        long sum = 0;
        for(int i=0; i<n; i++) {
            sum += arr[i];
        }
        while(n > 1) {
            long temp = 0;
            for(int i=0; i<n-1; i++) {
                arr[i] = arr[i] - arr[i+1];
                temp = temp + arr[i];
            }
            sum = Math.max(sum, Math.abs(temp));
            n--;
        }
        return Math.max(sum, Math.abs(arr[0]));
    }
}


Solution 2 (runtime: 280 ms):
import java.util.*;
public class Solution{
    static int max=0;
    public static long sumCalc(ArrayList<Long> list){
        long sum=0;
        int i=1;
                
         while(i<list.size()){
            long v=list.get(i)-list.get(i-1);
            list.set(i-1,v);
            sum+=v;
            i++;
        }
        
        list.remove(list.size()-1);
        
        return sum;
    }
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        
        int t=sc.nextInt();
        
        for(int test=0;test<t;test++){
            int n=sc.nextInt();
            ArrayList<Long> list=new ArrayList<>();
           
            long sum=0;
            for(int i=0;i<n;i++){
                long val=sc.nextLong();
                list.add(val);
                sum+=val;
            }
            
            long max=sum;
            while(list.size()>1){
                long ans=sumCalc(list);
                max=Math.max(ans,max);
                sum=0;
                for(int i=0;i<list.size();i++){
                    sum+=(-1)*list.get(i);
                }
                
                max=Math.max(sum,max);
            }
            
            System.out.println(max);
        }
    }
}
Problem: 2050D - Digital string maximization
Language: java

Solution 1 (runtime: 312 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[] res=s.toCharArray();
            for(int i=1;i<s.length();i++){
                if(res[i]>res[i-1]){
                    while(i>0 && res[i]>res[i-1] && res[i]-res[i-1]>1){
                        res[i]--;
                        char temp=res[i];
                        res[i]=res[i-1];
                        res[i-1]=temp;
                        i--;
                    }
                }
            }
            String str=new String(res);
            System.out.println(str);
        }
        sc.close();
    }
}

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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        sc.nextLine(); 
        while (t-- > 0) {
            String s = sc.nextLine();
            char[] arr = s.toCharArray();
            int swapcount = 1;
            int ans = 0;
            while (swapcount != 0) {
                int sw = 0;
                for (int i = 1; i < arr.length; i++) {
                    char a = (char) (arr[i] - 1);
                    char b = arr[i - 1];
                    if ((arr[i] != '1' || arr[i] != '0') && a - b > 0) {
                        if (arr[i] > arr[i - 1]) {
                            swap(i - 1, i, arr);
                            sw++;
                        }
                    }
                }

                if (sw == 0) {
                    break;
                }
            }
            System.out.println(new String(arr));
        }
    }

    public static void swap(int i, int j, char[] arr) {
        arr[j] = (char) (arr[j] - 1);
        char t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }
}
Problem: 2060D - Subtract Min Sort
Language: java

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

        }

    }

}



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

public class Solution {

public static void main(String[] args) {

Scanner scn = new Scanner(System.in);

int cases = scn.nextInt();

while (cases > 0) {

int n = scn.nextInt();

int[] arr = new int[n];

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

arr[i] = scn.nextInt();

}

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

int cur = arr[i];

int next= arr[i+1];

arr[i]-=Math.min(cur,next);

arr[i+1]-=Math.min(cur,next);

}

boolean flag=true;

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

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

flag=false;

break;

}

}

if(flag) System.out.println("YES");

else System.out.println("NO");

cases--;

}

}

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

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

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

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

            int[] nums = new int[n];

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

            for(int i = 1 ; i < n ; i++){
                int min = Math.min(nums[i],nums[i-1]);
                nums[i] = nums[i] - min;
                nums[i-1] = nums[i-1] - min;

                if(nums[i-1] > nums[i] ) {
                    System.out.println("NO");
                    t--;
                    continue outer;
                }
            }
            System.out.println("YES");
            t--;
        }
    }
}

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: 2050C - Uninteresting Number
Language: java

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

public class number {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            String n = sc.next(); 
            int[] digits = new int[n.length()];
            int sum = 0;
            int twocount = 0;
            int threecount = 0;

            for (int i = 0; i < n.length(); i++) {
                digits[i] = n.charAt(i) - '0';
                sum += digits[i];
                if (digits[i] == 2) twocount++;
                if (digits[i] == 3) threecount++;
            }
            boolean flag=false;
            for (int i = 0; i <= Math.min(8, twocount); i++) {
                for (int j = 0; j <= Math.min(8, threecount); j++) {
                    if ((sum + i * 2 + j * 6) % 9 == 0) {
                        flag=true;
                        break;
                    }
                }
            }
            System.out.println(flag?"YES":"NO");
        }
    }
}


Solution 2 (runtime: 327 ms):
import java.util.*;
public class temp{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        sc.nextLine();
        while(t-->0){
            String s=sc.nextLine();
            char[] c=s.toCharArray();
            int[] fre=new int[10];
            int sum=0;
            for(int i=0;i<c.length;i++){
                int cr=c[i]-'0';
                fre[cr]++;
                sum+=cr;
                if(sum>9)sum=sum%10+sum/10;
            }
            int f=-1;
            if(sum==0 || sum==9)f=1;
            else{
                int[][] dp=new int[Math.min(10,fre[2]+1)][Math.min(10,fre[3]+1)];
                for(int i=0;i<dp.length;i++)
                    Arrays.fill(dp[i],-1);
                int ans=solve(sum,dp,1,1);
                if(ans==0)f=1;
            }
            if(f==1)System.out.println("YES");
            else System.out.println("NO");
        }
    }
    public static int solve(int sum,int[][] dp,int i,int j){
        if(sum==0){return 0;}
        if(i==dp.length && j==dp[0].length)return 1;
        if(i<dp.length && j<dp[0].length && dp[i][j]!=-1)return dp[i][j];
        if((i<dp.length && solve((sum+2)%9,dp,i+1,j)==0) || (j<dp[0].length && solve((sum+6)%9,dp,i,j+1)==0)){
            if(i<dp.length && j<dp[0].length)dp[i][j]=0;
            return 0;}
        if(i<dp.length && j<dp[0].length)dp[i][j]=1;
        return 1;
    }
}
Problem: 2063B - Subsequence Update
Language: java

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



public class Main {

    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        

        int t = sc.nextInt();



        while(t-- > 0) {

            solve();

        }

    }



    static void solve() {

        int n = sc.nextInt();

        int l = sc.nextInt() - 1;

        int r = sc.nextInt() - 1;



        int[] a = new int[n];



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



        Queue<Integer> before = new PriorityQueue<>();

        Queue<Integer> after = new PriorityQueue<>();

        Queue<Integer> res1 = new PriorityQueue<>(Collections.reverseOrder());

        Queue<Integer> res2 = new PriorityQueue<>(Collections.reverseOrder());



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

            if (i < l) before.add(a[i]);

            else if (i > r) after.add(a[i]);

            else {

                res1.add(a[i]);

                res2.add(a[i]);

            }

        }



        while(!before.isEmpty() && before.peek() < res1.peek()) {

            res1.add(before.poll());

            res1.poll();

        }



        long  sum = 0;

        while(!res1.isEmpty()) sum += res1.poll();



        while(!after.isEmpty() && after.peek() < res2.peek()) {

            res2.add(after.poll());

            res2.poll();

        }

        long sum2 = 0;

        while(!res2.isEmpty()) sum2 += res2.poll();



        System.out.println(Math.min(sum, sum2));

    }

}

Solution 2 (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--;
    }
  }
}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (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");
            }
        }
    }
}

Solution 2 (runtime: 484 ms):
// Problem Page: https://codeforces.com/contest/2060/problem/D

import java.util.*;

public class SubtractMinSort {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            solve(sc);
        }
    }

    static void solve(Scanner sc) {
        int n = sc.nextInt();
        int[] a = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }

        if (n == 2) {
            if (a[1] <= a[2]) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
            return;
        }

        String ans = "YES";
        int diff = a[2] - a[1];
        for (int i = 3; i <= n; i++) {
            if ((diff >= 0) && (a[i] >= diff)) {
                diff = a[i] - diff;
            } else {
                ans = "NO";
                break;
            }
        }
        System.out.println(ans);
    }
}
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: 281 ms):
import java.util.*;

import java.io.*;

public class MooCounting {

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

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

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



        while(t-- > 0)

        {

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

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

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

            left--;

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

            right--;

            long k = (long)right - (long)left + 1;



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

            int[] arr = new int[n];

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

            {

                arr[i] = Integer.parseInt(st.nextToken());

            }



            // Case 1: [l, n]

            int[] option1 = Arrays.copyOfRange(arr, left, n);

            Arrays.sort(option1);

            long sum1 = 0;

            for (int i = 0; i < k; i++) sum1 += option1[i];



            // Case 2: [1, r]

            int[] option2 = Arrays.copyOfRange(arr, 0, right + 1);

            Arrays.sort(option2);

            long sum2 = 0;

            for (int i = 0; i < k; i++) sum2 += option2[i];



            

            System.out.println(Math.min(sum1, sum2)); 

        }

    }

}
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: 452 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 l = sc.nextInt();
      int r = sc.nextInt();
      l--;
      r--;
      int []arr = new int[n];
      for(int i = 0; i < n; i++){
        arr[i] = sc.nextInt();
      }
      List<Integer> left = new ArrayList<>();
      for(int i = 0; i <= r; i++)left.add(arr[i]);
      List<Integer> right = new ArrayList<>();
      for(int i = l; i < n; i++)right.add(arr[i]);
      Collections.sort(left);
      Collections.sort(right);
      long j = 0;
      long h = 0;
      for(int i = 0; i < r-l+1; i++){
        j += (long)left.get(i);
        h += (long)right.get(i);
      }
      System.out.println(Math.min(j,h));
    }
      
    }
}