ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2062C - Cirno and Operations
Language: java

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

public class Main {


    public static void main(String[] args) {
        int t=1;
        Scanner sc = new Scanner(System.in);
        t=sc.nextInt();
        while(t-->0){
            int n=sc.nextInt();
            long[] arr=new long[n];
            long ans=0;
            for(int i=0;i<n;i++){
                arr[i]=sc.nextInt();
                ans+=arr[i];
            }

            while(n>1){
                ans=Math.max(ans,Math.abs(arr[n-1]-arr[0]));
                for (int i = 0; i < n-1; i++) {
                    arr[i]=arr[i+1]-arr[i];
                }
                n--;
            }
            System.out.println(ans);
        }
    }
}


Solution 2 (runtime: 312 ms):
import java.util.*;
public class cirnoop2
{
   public static void main(String args[])
   {
       Scanner in = new Scanner(System.in);
       int t=in.nextInt();
       while(t!=0)
       {
           t--;
           int n=in.nextInt();
           long arr[]=new long[n];
           int i,j;
           long s=0,max=0;
           
           for(i=0;i<n;i++)
           {
               arr[i]=in.nextInt();
               s=s+arr[i];
           }
           if(n==1)
           {
               System.out.println(arr[0]);
           }          
           else
           {
           
           max=Math.max(s,-(long)Math.pow(10,18));
           while(n>=1)
           {
               
               
               --n;
               
               s=0;
               
               for(i=0;i<n;i++)
               {
                   arr[i]=arr[i+1]-arr[i];
                   s=s+arr[i];
               }
               arr[n]=0;
               
              
               s=Math.abs(s);
               max=Math.max(s,max);
           }
           System.out.println(max);
           }
           
   }
}
}
Problem: 2057B - Gorilla and the Exam
Language: java

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

public class cfhello2025B 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int ret[] = new int[n];
        for(int i = 0;i<n;i++)
        {
            int size = sc.nextInt();
            int k = sc.nextInt();
            int temp[] = new int[size];
            Map<Integer,Integer> store = new HashMap<>();
            for(int j = 0;j<size;j++)
            {
                temp[j] = sc.nextInt();
                if(store.containsKey(temp[j]))
                {
                    store.put(temp[j],store.get(temp[j])+1);
                }
                else
                {
                    store.put(temp[j],1);
                }
            }
            PriorityQueue<Integer> occ = new PriorityQueue<>();
            for (Map.Entry<Integer, Integer> entry : store.entrySet()) 
            {
                occ.add(entry.getValue());
            }
            while(k!=0)
            {
                if(occ.size() == 0)
                {
                    break;
                }
                int smallest = occ.poll();
                if(k>=smallest)
                {
                    k-=smallest;
                }
                else
                {
                    occ.add(smallest);
                    break;
                }
            }
            ret[i] = occ.size();
            if(occ.size() == 0)
            {
                ret[i] = 1;
            }
        }
        for(int i = 0;i<ret.length;i++)
        {
            System.out.println(ret[i]);
        }
    }
}


Solution 2 (runtime: 390 ms):
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;

public class B_Gorilla_And_The_Exam {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int t = input.nextInt();
		for(int test = 0; test < t; test++) {
			int n = input.nextInt();
			int k = input.nextInt();
			HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>();
			for(int i = 0; i < n; i++) {
				int a = input.nextInt();
				int count = counts.getOrDefault(a, 0);
				counts.put(a, count + 1);
			}
			List<Integer> sortedCounts = new ArrayList<Integer>(counts.values());
			Collections.sort(sortedCounts);
			int index = 0;
			while(index < sortedCounts.size() && k >= sortedCounts.get(index)) {
				k -= sortedCounts.get(index);
				index++;
			}
			System.out.println(Math.max(1, sortedCounts.size() - index));
		}
		input.close();
	}

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

Solution 1 (runtime: 656 ms):
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){

            int n=sc.nextInt();
            int[] arr = new int[n];

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

            boolean flag = true;
            for(int i=0;i<n-1;i++){
                int min = Math.min(arr[i],arr[i+1]);
                arr[i] = arr[i]-min;
                arr[i+1] = arr[i+1]-min;
            }

            for(int i=0;i<n-1;i++){
                if(arr[i]>arr[i+1]){
                    System.out.println("NO");
                    flag = false;
                    break;
                }
            }
            if(flag){
                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: 687 ms):
import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
    static long mod = 1000000007;
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
        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();
            }
            boolean ans = true;     
            for(int i=0;i<n-1;i++){
                int x = Math.min(arr[i],arr[i+1]);
                arr[i]-=x;
                arr[i+1]-=x;
                if(arr[i]>arr[i+1]){
                    ans = false;
                    break;
                }
            }
            
            if(ans){
                System.out.println("YES");
            }
            else{
                System.out.println("NO");
            }
        }
	}
}

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

public class div2 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int i = 0 ; i<t ; i++){
            int n = sc.nextInt();

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

            int ind = 0;
            boolean flag = true;
            while(ind < n-1){
                if(arr[ind] <= arr[ind+1]){
                    arr[ind+1] -= arr[ind];
                    arr[ind] = 0;
                    ind++;
                }
                else {
                    flag = false;
                    break;
                }
            }
            if(!flag) System.out.println("NO");
            else System.out.println("YES");
        }
    }
}
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: 312 ms):
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
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();
            long sum=0;
            long cnt1=0;
            long cnt2=0;
            for(long i=0; i<n.length(); i++){
                char ch=n.charAt((int)i);
                if(ch=='2'){
                    cnt1++;
                }
                else if(ch=='3'){
                    cnt2++;
                }
                sum+=(ch-'0');
                sum=sum%9;
            }
                boolean check=false;
                for(int i=0; i<=cnt1; i++){
                    for(int j=0; j<=cnt2; j++){
                        long temp1=sum;
                        temp1=temp1+(i*2)+(j*6);
                        if(temp1%9==0){
                            check=true;
                            break;
                        }
                    }
                    if(check==true){
                        break;
                    }
                }
                if(check==true){
                    System.out.println("YES");
                }
                else{
                    System.out.println("NO");
                }
            t--;
        }
    }
}
Problem: 2050D - Digital string maximization
Language: java

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

public class Solution {

    static void tc(Scanner sc){
        String str = sc.next();
        Stack<Integer> ans = new Stack<>();
        for(int i=0; i<str.length(); i++){
            int x = str.charAt(i)-'0';
            
            Stack<Integer> temp = new Stack<>();
            while (ans.isEmpty()==false && ans.peek() < x-1) {
                x--;
                temp.add(ans.pop());
            }
            ans.add(x);
            while (temp.isEmpty()==false) {
                ans.add(temp.pop());
            }
        }
        for(int x:ans){
            System.out.print(x);
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            // System.out.println(t);
            tc(sc);
        }
        sc.close();
    }
}

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

public class DigitalStringMaximization{

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String[] result = new String[n];
		for(int i = 0; i < n; i++){
			result[i]= maximize(sc.next());
		}
		Arrays.stream(result).forEach(System.out::println);
	}
	
	private static String maximize(String str){
		char[] chars = str.toCharArray();
		int num;
		for(int i = 1; i < chars.length; i++){
			num = chars[i] - '0';
			for(int j = num; j >= 0; j--){
				if( i - j < 0) continue;
				if(chars[ i - j ] < (char)(chars[i] - j)){
					char temp = (char)(chars[i] - j);
					for(int k = i; k > i - j; k--){
						chars[k] = chars[k - 1];
					}
					chars[i - j] = temp;
					break;
				}
			}
		} 
		return new String(chars);
	}
	
	
}

/*
i = 1 51476
i = 2 53176
i = 3 53616 55316
I = 4 55316
i = 5 55351 55431
19


*/
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: 656 ms):
import java.util.*;

public class Main{

public static void main(String args[]){

Scanner scanner = new Scanner(System.in);

int tests = scanner.nextInt();

while(tests-- > 0){

int n = scanner.nextInt();

int A[] = new int[n];

boolean flag = false;

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

A[i] = scanner.nextInt();

}

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

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

flag = true;

break;

}else{

int temp = A[i];

A[i] -= temp;

A[i + 1] -= temp;

}

}

if(!flag){

System.out.println("YES");

}else{

System.out.println("NO");

}

}

scanner.close();

}



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

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

public class Main{

public static void main(String args[]){

Scanner scanner = new Scanner(System.in);

int tests = scanner.nextInt();

while(tests-- > 0){

int n = scanner.nextInt();

int A[] = new int[n];

boolean flag = false;

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

A[i] = scanner.nextInt();

}

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

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

flag = true;

break;

}else{

int temp = A[i];

A[i] -= temp;

A[i + 1] -= temp;

}

}

if(!flag){

System.out.println("YES");

}else{

System.out.println("NO");

}

}

scanner.close();

}



}

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



public class mywork {

public static boolean isPrime(int n) {

if (n <= 1) {

return false;

}

for (int i = 2; i * i <= n; i++) {

if (n % i == 0) {

return false;

}

}

return true;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

long T = sc.nextLong();

while (T-- > 0) {

int N = sc.nextInt();

int A[] = new int[N];

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

A[i] = sc.nextInt();

}

int flag = 0;

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

if(A[i+1]<A[i]){

flag=1;

break;

}

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

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

A[i]= A[i]-min;

}

if(flag==1){

System.out.println("NO");

}else{

System.out.println("YES");

}



}

}

}
Problem: 2050E - Three Strings
Language: java

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

public class Main {
    private static final int MAX_LEN = 2003; // Ensure larger array size for safety

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        scanner.nextLine(); // Consume the newline character

        while (t-- > 0) {
            String a = scanner.nextLine();
            String b = scanner.nextLine();
            String c = scanner.nextLine();

            int n = a.length();
            int m = b.length();
            int[][] dp = new int[n + 1][m + 1]; // Use n + 1 and m + 1 for proper indexing

            for (int i = 0; i <= n; i++) {
                for (int j = 0; j <= m; j++) {
                    dp[i][j] = MAX_LEN; // Initialize with a large value
                }
            }

            dp[0][0] = 0;

            for (int i = 0; i <= n; i++) {
                for (int j = 0; j <= m; j++) {
                    if (i > 0) {
                        dp[i][j] = Math.min(dp[i][j], dp[i - 1][j] + (a.charAt(i - 1) != c.charAt(i + j - 1) ? 1 : 0));
                    }
                    if (j > 0) {
                        dp[i][j] = Math.min(dp[i][j], dp[i][j - 1] + (b.charAt(j - 1) != c.charAt(i + j - 1) ? 1 : 0));
                    }
                }
            }

            System.out.println(dp[n][m]);
        }
        scanner.close(); // Close the scanner to avoid resource leaks
    }
}


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

public class E {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);

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

        while (tc-- > 0) {
            String s1 = br.readLine();
            String s2 = br.readLine();
            String s3 = br.readLine();

            int[][] dp = new int[s2.length()+1][s1.length()+1];

            for (int c = 1; c < dp[0].length; c++) {
                dp[0][c] = dp[0][c-1];                
                if (s3.charAt(c-1) != s1.charAt(c-1)) {
                    dp[0][c]++;
                }
            }

            for (int r = 1; r < dp.length; r++) {
                dp[r][0] = dp[r-1][0];
                if (s3.charAt(r-1) != s2.charAt(r-1)) {
                    dp[r][0]++;
                }
            }

            for (int r = 1; r < dp.length; r++) { // r = s2 used
                for (int c = 1; c < dp[0].length; c++) { // c = s1 used
                    int s1add = s3.charAt(r+c-1) == s1.charAt(c-1) ? 0 : 1;
                    int s2add = s3.charAt(r+c-1) == s2.charAt(r-1) ? 0 : 1;

                    dp[r][c] = Math.min(dp[r-1][c] + s2add, dp[r][c-1] + s1add);
                }
            }
            
            pw.println(dp[s2.length()][s1.length()]);
        }

        pw.close();
    }
}
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: 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();
    }
}
Problem: 2062C - Cirno and Operations
Language: java

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

public class Main
{
    private static void solve(Scanner in)
    {
        int n = in.nextInt();

        long[] a = new long[n];

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

        long ans = 0;

        for (long el : a)
            ans += el;

        for (int it = 1; it < n; it++)
        {
            ans = Math.max(ans, Math.abs(a[n - it] - a[0]));

            for (int i = 0; i + it < n; i++)
                a[i] = a[i + 1] - a[i];
        }

        System.out.println(ans);
    }

    public static void main (String[] args)
    {
        Scanner in = new Scanner(System.in);

        int t = in.nextInt();

        for (int test = 0; test < t; test++)
            solve(in);
        
        in.close();
    }
}

Solution 2 (runtime: 312 ms):
import java.util.*;
public class cirnoop2
{
   public static void main(String args[])
   {
       Scanner in = new Scanner(System.in);
       int t=in.nextInt();
       while(t!=0)
       {
           t--;
           int n=in.nextInt();
           long arr[]=new long[n];
           int i,j;
           long s=0,max=0;
           
           for(i=0;i<n;i++)
           {
               arr[i]=in.nextInt();
               s=s+arr[i];
           }
           if(n==1)
           {
               System.out.println(arr[0]);
           }          
           else
           {
           
           max=Math.max(s,-(long)Math.pow(10,18));
           while(n>=1)
           {
               
               
               --n;
               
               s=0;
               
               for(i=0;i<n;i++)
               {
                   arr[i]=arr[i+1]-arr[i];
                   s=s+arr[i];
               }
               arr[n]=0;
               
              
               s=Math.abs(s);
               max=Math.max(s,max);
           }
           System.out.println(max);
           }
           
   }
}
}