ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050E - Three Strings
Language: java

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

public class ThreeStrings {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int t= sc.nextInt();
        for (int i = 0; i < t; i++) {
            String a = sc.next();
            String b = sc.next();
            String c = sc.next();

            int lenA = a.length();
            int lenB = b.length();
            int lenC = c.length();

            long dp[][] = new long[lenA + 10][lenB + 10];

            for(long [] x : dp ) Arrays.fill( x , -1 );

            System.out.println(rec( 0 , 0 , dp , lenA , lenB , a , b, c));
        }
    }

    static long rec ( int x , int y , long dp[][] , int lenA , int lenB , String a , String b, String c){

        if ( x == lenA ){
            long add = 0;
            for(int i = y ; i < lenB ;i++){
                if ( b.charAt(i) != c.charAt( x + i )) add ++ ;
            }
            return add ;
        }
        if ( y == lenB ){
            long add = 0;
            for(int i = x ; i < lenA ;i++){
                if ( a.charAt(i) != c.charAt( i + y )) add ++ ;
            }
            return add ;
        }
        if ( dp[x][y] != -1) return dp[x][y];
        long ans1 = (a.charAt( x ) == c.charAt( x + y ) ? 0 : 1 ) + rec( x + 1 , y , dp , lenA , lenB , a, b, c);
        long ans2 = (b.charAt( y ) == c.charAt( x + y ) ? 0 : 1 ) + rec( x  , y + 1 , dp , lenA , lenB , a, b, c);

        return dp[x][y] = Math.min(ans1 , ans2);
    }
}


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

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while (t-->0){
           String a=sc.next();
           String b=sc.next();
           String c=sc.next();
           int[][] f=new int[a.length()+1][b.length()+1];
            for (int i = 0; i < a.length(); i++) {
                f[i+1][0]=f[i][0]+(a.charAt(i)==c.charAt(i)?0:1);
            }
            for (int i = 0; i < b.length(); i++) {
                f[0][i+1]=f[0][i]+(b.charAt(i)==c.charAt(i)?0:1);
            }
            f[0][0]=0;
            for (int i = 1; i <= a.length(); i++) {
                for (int j = 1; j <= b.length(); j++) {
                    f[i][j]=Integer.MAX_VALUE;
                    f[i][j]=Math.min(f[i][j],f[i-1][j]+(a.charAt(i-1)==c.charAt(i+j-1)? 0 : 1));
                    f[i][j]=Math.min(f[i][j],f[i][j-1]+(b.charAt(j-1)==c.charAt(i+j-1)? 0 : 1));
                }
            }
            System.out.println(f[a.length()][b.length()]);
        }
    }
}
Problem: 2060D - Subtract Min Sort
Language: java

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


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



public class Main {

    static  boolean check(int[] arr){

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

            if (arr[i]>arr[i+1]) return  false;

        }

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

            }

            boolean flag=false;

            int i =0;

            while (i< n-1){

                int min=Math.min(a[i],a[i+1]);

                a[i]=a[i]-min;

                a[i+1]=a[i+1]-min;

                i++;

            }

            if (check(a)) System.out.println("YES");

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

        }

    }

}
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: 983 ms):
import java.io.*;
import java.util.*;
public class Main
{

    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        int t = Integer.parseInt(br.readLine());
        char[] string;
        TreeSet<Integer> unused;
        ArrayList<Integer> used;
        while(t-- > 0)
        {
            char[] c = br.readLine().toCharArray();
            unused = new TreeSet<>();
            string = new char[c.length];
            for(int i = 0; i < c.length; i++)
            {
                unused.add(i);
            }
            for(int i = 0; i < c.length; i++)
            {
                int max = -1;
                int index = -1;
                int size = unused.size();
                used = new ArrayList<>();
                for(int j = 0; j < Math.min(size, 10); j++)
                {
                    int a = unused.pollFirst();
                    if(c[a] - 48 - j > max)
                    {
                        index = a;
                        max = c[a] - 48 - j;
                    }
                    used.add(a);
                }
                unused.addAll(used);
                unused.remove(index);
                string[i] = (char)(max + 48);
            }
            pw.println(new String(string));
        }
        pw.close();

    }
}
Problem: 2057C - Trip to the Olympiad
Language: java

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

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt(); // number of test cases
        // int t = 1;
        while (t-- > 0) {
            long l = sc.nextLong();
            long r = sc.nextLong();

            long ans = 0;
            long a = 0;
            long b = 0;
            // long c = 0;
            for(int i = 30;i>=0;i--) {
                long and1 = r & (1 << i);
                long and2 = l & (1 << i);

                if(and1 == and2) {
                    if(and1 >= 1) ans = ans + (1 << i);
                } else {
                    a = ans + (1 << i);
                    b = a - 1;
                    break;
                }
            }
            long c = r;
            if(a == r) {
                c = l;
            }
            System.out.println(a + " " + b + " " + c);
        }
        
    }
}



Solution 2 (runtime: 234 ms):
import java.util.*;
public class C_Trip_to_the_Olympiad {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            long l=sc.nextLong();
            long r=sc.nextLong();
            long a=0;
            long b=0;
            long c=0;
            long ans=0;
            for(int i=30;i>=0;i--){
                long x=(l&(1L<<i))!=0?1:0;
                long y=(r&(1L<<i))!=0?1:0;
                if(x==y){
                    ans+=x*(1L<<i);
                }else{
                    a=ans+(1L<<i);
                    b=a-1;
                    break;
                }
            }
            for(long i=l;i<=r;i++){
                if(a!=i&&b!=i){
                    c=i;
                    break;
                }
            }
            System.out.println(a+" "+b+" "+c);
        }
    }
}
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: 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);

        }

    }

}
Problem: 2050C - Uninteresting Number
Language: java

Solution 1 (runtime: 328 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 n=sc.next();
	        int s=0;
	        HashMap<Integer,Integer>hm=new HashMap();
	        for(int i=0;i<n.length();i++){
	            s+=n.charAt(i)-'0';
	            if((n.charAt(i)-'0')==3)
	            hm.put(6,hm.getOrDefault(6,0)+1);
	            else if((n.charAt(i)-'0')==2)
	            hm.put(2,hm.getOrDefault(2,0)+1);

	            
	        }

            int f=0;
	        if(s%9==0)
	        		System.out.println("YES");
	        		else{
	        		    for(int i=0;i<=hm.getOrDefault(2,0);i++){
	        		        if(f==1)
	        		        break;
	        		        
	        		        for(int j=0;j<=hm.getOrDefault(6,0);j++){
	        		            if((s+i*2+j*6)%9==0)
	        		            {
	        		                f=1;
	        		                break;
	        		            }
	        		        }
	        		    }
	        		    System.out.println((f==1)?"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: 2061D - Kevin and Numbers
Language: java

Solution 1 (runtime: 1249 ms):
import java.lang.*;
import java.util.*;
public class cf {
  public static final int mod = 998244353;
  public static void main(String []args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    List<String> ans = new ArrayList<>();
    for (int i = 0; i < n; i++) {
      int a = sc.nextInt();
      int b = sc.nextInt();
      int[] arr1 = new int[a];
      int[] arr2 = new int[b];
      for (int j = 0; j < a; j++) arr1[j] = sc.nextInt();
      for (int j = 0; j < b; j++) arr2[j] = sc.nextInt();
      if (gScore(arr1, arr2)) ans.add("Yes");
      else ans.add("No");
    }

    for (String i : ans) System.out.println(i);
  }

  public static boolean gScore(int[] a, int[] b) {
    PriorityQueue<Long> pq1 = new PriorityQueue<>((o1, o2) -> Long.compare(o2, o1));
    PriorityQueue<Long> pq2 = new PriorityQueue<>((o1, o2) -> Long.compare(o2, o1));
    for (int x : a) pq1.offer((long)x);
    for (int x : b) pq2.offer((long)x);
    while (!pq1.isEmpty() || !pq2.isEmpty()) {
      if (pq1.size() < pq2.size()) return false;
      if (pq1.isEmpty() || pq2.isEmpty()) return false;
      if (Objects.equals(pq1.peek(), pq2.peek())){
        pq1.poll();
        pq2.poll();
      }
      else {
        long x = pq2.poll();
        long x1 = x / 2;
        long x2 = x / 2 + x % 2;
        pq2.offer(x1);
        pq2.offer(x2);
      }
    }
    return true;
  }
}


Solution 2 (runtime: 1436 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 m = sc.nextInt();
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = sc.nextInt();
            int[] b = new int[m];
            for (int i = 0; i < m; i++) b[i] = sc.nextInt();

            PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
            for (int val : b) pq.offer(val);

            Map<Integer, Integer> freqMap = new HashMap<>();
            for (int val : a) freqMap.put(val, freqMap.getOrDefault(val, 0) + 1);

            boolean isValid = true;

            for (int i = 0; i < n - m + n; i++) {
                if (pq.isEmpty()) {
                    isValid = false;
                    break;
                }

                int val = pq.poll();
                if (freqMap.getOrDefault(val, 0) == 0) {
                    pq.offer(val / 2);
                    pq.offer((val + 1) / 2);
                } else {
                    freqMap.put(val, freqMap.get(val) - 1);
                }
            }

            System.out.println(isValid && pq.isEmpty() ? "Yes" : "No");
        }
        sc.close();
    }
}
Problem: 2062C - Cirno and Operations
Language: java

Solution 1 (runtime: 312 ms):
// package main;
import java.util.Scanner;
import java.io.PrintWriter;
import java.util.List;
import java.util.ArrayList;


// 100 * 50

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int t = scanner.nextInt();
        for (int i = 0; i < t; i++) {
            int n = scanner.nextInt();
            List<Long> ar = new ArrayList<>();

            for (int j = 0; j < n; j++) {
                ar.add(scanner.nextLong());
            }

            long res = sum(ar);

            while (ar.size() > 1) {
                res = Math.max(res, sum(ar));

                List<Long> newAr = new ArrayList<>();
                for (int j = 1; j < ar.size(); j++) {
                    newAr.add(ar.get(j) - ar.get(j - 1));
                }
                res = Math.max(res, Math.abs(sum(newAr)));
                ar = newAr;
            }
            out.println(res);
        }
        out.flush();
        out.close();
        scanner.close();
    }

    private static long sum(List<Long> ar) {
        long sum = 0;
        for (long a : ar) {
            sum += a;
        }
        return sum;
    }
}


Solution 2 (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]));
    }
}
Problem: 2057C - Trip to the Olympiad
Language: java

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

Solution 2 (runtime: 374 ms):
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int tt = sc.nextInt();
        while(tt-->0){
            int l = sc.nextInt(); int r = sc.nextInt();
            int a = pick(l,r);
            System.out.println(a);
            int b = a-1; System.out.println(b);
            int c = l; while (c==a || c==b) c++;
            System.out.println(c); 
        }
        sc.close();
    }

    public static int pick(int l, int r){
        int a = 1;
        while (a<<1 <= r) a=a<<1;

        if ((a&r) == (a&l)) return a+pick(l-a, r-a);
        return a;
    }
}