diff --git "a/version_1/validation.csv" "b/version_1/validation.csv" new file mode 100644--- /dev/null +++ "b/version_1/validation.csv" @@ -0,0 +1,43573 @@ +filename_1,filename_2,code_1,code_2,labels,notes +4a5b029f,fb05904c,"/* package whatever; // don't place package name! */ + +import java.util.*; +import java.lang.*; +import java.io.*; + +/* Name of the class has to be ""Main"" only if the class is public. */ +public class Ideone +{ + public static void main (String[] args) throws java.lang.Exception + { + Scanner scn = new Scanner(System.in); + int t = scn.nextInt(); + while(t>0){ + int n = scn.nextInt(); + String a = scn.next(); + String b = scn.next(); + + // System.out.println(a); + int ob=0,oa=0,za=0; + for(int i=0;i0 && od>0 && os>0 && zs>0) str.append(Math.min(nsame, same)).append(""\n""); + // else if(zd>0 && od>0) str.append(nsame).append(""\n""); + // else if(zs>0 && os>0) str.append(same).append(""\n""); + // else str.append(""-1\n""); + if(same==1 && nsame==n-1 && os==1) str.append(""1\n""); + else if(zd==od && od>0 && os>0 && os-zs==1) str.append(Math.min(nsame, same)).append(""\n""); + else if(zd==od && od>0) str.append(nsame).append(""\n""); + else if(os>0 && os-zs==1) str.append(same).append(""\n""); + else str.append(""-1\n""); + } + } + } + + public static void main(String[] args) throws java.lang.Exception { + BufferedReader bf; + PrintWriter pw; + boolean lenv=false; + if(lenv){ + bf = new BufferedReader( + new FileReader(""input.txt"")); + pw=new PrintWriter(new + BufferedWriter(new FileWriter(""output.txt""))); + }else{ + bf = new BufferedReader(new InputStreamReader(System.in)); + pw = new PrintWriter(new OutputStreamWriter(System.out)); + } + + int t = Integer.parseInt(bf.readLine().trim()); + while (t-- > 0) { + String st[]=bf.readLine().trim().split(""\\s+""); + n=Integer.parseInt(st[0]); + a=bf.readLine().trim().toCharArray(); + b=bf.readLine().trim().toCharArray(); + solve(); + } + pw.println(str); + pw.flush(); + // System.outin.print(str); + } +}",0,Non-plagiarised +69c97258,db01da00,"import java.util.Scanner; +import java.util.ArrayList; +import java.lang.Math; + +public class fell { + private static final Scanner cin = new Scanner(System.in); + private static final int maxVal = 1000000000; + + public static void main(String[] args) { + int n = cin.nextInt(); + ArrayList a = new ArrayList<>(); + ArrayList b = new ArrayList<>(); + + for (int i = 0; i < n; i++) { + int t = cin.nextInt(); + if (t == 0) + a.add(i); + else + b.add(i); + } + + if (a.size() == n) { + System.out.println(0); + return; + } + + int[][] dp = new int[n][n]; + + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) { + if (i == 0) + dp[i][j] = 0; + else + dp[i][j] = maxVal; + } + + for (int i = 0; i < a.size(); i++) { + for (int j = 0; j < Math.min(i + 1, b.size() + 1); j++) { + dp[i + 1][j] = Math.min(dp[i + 1][j], dp[i][j]); + if (j == b.size()) + continue; + dp[i + 1][j + 1] = Math.min(dp[i + 1][j + 1], dp[i][j] + Math.abs(a.get(i) - b.get(j))); + } + } + + System.out.println(dp[a.size()][b.size()]); + } +}","import java.util.*; +import java.lang.*; +import java.io.*; + +public class Main { + static int func(List ones, List zeroes, int idO, int idZ, int o, int z, Integer[][] memo) { + if(idZ == zeroes.size() || idO == ones.size()) { + return 0; + } + + if(memo[idO][idZ] != null) { + return memo[idO][idZ]; + } + + if(o == z) { + return memo[idO][idZ] = Math.abs(ones.get(idO) - zeroes.get(idZ)) + + func(ones, zeroes, idO + 1, idZ + 1, o - 1, z - 1, memo); + } else { + return memo[idO][idZ] = Math.min( + Math.abs(ones.get(idO) - zeroes.get(idZ)) + + func(ones, zeroes, idO + 1, idZ + 1, o - 1, z - 1, memo), + func(ones, zeroes, idO, idZ + 1, o, z - 1, memo) + ); + } + } + + public static void main (String[] args) { + // Scanner scan = new Scanner(System.in); + FastScanner scan = new FastScanner(); + int n = scan.nextInt(); + int[] arr = scan.readArray(n); + + List ones = new ArrayList<>(); + List zeroes = new ArrayList<>(); + + for(int i = 0; i < n; i++) { + if(arr[i] == 0) { + zeroes.add(i); + } else { + ones.add(i); + } + } + + int o = ones.size(); + int z = zeroes.size(); + + Integer[][] memo = new Integer[o + 1][z + 1]; + + int ans = func(ones, zeroes, 0, 0, o, z, memo); + + System.out.println(ans); + } +/* + int n = scan.nextInt(); + int[] arr = scan.readArray(n); + + int n = scan.nextInt(); + int m = scan.nextInt(); + int[][] arr = new int[n][m]; + for(int i = 0; i < n; i++) { + for(int j = 0; j < m; j++) { + arr[i][j] = scan.nextInt(); + } + } + +*/ + + + static class FastScanner { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(""""); + String next() { + while (!st.hasMoreTokens()) + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + int[] readArray(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) a[i] = nextInt(); + return a; + } + long nextLong() { + return Long.parseLong(next()); + } + } + +}",0,Non-plagiarised +ca0c55ad,e14d1ba0,"import java.util.*; +public class ss +{ + public static void main(String[]args) + { + Scanner in=new Scanner (System.in); + int t=in.nextInt(); + for(int i1=0;i1ar[i]) + { + m=ar[i]; + } + s1+=ar[i]; + ans[i]=s1-m+m*(n-i/2); + + + + + } + ans[1]=ar[1]*n; + m=ar[1]; + s1=ar[1]; + for(int i=3;iar[i]) + { + m=ar[i]; + } + s1+=ar[i]; + ans[i]=s1-m+m*(n-i/2); + } + long mini=ans[0]+ans[1]; + for(int i=1;iar[i]) + { + m=ar[i]; + } + s1+=ar[i]; + ans[i]=s1-m+m*(n-i/2); + + + + + } + ans[1]=ar[1]*n; + m=ar[1]; + s1=ar[1]; + for(int i=3;iar[i]) + { + m=ar[i]; + } + s1+=ar[i]; + ans[i]=s1-m+m*(n-i/2); + } + long mini=ans[0]+ans[1]; + for(int i=1;i0) + { + int n=input.nextInt(); + int a[]=new int[n]; + for(int i=0;i=2;i--) + { + if(b[i]+arr[i] 0) { + //out.print(""Case #""+(a++)+"": ""); + solver.call(in,out); + t--; + } + out.close(); + + } + + static class TaskA { + int n; + long[] arr; + public void call(InputReader in, PrintWriter out) { + n = in.nextInt(); + arr = new long[n]; + + for (int i = 0; i < n; i++) { + arr[i] = in.nextLong(); + } + long l, r, mid; + l = 0; + r = (long)1e10; + while (l + 1 < r){ + mid = ( l + r)/2; + if(ans(mid)){ + l = mid; + } + else{ + r = mid; + } + } + + out.println(l); + } + + public boolean ans(long mid) { + long[] array = new long[n]; + long a, b; + for (int i = n - 1; i >= 2; i--) { + a = mid - array[i]; + if(a > 0){ + b = arr[i] - a; + if(b >= 0){ + b/=3; + array[i - 1] += b; + array[i - 2] += 2*b; + } + else{ + return false; + } + } + else{ + b = arr[i]; + b/=3; + array[i - 1] += b; + array[i - 2] += 2*b; + } + } + for (int i = 0; i < 2; i++) { + if(arr[i] + array[i] < mid){ + return false; + } + } + return true; + } + } + + static int gcd(int a, int b) + { + if (a == 0) + return b; + return gcd(b % a, a); + } + + static int lcm(int a, int b) + { + return (a / gcd(a, b)) * b; + } + + + static class answer implements Comparable{ + int a, b; + + public answer(int a, int b) { + this.a = a; + this.b = b; + } + @Override + public int compareTo(answer o) { + return Integer.compare(this.a, o.a); + } + + } + + static class answer1 implements Comparable{ + int a, b, c; + + public answer1(int a, int b, int c) { + this.a = a; + this.b = b; + this.c = c; + + } + + @Override + public int compareTo(answer1 o) { + return (o.a - this.a); + } + } + + static long gcd(long a, long b) + { + if (b == 0) + return a; + return gcd(b, a % b); + } + + static void sort(long[] a) { + ArrayList l = new ArrayList<>(); + for (Long i:a) l.add(i); + l.sort(Collections.reverseOrder()); + for (int i=0; i0) { + int n = sc.nextInt(); + Set sums = new HashSet<>(); + boolean found = false; + int[] arr = readArrayInt(n); + for(int j = 0;j copy = new ArrayList<>(); + for (int i : array) + copy.add(i); + Collections.sort(copy); + for(int i = 0;i0) { + int n = sc.nextInt(); + Set sums = new HashSet<>(); + boolean found = false; + int[] arr = readArrayInt(n); + for(int j = 0;j copy = new ArrayList<>(); + for (int i : array) + copy.add(i); + Collections.sort(copy); + for(int i = 0;i p = new HashSet<>(); +static boolean debug =true; +// Arrays.sort(time , (a1,a2) -> (a1[0]-a2[0])); 2d array sort lamda +public static void main(String[] args) throws Exception { +br = new BufferedReader(new InputStreamReader(System.in)); +PrintWriter pr = new PrintWriter(System.out); +int tc = 1; +tc= cinI(); +while(tc-->0){ +int n =cinI(); +String[] a= new String[n]; + +int[][] f =new int[10][n]; + +for(int i=0;i=0;i--){ +sum+=f[j][i]; +if(sum>0){ +cnt+=1; +} +else{ +break; +} +} +max=max(max,cnt); +} +System.out.println(max); +} + +} + +public static void print(String var ,E e){ +if(debug==true){ +System.out.println(var +"" ""+e); +} +} +private static long[] sort(long[] e){ +ArrayList x=new ArrayList<>(); +for(long c:e){ +x.add(c); +} +Collections.sort(x); +long[] y = new long[e.length]; +for(int i=0;i b) +return gcd(a % b, b); +return gcd(a, b % a); +} + + +public static long min(long a, long b) { +return Math.min(a, b); +} + +public static int min(int a, int b) { +return Math.min(a, b); +} + + +public static void sieve() { +int[] pf = new int[100000000 + 1]; +//0 prime //1 not prime +pf[0] = 1; +pf[1] = 1; +for (int j = 2; j <= 10000; j++) { + +if (pf[j] == 0) { +p.add(j); +for (int k = j * j; k < pf.length; k += j) { +pf[k] = 1; +} +} +} + +} + + +public static int[] readArray(int n, int x, int z) throws Exception { +int[] arr = new int[n]; +String[] ar = cinA(); +for (int i = x; i < n + x; i++) { +arr[i] = getI(ar[i - x]); +} +return arr; +} + +public static long[] readArray(int n, int x) throws Exception { +long[] arr = new long[n]; +String[] ar = cinA(); +for (int i = x; i < n + x; i++) { +arr[i] = getL(ar[i - x]); +} +return arr; +} + +public static void arrinit(String[] a, long[] b) throws Exception { +for (int i = 0; i < a.length; i++) { +b[i] = Long.parseLong(a[i]); +} +} + +public static HashSet[] Graph(int n, int edge, int directed) throws Exception { +HashSet[] tree = new HashSet[n]; + +for (int j = 0; j < edge; j++) { + +String[] uv = cinA(); +int u = getI(uv[0]); +int v = getI(uv[1]); +if (directed == 0) { + +tree[v].add(u); +} +tree[u].add(v); +} +return tree; +} + +public static void arrinit(String[] a, int[] b) throws Exception { +for (int i = 0; i < a.length; i++) { +b[i] = Integer.parseInt(a[i]); +} +} + + +static double findRoots(int a, int b, int c) { +// If a is 0, then equation is not +//quadratic, but linear + + +int d = b * b - 4 * a * c; +double sqrt_val = Math.sqrt(Math.abs(d)); + + +// System.out.println(""Roots are real and different \n""); + +return Math.max((double) (-b + sqrt_val) / (2 * a), +(double) (-b - sqrt_val) / (2 * a)); + + +} + +public static String cin() throws Exception { +return br.readLine(); +} + +public static String[] cinA() throws Exception { +return br.readLine().split("" ""); +} + +public static String[] cinA(int x) throws Exception { +return br.readLine().split(""""); +} + +public static String ToString(Long x) { +return Long.toBinaryString(x); +} + +public static void cout(String s) { +System.out.println(s); +} + +public static Integer cinI() throws Exception { +return Integer.parseInt(br.readLine()); +} + +public static int getI(String s) throws Exception { +return Integer.parseInt(s); +} + +public static long getL(String s) throws Exception { +return Long.parseLong(s); +} + +public static long max(long a, long b) { +return Math.max(a, b); +} + +public static int max(int a, int b) { +return Math.max(a, b); +} + +public static void coutI(int x) { +System.out.println(String.valueOf(x)); +} + +public static void coutI(long x) { +System.out.println(String.valueOf(x)); +} + +public static Long cinL() throws Exception { +return Long.parseLong(br.readLine()); +} + +public static void arrInit(String[] arr, int[] arr1) throws Exception { +for (int i = 0; i < arr.length; i++) { +arr1[i] = getI(arr[i]); +} + +} +}","import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.*; + +public class Practice { + static HashMap map = new HashMap<>(); + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while (t-->0) { + int n = sc.nextInt(); + int[][] occurances = new int[5][n]; + for(int i=0;i=0;j--){ + tmpSum+=occurances[i][j]; + if(tmpSum>0) tmpAns++; + else break; + } + ans = Math.max(ans, tmpAns); + } + System.out.println(ans); + } + } +} + +",1,Plagiarised +2b8f032c,a195911e,"import java.io.*; +import java.util.*; + +public class Main { + + static int INF = (int)1e9 + 1; + + public static void main(String[] args) { + FastScanner fs=new FastScanner(); + PrintWriter out = new PrintWriter(System.out); + + /****** CODE STARTS HERE *****/ + //-------------------------------------------------------------------------------------------------------- + int t = fs.nextInt(); + w:while(t-->0) { + int n = fs.nextInt(); + int[] k = fs.readArray(n); + int[] h = fs.readArray(n); + long ans = 0; + int mtn = INF, prev = k[n-1]; + + for(int j=n-1; j>=0; j--) { + if(mtn!=INF && mtn > k[j]) { + int x = prev-mtn+1; + ans += ((long)x*(x+1))/2; + mtn = INF; + prev = k[j]; + } + if(mtn >= k[j]-h[j]+1) + mtn = k[j]-h[j]+1; + + if(j==0) { + int x = prev-mtn+1; + ans += ((long)x*(x+1))/2; + } + } + out.println(ans); + } + out.close(); + } + + //****** CODE ENDS HERE ***** + //---------------------------------------------------------------------------------------------------------------- + + static void sort(int[] a) { + ArrayList l=new ArrayList<>(); + for (int i:a) l.add(i); + Collections.sort(l); + for (int i=0; i map; + //static StringBuffer sb=new StringBuffer(""""); + //static HashMap map; + static PrintWriter out=new PrintWriter(System.out); + public static void main(String[] args) + { + // StringBuffer sb=new StringBuffer(""""); + int ttt=1; + ttt =i(); + outer :while (ttt-- > 0) + { + int n=i(); + long A[]=inputL(n); + long B[]=inputL(n); + long C[]=new long[n]; + for(int i=0;i=0;i--) { + if(C[i]>min) { + continue; + } + if(A[i] + { + int x; + int y; + int z; + Pair(int x,int y){ + this.x=x; + this.y=y; +// this.z=z; + + } + @Override + public int compareTo(Pair o) { + if(this.x>o.x) + return 1; + else if(this.xo.y) + return 1; + else if(this.y o.x) { +// return 1; +// } +// if (x < o.x) { +// return -1; +// } +// if (y > o.y) { +// return 1; +// } +// if (y < o.y) { +// return -1; +// } +// return 0; +// } + + } + +static int find(int A[],int a) { + if(A[a]==a) + return a; + return A[a]=find(A, A[a]); +} +//static int find(int A[],int a) { +// if(A[a]==a) +// return a; +// return find(A, A[a]); +//} +//FENWICK TREE +static void update(int i, int x){ + for(; i < bit.length; i += (i&-i)) + bit[i] += x; +} + +static int sum(int i){ + int ans = 0; + for(; i > 0; i -= (i&-i)) + ans += bit[i]; + return ans; +} +//END +static void add(int v) { + if(!map.containsKey(v)) { + map.put(v, 1); + } + else { + map.put(v, map.get(v)+1); + } +} +static void remove(int v) { + if(map.containsKey(v)) { + map.put(v, map.get(v)-1); + if(map.get(v)==0) + map.remove(v); + } +} +public static int upper(int A[],int k,int si,int ei) +{ + int l=si; + int u=ei; + int ans=-1; + while(l<=u) { + int mid=(l+u)/2; + if(A[mid]<=k) { + ans=mid; + l=mid+1; + } + else { + u=mid-1; + } + } + return ans; +} +public static int lower(int A[],int k,int si,int ei) +{ + int l=si; + int u=ei; + int ans=-1; + while(l<=u) { + int mid=(l+u)/2; + if(A[mid]<=k) { + l=mid+1; + } + else { + ans=mid; + u=mid-1; + } + } + return ans; + + +} +static int[] copy(int A[]) { + int B[]=new int[A.length]; + for(int i=0;i> 1; + n |= n >> 2; + n |= n >> 4; + n |= n >> 8; + n |= n >> 16; + n++; + + return n; +} +static int highestPowerof2(int x) +{ + + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + + return x ^ (x >> 1); + +} +static long highestPowerof2(long x) +{ + + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + return x ^ (x >> 1); + +} + +static int max(int A[]) { + int max=Integer.MIN_VALUE; + for(int i=0;i=0;i--) + p[i]=p[i+1]+A[i]; + return p; +} +static long [] suffix(int A[]) { + long p[]=new long[A.length]; + p[A.length-1]=A[A.length-1]; + for(int i=A.length-2;i>=0;i--) + p[i]=p[i+1]+A[i]; + return p; +} +static void fill(int dp[]) { + Arrays.fill(dp, -1); +} +static void fill(int dp[][]) { + for(int i=0;i 0) { + + if (y % 2 == 1) + res = (res * x) % p; + + y = y >> 1; + x = (x * x) % p; + } + + return res; +} +static long power(long x, long y) +{ + + if(y==0) + return 1; + if(x==0) + return 0; + long res = 1; + + while (y > 0) { + + if (y % 2 == 1) + res = (res * x); + + y = y >> 1; + x = (x * x); + } + + return res; +} +static void print(int A[]) { + for(int i : A) { + out.print(i+"" ""); + } + out.println(); +} +static void print(long A[]) { + for(long i : A) { + System.out.print(i+"" ""); + } + System.out.println(); +} +static long mod(long x) { + return ((x%mod + mod)%mod); +} +static String reverse(String s) { + StringBuffer p=new StringBuffer(s); + p.reverse(); + return p.toString(); +} + + static int i() { + return sc.nextInt(); + } + static String s() { + return sc.next(); + } + static long l() { + return sc.nextLong(); + } + static void sort(int[] A){ + int n = A.length; + Random rnd = new Random(); + for(int i=0; i hash(int A[]){ + HashMap map=new HashMap(); + for(int i : A) { + if(map.containsKey(i)) { + map.put(i, map.get(i)+1); + } + else { + map.put(i, 1); + } + } + return map; + } + static HashMap hash(long A[]){ + HashMap map=new HashMap(); + for(long i : A) { + if(map.containsKey(i)) { + map.put(i, map.get(i)+1); + } + else { + map.put(i, 1); + } + } + return map; + } + static TreeMap tree(int A[]){ + TreeMap map=new TreeMap(); + for(int i : A) { + if(map.containsKey(i)) { + map.put(i, map.get(i)+1); + } + else { + map.put(i, 1); + } + } + return map; + } + static TreeMap tree(long A[]){ + TreeMap map=new TreeMap(); + for(long i : A) { + if(map.containsKey(i)) { + map.put(i, map.get(i)+1); + } + else { + map.put(i, 1); + } + } + return map; + } + static boolean prime(int n) + { + if (n <= 1) + return false; + if (n <= 3) + return true; + if (n % 2 == 0 || n % 3 == 0) + return false; + double sq=Math.sqrt(n); + + for (int i = 5; i <= sq; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + return true; + } + static boolean prime(long n) + { + if (n <= 1) + return false; + if (n <= 3) + return true; + if (n % 2 == 0 || n % 3 == 0) + return false; + double sq=Math.sqrt(n); + + for (int i = 5; i <= sq; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + return true; + } + static int gcd(int a, int b) + { + if (a == 0) + return b; + return gcd(b % a, a); + } + static long gcd(long a, long b) + { + if (a == 0) + return b; + return gcd(b % a, a); + } + + + static class FastReader + { + BufferedReader br; + StringTokenizer st; + + public FastReader() + { + br = new BufferedReader(new + InputStreamReader(System.in)); + } + + String next() + { + while (st == null || !st.hasMoreElements()) + { + try + { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() + { + return Integer.parseInt(next()); + } + + long nextLong() + { + return Long.parseLong(next()); + } + + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str = """"; + try + { + str = br.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + return str; + } + } +} + + + +",0,Non-plagiarised +2c865582,2ef4c176,"import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; +import java.util.StringTokenizer; + +public class Main { + static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader() + { + br = new BufferedReader( + new InputStreamReader(System.in)); + } + + String next() + { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { return Integer.parseInt(next()); } + + long nextLong() { return Long.parseLong(next()); } + + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str = """"; + try { + str = br.readLine(); + } + catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } + + + static long dfs(int cur,int parent,int taken,HashMap> hm,ArrayList al,long[][] dp){ + long ans=0; + if(dp[cur-1][taken]!=-1) return dp[cur-1][taken]; + int v=taken==0?al.get(cur-1).l:al.get(cur-1).r; + for(node x:hm.get(cur)){ + int xl=x.l,xr=x.r,idx=x.idx; + if(idx!=parent){ + ans+=Math.max(Math.abs(v-xl)+dfs(idx,cur,0,hm,al,dp),Math.abs(v-xr)+dfs(idx,cur,1,hm,al,dp)); + } + } + return dp[cur-1][taken]=ans; + } + public static void main(String[] args) { + FastReader obj = new FastReader(); + int t = obj.nextInt(); + while(t-->0) { + int n = obj.nextInt(),a,b; + ArrayList al=new ArrayList<>(); + for(int i=0;i> hm=new HashMap<>(); + for(int i=0;i()); + } + if(!hm.containsKey(b)){ + hm.put(b,new ArrayList()); + } + hm.get(a).add(al.get(b-1)); + hm.get(b).add(al.get(a-1)); + } + long[][] dp=new long[n+2][2]; + for(long[] x:dp) Arrays.fill(x,-1); + long ans1,ans2; + ans1=dfs(1,0,0,hm,al,dp); + ans2=dfs(1,0,1,hm,al,dp); + System.out.println(Math.max(ans1,ans2)); + } + } +} + +class node{ + int l,r,idx; + node(int l1,int r1,int i){ + l=l1; + r=r1; + idx=i; + } +}","import java.io.*; +import java.util.*; + +public class Template { + + static int mod = 1000000007; + + public static void main(String[] args) { + FastScanner sc = new FastScanner(); + PrintWriter out = new PrintWriter(System.out); + int yo = sc.nextInt(); + while (yo-- > 0) { + int n = sc.nextInt(); + + Map map = new HashMap<>(); + + for(int i = 0; i < n; i++) { + map.put(i, new Pair(sc.nextLong(),sc.nextLong())); + } + + List> list = new ArrayList<>(); + for(int i = 0; i < n; i++) { + list.add(new ArrayList<>()); + } + for(int i = 0; i < n-1; i++) { + int x = sc.nextInt()-1; + int y = sc.nextInt()-1; + list.get(x).add(y); + list.get(y).add(x); + } + + // l -> 0, r -> 1 + + for(int i = 0; i < 1e5+3; i++) { + for(int j = 0; j < 2; j++) { + dp[i][j] = -1; + } + } + long a1 = dfs(map,list,0,0,-1); + long a2 = dfs(map,list,1,0,-1); + long ans = Math.max(a1, a2); + out.println(ans); + } + out.close(); + } + +// static Map dp = new HashMap<>(); + static long[][] dp = new long[(int)1e5+10][2]; + private static long dfs(Map map, List> list, + int x, int node, int parent) { + + + if(dp[node][x] != -1) { + return dp[node][x]; + } + + List neighbours = list.get(node); + + long ans1 = 0; + if(x == 0) { + ans1 = map.get(node).x; + } + else { + ans1 = map.get(node).y; + } + + + long uAns = 0; + for(int e : neighbours) { + if(e == parent) continue; + long ua1 = dfs(map,list,0,e,node); + long ua2 = dfs(map,list,1,e,node); + // consider 0 + long a1 = ua1 + Math.abs(map.get(e).x-ans1); + // consider 1 + long a2 = ua2 + Math.abs(map.get(e).y-ans1); + uAns += Math.max(a1, a2); + } + + return dp[node][x] = uAns; + } + + static class Pair { + long x; + long y; + + public Pair(long x, long y) { + this.x = x; + this.y = y; + } + } + + static void ruffleSort(int[] a) { + + int n = a.length; + Random r = new Random(); + for (int i = 0; i < a.length; i++) { + int oi = r.nextInt(n), temp = a[i]; + a[i] = a[oi]; + a[oi] = temp; + } + Arrays.sort(a); + } + + static long gcd(long a, long b) { + if (b == 0) + return a; + return gcd(b, a % b); + } + + static boolean[] sieve(int N) { + boolean[] sieve = new boolean[N]; + for (int i = 2; i < N; i++) + sieve[i] = true; + + for (int i = 2; i < N; i++) { + if (sieve[i]) { + if (i * i < 0) + continue; + for (int j = i * i; j < N; j += i) { + sieve[j] = false; + } + } + } + return sieve; + } + + static long pow(int a, long b) { + if (b == 0) { + return 1; + } + if (b == 1) { + return a; + } + if (b % 2 == 0) { + long ans = pow(a, b / 2); + return ans * ans; + } else { + long ans = pow(a, (b - 1) / 2); + return a * ans * ans; + } + + } + + static class FastScanner { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(""""); + + String next() { + while (!st.hasMoreTokens()) + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + int[] readArray(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) + a[i] = nextInt(); + return a; + } + + long nextLong() { + return Long.parseLong(next()); + } + } + + // For Input.txt and Output.txt + // FileInputStream in = new FileInputStream(""input.txt""); + // FileOutputStream out = new FileOutputStream(""output.txt""); + // PrintWriter pw = new PrintWriter(out); + // Scanner sc = new Scanner(in); +} +",0,Non-plagiarised +2e404969,d76e3b9d,"import java.util.*; +import java.lang.*; +import java.io.*; + +public class Codechef { + static long fans[] = new long[200001]; + static long inv[] = new long[200001]; + static long mod = 1000000007; + + static void init() { + fans[0] = 1; + inv[0] = 1; + fans[1] = 1; + inv[1] = 1; + for (int i = 2; i < 200001; i++) { + fans[i] = ((long) i * fans[i - 1]) % mod; + inv[i] = power(fans[i], mod - 2); + } + } + + static long ncr(int n, int r) { + return (((fans[n] * inv[n - r]) % mod) * (inv[r])) % mod; + } + + public static void main(String[] args) throws java.lang.Exception { + FastReader in = new FastReader(System.in); + StringBuilder sb = new StringBuilder(); + int t = 1; + t = in.nextInt(); + while (t > 0) { + --t; + int n = in.nextInt(); + long time[] = generateArray(in, n); + long hp[] = generateArray(in, n); + int s = 0; + long ans = 0; + while(s=l && time[s]<=r) + { + ++s; + } + long temp = r - l; + ans += (temp*(temp+1))/2; + } + sb.append(ans+""\n""); + + } + System.out.print(sb); + } + + static long power(long x, long y) { + long res = 1; // Initialize result + + while (y > 0) { + + // If y is odd, multiply x with result + if ((y & 1) != 0) + res = ((res % mod) * (x % mod)) % mod; + + // y must be even now + y = y >> 1; // y = y/2 + x = ((x % mod) * (x % mod)) % mod; // Change x to x^2 + } + return res; + } + + static long[] generateArray(FastReader in, int n) throws IOException { + long arr[] = new long[n]; + for (int i = 0; i < n; i++) + arr[i] = in.nextLong(); + return arr; + } + + static long[][] generatematrix(FastReader in, int n, int m) throws IOException { + long arr[][] = new long[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + arr[i][j] = in.nextLong(); + } + } + return arr; + } + + static long gcd(long a, long b) { + if (a == 0) + return b; + else + return gcd(b % a, a); + } + + static long lcm(long a, long b) { + return (a / gcd(a, b)) * b; + } + + static void sort(long[] a) { + ArrayList l = new ArrayList<>(); + for (long i : a) + l.add(i); + Collections.sort(l); + for (int i = 0; i < a.length; i++) + a[i] = l.get(i); + } +} + +class FastReader { + + byte[] buf = new byte[2048]; + int index, total; + InputStream in; + + FastReader(InputStream is) { + in = is; + } + + int scan() throws IOException { + if (index >= total) { + index = 0; + total = in.read(buf); + if (total <= 0) { + return -1; + } + } + return buf[index++]; + } + + String next() throws IOException { + int c; + for (c = scan(); c <= 32; c = scan()) + ; + StringBuilder sb = new StringBuilder(); + for (; c > 32; c = scan()) { + sb.append((char) c); + } + return sb.toString(); + } + + String nextLine() throws IOException { + int c; + for (c = scan(); c <= 32; c = scan()) + ; + StringBuilder sb = new StringBuilder(); + for (; c != 10 && c != 13; c = scan()) { + sb.append((char) c); + } + return sb.toString(); + } + + char nextChar() throws IOException { + int c; + for (c = scan(); c <= 32; c = scan()) + ; + return (char) c; + } + + int nextInt() throws IOException { + int c, val = 0; + for (c = scan(); c <= 32; c = scan()) + ; + boolean neg = c == '-'; + if (c == '-' || c == '+') { + c = scan(); + } + for (; c >= '0' && c <= '9'; c = scan()) { + val = (val << 3) + (val << 1) + (c & 15); + } + return neg ? -val : val; + } + + long nextLong() throws IOException { + int c; + long val = 0; + for (c = scan(); c <= 32; c = scan()) + ; + boolean neg = c == '-'; + if (c == '-' || c == '+') { + c = scan(); + } + for (; c >= '0' && c <= '9'; c = scan()) { + val = (val << 3) + (val << 1) + (c & 15); + } + return neg ? -val : val; + } +}","import java.io.*; +import java.util.*; + +public class c { + public static void main(String[] args) throws Exception { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + PrintWriter out = new PrintWriter(System.out); + int numcases = Integer.parseInt(in.readLine()); + for(int casenum = 0; casenum < numcases; casenum++){ + int n = Integer.parseInt(in.readLine()); + long[] t = new long[n]; + long[] h = new long[n]; + StringTokenizer tokenizer = new StringTokenizer(in.readLine()); + for(int i = 0; i < n; i++){ + t[i] = Integer.parseInt(tokenizer.nextToken()); + } + tokenizer = new StringTokenizer(in.readLine()); + for(int i = 0; i < n; i++){ + h[i] = Integer.parseInt(tokenizer.nextToken()); + } + long mana = 0; + int index = 0; + while(index < n){ + long start = t[index] - h[index]; + long end = t[index]; + for(int i = index+1; i < n; i++){ + if(t[i] - h[i] < start){ + start = t[i] - h[i]; + end = t[i]; + index = i; + } + else if(t[i] - end < h[i]){ + end = t[i]; + index = i; + } + } + mana += (end - start + 1) * (end - start) / 2; + index++; + } + System.out.println(mana); + } + in.close(); + out.close(); + } +}",0,Non-plagiarised +9debf95c,ac596a43,"// A Computer is Like a mischievous genie. +// It will give you exactly what you ask for, +// but not always what you want +// A code by Rahul Verma + + +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.PriorityQueue; +import java.util.Queue; +import java.util.Stack; +import java.util.StringTokenizer; +import java.util.TreeMap; + + +public class Main { + + + static class Clock { + + protected long start, stop; + + public void start() { + start = System.currentTimeMillis(); + } + + public void stop() { + stop = System.currentTimeMillis(); + } + + public String getTime() { + return ((stop - start) + "" ms""); + } + } + + + public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); + + static class FastReader { + + BufferedReader br; + StringTokenizer st; + + public FastReader() { + br = new BufferedReader(new + InputStreamReader(System.in)); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String[] nextSArray() { + String sr[] = null; + try { + sr = br.readLine().trim().split("" ""); + } catch (IOException e) { + e.printStackTrace(); + } + return sr; + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + + return str; + } + } + + + static long powmodulo(long a, long p) { + if (p == 0) { + return 1 % mod; + } + if (p == 1) { + return a % mod; + } + long ans = 1; + while (p > 0) { + if ((p & 1) > 0) { + ans = (ans * a) % mod; + } + a = (a * a) % mod; + p = p >> 1; + } + return ans % mod; + } + + + static long mod = 1000000007; + + static long gcd(long a, long b) { + if (a == 0) { + return b; + } + return gcd(b % a, a); + } + + static long fast_powerNumbers(long a, long n) { + if (n == 1) { + return a; + } + long ans = fast_powerNumbers(a, n / 2); + if (n % 2 == 0) { + return (ans * ans); + } else { + return ((ans * ans) * (a)); + } + } + + + static void dfs_helper(int[][] arr, int i, int j, int team, int n, int m) { + arr[i][j] = team; + if (i - 1 >= 0 && arr[i - 1][j] == 1) { + dfs(arr, i - 1, j, team, n, m); + } + if (j - 1 >= 0 && arr[i][j - 1] == 1) { + dfs(arr, i, j - 1, team, n, m); + } + if (i + 1 < n && arr[i + 1][j] == 1) { + dfs(arr, i + 1, j, team, n, m); + } + if (j + 1 < m && arr[i][j + 1] == 1) { + dfs(arr, i, j + 1, team, n, m); + } + + } + + static void dfs(int[][] arr, int i, int j, int team, int n, int m) { + dfs_helper(arr, i, j, team, n, m); + + } + + static int parent[]; + static int rank[]; + + static int find(int i) { + if (parent[i] == -1) { + parent[i] = i; + return i; + } + + if (parent[i] == i) { + return i; + } else { + parent[i] = find(parent[i]); + } + return parent[i]; + + } + + static void unite(int s1, int s2) { + + if (rank[s1] > rank[s2]) { + parent[s2] = s1; + rank[s1] += rank[s2]; + } else { + parent[s1] = s2; + rank[s2] += rank[s1]; + } + } + + + public static long arr[]; + public static int arr1[]; + +// static void seive(int n) { +// arr = new int[n + 1]; +// arr[0] = arr[1] = 1; +// for (int i = 4; i <= n; i = i + 2) { +// arr[i] = 1; +// } +// for (int i = 3; i * i <= n; i = i + 2) { +// if (arr[i] == 0) { +// for (int j = i * i; j <= n; j = j + i) { +// arr[j] = 1; +// } +// +// } +// } +// } + + static void seive(int n) + { + arr1=new int[n+1]; + arr1[0]=arr1[1]=1; + for (int i = 4; i <=n ; i+=2) { + arr1[i]=1; + } + for (int i = 3; i*i <=n ; i+=2) { + if (arr1[i] == 0) + { + for (int j = i*i; j <=n ; j+=i) { + arr1[j]=1; + } + } + } + + } + + public static boolean ccw(Point a,Point b,Point c) + { + long s1=(c.x-b.x)*(b.y-a.y) ; + long s2 = (c.y-b.y)*(b.x-a.x); + if(s1rank[p2]) + { + parent[p2]=p1; + rank[p1]+=rank[p2]; + // System.out.println(arr[p2]); + + sum[p1]+=sum[p2]; + //sum[p2]+=sum[p1]; + + } + else + { + parent[p1]=p2; + rank[p2]+=rank[p1]; + //System.out.println(arr[p1]); + sum[p2]+=sum[p1]; + //sum[p1]+=sum[p2]; + } + } + + +} + +class Gaph { + + HashMap> hm; + + Gaph() { + hm = new HashMap<>(); + } + + Gaph(int n) { + + hm = new HashMap<>(); + for (int i = 0; i < n; i++) { + hm.put(i, new ArrayList()); + } + } + + // function for adding an edge................................................. + public void addEdge(int a, int b, boolean isDir) { + if (isDir) { + if (hm.containsKey(a)) { + hm.get(a).add(b); + } else { + hm.put(a, new ArrayList<>(Arrays.asList(b))); + } + } else { + if (hm.containsKey(a)) { + hm.get(a).add(b); + } else if (!hm.containsKey(a)) { + hm.put(a, new ArrayList<>(Arrays.asList(b))); + } + + if (hm.containsKey(b)) { + hm.get(b).add(a); + } else if (!hm.containsKey(b)) { + hm.put(b, new ArrayList<>(Arrays.asList(a))); + } + } + } + + +} + +// out.println(al.toString().replaceAll(""[\\[|\\]|,]"","""")); + + +","// A Computer is Like a mischievous genie. +// It will give you exactly what you ask for, +// but not always what you want +// A code by Rahul Verma + + +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.PriorityQueue; +import java.util.Queue; +import java.util.Stack; +import java.util.StringTokenizer; +import java.util.TreeMap; + + +public class Main { + + + static class Clock { + + protected long start, stop; + + public void start() { + start = System.currentTimeMillis(); + } + + public void stop() { + stop = System.currentTimeMillis(); + } + + public String getTime() { + return ((stop - start) + "" ms""); + } + } + + + public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); + + static class FastReader { + + BufferedReader br; + StringTokenizer st; + + public FastReader() { + br = new BufferedReader(new + InputStreamReader(System.in)); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String[] nextSArray() { + String sr[] = null; + try { + sr = br.readLine().trim().split("" ""); + } catch (IOException e) { + e.printStackTrace(); + } + return sr; + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + + return str; + } + } + + + static long powmodulo(long a, long p) { + if (p == 0) { + return 1 % mod; + } + if (p == 1) { + return a % mod; + } + long ans = 1; + while (p > 0) { + if ((p & 1) > 0) { + ans = (ans * a) % mod; + } + a = (a * a) % mod; + p = p >> 1; + } + return ans % mod; + } + + + static long mod = 1000000007; + + static long gcd(long a, long b) { + if (a == 0) { + return b; + } + return gcd(b % a, a); + } + + static long fast_powerNumbers(long a, long n) { + if (n == 1) { + return a; + } + long ans = fast_powerNumbers(a, n / 2); + if (n % 2 == 0) { + return (ans * ans); + } else { + return ((ans * ans) * (a)); + } + } + + + static void dfs_helper(int[][] arr, int i, int j, int team, int n, int m) { + arr[i][j] = team; + if (i - 1 >= 0 && arr[i - 1][j] == 1) { + dfs(arr, i - 1, j, team, n, m); + } + if (j - 1 >= 0 && arr[i][j - 1] == 1) { + dfs(arr, i, j - 1, team, n, m); + } + if (i + 1 < n && arr[i + 1][j] == 1) { + dfs(arr, i + 1, j, team, n, m); + } + if (j + 1 < m && arr[i][j + 1] == 1) { + dfs(arr, i, j + 1, team, n, m); + } + + } + + static void dfs(int[][] arr, int i, int j, int team, int n, int m) { + dfs_helper(arr, i, j, team, n, m); + + } + + static int parent[]; + static int rank[]; + + static int find(int i) { + if (parent[i] == -1) { + parent[i] = i; + return i; + } + + if (parent[i] == i) { + return i; + } else { + parent[i] = find(parent[i]); + } + return parent[i]; + + } + + static void unite(int s1, int s2) { + + if (rank[s1] > rank[s2]) { + parent[s2] = s1; + rank[s1] += rank[s2]; + } else { + parent[s1] = s2; + rank[s2] += rank[s1]; + } + } + + + public static long arr[]; + public static int arr1[]; + +// static void seive(int n) { +// arr = new int[n + 1]; +// arr[0] = arr[1] = 1; +// for (int i = 4; i <= n; i = i + 2) { +// arr[i] = 1; +// } +// for (int i = 3; i * i <= n; i = i + 2) { +// if (arr[i] == 0) { +// for (int j = i * i; j <= n; j = j + i) { +// arr[j] = 1; +// } +// +// } +// } +// } + + static void seive(int n) + { + arr1=new int[n+1]; + arr1[0]=arr1[1]=1; + for (int i = 4; i <=n ; i+=2) { + arr1[i]=1; + } + for (int i = 3; i*i <=n ; i+=2) { + if (arr1[i] == 0) + { + for (int j = i*i; j <=n ; j+=i) { + arr1[j]=1; + } + } + } + + } + + public static boolean ccw(Point a,Point b,Point c) + { + long s1=(c.x-b.x)*(b.y-a.y) ; + long s2 = (c.y-b.y)*(b.x-a.x); + if(s1rank[p2]) + { + parent[p2]=p1; + rank[p1]+=rank[p2]; + // System.out.println(arr[p2]); + + sum[p1]+=sum[p2]; + //sum[p2]+=sum[p1]; + + } + else + { + parent[p1]=p2; + rank[p2]+=rank[p1]; + //System.out.println(arr[p1]); + sum[p2]+=sum[p1]; + //sum[p1]+=sum[p2]; + } + } + + +} + +class Gaph { + + HashMap> hm; + + Gaph() { + hm = new HashMap<>(); + } + + Gaph(int n) { + + hm = new HashMap<>(); + for (int i = 0; i < n; i++) { + hm.put(i, new ArrayList()); + } + } + + // function for adding an edge................................................. + public void addEdge(int a, int b, boolean isDir) { + if (isDir) { + if (hm.containsKey(a)) { + hm.get(a).add(b); + } else { + hm.put(a, new ArrayList<>(Arrays.asList(b))); + } + } else { + if (hm.containsKey(a)) { + hm.get(a).add(b); + } else if (!hm.containsKey(a)) { + hm.put(a, new ArrayList<>(Arrays.asList(b))); + } + + if (hm.containsKey(b)) { + hm.get(b).add(a); + } else if (!hm.containsKey(b)) { + hm.put(b, new ArrayList<>(Arrays.asList(a))); + } + } + } + + +} + +// out.println(al.toString().replaceAll(""[\\[|\\]|,]"","""")); +",1,Plagiarised +b434c275,fdc4f384,"import java.io.*; +import java.util.*; + + + +import java.math.*; +import java.math.BigInteger; + + +public final class A +{ + static PrintWriter out = new PrintWriter(System.out); + static StringBuilder ans=new StringBuilder(); + static FastReader in=new FastReader(); + static ArrayList g[]; + static long mod=(long)1e9+7,INF=Long.MAX_VALUE; + static boolean set[],col[]; + static int par[],tot[],partial[]; + static int D[],P[][]; + static long dp[][],sum=0,max=0,size[]; + // static node1 seg[]; + //static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)}; + public static void main(String args[])throws IOException + { + + + + int T=i(); + outer:while(T-->0) + { + int N=i(); + int size[]=new int[N]; + PriorityQueue q[]=new PriorityQueue[26]; + for(int i=0; i<26; i++)q[i]=new PriorityQueue(); + for(int i=0; i q_new=new PriorityQueue<>(); + q_new=q[i]; + int c=0; + long f=0; + while(q_new.size()>0) + { + node1 x=q_new.remove(); +// System.out.println(x.f+"" ""+x.size+"" ""+x.a); + f+=x.a; + if(f>0) + { + c++; + max=Math.max(max, c); + } + else break; + } + } + out.println(max); + } + out.close(); + + } + static int OR(int i,int j) + { + if(i>j) + { + int t=i; + i=j; + j=t; + } + System.out.println(""OR ""+i+"" ""+j); + return i(); + } + static int AND(int i,int j) + { + if(i>j) + { + int t=i; + i=j; + j=t; + } + System.out.println(""AND ""+i+"" ""+j); + return i(); + } + static int XOR(int i,int j) + { + if(i>j) + { + int t=i; + i=j; + j=t; + } + System.out.println(""XOR ""+i+"" ""+j); + return i(); + } + + static boolean f1(int l,char X[]) + { + int i=0; + for(; l0; a++) + { + if(GCD(i,a)==1)return i; + } + return 0; + } + static int min(char X[],char str[],int N) + { + int s=0; + for(int i=0; i=b && a>=c)return 'R'; + if(b>=a && b>=c)return 'B'; + return 'G'; + } + + + static void f1(int n,int p,long sum,int N) + { + for(int c:g[n]) + { + if(c==p)continue; + + long s=sum+N-2*size[c]; + f1(c,n,s,N); + max=Math.max(max, s); + } + } + static long f(int i,int j,ArrayList A) + { + + if(i+1==A.size()) + { + return j; + } + int moves=1+A.get(i); + if(j==1)return 1+f(i+1,moves,A); + if(j>0 && dp[i][j-1]==0)f(i,j-1,A); + return dp[i][j]=dp[i][j-1]+f(i+1,j+moves,A); + } + // static void build(int v,int tl,int tr,long A[]) + // { + // if(tl==tr) + // { + // seg[v]=new node1(A[tl],A[tr],1,true); + // return ; + // } + // int tm=(tl+tr)/2; + // build(2*v,tl,tm,A); + // build(2*v+1,tm+1,tr,A); + // seg[v]=merge(seg[2*v],seg[2*v+1]); + // } + // static node1 ask(int v,int tl,int tr,int l,int r) + // { + // if(l>r)return new node1(0,0,0,false);//verify true or false + // if(tl==l && tr==r)return seg[v]; + // int tm=(tl+tr)/2; + // node1 a=ask(v*2,tl,tm,l,Math.min(tm, r)); + // node1 b=ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r); + // return merge(a,b); + // } + // static node1 merge(node1 a,node1 b) + // { + // long s=0; + // long l1=a.L,r1=a.R,c1=a.cnt; + // long l2=b.L,r2=b.R,c2=b.cnt; + // long g=GCD(l2,r1); s=c1+c2; + // if(g==1) + // { + // s--; + // g=(l2*r1)/g; + // if(c1==1) + // { + // l1=g; + // } + // if(c2==1)r2=g; + // return new node1(l1,r2,s,true); + // } + // return new node1(l1,r2,s,a.leaf^b.leaf); + // } + static long f(long l,long r,long a,long b,long N) + { + while(r-l>1) + { + long m=(l+r)/2; + long x=m*b; + if(N1) + { + long m=(l+r)/2; + if(x-m*s>max)l=m; + else r=m; + } + return l+1; + } + static int f(long A[],long x) + { + int l=-1,r=A.length; + while(r-l>1) + { + int m=(l+r)/2; + if(A[m]>=x)r=m; + else l=m; + } + return r; + } + + + + static int bin(int x,ArrayList A) + { + int l=0,r=A.size()-1; + while(l<=r) + { + int m=(l+r)/2; + int a=A.get(m); + if(a==x)return m; + if(a A) + { + int l=-1,r=A.size(); + while(r-l>1) + { + int m=(l+r)/2; + int a=A.get(m); + if(a<=x)l=m; + else r=m; + } + return l; + } + static int right(int x,ArrayList A) + { + int l=-1,r=A.size(); + while(r-l>1) + { + int m=(l+r)/2; + int a=A.get(m); + if(a Hash(int A[]) + { + HashMap mp=new HashMap<>(); + for(int a:A) + { + int f=mp.getOrDefault(a,0)+1; + mp.put(a, f); + } + return mp; + } + static long mul(long a, long b) + { + return ( a %mod * 1L * b%mod )%mod; + } + static void swap(int A[],int a,int b) + { + int t=A[a]; + A[a]=A[b]; + A[b]=t; + } + + + static int find(int a) + { + if(par[a]<0)return a; + return par[a]=find(par[a]); + } + static void union(int a,int b) + { + a=find(a); + b=find(b); + if(a!=b) + { + par[a]+=par[b]; + par[b]=a; + } + } + static boolean isSorted(int A[]) + { + for(int i=1; i high) + if (x >= A[high]) + return A[high]; + + int mid = (low + high) / 2; + + if (A[mid] == x) + return A[mid]; + + if (mid > 0 && A[mid - 1] <= x && x < A[mid]) + return A[mid - 1]; + + if (x < A[mid]) + return lower_Bound( A, low, mid - 1, x); + + return lower_Bound(A, mid + 1, high, x); + } + + static String f(String A) + { + String X=""""; + for(int i=A.length()-1; i>=0; i--) + { + int c=A.charAt(i)-'0'; + X+=(c+1)%2; + } + return X; + } + + static void sort(long[] a) //check for long + { + ArrayList l=new ArrayList<>(); + for (long i:a) l.add(i); + Collections.sort(l); + for (int i=0; i(); + D[i]=Integer.MAX_VALUE; + //D2[i]=INF; + } + } + + + + static long pow(long a,long b) + { + //long mod=1000000007; + long pow=1; + long x=a; + while(b!=0) + { + if((b&1)!=0)pow=(pow*x)%mod; + x=(x*x)%mod; + b/=2; + } + return pow; + } + + static long toggleBits(long x)//one's complement || Toggle bits + { + int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; + + return ((1< l=new ArrayList<>(); + for (int i:a) l.add(i); + Collections.sort(l); + for (int i=0; i A) + { + for(int a:A)System.out.print(a+"" ""); + System.out.println(); + } + + static int i() + { + return in.nextInt(); + } + + static long l() + { + return in.nextLong(); + } + + static int[] input(int N){ + int A[]=new int[N]; + for(int i=0; i +{ + int index,f,size; + long a; + node1(int f,int i,int size) + { + this.f=f; + this.index=i; + this.size=size; + a=2*f-size; + } + public int compareTo(node1 x) + { + if(this.a==x.a)return 0; + else if(this.a g[]; + static long mod=(long)1e9+7,INF=Long.MAX_VALUE; + static boolean set[],col[]; + static int par[],tot[],partial[]; + static int D[],P[][]; + static long dp[][],sum=0,max=0,size[]; + // static node1 seg[]; + //static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)}; + public static void main(String args[])throws IOException + { + + + + int T=i(); + outer:while(T-->0) + { + int N=i(); + int size[]=new int[N]; + PriorityQueue q[]=new PriorityQueue[6]; + for(int i=0; i<6; i++)q[i]=new PriorityQueue(); + for(int i=0; i q_new=new PriorityQueue<>(); + q_new=q[i]; + int c=0; + long f=0; + while(q_new.size()>0) + { + node1 x=q_new.remove(); +// System.out.println(x.f+"" ""+x.size+"" ""+x.a); + f+=x.a; + if(f>0) + { + c++; + max=Math.max(max, c); + } + else break; + } + } + out.println(max); + } + out.close(); + + } + static int OR(int i,int j) + { + if(i>j) + { + int t=i; + i=j; + j=t; + } + System.out.println(""OR ""+i+"" ""+j); + return i(); + } + static int AND(int i,int j) + { + if(i>j) + { + int t=i; + i=j; + j=t; + } + System.out.println(""AND ""+i+"" ""+j); + return i(); + } + static int XOR(int i,int j) + { + if(i>j) + { + int t=i; + i=j; + j=t; + } + System.out.println(""XOR ""+i+"" ""+j); + return i(); + } + + static boolean f1(int l,char X[]) + { + int i=0; + for(; l0; a++) + { + if(GCD(i,a)==1)return i; + } + return 0; + } + static int min(char X[],char str[],int N) + { + int s=0; + for(int i=0; i=b && a>=c)return 'R'; + if(b>=a && b>=c)return 'B'; + return 'G'; + } + + + static void f1(int n,int p,long sum,int N) + { + for(int c:g[n]) + { + if(c==p)continue; + + long s=sum+N-2*size[c]; + f1(c,n,s,N); + max=Math.max(max, s); + } + } + static long f(int i,int j,ArrayList A) + { + + if(i+1==A.size()) + { + return j; + } + int moves=1+A.get(i); + if(j==1)return 1+f(i+1,moves,A); + if(j>0 && dp[i][j-1]==0)f(i,j-1,A); + return dp[i][j]=dp[i][j-1]+f(i+1,j+moves,A); + } + // static void build(int v,int tl,int tr,long A[]) + // { + // if(tl==tr) + // { + // seg[v]=new node1(A[tl],A[tr],1,true); + // return ; + // } + // int tm=(tl+tr)/2; + // build(2*v,tl,tm,A); + // build(2*v+1,tm+1,tr,A); + // seg[v]=merge(seg[2*v],seg[2*v+1]); + // } + // static node1 ask(int v,int tl,int tr,int l,int r) + // { + // if(l>r)return new node1(0,0,0,false);//verify true or false + // if(tl==l && tr==r)return seg[v]; + // int tm=(tl+tr)/2; + // node1 a=ask(v*2,tl,tm,l,Math.min(tm, r)); + // node1 b=ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r); + // return merge(a,b); + // } + // static node1 merge(node1 a,node1 b) + // { + // long s=0; + // long l1=a.L,r1=a.R,c1=a.cnt; + // long l2=b.L,r2=b.R,c2=b.cnt; + // long g=GCD(l2,r1); s=c1+c2; + // if(g==1) + // { + // s--; + // g=(l2*r1)/g; + // if(c1==1) + // { + // l1=g; + // } + // if(c2==1)r2=g; + // return new node1(l1,r2,s,true); + // } + // return new node1(l1,r2,s,a.leaf^b.leaf); + // } + static long f(long l,long r,long a,long b,long N) + { + while(r-l>1) + { + long m=(l+r)/2; + long x=m*b; + if(N1) + { + long m=(l+r)/2; + if(x-m*s>max)l=m; + else r=m; + } + return l+1; + } + static int f(long A[],long x) + { + int l=-1,r=A.length; + while(r-l>1) + { + int m=(l+r)/2; + if(A[m]>=x)r=m; + else l=m; + } + return r; + } + + + + static int bin(int x,ArrayList A) + { + int l=0,r=A.size()-1; + while(l<=r) + { + int m=(l+r)/2; + int a=A.get(m); + if(a==x)return m; + if(a A) + { + int l=-1,r=A.size(); + while(r-l>1) + { + int m=(l+r)/2; + int a=A.get(m); + if(a<=x)l=m; + else r=m; + } + return l; + } + static int right(int x,ArrayList A) + { + int l=-1,r=A.size(); + while(r-l>1) + { + int m=(l+r)/2; + int a=A.get(m); + if(a Hash(int A[]) + { + HashMap mp=new HashMap<>(); + for(int a:A) + { + int f=mp.getOrDefault(a,0)+1; + mp.put(a, f); + } + return mp; + } + static long mul(long a, long b) + { + return ( a %mod * 1L * b%mod )%mod; + } + static void swap(int A[],int a,int b) + { + int t=A[a]; + A[a]=A[b]; + A[b]=t; + } + + + static int find(int a) + { + if(par[a]<0)return a; + return par[a]=find(par[a]); + } + static void union(int a,int b) + { + a=find(a); + b=find(b); + if(a!=b) + { + par[a]+=par[b]; + par[b]=a; + } + } + static boolean isSorted(int A[]) + { + for(int i=1; i high) + if (x >= A[high]) + return A[high]; + + int mid = (low + high) / 2; + + if (A[mid] == x) + return A[mid]; + + if (mid > 0 && A[mid - 1] <= x && x < A[mid]) + return A[mid - 1]; + + if (x < A[mid]) + return lower_Bound( A, low, mid - 1, x); + + return lower_Bound(A, mid + 1, high, x); + } + + static String f(String A) + { + String X=""""; + for(int i=A.length()-1; i>=0; i--) + { + int c=A.charAt(i)-'0'; + X+=(c+1)%2; + } + return X; + } + + static void sort(long[] a) //check for long + { + ArrayList l=new ArrayList<>(); + for (long i:a) l.add(i); + Collections.sort(l); + for (int i=0; i(); + D[i]=Integer.MAX_VALUE; + //D2[i]=INF; + } + } + + + + static long pow(long a,long b) + { + //long mod=1000000007; + long pow=1; + long x=a; + while(b!=0) + { + if((b&1)!=0)pow=(pow*x)%mod; + x=(x*x)%mod; + b/=2; + } + return pow; + } + + static long toggleBits(long x)//one's complement || Toggle bits + { + int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; + + return ((1< l=new ArrayList<>(); + for (int i:a) l.add(i); + Collections.sort(l); + for (int i=0; i A) + { + for(int a:A)System.out.print(a+"" ""); + System.out.println(); + } + + static int i() + { + return in.nextInt(); + } + + static long l() + { + return in.nextLong(); + } + + static int[] input(int N){ + int A[]=new int[N]; + for(int i=0; i +{ + int index,f,size; + long a; + node1(int f,int i,int size) + { + this.f=f; + this.index=i; + this.size=size; + a=2*f-size; + } + public int compareTo(node1 x) + { + if(this.a==x.a)return 0; + else if(this.a adj[] = new ArrayList[(int)1e5+7]; + static int diameter = 0; + static int[] depth = new int[(int)1e5 + 7]; + public static void main(String[] args) { + FastReader in = new FastReader(); + int t = in.nextInt(); + while(t-- > 0){ + int n = in.nextInt(); + int a = in.nextInt(), b = in.nextInt(), da = in.nextInt(), db = in.nextInt(); + for(int i = 1; i <= n; i++){ + adj[i] = new ArrayList<>(); + } + for(int i = 1; i <=n; i++){ + adj[i].clear(); + } + for(int i = 0; i < n- 1; i++){ + int u = in.nextInt(); + int v = in.nextInt(); + adj[u].add(v); + adj[v].add(u); + } + diameter = 0; + depth[a] = 0; + dfs(a, -1); + System.out.println(2 * da >= Math.min(diameter, db) || depth[b] <= da ? ""Alice"" : ""Bob""); + } + } + static int dfs(int node, int parent){ + int len = 0; + for(int x : adj[node]){ + if(x != parent){ + depth[x] = depth[node] + 1; + int cur = 1 + dfs(x, node); + diameter = Math.max(diameter, cur + len); + len = Math.max(len, cur); +// System.out.print(""x "" + x + "" node "" + node + "" par "" + parent); +// System.out.println("" cur "" + cur + "" len "" + len + "" diam "" + diameter); + } + } + return len; + } + static long getDigitSum(long n) + { + long sum = 0; + + while (n > 0) + { + sum = sum + n % 10; + n = n/10; + } + + return sum; + } + + static class FastReader + { + BufferedReader br; + StringTokenizer st; + + public FastReader() + { + br = new BufferedReader(new + InputStreamReader(System.in)); + } + + String next() + { + while (st == null || !st.hasMoreElements()) + { + try + { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() + { + return Integer.parseInt(next()); + } + + long nextLong() + { + return Long.parseLong(next()); + } + + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str = """"; + try + { + str = br.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + return str; + } + int[] readArray(int n){ + int[] a = new int[n]; + for(int i = 0; i < n; i++){ + a[i] = nextInt(); + } + return a; + } + } +} +","import static java.lang.Math.max; +import static java.lang.Math.min; +import static java.lang.Math.abs; +import static java.lang.System.out; +import java.util.*; +import java.io.*; +import java.math.*; + +public class Template { + + static int mod = 1000000007; + + public static void main(String[] args) { + FastScanner sc = new FastScanner(); + int yo = sc.nextInt(); + while (yo-- > 0) { + int n = sc.nextInt(); + int a = sc.nextInt()-1; + int b = sc.nextInt()-1; + int da = sc.nextInt(); + int db = sc.nextInt(); + + List> list = new ArrayList<>(); + for(int i = 0; i < n; i++) list.add(new ArrayList<>()); + + for(int i = 0; i < n-1; i++){ + int x = sc.nextInt()-1; + int y = sc.nextInt()-1; + list.get(x).add(y); + list.get(y).add(x); + } + + for(int i = 0; i <= n; i++) depth[i] = 0; + diam = 0; + dfs(a,-1,list); + + if(2 * da >= min(diam, db) || depth[b] <= da){ + out.println(""Alice""); + } + else { + out.println(""Bob""); + } + + } + } + + static int[] depth = new int[200001]; + static int diam = 0; + static int dfs(int x, int p, List> list) { + int len = 0; + List ne = list.get(x); + for(int y : ne) { + if(y != p) { + depth[y] = depth[x] + 1; + int cur = 1 + dfs(y, x,list); + diam = max(diam, cur + len); + len = max(len, cur); + } + } + return len; + } + + + public static class Pair { + int x; + int y; + + public Pair(int x, int y) { + this.x = x; + this.y = y; + } + } + + public static void sort(int[] arr) { + ArrayList ls = new ArrayList(); + for (int x : arr) + ls.add(x); + Collections.sort(ls); + for (int i = 0; i < arr.length; i++) + arr[i] = ls.get(i); + } + + public static long gcd(long a, long b) { + if (b == 0) + return a; + return gcd(b, a % b); + } + + static boolean[] sieve(int N) { + boolean[] sieve = new boolean[N + 1]; + for (int i = 2; i <= N; i++) + sieve[i] = true; + + for (int i = 2; i <= N; i++) { + if (sieve[i]) { + for (int j = 2 * i; j <= N; j += i) { + sieve[j] = false; + } + } + } + return sieve; + } + + public static long power(long x, long y, long p) { + long res = 1L; + x = x % p; + while (y > 0) { + if ((y & 1) == 1) + res = (res * x) % p; + y >>= 1; + x = (x * x) % p; + } + return res; + } + + public static void print(int[] arr) { + //for debugging only + for (int x : arr) + out.print(x + "" ""); + out.println(); + } + + static class FastScanner { + private int BS = 1 << 16; + private char NC = (char) 0; + private byte[] buf = new byte[BS]; + private int bId = 0, size = 0; + private char c = NC; + private double cnt = 1; + private BufferedInputStream in; + + public FastScanner() { + in = new BufferedInputStream(System.in, BS); + } + + public FastScanner(String s) { + try { + in = new BufferedInputStream(new FileInputStream(new File(s)), BS); + } catch (Exception e) { + in = new BufferedInputStream(System.in, BS); + } + } + + private char getChar() { + while (bId == size) { + try { + size = in.read(buf); + } catch (Exception e) { + return NC; + } + if (size == -1) + return NC; + bId = 0; + } + return (char) buf[bId++]; + } + + public int nextInt() { + return (int) nextLong(); + } + + public int[] readInts(int N) { + int[] res = new int[N]; + for (int i = 0; i < N; i++) { + res[i] = (int) nextLong(); + } + return res; + } + + public long[] readLongs(int N) { + long[] res = new long[N]; + for (int i = 0; i < N; i++) { + res[i] = nextLong(); + } + return res; + } + + public long nextLong() { + cnt = 1; + boolean neg = false; + if (c == NC) + c = getChar(); + for (; (c < '0' || c > '9'); c = getChar()) { + if (c == '-') + neg = true; + } + long res = 0; + for (; c >= '0' && c <= '9'; c = getChar()) { + res = (res << 3) + (res << 1) + c - '0'; + cnt *= 10; + } + return neg ? -res : res; + } + + public double nextDouble() { + double cur = nextLong(); + return c != '.' ? cur : cur + nextLong() / cnt; + } + + public double[] readDoubles(int N) { + double[] res = new double[N]; + for (int i = 0; i < N; i++) { + res[i] = nextDouble(); + } + return res; + } + + public String next() { + StringBuilder res = new StringBuilder(); + while (c <= 32) + c = getChar(); + while (c > 32) { + res.append(c); + c = getChar(); + } + return res.toString(); + } + + public String nextLine() { + StringBuilder res = new StringBuilder(); + while (c <= 32) + c = getChar(); + while (c != '\n') { + res.append(c); + c = getChar(); + } + return res.toString(); + } + + public boolean hasNext() { + if (c > 32) + return true; + while (true) { + c = getChar(); + if (c == NC) + return false; + else if (c > 32) + return true; + } + } + } + + // For Input.txt and Output.txt + // FileInputStream in = new FileInputStream(""input.txt""); + // FileOutputStream out = new FileOutputStream(""output.txt""); + // PrintWriter pw = new PrintWriter(out); + // Scanner sc = new Scanner(in); +}",1,Plagiarised +35857a5a,ab7507bf,"import java.io.PrintWriter; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; +public class solution { + public static void main(String args[]) throws java.lang.Exception{ + FastScanner s=new FastScanner(); + PrintWriter out=new PrintWriter(System.out); + int t=s.nextInt(); + for(int tt=0;ttsum2) { + ans=n; + break; + }else { + + ArrayList f=new ArrayList<>(); + for(int j=0;j0 && f.get(f.size()-1)>0) { + sum2-=f.get(f.size()-1); + f.remove(f.size()-1); + //out.println(sum2); + } + if(sum[i]>sum2) { + ans=Math.max(ans,f.size()); + } + //out.println(ans+"" Ayush""); + } + } + out.println(ans); + } + out.close(); + } + public static int modulus(int a,int b) { + int ans=a%b; + if(ans<0) { + ans=b+ans; + } + return ans; + } + static void sort(long [] a) { + ArrayList l=new ArrayList<>(); + for (long i:a) l.add(i); + Collections.sort(l); + for (int i=0; i l=new ArrayList<>(); + for (int i:a) l.add(i); + Collections.sort(l); + for (int i=0; i { + if(x[c]>y[c]) { + return 1; + }else { + return -1; + } + }); + } + public static void printb(boolean ans) { + if(ans) { + System.out.println(""Yes""); + }else { + System.out.println(""No""); + } + } + static class FastScanner { + BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st=new StringTokenizer(""""); + String next() { + while (!st.hasMoreTokens()) + try { + st=new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + return st.nextToken(); + } + double nextDouble() { + return Double.parseDouble(next()); + } + int nextInt() { + return Integer.parseInt(next()); + } + int[] readArray(int n) { + int[] a=new int[n]; + for (int i=0; i{ + int a , b; + Pair(int x , int y){ + a=x; + b=y; + } + public int compareTo(Pair o) { + return a != o.a ? a - o.a : b - o.b; + } + } +} +","import java.util.*; +import java.io.*; + +public class C1551 { + + public static void main(String[] args) throws IOException { + Scanner sc = new Scanner(System.in); + PrintWriter pw = new PrintWriter(System.out); + int t = sc.nextInt(); + while (t-- > 0) { + int n = sc.nextInt(); + char[][] arr = new char[n][]; + for (int i = 0; i < arr.length; i++) { + arr[i] = sc.next().toCharArray(); + } + int[][] cnt = new int[n][5]; + for (int i = 0; i < cnt.length; i++) { + for (char c : arr[i]) { + cnt[i][c - 'a']++; + } + } + int fans = 0; + for (int letter = 0; letter < 5; letter++) { + ArrayList al = new ArrayList(); + for (int i = 0; i < n; i++) { + al.add(2 * cnt[i][letter] - arr[i].length); + } + Collections.sort(al, Collections.reverseOrder()); + int sum = 0; + int ans = 0; + for (int x : al) { + sum += x; + if (sum > 0) { + ans++; + } else { + break; + } + } + fans = Math.max(ans, fans); + } + pw.println(fans); + } + pw.close(); + } + + static class Scanner { + BufferedReader br; + StringTokenizer st; + + public Scanner(InputStream s) { + br = new BufferedReader(new InputStreamReader(s)); + } + + public Scanner(FileReader f) { + br = new BufferedReader(f); + } + + public String next() throws IOException { + while (st == null || !st.hasMoreTokens()) + st = new StringTokenizer(br.readLine()); + return st.nextToken(); + } + + public int nextInt() throws IOException { + return Integer.parseInt(next()); + } + + public long nextLong() throws IOException { + return Long.parseLong(next()); + } + + public double nextDouble() throws IOException { + return Double.parseDouble(next()); + } + + public int[] nextIntArr(int n) throws IOException { + int[] arr = new int[n]; + for (int i = 0; i < n; i++) { + arr[i] = Integer.parseInt(next()); + } + return arr; + } + + } + +} +",0,Non-plagiarised +32ff22fe,f0d91796,"import java.io.*; +import java.util.*; +public class MyClass { + public static void pA(int[]a){ + for(int i=0;i0){ + int n=Integer.parseInt(br.readLine()); + String s[]=br.readLine().split("" ""); + int arr[]=new int[n]; + int a[]=new int[n]; + for(int i=0;i 0){ + + int n=sc.nextInt(); + int arr[]=new int[n]; + for(int i=0;i '9'); c = getChar()) { + if (c == '-') neg = true; + } + long res = 0; + for (; c >= '0' && c <= '9'; c = getChar()) { + res = (res << 3) + (res << 1) + c - '0'; + cnt *= 10; + } + return neg ? -res : res; + } + + public double nextDouble() { + double cur = nextLong(); + return c != '.' ? cur : cur + nextLong() / cnt; + } + + public double[] nextDoubles(int N) { + double[] res = new double[N]; + for (int i = 0; i < N; i++) { + res[i] = nextDouble(); + } + return res; + } + + public String next() { + StringBuilder res = new StringBuilder(); + while (c <= 32) c = getChar(); + while (c > 32) { + res.append(c); + c = getChar(); + } + return res.toString(); + } + + public String nextLine() { + StringBuilder res = new StringBuilder(); + while (c <= 32) c = getChar(); + while (c != '\n') { + res.append(c); + c = getChar(); + } + return res.toString(); + } + + public boolean hasNext() { + if (c > 32) return true; + while (true) { + c = getChar(); + if (c == NC) return false; + else if (c > 32) return true; + } + } +} + +",0,Non-plagiarised +4499e3b9,cbbbed5f,"import java.io.*; +import java.util.*; + +public class Main { + + static public void dfs(ArrayList> g, int u, long[][] dp, long[] l, long[] r, int p) { + dp[u][0] = 1; + for (int i = 0; i < g.get(u).size(); i++) { + int v = g.get(u).get(i); + if (v == p) continue; + if (dp[v][0] == 0) { + dfs(g, v, dp, l, r, u); + } + dp[u][1] += Math.max(Math.abs(l[u] - l[v]) + dp[v][1], Math.abs(l[u] - r[v]) + dp[v][2]); + dp[u][2] += Math.max(Math.abs(r[u] - l[v]) + dp[v][1], Math.abs(r[u] - r[v]) + dp[v][2]); + } + } + + static public void main(String args[]) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int T = Integer.parseInt(st.nextToken()); + for (int t = 1; t <= T; t++) { + st = new StringTokenizer(br.readLine()); + int n = Integer.parseInt(st.nextToken()); + long[] l = new long[n + 1], r = new long[n + 1]; + ArrayList> g = new ArrayList<>(); + for (int i = 0; i < n; i++) { + st = new StringTokenizer(br.readLine()); + l[i + 1] = Integer.parseInt(st.nextToken()); + r[i + 1] = Integer.parseInt(st.nextToken()); + g.add(new ArrayList<>()); + } + g.add(new ArrayList<>()); + + for (int i = 0; i < n - 1; i++) { + st = new StringTokenizer(br.readLine()); + int u = Integer.parseInt(st.nextToken()), v = Integer.parseInt(st.nextToken()); + g.get(v).add(u); + g.get(u).add(v); + } + long[][] dp = new long[n + 1][3]; + dfs(g, 1 , dp, l, r, -1); + bw.write(Math.max(dp[1][1], dp[1][2]) + ""\n""); + } + bw.flush(); + } +} +","import java.util.*; +import java.io.*; + +public class C{ + + private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static int vertices; + private static LinkedList adj[]; + private static long dp[][]; + private static int l[]; + private static int r[]; + private static boolean vis[]; + + public static void dfs(int v) { + if(vis[v]) + return; + vis[v] = true; + Iterator iterator = adj[v].listIterator(); + while(iterator.hasNext()) { + int child = iterator.next(); + if(!vis[child]) { + dfs(child); + long left = dp[child][0] + Math.abs(l[v] - l[child]); + dp[v][0] += Math.max(left, dp[child][1] + Math.abs(l[v] - r[child])); + long right = dp[child][0] + Math.abs(r[v] - l[child]); + dp[v][1] += Math.max(right, dp[child][1] + Math.abs(r[v] - r[child])); + } + } + } + + public static void main (String[] args) throws IOException { + int t = Integer.parseInt(br.readLine()); + while(t-- > 0) { + int n = Integer.parseInt(br.readLine()); + vertices = n; + dp = new long[n+1][2]; + adj = new LinkedList[n+1]; + vis = new boolean[n+1]; + l = new int[n+1]; + r = new int[n+1]; + for(int i=1;i<=n;i++) { + adj[i] = new LinkedList<>(); + } + for(int i=1;i<=n;i++) { + String lr[] = br.readLine().split("" ""); + l[i] = Integer.parseInt(lr[0]); + r[i] = Integer.parseInt(lr[1]); + } + for(int i=0;i0){ + int n = sc.nextInt(); + sc.nextLine(); + String a[] = new String[n]; + for(int i=0;i a1 = new ArrayList<>(); + a1.add('a'); + a1.add('b'); + a1.add('c'); + a1.add('d'); + a1.add('e'); + int res = 0; + for(Character ch : a1){ + ArrayList a2 = new ArrayList<>(); + for(int i=0;i=0;j--){ + int num = a2.get(j); + sum+=num; + if(sum>0){ + count++; + } + else + break; + } + res = Math.max(res,count); + } + System.out.println(res); + } + } +} + +","import java.util.*; +public class shivam{ + public static int diff(String str, char ch){ + int cnt=0; + for(int i=0;i=0;i--){ + sum+=a[i]; + if(sum>0){ + max++; + } + else{ + break; + } + } + + + + + + return max; + } + + + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + int k=sc.nextInt(); + while(k-->0){ + int n=sc.nextInt(); + String []arr=new String[n]; + for(int i=0;i empty = new ArrayList<>(); + List chairs = new ArrayList<>(); + for(int i = 0; i < n; i++) { + int status = fs.nextInt(); + if(status == 1) chairs.add(i+1); + else empty.add(i+1); + } + int[][] dp = new int[empty.size() + 1][chairs.size() + 1]; + dp[0][0] = 0; + for(int i = 1; i <= chairs.size(); i++) dp[0][i] = (int)3e+8; + for(int i = 1; i <= empty.size(); i++) { + for(int j = 1; j <= chairs.size(); j++) { + // Shift jth person to ith chair + dp[i][j] = dp[i-1][j-1] + Math.abs(empty.get(i-1) - chairs.get(j-1)); + dp[i][j] = Math.min(dp[i][j], dp[i-1][j]); + } + //System.out.println(i + "" "" + Arrays.toString(dp[i])); + } + //System.out.println(empty.size() + "" "" + chairs.size()); + System.out.println(dp[empty.size()][chairs.size()]); + } + } +}","import java.io.*; +import java.util.*; +import java.math.*; +import java.math.BigInteger; +//import javafx.util.*; +public final class B +{ + static StringBuilder ans=new StringBuilder(); + static FastReader in=new FastReader(); + static ArrayList> g; + static long mod=(long)(1e9+7); + static int D1[],D2[],par[]; + static boolean set[]; + static int value[]; + static long INF=Long.MAX_VALUE; + static int dp[][]; + static int N,M; + static int A[][],B[][]; + static int s=1; + public static void main(String args[])throws IOException + { + int N=i(); + int A[]=input(N); + ArrayList one=new ArrayList(); + ArrayList zero=new ArrayList(); + for(int i=1; i<=N; i++) + { + if(A[i-1]==1)one.add(i); + else zero.add(i); + } + int sum[][]=new int[N+5][N+5]; + for(int i=1; i<=one.size(); i++) + { + for(int j=1; j<=zero.size(); j++) + { + sum[i][j]=Math.abs(one.get(i-1)-zero.get(j-1)); + } + //print(sum[i]); + } + dp=new int[N+5][N+5]; + //for(int d[]:dp)Arrays.fill(d, Integer.MAX_VALUE); + Arrays.fill(dp[0], 0); + for(int i=1; i<=one.size(); i++) + { + for(int j=i; j<=zero.size(); j++) + { + if(i==j) + { + dp[i][j]=dp[i-1][j-1]+sum[i][j]; + } + else + { + dp[i][j]=Math.min(dp[i][j-1], dp[i-1][j-1]+sum[i][j]); + } + } + } + System.out.println(dp[one.size()][zero.size()]); +// f(0,0,one,zero,0); + //for(int d[]:dp)print(d); + + } + static int f(int i,int j,ArrayList one,ArrayList zero, int s) + { + if(i==one.size())return s; + if(j==zero.size())return Integer.MAX_VALUE; + int a=one.get(i),b=zero.get(j); + if(dp[a][b]==-1) + { + int min=Integer.MAX_VALUE; + for(int t=j; tA[i+1])return false; + } + + return true; + } + static int f1(int x,ArrayList A) + { + int l=-1,r=A.size(); + while(r-l>1) + { + int m=(l+r)/2; + int a=A.get(m); + if(aend)return 0; + int a=0,b=0,c=0; + if(d>1)a=f(st+d-1,end,d-1); + b=f(st+d,end,d); + c=f(st+d+1,end,d+1); + return value[st]+max(a,b,c); + } + static int max(int a,int b,int c) + { + return Math.max(a, Math.max(c, b)); + } + static int value(char X[],char Y[]) + { + int c=0; + for(int i=0; i<7; i++) + { + if(Y[i]=='1' && X[i]=='0')return -1; + if(X[i]=='1' && Y[i]=='0')c++; + } + return c; + } + static boolean isValid(int i,int j) + { + if(i<0 || i>=N)return false; + if(j<0 || j>=M)return false; + return true; + } + + static long fact(long N) + { + long num=1L; + while(N>=1) + { + num=((num%mod)*(N%mod))%mod; + N--; + } + return num; + } + static boolean reverse(long A[],int l,int r) + { + while(l l=new ArrayList<>(); + for (long i:a) l.add(i); + Collections.sort(l); + for (int i=0; i>(); + for(int i=0; i<=N; i++) + { + g.add(new ArrayList()); + } + } + + static long pow(long a,long b) + { + long mod=1000000007; + long pow=1; + long x=a; + while(b!=0) + { + if((b&1)!=0)pow=(pow*x)%mod; + x=(x*x)%mod; + b/=2; + } + return pow; + } + static long toggleBits(long x)//one's complement || Toggle bits + { + int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; + + return ((1< l=new ArrayList<>(); + for (int i:a) l.add(i); + Collections.sort(l); + for (int i=0; i A) + { + for(int a:A)System.out.print(a+"" ""); + System.out.println(); + } + + //Debugging Functions END + //---------------------- + //IO FUNCTIONS STARTS + static HashMap Hash(int A[]) + { + HashMap mp=new HashMap<>(); + for(int a:A) + { + int f=mp.getOrDefault(a,0)+1; + mp.put(a, f); + } + return mp; + } + static int i() + { + return in.nextInt(); + } + + static long l() + { + return in.nextLong(); + } + + static int[] input(int N){ + int A[]=new int[N]; + for(int i=0; i +{ + int l,r,index; + node(int l,int r,int index) + { + this.l=l; + this.r=r; + this.index=index; + } + public int compareTo(node X) + { + return X.r-this.r; + } +} +class node1 implements Comparable +{ + int l,r,index; + node1(int l,int r,int index) + { + this.l=l; + this.r=r; + this.index=index; + } + public int compareTo(node1 X) + { + if(this.l==X.l) + return X.r-this.r; + return this.l-X.l; + } +} +//Code For FastReader +//Code For FastReader +//Code For FastReader +//Code For FastReader +class FastReader +{ + BufferedReader br; + StringTokenizer st; + public FastReader() + { + br=new BufferedReader(new InputStreamReader(System.in)); + } + + String next() + { + while(st==null || !st.hasMoreElements()) + { + try + { + st=new StringTokenizer(br.readLine()); + } + catch(IOException e) + { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() + { + return Integer.parseInt(next()); + } + + long nextLong() + { + return Long.parseLong(next()); + } + //gey + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str=""""; + try + { + str=br.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + return str; + } + +}",0,Non-plagiarised +90d0ffd0,b21c7532,"import java.io.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Random; +import java.util.StringTokenizer; + +public class C { + + public static void main(String[] args) { + FastReader scan = new FastReader(); + PrintWriter out = new PrintWriter(System.out); + Task solver = new Task(); + int t = scan.nextInt(); + for(int tt = 1; tt <= t; tt++) solver.solve(tt, scan, out); + out.close(); + } + + static class Task { + int n, m; + char[][] a; + ArrayList moves; + + public void solve(int testNumber, FastReader scan, PrintWriter out) { + n = scan.nextInt(); + m = scan.nextInt(); + a = new char[n][m]; + moves = new ArrayList<>(); + for(int i = 0; i < n; i++) a[i] = scan.next().toCharArray(); + if(n % 2 == 0) { + for (int i = 0; i < n; i += 2) { + if (m % 2 == 0) { + for (int j = 0; j < m; j += 2) move(i, j); + } else { + for (int j = 0; j < m - 3; j += 2) move(i, j); + if (one(i, m - 2).size() == 4) { + move(i, m - 3); + move(i, m - 2); + } else { + move(i, m - 3); + move(i, m - 2); + } + } + } + } + else { + for (int i = 0; i < n - 1; i += 2) { + if (m % 2 == 0) { + for (int j = 0; j < m; j += 2) move(i, j); + } else { + for (int j = 0; j < m - 3; j += 2) move(i, j); + if (one(i, m - 2).size() == 4) { + move(i, m - 3); + move(i, m - 2); + } else { + move(i, m - 3); + move(i, m - 2); + } + } + } + if (m % 2 == 0) { + for (int j = 0; j < m; j += 2) move(n - 2, j); + } else { + for (int j = 0; j < m - 3; j += 2) move(n - 2, j); + if (one(n - 2, m - 2).size() == 4) { + move(n - 2, m - 3); + move(n - 2, m - 2); + } else { + move(n - 2, m - 3); + move(n - 2, m - 2); + } + } + } + out.println(moves.size() / 3); + for(int i = 0; i < moves.size(); i += 3) { + for(int j = 0; j < 3; j++) out.printf(""%d %d "", moves.get(i + j)[0], moves.get(i + j)[1]); + out.println(); + } + } + + void move(int y, int x) { + ArrayList o = one(y, x), z = zero(y, x); + while(!o.isEmpty()) { + int first = -1, second = -1; + if(o.size() <= 2) { + first = 1; + second = 2; + } + else { + first = 3; + second = 0; + } + for(int i = 0; i < first; i++) { + int[] t = o.get(i); + a[t[0] - 1][t[1] - 1] = '0'; + moves.add(t); + } + for(int i = 0; i < second; i++) { + int[] t = z.get(i); + a[t[0] - 1][t[1] - 1] = '1'; + moves.add(t); + } + o = one(y, x); + z = zero(y, x); + } + } + + ArrayList one(int y, int x) { + ArrayList res = new ArrayList<>(); + for(int i = y + 1; i >= y; i--) { + for(int j = x + 1; j >= x; j--) { + if(a[i][j] == '1') res.add(new int[] {i + 1, j + 1}); + } + } + return res; + } + ArrayList zero(int y, int x) { + ArrayList res = new ArrayList<>(); + for(int i = y; i <= y + 1; i++) { + for(int j = x; j <= x + 1; j++) { + if(a[i][j] == '0') res.add(new int[] {i + 1, j + 1}); + } + } + return res; + } + } + + static void ruffleSort(int[] a) { + Random get = new Random(); + for (int i = 0; i < a.length; i++) { + int r = get.nextInt(a.length); + int temp = a[i]; + a[i] = a[r]; + a[r] = temp; + } + Arrays.sort(a); + } + + static void ruffleSort(long[] a) { + Random get = new Random(); + for (int i = 0; i < a.length; i++) { + int r = get.nextInt(a.length); + long temp = a[i]; + a[i] = a[r]; + a[r] = temp; + } + Arrays.sort(a); + } + + static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + public FastReader(String s) throws FileNotFoundException { + br = new BufferedReader(new FileReader(new File(s))); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } + +}","import java.util.*; +import java.math.*; +import java.io.*; + +public class B{ + public static void main(String[] args){ + FastReader in = new FastReader(); + int t = in.nextInt(); + // int t = 1; + while(t-- != 0){ + int n = in.nextInt(), m = in.nextInt(); + + int[][] arr = new int[n][m]; + + for(int i = 0; i < n; i++){ + String s = in.next(); + for(int j = 0; j < m; j++){ + arr[i][j] = s.charAt(j)-'0'; + } + } + + // for(int i = 0; i < n; i++){ + // System.out.println(Arrays.toString(arr[i])); + // } + int res = 0; + for(int i = 0; i < n-1; i += 2){ + for(int j = 0; j < m-1; j += 2){ + int r = arr[i][j] + arr[i][j+1] + arr[i+1][j] + arr[i+1][j+1]; + if(r == 1) res += 3; + else if(r == 2) res += 2; + else if(r == 3) res += 1; + else if(r == 4) res += 4; + } + } + + if(n % 2 == 1){ + for(int j = 0; j < m-1; j += 2){ + int r = arr[n-1][j] + arr[n-1][j+1]; + if(r == 1) res += 3; + else if(r == 2) res += 2; + } + } + if(m % 2 == 1){ + for(int i = 0; i < n-1; i += 2){ + int r = arr[i][m-1] + arr[i+1][m-1]; + if(r == 1) res += 3; + else if(r == 2) res += 2; + } + } + + if(n % 2 == 1 && m % 2 == 1){ + if(arr[n-1][m-1] == 1) res+= 3; + } + + System.out.println(res); + + for(int i = 0; i < n-1; i += 2){ + for(int j = 0; j < m-1; j += 2){ + int a = arr[i][j], b = arr[i][j+1], c = arr[i+1][j], d = arr[i+1][j+1]; + int r = a+b+c+d; + if(r == 1){ + print1(i+1, j+1, a,b,c,d); + } + else if(r == 2){ + print2(i+1, j+1, a,b,c,d); + } + else if(r == 3){ + print3(i+1, j+1, a,b,c,d); + } + else if(r == 4){ + print4(i+1, j+1, a,b,c,d); + } + } + } + + // System.out.println(""AFTER""); + if(n % 2 == 1){ + for(int j = 0; j < m-1; j += 2){ + int r = arr[n-1][j] + arr[n-1][j+1]; + int a = 0, b = 0, c = arr[n-1][j], d=arr[n-1][j+1]; + if(r == 1) print1(n-1,j+1, a,b,c,d); + else if(r==2)print2(n-1,j+1, a,b,c,d); + // System.out.println(""n is od + "" + j); + // System.out.printf(""%d %d %d %d\nABCD\n"", a,b,c,d); + } + } + if(m % 2 == 1){ + for(int i = 0; i < n-1; i += 2){ + int r = arr[i][m-1] + arr[i+1][m-1]; + int a = 0, b = arr[i][m-1], c = 0, d=arr[i+1][m-1]; + if(r == 1) print1(i+1,m-1, a,b,c,d); + else if(r==2) print2(i+1,m-1, a,b,c,d); + // System.out.println(""m is odd + "" + i); + } + } + + if(n % 2 == 1 && m % 2 == 1){ + if(arr[n-1][m-1] == 1){ + print1(n-1, m-1,0,0,0,1); + // System.out.println(""ekeui de ""); + } + } + + } + + + } + + public static void print3(int i, int j, int a, int b, int c, int d){ + if(a == 0){ + System.out.printf(""%d %d %d %d %d %d\n"", i, j+1, i+1,j, i+1, j+1);//b c d + }else if(b == 0){ + System.out.printf(""%d %d %d %d %d %d\n"", i,j,i+1,j,i+1,j+1); // a c d + }else if(c == 0){ + System.out.printf(""%d %d %d %d %d %d\n"", i,j,i,j+1,i+1,j+1); //a b d + }else{ + System.out.printf(""%d %d %d %d %d %d\n"", i,j,i+1,j,i,j+1); //a c b + } + } + + public static void print2(int i, int j, int a, int b, int c, int d){ + if(a == 0 && b==0){ + System.out.printf(""%d %d %d %d %d %d\n"", i,j, i+1,j, i,j+1); //a c b + a = 1; + b = 1; + c = 0; + print3(i, j, a, b, c, d); + }else if(a == 0 && c == 0){ + System.out.printf(""%d %d %d %d %d %d\n"", i,j, i+1,j, i,j+1); //a c b + a = 1; + c = 1; + b = 0; + print3(i, j, a, b, c, d); + }else if(a == 0 && d == 0){ + System.out.printf(""%d %d %d %d %d %d\n"", i,j, i+1,j+1, i,j+1); //a d b + a = 1; + d = 1; + b = 0; + print3(i, j, a, b, c, d); + }else if(b == 0 && d == 0){ + System.out.printf(""%d %d %d %d %d %d\n"", i,j,i+1,j+1,i,j+1); //a d b + a = 0; + d = 1; + b = 1; + print3(i, j, a, b, c, d); + }else if(c == 0 && d == 0){ + System.out.printf(""%d %d %d %d %d %d\n"", i+1,j,i+1,j+1,i,j+1); //c d b + c = 1; + d = 1; + b = 0; + print3(i, j, a, b, c, d); + }else if(c == 0 && b == 0){ + System.out.printf(""%d %d %d %d %d %d\n"", i+1,j, i+1,j+1, i,j+1); //c d b + c = 1; + d = 0; + b = 1; + print3(i, j, a, b, c, d); + } + } + + public static void print1(int i, int j, int a, int b, int c, int d){ + if(a == 1){ + System.out.printf(""%d %d %d %d %d %d\n"", i, j, i+1,j, i+1, j+1);//a c d + a = 0; + c=1; + d=1; + print2(i, j,a,b,c,d); + }else if(b == 1){ + System.out.printf(""%d %d %d %d %d %d\n"", i,j+1,i+1,j,i+1,j+1); //b c d + b=0; + c=1; + d=1; + print2(i, j,a,b,c,d); + }else if(c == 1){ + System.out.printf(""%d %d %d %d %d %d\n"", i,j, i+1,j, i+1,j+1); // a c d + c=0; + a=1; + d=1; + print2(i, j,a,b,c,d); + }else{ + System.out.printf(""%d %d %d %d %d %d\n"", i,j,i+1,j,i+1,j+1); // a c d + d=0; + a=1; + c=1; + print2(i, j,a,b,c,d); + } + } + + public static void print4(int i, int j, int a, int b, int c, int d){ + System.out.printf(""%d %d %d %d %d %d\n"", i,j,i+1,j,i+1,j+1); // a c d + a=0; + d=0; + c=0; + print1(i,j,a,b,c,d); + } + + + static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader(){ + br = new BufferedReader(new + InputStreamReader(System.in)); + } + + String next(){ + while (st == null || !st.hasMoreElements()){ + try{ + st = new StringTokenizer(br.readLine()); + }catch (IOException e){ + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt(){ + return Integer.parseInt(next()); + } + + long nextLong(){ + return Long.parseLong(next()); + } + + double nextDouble(){ + return Double.parseDouble(next()); + } + + String nextLine(){ + String str = """"; + try{ + str = br.readLine(); + }catch (IOException e){ + e.printStackTrace(); + } + return str; + } + } +} + + +",0,Non-plagiarised +38601291,3bcd2014,"import java.io.*; +import java.util.*; + +public class stones { + public static void main (String[] args) throws IOException { + // set up + BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); + //BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(""test.in""))); + PrintWriter out = new PrintWriter(System.out); + int T = Integer.parseInt(input.readLine()); + for (int i=0;i=2;i--) { + //System.out.println(Arrays.toString(arr)); + if (arr[i] < goal) return false; + int max_d = Math.min((arr[i] - goal)/3, seq[i]/3); + //System.out.println(arr[i]); + //System.out.println(max_d); + arr[i-1] += max_d; + arr[i-2] += max_d*2; + arr[i] -= max_d*3; + } + //System.out.println(Arrays.toString(arr)); + for (int num: arr) { + if (num < goal) return false; } + return true; + } +} +","import java.io.*; +import java.util.*; + +import javax.sound.midi.MidiChannel; + +public class Main { + static PrintWriter out; + static Kioken sc; + + public static void main(String[] args) throws FileNotFoundException { + boolean t = true; + boolean f = false; + if (f) { + out = new PrintWriter(""output.txt""); + sc = new Kioken(""input.txt""); + } else { + out = new PrintWriter((System.out)); + sc = new Kioken(); + } + + int tt = 1; + tt = sc.nextInt(); + while (tt-- > 0) { + solve(); + } + out.flush(); + out.close(); + } + + static boolean bs(int[] arr, int a){ + + // out.println("" mid1 "" + Arrays.toString(arr) + "" "" + a); + + int[] curr = Arrays.copyOf(arr, arr.length); + for(int i = arr.length - 1; i >= 2; i--){ + if(arr[i] < a){ + return false; + } + int min = Math.min(arr[i] - a, curr[i]); + min = min/3; + arr[i] -= 3*min; + arr[i - 1] += min; + arr[i-2] += 2*min; + } + + for(int i: arr){ + if(i < a){ + return false; + } + } + return true; + } + public static void solve() { + int n = sc.nextInt(); + int[] arr = new int[n]; + int max = Integer.MIN_VALUE; + for(int i = 0; i < n; i++){ + arr[i] = sc.nextInt(); + max = Math.max(max, arr[i]); + } + + int l = 0, r = max, ans = 0; + while(l <= r){ + int mid = (l+r)/2; + // out.println("" l r "" + l + "" "" + r); + int[] aa = Arrays.copyOf(arr, arr.length); + if(bs(aa, mid)){ + // out.println(Arrays.toString(arr) + "" "" + Arrays.toString(aa) + "" mid "" + mid ); + ans = mid; + l = mid+1; + }else{ + r = mid-1; + } + } + out.println(ans); + } + + public static long leftShift(long a) { + return (long) Math.pow(2, a); + } + + public static void reverse(int[] arr) { + Arrays.sort(arr); + int n = arr.length; + for (int i = 0; i < arr.length; i++) { + int temp = arr[i]; + arr[i] = arr[n - 1 - i]; + arr[n - 1 - i] = temp; + } + return; + } + + public static int lower_bound(ArrayList ar, int k) { + int s = 0, e = ar.size(); + while (s != e) { + int mid = s + e >> 1; + if (ar.get(mid) <= k) { + s = mid + 1; + } else { + e = mid; + } + } + return Math.abs(s) - 1; + } + + public static int upper_bound(ArrayList ar, int k) { + int s = 0; + int e = ar.size(); + while (s != e) { + int mid = s + e >> 1; + if (ar.get(mid) < k) { + s = mid + 1; + } else { + e = mid; + } + } + if (s == ar.size()) { + return -1; + } + return s; + } + + static class Kioken { + // FileInputStream br = new FileInputStream(""input.txt""); + + BufferedReader br; + StringTokenizer st; + + Kioken(String filename) { + try { + FileReader fr = new FileReader(filename); + br = new BufferedReader(fr); + st = new StringTokenizer(""""); + + } catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + } + } + + Kioken() { + try { + br = new BufferedReader(new InputStreamReader(System.in)); + st = new StringTokenizer(""""); + + } catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + } + } + + public String next() { + while (!st.hasMoreTokens()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (Exception e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + public int nextInt() { + return Integer.parseInt(next()); + } + + public long nextLong() { + return Long.parseLong(next()); + } + + public double nextDouble() { + return Double.parseDouble(next()); + } + + public String nextLine() { + try { + return br.readLine(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public boolean hasNext() { + String next = null; + try { + next = br.readLine(); + } catch (Exception e) { + } + if (next == null || next.length() == 0) { + return false; + } + st = new StringTokenizer(next); + return true; + } + } +}",0,Non-plagiarised +a101df86,dffa45ac,"import java.util.*; +public class Main { + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + + // long mod = 1_000_000_007L; + // long mod = 998_244_353L; + + int t = sc.nextInt(); + + for ( int zzz=0; zzz0) { + int n = sc.nextInt(); + int[] arr = new int[n]; + for(int i = 0; i < n; i++) { + arr[i] = sc.nextInt(); + } + if(check(arr)) System.out.println(""YES""); + else System.out.println(""NO""); + } + + } + static boolean check(int[] arr) { + int n = arr.length; + TreeSet set = new TreeSet<>(); + set.add(arr[0]); + for(int i = 1; i < n; i++) { + set.add(arr[i]); + if(arr[i-1] == arr[i]) continue; + Integer x = set.lower(arr[i]); + if(x != null && x == arr[i-1]) continue; + x = set.higher(arr[i]); + if(x != null && x == arr[i-1]) continue; + return false; + } + return true; + } + + static class FastScanner { + public BufferedReader reader; + public StringTokenizer tokenizer; + public FastScanner() { + reader = new BufferedReader(new InputStreamReader(System.in), 32768); + tokenizer = null; + } + public String next() { + while (tokenizer == null || !tokenizer.hasMoreTokens()) { + try { + tokenizer = new StringTokenizer(reader.readLine()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + return tokenizer.nextToken(); + } + public int nextInt() { + return Integer.parseInt(next()); + } + public long nextLong() { + return Long.parseLong(next()); + } + public double nextDouble() { + return Double.parseDouble(next()); + } + public String nextLine() { + try { + return reader.readLine(); + } catch(IOException e) { + throw new RuntimeException(e); + } + } + } + +} +","import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.util.Scanner; +import java.util.StringTokenizer; +import java.util.TreeSet; + +public class A { + public static void main(String args[]) throws Exception { + FastScanner sc = new FastScanner(); + int T = 1; + T = sc.nextInt(); + PrintWriter pw = new PrintWriter(System.out); + while (T-- > 0) { + solve(sc, pw); + } + pw.close(); + } + + public static void solve(FastScanner sc, PrintWriter pw) throws Exception { + int n = sc.nextInt(); + int[] arr = new int[n]; + for(int i=0;i S = new TreeSet<>(); + S.add(arr[0]); + for(int i=1;i>hashes=new HashMap>(); + HashSethashes2=new HashSet(); + long pow_p[]=new long[m]; + pow_p[0]=1; + for(int i=1;imap=new HashMap(); + for(int i=1;i<=333333334;i++) + { + map.put(i+2*i,new Pair(i,2*i)); + map.put(i+(2*(i+1)),new Pair(i,i+1)); + map.put(i+1+(2*i),new Pair(i+1,i)); + }*/ + + outer:while(tt-->0) + { + int n=scan.nextInt(); + int cnt[][]=new int[n][5]; + String arr[]=new String[n]; + int sumlens=0; + for(int i=0;itmp=new ArrayList(); + int sumall=0; + for(int i=0;iTHAT) + { + // if(to==3) + // out.println(""AHA ""+(n-i)); + res=Math.max(res,n-i); + break; + } + THIS-=tmp.get(i).x; + THAT-=tmp.get(i).y; + } + } + out.println(res); + + + + + + + + + } + out.close(); + } + + + + + static class special{ + boolean bool; + int n; + //int id; + special(boolean bool,int n) + { + this.bool=bool; + this.n=n; + } + @Override + public int hashCode() { + int hash = 7; + hash = 31 * hash + (int) n; + + return hash; + } + @Override + public boolean equals(Object o){ + if (o == this) return true; + if (o.getClass() != getClass()) return false; + special t = (special)o; + return t.bool == bool && t.n == n; + } + } + + + static long binexp(long a,long n) + { + if(n==0) + return 1; + long res=binexp(a,n/2); + if(n%2==1) + return res*res*a; + else + return res*res; + } + + static long powMod(long base, long exp, long mod) { + if (base == 0 || base == 1) return base; + if (exp == 0) return 1; + if (exp == 1) return (base % mod+mod)%mod; + long R = (powMod(base, exp/2, mod) % mod+mod)%mod; + R *= R; + R %= mod; + if ((exp & 1) == 1) { + return (base * R % mod+mod)%mod; + } + else return (R %mod+mod)%mod; + } + static double dis(double x1,double y1,double x2,double y2) + { + return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); + } + static long mod(long x,long y) + { + if(x<0) + x=x+(-x/y+1)*y; + return x%y; + } + public static long pow(long b, long e) { + long r = 1; + while (e > 0) { + if (e % 2 == 1) r = r * b ; + b = b * b; + e >>= 1; + } + return r; + } + private static void sort(long[] arr) { + List list = new ArrayList<>(); + for (long object : arr) list.add(object); + Collections.sort(list); + //Collections.reverse(list); + for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); + } + private static void sort2(long[] arr) { + List list = new ArrayList<>(); + for (Long object : arr) list.add(object); + Collections.sort(list); + Collections.reverse(list); + for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); + } + static class FastScanner + { + private int BS = 1 << 16; + private char NC = (char) 0; + private byte[] buf = new byte[BS]; + private int bId = 0, size = 0; + private char c = NC; + private double cnt = 1; + private BufferedInputStream in; + + public FastScanner() { + in = new BufferedInputStream(System.in, BS); + } + + public FastScanner(String s) { + try { + in = new BufferedInputStream(new FileInputStream(new File(s)), BS); + } catch (Exception e) { + in = new BufferedInputStream(System.in, BS); + } + } + + private char getChar() { + while (bId == size) { + try { + size = in.read(buf); + } catch (Exception e) { + return NC; + } + if (size == -1) return NC; + bId = 0; + } + return (char) buf[bId++]; + } + + public int nextInt() { + return (int) nextLong(); + } + + public int[] nextInts(int N) { + int[] res = new int[N]; + for (int i = 0; i < N; i++) { + res[i] = (int) nextLong(); + } + return res; + } + + public long[] nextLongs(int N) { + long[] res = new long[N]; + for (int i = 0; i < N; i++) { + res[i] = nextLong(); + } + return res; + } + + public long nextLong() { + cnt = 1; + boolean neg = false; + if (c == NC) c = getChar(); + for (; (c < '0' || c > '9'); c = getChar()) { + if (c == '-') neg = true; + } + long res = 0; + for (; c >= '0' && c <= '9'; c = getChar()) { + res = (res << 3) + (res << 1) + c - '0'; + cnt *= 10; + } + return neg ? -res : res; + } + + public double nextDouble() { + double cur = nextLong(); + return c != '.' ? cur : cur + nextLong() / cnt; + } + + public double[] nextDoubles(int N) { + double[] res = new double[N]; + for (int i = 0; i < N; i++) { + res[i] = nextDouble(); + } + return res; + } + + public String next() { + StringBuilder res = new StringBuilder(); + while (c <= 32) c = getChar(); + while (c > 32) { + res.append(c); + c = getChar(); + } + return res.toString(); + } + + public String nextLine() { + StringBuilder res = new StringBuilder(); + while (c <= 32) c = getChar(); + while (c != '\n') { + res.append(c); + c = getChar(); + } + return res.toString(); + } + + public boolean hasNext() { + if (c > 32) return true; + while (true) { + c = getChar(); + if (c == NC) return false; + else if (c > 32) return true; + } + } + } + + static class Pair implements Comparable{ + public long x, y,z; + public Pair(long x1, long y1,long z1) { + x=x1; + y=y1; + z=z1; + } + public Pair(long x1, long y1) { + x=x1; + y=y1; + + } + + @Override + public int hashCode() { + return (int)(x + 31 * y); + } + public String toString() { + return x + "" "" + y+"" ""+z; + } + @Override + public boolean equals(Object o){ + if (o == this) return true; + if (o.getClass() != getClass()) return false; + Pair t = (Pair)o; + return t.x == x && t.y == y&&t.z==z; + } + public int compareTo(Pair o) + { + + + return (int)((o.y-o.x)-(y-x)); + + } + + } + + } + + +","import java.util.StringTokenizer; +import java.io.*; + +public class CF_1551c{ + public static final void main(String[] args){ + Kattio io= new Kattio(); + int t= io.getInt(); + while(t-->0){ + int n= io.getInt(); + int[][] ps= new int[5][n]; + for(int i=0; imax) max= i; + } + } + io.println(max); + } + io.close(); + } + + // using mergeSort to avoid Java quicksort TLE hacks + static void mergeSort(int arr[]){ + int n= arr.length; + for (int sz= 1; sz<=n-1; sz=2*sz){ + for (int l= 0; l occupied = new ArrayList<>(); + for (int i = 0; i < n; i++) { + arr[i] = scn.nextInt(); + if (arr[i] == 1) occupied.add(i); + } + int[][] dp = new int[n + 1][occupied.size() + 1]; + for (int[] row : dp) Arrays.fill(row, (int) 1e9); + dp[0][0] = 0; + for (int i = 1; i <= n; i++) { + dp[i][0] = 0; + for (int j = 1; j <= occupied.size(); j++) { + dp[i][j] = dp[i - 1][j]; + if (arr[i - 1] == 0) + dp[i][j] = Math.min(dp[i][j], dp[i - 1][j - 1] + Math.abs(i - 1 - occupied.get(j - 1))); + } + } + System.out.println(dp[n][occupied.size()]); + + + } +} +","import java.util.*; + +public class D { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + ArrayList occupied = new ArrayList<>(); + ArrayList vacant = new ArrayList<>(); + for (int i = 0; i < n; i++) { + int x = scanner.nextInt(); + if (x == 1) + occupied.add(i); + else + vacant.add(i); + } + + Solution Solution = new Solution(occupied, vacant); + +// System.out.println(Solution.tabulation()); + System.out.println(Solution.memoization()); + } +} + +class Solution { + + ArrayList occupied, vacant; + int x, y; + + public Solution(ArrayList occupied, ArrayList vacant) { + this.occupied = occupied; + this.vacant = vacant; + x = occupied.size(); y = vacant.size(); + } + + int tabulation() { + return tabulation(x, y); + } + + int tabulation(int x, int y) { + int[][] dp = new int[x+1][y+1]; + for (int i = 0; i <= x; i++) { + Arrays.fill(dp[i], Integer.MAX_VALUE/2); + } + for (int i = 0; i <= x; i++) { + dp[i][0] = 0; + } + for (int i = 0; i <= y; i++) { + dp[0][i] = 0; + } + + for (int i = 1; i <= x; i++) { + for (int j = 1; j <= y; j++) { + if(i == j) { + dp[i][j] = dp[i-1][j-1] + Math.abs(occupied.get(i-1) - vacant.get(j-1)); + } + else { + dp[i][j] = Math.min(dp[i][j-1], dp[i-1][j-1] + Math.abs(occupied.get(i-1) - vacant.get(j-1))); + } + } + } + return dp[x][y]; + } + + int memoization() { + int[][] dp = new int[x][y]; + for (int i = 0; i < x; i++) { + Arrays.fill(dp[i], -1); + } + return memoization(dp, x-1, y-1); + } + + int memoization(int[][] dp, int n, int m) { + if(n < 0) { + return 0; + } + if(m < n) { + return Integer.MAX_VALUE; + } + if(dp[n][m] != -1) { + return dp[n][m]; + } + int first = memoization(dp, n, m-1); + int second = memoization(dp, n-1, m-1) + Math.abs(occupied.get(n) - vacant.get(m)); + dp[n][m] = Math.min(first, second); + return dp[n][m]; + } +}",0,Non-plagiarised +316c9955,e7370eec,"import java.io.DataInputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.util.LinkedList; + +public class C { + + private static boolean[] visited; + private static Node[] nodes; + + private static void run() throws IOException { + int n = in.nextInt(); + nodes = new Node[n]; + for (int i = 0; i < n; i++) { + nodes[i] = new Node(); + nodes[i].index = i; + nodes[i].l = in.nextInt(); + nodes[i].r = in.nextInt(); + } + for (int i = 0; i < n - 1; i++) { + int x = in.nextInt() - 1; + int y = in.nextInt() - 1; + nodes[x].next.add(y); + nodes[y].next.add(x); + } + + visited = new boolean[n]; + dfs(0); + out.println(Math.max(nodes[0].ans_l, nodes[0].ans_r)); + } + + private static void dfs(int now) { + visited[now] = true; + + for (int next : nodes[now].next) { + if (visited[next]) continue; + + dfs(next); + nodes[now].ans_l += Math.max(nodes[next].ans_l + Math.abs(nodes[now].l - nodes[next].l), nodes[next].ans_r + Math.abs(nodes[now].l - nodes[next].r)); + nodes[now].ans_r += Math.max(nodes[next].ans_l + Math.abs(nodes[now].r - nodes[next].l), nodes[next].ans_r + Math.abs(nodes[now].r - nodes[next].r)); + } + } + + static class Node { + int index; + long l, r; + long ans_l, ans_r; + LinkedList next = new LinkedList<>(); + } + + public static void main(String[] args) throws IOException { + in = new Reader(); + out = new PrintWriter(new OutputStreamWriter(System.out)); + + int t = in.nextInt(); + for (int i = 0; i < t; i++) { + run(); + } + + out.flush(); + in.close(); + out.close(); + } + + private static int gcd(int a, int b) { + if (a == 0 || b == 0) + return 0; + while (b != 0) { + int tmp; + tmp = a % b; + a = b; + b = tmp; + } + return a; + } + + static final long mod = 1000000007; + + static long pow_mod(long a, long b) { + long result = 1; + while (b != 0) { + if ((b & 1) != 0) result = (result * a) % mod; + a = (a * a) % mod; + b >>= 1; + } + return result; + } + + private static long multiplied_mod(long... longs) { + long ans = 1; + for (long now : longs) { + ans = (ans * now) % mod; + } + return ans; + } + + @SuppressWarnings(""FieldCanBeLocal"") + private static Reader in; + private static PrintWriter out; + + private static int[] read_int_array(int len) throws IOException { + int[] a = new int[len]; + for (int i = 0; i < len; i++) { + a[i] = in.nextInt(); + } + return a; + } + + private static long[] read_long_array(int len) throws IOException { + long[] a = new long[len]; + for (int i = 0; i < len; i++) { + a[i] = in.nextLong(); + } + return a; + } + + private static void print_array(int[] array) { + for (int now : array) { + out.print(now); + out.print(' '); + } + out.println(); + } + + private static void print_array(long[] array) { + for (long now : array) { + out.print(now); + out.print(' '); + } + out.println(); + } + + static class Reader { + private static final int BUFFER_SIZE = 1 << 16; + private final DataInputStream din; + private final byte[] buffer; + private int bufferPointer, bytesRead; + + Reader() { + din = new DataInputStream(System.in); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public String readLine() throws IOException { + final byte[] buf = new byte[1024]; // line length + int cnt = 0, c; + while ((c = read()) != -1) { + if (c == '\n') { + break; + } + buf[cnt++] = (byte) c; + } + return new String(buf, 0, cnt); + } + + public int nextSign() throws IOException { + byte c = read(); + while ('+' != c && '-' != c) { + c = read(); + } + return '+' == c ? 0 : 1; + } + + private static boolean isSpaceChar(int c) { + return !(c >= 33 && c <= 126); + } + + public int skip() throws IOException { + int b; + // noinspection ALL + while ((b = read()) != -1 && isSpaceChar(b)) { + ; + } + return b; + } + + public char nc() throws IOException { + return (char) skip(); + } + + public String next() throws IOException { + int b = skip(); + final StringBuilder sb = new StringBuilder(); + while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') + sb.appendCodePoint(b); + b = read(); + } + return sb.toString(); + } + + public int nextInt() throws IOException { + int ret = 0; + byte c = read(); + while (c <= ' ') { + c = read(); + } + final boolean neg = c == '-'; + if (neg) { + c = read(); + } + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (neg) { + return -ret; + } + return ret; + } + + public long nextLong() throws IOException { + long ret = 0; + byte c = read(); + while (c <= ' ') { + c = read(); + } + final boolean neg = c == '-'; + if (neg) { + c = read(); + } + do { + ret = ret * 10 + c - '0'; + } + while ((c = read()) >= '0' && c <= '9'); + if (neg) { + return -ret; + } + return ret; + } + + public double nextDouble() throws IOException { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') { + c = read(); + } + final boolean neg = c == '-'; + if (neg) { + c = read(); + } + + do { + ret = ret * 10 + c - '0'; + } + while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') { + while ((c = read()) >= '0' && c <= '9') { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) { + return -ret; + } + return ret; + } + + private void fillBuffer() throws IOException { + bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); + if (bytesRead == -1) { + buffer[0] = -1; + } + } + + private byte read() throws IOException { + if (bufferPointer == bytesRead) { + fillBuffer(); + } + return buffer[bufferPointer++]; + } + + public void close() throws IOException { + din.close(); + } + + } +}","//package codeforces.round722div2; + +import java.io.*; +import java.util.*; + +public class C { + static InputReader in; + static PrintWriter out; + + public static void main(String[] args) { + //initReaderPrinter(true); + initReaderPrinter(false); + solve(in.nextInt()); + //solve(1); + } + static int n; + static List[] adj; + static long[] l, r; + static void solve(int testCnt) { + for (int testNumber = 0; testNumber < testCnt; testNumber++) { + n = in.nextInt(); + adj = new List[n + 1]; + for(int i = 0; i <= n; i++) { + adj[i] = new ArrayList<>(); + } + l = new long[n + 1]; + r = new long[n + 1]; + for(int i = 1; i <= n; i++) { + l[i] = in.nextInt(); + r[i] = in.nextInt(); + } + for(int i = 0; i < n - 1; i++) { + int u = in.nextInt(); + int v = in.nextInt(); + adj[u].add(v); + adj[v].add(u); + } + long[] ans = dfs(1, 0); + out.println(Math.max(ans[0], ans[1])); + } + out.close(); + } + + static long[] dfs(int curr, int par) { + long[] ans = new long[2]; + for(int next : adj[curr]) { + if(next == par) continue; + long[] nextAns = dfs(next, curr); + ans[0] += Math.max(nextAns[0] + Math.abs(l[curr] - l[next]), nextAns[1] + Math.abs(l[curr] - r[next])); + ans[1] += Math.max(nextAns[0] + Math.abs(r[curr] - l[next]), nextAns[1] + Math.abs(r[curr] - r[next])); + } + return ans; + } + + static void initReaderPrinter(boolean test) { + if (test) { + try { + in = new InputReader(new FileInputStream(""src/input.in"")); + out = new PrintWriter(new FileOutputStream(""src/output.out"")); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + in = new InputReader(System.in); + out = new PrintWriter(System.out); + } + } + + static class InputReader { + BufferedReader br; + StringTokenizer st; + + InputReader(InputStream stream) { + try { + br = new BufferedReader(new InputStreamReader(stream), 32768); + } catch (Exception e) { + e.printStackTrace(); + } + } + + String next() { + while (st == null || !st.hasMoreTokens()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + + Integer[] nextIntArray(int n) { + Integer[] a = new Integer[n]; + for (int i = 0; i < n; i++) a[i] = nextInt(); + return a; + } + + int[] nextIntArrayPrimitive(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) a[i] = nextInt(); + return a; + } + + int[] nextIntArrayPrimitiveOneIndexed(int n) { + int[] a = new int[n + 1]; + for (int i = 1; i <= n; i++) a[i] = nextInt(); + return a; + } + + Long[] nextLongArray(int n) { + Long[] a = new Long[n]; + for (int i = 0; i < n; i++) a[i] = nextLong(); + return a; + } + + long[] nextLongArrayPrimitive(int n) { + long[] a = new long[n]; + for (int i = 0; i < n; i++) a[i] = nextLong(); + return a; + } + + long[] nextLongArrayPrimitiveOneIndexed(int n) { + long[] a = new long[n + 1]; + for (int i = 1; i <= n; i++) a[i] = nextLong(); + return a; + } + + String[] nextStringArray(int n) { + String[] g = new String[n]; + for (int i = 0; i < n; i++) g[i] = next(); + return g; + } + } +}",0,Non-plagiarised +7814575b,8cfe3ee0,"import java.io.*; +import java.util.*; +import static java.lang.Math.max; +import static java.lang.Math.min; +import static java.lang.Math.abs; + +public class C { + + static int fval(String s, char x){ + int fx = 0, oth = 0; + for(int i = 0; i0){ + int n = Integer.parseInt(br.readLine()); + ArrayList lst = new ArrayList<>(); + for(int i = 0; i vals = new ArrayList<>(); + for(int j = 0; j0){ + sum+=vals.get(++pt); + } + ans = max(ans, pt+1); + } + sb.append(ans).append('\n'); + } + System.out.println(sb); + } +} +/** + * min deletions + * for each letter calc max len of words + * Its desirable to add someting that has more of x and less of other letters -> f(x, s)=>fx-sum(fz) + */ +","import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.Comparator; +import java.util.StringTokenizer; + +public class Comprog { + + static FastReader fr = new FastReader(); + + private static void testCase() { + int n = fr.nextInt(); + int [][] scoreChanges = new int [5][n]; + int [] totalScores = new int [5]; + + // initialize + for (int i = 0; i < 5; i++) + totalScores[i] = 0; + + for (int i = 0; i < 5; i++) + for (int j = 0; j < n; j++) + scoreChanges[i][j] = 0; + + for (int wordIndex = 0; wordIndex < n; wordIndex++) { + String nextWord = fr.nextLine(); + for (int charIndex = 0; charIndex < 5; charIndex++) { + // How many more of the current char ('a' or 'b' or etc.) are in nextWord than + // the total number of chars in it + int change = 2 * countCharsInString(nextWord, (char) ('a' + charIndex)) - nextWord.length(); + totalScores[charIndex] += change; + scoreChanges[charIndex][wordIndex] = change; + } + } + + + for (int charIndex = 0; charIndex < 5; charIndex++) + Arrays.sort(scoreChanges[charIndex]); + + int round = 0; + boolean done = false; + while (round < n && !done) { + for (int charIndex = 0; charIndex < 5; charIndex++) { + if (totalScores[charIndex] > 0) { + System.out.println(n - round); + done = true; + break; + } + totalScores[charIndex] -= scoreChanges[charIndex][round]; + } + round++; + } + if (!done) + System.out.println(0); + } + + + public static int countCharsInString(String str, char c) { + int cnt = 0; + for (int i = 0; i < str.length(); i++) + if (str.charAt(i) == c) + cnt++; + return cnt; + } + + public static void main(String[] args) { + int t; + t = fr.nextInt(); + for (int i = 0; i < t; i++) + testCase(); + } + + + + + static class NumberFrequency { + int number; + int frequency; + public NumberFrequency(int number, int frequency) { + this.number = number; + this.frequency = frequency; + } + public void incrementFrequency() { + this.frequency++; + } + } + + static class SortByFrequency implements Comparator { + public int compare(NumberFrequency nf1, NumberFrequency nf2) { + return nf2.frequency - nf1.frequency; + } + } + + + + static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } +}",0,Non-plagiarised +6d55e68b,d8a171a3,"import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class _1615C16 { + + public static void main(String[] args) throws IOException { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(in.readLine()); + int n; + int s1, e1; + char[] start, end; + for (int s = 0; s < t; s++) { + n = Integer.parseInt(in.readLine()); + start = in.readLine().toCharArray(); + end = in.readLine().toCharArray(); + s1 = 0; + e1 = 0; + for (int i = 0; i < n; i++) if (start[i] == '1') s1++; + for (int i = 0; i < n; i++) if (end[i] == '1') e1++; + if (e1 == n - s1 + 1) { + for (int i = 0; i < n; i++) { + if (start[i] == '1') start[i] = '0'; + else start[i] = '1'; + } + int ct = 0; + for (int i = 0; i < n; i++) if (start[i] != end[i]) ct++; + int ct2 = Integer.MAX_VALUE; + if (s1 == e1) { + for (int i = 0; i < n; i++) { + if (start[i] == '1') start[i] = '0'; + else start[i] = '1'; + } + ct2 = 0; + for (int i = 0; i < n; i++) if (start[i] != end[i]) ct2++; + } + System.out.println(Math.min(ct, ct2)); + } else if (s1 == e1) { + int ct = 0; + for (int i = 0; i < n; i++) if (start[i] != end[i]) ct++; + int ct2 = Integer.MAX_VALUE; + if (e1 == n - s1 + 1) { + ct2 = 0; + for (int i = 0; i < n; i++) if (start[i] != end[i]) ct2++; + } + System.out.println(Math.min(ct, ct2)); + } else { + System.out.println(-1); + } + } + } +} +","import java.io.*; +import java.util.*; + +public class B { + + public static void main(String[] args)throws IOException { + + FastScanner scan = new FastScanner(); + PrintWriter output = new PrintWriter(System.out); + int t = scan.nextInt(); + for(int tt = 0;tt list = new ArrayList<>(); + for(int i:arr) list.add(i); + Collections.sort(list); + for(int i = 0;ilist=new ArrayList<>(); + for(long ele:arr) + { + list.add(ele); + } + Collections.sort(list,Collections.reverseOrder()); + int n=arr.length; + for (int i=0;i= 0) { + return X / Y; + } else { + return (X-Y+1) / Y; + } + } + static PrintWriter out=new PrintWriter((System.out)); + public static void main(String args[])throws IOException + { + int t=inputReader.nextInt(); + while (t-->0) { + solve(); + } + out.close(); + } + static class InputReader { + + private InputStream stream; + private byte[] buf = new byte[8192]; + private int curChar, snumChars; + private SpaceCharFilter filter; + + public InputReader(InputStream stream) { + this.stream = stream; + } + + public int snext() { + if (snumChars == -1) + throw new InputMismatchException(); + if (curChar >= snumChars) { + curChar = 0; + try { + snumChars = stream.read(buf); + } catch (IOException e) { + throw new InputMismatchException(); + } + if (snumChars <= 0) + return -1; + } + return buf[curChar++]; + } + + public int nextInt() { + int c = snext(); + while (isSpaceChar(c)) + c = snext(); + int sgn = 1; + if (c == '-') { + sgn = -1; + c = snext(); + } + int res = 0; + do { + if (c < '0' || c > '9') + throw new InputMismatchException(); + res *= 10; + res += c - '0'; + c = snext(); + } while (!isSpaceChar(c)); + return res * sgn; + } + + public long nextLong() { + int c = snext(); + while (isSpaceChar(c)) + c = snext(); + int sgn = 1; + if (c == '-') { + sgn = -1; + c = snext(); + } + long res = 0; + do { + if (c < '0' || c > '9') + throw new InputMismatchException(); + res *= 10; + res += c - '0'; + c = snext(); + } while (!isSpaceChar(c)); + return res * sgn; + } + + public int[] nextIntArray(int n) { + int a[] = new int[n]; + for (int i = 0; i < n; i++) + a[i] = nextInt(); + return a; + } + + public String readString() { + int c = snext(); + while (isSpaceChar(c)) + c = snext(); + StringBuilder res = new StringBuilder(); + do { + res.appendCodePoint(c); + c = snext(); + } while (!isSpaceChar(c)); + return res.toString(); + } + + public boolean isSpaceChar(int c) { + if (filter != null) + return filter.isSpaceChar(c); + return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; + } + + public interface SpaceCharFilter { + public boolean isSpaceChar(int ch); + } + } + }","import java.io.*; +import java.util.*; + + +public class Main { + static int i, j, k, n, m, t, y, x, sum = 0; + static long mod = 998244353; + static FastScanner fs = new FastScanner(); + static PrintWriter out = new PrintWriter(System.out); + static String str; + + public static void main(String[] args) throws IOException { + + t = fs.nextInt(); + + while (t-- > 0) { + + n = fs.nextInt(); + long k = fs.nextLong(); + + long sum=0; + + int[] arr = fs.readArray(n); + + for(i=0;i=0;i--){ + suffix[i] = suffix[i+1]+arr[i]-arr[0]; + } + + long ans = sum-k; + + if(ans<=0){ + out.println(0); + continue; + } + + for(i=n-1;i>0;i--){ + + long temp = sum-k; + long ansHere; + if(suffix[i]>=temp){ + ansHere = n-i; + } + + else{ + long y = temp - suffix[i]; + + long x = n-i+1; + + long moves = y/x; + + if(y%x!=0) + moves++; + + ansHere = moves+(n-i); + } + + ans = Math.min(ans,ansHere); + } + + out.println(ans); + + } + + out.close(); + } + + /*static long nck(int n , int k){ + long a = fact[n]; + long b = modInv(fact[k]); + b*= modInv(fact[n-k]); + b%=mod; + + return (a*b)%mod; + } + + static void populateFact(){ + fact[0]=1; + + fact[1] = 1; + + for(i=2;i<300005;i++){ + fact[i]=i*fact[i-1]; + fact[i]%=mod; + } + } + */ + + + static long gcd(long a, long b) { + if (b == 0) + return a; + return gcd(b, a % b); + } + + static long exp(long base, long pow) { + if (pow == 0) return 1; + long half = exp(base, pow / 2); + if (pow % 2 == 0) return mul(half, half); + return mul(half, mul(half, base)); + } + + static long mul(long a, long b) { + return ((a % mod) * (b % mod)) % mod; + } + + static long add(long a, long b) { + return ((a % mod) + (b % mod)) % mod; + } + + static long modInv(long x) { + return exp(x, mod - 2); + } + + static void ruffleSort(int[] a) { + //ruffle + int n = a.length; + Random r = new Random(); + for (int i = 0; i < a.length; i++) { + int oi = r.nextInt(n), temp = a[i]; + a[i] = a[oi]; + a[oi] = temp; + } + + //then sort + Arrays.sort(a); + } + + static void ruffleSort(long[] a) { + //ruffle + int n = a.length; + Random r = new Random(); + for (int i = 0; i < a.length; i++) { + int oi = r.nextInt(n); + long temp = a[i]; + a[i] = a[oi]; + a[oi] = temp; + } + + Arrays.sort(a); + } + + static class FastScanner { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(""""); + + String next() { + while (!st.hasMoreTokens()) + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + int[] readArray(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) a[i] = nextInt(); + return a; + } + + long nextLong() { + return Long.parseLong(next()); + } + } + + + static class Pair implements Comparable { + public int x, y; + + Pair(int x, int y) { + this.x = x; + this.y = y; + } + + @Override + public int compareTo(Pair o) { + + if (x == o.x) + return Integer.compare(y, o.y); + + return Integer.compare(x, o.x); + } + + + } + +}",0,Non-plagiarised +f28b8cb4,ff3283cf,"import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.StringTokenizer; + +public class Main { + private static int gcd(int a, int b) { + return b == 0 ? a : gcd(b, a % b); + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader( + new InputStreamReader(System.in)); + + int q = Integer.parseInt(br.readLine()); + + for (int i = 0; i < q; i++) { + br.readLine(); + + StringTokenizer st = new StringTokenizer(br.readLine()); + int n = Integer.parseInt(st.nextToken()); + int k = Integer.parseInt(st.nextToken()); + + // read input a + st = new StringTokenizer(br.readLine()); + int[] a = new int[k]; + for (int j = 0; j < k; j++) { + a[j] = Integer.parseInt(st.nextToken()); + } + + // read input t + st = new StringTokenizer(br.readLine()); + int[] t = new int[k]; + for (int j = 0; j < k; j++) { + t[j] = Integer.parseInt(st.nextToken()); + } + + // known temperatures + int[] c = new int[n]; + Arrays.fill(c, Integer.MAX_VALUE); + for (int j = 0; j < k; j++) { + c[a[j] - 1] = t[j]; + } + + // temperature from left to right + long[] left = new long[n]; + Arrays.fill(left, Integer.MAX_VALUE); + long p = Integer.MAX_VALUE; + for (int j = 0; j < n; j++) { + p = Math.min(p + 1, c[j]); + left[j] = p; + } + + // temperature from right to left + long[] right = new long[n]; + Arrays.fill(right, Integer.MAX_VALUE); + p = Integer.MAX_VALUE; + for (int j = n - 1; j >= 0; j--) { + p = Math.min(p + 1, c[j]); + right[j] = p; + } + + for (int j = 0; j < n; j++) { + System.out.print(Math.min(left[j], right[j]) + "" ""); + } + System.out.println(); + } + } +} +","import java.util.*; +import java.lang.*; +import java.io.*; + +/* Name of the class has to be ""Main"" only if the class is public. */ +public class Codeforces { + public static void main(String[] args) throws java.lang.Exception { + /* your code goes here */ + BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(buf.readLine()); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < t; i++) { + String st=buf.readLine(); + String st1[]=(buf.readLine()).split("" ""); + int n=Integer.parseInt(st1[0]); + int k=Integer.parseInt(st1[1]); + int a[]=new int[k]; + int temp[]=new int[k]; + long arr[]=new long[n]; + String st2[]=(buf.readLine()).split("" ""); + String st3[]=(buf.readLine()).split("" ""); + for(int j=0;j=0;j--) + { + if(arr[j]==0) + { + right[j]=right[j+1]+1; + } + else + { + right[j]=Math.min(right[j+1]+1,arr[j]); + } + } + for(int j=0;j 0) { + t--; + n = sc.nextInt(); + String s1,s2; + s1 = sc.next(); + s2 = sc.next(); + int a[] = new int[4]; + a[0] = 0; a[1] = 0; a[2] = 0; a[3] = 0; + for(int i = 0 ; i < n ; i++) { + if(s1.charAt(i) == '0'&& s2.charAt(i) == '1') a[0]++; + else if(s1.charAt(i) == '1'&& s2.charAt(i) == '0') a[1]++; + else if(s1.charAt(i) == '1'&& s2.charAt(i) == '1') a[2]++; + else a[3]++; + } + // System.out.println(a[0] + "" "" + a[1] + "" "" + a[2] + "" "" + a[3]); + int n1 = Integer.MAX_VALUE, n2 = Integer.MAX_VALUE, n3 = Integer.MAX_VALUE; + + if (a[0] == a[1]) { + n1 = 2*a[0]; + } + if((a[2] - 1) == a[3]) { + // System.out.println(a[3] + 1); + n2 = 2*a[3] + 1; + } + if((a[3] + 1) == a[2]) { + // System.out.println(a[2] + 1); + n3 = 2*a[2] + 1; + } + int ans = Math.min(n1, Math.min(n2,n3)); + if(ans == Integer.MAX_VALUE) { + System.out.println(""-1""); + } else { + System.out.println(ans); + } + } + } +} +","import java.io.*; +import java.util.*; + +public class new1{ + + public static long gcd(long a, long b) + { + if (a == 0) + return b; + + return gcd(b%a, a); + } + public static int count(int[] arr, int[] dest) { + int n = arr.length; + int count = 0; + for(int i = 0; i < n; i++) { + if(arr[i] != dest[i]) count++; + } + return count; + } + + + + + + public static void main(String[] args) throws IOException{ + BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); + FastReader s = new FastReader(); + int t = s.nextInt(); + + + for(int z = 0; z < t; z++) { + int n = s.nextInt(); + String str1 = s.next(); + String str2 = s.next(); + int count1 = 0; int count2 = 0; + for(int i = 0; i < n; i++) { + if(str1.charAt(i) == '1') count1++; + if(str2.charAt(i) == '1') count2++; + } + if(!(count1 == count2 || n - count1 + 1 == count2)) { + System.out.println(-1); + continue; + } + + int[] arr = new int[n]; + int[] dest = new int[n]; + for(int i = 0; i < n; i++) { + arr[i] = str1.charAt(i) - '0'; + dest[i] = str2.charAt(i) - '0'; + } + int ans1 = count(arr, dest); + int ans2 = Integer.MAX_VALUE; + int flag = 0; + for(int i = 0; i < n; i++) { + if(flag == 0 && arr[i] == 1 && dest[i] == 1) { + flag = 1; + } + else { + arr[i] = 1 - arr[i]; + } + } + //System.out.println(ans1); + if(flag == 1) { + ans2 = Math.min(ans2, count(arr, dest) + 1); + } + else { + for(int i = 0; i < n; i++) { + if(str1.charAt(i) == '1' && flag == 0) { + flag = 1; arr[i] = 1; + break; + } + } + + } + if(flag == 1) ans2 = Math.min(ans2, count(arr, dest) + 1); + int ans = Integer.MAX_VALUE; + if(count1 == count2) ans = Math.min(ans, ans1); + if(n - count1 + 1 == count2) ans = Math.min(ans, ans2); + System.out.println(ans); + + + + } + //output.flush(); + + } +} + + +class FastReader { + BufferedReader br; + StringTokenizer st; + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + public int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; +}}",0,Non-plagiarised +856a8eda,a3e272af,"/*input +2 +5 2 3 +1 2 3 1 2 +4 3 3 +1 1 2 3 +*/ +import java.io.*; +import java.util.*; + +public class three{ + public static class Pair implements Comparable{ + int min; + int idx; + @Override + public int compareTo(Pair o) { + return min - o.min; + } + } + +public static void main(String[] args) throws Exception { + MyScanner scn = new MyScanner(); + out = new PrintWriter(new BufferedOutputStream(System.out)); + /* + int n = sc.nextInt(); // read input as integer + long k = sc.nextLong(); // read input as long + double d = sc.nextDouble(); // read input as double + String str = sc.next(); // read input as String + String s = sc.nextLine(); // read whole line as String + + int result = 3*n; + out.println(result); // print via PrintWriter + */ + + //The Code Starts here + int t = scn.nextInt(); + while(t-- > 0){ + int n = scn.nextInt(); + int m = scn.nextInt(); + + int x = scn.nextInt(); + int arr[] = scn.nextIntArray(n); + PriorityQueue pq = new PriorityQueue<>(); + System.out.println(""YES""); + + for(int i=0;i { + // long u; + // long v; + + // public Pair(long u, long v) { + // this.u = u; + // this.v = v; + // } + + + // public int hashCode() { + // int hu = (int) (u ^ (u >>> 32)); + // int hv = (int) (v ^ (v >>> 32)); + // return 31 * hu + hv; + // } + + // public boolean equals(Object o) { + // Pair other = (Pair) o; + // return u == other.u && v == other.v; + // } + + // public int compareTo(Pair other) { + // return Long.compare(u, other.u) != 0 ? Long.compare(u, other.u) : Long.compare(v, other.v); + // } + + // public String toString() { + // return ""[u="" + u + "", v="" + v + ""]""; + // } + // } + //-------------------------------------------------------- +} + + + + + + + + + +"," +import java.io.*; +import java.util.*; + +public class Asd { + + static PrintWriter w = new PrintWriter(System.out); + static FastScanner s = new FastScanner(); + static boolean sd = false; + + public static void main(String[] args) { + + int t = s.nextInt(); + //int t=1; + while (t-- > 0) { + solve(); + } + w.close(); + + } + public static class Student { + public int i1; + public int value; + + // A parameterized student constructor + public Student(int i1,int i2) { + + this.i1 = i1; + this.value=i2; + } + + public int getkey() { + return i1; + } + public int getValue() { + return value; + } +} + static class StudentComparator implements Comparator{ + + // Overriding compare()method of Comparator + // for descending order of cgpa + @Override + public int compare(Student s1, Student s2) { + if (s1.i1 < s2.i1) + return -1; + else if (s1.i1 >s2.i1) + return 1; + return 0; + } + } + + /* Function to print all the permutations of the string + static String swap(String str, int i, int j) + { + char ch; + char[] array = str.toCharArray(); + ch = array[i]; + array[i] = array[j]; + array[j] = ch; + return String.valueOf(array); + } + + static void permute(String str,int low,int high) + { + if(low == high) + list.add(Long.parseLong(str)); + + int i; + for(i = low; i<=high; i++){ + str = swap(str,low,i); + permute(str, low+1,high); + str = swap(str,low,i); + } + } + use permute(str2,0,str2.length()-1); to perform combinations + */ + + public static void solve() { + int n=s.nextInt(); + int m=s.nextInt(); + int x=s.nextInt(); + int arr[]=new int[n];int res[]=new int[n]; + for(int i=0;i pq=new PriorityQueue(new StudentComparator()); + for(int i=0;i t, ArrayList m) { + if (t.size() == 0 && m.size() == 0) { + sd = true; + return; + } + t.remove(0); + t.remove(t.size() - 1); + call(t, new ArrayList(m.subList(0, m.size() - 1))); + call(t, new ArrayList(m.subList(1, m.size()))); + } + + static long gcd(long a, long b) { + if (b == 0) { + return a; + } + return gcd(b, a % b); + } + + static int noofdivisors(int n) { + //it counts no of divisors of every number upto number n + + int arr[] = new int[n + 1]; + for (int i = 1; i <= (n); i++) { + for (int j = i; j <= (n); j = j + i) { + arr[j]++; + } + } + return arr[0]; + } + + static char[] reverse(char arr[]) { + char[] b = new char[arr.length]; + int j = arr.length; + for (int i = 0; i < arr.length; i++) { + b[j - 1] = arr[i]; + j = j - 1; + } + return b; + } + + static long factorial(int n) { + long su = 1; + for (int i = 1; i <= n; i++) { + su *= (long) i; + } + return su; + } + + static class FastScanner { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(""""); + + public String next() { + while (!st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + long[] readArray(int n) { + long[] a = new long[n]; + for (int i = 0; i < n; i++) { + a[i] = nextLong(); + } + return a; + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + } + +} +",0,Non-plagiarised +2695ce73,ac8acb97,"import java.io.*; +import java.util.*; + +public class c { +/* +3 +2 +13 88 +3 +2 3 1 +5 +4 3 2 1 4 + +202 +13 +19 + ++ - + - + +1, 2 +2, 1 +3, 3 +*/ + public static void main(String args[]) throws IOException { + FastScanner in = new FastScanner(System.in); + PrintWriter out = new PrintWriter(System.out); + + int t = in.nextInt(); + for ( ; t > 0; t--) { + int n = in.nextInt(); + long[] vals = new long[n]; + for (int i = 0; i < n; i++) + vals[i] = in.nextInt(); + + + // System.out.println(Arrays.toString(pre)); + // sSystem.out.println(Arrays.toString(pre1)); + + long oo = (long)(1e18); + long[] min = {oo, oo}; + long[] sub = {n, n}; + long sum = 0; + long max = oo; + for (int i = 0; i < n; i++) { + min[i % 2] = Math.min(min[i % 2], vals[i]); + sub[i % 2]--; + sum += vals[i]; + if (i > 0) { + max = Math.min(max, sum + min[0] * sub[0] + min[1] * sub[1]); + } + } + out.println(max); + } + + + out.close(); + } + + static class FastScanner { + BufferedReader br; + StringTokenizer st; + + public FastScanner(InputStream i) { + br = new BufferedReader(new InputStreamReader(i)); + st = new StringTokenizer(""""); + } + + public String next() throws IOException { + if(st.hasMoreTokens()) + return st.nextToken(); + else + st = new StringTokenizer(br.readLine()); + return next(); + } + + public int nextInt() throws IOException { + return Integer.parseInt(next()); + } + //# + public long nextLong() throws IOException { + return Long.parseLong(next()); + } + public double nextDouble() throws IOException { + return Double.parseDouble(next()); + } + //$ + } +} + +","import java.util.Scanner; + +public class C1499 { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + int t = in.nextInt(); + while (t-- > 0) { + int n = in.nextInt(); + // int[] arr = new int[n]; + long[] mn = { Long.MAX_VALUE, Long.MAX_VALUE }; + long[] rem = { n, n }; + long sum = 0; + long ans = Long.MAX_VALUE; + for (int i = 0; i < n; i++) { + int temp = in.nextInt(); + mn[i % 2] = Math.min(mn[i % 2], temp); + rem[i % 2]--; + sum += temp; + if (i > 0) { + long cur = sum + rem[0] * mn[0] + rem[1] * mn[1]; + ans = Math.min(ans, cur); + } + } + System.out.println(ans); + +// int a = Integer.MAX_VALUE; +// int aIndex = -1; +// int b = Integer.MAX_VALUE; +// int bIndex = -1; +// +// for (int i = 0; i < n; i++) { +// arr[i] = in.nextInt(); +// if (i % 2 == 0) { +// if (arr[i] < a) { +// a = arr[i]; +// aIndex = i; +// } +// } else { +// if (arr[i] < b) { +// b = arr[i]; +// bIndex = i; +// } +// } +// } +// int sum = 0; +// for (int i = 0; i < Math.max(bIndex, aIndex) + 1; i++) { +// if (i % 2 == 0) { +// if (i == aIndex) { +// if (aIndex < bIndex) { +// sum += (n - (i / 2) - ((bIndex - aIndex) / 2)) * arr[i]; +// } else { +// sum += (n - (i / 2)) * arr[i]; +// } +// } else { +// sum += arr[i]; +// } +// } else { +// if (i == bIndex) { +// if (bIndex < aIndex) { +// sum += (n - (i / 2) - ((aIndex - bIndex) / 2)) * arr[i]; +// } else { +// sum += (n - (i / 2)) * arr[i]; +// } +// } else { +// sum += arr[i]; +// } +// } +// +// } +// System.out.println(sum); +// for (int i = 0; i < n; i++) { +// arr[i] = in.nextInt(); +// if (arr[i] < a) { +// a = arr[i]; +// aIndex = i; +// +// } +// +// } +// if (aIndex == 0) { +// +// for (int i = 1; i < n; i++) { +// if (arr[i] < b) { +// b = arr[i]; +// bIndex = i; +// } +// } +// System.out.println(aIndex + "" "" + bIndex); +// System.out.println(a + "" "" + b); +// int sum = 0; +// for (int i = 1; i < bIndex; i++) { +// sum += arr[i]; +// } +// sum += b * (n - bIndex + 1); +// sum += a * n; +// System.out.println(sum); +// } else { +// int b = Integer.MAX_VALUE; +// int bIndex = -1; +// for (int i = 0; i < aIndex; i++) { +// if (arr[i] < b) { +// b = arr[i]; +// bIndex = i; +// } +// } +// System.out.println(aIndex + "" "" + bIndex); +// System.out.println(a + "" "" + b); +// int sum = 0; +// for (int i = 0; i < bIndex; i++) { +// sum += arr[i]; +// } +// sum += b * (n - bIndex); +// sum += a * n; +// System.out.println(sum); +// } + + } + } +} + +",1,Plagiarised +5f3a2a70,e7a997b5,"import java.io.DataInputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.util.ArrayList; + +public class Main { + + private static Node[] nodes; + private static boolean[] visited; + + private static void run() throws IOException { + int n = in.nextInt(); + + nodes = new Node[n]; + for (int i = 0; i < n; i++) { + nodes[i] = new Node(); + } + + Edge[] edges = new Edge[n - 1]; + + for (int i = 0; i < n - 1; i++) { + edges[i] = new Edge(); + edges[i].id = i; + edges[i].u = in.nextInt() - 1; + edges[i].v = in.nextInt() - 1; + + nodes[edges[i].u].edge.add(edges[i]); + nodes[edges[i].v].edge.add(edges[i]); + } + + for (int i = 0; i < n; i++) { + if (nodes[i].edge.size() >= 3) { + out.println(-1); + return; + } + } + + int end_pos = 0; + visited = new boolean[n]; + while (nodes[end_pos].edge.size() != 1) { + end_pos++; + } + + visited = new boolean[n]; + dfs(end_pos, 2); + + for (int i = 0; i < n - 1; i++) { + out.print(edges[i].ans); + out.print(' '); + } + out.println(); + } + + private static void dfs(int now, int d) { + visited[now] = true; + for (Edge edge : nodes[now].edge) { + if (visited[edge.get_next(now)]) { + continue; + } + + edge.ans = d; + dfs(edge.get_next(now), d ^ 1); + } + } + + private static class Node { + ArrayList edge = new ArrayList<>(); + } + + private static class Edge { + int id, u, v, ans = -1; + + int get_next(int now) { + return now != u ? u : v; + } + } + + public static void main(String[] args) throws IOException { + in = new Reader(); + out = new PrintWriter(new OutputStreamWriter(System.out)); + + int t = in.nextInt(); + for (int i = 0; i < t; i++) { + run(); + } + + out.flush(); + in.close(); + out.close(); + } + + private static int gcd(int a, int b) { + if (a == 0 || b == 0) + return 0; + while (b != 0) { + int tmp; + tmp = a % b; + a = b; + b = tmp; + } + return a; + } + + static final long mod = 1000000007; + + static long pow_mod(long a, long b) { + long result = 1; + while (b != 0) { + if ((b & 1) != 0) result = (result * a) % mod; + a = (a * a) % mod; + b >>= 1; + } + return result; + } + + private static long add_mod(long... longs) { + long ans = 0; + for (long now : longs) { + ans = (ans + now) % mod; + if (ans < 0) ans += mod; + } + return ans; + } + + private static long multiplied_mod(long... longs) { + long ans = 1; + for (long now : longs) { + ans = (ans * now) % mod; + } + return ans; + } + + @SuppressWarnings(""FieldCanBeLocal"") + private static Reader in; + private static PrintWriter out; + + private static int[] read_int_array(int len) throws IOException { + int[] a = new int[len]; + for (int i = 0; i < len; i++) { + a[i] = in.nextInt(); + } + return a; + } + + private static long[] read_long_array(int len) throws IOException { + long[] a = new long[len]; + for (int i = 0; i < len; i++) { + a[i] = in.nextLong(); + } + return a; + } + + private static void print_array(int[] array) { + for (int now : array) { + out.print(now); + out.print(' '); + } + out.println(); + } + + private static void print_array(long[] array) { + for (long now : array) { + out.print(now); + out.print(' '); + } + out.println(); + } + + static class Reader { + private static final int BUFFER_SIZE = 1 << 16; + private final DataInputStream din; + private final byte[] buffer; + private int bufferPointer, bytesRead; + + Reader() { + din = new DataInputStream(System.in); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public String readLine() throws IOException { + final byte[] buf = new byte[1024]; // line length + int cnt = 0, c; + while ((c = read()) != -1) { + if (c == '\n') { + break; + } + buf[cnt++] = (byte) c; + } + return new String(buf, 0, cnt); + } + + public int nextSign() throws IOException { + byte c = read(); + while ('+' != c && '-' != c) { + c = read(); + } + return '+' == c ? 0 : 1; + } + + private static boolean isSpaceChar(int c) { + return !(c >= 33 && c <= 126); + } + + public int skip() throws IOException { + int b; + // noinspection ALL + while ((b = read()) != -1 && isSpaceChar(b)) { + ; + } + return b; + } + + public char nc() throws IOException { + return (char) skip(); + } + + public String next() throws IOException { + int b = skip(); + final StringBuilder sb = new StringBuilder(); + while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') + sb.appendCodePoint(b); + b = read(); + } + return sb.toString(); + } + + public int nextInt() throws IOException { + int ret = 0; + byte c = read(); + while (c <= ' ') { + c = read(); + } + final boolean neg = c == '-'; + if (neg) { + c = read(); + } + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (neg) { + return -ret; + } + return ret; + } + + public long nextLong() throws IOException { + long ret = 0; + byte c = read(); + while (c <= ' ') { + c = read(); + } + final boolean neg = c == '-'; + if (neg) { + c = read(); + } + do { + ret = ret * 10 + c - '0'; + } + while ((c = read()) >= '0' && c <= '9'); + if (neg) { + return -ret; + } + return ret; + } + + public double nextDouble() throws IOException { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') { + c = read(); + } + final boolean neg = c == '-'; + if (neg) { + c = read(); + } + + do { + ret = ret * 10 + c - '0'; + } + while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') { + while ((c = read()) >= '0' && c <= '9') { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) { + return -ret; + } + return ret; + } + + private void fillBuffer() throws IOException { + bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); + if (bytesRead == -1) { + buffer[0] = -1; + } + } + + private byte read() throws IOException { + if (bufferPointer == bytesRead) { + fillBuffer(); + } + return buffer[bufferPointer++]; + } + + public void close() throws IOException { + din.close(); + } + + } +}","import java.io.*; +import java.util.*; + +public class c { + + public static void main(String[] args){ + + FastScanner sc = new FastScanner(); + int t = sc.nextInt(); + while(t-- > 0){ + + int n = sc.nextInt(); + + + ArrayList> graph = new ArrayList<>(); + for(int i=0; i()); + } + + for(int i=0; i list : graph){ + if(list.size() == 1){ + indegree1count++; + } + else if(list.size() == 2){ + indegree2count++; + } + } + + if(indegree1count == 2 && indegree1count+indegree2count==n){ + + for(int i=0; i list = graph.get(i); + if(list.size() == 1){ + dfs(graph, edges, false, -1, i) ; + } + } + + for(int i=1; i> graph, int[] edges, boolean isprev2, int parent, int current){ + + for(Edge e : graph.get(current)){ + + if(e.v == parent){ + continue; + } + + edges[e.id] = isprev2 ? 5 : 2; + + dfs(graph, edges, !isprev2, current, e.v); + + } + + } + +} + +class Edge { + + int u; + int v; + int id; + + public Edge(int u, int v, int id) { + this.u = u; + this.v = v; + this.id = id; + } + +} + +class FastScanner +{ + //I don't understand how this works lmao + private int BS = 1 << 16; + private char NC = (char) 0; + private byte[] buf = new byte[BS]; + private int bId = 0, size = 0; + private char c = NC; + private double cnt = 1; + private BufferedInputStream in; + + public FastScanner() { + in = new BufferedInputStream(System.in, BS); + } + + public FastScanner(String s) { + try { + in = new BufferedInputStream(new FileInputStream(new File(s)), BS); + } catch (Exception e) { + in = new BufferedInputStream(System.in, BS); + } + } + + private char getChar() { + while (bId == size) { + try { + size = in.read(buf); + } catch (Exception e) { + return NC; + } + if (size == -1) return NC; + bId = 0; + } + return (char) buf[bId++]; + } + + public int nextInt() { + return (int) nextLong(); + } + + public int[] nextInts(int N) { + int[] res = new int[N]; + for (int i = 0; i < N; i++) { + res[i] = (int) nextLong(); + } + return res; + } + + public long[] nextLongs(int N) { + long[] res = new long[N]; + for (int i = 0; i < N; i++) { + res[i] = nextLong(); + } + return res; + } + + public long nextLong() { + cnt = 1; + boolean neg = false; + if (c == NC) c = getChar(); + for (; (c < '0' || c > '9'); c = getChar()) { + if (c == '-') neg = true; + } + long res = 0; + for (; c >= '0' && c <= '9'; c = getChar()) { + res = (res << 3) + (res << 1) + c - '0'; + cnt *= 10; + } + return neg ? -res : res; + } + + public double nextDouble() { + double cur = nextLong(); + return c != '.' ? cur : cur + nextLong() / cnt; + } + + public double[] nextDoubles(int N) { + double[] res = new double[N]; + for (int i = 0; i < N; i++) { + res[i] = nextDouble(); + } + return res; + } + + public String next() { + StringBuilder res = new StringBuilder(); + while (c <= 32) c = getChar(); + while (c > 32) { + res.append(c); + c = getChar(); + } + return res.toString(); + } + + public String nextLine() { + StringBuilder res = new StringBuilder(); + while (c <= 32) c = getChar(); + while (c != '\n') { + res.append(c); + c = getChar(); + } + return res.toString(); + } + + public boolean hasNext() { + if (c > 32) return true; + while (true) { + c = getChar(); + if (c == NC) return false; + else if (c > 32) return true; + } + } +}",0,Non-plagiarised +2ff0355e,83935617,"import java.io.*; +import java.util.*; + +public class PhoenixAndTowers { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + + static class Pair implements Comparable { + int val, idx; + Pair (int val, int idx) { this.val = val; this.idx = idx; } + public int compareTo(Pair p) { + if (val == p.val) return Integer.compare(idx, p.idx); + return Integer.compare(val, p.val); + } + } + public static void main(String[] args) throws IOException { + int T = readInt(); + for (int t = 0; t < T; t ++) { + int n = readInt(), m = readInt(), x = readInt(); + Pair[] h = new Pair[n + 1]; + h[0] = new Pair(100000, 0); + for (int i = 1; i <= n; i ++) h[i] = new Pair(readInt(), i); + Arrays.sort(h, Collections.reverseOrder()); + + int[] ans = new int[n + 1], sum = new int[m + 1]; + + PriorityQueue q = new PriorityQueue(); + for (int i = 1; i <= m; i ++) q.add(new Pair(0, i)); + for (int i = 1; i <= n; i ++) { + Pair p = q.poll(); + sum[p.idx] += h[i].val; + ans[h[i].idx] = p.idx; + q.add(new Pair(p.val + h[i].val, p.idx)); + } + + + int max = 0, min = Integer.MAX_VALUE; + for (int i = 1; i <= m; i ++) { + max = Math.max(max, sum[i]); + min = Math.min(min, sum[i]); + } + + if (max - min > x) System.out.println(""NO""); + else { + System.out.println(""YES""); + for (int i = 1; i < n; i ++) System.out.print(ans[i] + "" ""); + System.out.println(ans[n]); + } + } + } + + static String next() throws IOException { + while (st == null || !st.hasMoreTokens()) + st = new StringTokenizer(br.readLine().trim()); + return st.nextToken(); + } + static long readLong() throws IOException { + return Long.parseLong(next()); + } + static int readInt() throws IOException { + return Integer.parseInt(next()); + } + static double readDouble() throws IOException { + return Double.parseDouble(next()); + } + static char readCharacter() throws IOException { + return next().charAt(0); + } + static String readLine() throws IOException { + return br.readLine().trim(); + } +}","import java.util.*; +import java.lang.*; +import java.io.*; + +// Created by @thesupremeone on 02/05/21 +public class C { + static class Tower implements Comparable{ + int index; + int height = 0; + LinkedList blocks; + public Tower(int index) { + this.index = index; + blocks = new LinkedList<>(); + } + void addBlock(int block){ + blocks.add(block); + height += block; + } + @Override + public int compareTo(Tower tower) { + if(height!=tower.height) + return Integer.compare(height, tower.height); + return Integer.compare(index, tower.index); + } + } + + void solve() throws IOException { + int ts = getInt(); + for (int t = 1; t <= ts; t++){ + int n = getInt(); + int m = getInt(); + int x = getInt(); + Integer[] h = new Integer[n]; + Integer[] org = new Integer[n]; + for (int i = 0; i < n; i++){ + h[i] = getInt(); + org[i] = h[i]; + } + if(m==1){ + println(""YES""); + for (int i = 0; i < n; i++) { + print(""1 ""); + } + println(""""); + continue; + } + TreeSet towers = new TreeSet<>(); + for (int i = 0; i < m; i++){ + Tower tower = new Tower(i+1); + towers.add(tower); + } + Arrays.sort(h, Comparator.reverseOrder()); + for (int i = 0; i < n; i++) { + Tower tower = towers.pollFirst(); + if(tower!=null){ + tower.addBlock(h[i]); + towers.add(tower); + } + } + Tower first = towers.first(); + Tower last = towers.last(); + int diff = Math.abs(first.height-last.height); + if(diff<=x){ + println(""YES""); + HashMap> map = new HashMap<>(); + for (Tower tower : towers){ + for(int block : tower.blocks){ + LinkedList list; + if(map.containsKey(block)){ + list = map.get(block); + }else { + list = new LinkedList<>(); + map.put(block, list); + } + list.add(tower.index); + } + } + for (int i = 0; i < n; i++) { + int block = org[i]; + LinkedList list = map.get(block); + if(!list.isEmpty()){ + int e = list.pollFirst();; + print(e+"" ""); + } + } + println(""""); + }else { + println(""NO""); + } + } + } + + public static void main(String[] args) throws Exception { + if (isOnlineJudge()) { + in = new BufferedReader(new InputStreamReader(System.in)); + out = new BufferedWriter(new OutputStreamWriter(System.out)); + new C().solve(); + out.flush(); + } else { + Thread judge = new Thread(); + in = new BufferedReader(new FileReader(""input.txt"")); + out = new BufferedWriter(new FileWriter(""output.txt"")); + judge.start(); + new C().solve(); + out.flush(); + judge.suspend(); + } + } + static boolean isOnlineJudge(){ + try { + return System.getProperty(""ONLINE_JUDGE"")!=null + || System.getProperty(""LOCAL"")==null; + }catch (Exception e){ + return true; + } + } + // Fast Input & Output + static BufferedReader in; + static StringTokenizer st; + static BufferedWriter out; + static String getLine() throws IOException{ + return in.readLine(); + } + static String getToken() throws IOException{ + if(st==null || !st.hasMoreTokens()) + st = new StringTokenizer(getLine()); + return st.nextToken(); + } + static int getInt() throws IOException { + return Integer.parseInt(getToken()); + } + static long getLong() throws IOException { + return Long.parseLong(getToken()); + } + static void print(Object s) throws IOException{ + out.write(String.valueOf(s)); + } + static void println(Object s) throws IOException{ + out.write(String.valueOf(s)); + out.newLine(); + } +}",0,Non-plagiarised +04df7bb8,ee270b2a," import java.math.BigInteger; + import java.sql.Array; + import java.util.ArrayList; + import java.util.Arrays; + import java.util.Collection; + import java.util.Collections; + import java.util.Comparator; + import java.util.Deque; + import java.util.HashMap; + import java.util.HashSet; + import java.util.InputMismatchException; + import java.util.LinkedList; + import java.util.PriorityQueue; + import java.util.Queue; + import java.util.Scanner; + import java.util.Set; + import java.util.Stack; + import java.io.BufferedReader; + import java.io.IOException; + import java.io.InputStream; + import java.io.InputStreamReader; + import java.util.Scanner; + import java.util.StringTokenizer; + import java.util.TreeMap; + import java.util.TreeSet; + + + + + + + public class Main { + + + static HashMapmap; + + static long dp[][]; + static boolean flag; + static HashSeths; + static long mod=(long)(1e9+7); + public static void main(String[] args) { + StringBuilder ans=new StringBuilder(); + FastReader sc=new FastReader(); + + + int t=sc.nextInt(); + + + while(t-->0) { + + int n=sc.nextInt(); + + + //int n=sb.length(); + + + + int k=sc.nextInt(); + + + long L[]=new long[n]; + long R[]=new long[n]; + int a[]=new int[k]; + int temp[]=new int[k]; + + for(int i=0;i=0;i--) + { + p=Math.min(p+1, c[i]); + + R[i]=p; + + + } + + for(int i=0;i>adj,int d[],boolean visited[]) { + + + visited[v]=true; + for(int u:adj.get(v)) { + + if(!visited[u]) + { + solve(u, adj, d, visited); + + } + d[v]=Math.max(1+d[u], d[v]); + } + + + return d[v]; + + + } + + + + + static class colors{ + + int c;int n; + public colors(int c,int n) { + // TODO Auto-generated constructor stub + this.c=c; + this.n=n; + } + + } + + + + + static int CeilIndex(long A[], int l, int r, long key) + { + while (r - l > 1) { + int m = l + (r - l) / 2; + if (A[m] >= key) + r = m; + else + l = m; + } + + return r; + } + + static int CeilIndexd(long A[], int l, int r, long key) + { + while (r - l > 1) { + int m = l + (r - l) / 2; + if (-1*A[m] >= key) + r = m; + else + l = m; + } + + return r; + } + + + + + + + static void solve(ArrayListA,long bd) { + + if(bd>(long)1e9)return; + // if(hs.contains(bd))return; + //A.add(bd); + hs.add(bd); + A.add(bd*10); + A.add(bd*10+1); + // hs.add(bd*10); + //hs.add(bd*10+1); + + solve(A,bd*10); + solve(A,bd*10+1); + + + } + + + + + + static boolean isPrime(int n) + { + // Corner cases + if (n <= 1) + return false; + if (n <= 3) + return true; + + // This is checked so that we can skip + // middle five numbers in below loop + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; + } + + static long lcm(long a, long b) + { + return ((a / gcd(a, b))%mod * b%mod)%mod; + } + static long gcd(long a,long b) + { + if (a == 0) + return b%mod; + return (gcd(b % a, a))%mod; + } + + + //System.out.println(count); + + + + + static void dfs(int v,boolean visited[],ArrayList>adj,int div[],int t) + { + visited[v]=true; + + div[v]=t+1; + for(int u:adj.get(v)) { + + + + if(!visited[u]) { + dfs(u,visited,adj,div,(t+1)%2); + } + + } + + + } + + + static class Helper{ + + int a;int b;int t; + public Helper(int a,int b) { + // TODO Auto-generated constructor stub + + this.a=a; + this.b=b; + + + + } + + + } + + + + + + + + + // System.out.println(max); + + + + + + + //System.out.println(ans.toString()); + //main( + + + + static void solvedfs(ArrayList>adj,int n,int v,int subt[],int subtAns[],boolean []visited) { + + + int count=1; + int ans=0; + visited[v]=true; + for(int u:adj.get(v)) { + + + if(!visited[u]) + { + //System.out.println(v+"" ""+subt[v]+"" ""+n); + subtAns[u]=Math.max(subtAns[u], subt[v]-subtAns[u]); + + solvedfs(adj, n, u, subt, subtAns, visited); + + } + } + + + + + + + + } + + + + + static int dfs(ArrayList>adj,int v,int subt[],int subtAns[],boolean []visited) { + + + int count=0; + int ans=0; + visited[v]=true; + for(int u:adj.get(v)) { + + + if(!visited[u]) + { + count+=ans; + ans=Math.max(dfs(adj,u,subt,subtAns,visited),ans); + } + } + subt[v]=count; + subtAns[v]=ans; + return ans+1; + + + + + + + } + + + + + static int solve(ArrayList>adj,int node,ArrayListA) + { + if(adj.get(node).size()==0)return 1; + + int count=0; + for(int v:adj.get(node)) { + + count+=solve(adj,v,A); + + } + + A.set(node,count); + return count+1; + + + + } + + + + + + + + static void dfs(String[]building,int i,int j,int n,int m, boolean visited[][]) { + + + visited[i][j]=true; + + if(isValid(building,i+1,j,n,m,visited)) + { visited[i+1][j]=true; + dfs(building,i+1,j,n,m,visited); + + } + if(isValid(building,i-1,j,n,m,visited)) + { + visited[i-1][j]=true; + dfs(building,i-1,j,n,m,visited); + } + if(isValid(building,i,j+1,n,m,visited)) + {visited[i][j+1]=true; + dfs(building,i,j+1,n,m,visited); + } + if(isValid(building,i,j-1,n,m,visited)) + {visited[i][j-1]=true; + dfs(building,i,j-1,n,m,visited); + } + + + + + } + static boolean isValid(String[]building,int i,int j,int n,int m, boolean visited[][]) + { + if(i==-1||j==-1||i==n||j==m||visited[i][j]||building[i].charAt(j)=='#') + return false; + return true; + } + + + + + static void compute(boolean sieve[],int n) { + + for(int i=2;i<=n;i++) { + if(sieve[i])continue; + + for(int j=2*i;js.length())return false; + HashSeths=new HashSet(); + int a[]=new int[3]; + + for(int i=0;i0&&a[1]>0&&a[2]>0)return true; + + int start=0; + int end=w; + + while(end!=s.length()) { + --a[s.charAt(start)-49]; + + ++a[s.charAt(end)-49]; + + start++; + end++; + if(a[0]>0&&a[1]>0&&a[2]>0)return true; + + } + return false; + + + + } + + + + + + + + static int find(int parent[],int i) { + + if(parent[i]==-1)return i; + + return parent[i]=find(parent,parent[i]); + + } + static void union(int parent[],int rank[],int s1,int s2) { + + if(rank[s1]>=rank[s2]) { + + parent[s2]=s1; + rank[s1]+=rank[s2]; + } + + else { + + parent[s1]=s2; + rank[s2]+=rank[s1]; + } + + + } + + + + + + + + + + + + static int solve(String S,int K) { + + if(K<=0)return 0; + if(S.charAt(K-1)!=S.charAt(K)) + return 1+solve(S,K-1); + else return solve(S,K-1); + } + + + + + + + + static boolean isValid(int g[][],int r,int c,int n,int m,boolean visited[][],int s) { + if(r==-1||r==n||c==-1||c==m||visited[r][c]||g[r][c]!=s)return false; + return true; + } + + + + + + static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader() + { + br = new BufferedReader( + new InputStreamReader(System.in)); + } + + String next() + { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { return Integer.parseInt(next()); } + + long nextLong() { return Long.parseLong(next()); } + + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str = """"; + try { + str = br.readLine(); + } + catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } + + + } + + + + + ","import java.util.*; + +public class D{ + private static Scanner scanner = new Scanner(System.in); + public static void main(String[] args){ + int q = scanner.nextInt(); + while(q-- > 0){ + int n = scanner.nextInt(), + k = scanner.nextInt(); + int[] a = new int[k]; + for(int i=0;i=0;i--){ + R[i] = Math.min(min+1,R[i]); + min = R[i]; + } + for(int i=0;i0) { + int n = sc.nextInt(); + int[] arr = new int[n]; + for(int i = 0; i < n; i++) { + arr[i] = sc.nextInt(); + } + if(check(arr)) System.out.println(""YES""); + else System.out.println(""NO""); + } + + } + static boolean check(int[] arr) { + int n = arr.length; + TreeSet set = new TreeSet<>(); + set.add(arr[0]); + for(int i = 1; i < n; i++) { + set.add(arr[i]); + if(arr[i-1] == arr[i]) continue; + Integer x = set.lower(arr[i]); + if(x != null && x == arr[i-1]) continue; + x = set.higher(arr[i]); + if(x != null && x == arr[i-1]) continue; + return false; + } + return true; + } + + static class FastScanner { + public BufferedReader reader; + public StringTokenizer tokenizer; + public FastScanner() { + reader = new BufferedReader(new InputStreamReader(System.in), 32768); + tokenizer = null; + } + public String next() { + while (tokenizer == null || !tokenizer.hasMoreTokens()) { + try { + tokenizer = new StringTokenizer(reader.readLine()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + return tokenizer.nextToken(); + } + public int nextInt() { + return Integer.parseInt(next()); + } + public long nextLong() { + return Long.parseLong(next()); + } + public double nextDouble() { + return Double.parseDouble(next()); + } + public String nextLine() { + try { + return reader.readLine(); + } catch(IOException e) { + throw new RuntimeException(e); + } + } + } + +} +","import java.io.*; +import java.util.*; +import java.math.*; +import java.math.BigInteger; +//import javafx.util.*; +public final class B +{ + static StringBuilder ans=new StringBuilder(); + static FastReader in=new FastReader(); + static ArrayList> g; + static long mod=1000000007; + static int D1[],D[],par[]; + static boolean set[]; + static long INF=Long.MAX_VALUE; + public static void main(String args[])throws IOException + { + + int T=i(); + while(T-->0) + { + int N=i(); + long A[]=inputLong(N); + boolean f=true; + TreeSet set=new TreeSet<>(); + for(int i=0; i=2) + { + if(A[i]A[i-1]) + { + if(set.floor(A[i]-1)!=A[i-1])f=false; + } + } + set.add(A[i]); + } + if(f)System.out.println(""YES""); + else System.out.println(""NO""); + } + } + + static int f2(int l,int r,int index) + { + while(r-l>1) + { + int m=(l+r)/2; + if(ask(index,m)==index)r=m; + else l=m; + } + return r; + } + static long fact(long N) + { + long num=1L; + while(N>=1) + { + num=((num%mod)*(N%mod))%mod; + N--; + } + return num; + } + static boolean reverse(long A[],int l,int r) + { + while(l l=new ArrayList<>(); + for (long i:a) l.add(i); + Collections.sort(l); + for (int i=0; i>(); + for(int i=0; i<=N; i++) + { + D[i]=N+1; + g.add(new ArrayList()); + } + } + + static long pow(long a,long b) + { + long mod=1000000007; + long pow=1; + long x=a; + while(b!=0) + { + if((b&1)!=0)pow=(pow*x)%mod; + x=(x*x)%mod; + b/=2; + } + return pow; + } + static long toggleBits(long x)//one's complement || Toggle bits + { + int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; + + return ((1< l=new ArrayList<>(); + for (int i:a) l.add(i); + Collections.sort(l); + for (int i=0; i A) + { + for(int a:A)System.out.print(a+"" ""); + System.out.println(); + } + + //Debugging Functions END + //---------------------- + //IO FUNCTIONS STARTS + static HashMap Hash(int A[]) + { + HashMap mp=new HashMap<>(); + for(int a:A) + { + int f=mp.getOrDefault(a,0)+1; + mp.put(a, f); + } + return mp; + } + static int i() + { + return in.nextInt(); + } + + static long l() + { + return in.nextLong(); + } + + static int[] input(int N){ + int A[]=new int[N]; + for(int i=0; i{ +// int index; long a; +// pair(long a,int index) +// { +// this.a=a; +// this.index=index; +// } +// public int compareTo(pair X) +// { +// if(this.a>X.a)return 1; +// if(this.a==X.a)return this.index-X.index; +// return -1; +// } +//} + +//Code For FastReader +//Code For FastReader +//Code For FastReader +//Code For FastReader +class FastReader +{ + BufferedReader br; + StringTokenizer st; + public FastReader() + { + br=new BufferedReader(new InputStreamReader(System.in)); + } + + String next() + { + while(st==null || !st.hasMoreElements()) + { + try + { + st=new StringTokenizer(br.readLine()); + } + catch(IOException e) + { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() + { + return Integer.parseInt(next()); + } + + long nextLong() + { + return Long.parseLong(next()); + } + //gey + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str=""""; + try + { + str=br.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + return str; + } + +}",0,Non-plagiarised +3d06b643,6bcc5afd,"import java.io.*; +import java.util.*; + +public class ArmChairs { + + + public static int solution(int n, int[] arr) { + + ArrayList one = new ArrayList(); + ArrayList zero = new ArrayList(); + + for (int i = 0; i < n; i++) { + if (arr[i] == 1) { + one.add(i); + } else { + zero.add(i); + } + } + + int[][] dp = new int[one.size() + 1][zero.size() + 1]; + + for (int i = 1; i <= one.size(); i++) { + dp[i][i] = dp[i - 1][i - 1] + Math.abs(one.get(i - 1) - zero.get(i - 1)); + for (int j = i + 1; j <= zero.size(); j++) { + dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j - 1] + Math.abs(one.get(i - 1) - zero.get(j - 1))); + } + } + + return dp[one.size()][zero.size()]; + } + + + public static void main(String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); + + int n = Integer.parseInt(br.readLine()); + + String[] s = br.readLine().split("" ""); + int[] arr = new int[n]; + for (int i = 0; i < n; i++) { + arr[i] = Integer.parseInt(s[i]); + } + + log.write(Integer.toString(solution(n, arr)) + ""\n""); + log.flush(); + + } + +} +","import java.util.*; +public class MyClass { + public static void main(String args[]) { + Scanner s=new Scanner(System.in); + int n=s.nextInt(); + int a[]=new int[n]; + ArrayList lt1=new ArrayList<>(); + ArrayList lt0=new ArrayList<>(); + for(int i=0;i 0) { + int n = Integer.parseInt(f.readLine()); + int[][] occ = new int[n][6]; + for(int i = 0; i < n; i++) { + char[] temp = f.readLine().toCharArray(); + for(char j: temp) { + occ[i][j-'a']++; + } + occ[i][5] = occ[i][0]+occ[i][1]+occ[i][2]+occ[i][3]+occ[i][4]; + } + int max = 0; + for(int i = 0; i < 5; i++) { + int[] temp = new int[n]; + for(int j = 0; j < n; j++) { + temp[j] = occ[j][i]-(occ[j][5]-occ[j][i]); + } + Arrays.sort(temp); + int j; + int cur = 0; + for(j = n-1; j >= 0; j--) { + if(cur+temp[j] <= 0) { + break; + } + cur += temp[j]; + } + max = Math.max(max, n-j-1); + } + out.println(max); + } + f.close(); + out.close(); + } +}","import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.StringTokenizer; +import java.util.TreeMap; + + +public class C_CF { + + public static void main(String[] args) { + FastScanner57 fs = new FastScanner57(); + PrintWriter pw = new PrintWriter(System.out); + int t = fs.ni(); + //int t = 1; + for (int tc = 0; tc < t; tc++) { + int n = fs.ni(); + String[] s = new String[n]; + for (int i = 0; i < n; i++) { + s[i] = fs.next(); + } + int res = c(0,s); + for (int i = 1; i < 5; i++) { + res = Math.max(res,c(i,s)); + } + pw.println(res); + } + + pw.close(); + + } + public static int c(int l, String[] s) { + List list = new ArrayList(); + for (int i = 0; i < s.length; i++) { + String t = s[i]; + int ct = 0; + for (int j = 0; j < t.length(); j++) { + if (t.charAt(j)-'a'==l) { + ct++; + } else { + ct--; + } + } + list.add(ct); + } + Collections.sort(list); + Collections.reverse(list); + int sum = 0; + for (int i = 0; i < s.length; i++) { + sum += list.get(i); + if (sum<=0) return i; + } + return list.size(); + } + public static int gcd(int n1, int n2) { + if (n2 == 0) { + return n1; + } + return gcd(n2, n1 % n2); + } + + static class BIT18 { + + int[] bit; + + public BIT18(int size) { + bit = new int[size]; + } + + public void update(int ind, int delta) { + while (ind < bit.length) { + bit[ind] += delta; + ind = ind + (ind & (-1 * ind)); + } + } + + public int sum(int ind) { + int s = 0; + while (ind > 0) { + s += bit[ind]; + ind = ind - (ind & (-1 * ind)); + } + return s; + } + + public int query(int l, int r) { + return sum(r) - sum(l); + } + } + + // 0 -> left was chosen + // 1 -> right was chosen + + public static void sort(long[] a) { + List list = new ArrayList(); + for (int i = 0; i < a.length; i++) { + list.add(a[i]); + } + Collections.sort(list); + for (int i = 0; i < a.length; i++) { + a[i] = list.get(i); + } + } + + public static long gcd(long n1, long n2) { + if (n2 == 0) { + return n1; + } + return gcd(n2, n1 % n2); + } + +} + +class UnionFind16 { + + int[] id; + + public UnionFind16(int size) { + id = new int[size]; + for (int i = 0; i < size; i++) { + id[i] = i; + } + } + + public int find(int p) { + int root = p; + while (root != id[root]) { + root = id[root]; + } + while (p != root) { + int next = id[p]; + id[p] = root; + p = next; + } + return root; + } + + public void union(int p, int q) { + int a = find(p), b = find(q); + if (a == b) { + return; + } + id[b] = a; + } +} + +class FastScanner57 { + + BufferedReader br; + StringTokenizer st; + + public FastScanner57() { + br = new BufferedReader(new InputStreamReader(System.in), 32768); + st = null; + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int ni() { + return Integer.parseInt(next()); + } + + int[] intArray(int N) { + int[] ret = new int[N]; + for (int i = 0; i < N; i++) { + ret[i] = ni(); + } + return ret; + } + + long nl() { + return Long.parseLong(next()); + } + + long[] longArray(int N) { + long[] ret = new long[N]; + for (int i = 0; i < N; i++) { + ret[i] = nl(); + } + return ret; + } + + double nd() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } +} +",0,Non-plagiarised +53196443,e496d229,"import java.lang.*; +import java.util.*; +import java.io.*; + +public class Main { + static FastScanner in = new FastScanner(); + + static void solve() { + int n = in.nextInt(); + long[] a = new long[n], odd = new long[n], even = new long[n]; + long[] sum = new long[n]; + long m1 = Long.MAX_VALUE, m2 = Long.MAX_VALUE; + long st = 0; + + for (int i = 0; i < n; ++i) { + a[i] = in.nextLong(); + if (i % 2 == 0 && a[i] < m1) + m1 = a[i]; + if (i % 2 == 1 && a[i] < m2) + m2 = a[i]; + st += a[i]; + odd[i] = m1; even[i] = m2; + sum[i] = st; + } + + long ans = Long.MAX_VALUE; + for (int i = 1; i < n; ++i) { + long aux = sum[i] + odd[i] * (n - i - 1 + (i + 1) / 2) + even[i] * (n - (i + 1) / 2); + if (aux < ans && aux > 0) + ans = aux; + } + + System.out.println(ans); + } + + public static void main(String[] args) { + int T = in.nextInt(); + while (T-- > 0) + solve(); + } + + static class Pair { + X x; + Y y; + + public Pair(X x, Y y) { + this.x = x; + this.y = y; + } + } +} + +class FastScanner { + BufferedReader br; + StringTokenizer st; + + public FastScanner() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } +} + +","import java.util.*; +//import java.util.Stack; + + +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(); + long[] a=new long[n]; + long[] odd=new long[n]; + long[] even=new long[n]; + long[] sum=new long[n]; + long m1=Long.MAX_VALUE; + long m2=Long.MAX_VALUE; + long st=(long)0; + for(int i=0;ia[i]) + m1=a[i]; + if(i%2==1&&m2>a[i]) + m2=a[i]; + st+=a[i]; + odd[i]=m1; + even[i]=m2; + sum[i]=st; + } + long minc=Long.MAX_VALUE; + for(int i=1;i0) + minc=c; + } + System.out.println(minc); + } + } +} +",1,Plagiarised +016510dc,f1540246,"import java.io.OutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.StringTokenizer; +import java.io.IOException; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.InputStream; + +public class TaskC { + public static void main(String[] args) { + InputStream inputStream = System.in; + OutputStream outputStream = System.out; + FastReader in = new FastReader(inputStream); + PrintWriter out = new PrintWriter(outputStream); + Solver solver = new Solver(); + int testCount = Integer.parseInt(in.next()); + for (int i = 1; i <= testCount; i++) + solver.solve(i, in, out); + out.close(); + } + + static class Solver { + public void solve(int testNumber, FastReader in, PrintWriter out) { + int n = in.nextInt(),m = in.nextInt(); + int[][] a = new int[n][m]; + for(int i=0;i ans = new ArrayList<>(); + for(int i=0;i<=n-2;i++){ + for(int j=0;j<=m-2;j++) { + int cnt = 0; + if (a[i][j] == 1) cnt++; + if (a[i + 1][j] == 1) cnt++; + if(a[i][j+1]==1) cnt++; + if(a[i+1][j+1]==1) cnt++; + if(cnt==4){ + a[i][j] = 0; + a[i+1][j] = 0; + a[i+1][j+1] = 0; + ans.add(new Pair((i+1),(j+1))); + ans.add(new Pair((i+2),(j+1))); + ans.add(new Pair((i+2),(j+2))); +// out.println(); + cnt = 1; + } + if(cnt==1){ + boolean flag = false; + for(int x=0;x<2;x++){ + for(int y=0;y<2;y++){ + if(a[i+x][j+y]==1){ +// out.print((i+1+x)+"" ""+(j+1+y)+"" ""); + ans.add(new Pair((i+1+x),(j+1+y))); + int cnt2 = 1; + for(int sx=0;sx<2;sx++){ + for(int sy=0;sy<2;sy++){ + if(a[i+sx][j+sy]!=1){ +// out.print((i+sx+1)+"" ""+(j+1+sy)+"" ""); + a[i+sx][j+sy]=1; + ans.add(new Pair((i+sx+1),(j+sy+1))); + cnt2++; + if(cnt2==3){ + a[i+x][j+y]=0; + flag = true; + break; + } + } + } + if(flag) break; + } + if(flag) break; + } + if(flag) break; + } + if(flag) break; + } +// out.println(); + cnt = 2; + } + if(cnt==2){ + boolean flag = false; + for(int x=0;x<2;x++){ + for(int y=0;y<2;y++){ + if(a[i+x][j+y]==1){ +// out.print((i+1+x)+"" ""+(j+1+y)+"" ""); + ans.add(new Pair((i+1+x),(j+1+y))); + int cnt2 = 1; + for(int sx=0;sx<2;sx++){ + for(int sy=0;sy<2;sy++){ + if(a[i+sx][j+sy]!=1){ +// out.print((i+sx+1)+"" ""+(j+1+sy)+"" ""); + cnt2++; + ans.add(new Pair((i+sx+1),(j+1+sy))); + a[i+sx][j+sy]=1; + if(cnt2==3){ + a[i+x][j+y]=0; + flag = true; + break; + } + } + } + if(flag) break; + } + if(flag) break; + } + if(flag) break; + } + if(flag) break; + } +// out.println(); + cnt = 3; + } + if(cnt==3){ + for(int x=0;x<2;x++){ + for(int y=0;y<2;y++){ + if(a[i+x][j+y]==1){ + a[i+x][j+y]=0; + ans.add(new Pair((i+1+x),(j+1+y))); +// out.print((i+1+x)+"" ""+(j+1+y)+"" ""); + } + } + } +// out.println(); + } + } + } + out.println((ans.size())/3); + for(int i=0;i> list = new ArrayList<>(); + for(int i = 0; i < n - 1; i++) { + for(int j = 0; j < m - 1; j++) { + list.addAll(change(g, i, j)); + } + } + out.println(list.size()); + for(int i = 0; i < list.size(); i++) { + for(int j = 0; j < 6; j++) { + out.print(list.get(i).get(j) + "" ""); + } + out.println(); + } + } + out.close(); + } + + static List> change(char[][] g, int i, int j) { + List> list = new ArrayList<>(); + int cnt = 0; + cnt += g[i][j] - '0'; + cnt += g[i][j + 1] - '0'; + cnt += g[i + 1][j] - '0'; + cnt += g[i + 1][j + 1] - '0'; + if(cnt != 0) { + if(cnt == 4) { + List op = new ArrayList<>(); + op.add(i + 1); op.add(j + 1 + 1); op.add(i + 1 + 1); op.add(j + 1); op.add(i + 1 + 1); op.add(j + 1 + 1); + list.add(op); + g[i][j + 1] = '0'; + g[i + 1][j] = '0'; + g[i + 1][j + 1] = '0'; + cnt = 1; + } + if(cnt == 1) { + List op = new ArrayList<>(); + int zeroCnt = 0; + for(int k1 = 0; k1 < 2; k1++) { + for(int k2 = 0; k2 < 2; k2++) { + if(g[i + k1][j + k2] == '0' && zeroCnt < 2) { + op.add(i + k1 + 1); + op.add(j + k2 + 1); + zeroCnt++; + } + else if(g[i + k1][j + k2] == '1'){ + op.add(i + k1 + 1); + op.add(j + k2 + 1); + } + } + } + list.add(op); + zeroCnt = 0; + for(int k1 = 0; k1 < 2; k1++) { + for(int k2 = 0; k2 < 2; k2++) { + if(g[i + k1][j + k2] == '0' && zeroCnt < 2) { + g[i + k1][j + k2] = '1'; + zeroCnt++; + } + else if(g[i + k1][j + k2] == '1'){ + g[i + k1][j + k2] = '0'; + } + } + } + cnt = 2; + } + if(cnt == 2) { + List op = new ArrayList<>(); + int oneCnt = 0; + for(int k1 = 0; k1 < 2; k1++) { + for(int k2 = 0; k2 < 2; k2++) { + if(g[i + k1][j + k2] == '0') { + op.add(i + k1 + 1); + op.add(j + k2 + 1); + } + else if(oneCnt < 1){ + op.add(i + k1 + 1); + op.add(j + k2 + 1); + oneCnt++; + } + } + } + list.add(op); + oneCnt = 0; + for(int k1 = 0; k1 < 2; k1++) { + for(int k2 = 0; k2 < 2; k2++) { + if(g[i + k1][j + k2] == '0') { + g[i + k1][j + k2] = '1'; + } + else if(oneCnt < 1){ + g[i + k1][j + k2] = '0'; + oneCnt++; + } + } + } + cnt = 3; + } + if(cnt == 3) { + List op = new ArrayList<>(); + for(int k1 = 0; k1 < 2; k1++) { + for(int k2 = 0; k2 < 2; k2++) { + if(g[i + k1][j + k2] == '1') { + op.add(i + k1 + 1); + op.add(j + k2 + 1); + } + } + } + list.add(op); + } + for(int k1 = 0; k1 < 2; k1++) { + for(int k2 = 0; k2 < 2; k2++) { + g[i + k1][j + k2] = '0'; + } + } + } + return list; + } + + static void initReaderPrinter(boolean test) { + if (test) { + try { + in = new InputReader(new FileInputStream(""src/input.in"")); + out = new PrintWriter(new FileOutputStream(""src/output.out"")); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + in = new InputReader(System.in); + out = new PrintWriter(System.out); + } + } + + static class InputReader { + BufferedReader br; + StringTokenizer st; + + InputReader(InputStream stream) { + try { + br = new BufferedReader(new InputStreamReader(stream), 32768); + } catch (Exception e) { + e.printStackTrace(); + } + } + + String next() { + while (st == null || !st.hasMoreTokens()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + + Integer[] nextIntArray(int n) { + Integer[] a = new Integer[n]; + for (int i = 0; i < n; i++) { + a[i] = nextInt(); + } + return a; + } + + Long[] nextLongArray(int n) { + Long[] a = new Long[n]; + for (int i = 0; i < n; i++) { + a[i] = nextLong(); + } + return a; + } + } +} + + +",0,Non-plagiarised +7686c854,abd16ff0,"import java.util.*; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.IntToLongFunction; +import java.lang.*; +import java.io.*; +import java.math.*; +public final class CF +{ + public static void main(String[]args)throws IOException + { + FastReader ob=new FastReader(); + int t=ob.nextInt(); + StringBuffer sb=new StringBuffer(); + while(t-->0) + { + int n=ob.nextInt(); + PriorityQueue a=new PriorityQueue<>(); + PriorityQueue b=new PriorityQueue<>(); + long ans=Long.MAX_VALUE; + long sum=0; + for(int i=0;i +{ + int x,y; + Pair(int x,int y) + { + this.x=x; + this.y=y; + } + @Override + public int compareTo(Pair a) { + return a.y-y; + } +} +class FastReader { + BufferedReader br; + StringTokenizer st; + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + public String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + public String nextLine() + { + String s=""""; + try { + s=br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return s; + } + public int nextInt() { + return Integer.parseInt(next()); + } + public long nextLong() { + return Long.parseLong(next()); + } + public double nextDouble() { + return Double.parseDouble(next()); + } +} +","import java.util.*; + +public class Solve{ + + public static void main(String[] args){ + Scanner sc=new Scanner(System.in); + int t=sc.nextInt(); + + + while(t-->0){ + int n=sc.nextInt(); + long ans=Long.MAX_VALUE; + long pre=0; + PriorityQueue epq=new PriorityQueue<>(); + PriorityQueue opq=new PriorityQueue<>(); + for(int i=0;i0) ans=Math.min(ans,pre+opq.peek()*(n-opq.size())+epq.peek()*(n-epq.size())); + + } + System.out.println(ans); + } + } +}",1,Plagiarised +72d9eb5b,af0deeab,"import java.util.*; +import java.io.*; + +public class D { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + + int T = in.nextInt(); + while(T-- > 0) { + int n = in.nextInt(); + int[] a = new int[n]; + for(int j=0;j blue = new ArrayList<>(); + List red = new ArrayList<>(); + for(int j=0;jcur) { + p = false; + break; + } + else cur++; + } + + if(p) System.out.println(""yes""); + else System.out.println(""no""); + } + } +} +","import java.io.*; +import java.util.*; + +public class Solution { + static int M = 998244353; + static Random rng = new Random(); + + private static boolean testCase(int n, int[] a, String colors) { + boolean[] contains = new boolean[n]; + int any = 0, rem = n, idx; + List fromLeft = new ArrayList<>(), fromRight = new ArrayList<>(); + + for (int i = 0; i < n; i++) { + if (colors.charAt(i) == 'B') { + if (a[i] <= 0) { + return false; + } else if (a[i] >= n) { + any++; + } else { + fromLeft.add(a[i]); + } + } else { + if (a[i] > n) { + return false; + } else if (a[i] <= 1) { + any++; + } else { + fromRight.add(a[i]); + } + } + } + + sort(fromLeft); + sort(fromRight); + + //System.out.println(fromLeft); + //System.out.println(fromRight); + + idx = 1; + + for (int i = 0; i < fromLeft.size(); i++) { + //try to assign a[i] to idx + //System.out.println(String.format(""Assign %d to %d"", a[i], idx)); + if (fromLeft.get(i) < idx) { + return false; + } else { + contains[idx - 1] = true; + rem--; + idx++; + } + } + + idx = n; + + for (int i = fromRight.size() - 1; i >= 0; i--) { + //System.out.println(String.format(""Assign %d to %d"", a[i], idx)); + if (idx < fromRight.get(i)) { + return false; + } else { + contains[idx - 1] = true; + rem--; + idx--; + } + } + + return any >= rem; + } + + public static void main(String[] args) { + FastScanner in = new FastScanner(); + PrintWriter out = new PrintWriter(System.out); + int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc. + //in.nextLine(); + for (int tt = 1; tt <= t; ++tt) { + int n = in.nextInt(); + int[] a = in.readArray(n); + String colors = in.next(); + + out.println(testCase(n, a, colors) ? ""YES"" : ""NO""); + } + + out.close(); + } + + private static class FastScanner { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(""""); + String next() { + while (!st.hasMoreTokens()) + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + return st.nextToken(); + } + + boolean hasNext() { + return st.hasMoreTokens(); + } + + char[] readCharArray(int n) { + char[] arr = new char[n]; + try { + br.read(arr); + br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return arr; + } + + int nextInt() { + return Integer.parseInt(next()); + } + + int[] readArray(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) a[i] = nextInt(); + return a; + } + + long[] readLongArray(int n) { + long[] a = new long[n]; + for (int i = 0; i < n; i++) a[i] = nextLong(); + return a; + } + + long nextLong() { + return Long.parseLong(next()); + } + } + + private static void sort(int[] arr) { + int temp, idx; + + for (int i = arr.length - 1; i > 0; i--) { + idx = rng.nextInt(i + 1); + + temp = arr[i]; + arr[i] = arr[idx]; + arr[idx] = temp; + } + + Arrays.sort(arr); + } + + private static void sort(long[] arr) { + long temp; + int idx; + + for (int i = arr.length - 1; i > 0; i--) { + idx = rng.nextInt(i + 1); + + temp = arr[i]; + arr[i] = arr[idx]; + arr[idx] = temp; + } + + Arrays.sort(arr); + } + + private static void sort(T[] arr) { + T temp; + int idx; + + for (int i = arr.length - 1; i > 0; i--) { + idx = rng.nextInt(i + 1); + + temp = arr[i]; + arr[i] = arr[idx]; + arr[idx] = temp; + } + + Arrays.sort(arr); + } + + private static void sort(T[] arr, Comparator cmp) { + T temp; + int idx; + + for (int i = arr.length - 1; i > 0; i--) { + idx = rng.nextInt(i + 1); + + temp = arr[i]; + arr[i] = arr[idx]; + arr[idx] = temp; + } + + Arrays.sort(arr, cmp); + } + + private static > void sort(List list) { + T temp; + int idx; + + for (int i = list.size() - 1; i > 0; i--) { + idx = rng.nextInt(i + 1); + + temp = list.get(i); + list.set(i, list.get(idx)); + list.set(idx, temp); + } + + Collections.sort(list); + } + + private static void sort(List list, Comparator cmp) { + T temp; + int idx; + + for (int i = list.size() - 1; i > 0; i--) { + idx = rng.nextInt(i + 1); + + temp = list.get(i); + list.set(i, list.get(idx)); + list.set(idx, temp); + } + + Collections.sort(list, cmp); + } +} +",0,Non-plagiarised +7d7cf9a7,e45446bc,"import java.util.*; +import java.io.*; +import java.math.*; + +public class Coder { + static int n; + static long c[]; + static StringBuilder str = new StringBuilder(""""); + static void solve() { + long mne=c[0]; + long mno=c[1]; + long ans=(c[0]+c[1])*n; + long se=c[0]; + long so=c[1]; + long ecnt=1,ocnt=1; + for(int i=2;i0) { + n=Integer.parseInt(bf.readLine().trim()); + c=new long[n]; + String s[]=bf.readLine().trim().split(""\\s+""); + for(int i=0;i 0) { + String[] scn = (br.readLine()).trim().split("" ""); + int n = Integer.parseInt(scn[0]); + long[] arr = new long[n]; + scn = (br.readLine()).trim().split("" ""); + for (int i = 0; i < n; i++) { + arr[i] = Long.parseLong(scn[i]); + } + long min; + long hor = arr[0], ver = arr[1]; + long min1 = 0, min2 = 0; + min = (hor + ver) * n; + long x = 0, y = 0; + for (int i = 2; i < n; i++) { + if (i % 2 == 0) { + x += 1; + if (arr[i] >= hor) { + min1 += arr[i]; + } else { + min1 += hor; + hor = arr[i]; + } + + + } else { + y += 1; + if (arr[i] >= ver) { + min2 += arr[i]; + } else { + min2 += ver; + ver = arr[i]; + } + } + long pro = (n - x) * hor + (n - y) * ver; + min = Math.min(min, min1 + min2 +pro); + } + sb.append(min); + sb.append(""\n""); + } + System.out.println(sb); + return; + + } + + public static void sort(long[] arr) { + int n = arr.length; + for (int i = 0; i < n; i++) { + int idx = (int) Math.random() * n; + long temp = arr[i]; + arr[i] = arr[idx]; + arr[idx] = temp; + } + Arrays.sort(arr); + } + + public static void print(long[] dp) { + for (long ele : dp) { + System.out.print(ele + "" ""); + } + System.out.println(); + } + + public static void print(long[][] dp) { + for (long[] a : dp) { + for (long ele : a) { + System.out.print(ele + "" ""); + } + System.out.println(); + } + } + +} +",0,Non-plagiarised +884f5678,cf27732e,"import java.io.*; +import java.util.*; +import java.text.*; +import java.math.*; +import java.util.regex.*; + +public class JaiShreeRam{ + static Scanner in=new Scanner(); + static long mod = 1000000007; + static ArrayList> adj; + public static void main(String[] args) throws Exception{ + class Element{ + int x; + char c; + public Element(int y,char k) { + x=y; + c=k; + } + } + int z=in.readInt(); + while(z-->0) { + int n=in.readInt(); + int a[]=nia(n); + char c[]=in.readString().toCharArray(); + ArrayList d=new ArrayList<>(); + ArrayList in=new ArrayList<>(); + for(int i=0;i=0;i--) { + if(in.get(i)>n) { + ans=""NO""; + break; + } + n--; + } + System.out.println(ans); + } + } + static long ans(long l,long r,long x,long y) { + long mid=(r-l)/2+l; + long a=mid%x; + long b=y%mid; + long c=0; + if(l>r) { + return 0; + } + if(a==b) { + return mid; + } + else if(a0) { + int n=sc.nextInt(); + int []vals=new int[n]; + boolean numLine[]=new boolean[n+1]; + + for(int i=0;ib=new ArrayList(); + ArrayListr=new ArrayList(); + for(int i=0;i0 )b.add(vals[i]); + else if( s.charAt(i)=='R' && vals[i]<=n)r.add(vals[i]); + } + Collections.sort(b); + Collections.sort(r); + int small=1; + for(int i=0;i=0;i--) { + + int y=r.get(i); + if(y>large)continue; + // y=Math.max(large, y); + numLine[large]=true; + large--; + } + //pw.print(Arrays.toString(numLine)); + boolean can=true; + for(int i=1;i<=n;i++) { + if(numLine[i]==false) { + pw.println(""no""); + can=false; + break; + } + + } + if(can)pw.println(""yes""); + + + + + + + + } + + pw.close(); + + } + + // --------------------stuff ---------------------- + static class pair implements Comparable { + int v; + int w; + + public pair(int v,int w) { + this.v = v; + this.w = w; + + } + + public int compareTo(pair p) { + return this.w- p.w;// increasing order!! + + //return Double.compare(v*w, p.w*p.v); + + } + } + + static class Scanner { + StringTokenizer st; + BufferedReader br; + + public Scanner(InputStream s) { + br = new BufferedReader(new InputStreamReader(s)); + } + + public Scanner(FileReader r) { + br = new BufferedReader(r); + } + + public String next() throws IOException { + while (st == null || !st.hasMoreTokens()) + st = new StringTokenizer(br.readLine()); + return st.nextToken(); + } + + public int nextInt() throws IOException { + return Integer.parseInt(next()); + } + + public long nextLong() throws IOException { + return Long.parseLong(next()); + } + + public String nextLine() throws IOException { + return br.readLine(); + } + + public double nextDouble() throws IOException { + String x = next(); + StringBuilder sb = new StringBuilder(""0""); + double res = 0, f = 1; + boolean dec = false, neg = false; + int start = 0; + if (x.charAt(0) == '-') { + neg = true; + start++; + } + for (int i = start; i < x.length(); i++) + if (x.charAt(i) == '.') { + res = Long.parseLong(sb.toString()); + sb = new StringBuilder(""0""); + dec = true; + } else { + sb.append(x.charAt(i)); + if (dec) + f *= 10; + } + res += Long.parseLong(sb.toString()) / f; + return res * (neg ? -1 : 1); + } + + public long[] nextlongArray(int n) throws IOException { + long[] a = new long[n]; + for (int i = 0; i < n; i++) + a[i] = nextLong(); + return a; + } + + public Long[] nextLongArray(int n) throws IOException { + Long[] a = new Long[n]; + for (int i = 0; i < n; i++) + a[i] = nextLong(); + return a; + } + + public int[] nextIntArray(int n) throws IOException { + int[] a = new int[n]; + for (int i = 0; i < n; i++) + a[i] = nextInt(); + return a; + } + + public Integer[] nextIntegerArray(int n) throws IOException { + Integer[] a = new Integer[n]; + for (int i = 0; i < n; i++) + a[i] = nextInt(); + return a; + } + + public boolean ready() throws IOException { + return br.ready(); + } + } + +}",0,Non-plagiarised +7974ffba,94b3b86d,"import java.util.*; +import java.io.*; +import java.math.*; +public class Euler { + + static int N = (int)1e5 + 5; + static int n, a, b, da, db; + static int[] depth = new int[N]; + static ArrayList[] adj = new ArrayList[N]; + static int diam; + + public static int dfs(int x, int p) { + + int len = 0; + for (int y : adj[x]) { + if (y != p) { + depth[y] = depth[x] + 1; + int cur = 1 + dfs(y, x); + diam = Math.max(diam, cur + len); + len = Math.max(len, cur); + } + } + return len; + + } + + public static void main(String[] args){ + + FastReader in = new FastReader(); + PrintWriter o = new PrintWriter(System.out); + + int t = in.nextInt(); + + while(t-- > 0) { + n = in.nextInt(); + a = in.nextInt(); + b = in.nextInt(); + da = in.nextInt(); + db = in.nextInt(); + for (int i = 1; i <= n; i++) { + adj[i] = new ArrayList<>(); + } + for (int i = 0; i < n - 1; i++) { + int u = in.nextInt(); + int v = in.nextInt(); + adj[u].add(v); + adj[v].add(u); + } + diam = 0; + depth[a] = 0; + dfs(a, -1); + + boolean works = true; + + if (depth[b] <= da) { + o.println(""Alice""); + continue; + } + + if (2 * da >= diam) { + o.println(""Alice""); + continue; + } + + if (db > 2 * da) { + o.println(""Bob""); + continue; + } + + if (db <= 2 * da) { + o.println(""Alice""); + } + + + } + + + o.close(); + o.flush(); + return; + + + } + + + static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } + + +} +","import java.io.*; +import java.util.*; + +public class D +{ + public static ArrayList adj[]; + public static int node; + public static int dist = 0; + public static void main(String[] args) throws IOException + { + FastScanner sc = new FastScanner(); + int T = sc.nextInt(); + PrintWriter out = new PrintWriter(System.out); + for(int t = 0; t < T; t++){ + int N = sc.nextInt(); + int a = sc.nextInt(); int b = sc.nextInt(); + int da = sc.nextInt(); int db = sc.nextInt(); + adj = new ArrayList[N+1]; + for(int i = 0; i <= N; i++){ + adj[i] = new ArrayList(); + } + for(int i = 0; i < N-1; i++){ + int v = sc.nextInt(); + int u = sc.nextInt(); + adj[v].add(u); + adj[u].add(v); + } + if(db > 2*da){ + dfs1(a, 0, b, 0); + if(dist <= da){ + out.println(""Alice""); + } + else{ + node = 0; + dist = 0; + dfs(1, 0, 0); + dfs(node, 0, 0); + if(dist > 2*da){ + out.println(""Bob""); + } + else{ + out.println(""Alice""); + } + } + } + else{ + out.println(""Alice""); + } + } + out.close(); + } + public static void dfs1(int a, int p, int b, int d){ + if(a == b){ + dist = d; + } + for(int next : adj[a]){ + if(next != p){ + dfs1(next, a, b, d+1); + } + } + } + public static void dfs(int i, int p, int d){ + if(d > dist){ + node = i; + dist = d; + } + for(int next : adj[i]){ + if(next != p){ + dfs(next, i, d+1); + } + } + } + + + static class FastScanner { + private int BS = 1 << 16; + private char NC = (char) 0; + private byte[] buf = new byte[BS]; + private int bId = 0, size = 0; + private char c = NC; + private double cnt = 1; + private BufferedInputStream in; + + public FastScanner() { + in = new BufferedInputStream(System.in, BS); + } + + public FastScanner(String s) { + try { + in = new BufferedInputStream(new FileInputStream(new File(s)), BS); + } catch (Exception e) { + in = new BufferedInputStream(System.in, BS); + } + } + + private char getChar() { + while (bId == size) { + try { + size = in.read(buf); + } catch (Exception e) { + return NC; + } + if (size == -1) + return NC; + bId = 0; + } + return (char) buf[bId++]; + } + + public int nextInt() { + return (int) nextLong(); + } + + public int[] nextInts(int N) { + int[] res = new int[N]; + for (int i = 0; i < N; i++) { + res[i] = (int) nextLong(); + } + return res; + } + + public long[] nextLongs(int N) { + long[] res = new long[N]; + for (int i = 0; i < N; i++) { + res[i] = nextLong(); + } + return res; + } + + public long nextLong() { + cnt = 1; + boolean neg = false; + if (c == NC) c = getChar(); + for (; (c < '0' || c > '9'); c = getChar()) { + if (c == '-') neg = true; + } + long res = 0; + for (; c >= '0' && c <= '9'; c = getChar()) { + res = (res << 3) + (res << 1) + c - '0'; + cnt *= 10; + } + return neg ? -res : res; + } + + public double nextDouble() { + double cur = nextLong(); + return c != '.' ? cur : cur + nextLong() / cnt; + } + + public double[] nextDoubles(int N) { + double[] res = new double[N]; + for (int i = 0; i < N; i++) { + res[i] = nextDouble(); + } + return res; + } + + public String next() { + StringBuilder res = new StringBuilder(); + while (c <= 32) c = getChar(); + while (c > 32) { + res.append(c); + c = getChar(); + } + return res.toString(); + } + + public String nextLine() { + StringBuilder res = new StringBuilder(); + while (c <= 32) c = getChar(); + while (c != '\n') { + res.append(c); + c = getChar(); + } + return res.toString(); + } + + public boolean hasNext() { + if (c > 32) + return true; + while (true) { + c = getChar(); + if (c == NC) + return false; + else if (c > 32) + return true; + } + } + } + + static void ASSERT(boolean assertion, String message) { + if (!assertion) throw new AssertionError(message); + } +} +",0,Non-plagiarised +3dd65549,f229aa7f,"import java.util.*; +import java.io.*; + +public class codeforces { + public static void main(String[] args) throws Exception { + int t=sc.nextInt(); + while(t-->0) { + int n=sc.nextInt(); + char[] a=sc.next().toCharArray(); + char[] b=sc.next().toCharArray(); + int e0=0; + int e1=0; + int o0=0; + int o1=0; + for(int i=0;i { + long x; + long y; + + public pair(long x, long y) { + this.x = x; + this.y = y; + } + + public String toString() { + return x + "" "" + y; + } + + public boolean equals(Object o) { + if (o instanceof pair) { + pair p = (pair) o; + return p.x == x && p.y == y; + } + return false; + } + + public int hashCode() { + return new Long(x).hashCode() * 31 + new Long(y).hashCode(); + } + + public int compareTo(pair other) { + if (this.x == other.x) { + return Long.compare(this.y, other.y); + } + return Long.compare(this.x, other.x); + } + } + + static class tuble implements Comparable { + int x; + int y; + int z; + + public tuble(int x, int y, int z) { + this.x = x; + this.y = y; + this.z = z; + } + + public String toString() { + return x + "" "" + y + "" "" + z; + } + + public int compareTo(tuble other) { + if (this.x == other.x) { + if (this.y == other.y) { + return this.z - other.z; + } + return this.y - other.y; + } else { + return this.x - other.x; + } + } + } + + static long mod = 1000000007; + static Random rn = new Random(); + static Scanner sc = new Scanner(System.in); + static PrintWriter pw = new PrintWriter(System.out); +}","import java.util.*; +import java.io.*; +import java.math.*; + +public class cf { + static PrintWriter pw = new PrintWriter(System.out); + + public static void main(String[] args) throws IOException, InterruptedException { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while (t-- > 0) { + int n = sc.nextInt(); + char[] a = sc.next().toCharArray(); + char[] b = sc.next().toCharArray(); + int x = 0, y = 0, lit = 0,lit2 = 0; + for (int i = 0; i < n; i++) { + if (a[i] == '1') + lit++; + if (b[i] == '1') + lit2++; + if (a[i] == b[i]) + x++; + else + y++; + } + if(lit == lit2 || n - lit + 1 == lit2) { + if (lit == lit2 && n - lit + 1 == lit2) { + pw.println(Math.min(x,y)); + }else if(lit == lit2) { + pw.println(y); + }else { + pw.println(x); + } + }else { + pw.println(-1); + } + } + pw.close(); + } + + public static class tuble implements Comparable { + int x; + int y; + int z; + + public tuble(int x, int y, int z) { + this.x = x; + this.y = y; + this.z = z; + } + + public String toString() { + return x + "" "" + y + "" "" + z; + } + + public int compareTo(tuble other) { + if (this.x == other.x) { + if (this.y == other.y) + return this.z - other.z; + return this.y - other.y; + } else { + return this.x - other.x; + } + } + } + + public static class pair implements Comparable { + int x; + int y; + + public pair(int x, int y) { + this.x = x; + this.y = y; + } + + public String toString() { + return x + "" "" + y; + } + + public boolean equals(Object o) { + if (o instanceof pair) { + pair p = (pair) o; + return p.x == x && p.y == y; + } + return false; + } + + public int hashCode() { + return new Integer(x).hashCode() * 31 + new Integer(y).hashCode(); + } + + public int compareTo(pair other) { + if (this.x == other.x) { + return Long.compare(this.y, other.y); + } + return Long.compare(this.x, other.x); + } + } + + static class Scanner { + StringTokenizer st; + BufferedReader br; + + public Scanner(InputStream s) { + br = new BufferedReader(new InputStreamReader(s)); + } + + public Scanner(FileReader r) { + br = new BufferedReader(r); + } + + public String next() throws IOException { + while (st == null || !st.hasMoreTokens()) + st = new StringTokenizer(br.readLine()); + return st.nextToken(); + } + + public int nextInt() throws IOException { + return Integer.parseInt(next()); + } + + public long nextLong() throws IOException { + return Long.parseLong(next()); + } + + public String nextLine() throws IOException { + return br.readLine(); + } + + public double nextDouble() throws IOException { + return Double.parseDouble(next()); + } + + public boolean ready() throws IOException { + return br.ready(); + } + + } + +}",0,Non-plagiarised +60557643,948e98b6," + +import java.util.Scanner; + +public class D { + public static void main(String[] args) { + Scanner s = new Scanner(System.in); + int lines = s.nextInt(); + + s.nextLine(); + + for (int i = 0; i < lines; i += 1) { + solve(s.nextInt(), s); + } + } + + + public static void solve(int n, Scanner s) { + int a[] = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = s.nextInt(); + } + + int b[] = new int[n]; + for (int i = 0; i + 1 < n; i += 2) { + b[i + 1] = a[i]; + b[i] = -a[i + 1]; + } + + while (b[n - 2] == 0 || b[n - 1] == 0) { + b[n - 1] += a[n - 2]; + b[n - 2] -= a[n - 1]; + } + + StringBuilder erg = new StringBuilder(); + for (int i = 0; i < n; i++) { + erg.append(b[i]).append("" ""); + } + + System.out.println(erg); + } +} +"," +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.math.BigInteger; + +import java.util.*; + +public class Main +{ + + + + public static void main(String[] args) + { + FastScanner input = new FastScanner(); + StringBuilder result = new StringBuilder(); + int tc = input.nextInt(); + while (tc-- > 0) { + int n = input.nextInt(); + int a[] = new int[n]; + int b[] = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = input.nextInt(); + + } + if(n%2==0) + { + for (int i = 0; i 0) + { + int n=input.nextInt(); + int a[]=new int[n]; + ArrayList list=new ArrayList<>(); + ArrayList space=new ArrayList<>(); + for(int i=0;i a = new ArrayList<>(); + ArrayList b = new ArrayList<>(); + for(int i = 0; i < n; i++){ + if(arr[i] == 1){ + a.add(i); + }else{ + b.add(i); + } + } + if(a.size() == 0){ + System.out.println(""0""); + return; + } + int [][] dp = new int[a.size()][b.size()]; + for(int i = 0; i < a.size(); i++){ + for(int j = i; j < b.size(); j++) { + if (j == 0) { + dp[i][j] = Math.abs(a.get(i) - b.get(j)); + } else if (i == 0) { + dp[i][j] = Math.min(dp[i][j - 1], Math.abs(a.get(i) - b.get(j))); + } else if (i == j) { + dp[i][j] = dp[i - 1][j - 1] + Math.abs(a.get(i) - b.get(j)); + } else { + dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j - 1] + Math.abs(a.get(i) - b.get(j))); + } + } + } + System.out.println(dp[a.size() - 1][b.size() - 1]); + } + + + static int readInt() throws IOException { + return Integer.parseInt(br.readLine()); + } + + static long readLong() throws IOException { + return Long.parseLong(br.readLine()); + } + + static int[] readIntarray() throws IOException { + String[] _a = br.readLine().split("" ""); + int[] _res = new int[_a.length]; + for (int i = 0; i < _a.length; i++) { + _res[i] = Integer.parseInt(_a[i]); + } + return _res; + } + + static long[] readLongarray() throws IOException { + String[] _a = br.readLine().split("" ""); + long[] _res = new long[_a.length]; + for (int i = 0; i < _a.length; i++) { + _res[i] = Long.parseLong(_a[i]); + } + return _res; + } +} +",0,Non-plagiarised +c34bb733,ea899386,"//package pack; +import java.io.*; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; +import java.util.*; +import java.io.*; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.*; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; +import java.math.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; +import java.util.Vector; +import static java.lang.Math.sqrt; +import static java.lang.Math.floor; + + + + + + +public class topcoder { + + static class pair{ + + int first; + int second; + public pair(int first, int second) { + this.first = first; + this.second = second; + } + } + + + static class Compare{ + + static void compare(ArrayListarr, int n) { + + Collections.sort(arr,new Comparator() { + public int compare(pair p1, pair p2) { + return p1.first-p2.first; + } + }); + + } + } + + +static class pairr implements Comparable{ + Integer value; + Integer index; + + public pairr(Integer value, Integer index) { + this.value = value; + this.index = index; + } + @Override + + public int compareTo(pairr o) { + return value-o.value; + } +} + + + + static class Key + { + public K1 key1; + public K2 key2; + + public Key(K1 key1, K2 key2) + { + this.key1 = key1; + this.key2 = key2; + } + + @Override + public boolean equals(Object o) + { + if (this == o) { + return true; + } + + if (o == null || getClass() != o.getClass()) { + return false; + } + + Key key = (Key) o; + if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) { + return false; + } + + if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) { + return false; + } + + return true; + } + + @Override + public int hashCode() + { + int result = key1 != null ? key1.hashCode() : 0; + result = 31 * result + (key2 != null ? key2.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return ""["" + key1 + "", "" + key2 + ""]""; + } + } + + + public static int sumOfDigits (long n) { + + int sum = 0; + + while(n > 0) { + sum += n%10; + n /= 10; + } + + return sum; + + } + + + public static long binary_search(int s, int e, long num, long []ar) { + + if(s > e) { + return -1; + } + + + int mid = (s+e)/2; + + if(s == e && ar[s] >= num) { + return ar[s]; + }else if(s == e && ar[s] < num) { + return -1; + }else if(ar[mid] < num) { + return binary_search(mid+1,e,num,ar); + }else if(ar[mid] >= num) { + return binary_search(s,mid,num,ar); + } + + return -1; + + } + + + public static int index_search(int s, int e, long num, long []ar) { + + if(s > e) { + return -1; + } + + + int mid = (s+e)/2; + + if(s == e && ar[s] >= num) { + return s; + }else if(s == e && ar[s] < num) { + return -1; + }else if(ar[mid] < num) { + return index_search(mid+1,e,num,ar); + }else if(ar[mid] >= num) { + return index_search(s,mid,num,ar); + } + + return -1; + + } + public static void swap(int []ar, int i, int j) { + + + for(int k= j; k >= i; k--) { + int temp = ar[k]; + ar[k] = ar[k+1]; + ar[k+1] = temp; + } + } + + public static boolean digit_exists(long n) { + + while(n > 0) { + if(n%10 == 9) + return true; + n = n/10; + } + + return false; + } + public static int log(int n) { + + int c = 0; + while(n > 0) { + c++; + n /=2; + } + + return c; + } + public static int findOr(int[]bits){ + int or=0; + for(int i=0;i<32;i++){ + or=or<<1; + if(bits[i]>0) + or=or+1; + } + return or; + } + + static void simpleSieve(int limit, Vector prime) + { + // Create a boolean array ""mark[0..n-1]"" and initialize + // all entries of it as true. A value in mark[p] will + // finally be false if 'p' is Not a prime, else true. + boolean mark[] = new boolean[limit+1]; + + for (int i = 0; i < mark.length; i++) + mark[i] = true; + + for (int p=2; p*pl) + { + // Compute all primes smaller than or equal + // to square root of n using simple sieve + int limit = (int) (floor(sqrt(n))+1); + Vector prime = new Vector<>(); + + simpleSieve(limit, prime); + + // Divide the range [0..n-1] in different segments + // We have chosen segment size as sqrt(n). + int low = limit; + int high = 2*limit; + + // While all segments of range [0..n-1] are not processed, + // process one segment at a time + while (low < n) + { + if (high >= n) + high = n; + + // To mark primes in current range. A value in mark[i] + // will finally be false if 'i-low' is Not a prime, + // else true. + boolean mark[] = new boolean[limit+1]; + + for (int i = 0; i < mark.length; i++) + mark[i] = true; + + // Use the found primes by simpleSieve() to find + // primes in current range + for (int i = 0; i < prime.size(); i++) + { + // Find the minimum number in [low..high] that is + // a multiple of prime.get(i) (divisible by prime.get(i)) + // For example, if low is 31 and prime.get(i) is 3, + // we start with 33. + int loLim = (int) (floor(low/prime.get(i)) * prime.get(i)); + if (loLim < low) + loLim += prime.get(i); + + /* Mark multiples of prime.get(i) in [low..high]: + We are marking j - low for j, i.e. each number + in range [low, high] is mapped to [0, high-low] + so if range is [50, 100] marking 50 corresponds + to marking 0, marking 51 corresponds to 1 and + so on. In this way we need to allocate space only + for range */ + for (int j=loLim; j 0) { + power++; + k /=2 ; + } + + long check = (long)Math.pow(2, power-1); + if(k1 == check) { + return power; + } + // System.out.println(power); + long f = (long)Math.pow(2, power-1); + long rem = k1-f; + return find_indexNum(rem); + } + + public static void sortPair(ArrayListl, int n) { + n = l.size(); + + Compare obj = new Compare(); + obj.compare(l, n); + } + + + public static int add(long n, long num, long a, int i) { + //System.out.println(num); + if(num > n)return -1; + + if(num == n) { + + return i; + } + + if(a < 2050)return -1; + + + + long temp = num+a; + + if(temp <= n) { + return add(n,temp,a,i+1); + }else if(temp > n){ + a /= 10; + return add(n,num,a,i); + } + + return -1; + } + + + public static void shuffle(int []array, int num,int t_index, boolean []vis, int m ) { + + + for(int i = 0; i < m; i++) { + if(vis[i] == false) { + + + int temp = array[i]; + if(i < t_index) { + vis[i] = true; + } + array[i] = num; + array[t_index] = temp; + // System.out.println(array[t_index]+"" ""+array[i]); + break; + } + } + } + + public static void rotate(int []arr,int j, int times, int m) { + + + + if(j == 0) { + int temp1 = arr[0]; + arr[0] = arr[times]; + arr[times] = temp1; + + }else { + int temp = arr[j]; + int z = arr[0]; + + arr[0] = arr[times]; + + arr[j] = z; + arr[times] = temp; + + + + } + + } + + public static void recur(int i,int A, int B,int []dp,int []metal, int n, boolean took,int ind) { + + if(i-A <= 0 && i-B <= 0)return; + int count = 0; + + for(int j = 1; j <= n; j++) { + if(dp[j] >= metal[j]) { + count++; + } + } + + if(count == n)return; + if(i-A >= 0 && i-B >= 0 && dp[i] > 0 && dp[i] > metal[i]) { + dp[i]--; + + dp[i-A]++; + + dp[i-B]++; + } + + + if(ind == 6) { + // System.out.println(Arrays.toString(dp)); + } + recur(i-A,A,B,dp,metal,n,took,ind); + recur(i-B,A,B,dp,metal,n,took,ind); + } + + + public static boolean isPrime(int n) { + if (n <= 1) return false; + if (n <= 3) return true; + if (n % 2 == 0 || n % 3 == 0) return false; + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + return true; + } + + public static boolean[] getSieve(int n) { + boolean[] isPrime = new boolean[n+1]; + for (int i = 2; i <= n; i++) isPrime[i] = true; + for (int i = 2; i*i <= n; i++) if (isPrime[i]) + for (int j = i; i*j <= n; j++) isPrime[i*j] = false; + return isPrime; + } + + + public static int gcd(int a, int b) { + int tmp = 0; + while(b != 0) { + tmp = b; + b = a%b; + a = tmp; + } + return a; + } + + + public static long gcd(long a, long b) { + long tmp = 0; + while(b != 0) { + tmp = b; + b = a%b; + a = tmp; + } + return a; + } + + + public static boolean poss( int r, int c, int k, int n, int m,boolean [][]vis) { + + if(k < 0)return false; + if(r > n || c > m || r < 1 || c < 1)return false; + + if(r == n && c == m && k == 0) { + // System.out.println(k); + return true; + } + if(vis[r][c] == true) { + return false; + } + vis[r][c] = true; + + + return poss(r+1,c,k-c,n,m,vis) || poss(r,c+1,k-r,n,m,vis); + + + } + + public static void dfs(LinkedList[]list, HashMapmap, int parent, int n) { + + Stackst = new Stack<>(); + + + } + public static boolean pos(int n) { + + + int i = 1; + boolean pos = false; + + while(i*i <= n) { + if(i*i*2 == n || i*i*4 == n) { + pos = true; + break; + } + i++; + } + if(pos)return true; + return false; + + } + + + public static void main(String args[])throws IOException{ + + // System.setIn(new FileInputStream(""Case.txt"")); + BufferedReader ob = new BufferedReader(new InputStreamReader(System.in)); + + + int t = Integer.parseInt(ob.readLine()); + + + while( t--> 0) { + + StringTokenizer st = new StringTokenizer(ob.readLine()); + int n = Integer.parseInt(st.nextToken()); + int m = Integer.parseInt(st.nextToken()); + int x = Integer.parseInt(st.nextToken()); + + int []ar = new int[n]; + st = new StringTokenizer(ob.readLine()); + PriorityQueuepq = new PriorityQueue<>(); + + for(int i = 0; i < n; i++) { + ar[i] = Integer.parseInt(st.nextToken()); + pq.add(ar[i]); + } + + + PriorityQueuep = new PriorityQueue<>(); + for(int i =0; i < n; i++) { + p.add(new pairr(ar[i],i)); + } + int []ans = new int[n]; + + + for(int i = 0; i < n; i++) { + pairr a = p.poll(); + ans[a.index] = (i%m)+1; + } + + System.out.println(""YES""); + for(int i = 0; i < n; i++ ) { + System.out.print(ans[i]+"" ""); + } + System.out.println(); + } + } +} + + + + ","import java.io.*; +import java.util.*; + +public class Codeforces { + public static class Tower implements Comparable{ + int val; + int index; + public Tower(int ind, int v) { + val = v; + index = ind; + } + @Override + public int compareTo(Tower o) { + return Integer.compare(val, o.val); + } + } + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int cases = Integer.parseInt(br.readLine()); + while(cases-- > 0) { + String[] str = br.readLine().split("" ""); + int n = Integer.parseInt(str[0]); + int m = Integer.parseInt(str[1]); + int x = Integer.parseInt(str[2]); + int[] h = new int[n]; + str = br.readLine().split("" ""); + for(int i=0; i q = new PriorityQueue<>(m); + int[] ans = new int[n]; + for(int i=0; i actions = new ArrayList<>(); + static String[] move = new String[]{"""", ""0110"", ""1001"", ""1101"", ""1001"", ""1110"", ""1011"", ""0000"", ""0110"", ""0111"", ""1101"", ""0000"", ""0111"", ""0000"", ""0000"", ""0001""}; + static int[] xdif = new int[]{0, 0, 1, 1}, ydif = new int[]{0, 1, 0, 1}; + + public static void main(String[] args) throws IOException { + int tests = sc.nextInt(); + for (int t = 0; t < tests; t++) + solve(); + pw.close(); + } + + static void solve() throws IOException { + n = sc.nextInt(); + m = sc.nextInt(); + map = new int[n][m]; + actions.clear(); + for (int i = 0; i < n; i++) { + String line = sc.next(); + for (int j = 0; j < m; j++) + map[i][j] = line.charAt(j) - '0'; + } + if (n % 2 + m % 2 == 2) + removeCorner(); + if (n % 2 == 1) + for (int y = 0; y + 1 < m; y += 2) + removeDown(y); + if (m % 2 == 1) + for (int x = 0; x + 1 < n; x += 2) + removeRight(x); + for (int x = 0; x + 1 < n; x += 2) + for (int y = 0; y + 1 < m; y += 2) + square(x, y); + pw.println(actions.size()); + for (String action : actions) + pw.println(action); + } + + static void square(int x, int y) { + String type = """" + map[x][y] + map[x][y + 1] + map[x + 1][y] + map[x + 1][y + 1]; + if (map[x][y] + map[x + 1][y] + map[x][y + 1] + map[x + 1][y + 1] > 0) { + int[][] poses = new int[3][]; + int index = 0; + for (int t = 0; t < 4; t++) { + if (map[x + xdif[t]][y + ydif[t]] != move[Integer.parseInt(type, 2)].charAt(t) - '0') + poses[index++] = new int[]{x + xdif[t], y + ydif[t]}; + } + action(poses[0][0], poses[0][1], poses[1][0], poses[1][1], poses[2][0], poses[2][1]); + square(x, y); + } + } + + static void invert(int x, int y) { + map[x][y] = 1 - map[x][y]; + } + + static void removeCorner() { + if (map[n - 1][m - 1] == 1) + action(n - 1, m - 1, n - 2, m - 1, n - 1, m - 2); + } + + static void removeRight(int x) { + int y = m - 1; + if (map[x][y] + map[x + 1][y] == 2) + action(x, y, x + 1, y, x, y - 1); + else if (map[x][y] == 1) + action(x, y, x, y - 1, x + 1, y - 1); + else if (map[x + 1][y] == 1) + action(x + 1, y, x, y - 1, x + 1, y - 1); + } + + static void removeDown(int y) { + int x = n - 1; + if (map[x][y] + map[x][y + 1] == 2) + action(x, y, x, y + 1, x - 1, y); + else if (map[x][y] == 1) + action(x, y, x - 1, y, x - 1, y + 1); + else if (map[x][y + 1] == 1) + action(x, y + 1, x - 1, y, x - 1, y + 1); + } + + static void action(int x1, int y1, int x2, int y2, int x3, int y3) { + actions.add((x1 + 1) + "" "" + (y1 + 1) + "" "" + (x2 + 1) + "" "" + (y2 + 1) + "" "" + (x3 + 1) + "" "" + (y3 + 1)); + invert(x1, y1); + invert(x2, y2); + invert(x3, y3); + } +} + + +class Scanner { + static BufferedReader br; + static StringTokenizer st = new StringTokenizer(""""); + + public Scanner(InputStream inputStream) { + br = new BufferedReader(new InputStreamReader(inputStream)); + } + + public Scanner(String fileName) throws FileNotFoundException { + br = new BufferedReader(new FileReader(fileName)); + } + + public boolean hasNext() throws IOException { + if (st.hasMoreTokens()) + return true; + String line; + while ((line = br.readLine()) != null) { + st = new StringTokenizer(line); + if (st.hasMoreTokens()) + return true; + } + return false; + } + + public double nextDouble() throws IOException { + return Double.parseDouble(next()); + } + + public long nextLong() throws IOException { + return Long.parseLong(next()); + } + + public int nextInt() throws IOException { + return Integer.parseInt(next()); + } + + public String next() throws IOException { + hasNext(); + return st.nextToken(); + } +}","import java.util.*; + +public class CodeForces{ +// 4 +// -3 -5 -2 1 +// k 2 4 0 3 +static ArrayList ans = new ArrayList<>(); + + public static void add(int a,int b,int c,int d,int e,int f){ + int[] tem=new int[6]; + tem[0]=a;tem[1]=b;tem[2]=c;tem[3]=d;tem[4]=e;tem[5]=f; + ans.add(tem); + } + public static void helper(int[][] arr,int r,int c){ + for(int k=0;k<12;k++){ + if(arr[r][c]==1){ + add(r,c,r+1,c,r,c+1); + arr[r][c]=1-arr[r][c]; + arr[r+1][c]=1-arr[r+1][c]; + arr[r][c+1]=1-arr[r][c+1]; + } + if(arr[r][c+1]==1){ + add(r,c+1,r+1,c+1,r,c); + arr[r][c+1]=1-arr[r][c+1]; + arr[r+1][c+1]=1-arr[r+1][c+1]; + arr[r][c]=1-arr[r][c]; + } + if(arr[r+1][c]==1){ + add(r+1,c+1,r,c,r+1,c); + arr[r+1][c+1]=1-arr[r+1][c+1]; + arr[r][c]=1-arr[r][c]; + arr[r+1][c]=1-arr[r+1][c]; + } + if(arr[r+1][c+1]==1){ + add(r+1,c+1,r+1,c,r,c+1); + arr[r+1][c+1]=1-arr[r+1][c+1]; + arr[r+1][c]=1-arr[r+1][c]; + arr[r][c+1]=1-arr[r][c+1]; + } + + } + + } + 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(); + ans.clear(); + int[][] arr=new int[n][m]; + for(int i=0;i0) + { + int n = sc.nextInt(); + sc.nextLine(); + String [] str = new String[n]; + int res = 0; + for(int i=0;i=0;p--) + { + sum+=arr[p]; + if(sum>0) + { + count++; + } + else + { + break; + } + } + res=Math.max(count , res); + } + + System.out.println(res); + t--; + + } + } +}"," +/* + Author : Kartikey Rana + from MSIT New Delhi + */ +import java.util.*; + +import javax.sql.rowset.serial.SerialArray; +import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction; +import java.io.*; +import java.math.*; +import java.sql.Array;; + +public class Main { + static class FR { + + BufferedReader br; + StringTokenizer st; + + public FR() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + + // NEXT INT ARRAY + + int[] NIA(int n) { + int arr[] = new int[n]; + for (int i = 0; i < n; i++) + arr[i] = nextInt(); + return arr; + } + + // NEXT DOUBLE ARRAY + + double[] NDA(int n) { + double arr[] = new double[n]; + for (int i = 0; i < n; i++) + arr[i] = nextDouble(); + return arr; + } + + // NEXT LONG ARRAY + + long[] NLA(int n) { + long[] arr = new long[n]; + for (int i = 0; i < n; i++) + arr[i] = nextLong(); + return arr; + } + + // NEXT STRING ARRAY + + String[] NSA(int n) { + String[] arr = new String[n]; + for (int i = 0; i < n; i++) + arr[i] = next(); + return arr; + } + + // NEXT CHARACTER ARRAY + + char[] NCA(int n) { + char[] arr = new char[n]; + String s = sc.nextLine(); + + for (int i = 0; i < n; i++) + arr[i] = s.charAt(i); + return arr; + } + + } + +//************************* FR CLASS ENDS ********************************** + + static long mod = (long) (1e9 + 7); + + public static int[] sieve(int n) { + + int[] primes = new int[n + 1]; + for (int i = 0; i <= n; i++) { + primes[i] = i; + } + + for (int i = 2; i < n; i++) { + if (primes[i] < 0) + continue; + if ((long) i * (long) i > n) + break; + for (int j = i * i; j < n; j++) { + if (primes[j] > 0 && primes[j] % primes[i] == 0) + primes[j] = -primes[j]; + } + } + return primes; + } + + static int lcm(int a, int b) { + return (int) ((a / gcd(a, b)) * b); + } + + static long gcd(long a, long b) { + if (b == 0) + return a; + + return gcd(b, a % b); + } + + static long[][] ncr(int n, int k) { + long C[][] = new long[n + 1][k + 1]; + int i, j; + + // Calculate value of Binomial + // Coefficient in bottom up manner + for (i = 0; i <= n; i++) { + for (j = 0; j <= Math.min(i, k); j++) { + // Base Cases + if (j == 0 || j == i) + C[i][j] = 1; + + // Calculate value using + // previously stored values + else + C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; + } + } + + return C; + } + + static long modInverse(long a, long m) { + long g = gcd(a, m); + + return power(a, m - 2, m); + + } + + static long power(long x, long y, long m) { + if (y == 0) + return 1; + long p = power(x, y / 2, m) % m; + p = (int) ((p * (long) p) % m); + if (y % 2 == 0) + return p; + else + return (int) ((x * (long) p) % m); + } + + static int XOR(int n) { + if (n % 4 == 0) + return n; + + if (n % 4 == 1) + return 1; + + if (n % 4 == 2) + return n + 1; + + return 0; + } + +// ----------------------------------DSU-------------------------------- + + + static int parent[]; + static int rank[]; + + public static int find(int x) { + + if(x == parent[x]) { + return x; + } + int temp = find(parent[x]); + parent[x] = temp; + return temp; + + } + + public static void union(int x, int y) { + + int lx = find(x); + int ly = find(y); + if(parent[x] == parent[y]) + return; + if(rank[lx] < rank[ly]) { + parent[lx] = ly; + } else if(rank[lx] >rank[ly]) { + parent[ly] = lx; + } else { + parent[lx] = ly; + rank[ly]++; + } + find(x); + find(y); + } + + /* ***************************************************************************************************************************************************/ + + + static class Pair implements Comparable { + + int x; + int y; + + public Pair(int x, int y) { + super(); + this.x = x; + this.y = y; + } + + @Override + public int compareTo(Pair o) { + return this.x - o.x; + } + + } + + + static FR sc = new FR(); + static StringBuilder sb = new StringBuilder(); + + + public static void main(String args[]) throws IOException { + + int tc = sc.nextInt(); +// int tc = 1; + while (tc-- > 0) { + TEST_CASE(); + } + sb.setLength(sb.length() - 1); + System.out.println(sb); + } + + static void TEST_CASE() throws IOException { + // BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + int n = sc.nextInt(); + String[] strs = new String[n]; + + for(int i = 0; i < n; i++) { + strs[i] = sc.next(); + } + + int max = -1; + + for(int i = 0; i < 5; i++) { + ArrayList arr = new ArrayList<>(); + for(int j = 0; j < n; j++) { + String s = strs[j]; + char letter = (char)(97+i); + int val = 0; + for(int k = 0; k < s.length(); k++) { + if(s.charAt(k) == letter) + val++; + else + val--; + } + arr.add(val); + } + Collections.sort(arr, Collections.reverseOrder()); + int val = 0; + int ans = 0; + for(int x : arr) { + val += x; + if(val <= 0) + break; + else + ans++; + } + max = Math.max(ans, max); + } + sb.append(max + ""\n""); + } +} + + + + + + + + + + + + + + + + + + + +",0,Non-plagiarised +22b41936,45f5632f," + + +import java.io.*; +import java.util.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.util.Collections; +import java.io.InputStreamReader; +import static java.lang.Math.*; +import static java.lang.System.*; + +public class Main1 { + + static ArrayList list1 = new ArrayList<>() ; + static ArrayList list2 = new ArrayList<>() ; + static int n , m ; + static long dp[][] ; + + static long solver(int i , int j ){ + // i = empty chairs + + if (j == m)return 0 ; + int tt1 = n-i ; + int tt2 = m-j ; + if (n-i < m-j)return Long.MAX_VALUE/2 ; + if ( dp[i][j] != -1 )return dp[i][j] ; + + long a = solver(i+1 , j) ; + long b = abs( list1.get(i) - list2.get(j)) + solver(i+1 , j+1) ; + + return dp[i][j] = min(a , b) ; + + } + + + + public static void main(String[] args) throws IOException { + +// try { + + + FastScanner in = new FastScanner(); + PrintWriter out = new PrintWriter(System.out); + + + int N = in.nextInt() ; + int a[] = in.readArray(N) ; + for (int i = 0; i list = new ArrayList<>(); + static boolean A[] = new boolean[2 * 90000001]; + + + static void seive(int n) { + int maxn = n; + //int maxn = 1000000 ; + A[0] = A[1] = true; + + for (int i = 2; i * i <= maxn; i++) { + if (!A[i]) { + for (int j = i * i; j <= maxn; j += i) + A[j] = true; + } + } + + for (int i = 2; i <= maxn; i++) + if (!A[i]) + list.add(i); + + } + + static int findLCA(int a, int b, int par[][], int depth[]) { + if (depth[a] > depth[b]) { + a = a ^ b; + b = a ^ b; + a = a ^ b; + } + int diff = depth[b] - depth[a]; + for (int i = 19; i >= 0; i--) { + if ((diff & (1 << i)) > 0) { + b = par[b][i]; + } + } + if (a == b) + return a; + for (int i = 19; i >= 0; i--) { + if (par[b][i] != par[a][i]) { + b = par[b][i]; + a = par[a][i]; + } + } + return par[a][0]; + } + + static int gcd(int a, int b) { + return (b == 0) ? a : gcd(b, a % b); + } + + static void formArrayForBinaryLifting(int n, int par[][]) { + for (int j = 1; j < 20; j++) { + for (int i = 0; i < n; i++) { + if (par[i][j - 1] == -1) + continue; + par[i][j] = par[par[i][j - 1]][j - 1]; + } + } + } + + + static void sort(int ar[]) { + int n = ar.length; + ArrayList a = new ArrayList<>(); + for (int i = 0; i < n; i++) + a.add(ar[i]); + Collections.sort(a); + for (int i = 0; i < n; i++) + ar[i] = a.get(i); + } + + static void sort1(long ar[]) { + int n = ar.length; + ArrayList a = new ArrayList<>(); + for (int i = 0; i < n; i++) + a.add(ar[i]); + Collections.sort(a); + for (int i = 0; i < n; i++) + ar[i] = a.get(i); + } + + static long ncr(long n, long r, long mod) { + if (r == 0) + return 1; + long val = ncr(n - 1, r - 1, mod); + val = (n * val) % mod; + val = (val * modInverse(r, mod)) % mod; + return val; + } + + static long fast_pow(long base, long n, long M) { + if (n == 0) + return 1; + if (n == 1) + return base % M; + long halfn = fast_pow(base, n / 2, M); + if (n % 2 == 0) + return (halfn * halfn) % M; + else + return (((halfn * halfn) % M) * base) % M; + } + + static long modInverse(long n, long M) { + return fast_pow(n, M - 2, M); + } + + + private static int countDigits(long l) { + if (l >= 1000000000000000000L) return 19; + if (l >= 100000000000000000L) return 18; + if (l >= 10000000000000000L) return 17; + if (l >= 1000000000000000L) return 16; + if (l >= 100000000000000L) return 15; + if (l >= 10000000000000L) return 14; + if (l >= 1000000000000L) return 13; + if (l >= 100000000000L) return 12; + if (l >= 10000000000L) return 11; + if (l >= 1000000000L) return 10; + if (l >= 100000000L) return 9; + if (l >= 10000000L) return 8; + if (l >= 1000000L) return 7; + if (l >= 100000L) return 6; + if (l >= 10000L) return 5; + if (l >= 1000L) return 4; + if (l >= 100L) return 3; + if (l >= 10L) return 2; + return 1; + } + + + static class FastOutput implements AutoCloseable, Closeable, Appendable { + private static final int THRESHOLD = 32 << 10; + private final Writer os; + private StringBuilder cache = new StringBuilder(THRESHOLD * 2); + + public FastOutput append(CharSequence csq) { + cache.append(csq); + return this; + } + + public FastOutput append(CharSequence csq, int start, int end) { + cache.append(csq, start, end); + return this; + } + + private void afterWrite() { + if (cache.length() < THRESHOLD) { + return; + } + flush(); + } + + public FastOutput(Writer os) { + this.os = os; + } + + public FastOutput(OutputStream os) { + this(new OutputStreamWriter(os)); + } + + public FastOutput append(char c) { + cache.append(c); + afterWrite(); + return this; + } + + public FastOutput append(long c) { + cache.append(c); + afterWrite(); + return this; + } + + public FastOutput append(String c) { + cache.append(c); + afterWrite(); + return this; + } + + public FastOutput println() { + return append(System.lineSeparator()); + } + + public FastOutput flush() { + try { + // boolean success = false; + // if (stringBuilderValueField != null) { + // try { + // char[] value = (char[]) stringBuilderValueField.get(cache); + // os.write(value, 0, cache.length()); + // success = true; + // } catch (Exception e) { + // } + // } + // if (!success) { + os.append(cache); + // } + os.flush(); + cache.setLength(0); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + return this; + } + + public void close() { + flush(); + try { + os.close(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + public String toString() { + return cache.toString(); + } + + } + + + static class FastScanner { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(""""); + + String next() { + while (!st.hasMoreTokens()) + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + int[] readArray(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) a[i] = nextInt(); + return a; + } + + + long nextLong() { + return Long.parseLong(next()); + } + } + + +} + + + + + + + +","import java.io.*; +import java.util.*; + +public class A{ + public static void main(String[] args) { + InputStream inputStream = System.in; + OutputStream outputStream = System.out; + InputReader in = new InputReader(inputStream); + PrintWriter out = new PrintWriter(outputStream); + Task solver = new Task(); + solver.solve(in, out); + out.close(); + } + // main solver + static class Task{ + + double eps= 0.00000001; + static final int MAXN = 10000001; + + // stores smallest prime factor for every number + static int spf[] = new int[MAXN]; + + Map> dp= new HashMap<>(); + + // Calculating SPF (Smallest Prime Factor) for every + // number till MAXN. + // Time Complexity : O(nloglogn) + public void sieve() + { + spf[1] = 1; + for (int i=2; i getFactorization(int x) + { + if(dp.containsKey(x)) return dp.get(x); + Set ret = new HashSet<>(); + while (x != 1) + { + if(spf[x]!=2) ret.add(spf[x]); + x = x / spf[x]; + } + dp.put(x,ret); + return ret; + } + // function to find first index >= x + public int lowerIndex(List arr, int n, int x) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr.get(mid) >= x) + h = mid - 1; + else + l = mid + 1; + } + return l; + } + + // function to find last index <= y + public int upperIndex(List arr, int n, int y) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr.get(mid) <= y) + l = mid + 1; + else + h = mid - 1; + } + return h; + } + + // function to count elements within given range + public int countInRange(List arr, int n, int x, int y) + { + // initialize result + int count = 0; + count = upperIndex(arr, n, y) - + lowerIndex(arr, n, x) + 1; + return count; + } + ArrayListA = new ArrayList<>(); + ArrayListB = new ArrayList<>(); + ArrayList> L = new ArrayList>(); + public void solve(InputReader in, PrintWriter out) { + int n= in.nextInt(); + int[] A= new int[n]; + final int INF= 1000000000; + List pos= new ArrayList<>(); + for (int i = 0; i < n; i++){ + A[i]= in.nextInt(); + if (A[i] == 1) pos.add(i); + } + int cnt = pos.size(); + int[][] dp= new int[cnt][n+1]; + for(int[] temp: dp) Arrays.fill(temp,INF); + if (cnt == 0){ + out.println(0); + return; + } + for (int i = n - 1; i >= 0; i--){ + dp[cnt - 1][i] = dp[cnt - 1][i + 1]; + if (A[i] != 1){ + dp[cnt - 1][i] = Math.min(dp[cnt - 1][i], Math.abs(i - pos.get(pos.size()-1))); + } + } + for (int i = cnt - 2; i >= 0; i--){ + for (int j = n - 1; j >= 0; j--){ + dp[i][j] = dp[i][j + 1]; + if (A[j] != 1){ + dp[i][j] = Math.min(dp[i][j], Math.abs(j - pos.get(i)) + dp[i + 1][j + 1]); + } + } + } + out.println(dp[0][0]); + } + void backtrack(PrintWriter out, int i, List considered, int used, int sum, int cur, int g){ + if (used == considered.size()){ + int percentage = (cur * 100) / sum; + if (percentage >= g){ + considered.add(percentage); + L.add(considered); + } + return; + } + if (i == B.size()){ + return; + } + for (int j = i; j < B.size(); j++){ + List au = new ArrayList<>(considered); + au.add(j + 1); + backtrack(out, j + 1, au, used, sum, cur + B.get(j), g); + } + } + public int _gcd(int a, int b) + { + + if(b == 0) { + return a; + } + else { + return _gcd(b, a % b); + } + } + + } + + static class Tuple implements Comparable{ + int x, y, z; + public Tuple(int x, int y, int z){ + this.x= x; + this.y= y; + this.z=z; + } + @Override + public int compareTo(Tuple o){ + return this.x-o.x; + } + } + + static class Pair implements Comparable{ + public int x; + public int y; + public Pair(int x, int y){ + this.x= x; + this.y= y; + } + + @Override + public int compareTo(Pair o) { + return this.x-o.x; + } + } + // fast input reader class; + static class InputReader { + BufferedReader br; + StringTokenizer st; + + public InputReader(InputStream stream) { + br = new BufferedReader(new InputStreamReader(stream)); + } + + public String nextToken() { + while (st == null || !st.hasMoreTokens()) { + String line = null; + try { + line = br.readLine(); + } catch (IOException e) { + throw new RuntimeException(e); + } + if (line == null) { + return null; + } + st = new StringTokenizer(line); + } + return st.nextToken(); + } + + public int nextInt() { + return Integer.parseInt(nextToken()); + } + public double nextDouble(){ + return Double.parseDouble(nextToken()); + } + public long nextLong(){ + return Long.parseLong(nextToken()); + } + } +} + ",0,Non-plagiarised +05f939b3,e647bef7," +//package cf; +import java.io.*; +import java.util.*; +public class D_668 { + static int p=1000000007; + static int dia=Integer.MIN_VALUE; + public static void main(String[] args) throws Exception{ + BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), ""ASCII""), 512); + FastReader sc=new FastReader(); + int t=sc.nextInt(); + StringBuilder sb=new StringBuilder(); + while(t-->0) + { + int n=sc.nextInt(); + int a=sc.nextInt(); + int b=sc.nextInt(); + int da=sc.nextInt(); + int db=sc.nextInt(); + dia=Integer.MIN_VALUE; + int dp[]=new int[(int) n+1]; + List adj[]=new ArrayList[n+1]; + for(int i=0;i<=n;i++) + { + adj[i]=new ArrayList<>(); + } + Set s=new HashSet<>(); + for(int i=1;i=db||2*da>=dia||da>=dp[b]) + sb.append(""Alice\n""); + else + sb.append(""Bob\n""); + + } + System.out.println(sb.toString()); + out.flush(); + } + public static int dfs(int dp[],List adj[],int i,int p,int d) + { + dp[i]=d; + int last_max=0; + for(int v:adj[i]) + { + if(p!=v&&dp[v]==0) + { + int max=1+dfs(dp,adj,v,i,d+1); + dia=Math.max(dia,max+last_max); + last_max=Math.max(last_max,max); + } + } + return last_max; + } + public static int binary_Search_upper(int ar[],int x) + { + int res=-1; + int l=0;int r=ar.length-1; + while(l<=r) + { + int mid=(l+r)>>1; + if(ar[mid]==x) + { + res=mid; + l=mid+1; + } + else if(ar[mid]>x) + { + r=mid-1; + } + else + { + l=mid+1; + } + } + return res; + } + public static int binary_Search_lower(int ar[],int x) + { + int res=-1; + int l=0;int r=ar.length-1; + while(l<=r) + { + int mid=(l+r)>>1; + if(ar[mid]==x) + { + res=mid; + r=mid-1; + } + else if(ar[mid]>x) + { + r=mid-1; + } + else + { + l=mid+1; + } + } + return res; + } + int bit[]=new int[(int)1e6]; + public void update(int n,int val,int i) + { + i++; + while(i0) + { + sum+=bit[i]; + i-=(i)&(-i); + } + return sum; + } + + + /////////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////////////////////// + static class FastReader { + + BufferedReader br; + StringTokenizer st; + + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } + + +} +"," +import java.io.*; +import java.util.*; + +public class Main { + private static final boolean N_CASE = true; + private List> g; + private int a; + private int b; + private int da; + private int db; + private int max; + private int ab; + + private int dfs(int u, int fa, int depth) { + if (u == a) { + ab = depth; + } + + int m1 = 0, m2 = 0; + for (int v : g.get(u)) { + if (v != fa) { + int m = dfs(v, u, depth + 1) + 1; + if (m > m1) { m2 = m1; m1 = m; } + else if (m > m2) { m2 = m; } + } + } + max = Math.max(max, m1 + m2); + int cmax = Math.max(m1, m2); + max = Math.max(max, cmax + depth); + return cmax; + } + + private void solve() { + int n = sc.nextInt(); + a = sc.nextInt() - 1; b = sc.nextInt() - 1; + da = sc.nextInt(); db = sc.nextInt(); + + g = createGraph(n); + for (int i = 0; i < n - 1; ++i) { + int u = sc.nextInt() - 1, v = sc.nextInt() - 1; + g.get(u).add(v); + g.get(v).add(u); + } + max = 0; + dfs(b, -1, 0); + db = Math.min(max, db); + + boolean win = true; + if (ab > da) { + if (db > da * 2) { + win = false; + } + } + + out.println(win ? ""Alice"" : ""Bob""); + } + + private void run() { + int T = N_CASE ? sc.nextInt() : 1; + for (int t = 0; t < T; ++t) { + solve(); + } + } + + private static MyWriter out; + private static MyScanner sc; + + private static class MyScanner { + BufferedReader br; + StringTokenizer st; + + private MyScanner() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + int[] nextIntArray(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = nextInt(); + } + return a; + } + + int[][] nextIntArray(int n, int m) { + int[][] a = new int[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + a[i][j] = sc.nextInt(); + } + } + return a; + } + + long[] nextLongArray(int n) { + long[] a = new long[n]; + for (int i = 0; i < n; i++) { + a[i] = nextLong(); + } + return a; + } + + long[][] nextLongArray(int n, int m) { + long[][] a = new long[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + a[i][j] = nextLong(); + } + } + return a; + } + + List nextList(int n) { + List list = new ArrayList<>(); + for (int i = 0; i < n; i++) { + list.add(nextInt()); + } + return list; + } + + List nextLongList(int n) { + List list = new ArrayList<>(); + for (int i = 0; i < n; i++) { + list.add(nextLong()); + } + return list; + } + + char[] nextCharArray(int n) { + return sc.next().toCharArray(); + } + + char[][] nextCharArray(int n, int m) { + char[][] c = new char[n][m]; + for (int i = 0; i < n; i++) { + String s = sc.next(); + for (int j = 0; j < m; j++) { + c[i][j] = s.charAt(j); + } + } + return c; + } + } + + private static class MyWriter extends PrintWriter { + private MyWriter(OutputStream outputStream) { + super(outputStream); + } + + void printArray(int[] a) { + for (int i = 0; i < a.length; ++i) { + print(a[i]); + print(i == a.length - 1 ? '\n' : ' '); + } + } + + void printArray(long[] a) { + for (int i = 0; i < a.length; ++i) { + print(a[i]); + print(i == a.length - 1 ? '\n' : ' '); + } + } + + void println(int[] a) { + for (int v : a) { + println(v); + } + } + + void print(List list) { + for (int i = 0; i < list.size(); ++i) { + print(list.get(i)); + print(i == list.size() - 1 ? '\n' : ' '); + } + } + + void println(List list) { + list.forEach(this::println); + } + } + + private List> createGraph(int n) { + List> g = new ArrayList<>(); + for (int i = 0; i < n; ++i) { + g.add(new ArrayList<>()); + } + return g; + } + + private void fill(int[][] a, int value) { + for (int[] row : a) { + fill(row, value); + } + } + + private void fill(int[] a, int value) { + Arrays.fill(a, value); + } + + public static void main(String[] args) { + out = new MyWriter(new BufferedOutputStream(System.out)); + sc = new MyScanner(); + new Main().run(); + out.close(); + } +}",0,Non-plagiarised +2bda1866,e4ab72e5,"import java.io.*; +import java.util.*; + +public class Main { + static class Scanner { + Scanner(InputStream in) { this.in = in; } InputStream in; + byte[] bb = new byte[1 << 15]; int i, n; + byte getc() { + if (i == n) { + i = n = 0; + try { n = in.read(bb); } catch (IOException e) {} + } + return i < n ? bb[i++] : 0; + } + int nextInt() { + byte c = 0; while (c <= ' ') c = getc(); + int a = 0; while (c > ' ') { a = a * 10 + c - '0'; c = getc(); } + return a; + } + } + + public static void main(String[] args) throws Exception { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); + + int T = Integer.parseInt(in.readLine()); + + for (int ts=1; ts<=T; ts++) { + int N = Integer.parseInt(in.readLine()); + String A = in.readLine(); + String B = in.readLine(); + + int ans = -1; + + //even number of ops + int fromZero = 0; + int fromOne = 0; + for (int i=0; i vis = new HashSet<>(); + Queue que = new LinkedList<>(); + + vis.add(ez + "" "" + eo + "" "" + nez + "" "" + neo); + que.add(ez + "" "" + eo + "" "" + nez + "" "" + neo); + + int level = 0; + while(que.size() > 0){ + for(int s = que.size() - 1;s >= 0;s--) { + String[] rem = que.remove().split("" ""); + int v1 = Integer.parseInt(rem[0]); + int v2 = Integer.parseInt(rem[1]); + int v3 = Integer.parseInt(rem[2]); + int v4 = Integer.parseInt(rem[3]); + + if (v3 == 0 && v4 == 0) { + System.out.println(level); + return; + } + + String first = (v4) + "" "" + (v3 + 1) + "" "" + (v2 - 1) + "" "" + (v1); + if (vis.add(first)) que.add(first); + + String second = (v4 - 1) + "" "" + (v3) + "" "" + (v2) + "" "" + (v1 + 1); + if (vis.add(second)) que.add(second); + } + level++; + } + + System.out.println(-1); + } + + public static void main(String[] args) throws Exception { + int tests = Integer.parseInt(br.readLine()); + for (int test = 1; test <= tests; test++) { + solve(); + } + } + +// public static ArrayList primes; +// public static void seive(int n){ +// primes = new ArrayList<>(); +// boolean[] arr = new boolean[n + 1]; +// Arrays.fill(arr,true); +// +// for(int i = 2;i * i <= n;i++){ +// if(arr[i]) { +// for (int j = i * i; j <= n; j += i) { +// arr[j] = false; +// } +// } +// } +// for(int i = 2;i <= n;i++) if(arr[i]) primes.add(i); +// } + + +// public static void sort(int[] arr){ +// ArrayList temp = new ArrayList<>(); +// for(int val : arr) temp.add(val); +// +// Collections.sort(temp); +// +// for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i); +// } + +// public static void sort(long[] arr){ +// ArrayList temp = new ArrayList<>(); +// for(long val : arr) temp.add(val); +// +// Collections.sort(temp); +// +// for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i); +// } +// +// public static long power(long a,long b,long mod){ +// if(b == 0) return 1; +// +// long p = power(a,b / 2,mod); +// p = (p * p) % mod; +// +// if(b % 2 == 1) return (p * a) % mod; +// return p; +// } +// public static long modDivide(long a,long b,long mod){ +// return ((a % mod) * (power(b,mod - 2,mod) % mod)) % mod; +// } +// +// public static int GCD(int a,int b){ +// return b == 0 ? a : GCD(b,a % b); +// } +// public static long GCD(long a,long b){ +// return b == 0 ? a : GCD(b,a % b); +// } +// +// public static int LCM(int a,int b){ +// return a * b / GCD(a,b); +// } +// public static long LCM(long a,long b){ +// return a * b / GCD(a,b); +// } +} +",0,Non-plagiarised +a4ec80a0,d314aa92,"import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.PriorityQueue; +import java.util.Scanner; + +public class Armchairs { + + static List zpos ; + static List opos ; + static long[][] dp ; + + public static void main(String[] args) { + // TODO Auto-generated method stub + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + zpos = new ArrayList(); + opos = new ArrayList(); + for (int i = 0; i < n; i++) { + int x = sc.nextInt(); + if (x == 1) opos.add(i); + else zpos.add(i); + } + dp = new long[5001][5001]; + for (int i = 0; i < 5001; i++) Arrays.fill(dp[i], -1); + System.out.println(dp(0, 0, opos.size(), zpos.size(), opos.size())); + } + + private static long dp(int i, int j, int m, int n, int cnt) { + // TODO Auto-generated method stub + if (cnt == 0) return 0; + if (i >= m || j >= n) return Integer.MAX_VALUE; + if (dp[i][j] != -1) return dp[i][j]; + long dns = dp(i, j + 1, m, n, cnt); + long s = Math.abs(zpos.get(j) - opos.get(i)) + dp(i + 1, j + 1, m, n, cnt - 1); + return dp[i][j] = Math.min(dns, s); + } + +} +","import java.io.*; +import java.util.*; + +public class Codeforces { + + public static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader(String s) { + try { + br = new BufferedReader(new FileReader(s)); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + String nextToken() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(nextToken()); + } + + long nextLong() { + return Long.parseLong(nextToken()); + } + + double nextDouble() { + return Double.parseDouble(nextToken()); + } + } + + + static FastReader f = new FastReader(); + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static StringBuilder sb = new StringBuilder(""""); + private static int m = (int) 1e9 + 7; + static int MAX = 500005; + static long[] fact; + + static int[] inputArray(int n) throws IOException { + int[] a = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = f.nextInt(); + } + return a; + } + + private static class SegmentTree { + int[] array; + int[] tree; + int n; + int max; + + SegmentTree(int a[], int n) { + this.tree = new int[4 * n + 1]; + this.array = a; + this.n = n; + buildTree(); + } + + void buildTree() { + buildTreeHelper(0, 0, n - 1); + } + + void buildTreeHelper(int i, int start, int end) { + if (start > end) { + return; + } + if (start == end) { + tree[i] = array[start]; + return; + } + int mid = (start + end) / 2; + buildTreeHelper(2 * i + 1, start, mid); + buildTreeHelper(2 * i + 2, mid + 1, end); + tree[i] = Math.max(tree[2 * i + 1], tree[2 * i + 2]); + } + + char overlap(int start, int end, int qs, int qe) { + if (qe < start || qs > end || start > end) { + return 0; + } + if (qs <= start && qe >= end) { + return 2; + } + return 1; + } + + int query(int start, int end) { + return andQueryHelper(0, 0, n - 1, start, end); + } + + int andQueryHelper(int i, int start, int end, int qs, int qe) { + if (overlap(start, end, qs, qe) == 0) { + return 0; + } + if (overlap(start, end, qs, qe) == 1) { + int mid = (start + end) / 2; + return Math.max(andQueryHelper(2 * i + 1, start, mid, qs, qe), + andQueryHelper(2 * i + 2, mid + 1, end, qs, qe)); + } else { + return tree[i]; + } + } + } + + static int query(int l, int r) { + System.out.println(""? "" + l + "" "" + r); + System.out.flush(); + int res = f.nextInt(); + System.out.flush(); + return res; + } + + static long gcd(long a , long b) { + if(a == 0 || b == 0) { + return Math.max(a , b); + } + //System.out.println(""a - "" + a + "" b - "" + b); + if(a % b == 0) { + return b; + } + return gcd(b , a % b); + } + + public static void main(String[] args) throws IOException { + +// System.out.println(gcd(11, 3)); + + int t = 1; + while(t-- != 0) { + int N = f.nextInt(); + int a[] = inputArray(N); + ArrayList space = new ArrayList<>(); + ArrayList fill = new ArrayList<>(); + for(int i = 0 ; i < N; i++) { + if(a[i] == 0) { + space.add(i); + } + else { + fill.add(i); + } + } + int n = fill.size(), m = space.size(); + int[][] dp = new int[n+1][m+1]; + // i - no of filled + // j is no of space + Arrays.fill(dp[0], 0); + for(int i = 1 ; i <= n ; i++) { + dp[i][0] = -1; + } + for(int i = 1 ; i <= n ; i++) { + for(int j = 1 ; j <= m ; j++) { + if(i > j) { + dp[i][j] = -1; + continue; + } + dp[i][j] = Integer.MAX_VALUE; + if(dp[i-1][j-1] != -1) { + dp[i][j] = Math.min(dp[i][j], dp[i-1][j-1]) + Math.abs(fill.get(i-1) - space.get(j-1)); + } + if(dp[i][j-1] != -1) { + dp[i][j] = Math.min(dp[i][j], dp[i][j-1]); + } + } + } + + System.out.println(dp[n][m]); + + } + + System.out.println(sb); + + } + +} + +/* +5 +2 1 +1 1 +500 4 +217871987498122 10 +100000000000000001 1 + */ + + +",0,Non-plagiarised +4da08761,d6fb3b9e,"import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Arrays; + +/** + * Built using CHelper plug-in + * Actual solution is at the top + */ +public class Solution { + public static void main(String[] args) { + InputStream inputStream = System.in; + OutputStream outputStream = System.out; + InputReader in = new InputReader(inputStream); + PrintWriter out = new PrintWriter(outputStream); + Main solver = new Main(); + boolean multipleTC = true; + int testCount = multipleTC ? Integer.parseInt(in.next()) : 1; + for (int i = 1; i <= testCount; i++) + solver.solve(in, out, i); + out.close(); + } + static class Main { + PrintWriter out; + InputReader in; + + public void solve(InputReader in, PrintWriter out, int test) { + this.out = out; + this.in = in; + int n = ni(); + String[] arr = new String[n]; + int[][] freq = new int[n][5]; + int[][] rem = new int[n][5]; + for(int i = 0; i < n; i++){ + arr[i] = n(); + for(int j = 0; j < arr[i].length(); j++) + freq[i][arr[i].charAt(j) - 'a']++; + for(int j = 0; j < 5; j++) + rem[i][j] = arr[i].length() - freq[i][j]; + } + int ans = 0; + for(int i = 0; i < 5; i++){ + int[] vals = new int[n]; + for(int j = 0; j < n; j++) + vals[j] = freq[j][i] - rem[j][i]; + Arrays.sort(vals); + int sum = 0, x = 0; + for(int j = n - 1; j >= 0; j--){ + if(sum + vals[j] > 0){ + x++; + sum += vals[j]; + } else { + break; + } + } + if(x > ans) { + ans = x; + } + } + pn(ans); + } + + int gcd(int a, int b) + { + if (a == 0) + return b; + + return gcd(b%a, a); + } + + + String n(){ + return in.next(); + } + + int ni() { + return in.nextInt(); + } + + long nl() { + return in.nextLong(); + } + + void pn(long zx) { + out.println(zx); + } + + void pn(String sz) { + out.println(sz); + } + + void pn(double dx){ + out.println(dx); + } + + class Tuple { + long x; + long y; + + Tuple(long a, long b) { + x = a; + y = b; + } + } + + } + + static class InputReader { + private InputStream stream; + private byte[] buf = new byte[1024]; + private int curChar; + private int numChars; + + public InputReader(InputStream stream) { + this.stream = stream; + } + + public int read() { + if (numChars == -1) { + throw new UnknownError(); + } + if (curChar >= numChars) { + curChar = 0; + try { + numChars = stream.read(buf); + } catch (IOException e) { + throw new UnknownError(); + } + if (numChars <= 0) { + return -1; + } + } + return buf[curChar++]; + } + + public int nextInt() { + return Integer.parseInt(next()); + } + + public long nextLong() { + return Long.parseLong(next()); + } + + public String next() { + int c = read(); + while (isSpaceChar(c)) { + c = read(); + } + StringBuffer res = new StringBuffer(); + do { + res.appendCodePoint(c); + c = read(); + } while (!isSpaceChar(c)); + + return res.toString(); + } + + private boolean isSpaceChar(int c) { + return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; + } + + } +}","import java.util.*; +public class Sol +{ + 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][5]; + int tot[]=new int[n]; + for(int i=0;i0) + res+=ans[j++]; + return j; + } +} +",0,Non-plagiarised +63bfa731,9fc811f7," +import java.util.*; +import java.io.*; + +public class D { + static ArrayList adj[] = new ArrayList[(int)1e5+7]; + static int diameter = 0; + static int[] depth = new int[(int)1e5 + 7]; + public static void main(String[] args) { + FastReader in = new FastReader(); + int t = in.nextInt(); + while(t-- > 0){ + int n = in.nextInt(); + int a = in.nextInt(), b = in.nextInt(), da = in.nextInt(), db = in.nextInt(); + for(int i = 1; i <= n; i++){ + adj[i] = new ArrayList<>(); + } + for(int i = 1; i <=n; i++){ + adj[i].clear(); + } + for(int i = 0; i < n- 1; i++){ + int u = in.nextInt(); + int v = in.nextInt(); + adj[u].add(v); + adj[v].add(u); + } + diameter = 0; + depth[a] = 0; + dfs(a, -1); + System.out.println(2 * da >= Math.min(diameter, db) || depth[b] <= da ? ""Alice"" : ""Bob""); + } + } + static int dfs(int node, int parent){ + int len = 0; + for(int x : adj[node]){ + if(x != parent){ + depth[x] = depth[node] + 1; + int cur = 1 + dfs(x, node); + diameter = Math.max(diameter, cur + len); + len = Math.max(len, cur); +// System.out.print(""x "" + x + "" node "" + node + "" par "" + parent); +// System.out.println("" cur "" + cur + "" len "" + len + "" diam "" + diameter); + } + } + return len; + } + static long getDigitSum(long n) + { + long sum = 0; + + while (n > 0) + { + sum = sum + n % 10; + n = n/10; + } + + return sum; + } + + static class FastReader + { + BufferedReader br; + StringTokenizer st; + + public FastReader() + { + br = new BufferedReader(new + InputStreamReader(System.in)); + } + + String next() + { + while (st == null || !st.hasMoreElements()) + { + try + { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() + { + return Integer.parseInt(next()); + } + + long nextLong() + { + return Long.parseLong(next()); + } + + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str = """"; + try + { + str = br.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + return str; + } + int[] readArray(int n){ + int[] a = new int[n]; + for(int i = 0; i < n; i++){ + a[i] = nextInt(); + } + return a; + } + } +} +","import java.io.OutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.util.StringTokenizer; +import java.io.IOException; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.io.InputStream; + +public class Main { + public static void main(String[] args) { + InputStream inputStream = System.in; + OutputStream outputStream = System.out; + InputReader in = new InputReader(inputStream); + PrintWriter out = new PrintWriter(outputStream); + DTreeTag solver = new DTreeTag(); + int testCount = Integer.parseInt(in.next()); + for (int i = 1; i <= testCount; i++) + solver.solve(i, in, out); + out.close(); + } + + static class DTreeTag { + int diam = 0; + + public int dfs(ArrayList g[], int x, int depth[], int p) { + int len = 0; + for (int y : g[x]) { + if (y != p) { + depth[y] = depth[x] + 1; + int cur = 1 + dfs(g, y, depth, x); + diam = Math.max(diam, cur + len); + len = Math.max(len, cur); + } + } + return len; + } + + public void solve(int testNumber, InputReader in, PrintWriter out) { + int n = in.nextInt(); + int a = in.nextInt() - 1; + int b = in.nextInt() - 1; + int da = in.nextInt(); + int db = in.nextInt(); + int dis[] = new int[n]; + ArrayList g[] = new ArrayList[n]; + for (int i = 0; i < n; i++) g[i] = new ArrayList<>(); + for (int i = 0; i < n - 1; i++) { + int u = in.nextInt() - 1; + int v = in.nextInt() - 1; + g[u].add(v); + g[v].add(u); + } + diam = 0; + dfs(g, a, dis, -1); + int disb = dis[b]; + if (2 * da >= Math.min(diam, db) || disb <= da) { + out.println(""Alice""); + } else { + out.println(""Bob""); + } + + } + + } + + static class InputReader { + public BufferedReader reader; + public StringTokenizer tokenizer; + + public InputReader(InputStream stream) { + reader = new BufferedReader(new InputStreamReader(stream), 32768); + tokenizer = null; + } + + public String next() { + while (tokenizer == null || !tokenizer.hasMoreTokens()) { + try { + tokenizer = new StringTokenizer(reader.readLine()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + return tokenizer.nextToken(); + } + + public int nextInt() { + return Integer.parseInt(next()); + } + + } +} + +",1,Plagiarised +392218ef,f0ede32a,"import java.util.*; +import java.lang.*; +import java.io.*; +public class Main +{ + static void sort(int a[]){ + ArrayList arr=new ArrayList<>(); + for(int i=0;i=0){ + int n=Integer.parseInt(br.readLine()); + int a[]=new int[n]; + StringTokenizer st=new StringTokenizer(br.readLine()); + for(int i=0;i inc=new ArrayList<>(); + ArrayList dec=new ArrayList<>(); + + for(int i=0;ip)flag=true; + p--; + } + p=1; + for(int i=0;i0){ + int n = sc.nextInt(); + Integer[] arr = new Integer[n]; + for(int i=0;i blue = new ArrayList<>(); + List red = new ArrayList<>(); + + for(int i=0;i=0;i--){ + if(red.get(i)>q){ + flag = false; + break; + } + q--; + } + + for(int i=0;i0) + { + int n=sc.nextInt(); + int[] c=new int[n]; + for(int i=0;i0) + { + long cur=sum+rem[0]*mn[0]+rem[1]*mn[1]; + ans=Math.min(ans, cur); + } + } + System.out.println(ans); + } + } +}","import java.util.Scanner; + +public class C1499 { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + int t = in.nextInt(); + while (t-- > 0) { + int n = in.nextInt(); + // int[] arr = new int[n]; + long[] mn = { Long.MAX_VALUE, Long.MAX_VALUE }; + long[] rem = { n, n }; + long sum = 0; + long ans = Long.MAX_VALUE; + for (int i = 0; i < n; i++) { + int temp = in.nextInt(); + mn[i % 2] = Math.min(mn[i % 2], temp); + rem[i % 2]--; + sum += temp; + if (i > 0) { + long cur = sum + rem[0] * mn[0] + rem[1] * mn[1]; + ans = Math.min(ans, cur); + } + } + System.out.println(ans); + +// int a = Integer.MAX_VALUE; +// int aIndex = -1; +// int b = Integer.MAX_VALUE; +// int bIndex = -1; +// +// for (int i = 0; i < n; i++) { +// arr[i] = in.nextInt(); +// if (i % 2 == 0) { +// if (arr[i] < a) { +// a = arr[i]; +// aIndex = i; +// } +// } else { +// if (arr[i] < b) { +// b = arr[i]; +// bIndex = i; +// } +// } +// } +// int sum = 0; +// for (int i = 0; i < Math.max(bIndex, aIndex) + 1; i++) { +// if (i % 2 == 0) { +// if (i == aIndex) { +// if (aIndex < bIndex) { +// sum += (n - (i / 2) - ((bIndex - aIndex) / 2)) * arr[i]; +// } else { +// sum += (n - (i / 2)) * arr[i]; +// } +// } else { +// sum += arr[i]; +// } +// } else { +// if (i == bIndex) { +// if (bIndex < aIndex) { +// sum += (n - (i / 2) - ((aIndex - bIndex) / 2)) * arr[i]; +// } else { +// sum += (n - (i / 2)) * arr[i]; +// } +// } else { +// sum += arr[i]; +// } +// } +// +// } +// System.out.println(sum); +// for (int i = 0; i < n; i++) { +// arr[i] = in.nextInt(); +// if (arr[i] < a) { +// a = arr[i]; +// aIndex = i; +// +// } +// +// } +// if (aIndex == 0) { +// +// for (int i = 1; i < n; i++) { +// if (arr[i] < b) { +// b = arr[i]; +// bIndex = i; +// } +// } +// System.out.println(aIndex + "" "" + bIndex); +// System.out.println(a + "" "" + b); +// int sum = 0; +// for (int i = 1; i < bIndex; i++) { +// sum += arr[i]; +// } +// sum += b * (n - bIndex + 1); +// sum += a * n; +// System.out.println(sum); +// } else { +// int b = Integer.MAX_VALUE; +// int bIndex = -1; +// for (int i = 0; i < aIndex; i++) { +// if (arr[i] < b) { +// b = arr[i]; +// bIndex = i; +// } +// } +// System.out.println(aIndex + "" "" + bIndex); +// System.out.println(a + "" "" + b); +// int sum = 0; +// for (int i = 0; i < bIndex; i++) { +// sum += arr[i]; +// } +// sum += b * (n - bIndex); +// sum += a * n; +// System.out.println(sum); +// } + + } + } +} + +",1,Plagiarised +26e699de,fcc7e8fa,"//package Task1; + +import java.util.Scanner; + +public class Menorah { + static int MOD9= 1000000000; + + public static void main(String[] args){ + + Scanner sc= new Scanner(System.in); + int numberTest=sc.nextInt(); + while(numberTest-->0){ + int n=sc.nextInt(); + char[] s=new char[n+5]; + char[] t=new char[n+5]; + String ss=sc.next(); + String tt=sc.next(); + s=ss.toCharArray(); + t=tt.toCharArray(); + int cntax = 0, cntbx = 0, same = 0; + int ans=MOD9; + for(int i=0; i 0) { + solver.call(in,out); + t--; + } + out.close(); + } + + static class TaskA { + public void call(InputReader in, PrintWriter out) { + int n, _00 = 0, _01 = 0, _11 = 0, _10 = 0; + n = in.nextInt(); + char[] s = in.next().toCharArray(); + char[] s1 = in.next().toCharArray(); + + for (int i = 0; i < n; i++) { + if(s[i]==s1[i]){ + if(s[i]=='0'){ + _00++; + } + else{ + _11++; + } + } + else{ + if(s[i]=='0'){ + _01++; + } + else{ + _10++; + } + } + } + int ans = Integer.MAX_VALUE; + + if(_10 ==_01){ + ans = 2*_01; + } + if(_11 == _00 + 1){ + ans = Math.min(ans, 2*_00 + 1); + } + + if(ans == Integer.MAX_VALUE){ + out.println(-1); + } + else{ + out.println(ans); + } + } + } + + static int gcd(int a, int b) + { + if (a == 0) + return b; + return gcd(b % a, a); + } + + static int lcm(int a, int b) + { + return (a / gcd(a, b)) * b; + } + + static class answer implements Comparable{ + int a, b; + + public answer(int a, int b) { + this.a = a; + this.b = b; + } + + @Override + public int compareTo(answer o) { + return o.a - this.a; + } + } + + static class arrayListClass { + ArrayList arrayList2 ; + + public arrayListClass(ArrayList arrayList) { + this.arrayList2 = arrayList; + } + } + + static long gcd(long a, long b) + { + if (b == 0) + return a; + return gcd(b, a % b); + } + + static void sort(long[] a) { + ArrayList l=new ArrayList<>(); + for (long i:a) l.add(i); + Collections.sort(l); + for (int i=0; i0) { + int n = sc.nextInt(); + char[] s = new char[n]; + char[] t = new char[n]; + s = sc.next().toCharArray(); + t = sc.next().toCharArray(); + int a = 0, b = 0, c = 0, d = 0; + for(int i = 0; i < n; i++) { + if(s[i] == '0' && t[i] == '0') a++; + if(s[i] == '1' && t[i] == '0') b++; + if(s[i] == '0' && t[i] == '1') c++; + if(s[i] == '1' && t[i] == '1') d++; + } + int res = Integer.MAX_VALUE; + if(b == c || b+1 == c) { + if((b + c) % 2 == 0) { + res = Math.min(res, b + c); + } + } + if(a == d || a+1 == d) { + if((a + d) % 2 == 1) { + res = Math.min(res, a + d); + } + } + if(res == Integer.MAX_VALUE) System.out.println(-1); + else System.out.println(res); + } + } + + static class FastScanner { + public BufferedReader reader; + public StringTokenizer tokenizer; + public FastScanner() { + reader = new BufferedReader(new InputStreamReader(System.in), 32768); + tokenizer = null; + } + public String next() { + while (tokenizer == null || !tokenizer.hasMoreTokens()) { + try { + tokenizer = new StringTokenizer(reader.readLine()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + return tokenizer.nextToken(); + } + public int nextInt() { + return Integer.parseInt(next()); + } + public long nextLong() { + return Long.parseLong(next()); + } + public double nextDouble() { + return Double.parseDouble(next()); + } + public String nextLine() { + try { + return reader.readLine(); + } catch(IOException e) { + throw new RuntimeException(e); + } + } + } + +} +","// हर हर महादेव +import java.util.*; +import java.lang.*; +import java.io.*; +import java.math.BigInteger; +import java.text.DecimalFormat; + +public final class Solution { + + static int inf = Integer.MAX_VALUE; + static long mod = 1000000000 + 7; + + static void ne(Scanner sc, BufferedWriter op) throws Exception { + int n=sc.nextInt(); + String one=sc.next(); + String two=sc.next(); + int _10=0; + int _00=0; + int _11=0; + int _01=0; + for(int i=0;i 0) + return true; + else + return false; + } + + static int gcd(int a, int b) + { + if (a == 0) + return b; + return gcd(b % a, a); + } + + + public static void main(String[] args) throws Exception { + BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out)); + // Reader sc = new Reader(); + Scanner sc= new Scanner(System.in); + int t = sc.nextInt(); + while (t-->0){ ne(sc, op); } + + // ne(sc,op); + + + op.flush(); + } + + static void print(Object o) { + System.out.println(String.valueOf(o)); + } + static int[] toIntArr(String s){ + int[] val= new int[s.length()]; + for(int i=0;i list= new ArrayList<>(); + for(int i=0;i list= new ArrayList<>(); + for(int i=0;i { + public int compare(pair p1, pair p2) { + if (p1.yy > p2.yy) { + return 1; + } else if (p1.yy == p2.yy) { + if (p1.xx > p2.xx) { + return 1; + } else if (p1.xx < p2.xx) { + return -1; + } + return 0; + + } + return -1; + } +} + +class sortX implements Comparator { + public int compare(pair p1, pair p2) { + if (p1.xx > p2.xx) { + return 1; + } else if (p1.xx == p2.xx) { + if (p1.yy > p2.yy) { + return 1; + } else if (p1.yy < p2.yy) { + return -1; + } + return 0; + + } + return -1; + } +} + +class debug { + + static void print1d(long[] arr) { + System.out.println(); + for (int i = 0; i < arr.length; i++) { + System.out.print(arr[i] + "" ""); + } + + } + + static void print1d(int[] arr) { + System.out.println(); + for (int i = 0; i < arr.length; i++) { + System.out.println(i+"" = ""+arr[i]); + } + + } + + static void print1d(boolean[] arr) { + System.out.println(); + for (int i = 0; i < arr.length; i++) { + System.out.println(i + ""= "" + arr[i]); + } + + } + + static void print2d(int[][] arr) { + System.out.println(); + int n = arr.length; + int n2 = arr[0].length; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n2; j++) { + System.out.print(arr[i][j] + "" ""); + } + System.out.println(); + } + + } + static void print2d(boolean[][] arr) { + System.out.println(); + int n = arr.length; + int n2 = arr[0].length; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n2; j++) { + System.out.print(arr[i][j] + "" ""); + } + System.out.println(); + } + + } + + static void print2d(long[][] arr) { + System.out.println(); + int n = arr.length; + int n2 = arr[0].length; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n2; j++) { + System.out.print(arr[i][j] + "" ""); + } + System.out.println(); + } + + } + static void printPair(ArrayList list) { + if(list.size()==0){ + System.out.println(""empty list""); + return; + } + System.out.println(); + for(int i=0;i= '0' && c <= '9'); + + if (neg) + return -ret; + return ret; + } + + public long nextLong() throws IOException { + long ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + if (neg) + return -ret; + return ret; + } + + public double nextDouble() throws IOException { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') { + while ((c = read()) >= '0' && c <= '9') { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) + return -ret; + return ret; + } + + private void fillBuffer() throws IOException { + bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); + if (bytesRead == -1) + buffer[0] = -1; + } + + private byte read() throws IOException { + if (bufferPointer == bytesRead) + fillBuffer(); + return buffer[bufferPointer++]; + } + + public void close() throws IOException { + if (din == null) + return; + din.close(); + } +} + class MultiTreeSet { + TreeMap freqTreeMap = new TreeMap(); + int size; + + public MultiTreeSet() {} + + public MultiTreeSet(Collection c) { + for(E element : c) + add(element); + } + + public int size() { + return size; + } + + public void add(E element) { + Integer freq = freqTreeMap.get(element); + if(freq==null) + freqTreeMap.put(element, 1); + else + freqTreeMap.put(element,freq+1); + ++size; + } + + public void remove(E element) { + Integer freq = freqTreeMap.get(element); + if(freq!=null) { + if(freq==1) + freqTreeMap.remove(element); + else + freqTreeMap.put(element, freq-1); + --size; + } + } + + public int get(E element) { + Integer freq = freqTreeMap.get(element); + if(freq==null) + return 0; + return freq; + } + + public boolean contains(E element) { + return get(element)>0; + } + + public boolean isEmpty() { + return size==0; + } + + public E first() { + return freqTreeMap.firstKey(); + } + + public E last() { + return freqTreeMap.lastKey(); + } + + public E ceiling(E element) { + return freqTreeMap.ceilingKey(element); + } + + public E floor(E element) { + return freqTreeMap.floorKey(element); + } + + public E higher(E element) { + return freqTreeMap.higherKey(element); + } + + public E lower(E element) { + return freqTreeMap.lowerKey(element); + } +} + ",0,Non-plagiarised +11c2ab99,4f7af821,"import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.StringTokenizer; + +public class Main { + + public static void main(String[] args) { + FastScanner fs=new FastScanner(); + int T=fs.nextInt(); + PrintWriter out=new PrintWriter(System.out); + for (int tt=0; tt=0; i--) + forced[i]=Math.min(forced[i], forced[i+1]+1); + for (int i=0; i l=new ArrayList<>(); + for (int i:a) l.add(i); + Collections.sort(l); + for (int i=0; i0){ + int n = scanner.nextInt(); + int k = scanner.nextInt(); + int[] c = new int[n]; + Arrays.fill(c,inf); + int[] a = new int[k]; + int[] b = new int[k]; + for(int i=0;i=0;--i){ + c[i] = Math.min(c[i],c[i+1]+1); + } + for(int i=0;i= w && black_dominoes >= b) + { + System.out.println(""YES""); + } + else + System.out.println(""NO""); + } + scanner.close(); + } + + public static void solution2(){ + Scanner scanner = new Scanner(System.in); + int t = scanner.nextInt(); + scanner.nextLine(); + while (t-- != 0) { + String s; + s = scanner.nextLine(); + boolean flag = true; + int first_ones = -1, last_zeroes = -1; + for (int i=0; i0; i--){ + if (s.charAt(i) == '0' && s.charAt(i-1) == '0'){ + last_zeroes = i-1; + break; + } + } + if (first_ones != -1 && last_zeroes != -1 && first_ones < last_zeroes) flag = false; + if (flag) System.out.println(""YES""); + else System.out.println(""NO""); + } + scanner.close(); + } + + public static void solution3(){ + Scanner scanner = new Scanner(System.in); + int t = scanner.nextInt(); + while (t-- > 0){ + int n; + n = scanner.nextInt(); + int[] costs = new int[n]; + for (int i=0; i 0){ + if (b%2 == 1){ + ans *= a; + } + a *= a; + b >>= 1; + } + return ans; + } + + public static void main(String[] args) { + solution3(); + } +}","/*input +3 +2 +13 88 +3 +2 3 1 +5 +4 3 2 1 4 + +*/ +import java.util.*; +import java.lang.*; +import java.io.*; + +public class Main +{ + static PrintWriter out; + static int MOD = 1000000007; + static FastReader scan; + + /*-------- I/O using short named function ---------*/ + public static String ns(){return scan.next();} + public static int ni(){return scan.nextInt();} + public static long nl(){return scan.nextLong();} + public static double nd(){return scan.nextDouble();} + public static String nln(){return scan.nextLine();} + public static void p(Object o){out.print(o);} + public static void ps(Object o){out.print(o + "" "");} + public static void pn(Object o){out.println(o);} + /*-------- for output of an array ---------------------*/ + static void iPA(int arr []){ + StringBuilder output = new StringBuilder(); + for(int i=0; i0){ + int n = ni(); + long arr[] = new long[n]; + lIA(arr); + long ans = (long)(n*(arr[0] + arr[1])); + long sum = arr[0] + arr[1]; + long emin = arr[0], omin = arr[1]; + + for(int i=2; i0) { + int n = sc.nextInt(), k = sc.nextInt(); + int[] a = sc.nextIntArray(k); + int[] t = sc.nextIntArray(k); + + int[] array = new int[n]; + Arrays.fill(array, Integer.MAX_VALUE); + for(int i = 0; i < k; i++) + array[a[i] - 1] = t[i]; + + int[] pre = new int[n]; + int[] post = new int[n]; + + int prev = (int)2e9; + for(int i = 0; i < n; i++) + prev = pre[i] = Math.min(prev + 1, array[i]); + + prev = (int)2e9; + for(int i = n - 1; i >= 0; i--) + prev = post[i] = Math.min(prev + 1, array[i]); + + for(int i = 0; i < n; i++) + array[i] = Math.min(pre[i], post[i]); + + for(int i = 0; i < n; i++) + pw.print(array[i] + (i == n - 1 ? ""\n"" : "" "")); + } + + pw.flush(); + } + + public static class Scanner { + StringTokenizer st; + BufferedReader br; + + public Scanner(InputStream system) { + br = new BufferedReader(new InputStreamReader(system)); + } + + public Scanner(String file) throws Exception { + br = new BufferedReader(new FileReader(file)); + } + + public String next() throws IOException { + while (st == null || !st.hasMoreTokens()) + st = new StringTokenizer(br.readLine()); + return st.nextToken(); + } + + public String nextLine() throws IOException { + return br.readLine(); + } + + public int nextInt() throws IOException { + return Integer.parseInt(next()); + } + + public double nextDouble() throws IOException { + return Double.parseDouble(next()); + } + + public char nextChar() throws IOException { + return next().charAt(0); + } + + public long nextLong() throws IOException { + return Long.parseLong(next()); + } + + public int[] nextIntArray(int n) throws IOException { + int[] array = new int[n]; + for (int i = 0; i < n; i++) + array[i] = nextInt(); + return array; + } + + public Integer[] nextIntegerArray(int n) throws IOException { + Integer[] array = new Integer[n]; + for (int i = 0; i < n; i++) + array[i] = new Integer(nextInt()); + return array; + } + + public long[] nextLongArray(int n) throws IOException { + long[] array = new long[n]; + for (int i = 0; i < n; i++) + array[i] = nextLong(); + return array; + } + + public double[] nextDoubleArray(int n) throws IOException { + double[] array = new double[n]; + for (int i = 0; i < n; i++) + array[i] = nextDouble(); + return array; + } + + public static int[] shuffle(int[] a) { + int n = a.length; + Random rand = new Random(); + for (int i = 0; i < n; i++) { + int tmpIdx = rand.nextInt(n); + int tmp = a[i]; + a[i] = a[tmpIdx]; + a[tmpIdx] = tmp; + } + return a; + } + + public boolean ready() throws IOException { + return br.ready(); + } + + public void waitForInput() throws InterruptedException { + Thread.sleep(3000); + } + } +} +","import java.util.*; + +public class file +{ + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while(t!=0) + { + sc.nextLine(); + int n = sc.nextInt(); + int k = sc.nextInt(); + int[] position = new int[k]; + for(int i = 0;i < k;i++) + { + position[i] = sc.nextInt(); + } + int[] temprature = new int[k]; + for(int i = 0;i < k;i++) + { + temprature[i] = sc.nextInt(); + } + int[] left = new int[n]; + int[] right = new int[n]; + int c[] = new int[n]; + Arrays.fill(c, Integer.MAX_VALUE); + for(int i = 0;i < k;i++) + { + c[position[i]-1] = temprature[i]; + } + long min = Integer.MAX_VALUE - 1; + for(int i = 0;i < n;i++) + { + min = Math.min(min+1, c[i]); + left[i] = (int)min; + + } + min = Integer.MAX_VALUE - 1; + for(int i = n-1;i >= 0;i--) + { + min = Math.min(min+1, c[i]); + right[i] = (int)min; + } + for(int i = 0;i < n;i++) + { + System.out.print(Math.min(left[i], right[i])+"" ""); + } + System.out.println(); + t--; + } + } +}",0,Non-plagiarised +6f393cfe,f38ae053,"import java.io.*; +import java.util.*; + +public class C { + + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + try{ + int t = Integer.parseInt(br.readLine()); + while(t-->0){ + int n = Integer.parseInt(br.readLine()); + int lst[][] = new int[n][5]; + for(int i=0; i=0; x--){ + sum+=val[x]; + if(sum>0){ + ans++; + }else{ + break; + } + } + fans = Math.max(fans, ans); + } + bw.write(fans+""\n""); + } + bw.flush(); + }catch(Exception e){ + return; + } + } + +}","import java.util.*; +import java.io.*; +public class Fixed_Points +{ + + public static void process()throws IOException + { + int n=I(); + String s[]=new String[n]; + for(int i=0;i po=new ArrayList(); + ArrayList ne=new ArrayList(); + for(int j=0;jf2) + po.add(f1-f2); + else + ne.add(f1-f2); + } + Collections.sort(po); + Collections.reverse(po); + Collections.sort(ne); + Collections.reverse(ne); + int sum=0; + for(int i=0;i0) + { + sum=sum+ne.get(i); + tp++; + } + } + t=tp+po.size(); + max=Math.max(max, t); + } + pn(max); + + } + static Scanner sc = new Scanner(System.in); + static PrintWriter out = new PrintWriter(System.out); + static void pn(Object o){out.println(o);out.flush();} + static void p(Object o){out.print(o);out.flush();} + static void pni(Object o){out.println(o);System.out.flush();} + static int I() throws IOException{return sc.nextInt();} + static long L() throws IOException{return sc.nextLong();} + static double D() throws IOException{return sc.nextDouble();} + static String S() throws IOException{return sc.next();} + static char C() throws IOException{return sc.next().charAt(0);} + static int[] Ai(int n) throws IOException{int[] arr = new int[n];for (int i = 0; i < n; i++)arr[i] = I();return arr;} + static String[] As(int n) throws IOException{String s[] = new String[n];for (int i = 0; i < n; i++)s[i] = S();return s;} + static long[] Al(int n) throws IOException {long[] arr = new long[n];for (int i = 0; i < n; i++)arr[i] = L();return arr;} + static void dyn(int dp[][],int n,int m,int z)throws IOException {for(int i=0;i0) + {process();}out.flush();out.close();}catch(Exception e){return;}}} +//*-----------------------------------------------------------------------------------------------------------------------------------*// + + +",0,Non-plagiarised +22dff786,624529000,"import java.util.*; + +public class Solution { + + public static void main(String args[]) + + { Scanner s = new Scanner(System.in); + + int t = s.nextInt(); + + while(t-- > 0){ + + int n= s.nextInt(); + + long [] time= new long [n]; + + long [] health= new long [n]; + + for(int i=0; i=0; i--) + + { if(currtime-time[i]>= currhealth) + + { ans+= (currhealth*(currhealth+1))/2; + + currhealth= health[i]; + + currtime= time[i]; + + } + + else if(currtime-time[i]>currhealth-health[i]) + + currhealth+= (currtime-time[i]-currhealth+health[i]); + + } + + ans+= (currhealth*(currhealth+1))/2; + + System.out.println(ans); + + } + + s.close(); + + } + +}","import java.util.*; +import java.io.*; +import java.lang.*; +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(); + long k[]=new long[n]; + long h[]=new long[n]; + for(int i=0;i=1;i--){ + if(k[i]-k[i-1]>=length){ + curr=curr+length; + ans=ans+(curr*(curr+1))/2; + length=h[i-1]; + curr=0; + } + else{ + length=Math.max(length-(k[i]-k[i-1]),h[i-1]); + curr=curr+k[i]-k[i-1]; + } + } + curr=curr+length; + ans=ans+(curr*(curr+1))/2; + System.out.println(ans); + } + } +} + +",0,Non-plagiarised +0ea5a836,3c74c140,"import java.util.*; +import java.io.*; +public class Solution +{ + static class FastReader + { + BufferedReader br; + StringTokenizer st; + + public FastReader() + { + br = new BufferedReader(new + InputStreamReader(System.in)); + } + + String next() + { + while (st == null || !st.hasMoreElements()) + { + try + { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() + { + return Integer.parseInt(next()); + } + + long nextLong() + { + return Long.parseLong(next()); + } + + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str = """"; + try + { + str = br.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + return str; + } + } + //static final long mod=(long)1e9+7; + static final long mod=998244353L; + public static long pow(long a,long p) + { + long res=1; + while(p>0) + { + if(p%2==1) + { + p--; + res*=a; + res%=mod; + } + else + { + a*=a; + a%=mod; + p/=2; + } + } + return res; + } + static class Pair + { + int u,v; + Pair(int u,int v) + { + this.u=u; + this.v=v; + } + } + /*static class Pair implements Comparable + { + int v,l; + Pair(int v,int l) + { + this.v=v; + this.l=l; + } + public int compareTo(Pair p) + { + return l-p.l; + } + }*/ + static long gcd(long a,long b) + { + if(b%a==0) + return a; + return gcd(b%a,a); + } + public static class comp implements Comparator + { + public int compare(Pair p1,Pair p2) + { + if(p1.v>p2.v) + return 1; + return -1; + } + } + public static void upd1(int a[],int i) + { + for(;i0;i-=(i&(-i))) + s+=a[i]; + return s; + } + public static long q2(long a[],int i) + { + long s=0; + for(;i>0;i-=(i&(-i))) + s+=a[i]; + return s; + } + public static void main(String args[])throws Exception + { + FastReader fs=new FastReader(); + PrintWriter pw=new PrintWriter(System.out); + int tc=fs.nextInt(); + while(tc-->0) + { + int n=fs.nextInt(); + char[][] c=new char[n][]; + for(int i=0;i list[]=new ArrayList[5]; + for(int i=0;i<5;i++) + list[i]=new ArrayList<>(); + for(int i=0;i=0;j--) + { + ans=Math.max(ans,n-1-j); + s+=list[i].get(j); + if(s<=0) + break; + } + if(s>0) + ans=n; + } + pw.println(ans); + } + pw.flush(); + pw.close(); + } +}"," +//package com.company; + + + +import java.math.*; +import java.util.*; +import java.lang.*; +import java.io.*; + + + +public final class Main { + + FastReader s; + + + public static void main (String[] args) throws java.lang.Exception + { + + new Main().run(); + + } + void run() + { + s = new FastReader(); + solve(); + } + StringBuffer sb; + // int counter; + void solve() + { + sb = new StringBuffer(); + + for(int T = s.nextInt();T > 0;T--) + { + + + start(); + + + } + + // System.out.print(sb); + + } + + + void start() + { + + int n = s.nextInt(); + + int mat[][] = new int[n][5]; + for(int i = 0; i x = new ArrayList<>(); + for(int j = 0; j=0) + { + int u = x.get(l); + if(s+u>0) + { + s+=u; + cnt++; + } + else + break; + l--; + } + return cnt; + } + + + + + long gcd(long a, long b) + { + if (b == 0) + return a; + return gcd(b, a % b); + } + long power(long x, long y, long p) + { + long res = 1; // Initialize result + + // Update x if it is more + // than or equal to p + x = x % p; + + while (y > 0) + { + // If y is odd, multiply + // x with the result + if ((y & 1) > 0) + res = (res * x) % p; + + // y must be even now + y = y >> 1; // y = y/2 + x = (x * x) % p; + } + return res; + } + + int lower_bound(int [] arr , int key) + { + + int i = 0; + int j = arr.length-1; + //if(arr[i] > key)return -1; + if(arr[j] < key)return -1; + + while(i 0) { + int n = scan.nextInt(); + int[] l = new int[n + 1]; + int[] r = new int[n + 1]; + for (int i = 1; i <= n; i++) { + l[i] = scan.nextInt(); + r[i] = scan.nextInt(); + } + Graph g = new Graph(n); + for (int i = 0; i < n - 1; i++) { + g.addEdge(scan.nextInt(), scan.nextInt()); + } + sb.append(g.dfs(l, r) + ""\n""); + } + System.out.println(sb); + } +} + +class Graph { + + ArrayList[] node; + int n; + int c = 0; + boolean[] vis; + + Graph(int s) { + n = s + 1; + vis = new boolean[n + 1]; + node = new ArrayList[n + 1]; + for (int i = 0; i < n + 1; i++) { + node[i] = new ArrayList<>(); + } + } + + void addEdge(int u, int v) { + + node[u].add(v); + node[v].add(u); + if (node[u].size() == 1) { + c = u; + } + if (node[v].size() == 1) { + c = v; + } + } + + void cleanVisArray() { + for (int i = 0; i < n + 1; i++) { + vis[i] = false; + } + } + + long dfs(int[] l, int[] r) { + cleanVisArray(); + + long[][] dp = new long[n][2]; + dfsMain(1, dp, l, r); + return Math.max(dp[1][0], dp[1][1]); + } + + void dfsMain(int v, long[][] dp, int[] l, int[] r) { + vis[v] = true; + for (int i : node[v]) { + if (!vis[i]) { + dfsMain(i, dp, l, r); + dp[v][0] += Math.max(Math.abs(l[v] - l[i]) + dp[i][0], Math.abs(l[v] - r[i]) + dp[i][1]); + dp[v][1] += Math.max(Math.abs(r[v] - l[i]) + dp[i][0], Math.abs(r[v] - r[i]) + dp[i][1]); + } + } + } + +} + +class Reader { + + final private int BUFFER_SIZE = 1 << 16; + private DataInputStream din; + private byte[] buffer; + private int bufferPointer, bytesRead; + + public Reader() { + din = new DataInputStream(System.in); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public Reader(String file_name) throws IOException { + din = new DataInputStream(new FileInputStream(file_name)); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public String readLine() throws IOException { + byte[] buf = new byte[64]; // line length + int cnt = 0, c; + while ((c = read()) != -1) { + if (c == '\n') { + break; + } + buf[cnt++] = (byte) c; + } + return new String(buf, 0, cnt); + } + + public int nextInt() throws IOException { + int ret = 0; + byte c = read(); + while (c <= ' ') { + c = read(); + } + boolean neg = (c == '-'); + if (neg) { + c = read(); + } + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (neg) { + return -ret; + } + return ret; + } + + public long nextLong() throws IOException { + long ret = 0; + byte c = read(); + while (c <= ' ') { + c = read(); + } + boolean neg = (c == '-'); + if (neg) { + c = read(); + } + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + if (neg) { + return -ret; + } + return ret; + } + + public double nextDouble() throws IOException { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') { + c = read(); + } + boolean neg = (c == '-'); + if (neg) { + c = read(); + } + + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') { + while ((c = read()) >= '0' && c <= '9') { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) { + return -ret; + } + return ret; + } + + private void fillBuffer() throws IOException { + bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); + if (bytesRead == -1) { + buffer[0] = -1; + } + } + + private byte read() throws IOException { + if (bufferPointer == bytesRead) { + fillBuffer(); + } + return buffer[bufferPointer++]; + } + + public void close() throws IOException { + if (din == null) { + return; + } + din.close(); + } +} +","import java.io.*; + + + + +import java.util.*; +/* + + + + */ + + + + + + + + + + public class C { + static FastReader sc=null; + + public static void main(String[] args) { + sc=new FastReader(); + int t=sc.nextInt(); + while(t-->0) { + int n=sc.nextInt(); + Node nodes[]=new Node[n]; + for(int i=0;i adj=new ArrayList<>(); + + Node(int id,int l,int r){ + this.id=id; + this.l=l; + this.r=r; + } + } + + + + static void ruffleSort(int a[]) { + ArrayList al=new ArrayList<>(); + for(int i:a)al.add(i); + Collections.sort(al); + for(int i=0;i al=new ArrayList<>(); + for(int i:a)al.add(i); + Collections.sort(al,Collections.reverseOrder()); + for(int i=0;i0){ + + solve(); + } + } + + static void solve(){ + int n=sc.nextInt(); + ArrayList> graph= new ArrayList>(); + for(int i=0;i()); + } + for (int i = 0; i < n - 1; i++) { + int u = sc.nextInt(); + int v = sc.nextInt(); + u--; v--; + + graph.get(u).add(new Edge(v, i)); + graph.get(v).add(new Edge(u, i)); + } + + int start = 0; + for (int i = 0; i < n; i++) { + if (graph.get(i).size() > 2) { + System.out.println(""-1""); + return; + } else if (graph.get(i).size() == 1) { + start = i; + } + } + int[] weight = new int[n - 1]; + + int prevNode = -1; + int curNode = start; + int curWeight = 2; + + while (true) { + ArrayList edges = graph.get(curNode); + Edge next = edges.get(0); + + if (next.node == prevNode) { + if (edges.size() == 1) { + break; + } else { + next = edges.get(1); + } + } + weight[next.index] = curWeight; + + prevNode = curNode; + curNode = next.node; + curWeight = 5 - curWeight; + } + for (int i = 0; i < n - 1; i++) { + System.out.print(weight[i]); + System.out.print("" ""); + } + System.out.println(); + } +} +","import java.util.*; + +public class Main +{ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int tc = sc.nextInt(); + while(tc>0) + { + tc--; + int ans = 0; + int n = sc.nextInt(); + int in[] = new int[n]; + int wx[] = new int[n-1]; + int wy[] = new int[n-1]; + ArrayList> arr = new ArrayList>(); + HashMap h = new HashMap(); + HashSet h2 = new HashSet(); + for(int i=0;i()); + } + for(int i=0;i2 || in[y]>2) + { + ans = -1; + } + else if(ans!=-1) + { + arr.get(x).add(y); + arr.get(y).add(x); + wx[i] = x; + wy[i] = y; + } + + } + if(ans == -1) + { + System.out.println(""-1""); + } + else + { + int vis[] = new int[n]; + ArrayDeque q = new ArrayDeque(); + q.add(0); + vis[0] = 1; + while(q.size()>0) + { + int x = q.removeFirst(); + for(int i=0;i 0) { + int n = in.nextInt(); + int[] a = new int[n]; + for(int j=0;j blue = new ArrayList<>(); + List red = new ArrayList<>(); + for(int j=0;jcur) { + p = false; + break; + } + else cur++; + } + + if(p) System.out.println(""yes""); + else System.out.println(""no""); + } + } +} +","import java.util.*; +import java.io.*; + +public class D { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + + int T = in.nextInt(); + while(T-- > 0) { + int n = in.nextInt(); + int[] a = new int[n]; + for(int j=0;j blue = new ArrayList<>(); + List red = new ArrayList<>(); + for(int j=0;jcur) { + p = false; + break; + } + else cur++; + } + + if(p) System.out.println(""yes""); + else System.out.println(""no""); + } + } +} +",1,Plagiarised +a3e272af,d1cd194e," +import java.io.*; +import java.util.*; + +public class Asd { + + static PrintWriter w = new PrintWriter(System.out); + static FastScanner s = new FastScanner(); + static boolean sd = false; + + public static void main(String[] args) { + + int t = s.nextInt(); + //int t=1; + while (t-- > 0) { + solve(); + } + w.close(); + + } + public static class Student { + public int i1; + public int value; + + // A parameterized student constructor + public Student(int i1,int i2) { + + this.i1 = i1; + this.value=i2; + } + + public int getkey() { + return i1; + } + public int getValue() { + return value; + } +} + static class StudentComparator implements Comparator{ + + // Overriding compare()method of Comparator + // for descending order of cgpa + @Override + public int compare(Student s1, Student s2) { + if (s1.i1 < s2.i1) + return -1; + else if (s1.i1 >s2.i1) + return 1; + return 0; + } + } + + /* Function to print all the permutations of the string + static String swap(String str, int i, int j) + { + char ch; + char[] array = str.toCharArray(); + ch = array[i]; + array[i] = array[j]; + array[j] = ch; + return String.valueOf(array); + } + + static void permute(String str,int low,int high) + { + if(low == high) + list.add(Long.parseLong(str)); + + int i; + for(i = low; i<=high; i++){ + str = swap(str,low,i); + permute(str, low+1,high); + str = swap(str,low,i); + } + } + use permute(str2,0,str2.length()-1); to perform combinations + */ + + public static void solve() { + int n=s.nextInt(); + int m=s.nextInt(); + int x=s.nextInt(); + int arr[]=new int[n];int res[]=new int[n]; + for(int i=0;i pq=new PriorityQueue(new StudentComparator()); + for(int i=0;i t, ArrayList m) { + if (t.size() == 0 && m.size() == 0) { + sd = true; + return; + } + t.remove(0); + t.remove(t.size() - 1); + call(t, new ArrayList(m.subList(0, m.size() - 1))); + call(t, new ArrayList(m.subList(1, m.size()))); + } + + static long gcd(long a, long b) { + if (b == 0) { + return a; + } + return gcd(b, a % b); + } + + static int noofdivisors(int n) { + //it counts no of divisors of every number upto number n + + int arr[] = new int[n + 1]; + for (int i = 1; i <= (n); i++) { + for (int j = i; j <= (n); j = j + i) { + arr[j]++; + } + } + return arr[0]; + } + + static char[] reverse(char arr[]) { + char[] b = new char[arr.length]; + int j = arr.length; + for (int i = 0; i < arr.length; i++) { + b[j - 1] = arr[i]; + j = j - 1; + } + return b; + } + + static long factorial(int n) { + long su = 1; + for (int i = 1; i <= n; i++) { + su *= (long) i; + } + return su; + } + + static class FastScanner { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(""""); + + public String next() { + while (!st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + long[] readArray(int n) { + long[] a = new long[n]; + for (int i = 0; i < n; i++) { + a[i] = nextLong(); + } + return a; + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + } + +} +","import java.util.*; + +import java.lang.*; +import java.io.*; + +public class Template { + + static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader() + { + br = new BufferedReader( + new InputStreamReader(System.in)); + } + + String next() + { + while (st == null ||!st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { return Integer.parseInt(next()); } + + long nextLong() { return Long.parseLong(next()); } + + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str = """"; + try { + str = br.readLine(); + } + catch (IOException e) { + e.printStackTrace(); + } + return str; + } + +} + +// static void solve(String s) +// { +//// Scanner sc = new Scanner(System.in); +//// String s = sc.next(); +// +// int x[] = new int[2]; +// x[0] = x[1] = -1; +// +// int ans = 0; +// int n = s.length(); +// for(int i=0;i 0) +// { +// String s = sc.next(); +// solve(s); +// +// } +// +// } + + static class Pair implements Comparable + { + int h; + int ind; + + Pair(int h, int ind) + { + this.h = h; + this.ind = ind; + } + @Override + public int compareTo(Pair o) { + + return this.h - o.h; + } + + + } + public static void main(String[] args) { + + FastReader fs=new FastReader(); + + int T=fs.nextInt(); + + for (int tt=0; tt heap = new PriorityQueue<>(); + for(int i=0;i x) +//// { +//// System.out.println(""NO"" + a[i].ind+"" ""+a[i].h +"" ""+min +"" ""+max); +//// flag = true; +//// break; +//// } +// if(inc) +// tower++; +// else +// tower--; +// } +// for(int i=1;i<=m;i++) +// { +// min = Math.min(min, towers[i]); +// max = Math.max(max, towers[i]); +// } +// if(Math.abs(max - min) > x) +// { +// System.out.println(""NO"" + max+"" ""+min);// + a[i].ind+"" ""+a[i].h +"" ""+min +"" ""+max); +// //flag = true; +// continue; +// } +// if(flag) +// continue; + System.out.println(""YES""); + for(int i:ans) + System.out.print(i+"" ""); + + System.out.println(); + + } + +} +} +",0,Non-plagiarised +3380fa52,8a39dbf5,"import java.util.*; +import java.io.*; +import java.math.*; + +/* Name of the class has to be ""Main"" only if the class is public. */ +public class Coder { + static int n, k; + static long a[]; + static int pos[]; + static int temp[]; + static StringBuilder str = new StringBuilder(""""); + static int cnt[][] = new int[(int)1e5+5][2]; + static void solve() { + long []l = new long[n]; + long []r = new long[n]; + long p = Integer.MAX_VALUE; + for(int i=0;i=0;i--){ + p = Math.min(p+1, a[i]); + r[i] = p; + } + for(int i=0;i 0) { + bf.readLine(); + String s[] = bf.readLine().trim().split(""\\s+""); + n = Integer.parseInt(s[0]); + k = Integer.parseInt(s[1]); + s = bf.readLine().trim().split(""\\s+""); + pos = new int[k]; + temp = new int[k]; + a = new long[n]; + for(int i=0;i=0;j--) + { + if(arr[j]==0) + { + right[j]=right[j+1]+1; + } + else + { + right[j]=Math.min(right[j+1]+1,arr[j]); + } + } + for(int j=0;j 0) { + solve(ioAdapter); + } + } + } + + static void ruffleSort(int[] arr) { + int n = arr.length; + Random rnd = new Random(); + for (int i = 0; i < n; ++i) { + int tmp = arr[i]; + int randomPos = i + rnd.nextInt(n - i); + arr[i] = arr[randomPos]; + arr[randomPos] = tmp; + } + Arrays.sort(arr); + } + + static class FastIOAdapter implements AutoCloseable { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out)))); + StringTokenizer st = new StringTokenizer(""""); + + String next() { + while (!st.hasMoreTokens()) + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + return st.nextToken(); + } + + String nextLine() { + try { + return br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + int nextInt() { + return Integer.parseInt(next()); + } + + int[] readArray(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) a[i] = nextInt(); + return a; + } + + long[] readArrayLong(int n) { + long[] a = new long[n]; + for (int i = 0; i < n; i++) a[i] = nextLong(); + return a; + } + + long nextLong() { + return Long.parseLong(next()); + } + + @Override + public void close() throws Exception { + out.flush(); + out.close(); + br.close(); + } + } +} +","import java.util.Scanner; + +public class VupsenPupsenand0 { + + public static void main(String args[]) { + Scanner scan = new Scanner(System.in); + int t = scan.nextInt(); + while (t-- > 0) { + int n = scan.nextInt(); + int[] a = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = scan.nextInt(); + } + if (n % 2 == 0) { + for (int i = 0; i < n; i += 2) { + System.out.print(a[i + 1] + "" "" + -a[i] + "" ""); + } + System.out.println(); + } else { + for (int i = 0; i < n - 3; i += 2) { + System.out.print(a[i + 1] + "" "" + -a[i] + "" ""); + } + int x = a[n - 3]; + int y = a[n - 2]; + int z = a[n - 1]; + if (x + y != 0) { + System.out.println(-z + "" "" + (-z) + "" "" + (x + y)); + } else if (y + z != 0) { + System.out.println((y + z) + "" "" + (-x) + "" "" + (-x)); + } else { + System.out.println(-y + "" "" + (x + z) + "" "" + (-y)); + } + } + } + } + +} +",1,Plagiarised +829d2024,8a20e743,"import java.util.*; +import java.io.*; + + +public class Main{ + public static class Element implements Comparable{ + public int key; + public int value; + Element(int k, int v) + { + key=k; + value=v; + } + + @Override + public int compareTo(Element o) { + if(this.valueo.value) + return 1; + return 0; + } + } + 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 x=sc.nextInt(); + int[] arr=new int[n]; + for(int i=0;i pq=new PriorityQueue<>(); + for(int i=1;i<=m;++i) + { + pq.add(new Element(i,0)); + } + System.out.println(""YES""); + for(int j=0;j{ + public int compare(Pair a,Pair b) { + return a.h-b.h; + } + } + public static void main(String[] args) + { + FastReader sc=new FastReader(); + StringBuffer ans=new StringBuffer(); + int test=sc.nextInt(); + while(test-->0) + { + + int n=sc.nextInt(),m=sc.nextInt(),x=sc.nextInt(); + + int arr[]=new int[n]; + int res[]=new int[n]; + + int sum =0; + for(int i=0;i pq=new PriorityQueue<>(new customSort()); + + for(int i=1;i<=m;i++) { + pq.add(new Pair(i,0)); + } + + ans.append(""YES\n""); + for(int a:arr) { + Pair xx=pq.poll(); + ans.append(xx.in+"" ""); + xx.h+=a; + pq.add(xx); + } + ans.append(""\n""); + + + } + System.out.print(ans); + } + + + static long _gcd(long a,long b) { + if(b == 0) return a; + return _gcd(b,a%b); + } + + static final Random random=new Random(); + + static void ruffleSort(int[] a) { + int n=a.length;//shuffle, then sort + for (int i=0; i0) + solve(); + } + public static void solve() { + int n = fs.nextInt(); + int[] c = fs.readArray(n); + long sumEven = c[0]; + long minEven = c[0]; + long sumOdd = c[1]; + long minOdd = c[1]; + long ans = n*minOdd+n*minEven; + for (int i=2; i0;i--) { + moves--; + if(moves<0)break; + long val=a[0]-moves; + suf-=a[i]; + long tot=suf+val*(n-i+1); + min=Math.min(min, tot); + } + + return min<=k; + } + + static int[] ruffleSort(int a[]) { + ArrayList al=new ArrayList<>(); + for(int i:a)al.add(i); + Collections.sort(al); + for(int i=0;i0){ + String []nk=br.readLine().split("" ""); + int n=Integer.parseInt(nk[0]); + long k=Long.parseLong(nk[1]); + String []str=br.readLine().split("" ""); + List list=new ArrayList<>(); + for(int i=0;i 0; i--) { + //assignment at ith element + drop += list.get(i) - list.get(0); + if (drop >= targetDrop) { + if (minSteps > n - i) + minSteps = n - i; + } else { + long diff = targetDrop - drop; + long div = diff / (n - i + 1); + long rem = diff % (n - i + 1); + long steps = rem > 0 ? (div + 1) + n - i : div + n - i; + if (minSteps > steps) + minSteps = steps; + + } + } + System.out.println(minSteps); + } + t--; + } + } +} +",0,Non-plagiarised +6f02c6d9,d3a96420," +import java.io.*; +import java.util.*; + +public class Main { + + + static long mod = 1000000007; + + static long inv(long a, long b) { + return 1 < a ? b - inv(b % a, a) * b / a : 1; + } + + static long mi(long a) { + return inv(a, mod); + } + + static InputReader sc = new InputReader(System.in); + static PrintWriter out = new PrintWriter(System.out); + + public static void main(String[] args) throws IOException { + + int t = sc.nextInt(); + while (t-- > 0) { + + int n = sc.nextInt(); + int[] A = new int[n]; + + for (int i = 0; i < A.length; i++) { + A[i] = sc.nextInt(); + } + String word = sc.next(); + ArrayList blue = new ArrayList<>(); + ArrayList red = new ArrayList<>(); + + for (int i = 0; i < word.length(); i++) { + if (word.charAt(i) == 'R') { + red.add(A[i]); + } else { + blue.add(A[i]); + } + } + Collections.sort(blue); + Collections.sort(red); + + boolean possible = true; + + int a = 1; + for (int i = 0; i < blue.size(); i++, a++) { + if (blue.get(i) < a) { + possible = false; + break; + } + } + + for (int i = 0; i < red.size(); i++, a++) { + if (red.get(i) > a) { + possible = false; + break; + } + } + + if (possible) out.println(""YES""); + else out.println(""NO""); + + + } + out.flush(); + out.close(); + } + + static class InputReader { + BufferedReader reader; + StringTokenizer tokenizer; + + public InputReader(InputStream stream) { + reader = new BufferedReader(new InputStreamReader(stream), 32768); + tokenizer = null; + } + + + String next() { + while (tokenizer == null || !tokenizer.hasMoreTokens()) { + try { + tokenizer = new StringTokenizer(reader.readLine()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + return tokenizer.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = reader.readLine(); + } catch (IOException e) { + throw new RuntimeException(e); + } + return str; + } + } + + public static void shuffle(int[] a) { + Random get = new Random(); + for (int i = 0; i < a.length; i++) { + int r = get.nextInt(a.length); + int temp = a[i]; + a[i] = a[r]; + a[r] = temp; + } + } + + public static void shuffle(long[] a) { + Random get = new Random(); + for (int i = 0; i < a.length; i++) { + int r = get.nextInt(a.length); + long temp = a[i]; + a[i] = a[r]; + a[r] = temp; + } + } + +} +","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[] a=new int[n]; + for(int i=0;i R=new Vector<>(); + Vector B=new Vector<>(); + for(int i=0;in+1){System.out.println(""NO"");yes=false;break;} + } + } + if(yes)System.out.println(""YES""); + } + sc.close(); + } +} +",0,Non-plagiarised +20166f71,a98d56f9,"import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.File; +import java.io.FileInputStream; +import java.util.*; + +public class Main { + + // static final File ip = new File(""input.txt""); + // static final File op = new File(""output.txt""); + // static { + // try { + // System.setOut(new PrintStream(op)); + // System.setIn(new FileInputStream(ip)); + // } catch (Exception e) { + // } + // } + private static final PrintWriter out = new PrintWriter(System.out); + + public static void main(String[] args) { + FastReader sc = new FastReader(); + int test = 1; + test = sc.nextInt(); + while (test-- > 0) { + int n = sc.nextInt(); + long k = sc.nextLong(); + long[] a = new long[n]; + for (int i = 0; i < n; i++) { + a[i] = sc.nextLong(); + } + sortL(a); + long[] pref = new long[n + 1]; + for (int i = 0; i < n; i++) { + pref[i + 1] = pref[i] + a[i]; + } + long ans = (long) 9e18; + for (int t = 0; t <= n - 1; t++) { + long sum = pref[n - t] + a[0] * t; + long cur = t; + if (sum > k) { + long diff = sum - k; + cur += (diff + t) / (t + 1); + } + ans = Math.min(ans, cur); + } + out.println(ans); + } + out.close(); + } + + static long power(long x, long y, long p) { + long res = 1; + x = x % p; + if (x == 0) + return 0; + while (y > 0) { + if ((y & 1) != 0) + res = (res * x) % p; + y = y >> 1; + x = (x * x) % p; + } + return res; + } + + public static int countSetBits(long number) { + int count = 0; + while (number > 0) { + ++count; + number &= number - 1; + } + return count; + } + + static int lower_bound(long target, long[] a, int pos) { + if (pos >= a.length) + return -1; + int low = pos, high = a.length - 1; + while (low < high) { + int mid = low + (high - low) / 2; + if (a[mid] < target) + low = mid + 1; + else + high = mid; + } + return a[low] >= target ? low : -1; + } + + private static void swap(long[] a, int i, int j) { + long tmp = a[i]; + a[i] = a[j]; + a[j] = tmp; + } + + static class pair { + long a; + int b; + + pair(long x, int y) { + this.a = x; + this.b = y; + } + } + + static class first implements Comparator { + + public int compare(pair p1, pair p2) { + if (p1.a > p2.a) + return 1; + else if (p1.a < p2.a) + return -1; + return 0; + } + } + + static class second implements Comparator { + + public int compare(pair p1, pair p2) { + if (p1.b > p2.b) + return 1; + else if (p1.b < p2.b) + return -1; + return 0; + } + } + + private static long getSum(int[] array) { + long sum = 0; + for (int value : array) { + sum += value; + } + return sum; + } + + private static boolean isPrime(Long x) { + if (x < 2) + return false; + for (long d = 2; d * d <= x; ++d) { + if (x % d == 0) + return false; + } + return true; + } + + static long[] reverse(long a[]) { + int n = a.length; + int i; + long t; + for (i = 0; i < n / 2; i++) { + t = a[i]; + a[i] = a[n - i - 1]; + a[n - i - 1] = t; + } + return a; + } + + private static boolean isPrimeInt(int x) { + if (x < 2) + return false; + for (int d = 2; d * d <= x; ++d) { + if (x % d == 0) + return false; + } + return true; + } + + public static String reverse(String input) { + StringBuilder str = new StringBuilder(""""); + + for (int i = input.length() - 1; i >= 0; i--) { + str.append(input.charAt(i)); + } + + return str.toString(); + } + + private static int[] getPrimes(int n) { + boolean[] used = new boolean[n + 1]; + used[0] = used[1] = true; + // int size = 0; + for (int i = 2; i <= n; ++i) { + if (!used[i]) { + // ++size; + for (int j = 2 * i; j <= n; j += i) { + used[j] = true; + } + } + } + int[] primes = new int[n + 1]; + for (int i = 0; i <= n; ++i) { + if (!used[i]) { + primes[i] = 1; + } + } + return primes; + } + + private static long lcm(long a, long b) { + return a / gcd(a, b) * b; + } + + private static long gcd(long a, long b) { + return (a == 0 ? b : gcd(b % a, a)); + } + + static void sortI(int[] arr) { + int n = arr.length; + Random rnd = new Random(); + for (int i = 0; i < n; ++i) { + int tmp = arr[i]; + int randomPos = i + rnd.nextInt(n - i); + arr[i] = arr[randomPos]; + arr[randomPos] = tmp; + } + Arrays.sort(arr); + } + + static void shuffleList(ArrayList arr) { + int n = arr.size(); + Random rnd = new Random(); + for (int i = 0; i < n; ++i) { + long tmp = arr.get(i); + int randomPos = i + rnd.nextInt(n - i); + arr.set(i, arr.get(randomPos)); + arr.set(randomPos, tmp); + } + } + + static void factorize(long n) { + int count = 0; + while (!(n % 2 > 0)) { + n >>= 1; + + count++; + } + if (count > 0) { + // System.out.println(""2"" + "" "" + count); + } + long i = 0; + for (i = 3; i <= (long) Math.sqrt(n); i += 2) { + count = 0; + while (n % i == 0) { + count++; + n = n / i; + } + if (count > 0) { + // System.out.println(i + "" "" + count); + } + } + + if (n > 2) { + // System.out.println(i + "" "" + count); + } + } + + static void sortL(long[] arr) { + int n = arr.length; + Random rnd = new Random(); + for (int i = 0; i < n; ++i) { + long tmp = arr[i]; + int randomPos = i + rnd.nextInt(n - i); + arr[i] = arr[randomPos]; + arr[randomPos] = tmp; + } + Arrays.sort(arr); + } + + ////////////////////////////////// DSU START /////////////////////////// + + static class DSU { + int[] parent, rank, total_Elements; + + DSU(int n) { + parent = new int[n + 1]; + rank = new int[n + 1]; + total_Elements = new int[n + 1]; + for (int i = 0; i <= n; i++) { + parent[i] = i; + rank[i] = 1; + total_Elements[i] = 1; + } + } + + int find(int u) { + if (parent[u] == u) + return u; + return parent[u] = find(parent[u]); + } + + void unionByRank(int u, int v) { + int pu = find(u); + int pv = find(v); + + if (pu != pv) { + if (rank[pu] > rank[pv]) { + parent[pv] = pu; + total_Elements[pu] += total_Elements[pv]; + } else if (rank[pu] < rank[pv]) { + parent[pu] = pv; + total_Elements[pv] += total_Elements[pu]; + } else { + parent[pu] = pv; + total_Elements[pv] += total_Elements[pu]; + rank[pv]++; + } + } + } + + boolean unionBySize(int u, int v) { + u = find(u); + v = find(v); + if (u != v) { + parent[u] = v; + total_Elements[v] += total_Elements[u]; + total_Elements[u] = 0; + return true; + } + return false; + } + } + + ////////////////////////////////// DSU END ///////////////////////////// + + static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + public boolean hasNext() { + return false; + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } +}","import java.util.*; +import java.io.*; +import java.math.*; + +public class Test{ + + //Global declarations + final static Random random = new Random(); + final static FastReader in = new FastReader(); + final static PrintWriter out = new PrintWriter(System.out); + + + + public static long helperFunction(long x, long y) { + //Long range is -10^18 to 10^18 and is 64 bit (8 bytes) + //Integer range is ~~ -2 * 10^9(-2^31) to 2 * 10^9(2^31 - 1) and is 32 bit (4 bytes) + + if(x>=0) + return x/y; + + + //for ceiling negative numbers (integers) + return (x-y+1)/y; + } + + public static void ruffleLong(long a[]) { + + //shuffle, then sort + + int i,j,n=a.length; + long temp; + + for (i=0; i multiply + // >> --> divide + + //variable_name <<= 1 --> (variable)multiply by 2 + //variable_name >>= 1 --> (variable)divide by 2 + + //2<<3 --> (multiply)2 * 2^3 --> 16; + //6>>1 --> (divide)6 / 2^1 --> 3; + + /** + + int n = in.nextInt(),a,b; + in.nextLine(); + while(n-->0){ + a = in.nextInt(); + b = in.nextInt(); + out.println(a+b); + if(n>0) + in.nextLine(); + } + out.close(); + + + int tt = in.nextInt(); + while(tt-->0){ + out.println(helperFunction()); + } + + //out.println(helperFunction()); + + **/ + + + //it returns the remaining length (+ve) so bigger so swap + //System.out.println(""h"".compareTo(""ha"")); + + /* + Problem 13 - Large sum + + String s = ""37107287533902102798797998220837590246510135740250463769376774900097126481248969700780504170182605387432498619952474105947423330951305812372661730962991942213363574161572522430563301811072406154908250230675882075393461711719803104210475137780632466768926167069662363382013637841838368417873436172675728112879812849979408065481931592621691275889832738442742289174325203219235894228767964876702721893184745144573600130643909116721685684458871160315327670386486105843025439939619828917593665686757934951621764571418565606295021572231965867550793241933316490635246274190492910143244581382266334794475817892575867718337217661963751590579239728245598838407582035653253593990084026335689488301894586282278288018119938482628201427819413994056758715117009439035398664372827112653829987240784473053190104293586865155060062958648615320752733719591914205172558297169388870771546649911559348760353292171497005693854370070576826684624621495650076471787294438377604532826541087568284431911906346940378552177792951453612327252500029607107508256381565671088525835072145876576172410976447339110607218265236877223636045174237069058518606604482076212098132878607339694128114266041808683061932846081119106155694051268969251934325451728388641918047049293215058642563049483624672216484350762017279180399446930047329563406911573244438690812579451408905770622942919710792820955037687525678773091862540744969844508330393682126183363848253301546861961243487676812975343759465158038628759287849020152168555482871720121925776695478182833757993103614740356856449095527097864797581167263201004368978425535399209318374414978068609844840309812907779179908821879532736447567559084803087086987551392711854517078544161852424320693150332599594068957565367821070749269665376763262354472106979395067965269474259770973916669376304263398708541052684708299085211399427365734116182760315001271653786073615010808570091499395125570281987460043753582903531743471732693212357815498262974255273730794953759765105305946966067683156574377167401875275889028025717332296191766687138199318110487701902712526768027607800301367868099252546340106163286652636270218540497705585629946580636237993140746255962240744869082311749777923654662572469233228109171419143028819710328859780666976089293863828502533340334413065578016127815921815005561868836468420090470230530811728164304876237919698424872550366387845831148769693215490281042402013833512446218144177347063783299490636259666498587618221225225512486764533677201869716985443124195724099139590089523100588229554825530026352078153229679624948164195386821877476085327132285723110424803456124867697064507995236377742425354112916842768655389262050249103265729672370191327572567528565324825826546309220705859652229798860272258331913126375147341994889534765745501184957014548792889848568277260777137214037988797153829820378303147352772158034814451349137322665138134829543829199918180278916522431027392251122869539409579530664052326325380441000596549391598795936352974615218550237130764225512118369380358038858490341698116222072977186158236678424689157993532961922624679571944012690438771072750481023908955235974572318970677254791506150550495392297953090112996751986188088225875314529584099251203829009407770775672113067397083047244838165338735023408456470580773088295917476714036319800818712901187549131054712658197623331044818386269515456334926366572897563400500428462801835170705278318394258821455212272512503275512160354698120058176216521282765275169129689778932238195734329339946437501907836945765883352399886755061649651847751807381688378610915273579297013376217784275219262340194239963916804498399317331273132924185707147349566916674687634660915035914677504995186714302352196288948901024233251169136196266227326746080059154747183079839286853520694694454072476841822524674417161514036427982273348055556214818971426179103425986472045168939894221798260880768528778364618279934631376775430780936333301898264209010848802521674670883215120185883543223812876952786713296124747824645386369930090493103636197638780396218407357239979422340623539380833965132740801111666627891981488087797941876876144230030984490851411606618262936828367647447792391803351109890697907148578694408955299065364044742557608365997664579509666024396409905389607120198219976047599490197230297649139826800329731560371200413779037855660850892521673093931987275027546890690370753941304265231501194809377245048795150954100921645863754710598436791786391670211874924319957006419179697775990283006991536871371193661495281130587638027841075444973307840789923115535562561142322423255033685442488917353448899115014406480203690680639606723221932041495354150312888033953605329934036800697771065056663195481234880673210146739058568557934581403627822703280826165707739483275922328459417065250945123252306082291880205877731971983945018088807242966198081119777158542502016545090413245809786882778948721859617721078384350691861554356628840622574736922845095162084960398013400172393067166682355524525280460972253503534226472524250874054075591789781264330331690""; + StringBuilder st = new StringBuilder(""""); + int i,ct=0,p=0; + BigInteger res = BigInteger.valueOf(0); + String a[] = new String[100]; + for(i=0;i<5000;++i){ + + st.append(s.charAt(i)); + ++ct; + + if(ct == 50){ + a[p] = st.toString(); + ct=0; + ++p; + st.setLength(0); + } + + } + //out.println(Arrays.toString(a)); + + for(String val: a){ + res = res.add(new BigInteger(val)); + } + + out.println(res.toString());*/ + + //out.println(helperFunction()); + + + //String airports[] = {""AAA"", ""AAAA""}; + + //int i,n=airports.length; + + + + + /* + Maximum number of unique values in the array after performing given operations + + int a[] = {1,2,4,4}; + int i,n = a.length,res=0; + + int mp[] = new int[n+2]; + + Arrays.sort(a); + + for(i=0;i 0) + ++res; + } + + out.println(res);*/ + + + int tt = in.nextInt(); + + while(tt-->0){ + + int n = in.nextInt(); + long k = in.nextLong(); + + + int i,y; + long a[] = new long[n]; + long pref[] = new long[n]; + + long x,res=Long.MAX_VALUE; + + for(i=0;i decrease/minimize the smallest value (a[0]) by x + //y --> no.of times settting largest values to the smallest value in (descending order) + + for(y=0;y0){ + int n=sc.nextInt(); + int k=sc.nextInt(); + + int idx[]=new int[k]; + + for(int i=0;i=0;i--){ + right[i]=Math.min(right[i+1]+1,arr[i]); + } + + for(int i=0;i 0;T--) + start(); + // System.out.print(sb); + + } + + + void start() + { + int n = s.nextInt(); + int m = s.nextInt(); + long arr[] = longArr(m); + long arr2[] = longArr(m); + long ans [] = new long[n]; + Arrays.fill(ans,Integer.MAX_VALUE); + for(int i = 0; i=0; i--) + { + right[i] = Math.min(right[i+1]+1, ans[i]); + } + StringBuffer x = new StringBuffer(); + for(int i =0; i0) + { + m/=2; + y++; + } + return y; + + } + long gcd(long a, long b) + { + if (b == 0) + return a; + return gcd(b, a % b); + } + long power(long x, long y, long p) + { + long res = 1; // Initialize result + + // Update x if it is more + // than or equal to p + x = x % p; + + while (y > 0) + { + // If y is odd, multiply + // x with the result + if ((y & 1) > 0) + res = (res * x) % p; + + // y must be even now + y = y >> 1; // y = y/2 + x = (x * x) % p; + } + return res; + } + + int lower_bound(int [] arr , int key) + { + + int i = 0; + int j = arr.length-1; + //if(arr[i] > key)return -1; + if(arr[j] < key)return -1; + + while(i= '0' && c <= '9'); + + if (neg) + return -ret; + return ret; + } + + public long nextLong() throws IOException + { + long ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + if (neg) + return -ret; + return ret; + } + + public double nextDouble() throws IOException + { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') { + while ((c = read()) >= '0' && c <= '9') { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) + return -ret; + return ret; + } + + private void fillBuffer() throws IOException + { + bytesRead = din.read(buffer, bufferPointer = 0, + BUFFER_SIZE); + if (bytesRead == -1) + buffer[0] = -1; + } + + private byte read() throws IOException + { + if (bufferPointer == bytesRead) + fillBuffer(); + return buffer[bufferPointer++]; + } + + public void close() throws IOException + { + if (din == null) + return; + din.close(); + } + } + + + static int parent(int a , int p[]) + { + if(a == p[a]) + return a; + + return p[a] = parent(p[a],p); + } + + static void union(int a , int b , int p[] , int size[]) + { + a = parent(a,p); + b = parent(b,p); + + if(a == b) + return; + + if(size[a] < size[b]) + { + int temp = a; + a = b; + b = temp; + } + + p[b] = a; + size[a] += size[b]; + } + + + + + static long getSum(int index , long BITree[]) + { + long sum = 0; // Iniialize result + + // index in BITree[] is 1 more than + // the index in arr[] + // index = index + 1; + + // Traverse ancestors of BITree[index] + while(index>0) + { + // Add current element of BITree + // to sum + sum += BITree[index]; + + // Move index to parent node in + // getSum View + index -= index & (-index); + } + return sum; + } + + // Updates a node in Binary Index Tree (BITree) + // at given index in BITree. The given value + // 'val' is added to BITree[i] and all of + // its ancestors in tree. + public static void updateBIT(int n, int index, + long val , long BITree[]) + { + // index in BITree[] is 1 more than + // the index in arr[] + // index = index + 1; + + // Traverse all ancestors and add 'val' + while(index <= n) + { + // Add 'val' to current node of BIT Tree + BITree[index] += val; + + // Update index to that of parent + // in update View + index += index & (-index); + } + } + + + static int gcd(int a, int b) +{ + if (a == 0) + return b; + return gcd(b % a, a); +} + +static int dp[][]; + + +static int f(int pos , int take , int arr[]) +{ + if(pos == -1) + { + if(take == 0) + return 0; + + return -10000000; + + } + + + if(dp[pos][take] != -1) + return dp[pos][take]; + + + if(pos+1-take == arr[pos]) + dp[pos][take] = Math.max(dp[pos][take],1 + f(pos-1,take,arr)); + + + dp[pos][take] = Math.max(dp[pos][take],f(pos-1,take,arr)); + + + if(take > 0) + dp[pos][take] = Math.max(dp[pos][take],f(pos-1,take-1,arr)); + + return dp[pos][take]; +} + 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(); + sc.nextLine(); + String a = sc.nextLine(); + String b = sc.nextLine(); + int same = 0 , zo = 0 , oz = 0 , oo = 0 , zz = 0; + for(int i = 0 ; i < n ; i++) + { + if(a.charAt(i) == '0' && b.charAt(i) == '1') + oz++; + + else if(a.charAt(i) == '1' && b.charAt(i) == '0') + zo++; + + else if(a.charAt(i) == '1' && b.charAt(i) == '1') + oo++; + + else + zz++; + } + + if(oz == zo || (zz == oo-1)) + { + int mx = Integer.MAX_VALUE; + if(oz == zo) + mx = Math.min(mx,2*oz); + + if(oo-1 == zz) + mx = Math.min(mx,zz+oo); + + System.out.println(mx); + } + + else + { + System.out.println(-1); + } + } + } +}"," +import java.util.Scanner; + +public class Menorah { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int t = scan.nextInt(); + k: + while (t-- > 0) { + int n = scan.nextInt(); + String a = scan.next(); + String b = scan.next(); + int zo = 0; + int oz = 0; + int ind = -1; + for (int i = 0; i < n; i++) { + if (a.charAt(i) == '0' && b.charAt(i) == '1') { + ++zo; + } else if (a.charAt(i) == '1' && b.charAt(i) == '0') { + ++oz; + } else if (a.charAt(i) == '1' && b.charAt(i) == '1') { + ind = i; + } + } + int res = 1000000000; + int res2 = 1000000000; + if (zo == oz) { + res = zo + oz; + } + if (ind != -1) { + char[] s = a.toCharArray(); + for (int i = 0; i < n; i++) { + if (i == ind) { + continue; + } + if (s[i] == '0') { + s[i] = '1'; + } else { + s[i] = '0'; + } + } + + zo = 0; + oz = 0; + + for (int i = 0; i < n; i++) { + + if (s[i] == '0' && b.charAt(i) == '1') { + ++zo; + } else if (s[i] == '1' && b.charAt(i) == '0') { + ++oz; + } + } + + if (zo == oz) { + res2 = zo + oz + 1; + } + + } + if (res == 1000000000 && res2 == 1000000000) { + System.out.println(""-1""); + } else { + System.out.println(Math.min(res, res2)); + } + + } + } +} +",0,Non-plagiarised +4ea10951,66e74577,"import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class Main { + static String f1(String s, int index) { + StringBuffer sb = new StringBuffer(); + for(int i =0;i 0) { + int n = Integer.parseInt(br.readLine()); + String s1 = br.readLine(); + String s2 = br.readLine(); + long res = solve(s1, s2); + for(int i=0;i0){ + // int x=sc.nextInt(); + // int y=sc.nextInt(); + //String str[]=new String[size]; + solve(); + System.out.println(); + } + } + + public static void solve(){ + HashMap map=new HashMap(); + int size=sc.nextInt(); + int arr[][]=new int[size-1][2]; + for(int i=0;i2||map.get(x[1])>2){ + System.out.println(-1); + return; + } + } + List> adj=new ArrayList<>(); + for(int i=0;i<=size;i++) + adj.add(new ArrayList()); + for(int x[]:arr){ + adj.get(x[0]).add(x[1]); + adj.get(x[1]).add(x[0]); + } + //System.out.println(adj); + int vist[]=new int[size+1]; + HashMap ans=new HashMap(); + for(int i=1;i<=size;i++){ + if(vist[i]==0){ + dfs(i,vist,adj,ans,2); + } + } + //System.out.println(ans); + for(int x[]:arr){ + //System.out.print(map.get(x[0])); + int a=Math.min(x[0],x[1]); + int b=Math.max(x[0],x[1]); + String s=a+"" ""+b; + System.out.print(ans.get(s)+"" ""); + } + // map=new HashMap(); + // for(int x[]:arr){ + // if(map.containsKey(x[0])){ + // int val=13-map.get(x[0]); + // map.put(x[1],val); + // System.out.print(val+"" ""); + // }else if(map.containsKey(x[1])){ + // int val=13-map.get(x[1]); + // map.put(x[0],val); + // System.out.print(val+"" ""); + // }else{ + // System.out.print(2+"" ""); + // map.put(x[0],2); + // map.put(x[1],2); + // } + // } + + } + public static void dfs(int node,int vist[],List> adj,HashMap ans,int val){ + vist[node]=1; + for(int i:adj.get(node)){ + if(vist[i]==1) + continue; + int x=Math.min(i, node); + int y=Math.max(i, node); + ans.put(x+"" ""+y,val); + dfs(i,vist,adj,ans,5-val); + val=5-val; + } + } +}","import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; +import java.util.concurrent.ThreadLocalRandom; + +public class A { + private static void sport(List[] g, Map map) { + int n = g.length; + for (int i = 0; i < n; i++) { + if (g[i].size() > 2) { + System.out.println(-1); + return; + } + } + int[] ans = new int[n - 1]; + //dfs(new C(-1, 0), g, ans, 3, new HashSet<>()); + Queue queue = new LinkedList<>(); + Set seen = new HashSet<>(); + int val = 3; + for (Integer integer : g[0]) { + Integer idx = map.get(new W(0, integer)); + ans[idx] = val; + queue.add(new int[]{val, integer}); + seen.add(integer); + val = val == 2 ? 3 : 2; + } + seen.add(0); + while (!queue.isEmpty()) { + int[] poll = queue.poll(); + for (Integer u : g[poll[1]]) { + if (!seen.contains(u)) { + seen.add(u); + int curr = poll[0] == 2 ? 3 : 2; + Integer integer = map.get(new W(poll[1], u)); + ans[integer] = curr; + queue.add(new int[]{curr, u}); + } + } + } + for (int an : ans) { + System.out.print(an + "" ""); + } + System.out.println(); + } + + static class W { + int u; + int v; + + public W(int u, int v) { + this.u = u; + this.v = v; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + W w = (W) o; + return u == w.u && v == w.v; + } + + @Override + public int hashCode() { + return Objects.hash(u, v); + } + } + + static void dfs(C v, List[] g, int[] ans, int prev, Set seen) { + if (v.i != -1) { + ans[v.i] = prev == 2 ? 3 : 2; + } + seen.add(v.v); + int next = prev == 2 ? 3 : 2; + for (C c : g[v.v]) { + if (!seen.contains(c.v)) { + dfs(c, g, ans, next, seen); + } + next = next == 2 ? 3 : 2; + + } + } + + static class C { + int i; + int v; + + public C(int i, int v) { + this.i = i; + this.v = v; + } + } + + public static void main(String[] args) throws IOException { + FastScanner sc = new FastScanner(); + int t = sc.nextInt(); + for (int i = 0; i < t; i++) { + int n = sc.nextInt(); + List[] g = new ArrayList[n]; + for (int j = 0; j < n; j++) { + g[j] = new ArrayList<>(); + } + Map map = new HashMap<>(); + for (int j = 0; j < n - 1; j++) { + int u = sc.nextInt() - 1; + int v = sc.nextInt() - 1; + g[u].add(v); + g[v].add(u); + map.put(new W(u, v), j); + map.put(new W(v, u), j); + } + sport(g, map); + } + } + + static void shuffleArray(int[] ar) { + // If running on Java 6 or older, use `new Random()` on RHS here + Random rnd = ThreadLocalRandom.current(); + for (int i = ar.length - 1; i > 0; i--) { + int index = rnd.nextInt(i + 1); + // Simple swap + int a = ar[index]; + ar[index] = ar[i]; + ar[i] = a; + } + } + + static class FastScanner { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(""""); + + String next() { + while (!st.hasMoreTokens()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + void nextLine() throws IOException { + br.readLine(); + } + + long[] readArrayLong(int n) { + long[] a = new long[n]; + for (int i = 0; i < n; i++) { + a[i] = nextLong(); + } + return a; + } + + int[] readArrayInt(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = nextInt(); + } + return a; + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + } +}",0,Non-plagiarised +26e699de,402aff07,"//package Task1; + +import java.util.Scanner; + +public class Menorah { + static int MOD9= 1000000000; + + public static void main(String[] args){ + + Scanner sc= new Scanner(System.in); + int numberTest=sc.nextInt(); + while(numberTest-->0){ + int n=sc.nextInt(); + char[] s=new char[n+5]; + char[] t=new char[n+5]; + String ss=sc.next(); + String tt=sc.next(); + s=ss.toCharArray(); + t=tt.toCharArray(); + int cntax = 0, cntbx = 0, same = 0; + int ans=MOD9; + for(int i=0; i 0){ + int n = s.nextInt(); + String a = s.next(); + String b = s.next(); + int a1 = 0, b1 = 0; + for (char c: a.toCharArray()){ + if (c == '1') a1++; + } + for (char c: b.toCharArray()){ + if (c == '1') b1++; + } + int ans = Integer.MAX_VALUE; + int res = 0; + for (int i = 0; i < n; i++) { + if (a.charAt(i) != b.charAt(i)) res++; + } + if (a1 == b1) ans = Math.min(ans, res); + if (b1 == n-a1+1) ans = Math.min(ans, n-res); + if (ans == Integer.MAX_VALUE){ + System.out.println(""-1""); + } else { + System.out.println(ans); + } + } + } +}",0,Non-plagiarised +71c7ac71,8f778337,"import java.io.*; +import java.util.*; +import static java.lang.Math.*; + +public class E{ + public static void main(String[] args) throws IOException { + // br = new BufferedReader(new FileReader("".in"")); + // out = new PrintWriter(new FileWriter("".out"")); + //new Thread(null, new (), ""peepee"", 1<<28).start(); + + int q =readInt(); + while(q-->0) { + br.readLine(); + int n =readInt(); + int k =readInt(); + int[] ans = new int[n]; + Arrays.fill(ans, (int)2e9+1); + int[] a = new int[k]; + int[] t = new int[k]; + int mini = 0, maxi = 0; + for (int i = 0 ;i a[maxi]) maxi = i; + } + for (int j = 0; j = 0; i--) { + r[i] = temp = min(temp+1,ans[i]); + } + + for (int i = 0; i < n; i++) out.print(min(l[i],r[i]) + "" ""); + out.println(); + } + + out.close(); + } + + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); + static StringTokenizer st = new StringTokenizer(""""); + static String read() throws IOException{return st.hasMoreTokens() ? st.nextToken():(st = new StringTokenizer(br.readLine())).nextToken();} + static int readInt() throws IOException{return Integer.parseInt(read());} + static long readLong() throws IOException{return Long.parseLong(read());} + static double readDouble() throws IOException{return Double.parseDouble(read());} + +}","import java.util.*; +import java.io.*; +public class AirConditioner{ + public static void main(String[] args) throws Exception{ + BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); + int t=Integer.parseInt(br.readLine()); + while(t-->0){ + br.readLine(); + StringTokenizer st=null; + st=new StringTokenizer(br.readLine()); + int n=Integer.parseInt(st.nextToken()); + int k=Integer.parseInt(st.nextToken()); + int[] idx=new int[k]; + int[] temp=new int[k]; + st=new StringTokenizer(br.readLine()); + for(int i=0;i=0;i--){ + right[i]=Math.min(right[i+1]+1,arr[i]); + } + for(int i=0;i 0) { + int n = sc.nextInt(); + int a = sc.nextInt()-1; + int b = sc.nextInt()-1; + int da = sc.nextInt(); + int db = sc.nextInt(); + + List> list = new ArrayList<>(); + for(int i = 0; i < n; i++) list.add(new ArrayList<>()); + + for(int i = 0; i < n-1; i++){ + int x = sc.nextInt()-1; + int y = sc.nextInt()-1; + list.get(x).add(y); + list.get(y).add(x); + } + + for(int i = 0; i <= n; i++) depth[i] = 0; + diam = 0; + dfs(a,-1,list); + + if(2 * da >= min(diam, db) || depth[b] <= da){ + out.println(""Alice""); + } + else { + out.println(""Bob""); + } + + } + } + + static int[] depth = new int[200001]; + static int diam = 0; + static int dfs(int x, int p, List> list) { + int len = 0; + List ne = list.get(x); + for(int y : ne) { + if(y != p) { + depth[y] = depth[x] + 1; + int cur = 1 + dfs(y, x,list); + diam = max(diam, cur + len); + len = max(len, cur); + } + } + return len; + } + + + public static class Pair { + int x; + int y; + + public Pair(int x, int y) { + this.x = x; + this.y = y; + } + } + + public static void sort(int[] arr) { + ArrayList ls = new ArrayList(); + for (int x : arr) + ls.add(x); + Collections.sort(ls); + for (int i = 0; i < arr.length; i++) + arr[i] = ls.get(i); + } + + public static long gcd(long a, long b) { + if (b == 0) + return a; + return gcd(b, a % b); + } + + static boolean[] sieve(int N) { + boolean[] sieve = new boolean[N + 1]; + for (int i = 2; i <= N; i++) + sieve[i] = true; + + for (int i = 2; i <= N; i++) { + if (sieve[i]) { + for (int j = 2 * i; j <= N; j += i) { + sieve[j] = false; + } + } + } + return sieve; + } + + public static long power(long x, long y, long p) { + long res = 1L; + x = x % p; + while (y > 0) { + if ((y & 1) == 1) + res = (res * x) % p; + y >>= 1; + x = (x * x) % p; + } + return res; + } + + public static void print(int[] arr) { + //for debugging only + for (int x : arr) + out.print(x + "" ""); + out.println(); + } + + static class FastScanner { + private int BS = 1 << 16; + private char NC = (char) 0; + private byte[] buf = new byte[BS]; + private int bId = 0, size = 0; + private char c = NC; + private double cnt = 1; + private BufferedInputStream in; + + public FastScanner() { + in = new BufferedInputStream(System.in, BS); + } + + public FastScanner(String s) { + try { + in = new BufferedInputStream(new FileInputStream(new File(s)), BS); + } catch (Exception e) { + in = new BufferedInputStream(System.in, BS); + } + } + + private char getChar() { + while (bId == size) { + try { + size = in.read(buf); + } catch (Exception e) { + return NC; + } + if (size == -1) + return NC; + bId = 0; + } + return (char) buf[bId++]; + } + + public int nextInt() { + return (int) nextLong(); + } + + public int[] readInts(int N) { + int[] res = new int[N]; + for (int i = 0; i < N; i++) { + res[i] = (int) nextLong(); + } + return res; + } + + public long[] readLongs(int N) { + long[] res = new long[N]; + for (int i = 0; i < N; i++) { + res[i] = nextLong(); + } + return res; + } + + public long nextLong() { + cnt = 1; + boolean neg = false; + if (c == NC) + c = getChar(); + for (; (c < '0' || c > '9'); c = getChar()) { + if (c == '-') + neg = true; + } + long res = 0; + for (; c >= '0' && c <= '9'; c = getChar()) { + res = (res << 3) + (res << 1) + c - '0'; + cnt *= 10; + } + return neg ? -res : res; + } + + public double nextDouble() { + double cur = nextLong(); + return c != '.' ? cur : cur + nextLong() / cnt; + } + + public double[] readDoubles(int N) { + double[] res = new double[N]; + for (int i = 0; i < N; i++) { + res[i] = nextDouble(); + } + return res; + } + + public String next() { + StringBuilder res = new StringBuilder(); + while (c <= 32) + c = getChar(); + while (c > 32) { + res.append(c); + c = getChar(); + } + return res.toString(); + } + + public String nextLine() { + StringBuilder res = new StringBuilder(); + while (c <= 32) + c = getChar(); + while (c != '\n') { + res.append(c); + c = getChar(); + } + return res.toString(); + } + + public boolean hasNext() { + if (c > 32) + return true; + while (true) { + c = getChar(); + if (c == NC) + return false; + else if (c > 32) + return true; + } + } + } + + // For Input.txt and Output.txt + // FileInputStream in = new FileInputStream(""input.txt""); + // FileOutputStream out = new FileOutputStream(""output.txt""); + // PrintWriter pw = new PrintWriter(out); + // Scanner sc = new Scanner(in); +}"," +import java.io.*; +import java.util.*; + +public class Main { + private static final boolean N_CASE = true; + private List> g; + private int a; + private int b; + private int da; + private int db; + private int max; + private int ab; + + private int dfs(int u, int fa, int depth) { + if (u == a) { + ab = depth; + } + + int m1 = 0, m2 = 0; + for (int v : g.get(u)) { + if (v != fa) { + int m = dfs(v, u, depth + 1) + 1; + if (m > m1) { m2 = m1; m1 = m; } + else if (m > m2) { m2 = m; } + } + } + max = Math.max(max, m1 + m2); + int cmax = Math.max(m1, m2); + max = Math.max(max, cmax + depth); + return cmax; + } + + private void solve() { + int n = sc.nextInt(); + a = sc.nextInt() - 1; b = sc.nextInt() - 1; + da = sc.nextInt(); db = sc.nextInt(); + + g = createGraph(n); + for (int i = 0; i < n - 1; ++i) { + int u = sc.nextInt() - 1, v = sc.nextInt() - 1; + g.get(u).add(v); + g.get(v).add(u); + } + max = 0; + dfs(b, -1, 0); + db = Math.min(max, db); + + boolean win = true; + if (ab > da) { + if (db > da * 2) { + win = false; + } + } + + out.println(win ? ""Alice"" : ""Bob""); + } + + private void run() { + int T = N_CASE ? sc.nextInt() : 1; + for (int t = 0; t < T; ++t) { + solve(); + } + } + + private static MyWriter out; + private static MyScanner sc; + + private static class MyScanner { + BufferedReader br; + StringTokenizer st; + + private MyScanner() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + int[] nextIntArray(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = nextInt(); + } + return a; + } + + int[][] nextIntArray(int n, int m) { + int[][] a = new int[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + a[i][j] = sc.nextInt(); + } + } + return a; + } + + long[] nextLongArray(int n) { + long[] a = new long[n]; + for (int i = 0; i < n; i++) { + a[i] = nextLong(); + } + return a; + } + + long[][] nextLongArray(int n, int m) { + long[][] a = new long[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + a[i][j] = nextLong(); + } + } + return a; + } + + List nextList(int n) { + List list = new ArrayList<>(); + for (int i = 0; i < n; i++) { + list.add(nextInt()); + } + return list; + } + + List nextLongList(int n) { + List list = new ArrayList<>(); + for (int i = 0; i < n; i++) { + list.add(nextLong()); + } + return list; + } + + char[] nextCharArray(int n) { + return sc.next().toCharArray(); + } + + char[][] nextCharArray(int n, int m) { + char[][] c = new char[n][m]; + for (int i = 0; i < n; i++) { + String s = sc.next(); + for (int j = 0; j < m; j++) { + c[i][j] = s.charAt(j); + } + } + return c; + } + } + + private static class MyWriter extends PrintWriter { + private MyWriter(OutputStream outputStream) { + super(outputStream); + } + + void printArray(int[] a) { + for (int i = 0; i < a.length; ++i) { + print(a[i]); + print(i == a.length - 1 ? '\n' : ' '); + } + } + + void printArray(long[] a) { + for (int i = 0; i < a.length; ++i) { + print(a[i]); + print(i == a.length - 1 ? '\n' : ' '); + } + } + + void println(int[] a) { + for (int v : a) { + println(v); + } + } + + void print(List list) { + for (int i = 0; i < list.size(); ++i) { + print(list.get(i)); + print(i == list.size() - 1 ? '\n' : ' '); + } + } + + void println(List list) { + list.forEach(this::println); + } + } + + private List> createGraph(int n) { + List> g = new ArrayList<>(); + for (int i = 0; i < n; ++i) { + g.add(new ArrayList<>()); + } + return g; + } + + private void fill(int[][] a, int value) { + for (int[] row : a) { + fill(row, value); + } + } + + private void fill(int[] a, int value) { + Arrays.fill(a, value); + } + + public static void main(String[] args) { + out = new MyWriter(new BufferedOutputStream(System.out)); + sc = new MyScanner(); + new Main().run(); + out.close(); + } +}",0,Non-plagiarised +8a39dbf5,ff3283cf,"import java.util.*; +import java.lang.*; +import java.io.*; + +/* Name of the class has to be ""Main"" only if the class is public. */ +public class Codeforces { + public static void main(String[] args) throws java.lang.Exception { + /* your code goes here */ + BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(buf.readLine()); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < t; i++) { + String st=buf.readLine(); + String st1[]=(buf.readLine()).split("" ""); + int n=Integer.parseInt(st1[0]); + int k=Integer.parseInt(st1[1]); + int a[]=new int[k]; + int temp[]=new int[k]; + long arr[]=new long[n]; + String st2[]=(buf.readLine()).split("" ""); + String st3[]=(buf.readLine()).split("" ""); + for(int j=0;j=0;j--) + { + if(arr[j]==0) + { + right[j]=right[j+1]+1; + } + else + { + right[j]=Math.min(right[j+1]+1,arr[j]); + } + } + for(int j=0;j=0;j--) + { + if(arr[j]==0) + { + right[j]=right[j+1]+1; + } + else + { + right[j]=Math.min(right[j+1]+1,arr[j]); + } + } + for(int j=0;j towers = new PriorityQueue(); + for (int j = 1; j <= M; j++) towers.add(new Tower(j, 0)); + out.println(""YES""); + st = new StringTokenizer(in.readLine()); + for (int j = 0; j < N; j++) { + Tower t = towers.remove(); + t.size += Integer.parseInt(st.nextToken()); + towers.add(t); + out.print(t.idx + "" ""); + } + out.println(); + } + + out.close(); + in.close(); + } + + public static class Tower implements Comparable { + int idx, size; + public Tower(int idx, int size) { + this.idx = idx; + this.size = size; + } + @Override + public int compareTo(Tower o) { + return size - o.size; + } + } +}","//package pack; +import java.io.*; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; +import java.util.*; +import java.io.*; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.*; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; +import java.math.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; +import java.util.Vector; +import static java.lang.Math.sqrt; +import static java.lang.Math.floor; + + + + + + +public class topcoder { + + static class pair{ + + int first; + int second; + public pair(int first, int second) { + this.first = first; + this.second = second; + } + } + + + static class Compare{ + + static void compare(ArrayListarr, int n) { + + Collections.sort(arr,new Comparator() { + public int compare(pair p1, pair p2) { + return p1.first-p2.first; + } + }); + + } + } + + +static class pairr implements Comparable{ + Integer value; + Integer index; + + public pairr(Integer value, Integer index) { + this.value = value; + this.index = index; + } + @Override + + public int compareTo(pairr o) { + return value-o.value; + } +} + + + + static class Key + { + public K1 key1; + public K2 key2; + + public Key(K1 key1, K2 key2) + { + this.key1 = key1; + this.key2 = key2; + } + + @Override + public boolean equals(Object o) + { + if (this == o) { + return true; + } + + if (o == null || getClass() != o.getClass()) { + return false; + } + + Key key = (Key) o; + if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) { + return false; + } + + if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) { + return false; + } + + return true; + } + + @Override + public int hashCode() + { + int result = key1 != null ? key1.hashCode() : 0; + result = 31 * result + (key2 != null ? key2.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return ""["" + key1 + "", "" + key2 + ""]""; + } + } + + + public static int sumOfDigits (long n) { + + int sum = 0; + + while(n > 0) { + sum += n%10; + n /= 10; + } + + return sum; + + } + + + public static long binary_search(int s, int e, long num, long []ar) { + + if(s > e) { + return -1; + } + + + int mid = (s+e)/2; + + if(s == e && ar[s] >= num) { + return ar[s]; + }else if(s == e && ar[s] < num) { + return -1; + }else if(ar[mid] < num) { + return binary_search(mid+1,e,num,ar); + }else if(ar[mid] >= num) { + return binary_search(s,mid,num,ar); + } + + return -1; + + } + + + public static int index_search(int s, int e, long num, long []ar) { + + if(s > e) { + return -1; + } + + + int mid = (s+e)/2; + + if(s == e && ar[s] >= num) { + return s; + }else if(s == e && ar[s] < num) { + return -1; + }else if(ar[mid] < num) { + return index_search(mid+1,e,num,ar); + }else if(ar[mid] >= num) { + return index_search(s,mid,num,ar); + } + + return -1; + + } + public static void swap(int []ar, int i, int j) { + + + for(int k= j; k >= i; k--) { + int temp = ar[k]; + ar[k] = ar[k+1]; + ar[k+1] = temp; + } + } + + public static boolean digit_exists(long n) { + + while(n > 0) { + if(n%10 == 9) + return true; + n = n/10; + } + + return false; + } + public static int log(int n) { + + int c = 0; + while(n > 0) { + c++; + n /=2; + } + + return c; + } + public static int findOr(int[]bits){ + int or=0; + for(int i=0;i<32;i++){ + or=or<<1; + if(bits[i]>0) + or=or+1; + } + return or; + } + + static void simpleSieve(int limit, Vector prime) + { + // Create a boolean array ""mark[0..n-1]"" and initialize + // all entries of it as true. A value in mark[p] will + // finally be false if 'p' is Not a prime, else true. + boolean mark[] = new boolean[limit+1]; + + for (int i = 0; i < mark.length; i++) + mark[i] = true; + + for (int p=2; p*pl) + { + // Compute all primes smaller than or equal + // to square root of n using simple sieve + int limit = (int) (floor(sqrt(n))+1); + Vector prime = new Vector<>(); + + simpleSieve(limit, prime); + + // Divide the range [0..n-1] in different segments + // We have chosen segment size as sqrt(n). + int low = limit; + int high = 2*limit; + + // While all segments of range [0..n-1] are not processed, + // process one segment at a time + while (low < n) + { + if (high >= n) + high = n; + + // To mark primes in current range. A value in mark[i] + // will finally be false if 'i-low' is Not a prime, + // else true. + boolean mark[] = new boolean[limit+1]; + + for (int i = 0; i < mark.length; i++) + mark[i] = true; + + // Use the found primes by simpleSieve() to find + // primes in current range + for (int i = 0; i < prime.size(); i++) + { + // Find the minimum number in [low..high] that is + // a multiple of prime.get(i) (divisible by prime.get(i)) + // For example, if low is 31 and prime.get(i) is 3, + // we start with 33. + int loLim = (int) (floor(low/prime.get(i)) * prime.get(i)); + if (loLim < low) + loLim += prime.get(i); + + /* Mark multiples of prime.get(i) in [low..high]: + We are marking j - low for j, i.e. each number + in range [low, high] is mapped to [0, high-low] + so if range is [50, 100] marking 50 corresponds + to marking 0, marking 51 corresponds to 1 and + so on. In this way we need to allocate space only + for range */ + for (int j=loLim; j 0) { + power++; + k /=2 ; + } + + long check = (long)Math.pow(2, power-1); + if(k1 == check) { + return power; + } + // System.out.println(power); + long f = (long)Math.pow(2, power-1); + long rem = k1-f; + return find_indexNum(rem); + } + + public static void sortPair(ArrayListl, int n) { + n = l.size(); + + Compare obj = new Compare(); + obj.compare(l, n); + } + + + public static int add(long n, long num, long a, int i) { + //System.out.println(num); + if(num > n)return -1; + + if(num == n) { + + return i; + } + + if(a < 2050)return -1; + + + + long temp = num+a; + + if(temp <= n) { + return add(n,temp,a,i+1); + }else if(temp > n){ + a /= 10; + return add(n,num,a,i); + } + + return -1; + } + + + public static void shuffle(int []array, int num,int t_index, boolean []vis, int m ) { + + + for(int i = 0; i < m; i++) { + if(vis[i] == false) { + + + int temp = array[i]; + if(i < t_index) { + vis[i] = true; + } + array[i] = num; + array[t_index] = temp; + // System.out.println(array[t_index]+"" ""+array[i]); + break; + } + } + } + + public static void rotate(int []arr,int j, int times, int m) { + + + + if(j == 0) { + int temp1 = arr[0]; + arr[0] = arr[times]; + arr[times] = temp1; + + }else { + int temp = arr[j]; + int z = arr[0]; + + arr[0] = arr[times]; + + arr[j] = z; + arr[times] = temp; + + + + } + + } + + public static void recur(int i,int A, int B,int []dp,int []metal, int n, boolean took,int ind) { + + if(i-A <= 0 && i-B <= 0)return; + int count = 0; + + for(int j = 1; j <= n; j++) { + if(dp[j] >= metal[j]) { + count++; + } + } + + if(count == n)return; + if(i-A >= 0 && i-B >= 0 && dp[i] > 0 && dp[i] > metal[i]) { + dp[i]--; + + dp[i-A]++; + + dp[i-B]++; + } + + + if(ind == 6) { + // System.out.println(Arrays.toString(dp)); + } + recur(i-A,A,B,dp,metal,n,took,ind); + recur(i-B,A,B,dp,metal,n,took,ind); + } + + + public static boolean isPrime(int n) { + if (n <= 1) return false; + if (n <= 3) return true; + if (n % 2 == 0 || n % 3 == 0) return false; + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + return true; + } + + public static boolean[] getSieve(int n) { + boolean[] isPrime = new boolean[n+1]; + for (int i = 2; i <= n; i++) isPrime[i] = true; + for (int i = 2; i*i <= n; i++) if (isPrime[i]) + for (int j = i; i*j <= n; j++) isPrime[i*j] = false; + return isPrime; + } + + + public static int gcd(int a, int b) { + int tmp = 0; + while(b != 0) { + tmp = b; + b = a%b; + a = tmp; + } + return a; + } + + + public static long gcd(long a, long b) { + long tmp = 0; + while(b != 0) { + tmp = b; + b = a%b; + a = tmp; + } + return a; + } + + + public static boolean poss( int r, int c, int k, int n, int m,boolean [][]vis) { + + if(k < 0)return false; + if(r > n || c > m || r < 1 || c < 1)return false; + + if(r == n && c == m && k == 0) { + // System.out.println(k); + return true; + } + if(vis[r][c] == true) { + return false; + } + vis[r][c] = true; + + + return poss(r+1,c,k-c,n,m,vis) || poss(r,c+1,k-r,n,m,vis); + + + } + + public static void dfs(LinkedList[]list, HashMapmap, int parent, int n) { + + Stackst = new Stack<>(); + + + } + public static boolean pos(int n) { + + + int i = 1; + boolean pos = false; + + while(i*i <= n) { + if(i*i*2 == n || i*i*4 == n) { + pos = true; + break; + } + i++; + } + if(pos)return true; + return false; + + } + + + public static void main(String args[])throws IOException{ + + // System.setIn(new FileInputStream(""Case.txt"")); + BufferedReader ob = new BufferedReader(new InputStreamReader(System.in)); + + + int t = Integer.parseInt(ob.readLine()); + + + while( t--> 0) { + + StringTokenizer st = new StringTokenizer(ob.readLine()); + int n = Integer.parseInt(st.nextToken()); + int m = Integer.parseInt(st.nextToken()); + int x = Integer.parseInt(st.nextToken()); + + int []ar = new int[n]; + st = new StringTokenizer(ob.readLine()); + PriorityQueuepq = new PriorityQueue<>(); + + for(int i = 0; i < n; i++) { + ar[i] = Integer.parseInt(st.nextToken()); + pq.add(ar[i]); + } + + + PriorityQueuep = new PriorityQueue<>(); + for(int i =0; i < n; i++) { + p.add(new pairr(ar[i],i)); + } + int []ans = new int[n]; + + + for(int i = 0; i < n; i++) { + pairr a = p.poll(); + ans[a.index] = (i%m)+1; + } + + System.out.println(""YES""); + for(int i = 0; i < n; i++ ) { + System.out.print(ans[i]+"" ""); + } + System.out.println(); + } + } +} + + + + ",0,Non-plagiarised +c57997b6,ff10b574,"import java.io.*; +import java.util.*; +import java.util.Map.Entry; + +public class AMain { + + public static void main(String[] args) { + QuickReader in = new QuickReader(System.in); + + try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));) + { + new AMain().solve(in, out); + } + } + + public void solve(QuickReader in, PrintWriter out) + { + int t = in.nextInt(); + while(t-->0) + { + int n = in.nextInt(); + int[] a= in.nextInts(n); + char[] clr = in.next().toCharArray(); + + ArrayList > rgh = new ArrayList<>(); + for(int i=0;i()); + + boolean ok = true; + + for(int i=0;in) + ok = false; + else + rgh.get(Math.max(0, a[i]-1)).add(n-1); + } + + + TreeMap q = new TreeMap<>(); + for(int i=0;i cur = q.firstEntry(); + + if(cur.getValue() == 1) + q.remove(cur.getKey()); + else + q.put(cur.getKey(), cur.getValue()-1); + } + out.println(ok? ""YES"":""NO""); + out.flush(); + } + + } + +} + +class QuickReader +{ + BufferedReader in; + StringTokenizer token; + + public QuickReader(InputStream ins) + { + in=new BufferedReader(new InputStreamReader(ins)); + token=new StringTokenizer(""""); + } + + public boolean hasNext() + { + while (!token.hasMoreTokens()) + { + try + { + String s = in.readLine(); + if (s == null) return false; + token = new StringTokenizer(s); + } catch (IOException e) + { + throw new InputMismatchException(); + } + } + return true; + } + + public String next() + { + hasNext(); + return token.nextToken(); + } + + public int nextInt() + { + return Integer.parseInt(next()); + } + + public int[] nextInts(int n) + { + int[] res = new int[n]; + for (int i = 0; i < n; i++) + res[i] = nextInt(); + return res; + } + + public long nextLong() { + return Long.parseLong(next()); + } + +} + +","import java.io.*; +import java.util.*; + +public class Main { + public static void main(String[] args) throws IOException { + int t = nextInt(); + while (t-- != 0) { + int n = nextInt(); + int[] a = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = nextInt(); + } + String s = nextToken(); + ArrayList r = new ArrayList<>(); + ArrayList b = new ArrayList<>(); + for (int i = 0; i < n; i++) { + if (s.charAt(i) == 'B') b.add(a[i]); + else r.add(a[i]); + } + Collections.sort(b); + Collections.sort(r); + int y = 1; + boolean e = true; + for (int i = 0; i < b.size(); i++) { + if (b.get(i) < y) { + e = false; + break; + } + y++; + } + for (int i = 0; i < r.size(); i++) { + if (r.get(i) > y) { + e = false; + break; + } + y++; + } + if(e)out.println(""YES""); + else out.println(""NO""); + } + out.close(); + } + + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static PrintWriter out = new PrintWriter(System.out); + static StringTokenizer in = new StringTokenizer(""""); + + + public static boolean hasNext() throws IOException { + if (in.hasMoreTokens()) return true; + String s; + while ((s = br.readLine()) != null) { + in = new StringTokenizer(s); + if (in.hasMoreTokens()) return true; + } + return false; + } + + public static String nextToken() throws IOException { + while (!in.hasMoreTokens()) { + in = new StringTokenizer(br.readLine()); + } + return in.nextToken(); + } + + public static int nextInt() throws IOException { + return Integer.parseInt(nextToken()); + } + + public static long nextLong() throws IOException { + return Long.parseLong(nextToken()); + } + + public static double nextDouble() throws IOException { + return Double.parseDouble(nextToken()); + } +}",0,Non-plagiarised +829d2024,9852706b,"import java.util.*; +import java.io.*; + + +public class Main{ + public static class Element implements Comparable{ + public int key; + public int value; + Element(int k, int v) + { + key=k; + value=v; + } + + @Override + public int compareTo(Element o) { + if(this.valueo.value) + return 1; + return 0; + } + } + 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 x=sc.nextInt(); + int[] arr=new int[n]; + for(int i=0;i pq=new PriorityQueue<>(); + for(int i=1;i<=m;++i) + { + pq.add(new Element(i,0)); + } + System.out.println(""YES""); + for(int j=0;j0){ + int n=in.nextInt(); + int m=in.nextInt(); + int x=in.nextInt(); + PriorityQueue pq=new PriorityQueue<>(); + int arr[]=new int[n]; + for(int i=1;i<=m;i++){ + pq.add(new pair(i,0)); + } + System.out.println(""YES""); + for(int i=0;ik;i--){ + num*=i; + den*=j; + j++; + } + return num/den; + } + public static int countDiff(int arr[],int n,int diff){ + int sum=0; + + for(int i=0;i + { + int x; + int y; + + pair(int x, int y) { + this.x = x; + this.y = y; + } + public int compareTo(pair o) + { + return (int)(y-o.y); + + } +}",1,Plagiarised +213340b3,35f0c004,"import java.util.*; +import java.io.*; +public class Solution { + static class FastReader { + BufferedReader br; + StringTokenizer st; + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + int nextInt() { + return Integer.parseInt(next()); + } + long nextLong() { + return Long.parseLong(next()); + } + double nextDouble() { + return Double.parseDouble(next()); + } + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } + static void sort(int a[]){ // int -> long + ArrayList arr=new ArrayList<>(); // Integer -> Long + for(int i=0;i0){ + res++; + n/=2; + } + return res; + } + + static int mod = (int)1e9+7; + static int INF = Integer.MAX_VALUE; + static PrintWriter out; + static FastReader sc ; + public static void main(String[] args) throws IOException { + sc = new FastReader(); + out = new PrintWriter(System.out); + // primes(); + // ================================ // + int test = sc.nextInt(); + while (test-- > 0) { + int n = sc.nextInt(); + String s = sc.nextLine(); + String t = sc.nextLine(); + solver(s,t, n); + } + // ================================ // + // int n = sc.nextInt(); + // solver(); + // ================================ // + out.flush(); + } + + public static void solver(String s, String t, int n) { + int diff = 0; + int one = 0; + int zero = 0; + for(int i=0;i=0 && (n-diff-1)%2==0 && zero>0 && (n-diff-zero)==zero-1){ + res = Math.min(res, (n-diff-1)+1); + } + out.println(res==INF?-1:res); + } +} +","import java.io.*; +import java.util.*; +public class Main { + public static void main(String args[]) + { + FastReader input=new FastReader(); + PrintWriter out=new PrintWriter(System.out); + int T=input.nextInt(); + while(T-->0) + { + int n=input.nextInt(); + String a=input.next(); + String b=input.next(); + int same1=0,same0=0,opp1=0,opp0=0; + for(int i=0;i x=new ArrayList<>(); + ArrayList y=new ArrayList<>(); + for(int i=0;i=0;i--){ + if(y.get(i)>p){System.out.println(""NO""); return;} + p-=1; + } + for(int i=0;i0) solve(); + } +} +","import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Scanner; + +public class Simple{ + public static void main(String args[]){ + //System.out.println(""Hello Java""); + 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 blue = new ArrayList<>(); + ArrayList red = new ArrayList<>(); + for(int i=0;istart){ + bool = false; + break; + } + start++; + } + if(bool){ + System.out.println(""YES""); + } + else{ + System.out.println(""NO""); + } + } + + + + + t--; + } + s.close(); + } + +} +",0,Non-plagiarised +39210347,68dff461,"//#Rohitpratap311 +//Keep_Calm_And_Stay_Happy +import java.io.*; +import java.util.*; +public class Main { + public static void main(String[] args) { + FastReader input=new FastReader(); + PrintWriter out=new PrintWriter(System.out); + int T=input.nextInt(); + while(T-->0) + { + int n=input.nextInt(); + int a[]=new int[n]; + for(int i=0;i=0 && a[i+1]<0) || (a[i]<0 && a[i+1]>=0)) + { + b[i]=x; + b[i+1]=y; + } + else + { + b[i]=x; + b[i+1]=-y; + } + } + for(int i=0;i=0 && a[i+1]<0) || (a[i]<0 && a[i+1]>=0)) + { + b[i]=x; + b[i+1]=y; + } + else + { + b[i]=x; + b[i+1]=-y; + } + } + int l1=lcm(Math.abs(a[n-3]),Math.abs(a[n-2])); + int x=l1/Math.abs(a[n-3]),y=l1/Math.abs(a[n-2]); + int l2=lcm(Math.abs(a[n-3]),Math.abs(a[n-1])); + int z=l2/Math.abs(a[n-3]); + if((a[n-3]>=0 && a[n-2]<0) || (a[n-3]<0 && a[n-2]>=0)) + { + + } + else + { + y=-y; + } + x+=z; + int sum=a[n-3]*x+a[n-2]*y; + sum=-sum; + z=sum/a[n-1]; + b[n-3]=x; + b[n-2]=y; + b[n-1]=z; + for(int i=0;i0) + { + int n=input.nextInt(); + int a[]=new int[n]; + for(int i=0;i=0 && a[i+1]<0) || (a[i]<0 && a[i+1]>=0)) + { + b[i]=x; + b[i+1]=y; + } + else + { + b[i]=x; + b[i+1]=-y; + } + } + for(int i=0;i=0 && a[i+1]<0) || (a[i]<0 && a[i+1]>=0)) + { + b[i]=x; + b[i+1]=y; + } + else + { + b[i]=x; + b[i+1]=-y; + } + } + int l1=lcm(Math.abs(a[n-3]),Math.abs(a[n-2])); + int x=l1/Math.abs(a[n-3]),y=l1/Math.abs(a[n-2]); + int l2=lcm(Math.abs(a[n-3]),Math.abs(a[n-1])); + int z=l2/Math.abs(a[n-3]); + if((a[n-3]>=0 && a[n-2]<0) || (a[n-3]<0 && a[n-2]>=0)) + { + + } + else + { + y=-y; + } + x+=z; + int sum=a[n-3]*x+a[n-2]*y; + sum=-sum; + z=sum/a[n-1]; + b[n-3]=x; + b[n-2]=y; + b[n-1]=z; + for(int i=0;i l = new ArrayList<>(a.length); + for (int i : a) l.add(i); + Collections.sort(l); + for (int i = 0; i < a.length; i++) a[i] = l.get(i); + } + + //-----------PrintWriter for faster output--------------------------------- + public static FastIO io = new FastIO(); + + //-----------MyScanner class for faster input---------- + static class FastIO extends PrintWriter { + private InputStream stream; + private byte[] buf = new byte[1 << 16]; + private int curChar, numChars; + + // standard input + public FastIO() { + this(System.in, System.out); + } + + public FastIO(InputStream i, OutputStream o) { + super(o); + stream = i; + } + + // file input + public FastIO(String i, String o) throws IOException { + super(new FileWriter(o)); + stream = new FileInputStream(i); + } + + // throws InputMismatchException() if previously detected end of file + private int nextByte() { + if (numChars == -1) throw new InputMismatchException(); + if (curChar >= numChars) { + curChar = 0; + try { + numChars = stream.read(buf); + } catch (IOException e) { + throw new InputMismatchException(); + } + if (numChars == -1) return -1; // end of file + } + return buf[curChar++]; + } + + // to read in entire lines, replace c <= ' ' + // with a function that checks whether c is a line break + public String next() { + int c; + do { + c = nextByte(); + } while (c <= ' '); + StringBuilder res = new StringBuilder(); + do { + res.appendCodePoint(c); + c = nextByte(); + } while (c > ' '); + return res.toString(); + } + + public String nextLine() { + int c; + do { + c = nextByte(); + } while (c < '\n'); + StringBuilder res = new StringBuilder(); + do { + res.appendCodePoint(c); + c = nextByte(); + } while (c > '\n'); + return res.toString(); + } + + public int nextInt() { + int c; + do { + c = nextByte(); + } while (c <= ' '); + int sgn = 1; + if (c == '-') { + sgn = -1; + c = nextByte(); + } + int res = 0; + do { + if (c < '0' || c > '9') + throw new InputMismatchException(); + res = 10 * res + c - '0'; + c = nextByte(); + } while (c > ' '); + return res * sgn; + } + + public long nextLong() { + int c; + do { + c = nextByte(); + } while (c <= ' '); + int sgn = 1; + if (c == '-') { + sgn = -1; + c = nextByte(); + } + long res = 0; + do { + if (c < '0' || c > '9') + throw new InputMismatchException(); + res = 10 * res + c - '0'; + c = nextByte(); + } while (c > ' '); + return res * sgn; + } + + + public double nextDouble() { + return Double.parseDouble(next()); + } + } + //-------------------------------------------------------- +}"," +import java.util.*; +import java.util.stream.Collectors; +import java.io.*; +import java.math.*; + +public class GR18_C2 { + public static FastScanner sc; + public static StringBuilder sb ;public static int MOD= 1000000007; + public static class FastScanner { + BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st=new StringTokenizer(""""); + String next() { + while (!st.hasMoreTokens()) + try { + st=new StringTokenizer(br.readLine()); + } catch (IOException e) {} + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + long nextLong() { + return Long.parseLong(next()); + } + String nextLine() throws IOException { + return br.readLine(); + } + } + + public static void solve(int t) throws IOException { + int n=sc.nextInt(); + String a=sc.next(); + String b=sc.next(); + + int _00=0; + int _01=0; + int _10=0; + int _11=0; + + for(int i=0;i0) { + int n=sc.nextInt(); + int freq[][]=new int[n][5]; + int rem[][]=new int[n][5]; + for(int i=0;i=0;k--) { + if(sum+arr[k]>0) { + sum=sum+arr[k]; + total++; + } + else { + break; + } + } + ans=Math.max(ans,total); + } + out.println(ans); + + } + out.flush(); + out.close(); + } +} + +","import java.util.*; + +import java.io.*; +import java.math.*; + + +public class A +{ + static FastReader sc=new FastReader(); + static long dp[][]; + static int mod=1000000007; + static int max; + static int bit[]; + static long ans; + static HashMap map; + + + public static void main(String[] args) + { + //CHECK FOR N=1 + //CHECK FOR N=1 + PrintWriter out=new PrintWriter(System.out); + StringBuffer sb=new StringBuffer(""""); + + int ttt=1; + ttt =i(); + + outer :while (ttt-- > 0) + { + int n=i(); + String A[]=inputS(n); + Pair P[]=new Pair[n]; + int B[][]=new int[n][5]; + for(int i=0;i + { + int x; + int y; + Pair(int x,int y){ + this.x=x; + this.y=y; + + } + @Override + public int compareTo(Pair o) { + int a=o.y-o.x; + int b=this.y-this.x; + if(a>b) + return 1; + else if(a o.x) { +// return 1; +// } +// if (x < o.x) { +// return -1; +// } +// if (y > o.y) { +// return 1; +// } +// if (y < o.y) { +// return -1; +// } +// return 0; +// } +// public int hashCode() +// { +// final int temp = 14; +// int ans = 1; +// ans =x*31+y*13; +// return ans; +// } +// +// // Equal objects must produce the same +// // hash code as long as they are equal +// @Override +// public boolean equals(Object o) +// { +// if (this == o) { +// return true; +// } +// if (o == null) { +// return false; +// } +// if (this.getClass() != o.getClass()) { +// return false; +// } +// Pair other = (Pair)o; +// if (this.x != other.x || this.y!=other.y) { +// return false; +// } +// return true; +// } + + } +//FRENWICK TREE +static void update(int i, int x){ + for(; i < bit.length; i += (i&-i)) + bit[i] += x; +} + +static int sum(int i){ + int ans = 0; + for(; i > 0; i -= (i&-i)) + ans += bit[i]; + return ans; +} +//END +static void add(int v) { + if(!map.containsKey(v)) { + map.put(v, 1); + } + else { + map.put(v, map.get(v)+1); + } +} +static void remove(int v) { + if(map.containsKey(v)) { + map.put(v, map.get(v)-1); + if(map.get(v)==0) + map.remove(v); + } +} +static int[] copy(int A[]) { + int B[]=new int[A.length]; + for(int i=0;i=0;i--) + p[i]=p[i+1]+A[i]; + return p; +} +static long [] suffix(int A[]) { + long p[]=new long[A.length]; + p[A.length-1]=A[A.length-1]; + for(int i=A.length-2;i>=0;i--) + p[i]=p[i+1]+A[i]; + return p; +} +static void fill(int dp[]) { + Arrays.fill(dp, -1); +} +static void fill(int dp[][]) { + for(int i=0;i 0) { + + if (y % 2 == 1) + res = (res * x) % p; + + y = y >> 1; + x = (x * x) % p; + } + + return res; +} +static void print(int A[]) { + for(int i : A) { + System.out.print(i+"" ""); + } + System.out.println(); +} +static void print(long A[]) { + for(long i : A) { + System.out.print(i+"" ""); + } + System.out.println(); +} +static long mod(long x) { + return ((x%mod + mod)%mod); +} +static String reverse(String s) { + StringBuffer p=new StringBuffer(s); + p.reverse(); + return p.toString(); +} + + static int i() { + return sc.nextInt(); + } + static String s() { + return sc.next(); + } + static long l() { + return sc.nextLong(); + } + static void sort(int[] A){ + int n = A.length; + Random rnd = new Random(); + for(int i=0; i hash(int A[]){ + HashMap map=new HashMap(); + for(int i : A) { + if(map.containsKey(i)) { + map.put(i, map.get(i)+1); + } + else { + map.put(i, 1); + } + } + return map; + } + static TreeMap tree(int A[]){ + TreeMap map=new TreeMap(); + for(int i : A) { + if(map.containsKey(i)) { + map.put(i, map.get(i)+1); + } + else { + map.put(i, 1); + } + } + return map; + } + static boolean prime(int n) + { + if (n <= 1) + return false; + if (n <= 3) + return true; + if (n % 2 == 0 || n % 3 == 0) + return false; + double sq=Math.sqrt(n); + + for (int i = 5; i <= sq; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + return true; + } + static boolean prime(long n) + { + if (n <= 1) + return false; + if (n <= 3) + return true; + if (n % 2 == 0 || n % 3 == 0) + return false; + double sq=Math.sqrt(n); + + for (int i = 5; i <= sq; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + return true; + } + static int gcd(int a, int b) + { + if (a == 0) + return b; + return gcd(b % a, a); + } + static long gcd(long a, long b) + { + if (a == 0) + return b; + return gcd(b % a, a); + } + + + static class FastReader + { + BufferedReader br; + StringTokenizer st; + + public FastReader() + { + br = new BufferedReader(new + InputStreamReader(System.in)); + } + + String next() + { + while (st == null || !st.hasMoreElements()) + { + try + { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() + { + return Integer.parseInt(next()); + } + + long nextLong() + { + return Long.parseLong(next()); + } + + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str = """"; + try + { + str = br.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + return str; + } + } +} +",0,Non-plagiarised +1a400c1b,b505f95e,"import java.util.*; +import java.io.*; +import java.lang.*; +import static java.lang.Math.*; + +public class cp{ + static BufferedReader br; + static StringTokenizer st; + + public static void main(String[] args)throws IOException{ + + br = new BufferedReader(new InputStreamReader(System.in)); + List answer = new ArrayList(); + + for(int t=Integer.parseInt(br.readLine()); t>0; t--){ + + st = readLine(); + st = readLine(); + int n = tokenInt(); + int k = tokenInt(); + + st = readLine(); + int[] posArr = readIntArray(k); + st = readLine(); + int[] tempArr = readIntArray(k); + + answer.add(new Solver().solve(n, k, posArr, tempArr)); + + } + + ListIterator it = answer.listIterator(); + while (it.hasNext()) + printArr(it.next()); + + } + + static StringTokenizer readLine() throws IOException { return new StringTokenizer(br.readLine()); } + static int tokenInt() { return Integer.parseInt(st.nextToken()); } + static long tokenLong(){ return Long.parseLong(st.nextToken()); } + static double tokenDouble(){ return Double.parseDouble(st.nextToken()); } + static char[] tokenChar(){ return st.nextToken().toCharArray(); } + static int[] readIntArray(int n) { + int[] a=new int[n]; + for (int i=0; i=0; j--) + answer[j] = min(answer[j+1]+1, answer[j]); + + return answer; + } +} + + +/* + + + +*/ +","import java.util.*; +import java.io.*; + +public class E { + public static void main(String[] args) throws IOException { + setIO(); + + int INF = 2000000000; + + int Q = ni(); + while (Q-- > 0) { + rl(); + st = nl(); + int N = ni(st), K = ni(st); + int[] P = nia(K); + int[] T = nia(K); + int[] A = new int[N]; + Arrays.fill(A, INF); + for (int i = 0; i < K; i++) { + A[P[i]-1] = T[i]; + } + + int[] R = new int[N]; + R[N-1] = A[N-1]; + for (int i = N-2; i >= 0; i--) { + R[i] = Math.min(R[i+1] + 1, A[i]); + } + + int[] L = new int[N]; + L[0] = A[0]; + for (int i = 1; i < N; i++) { + L[i] = Math.min(L[i-1] + 1, A[i]); + } + + for (int i = 0; i < N; i++) { + out.print(Math.min(L[i], R[i]) + "" ""); + } + out.println(); + } + + f.close(); + out.close(); + } + + static BufferedReader f; + static PrintWriter out; + static StringTokenizer st; + + static String rl() throws IOException { + return f.readLine(); + } + + static int ni(StringTokenizer st) { + return Integer.parseInt(st.nextToken()); + } + + static long nlg(StringTokenizer st) { + return Long.parseLong(st.nextToken()); + } + + static int ni() throws IOException { + return Integer.parseInt(rl()); + } + + static long nlg() throws IOException { + return Long.parseLong(rl()); + } + + static StringTokenizer nl() throws IOException { + return new StringTokenizer(rl()); + } + + static int[] nia(int N) throws IOException { + StringTokenizer st = nl(); + int[] A = new int[N]; + for (int i = 0; i < N; i++) + A[i] = ni(st); + return A; + } + + static void setIn(String s) throws IOException { + f = new BufferedReader(new FileReader(s)); + } + + static void setOut(String s) throws IOException { + out = new PrintWriter(new FileWriter(s)); + } + + static void setIn() { + f = new BufferedReader(new InputStreamReader(System.in)); + } + + static void setOut() { + out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); + } + + static void setIO(String s) throws IOException { + setIn(s + "".in""); + setOut(s + "".out""); + } + + static void setTextIO(String s) throws IOException { + setIn(s + "".txt""); + setOut(s + ""_out.txt""); + } + + static void setIO() { + setIn(); + setOut(); + } +}",0,Non-plagiarised +b2590225,f7fc2e94,"//package com.company; + +import java.util.*; + +public class P3 { + public static class tower implements Comparable{ + ArrayList index; + int size; + tower(){ + size = 0; + index = new ArrayList<>(); + } + @Override + public int compareTo(tower a) { + if(this.size > a.size) { + return 1; + } else if (this.size < a.size) { + return -1; + } else { + return 0; + } + } + } + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int t = scan.nextInt(); + while(t>0){ + t--; + int n = scan.nextInt(); + int m = scan.nextInt(); + int x = scan.nextInt(); + + + ArrayList arr = new ArrayList<>(); + HashMap map = new HashMap<>(); + for(int i = 0;i towers = new PriorityQueue<>(); + for(int i = 0;i 0){ + tower curr = towers.poll(); + for(Integer p : curr.index){ + ans[p] = count; + } + count++; + } + for(int i = 0;i + { + int h,index; + height(int hi,int i) + { + h=hi; + index=i; + } + public int compareTo(height a) + { + return this.h-a.h; + } + public String toString() + { + return ""(""+this.h+"",""+this.index+"") ""; + } + } + static class FastReader + { + BufferedReader br; + StringTokenizer st; + + public FastReader() + { + br = new BufferedReader(new + InputStreamReader(System.in)); + } + String next() + { + while (st == null || !st.hasMoreElements()) + { + try + { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() + { + return Integer.parseInt(next()); + } + + long nextLong() + { + return Long.parseLong(next()); + } + + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str = """"; + try + { + str = br.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + return str; + } + } + public static void main(String[] args) + { + FastReader sc=new FastReader(); + int T=sc.nextInt(); + while(T-->0) + { + int n=sc.nextInt(),m=sc.nextInt(),x=sc.nextInt(),i; + ArrayList arr=new ArrayList<>(); + int ans[]=new int[n]; + PriorityQueue tower=new PriorityQueue(); + for(i=0;i [] adj; + static ArrayList temp; + static int mod = (int) 1e9+7; + static boolean[] vis = new boolean[(int)1e5]; + static List l = new ArrayList<>(); + static final Reader s = new Reader(); + + public static void main(String[] args) throws IOException { + int t = s.nextInt(); +// int t=1; + while(t-->0) { + int n = s.nextInt(); + int m = s.nextInt(); + int d = s.nextInt(); + System.out.println(""YES""); + PriorityQueue pq = new PriorityQueue<>(); + for(int i=1;i<=m;i++){ + pq.add(new Pair(i)); + } + for(int i=0;i { + int id; + long sum=0; + public Pair(int id) { + this.id=id; + } + public int compareTo(Pair o) { + return Long.compare(sum, o.sum); + } + } + //Important + //Dont't stick on same approach + //Check long + static void dfs(int i){ + vis[i]=true; + l.add(i); + List Y = adj[i]; + for(int x:Y){ + if(!vis[x])dfs(x); + } + } + + static long gcd(long a, long b) throws IOException { + return (b == 0) ? a : gcd(b, a % b); + } + + static int gcd(int a, int b) throws IOException { + return (b == 0) ? a : gcd(b, a % b); + } + + static void sortr(int[] a) { + ArrayList l = new ArrayList<>(); + for (int i : a) + l.add(i); + Collections.sort(l, Collections.reverseOrder()); + for (int i = 0; i < a.length; i++) + a[i] = l.get(i); + } + + static void sort(int[] a) { + ArrayList l = new ArrayList<>(); + for (int i : a) + l.add(i); + Collections.sort(l); + for (int i = 0; i < a.length; i++) + a[i] = l.get(i); + } + + static void sort(long[] a) { + ArrayList l = new ArrayList<>(); + for (long i : a) + l.add(i); + Collections.sort(l); + for (int i = 0; i < a.length; i++) + a[i] = l.get(i); + } + + static class Reader { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + String next() { + while(st==null||!st.hasMoreTokens()) { + try { + st=new StringTokenizer(in.readLine()); + } catch(Exception e) {} + } + return st.nextToken(); + } + int nextInt() { + return Integer.parseInt(next()); + } + long nextLong() { + return Long.parseLong(next()); + } + } + +}","import java.io.BufferedReader; +import java.io.DataInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; +public class Main { + static int modulo=998244353; + public static void main(String[] args) { + + FastScanner in = new FastScanner(); + int test=in.nextInt(); + while(test-->0){ + int n=in.nextInt(); + int m=in.nextInt(); + int x=in.nextInt(); + PriorityQueue pq=new PriorityQueue<>(); + int arr[]=new int[n]; + for(int i=1;i<=m;i++){ + pq.add(new pair(i,0)); + } + System.out.println(""YES""); + for(int i=0;ik;i--){ + num*=i; + den*=j; + j++; + } + return num/den; + } + public static int countDiff(int arr[],int n,int diff){ + int sum=0; + + for(int i=0;i + { + int x; + int y; + + pair(int x, int y) { + this.x = x; + this.y = y; + } + public int compareTo(pair o) + { + return (int)(y-o.y); + + } +}",1,Plagiarised +48c5f745,4d25818e," +import java.io.*; +import java.util.*; +public class E { + + + public static void main(String[] args) throws Exception { + // TODO Auto-generated method stub + Reader sc=new Reader(); + PrintWriter pw=new PrintWriter(System.out); + int t=sc.nextInt(); + while(t-->0) { + int n=sc.nextInt(); + int k=sc.nextInt(); + int[] idxes=new int[k]; + int[] temps=new int[k]; + for(int i=0;i=0;i--) { + ans[i]=Math.min(ans[i], ans[i+1]+1); + } + for(int i=0;i= '0' && c <= '9'); + + if (neg) + return -ret; + return ret; + } + + public long nextLong() throws IOException + { + long ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + if (neg) + return -ret; + return ret; + } + + public double nextDouble() throws IOException + { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') { + while ((c = read()) >= '0' && c <= '9') { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) + return -ret; + return ret; + } + + private void fillBuffer() throws IOException + { + bytesRead = din.read(buffer, bufferPointer = 0, + BUFFER_SIZE); + if (bytesRead == -1) + buffer[0] = -1; + } + + private byte read() throws IOException + { + if (bufferPointer == bytesRead) + fillBuffer(); + return buffer[bufferPointer++]; + } + + public void close() throws IOException + { + if (din == null) + return; + din.close(); + } + } + +} + +","import java.util.*; +public class Sol +{ + 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 ac[] = new int[k]; + int temp[] = new int[k]; + int ans[] = new int[n+1]; + for(int i=0;i0;i--) + ans[i]=Math.min(ans[i],ans[i+1]+1); + + for(int i=1;i 0) { + int n = in.nextInt(); + long a[] = new long[n]; + for (int i = 0; i < n; i++) { + a[i] = in.nextLong(); + } + long result = Long.MAX_VALUE; + for (int i = 0; i < 2; i++) { + long x = 0; + long y = 0; + long minX = Long.MAX_VALUE; + long minY = Long.MAX_VALUE; + long prefix = 0; + for (int j = 0; j < n; j++) { + if (j % 2 == i) { + if (j > 0) { + result = Math.min(result, (n - x) * a[j] + (n - y) * minY + prefix); + } + ++x; + prefix += a[j]; + minX = Math.min(minX, a[j]); + } else { + if (j > 0) { + result = Math.min(result, (n - y) * a[j] + (n - x) * minX + prefix); + } + ++y; + prefix += a[j]; + minY = Math.min(minY, a[j]); + } + } + } + out.println(result); + } + + out.close(); + } + + static void shuffle(int[] a) { + Random get = new Random(); + for (int i = 0; i < a.length; i++) { + int r = get.nextInt(a.length); + int temp = a[i]; + a[i] = a[r]; + a[r] = temp; + } + } + + static void shuffle(long[] a) { + Random get = new Random(); + for (int i = 0; i < a.length; i++) { + int r = get.nextInt(a.length); + long temp = a[i]; + a[i] = a[r]; + a[r] = temp; + } + } + + static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + public FastReader(String s) throws FileNotFoundException { + br = new BufferedReader(new FileReader(new File(s))); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } + + static class Tuple implements Comparable { + int a; + int b; + int c; + public Tuple(int a, int b) { + this.a = a; + this.b = b; + this.c = 0; + } + public Tuple(int a, int b, int c) { + this.a = a; + this.b = b; + this.c = c; + } + public int getA() { return a; } + public int getB() { return b; } + public int getC() { return c; } + public int compareTo(Tuple other) { + if (this.a == other.a) { + if (this.b == other.b) return Long.compare(this.c, other.c); + return Long.compare(this.b, other.b); + } + return Long.compare(this.a, other.a); + } + @Override + public int hashCode() { return Arrays.deepHashCode(new Integer[] {a, b, c}); } + @Override + public boolean equals(Object o) { + if (!(o instanceof Tuple)) return false; + Tuple pairo = (Tuple) o; + return (this.a == pairo.a && this.b == pairo.b && this.c == pairo.c); + } + @Override + public String toString() { return String.format(""(%d %d %d) "", this.a, this.b, this.c); } + } +} +",0,Non-plagiarised +3e93b259,a60fba84,"import java.util.*; +import java.io.*; +public class Main +{ + static class FastReader + { + BufferedReader br; + StringTokenizer st; + + public FastReader() + { + br = new BufferedReader(new + InputStreamReader(System.in)); + } + + String next() + { + while (st == null || !st.hasMoreElements()) + { + try + { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() + { + return Integer.parseInt(next()); + } + + long nextLong() + { + return Long.parseLong(next()); + } + + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str = """"; + try + { + str = br.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + return str; + } + } + static class Pair implements Comparable + { + int f,s; + Pair(int f,int s) + { + this.f=f; + this.s=s; + } + public int compareTo(Pair p) + { + return this.f-p.f; + } + } + public static void main(String args[]) + { + FastReader fs=new FastReader(); + PrintWriter pw=new PrintWriter(System.out); + int tc=fs.nextInt(); + while(tc-->0) + { + int n=fs.nextInt(); + long a[]=new long[n]; + for(int i=0;i0){ + int n = ni(); + long arr[] = new long[n]; + lIA(arr); + long ans = (long)(n*(arr[0] + arr[1])); + long sum = arr[0] + arr[1]; + long emin = arr[0], omin = arr[1]; + + for(int i=2; i 0) { + solve(); + } + out.flush(); + out.close(); + } + + static boolean flag = false; + static List[] ans; + public static void dfs(int index, boolean[] visited, int parent, int val, List[] ll){ + if(visited[index]){ + return; + } + + List l1 = ll[index]; + // out.println("" == "" + l1 + "" "" + index); + if(l1.size() > 2){ + flag = true; + return; + } + + visited[index] = true; + + for(int k: l1){ + if(visited[k] == false){ + // out.println(visited[k] + "" "" + index + "" "" + k + "" "" + val); + ans[index].add(new int[]{k, val}); + ans[k].add(new int[]{index, val}); + dfs(k, visited, index, (val == 2) ? 5 : 2, ll); + } + } + } + public static void solve() { + int n = sc.nextInt(); + List[] ll = new List[n + 1]; + + // out.println("" mm "" + n); + for (int i = 0; i <= n; i++) { + ll[i] = new ArrayList<>(); + } + int[][] store = new int[n][2]; + for (int i = 0; i < n - 1; i++) { + int u = sc.nextInt(); + int v = sc.nextInt(); + store[i][0] = u; + store[i][1] = v; + // out.println("" === "" + u + "" "" + v); + int mm = Math.min(u, v); + int mx = Math.max(u, v); + // out.println("" u "" + v + "" "" + u); + ll[mm].add(mx); + ll[mx].add(mm); + // ll[v].add(u) + } + + int two = 2; + int prime = 5; + flag = false; + ans = new List[n+1]; + + int[] arr = new int[n + 1]; + + + for(int i = 0; i <= n; i++){ + ans[i] = new ArrayList<>(); + } + + boolean[] visited = new boolean[n+1]; + List one = ll[1]; + if(one.size() > 2){ + out.println(-1); + return; + } + + // out.println("" -- "" + one); + visited[1] = true; + for(int i = 0; i < one.size(); i++){ + if(i == 0){ + ans[1].add(new int[]{one.get(i), 2}); + ans[one.get(i)].add(new int[]{1, 2}); + dfs(one.get(i), visited, -1, 5, ll); + }else{ + ans[1].add(new int[]{one.get(i), 5}); + ans[one.get(i)].add(new int[]{1, 5}); + dfs(one.get(i), visited, -1, 2, ll); + } + } + if (flag) { + out.println(-1); + return; + } + + for(int i = 0; i < n-1; i++){ + // out.println("" -- ""); + int u = store[i][0]; + int v = store[i][1]; + + // out.println("" uu "" + u + "" "" + v); + if(u == 0 || v == 0){ + continue; + } + int mm = Math.min(u, v); + int mx = Math.max(u, v); + List vv = ans[mm]; + // for(int[] ii:vv){ + // out.println(Arrays.toString(ii)); + // } + if(vv == null){ + continue; + } + for(int[] j: vv){ + if(j[0] == mx){ + out.print(j[1]+ "" ""); + break; + } + } + } + out.println(); + } + + public static long gcd(long a, long b) { + while (b != 0) { + long rem = a % b; + a = b; + b = rem; + } + return a; + } + + public static long leftShift(long a) { + return (long) Math.pow(2, a); + } + + public static void reverse(int[] arr) { + Arrays.sort(arr); + int n = arr.length; + for (int i = 0; i < arr.length / 2; i++) { + int temp = arr[i]; + arr[i] = arr[n - 1 - i]; + arr[n - 1 - i] = temp; + } + return; + } + + public static int lower_bound(ArrayList ar, int k) { + int s = 0, e = ar.size(); + while (s != e) { + int mid = s + e >> 1; + if (ar.get(mid) <= k) { + s = mid + 1; + } else { + e = mid; + } + } + return Math.abs(s) - 1; + } + + public static int upper_bound(ArrayList ar, int k) { + int s = 0; + int e = ar.size(); + while (s != e) { + int mid = s + e >> 1; + if (ar.get(mid) < k) { + s = mid + 1; + } else { + e = mid; + } + } + if (s == ar.size()) { + return -1; + } + return s; + } + + static class Kioken { + // FileInputStream br = new FileInputStream(""input.txt""); + + BufferedReader br; + StringTokenizer st; + + Kioken(String filename) { + try { + FileReader fr = new FileReader(filename); + br = new BufferedReader(fr); + st = new StringTokenizer(""""); + + } catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + } + } + + Kioken() { + try { + br = new BufferedReader(new InputStreamReader(System.in)); + st = new StringTokenizer(""""); + + } catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + } + } + + public String next() { + while (!st.hasMoreTokens()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (Exception e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + public int nextInt() { + return Integer.parseInt(next()); + } + + public long nextLong() { + return Long.parseLong(next()); + } + + public double nextDouble() { + return Double.parseDouble(next()); + } + + public String nextLine() { + try { + return br.readLine(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public boolean hasNext() { + String next = null; + try { + next = br.readLine(); + } catch (Exception e) { + } + if (next == null || next.length() == 0) { + return false; + } + st = new StringTokenizer(next); + return true; + } + } +}","/* package codechef; // don't place package name! */ + +import java.util.*; +import java.lang.*; +import java.io.*; +/* Name of the class has to be ""Main"" only if the class is public. */ +public class Codechef +{static class FastReader + { + BufferedReader br; + StringTokenizer st; + + public FastReader() + { + br = new BufferedReader(new + InputStreamReader(System.in)); + } + + String next() + { + while (st == null || !st.hasMoreElements()) + { + try + { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() + { + return Integer.parseInt(next()); + } + + long nextLong() + { + return Long.parseLong(next()); + } + + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str = """"; + try + { + str = br.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + return str; + } + } +// static int mod = 998244353 ; +// static int N = 200005; +// static long factorial_num_inv[] = new long[N+1]; +// static long natual_num_inv[] = new long[N+1]; +// static long fact[] = new long[N+1]; +// static void InverseofNumber() +//{ +// natual_num_inv[0] = 1; +// natual_num_inv[1] = 1; +// for (int i = 2; i <= N; i++) +// natual_num_inv[i] = natual_num_inv[mod % i] * (mod - mod / i) % mod; +//} +//static void InverseofFactorial() +//{ +// factorial_num_inv[0] = factorial_num_inv[1] = 1; + +// for (int i = 2; i <= N; i++) +// factorial_num_inv[i] = (natual_num_inv[i] * factorial_num_inv[i - 1]) % mod; +//} +//static long nCrModP(long N, long R) +//{ +// long ans = ((fact[(int)N] * factorial_num_inv[(int)R]) % mod * factorial_num_inv[(int)(N - R)]) % mod; +// return ans%mod; +//} + //static boolean prime[]; + //static void sieveOfEratosthenes(int n) + //{ + // prime = new boolean[n+1]; + + // for (int i = 0; i <= n; i++) + // prime[i] = true; + + // for (int p = 2; p * p <= n; p++) + // { + // // If prime[p] is not changed, then it is a + // // prime + // if (prime[p] == true) + // { + // // Update all multiples of p + // for (int i = p * p; i <= n; i += p) + // prime[i] = false; + // } + // } + + + //} + static int visited[]; + static HashMap hm; + public static void main (String[] args) throws java.lang.Exception + { + // InverseofNumber(); + // InverseofFactorial(); + // fact[0] = 1; + // for (long i = 1; i <= 2*100000; i++) + // { + // fact[(int)i] = (fact[(int)i - 1] * i) % mod; + // } + + FastReader scan = new FastReader(); + PrintWriter pw = new PrintWriter(System.out); + int t = scan.nextInt(); + while(t-->0){ + int n = scan.nextInt(); + List> a = new ArrayList>(); + for(int i=0;i<=n;i++){ + a.add(new ArrayList()); + } + Pair edge[] = new Pair[n-1]; + for(int i=0;i2) + flag = 1; + if(a.get(i).size()==1) + start = i; + } + if(flag==1) + pw.println(-1); + else{ + visited = new int[n+1]; + hm = new HashMap(); + dfs(a,start,2); + for(int i=0;i> a,int start,int parent){ + if(visited[start]==0){ + visited[start] = 1; + List temp = a.get(start); + int len = temp.size(); + for(int i=0;i{ +// public int compare(Pair p1,Pair p2){ +// if(p1.x==p2.x) +// return 0; +// else if(p1.x n) ? 0 : mul(f[n], mul(rf[n-k], rf[k])); + } + + public static int pow(int a, int n) { + int res = 1; + while (n != 0) { + if ((n & 1) == 1) { + res = mul(res, a); + } + a = mul(a, a); + n >>= 1; + } + return res; + } + + static void shuffleArray(int[] a) { + Random rnd = new Random(); + for (int i = a.length-1; i > 0; i--) { + int index = rnd.nextInt(i + 1); + int tmp = a[index]; + a[index] = a[i]; + a[i] = tmp; + } + } + + public static int inv(int a) { + return pow(a, MOD-2); + } + + public void doIt() throws IOException { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + + StringTokenizer tok = new StringTokenizer(in.readLine()); + int n = Integer.parseInt(tok.nextToken()); + int k = Integer.parseInt(tok.nextToken()); + + f = new int[n+42]; + rf = new int[n+42]; + f[0] = rf[0] = 1; + for (int i = 1; i < f.length; ++i) { + f[i] = mul(f[i-1], i); + rf[i] = mul(rf[i-1], inv(i)); + } + + int[] events = new int[2*n]; + for (int i = 0; i < n; ++i) { + tok = new StringTokenizer(in.readLine()); + int le = Integer.parseInt(tok.nextToken()); + int ri = Integer.parseInt(tok.nextToken()); + events[i] = le*2; + events[i + n] = ri*2 + 1; + } + shuffleArray(events); + Arrays.sort(events); + + int ans = 0; + int balance = 0; + for (int r = 0; r < 2*n;) { + int l = r; + while (r < 2*n && events[l] == events[r]) { + ++r; + } + int added = r - l; + if (events[l] % 2 == 0) { + // Open event + ans += C(balance + added, k); + if (ans >= MOD) ans -= MOD; + ans += MOD - C(balance, k); + if (ans >= MOD) ans -= MOD; + balance += added; + } else { + // Close event + balance -= added; + } + } + + in.close(); + System.out.println(ans); + } + + public static void main(String[] args) throws IOException { + (new D_Java()).doIt(); + } +}","import java.io.*; +import java.util.*; + +public class D_Java { + public static final int MOD = 998244353; + + public static int mul(int a, int b) { + return (int)((long)a * (long)b % MOD); + } + + int[] f; + int[] rf; + + public int C(int n, int k) { + return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n-k], rf[k])); + } + + public static int pow(int a, int n) { + int res = 1; + while (n != 0) { + if ((n & 1) == 1) { + res = mul(res, a); + } + a = mul(a, a); + n >>= 1; + } + return res; + } + + static void shuffleArray(int[] a) { + Random rnd = new Random(); + for (int i = a.length-1; i > 0; i--) { + int index = rnd.nextInt(i + 1); + int tmp = a[index]; + a[index] = a[i]; + a[i] = tmp; + } + } + + public static int inv(int a) { + return pow(a, MOD-2); + } + + public void doIt() throws IOException { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + + StringTokenizer tok = new StringTokenizer(in.readLine()); + int n = Integer.parseInt(tok.nextToken()); + int k = Integer.parseInt(tok.nextToken()); + + f = new int[n+42]; + rf = new int[n+42]; + f[0] = rf[0] = 1; + for (int i = 1; i < f.length; ++i) { + f[i] = mul(f[i-1], i); + rf[i] = mul(rf[i-1], inv(i)); + } + + int[] events = new int[2*n]; + for (int i = 0; i < n; ++i) { + tok = new StringTokenizer(in.readLine()); + int le = Integer.parseInt(tok.nextToken()); + int ri = Integer.parseInt(tok.nextToken()); + events[i] = le*2; + events[i + n] = ri*2 + 1; + } + shuffleArray(events); + Arrays.sort(events); + + int ans = 0; + int balance = 0; + for (int r = 0; r < 2*n;) { + int l = r; + while (r < 2*n && events[l] == events[r]) { + ++r; + } + int added = r - l; + if (events[l] % 2 == 0) { + // Open event + ans += C(balance + added, k); + if (ans >= MOD) ans -= MOD; + ans += MOD - C(balance, k); + if (ans >= MOD) ans -= MOD; + balance += added; + } else { + // Close event + balance -= added; + } + } + + in.close(); + System.out.println(ans); + } + + public static void main(String[] args) throws IOException { + (new D_Java()).doIt(); + } +} +",1,Plagiarised +6946f466,ce7027ee,"import java.util.*; + +public class A { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + StringBuilder sb = new StringBuilder(); + int testCases = sc.nextInt(); + + for (int t = 0; t < testCases; t++) { + int n = sc.nextInt(); + int m = sc.nextInt(); + + char c[][] = new char[n][m]; + + for (int i = 0; i < n; i++) { + String str = sc.next(); + c[i] = str.toCharArray(); + } + + int count = 0; + StringBuilder ans = new StringBuilder(); + + for (int i = 0; i < m - 2; i++) { + for (int j = 0; j < n; j++) { + + if (c[j][i] == '0') { + continue; + } + count++; + + if (j == n - 1) { + + c[j][i] = '0'; + c[j][i + 1] = (c[j][i + 1] == '0' ? '1' : '0'); + c[j - 1][i + 1] = (c[j - 1][i + 1] == '0' ? '1' : '0'); + + ans.append((j + 1) + "" "" + (i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j) + "" "" + (i + 2)) + .append(""\n""); + continue; + } + + c[j][i] = '0'; + c[j][i + 1] = (c[j][i + 1] == '0' ? '1' : '0'); + c[j + 1][i + 1] = (c[j + 1][i + 1] == '0' ? '1' : '0'); + + ans.append((j + 1) + "" "" + (i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 2) + "" "" + (i + 2)) + .append(""\n""); + + } + } + + for (int i = 0; i < n - 2; i++) { + for (int j = m - 2; j < m; j++) { + if (c[i][j] == '0') { + continue; + } + count++; + + if (j == m - 1) { + c[i][j] = '0'; + c[i + 1][j] = (c[i + 1][j] == '0' ? '1' : '0'); + c[i + 1][j - 1] = (c[i + 1][j - 1] == '0' ? '1' : '0'); + + ans.append((i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j)) + .append(""\n""); + + continue; + } + + c[i][j] = '0'; + c[i + 1][j] = (c[i + 1][j] == '0' ? '1' : '0'); + c[i + 1][j + 1] = (c[i + 1][j + 1] == '0' ? '1' : '0'); + + ans.append((i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 2)) + .append(""\n""); + } + } + + ArrayList al = new ArrayList<>(); + + int count_one = 0; + + for (int i = n - 2; i < n; i++) { + for (int j = m - 2; j < m; j++) { + + if (c[i][j] == '0') { + al.add((i + 1) + "" "" + (j + 1)); + } else { + count_one++; + al.add(0, (i + 1) + "" "" + (j + 1)); + } + + } + } + + if (count_one == 0) { + sb.append(count).append(""\n""); + sb.append(ans).append(""\n""); + continue; + } + + if (count_one == 1) { + ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(2)).append(""\n""); + ans.append(al.get(0) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); + ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(3)).append(""\n""); + + count = count + 3; + + sb.append(count).append(""\n""); + sb.append(ans).append(""\n""); + continue; + } + + if (count_one == 2) { + ans.append(al.get(0) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); + ans.append(al.get(1) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); + + count = count + 2; + sb.append(count).append(""\n""); + sb.append(ans).append(""\n""); + continue; + } + + if (count_one == 3) { + ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(2)).append(""\n""); + + count = count + 1; + sb.append(count).append(""\n""); + sb.append(ans).append(""\n""); + continue; + } + + if (count_one == 4) { + ans.append(al.get(1) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); + + ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(2)).append(""\n""); + ans.append(al.get(0) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); + ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(3)).append(""\n""); + + count = count + 4; + + sb.append(count).append(""\n""); + sb.append(ans).append(""\n""); + continue; + + } + + } + + System.out.print(sb); + } +} +","import java.util.*; + +public class A { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + StringBuilder sb = new StringBuilder(); + int testCases = sc.nextInt(); + + for (int t = 0; t < testCases; t++) { + int n = sc.nextInt(); + int m = sc.nextInt(); + + char c[][] = new char[n][m]; + + for (int i = 0; i < n; i++) { + String str = sc.next(); + c[i] = str.toCharArray(); + } + + int count = 0; + StringBuilder ans = new StringBuilder(); + + for (int i = 0; i < m - 2; i++) { + for (int j = 0; j < n; j++) { + + if (c[j][i] == '0') { + continue; + } + count++; + + if (j == n - 1) { + + c[j][i] = '0'; + c[j][i + 1] = (c[j][i + 1] == '0' ? '1' : '0'); + c[j - 1][i + 1] = (c[j - 1][i + 1] == '0' ? '1' : '0'); + + ans.append((j + 1) + "" "" + (i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j) + "" "" + (i + 2)) + .append(""\n""); + continue; + } + + c[j][i] = '0'; + c[j][i + 1] = (c[j][i + 1] == '0' ? '1' : '0'); + c[j + 1][i + 1] = (c[j + 1][i + 1] == '0' ? '1' : '0'); + + ans.append((j + 1) + "" "" + (i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 2) + "" "" + (i + 2)) + .append(""\n""); + + } + } + + for (int i = 0; i < n - 2; i++) { + for (int j = m - 2; j < m; j++) { + if (c[i][j] == '0') { + continue; + } + count++; + + if (j == m - 1) { + c[i][j] = '0'; + c[i + 1][j] = (c[i + 1][j] == '0' ? '1' : '0'); + c[i + 1][j - 1] = (c[i + 1][j - 1] == '0' ? '1' : '0'); + + ans.append((i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j)) + .append(""\n""); + + continue; + } + + c[i][j] = '0'; + c[i + 1][j] = (c[i + 1][j] == '0' ? '1' : '0'); + c[i + 1][j + 1] = (c[i + 1][j + 1] == '0' ? '1' : '0'); + + ans.append((i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 2)) + .append(""\n""); + } + } + + ArrayList al = new ArrayList<>(); + + int count_one = 0; + + for (int i = n - 2; i < n; i++) { + for (int j = m - 2; j < m; j++) { + + if (c[i][j] == '0') { + al.add((i + 1) + "" "" + (j + 1)); + } else { + count_one++; + al.add(0, (i + 1) + "" "" + (j + 1)); + } + + } + } + + if (count_one == 0) { + sb.append(count).append(""\n""); + sb.append(ans).append(""\n""); + continue; + } + + if (count_one == 1) { + ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(2)).append(""\n""); + ans.append(al.get(0) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); + ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(3)).append(""\n""); + + count = count + 3; + + sb.append(count).append(""\n""); + sb.append(ans).append(""\n""); + continue; + } + + if (count_one == 2) { + ans.append(al.get(0) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); + ans.append(al.get(1) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); + + count = count + 2; + sb.append(count).append(""\n""); + sb.append(ans).append(""\n""); + continue; + } + + if (count_one == 3) { + ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(2)).append(""\n""); + + count = count + 1; + sb.append(count).append(""\n""); + sb.append(ans).append(""\n""); + continue; + } + + if (count_one == 4) { + ans.append(al.get(1) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); + + ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(2)).append(""\n""); + ans.append(al.get(0) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); + ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(3)).append(""\n""); + + count = count + 4; + + sb.append(count).append(""\n""); + sb.append(ans).append(""\n""); + continue; + + } + + } + + System.out.print(sb); + } +// +} +",1,Plagiarised +3cf63146,bdfe8110,"import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.time.LocalDateTime; +import java.util.*; + +public class B { + static int tree[]; + static int up[]; + static int n; + static int mod =1000000007; + public static void main(String[] args) throws Exception { + PrintWriter out=new PrintWriter(System.out); + FastScanner fs=new FastScanner(); + int t=fs.nextInt(); + outer: while(t-->0) { + n=fs.nextInt(); + int k=fs.nextInt(); + int ac[]=fs.readArray(k); + int temp[]=fs.readArray(k); + int arr[]=new int[n]; + Arrays.fill(arr, Integer.MAX_VALUE/2); + for(int i=0;i=0;i--) { + ans[i]=Math.min(Math.min(arr[i],left[i]),ans[i+1]+1); + } +// for(int i=0;in) return 0; + long res=1; + res*=fact(n); + res%=mod; + res*=modInv(fact(k)); + res%=mod; + res*=modInv(fact(n-k)); + res%=mod; + return res; + } + static long fact(long n) { + long res=1; + for(int i=2;i<=n;i++) { + res*=i; + res%=mod; + } + return res; + } + static long pow(long a,long b) { + long res=1; + while(b!=0) { + if((b&1)!=0) { + res*=a; + res%=mod; + } + a*=a; + a%=mod; + b=b>>1; + } + return res; + } + static long modInv(long n) { + return pow(n,mod-2); + } + + static void sort(int[] a) { + //suffle + int n=a.length; + Random r=new Random(); + for (int i=0; i=0; i--) + forced[i]=Math.min(forced[i], forced[i+1]+1); + for (int i=0; i l=new ArrayList<>(); + for (int i:a) l.add(i); + Collections.sort(l); + for (int i=0; i 0) { + solve(sc, pw); + } + pw.close(); + } + + public static void solve(FastScanner sc, PrintWriter pw) throws Exception { + int n = sc.nextInt(); + int[] arr = new int[n]; + for(int i=0;i S = new TreeSet<>(); + S.add(arr[0]); + for(int i=1;i0) { + int n = sc.nextInt(); + int b[] = sc.readArray(n); + TreeSet set = new TreeSet<>(); + boolean f = true; + set.add(b[0]); + for(int i=1 ; i 2) + count++; + + return count; + } + + static class Node + { + int val, i; + + public Node(int val, int i) { + this.val = val; + this.i = i; + } + } + public static void main(String[] args) { + StringBuilder ans = new StringBuilder(); + int t = ri(); +// int t=1; + while(t-- >0) + { + int n=ri(); + int[] arr=rai(n); + int[] res = new int[n]; + if(n%2==0) + { + + for(int i=0;i 0) { + int n = scn.nextInt(); + int[] arr = new int[n]; + int[] ans = new int[n]; + for (int i = 0; i < n; i++) { + arr[i] = scn.nextInt(); + } + if (n % 2 != 0) { + if (arr[n - 2] + arr[n - 3] == 0) { + ans[n - 3] = -2 * arr[n - 1]; + ans[n - 2] = -arr[n - 1]; + ans[n - 1] = arr[n - 2] + arr[n - 3] * 2; + } else { + ans[n - 3] = -arr[n - 1]; + ans[n - 2] = -arr[n - 1]; + ans[n - 1] = arr[n - 2] + arr[n - 3]; + } + n -= 3; + } + for (int i = 0; i < n; i += 2) { + int div = 1; + if (arr[i] % 2 == 0 && arr[i + 1] % 2 == 0) + div = 2; + ans[i] = arr[i + 1] / div; + ans[i + 1] = -arr[i] / div; + } + for (int v : ans) { + sb.append(v); + sb.append("" ""); + } + sb.append(""\n""); + } + System.out.println(sb); + } +} +",0,Non-plagiarised +0889dcfb,22138fad,"import java.io.*; +import java.util.*; +import static java.lang.Math.*; + +public class C{ + + static class Event implements Comparable{ + long i; + int index; + boolean start; + public Event(long a,int c, boolean b) { + i=a; + index = c; + start=b; + } + public int compareTo(Event o){ + if (o.i != i) return (int)(i-o.i); + if (start == o.start) return 0; + return start ? -1:1; + } + } + + public static void main(String[] args) throws IOException{ + // br = new BufferedReader(new FileReader("".in"")); + // out = new PrintWriter(new FileWriter("".out"")); + // new Thread(null, new (), ""fisa balls"", 1<<28).start(); + + int t= readInt(); + while(t-->0) { + int n =readInt(); + long[] k = new long[n], h = new long[n]; + for (int i = 0; i < n; i++) k[i]=readInt(); + for (int i = 0; i < n; i++) h[i]=readInt(); + List e = new ArrayList(); + for (int i = 0 ; i { + + int time; + int monsterNum; + boolean isMonster; + + public Event(int time, int monsterNum, boolean isMonster) { + this.time = time; this.monsterNum = monsterNum; this.isMonster = isMonster; + } + + @Override + public int compareTo(Event o) { + if(time == o.time) { + if(!(isMonster ^ o.isMonster)) { + return monsterNum - o.monsterNum; + } + if(o.isMonster) { + return -1; + } + return 1; + } + return time - o.time; + } + + } + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int t = Integer.parseInt(st.nextToken()); + + for(int test = 0; test < t; test++) { + + st = new StringTokenizer(br.readLine()); + + int n = Integer.parseInt(st.nextToken()); + + int[] k = new int[n]; + int[] h = new int[n]; + + st = new StringTokenizer(br.readLine()); + for(int i = 0; i < n; i++) + k[i] = Integer.parseInt(st.nextToken()); + + st = new StringTokenizer(br.readLine()); + for(int i = 0; i < n; i++) + h[i] = Integer.parseInt(st.nextToken()); + + int[] latestStart = new int[n]; + + for(int i = 0; i < n; i++) { + latestStart[i] = k[i] - h[i] + 1; + } + + + + PriorityQueue pq = new PriorityQueue(); + for(int i = 0; i < n; i++) { + pq.add(new Event(k[i],i,true)); + pq.add(new Event(latestStart[i],i,false)); + } + + long manaUsed = 0; + int lastEvent = 0; + + HashMap waiting = new HashMap(); + + long currentSpell = 1; + + //pw.println(""NEW GAME""); + + while(!pq.isEmpty()) { + + Event nextEvent = pq.poll(); + + if(!waiting.isEmpty() && nextEvent.time > lastEvent) { + manaUsed += rangeSum(currentSpell+1, currentSpell + nextEvent.time - lastEvent); + currentSpell = currentSpell + nextEvent.time - lastEvent; + } + if(waiting.isEmpty()) { + currentSpell = 1; + manaUsed++; + } + + //pw.println(manaUsed + "" "" + currentSpell + "" "" + nextEvent.monsterNum + "" "" + nextEvent.isMonster); + + if(nextEvent.isMonster) { + waiting.remove(nextEvent.monsterNum); + + } + + if(!nextEvent.isMonster) { + waiting.put(nextEvent.monsterNum,nextEvent); + } + + lastEvent = nextEvent.time; + + } + + pw.println(manaUsed); + + } + + pw.close(); + + } + +} +",0,Non-plagiarised +d5dc6626,f59d9b6e,"import java.util.*; +import java.io.*; + +public class Main { + // For fast input output + static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader() { + try { + br = new BufferedReader( + new FileReader(""input.txt"")); + PrintStream out = new PrintStream(new FileOutputStream(""output.txt"")); + System.setOut(out); + } catch (Exception e) { + br = new BufferedReader(new InputStreamReader(System.in)); + } + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } + + // end of fast i/o code + public static void main(String[] args) { + FastReader reader = new FastReader(); + StringBuilder sb = new StringBuilder(""""); + + int t = reader.nextInt(); + int ans = 0; + + while (t-- > 0) { + int n = reader.nextInt(); + int nodes[] = new int[n]; + int edges[][] = new int[n-1][2]; + ArrayList graph[] = new ArrayList[n]; + for(int i=0; i(); + } + for(int i=0; i2){ + possible = false; + } + } + Arrays.fill(nodes, 0); + int first = 2; + int second = 5; + + if(possible){ + int fill_ans[] = new int[n-1]; + find(0, -1, graph, fill_ans, n, first, second, true); + for(int i=0; i graph[], int fill_ans[], int n, int first, int second, boolean first_fill){ + + for(Pair p: graph[node]){ + if(p.a==par){ + continue; + } + if(first_fill){ + fill_ans[p.b] = first; + }else{ + fill_ans[p.b] = second; + } + find(p.a, node, graph, fill_ans, n, first, second, !first_fill); + first_fill = !first_fill; + } + + } + + static class Pair{ + int a, b; + Pair(int x, int y){ + a = x; + b = y; + } + } +}","/* package codechef; // don't place package name! */ + +import java.util.*; +import java.lang.*; +import java.io.*; + +/* Name of the class has to be ""Main"" only if the class is public. */ +public class Codechef{ + public static class Edge{ + int node; + int index; + Edge(int node, int index){ + this.node = node; + this.index = index; + } + } + static Scanner scn = new Scanner(System.in); + public static void main (String[] args) throws java.lang.Exception{ + int t = scn.nextInt(); + while(t-->0){ + solve(); + } + } + public static void solve(){ + int n = scn.nextInt(); + ArrayList[]graph = new ArrayList[n]; + for(int i = 0; i < n; i++){ + graph[i] = new ArrayList<>(); + } + for(int i = 0; i < n - 1; i++){ + int u = scn.nextInt() - 1; + int v = scn.nextInt() - 1; + graph[u].add(new Edge(v, i)); + graph[v].add(new Edge(u, i)); + } + int start = 0; + for(int i = 0; i < n; i++){ + if(graph[i].size() > 2){ + System.out.println(""-1""); + return; + }else if(graph[i].size() == 1){ + start = i; + } + } + int[]weight = new int[n - 1]; + int prevNode = -1, curNode = start, curWeight = 2; + while(true){ + ArrayListedges = graph[curNode]; + Edge next = edges.get(0); + if(next.node == prevNode){ + if(edges.size() == 1){ + break; + }else{ + next = edges.get(1); + } + } + weight[next.index] = curWeight; + prevNode = curNode; + curNode = next.node; + curWeight = 5 - curWeight; + } + for(int i = 0; i < n - 1; i++){ + System.out.print(weight[i]); + System.out.print("" ""); + } + System.out.println(); + } +} +",0,Non-plagiarised +04ed33a5,7ea34254,"import java.util.Scanner; + +public class Subsequence { + private static Scanner sc = new Scanner(System.in); + + public static void main(String args[]) { + int t = sc.nextInt(); + + while(t-->0) { + int n = sc.nextInt(); + + int a[] = new int[n]; + int b[] = new int[n]; + + for(int i=0;i0){ + int n= scan.nextInt(); + long a[] = new long[n]; + for(int i=0;i{ +// public int compare(Pair p1,Pair p2){ +// return p1.x-p2.x; +// } +//} +",0,Non-plagiarised +2.921E+060,4323e2bd,"import java.util.*; +import java.io.*; +import java.math.*; + +/** + * + * @Har_Har_Mahadev + */ + +/** + * Main , Solution , Remove Public + */ +public class A { + + private static long[][] dp; + private static ArrayList lis1,lis0; + + public static void process() throws IOException { + + int n = sc.nextInt(); + int arr[] = sc.readArray(n); + lis1 = new ArrayList(); + lis0 = new ArrayList(); + for(int i = 0; i 0) { + // google(TTT++); + process(); + } + out.flush(); + out.close(); + } + + static class Pair implements Comparable { + int x, y; + + Pair(int x, int y) { + this.x = x; + this.y = y; + } + + @Override + public int compareTo(Pair o) { + return Integer.compare(this.x, o.x); + } + + // @Override + // public boolean equals(Object o) { + // if (this == o) return true; + // if (!(o instanceof Pair)) return false; + // Pair key = (Pair) o; + // return x == key.x && y == key.y; + // } + // + // @Override + // public int hashCode() { + // int result = x; + // result = 31 * result + y; + // return result; + // } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////// + + static void println(Object o) { + out.println(o); + } + + static void println() { + out.println(); + } + + static void print(Object o) { + out.print(o); + } + + static void pflush(Object o) { + out.println(o); + out.flush(); + } + + static int ceil(int x, int y) { + return (x % y == 0 ? x / y : (x / y + 1)); + } + + static long ceil(long x, long y) { + return (x % y == 0 ? x / y : (x / y + 1)); + } + + static int max(int x, int y) { + return Math.max(x, y); + } + + static int min(int x, int y) { + return Math.min(x, y); + } + + static int abs(int x) { + return Math.abs(x); + } + + static long abs(long x) { + return Math.abs(x); + } + + static long sqrt(long z) { + long sqz = (long) Math.sqrt(z); + while (sqz * 1L * sqz < z) { + sqz++; + } + while (sqz * 1L * sqz > z) { + sqz--; + } + return sqz; + } + + static int log2(int N) { + int result = (int) (Math.log(N) / Math.log(2)); + return result; + } + + static long max(long x, long y) { + return Math.max(x, y); + } + + static long min(long x, long y) { + return Math.min(x, y); + } + + public static int gcd(int a, int b) { + BigInteger b1 = BigInteger.valueOf(a); + BigInteger b2 = BigInteger.valueOf(b); + BigInteger gcd = b1.gcd(b2); + return gcd.intValue(); + } + + public static long gcd(long a, long b) { + BigInteger b1 = BigInteger.valueOf(a); + BigInteger b2 = BigInteger.valueOf(b); + BigInteger gcd = b1.gcd(b2); + return gcd.longValue(); + } + + public static long lcm(long a, long b) { + return (a * b) / gcd(a, b); + } + + public static int lcm(int a, int b) { + return (a * b) / gcd(a, b); + } + + public static int lower_bound(int[] arr, int x) { + int low = 0, high = arr.length, mid = -1; + + while (low < high) { + mid = (low + high) / 2; + + if (arr[mid] >= x) + high = mid; + else + low = mid + 1; + } + + return low; + } + + public static int upper_bound(int[] arr, int x) { + int low = 0, high = arr.length, mid = -1; + + while (low < high) { + mid = (low + high) / 2; + + if (arr[mid] > x) + high = mid; + else + low = mid + 1; + } + + return low; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////// + + static class FastScanner { + BufferedReader br; + StringTokenizer st; + + FastScanner() throws FileNotFoundException { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + FastScanner(int a) throws FileNotFoundException { + br = new BufferedReader(new FileReader(""input.txt"")); + } + + String next() throws IOException { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() throws IOException { + return Integer.parseInt(next()); + } + + long nextLong() throws IOException { + return Long.parseLong(next()); + } + + double nextDouble() throws IOException { + return Double.parseDouble(next()); + } + + String nextLine() throws IOException { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + + int[] readArray(int n) throws IOException { + int[] A = new int[n]; + for (int i = 0; i != n; i++) { + A[i] = sc.nextInt(); + } + return A; + } + + long[] readArrayLong(int n) throws IOException { + long[] A = new long[n]; + for (int i = 0; i != n; i++) { + A[i] = sc.nextLong(); + } + return A; + } + } + + static void ruffleSort(int[] a) { + Random get = new Random(); + for (int i = 0; i < a.length; i++) { + int r = get.nextInt(a.length); + int temp = a[i]; + a[i] = a[r]; + a[r] = temp; + } + Arrays.sort(a); + } + + static void ruffleSort(long[] a) { + Random get = new Random(); + for (int i = 0; i < a.length; i++) { + int r = get.nextInt(a.length); + long temp = a[i]; + a[i] = a[r]; + a[r] = temp; + } + Arrays.sort(a); + } +} +","import java.io.*; +import java.util.*; +import java.math.*; +import java.math.BigInteger; +//import javafx.util.*; +public final class B +{ + static StringBuilder ans=new StringBuilder(); + static FastReader in=new FastReader(); + static ArrayList> g; + static long mod=(long)(1e9+7); + static int D1[],D2[],par[]; + static boolean set[]; + static int value[]; + static long INF=Long.MAX_VALUE; + static int dp[][]; + static int N,M; + static int A[][],B[][]; + static int s=1; + public static void main(String args[])throws IOException + { + int N=i(); + int A[]=input(N); + ArrayList one=new ArrayList(); + ArrayList zero=new ArrayList(); + for(int i=1; i<=N; i++) + { + if(A[i-1]==1)one.add(i); + else zero.add(i); + } + int sum[][]=new int[N+5][N+5]; + for(int i=1; i<=one.size(); i++) + { + for(int j=1; j<=zero.size(); j++) + { + sum[i][j]=Math.abs(one.get(i-1)-zero.get(j-1)); + } + //print(sum[i]); + } + dp=new int[N+5][N+5]; + //for(int d[]:dp)Arrays.fill(d, Integer.MAX_VALUE); + Arrays.fill(dp[0], 0); + for(int i=1; i<=one.size(); i++) + { + for(int j=i; j<=zero.size(); j++) + { + if(i==j) + { + dp[i][j]=dp[i-1][j-1]+sum[i][j]; + } + else + { + dp[i][j]=Math.min(dp[i][j-1], dp[i-1][j-1]+sum[i][j]); + } + } + } + System.out.println(dp[one.size()][zero.size()]); +// f(0,0,one,zero,0); + //for(int d[]:dp)print(d); + + } + static int f(int i,int j,ArrayList one,ArrayList zero, int s) + { + if(i==one.size())return s; + if(j==zero.size())return Integer.MAX_VALUE; + int a=one.get(i),b=zero.get(j); + if(dp[a][b]==-1) + { + int min=Integer.MAX_VALUE; + for(int t=j; tA[i+1])return false; + } + + return true; + } + static int f1(int x,ArrayList A) + { + int l=-1,r=A.size(); + while(r-l>1) + { + int m=(l+r)/2; + int a=A.get(m); + if(aend)return 0; + int a=0,b=0,c=0; + if(d>1)a=f(st+d-1,end,d-1); + b=f(st+d,end,d); + c=f(st+d+1,end,d+1); + return value[st]+max(a,b,c); + } + static int max(int a,int b,int c) + { + return Math.max(a, Math.max(c, b)); + } + static int value(char X[],char Y[]) + { + int c=0; + for(int i=0; i<7; i++) + { + if(Y[i]=='1' && X[i]=='0')return -1; + if(X[i]=='1' && Y[i]=='0')c++; + } + return c; + } + static boolean isValid(int i,int j) + { + if(i<0 || i>=N)return false; + if(j<0 || j>=M)return false; + return true; + } + + static long fact(long N) + { + long num=1L; + while(N>=1) + { + num=((num%mod)*(N%mod))%mod; + N--; + } + return num; + } + static boolean reverse(long A[],int l,int r) + { + while(l l=new ArrayList<>(); + for (long i:a) l.add(i); + Collections.sort(l); + for (int i=0; i>(); + for(int i=0; i<=N; i++) + { + g.add(new ArrayList()); + } + } + + static long pow(long a,long b) + { + long mod=1000000007; + long pow=1; + long x=a; + while(b!=0) + { + if((b&1)!=0)pow=(pow*x)%mod; + x=(x*x)%mod; + b/=2; + } + return pow; + } + static long toggleBits(long x)//one's complement || Toggle bits + { + int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; + + return ((1< l=new ArrayList<>(); + for (int i:a) l.add(i); + Collections.sort(l); + for (int i=0; i A) + { + for(int a:A)System.out.print(a+"" ""); + System.out.println(); + } + + //Debugging Functions END + //---------------------- + //IO FUNCTIONS STARTS + static HashMap Hash(int A[]) + { + HashMap mp=new HashMap<>(); + for(int a:A) + { + int f=mp.getOrDefault(a,0)+1; + mp.put(a, f); + } + return mp; + } + static int i() + { + return in.nextInt(); + } + + static long l() + { + return in.nextLong(); + } + + static int[] input(int N){ + int A[]=new int[N]; + for(int i=0; i +{ + int l,r,index; + node(int l,int r,int index) + { + this.l=l; + this.r=r; + this.index=index; + } + public int compareTo(node X) + { + return X.r-this.r; + } +} +class node1 implements Comparable +{ + int l,r,index; + node1(int l,int r,int index) + { + this.l=l; + this.r=r; + this.index=index; + } + public int compareTo(node1 X) + { + if(this.l==X.l) + return X.r-this.r; + return this.l-X.l; + } +} +//Code For FastReader +//Code For FastReader +//Code For FastReader +//Code For FastReader +class FastReader +{ + BufferedReader br; + StringTokenizer st; + public FastReader() + { + br=new BufferedReader(new InputStreamReader(System.in)); + } + + String next() + { + while(st==null || !st.hasMoreElements()) + { + try + { + st=new StringTokenizer(br.readLine()); + } + catch(IOException e) + { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() + { + return Integer.parseInt(next()); + } + + long nextLong() + { + return Long.parseLong(next()); + } + //gey + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str=""""; + try + { + str=br.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + return str; + } + +}",0,Non-plagiarised +2af19170,ac8ef97c,"import java.util.*; +import java.io.*; + +public class Solution{ + + static long abs(long a){return Math.abs(a);} + static long max(long a, long b){return Math.max(a, b);} + + static class Graph{ + + int l[], r[], v; + ArrayList adj[]; + Graph(int v){ + this.v = v; + this.l = new int[v]; + this.r = new int[v]; + adj = new ArrayList[v]; + for(int i = 0 ; i < v ; i++){ + adj[i] = new ArrayList<>(); + } + } + + void addEdge(int u, int v){ + this.adj[u].add(v); + this.adj[v].add(u); + } + + long[] dfs(int u, int parent){ + long left = 0, right = 0; + int n = adj[u].size(); + for (int i = 0 ; i < n; i ++ ) { + int child = adj[u].get(i); + if ( child == parent ) continue; + long val[] = dfs(child, u); + left += max(abs(l[child]- l[u])+val[0], abs(r[child] - l[u])+val[1]); + right += max(abs(l[child]- r[u])+val[0], abs(r[child] - r[u])+val[1]); + } + + return new long[]{left, right}; + } + } + + public static void main(String args[])throws IOException{ + BufferedReader br = new + BufferedReader(new InputStreamReader(System.in)); + + int tests = Integer.parseInt(br.readLine()); + while( tests --> 0 ){ + int n = Integer.parseInt(br.readLine()); + Graph g = new Graph(n); + for(int i = 0 ; i < n ; i++ ){ + String line[] = br.readLine().split("" ""); + g.l[i] = Integer.parseInt(line[0]); + g.r[i] = Integer.parseInt(line[1]); + } + + for(int i = 0 ; i < n - 1 ; i++ ){ + String line[] = br.readLine().split("" ""); + int u = Integer.parseInt(line[0]) - 1; + int v = Integer.parseInt(line[1]) - 1; + g.addEdge(u, v); + } + + long res[] = g.dfs(0, -1); + System.out.println(max(res[0], res[1])); + } + } +}","import java.io.*; +import java.util.*; + +public class Codeforces +{ + public static void main(String args[])throws Exception + { + BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); + StringBuilder sb=new StringBuilder(); + int t=Integer.parseInt(bu.readLine()); + while(t-->0) + { + int n=Integer.parseInt(bu.readLine()); + g=new ArrayList[n]; + int i; + for(i=0;i(); + String st[]=bu.readLine().split("" ""); + a[i][0]=Integer.parseInt(st[0]); a[i][1]=Integer.parseInt(st[1]); + s[i][0]=s[i][1]=0; + } + + for(i=0;i g[]; + static int N=100000,a[][]=new int[N][2]; + static long s[][]=new long[N][2]; + static void dfs(int n,int p) + { + for(int x:g[n]) + if(x!=p) + { + dfs(x,n); + s[n][0]+=Math.max(s[x][0]+Math.abs(a[x][0]-a[n][0]),s[x][1]+Math.abs(a[x][1]-a[n][0])); + s[n][1]+=Math.max(s[x][0]+Math.abs(a[x][0]-a[n][1]),s[x][1]+Math.abs(a[x][1]-a[n][1])); + } + } +} +",0,Non-plagiarised +3380fa52,489930000000,"import java.util.*; +import java.io.*; +import java.math.*; + +/* Name of the class has to be ""Main"" only if the class is public. */ +public class Coder { + static int n, k; + static long a[]; + static int pos[]; + static int temp[]; + static StringBuilder str = new StringBuilder(""""); + static int cnt[][] = new int[(int)1e5+5][2]; + static void solve() { + long []l = new long[n]; + long []r = new long[n]; + long p = Integer.MAX_VALUE; + for(int i=0;i=0;i--){ + p = Math.min(p+1, a[i]); + r[i] = p; + } + for(int i=0;i 0) { + bf.readLine(); + String s[] = bf.readLine().trim().split(""\\s+""); + n = Integer.parseInt(s[0]); + k = Integer.parseInt(s[1]); + s = bf.readLine().trim().split(""\\s+""); + pos = new int[k]; + temp = new int[k]; + a = new long[n]; + for(int i=0;i= '0' && c <= '9'); + + if (neg) + return -ret; + return ret; + } + + public long nextLong() throws IOException { + long ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + if (neg) + return -ret; + return ret; + } + + public double nextDouble() throws IOException { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') { + while ((c = read()) >= '0' && c <= '9') { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) + return -ret; + return ret; + } + + private void fillBuffer() throws IOException { + bytesRead = din.read(buffer, bufferPointer = 0, + BUFFER_SIZE); + if (bytesRead == -1) + buffer[0] = -1; + } + + private byte read() throws IOException { + if (bufferPointer == bytesRead) + fillBuffer(); + return buffer[bufferPointer++]; + } + + public void close() throws IOException { + if (din == null) + return; + din.close(); + } + } + + static int mod = (int) (1e9 + 7); + + public static class pair implements Comparator { + int x; + int y; + + public pair() { + + } + + public pair(int x, int y) { + this.x = x; + this.y = y; + + } + + @Override + public int compare(pair o1, pair o2) { + return o1.y - o2.y; + } + } + + public static long modularpow(long a, long b) { + long res = 1; + if (b == 0) + return res; + else { + while (b > 0) { + if (b % 2 == 1) { + res *= a; + res %= mod; + + } + a = a * a; + a %= mod; + b /= 2; + } + return res % mod; + } + } + + public static int gcd(int a, int b) { + if (b == 0) + return a; + else + return gcd(b, a % b); + } + + public static String binary(int a) { + String s1 = """"; + while (a > 0) { + s1 = a % 2 + s1; + a /= 2; + } + return s1; + } + + public static int Lower_Bound(long a[], int l, int r, long k) { + + + while (r - l > 1) { + int mid = l + (r - l) / 2; + if (a[mid] <= k) l = mid; + else + r = mid; + + } + return l; + } + + public static int Upper_Bound(int a[], int l, int r, int k) { + + while (r - l > 1) { + int mid = (l + r) / 2; + if (a[mid] <= k) + l = mid; + else + r = mid; + } + return l + 1; + } + + public static void main(String arg[]) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(br.readLine()); + StringBuilder sb = new StringBuilder(); + while (t-- > 0) { + br.readLine(); + String s[]=br.readLine().split("" ""); + int n=Integer.parseInt(s[0]); + int k=Integer.parseInt(s[1]); + long a[]=new long[n]; + long b[]=new long[n]; + long c[]=new long[n]; + Arrays.fill(c,Integer.MAX_VALUE); + String s1[]=br.readLine().split("" ""); + s=br.readLine().split("" ""); + for(int i=0;i=0;i--){ + p=Math.min(p+1,c[i]); + b[i]=p; + } + for(int i=0;i=0;i--) forced[i]=Math.min(forced[i], forced[i+1]+1); + for (int i=0;i=0;i--) + { + answer[i]=Math.min(answer[i],answer[i+1]+1); + } + for (int i=0;i0) + { + solve(); + out.println(); + } + out.close(); + } + static class InputReader { + + private InputStream stream; + private byte[] buf = new byte[8192]; + private int curChar, snumChars; + private SpaceCharFilter filter; + + public InputReader(InputStream stream) { + this.stream = stream; + } + + public int snext() { + if (snumChars == -1) + throw new InputMismatchException(); + if (curChar >= snumChars) { + curChar = 0; + try { + snumChars = stream.read(buf); + } catch (IOException e) { + throw new InputMismatchException(); + } + if (snumChars <= 0) + return -1; + } + return buf[curChar++]; + } + + public int nextInt() { + int c = snext(); + while (isSpaceChar(c)) + c = snext(); + int sgn = 1; + if (c == '-') { + sgn = -1; + c = snext(); + } + int res = 0; + do { + if (c < '0' || c > '9') + throw new InputMismatchException(); + res *= 10; + res += c - '0'; + c = snext(); + } while (!isSpaceChar(c)); + return res * sgn; + } + + public long nextLong() { + int c = snext(); + while (isSpaceChar(c)) + c = snext(); + int sgn = 1; + if (c == '-') { + sgn = -1; + c = snext(); + } + long res = 0; + do { + if (c < '0' || c > '9') + throw new InputMismatchException(); + res *= 10; + res += c - '0'; + c = snext(); + } while (!isSpaceChar(c)); + return res * sgn; + } + + public int[] nextIntArray(int n) { + int a[] = new int[n]; + for (int i = 0; i < n; i++) + a[i] = nextInt(); + return a; + } + + public String readString() { + int c = snext(); + while (isSpaceChar(c)) + c = snext(); + StringBuilder res = new StringBuilder(); + do { + res.appendCodePoint(c); + c = snext(); + } while (!isSpaceChar(c)); + return res.toString(); + } + + public boolean isSpaceChar(int c) { + if (filter != null) + return filter.isSpaceChar(c); + return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; + } + + public interface SpaceCharFilter { + public boolean isSpaceChar(int ch); + } + } +} +",1,Plagiarised +23194f89,bcc5473d,"import java.io.*; +import java.util.*; +public class new1{ + + static int gcd(int a, int b) + { + if (b == 0) + return a; + return gcd(b, a % b); + } + + + + public static void main(String[] args) throws IOException{ + + + BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); + FastReader s = new FastReader(); + int t = s.nextInt(); + for(int z = 0; z < t; z++) { + int n = s.nextInt(); + int[] ti = new int[n]; + for(int i = 0; i < n; i++) ti[i] = s.nextInt(); + int[] he = new int[n]; + for(int i = 0; i < n; i++) he[i] = s.nextInt(); + long ans = 0; + int st = ti[n - 1] - he[n - 1] + 1; + int en = ti[n - 1]; + //System.out.println(st + "" "" + en); + for(int i = n - 2; i >= 0; i--) { + int st1 = ti[i] - he[i] + 1; + int en1 = ti[i]; + if(en1 >= st) { + st = Math.min(st, st1); + } + else { + long a1 = ((en - st + 1L) * (en - st + 2L)) / 2; + ans = ans + a1; + st = st1; en = en1; + //System.out.println(a1); + } + + } + ans = ans + ((en - st + 1L) * (en - st + 2L)) / 2; + System.out.println(ans); + + + + + + + } + //output.flush(); + } +} + + + + class FastReader { + BufferedReader br; + StringTokenizer st; + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + public int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + }}"," +import java.util.*; +import java.lang.*; +import java.io.*; +import java.math.BigInteger; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.math.BigInteger; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; + +public class k +{ //public static int mod=1000000007; + public static void printDivisors(int n) + { + // Note that this loop runs till square root + int x=(int) Math.sqrt(n); + int p=0; + for (int i=1; i<=x; i++) + { + if (n%i == 0) + { + // If divisors are equal, print only one + if (n/i == i) + p++; + + else // Otherwise print both + p=p+2; + } + } + System.out.println(p); + } + public static ArrayList Factors(long n) + { + ArrayList arr=new ArrayList(); + int k=0; + while (n%2==0) + { + k++; + n /=2; + arr.add((long)2); + } + int p=(int) Math.sqrt(n); + for (int i = 3; i <=p; i+= 2) + + { if(n==1)break; + while (n%i == 0) + { + k++; + arr.add((long)i); + n /= i; + } + } + if (n > 2) + { + arr.add(n); + } + + return arr; + } + static class Reader { + + final private int BUFFER_SIZE = 1 << 16; + private DataInputStream din; + private byte[] buffer; + private int bufferPointer, bytesRead; + + public Reader() + { + din = new DataInputStream(System.in); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public Reader(String file_name) throws IOException + { + din = new DataInputStream( + new FileInputStream(file_name)); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public String readLine() throws IOException + { + byte[] buf = new byte[64]; // line length + int cnt = 0, c; + while ((c = read()) != -1) { + if (c == '\n') { + if (cnt != 0) { + break; + } + else { + continue; + } + } + buf[cnt++] = (byte)c; + } + return new String(buf, 0, cnt); + } + + public int nextInt() throws IOException + { + int ret = 0; + byte c = read(); + while (c <= ' ') { + c = read(); + } + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (neg) + return -ret; + return ret; + } + + public long nextLong() throws IOException + { + long ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + if (neg) + return -ret; + return ret; + } + + public double nextDouble() throws IOException + { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') { + while ((c = read()) >= '0' && c <= '9') { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) + return -ret; + return ret; + } + + private void fillBuffer() throws IOException + { + bytesRead = din.read(buffer, bufferPointer = 0, + BUFFER_SIZE); + if (bytesRead == -1) + buffer[0] = -1; + } + + private byte read() throws IOException + { + if (bufferPointer == bytesRead) + fillBuffer(); + return buffer[bufferPointer++]; + } + + public void close() throws IOException + { + if (din == null) + return; + din.close(); + } + } + public static long gcd(long x, long p) + { + if (x == 0) + return p; + + return gcd(p%x, x); + } + // method to return LCM of two numbers + static long lcm(long a, long b) + { + return (a / gcd(a, b)) * b; + } + public static HashMap sortByValue(HashMap hm) + { + // Create a list from elements of HashMap + List > list = + new LinkedList >(hm.entrySet()); + + // Sort the list + Collections.sort(list, new Comparator >() { + public int compare(Map.Entry o1, + Map.Entry o2) + { + return (o1.getValue()).compareTo(o2.getValue()); + } + }); + + // put data from sorted list to hashmap + HashMap temp = new LinkedHashMap(); + for (Map.Entry aa : list) { + temp.put(aa.getKey(), aa.getValue()); + } + return temp; + } + static int sieve = 1000000 ; + + + static boolean[] prime = new boolean[sieve + 1] ; + + public static void sieveOfEratosthenes() + { + // FALSE == prime and 1 // TRUE == COMPOSITE + + // time complexity = 0(NlogLogN)== o(N) + + // gives prime nos bw 1 to N // size - 1e7(at max) + + for(int i = 4; i<= sieve ; i++) + { + prime[i] = true ; i++ ; + + } + + for(int p = 3; p*p <= sieve; p++) + { + if(prime[p] == false) + { + for(int i = p*p; i <= sieve; i += p) + prime[i] = true; + } + + p++ ; + } + + } + public static void arrInpInt(int [] arr, int n) throws IOException + { + Reader reader = new Reader(); + for(int i=0;i high) { + return low; + } + int mid = low + (high - low) / 2; + if (arr[mid] >= X) { + return lower_bound(arr, low, + mid - 1, X); + } + + return lower_bound(arr, mid + 1, + high, X); + } + //if present - return the index of next greater value + //not present- return the index of next greater value + //if greater than all the values return N(taking high=N-1) + //if smaller than all the values return 0(taking low =0)\ + + static int upper_bound(int arr[], int low, int high, int X) +{ + +if (low > high) + return low; + +int mid = low + (high - low) / 2; +if (arr[mid] <= X) { + return upper_bound(arr, mid + 1, + high, X); +} + +return upper_bound(arr, low, + mid - 1, X); +} + + + public static class Pair {// comparator with class + int x; + int y; + public Pair(int x, int y) + { + this.x = x; + this.y = y; + } + } + + public static void sortbyColumn(int arr[][], int col) // send 2d array and col no + { + Arrays.sort(arr, new Comparator() { + @Override + public int compare(final int[] entry1, + final int[] entry2) { + if (entry1[col] > entry2[col]) + return 1; + else if (entry1[col] < entry2[col]) + return -1; + else return 0; + } + }); + } + public static void sortbyColumn1(int arr[][], int col) // send 2d array and col no + { + Arrays.sort(arr, new Comparator() { + @Override + public int compare(final int[] entry1, + final int[] entry2) { + if (entry1[col] > entry2[col]) + return 1; + else if (entry1[col] < entry2[col]) + return -1; + else if(entry1[col] == entry2[col]) + { + if(entry1[col-1]>entry2[col-1]) + return -1; + else if(entry1[col-1]{ + public int compare(p s1, p s2) { + if (s1.h > s2.h) + return -1; + else if (s1.h < s2.h) + return 1; + else if(s1.h==s2.h) + { + if(s1.no>s2.no)return -1; + else return 1; + } + return 0; + } + } + static long hcf(long a,long b) + { + while (b > 0) + { + long temp = b; + b = a % b; + a = temp; + } + return a; + } + + static int lower_bound_arr(ArrayList arr, int low, + int high, int X) + { + + if (low > high) { + return low; + } + int mid = low + (high - low) / 2; + if (arr.get(mid) >= X) { + return lower_bound_arr(arr, low, + mid - 1, X); + } + + return lower_bound_arr(arr, mid + 1, + high, X); + } + + + public static void main(String args[]) throws NumberFormatException, IOException ,java.lang.Exception + { + + Reader reader = new Reader(); + + + //sieveOfEratosthenes(); + //Scanner reader=new Scanner(System.in); + PrintWriter out = new PrintWriter(System.out); + BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); +// BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); +// int cases=Integer.parseInt(br.readLine()); +// int cases=1; + int cases=reader.nextInt(); + + while (cases-->0){ +// long N=reader.nextLong(); + //long M=reader.nextLong(); + int N=reader.nextInt(); +// int P=reader.nextInt(); + + +//// +// String[] first=br.readLine().split("" ""); +// long N=Long.parseLong(first[0]); +// long K=Long.parseLong(first[1]); +// long X=Long.parseLong(first[2]); +// String s2=br.readLine(); +// String s3=br.readLine(); +// char[] s11=s2.toCharArray(); +// char[] s12=new char[s11.length]; + + //int max=Integer.MIN_VALUE; +// int min=Integer.MAX_VALUE; + //int mod=1000000007; + +// HashMap> map=new HashMap>(); + + HashMap map=new HashMap(); +// HashMap map=new HashMap(); +// HashMap map1=new HashMap(); + //HashMap path=new HashMap(); + + //TreeMap map=new TreeMap(Collections.reverseOrder()); + +// HashSet set =new HashSet(); +// HashSet right =new HashSet(); + + // TreeSet a =new TreeSet(); + //TreeSet b =new TreeSet(); +// TreeSet map=new TreeSet(); + +// long[] arr=new long[N]; +// int[] odd=new int[100001]; + + int[] sec=new int[N]; + int[] pow=new int[N]; +// int[][] arr=new int[N][P]; + ArrayList list=new ArrayList(); +// ArrayList list3=new ArrayList(); +// ArrayList list1=new ArrayList(); +// ArrayList bees=new ArrayList(); + +// boolean[]arr1=new boolean[N]; +// +// for(int i=0;i=0) + { +// System.out.println(last +"" ""+ind); + if(sec[i]>=ind) + { + if(sec[i]-ind+1> graph = new ArrayList<>(); + for (int j = 0; j < n; j++) { + graph.add(new ArrayList<>()); + } + for (int j = 0; j < n - 1; j++) { + int u; + int v; + u = scan.nextInt(); + v = scan.nextInt(); + u--; + v--; + graph.get(u).add(new Pair(v, j)); + graph.get(v).add(new Pair(u, j)); + } + boolean soluble = true; + int curV = 0; + int prevV = -1; + int[] ans = new int[n]; + int prime = 2; + for (int j = 0; j < n; j++) { + ArrayList list = graph.get(j); + if (list.size() > 2) { + soluble = false; + } else if (list.size() == 1) { + curV = j; + } + } + if (soluble) { + for (int j = 0; j < n - 1; j++) { + ArrayList list = graph.get(curV); + for (int z = 0; z < list.size(); z++) { + if (list.get(z).vertex != prevV) { + ans[list.get(z).numberOfEdge] = prime; + prime = changePrime(prime); + prevV = curV; + curV = list.get(z).vertex; + break; + } + } + } + + for (int j = 0; j < n - 1; j++) { + System.out.print(ans[j] + "" ""); + } + System.out.println(); + } else { + System.out.println(-1); + } + } + } + public static int changePrime(int prime) { + if (prime == 2) { + prime = 3; + } else { + prime = 2; + } + return prime; + } +} + +class Pair { + int vertex; + int numberOfEdge; + + public Pair(int vertex, int numberOfEdge) { + this.vertex = vertex; + this.numberOfEdge = numberOfEdge; + } +} +","import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.StringTokenizer; + +public class NotAssigning { + + static class FastReader { + + BufferedReader br; + StringTokenizer st; + + public FastReader() { + + br = new BufferedReader(new InputStreamReader(System.in)); + + } + + String next() { + + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + + } + + int nextInt() { + + return Integer.parseInt(next()); + + } + + long nextLong() { + + return Long.parseLong(next()); + + } + + double nextFloat() { + + return Float.parseFloat(next()); + + } + + double nextDouble() { + + return Double.parseDouble(next()); + + } + + String nextLine() { + + String str = """"; + try { + str = br.readLine(); + } + catch (IOException e) { + e.printStackTrace(); + } + return str; + + } + + int[] nextArray(int n) { + + int[] a = new int[n]; + for (int i=0; i> t, int cur, boolean mode, int[] w) { + vis[cur] = true; + for (Pair p : t.get(cur)) { + if (!vis[p.a]) { + if (mode) { + w[p.b] = 3; + } + else { + w[p.b] = 2; + } + dfs(t, p.a, !mode, w); + } + } + } + + public static void solve(int n, int[] u, int[] v) { + + ArrayList> t = new ArrayList>(n); + for (int i=0; i()); + } + for (int i=0; i 2) { + System.out.println(""-1""); + return; + } + if (t.get(i).size() == 1) { + start = i; + } + } + vis = new boolean[n]; + int[] w = new int[n-1]; + dfs(t, start, false, w); + StringBuilder ans = new StringBuilder(); + for (int i=0; i 0) { + int n = in.nextInt(); + int[] u = new int[n-1]; + int[] v = new int[n-1]; + for (int i=0; i adj[]; + static HashMap> mp; + public static void insert(int i, int j, int v) { + int a = Math.min(i, j), b = Math.max(i, j); + if(!mp.containsKey(a)) mp.put(a, new HashMap<>()); + mp.get(a).put(b, v); + } + public static int get(int i, int j) { + int a = Math.min(i, j), b = Math.max(i, j); + return mp.get(a).get(b); + } + public static void dfs(int i, int p, boolean two) { + + for(int to : adj[i]) { + if(to == p) continue; + insert(i, to, two?2:3); + dfs(to,i,!two); + two = !two; + } + } + public static void solve() { + mp = new HashMap<>(); + int n = in.nextInt(); + adj = new ArrayList[n]; + for(int i = 0; i(); + int[][] e = new int[n-1][2]; + int[] deg = new int[n]; + for(int i = 0; i=3) { + out.println(-1); + return; + } + } + dfs(0,-1,true); + for(int i = 0; i0) solve(); + out.exit(); + } + static Reader in; static Writer out; + +static class Reader { + static BufferedReader br; + static StringTokenizer st; + + public Reader() { + this.br = new BufferedReader(new InputStreamReader(System.in)); + } + + public Reader(String f){ + try { + this.br = new BufferedReader(new FileReader(f)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public int[] na(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) a[i] = nextInt(); + return a; + } + + public double[] nd(int n) { + double[] a = new double[n]; + for (int i = 0; i < n; i++) a[i] = nextDouble(); + return a; + } + + public long[] nl(int n) { + long[] a = new long[n]; + for (int i = 0; i < n; i++) a[i] = nextLong(); + return a; + } + + public char[] nca() { + return next().toCharArray(); + } + + public String[] ns(int n) { + String[] a = new String[n]; + for (int i = 0; i < n; i++) a[i] = next(); + return a; + } + + public int nextInt() { + ensureNext(); + return Integer.parseInt(st.nextToken()); + } + + public double nextDouble() { + ensureNext(); + return Double.parseDouble(st.nextToken()); + } + + public Long nextLong() { + ensureNext(); + return Long.parseLong(st.nextToken()); + } + + public String next() { + ensureNext(); + return st.nextToken(); + } + + public String nextLine() { + try { + return br.readLine(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + private void ensureNext() { + if (st == null || !st.hasMoreTokens()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} + +static class Util{ + private static Random random = new Random(); + static long[] fact; + + public static void initFactorial(int n, long mod) { + fact = new long[n+1]; + fact[0] = 1; + for (int i = 1; i < n+1; i++) fact[i] = (fact[i - 1] * i) % mod; + } + + public static long modInverse(long a, long MOD) { + long[] gcdE = gcdExtended(a, MOD); + if (gcdE[0] != 1) return -1; // Inverted doesn't exist + long x = gcdE[1]; + return (x % MOD + MOD) % MOD; + } + + public static long[] gcdExtended(long p, long q) { + if (q == 0) return new long[] { p, 1, 0 }; + long[] vals = gcdExtended(q, p % q); + long tmp = vals[2]; + vals[2] = vals[1] - (p / q) * vals[2]; + vals[1] = tmp; + return vals; + } + + public static long nCr(int n, int r, long MOD) { + if (r == 0) return 1; + return (fact[n] * modInverse(fact[r], MOD) % MOD * modInverse(fact[n - r], MOD) % MOD) % MOD; + } + + public static long nCr(int n, int r) { + return (fact[n]/fact[r])/fact[n-r]; + } + + public static long nPr(int n, int r, long MOD) { + if (r == 0) return 1; + return (fact[n] * modInverse(fact[n - r], MOD) % MOD) % MOD; + } + public static long nPr(int n, int r) { + return fact[n]/fact[n-r]; + } + + public static boolean isPrime(int n) { + if (n <= 1) return false; + if (n <= 3) return true; + if (n % 2 == 0 || n % 3 == 0) return false; + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + return true; + } + + public static boolean[] getSieve(int n) { + boolean[] isPrime = new boolean[n+1]; + for (int i = 2; i <= n; i++) isPrime[i] = true; + for (int i = 2; i*i <= n; i++) if (isPrime[i]) + for (int j = i; i*j <= n; j++) isPrime[i*j] = false; + return isPrime; + } + + static long pow(long x, long pow, long mod){ + long res = 1; + x = x % mod; + if (x == 0) return 0; + while (pow > 0){ + if ((pow & 1) != 0) res = (res * x) % mod; + pow >>= 1; + x = (x * x) % mod; + } + return res; + } + + public static int gcd(int a, int b) { + int tmp = 0; + while(b != 0) { + tmp = b; + b = a%b; + a = tmp; + } + return a; + } + + public static long gcd(long a, long b) { + long tmp = 0; + while(b != 0) { + tmp = b; + b = a%b; + a = tmp; + } + return a; + } + + public static int random(int min, int max) { + return random.nextInt(max-min+1)+min; + } + + public static void dbg(Object... o) { + System.out.println(Arrays.deepToString(o)); + } + + public static void reverse(int[] s, int l , int r) { + for(int i = l; i<=(l+r)/2; i++) { + int tmp = s[i]; + s[i] = s[r+l-i]; + s[r+l-i] = tmp; + } + } + + public static void reverse(int[] s) { + reverse(s, 0, s.length-1); + } + + public static void reverse(long[] s, int l , int r) { + for(int i = l; i<=(l+r)/2; i++) { + long tmp = s[i]; + s[i] = s[r+l-i]; + s[r+l-i] = tmp; + } + } + + public static void reverse(long[] s) { + reverse(s, 0, s.length-1); + } + + public static void reverse(float[] s, int l , int r) { + for(int i = l; i<=(l+r)/2; i++) { + float tmp = s[i]; + s[i] = s[r+l-i]; + s[r+l-i] = tmp; + } + } + + public static void reverse(float[] s) { + reverse(s, 0, s.length-1); + } + + public static void reverse(double[] s, int l , int r) { + for(int i = l; i<=(l+r)/2; i++) { + double tmp = s[i]; + s[i] = s[r+l-i]; + s[r+l-i] = tmp; + } + } + + public static void reverse(double[] s) { + reverse(s, 0, s.length-1); + } + + public static void reverse(char[] s, int l , int r) { + for(int i = l; i<=(l+r)/2; i++) { + char tmp = s[i]; + s[i] = s[r+l-i]; + s[r+l-i] = tmp; + } + } + + public static void reverse(char[] s) { + reverse(s, 0, s.length-1); + } + + public static void reverse(T[] s, int l , int r) { + for(int i = l; i<=(l+r)/2; i++) { + T tmp = s[i]; + s[i] = s[r+l-i]; + s[r+l-i] = tmp; + } + } + + public static void reverse(T[] s) { + reverse(s, 0, s.length-1); + } + + public static void shuffle(int[] s) { + for (int i = 0; i < s.length; ++i) { + int j = random.nextInt(i + 1); + int t = s[i]; + s[i] = s[j]; + s[j] = t; + } + } + + public static void shuffle(long[] s) { + for (int i = 0; i < s.length; ++i) { + int j = random.nextInt(i + 1); + long t = s[i]; + s[i] = s[j]; + s[j] = t; + } + } + + public static void shuffle(float[] s) { + for (int i = 0; i < s.length; ++i) { + int j = random.nextInt(i + 1); + float t = s[i]; + s[i] = s[j]; + s[j] = t; + } + } + + public static void shuffle(double[] s) { + for (int i = 0; i < s.length; ++i) { + int j = random.nextInt(i + 1); + double t = s[i]; + s[i] = s[j]; + s[j] = t; + } + } + + public static void shuffle(char[] s) { + for (int i = 0; i < s.length; ++i) { + int j = random.nextInt(i + 1); + char t = s[i]; + s[i] = s[j]; + s[j] = t; + } + } + + public static void shuffle(T[] s) { + for (int i = 0; i < s.length; ++i) { + int j = random.nextInt(i + 1); + T t = s[i]; + s[i] = s[j]; + s[j] = t; + } + } + + public static void sortArray(int[] a) { + shuffle(a); + Arrays.sort(a); + } + + public static void sortArray(long[] a) { + shuffle(a); + Arrays.sort(a); + } + + public static void sortArray(float[] a) { + shuffle(a); + Arrays.sort(a); + } + + public static void sortArray(double[] a) { + shuffle(a); + Arrays.sort(a); + } + + public static void sortArray(char[] a) { + shuffle(a); + Arrays.sort(a); + } + + public static > void sortArray(T[] a) { + Arrays.sort(a); + } + + public static int[][] rotate90(int[][] a){ + int n = a.length, m = a[0].length; + int[][] ans = new int[m][n]; + for(int i = 0; i0){ + int n=sc.nextInt(); + + int degree[]=new int[n]; + + Arrays.fill(degree, 0); + int u[]=new int[n-1]; + int v[]=new int[n-1]; + boolean brea=false; + HashMap>graph=new HashMap<>(); + for(int i=0;i ll=graph.get(u[i]); + ll.add(i); + graph.put(u[i],ll); + } + else { + LinkedList ll=new LinkedList<>(); + ll.add(i); + graph.put(u[i],ll); + } + if(graph.containsKey(v[i])) { + LinkedList ll=graph.get(v[i]); + ll.add(i); + graph.put(v[i],ll); + } + else { + LinkedList ll=new LinkedList<>(); + ll.add(i); + graph.put(v[i],ll); + } + + + + degree[u[i]]++; + if(degree[u[i]]>2)brea=true; + degree[v[i]]++; + if(degree[v[i]]>2)brea=true; + } + + if(brea) { + System.out.println(-1); + continue; + } + + + + int i; + for( i=0;i1) { + + if(graph.get(i).get(0)==ind) { + ind=graph.get(i).get(1); + } + else { + ind=graph.get(i).get(0); + } + + if(flip) { + ans[ind]=3; + }else ans[ind]=2; + + flip=!flip; + + + if(u[ind]==i)i=v[ind]; + else i=u[ind]; + + } + + + + for(int j:ans) + System.out.print(j+"" ""); + System.out.println(); + + + + } + } + + + + + + + + + + +} +",0,Non-plagiarised +175201f9,40cc90ae,"import java.util.*; +import java.io.*; +public class Sol{ + +/* +->check n=1, int overflow , array bounds , all possibilites(dont stuck on 1 approach) +->Problem = Observation(constraints(m<=n/3 or k<=min(100,n)) + + Thinking + Technique (seg_tree,binary lift,rmq,bipart,dp,connected comp etc) +->solve or leave it (- tutorial improves you in minimal way -) +*/ + +public static void main (String []args) { +//precomp(); + +int times=ni();while(times-->0){solve();}out.close();} + +static void solve(){ + int n=ni(); + int sum[]=new int[5]; + PriorityQueue p[]=new PriorityQueue[5];for(int i=0;i<5;i++)p[i]=new PriorityQueue(); + for(int i=0;i0 && sum[i]<=0){cmax--;sum[i]-=p[i].poll();} + max=Math.max(max,cmax); + } + out.println(max); + return; + +} + +//-----------------Utility-------------------------------------------- + +static long gcd(long a,long b){if(b==0)return a; return gcd(b,a%b);} + +static int Max=Integer.MAX_VALUE; static long mod=1000000007; + +//static int v(char c){return (int)(c-'a');} + +public static long power(long x, long y ) + { + //0^0 = 1 + long res = 1L; + x = x%mod; + while(y > 0) + { + if((y&1)==1) + res = (res*x)%mod; + y >>= 1; + x = (x*x)%mod; + } + return res; + } + +static class Pair implements Comparable{ + int id;int value;Pair next; + public Pair(int id,int value) { + + this.id=id;this.value=value;next=null; + } + @Override + public int compareTo(Pair p){return Long.compare(value,p.value);} + } + + +//----------------------I/O--------------------------------------------- + +static InputStream inputStream = System.in; +static OutputStream outputStream = System.out; +static FastReader in=new FastReader(inputStream); +static PrintWriter out=new PrintWriter(outputStream); + +static class FastReader +{ + BufferedReader br; + StringTokenizer st; + + FastReader(InputStream is) { + br = new BufferedReader(new InputStreamReader(is)); + } + + public String next() + { + while (st == null || !st.hasMoreElements()) + { + try + { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + public int nextInt() + { + return Integer.parseInt(next()); + } + + public long nextLong() + { + return Long.parseLong(next()); + } + + public double nextDouble() + { + return Double.parseDouble(next()); + } + + + String nextLine() + { + String str = """"; + try + { + str = br.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + return str; + } +} +/*static int ni() { + try { + boolean in = false; + int res = 0; + for (;;) { + int b = System.in.read() - '0'; + if (b >= 0) { + in = true; + res = 10 * res + b; + } + else if (in) + return res; + } + } catch (IOException e) { + throw new Error(e); + } + }*/ +static int ni(){return in.nextInt();} +static long nl(){return in.nextLong();} +static double nd(){return in.nextDouble();} +static String ns(){return in.nextLine();} +}","import javax.swing.text.html.parser.Entity; +import java.io.*; +import java.util.*; +import java.math.BigInteger; + + +public class Main{ + public static void main(String[] args) { + InputStream inputStream = System.in; + OutputStream outputStream = System.out; + InputReader in = new InputReader(inputStream); + PrintWriter out = new PrintWriter(outputStream); + Task solver = new Task(); + solver.solve(in, out); + out.close(); + } + // main solver + static class Task{ + + double eps= 0.00000001; + static final int MAXN = 1010; + static final int MOD= 1000000007; + + // stores smallest prime factor for every number + static int spf[] = new int[MAXN]; + + static boolean[] prime; + + // Calculating SPF (Smallest Prime Factor) for every + // number till MAXN. + // Time Complexity : O(nloglogn) + public void sieve() + { + spf[1] = 1; + for (int i=2; i= x + public int lowerIndex(List arr, int n, int x) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr.get(mid) >= x) + h = mid - 1; + else + l = mid + 1; + } + return l; + } + public int lowerIndex(int[] arr, int n, int x) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr[mid] >= x) + h = mid - 1; + else + l = mid + 1; + } + return l; + } + + // function to find last index <= y + public int upperIndex(List arr, int n, int y) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr.get(mid) <= y) + l = mid + 1; + else + h = mid - 1; + } + return h; + } + public int upperIndex(int[] arr, int n, int y) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr[mid] <= y) + l = mid + 1; + else + h = mid - 1; + } + return h; + } + + // function to count elements within given range + public int countInRange(List arr, int n, int x, int y) + { + // initialize result + int count = 0; + count = upperIndex(arr, n, y) - + lowerIndex(arr, n, x) + 1; + return count; + } + + public int _gcd(int a, int b) + { + + if(b == 0) { + return a; + } + else { + return _gcd(b, a % b); + } + } + + public int add(int a, int b){ + a+=b; + if(a>=MOD) a-=MOD; + else if(a<0) a+=MOD; + return a; + } + + public int mul(int a, int b){ + long res= (long)a*(long)b; + return (int)(res%MOD); + } + + public int power(int a, int b) { + int ans=1; + while(b>0){ + if((b&1)!=0) ans= mul(ans,a); + b>>=1; + a= mul(a,a); + } + return ans; + } + + int[] fact= new int[MAXN]; + int[] inv= new int[MAXN]; + + public int Ckn(int n, int k){ + if(k<0 || n<0) return 0; + return mul(mul(fact[n],inv[k]),inv[n-k]); + } + + public int inverse(int a){ + return power(a,MOD-2); + } + + public void preprocess() { + fact[0]=1; + for(int i=1;i=0;i--){ + inv[i]= mul(inv[i+1],i+1); + } + } + + /** + * return VALUE of lower bound for unsorted array + */ + public int lowerBoundNormalArray(int[] arr, int x){ + TreeSet set= new TreeSet<>(); + for(int num: arr) set.add(num); + return set.lower(x); + } + /** + * return VALUE of upper bound for unsorted array + */ + public int upperBoundNormalArray(int[] arr, int x){ + TreeSet set= new TreeSet<>(); + for(int num: arr) set.add(num); + return set.higher(x); + } + + public void debugArr(int[] arr){ + for(int i: arr) out.print(i+"" ""); + out.println(); + } + + public int rand(){ + int min=0, max= MAXN; + int random_int = (int)Math.floor(Math.random()*(max-min+1)+min); + return random_int; + } + + InputReader in; PrintWriter out; + static int mod = 1000000007; + int ans = 0; + ArrayListg[] = new ArrayList[101]; + int cnt[][] = new int[101][101]; + int dp[][] = new int[101][101]; + int Add(int a, int b){ + a+=b; while (a>=mod)a-=mod; + return a; + } + int Mul(int a, int b){ + return (int)(((long)a * b) % mod); + } + void dfs(int node, int par, int root, int depth){ + cnt[root][depth]++; + for (int v : g[node]){ + if (v != par){ + dfs(v, node, root, depth+1); + } + } + } + public void solve(InputReader in, PrintWriter out) { + int t = in.nextInt(); + while (t-- > 0){ + int n = in.nextInt(); + String[] str = new String[n]; + ArrayList diff[] = new ArrayList[5]; + for (int i = 0; i < 5; i++) diff[i] = new ArrayList<>(); + for (int i = 0; i < n; i++){ + str[i] = in.nextToken(); + int[] cnt = new int[5]; + for (int j = 0; j < str[i].length(); j++){ + cnt[str[i].charAt(j) - 'a']++; + } + for (int j = 0; j < 5; j++){ + diff[j].add(cnt[j] * 2 - str[i].length()); + } + } + int ans = 0; + for (int i = 0; i < 5; i++){ + Collections.sort(diff[i]); + Collections.reverse(diff[i]); + int cur = 0, x = 0; + for (int j = 0; j < diff[i].size(); j++){ + cur+=diff[i].get(j); + if (cur <= 0){ + break; + } + x++; + } + ans = Math.max(ans, x); + } + out.println(ans); + } + } + + class SEG { + int n; + int[] segs; + public SEG (int[] a){ + this.n= a.length; + segs= new int[4*this.n]; + build(a,0,0,this.n-1); + } + public void build(int[] a, int root, int l, int r){ + if(l==r){ + segs[root]=a[l]; + return; + } + int m= (l+r)/2; + build(a,2*root+1,l,m); + build(a,2*root+2,m+1,r); + segs[root]= _gcd(segs[2*root+1], segs[2*root+2]); + } + public int query(int root, int l, int r, int lq, int rq){ + if(lq<=l && rq>=r) return segs[root]; + if(lq>r || rq m= new HashMap<>(); + public long base=0; + public long totalValue=0; + private int M= 1000000007; + + private long addMod(long a, long b){ + a+=b; + if(a>=M) a-=M; + return a; + } + + public void reset(){ + m= new HashMap<>(); + base=0; + totalValue=0; + } + public void update(long add){ + base= base+ add; + } + public void add(long key, long val){ + long newKey= key-base; + m.put(newKey, addMod(m.getOrDefault(newKey,(long)0),val)); + } + } + + + + static class Tuple implements Comparable{ + int x, y, z; + public Tuple(int x, int y, int z){ + this.x= x; + this.y= y; + this.z=z; + } + @Override + public int compareTo(Tuple o){ + return this.x-o.x; + } + } + + static class Pair implements Comparable{ + public int x; + public int y; + public Pair(int x, int y){ + this.x= x; + this.y= y; + } + + @Override + public int compareTo(Pair o) { + return this.x-o.x; + } + } + + // public static class compareL implements Comparator{ + // @Override + // public int compare(Tuple t1, Tuple t2) { + // return t2.l - t1.l; + // } + // } + + // fast input reader class; + static class InputReader { + BufferedReader br; + StringTokenizer st; + + public InputReader(InputStream stream) { + br = new BufferedReader(new InputStreamReader(stream)); + } + + public String nextToken() { + while (st == null || !st.hasMoreTokens()) { + String line = null; + try { + line = br.readLine(); + } catch (IOException e) { + throw new RuntimeException(e); + } + if (line == null) { + return null; + } + st = new StringTokenizer(line); + } + return st.nextToken(); + } + + public int nextInt() { + return Integer.parseInt(nextToken()); + } + public double nextDouble(){ + return Double.parseDouble(nextToken()); + } + public long nextLong(){ + return Long.parseLong(nextToken()); + } + public int[] nextIntArr(int n){ + int[] arr= new int[n]; + for(int i=0;i nextIntList(int n){ + List arr= new ArrayList<>(); + for(int i=0;i> nextIntMatList(int n, int m){ + List> mat= new ArrayList<>(); + for(int i=0;i temp= new ArrayList<>(); + for(int j=0;j a) val--; + return val; + } + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + PrintWriter writer = new PrintWriter(System.out); + int T = Integer.parseInt(br.readLine()); + while(T-- > 0) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int n = Integer.parseInt(st.nextToken()); + long k = Long.parseLong(st.nextToken()); + Long[] a = new Long[n]; + st = new StringTokenizer(br.readLine()); + long[] sum = new long[n]; + for(int i = 0; i < n; i++) { + a[i] = Long.parseLong(st.nextToken()); + } + Arrays.sort(a); + sum[0] = a[0]; + for(int i = 1; i < n; i++) { + sum[i] += sum[i-1]+a[i]; + } + long ans = Long.MAX_VALUE; + for(int y = 0; y < n; y++) { + // long z = k-sum[n-y-1]+a[0]; + // if(z > 0) z = z/(y+1); + // else { + // if(z%(y+1) == 0) z = z/(y+1); + // else { + // z = z/(y+1); + // z -= 1; + // } + // } + long x = a[0]-accurateFloor(k-sum[n-y-1]+a[0], y+1); + x = Math.max(0, x); + ans = Math.min(ans, y+x); + } + writer.println(ans); + } + writer.close(); + br.close(); + } + +} +","import java.math.BigInteger; +import java.util.*; +import java.io.*; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; + +public class CodeForces { + + + public void run() throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(br.readLine()); + next : while (t-- > 0) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int n = Integer.parseInt(st.nextToken()); + long k = Long.parseLong(st.nextToken()); + Long[] a = new Long[n]; + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < n; i++) { + a[i] = Long.parseLong(st.nextToken()); + } + Arrays.sort(a); + long ans = Long.MAX_VALUE; + long[] lsum = new long[n + 1]; + for (int i = 0; i < n; i++) { + lsum[i + 1] = lsum[i] + a[i]; + } + for (long y = 0; y < n; y++) { + long x = 0; + if ((k - lsum[n - (int)y] + a[0]) >= 0) { + x = (k - lsum[n - (int)y] + a[0]) / (y + 1); + } else { + if ((k - lsum[n - (int)y] + a[0]) % (y + 1) == 0) { + x = (k - lsum[n - (int)y] + a[0]) / (y + 1); + } else { + x = (k - lsum[n - (int)y] + a[0]) / (y + 1) - 1; + } + } + x = a[0] - x; + ans = Math.min(ans, Math.max(0, x) + y); + } + System.out.println(ans); + } + + } + + public static void main(String[] args) throws Exception { + new CodeForces().run(); + } + +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +",0,Non-plagiarised +24044589,e435b1ac,"import java.util.*; +import java.io.*; + +public class C_Phoenix_and_Towers{ + public static void main(String[] args) { + FastScanner s= new FastScanner(); + StringBuilder res = new StringBuilder(); + int t=s.nextInt(); + int p=0; + while(p> indices = new HashMap>(); + for(int i=0;i obj = new LinkedList(); + obj.add(i); + indices.put(array[i],obj); + } + } + long tower[]= new long[m]; + int ans[]= new int[n]; + Arrays.sort(array); + int k=0; + for(int i=0;i0) { + int n = sc.nextInt(); + int m = sc.nextInt(); + long x = sc.nextLong(); + long[] arr = new long[n]; + for(int i = 0; i < n; i++) { + arr[i] = sc.nextLong(); + } + int[] res = new int[n]; + PriorityQueue q = new PriorityQueue<>(); + for(int i = 0; i < m; i++) { + q.add(new Pair(i+1, 0)); + } + for(int i = 0; i < n; i++) { + Pair p = q.poll(); + res[i] = p.i; + q.add(new Pair(p.i, p.w + arr[i])); + } + sb.append(""YES\n""); + for(int i = 0; i < n; i++) { + sb.append(res[i]+"" ""); + } + sb.replace(sb.length()-1, sb.length(), ""\n""); + } + PrintWriter pw = new PrintWriter(System.out); + pw.println(sb.toString().trim()); + pw.flush(); + + } + static class Pair implements Comparable{ + int i; long w; + public Pair(int i, long w) { + this.i = i; this.w = w; + } + public String toString() { + return i+"" ""+w; + } + @Override + public int compareTo(Pair p) { + return Long.compare(w, p.w); + } + } + + static class FastScanner { + public BufferedReader reader; + public StringTokenizer tokenizer; + public FastScanner() { + reader = new BufferedReader(new InputStreamReader(System.in), 32768); + tokenizer = null; + } + public String next() { + while (tokenizer == null || !tokenizer.hasMoreTokens()) { + try { + tokenizer = new StringTokenizer(reader.readLine()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + return tokenizer.nextToken(); + } + public int nextInt() { + return Integer.parseInt(next()); + } + public long nextLong() { + return Long.parseLong(next()); + } + public double nextDouble() { + return Double.parseDouble(next()); + } + public String nextLine() { + try { + return reader.readLine(); + } catch(IOException e) { + throw new RuntimeException(e); + } + } + } + +} +",0,Non-plagiarised +2acaa7df,46e9aed4,"import java.io.*; +import java.util.*; +public class Ishu +{ + static Scanner scan = new Scanner(System.in); + static BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); + static void tc() throws Exception + { + int n = scan.nextInt(); + int[] a = new int[n]; + int one = 0; + int i,j; + + for(i=0;i 0) + tc(); + } +} +"," +import java.util.*; + +import java.lang.*; +import java.io.*; + +/* Name of the class has to be ""Main"" only if the class is public. */ +public final class Solution { + + + public static void main(String[] args) throws Exception { + + Reader sc = new Reader(); + BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out)); + + int n=sc.nextInt(); + ArrayList fill= new ArrayList(); + ArrayList unfilled= new ArrayList<>(); + for(int i=0;i= '0' && c <= '9'); + + if (neg) + return -ret; + return ret; + } + + public long nextLong() throws IOException { + long ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + if (neg) + return -ret; + return ret; + } + + public double nextDouble() throws IOException { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') { + while ((c = read()) >= '0' && c <= '9') { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) + return -ret; + return ret; + } + + private void fillBuffer() throws IOException { + bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); + if (bytesRead == -1) + buffer[0] = -1; + } + + private byte read() throws IOException { + if (bufferPointer == bytesRead) + fillBuffer(); + return buffer[bufferPointer++]; + } + + public void close() throws IOException { + if (din == null) + return; + din.close(); + } +} + +",0,Non-plagiarised +e1c4f3db,e81b2d16,"import java.util.*; + +public class BalancedStoneHeaps { + + public static boolean check(int n, int x, int[] h) { + int[] c_h = new int[n]; + for (int i = 0; i < n; i++) + c_h[i] = h[i]; + for (int i = n - 1; i >= 2; i--) { + if (c_h[i] < x) + return false; + int d = Math.min(h[i], c_h[i] - x) / 3; + c_h[i - 1] += d; + c_h[i - 2] += 2 * d; + } + return c_h[0] >= x && c_h[1] >= x; + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while (t-- > 0) { + int n = sc.nextInt(); + int[] h = new int[n]; + int max = Integer.MIN_VALUE; + for (int i = 0; i < n; i++) { + h[i] = sc.nextInt(); + if (h[i] > max) { + max = h[i]; + } + } + int l = 0; + int r = max; + while (l < r) { + int mid = l + (r - l + 1) / 2; + if (check(n, mid, h)) { + l = mid; + } else { + r = mid - 1; + } + } + System.out.println(l); + } + } +}","import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class fastTemp { + static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader() + { + br = new BufferedReader( + new InputStreamReader(System.in)); + } + + String next() + { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { return Integer.parseInt(next()); } + + long nextLong() { return Long.parseLong(next()); } + + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str = """"; + try { + str = br.readLine(); + } + catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } + + static int n; + static int arr[]; + public static void main(String[] args) + { + FastReader fs = new FastReader(); + int t = fs.nextInt(); + while(t-- >0){ + n = fs.nextInt(); + arr = new int[n]; + int max = Integer.MIN_VALUE; + for(int i=0;i=2;i--){ + if(ar[i]=x && ar[1]>=x; + } + + +}",1,Plagiarised +3951966f,b842cf12," +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; + +public class Armchairs { + static ArrayList f; + static ArrayList u; + static int dp[][]; + static int fun(int i, int j){ + if(i == f.size()) return 0; + if(j == u.size()) return 99999999; + if(dp[i][j] != -1) return dp[i][j]; + int ans1 = fun(i, j+1); + int ans2 = fun(i+1, j+1) + Math.abs(f.get(i)-u.get(j)); + return dp[i][j] = Math.min(ans1, ans2); + } + private static int solve(int n, int a[]) { + for (int i = 0; i < n; i++) { + if (a[i]==0) + u.add(i); + else + f.add(i); + } + + return fun(0,0); + } + + public static void main(String[] args) + throws IOException { + Scanner s = new Scanner(); + int t = 1; + StringBuilder ans = new StringBuilder(); + int count = 0; + while (t-- > 0) { + int n = s.nextInt(); + int a[] = new int[n]; + dp=new int[n][n]; + for (int i = 0; i < n; i++) { + a[i]=s.nextInt(); + } + f=new ArrayList<>(); + u=new ArrayList<>(); + for( int i=0; i= '0' && c <= '9'); + + if (neg) + return -ret; + return ret; + } + + public long nextLong() throws IOException { + long ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + if (neg) + return -ret; + return ret; + } + + public double nextDouble() throws IOException { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') { + while ((c = read()) >= '0' && c <= '9') { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) + return -ret; + return ret; + } + + private void fillBuffer() throws IOException { + bytesRead = din.read(buffer, bufferPointer = 0, + BUFFER_SIZE); + if (bytesRead == -1) + buffer[0] = -1; + } + + private byte read() throws IOException { + if (bufferPointer == bytesRead) + fillBuffer(); + return buffer[bufferPointer++]; + } + + public void close() throws IOException { + if (din == null) + return; + din.close(); + } + } + + public static long norm(long a, long MOD) { + return ((a % MOD) + MOD) % MOD; + } + + public static long msub(long a, long b, long MOD) { + return norm(norm(a, MOD) - norm(b, MOD), MOD); + + } + + public static long madd(long a, long b, long MOD) { + return norm(norm(a, MOD) + norm(b, MOD), MOD); + + } + + public static long mMul(long a, long b, long MOD) { + return norm(norm(a, MOD) * norm(b, MOD), MOD); + + + } + + public static long mDiv(long a, long b, long MOD) { + return norm(norm(a, MOD) / norm(b, MOD), MOD); + } +} +"," +import java.io.*; +import java.util.*; + + + +public class A { + + static long modInverse(long a, long m) { + long m0 = m; + long y = 0, x = 1; + + if (m == 1) + return 0; + + while (a > 1) { + + long q = a / m; + + long t = m; + + m = a % m; + a = t; + t = y; + + y = x - q * y; + x = t; + } + + if (x < 0) + x += m0; + + return x; + } + + static void pla() { + + int a = 0; + int b = 2; + int c = 0; + int d = 0; + + a = (int) Math.pow(2, 32); + + c = a + b; + b = c / a; + d = a + b + c; + + } + + static int mod = (int) 1e9 + 7; + + static Scanner sc = new Scanner(System.in); + static StringBuilder out = new StringBuilder(); + + public static void main(String[] args) throws IOException { + + int t = 1;// sc.nextInt(); + + while (t-- > 0) { + + A run = new A(); + + run.run(); + + } + + System.out.println(out); + + } + + public static int gcd(int a, int b) { + return (b == 0) ? a : gcd(b, a % b); + } + + public void run() throws IOException { + +// int k = sc.nextInt(); + +// if(100%k==0)out.append((100/k)+""\n""); +// +// else { +// out.append(100+""\n""); +// } + +// int ans=(int)1e9; +// for(int i=1;i<=k;i++) { +// +// +// if(i*100%k==0) { +// +// ans=Math.min(ans,i*) +// +// +// } +// +// } + +// +// int ans = 100 - k; +// if (k == 100) +// out.append(1); +// else { +// int g = gcd(k, ans); +// out.append((k / g) + (ans / g)); +// } +// out.append(""\n""); +// + +// int n = sc.nextInt(); +// int a[] = new int[n]; +// +// for(int i=0;i ts = new TreeSet<>(); + + ArrayList a1 = new ArrayList<>(); + ArrayList b = new ArrayList<>(); + for (int i = 0; i < n; i++) { + + a[i] = sc.nextInt(); + if (a[i] == 1) + a1.add(i); + else + b.add(i); + + } + + int dp[] = new int[n]; + int sum = 0; + + memo = new int[n][n]; + + for (int tem[] : memo) + Arrays.fill(tem, -1); + + sum = solve(0, 0, a1, b); + out.append(sum + ""\n""); + } + + static int memo[][]; + + static int solve(int i, int j, ArrayList a, ArrayList b) { + + if(i==a.size())return 0; + if(a.size()-i>b.size()-j)return (int)1e9; + if(j==b.size())return (int)1e9; + if(memo[i][j]!=-1)return memo[i][j]; + int ans = (int) 1e9; + + + + + + ans = Math.min(solve(i + 1, j+1 , a, b) + Math.abs(a.get(i) - b.get(j)),solve(i,j+1,a,b)); + + + return memo[i][j]=ans; + + } + + static boolean check(int[] dp, int cnt) { + + int count = 0; + + for (int i = 0; i < dp.length; i++) { + + if (dp[i] == 1) + count++; + } + + return (count == cnt); + + } + + static void sort(int a[], int n) { + + ArrayList al = new ArrayList<>(); + + for (int i = 0; i < n; i++) { + + al.add(a[i]); + } + + Collections.sort(al); + + for (int i = 0; i < n; i++) { + + a[i] = al.get(i); + } + } + + static void sort(long a[], int n) { + + ArrayList al = new ArrayList<>(); + + for (int i = 0; i < n; i++) { + + al.add(a[i]); + } + + Collections.sort(al); + + for (int i = 0; i < n; i++) { + + a[i] = al.get(i); + } + } + +// static Reader sc = new Reader(); + + static class Reader { + final private int BUFFER_SIZE = 1 << 16; + private DataInputStream din; + private byte[] buffer; + private int bufferPointer, bytesRead; + + public Reader() { + din = new DataInputStream(System.in); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public Reader(String file_name) throws IOException { + din = new DataInputStream(new FileInputStream(file_name)); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public String readLine() throws IOException { + byte[] buf = new byte[64]; // line length + int cnt = 0, c; + while ((c = read()) != -1) { + if (c == '\n') + break; + buf[cnt++] = (byte) c; + } + return new String(buf, 0, cnt); + } + + public int nextInt() throws IOException { + int ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (neg) + return -ret; + return ret; + } + + public long nextLong() throws IOException { + long ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + if (neg) + return -ret; + return ret; + } + + public double nextDouble() throws IOException { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') { + while ((c = read()) >= '0' && c <= '9') { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) + return -ret; + return ret; + } + + private void fillBuffer() throws IOException { + bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); + if (bytesRead == -1) + buffer[0] = -1; + } + + private byte read() throws IOException { + if (bufferPointer == bytesRead) + fillBuffer(); + return buffer[bufferPointer++]; + } + + public void close() throws IOException { + if (din == null) + return; + din.close(); + } + } + +}",0,Non-plagiarised +a368f345,b434c275,"import java.util.*; +import java.io.*; + +public class codeforces { + static class Pair { + char type; + int L; + int R; + + Pair(char type, int L, int R) { + this.type = type; + this.L = L; + this.R = R; + } + } + + static FastReader fr; + static StringBuilder res; + + static class FastReader { + + BufferedReader br; + StringTokenizer st; + BufferedWriter bw; + + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + bw = new BufferedWriter(new OutputStreamWriter(System.out)); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + char nextChar() { + return next().charAt(0); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + void write(String str) { + + try { + bw.write(str); + } catch (IOException e) { + e.printStackTrace(); + } + } + + void close() { + try { + bw.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } + + public static void main(String[] args) throws java.lang.Exception { + fr = new FastReader(); + boolean fixed = false; + int t = !fixed ? fr.nextInt() : 1; + res = new StringBuilder(); + while (t-- > 0) { + solve(); + } + fr.write(res.toString()); + fr.close(); + } + + static int M = 1000008; + static boolean[] primes = new boolean[M]; + static int[] dp = new int[M]; + + public static void sieve() { + for (int i = 2; i * i <= M; i++) { + if (!primes[i]) { + for (int j = i * i; j < M; j += i) { + primes[j] = true; + } + } + } + + for (int i = 5; i < 1000001; i++) { + if (!primes[i] && !primes[i - 2]) + dp[i] = dp[i - 1] + 1; + else + dp[i] = dp[i - 1]; + } + } + + public static void solve() { + int n = fr.nextInt(); + String[] arr = new String[n]; + for (int i = 0; i < n; i++) { + arr[i] = fr.nextLine(); + } + char[] chars = { 'a', 'b', 'c', 'd', 'e' }; + int ans = 0; + for (int i = 0; i < 5; i++) { + char ch = chars[i]; + List a = new ArrayList<>(); + for (int j = 0; j < n; j++) { + String s = arr[j]; + int c = 0; + for (int k = 0; k < s.length(); k++) { + if (s.charAt(k) == ch) + c++; + } + a.add(2 * c - s.length()); + } + // System.out.println(a); + Collections.sort(a, Collections.reverseOrder()); + int sum = 0, len = 0; + for (int k = 0; k < a.size(); k++) { + if (sum + a.get(k) <= 0) { + break; + } else { + len++; + sum += a.get(k); + } + } + ans = Math.max(ans, len); + } + res.append(ans + ""\n""); + } + + public static int[] readArray(int n) { + int[] arr = new int[n]; + for (int i = 0; i < n; i++) { + arr[i] = fr.nextInt(); + } + return arr; + } + + public static long gcd(long a, long b) { + if (a == 0) + return b; + return gcd(b % a, a); + } + + public static List readArrayList(int n) { + List arr = new ArrayList<>(); + for (int i = 0; i < n; i++) { + arr.add(fr.nextInt()); + } + return arr; + } +} +","import java.io.*; +import java.util.*; + + + +import java.math.*; +import java.math.BigInteger; + + +public final class A +{ + static PrintWriter out = new PrintWriter(System.out); + static StringBuilder ans=new StringBuilder(); + static FastReader in=new FastReader(); + static ArrayList g[]; + static long mod=(long)1e9+7,INF=Long.MAX_VALUE; + static boolean set[],col[]; + static int par[],tot[],partial[]; + static int D[],P[][]; + static long dp[][],sum=0,max=0,size[]; + // static node1 seg[]; + //static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)}; + public static void main(String args[])throws IOException + { + + + + int T=i(); + outer:while(T-->0) + { + int N=i(); + int size[]=new int[N]; + PriorityQueue q[]=new PriorityQueue[26]; + for(int i=0; i<26; i++)q[i]=new PriorityQueue(); + for(int i=0; i q_new=new PriorityQueue<>(); + q_new=q[i]; + int c=0; + long f=0; + while(q_new.size()>0) + { + node1 x=q_new.remove(); +// System.out.println(x.f+"" ""+x.size+"" ""+x.a); + f+=x.a; + if(f>0) + { + c++; + max=Math.max(max, c); + } + else break; + } + } + out.println(max); + } + out.close(); + + } + static int OR(int i,int j) + { + if(i>j) + { + int t=i; + i=j; + j=t; + } + System.out.println(""OR ""+i+"" ""+j); + return i(); + } + static int AND(int i,int j) + { + if(i>j) + { + int t=i; + i=j; + j=t; + } + System.out.println(""AND ""+i+"" ""+j); + return i(); + } + static int XOR(int i,int j) + { + if(i>j) + { + int t=i; + i=j; + j=t; + } + System.out.println(""XOR ""+i+"" ""+j); + return i(); + } + + static boolean f1(int l,char X[]) + { + int i=0; + for(; l0; a++) + { + if(GCD(i,a)==1)return i; + } + return 0; + } + static int min(char X[],char str[],int N) + { + int s=0; + for(int i=0; i=b && a>=c)return 'R'; + if(b>=a && b>=c)return 'B'; + return 'G'; + } + + + static void f1(int n,int p,long sum,int N) + { + for(int c:g[n]) + { + if(c==p)continue; + + long s=sum+N-2*size[c]; + f1(c,n,s,N); + max=Math.max(max, s); + } + } + static long f(int i,int j,ArrayList A) + { + + if(i+1==A.size()) + { + return j; + } + int moves=1+A.get(i); + if(j==1)return 1+f(i+1,moves,A); + if(j>0 && dp[i][j-1]==0)f(i,j-1,A); + return dp[i][j]=dp[i][j-1]+f(i+1,j+moves,A); + } + // static void build(int v,int tl,int tr,long A[]) + // { + // if(tl==tr) + // { + // seg[v]=new node1(A[tl],A[tr],1,true); + // return ; + // } + // int tm=(tl+tr)/2; + // build(2*v,tl,tm,A); + // build(2*v+1,tm+1,tr,A); + // seg[v]=merge(seg[2*v],seg[2*v+1]); + // } + // static node1 ask(int v,int tl,int tr,int l,int r) + // { + // if(l>r)return new node1(0,0,0,false);//verify true or false + // if(tl==l && tr==r)return seg[v]; + // int tm=(tl+tr)/2; + // node1 a=ask(v*2,tl,tm,l,Math.min(tm, r)); + // node1 b=ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r); + // return merge(a,b); + // } + // static node1 merge(node1 a,node1 b) + // { + // long s=0; + // long l1=a.L,r1=a.R,c1=a.cnt; + // long l2=b.L,r2=b.R,c2=b.cnt; + // long g=GCD(l2,r1); s=c1+c2; + // if(g==1) + // { + // s--; + // g=(l2*r1)/g; + // if(c1==1) + // { + // l1=g; + // } + // if(c2==1)r2=g; + // return new node1(l1,r2,s,true); + // } + // return new node1(l1,r2,s,a.leaf^b.leaf); + // } + static long f(long l,long r,long a,long b,long N) + { + while(r-l>1) + { + long m=(l+r)/2; + long x=m*b; + if(N1) + { + long m=(l+r)/2; + if(x-m*s>max)l=m; + else r=m; + } + return l+1; + } + static int f(long A[],long x) + { + int l=-1,r=A.length; + while(r-l>1) + { + int m=(l+r)/2; + if(A[m]>=x)r=m; + else l=m; + } + return r; + } + + + + static int bin(int x,ArrayList A) + { + int l=0,r=A.size()-1; + while(l<=r) + { + int m=(l+r)/2; + int a=A.get(m); + if(a==x)return m; + if(a A) + { + int l=-1,r=A.size(); + while(r-l>1) + { + int m=(l+r)/2; + int a=A.get(m); + if(a<=x)l=m; + else r=m; + } + return l; + } + static int right(int x,ArrayList A) + { + int l=-1,r=A.size(); + while(r-l>1) + { + int m=(l+r)/2; + int a=A.get(m); + if(a Hash(int A[]) + { + HashMap mp=new HashMap<>(); + for(int a:A) + { + int f=mp.getOrDefault(a,0)+1; + mp.put(a, f); + } + return mp; + } + static long mul(long a, long b) + { + return ( a %mod * 1L * b%mod )%mod; + } + static void swap(int A[],int a,int b) + { + int t=A[a]; + A[a]=A[b]; + A[b]=t; + } + + + static int find(int a) + { + if(par[a]<0)return a; + return par[a]=find(par[a]); + } + static void union(int a,int b) + { + a=find(a); + b=find(b); + if(a!=b) + { + par[a]+=par[b]; + par[b]=a; + } + } + static boolean isSorted(int A[]) + { + for(int i=1; i high) + if (x >= A[high]) + return A[high]; + + int mid = (low + high) / 2; + + if (A[mid] == x) + return A[mid]; + + if (mid > 0 && A[mid - 1] <= x && x < A[mid]) + return A[mid - 1]; + + if (x < A[mid]) + return lower_Bound( A, low, mid - 1, x); + + return lower_Bound(A, mid + 1, high, x); + } + + static String f(String A) + { + String X=""""; + for(int i=A.length()-1; i>=0; i--) + { + int c=A.charAt(i)-'0'; + X+=(c+1)%2; + } + return X; + } + + static void sort(long[] a) //check for long + { + ArrayList l=new ArrayList<>(); + for (long i:a) l.add(i); + Collections.sort(l); + for (int i=0; i(); + D[i]=Integer.MAX_VALUE; + //D2[i]=INF; + } + } + + + + static long pow(long a,long b) + { + //long mod=1000000007; + long pow=1; + long x=a; + while(b!=0) + { + if((b&1)!=0)pow=(pow*x)%mod; + x=(x*x)%mod; + b/=2; + } + return pow; + } + + static long toggleBits(long x)//one's complement || Toggle bits + { + int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; + + return ((1< l=new ArrayList<>(); + for (int i:a) l.add(i); + Collections.sort(l); + for (int i=0; i A) + { + for(int a:A)System.out.print(a+"" ""); + System.out.println(); + } + + static int i() + { + return in.nextInt(); + } + + static long l() + { + return in.nextLong(); + } + + static int[] input(int N){ + int A[]=new int[N]; + for(int i=0; i +{ + int index,f,size; + long a; + node1(int f,int i,int size) + { + this.f=f; + this.index=i; + this.size=size; + a=2*f-size; + } + public int compareTo(node1 x) + { + if(this.a==x.a)return 0; + else if(this.a high = new Stack<>(); + Stack low = new Stack<>(); + high.push(0); + low.push(0); + for(int i = 1; i < n; i++) { + dp[i] = dp[i-1]+1; + while(!high.isEmpty() && heights[high.peek()] < heights[i]) { + dp[i] = Math.min(dp[i], dp[high.peek()]+1); + high.pop(); + } + if(!high.isEmpty()) { + dp[i] = Math.min(dp[i], dp[high.peek()]+1); + if(heights[high.peek()] == heights[i]) + high.pop(); + } + while(!low.isEmpty() && heights[low.peek()] > heights[i]) { + dp[i] = Math.min(dp[i], dp[low.peek()]+1); + low.pop(); + } + if(!low.isEmpty()) { + dp[i] = Math.min(dp[i], dp[low.peek()]+1); + if(heights[low.peek()] == heights[i]) + low.pop(); + } + high.push(i); + low.push(i); + } + System.out.println(dp[n-1]); + } + +} +","import java.io.OutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.util.Vector; +import java.util.InputMismatchException; +import java.io.IOException; +import java.util.Stack; +import java.io.InputStream; + +public class Main { + public static void main(String[] args) { + InputStream inputStream = System.in; + OutputStream outputStream = System.out; + InputReader in = new InputReader(inputStream); + PrintWriter out = new PrintWriter(outputStream); + DDiscreteCentrifugalJumps solver = new DDiscreteCentrifugalJumps(); + solver.solve(1, in, out); + out.close(); + } + + static class DDiscreteCentrifugalJumps { + public void solve(int testNumber, InputReader s, PrintWriter w) { + int n = s.nextInt(); + int[] a = new int[n]; + for (int i = 0; i < n; i++) + a[i] = s.nextInt(); + int[] dp = new int[n]; + for (int i = 0; i < n; i++) + dp[i] = i; + Stack dec = new Stack<>(); + dec.push(0); + Stack inc = new Stack<>(); + inc.push(0); + for (int i = 1; i < n; i++) { + while (!dec.isEmpty() && a[dec.peek()] < a[i]) { + dp[i] = Math.min(dp[i], dp[dec.peek()] + 1); + dec.pop(); + } + if (!dec.isEmpty()) { + dp[i] = Math.min(dp[i], dp[dec.peek()] + 1); + if (a[dec.peek()] == a[i]) + dec.pop(); + } + dec.push(i); + while (!inc.isEmpty() && a[inc.peek()] > a[i]) { + dp[i] = Math.min(dp[i], dp[inc.peek()] + 1); + inc.pop(); + } + if (!inc.isEmpty()) { + dp[i] = Math.min(dp[i], dp[inc.peek()] + 1); + if (a[inc.peek()] == a[i]) + inc.pop(); + } + inc.push(i); + } + w.println(dp[n - 1]); + } + + } + + static class InputReader { + private InputStream stream; + private byte[] buf = new byte[1024]; + private int curChar; + private int numChars; + private InputReader.SpaceCharFilter filter; + + public InputReader(InputStream stream) { + this.stream = stream; + } + + public int read() { + if (numChars == -1) { + throw new InputMismatchException(); + } + if (curChar >= numChars) { + curChar = 0; + try { + numChars = stream.read(buf); + } catch (IOException e) { + throw new InputMismatchException(); + } + if (numChars <= 0) { + return -1; + } + } + return buf[curChar++]; + } + + public int nextInt() { + int c = read(); + while (isSpaceChar(c)) { + c = read(); + } + int sgn = 1; + if (c == '-') { + sgn = -1; + c = read(); + } + int res = 0; + do { + if (c < '0' || c > '9') { + throw new InputMismatchException(); + } + res *= 10; + res += c - '0'; + c = read(); + } while (!isSpaceChar(c)); + return res * sgn; + } + + public boolean isSpaceChar(int c) { + if (filter != null) { + return filter.isSpaceChar(c); + } + return isWhitespace(c); + } + + public static boolean isWhitespace(int c) { + return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; + } + + public interface SpaceCharFilter { + public boolean isSpaceChar(int ch); + + } + + } +} + +",1,Plagiarised +856a8eda,c354b74f,"/*input +2 +5 2 3 +1 2 3 1 2 +4 3 3 +1 1 2 3 +*/ +import java.io.*; +import java.util.*; + +public class three{ + public static class Pair implements Comparable{ + int min; + int idx; + @Override + public int compareTo(Pair o) { + return min - o.min; + } + } + +public static void main(String[] args) throws Exception { + MyScanner scn = new MyScanner(); + out = new PrintWriter(new BufferedOutputStream(System.out)); + /* + int n = sc.nextInt(); // read input as integer + long k = sc.nextLong(); // read input as long + double d = sc.nextDouble(); // read input as double + String str = sc.next(); // read input as String + String s = sc.nextLine(); // read whole line as String + + int result = 3*n; + out.println(result); // print via PrintWriter + */ + + //The Code Starts here + int t = scn.nextInt(); + while(t-- > 0){ + int n = scn.nextInt(); + int m = scn.nextInt(); + + int x = scn.nextInt(); + int arr[] = scn.nextIntArray(n); + PriorityQueue pq = new PriorityQueue<>(); + System.out.println(""YES""); + + for(int i=0;i { + // long u; + // long v; + + // public Pair(long u, long v) { + // this.u = u; + // this.v = v; + // } + + + // public int hashCode() { + // int hu = (int) (u ^ (u >>> 32)); + // int hv = (int) (v ^ (v >>> 32)); + // return 31 * hu + hv; + // } + + // public boolean equals(Object o) { + // Pair other = (Pair) o; + // return u == other.u && v == other.v; + // } + + // public int compareTo(Pair other) { + // return Long.compare(u, other.u) != 0 ? Long.compare(u, other.u) : Long.compare(v, other.v); + // } + + // public String toString() { + // return ""[u="" + u + "", v="" + v + ""]""; + // } + // } + //-------------------------------------------------------- +} + + + + + + + + + +","//Some of the methods are copied from GeeksforGeeks Website +import java.util.*; +import java.lang.*; +import java.io.*; +public class Main +{ + static Reader sc=new Reader(); + // static FastReader sc=new FastReader(System.in); + static class PairComparator implements Comparator{ + + // Overriding compare()method of Comparator + // for descending order of cgpa + public int compare(Pair s1, Pair s2) { + if (s1.x < s2.x) + return -1; + else if (s1.x > s2.x) + return 1; + return 0; + } + } + public static void main (String[] args) throws java.lang.Exception + { +// try{ + /* + int n=sc.nextInt(); + ArrayList al=new ArrayList<>(); + ArrayList al=new ArrayList<>(); + Set set=new HashSet<>(); + Collections.sort(al,Collections.reverseOrder()); + + long n=sc.nextLong(); + for(int i=0;i0) + { + int n=sc.nextInt(); + int m=sc.nextInt(); + int x=sc.nextInt(); + PriorityQueue pq=new PriorityQueue<>(m, new PairComparator()); + for(int i=1;i<=m;i++) + { + Pair p=new Pair(0,i); + pq.add(p); + } + out.println(""YES""); + int a[]=new int[n]; + for(int i=0;i map=new HashMap<>(); + for(int i=0;i> hmap=map.entrySet(); + for(Map.Entry data : hmap) + { + + } + + Iterator it = set.iterator(); + while(it.hasNext()) + { + int x=it.next(); + } + */ + +static void print(int a[]) + { + int n=a.length; + for(int i=0;i al) + { + int si=al.size(); + for(int i=0;i al) + { + int si=al.size(); + for(int i=0;i>>1; + if(a[m]>=x) r=m; + else l=m; + } + return r; +} + + static int UpperBound(int a[], int x) + {// x is the key or target value + int l=-1,r=a.length; + while(l+1>>1; + if(a[m]<=x) l=m; + else r=m; + } + return l+1; + } +static class Graph + { + int v; + ArrayList list[]; + Graph(int v) + { + this.v=v; + list=new ArrayList[v+1]; + for(int i=1;i<=v;i++) + list[i]=new ArrayList(); + } + void addEdge(int a, int b) + { + this.list[a].add(b); + } + } +static void DFS(Graph g, boolean[] visited, int u) + { + visited[u]=true; + int v=0; + for(int i=0;i l=new ArrayList<>(); + for (int i:a) l.add(i); + Collections.sort(l); + for (int i=0; i l=new ArrayList<>(); + for (long i:a) l.add(i); + Collections.sort(l); + for (int i=0; i= total) { + index = 0; + total = in.read(buf); + if (total <= 0) { + return -1; + } + } + return buf[index++]; + } + + String next() throws IOException { + int c; + for (c = scan(); c <= 32; c = scan()); + StringBuilder sb = new StringBuilder(); + for (; c > 32; c = scan()) { + sb.append((char) c); + } + return sb.toString(); + } + + int nextInt() throws IOException { + int c, val = 0; + for (c = scan(); c <= 32; c = scan()); + boolean neg = c == '-'; + if (c == '-' || c == '+') { + c = scan(); + } + for (; c >= '0' && c <= '9'; c = scan()) { + val = (val << 3) + (val << 1) + (c & 15); + } + return neg ? -val : val; + } + + long nextLong() throws IOException { + int c; + long val = 0; + for (c = scan(); c <= 32; c = scan()); + boolean neg = c == '-'; + if (c == '-' || c == '+') { + c = scan(); + } + for (; c >= '0' && c <= '9'; c = scan()) { + val = (val << 3) + (val << 1) + (c & 15); + } + return neg ? -val : val; + } + } + + static class Reader + { + final private int BUFFER_SIZE = 1 << 16; + private DataInputStream din; + private byte[] buffer; + private int bufferPointer, bytesRead; + + public Reader() + { + din = new DataInputStream(System.in); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public Reader(String file_name) throws IOException + { + din = new DataInputStream(new FileInputStream(file_name)); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public String readLine() throws IOException + { + byte[] buf = new byte[64]; // line length + int cnt = 0, c; + while ((c = read()) != -1) + { + if (c == '\n') + break; + buf[cnt++] = (byte) c; + } + return new String(buf, 0, cnt); + } + + public int nextInt() throws IOException + { + int ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do + { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (neg) + return -ret; + return ret; + } + + public long nextLong() throws IOException + { + long ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } + while ((c = read()) >= '0' && c <= '9'); + if (neg) + return -ret; + return ret; + } + + public double nextDouble() throws IOException + { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + + do { + ret = ret * 10 + c - '0'; + } + while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') + { + while ((c = read()) >= '0' && c <= '9') + { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) + return -ret; + return ret; + } + + private void fillBuffer() throws IOException + { + bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); + if (bytesRead == -1) + buffer[0] = -1; + } + + private byte read() throws IOException + { + if (bufferPointer == bytesRead) + fillBuffer(); + return buffer[bufferPointer++]; + } + + public void close() throws IOException + { + if (din == null) + return; + din.close(); + } + } + static PrintWriter out=new PrintWriter(System.out); + static int int_max=Integer.MAX_VALUE; + static int int_min=Integer.MIN_VALUE; + static long long_max=Long.MAX_VALUE; + static long long_min=Long.MIN_VALUE; +} +// Thank You !",0,Non-plagiarised +c4ca2ff3,ccc8ef27,"/* package codechef; // don't place package name! */ + +import java.util.*; +import java.lang.*; +import java.io.*; + +/* Name of the class has to be ""Main"" only if the class is public. */ +public class Main +{ + public static void main (String[] args) throws java.lang.Exception + { + // your code goes here + Scanner sc=new Scanner(System.in); + int t=sc.nextInt(); + PrintWriter out=new PrintWriter(System.out); + while(t-->0) { + int n=sc.nextInt(); + int freq[][]=new int[n][5]; + int rem[][]=new int[n][5]; + for(int i=0;i=0;k--) { + if(sum+arr[k]>0) { + sum=sum+arr[k]; + total++; + } + else { + break; + } + } + ans=Math.max(ans,total); + } + out.println(ans); + + } + out.flush(); + out.close(); + } +} + +","import java.util.*; +public class Sol +{ + 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][5]; + int tot[]=new int[n]; + for(int i=0;i0) + res+=ans[j++]; + return j; + } +}",0,Non-plagiarised +8782922d,f5f97f19," +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.StringTokenizer; + +public class Interesting_Story { + static class RealScanner { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(""""); + + String next() { + while (!st.hasMoreTokens()) + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + int[] readArray(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) a[i] = nextInt(); + return a; + } + + long nextLong() { + return Long.parseLong(next()); + } + } + + public static void main(String[] args) { + RealScanner sc = new RealScanner(); + int t = sc.nextInt(); + while (t-- > 0) { + int n = sc.nextInt(); + List l = new ArrayList<>(); + for (int i = 0; i < n; i++) { + l.add(sc.next()); + } + int res = 0; + for (int i = 0; i <= 5; i++) { + List myL = new ArrayList<>(); + for (String s : l) { + int count1 = 0, count2 = 0; + for (char ch : s.toCharArray()) { + if (ch == (char) (i + 97)) { + count1++; + } else { + count2++; + } + } + myL.add(count2 - count1); + } + //System.out.println(myL); + Collections.sort(myL); + int sum = 0; + for (int m = 0; m < myL.size(); m++) { + sum += myL.get(m); + if (sum < 0) { + res = Math.max(res, m + 1); + } + } + } + System.out.println(res); + } + } +} +","import java.util.Arrays; +import java.util.Comparator; +import java.util.Scanner; + +public class One { + static class Word { + int a = 0; + int b = 0; + int c = 0; + int d = 0; + int e = 0; + int total = 0; + String s = """"; + + @Override + public String toString() { + return s; + } + } + + static class CompA implements Comparator { + public int compare(Word a, Word b) { + return (2 * b.a - b.total) - (2 * a.a - a.total); + } + } + + static class CompB implements Comparator { + public int compare(Word a, Word b) { + return (2 * b.b - b.total) - (2 * a.b - a.total); + } + } + + static class CompC implements Comparator { + public int compare(Word a, Word b) { + return (2 * b.c - b.total) - (2 * a.c - a.total); + } + } + + static class CompD implements Comparator { + public int compare(Word a, Word b) { + return (2 * b.d - b.total) - (2 * a.d - a.total); + } + } + + static class CompE implements Comparator { + public int compare(Word a, Word b) { + return (2 * b.e - b.total) - (2 * a.e - a.total); + } + } + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int t = scan.nextInt(); + + while (t != 0) { + t--; + int n = scan.nextInt(); + Word[] words = new Word[n]; + + for (int i = 0 ; i < n ; i++) { + String s = scan.next(); + Word word = new Word(); + word.s = s; + + for (int j = 0 ; j < s.length() ; j++) { + if (s.charAt(j) == 'a') { + word.a++; + } else if (s.charAt(j) == 'b') { + word.b++; + } else if (s.charAt(j) == 'c') { + word.c++; + } else if (s.charAt(j) == 'd') { + word.d++; + } else if (s.charAt(j) == 'e') { + word.e++; + } + + word.total++; + } + + words[i] = word; + } + + int answer = 0; + int main = 0; + int other = 0; + int count = 0; + Arrays.sort(words, new CompA()); + + for (int i = 0 ; i < n ; i++) { + main += words[i].a; + other += words[i].total - words[i].a; + + if (main > other) { + count++; + } else { + break; + } + } + + answer = Math.max(answer, count); + main = 0; + other = 0; + count = 0; + Arrays.sort(words, new CompB()); + + for (int i = 0 ; i < n ; i++) { + main += words[i].b; + other += words[i].total - words[i].b; + + if (main > other) { + count++; + } else { + break; + } + } + + answer = Math.max(answer, count); + main = 0; + other = 0; + count = 0; + Arrays.sort(words, new CompC()); + + for (int i = 0 ; i < n ; i++) { + main += words[i].c; + other += words[i].total - words[i].c; + + if (main > other) { + count++; + } else { + break; + } + } + + answer = Math.max(answer, count); + main = 0; + other = 0; + count = 0; + Arrays.sort(words, new CompD()); + + for (int i = 0 ; i < n ; i++) { + main += words[i].d; + other += words[i].total - words[i].d; + + if (main > other) { + count++; + } else { + break; + } + } + + answer = Math.max(answer, count); + main = 0; + other = 0; + count = 0; + Arrays.sort(words, new CompE()); + + for (int i = 0 ; i < n ; i++) { + main += words[i].e; + other += words[i].total - words[i].e; + + if (main > other) { + count++; + } else { + break; + } + } + + answer = Math.max(answer, count); + System.out.println(answer); + } + + scan.close(); + } +} +",0,Non-plagiarised +0b04b41e,c4ca2ff3,"import java.io.BufferedReader; +import java.io.*; +import java.util.*; + +public class josph { + +static BufferedReader br; + + +static long mod = 1000000000 + 7; +static HashSet p = new HashSet<>(); +static boolean debug =true; +// Arrays.sort(time , (a1,a2) -> (a1[0]-a2[0])); 2d array sort lamda +public static void main(String[] args) throws Exception { +br = new BufferedReader(new InputStreamReader(System.in)); +PrintWriter pr = new PrintWriter(System.out); +int tc = 1; +tc= cinI(); +while(tc-->0){ +int n =cinI(); +String[] a= new String[n]; + +int[][] f =new int[10][n]; + +for(int i=0;i=0;i--){ +sum+=f[j][i]; +if(sum>0){ +cnt+=1; +} +else{ +break; +} +} +max=max(max,cnt); +} +System.out.println(max); +} + +} + +public static void print(String var ,E e){ +if(debug==true){ +System.out.println(var +"" ""+e); +} +} +private static long[] sort(long[] e){ +ArrayList x=new ArrayList<>(); +for(long c:e){ +x.add(c); +} +Collections.sort(x); +long[] y = new long[e.length]; +for(int i=0;i b) +return gcd(a % b, b); +return gcd(a, b % a); +} + + +public static long min(long a, long b) { +return Math.min(a, b); +} + +public static int min(int a, int b) { +return Math.min(a, b); +} + + +public static void sieve() { +int[] pf = new int[100000000 + 1]; +//0 prime //1 not prime +pf[0] = 1; +pf[1] = 1; +for (int j = 2; j <= 10000; j++) { + +if (pf[j] == 0) { +p.add(j); +for (int k = j * j; k < pf.length; k += j) { +pf[k] = 1; +} +} +} + +} + + +public static int[] readArray(int n, int x, int z) throws Exception { +int[] arr = new int[n]; +String[] ar = cinA(); +for (int i = x; i < n + x; i++) { +arr[i] = getI(ar[i - x]); +} +return arr; +} + +public static long[] readArray(int n, int x) throws Exception { +long[] arr = new long[n]; +String[] ar = cinA(); +for (int i = x; i < n + x; i++) { +arr[i] = getL(ar[i - x]); +} +return arr; +} + +public static void arrinit(String[] a, long[] b) throws Exception { +for (int i = 0; i < a.length; i++) { +b[i] = Long.parseLong(a[i]); +} +} + +public static HashSet[] Graph(int n, int edge, int directed) throws Exception { +HashSet[] tree = new HashSet[n]; + +for (int j = 0; j < edge; j++) { + +String[] uv = cinA(); +int u = getI(uv[0]); +int v = getI(uv[1]); +if (directed == 0) { + +tree[v].add(u); +} +tree[u].add(v); +} +return tree; +} + +public static void arrinit(String[] a, int[] b) throws Exception { +for (int i = 0; i < a.length; i++) { +b[i] = Integer.parseInt(a[i]); +} +} + + +static double findRoots(int a, int b, int c) { +// If a is 0, then equation is not +//quadratic, but linear + + +int d = b * b - 4 * a * c; +double sqrt_val = Math.sqrt(Math.abs(d)); + + +// System.out.println(""Roots are real and different \n""); + +return Math.max((double) (-b + sqrt_val) / (2 * a), +(double) (-b - sqrt_val) / (2 * a)); + + +} + +public static String cin() throws Exception { +return br.readLine(); +} + +public static String[] cinA() throws Exception { +return br.readLine().split("" ""); +} + +public static String[] cinA(int x) throws Exception { +return br.readLine().split(""""); +} + +public static String ToString(Long x) { +return Long.toBinaryString(x); +} + +public static void cout(String s) { +System.out.println(s); +} + +public static Integer cinI() throws Exception { +return Integer.parseInt(br.readLine()); +} + +public static int getI(String s) throws Exception { +return Integer.parseInt(s); +} + +public static long getL(String s) throws Exception { +return Long.parseLong(s); +} + +public static long max(long a, long b) { +return Math.max(a, b); +} + +public static int max(int a, int b) { +return Math.max(a, b); +} + +public static void coutI(int x) { +System.out.println(String.valueOf(x)); +} + +public static void coutI(long x) { +System.out.println(String.valueOf(x)); +} + +public static Long cinL() throws Exception { +return Long.parseLong(br.readLine()); +} + +public static void arrInit(String[] arr, int[] arr1) throws Exception { +for (int i = 0; i < arr.length; i++) { +arr1[i] = getI(arr[i]); +} + +} +}","/* package codechef; // don't place package name! */ + +import java.util.*; +import java.lang.*; +import java.io.*; + +/* Name of the class has to be ""Main"" only if the class is public. */ +public class Main +{ + public static void main (String[] args) throws java.lang.Exception + { + // your code goes here + Scanner sc=new Scanner(System.in); + int t=sc.nextInt(); + PrintWriter out=new PrintWriter(System.out); + while(t-->0) { + int n=sc.nextInt(); + int freq[][]=new int[n][5]; + int rem[][]=new int[n][5]; + for(int i=0;i=0;k--) { + if(sum+arr[k]>0) { + sum=sum+arr[k]; + total++; + } + else { + break; + } + } + ans=Math.max(ans,total); + } + out.println(ans); + + } + out.flush(); + out.close(); + } +} + +",0,Non-plagiarised +2ef4c176,fa1bf524,"import java.io.*; +import java.util.*; + +public class Template { + + static int mod = 1000000007; + + public static void main(String[] args) { + FastScanner sc = new FastScanner(); + PrintWriter out = new PrintWriter(System.out); + int yo = sc.nextInt(); + while (yo-- > 0) { + int n = sc.nextInt(); + + Map map = new HashMap<>(); + + for(int i = 0; i < n; i++) { + map.put(i, new Pair(sc.nextLong(),sc.nextLong())); + } + + List> list = new ArrayList<>(); + for(int i = 0; i < n; i++) { + list.add(new ArrayList<>()); + } + for(int i = 0; i < n-1; i++) { + int x = sc.nextInt()-1; + int y = sc.nextInt()-1; + list.get(x).add(y); + list.get(y).add(x); + } + + // l -> 0, r -> 1 + + for(int i = 0; i < 1e5+3; i++) { + for(int j = 0; j < 2; j++) { + dp[i][j] = -1; + } + } + long a1 = dfs(map,list,0,0,-1); + long a2 = dfs(map,list,1,0,-1); + long ans = Math.max(a1, a2); + out.println(ans); + } + out.close(); + } + +// static Map dp = new HashMap<>(); + static long[][] dp = new long[(int)1e5+10][2]; + private static long dfs(Map map, List> list, + int x, int node, int parent) { + + + if(dp[node][x] != -1) { + return dp[node][x]; + } + + List neighbours = list.get(node); + + long ans1 = 0; + if(x == 0) { + ans1 = map.get(node).x; + } + else { + ans1 = map.get(node).y; + } + + + long uAns = 0; + for(int e : neighbours) { + if(e == parent) continue; + long ua1 = dfs(map,list,0,e,node); + long ua2 = dfs(map,list,1,e,node); + // consider 0 + long a1 = ua1 + Math.abs(map.get(e).x-ans1); + // consider 1 + long a2 = ua2 + Math.abs(map.get(e).y-ans1); + uAns += Math.max(a1, a2); + } + + return dp[node][x] = uAns; + } + + static class Pair { + long x; + long y; + + public Pair(long x, long y) { + this.x = x; + this.y = y; + } + } + + static void ruffleSort(int[] a) { + + int n = a.length; + Random r = new Random(); + for (int i = 0; i < a.length; i++) { + int oi = r.nextInt(n), temp = a[i]; + a[i] = a[oi]; + a[oi] = temp; + } + Arrays.sort(a); + } + + static long gcd(long a, long b) { + if (b == 0) + return a; + return gcd(b, a % b); + } + + static boolean[] sieve(int N) { + boolean[] sieve = new boolean[N]; + for (int i = 2; i < N; i++) + sieve[i] = true; + + for (int i = 2; i < N; i++) { + if (sieve[i]) { + if (i * i < 0) + continue; + for (int j = i * i; j < N; j += i) { + sieve[j] = false; + } + } + } + return sieve; + } + + static long pow(int a, long b) { + if (b == 0) { + return 1; + } + if (b == 1) { + return a; + } + if (b % 2 == 0) { + long ans = pow(a, b / 2); + return ans * ans; + } else { + long ans = pow(a, (b - 1) / 2); + return a * ans * ans; + } + + } + + static class FastScanner { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(""""); + + String next() { + while (!st.hasMoreTokens()) + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + int[] readArray(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) + a[i] = nextInt(); + return a; + } + + long nextLong() { + return Long.parseLong(next()); + } + } + + // For Input.txt and Output.txt + // FileInputStream in = new FileInputStream(""input.txt""); + // FileOutputStream out = new FileOutputStream(""output.txt""); + // PrintWriter pw = new PrintWriter(out); + // Scanner sc = new Scanner(in); +} +","import java.util.*; +import java.io.*; + +public class Solution { + + static LinkedList graph[]; + static long key[][]; + static long value[][]; + + public static void main(String[] args) throws IOException { + + Scanner in = new Scanner(System.in); + StringBuffer out=new StringBuffer(); + BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); + + int t=Integer.parseInt(br.readLine()); + String input[]; + + OUTER: + while(t--!=0) { + + int n=Integer.parseInt(br.readLine()); + + value=new long[n][2]; + key=new long[n][2]; + + for(int i=0; i= x + public int lowerIndex(List arr, int n, int x) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr.get(mid) >= x) + h = mid - 1; + else + l = mid + 1; + } + return l; + } + public int lowerIndex(int[] arr, int n, int x) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr[mid] >= x) + h = mid - 1; + else + l = mid + 1; + } + return l; + } + + // function to find last index <= y + public int upperIndex(List arr, int n, int y) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr.get(mid) <= y) + l = mid + 1; + else + h = mid - 1; + } + return h; + } + public int upperIndex(int[] arr, int n, int y) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr[mid] <= y) + l = mid + 1; + else + h = mid - 1; + } + return h; + } + + // function to count elements within given range + public int countInRange(List arr, int n, int x, int y) + { + // initialize result + int count = 0; + count = upperIndex(arr, n, y) - + lowerIndex(arr, n, x) + 1; + return count; + } + + public int _gcd(int a, int b) + { + + if(b == 0) { + return a; + } + else { + return _gcd(b, a % b); + } + } + + public int add(int a, int b){ + a+=b; + if(a>=MOD) a-=MOD; + else if(a<0) a+=MOD; + return a; + } + + public int mul(int a, int b){ + long res= (long)a*(long)b; + return (int)(res%MOD); + } + + public int power(int a, int b) { + int ans=1; + while(b>0){ + if((b&1)!=0) ans= mul(ans,a); + b>>=1; + a= mul(a,a); + } + return ans; + } + + int[] fact= new int[MAXN]; + int[] inv= new int[MAXN]; + + public int Ckn(int n, int k){ + if(k<0 || n<0) return 0; + return mul(mul(fact[n],inv[k]),inv[n-k]); + } + + public int inverse(int a){ + return power(a,MOD-2); + } + + public void preprocess() { + fact[0]=1; + for(int i=1;i=0;i--){ + inv[i]= mul(inv[i+1],i+1); + } + } + + /** + * return VALUE of lower bound for unsorted array + */ + public int lowerBoundNormalArray(int[] arr, int x){ + TreeSet set= new TreeSet<>(); + for(int num: arr) set.add(num); + return set.lower(x); + } + /** + * return VALUE of upper bound for unsorted array + */ + public int upperBoundNormalArray(int[] arr, int x){ + TreeSet set= new TreeSet<>(); + for(int num: arr) set.add(num); + return set.higher(x); + } + + public void debugArr(int[] arr){ + for(int i: arr) out.print(i+"" ""); + out.println(); + } + + public int rand(){ + int min=0, max= MAXN; + int random_int = (int)Math.floor(Math.random()*(max-min+1)+min); + return random_int; + } + + InputReader in; PrintWriter out; + static int mod = 1000000007; + int ans = 0; + ArrayListg[] = new ArrayList[101]; + int cnt[][] = new int[101][101]; + int dp[][] = new int[101][101]; + int Add(int a, int b){ + a+=b; while (a>=mod)a-=mod; + return a; + } + int Mul(int a, int b){ + return (int)(((long)a * b) % mod); + } + void dfs(int node, int par, int root, int depth){ + cnt[root][depth]++; + for (int v : g[node]){ + if (v != par){ + dfs(v, node, root, depth+1); + } + } + } + public void solve(InputReader in, PrintWriter out) { + int t = in.nextInt(); + while (t-- > 0){ + int n = in.nextInt(); + String[] str = new String[n]; + ArrayList diff[] = new ArrayList[5]; + for (int i = 0; i < 5; i++) diff[i] = new ArrayList<>(); + for (int i = 0; i < n; i++){ + str[i] = in.nextToken(); + int[] cnt = new int[5]; + for (int j = 0; j < str[i].length(); j++){ + cnt[str[i].charAt(j) - 'a']++; + } + for (int j = 0; j < 5; j++){ + diff[j].add(cnt[j] * 2 - str[i].length()); + } + } + int ans = 0; + for (int i = 0; i < 5; i++){ + Collections.sort(diff[i]); + Collections.reverse(diff[i]); + int cur = 0, x = 0; + for (int j = 0; j < diff[i].size(); j++){ + cur+=diff[i].get(j); + if (cur <= 0){ + break; + } + x++; + } + ans = Math.max(ans, x); + } + out.println(ans); + } + } + + class SEG { + int n; + int[] segs; + public SEG (int[] a){ + this.n= a.length; + segs= new int[4*this.n]; + build(a,0,0,this.n-1); + } + public void build(int[] a, int root, int l, int r){ + if(l==r){ + segs[root]=a[l]; + return; + } + int m= (l+r)/2; + build(a,2*root+1,l,m); + build(a,2*root+2,m+1,r); + segs[root]= _gcd(segs[2*root+1], segs[2*root+2]); + } + public int query(int root, int l, int r, int lq, int rq){ + if(lq<=l && rq>=r) return segs[root]; + if(lq>r || rq m= new HashMap<>(); + public long base=0; + public long totalValue=0; + private int M= 1000000007; + + private long addMod(long a, long b){ + a+=b; + if(a>=M) a-=M; + return a; + } + + public void reset(){ + m= new HashMap<>(); + base=0; + totalValue=0; + } + public void update(long add){ + base= base+ add; + } + public void add(long key, long val){ + long newKey= key-base; + m.put(newKey, addMod(m.getOrDefault(newKey,(long)0),val)); + } + } + + + + static class Tuple implements Comparable{ + int x, y, z; + public Tuple(int x, int y, int z){ + this.x= x; + this.y= y; + this.z=z; + } + @Override + public int compareTo(Tuple o){ + return this.x-o.x; + } + } + + static class Pair implements Comparable{ + public int x; + public int y; + public Pair(int x, int y){ + this.x= x; + this.y= y; + } + + @Override + public int compareTo(Pair o) { + return this.x-o.x; + } + } + + // public static class compareL implements Comparator{ + // @Override + // public int compare(Tuple t1, Tuple t2) { + // return t2.l - t1.l; + // } + // } + + // fast input reader class; + static class InputReader { + BufferedReader br; + StringTokenizer st; + + public InputReader(InputStream stream) { + br = new BufferedReader(new InputStreamReader(stream)); + } + + public String nextToken() { + while (st == null || !st.hasMoreTokens()) { + String line = null; + try { + line = br.readLine(); + } catch (IOException e) { + throw new RuntimeException(e); + } + if (line == null) { + return null; + } + st = new StringTokenizer(line); + } + return st.nextToken(); + } + + public int nextInt() { + return Integer.parseInt(nextToken()); + } + public double nextDouble(){ + return Double.parseDouble(nextToken()); + } + public long nextLong(){ + return Long.parseLong(nextToken()); + } + public int[] nextIntArr(int n){ + int[] arr= new int[n]; + for(int i=0;i nextIntList(int n){ + List arr= new ArrayList<>(); + for(int i=0;i> nextIntMatList(int n, int m){ + List> mat= new ArrayList<>(); + for(int i=0;i temp= new ArrayList<>(); + for(int j=0;j0){ + int n = sc.nextInt(); + String[] strArr = new String[n]; + for(int i=0; i al = new ArrayList<>(); + for(int i=0; i all = new ArrayList<>(); + for(int j=0; j(); +// sieveOfEratosthenes((int)1e6+5); + while(tc-->0) + { + int n=sc.nextInt(); + int k[]=new int[n]; + int h[]=new int[n]; + for(int i=0;i interval=new ArrayList(); + ArrayList act=new ArrayList(); + for(int i=0;i=x; + } + + static void arrInt(int n) throws IOException + { + arInt=new int[n]; + for (int i = 0; i < arInt.length; i++) { + arInt[i]=sc.nextInt(); + } + } + + static void arrLong(int n) throws IOException + { + arLong=new long[n]; + for (int i = 0; i < arLong.length; i++) { + arLong[i]=sc.nextLong(); + } + } + + + + static ArrayList add(int id,int c) + { + ArrayList newArr=new ArrayList<>(); + for(int i=0;i arr, int n, int y) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr.get(mid) <= y) + l = mid + 1; + else + h = mid - 1; + } + return h; + } + + + + static int lower(ArrayList arr, int n, int x) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr.get(mid) >= x) + h = mid - 1; + else + l = mid + 1; + } + return l; + } + + + + + + static int N = 501; + + // Array to store inverse of 1 to N + static long[] factorialNumInverse = new long[N + 1]; + + // Array to precompute inverse of 1! to N! + static long[] naturalNumInverse = new long[N + 1]; + + // Array to store factorial of first N numbers + static long[] fact = new long[N + 1]; + + // Function to precompute inverse of numbers + public static void InverseofNumber(int p) + { + naturalNumInverse[0] = naturalNumInverse[1] = 1; + + for(int i = 2; i <= N; i++) + naturalNumInverse[i] = naturalNumInverse[p % i] * + (long)(p - p / i) % p; + } + + // Function to precompute inverse of factorials + public static void InverseofFactorial(int p) + { + factorialNumInverse[0] = factorialNumInverse[1] = 1; + + // Precompute inverse of natural numbers + for(int i = 2; i <= N; i++) + factorialNumInverse[i] = (naturalNumInverse[i] * + factorialNumInverse[i - 1]) % p; + } + + // Function to calculate factorial of 1 to N + public static void factorial(int p) + { + fact[0] = 1; + + // Precompute factorials + for(int i = 1; i <= N; i++) + { + fact[i] = (fact[i - 1] * (long)i) % p; + } + } + + // Function to return nCr % p in O(1) time + public static long Binomial(int N, int R, int p) + { + + // n C r = n!*inverse(r!)*inverse((n-r)!) + long ans = ((fact[N] * factorialNumInverse[R]) % + p * factorialNumInverse[N - R]) % p; + + return ans; + } + + + static String tr(String s) + { + int now = 0; + while (now + 1 < s.length() && s.charAt(now)== '0') + ++now; + return s.substring(now); + } + + static ArrayList ans; + static void dfs(int node,Graph gg,int cnt,int k,ArrayList temp) + { + if(cnt==k) + return; + + for(Integer each:gg.list[node]) + { + if(each==0) + { + temp.add(each); + ans=new ArrayList<>(temp); + temp.remove(temp.size()-1); + continue; + } + temp.add(each); + dfs(each,gg,cnt+1,k,temp); + temp.remove(temp.size()-1); + + } + + return; + } + + + static boolean isPrime(long n) + { + // Corner cases + if (n <= 1) + return false; + if (n <= 3) + return true; + + // This is checked so that we can skip + // middle five numbers in below loop + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; + } + + + static ArrayList commDiv(int a, int b) + { + // find gcd of a, b + int n = gcd(a, b); + + // Count divisors of n. + ArrayList Div=new ArrayList<>(); + for (int i = 1; i <= Math.sqrt(n); i++) { + // if 'i' is factor of n + if (n % i == 0) { + // check if divisors are equal + if (n / i == i) + Div.add(i); + else + { + Div.add(i); + Div.add(n/i); + } + } + } + return Div; + } + + static HashSet factors(int x) + { + HashSet a=new HashSet(); + for(int i=2;i*i<=x;i++) + { + if(x%i==0) + { + a.add(i); + a.add(x/i); + } + } + return a; + } + + +static class Node +{ + int vertex; + HashSet adj; + boolean rem; + Node(int ver) + { + vertex=ver; + rem=false; + adj=new HashSet(); + } + @Override + public String toString() + { + return vertex+"" ""; + } +} + + static class Tuple{ + int a; + int b; + int c; + public Tuple(int a,int b,int c) { + this.a=a; + this.b=b; + this.c=c; + } + + } + + //function to find prime factors of n + static HashMap findFactors(long n2) + { + HashMap ans=new HashMap<>(); + if(n2%2==0) + { + ans.put(2L, 0L); +// cnt++; + while((n2&1)==0) + { + n2=n2>>1; + ans.put(2L, ans.get(2L)+1); +// + } + + } + for(long i=3;i*i<=n2;i+=2) + { + if(n2%i==0) + { + ans.put((long)i, 0L); +// cnt++; + while(n2%i==0) + { + n2=n2/i; + ans.put((long)i, ans.get((long)i)+1); + + } + } + } + if(n2!=1) + { + ans.put(n2, ans.getOrDefault(n2, (long) 0)+1); + + } + + return ans; + } + + + //fenwick tree implementaion + static class fwt + { + int n; + long BITree[]; + fwt(int n) + { + this.n=n; + BITree=new long[n+1]; + } + + fwt(int arr[], int n) + { + this.n=n; + BITree=new long[n+1]; + for(int i = 0; i < n; i++) + updateBIT(n, i, arr[i]); + } + + long getSum(int index) + { + long sum = 0; + index = index + 1; + while(index>0) + { + sum += BITree[index]; + index -= index & (-index); + } + return sum; + } + void updateBIT(int n, int index,int val) + { + index = index + 1; + while(index <= n) + { + BITree[index] += val; + index += index & (-index); + } + } + void print() + { + for(int i=0;i primes; + static HashSet primeSet; + static boolean prime[]; + static void sieveOfEratosthenes(int n) + { + // Create a boolean array + // ""prime[0..n]"" and + // initialize all entries + // it as true. A value in + // prime[i] will finally be + // false if i is Not a + // prime, else true. + prime= new boolean[n + 1]; + for (int i = 2; i <= n; i++) + prime[i] = true; + + for (int p = 2; p * p <= n; p++) + { + // If prime[p] is not changed, then it is a + // prime + if (prime[p] == true) + { + // Update all multiples of p + for (int i = p * p; i <= n; i += p) + prime[i] = false; + } + } + + // Print all prime numbers + for (int i = 2; i <= n; i++) + { + if (prime[i] == true) + primeSet.add(i); + } + } + + + + static long mod(long a, long b) { + long c = a % b; + return (c < 0) ? c + b : c; + } + + static void swap(long arr[],int i,int j) + { + long temp=arr[i]; + arr[i]=arr[j]; + arr[j]=temp; + } + + + + static boolean util(int a,int b,int c) + { + if(b>a)util(b, a, c); + + while(c>=a) + { + c-=a; + if(c%b==0) + return true; + } + + + return (c%b==0); + } + + + + static void flag(boolean flag) + { + out.println(flag ? ""YES"" : ""NO""); + out.flush(); + } + + + static void print(int a[]) + { + int n=a.length; + for(int i=0;i al) + { + int si=al.size(); + for(int i=0;i al) + { + int si=al.size(); + for(int i=0;i>>1; + if(a[m]>=x) r=m; + else l=m; + } + return r; + } + + + static int lowerIndex(int arr[], int n, int x) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr[mid] >= x) + h = mid - 1; + else + l = mid + 1; + } + return l; + } + + + // function to find last index <= y + static int upperIndex(int arr[], int n, int y) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr[mid] <= y) + l = mid + 1; + else + h = mid - 1; + } + return h; + } + static int upperIndex(long arr[], int n, long y) + { + int l = 0, h = n - 1; + while (l <= h) + { + int mid = (l + h) / 2; + if (arr[mid] <= y) + l = mid + 1; + else + h = mid - 1; + } + return h; + } + + + static int UpperBound(int a[], int x) + {// x is the key or target value + int l=-1,r=a.length; + while(l+1>>1; + if(a[m]<=x) l=m; + else r=m; + } + return l+1; + } + static int UpperBound(long a[], long x) + {// x is the key or target value + int l=-1,r=a.length; + while(l+1>>1; + if(a[m]<=x) l=m; + else r=m; + } + return l+1; + } + + + static class DisjointUnionSets + { + int[] rank, parent; + int n; + // Constructor + public DisjointUnionSets(int n) + { + rank = new int[n]; + parent = new int[n]; + this.n = n; + makeSet(); + } + // Creates n sets with single item in each + void makeSet() + { + for (int i = 0; i < n; i++) + parent[i] = i; + } + int find(int x) + { + if (parent[x] != x) { + parent[x] = find(parent[x]); + } + return parent[x]; + } + // Unites the set that includes x and the set + // that includes x + void union(int x, int y) + { + int xRoot = find(x), yRoot = find(y); + if (xRoot == yRoot) + return; + if (rank[xRoot] < rank[yRoot]) + parent[xRoot] = yRoot; + else if (rank[yRoot] < rank[xRoot]) + parent[yRoot] = xRoot; + else // if ranks are the same + { + parent[yRoot] = xRoot; + rank[xRoot] = rank[xRoot] + 1; + } + +// if(xRoot!=yRoot) +// parent[y]=x; + } + + int connectedComponents() + { + int cnt=0; + for(int i=0;i list[]; + Graph(int v) + { + this.v=v; + list=new ArrayList[v+1]; + for(int i=1;i<=v;i++) + list[i]=new ArrayList(); + } + void addEdge(int a, int b) + { + this.list[a].add(b); + } + } + + + +// static class GraphMap{ +// Map> graph; +// GraphMap() { +// // TODO Auto-generated constructor stub +// graph=new HashMap>(); +// +// } +// void addEdge(String a,String b) +// { +// if(graph.containsKey(a)) +// this.graph.get(a).add(b); +// else { +// this.graph.put(a, new ArrayList<>()); +// this.graph.get(a).add(b); +// } +// } +// } +// static void dfsMap(GraphMap g,HashSet vis,String src,int ok) +// { +// vis.add(src); +// +// if(g.graph.get(src)!=null) +// { +// for(String each:g.graph.get(src)) +// { +// if(!vis.contains(each)) +// { +// dfsMap(g, vis, each, ok+1); +// } +// } +// } +// +// cnt=Math.max(cnt, ok); +// } + static double sum[]; + static long cnt; +// static void DFS(Graph g, boolean[] visited, int u) +// { +// visited[u]=true; +// +// for(int i=0;i + { + int x; + int y; + Pair(int x,int y) + { + this.x=x; + this.y=y; + + } + @Override + public int compareTo(Pair o) { + // TODO Auto-generated method stub + return this.x-o.x; + } + + + + } + + static long sum_array(int a[]) + { + int n=a.length; + long sum=0; + for(int i=0;i l=new ArrayList<>(); + for (int i:a) l.add(i); + Collections.sort(l); + for (int i=0; i l=new ArrayList<>(); + for (long i:a) l.add(i); + Collections.sort(l); + for (int i=0; i 0) + { + if ((y & 1) != 0) + res = (res * x) % mod; + + y = y >> 1; // y = y/2 + x = (x * x) % mod; + } + return res; + } + static int power(int x, int y) + { + int res = 1; + x = x % mod; + if (x == 0) + return 0; + while (y > 0) + { + if ((y & 1) != 0) + res = (res * x) % mod; + + y = y >> 1; // y = y/2 + x = (x * x) % mod; + } + return res; + } + static long gcd(long a, long b) + { + + if (a == 0) + return b; + //cnt+=a/b; + return gcd(b%a,a); + } + static int gcd(int a, int b) + { + if (a == 0) + return b; + + return gcd(b%a, a); + } + + static class FastReader{ + + byte[] buf = new byte[2048]; + int index, total; + InputStream in; + + FastReader(InputStream is) { + in = is; + } + + int scan() throws IOException { + if (index >= total) { + index = 0; + total = in.read(buf); + if (total <= 0) { + return -1; + } + } + return buf[index++]; + } + + String next() throws IOException { + int c; + for (c = scan(); c <= 32; c = scan()); + StringBuilder sb = new StringBuilder(); + for (; c > 32; c = scan()) { + sb.append((char) c); + } + return sb.toString(); + } + + int nextInt() throws IOException { + int c, val = 0; + for (c = scan(); c <= 32; c = scan()); + boolean neg = c == '-'; + if (c == '-' || c == '+') { + c = scan(); + } + for (; c >= '0' && c <= '9'; c = scan()) { + val = (val << 3) + (val << 1) + (c & 15); + } + return neg ? -val : val; + } + + long nextLong() throws IOException { + int c; + long val = 0; + for (c = scan(); c <= 32; c = scan()); + boolean neg = c == '-'; + if (c == '-' || c == '+') { + c = scan(); + } + for (; c >= '0' && c <= '9'; c = scan()) { + val = (val << 3) + (val << 1) + (c & 15); + } + return neg ? -val : val; + } + } + + static class Reader + { + final private int BUFFER_SIZE = 1 << 16; + private DataInputStream din; + private byte[] buffer; + private int bufferPointer, bytesRead; + + public Reader() + { + din = new DataInputStream(System.in); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public Reader(String file_name) throws IOException + { + din = new DataInputStream(new FileInputStream(file_name)); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public String readLine() throws IOException + { + byte[] buf = new byte[64]; // line length + int cnt = 0, c; + while ((c = read()) != -1) + { + if (c == '\n') + break; + buf[cnt++] = (byte) c; + } + return new String(buf, 0, cnt); + } + + public int nextInt() throws IOException + { + int ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do + { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (neg) + return -ret; + return ret; + } + + public long nextLong() throws IOException + { + long ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } + while ((c = read()) >= '0' && c <= '9'); + if (neg) + return -ret; + return ret; + } + + public double nextDouble() throws IOException + { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + + do { + ret = ret * 10 + c - '0'; + } + while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') + { + while ((c = read()) >= '0' && c <= '9') + { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) + return -ret; + return ret; + } + + private void fillBuffer() throws IOException + { + bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); + if (bytesRead == -1) + buffer[0] = -1; + } + + private byte read() throws IOException + { + if (bufferPointer == bytesRead) + fillBuffer(); + return buffer[bufferPointer++]; + } + + public void close() throws IOException + { + if (din == null) + return; + din.close(); + } + } + static PrintWriter out=new PrintWriter(System.out); + static int int_max=Integer.MAX_VALUE; + static int int_min=Integer.MIN_VALUE; + static long long_max=Long.MAX_VALUE; + static long long_min=Long.MIN_VALUE; + +} + + + + +","import java.io.*; +import java.util.*; + + +public class Practice +{ +// static final long mod=7420738134811L; + static int mod=1000000007; + static final int size=501; + static FastReader sc=new FastReader(System.in); +// static Reader sc=new Reader(); +// static Scanner sc=new Scanner(System.in); + static PrintWriter out=new PrintWriter(System.out); + static long[] factorialNumInverse; + static long[] naturalNumInverse; + static int[] sp; + static long[] fact; + static ArrayList pr; + public static void main(String[] args) throws IOException, CloneNotSupportedException + { +// System.setIn(new FileInputStream(""input.txt"")); +// System.setOut(new PrintStream(""output.txt"")); +// factorial(mod); +// InverseofNumber(mod); +// InverseofFactorial(mod); +// make_seive(); + int t=1; + t=sc.nextInt(); + for(int i=1;i<=t;i++) + solve(i); + out.close(); + out.flush(); +// System.out.flush(); +// System.exit(0); + } + + static void solve(int CASENO) throws IOException, CloneNotSupportedException + { + int n=sc.nextInt(); + int k[]=new int[n]; + int h[]=new int[n]; + for(int i=0;i interval=new ArrayList(); + ArrayList act=new ArrayList(); + for(int i=0;i + { + int x,y; + Pair(int a,int b) + { + this.x=a; + this.y=b; + } + @Override + public boolean equals(Object obj) + { + if(obj instanceof Pair) + { + Pair p=(Pair)obj; + return p.x==this.x && p.y==this.y; + } + return false; + } + @Override + public int hashCode() + { + return Math.abs(x)+500*Math.abs(y); + } + @Override + public String toString() + { + return ""(""+x+"" ""+y+"")""; + } + @Override + protected Pair clone() throws CloneNotSupportedException { + return new Pair(this.x,this.y); + } + @Override + public int compareTo(Pair a) + { + long t=(this.x-a.x); + if(t!=0) + return t>0?1:-1; + else + return (int)(this.y-a.y); + } + public void swap() + { + this.y=this.y+this.x; + this.x=this.y-this.x; + this.y=this.y-this.x; + } + } + static class Tuple implements Cloneable, Comparable + { + int x,y,z; + Tuple(int a,int b,int c) + { + this.x=a; + this.y=b; + this.z=c; + } + public boolean equals(Object obj) + { + if(obj instanceof Tuple) + { + Tuple p=(Tuple)obj; + return p.x==this.x && p.y==this.y && p.z==this.z; + } + return false; + } + @Override + public int hashCode() + { + return (this.x+501*this.y); + } + @Override + public String toString() + { + return ""(""+x+"",""+y+"",""+z+"")""; + } + @Override + protected Tuple clone() throws CloneNotSupportedException { + return new Tuple(this.x,this.y,this.z); + } + @Override + public int compareTo(Tuple a) + { + int x=this.z-a.z; + if(x!=0) + return x; + int X= this.x-a.x; + if(X!=0) + return X; + return a.y-this.y; + } + } + static void arraySort(int arr[]) + { + ArrayList a=new ArrayList(); + for (int i = 0; i < arr.length; i++) { + a.add(arr[i]); + } + Collections.sort(a,Comparator.reverseOrder()); + for (int i = 0; i < arr.length; i++) { + arr[i]=a.get(i); + } + } + static void arraySort(long arr[]) + { + ArrayList a=new ArrayList(); + for (int i = 0; i < arr.length; i++) { + a.add(arr[i]); + } + Collections.sort(a); + for (int i = 0; i < arr.length; i++) { + arr[i]=a.get(i); + } + } + static HashSet primeFactors(int n) + { + HashSet ans=new HashSet(); + if(n%2==0) + { + ans.add(2); + while((n&1)==0) + n=n>>1; + } + for(int i=3;i*i<=n;i+=2) + { + if(n%i==0) + { + ans.add(i); + while(n%i==0) + n=n/i; + } + } + if(n!=1) + ans.add(n); + return ans; + } + static void make_seive() + { + sp=new int[size]; + pr=new ArrayList(); + for (int i=2; i adj; + Node(int ver) + { + vertex=ver; + dis=-1; + adj=new HashSet(); + } + @Override + public String toString() + { + return vertex+"" ""; + } + } + static class Edge + { + Node to; + int cost; + Edge(Node t,int c) + { + this.to=t; + this.cost=c; + } + @Override + public String toString() { + return ""(""+to.vertex+"",""+cost+"") ""; + } + } + static long power(long x, long y) + { + if(x<=0) + return 1; + long res = 1; + x = x % mod; + if (x == 0) + return 0; + while (y > 0) + { + if ((y & 1) != 0) + res = (res * x) % mod; + + y = y >> 1; // y = y/2 + x = (x * x) % mod; + } + return res%mod; + } + static long binomialCoeff(long n, long k) + { + if(n n - k) + k = n - k; + + // Calculate value of + // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] + for (long i = 0; i < k; ++i) { + res *= (n - i); + res /= (i + 1); + } + + return res; + } + static class FastReader + { + byte[] buf = new byte[2048]; + int index, total; + InputStream in; + + FastReader(InputStream is) + { + in = is; + } + + int scan() throws IOException + { + if (index >= total) { + index = 0; + total = in.read(buf); + if (total <= 0) { + return -1; + } + } + return buf[index++]; + } + + String next() throws IOException + { + int c; + for (c = scan(); c <= 32; c = scan()); + StringBuilder sb = new StringBuilder(); + for (; c > 32; c = scan()) { + sb.append((char) c); + } + return sb.toString(); + } + + int nextInt() throws IOException + { + int c, val = 0; + for (c = scan(); c <= 32; c = scan()); + boolean neg = c == '-'; + if (c == '-' || c == '+') { + c = scan(); + } + for (; c >= '0' && c <= '9'; c = scan()) { + val = (val << 3) + (val << 1) + (c & 15); + } + return neg ? -val : val; + } + + long nextLong() throws IOException + { + int c; + long val = 0; + for (c = scan(); c <= 32; c = scan()); + boolean neg = c == '-'; + if (c == '-' || c == '+') { + c = scan(); + } + for (; c >= '0' && c <= '9'; c = scan()) { + val = (val << 3) + (val << 1) + (c & 15); + } + return neg ? -val : val; + } + } + static class Reader + { + final private int BUFFER_SIZE = 1 << 16; + private DataInputStream din; + private byte[] buffer; + private int bufferPointer, bytesRead; + + public Reader() + { + din = new DataInputStream(System.in); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public Reader(String file_name) throws IOException + { + din = new DataInputStream( + new FileInputStream(file_name)); + buffer = new byte[BUFFER_SIZE]; + bufferPointer = bytesRead = 0; + } + + public String readLine() throws IOException + { + byte[] buf = new byte[64]; // line length + int cnt = 0, c; + while ((c = read()) != -1) { + if (c == '\n') { + if (cnt != 0) { + break; + } + else { + continue; + } + } + buf[cnt++] = (byte)c; + } + return new String(buf, 0, cnt); + } + + public int nextInt() throws IOException + { + int ret = 0; + byte c = read(); + while (c <= ' ') { + c = read(); + } + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (neg) + return -ret; + return ret; + } + + public long nextLong() throws IOException + { + long ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + if (neg) + return -ret; + return ret; + } + + public double nextDouble() throws IOException + { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + + do { + ret = ret * 10 + c - '0'; + } while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') { + while ((c = read()) >= '0' && c <= '9') { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) + return -ret; + return ret; + } + + private void fillBuffer() throws IOException + { + bytesRead = din.read(buffer, bufferPointer = 0, + BUFFER_SIZE); + if (bytesRead == -1) + buffer[0] = -1; + } + + private byte read() throws IOException + { + if (bufferPointer == bytesRead) + fillBuffer(); + return buffer[bufferPointer++]; + } + + public void close() throws IOException + { + if (din == null) + return; + din.close(); + } + public void printarray(int arr[]) + { + for (int i = 0; i < arr.length; i++) + System.out.print(arr[i]+"" ""); + System.out.println(); + } + } +} + + + + + + + +",1,Plagiarised +0f6ca337,6843cc19,"import java.io.*; +import java.util.*; + +import java.math.*; +import java.awt.Point; + +public class Main { + static final long MOD = 1000000007L; + //static final long MOD2 = 1000000009L; + //static final long MOD = 998244353L; + //static final long INF = 500000000000L; + static final int INF = 1000000005; + static final int NINF = -1000000005; + //static final long INF = 1000000000000000000L; + static FastScanner sc; + static PrintWriter pw; + static final int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}}; + + public static void main(String[] args) { + sc = new FastScanner(); + pw = new PrintWriter(System.out); + + + int Q = sc.ni(); + for (int q = 0; q < Q; q++) { + int N = sc.ni(); + int[] nums = sc.intArray(N,0); + Integer[] less = new Integer[N]; + Integer[] more = new Integer[N]; + TreeSet ts = new TreeSet(); + for (int i = 0; i < N; i++) { + ts.add(nums[i]); + less[i] = ts.lower(nums[i]); + more[i] = ts.higher(nums[i]); + } + + String ans = ""YES""; + for (int i = 1; i < N; i++) { + if (nums[i-1]==nums[i]) continue; + + if (nums[i-1] < nums[i]) { + if (more[i-1]!=null && more[i-1] < nums[i]) { + ans = ""NO""; + break; + } + } else { + if (less[i-1]!=null && less[i-1] > nums[i]) { + ans = ""NO""; + break; + } + } + } + pw.println(ans); + } + pw.close(); + } + + + public static void sort(int[] arr) { + Random rgen = new Random(); + for (int i = 0; i < arr.length; i++) { + int r = rgen.nextInt(arr.length); + int temp = arr[i]; + arr[i] = arr[r]; + arr[r] = temp; + } + Arrays.sort(arr); + } + + public static void sort(long[] arr) { + Random rgen = new Random(); + for (int i = 0; i < arr.length; i++) { + int r = rgen.nextInt(arr.length); + long temp = arr[i]; + arr[i] = arr[r]; + arr[r] = temp; + } + Arrays.sort(arr); + } + + //Sort an array (immune to quicksort TLE) + public static void sort(int[][] arr) { + Random rgen = new Random(); + for (int i = 0; i < arr.length; i++) { + int r = rgen.nextInt(arr.length); + int[] temp = arr[i]; + arr[i] = arr[r]; + arr[r] = temp; + } + Arrays.sort(arr, new Comparator() { + @Override + public int compare(int[] a, int[] b) { + return a[0]-b[0]; + } + }); + } + + public static void sort(long[][] arr) { + Random rgen = new Random(); + for (int i = 0; i < arr.length; i++) { + int r = rgen.nextInt(arr.length); + long[] temp = arr[i]; + arr[i] = arr[r]; + arr[r] = temp; + } + Arrays.sort(arr, new Comparator() { + @Override + public int compare(long[] a, long[] b) { + if (a[0] > b[0]) + return 1; + else if (a[0] < b[0]) + return -1; + else + return 0; + //Ascending order. + } + }); + } + + static class FastScanner { + BufferedReader br; + StringTokenizer st; + + public FastScanner() { + br = new BufferedReader(new InputStreamReader(System.in), 32768); + st = null; + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int ni() { + return Integer.parseInt(next()); + } + + int[][] graph(int N, int[][] edges) { + int[][] graph = new int[N][]; + int[] sz = new int[N]; + for (int[] e: edges) { + sz[e[0]] += 1; + sz[e[1]] += 1; + } + for (int i = 0; i < N; i++) { + graph[i] = new int[sz[i]]; + } + int[] cur = new int[N]; + for (int[] e: edges) { + graph[e[0]][cur[e[0]]] = e[1]; + graph[e[1]][cur[e[1]]] = e[0]; + cur[e[0]] += 1; + cur[e[1]] += 1; + } + return graph; + } + + int[] intArray(int N, int mod) { + int[] ret = new int[N]; + for (int i = 0; i < N; i++) + ret[i] = ni()+mod; + return ret; + } + + char[] charArray(int N) { + char[] ret = new char[N]; + for (int i = 0; i < N; i++) + ret[i] = next().charAt(0); + return ret; + } + + long nl() { + return Long.parseLong(next()); + } + + long[] longArray(int N, long mod) { + long[] ret = new long[N]; + for (int i = 0; i < N; i++) + ret[i] = nl()+mod; + return ret; + } + + double nd() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } +}","import java.io.*; +import java.util.*; + +import java.math.*; +import java.awt.Point; + +public class Main { + static final long MOD = 1000000007L; + //static final long MOD2 = 1000000009L; + //static final long MOD = 998244353L; + //static final long INF = 500000000000L; + static final int INF = 1000000005; + static final int NINF = -1000000005; + //static final long INF = 1000000000000000000L; + static FastScanner sc; + static PrintWriter pw; + static final int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}}; + + public static void main(String[] args) { + sc = new FastScanner(); + pw = new PrintWriter(System.out); + + + int Q = sc.ni(); + for (int q = 0; q < Q; q++) { + int N = sc.ni(); + int[] nums = sc.intArray(N,0); + Integer[] less = new Integer[N]; + Integer[] more = new Integer[N]; + TreeSet ts = new TreeSet(); + for (int i = 0; i < N; i++) { + ts.add(nums[i]); + less[i] = ts.lower(nums[i]); + more[i] = ts.higher(nums[i]); + } + + String ans = ""YES""; + for (int i = 1; i < N; i++) { + if (nums[i-1]==nums[i]) continue; + + if (nums[i-1] < nums[i]) { + if (more[i-1]!=null && more[i-1] < nums[i]) { + ans = ""NO""; + break; + } + } else { + if (less[i-1]!=null && less[i-1] > nums[i]) { + ans = ""NO""; + break; + } + } + } + pw.println(ans); + } + pw.close(); + } + + + public static void sort(int[] arr) { + Random rgen = new Random(); + for (int i = 0; i < arr.length; i++) { + int r = rgen.nextInt(arr.length); + int temp = arr[i]; + arr[i] = arr[r]; + arr[r] = temp; + } + Arrays.sort(arr); + } + + public static void sort(long[] arr) { + Random rgen = new Random(); + for (int i = 0; i < arr.length; i++) { + int r = rgen.nextInt(arr.length); + long temp = arr[i]; + arr[i] = arr[r]; + arr[r] = temp; + } + Arrays.sort(arr); + } + + //Sort an array (immune to quicksort TLE) + public static void sort(int[][] arr) { + Random rgen = new Random(); + for (int i = 0; i < arr.length; i++) { + int r = rgen.nextInt(arr.length); + int[] temp = arr[i]; + arr[i] = arr[r]; + arr[r] = temp; + } + Arrays.sort(arr, new Comparator() { + @Override + public int compare(int[] a, int[] b) { + return a[0]-b[0]; + } + }); + } + + public static void sort(long[][] arr) { + Random rgen = new Random(); + for (int i = 0; i < arr.length; i++) { + int r = rgen.nextInt(arr.length); + long[] temp = arr[i]; + arr[i] = arr[r]; + arr[r] = temp; + } + Arrays.sort(arr, new Comparator() { + @Override + public int compare(long[] a, long[] b) { + if (a[0] > b[0]) + return 1; + else if (a[0] < b[0]) + return -1; + else + return 0; + //Ascending order. + } + }); + } + + static class FastScanner { + BufferedReader br; + StringTokenizer st; + + public FastScanner() { + br = new BufferedReader(new InputStreamReader(System.in), 32768); + st = null; + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int ni() { + return Integer.parseInt(next()); + } + + int[][] graph(int N, int[][] edges) { + int[][] graph = new int[N][]; + int[] sz = new int[N]; + for (int[] e: edges) { + sz[e[0]] += 1; + sz[e[1]] += 1; + } + for (int i = 0; i < N; i++) { + graph[i] = new int[sz[i]]; + } + int[] cur = new int[N]; + for (int[] e: edges) { + graph[e[0]][cur[e[0]]] = e[1]; + graph[e[1]][cur[e[1]]] = e[0]; + cur[e[0]] += 1; + cur[e[1]] += 1; + } + return graph; + } + + int[] intArray(int N, int mod) { + int[] ret = new int[N]; + for (int i = 0; i < N; i++) + ret[i] = ni()+mod; + return ret; + } + + char[] charArray(int N) { + char[] ret = new char[N]; + for (int i = 0; i < N; i++) + ret[i] = next().charAt(0); + return ret; + } + + long nl() { + return Long.parseLong(next()); + } + + long[] longArray(int N, long mod) { + long[] ret = new long[N]; + for (int i = 0; i < N; i++) + ret[i] = nl()+mod; + return ret; + } + + double nd() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } +}",1,Plagiarised +120878da,ac180326,"import java.util.*; +import java.lang.*; +import java.io.*; + +public class codeforces +{ + public static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader() + { + br = new BufferedReader( + new InputStreamReader(System.in)); + } + + String next() + { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } + catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { return Integer.parseInt(next()); } + + long nextLong() { return Long.parseLong(next()); } + + double nextDouble() + { + return Double.parseDouble(next()); + } + + String nextLine() + { + String str = """"; + try { + str = br.readLine(); + } + catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } + public static void main (String[] args) throws java.lang.Exception + { + // your code goes here + FastReader sc=new FastReader(); + int t=sc.nextInt(); + while(t-->0) + { + int n=sc.nextInt(); + int m=sc.nextInt(); + int x=sc.nextInt(); + PriorityQueue pq=new PriorityQueue<>(new myComp()); + myarr a[]=new myarr[n]; + int b[]=new int[n]; + // int c[]=new int[m]; + for(int i=0;i + { + public int compare(pair p1,pair p2) + { + if(p1.b==p2.b) + return 0; + else if(p1.b + { + public int compare(myarr a,myarr b) + { + if(a.val==b.val) + return 0; + else if(a.val 0) { + + int n = fr.nextInt(), m = fr.nextInt(), x = fr.nextInt(); + + ArrayList pp = new ArrayList<>(); + + int A[] = new int[n]; + + for (int i = 0; i < n; i++) { + A[i] = fr.nextInt(); + Pair pr = new Pair(A[i], i); + pp.add(pr); + } + + Collections.sort(pp); + Collections.reverse(pp); + + int ps[] = new int[n]; + int pk[] = new int[n]; + Arrays.fill(ps, 0); + Arrays.fill(pk, 0); + + int index = 0; + + for (int i = 0; i < n; i++) { + + if (pk[index] < x) { + pk[index] += pp.get(i).a; + } + ps[pp.get(i).b] = index + 1; + index++; + + index = index == m ? 0 : index; + + } + + pt.println(""YES""); + + for (int i = 0; i < n; i++) { + pt.print(ps[i] + "" ""); + } + pt.println(); + + t--; + } + + pt.close(); + + } catch ( + + Exception e) { + return; + } + } + + static void merge(long arr[], int l, int m, int r) { + + int n1 = m - l + 1; + int n2 = r - m; + + long L[] = new long[n1]; + long R[] = new long[n2]; + + for (int i = 0; i < n1; ++i) + L[i] = arr[l + i]; + for (int j = 0; j < n2; ++j) + R[j] = arr[m + 1 + j]; + + int i = 0, j = 0; + + int k = l; + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + arr[k] = L[i]; + i++; + } else { + arr[k] = R[j]; + j++; + } + k++; + } + + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } + } + + static void sort(long arr[], int l, int r) { + if (l < r) { + + int m = l + (r - l) / 2; + + sort(arr, l, m); + sort(arr, m + 1, r); + + merge(arr, l, m, r); + } + } + + static class Pair implements Comparable { + int a, b; + + Pair(int a, int b) { + this.a = a; + this.b = b; + } + + public int compareTo(Pair o) { + // TODO Auto-generated method stub + if (this.a != o.a) + return Integer.compare(this.a, o.a); + else + return Integer.compare(this.b, o.b); + // return 0; + } + + public boolean equals(Object o) { + if (o instanceof Pair) { + Pair p = (Pair) o; + return p.a == a && p.b == b; + } + return false; + } + + } + + static int binarySearch(int arr[], int first, int last, int key) { + int mid = (first + last) / 2; + while (first <= last) { + if (arr[mid] < key) { + first = mid + 1; + } else if (arr[mid] == key) { + return mid; + } else { + last = mid - 1; + } + mid = (first + last) / 2; + } + return -1; + } + + static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } +} +",0,Non-plagiarised +1eda0725,80881cae,"import java.io.*; +import java.util.*; +public class Main { + public static void main(String args[]) + { + FastReader input=new FastReader(); + PrintWriter out=new PrintWriter(System.out); + int T=input.nextInt(); + while(T-->0) + { + int n=input.nextInt(); + int k[]=new int[n]; + int h[]=new int[n]; + for(int i=0;i list=new ArrayList<>(); + for(int i=0;i0) + { + int j=list.size()-1; + int a[]=list.get(j); + int l=a[0],r=a[1]; + int l1=p,r1=k[i]; + if(l1>r) + { + list.add(new int[]{l1,r1}); + break; + } + else if(l1>=l && l1<=r) + { + list.remove(j); + list.add(new int[]{l,r1}); + break; + } + else + { + list.remove(j); + } + } + if(list.size()==0) list.add(new int[]{p,k[i]}); + } + } + long sum=0; + for(int i=0;i0) + { + int n=sc.nextInt(); + int k[]=new int[n]; + int h[]=new int[n]; + for(int i=0;i interval=new ArrayList(); + ArrayList act=new ArrayList(); + for(int i=0;i + { + int x,y; + Pair(int a,int b) + { + this.x=a; + this.y=b; + } +// @Override +// public boolean equals(Object obj) +// { +// if(obj instanceof Pair) +// { +// Pair p=(Pair)obj; +// return p.x==this.x && p.y==this.y; +// } +// return false; +// } +// @Override +// public int hashCode() +// { +// return Math.abs(x)+500*Math.abs(y); +// } +// @Override +// public String toString() +// { +// return ""(""+x+"" ""+y+"")""; +// } + @Override + protected Pair clone() throws CloneNotSupportedException { + return new Pair(this.x,this.y); + } + @Override + public int compareTo(Pair a) + { + long t=(this.x-a.x); + if(t!=0) + return t>0?1:-1; + else + return (int)(this.y-a.y); + } +// public void swap() +// { +// this.y=this.y+this.x; +// this.x=this.y-this.x; +// this.y=this.y-this.x; +// } + } +}",0,Non-plagiarised +4548305b,be3b1289,"import java.util.function.Consumer; +import java.util.*; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.io.*; +import java.lang.Math.*; + public class KickStart2020{ + static class FastReader { + BufferedReader br; + StringTokenizer st; + public FastReader(){br = new BufferedReader( + new InputStreamReader(System.in));} + String next(){ + while (st == null || !st.hasMoreElements()) { + try { st = new StringTokenizer(br.readLine()); } + catch (IOException e) {e.printStackTrace();}} + return st.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());} + String nextLine() { + String str = """"; + try {str = br.readLine();} + catch (IOException e) { e.printStackTrace();} + return str; }} + static boolean isBracketSequence(String s, int a, int b) { + Stack ss = new Stack<>(); + boolean hachu = true; + for(int i = a; i <= b; i++) { + if(s.charAt(i) == ')' && ss.isEmpty()) {hachu = false; break;} + if(s.charAt(i) == '(') ss.add('('); + else ss.pop(); + } + return ss.empty() && hachu; + } + static String reverseOfString(String a) { + StringBuilder ssd = new StringBuilder(); + for(int i = a.length() - 1; i >= 0; i--) { + ssd.append(a.charAt(i)); + } + return ssd.toString(); + } + static char[] reverseOfChar(char a[]) { + char b[] = new char[a.length]; + int j = 0; + for(int i = a.length - 1; i >= 0; i--) { + b[i] = a[j]; + j++; + } + return b; + } + static boolean isPalindrome(char a[]) { + boolean hachu = true; + for(int i = 0; i <= a.length / 2; i++) { + if(a[i] != a[a.length - 1 - i]) { + hachu = false; + break; + } + } + return hachu; + } + static long gcd(long a, long b) + { + if (b == 0) + return a; + return gcd(b, a % b); + + } + static long powermod(long x, long y, long mod){ + long ans = 1; + x = x % mod; + if (x == 0) + return 0; + int i = 1; + while (y > 0){ + if ((y & 1) != 0) + ans = (ans * x) % mod; + y = y >> 1; + x = (x * x) % mod; + } + return ans; + } + static long power(long x, long y){ + long ans = 1; + if (x == 0) + return 0; + int i = 1; + while (y > 0){ + if ((y & 1) != 0) + ans = (ans * x); + y = y >> 1; + x = (x * x); + } + return ans; + } + static boolean check(String a) { + boolean hachu = true; + for(int i = 0; i < a.length(); i++) { + if(a.charAt(0) != a.charAt(i)) {hachu = false; break;} + } + return hachu; + } + public static class Pair implements Comparable { + public int index; + public int value; + + public Pair(int index, int value) { + this.index = index; + this.value = value; + } + @Override + public int compareTo(Pair other) { + //multiplied to -1 as the author need descending sort order + return -1 * Integer.valueOf(this.value).compareTo(other.value); + } + } + static boolean equalString(int i, int j, int arr[], String b) { + int brr[] = new int[26]; + for(int k = i; k <= j; k++) brr[b.charAt(k) - 'a']++; + for(int k = 0; k < 26; k++) { + if(arr[k] != brr[k]) return false; + } + return true; + } + static boolean cequalArray(String a, String b) { + int count[] = new int[26]; + int count1[] = new int[26]; + for(int i = 0; i < a.length(); i++) count[a.charAt(i) - 'a']++; + for(int i = 0; i < a.length(); i++) count1[b.charAt(i) - 'a']++; + for(int i = 0; i < 26; i++) if(count[i] != count1[i]) return false; + return true; + } + static boolean isPrime(long d) { + if(d == 1) return true; + for(int i = 2; i <= (long)Math.sqrt(d); i++) { + if(d % i == 0) return false; + } + return true; + } + public static void main(String[] args) throws Exception{ + FastReader sc = new FastReader(); + PrintWriter out = new PrintWriter(System.out); + int t = sc.nextInt(); + while(t-- > 0) { + int n = sc.nextInt(); + int k = sc.nextInt(); + int arr[] = new int[k]; + int temp[] = new int[k]; + for(int i = 0; i < k; i++) arr[i] = sc.nextInt(); + for(int i = 0; i < k; i++) temp[i] = sc.nextInt(); + long brr[] = new long[n]; + Arrays.fill(brr, Integer.MAX_VALUE); + for(int i = 0; i < k; i++) brr[arr[i] - 1] = temp[i]; + for(int i = 1; i < n; i++) { + brr[i] = Math.min(brr[i], brr[i - 1] + 1); + } + for(int i = n - 2; i >= 0; i--) { + brr[i] = Math.min(brr[i], brr[i + 1] + 1); + } + for(long e: brr) out.print(e + "" ""); + out.println(); + + } + out.close(); + } + }","import java.util.*; +import java.lang.*; +import java.io.*; + + +public class cf { + static PrintWriter out; + static int MOD = 1000000007; + static FastReader scan; + + /*-------- I/O using short named function ---------*/ + public static String ns() { + return scan.next(); + } + + public static int ni() { + return scan.nextInt(); + } + + public static long nl() { + return scan.nextLong(); + } + + public static double nd() { + return scan.nextDouble(); + } + + public static String nln() { + return scan.nextLine(); + } + + public static void p(Object o) { + out.print(o); + } + + public static void ps(Object o) { + out.print(o + "" ""); + } + + public static void pn(Object o) { + out.println(o); + } + + /*-------- for output of an array ---------------------*/ + static void iPA(int arr[]) { + StringBuilder output = new StringBuilder(); + for (int i = 0; i < arr.length; i++) output.append(arr[i] + "" ""); + out.println(output); + } + + static void lPA(long arr[]) { + StringBuilder output = new StringBuilder(); + for (int i = 0; i < arr.length; i++) output.append(arr[i] + "" ""); + out.println(output); + } + + static void sPA(String arr[]) { + StringBuilder output = new StringBuilder(); + for (int i = 0; i < arr.length; i++) output.append(arr[i] + "" ""); + out.println(output); + } + + static void dPA(double arr[]) { + StringBuilder output = new StringBuilder(); + for (int i = 0; i < arr.length; i++) output.append(arr[i] + "" ""); + out.println(output); + } + + /*-------------- for input in an array ---------------------*/ + static void iIA(int arr[]) { + for (int i = 0; i < arr.length; i++) arr[i] = ni(); + } + + static void lIA(long arr[]) { + for (int i = 0; i < arr.length; i++) arr[i] = nl(); + } + + static void sIA(String arr[]) { + for (int i = 0; i < arr.length; i++) arr[i] = ns(); + } + + static void dIA(double arr[]) { + for (int i = 0; i < arr.length; i++) arr[i] = nd(); + } + + /*------------ for taking input faster ----------------*/ + static class FastReader { + BufferedReader br; + StringTokenizer st; + + public FastReader() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + String next() { + while (st == null || !st.hasMoreElements()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + long nextLong() { + return Long.parseLong(next()); + } + + double nextDouble() { + return Double.parseDouble(next()); + } + + String nextLine() { + String str = """"; + try { + str = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } + } + + public static ArrayList sieveOfEratosthenes(int n) { + // Create a boolean array + // ""prime[0..n]"" and + // initialize all entries + // it as true. A value in + // prime[i] will finally be + // false if i is Not a + // prime, else true. + boolean prime[] = new boolean[n + 1]; + for (int i = 0; i <= n; i++) + prime[i] = true; + + for (int p = 2; p * p <= n; p++) { + // If prime[p] is not changed, then it is a + // prime + if (prime[p] == true) { + // Update all multiples of p + for (int i = p * p; i <= n; i += p) + prime[i] = false; + } + } + ArrayList arr = new ArrayList<>(); + // Print all prime numbers + for (int i = 2; i <= n; i++) { + if (prime[i] == true) + arr.add(i); + } + return arr; + } + + // Method to check if x is power of 2 + static boolean isPowerOfTwo(int x) { + return x != 0 && ((x & (x - 1)) == 0); + } + + //Method to return lcm of two numbers + static int gcd(int a, int b) { + return a == 0 ? b : gcd(b % a, a); + } + + //Method to count digit of a number + static int countDigit(long n) { + String sex = Long.toString(n); + return sex.length(); + } + + static void reverse(int a[]) { + int i, k, t; + int n = a.length; + for (i = 0; i < n / 2; i++) { + t = a[i]; + a[i] = a[n - i - 1]; + a[n - i - 1] = t; + } + } + + static final Random random = new Random(); + + //Method for sorting + static void ruffleSort(int[] arr) { + int n = arr.length; + for (int i = 0; i < n; i++) { + int j = random.nextInt(n); + int temp = arr[j]; + arr[j] = arr[i]; + arr[i] = temp; + } + Arrays.sort(arr); + } + + static void sortadv(long[] a) { + ArrayList l = new ArrayList<>(); + for (long value : a) { + l.add(value); + } + Collections.sort(l); + for (int i = 0; i < l.size(); i++) + a[i] = l.get(i); + } + + //Method for checking if a number is prime or not + static boolean isPrime(int n) { + if (n <= 1) return false; + if (n <= 3) return true; + if (n % 2 == 0 || n % 3 == 0) return false; + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + return true; + } + + public static void main(String[] args) throws java.lang.Exception { + OutputStream outputStream = System.out; + out = new PrintWriter(outputStream); + scan = new FastReader(); + //for fast output sometimes + StringBuilder sb = new StringBuilder(); + int t = ni(); + while (t-- != 0) { + int n=ni(); + int k=ni(); + int[] a=new int[k]; + int[] temp=new int[k]; + iIA(a); + iIA(temp); + long dp[]=new long[n]; + Arrays.fill(dp,Integer.MAX_VALUE); + for(int i=0;i=0;i--){ + dp[i]=Math.min(dp[i+1]+1,dp[i]); + } + lPA(dp); + //pn(""""); + } + + out.flush(); + out.close(); + } +} + +",1,Plagiarised +0d4d22e0,d20e861b," + +import java.util.*; +public class CodeForces { + static ArrayListres; + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + int test = input.nextInt(); + while(test-->0){ + int n = input.nextInt(); + int m = input.nextInt(); + char arr[][] = new char[n][m]; + res = new ArrayList<>(); + for(int i =0;i ans = new ArrayList<>(); + + public static void add(int a,int b,int c,int d,int e,int f){ + int[] tem=new int[6]; + tem[0]=a;tem[1]=b;tem[2]=c;tem[3]=d;tem[4]=e;tem[5]=f; + ans.add(tem); + } + public static void helper(int[][] arr,int r,int c){ + for(int k=0;k<12;k++){ + if(arr[r][c]==1){ + add(r,c,r+1,c,r,c+1); + arr[r][c]=1-arr[r][c]; + arr[r+1][c]=1-arr[r+1][c]; + arr[r][c+1]=1-arr[r][c+1]; + } + if(arr[r][c+1]==1){ + add(r,c+1,r+1,c+1,r,c); + arr[r][c+1]=1-arr[r][c+1]; + arr[r+1][c+1]=1-arr[r+1][c+1]; + arr[r][c]=1-arr[r][c]; + } + if(arr[r+1][c]==1){ + add(r+1,c+1,r,c,r+1,c); + arr[r+1][c+1]=1-arr[r+1][c+1]; + arr[r][c]=1-arr[r][c]; + arr[r+1][c]=1-arr[r+1][c]; + } + if(arr[r+1][c+1]==1){ + add(r+1,c+1,r+1,c,r,c+1); + arr[r+1][c+1]=1-arr[r+1][c+1]; + arr[r+1][c]=1-arr[r+1][c]; + arr[r][c+1]=1-arr[r][c+1]; + } + + } + + } + 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(); + ans.clear(); + int[][] arr=new int[n][m]; + for(int i=0;i0) { + int n=fs.nextInt(); + int arr[]=fs.readArray(n); + int l=0, r=1000000000; + while(l1;i--) { + if(brr[i]+arr[i]=min)&&(arr[1]+brr[1]>=min); + } + static long pow(long a,long b) { + if(b<0) return 1; + long res=1; + while(b!=0) { + if((b&1)!=0) { + res*=a; + res%=mod; + } + a*=a; + a%=mod; + b=b>>1; + } + return res; + } + static int gcd(int a,int b) { + if(b==0) return a; + return gcd(b,a%b); + } + static long nck(int n,int k) { + if(k>n) return 0; + long res=1; + res*=fact(n); + res%=mod; + res*=modInv(fact(k)); + res%=mod; + res*=modInv(fact(n-k)); + res%=mod; + return res; + } + static long fact(long n) { +// return fact[(int)n]; + long res=1; + for(int i=2;i<=n;i++) { + res*=i; + res%=mod; + } + return res; + } + + static long modInv(long n) { + return pow(n,mod-2); + } + + static void sort(int[] a) { + //suffle + int n=a.length; + Random r=new Random(); + for (int i=0; i0) { + int n = fs.nextInt(); + int[] h = fs.readArray(n); + int l=1, r=(int)1e9; + int ans = -1; + while(l<=r) { + int mid = (l+r)/2; + if(isPossible(mid, h, n)) { + ans = mid; + l = mid+1; + }else { + r= mid-1; + } + } + out.println(ans); + } + out.close(); + } + + static boolean isPossible(int min, int[] h, int n) { + int[] c = new int[n]; + for(int i=n-1; i>=2; i--) { + + if(h[i]+c[i]-min<0)return false; + + int x = Math.min(h[i], h[i]+c[i]-min) / 3; + c[i-1] += x; + c[i-2] += 2*x; + } + + return (h[0]+c[0]>=min&&h[1]+c[1]>=min); + } + + //****** CODE ENDS HERE ***** + //---------------------------------------------------------------------------------------------------------------- + + static void sort(int[] a) { + ArrayList l=new ArrayList<>(); + for (int i:a) l.add(i); + Collections.sort(l); + for (int i=0; i