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

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



import java.util.*;



public class SubsequenceUpdate {



    public static void main(String[] args) {



        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();

        while (t-- > 0) {

            solve(sc);

        }

    }



    //subsequence does not have to be contiguous

    static void solve(Scanner sc) {

        int n = sc.nextInt();

        int l = sc.nextInt();

        int r = sc.nextInt();

        int[] a = new int[n];

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

            a[i] = sc.nextInt();

        }

        int[] b = new int[r];

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

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

            b[i] = a[i];

        }

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

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

        }

        Arrays.sort(b);

        Arrays.sort(c);

        long sum_b = 0, sum_c = 0;

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

            sum_b += b[i];

            sum_c += c[i];

        }

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

    }

}



Solution 2 (runtime: 359 ms):


import java.util.*;

import java.io.*;

public class Main {



	public static class cmp implements Comparator<Integer>{

		public int compare(Integer a,Integer b) {

			return a-b;

		}

	}

	public static BufferedReader rd=new BufferedReader(new InputStreamReader(System.in));

	public static BufferedWriter wd=new BufferedWriter(new OutputStreamWriter(System.out));

	public static void solve()throws Exception{

		String dr[]=rd.readLine().split(" ");

		int n=Integer.parseInt(dr[0]),l=Integer.parseInt(dr[1]),r=Integer.parseInt(dr[2]);

		long a[]=new long [n+10],b[]=new long [n+10],c[]=new long [n+10];

		int t1=0,t2=0,t3=0;

		dr=rd.readLine().split(" ");

		long res=0;

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

			int x=Integer.parseInt(dr[i-1]);

			if(i<l) a[++t1]=x;

			if(i>r) {

				c[++t3]=x;

			//	res+=x;

			}

			if(i>=l&&i<=r){

				b[++t2]=x;

				res+=x;

			//	wd.write(res+" "+x+"\n");

			}

		}

	//	wd.write(res+"\n");

	//	for(int i=1;i<=t2;i++) wd.write(b[i]+"\n");

		Arrays.sort(a,1,1+t1);Arrays.sort(b,1,1+t2);Arrays.sort(c,1,1+t3);

	//	wd.write(res+"\n");

		//for(int i=1;i<=t3;i++) wd.write(c[i]+"\n");

		for(int i=1;i<=t1;i++) a[i]+=a[i-1];

		for(int i=1;i<=t2;i++) b[i]+=b[i-1];

		for(int i=1;i<=t3;i++) c[i]+=c[i-1];

		long ans=res;

		int k=Math.min(t1,t2);

		//wd.write(res+" "+ans+"\n");

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

			long tem=b[t2]-b[t2-i];

			//wd.write(tem+" "+i+"\n");

			//wd.write(tem-a[i]+"\n");

			ans=Math.min(ans, res-(tem-a[i]));

		}

		int k2=Math.min(t2, t3);

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

			long tem=b[t2]-b[t2-i];

		//wd.write(i+" "+tem+" "+(tem-c[i])+"\n");

			ans=Math.min(ans,res-(tem-c[i]));

		}

		wd.write(ans+"\n");

		//long res=0;

		//for(int i=1;i<=t3;i++) wd.write(c[i]+"\n");

	}

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

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

		while(t-->0) {

			solve();

			wd.flush();

		}

	}



}
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: 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: 311 ms):
import java.io.*;
import java.util.*;

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

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

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


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

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

Solution 1 (runtime: 312 ms):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class prob {

    static class FastReader {
        BufferedReader b;
        StringTokenizer s;

        public FastReader() {
            b = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (s == null || !s.hasMoreElements()) {
                try {
                    s = new StringTokenizer(b.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return s.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        float nextFloat() {
            return Float.parseFloat(next());
        }

        boolean nextBoolean() {
            return Boolean.parseBoolean(next());
        }

        // Method to read the next line as a string
        String nextLine() {
            String str = "";
            try {
                if (s == null || !s.hasMoreTokens()) {
                    str = b.readLine();
                } else {
                    str = s.nextToken("\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    //===================================================================================================

    public static void main(String[] args) {
        FastReader fastReader = new FastReader();

        int t=0;
        t = fastReader.nextInt();

        while(t-- >0){
            int n = fastReader.nextInt();
            int[] arr = new int[n];

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

            solve(arr, n);
        }
    }

    public static void solve(int[] arr, int n){

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

        for(int i=0;i<n-1;i++){
            if(arr[i+1]<arr[i]){
                System.out.println("NO");
                return;
            }
        }

        System.out.println("YES");
    }
}

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: 2062C - Cirno and Operations
Language: java

Solution 1 (runtime: 327 ms):
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;

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

    int t = sc.nextInt();
    for (int tc = 0; tc < t; ++tc) {
      int n = sc.nextInt();
      int[] a = new int[n];
      for (int i = 0; i < a.length; ++i) {
        a[i] = sc.nextInt();
      }

      System.out.println(solve(a));
    }

    sc.close();
  }

  static long solve(int[] a) {
    long[] values = Arrays.stream(a).asLongStream().toArray();

    long result = Arrays.stream(values).sum();
    while (values.length != 1) {
      long[] values_ = values;
      values =
          IntStream.range(0, values.length - 1)
              .mapToLong(i -> values_[i + 1] - values_[i])
              .toArray();

      result = Math.max(result, Math.abs(Arrays.stream(values).sum()));
    }

    return result;
  }
}


Solution 2 (runtime: 311 ms):

import java.util.Scanner;

public class Sol {
    static Scanner rd = new Scanner(System.in);
    public static void main(String[] args) {
        int tt = rd.nextInt();
        while (tt-- > 0) {
            new Solution().solve();
        }
    }

    static class Solution {
        void solve() {
            int n = rd.nextInt();
            long[] a = new long[n];

            for (int i = 0; i < n; i++) {
                a[i] = rd.nextInt();
            }
            int len = n;
            if (len == 1) {
                System.out.println(a[0]);
                return;
            }
            long max = getSum(a, n, false);
            while (len > 1) {
                for (int i = 0; i < len - 1; i++) {
                    a[i] = a[i + 1] - a[i];
                }
                len--;
                max = Math.max(Math.max(getSum(a, len, true), getSum(a, len, false)), max);
            }
            System.out.println(max);
        }

        long getSum(long[] a, int len, boolean neg) {
            long sum = 0;
            for (int i = 0; i < len; i++) {
                if (neg) {
                    sum += (-1) * a[i];
                } else {
                    sum += a[i];
                }
            }
            return sum;
        }
    }
}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 687 ms):


import java.util.*;



public class AS {



    public static void main(String[] args) {



        Scanner s = new Scanner(System.in);



        int t = s.nextInt();

        s.nextLine();



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



            int c = s.nextInt();

            s.nextLine();



            int[] arr = new int[c];



            for (int j = 0; j < c; j++) {



                arr[j] = s.nextInt();

            }

            s.nextLine();



            boolean flag = true;

            for (int j = 0; j < c - 1; j++) {



                int temp = Math.min(arr[j], arr[j + 1]);

                arr[j] -= temp;

                arr[j + 1] -= temp;



                if (arr[j] > 0) {

                    flag = false;

                    break;

                }

            }



            if (flag)

                System.out.println("yes");

            else

                System.out.println("no");



        }



        s.close();

    }

}

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

public class Picky {
//    static boolean tcExist=true;

    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        long t=sc.nextLong();
        while(t-->0){
            long n=sc.nextLong();
            long m=sc.nextLong();
            long[] a=new long[(int) n];
            long[] b=new long[(int) m];

            for(int i=0; i<n; i++) a[i]=sc.nextLong();
            for(int i=0; i<m; i++) b[i]=sc.nextLong();
            Arrays.sort(a);
            Arrays.sort(b);

            Map<Long, Long> mpp=new HashMap<>();
            for(long num : a) mpp.put(num, mpp.getOrDefault(num, 0L)+1);

            PriorityQueue<Long> pq=new PriorityQueue<>(Collections.reverseOrder());
            for(long num : b) pq.add(num);

            long c=n-m;
            int j=0;

            while(c>=0 && !pq.isEmpty()){
                if(mpp.isEmpty()) break;
                long ele=pq.poll();

                if(mpp.containsKey(ele)){
                    long count = mpp.get(ele);
                    if(count>1) mpp.put(ele, count-1);
                    else mpp.remove(ele);
                }
                else{
                    if(ele==1){
                        System.out.println("No");
                        return;
                    }
                    long x=ele/2;
                    long y=ele/2;
                    if(ele%2!=0) y++;
                    pq.add(x);
                    pq.add(y);
                    c--;
                }
            }

            if(c>=0 && pq.isEmpty() && mpp.isEmpty()) System.out.println("Yes");
            else System.out.println("No");
        }
    }
}
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: 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;
    }
}