ShaysXIA/CodeInsightBench huggingface dataset:
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: 187 ms):
// package recurison;

import java.io.*;
import java.util.*;

public class ddd {
	public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-- > 0) {
        	int n = sc.nextInt();
        	ArrayList<Long> ll = new ArrayList<>();
        	long ans = 0;
        	for(int i = 0; i < n; i++) {
        		long a = sc.nextInt();
        		ll.add(a);
        		ans += a;
        	}
        	
        	while(n-- > 1) {
        		ArrayList<Long> arr = new ArrayList<>();
        		for(int i = 1; i < ll.size(); i++) {
        			arr.add(ll.get(i)-ll.get(i-1));
        		}
        		
        		long sum = 0;
        		for(long i : arr) sum += i;
        		ans = Math.max(ans, Math.abs(sum));
        		ll = new ArrayList<>(arr);
        	}
        	
        	System.out.println(ans);
        }
    }
}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 702 ms):
import java.util.*;
         
public class CompCode {
    static Scanner sc = new Scanner(System.in);
 
    static int MOD = 998244353; 
 
    public static void main(String... args) {
        int tc = sc.nextInt();
        while(tc-- > 0) {
            int n = sc.nextInt();
            int[] a = new int[n];
            int i;
            for(i=0; i<n; i++) {
                a[i] = sc.nextInt();
            }

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

            for(i=1; i<n-1; i++) {
                if(a[i]<a[i-1]) {
                    break;
                }
                if(a[i]-a[i-1] > a[i+1]) {
                    break;
                }
                a[i] = a[i]-a[i-1];
                // System.out.print(a[i]+ " ");
            }
            if(i==n-1) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        } 
    }
}

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

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

public class Main {
	static InputStream inputStream;
	static OutputStream outputStream;
	static InputReader in;
	static PrintWriter out;
	static int MULTIPLE_TESTACES = 1;
  
	///////////////////////////////////////
	// solution
	///////////////////////////////////////
	static class Solution {
    
    private boolean pos(ArrayList<Long> o){
			int n = o.size();
			ArrayList<Long> a = new ArrayList<Long>();
			for(Long x: o){
				a.add(x);
			}
			for(int i=0; i<n-1;++i){
				long x = a.get(i);
				long y = a.get(i+1);
				long m = Math.min(x, y);
				a.set(i, x-m);
				a.set(i+1, y-m);
			}
			for(int i=0; i<n-1;++i){
				long x = a.get(i);
				long y = a.get(i+1);
				if(x > y)return false;
			}
			return true;
		}
		
		public void solve(int tc) throws IOException{
			int n = in.nextInt();
			ArrayList<Long> a = new ArrayList<Long>();
			for(int i=0; i<n;++i){
				a.add(in.nextLong());
			}
			boolean ans = false;
			ans |= pos(a);
			out.println(ans ? "YES":"NO");
		}	
	}
	
	
	
	
	
	///////////////////////////////////////
	// template
	///////////////////////////////////////
	public static void main(String[] args) throws IOException{
			inputStream = System.in;
			outputStream = System.out;
			in = new InputReader(inputStream);
			out = new PrintWriter(outputStream);
			int tc = 1;
			if(MULTIPLE_TESTACES>0)tc = in.nextInt();
			Solution sol = new Solution();
			for(int i=1; i<=tc;++i){
				 sol.solve(i);
			}
			in.close();
			out.close();
	}
	
	static class InputReader {
		BufferedReader reader;
		StringTokenizer st;
		public InputReader(InputStream in){
			reader = new BufferedReader(new InputStreamReader(in));
			st = null;
		}
		
		public String next() throws IOException{
			while(st == null || !st.hasMoreTokens()){
				st = new StringTokenizer(reader.readLine());
			}
			return st.nextToken();
		}
		
		public Long nextLong() throws IOException{
			return Long.parseLong(next());
		}
		
		public Integer nextInt() throws IOException{
			return Integer.parseInt(next());
		}
		
		public void close() throws IOException{
			reader.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: 2050D - Digital string maximization
Language: java

Solution 1 (runtime: 186 ms):
import java.util.*;
public class practt {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String r = sc.nextLine();
        String[] arr = new String[n];
        for(int ifg = 0; ifg < n; ifg++){
            String temp = sc.next();
            int[] tt = new int[temp.length()];
            for(int i = 0; i < temp.length(); i++){
                tt[i] = (int)temp.charAt(i)-'0';
            }
            int fault =  1;
            while(fault!=0){
                int cnt = 0;
                //System.out.println("hi");
                for(int i = 1;i < tt.length;i++){
                    //System.out.println("hii");
                    if(tt[i-1]<(tt[i]-1)){
                        int s = tt[i-1];
                        tt[i-1]=tt[i]-1;
                        tt[i]=s;
                        cnt++;
                    }
                }
                fault=cnt;
            }
            StringBuilder ans = new StringBuilder();
            for(int i=0;i<tt.length;i++){
                ans.append((char)(tt[i]+'0'));
            }
            arr[ifg] = ans.toString();
        }
        for(String a:arr){
            System.out.println(a);
        }

    }
}


Solution 2 (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();
    }
}
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: 405 ms):
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            int l = in.nextInt(), r = in.nextInt();

            int a = r;
            int b = 0;

            int x = 0b01111111111111111111111111111111;
            for (int j = 30; j >= 1; j--) {
                x &= ~(1 << j);
                if (inRange(a ^ x, l, r)) {
                    b = a ^ x;
                    break;
                }
            }

            int c = l;
            while (c < r && (c == a || c == b)) c ++;
            System.out.println(a + " " + b + " " + c);
        }
    }

    private static boolean inRange(int num, int l, int r) {
        return num >= l && num <= r;
    }
}
Problem: 2060D - Subtract Min Sort
Language: java

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

public class Main {
    public static void main(String[] args) {
      Scanner s=new Scanner(System.in);
      int t=s.nextInt();
      while(t-->0){
        int n=s.nextInt();
        int arr[]=new int[n];
        for(int i=0;i<n;i++){
          arr[i]=s.nextInt();
        }
        for(int i=1;i<n;i++){
          int a=Math.min(arr[i],arr[i-1]);
          arr[i-1]-=a;
          arr[i]-=a;
        }
        boolean ans=true;
        for(int i=0;i<n-1;i++){
          if(arr[i]!=0){
            ans=false;
            break;
          }
        }
        if(ans){
          System.out.println("YES");
        }else{
          System.out.println("NO");
        }
      }
  }
}

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


        }
    }
}
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: 562 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 k=sc.nextInt();
            int[] a=new int[n];
            HashMap<Integer,Integer> mp=new HashMap<>();
            for(int i=0;i<n;i++)
            {
                a[i]=sc.nextInt();
                if(!mp.containsKey(a[i]))
                    mp.put(a[i],1);
                else {
                    mp.put(a[i], mp.get(a[i]) + 1);
                }
            }
            ArrayList<Integer> l=new ArrayList<>();
            for(int e:mp.values())
            {
                l.add(e);
            }
            Collections.sort(l);
            int ans=l.size();
            for(int i:l)
            {
                k-=i;
                if(k<0)
                    break;
                ans--;
            }
            if(ans<1)
                System.out.println(1);
            else System.out.println(ans);

        }

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

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




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: 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: 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: 2060D - Subtract Min Sort
Language: java

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

        }

    }

}

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