F1,F2,text_1,text_2,label 2470b521,f6ca6fc8,"// package com.company; import com.sun.security.jgss.GSSUtil; import javax.swing.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; //ankit singh public class Main { static int inf=Integer.MAX_VALUE/2; public static void main(String[] args) { Scanner input=new Scanner(System.in); int nn=input.nextInt(); int a[]=new int[nn]; ArrayList one=new ArrayList<>(); ArrayList zero=new ArrayList<>(); for (int i = 0; i o=new ArrayList(); ArrayList e=new ArrayList(); for(int i=1;i<=n;i++){ int x=sc.nextInt(); if(x==1)o.add(i); else e.add(i); } int dp[][]=new int[o.size()+1][e.size()+1]; for(int i=1;i<=o.size();i++){ dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1)); for(int j=i+1;j<=e.size();j++){ dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1))); } } System.out.println(dp[o.size()][e.size()]); } } ",0 a8e2cefc,ee270b2a,"import java.io.*; import java.sql.SQLOutput; import java.util.*; public class airconditioner { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() throws java.io.IOException { 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 class compare implements Comparator { public int compare(int[] o1, int[] o2) { return o1[0] - o2[0]; } } public static void main(String[] args) throws IOException { FastReader input = new FastReader(); int testcases = input.nextInt(); for (int t = 0; t < testcases; t++) { int length = input.nextInt(); int conditioners = input.nextInt(); int[] array = new int[length]; int[][] conpositions = new int[conditioners][2]; //0 = position, 1 = value for (int i = 0; i < length; i++) { array[i] = 2000000069; } for (int i = 0; i < conditioners; i++) { conpositions[i][0] = input.nextInt() - 1; } for (int i = 0; i < conditioners; i++) { conpositions[i][1] = input.nextInt(); array[conpositions[i][0]] = conpositions[i][1]; } Arrays.sort(conpositions, new compare()); int position = conditioners - 2; int counter = conpositions[conditioners - 1][1]; for (int i = conpositions[conditioners - 1][0]; i >= 0; i--) { //i = arrayposition array[i] = counter; if (position >= 0 && i == conpositions[position][0]) { if (conpositions[position][1] < counter) { counter = conpositions[position][1]; array[i] = counter; } position--; } counter++; } position = 1; counter = conpositions[0][1]; for (int i = conpositions[0][0]; i < length; i++) { if (counter < array[i]) { array[i] = counter; } if (position < conditioners && i == conpositions[position][0]) { if (conpositions[position][1] < counter) { counter = conpositions[position][1]; if (counter < array[i]) { array[i] = counter; } } position++; } counter++; } for (int i = 0; i < length; i++) { System.out.print(array[i] + "" ""); } System.out.println(); } } }","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 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.util.*; import java.io.*; public class Main{ static final Random random=new Random(); static long mod=1000000007L; static HashMapmap=new HashMap<>(); static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } 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().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } int[] readIntArray(int n){ int[] res=new int[n]; for(int i=0;i 0){ solve(in); } out.close(); } catch (Exception e) { return; } } public static void solve( FastReader in){ int n=in.nextInt(); List> graph=new ArrayList<>(); for(int i=0;i()); } //out.println(""hee""); for(int i=0;i2){ System.out.println(""""+-1); return; } else if(graph.get(i).size()==1){ start=i; } } int[] ans=new int[n-1]; int[] vis=new int[n+1]; vis[start]=1; int w=2; Queue q=new LinkedList<>(); q.add(start); while(!q.isEmpty()){ int x=q.poll(); for(Pair p:graph.get(x)){ if(vis[p.node]==0){ vis[p.node]=1; q.add(p.node); int idx=p.eg_indx; ans[idx]=w; if(w==2)w=5; else w=2; } } } for(int i:ans){ System.out.print(i+"" ""); } System.out.println(""""); } static class Pair{ int node; int eg_indx; Pair(int node,int eg_indx){ this.node=node; this.eg_indx=eg_indx; } } static int gcd(int a,int b){ if(b==0){ return a; } return gcd(b,a%b); } }",0 16857116,f3d7ce08,"import javax.swing.plaf.IconUIResource; import java.lang.reflect.Array; import java.text.CollationElementIterator; import java.util.*; import java.io.*; //Timus judge id- 323935JJ public class Main { //---------------------------------------------------------------------------------------------- public static class Pair implements Comparable { int x=0,y=0; int z=0; public Pair(int a, int b) { this.x=a; this.y=b; } public int compareTo(Pair o) { return this.x - o.x; } } public static int mod = (int) (1e9 + 7); static int ans = Integer.MAX_VALUE; public static void main(String hi[]) throws Exception { FastReader sc = new FastReader(); int t =sc.nextInt(); while(t-->0) { int n =sc.nextInt(); String a = sc.nextLine(),b=sc.nextLine(); int count1=0,count2=0,count3=0,count4=0; for(int i=0;i { int x=0,y=0; int z=0; public Pair(int a, int b) { this.x=a; this.y=b; } public int compareTo(Pair o) { return this.x - o.x; } } public static int mod = (int) (1e9 + 7); static int ans = Integer.MAX_VALUE; public static void main(String hi[]) throws Exception { FastReader sc = new FastReader(); int t =sc.nextInt(); while(t-->0) { int n =sc.nextInt(); String a = sc.nextLine(),b=sc.nextLine(); int count1=0,count2=0,count3=0,count4=0; for(int i=0;i0){ 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 0) { int n = in.nextInt(); String[] s = new String[n]; int[][] freq = new int[n][6]; for (int i = 0; i < n; i++) { s[i] = in.nextLine(); for (char c : s[i].toCharArray()) { freq[i][c - 'a']++; freq[i][5]++; } } int totmax = 0; for (int i = 0; i < 5; i++) { List diffs = new ArrayList<>(n); for (int j = 0; j < n; j++) { diffs.add(2 * freq[j][i] - freq[j][5]); } Collections.sort(diffs); Collections.reverse(diffs); int curr = 0, cnt = 0; while (cnt < diffs.size() && curr + diffs.get(cnt) > 0) { curr += diffs.get(cnt++); } totmax = Math.max(totmax, cnt); } out.println(totmax); } } } 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()); } public long nextLong() { return Long.parseLong(next()); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } } } ",0 00f79486,ce0b2178,"/* In_The_Name_Of_Allah_The_Merciful */ import java.util.*; import java.io.*; public class Main { class Pair implements Comparable{ int f,s; Pair(int x,int y){ f=x; s=y; } @Override public int compareTo(Pair a) { if(a.f!=this.f)return Integer.compare(f,a.f); else return Integer.compare(s,a.s); } } PrintWriter out; FastReader sc; long[] m1= {(long)(1e9+7),998244353}; long mod=m1[1]; long maxlong=Long.MAX_VALUE; long minlong=Long.MIN_VALUE; StringBuilder sb; /****************************************************************************************** *****************************************************************************************/ public void sol() { int n=ni(); int[] ar=new int[n]; for(int i=0;i red=new PriorityQueue<>(Collections.reverseOrder()),blue=new PriorityQueue<>(); for(int i=0;i0) { if(red.poll()>a) { no();return; }a--; }a=1; while(blue.size()>0) { if(blue.poll()0) g.sol(); g.out.flush(); } /**************************************************************************************** *****************************************************************************************/ 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 int ni(){ return sc.nextInt(); }public long nl(){ return sc.nextLong(); }public double nd(){ return sc.nextDouble(); }public char[] rl(){ return sc.nextLine().toCharArray(); }public String rl1(){ return sc.nextLine(); } public void pl(Object s){ out.println(s); } public void pr(Object s){ out.print(s); }public String next(){ return sc.next(); }public long abs(long x){ return Math.abs(x); } public int abs(int x){ return Math.abs(x); } public double abs(double x){ return Math.abs(x); }public long min(long x,long y){ return (long)Math.min(x,y); } public int min(int x,int y){ return (int)Math.min(x,y); } public double min(double x,double y){ return Math.min(x,y); }public long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); }public long lcm(long a, long b) { return (a / gcd(a, b)) * b; } void sort1(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); } } 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); } }void sort(char[] a) { ArrayList l = new ArrayList<>(); for (char i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort1(char[] a) { ArrayList l = new ArrayList<>(); for (char i : a) { l.add(i); } Collections.sort(l,Collections.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } 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); } }void sort1(long[] a) { ArrayList l = new ArrayList<>(); for (long i : a) { l.add(i); } Collections.sort(l,Collections.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } void sort(double[] a) { ArrayList l = new ArrayList<>(); for (double i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }long pow(long a,long b){ if(b==0){ return 1; }long p=pow(a,b/2); if(b%2==0) return mMultiplication(p,p)%mod; else return (mMultiplication(mMultiplication(p,p),a))%mod; } int swap(int a,int b){ return a; }long swap(long a,long b){ return a; }double swap(double a,double b){ return a; } boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0); }boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0); }public long max(long x,long y){ return (long)Math.max(x,y); } public int max(int x,int y){ return (int)Math.max(x,y); } public double max(double x,double y){ return Math.max(x,y); }long sqrt(long x){ return (long)Math.sqrt(x); }int sqrt(int x){ return (int)Math.sqrt(x); }void input(int[] ar,int n){ for(int i=0;i= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } int strSmall(ArrayList arr, int target) { int start = 0, end = arr.size()-1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr.get(mid) > target) { start = mid + 1; ans=start; } else { end = mid - 1; } } return ans; }long mMultiplication(long a,long b) { long res = 0; a %= mod; while (b > 0) { if ((b & 1) > 0) { res = (res + a) % mod; } a = (2 * a) % mod; b >>= 1; } return res; }long nCr(int n, int r ,long p) { if (n 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; }long modInverse(long n, long p) { return power(n, p - 2, p); } int[][] floydWarshall(int graph[][],int INF,int V) { int dist[][] = new int[V][V]; int i, j, k; for (i = 0; i < V; i++) for (j = 0; j < V; j++) dist[i][j] = graph[i][j]; for (k = 0; k < V; k++) { for (i = 0; i < V; i++) { for (j = 0; j < V; j++) { if (dist[i][k] + dist[k][j] < dist[i][j]) dist[i][j] = dist[i][k] + dist[k][j]; } } }return dist; } class minque { Deque q; minque(){ q=new ArrayDeque(); }public void add(long p){ while(!q.isEmpty()&&q.peekLast()>p)q.pollLast(); q.addLast(p); }public void remove(long p) { if(!q.isEmpty()&&q.getFirst()==p)q.removeFirst(); }public long min() { return q.getFirst(); } } int find(subset[] subsets, int i) { if (subsets[i].parent != i) subsets[i].parent = find(subsets, subsets[i].parent); return subsets[i].parent; }void Union(subset[] subsets, int x, int y) { int xroot = find(subsets, x); int yroot = find(subsets, y); if (subsets[xroot].rank < subsets[yroot].rank) { subsets[xroot].parent = yroot; } else if (subsets[yroot].rank < subsets[xroot].rank) { subsets[yroot].parent = xroot; } else { subsets[xroot].parent = yroot; subsets[yroot].rank++; } }class subset { int parent; int rank; } }","import java.io.*; import java.util.*; public class D { static class Pair implements Comparable{ int a; char b; public Pair(int a, char b) { this.a = a; this.b = b; } @Override public int compareTo(Pair p) { if(this.b == p.b) return this.a - p.a; return this.b - p.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 pairs = new ArrayList(); for(int i = 0;i i && pairs.get(i-1).b == 'B') { continue; } else { possible = false; break; } } output.println(possible == true ? ""YES"" : ""NO""); } output.flush(); } public static int[] sort(int arr[]) { List list = new ArrayList<>(); for(int i:arr) list.add(i); Collections.sort(list); for(int i = 0;i Give your 100%, that's it! -> Feynman Rule To Solve Any Problem: 1. Read the problem. 2. Think About It. 3. Solve it! */ 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(); int m = sc.nextInt(); int[][] arr = new int[n][m]; for(int i = 0; i < n; i++){ String s = sc.next(); for(int j = 0; j < m; j++){ arr[i][j] = s.charAt(j)-'0'; } } List list = new ArrayList<>(); for(int i = 0; i < n-1; i++){ for(int j = 0; j < m-1; j++){ check(list,i,j,n,m,arr); } } // for(int i = 0; i < n; i++){ // for(int j = 0; j < m; j++){ // out.print(arr[i][j] + "" ""); // } // out.println(); // } out.println(list.size()/3); for(int i = 0; i < list.size(); i+=3){ out.println(list.get(i) + "" "" + list.get(i+1) + "" "" + list.get(i+2)); } // out.println(list.size()); // for(int i = 0; i < list.size(); i++){ // out.println(list.get(i)); // } } out.close(); } static void check(List list, int i, int j, int n, int m, int[][] arr){ int[][] a = new int[2][2]; a[0][0] = arr[i][j]; a[0][1] = arr[i][j+1]; a[1][0] = arr[i+1][j]; a[1][1] = arr[i+1][j+1]; int ones = 0; for(int x = 0; x < 2; x++){ for(int y = 0; y < 2; y++){ if(a[x][y] == 1) ones++; } } if(ones == 0) return; if(ones == 1){ ones(i,j,list,a); twos(i,j,list,a); threes(i,j,list,a); } else if(ones == 2){ twos(i,j,list,a); threes(i,j,list,a); } else if(ones == 3){ threes(i,j,list,a); } else if(ones == 4){ fours(i,j,list,a); // for(int x = 0; x < 2; x++){ // for(int y = 0; y < 2; y++){ // out.print(a[x][y] + "" ""); // } // out.println(); // } ones(i,j,list,a); // for(int x = 0; x < 2; x++){ // for(int y = 0; y < 2; y++){ // out.print(a[x][y] + "" ""); // } // out.println(); // } twos(i,j,list,a); // for(int x = 0; x < 2; x++){ // for(int y = 0; y < 2; y++){ // out.print(a[x][y] + "" ""); // } // out.println(); // } threes(i,j,list,a); // for(int x = 0; x < 2; x++){ // for(int y = 0; y < 2; y++){ // out.print(a[x][y] + "" ""); // } // out.println(); // } } arr[i][j] = a[0][0]; arr[i+1][j] = a[1][0]; arr[i][j+1] = a[0][1]; arr[i+1][j+1] = a[1][1]; } public static void twos(int i, int j, List list, int[][] a){ int count = 0; for(int x = 0; x < 2; x++){ for(int y = 0; y < 2; y++){ if(a[x][y] == 1 && count < 1) { count++; a[x][y] = 0; list.add(get(x,y,i,j)); } else if(a[x][y] == 0){ list.add(get(x,y,i,j)); a[x][y] = 1; } } } } public static void threes(int i, int j, List list, int[][] a){ int count = 0; for(int x = 0; x < 2; x++){ for(int y = 0; y < 2; y++){ if(a[x][y] == 1) { a[x][y] = 0; list.add(get(x,y,i,j)); } } } } public static void fours(int i, int j, List list, int[][] a){ int count = 0; for(int x = 0; x < 2; x++){ for(int y = 0; y < 2; y++){ if(a[x][y] == 1 && count < 3) { count++; a[x][y] = 0; list.add(get(x,y,i,j)); } } } } public static void ones(int i, int j, List list, int[][] a){ int count = 0; for(int x = 0; x < 2; x++){ for(int y = 0; y < 2; y++){ if(a[x][y] == 1) { a[x][y] = 0; list.add(get(x,y,i,j)); } else if(a[x][y] == 0 && count < 2){ a[x][y] = 1; count++; list.add(get(x,y,i,j)); } } } } public static String get(int x, int y, int i, int j){ if(x == 0 && y == 0) { return (i+1) + "" "" + (j+1); } else if(x == 0 && y == 1){ return (i+1) + "" "" + (j+2); } else if(x == 1 && y == 0){ return (i+2) + "" "" + (j+1); } else { return (i+2) + "" "" + (j+2); } } /* Random stuff to try when stuck: -if it's 2C then it's dp -for combo/probability problems, expand the given form we're interested in -make everything the same then build an answer (constructive, make everything 0 then do something) -something appears in parts of 2 --> model as graph -assume a greedy then try to show why it works -find way to simplify into one variable if multiple exist -treat it like fmc (note any passing thoughts/algo that could be used so you can revisit them) -find lower and upper bounds on answer -figure out what ur trying to find and isolate it -see what observations you have and come up with more continuations -work backwards (in constructive, go from the goal to the start) -turn into prefix/suffix sum argument (often works if problem revolves around adjacent array elements) -instead of solving for answer, try solving for complement (ex, find n-(min) instead of max) -draw something -simulate a process -dont implement something unless if ur fairly confident its correct -after 3 bad submissions move on to next problem if applicable -do something instead of nothing and stay organized -write stuff down Random stuff to check when wa: -if code is way too long/cancer then reassess -switched N/M -int overflow -switched variables -wrong MOD -hardcoded edge case incorrectly Random stuff to check when tle: -continue instead of break -condition in for/while loop bad Random stuff to check when rte: -switched N/M -long to int/int overflow -division by 0 -edge case for empty list/data structure/N=1 */ 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.util.*; import java.math.*; public class Solution { final static int MOD = 1000000007; public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = Integer.parseInt(in.next()); for(int tt = 0; tt < t; tt++) { int n = Integer.parseInt(in.next()); int m = Integer.parseInt(in.next()); char[][] grid = new char[n][m]; List ans = new ArrayList<>(); for(int i = 0; i < n; i++) { grid[i] = in.next().toCharArray(); } int iii = 0; int jjj = 0; if(n % 2 != 0) { iii = 1; for(int j = 0; j + 1 < m; j++) { if(grid[0][j] == '1') { grid[0][j] = '0'; grid[1][j] = (grid[1][j] == '1') ? '0' : '1'; grid[1][j + 1] = (grid[1][j + 1] == '1') ? '0' : '1'; ans.add(1); ans.add(j + 1); ans.add(2); ans.add(j + 1); ans.add(2); ans.add(j + 2); } if(grid[0][j + 1] == '1') { grid[0][j + 1] = '0'; grid[1][j] = (grid[1][j] == '1') ? '0' : '1'; grid[1][j + 1] = (grid[1][j + 1] == '1') ? '0' : '1'; ans.add(1); ans.add(j + 2); ans.add(2); ans.add(j + 1); ans.add(2); ans.add(j + 2); } } } if(m % 2 != 0) { jjj = 1; for(int i = (n % 2 == 0) ? 0 : 1; i + 1 < n; i++) { if(grid[i][0] == '1') { grid[i][0] = '0'; grid[i][1] = (grid[i][1] == '1') ? '0' : '1'; grid[i + 1][1] = (grid[i + 1][1] == '1') ? '0' : '1'; ans.add(i + 1); ans.add(1); ans.add(i + 1); ans.add(2); ans.add(i + 2); ans.add(2); } if(grid[i + 1][0] == '1') { grid[i + 1][0] = '0'; grid[i][1] = (grid[i][1] == '1') ? '0' : '1'; grid[i + 1][1] = (grid[i + 1][1] == '1') ? '0' : '1'; ans.add(i + 2); ans.add(1); ans.add(i + 1); ans.add(2); ans.add(i + 2); ans.add(2); } } } for(int i = iii; i < n; i += 2) { for(int j = jjj; j < m; j += 2) { int cnt = count(grid, i, j); while(cnt > 0) { List g = new ArrayList<>(); for(int k = i; k < i + 2; k++) { for(int l = j; l < j + 2; l++) { g.add(new G(grid[k][l], k, l)); } } Collections.sort(g); int l = 0; int r = 0; if(cnt == 1 || cnt == 3) { l = 1; r = 3; }else { l = 0; r = 2; } for(int k = l; k <= r; k++) { int ii = g.get(k).i; int jj = g.get(k).j; grid[ii][jj] = (grid[ii][jj] == '1') ? '0' : '1'; ans.add(ii + 1); ans.add(jj + 1); } cnt = count(grid, i, j); } } } System.out.println(ans.size() / 6); for(int i = 0; i < ans.size(); i += 6) { for(int j = i; j < i + 6; j++) { System.out.print(ans.get(j) + "" "" ); } System.out.println(); } } } public static int count(char[][] grid, int i, int j) { int cnt = 0; for(int k = i; k < i + 2; k++) { for(int l = j; l < j + 2; l++) { if(grid[k][l] == '1') cnt += 1; } } return cnt; } } class G implements Comparable { Character val; int i; int j; public G(char val, int i, int j) { this.val = val; this.i = i; this.j = j; } @Override public int compareTo(G obj) { return this.val.compareTo(obj.val); } }",0 8d6f1bf5,e90a198b,"//package Div2.C; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Menorah { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t > 0) { int n = Integer.parseInt(br.readLine()); String source = br.readLine(); String destination = br.readLine(); int sameStatusOnes = 0; int sameStatusZeros = 0; int diffStatusOnes = 0; int diffStatusZeros = 0; for (int i = 0; i < n; i++) { char c1 = source.charAt(i); char c2 = destination.charAt(i); if (c1 == c2) { if (c1 == '0') { sameStatusZeros += 1; } else { sameStatusOnes += 1; } } else { if (c1 == '0') { diffStatusZeros += 1; } else { diffStatusOnes += 1; } } } int sameStatus = sameStatusOnes + sameStatusZeros; int diffStatus = diffStatusOnes + diffStatusZeros; //first case if (sameStatus == n) { System.out.println(0); } else if (diffStatus == n) { //second case if (diffStatus % 2 == 0 && diffStatusOnes == (n + 1) / 2) System.out.println(n); else System.out.println(-1); } else { int op1 = -1; int op2 = -1; if (sameStatus % 2 != 0 && sameStatusOnes == (sameStatus + 1) / 2) op1 = sameStatus; if (diffStatus % 2 == 0 && diffStatusOnes == (diffStatus + 1) / 2) op2 = diffStatus; if (op1 != -1 && op2 != -1) System.out.println(Integer.min(op1, op2)); else if (op1 != -1) System.out.println(op1); else if (op2 != -1) System.out.println(op2); else System.out.println(-1); } t--; } } } ","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(); PrintWriter out = new PrintWriter(System.out); int yo = sc.nextInt(); while (yo-- > 0) { int n = sc.nextInt(); String s = sc.next(); String t = sc.next(); int op1 = cal(s,t,n); int op2 = helper(s,t,n,'0'); int op3 = helper(s,t,n,'1'); int ans = min(min(op1,op2),op3); if(ans == Integer.MAX_VALUE){ out.println(""-1""); } else { out.println(ans); } } out.close(); } static int helper(String str1, String str2, int n, int ch){ char[] s = str1.toCharArray(); char[] t = str2.toCharArray(); int idx = -1; for(int i = 0; i < n; i++){ if(s[i] == '1' && t[i] == ch){ idx = i; break; } } if(idx == -1){ return Integer.MAX_VALUE; } for(int i = 0; i < n; i++){ if(i == idx) continue; if(s[i] == '1') s[i] = '0'; else s[i] = '1'; } int ans = cal(String.valueOf(s),String.valueOf(t),n); if(ans == Integer.MAX_VALUE) return ans; return 1 + ans; } static int cal(String s, String t, int n){ int op01 = 0; int op10 = 0; for(int i = 0; i < n; i++){ if(s.charAt(i) != t.charAt(i)){ if(s.charAt(i) == '1') op10++; else op01++; } } if(op10 != op01){ return Integer.MAX_VALUE; } return op01 + op10; } /* Source: hu_tao Random stuff to try when stuck: - use bruteforcer - check for n = 1, n = 2, so on -if it's 2C then it's dp -for combo/probability problems, expand the given form we're interested in -make everything the same then build an answer (constructive, make everything 0 then do something) -something appears in parts of 2 --> model as graph -assume a greedy then try to show why it works -find way to simplify into one variable if multiple exist -treat it like fmc (note any passing thoughts/algo that could be used so you can revisit them) -find lower and upper bounds on answer -figure out what ur trying to find and isolate it -see what observations you have and come up with more continuations -work backwards (in constructive, go from the goal to the start) -turn into prefix/suffix sum argument (often works if problem revolves around adjacent array elements) -instead of solving for answer, try solving for complement (ex, find n-(min) instead of max) -draw something -simulate a process -dont implement something unless if ur fairly confident its correct -after 3 bad submissions move on to next problem if applicable -do something instead of nothing and stay organized -write stuff down Random stuff to check when wa: -if code is way too long/cancer then reassess -switched N/M -int overflow -switched variables -wrong MOD -hardcoded edge case incorrectly Random stuff to check when tle: -continue instead of break -condition in for/while loop bad Random stuff to check when rte: -switched N/M -long to int/int overflow -division by 0 -edge case for empty list/data structure/N=1 */ 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, PrintWriter out) { //for debugging only for (int x : arr) out.print(x + "" ""); out.println(); } public static int log2(int a){ return (int)(Math.log(a)/Math.log(2)); } public static long ceil(long x, long y){ return (x + 0l + y - 1) / y; } 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); }",0 9291ca83,d61f51c5,"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); } } } "," /* /$$$$$ /$$$$$$ /$$ /$$ /$$$$$$ |__ $$ /$$__ $$ |$$ |$$ /$$__ $$ | $$| $$ \ $$| $$|$$| $$ \ $$ | $$| $$$$$$$$| $$ / $$/| $$$$$$$$ / $$ | $$| $$__ $$ \ $$ $$/ | $$__ $$ | $$ | $$| $$ | $$ \ $$$/ | $$ | $$ | $$$$$$/| $$ | $$ \ $/ | $$ | $$ \______/ |__/ |__/ \_/ |__/ |__/ /$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$ | $$__ $$| $$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$$ /$$$| $$_____/| $$__ $$ | $$ \ $$| $$ \ $$| $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$$$ /$$$$| $$ | $$ \ $$ | $$$$$$$/| $$$$$$$/| $$ | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$ | $$$$$$$/ | $$____/ | $$__ $$| $$ | $$| $$|_ $$| $$__ $$| $$__ $$| $$ $$$| $$| $$ $$$| $$| $$__/ | $$__ $$ | $$ | $$ \ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$| $$\ $ | $$| $$ | $$ \ $$ | $$ | $$ | $$| $$$$$$/| $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$| $$ \/ | $$| $$$$$$$$| $$ | $$ |__/ |__/ |__/ \______/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/ |__/|________/|__/ |__/ */ import java.util.*; import java.lang.*; import java.io.*; public class InterestingStrory { public static void main(String[] args) throws java.lang.Exception { // your code goes here try { // Scanner sc=new Scanner(System.in); FastReader sc = new FastReader(); int t =sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); int[][] occ=new int[n][5]; for(int i=0;i=0;l--){ if(currSum+sums[l]<=0){ break; } else{ currSum+=sums[l]; } } ans=Math.max(ans,n-l-1); } System.out.println(ans); } } catch (Exception e) { return; } } static int BS(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] < element) { low = mid + 1; } else { high = mid; } } if (arr[low] == element) { return low; } else if (arr[high] == element) { return high; } return -1; } static int lower_bound(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] < element) { low = mid + 1; } else { high = mid; } } if (arr[low] >= element) { return low; } else if (arr[high] >= element) { return high; } return -1; } static int upper_bound(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] <= element) { low = mid + 1; } else { high = mid; } } if (arr[low] > element) { return low; } else if (arr[high] > element) { return high; } return -1; } public static long gcd_long(long a,long b){ //a/b,a-> dividant b-> divisor if(b==0)return a; return gcd_long(b, a%b); } public static int gcd_int(int a,int b){ //a/b,a-> dividant b-> divisor if(b==0)return a; return gcd_int(b, a%b); } public static int lcm(int a,int b){ int gcd=gcd_int(a, b); return (a*b)/gcd; } 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()); } String nextLine() { String s = """"; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } Long nextLong() { return Long.parseLong(next()); } } } /* * public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){ * return true; } if(n<1 || m<1 || k<0){ return false; } boolean * tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; } * return false; } */ ",0 1386da0e,f87eb1b3,"import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; 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.Iterator; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.function.Function; import java.util.stream.Collectors; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; public class Solution { public static int INF= Integer.MAX_VALUE; public static long MOD= 1000000000+7L; public static int WHITE= 0; public static int GRAY= 1; public static int BLACK= 2; class Pair{ public int day; int count; Pair(int d, int c){ day=d; count=c; } } static class Solver { public void solve(InputReader in, PrintWriter out) { int tt=in.nextInt(); //int tt=1; for(int cases=1;cases<=tt;cases++){ int n= in.nextInt(); long odd_min= Long.MAX_VALUE; long even_min= Long.MAX_VALUE; long ans= Long.MAX_VALUE; int odd_length=0; int even_length=0; long total=0; for(int segment=1;segment<=n;segment++){ int cost= in.nextInt(); total+=cost; if(segment%2==1){ odd_min = Math.min(odd_min, cost); odd_length++; } else{ even_min = Math.min(even_min, cost); even_length++; } if(segment>1){ { long value=total-(odd_min+even_min); value+= odd_min * (n-odd_length+1); value+= even_min * (n-even_length+1); ans= Math.min(value, ans); } } } System.out.println(ans); } } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); solver.solve(in, out); out.close(); } 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()); } public long nextLong() { return Long.parseLong(next()); } } }","import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.PrintStream; import java.io.PrintWriter; public class A { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable { int first, second; public Pair(int aa, int bb) { first = aa; second = bb; } public int compareTo(Pair o) { if(this.second < o.second) return -1; if(this.second > o.second) return +1; return this.first - o.first; } } static class Tuple implements Comparable{ long first, second,third; public Tuple(long first, long second, long third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int[] parent; int[] rank; //Size of the trees is used as the rank public DSU(int n) { parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) //finding through path compression { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public boolean union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return false; //if they are already connected we exit by returning false. // if a's parent is less than b's parent if(rank[a] < rank[b]) { //then move a under b parent[a] = b; } //else if rank of j's parent is less than i's parent else if(rank[a] > rank[b]) { //then move b under a parent[b] = a; } //if both have the same rank. else { //move a under b (it doesnt matter if its the other way around. parent[b] = a; rank[a] = 1 + rank[a]; } return true; } } static class Reader { 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) throws IOException { int[] a=new int[n]; for (int i=0; i SieveList(int n) { boolean prime[] = new boolean[n+1]; Arrays.fill(prime, true); ArrayList l = new ArrayList<>(); for (int p=2; p*p<=n; p++) { if (prime[p] == true) { for(int i=p*p; i<=n; i += p) { prime[i] = false; } } } for (int p=2; p<=n; p++) { if (prime[p] == true) { l.add(p); } } return l; } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long modPow(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPow(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); // Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(int a[], int x) { int l=-1, r=a.length; while(l+1>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { long INF = 1000000000000000007L; int n = sc.nextInt(); long[] c = new long[n+1]; for(int i = 1;i<=n;i++) c[i] = sc.nextLong(); long ans = INF; long mo = INF, so = 0, co = 0; long me = INF, se = 0, ce = 0; for(int i=1;i<=n;i++) { if(i%2 == 1) { mo = min(mo,c[i]); so += c[i]; co++; } else { me = min(me,c[i]); se += c[i]; ce++; } if(i>=2) { long x = so + (n - co) * mo + se + (n - ce) * me; ans=min(ans,x); } } fout.println(ans); } fout.close(); } }",0 0fd5b95a,6490bbe8,"//package codeforces; import java.io.PrintWriter; import java.util.*; public class codeforces { static int dp[][]=new int[5001][5001]; public static void main(String[] args) { Scanner s=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int t=1; for(int tt=0;tt z=new ArrayList<>(); ArrayList o=new ArrayList<>(); for(int i=0;i l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i { if (x[c] != y[c]) return(int)( x[c] - y[c]); return (int)-(x[1]+x[2] - y[1]-y[2]); }); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static int sol(int i,int j,ArrayList z,ArrayList o) { if(j==o.size()) { return 0; } int h=z.size()-i; int l=o.size()-j; if(i==z.size()) { return 10000000; } if(dp[i][j]!=-1) { //System.out.println(i+"" ""+j); return dp[i][j]; } int ans1=sol(i+1,j,z,o); int ans2=sol(i+1,j+1,z,o)+Math.abs(z.get(i)-o.get(j)); dp[i][j]=Math.min(ans1, ans2); return dp[i][j]; } } ","import java.util.*; // import java.lang.*; import java.io.*; // THIS TEMPLATE MADE BY AKSH BANSAL. 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; } } private static boolean[] isPrime; private static void primes(){ int num = (int)1e6; // PRIMES FROM 1 TO NUM isPrime = new boolean[num]; for (int i = 2; i< isPrime.length; i++) { isPrime[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(isPrime[i] == true) { for(int j = (i*i); j[] adj; // static void getAdj(int n,int q, FastReader sc){ // adj = new ArrayList[n+1]; // for(int i=1;i<=n;i++){ // adj[i] = new ArrayList<>(); // } // for(int i=0;i 0) { // output.append(solver()).append(""\n""); // } // out.println(output); // _______________________________ int n = sc.nextInt(); int arr[] = new int[n]; for(int i=0;i a = new ArrayList<>(); ArrayList b = new ArrayList<>(); for(int i=0;i= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long 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 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 int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static void main(String[] args) throws FileNotFoundException { new Main(); } } ",0 8c591975,b6724dd9,"import java.io.DataInputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class Main { private static void run() throws IOException { int n, k; n = in.nextInt(); k = in.nextInt(); int[] a = new int[n]; long[] t = new long[k]; for (int i = 0; i < n; i++) { a[i] = -1; } for (int i = 0; i < k; i++) { a[in.nextInt() - 1] = i; } for (int i = 0; i < k; i++) { t[i] = in.nextLong(); } long[] dp = new long[n]; long now; now = 2000000000; for (int i = 0; i < n; i++) { now++; if (a[i] != -1) { now = Math.min(now, t[a[i]]); } dp[i] = now; } now = 2000000000; for (int i = n - 1; i >= 0; i--) { now++; if (a[i] != -1) { now = Math.min(now, t[a[i]]); } dp[i] = Math.min(dp[i], now); } for (int i = 0; i < n; i++) { out.printf(""%d "", dp[i]); } out.println(); } 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(); } } }"," import java.io.*; import java.util.*; public class AirConditioners { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub // System.in and System.out are input and output streams, respectively. InputStream inputStream = System.in; InputReader in = new InputReader(inputStream); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t = in.nextInt(); while (t-- != 0) { int n = in.nextInt(); int k = in.nextInt(); HashMap hm = new HashMap<>(); int[] ac = in.readArray(k); for (int idx = 0; idx < ac.length; idx++) { hm.put(ac[idx], in.nextInt()); } Arrays.sort(ac); long[] lmin = new long[n]; long val = (long) Integer.MAX_VALUE; int acdx = 0; for (int idx = 0; idx < lmin.length; idx++) { if (acdx < ac.length && ac[acdx] - 1 == idx) { val = (long) Math.min(val, hm.get(ac[acdx])); acdx++; } lmin[idx] = val; val++; } acdx = ac.length - 1; val = Integer.MAX_VALUE; for (int idx = lmin.length-1; idx >= 0; idx--) { if (acdx >= 0 && ac[acdx] - 1 == idx) { val = (long) Math.min(val, hm.get(ac[acdx])); acdx--; } lmin[idx] = Math.min(lmin[idx], val); val++; } for (long x : lmin) { out.write(x + "" ""); } out.newLine(); } out.close(); } 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()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } } } ",0 11373c16,7011024d,"/* 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; } } public static void main (String[] args) throws java.lang.Exception { FastReader scan = new FastReader(); PrintWriter pw = new PrintWriter(System.out); int n = scan.nextInt(); ArrayList a = new ArrayList<>(); ArrayList b = new ArrayList<>(); for(int i=0;i 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 3c667d4f,a7894e0b,"import java.util.*; public class j { public static void main(String args[]) { Scanner in=new Scanner(System.in); int n=in.nextInt(); while(n-->0) { int len=in.nextInt(); int t=in.nextInt(); int pos[]=new int[t]; int temp[]=new int[t]; for(int i=0;i=0;i--) { range[i]=Math.min(range[i+1]+1,range[i]); } for(int i=0;i { @Override public int compare(Pair a, Pair b) { if(a.x!=b.x) { return (int)(a.x - b.x); } else { return (int)(a.y-b.y); } } } static class Compare { void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator() { @Override public int compare(Pair p1, Pair p2) { if(p1.x!=p2.x) { return (int)(p1.x - p2.x); } else { return (int)(p1.y - p2.y); } } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + "" "" + arr[i].y + "" ""); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { 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 Exception { Scanner sc = new Scanner(); StringBuilder res = new StringBuilder(); int tc = sc.nextInt(); while(tc-->0) { int n=sc.nextInt(); int k=sc.nextInt(); int[] ac=new int[k]; long[] ans=new long[n]; Arrays.fill(ans, Integer.MAX_VALUE/2); for(int i=0;i=0;i--) { ans[i]=Math.min(ans[i], ans[i+1]+1); } 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 = scanner.nextInt(); ArrayList list = new ArrayList<>(); for(int i=0;i num = new ArrayList<>(); for(String s : list){ int cnt = 0; for(int i=0;i 0) { ans = n; break; } } System.out.println(ans); } } } ","import java.util.*; import java.io.*; public class Main { 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(); String[] s = new String[n]; for(int i=0; i pq = new PriorityQueue<>(Collections.reverseOrder()); //Big comes in top; for(int i=0; i 0){ cur += pq.poll(); numberOfWords++; }else{ break; } } MAX = Math.max(MAX, numberOfWords); } pw.println(MAX); } 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 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 0b04b41e,4da08761,"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]); } } }","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; } } }",0 94b3b86d,efe594c3,"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); } } ","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.LinkedList; import java.util.StringTokenizer; public class problemD { static ArrayList[] adj; static boolean found = false; static int[] D = new int[212345]; private static void solve() throws Exception { int n = fs.nextInt(); int a = fs.nextInt(); int b = fs.nextInt(); int da = fs.nextInt(); int db = fs.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 u = fs.nextInt(); int v = fs.nextInt(); adj[u].add(v); adj[v].add(u); } found = false; dfs(a, 0, 0, da, b); if (found) { out.println(""Alice""); return; } dfs(b,0,0); int maxD = -1; int node = -1; for (int i = 1; i <= n; i ++ ) { if (D[i] > maxD) { maxD = D[i]; node = i; } } dfs(node, 0, 0); maxD = -1; for (int i = 1; i <= n; i ++ ) { if (D[i] > maxD) { maxD = D[i]; } } boolean bob = db > da * 2 && maxD > da * 2; out.println(bob ? ""Bob"" : ""Alice""); } private static void dfs(int node, int parent, int dist) { D[node] = dist; for (int x: adj[node]) { if (x != parent) { dfs(x, node, dist+1); } } } private static void dfs(int node, int parent, int dist, int da, int b) { if (node == b) { found = true; return; } for (int x: adj[node]) { if (x != parent) { if (dist+1 <= da) { dfs(x, node, dist + 1, da, b); } } } } private static FastScanner fs = new FastScanner(); private static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws Exception { int T = 1; T = fs.nextInt(); for (int t = 0; t < T; t++) { solve(); } out.close(); } static void debug(Object... O) { System.err.print(""DEBUG ""); System.err.println(Arrays.deepToString(O)); } 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()); } double nextDouble() { return Double.parseDouble(next()); } String nextString() { return next(); } } } ",0 1230c43e,ce0b2178,"//package com.company; import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static boolean[] primecheck = new boolean[1000002]; public static void main(String[] args) throws IOException { OutputStream outputStream = System.out; FastReader in = new FastReader(); PrintWriter out = new PrintWriter(outputStream); PROBLEM solver = new PROBLEM(); int t = 1; t = in.nextInt(); for (int i = 0; i < t; i++) { solver.solve(in, out); } out.close(); } static class PROBLEM { public void solve(FastReader in, PrintWriter out) { int n = in.nextInt(); int[] a = in.readArray(n); char[] c = in.nextLine().toCharArray(); int cur = 1; ArrayList p = new ArrayList<>(); for (int i = 0; i < n; i++) { p.add(new Pair(c[i], a[i])); } Collections.sort(p); // for (int i = 0; i < n; i++) { // out.println(p.get(i).x + "" "" + p.get(i).y); // } for (int i = 0; i < n; i++) { if(p.get(i).x == 'B' && p.get(i).y < i+1){ out.println(""NO""); return; } if(p.get(i).x == 'R' && p.get(i).y > i+1){ out.println(""NO""); return; } } out.println(""YES""); // int n = in.nextInt(), m = in.nextInt(); // char[] s = in.nextLine().toCharArray(); // // int rl = 0, ud = 0, r = 0 , l = 0, rlf = 0, udf = 0; // int lmax = 0, rmax = 0, umax = 0, dmax = 0; // // for (int i = 0; i < s.length; i++) { // if(s[i] == 'L'){ // if(rlf == 0) rlf = -1; // rl--; // l--; // if(rl == 0) l = 0; // if(rl < 0 && lmax+rmax <= m) lmax = Math.max(Math.abs(l), lmax); // } // if(s[i] == 'R'){ // if(rlf == 0) rlf = 1; // rl++; // r++; // // if(rl > 0 && lmax+rmax <= m) rmax = Math.max(r, rmax); // } // if(s[i] == 'U'){ // if(udf == 0) udf = 1; // ud++; // r = Math.max(Math.abs(ud), r); // if(ud > 0 && umax+dmax <= n) umax = Math.max(Math.abs(ud), umax); // } // if(s[i] == 'D'){ // if(udf == 0) udf = -1; // ud--; // r = Math.max(Math.abs(ud), r); // if(ud < 0 && umax+dmax <= n) dmax = Math.max(Math.abs(ud), dmax); // } // } // // int ansc = 0, ansr = 0; // // out.println(rlf + "" lmx = "" + lmax + "" rmax"" + rmax); // // if(rlf == 1) ansc = m-rmax; // else if(rlf == -1) ansc = 1+lmax; // else ansc = 1; // // if(udf == 1) ansr = 1+umax; // else if(udf == -1) ansr = m-dmax; // else ansr = 1; // // out.println(ansr + "" "" + ansc); } } static HashMap initializeMap(int n){ HashMap hm = new HashMap<>(); for (int i = 0; i <= n; i++) { hm.put(i, 0); } return hm; } static boolean isRegular(char[] c){ Stack s = new Stack<>(); for (char value : c) { if (s.isEmpty() && value == ')') return false; if (value == '(') s.push(value); else s.pop(); } return s.isEmpty(); } static ArrayList> createAdj(int n, int e){ FastReader in = new FastReader(); ArrayList> adj = new ArrayList<>(); for (int i = 0; i < n + 1; i++) { adj.add(new ArrayList<>()); } for (int i = 0; i < e; i++) { int a = in.nextInt(), b = in.nextInt(); System.out.println(a); adj.get(a).add(b); adj.get(b).add(a); } return adj; } static int binarySearch(int[] a, int l, int r, int x){ if(r>=l){ int mid = l + (r-l)/2; if(a[mid] == x) return mid; if(a[mid] > x) return binarySearch(a, l, mid-1, x); else return binarySearch(a,mid+1, r, x); } return -1; } static boolean isPalindromeI(int n){ int d = 0; int y = n; while(y>0){ d++; y/=10; } int[] a = new int[d]; for (int i = 0; i < d; i++) { a[i] = n%10; n/=10; } System.out.println(Arrays.toString(a)); for (int i = 0; i < d / 2; i++) { if(a[i] != a[d-i-1]) return false; } return true; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static boolean isSquare(double a) { boolean isSq = false; double b = Math.sqrt(a); double c = Math.sqrt(a) - Math.floor(b); if (c == 0) isSq = true; return isSq; } static int exponentMod(int A, int B, int C) { // Base cases if (A == 0) return 0; if (B == 0) return 1; // If B is even long y; if (B % 2 == 0) { y = exponentMod(A, B / 2, C); y = (y * y) % C; } // If B is odd else { y = A % C; y = (y * exponentMod(A, B - 1, C) % C) % C; } return (int) ((y + C) % C); } static class Pair implements Comparable{ char x; int y; Pair(char x, int y){ this.x = x; this.y = y; } public int compareTo(Pair o){ int ans = Integer.compare(x, o.x); if(o.x == x) ans = Integer.compare(y, o.y); return ans; } } static class Tuple implements Comparable{ int x, y, id; Tuple(int x, int y, int id){ this.x = x; this.y = y; this.id = id; } public int compareTo(Tuple o){ int ans = Integer.compare(x, o.x); if(o.x == x) ans = Integer.compare(y, o.y); return ans; } } // public static class Pair, V extends Comparable> implements Comparable> { // public U x; // public V y; // // public Pair(U x, V y) { // this.x = x; // this.y = y; // } // // public int hashCode() { // return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode()); // } // // public boolean equals(Object o) { // if (this == o) // return true; // if (o == null || getClass() != o.getClass()) // return false; // Pair p = (Pair) o; // return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y)); // } // // public int compareTo(Pair b) { // int cmpU = x.compareTo(b.x); // return cmpU != 0 ? cmpU : y.compareTo(b.y); // } // // public int compareToY(Pair b) { // int cmpU = y.compareTo(b.y); // return cmpU != 0 ? cmpU : x.compareTo(b.x); // } // // public String toString() { // return String.format(""(%s, %s)"", x.toString(), y.toString()); // } // // } 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()); } char nextChar() { return next().charAt(0); } boolean nextBoolean() { return !(nextInt() == 0); } // boolean nextBoolean(){return Boolean.parseBoolean(next());} String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } } private static int[] mergeSort(int[] array) { //array.length replaced with ctr int ctr = array.length; if (ctr <= 1) { return array; } int midpoint = ctr / 2; int[] left = new int[midpoint]; int[] right; if (ctr % 2 == 0) { right = new int[midpoint]; } else { right = new int[midpoint + 1]; } for (int i = 0; i < left.length; i++) { left[i] = array[i]; } for (int i = 0; i < right.length; i++) { right[i] = array[i + midpoint]; } int[] result = new int[array.length]; left = mergeSort(left); right = mergeSort(right); result = merge(left, right); return result; } private static int[] merge(int[] left, int[] right) { int[] result = new int[left.length + right.length]; int leftPointer = 0, rightPointer = 0, resultPointer = 0; while (leftPointer < left.length || rightPointer < right.length) { if (leftPointer < left.length && rightPointer < right.length) { if (left[leftPointer] < right[rightPointer]) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } else if (leftPointer < left.length) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } return result; } public static void Sieve(int n) { Arrays.fill(primecheck, true); primecheck[0] = false; primecheck[1] = false; for (int i = 2; i * i < n + 1; i++) { if (primecheck[i]) { for (int j = i * 2; j < n + 1; j += i) { primecheck[j] = false; } } } } } ","import java.io.*; import java.util.*; public class D { static class Pair implements Comparable{ int a; char b; public Pair(int a, char b) { this.a = a; this.b = b; } @Override public int compareTo(Pair p) { if(this.b == p.b) return this.a - p.a; return this.b - p.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 pairs = new ArrayList(); for(int i = 0;i i && pairs.get(i-1).b == 'B') { continue; } else { possible = false; break; } } output.println(possible == true ? ""YES"" : ""NO""); } output.flush(); } public static int[] sort(int arr[]) { List list = new ArrayList<>(); for(int i:arr) list.add(i); Collections.sort(list); for(int i = 0;i{ int first; int second; Pair(int x,int y){ this.first=x; this.second=y; } @Override public int compareTo(Pair x){ return this.first-x.first; } } public static void main (String[] args) { Scanner scan=new Scanner(System.in); int t=scan.nextInt(); while(t-->0){ int n=scan.nextInt(); int m=scan.nextInt(); int x=scan.nextInt(); int[]arr=new int[n]; for(int i=0;i set = new TreeSet<>(); Queue set = new PriorityQueue<>(); for(int i=1;i<=m;i++){ set.add(new Pair(0,i)); } System.out.println(""YES""); for(int i=0;i> mp=new HashMap<>(); for(int i=0;i l=mp.get(a[i]); l.push(i); mp.put(a[i],l); } else{ Stack l=new Stack<>(); l.push(i); mp.put(a[i],l); } } Arrays.sort(a); int[] b=new int[n]; int cn=1; for(int i=0;i li=mp.get(a[i]); if(!li.isEmpty()){ int val=li.peek(); b[val]=cn+1; c[i]+=a[i]; li.pop(); mp.put(a[i],li); } cn++; } int mv=c[0]-c[n-1]; if(Math.abs(mv)>x) System.out.println(""NO""); else { System.out.println(""YES""); for(int i=0;i 0){ int n = sc.nextInt(); String a = sc.next(); String b = sc.next(); int change = 0; int nochange = 0; 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; } } } "," import java.io.*; import java.util.*; public class GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); String a=sc.next(); String b=sc.next(); int i; int zero=0,one=0; int x=0,y=0,x1=0,y1=0; for(i=0;i0) { 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 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 a7894e0b,fadc1365," import java.io.*; import java.math.*; import java.util.*; public class test { static class Pair{ long x; long y; Pair(long x,long y){ this.x = x; this.y = y; } } static class Sort implements Comparator { @Override public int compare(Pair a, Pair b) { if(a.x!=b.x) { return (int)(a.x - b.x); } else { return (int)(a.y-b.y); } } } static class Compare { void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator() { @Override public int compare(Pair p1, Pair p2) { if(p1.x!=p2.x) { return (int)(p1.x - p2.x); } else { return (int)(p1.y - p2.y); } } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + "" "" + arr[i].y + "" ""); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { 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 Exception { Scanner sc = new Scanner(); StringBuilder res = new StringBuilder(); int tc = sc.nextInt(); while(tc-->0) { int n=sc.nextInt(); int k=sc.nextInt(); int[] ac=new int[k]; long[] ans=new long[n]; Arrays.fill(ans, Integer.MAX_VALUE/2); for(int i=0;i=0;i--) { ans[i]=Math.min(ans[i], ans[i+1]+1); } for(int i=0;i=0;i--){ ans[i]=Math.min(ans[i],ans[i+1]+1); } for(int i=0;i l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i l=new ArrayList<>(); for (char 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; } } } ",1 6e207cbf,dc281165,"import java.util.*; import java.lang.*; import java.io.*; public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int sm, n; while(t > 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); } } } } ","/** * 12/24/21 morning * https://codeforces.com/contest/1615/problem/C */ // package codeforce.global.r18; import java.util.*; import java.io.*; public class C { static PrintWriter pw; void solve(int n, char[] a, char[] b) { int same = 0, diff = 0; int[] sameCnt = {0, 0}, diffCnt = {0, 0}; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { same++; sameCnt[a[i] - '0']++; } else { diff++; diffCnt[a[i] - '0']++; } } // tr(same, sameCnt, diff, diffCnt); int res = Integer.MAX_VALUE; if (same % 2 != 0 && sameCnt[1] - sameCnt[0] == 1) res = Math.min(res, same); if (diff % 2 == 0 && diffCnt[1] == diffCnt[0]) res = Math.min(res, diff); pr(res == Integer.MAX_VALUE ? -1 : res); } private void run() { // read_write_file(); // comment this before submission FastScanner fs = new FastScanner(); int t = fs.nextInt(); while (t-- > 0) { int n = fs.nextInt(); char[] a = fs.next().toCharArray(), b = fs.next().toCharArray(); solve(n, a, b); } } private final String INPUT = ""input.txt""; private final String OUTPUT = ""output.txt""; void read_write_file() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { } } public static void main(String[] args) { pw = new PrintWriter(System.out); new C().run(); pw.close(); } void pr(int num) { pw.println(num); } void pr(long num) { pw.println(num); } void pr(double num) { pw.println(num); } void pr(String s) { pw.println(s); } void pr(char c) { pw.println(c); } 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()); } double nextDouble() { return Double.parseDouble(next()); } } void tr(Object... o) { pw.println(Arrays.deepToString(o)); } }",0 764484c3,9028caf7,"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 Armchairs { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); Vector a = new Vector(); Vector b = new Vector(); for (int i=0; i>1;i++){int tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}} static void reverse(long a[]){for(int i=0;i>1;i++){long tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}} static String reverse(String s){StringBuilder sb=new StringBuilder(s);sb.reverse();return sb.toString();} } ","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=1; while(T-->0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList list=new ArrayList<>(); ArrayList space=new ArrayList<>(); for(int i=0;i 0) { solve(); } out.close(); } public static void solve() { int M = sc.nextInt(); String a = sc.nextLine(); String b = sc.nextLine(); int d = 0; int al = 0; int bl = 0; for (int i = 0; i < M; i++) { if (a.charAt(i) != b.charAt(i)) { d++; } if (a.charAt(i) == '1') { al++; } if (b.charAt(i) == '1') { bl++; } } if ((al == bl && d % 2 == 0) || (M - al + 1 == bl && (M - d) % 2 == 1)) { if ((al == bl && d % 2 == 0) && (M - al + 1 == bl && (M - d) % 2 == 1)) { out.println(Math.min(d, M - d)); } else if (al == bl && d % 2 == 0) { out.println(d); } else { out.println(M - d); } } else { out.println(-1); return; } } private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); private static MyScanner sc = new MyScanner(); private static int N = sc.nextInt(); @SuppressWarnings(""unused"") private static class MyScanner { BufferedReader br; StringTokenizer st; public 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[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }","/** * check out my youtube channel sh0rkyboy * https://tinyurl.com/zdxe2y4z * I do screencasts, solutions, and other fun stuff in the future */ import java.util.*; import java.io.*; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.Math.max; public class EdB { static long[] mods = {1000000007, 998244353, 1000000009}; static long mod = mods[0]; public static MyScanner sc; public static PrintWriter out; public static void main(String[] largewang) throws Exception{ // TODO Auto-generated method stub sc = new MyScanner(); out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-->0) { int n = sc.nextInt(); char[] a = sc.next().toCharArray(); char[] b = sc.next().toCharArray(); int counta = 0; int countb = 0; for (char j : a) { if (j == '1') counta++; } for (char j : b) { if (j == '1') countb++; } long ans = 0; if (counta == countb && n-counta+1 == countb){ for (int j = 0;j copy = new ArrayList<>(); for (int i : array) copy.add(i); Collections.sort(copy); for(int i = 0;i0){ if (y%2==1) ans = (ans*x)%mod; x = (x*x)%mod; y/=2; } return (int)(ans); } static void verdict(boolean a){ out.println(a ? ""YES"" : ""NO""); } public static class MyScanner { BufferedReader br; StringTokenizer st; public 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()); } String nextLine() { String str = """"; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } //StringJoiner sj = new StringJoiner("" ""); //sj.add(strings) //sj.toString() gives string of those stuff w spaces or whatever that sequence is",0 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 4241f473,4685c420,"import java.io.*; import java.util.*; public class C { public static void main(String[] args)throws IOException { FastScanner scan = new FastScanner(); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); int t = scan.nextInt(); for(int tt = 0;tt arr = new ArrayList<>(); for(int i = 0;i list = new ArrayList<>(); char ch = (char)('a'+cases); for(int i = 0;i=0;i--) { sum+=list.get(i); if(sum>0) count++; else break; } max = Math.max(max, count); } output.write(max+""\n""); } output.flush(); } public static int[] sort(int arr[]) { List list = new ArrayList<>(); for(int i:arr) list.add(i); Collections.sort(list); for(int i = 0;i stories[]=new ArrayList[5]; for(int i=0;i<5;i++){ stories[i]=new ArrayList<>(); } for(int i=1;i<=n;i++){ char c[]=br.readLine().toCharArray(); int freq[]=new int[5]; for(int j=0;j{ int x,y,diff; public Story(int x,int y){ this.x=x; this.y=y; this.diff=this.x-this.y; } @Override public int compareTo(Story o) { return -this.diff+o.diff; } }",0 11c2ab99,28c2d81a,"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 = fs.nextInt(), k = fs.nextInt(); int[] positions = fs.readArray(k), temps = fs.readArray(k); int[] forced = new int[n]; Arrays.fill(forced, Integer.MAX_VALUE/2); for(int i=0; i=0; i--) forced[i] = Math.min(forced[i], forced[i+1]+1); for(int i=0; i ""+a); } public static void debug(long a, long b){ System.out.println(""--> ""+ a +"" + "" + b); } public static void debug(char a, char b){ System.out.println(""--> ""+ a +"" + "" + b); } public static void debug(int[] array){ System.out.print(""Array--> ""); System.out.println(Arrays.toString(array)); } public static void debug(char[] array){ System.out.print(""Array--> ""); System.out.println(Arrays.toString(array)); } public static void debug(HashMap map){ System.out.print(""Map--> ""+map.toString()); } } } ",1 40cc90ae,9399204d,"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 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<5; 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= '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[] initArray(int n, Reader scan) throws IOException { long arr[] = new long[n]; for (int i = 0; i < n; i++) { arr[i] = scan.nextLong(); } return arr; } public static long sum(long arr[]) { long sum = 0; for (long i : arr) { sum += (long) i; } return sum; } public static long max(long arr[]) { long max = Long.MIN_VALUE; for (long i : arr) { max = Math.max(i, max); } return max; } public static long min(long arr[]) { long min = Long.MAX_VALUE; for (long i : arr) { min = Math.min(i, min); } return min; } public static List[] initAdjacency(int n, int e, Reader scan, boolean type) throws IOException { List adj[] = new ArrayList[n + 1]; for (int i = 0; i < e; i++) { int u = scan.nextInt(); int v = scan.nextInt(); if (adj[u] == null) adj[u] = new ArrayList<>(); if (type && adj[v] == null) adj[v] = new ArrayList<>(); adj[u].add(v); if (type) adj[v].add(u); } return adj; } public static void main(String[] args) throws IOException { Reader scan = new Reader(); // Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while (t-- > 0) { D(scan); } } public static void D(Reader scan) throws IOException { int n=scan.nextInt(); int a=scan.nextInt(); int b=scan.nextInt(); int da=scan.nextInt(); int db=scan.nextInt(); List adj[]=initAdjacency(n, n-1, scan, true); int d= dist(a, 0, adj, b); if(d<=da) { System.out.println(""Alice""); return; } if(db<=2*da) { System.out.println(""Alice""); return; } MyPair far1= farthest(1, 0, adj); MyPair far2=farthest(far1.weight, 0, adj); int diameter= far2.value-1; if(diameter<=2*da) { System.out.println(""Alice""); }else { System.out.println(""Bob""); } // int val=d-2*a; // // if(val>0) { // System.out.println(""Bob""); // return; // } // // int dep=0; // for(Integer x: adj[a]) { // if(x==last) continue; // // dep=Math.max(getDepth(x, a, adj), dep); // } // // if(dep>=val) { // System.out.println(""Bob""); // }else { // System.out.println(""Alice""); // } } public static MyPair farthest(int i, int parent, List adj[]) { int dist=0; int node= i; for(Integer x: adj[i]) { if(x==parent) continue; MyPair recAns= farthest(x, i, adj); if(recAns.value>dist) { dist= recAns.value; node= recAns.weight; } } return new MyPair(dist+1, node); } public static int getDepth(int i, int parent, List adj[]) { int max=0; for(Integer x: adj[i]) { if(x==parent) continue; max=Math.max(max, getDepth(x, i, adj)); } return max+1; } public static int dist(int i, int parent, List adj[], int target) { if(i==target) return 0; for(Integer x: adj[i]) { if(x==parent) continue; int recAns= dist(x, i, adj, target); if(recAns!=-1) { last=x; return recAns+1; } } return -1; } public static void C(int n, String s, int k) { char arr[]= s.toCharArray(); for(int i=0;i=n) break; if(arr[i]=='0'&&arr[i+k]=='1') { System.out.println(""NO""); return; } if(arr[i]=='1'&&arr[i+k]=='0') { System.out.println(""NO""); return; } if(arr[i]=='0'||arr[i]=='1') { if(arr[i+k]=='?') { arr[i+k]=arr[i]; } continue; } if(arr[i+k]=='0'||arr[i+k]=='1') { if(arr[i]=='?') { arr[i]=arr[i+k]; } continue; } } int c0[]=new int[n]; int c1[]=new int[n]; int last0=0; int last1=0; for(int i=0;ik/2||count1>k/2) { System.out.println(""NO""); return; } } System.out.println(""YES""); } public static void A(int n, long arr[]) { for(int i=n-1;i>=0;i--) { System.out.print(arr[i]+"" ""); } System.out.println(); } public static void B(int n, long arr[]) { for(int i=1;i0) { arr[i]+= arr[i-1]; arr[i-1]=0; } } // System.out.println(Arrays.toString(arr)); long ans=0; for(Long x: arr) { if(x<0) ans+= -x; } System.out.println(ans); } } class MyPair { int value; int weight; public MyPair(int value, int w) { this.value = value; weight = w; } }","import java.util.*; import java.io.*; import java.math.*; /** * * @Har_Har_Mahadev */ /** * Main , Solution , Remove Public */ public class D { static int visited[]; static int distance[]; private static ArrayList[] adj; private static void BFS(int node) { visited[node] = 1; Queue q = new LinkedList(); q.offer(node); distance[node] = 0; while(!q.isEmpty()) { int curr = q.poll(); for(int child : adj[curr]) { if(visited[child] == 0) { q.offer(child); distance[child] = distance[curr] + 1; visited[child] = 1; } } } } public static void process() throws IOException { int n = sc.nextInt(),a = sc.nextInt(),b = sc.nextInt(), da = sc.nextInt(),db = sc.nextInt(); adj = new ArrayList[n+1]; distance = new int[n+1]; visited = new int[n+1]; for(int i = 0; i<=n; i++)adj[i] = new ArrayList(); for(int i =1; i lis = new ArrayList(); for(int i = 1; i<=n; i++)lis.add(new Pair(distance[i], i)); Collections.sort(lis); Pair e = lis.get(n-1); distance = new int[n+1]; visited = new int[n+1]; BFS(e.y); int max = 0; for(int i = 1; i<=n; i++)max = max(max,distance[i]); if(max-da<=da) { System.out.println(""Alice""); return; } System.out.println(""Bob""); } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= private static void google(int tt) { System.out.print(""Case #"" + (tt) + "": ""); } static FastScanner sc; static PrintWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new PrintWriter(System.out); } else { sc = new FastScanner(100); out = new PrintWriter(""output.txt""); } int t = 1; t = sc.nextInt(); int TTT = 1; while (t-- > 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 debug(Object o) { System.out.println(o); } static void debug(Object x, Object y) { System.out.println(""("" + x + "" , "" + y + "")""); } static void println(Object o) { out.println(o); } static void println(int[] o) { for (int e : o) print(e + "" ""); println(); } static void println(long[] o) { for (long e : o) print(e + "" ""); println(); } 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); } static void reverseArray(int[] a) { int n = a.length; int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void reverseArray(long[] a) { int n = a.length; long arr[] = new long[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } } ",0 0f09b843,45e41c6d,"//Created by Aminul on 7/23/2021. import java.io.*; import java.util.*; import static java.lang.Math.*; public class C { public static void main(String[] args) throws Exception { InputReader in = new InputReader(System.in); PrintWriter pw = new PrintWriter(System.out); char[] keys = {'a', 'b', 'c', 'd', 'e'}; int test = in.nextInt(); for (int t = 1; t <= test; t++) { int n = in.nextInt(); String[] words = new String[n]; for (int i = 0; i < n; i++) { words[i] = in.next(); } int res = 0; for (char c : keys) { res = max(res, getMaxCount(n, words, c)); } pw.println(res); } pw.close(); } static int getMaxCount(int n, String[] words, char c) { Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = 2 * getCount(words[i], c) - words[i].length(); } Arrays.sort(arr, Comparator.reverseOrder()); int sum = 0, count = 0; for (int i : arr) { sum += i; if (sum <= 0) { break; } count++; } return count; } static int getCount(String s, char c) { int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == c) { count++; } } return count; } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() throws Exception { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(next()); } } }","import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class C { static char[] alphabet = {'a', 'b', 'c', 'd', 'e'}; public static void main(String[] args) { Scanner scn = new Scanner(System.in); int test = scn.nextInt(); for (int t = 0; t < test; t++) { int n = scn.nextInt(); String[] words = new String[n]; for (int i = 0; i < n; i++) { words[i] = scn.next(); } int maxWords = 0; for (char target : alphabet) { Integer[] diff = new Integer[n]; for (int i = 0; i < n; i++) { String word = words[i]; diff[i] = wordDiff(word, target); } Arrays.sort(diff, Collections.reverseOrder()); int pointer = 0; int sum = 0; for (; pointer < n && sum > -diff[pointer]; pointer++) { sum += diff[pointer]; } maxWords = Math.max(pointer, maxWords); } System.out.println(maxWords); } } static int wordDiff(String a, char target) { int ans = 0; for (char c : a.toCharArray()) { if (c == target) ans++; } return ans * 2 - a.length(); } } ",0 1f748faf,851fafb6,"/* JAI MATA DI */ import java.util.*; 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; } } static long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i find) r = m-1; else l = m+1; } return r; } static int UB(int[] arr , long find , int l , int r) { while(l<=r) { int m = (l+r)/2; if(arr[m] find) r = m-1; else l = m+1; } return r; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j= 0) { bool[0] = false; bool[1] = false; } return bool; } static void sort(char[] arr) { ArrayList al = new ArrayList(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i0) { TEST_CASE(); } System.out.println(sb); } static void TEST_CASE() { int n = sc.nextInt(); ; int[] arr = new int[n]; for(int i =0 ; i set = new HashSet<>(); set.add(0); for(int i =0 ; i al = new ArrayList<>(set); for(int e:al) { int num = e+arr[i]; if(set.contains(num)) { cond = true; break; } set.add(num); } } if(cond) System.out.println(""YES""); else System.out.println(""NO""); } } ","// package codeforce.cfGR15; import java.io.PrintWriter; import java.util.*; public class D { // MUST SEE BEFORE SUBMISSION // check whether int part would overflow or not, especially when it is a * b!!!! public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); // int t = 1; for (int i = 0; i < t; i++) { solve(sc, pw); } pw.close(); } static void solve(Scanner in, PrintWriter out){ int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } Set set = new HashSet<>(); for(int x : arr) { if (x == 0){ out.println(""YES""); return; }else if (set.contains(x) || set.contains(-x)){ out.println(""YES""); return; } set.add(x); } for (int i = 0; i < n; i++) { if (dfs(0, i, arr, 0)){ out.println(""YES""); return; } } out.println(""NO""); } static boolean dfs(int idx, int need, int[] arr, int cur){ if (cur == arr[need]) return true; if (idx == arr.length) return false; if (idx == need) return dfs(idx + 1, need, arr, cur); return dfs(idx + 1, need, arr, cur + arr[idx]) | dfs(idx + 1, need, arr, cur - arr[idx]) | dfs(idx + 1, need, arr, cur); } } ",0 5d175166,d1391025,"import java.io.*; import java.util.*; public class A { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty(""ONLINE_JUDGE"") == null) { w = new Printer(new FileOutputStream(""output.txt"")); sc = new fs(new FileInputStream(""input.txt"")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static int[] res; static List> graph; static boolean two = true; static HashMap, Integer> hm; static void solve() throws IOException { int n = sc.ni(); graph = new ArrayList<>(); res = new int[n-1]; hm = new HashMap<>(); for(int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } boolean f = false; for(int i = 0; i < n-1; i++) { int a = sc.ni(), b = sc.ni(); graph.get(a).add(b); graph.get(b).add(a); if(graph.get(a).size() > 2 || graph.get(b).size() > 2) f = true; hm.put(new pr<>(a, b), i); hm.put(new pr<>(b, a), i); } if(f) { w.p(-1); return; } int one = 0; for(int i = 0; i < n; i++) { if(graph.get(i).size() == 1) { one = i; break; } } dfs(one, -1); for(int i: res) { w.pr(i+"" ""); } w.pl(); } static void dfs(int at, int pt) { List li = graph.get(at); if(pt != -1) { res[hm.get(new pr<>(at, pt))] = two?2:3; two = !two; } for(int to: li) { if(to == pt) continue; dfs(to, at); } } static class pr { T a; V b; public pr(T a, V b) { this.a = a; this.b = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof pr)) return false; pr pr = (pr) o; return a.equals(pr.a) && b.equals(pr.b); } @Override public int hashCode() { return Objects.hash(a, b); } } }","import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class C { static class scanner { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException {return Integer.parseInt(next());} static double nextDouble() throws IOException {return Double.parseDouble(next());} static long nextLong() throws IOException {return Long.parseLong(next());} } static long min(long a, long b) {return Math.min(a, b);} static long min(long a, long b, long c) {return min(a, min(b, c));} static int min(int a, int b) {return Math.min(a, b);} static int min(int a, int b, int c) {return min(a, min(b, c));} static long max(long a, long b) {return Math.max(a, b);} static long max(long a, long b, long c) {return max(a, max(b, c));} static int max(int a, int b) {return Math.max(a, b);} static int max(int a, int b, int c) {return max(a, max(b, c));} static int abs(int x) {return Math.abs(x);} static long abs(long x) {return Math.abs(x);} static long ceil(double x) {return (long) Math.ceil(x);} static long floor(double x) {return (long) Math.floor(x);} static int ceil(int x) {return (int) Math.ceil(x);} static int floor(int x) {return (int) Math.floor(x);} static double sqrt(double x) {return Math.sqrt(x);} static double cbrt(double x) {return Math.cbrt(x);} static long sqrt(long x) {return (long) Math.sqrt(x);} static long cbrt(long x) {return (long) Math.cbrt(x);} static int gcd(int a, int b) {if(b == 0)return a;return gcd(b, a % b);} static long gcd(long a, long b) {if(b == 0)return a;return gcd(b, a % b);} static double pow(double n, double power) {return Math.pow(n, power);} static long pow(long n, double power) {return (long) Math.pow(n, power);} static class Pair {int first, second;public Pair(int first, int second) {this.first = first;this.second = second;}} public static void main(String[] args) throws IOException { scanner.init(System.in); int t = 1; t = scanner.nextInt(); while (t-- > 0) { solve(); } } static void solve() throws IOException { int n = scanner.nextInt(); List> tree = new ArrayList<>(); for (int i = 0; i < n; i++) { tree.add(new ArrayList<>()); } for (int i = 0; i < n-1; i++) { int u = scanner.nextInt()-1; int v = scanner.nextInt()-1; tree.get(u).add(new Pair(v, i)); tree.get(v).add(new Pair(u, i)); } int start = -1; for (int i = 0; i < n; i++) { if(tree.get(i).size() > 2) { System.out.println(-1); return; } else if(tree.get(i).size() == 1) { start = i; } } int[] res = new int[n-1]; Queue q = new LinkedList<>(); q.add(start); int weight = 2, prev = -1; while (!q.isEmpty()) { int u = q.poll(); for(Pair v : tree.get(u)) { if(v.first != prev) { q.add(v.first); res[v.second] = weight; weight = 5 - weight; } } prev = u; } for(int i : res) { System.out.print(i + "" ""); } System.out.println(); } } ",0 0588b869,69b2fd22,"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; public static long pow(long a,int 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,w; Pair(int u,int v,int w) { this.u=u; this.v=v; this.w=w; } } /*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 int gcd(int a,int b) { if(b%a==0) return a; return gcd(b%a,a); } public static void dfs(int u,int dist[],int sub[],int mxv[],int par[],ArrayList edge[]) { sub[u]=1; for(int v:edge[u]) { if(dist[v]==-1) { par[v]=u; dist[v]=dist[u]+1; dfs(v,dist,sub,mxv,par,edge); if(sub[v]+1>sub[u]) { sub[u]=sub[v]+1; mxv[u]=v; } } } } public static void main(String args[])throws Exception { FastReader fs=new FastReader(); PrintWriter pw=new PrintWriter(System.out); //int tc=fs.nextInt(); int n=fs.nextInt(); int a[]=new int[n]; for(int i=0;i o=new ArrayList<>(); ArrayList z=new ArrayList<>(); for(int i=0;i0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList list=new ArrayList<>(); ArrayList space=new ArrayList<>(); for(int i=0;i> adj = new ArrayList>(n); for(int k = 0; k < n; k++) adj.add(new HashSet()); for(int k = 0; k < n-1; k++){ adj.get(k).add(k+1); } //closest number before that is < Stack stk = new Stack(); stk.add(new Num(array[0],0)); for(int k = 1; k < n; k++){ while(!stk.isEmpty() && stk.peek().x > array[k]){ stk.pop(); } if(!stk.isEmpty()){ adj.get(stk.peek().i).add(k); } stk.add(new Num(array[k],k)); } //closest number after that is < stk = new Stack(); stk.add(new Num(array[n-1],n-1)); for(int k = n-2; k >= 0; k--){ while(!stk.isEmpty() && stk.peek().x > array[k]){ stk.pop(); } if(!stk.isEmpty()){ adj.get(k).add(stk.peek().i); } stk.add(new Num(array[k],k)); } //closest number before that is > stk = new Stack(); stk.add(new Num(array[0],0)); for(int k = 1; k < n; k++){ while(!stk.isEmpty() && stk.peek().x < array[k]){ stk.pop(); } if(!stk.isEmpty()){ adj.get(stk.peek().i).add(k); } stk.add(new Num(array[k],k)); } //closest number after that is > stk = new Stack(); stk.add(new Num(array[n-1],n-1)); for(int k = n-2; k >= 0; k--){ while(!stk.isEmpty() && stk.peek().x < array[k]){ stk.pop(); } if(!stk.isEmpty()){ adj.get(k).add(stk.peek().i); } stk.add(new Num(array[k],k)); } int[] path = new int[n]; Arrays.fill(path,Integer.MAX_VALUE); path[0] = 0; for(int k = 0; k < n; k++){ for(int nei : adj.get(k)){ path[nei] = Math.min(path[nei],path[k]+1); } } int answer = path[n-1]; out.println(answer); out.close(); } public static class Num{ int x; int i; public Num(int a, int b){ x = a; i = b; } } }","import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; import java.util.ArrayList; import java.util.Vector; import java.util.StringTokenizer; import java.io.BufferedReader; 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 in, PrintWriter out) { int n = in.nextInt(); ArrayList jumps[] = new ArrayList[n]; int h[] = new int[n]; for (int i = 0; i < n; i++) { h[i] = in.nextInt(); jumps[i] = new ArrayList<>(); } Stack s = new Stack<>(); for (int i = 0; i < n; i++) { while (s.size() > 0 && s.peek()[0] > h[i]) { s.pop(); } if (s.size() > 0) { jumps[i].add(s.peek()[1]); } s.add(new int[]{h[i], i}); } s.clear(); for (int i = 0; i < n; i++) { while (s.size() > 0 && s.peek()[0] < h[i]) { s.pop(); } if (s.size() > 0) { jumps[i].add(s.peek()[1]); } s.add(new int[]{h[i], i}); } s.clear(); for (int i = n - 1; i >= 0; i--) { while (s.size() > 0 && s.peek()[0] < h[i]) { s.pop(); } if (s.size() > 0) { jumps[s.peek()[1]].add(i); } s.add(new int[]{h[i], i}); } s.clear(); for (int i = n - 1; i >= 0; i--) { while (s.size() > 0 && s.peek()[0] > h[i]) { s.pop(); } if (s.size() > 0) { jumps[s.peek()[1]].add(i); } s.add(new int[]{h[i], i}); } s.clear(); int dp[] = new int[n]; dp[0] = 0; for (int i = 1; i < n; i++) { dp[i] = Integer.MAX_VALUE; for (int x : jumps[i]) { dp[i] = Math.min(dp[i], dp[x] + 1); } } out.print(dp[n - 1]); } } 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()); } } } ",0 68cdd55d,c18048bf,"import java.io.*; import java.util.*; 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 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.x0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(); long k = sc.nl(); Long[] arr = new Long[n]; for(int i = 0; i < n; i++) arr[i] = sc.nl(); Arrays.sort(arr); long sum = 0; for(int i = 0; i < n; i++) { sum += arr[i]; } if(sum <= k) { w.p(0); return; } long cont = 0; long min = sum-k; for(int i = n-1; i >= 0; i--) { cont += arr[i]; long psum = sum-cont; if(psum <= k) { long extra = k-psum; if(arr[0]*(n-i)<=extra) { min = Math.min(min, n-i); continue; } if(i == 0) { long q = k/n; long ans = arr[0]-q+n-1; min = Math.min(min, ans); continue; } extra += arr[0]; long q = extra/(n-i+1); long ans = arr[0]-q+n-i; min = Math.min(min, ans); } else { long toRem = psum-k-arr[0]; long q = (long)Math.ceil(toRem/(n-i+1.0)); long ans = q+arr[0]+n-i; min = Math.min(min, ans); } } w.p(min); } }","import java.io.*; import java.util.*; public class B { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty(""ONLINE_JUDGE"") == null) { w = new Printer(new FileOutputStream(""output.txt"")); sc = new fs(new FileInputStream(""input.txt"")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(); long k = sc.nl(); Long[] arr = new Long[n]; for(int i = 0; i < n; i++) arr[i] = sc.nl(); Arrays.sort(arr); long sum = 0; for(int i = 0; i < n; i++) { sum += arr[i]; } if(sum <= k) { w.p(0); return; } long cont = 0; long min = sum-k; for(int i = n-1; i >= 0; i--) { cont += arr[i]; long psum = sum-cont; if(psum <= k) { long extra = k-psum; if(arr[0]*(n-i)<=extra) { min = Math.min(min, n-i); continue; } if(i == 0) { long q = k/n; long ans = arr[0]-q+n-1; min = Math.min(min, ans); continue; } extra += arr[0]; long q = extra/(n-i+1); long ans = arr[0]-q+n-i; min = Math.min(min, ans); } else { long toRem = psum-k-arr[0]; long q = (long)Math.ceil(toRem/(n-i+1.0)); long ans = q+arr[0]+n-i; min = Math.min(min, ans); } } w.p(min); } }",1 69b2fd22,e6b7a899,"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=1; while(T-->0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList list=new ArrayList<>(); ArrayList space=new ArrayList<>(); for(int i=0;i ans=new ArrayList<>(); public static void main(String[] args){ PrintWriter out = new PrintWriter(System.out); InputReader sc=new InputReader(System.in); int N = sc.nextInt(); int[] A = sc.nextIntArray(N); ArrayList a1 = new ArrayList(); ArrayList a2 = new ArrayList(); for (int i = 0; i < A.length; i++) { if (A[i]==0) { a1.add(i); }else { a2.add(i); } } int[][] dp = new int[a1.size()+1][a2.size()+1]; for (int i = 0; i < dp.length; i++) { Arrays.fill(dp[i], Integer.MAX_VALUE/2); } dp[0][0] = 0; for (int i = 1; i <= a1.size() ; i++) { int pos1 = a1.get(i-1); for (int j = 0; j <= a2.size(); j++) { dp[i][j] = dp[i-1][j]; if (j-1>=0) { int pos2 = a2.get(j-1); dp[i][j] = Math.min(dp[i][j], dp[i-1][j-1] + Math.abs(pos1-pos2)); } } } System.out.println(dp[a1.size()][a2.size()]); } static class InputReader { private InputStream in; private byte[] buffer = new byte[1024]; private int curbuf; private int lenbuf; public InputReader(InputStream in) { this.in = in; this.curbuf = this.lenbuf = 0; } public boolean hasNextByte() { if (curbuf >= lenbuf) { curbuf = 0; try { lenbuf = in.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return false; } return true; } private int readByte() { if (hasNextByte()) return buffer[curbuf++]; else return -1; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private void skip() { while (hasNextByte() && isSpaceChar(buffer[curbuf])) curbuf++; } public boolean hasNext() { skip(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public double[] nextDoubleArray(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[][] nextCharMap(int n, int m) { char[][] map = new char[n][m]; for (int i = 0; i < n; i++) map[i] = next().toCharArray(); return map; } } } ",0 680ba922,76ad805a,"import java.util.*; import java.io.*; public class Solution { 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(); } } 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.Arrays; import java.util.Scanner; public class First { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); String a = sc.next(); String b = sc.next(); char ch1[] = a.toCharArray(); char ch2[] = b.toCharArray(); int zz = 0; int oz = 0; int zo = 0; int oo = 0; for (int i = 0; i < n; i++) { if (ch1[i] == '0') { if (ch2[i] == '0') { zz += 1; } else { oz += 1; } } else { if (ch2[i] == '0') { zo += 1; } else { oo += 1; } } } int ans = -1; if ((oo - zz) == 1 || zo == oz) { int s1 = (int) 1e7; int s2 = (int) 1e7; if ((oo - zz) == 1) { s1 = oo + zz; } if (zo == oz) s2 = zo + oz; ans = Math.min(s1, s2); } System.out.println(ans); } } } //9 //001011011 //011010101 3 2 2 2 //9 //100010111 //101101100 2 1 3 3",0 8a729537,abd16ff0," import java.io.*; import java.util.*; public class test3 { public static void main(String[] args) throws IOException { FastReader f = new FastReader(); int t = f.nextInt(); while(t-->0) { int n = f.nextInt(); int C[] = new int[n]; long ans=Long.MAX_VALUE,pre = 0; PriorityQueue epq = new PriorityQueue(); PriorityQueue opq = new PriorityQueue(); for(int i = 0;i0) { ans = Math.min(ans,pre+ ((long)n-(long)epq.size())*(long)epq.peek() + ((long)n-(long)opq.size())*(long)opq.peek()); } } System.out.println(ans); } } static boolean isPrime(int n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static void print(int x,int y,int d,int n) { int i = 0; System.out.print(x+"" ""+y+"" ""); for(int j = x+d;j0;j-=d) { if(i==n)return; System.out.print(j+"" ""); i++; } for(int j = y+d;j<1000000000;j+=d) { if(i==n)return; System.out.print(j+"" ""); i++; } } static int prime(int n){ int ret = 0; while(n%2==0){ ret++; n/=2; } for(int i=3;i<=Math.sqrt(n);i+=2){ while(n%i==0){ ret++; n/=i; } } if(n>2)ret++; return ret; } static long nCr(int n, int r) { if(n 0; j--) C[j] = (C[j] + C[j-1]); } return C[r]; } static int power(int a,int n, int p) { int res = 1; a = a % p; while (n > 0) { if ((n & 1) == 1) res = (res * a) % p; n = n >> 1; a = (a * a) % p; } return res; } static boolean isPrime(int n, int k) { if (n <= 1 || n == 4) return false; if (n <= 3) return true; while (k > 0) { int a = 2 + (int)(Math.random() % (n - 4)); if (power(a, n - 1, n) != 1) return false; k--; } return true; } static long GCD(long a,long b) { if(a%b==0)return b; else return GCD(b,a%b); } static ArrayList readArray(FastReader f,int size){ ArrayList ret = new ArrayList(); for(int i=0;i0){ 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 74b5ad88,dc575a6c,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.StringTokenizer; public class A { static int[][] A; static long[][] B; static ArrayList> ar; public static void main(String[] args) { CP sc =new CP(); int tt = sc.nextInt(); while (tt-- > 0) { int n = sc.nextInt(); ar = new ArrayList<>(); for(int i=0;i()); A = new int[n+1][2]; B = new long[n+1][2]; for(int i=1;i[]adj; static int[][]arr; 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()); } } public static void main(String[] args) { FastScanner sc=new FastScanner(); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); arr=new int[n][2]; adj=new ArrayList[n]; for(int i=0;i(); } for(int i=0;i[] readGraphOneIndexed(int n, int m) { List[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); adj[u].add(v); adj[v].add(u); } return adj; } List[] readGraphZeroIndexed(int n, int m) { List[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u].add(v); adj[v].add(u); } return adj; } /* A more efficient way of building a graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]] = end2[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]] = end1[i]; idxForEachNode[end2[i]]++; } return adj; } } }","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); }",0 baa8e497,cc9230d3,"import java.util.*; import java.io.*; public class Solution { public static void main(String str[]) throws IOException{ Reader sc = new Reader(); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); ArrayList tt = 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(); } } } class Pair{ long av; long bv; Pair(long av, long bv){ this.av = av; this.bv = bv; } } class Tree{ int l; int r; int i; ArrayList al = new ArrayList<>(); Tree(int l, int r, int i){ this.l = l; this.r = r; this.i = i; } } ","import java.io.BufferedReader; import java.util.StringTokenizer; import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; public class C { static int[][] lr = new int[2][(int)2e5+10]; static long[][] dp = new long[2][(int)2e5+10]; static ArrayList> g; public static void main(String[] args) { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out, true); int cases = fr.nextInt(); for(int c = 0; c < cases; c++) { int nodes = fr.nextInt(); g = new ArrayList>(); for(int i = 1; i <= nodes; i++) { lr[0][i] = fr.nextInt(); lr[1][i] = fr.nextInt(); } for(int i = 0; i <= nodes; i++) { g.add(new ArrayList()); } for(int i = 0; i < nodes-1; i++) { int f = fr.nextInt(); int t = fr.nextInt(); g.get(f).add(t); g.get(t).add(f); } DFS(1, -1); out.write(Math.max(dp[0][1], dp[1][1]) + ""\n""); } out.close(); } static void DFS(int v, int p) { dp[0][v] = dp[1][v] = 0; for(Integer u : g.get(v)) { if (u == p) continue; DFS(u, v); dp[0][v] += Math.max(Math.abs(lr[0][v] - lr[1][u]) + dp[1][u], dp[0][u] + Math.abs(lr[0][v] - lr[0][u])); dp[1][v] += Math.max(Math.abs(lr[1][v] - lr[1][u]) + dp[1][u], dp[0][u] + Math.abs(lr[1][v] - lr[0][u])); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { this.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 6e7cd58b,7279bcb3,"import java.util.*; import java.io.*; public class CodeforcesRound734 { static FastReader sc = new FastReader(); public static void main(String[] args) throws IOException { try { int t = sc.nextInt(); while (t-- > 0) { // A(); // B1(); // B2(); C(); } } catch (Exception e) { // return; e.printStackTrace(); } } static void C() { int n = sc.nextInt(); int a[][] = new int[5][n]; for (int i = 0; i < n; i++) { String s = sc.next(); for (int j = 0; j < s.length(); j++) { if (s.charAt(j) == 'a') a[0][i]++; else a[0][i]--; if (s.charAt(j) == 'b') a[1][i]++; else a[1][i]--; if (s.charAt(j) == 'c') a[2][i]++; else a[2][i]--; if (s.charAt(j) == 'd') a[3][i]++; else a[3][i]--; if (s.charAt(j) == 'e') a[4][i]++; else a[4][i]--; } } for (int x[] : a) { Arrays.sort(x); } int ans = 0; for (int i = 0; i < 5; i++) { int temp = 0, cnt = 0; for (int j = n - 1; j >= 0; j--) { temp += a[i][j]; if (temp <= 0) break; else cnt++; } ans = Math.max(ans, cnt); } System.out.println(ans); } static void B2() { int n = sc.nextInt(); int k = sc.nextInt(); ArrayList> al = new ArrayList<>(); for (int i = 0; i <= n; i++) { al.add(new ArrayList<>()); } int ans[] = new int[n + 1]; for (int i = 0; i < n; i++) { int x = sc.nextInt(); al.get(x).add(i + 1); } ArrayList temp = new ArrayList<>(); for (ArrayList ind : al) { if (ind.size() >= k) { for (int i = 0; i < k; i++) { ans[ind.get(i)] = i + 1; } } else { temp.addAll(ind); } } int color = 0; for (int i = 0; i < temp.size() / k; i++) { for (int j = i * k; j < i * k + k; j++) { ans[temp.get(j)] = ++color; if (color == k) color = 0; } } for (int i = 1; i <= n; i++) { System.out.print(ans[i] + "" ""); } System.out.println(); } static void B1() { String s = sc.next(); HashMap map = new HashMap<>(); for (char ch : s.toCharArray()) { map.put(ch, map.getOrDefault(ch, 0) + 1); } if (s.length() < 2) { System.out.println(0); return; } int s1 = 0; for (int v : map.values()) { if (v > 2) { s1 += 2; } else { s1 += v; } } System.out.println(s1 / 2); } static void A() { int n = sc.nextInt(); if (n % 3 == 0) { System.out.println(n / 3 + "" "" + n / 3); } else if (n % 3 == 1) { System.out.println((n / 3 + 1) + "" "" + (n / 3)); } else { System.out.println((n / 3) + "" "" + (n / 3 + 1)); } } static boolean[] seiveOfEratosthenes(int n) { boolean[] isPrime = new boolean[n + 1]; Arrays.fill(isPrime, true); for (int i = 2; i * i <= n; i++) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } return isPrime; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static boolean isPrime(long n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } } 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()); } int[] readIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(next()); } return a; } void printIntArray(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + "" ""); } } long[] readLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = Long.parseLong(next()); } return a; } void printLongArray(long a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + "" ""); } } 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 main { public static final Scanner sc = new Scanner(System.in); public static int gcd(int a,int b) { return b==0?a:gcd(b, a%b); } public static int max(int a,int b) { if(a>b) return a; else return b; } public static void solve(){ int n=sc.nextInt(); int[][] a=new int[5][n]; for(int i=0;i=0;i--){ tmp+=a[j][i]; if(tmp<=0) break; else cnt++; } ans=max(ans,cnt); } System.out.println(ans); } public static void main(String[] args) { int T=1; T=sc.nextInt(); while(--T>=0) solve(); } } ",1 1500a4fa,d7a8434f," import java.io.*; import java.util.*; public class GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); String a=sc.next(); String b=sc.next(); int i; int zero=0,one=0; int x=0,y=0,x1=0,y1=0; for(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); } } } } ",0 25151980,3b5cec19,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class C { public static void main(String[] args) { FastScanner fs=new FastScanner(); int T=fs.nextInt(); for (int tt=1; tt<=T; tt++) { int n=fs.nextInt(); int m=fs.nextInt(); int x=fs.nextInt(); int arr[]=fs.readArray(n); if(m>n) { System.out.println(""NO""); continue; } int copy[]=new int [n]; for(int i=0;i container[]= new ArrayList[x+1]; for(int i=1;i<=x;i++) { container[i]=new ArrayList(); } for(int i=0;i{ int wt; int idx; Pair(int x,int y){ this.wt=x; this.idx=y; } @Override public int compareTo(Pair x){ return this.wt-x.wt; } public String toString(){ return ""(""+wt+"" ""+idx+"")""; } } public static void main (String[] args) throws java.lang.Exception{ FastReader scan=new FastReader(); int t=scan.nextInt(); while(t-->0){ int n=scan.nextInt(); int m=scan.nextInt(); int x=scan.nextInt(); int[]arr=new int[n]; for(int i=0;iq=new PriorityQueue<>(); for(int i=1;i<=m;i++){ q.add(new Pair(0,i)); } System.out.println(""YES""); for(int i=0;i= 0; i--) ans[i] = Math.min(ans[i], ans[i + 1] + 1); for (int ele : ans) System.out.print(ele + "" ""); System.out.println(); } } ","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 df594a00,fcbe7917,"import java.io.*; import java.util.*; public class Codeforces { 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) { br.readLine(); String[] str = br.readLine().split("" ""); int n = Integer.parseInt(str[0]); int k = Integer.parseInt(str[1]); int[] a = new int[k]; int[] t = new int[k]; str = br.readLine().split("" ""); for(int i=0; i=0; i--) { right[i] = Math.min(right[i], minright); minright = right[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : right[i]+1; } for(int i=0; i0){ String gap=br.readLine(); st=new StringTokenizer(br.readLine()); int n= Integer.parseInt(st.nextToken()); int k= Integer.parseInt(st.nextToken()); int pos[]=new int[k]; st=new StringTokenizer(br.readLine()); for(int i=0;i=0;i--){ if(min==Integer.MAX_VALUE){ min=ans[i]; } else{ min=Math.min(min+1,ans[i]); } r[i]=min; } 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 scan = new Scanner(System.in); int t = scan.nextInt(); for (int i = 0; i < t; i++) { int n = scan.nextInt(); ArrayList> 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; } } ",0 28c2d81a,fa484fdd," import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class D { public static void main(String[] args) { FastScanner fs = new FastScanner(); int cases = fs.nextInt(); while(cases-->0){ int n = fs.nextInt(), k = fs.nextInt(); int[] positions = fs.readArray(k), temps = fs.readArray(k); int[] forced = new int[n]; Arrays.fill(forced, Integer.MAX_VALUE/2); for(int i=0; i=0; i--) forced[i] = Math.min(forced[i], forced[i+1]+1); for(int i=0; i ""+a); } public static void debug(long a, long b){ System.out.println(""--> ""+ a +"" + "" + b); } public static void debug(char a, char b){ System.out.println(""--> ""+ a +"" + "" + b); } public static void debug(int[] array){ System.out.print(""Array--> ""); System.out.println(Arrays.toString(array)); } public static void debug(char[] array){ System.out.print(""Array--> ""); System.out.println(Arrays.toString(array)); } public static void debug(HashMap map){ System.out.print(""Map--> ""+map.toString()); } } } ","//import java.io.IOException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class AirConditioners { static InputReader inputReader=new InputReader(System.in); static void solve() { int n=inputReader.nextInt(); int k=inputReader.nextInt(); int pos[]=inputReader.nextIntArray(k); int power[]=inputReader.nextIntArray(k); int answer[]=new int[n]; Arrays.fill(answer,(int)(2e9)); 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 6f02c6d9,c9159d9c," 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 SolutionB { public static long gcd(long a, long b){ if(b==0){ return a; } return gcd(b, a%b); } public static long gcdSum(long b){ long a = 0; long temp = b; while(temp!=0){ a = a + temp%10; temp = temp/10; } return gcd(a,b); } public static class Pair{ Long a; Long b; public Pair(Long a, Long b) { this.a = a; this.b = b; } } public static long factorial (long n){ if(n==0) return 1; else if(n==1) return n; return n * factorial(n-1); } public static long lcm (long n){ if(n<3) return n; return lcmForBig(n,n-1); } private static long lcmForBig(long n, long l) { if(l==1) return n; n = (n * l) / gcd(n, l); return lcmForBig(n, l-1); } public static void main(String[] args){ Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int i =0;i blue = new ArrayList<>(); ArrayList red = new ArrayList<>(); for(int j=0;jcounter){ counter++; } else{ ans=""NO""; break; } } if(ans==""NO""){ System.out.println(ans); } else{ int tempCounter = n+1; Collections.sort(red); for(int j=red.size()-1;j>=0;j--){ int current = red.get(j); if(current>=tempCounter){ ans=""NO""; break; } else{ tempCounter--; } } if(tempCounter-counter!=1) System.out.println(""NO""); else System.out.println(ans); } } return; } } ",0 548ffb07,921b6e4a,"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(); } } ","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 5766f176,e7a997b5,"import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { // write your code here boolean readFromLocal = true; //readFromLocal = false; String filepath = ""src/input.txt""; //FileInputStrviseam fileInputStream = new FileInputStream(filepath); InputReader inputReader = new InputReader(System.in); Solve s = new Solve(); s.solve(inputReader); } } class Solve { public void solve(InputReader inputReader) { int t,n; t = inputReader.nextInt(); while (t>0) { t--; n = inputReader.nextInt(); Graph g = new Graph(n); g.res = new int[n-1]; for(int i=1;i { @Override public int compare(Pair o1, Pair o2) { return (int) (o1.first - o2.first); } } class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private 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 peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } 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 long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long 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 String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } 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; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public BigInteger readBigInteger() { try { return new BigInteger(nextString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return nextString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public int[] nextSortedIntArray(int n) { int[] array = nextIntArray(n); Arrays.sort(array); return array; } public int[] nextSumIntArray(int n) { int[] array = new int[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; ++i) array[i] = nextLong(); return array; } public long[] nextSumLongArray(int n) { long[] array = new long[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextSortedLongArray(int n) { long[] array = nextLongArray(n); Arrays.sort(array); return array; } } class Pair implements Comparable { long first, second; public Pair(long first, long second) { this.first = first; this.second = second; } @Override public int compareTo(Pair o) { return (int) (this.second - o.second); } } class Utils { static void swap(int[] res, int i, int j) { int temp = res[i]; res[i] = res[j]; res[j] = temp; } static long pow(long num, int n, long mod) { long res = num % mod; while (n > 0) { if ((n & 1) != 0) { res = (res * num) % mod; } num = (num * num) % mod; n >>= 1; } return res; } } class Graph { public ArrayList[] adj; int size; boolean[] vis; public int [] res; Graph(int n){ this.size = n; this.adj = new ArrayList[n+1]; this.vis = new boolean[n+1]; for (int i = 0; i <=n; i++) { adj[i] = new ArrayList<>(); } } public void addEdge(int a, int b, int edge,boolean biDirectional){ adj[a].add(new int[]{b, edge}); if (biDirectional) { adj[b].add(new int[]{a, edge}); } } public void dfs(int node,int parent,int prime){ for (int[] nodePair: adj[node]) { if (nodePair[0]!=parent){ res[nodePair[1]] = prime; dfs(nodePair[0],node,prime^1); } } } boolean hasDegreeMoreThanTwo() { for (int i = 0; i <= size; i++) { if (adj[i].size()>2){ return true; } } return false; } } ","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 2b7b2d45,a0b406e6,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class D_1552 { 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 fs = new FastReader(); int tc = fs.nextInt(); while(tc-- > 0) { boolean flag=false; int n=fs.nextInt(); int[] ar=new int[n]; for(int i=0;i{ public int compare(obj o1,obj o2){ return o1.a1>o2.a1?1:-1; } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1>>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 long power(long x,long y,long p){ String b1=""""+y; long d=p; long reminderb=0; for(int i=0;i0){ if((y&1)>0) res=(res*x)%p; y=y>>1; x=(x*x)%p; } return res; } static boolean isComposite(int n) { // Corner cases if (n <= 1) System.out.println(""False""); if (n <= 3) System.out.println(""False""); // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return true; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return true; return false; } //static ArrayList[] a; // static int[] vis; //static int[] col; /* static boolean dfs(int i,int c){ vis[i]=1; col[i]=c; //System.out.println(i+"" ""+c); for(int j:a[i]){ if(vis[j]==0) { if (dfs(j, c ^ 1) == false) return false; } else if(col[i]==col[j]) return false; } return true; }*/ static int[] m; static int solve(int p,long d){ if(p<1) return 0; if(m[p]!=0) return m[p]; if(p==1) return 1; long to=0; for(int i=1;i<=p-1;i++){ to+=solve(i,d); to=to%d; } for(int i=p;i>=2;i--){ to+=solve(p/i,d); to=to%d; } m[p]=(int)to; return m[p]; } static long[][] dis; // static long d=1000000007; public static long solve(long[] arr,int i,int j){ if(i>j){ return 0; } if(dis[i][j]!=-1) { return dis[i][j]; } if(i==j){ return arr[i]; } long c=0; c=Math.max(arr[i]+Math.min(arr[j-1]+solve(arr,i+1,j-1),arr[i+1]+solve(arr,i+2,j)),arr[j]+Math.min(solve(arr,i+1,j-1),solve(arr,i,j-2))); dis[i][j]=c; return c; } public static void main(String[] args) { FastReader s = new FastReader(); //InputStream inputStream = System.in; OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); //Scanner s=new Scanner(System.in); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); int m = s.nextInt(); int x=s.nextInt(); long[] a = new long[n]; for(int i=0;i p = new PriorityQueue<>(new sortby()); for (int i = 0; i { int val; int idx; Pair(int a, int b){ this.val = a; this.idx = b; } public int compareTo(Pair o){ return this.val - o.val; } } public static void main(String[] args) { // CF1 // Scanner sc = new Scanner(System.in); // int t = sc.nextInt(); // for (int i = 0; i < t; i++) { // int n = sc.nextInt(); // int x = sc.nextInt(); // int[] arr = new int[n]; // int sum = 0; // for (int j = 0; j < n; j++) { // arr[j] = sc.nextInt(); // sum += arr[j]; // } // if (x == sum) { // System.out.println(""NO""); // } else if (x > sum) { // System.out.println(""YES""); // for (int j = 0; j < n; j++) { // System.out.print(arr[j] + "" ""); // } // System.out.println(); // } else {// x pq=new PriorityQueue<>(); for (int j = 0; j < m; j++) { pq.add(new Pair(arr[j],j)); ans[j]=j; } for (int j = m; j < n; j++) { Pair p=pq.remove(); pq.add(new Pair(p.val+arr[j],p.idx)); ans[j]=p.idx; } System.out.println(""YES""); printArr(ans); } } }",1 8a041ef5,bdf7bfb2,"// package com.company; import java.util.*; import java.io.*; import java.lang.*; import java.util.jar.JarEntry; public class Main{ /* || श्री गणेशाय नमः || @𝐚𝐮𝐭𝐡𝐨𝐫 𝐉𝐢𝐠𝐚𝐫 𝐍𝐚𝐢𝐧𝐮𝐣𝐢 𝐒𝐕𝐍𝐈𝐓- 𝐒𝐮𝐫𝐚𝐭 */ public static void main(String args[]){ InputReader in=new InputReader(System.in); TASK solver = new TASK(); int t=1; t = in.nextInt(); for(int i=1;i<=t;i++) { solver.solve(in,i); } } static class TASK { void solve(InputReader in, int testNumber) { int n = in.nextInt(); int a[][] = new int[n][5]; for(int i=0;i=0;i--) { sum+=x[i]; if(sum<=0) break; c++; } max=Math.max(max,c); } System.out.println(max); } } static class pair{ int x ; int y; pair(int x,int y) { this.x=x; this.y=y; } } static class Maths { static long gcd(long a, long b) { if (a < b) return gcd(b,a); if (b == 0) return a; else return gcd(b,a%b); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private 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 String nextLine() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } 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 long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long 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 double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } 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 String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } } ","import java.util.*; import java.io.*; public class Main { 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(); String[] s = new String[n]; for(int i=0; i pq = new PriorityQueue<>(Collections.reverseOrder()); //Big comes in top; for(int i=0; i 0){ cur += pq.poll(); numberOfWords++; }else{ break; } } MAX = Math.max(MAX, numberOfWords); } pw.println(MAX); } 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 2eb89317,5f0a176e,"import java.util.*; import java.io.*; ////*************************************************************************** /* public class E_Gardener_and_Tree implements Runnable{ public static void main(String[] args) throws Exception { new Thread(null, new E_Gardener_and_Tree(), ""E_Gardener_and_Tree"", 1<<28).start(); } public void run(){ WRITE YOUR CODE HERE!!!! JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!! } } */ /////************************************************************************** public class C_Menorah{ public static void main(String[] args) { FastScanner s= new FastScanner(); //PrintWriter out=new PrintWriter(System.out); //end of program //out.println(answer); //out.close(); StringBuilder res = new StringBuilder(); int t=s.nextInt(); int p=0; while(p=Integer.MAX_VALUE){ ans=-1; } res.append(ans+"" \n""); } } } p++; } System.out.println(res); } private static long solve( long correct1, long correct0, long wrong1, long wrong0,long a) { long op1=Integer.MAX_VALUE; long op2=Integer.MAX_VALUE; if(wrong1==0 && wrong0==0){ return 0; } if(a==1){ { // using correct1 if(correct1>0){ long newcorrect1=1+wrong0; long newcorrect0=wrong1; long newwrong1=correct0; long newwrong0=correct1-1; op1=(1+solve(newcorrect1,newcorrect0,newwrong1,newwrong0,0)); } } } else{ { //using wrong1 { if(wrong1>0){ long newcorrect1=wrong0; long newcorrect0=wrong1-1; long newwrong1=1+correct0; long newwrong0=correct1; op2=(1+solve(newcorrect1,newcorrect0,newwrong1,newwrong0,1)); } } } } long ans=Math.min(op1,op2); return ans; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { 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 long modpower(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with 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; } // SIMPLE POWER FUNCTION=> 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 * x; // y must be even now y = y >> 1; // y = y/2 x = x * x; // Change x to x^2 } return res; } }"," import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.*; import java.util.concurrent.ThreadLocalRandom; public class a729 { public static void main(String[] args) throws IOException { // try { BufferedWriter out = new BufferedWriter( new OutputStreamWriter(System.out)); BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); PrintWriter pt = new PrintWriter(System.out); FastReader sc = new FastReader(); int t = sc.nextInt(); for(int o = 0 ; oset = new HashSet(); int f = 0; int id = 0; ArrayList al = new ArrayList(); for(int i = 0 ; i=val) { // System.out.println(arr[i][j] + "" "" + i + "" "" + j); set.add(j); al.add(arr[i][j]); } } id = i; if(set.size()>=2) { f = 1; break; }else { set.clear(); } } if(f == 0) { return false; } // System.out.println(set); // System.out.println(al); // int g = 1; for(int i = 0 ; i=val) { h = 1; break; } } if(h == 0) { return false; } } return true; } static int cntDivisors(int n){ int cnt = 0; for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { if (n/i == i) cnt++; else cnt+=2; } } return cnt; } public static long findgcd(long[] arr,int v) { int n = arr.length; long g = arr[v]; for(int i = v ; i=l && hi<=r) { for(int i = 0 ; i<27;i++) { ans[i]+= seg[idx][i]; } return ; } if(hir) { return; } int mid = (lo + hi)/2; query(seg,ans,idx*2 +1, lo, mid, l, r); query(seg,ans,idx*2 + 2, mid + 1, hi, l, r); //return Math.min(left, right); } //// for sum // public static void update(int[][]seg,char[]arr,int idx, int lo , int hi , int node , char val) { if(lo == hi) { // seg[idx] += val; seg[idx][val-'a']++; seg[idx][arr[node]-'a']--; }else { int mid = (lo + hi )/2; if(node<=mid && node>=lo) { update(seg,arr, idx * 2 +1, lo, mid, node, val); }else { update(seg,arr, idx*2 + 2, mid + 1, hi, node, val); } //seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2]; for(int i = 0 ; i<27;i++) { seg[idx][i] = seg[2*idx+1][i] + seg[2*idx + 2][i]; } } } public static int lower_bound(int array[], int low, int high, int key){ // Base Case if (low > high) { return low; } // Find the middle index int mid = low + (high - low) / 2; // If key is lesser than or equal to // array[mid] , then search // in left subarray if (key <= array[mid]) { return lower_bound(array, low, mid - 1, key); } // If key is greater than array[mid], // then find in right subarray return lower_bound(array, mid + 1, high, key); } public static int upper_bound(int arr[], int low, int high, int key){ // Base Case if (low > high || low == arr.length) return low; // Find the value of middle index int mid = low + (high - low) / 2; // If key is greater than or equal // to array[mid], then find in // right subarray if (key >= arr[mid]) { return upper_bound(arr, mid + 1, high, key); } // If key is less than array[mid], // then find in left subarray return upper_bound(arr, low, mid - 1, key); } // ----------------------------------------------------------------------------------------------------------------------------------------------- public static long gcd(long a, long b){ if (a == 0) return b; return gcd(b % a, a); } //-------------------------------------------------------------------------------------------------------------------------------------------------------- public static long modInverse(long a, long m){ long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient long q = a / m; long t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } //----------------------------------------------------------------------------------------------------------------------------------- //segment tree //for finding minimum in range // public static void build(int [] seg,int []arr,int idx, int lo , int hi) { // if(lo == hi) { // seg[idx] = arr[lo]; // return; // } // int mid = (lo + hi)/2; // build(seg,arr,2*idx+1, lo, mid); // build(seg,arr,idx*2+2, mid +1, hi); // seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]); // } ////for finding minimum in range //public static int query(int[]seg,int idx , int lo , int hi , int l , int r) { // if(lo>=l && hi<=r) { // return seg[idx]; // } // if(hir) { // return Integer.MAX_VALUE; // } // int mid = (lo + hi)/2; // int left = query(seg,idx*2 +1, lo, mid, l, r); // int right = query(seg,idx*2 + 2, mid + 1, hi, l, r); // return Math.min(left, right); //} // // for sum // //public static void update(int[]seg,int idx, int lo , int hi , int node , int val) { // if(lo == hi) { // seg[idx] += val; // }else { //int mid = (lo + hi )/2; //if(node<=mid && node>=lo) { // update(seg, idx * 2 +1, lo, mid, node, val); //}else { // update(seg, idx*2 + 2, mid + 1, hi, node, val); //} //seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2]; // //} //} //--------------------------------------------------------------------------------------------------------------------------------------- static void shuffleArray(long[] 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 long a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } //----------------------------------------------------------------------------------------------------------------------------------------------------------- } 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; } } //------------------------------------------------------------------------------------------------------------------------------------------------------------ class SegmentTree{ int n; public SegmentTree(int[] arr,int n) { this.arr = arr; this.n = n; } int[] arr = new int[n]; // int n = arr.length; int[] seg = new int[4*n]; void build(int idx, int lo , int hi) { if(lo == hi) { seg[idx] = arr[lo]; return; } int mid = (lo + hi)/2; build(2*idx+1, lo, mid); build(idx*2+2, mid +1, hi); seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]); } int query(int idx , int lo , int hi , int l , int r) { if(lo<=l && hi>=r) { return seg[idx]; } if(hir) { return Integer.MAX_VALUE; } int mid = (lo + hi)/2; int left = query(idx*2 +1, lo, mid, l, r); int right = query(idx*2 + 2, mid + 1, hi, l, r); return Math.min(left, right); } } //----------------------------------------------------------------------------------------------------------------------------------------------------------- class coup{ char a; int b; public coup(char a , int b) { this.a = a; this.b = b; } } class trip{ int a , b, c; public trip(int a , int b, int c) { this.a = a; this.b = b; this.c = c; } } //--------------------------------------------------------------------------------------------------------------------------------------------------------------- ",0 1adac634,a7e7f371,"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; static long ans; public static void main(String[] args) { t = fs.nextInt(); while (t-- > 0) { n = fs.nextInt(); int [] k = fs.readArray(n); int [] h = fs.readArray(n); List arr = new ArrayList<>(); for(i=0;i comp = new ArrayList<>(); int temp = arr.get(0).x; int tempy = arr.get(0).y; for(i=1;i tempy){ comp.add(new Pair(temp, tempy)); temp = arr.get(i).x; tempy = arr.get(i).y; } else tempy = Math.max(tempy, arr.get(i).y); } comp.add(new Pair(temp, tempy)); for(i=0;i { 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); } } }"," import java.io.*; import java.util.*; public class cp { static int mod=(int)1e9+7; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; static int[] arInt; static long[] arLong; public static void main(String[] args) throws IOException { long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); // int tc=1; // primeSet=new HashSet<>(); // 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; } ",0 18e2441c,b728ba1d," import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.Map; import java.util.HashMap; public class cf1515 { 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(); } static class Task { public void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- != 0) { int n = in.nextInt(); int m = in.nextInt(); int x = in.nextInt(); TreeMap> map = new TreeMap<>(); for (int i = 0; i < n; i++) { int j = in.nextInt(); if (!map.containsKey(j)) { map.put(j, new ArrayList()); } map.get(j).add(i); } out.println(""YES""); int[] ans = new int[n]; int sta = 0; for (int s : map.keySet()) { for (int i = 0; i < map.get(s).size(); i++) { ans[map.get(s).get(i)] = (sta++) % m + 1; } } for(int i=0;i> a = new ArrayList<>(); for (int i = 0; i < n; ++i) { a.add(new Pair<>(in.nextInt(), i)); } Collections.sort(a); int[] ans = new int[n]; int[] sum = new int[m]; int j = 1; for (int i = 0; i < n; ++i) { ans[a.get(i).y] = j; sum[j - 1] += a.get(i).x; j++; if (j == m + 1) j = 1; } for (int i = 1; i < m; ++i) { if (Math.abs(sum[i - 1] - sum[i]) > k) { out.println(""NO""); } } out.println(""YES""); for (int e : ans) { out.print(e + "" ""); } out.println(); } } static class Pair implements Comparable> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int compareTo(Pair o) { int value = ((Comparable) x).compareTo(o.x); if (value != 0) return value; return ((Comparable) y).compareTo(o.y); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return x.equals(pair.x) && y.equals(pair.y); } public int hashCode() { return Objects.hash(x, y); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(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 String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } 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); } } } ",0 0df4050e,d9199dfd,"import java.io.*; import java.util.*; public class MainClass { public static void main(String[] args) { Reader in = new Reader(System.in); int t = in.nextInt(); StringBuilder stringBuilder = new StringBuilder(); while (t-- > 0) { ArrayList reds = new ArrayList<>(); ArrayList blue = new ArrayList<>(); int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt() - 1; } char[] s = in.next().toCharArray(); for (int i = 0; i < n; i++) { if (s[i] == 'R') { reds.add(a[i]); } else { blue.add(a[i]); } } Collections.sort(reds, Collections.reverseOrder()); Collections.sort(blue); boolean ff = true; int start = 0; for (int i = 0; i < blue.size(); i++) { if (blue.get(i) < start) { ff = false; break; } start++; } start = n - 1; for (int i = 0; i < reds.size(); i++) { if (reds.get(i) > start) { ff = false; break; } start--; } stringBuilder.append(ff?""YES"":""NO"").append(""\n""); } System.out.println(stringBuilder); } } class Reader { public BufferedReader reader; public StringTokenizer tokenizer; public Reader(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()); } public long nextLong() { return Long.parseLong(next()); } }","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(); } } ",1 29cb9b0e,c77654b8,"import java.util.*; public class Main { public static void solve (int ar[],int n) { if(n%2==1) { int c=ar[2]; int b=ar[1]; int a=ar[0]; if(a+b!=0) { ar[2]=-(a+b); ar[1]=c; ar[0]=c; } else if(b+c!=0) { ar[2]=a; ar[1]=a; ar[0]=-(b+c); } else { ar[2]=b; ar[1]=-(a+c); ar[0]=b; } System.out.print(ar[0]+"" ""+ar[1]+"" ""+ar[2]+"" ""); for(int i=3;i<(n);i=i+2) { System.out.print((-ar[i+1])+"" ""+ar[i]+"" ""); } System.out.println(); } // System.out.println(ans[n-1]+ "" x""+ans[n-2]+"" x""+ans[n-3]); else { for(int i=0;i<(n);i=i+2) { System.out.print((-ar[i+1])+"" ""+ar[i]+"" ""); } System.out.println(); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in);int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int ar[]=new int[n]; for(int i=0;i0){ int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i0){ int n = ni(); l = new long[n]; r = new long[n]; for(int i=0; i adj[] = new ArrayList[n]; for(int i=0; i(); for(int i=0; i adj[], int vertex, int prev, boolean visited[]){ visited[vertex] = true; if(dp[vertex][prev] != null) return dp[vertex][prev]; long ans = 0; for(int x : adj[vertex]){ if(!visited[x]){ if(prev == 0){ ans += Math.max(Math.abs(l[vertex] - l[x]) + solve(adj, x, 0, visited), Math.abs(l[vertex] - r[x]) + solve(adj, x, 1, visited)); //pn(vertex + "" "" + x + "" "" + ans); }else{ ans += Math.max(Math.abs(r[vertex] - l[x]) + solve(adj, x, 0, visited), Math.abs(r[vertex] - r[x]) + solve(adj, x, 1, visited)); //pn(vertex + "" "" + x + "" "" + ans); } } } visited[vertex] = false; //pn(ans); return dp[vertex][prev] = ans; } } ","import java.util.*; import java.io.*; public class Parsas_Humongous_Tree { 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 shuffle(int[] a) { Random r = new Random(); for (int i = 0; i <= a.length - 2; i++) { int j = i + r.nextInt(a.length - i); swap(a, i, j); } } public static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void main(String[] args) { // TODO Auto-generated method stub FastReader t = new FastReader(); PrintWriter o = new PrintWriter(System.out); int test = t.nextInt(); while (test-- > 0) { int n = t.nextInt(); long[][] cost = new long[n][2]; List[] graph = new ArrayList[n]; dp = new long[n][2]; for (int i = 0; i < n; ++i) { cost[i][0] = t.nextLong(); cost[i][1] = t.nextLong(); graph[i] = new ArrayList<>(); } for (int i = 0; i < n - 1; ++i) { int x = t.nextInt() - 1; int y = t.nextInt() - 1; graph[x].add(y); graph[y].add(x); } o.println(Math.max(dfs(graph, cost, 0, 0, -1), dfs(graph, cost, 0, 1, -1))); } o.flush(); o.close(); } private static long[][] dp; private static long dfs(List[] graph, long[][] cost, int u, int j, int par) { if (dp[u][j] != 0) return dp[u][j]; for (int v : graph[u]) if (v != par) { long c1 = Math.abs(cost[u][j] - cost[v][0]) + dfs(graph, cost, v, 0, u); long c2 = Math.abs(cost[u][j] - cost[v][1]) + dfs(graph, cost, v, 1, u); dp[u][j] += Math.max(c1, c2); } return dp[u][j]; } }",0 76ad805a,f229aa7f,"import java.util.Arrays; import java.util.Scanner; public class First { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); String a = sc.next(); String b = sc.next(); char ch1[] = a.toCharArray(); char ch2[] = b.toCharArray(); int zz = 0; int oz = 0; int zo = 0; int oo = 0; for (int i = 0; i < n; i++) { if (ch1[i] == '0') { if (ch2[i] == '0') { zz += 1; } else { oz += 1; } } else { if (ch2[i] == '0') { zo += 1; } else { oo += 1; } } } int ans = -1; if ((oo - zz) == 1 || zo == oz) { int s1 = (int) 1e7; int s2 = (int) 1e7; if ((oo - zz) == 1) { s1 = oo + zz; } if (zo == oz) s2 = zo + oz; ans = Math.min(s1, s2); } System.out.println(ans); } } } //9 //001011011 //011010101 3 2 2 2 //9 //100010111 //101101100 2 1 3 3","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 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;i0) { tt++; solve(); } w.close(); } static void solve() throws IOException { int n = sc.ni(); String s1 = sc.ns(); String s2 = sc.ns(); char[] strr = s1.toCharArray(); char[] strr2 = s2.toCharArray(); HashSet lia = new HashSet<>(), lib = new HashSet<>(); for(int i = 0; i < n; i++) { if(strr[i]=='1') lia.add(i); else lib.add(i); } HashSet liaa = new HashSet<>(); for(int i = 0; i < n; i++) { if(strr2[i]=='1') liaa.add(i); } if(!(lia.size() == liaa.size() || lib.size()+1 == liaa.size())) { w.p(-1); return; } int ac = 0, bc = 0; for(int i: lia) { if(liaa.contains(i)) ac++; } bc = liaa.size()-ac; if(lia.size() == liaa.size() && lia.size() == ac) { w.p(0); return; } int nac = lia.size()-ac; int nbc = lib.size()-bc; long ansa = ac != 0 && (ac-1 == nbc)? Math.abs(ac-1+nbc): ima; long ansb = nac != 0 && (nac-1 == bc-1)? Math.abs(nac-1+bc): ima; if(ansa == ima && ansb == ima) { w.p(-1); return; } w.p(Math.min(ansa, ansb)+1); } }","import java.io.*; import java.util.*; public class A { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// static int t = 0; //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static long ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty(""ONLINE_JUDGE"") == null) { w = new Printer(new FileOutputStream(""output.txt"")); sc = new fs(new FileInputStream(""input.txt"")); } } //-----------------------------------------------------------------------// static int tt = 0; //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); t = sc.ni();while(t-->0) { tt++; solve(); } w.close(); } static void solve() throws IOException { int n = sc.ni(); String s1 = sc.ns(); String s2 = sc.ns(); char[] strr = s1.toCharArray(); char[] strr2 = s2.toCharArray(); HashSet lia = new HashSet<>(), lib = new HashSet<>(); for(int i = 0; i < n; i++) { if(strr[i]=='1') lia.add(i); else lib.add(i); } HashSet liaa = new HashSet<>(); for(int i = 0; i < n; i++) { if(strr2[i]=='1') liaa.add(i); } if(!(lia.size() == liaa.size() || lib.size()+1 == liaa.size())) { w.p(-1); return; } int ac = 0, bc = 0; for(int i: lia) { if(liaa.contains(i)) ac++; } bc = liaa.size()-ac; if(lia.size() == liaa.size() && lia.size() == ac) { w.p(0); return; } int nac = lia.size()-ac; int nbc = lib.size()-bc; long ansa = ac != 0 && (ac-1 == nbc)? Math.abs(ac-1+nbc): ima; long ansb = nac != 0 && (nac-1 == bc-1)? Math.abs(nac-1+bc): ima; if(ansa == ima && ansb == ima) { w.p(-1); return; } w.p(Math.min(ansa, ansb)+1); } }",1 a4c98ae1,ee4f7b06,"import java.io.*; import java.util.*; public class Main { static ArrayList one=new ArrayList<>(); static ArrayList zero=new ArrayList<>(); static long dp[][]= new long[5001][5001]; static long solve(int i,int j){ if (i==one.size())return 0; if (j==zero.size())return Integer.MAX_VALUE; if (dp[i][j]!=-1){ return dp[i][j]; } return dp[i][j]=Math.min(solve(i+1,j+1)+Math.abs(one.get(i)-zero.get(j)),solve(i,j+1)); } public static void main(String[] args) { FastScanner sc = new FastScanner(); // int t=sc.nextInt(); // while (t-->=1){ int n=sc.nextInt(); int a[]=sc.readArray(n); for (long i[]:dp){ Arrays.fill(i,-1); } for (int i=0;i 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 String sortString(String s) { char temp[] = s.toCharArray(); Arrays.sort(temp); return new String(temp); } static class Pair implements Comparable { String a; int b; public Pair(String a, int b) { this.a = a; this.b = b; } // to sort first part // public int compareTo(Pair other) { // if (this.a == other.a) return other.b > this.b ? -1 : 1; // else if (this.a > other.a) return 1; // else return -1; // } public int compareTo(Pair other) { // if (this.b == other.b) return 0; if (this.b > other.b) return 1; else return -1; } //sort on the basis of first part only // public int compareTo(Pair other) { // if (this.a == other.a) return 0; // else if (this.a > other.a) return 1; // else return -1; // } } static int[] frequency(String s){ int fre[]= new int[26]; for (int i=0;i0){ long lastBit=n&1; if (lastBit==1){ ans=(int)mul(ans,a); } a=(int)mul(a,a); n>>=1; } return ans; } static int[] find(int n, int start, int diff) { int a[] = new int[n]; a[0] = start; for (int i = 1; i < n; i++) a[i] = a[i - 1] + diff; return a; } static void swap(int a, int b) { int c = a; a = b; b = c; } static void printArray(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + "" ""); } } static boolean sorted(int a[]) { int n = a.length; boolean flag = true; for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) flag = false; } if (flag) return true; else return false; } public static int findlog(long n) { if (n == 0) return 0; if (n == 1) return 0; if (n == 2) return 1; double num = Math.log(n); double den = Math.log(2); if (den == 0) return 0; return (int) (num / den); } public static long gcd(long a, long b) { if (b % a == 0) return a; return gcd(b % a, a); } public static int gcdInt(int a, int b) { if (b % a == 0) return a; return gcdInt(b % a, a); } static void sortReverse(int[] a) { ArrayList l = new ArrayList<>(); for (int i : a) l.add(i); // Collections.sort.(l); Collections.sort(l, Collections.reverseOrder()); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } 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[] readArrayLong(long n) { long[] a = new long[(int) n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }"," import java.io.*; import java.util.*; import java.lang.Math; public class Main { static PrintWriter pw; static Scanner sc; static StringBuilder ans; static long mod = 1000000000+7; static void pn(final Object arg) { pw.print(arg); pw.flush(); } /*-------------- for input in an value ---------------------*/ static int ni() { return sc.nextInt(); } static long nl() { return sc.nextLong(); } static double nd() { return sc.nextDouble(); } static String ns() { return sc.next(); } static void ap(int arg) { ans.append(arg); } static void ap(long arg) { ans.append(arg); } static void ap(String arg) { ans.append(arg); } static void ap(StringBuilder arg) { ans.append(arg); } /*-------------- for input in an array ---------------------*/ static void inputIntegerArray(int arr[]){ for(int i=0; i not prime // false->prime sv[0] = sv[1] = true; sv[2] = false; for(int i = 0; i< sv.length; i++) { if( !sv[i] && (long)i*(long)i < sv.length ) { for ( int j = i*i; j 0) { if ( (b & 1) > 0){ res = (res * a)%mod; } a = (a * a)%mod; b >>= 1; } return res; } static long factorial(long n) { long res = 1, i; for (i = 2; i <= n; i++){ res = ((res%mod) * (i%mod))%mod; } return res; } static class Pair { int idx; int v; Pair(int idx, int v){ this.idx = idx; this.v = v; } } public static void main(String[] args) throws Exception { // runFile(); runIo(); int t; t = 1; // t = sc.nextInt(); ans = new StringBuilder(); while( t-- > 0 ) { solve(); } pn(ans+""""); } static int N ; static int M ; static ArrayList f; static ArrayList e; static long dp[][]; static long find(int i, int j ) { if( i == N ) return 0; if( j == M ) return Integer.MAX_VALUE; if (dp[i][j] != -1 ) return dp[i][j]; return dp[i][j] = Math.min( find(i, j+1), Math.abs(f.get(i)-e.get(j)) + find(i+1, j+1) ); } public static void solve() { int n = ni(); f = new ArrayList(); e = new ArrayList(); for(int i = 0; i { 0, 1, 5 } // 0s -> { 2, 3, 6, 7 } ",1 2eb89317,d4a3869e,"import java.util.*; import java.io.*; ////*************************************************************************** /* public class E_Gardener_and_Tree implements Runnable{ public static void main(String[] args) throws Exception { new Thread(null, new E_Gardener_and_Tree(), ""E_Gardener_and_Tree"", 1<<28).start(); } public void run(){ WRITE YOUR CODE HERE!!!! JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!! } } */ /////************************************************************************** public class C_Menorah{ public static void main(String[] args) { FastScanner s= new FastScanner(); //PrintWriter out=new PrintWriter(System.out); //end of program //out.println(answer); //out.close(); StringBuilder res = new StringBuilder(); int t=s.nextInt(); int p=0; while(p=Integer.MAX_VALUE){ ans=-1; } res.append(ans+"" \n""); } } } p++; } System.out.println(res); } private static long solve( long correct1, long correct0, long wrong1, long wrong0,long a) { long op1=Integer.MAX_VALUE; long op2=Integer.MAX_VALUE; if(wrong1==0 && wrong0==0){ return 0; } if(a==1){ { // using correct1 if(correct1>0){ long newcorrect1=1+wrong0; long newcorrect0=wrong1; long newwrong1=correct0; long newwrong0=correct1-1; op1=(1+solve(newcorrect1,newcorrect0,newwrong1,newwrong0,0)); } } } else{ { //using wrong1 { if(wrong1>0){ long newcorrect1=wrong0; long newcorrect0=wrong1-1; long newwrong1=1+correct0; long newwrong0=correct1; op2=(1+solve(newcorrect1,newcorrect0,newwrong1,newwrong0,1)); } } } } long ans=Math.min(op1,op2); return ans; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { 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 long modpower(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with 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; } // SIMPLE POWER FUNCTION=> 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 * x; // y must be even now y = y >> 1; // y = y/2 x = x * x; // Change x to x^2 } return res; } }","import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); for (int i = 0; i < t; i++) { int n = scan.nextInt(); String a = scan.next(); String b = scan.next(); int[] cChet = new int[n]; int[] cNech = new int[n]; int sumChet = 0; int sumNechet = 0; int numOnesA = 0; int numOnesB = 0; boolean soluble = true; for (int j = 0; j < n; j++) { cChet[j] = (a.charAt(j) - 48) ^ (b.charAt(j) - 48); cNech[j] = cChet[j] ^ 1; sumChet += cChet[j]; sumNechet += cNech[j]; } int INF = 100000000; int ans = INF; if (((sumChet % 2 == 1) && (sumNechet % 2 == 0)) || !(numOnesB == numOnesA || numOnesB == (n - numOnesA + 1))) { ans = -1; } else { if (sumChet % 2 == 0) { int ones = 0; int zeroes = 0; for (int j = 0; j < n; j++) { if (cChet[j] == 1) { if (a.charAt(j) == '1') { ones++; } else { zeroes++; } } } if ((ones - zeroes) == 0 || (ones - zeroes) == 1) { ans = Math.min(sumChet, ans); } } if (sumNechet % 2 == 1) { int ones = 0; int zeroes = 0; for (int j = 0; j < n; j++) { if (cNech[j] == 1) { if (a.charAt(j) == '1') { ones++; } else { zeroes++; } } } if ((ones - zeroes) == 0 || (ones - zeroes) == 1) { ans = Math.min(sumNechet, ans); } } } if (ans != INF) { System.out.println(ans); } else { System.out.println(-1); } } } public static int find(int x, int log) { int pow = (int)Math.pow(2, log); int ans = (x / (pow * 2)) * pow; if (x % (pow * 2) > pow) { ans += pow; } else { ans += (x % (pow * 2)); } return ans; } } ",0 3a12e509,e812ee0b,"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 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 { // 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(); while(t-->0) solve(); out.close(); out.flush(); } static void solve() throws IOException { int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i blue=new ArrayList(); ArrayList red=new ArrayList(); for(int i=0;ii+1+blue.size()) { out.println(""NO""); return; } } out.println(""YES""); } static class Pair implements Cloneable, Comparable { 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) { int t= this.x-a.x; if(t!=0) return t; else return 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); 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; int taxC,taxD; Node(int ver) { vertex=ver; taxD=0; taxC=0; 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; } static long binomialCoeff(long n, long k) { if(nn) // return 0; // Since C(n, k) = C(n, n-k) if (k > 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(); } } } ","import java.util.*; import java.util.concurrent.Exchanger; public class Codeforces { static int[] mass = new int[200_001]; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); StringBuilder stringBuilder = new StringBuilder(); int t = scanner.nextInt(); for (int z =0 ; z < t ; ++z) { solve(scanner, stringBuilder); } System.out.print(stringBuilder); } private static void solve(Scanner scanner, StringBuilder stringBuilder) { int n = scanner.nextInt(); for (int i = 0 ; i < n ; ++i) { mass[i] = scanner.nextInt(); } String colors = scanner.next(); List red = new ArrayList<>(n); List blue = new ArrayList<>(n); for (int i = 0 ; i < n ; ++i) { if(colors.charAt(i) == 'R') red.add(mass[i]); else blue.add(mass[i]); } red.sort(Comparator.reverseOrder()); blue.sort(Comparator.naturalOrder()); int creatd = 0; int upper = n; int down = 1; for (Integer integer : red) { if (integer > upper) { stringBuilder.append(""NO\n""); return; } else { creatd++; upper--; } } for (Integer integer : blue) { if (integer < down) { stringBuilder.append(""NO\n""); return; } else { creatd++; down++; } } if (creatd == n) stringBuilder.append(""YES\n""); else stringBuilder.append(""NO\n""); } }",0 49b94994,d8654140,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main1582D { public static void main(String[] args) { final FastScanner in = new FastScanner(System.in); final PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int[] a = new int[n]; for (int j = 0; j < n; j++) { a[j] = in.nextInt(); } int[] b = solution(a, n); for (int j = 0; j < n; j++) { out.print(b[j]); out.print("" ""); } out.println(); } out.flush(); out.close(); in.close(); } private static int[] solution(int[] a, int n) { int[] b = new int[n]; int start = 0; if (n % 2 == 1) { if (a[0] + a[1] != 0) { b[0] = -a[2]; b[1] = -a[2]; b[2] = a[0] + a[1]; } else if (a[0] + a[2] != 0) { b[0] = -a[1]; b[1] = a[0] + a[2]; b[2] = -a[1]; } else { b[0] = a[1] + a[2]; b[1] = -a[0]; b[2] = -a[0]; } start = 3; } else { b[0] = -a[1]; b[1] = a[0]; int gcd = gcd(b[0], b[1]); b[0] /= gcd; b[1] /= gcd; start = 2; } for (int i = start; i < n; i += 2) { b[i] = -a[i + 1]; b[i + 1] = a[i]; } return b; } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream)); } 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()); } double nextDouble() { return Double.parseDouble(next()); } int[] readIntArr(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = Integer.parseInt(next()); } return result; } long[] readLongArr(int n) { long[] result = new long[n]; for (int i = 0; i < n; i++) { result[i] = Long.parseLong(next()); } return result; } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } long nextLong() { return Long.parseLong(next()); } } } ","import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.text.DecimalFormat; import java.util.*; public class Codeforces { static int mod=1000000007 ; static List[] adj; static boolean vst[]; static int dp[]; public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); int t=fs.nextInt(); while(t-->0) { int n=fs.nextInt(); int arr[]=fs.readArray(n); int ans[]=new int[n]; if(n%2==0) { for(int i=0;i>1; } return res; } static long gcd(long a,long 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; i{ 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 A = new ArrayList<>(); for(int i= 0; i < n; i++){ int x = sc.nextInt(); A.add(new Pair(x,i)); } int[] sum = new int[m]; int[] ans = new int[n]; Collections.sort(A); for(int i= 0; i < n; i++){ int idx = i%m; sum[idx] += A.get(i).x; ans[A.get(i).y] = idx+1; } ruffleSort(sum); if(Math.abs(sum[0]-sum[sum.length-1])>diff){ out.println(""NO""); return; } out.println(""YES""); for(int i = 0; i < n; i++){ out.print(ans[i]+"" ""); } out.println(); } static class Pair implements Comparable { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o){ if(this.x==o.x){ return -1*(o.y-this.y); } return this.x-o.x; } @Override public String toString() { return ""Pair{"" + ""x="" + x + "", y="" + y + '}'; } public boolean equals(Pair o){ return this.x==o.x&&this.y==o.y; } } public static long mul(long a, long b) { return ((a % MOD) * (b % MOD)) % MOD; } public static long add(long a, long b) { return ((a % MOD) + (b % MOD)) % MOD; } public static long c2(long n) { if ((n & 1) == 0) { return mul(n / 2, n - 1); } else { return mul(n, (n - 1) / 2); } } //Shuffle Sort static final Random random = new Random(); static void ruffleSort(int[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n); int temp= a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } //Brian Kernighans Algorithm static long countSetBits(long n) { if (n == 0) return 0; return 1 + countSetBits(n & (n - 1)); } //Euclidean Algorithm static long gcd(long A, long B) { if (B == 0) return A; return gcd(B, A % B); } //Modular Exponentiation static long fastExpo(long x, long n) { if (n == 0) return 1; if ((n & 1) == 0) return fastExpo((x * x) % MOD, n / 2) % MOD; return ((x % MOD) * fastExpo((x * x) % MOD, (n - 1) / 2)) % MOD; } //AKS Algorithm static boolean isPrime(long 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 <= Math.sqrt(n); i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static long modinv(long x) { return modpow(x, MOD - 2); } public static long modpow(long a, long b) { if (b == 0) { return 1; } long x = modpow(a, b / 2); x = (x * x) % MOD; if (b % 2 == 1) { return (x * a) % MOD; } return x; } 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 69b8ffb2,ba468e1f,"import java.util.*; //CODE FORCES public class anshulvmc { public static void sort(int[] a) { ArrayList l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i bfs = new ArrayDeque<>(); // for(Node nn:nodes) { // if(nn.adj.size()<2) { // bfs.addLast(nn); // nn.dist=0; // } // } // // while(bfs.size()>0) { // Node nn = bfs.removeFirst(); // for(Node a : nn.adj) { // if(a.dist!=-1) continue; // a.usedDegree++; // if(a.adj.size() - a.usedDegree <= 1) { // a.dist = nn.dist+1; // bfs.addLast(a); // } // } // } // // int[] cs = new int[n+1]; // for(Node nn:nodes) { // cs[nn.dist]++; // } // for(int i=1;i adj = new ArrayList<>(); int dist = -1; int usedDegree = 0; } public static void cat_mice(int dest,int[] arr) { sort(arr); int time = dest; int timeleft = dest-1; int counter=0; for(int i=arr.length-1;i>=0;i--) { int val = arr[i]; int takes = time - val; if(takes <= timeleft) { timeleft -= takes; counter++; } } System.out.println(counter); } public static void minex(int n,int[] arr) { sort(arr); int ans=arr[0]; for(int i=0;i blue = new ArrayList<>(); ArrayList red = new ArrayList<>(); for(int i=0;i= i+1) { } else { return false; } } for(int i=0;i i+1 + blue.size()) { // System.out.println(red.get(i)+"" ""+(i+1 + blue.size())); return false; } } return true; } public static void main(String args[]) { Scanner scn = new Scanner(System.in); int test = scn.nextInt(); for(int i=0;i 0) { int n = fastReader.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = fastReader.nextInt(); } ArrayList b = new ArrayList<>(); ArrayList r = new ArrayList<>(); char c[] = fastReader.next().toCharArray(); for (int i = 0; i < n; i++) { if (c[i] == 'B') { b.add(a[i]); } else { r.add(a[i]); } } Collections.sort(b); Collections.sort(r); int sizeb = b.size(); boolean isValid = true; for (int i = 1 , j = 0; i <=sizeb; i++ , j++){ if (b.get(j) < i){ isValid =false; } } for (int i = sizeb+1 , j = 0; i <=n && j < r.size(); i++ , j++){ if (r.get(j) > i){ isValid =false; } } if (isValid){ System.out.println(""YES""); }else{ System.out.println(""NO""); } } } 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 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 0) { n = fs.nextInt(); int [] k = fs.readArray(n); int [] h = fs.readArray(n); List arr = new ArrayList<>(); for(i=0;i comp = new ArrayList<>(); int temp = arr.get(0).x; int tempy = arr.get(0).y; for(i=1;i tempy){ comp.add(new Pair(temp, tempy)); temp = arr.get(i).x; tempy = arr.get(i).y; } else tempy = Math.max(tempy, arr.get(i).y); } comp.add(new Pair(temp, tempy)); for(i=0;i { 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); } } }","import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int[] k = new int[n]; int[] h = new int[n]; for (int i = 0; i < n; ++i) k[i] = sc.nextInt(); for (int i = 0; i < n; ++i) h[i] = sc.nextInt(); Stack stk = new Stack<>(); stk.push(new Pair(0, 0)); for (int i = 0; i < n; ++i) { int cur_pos = k[i]; int cur_inc = h[i]; while (!stk.isEmpty() && !(cur_pos - cur_inc + 1 > stk.peek().pos)) { cur_inc = Math.max(cur_inc, stk.peek().inc + cur_pos - stk.peek().pos); stk.pop(); } stk.add(new Pair(cur_pos, cur_inc)); } long answer = 0; while (!stk.isEmpty()) { answer += (1L * stk.peek().inc * (stk.peek().inc + 1) / 2); stk.pop(); } out.println(answer); } out.flush(); } static class Pair { int pos, inc; public Pair (int pos, int inc) { this.pos = pos; this.inc = inc; } } static class Scanner { BufferedReader in; StringTokenizer st; public Scanner() { this.in = new BufferedReader(new InputStreamReader(System.in)); } public Scanner(FileReader f) { this.in = new BufferedReader(f); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public void close() throws IOException { in.close(); } } } ",0 034030f3,bf992c91,"import java.util.*; import java.io.*; public class D_1525 { static int INF = (int)1e9; static int n, m; static int[] full, free; static int[][] memo; public static int dp(int i, int j) { if(i == n) return 0; if(j == m) return INF; if(memo[i][j] != -1) return memo[i][j]; return memo[i][j] = Math.min(dp(i, j + 1), Math.abs(free[j] - full[i]) + dp(i + 1, j + 1)); } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int l = sc.nextInt(); int[] array = sc.nextIntArray(l); n = 0; for(int i = 0; i < l; i++) if(array[i] == 1) n++; m = l - n; full = new int[n]; free = new int[m]; int ind1 = 0, ind2 = 0; for(int i = 0; i < l; i++) if(array[i] == 0) free[ind2++] = i; else full[ind1++] = i; memo = new int[n][m]; for(int[] i : memo) Arrays.fill(i, -1); pw.println(dp(0, 0)); 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.Arrays; import java.util.Scanner; public class P1525D { public static int[] ones, zeros; public static int[][] memo; public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int[] arr = new int[n]; int oneCount = 0; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); oneCount += arr[i] % 2; } int o = 0, z = 0; ones = new int[oneCount]; zeros = new int[n - oneCount]; for (int i = 0; i < n; i++) { if (arr[i] % 2 == 1) { ones[o++] = i; } else { zeros[z++] = i; } } memo = new int[oneCount][n - oneCount]; for (int[] row : memo) Arrays.fill(row, -1); System.out.println(dp(0, 0)); } public static int dp(int oi, int zi) { if (oi == ones.length) { return 0; } else if (zi == zeros.length) { return 100000000; } else { if (memo[oi][zi] == -1) { memo[oi][zi] = Math.min(Math.abs(ones[oi] - zeros[zi]) + dp(oi + 1, zi + 1), dp(oi, zi + 1)); } return memo[oi][zi]; } } } ",1 54d7c21e,a7e7f371," import java.io.*; import java.util.*; public class cp { static int mod=(int)1e9+7; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; static int[] arInt; static long[] arLong; public static void main(String[] args) throws IOException { long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); // int tc=1; // primeSet=new HashSet<>(); // 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 cp { static int mod=(int)1e9+7; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; static int[] arInt; static long[] arLong; public static void main(String[] args) throws IOException { long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); // int tc=1; // primeSet=new HashSet<>(); // 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; } ",1 317a209c,921b6e4a,"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(); } }","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 9be0602e,c4ca2ff3,"import java.io.*; import java.util.*; public class Codeforce { 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(); } } 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 int fun(int ch,int a[][],int tot[],int n){ ArrayListar=new ArrayList<>(); for(int i=0;i0){ ans++; } else break; } return ans; } 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) { int n=Integer.parseInt(br.readLine()); int a[][]=new int[n][5]; int tot[]=new int[n]; 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(); } } ",0 4138b081,f59d9b6e,"import java.io.*; import java.util.*; public class Contest1627C { static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { // reads in the next string while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { // reads in the next int return Integer.parseInt(next()); } public long nextLong() { // reads in the next long return Long.parseLong(next()); } public double nextDouble() { // reads in the next double return Double.parseDouble(next()); } } static InputReader r = new InputReader(System.in); static PrintWriter pw = new PrintWriter(System.out); static long mod = 1000000007; static ArrayList[] adj; static ArrayList[] num; static int[] ans; public static void main(String[] args) { int t = r.nextInt(); while (t > 0) { t--; int n = r.nextInt(); adj = new ArrayList[n]; num = new ArrayList[n]; for (int i = 0; i < n; i ++) { adj[i] = new ArrayList(); num[i] = new ArrayList(); } int[] deg = new int[n]; boolean flag = false; for (int i = 0; i < n - 1; i ++) { int a = r.nextInt()-1; int b = r.nextInt()-1; adj[a].add(b); adj[b].add(a); num[a].add(i); num[b].add(i); deg[a] ++; deg[b] ++; if (deg[a] > 2 || deg[b] > 2) { flag = true; } } if (flag) { pw.println(-1); continue; } ans = new int[n]; for (int i = 0; i < n; i ++) { if (deg[i] == 1) { dfs(i,3,-1); } } for (int i = 0; i < n - 1; i ++) { pw.println(ans[i]); } } pw.close(); } static void dfs(int node, int x, int p) { for (int j = 0; j < adj[node].size(); j ++) { int i = adj[node].get(j); if (i == p) { continue; } ans[num[node].get(j)] = x; dfs(i,5-x,node); } } }","/* 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 5e8d1aaa,c4d04ce7,"import java.io.*; import java.util.*; /* */ public class E { static FastReader sc=null; static long max=(long)1e15+7; public static void main(String[] args) { sc=new FastReader(); PrintWriter out=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(),k=sc.nextInt(); int a[]=sc.readArray(k),te[]=sc.readArray(k); int curr[]=new int[n]; Arrays.fill(curr, -1); for(int i=0;i=0;i--) { if(curr[i]!=-1) { min=Math.min(min, curr[i]+i); } ts[i]=Math.min(ts[i], min-i); } min=max; for(int i=0;i0) { int n=sc.nextInt(),k=sc.nextInt(); int a[]=sc.readArray(k),t[]=sc.readArray(k); int present[]=new int[n]; Arrays.fill(present, -1); for(int i=0;i=0;i--) { if(present[i]!=-1) { min=Math.min(min, present[i]+i); } temps[i]=Math.min(temps[i], min-i); } min=nax; for(int i=0;i{ int id,t; Pair(int id,int t){ this.id=id; this.t=t; } @Override public int compareTo(Pair o) { return this.id-o.id; } } static int[] ruffleSort(int a[]) { ArrayList al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i0){ 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; l0){ 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{ int sum, index; pair(int i){ sum = 0; index = i; } @Override public int compareTo(pair a){ return sum - a.sum; } } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int t = Integer.parseInt(br.readLine()); while(t-- > 0){ StringTokenizer st = new StringTokenizer(br.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(br.readLine()); int sum = 0, low = x; for(int i = 0; i < n; i++){ ar[i] = Integer.parseInt(st.nextToken()); } PriorityQueue q = new PriorityQueue(); for(int i = 0; i < m; i++) q.add(new pair(i + 1)); sb.append(""YES\n""); for(int i = 0; i < n; i++){ pair p = q.poll(); p.sum += ar[i]; q.add(p); sb.append(p.index + "" ""); } sb.append(""\n""); } System.out.print(sb); } } ","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=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;ir1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;ir1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1] pr; public static void main(String[] args) throws IOException { // 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(); while(t-->0) solve(); out.close(); out.flush(); } static void solve() throws IOException { int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i blue=new ArrayList(); ArrayList red=new ArrayList(); for(int i=0;ii+1+blue.size()) { out.println(""NO""); return; } } out.println(""YES""); } static class Pair implements Cloneable, Comparable { 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) { int t= this.x-a.x; if(t!=0) return t; else return 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); 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; int taxC,taxD; Node(int ver) { vertex=ver; taxD=0; taxC=0; 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; } static long binomialCoeff(long n, long k) { if(nn) // return 0; // Since C(n, k) = C(n, n-k) if (k > 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(); } } } ","import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.StringTokenizer; public class PC3C { static PrintWriter out = new PrintWriter(System.out); static MyFastReaderPC3C in = new MyFastReaderPC3C(); static long mod = (long) (1e9 + 7); public static void main(String[] args) throws Exception { int test = i(); while (test-- > 0) { int n=i(); int[] arr=arrI(n); String s=string(); ArrayList lR=new ArrayList<>(); ArrayList lB=new ArrayList<>(); for(int i=0;i=k) { k+=1; } else { st=false; break; } } boolean st2=true; k=n; for(int i=0;ik) { st2=false; break; } else { k-=1; } } if(st && st2) out.print(""YES""); else out.print(""NO""); out.print(""\n""); out.flush(); } out.close(); } static class pair { long x, y; pair(long ar, long ar2) { x = ar; y = ar2; } } 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 < 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); } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static class DescendingComparator implements Comparator { public int compare(Integer a, Integer b) { return b - a; } } static class AscendingComparator implements Comparator { public int compare(Integer a, Integer b) { return a - b; } } static boolean isPalindrome(char X[]) { int l = 0, r = X.length - 1; while (l <= r) { if (X[l] != X[r]) return false; l++; r--; } return true; } static long fact(long N) { long num = 1L; while (N >= 1) { num = ((num % mod) * (N % mod)) % mod; N--; } return num; } 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 << n) - 1) ^ x; } static int countBits(long a) { return (int) (Math.log(a) / Math.log(2) + 1); } static boolean isPrime(long 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; } static long GCD(long a, long b) { if (b == 0) { return a; } else return GCD(b, a % b); } // Debugging Functions Starts static void print(char A[]) { for (char c : A) System.out.print(c + "" ""); System.out.println(); } static void print(boolean A[]) { for (boolean c : A) System.out.print(c + "" ""); System.out.println(); } static void print(int A[]) { for (int a : A) System.out.print(a + "" ""); System.out.println(); } static void print(long A[]) { for (long i : A) System.out.print(i + "" ""); System.out.println(); } static void print(ArrayList A) { for (int a : A) System.out.print(a + "" ""); System.out.println(); } // Debugging Functions END // ---------------------- // IO FUNCTIONS STARTS static HashMap getHashMap(int A[]) { HashMap mp = new HashMap<>(); for (int a : A) { int f = mp.getOrDefault(a, 0) + 1; mp.put(a, f); } return mp; } public static Map mapSortByValue(Map 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() - o2.getValue(); } }); // put data from sorted list to hashmap Map temp = new LinkedHashMap(); for (Map.Entry aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static String string() { return in.nextLine(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] arrI(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] arrL(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) A[i] = in.nextLong(); return A; } } class MyFastReaderPC3C { BufferedReader br; StringTokenizer st; public MyFastReaderPC3C() { 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 2915ac5b,9b8c6bd7,"import java.util.*; import java.lang.*; import java.io.*; public class Main { // static Scanner sc=new Scanner(System.in); // static ArrayList al; static long min; // static long[] dp; static FastReader sc=new FastReader(); public static void main (String[] args) throws java.lang.Exception { PrintWriter w=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0){ // dp=new long[n+1][n+1][n]; int n=sc.nextInt(); long[] c=new long[n]; for(int i=0;i=1)min=Math.min(min,min1+min2); } System.out.println(min); } w.flush(); w.close(); } // public static long f(int n, long[] c, int x, int y, int i, long cost) { // long l=Long.MAX_VALUE; // if(i>=n || x>n || y>n)return 0; // if(dp[x][y][i]!=-1)return dp[x][y][i]; // if(x==0 && y==0) {min=Math.min(cost, min);return;} // if(x==0) { // if(i%2!=0)min=Math.min(cost+c[i]*y, min); // return; // } // if(y==0) { // if(i%2==0)min=Math.min(cost+c[i]*x, min); // return; // } // if(i%2==0) { // for(int j=1;j<=x;j++) { // l=Math.min(l,f(n,c,x-j,y,i+1,cost+j*c[i])); // } // } // else { // for(int j=1;j<=y;j++) { // l=Math.min(l,f(n,c,x,y-j,i+1,cost+j*c[i])); // } // } // } } 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.*; import java.io.*; import java.math.*; public final class Main{ public static void main(String[] args) throws IOException{ // Scanner sc=new Scanner(System.in); FastReader sc=new FastReader(); PrintWriter writer=new PrintWriter(System.out); int tc=sc.nextInt(); while(tc-->0) { int n=sc.nextInt(); long[] c=new long[n]; for(int i=0;i=1) ans=Math.min(ans,min1+min2); } System.out.println(ans); } writer.flush(); writer.close(); } 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; } } }",1 1ea771ea,3c667d4f," import java.io.*; import java.util.*; public class CODECHEF { 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(); } 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; } } static long MOD=1000000000; static class Pair{ long a; int b; Pair(long i,int j){ a=i; b=j; } } static long[] solve(int[] pos,long[] arr,int n,int k){ long[] ans=new long[n]; long[] left=new long[n]; long[] right=new long[n]; long min=Integer.MAX_VALUE; for(int i=0;i=0;i--){ min=Math.min(min+1,arr[i]); right[i]=min; } for(int i=0;i0){ int n=fs.nextInt(); int k=fs.nextInt(); int[] pos=new int[k]; for(int i=0;i0) { int len=in.nextInt(); int t=in.nextInt(); int pos[]=new int[t]; int temp[]=new int[t]; for(int i=0;i=0;i--) { range[i]=Math.min(range[i+1]+1,range[i]); } for(int i=0;i 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;i 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.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 0588b869,11373c16,"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; public static long pow(long a,int 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,w; Pair(int u,int v,int w) { this.u=u; this.v=v; this.w=w; } } /*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 int gcd(int a,int b) { if(b%a==0) return a; return gcd(b%a,a); } public static void dfs(int u,int dist[],int sub[],int mxv[],int par[],ArrayList edge[]) { sub[u]=1; for(int v:edge[u]) { if(dist[v]==-1) { par[v]=u; dist[v]=dist[u]+1; dfs(v,dist,sub,mxv,par,edge); if(sub[v]+1>sub[u]) { sub[u]=sub[v]+1; mxv[u]=v; } } } } public static void main(String args[])throws Exception { FastReader fs=new FastReader(); PrintWriter pw=new PrintWriter(System.out); //int tc=fs.nextInt(); int n=fs.nextInt(); int a[]=new int[n]; for(int i=0;i o=new ArrayList<>(); ArrayList z=new ArrayList<>(); for(int i=0;i a = new ArrayList<>(); ArrayList b = new ArrayList<>(); for(int i=0;i0) { z=c=0; n=d.nextInt(); int a[]=new int[n]; for(i=0;i { int first, second; public Pair(int aa, int bb) { first = aa; second = bb; } public int compareTo(Pair o) { if(this.second < o.second) return -1; if(this.second > o.second) return +1; return this.first - o.first; } } static class Tuple implements Comparable{ long first, second,third; public Tuple(long first, long second, long third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int[] parent; int[] rank; //Size of the trees is used as the rank public DSU(int n) { parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) //finding through path compression { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public boolean union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return false; //if they are already connected we exit by returning false. // if a's parent is less than b's parent if(rank[a] < rank[b]) { //then move a under b parent[a] = b; } //else if rank of j's parent is less than i's parent else if(rank[a] > rank[b]) { //then move b under a parent[b] = a; } //if both have the same rank. else { //move a under b (it doesnt matter if its the other way around. parent[b] = a; rank[a] = 1 + rank[a]; } return true; } } static class Reader { 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) throws IOException { int[] a=new int[n]; for (int i=0; i SieveList(int n) { boolean prime[] = new boolean[n+1]; Arrays.fill(prime, true); ArrayList l = new ArrayList<>(); for (int p=2; p*p<=n; p++) { if (prime[p] == true) { for(int i=p*p; i<=n; i += p) { prime[i] = false; } } } for (int p=2; p<=n; p++) { if (prime[p] == true) { l.add(p); } } return l; } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long modPow(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPow(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); // Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(int a[], int x) { int l=-1, r=a.length; while(l+1>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { long INF = 1000000000000000007L; int n = sc.nextInt(); long[] c = new long[n+1]; for(int i = 1;i<=n;i++) c[i] = sc.nextLong(); long ans = INF; long mo = INF, so = 0, co = 0; long me = INF, se = 0, ce = 0; for(int i=1;i<=n;i++) { if(i%2 == 1) { mo = min(mo,c[i]); so += c[i]; co++; } else { me = min(me,c[i]); se += c[i]; ce++; } if(i>=2) { long x = so + (n - co) * mo + se + (n - ce) * me; ans=min(ans,x); } } fout.println(ans); } fout.close(); } }",0 7de69858,b76bce8a,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.*; import java.util.*; public class 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; } } public static void main(String args[]) { if (System.getProperty(""ONLINE_JUDGE"") == null) { // Input is a file try { System.setIn(new FileInputStream(""input.txt"")); System.setOut(new PrintStream(new FileOutputStream(""output.txt""))); } catch (Exception e) { System.err.println(""Error""); } } else { // Input is System.in } FastReader sc = new FastReader(); // Scanner sc = new Scanner(System.in); //System.out.println(java.time.LocalTime.now()); StringBuilder sb = new StringBuilder(); int t = sc.nextInt(); while(t>0) { int n = sc.nextInt(); long[] arr = new long[n]; long[] pow = new long[n]; for(int i = 0; i= prevEnd){ long nn = p[i].y-p[i].x; ans+= ((nn)*(nn+1))/2; prevEnd = p[i].y; prevN = nn; }else{ if(prevEnd < p[i].y){ long a = prevN+1; long nn = p[i].y-prevEnd; long sum = ((nn)*(2*a+(nn-1)))/2; ans +=sum; prevN = a+(nn-1); prevEnd = p[i].y; } } } sb.append(ans+""\n""); t--; } System.out.println(sb); } } class Pair implements Comparable{ long x; long y; Pair(long x, long y){ this.x = x; this.y = y; } // @Override // public boolean equals(Object o) { // if(this == o)return true; // if(o == null || this.getClass() != o.getClass())return false; // Pair p = (Pair)o; // return x == p.x && y == p.y; // } // @Override // public int hashCode(){ // return Objects.hash(x , y); // } @Override public String toString() { return ""("" + x + "", "" + y + "")""; } @Override public int compareTo(Pair o) { return (int)this.x-(int)o.x; } }","import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.StringTokenizer; public class MonstersAndSpells { 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 IOException { FastReader input = new FastReader(); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int t = input.nextInt(); while (t-- > 0) { int n = input.nextInt(); long k[] = new long[n]; long h[] = new long[n]; long ans = 0; for (int i = 0; i < n; i++) { k[i] = input.nextInt(); } for (int i = 0; i < n; i++) { h[i] = input.nextInt(); } for (int i = n-1; i >0; i--) { long d = k[i]-k[i-1]; if(h[i-1]+d= h[i]) { ans += (h[i]) * (h[i] + 1) / 2; last = h[i]; } else { long x = (k[i] - k[i - 1]); long y = last + x; long sum1 = (last) * (last + 1) / 2; long sum2 = (y) * (y + 1) / 2; ans += (sum2 - sum1); last = y; } } log.write(ans + ""\n""); } log.flush(); } } ",0 d12f26f0,df594a00,"import java.io.*; import java.util.*; public class AirConditioners { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int q = readInt(); while (q-- > 0) { int n = readInt(), k = readInt(); int[] a = new int[k], t = new int[k], c = new int[n]; for (int i = 0; i < k; i ++) a[i] = readInt(); for (int i = 0; i < k; i ++) t[i] = readInt(); Arrays.fill(c, Integer.MAX_VALUE); for (int i = 0; i < k; i ++) c[a[i] - 1] = t[i]; int[] l = new int[n], r = new int[n]; int prev = (int) 2e9; for (int i = 0; i < n; i ++) { l[i] = Math.min(prev + 1, c[i]); prev = l[i]; } prev = (int) 2e9; for (int i = n - 1; i >= 0; i --) { r[i] = Math.min(prev + 1, c[i]); prev = r[i]; } for (int i = 0; i < n; i ++) System.out.print(Math.min(l[i], r[i]) + "" ""); System.out.println(); } } 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.io.*; import java.util.*; public class Codeforces { 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) { br.readLine(); String[] str = br.readLine().split("" ""); int n = Integer.parseInt(str[0]); int k = Integer.parseInt(str[1]); int[] a = new int[k]; int[] t = new int[k]; str = br.readLine().split("" ""); for(int i=0; i=0; i--) { right[i] = Math.min(right[i], minright); minright = right[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : right[i]+1; } for(int i=0; i=diam) { out.println(""Alice""); continue; } if(bdist>2*adist) { out.println(""Bob""); } else { out.println(""Alice""); } } out.close(); } static Node farthest(Node[] nodes) { Node ans=nodes[0]; for(Node nn:nodes) if(nn.dist>ans.dist) ans=nn; return ans; } static void bfs(Node[] nodes,Node from) { for(Node nn:nodes) nn.dist=-1; ArrayDeque bfs=new ArrayDeque<>(); bfs.add(from); from.dist=0; while(!bfs.isEmpty()) { Node nn=bfs.remove(); for(Node adj:nn.adj) { if(adj.dist==-1) { adj.dist=nn.dist+1; bfs.add(adj); } } } } static class Node{ ArrayList adj=new ArrayList<>(); int dist=-1; } 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;i0) { int n = fs.nextInt(), aStart = fs.nextInt()-1, bStart = fs.nextInt()-1, aMove = fs.nextInt(), bMove = fs.nextInt(); Node[] nodes = new Node[n]; for(int i=0;i=bMove) { out.println(""Alice""); continue; } bfs(nodes,nodes[aStart]); if(nodes[bStart].dis<=aMove) { out.println(""Alice""); continue; } Node fNode = farthest(nodes); bfs(nodes, fNode); fNode = farthest(nodes); if(fNode.dis>=2*aMove+1) { out.println(""Bob""); } else { out.println(""Alice""); } } out.close(); } static Node farthest(Node[] nodes) { Node fNode = nodes[0]; for(Node n: nodes) { if(n.dis>fNode.dis) { fNode = n; } } return fNode; } static void bfs(Node[] nodes, Node start) { for(Node n: nodes) n.dis = -1; start.dis = 0; ArrayDeque queue = new ArrayDeque(); queue.add(start); while(!queue.isEmpty()) { Node n = queue.remove(); for(Node node: n.adjList) { if(node.dis==-1) { node.dis = n.dis + 1; queue.add(node); } } } } static class Node{ LinkedList adjList = new LinkedList(); int dis = -1; } 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(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;il) { int mid = (l+r)/2; if(arr[mid]>=k) { r = mid; } else { l = mid+1; } } if(arr[l]l) { int mid = (l+r)/2; if(arr[mid]>k) { r = mid; } else { l = mid+1; } } if(arr[l]<=k) return -1; return l; } static long gcd(long a,long b) { return (b==0)?a:gcd(b,a%b); } }",1 5c54f087,c6c7fa0c,"import java.util.*; import java.lang.*; import java.io.*; public class Main { PrintWriter out = new PrintWriter(System.out); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""""); String next() throws IOException { if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } long nl() throws IOException { return Long.parseLong(next()); } int n,a,b,da,db,dist,vert; ArrayListA[]; void solve() throws IOException { for (int tc=ni();tc>0;tc--) { n=ni(); a=ni(); b=ni(); da=ni(); db=ni(); A=new ArrayList[n+1]; for (int i=1;i<=n;i++) A[i]=new ArrayList(); for (int i=1;idist) { dist=d; vert=u; } for (Integer v:A[u]) { if (v==p) continue; dfs2(v,u,d+1); } } void dfs1(int u,int p,int d) { if (u==b) dist=d; for (Integer v:A[u]) { if (v==p) continue; dfs1(v,u,d+1); } } public static void main(String[] args) throws IOException { new Main().solve(); } }","import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.PI; import static java.lang.System.in; import static java.lang.System.out; public class B { static ArrayList> adj; static int dis[]; static void dfs(int s, int p, int l) { dis[s] = l; for(int i : adj.get(s)) { if(i==p) continue; dfs(i, s, l+1); } } public static void main(String[] args) throws Exception { Foster sc = new Foster(); PrintWriter p = new PrintWriter(out); int t = sc.nextInt(); while(t--!=0) { int n = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt(), da = sc.nextInt(), db = sc.nextInt(); adj = new ArrayList<>(); for(int i = 0; i <= n; i++) { adj.add(new ArrayList<>()); } for(int i = 1; i < n; i++) { int u = sc.nextInt(), v = sc.nextInt(); adj.get(u).add(v); adj.get(v).add(u); } dis = new int[n+1]; dfs(a, 0, 0); if(dis[b] <= da) //Alice reached in first move { p.println(""Alice""); continue; } int farthest = 0, maxDis = 0; for(int i = 1; i <= n; i++) { if(maxDis < dis[i]) { maxDis = dis[i]; farthest = i; } } dfs(farthest, 0, 0); int diameter = 0; for(int i = 1; i <= n; i++) { diameter = Math.max(diameter, dis[i]); } if(db > 2*da && diameter > 2*da) { p.println(""Bob""); } else { p.println(""Alice""); } } p.close(); } /* */ /* 1. Check overflow in pow function or in general 2. Check indices of read array function 3. Think of an easier solution because the problems you solve are always easy 4. Check iterator of loop 5. If still doesn't work, then jump from the 729th floor 'coz ""beta tumse na ho paayega"" Move to top!! */ static class Foster { BufferedReader br = new BufferedReader(new InputStreamReader(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()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] intArray(int n) // Check indices { int arr[] = new int[n]; for(int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } long[] longArray(int n) // Check indices { long arr[] = new long[n]; for(int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } int[] getBits(int n) //in Reverse Order { int a[] = new int[31]; for(int i = 0; i < 31; i++) { if(((1< 0) { if((a[1] & 1) == 1) res = (res * a[0]) % mod; a[1] /= 2; a[0] = (a[0] * a[0]) % mod; } return res; } void print(Object... o) { for(Object next : o) { System.err.print(next + "" ""); } } void println(Object... o) { for(Object next : o) { System.err.print(next + "" ""); } System.err.println(); } void watch(Object...a) throws Exception { int i = 1; for (Object o: a) { boolean found = false; if (o.getClass().isArray()) { String type = o.getClass().getName().toString(); switch (type) { case ""[I"": { int[] test = (int[]) o; println(i + "" : "" + Arrays.toString(test)); break; } case ""[[I"": { int[][] obj = (int[][]) o; println(i + "" : "" + Arrays.deepToString(obj)); break; } case ""[J"": { long[] obj = (long[]) o; println(i + "" : "" + Arrays.toString(obj)); break; } case ""[[J"": { long[][] obj = (long[][]) o; println(i + "" : "" + Arrays.deepToString(obj)); break; } case ""[D"": { double[] obj = (double[]) o; println(i + "" : "" + Arrays.toString(obj)); break; } case ""[[D"": { double[][] obj = (double[][]) o; println(i + "" : "" + Arrays.deepToString(obj)); break; } case ""[Ljava.lang.String"": { String[] obj = (String[]) o; println(i + "" : "" + Arrays.toString(obj)); break; } case ""[[Ljava.lang.String"": { String[][] obj = (String[][]) o; println(i + "" : "" + Arrays.deepToString(obj)); break; } case ""[C"": { char[] obj = (char[]) o; println(i + "" : "" + Arrays.toString(obj)); break; } case ""[[C"": { char[][] obj = (char[][]) o; println(i + "" : "" + Arrays.deepToString(obj)); break; } default: { println(i + "" : type not identified""); break; } } found = true; } if (o.getClass() == ArrayList.class) { println(i + "" : LIST = "" + o); found = true; } if (o.getClass() == TreeSet.class) { println(i + "" : SET = "" + o); found = true; } if (o.getClass() == TreeMap.class) { println(i + "" : MAP = "" + o); found = true; } if (o.getClass() == HashMap.class) { println(i + "" : MAP = "" + o); found = true; } if (o.getClass() == LinkedList.class) { println(i + "" : LIST = "" + o); found = true; } if (o.getClass() == PriorityQueue.class) { println(i + "" : PQ = "" + o); found = true; } if (!found) { println(i + "" = "" + o); } i++; } } } }",0 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 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 zeros = new ArrayList<>(); static List ones = new ArrayList<>(); static int oneCount = 0; static int[][] dp = new int[5005][5005]; public static void main(String[] args) { t = 1; while (t-- > 0) { n = fs.nextInt(); for(int i = 0;i { long first, second; public Pair(long first, long second) { this.first = first; this.second = second; } @Override public int compareTo(Pair o) { return Long.compare(first, o.first); } } }","/* JAI MATA DI */ import java.util.*; import javax.print.attribute.HashAttributeSet; import java.io.*; import java.math.BigInteger; import java.sql.Array; public class CP { 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; } } static int mod = 1000000007; static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j= 0) { bool[0] = false; bool[1] = false; } return bool; } static class Pair implements Comparable{ int ind; int val; Pair(int key , int value){ this.ind = key; this.val = value; } @Override public int compareTo(Pair o) { return this.val - o.val; } // @Override // public int hashCode(){ // return first + second; // } } /* ***************************************************************************************************************************************************/ static FR sc = new FR(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) { // int tc = sc.nextInt(); // while(tc-- > 0) { TEST_CASE(); // } System.out.println(sb); } static void TEST_CASE() { int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0 ; i< n ;i++) { arr[i] = sc.nextInt(); } ArrayList ao = new ArrayList(); ArrayList az = new ArrayList(); for(int i = 0 ; i< n ;i++) { if(arr[i] == 1) ao.add(i); else az.add(i); } long[][] dp = new long[n+1][n+1]; for(int i = 0 ; i ao , ArrayList az ,int i , int j) { if(i == ao.size()) return 0; if(j == az.size()) return Long.parseLong(""1000000000000""); if(dp[i][j] != -1) return dp[i][j]; long a = Math.abs(ao.get(i) - az.get(j)) + fnc(dp, ao, az, i+1, j+1); long b = fnc(dp, ao, az, i, j+1); dp[i][j] = Math.min(a, b); return dp[i][j]; } }",0 7f42483d,c9a316ca,"import java.io.*; import java.util.*; public class Main { static final boolean INPUT_FROM_FILE = false; static final String INPUT_FILE = ""input/input.txt""; static final String OUTPUT_FILE = ""input/output.txt""; static final long M = (long) 1e9 + 7; static FastReader in; static FastWriter out; static { try { in = new FastReader(); out = new FastWriter(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { int t = in.nextInt(); while (t-- > 0) solve(); out.close(); } static long[][] dp; static long dfs(List> tree, int parent, int current, int parentState, int[][] vRange) { if(dp[current][parentState] != -1) { return dp[current][parentState]; } long left = Math.abs(vRange[current][0] - vRange[parent][parentState]); long right = Math.abs(vRange[current][1] - vRange[parent][parentState]); for(int child : tree.get(current)) { if(child != parent) { left += dfs(tree, current, child, 0, vRange); right += dfs(tree, current, child, 1, vRange); } } dp[current][parentState] = Math.max(left, right); return dp[current][parentState]; } private static void solve() { int n = in.nextInt(); int[][] vRange = new int[n+1][2]; for(int i=1; i<=n; i++) { int l = in.nextInt(), r = in.nextInt(); vRange[i][0] = l; vRange[i][1] = r; } List> tree = new ArrayList<>(); for(int i=0; i<=n; i++) tree.add(new LinkedList<>()); for(int i=0; i= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } 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 long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long 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 double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } 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 String next() { return readString(); } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } static class FastWriter { BufferedWriter writer; StringBuilder sb; public FastWriter() throws IOException { if (INPUT_FROM_FILE) writer = new BufferedWriter(new FileWriter(OUTPUT_FILE)); else writer = new BufferedWriter(new PrintWriter(System.out)); sb = new StringBuilder(); } public void print(Object obj) { sb.append(obj); } public void println(Object obj) { sb.append(obj).append('\n'); } public void close() throws IOException { writer.write(sb.toString()); writer.close(); } } } "," import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import static javax.swing.UIManager.get; import static javax.swing.UIManager.getString; public class Main { static class Pair implements Comparable { int x = 0; int y = 0; public Pair(int x1, int y1) { x = x1; y = y1; } @Override public int compareTo(Pair o) { return this.x - o.x; } } static boolean checkPallindrome(int n) { ArrayList list = new ArrayList<>(); while (n > 0) { list.add(n % 10); n /= 10; } int low = 0, high = list.size() - 1; while (low <= high) { if (list.get(low) != list.get(high)) return false; low++; high--; } return true; } static boolean check(String s) { int low = 0, high = s.length() - 1; while (low <= high) { if (s.charAt(low) != s.charAt(high)) return false; low++; high--; } return true; } class Node implements Comparable { int x = 0, y = 0, z = 0; @Override public int compareTo(Node o) { return this.y - o.y; } } static int min = Integer.MAX_VALUE; public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); //(1)very very important**(never take the first problem for granted, always check the test cases) take 5 minutes more and check the edge cases // 5 minutes will not decreases rating as much as a wrong submission does it is easy u just think with an open mind and u will surely get the answer //(2)let ur brain consume the problem don't just jump to the solution. after reading the problem take a pause 1 minute //(3)go through the example test cases and also at least two of ur own test cases.Think of testcases which are difficult(edge cases).dry run ur concept //(4) sometimes if else condition is not required but due to if else you miss some points and get wrong answer int t = sc.nextInt(); while (t-- > 0) { int n =sc.nextInt(); ArrayList> list = new ArrayList<>(); for(int i=0;i()); ArrayList list1 = new ArrayList<>(); for(int i=0;i> list,ArrayList list1) { for(int v:list.get(u)) { if(v==p) continue; dfs(v,u,dp,list,list1); dp[1][u]+= Math.max(Math.abs(list1.get(u).y-list1.get(v).x)+dp[0][v],Math.abs(list1.get(u).y-list1.get(v).y)+dp[1][v]); dp[0][u]+=Math.max(Math.abs(list1.get(u).x-list1.get(v).x)+dp[0][v],Math.abs(list1.get(u).x-list1.get(v).y)+dp[1][v]); } } //static int lcs( int[] X, int[] Y, int m, int n ) //{ // int L[][] = new int[m+1][n+1]; // // /* Following steps build L[m+1][n+1] in bottom up fashion. Note // that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */ // for (int i=0; i<=m; i++) // { // for (int j=0; j<=n; j++) // { // if (i == 0 || j == 0) // L[i][j] = 0; // else if (X[i-1] == Y[j-1]) // L[i][j] = L[i-1][j-1] + 1; // else // L[i][j] = Math.max(L[i-1][j], L[i][j-1]); // } // } // return L[m][n]; //} // syntax of conditional operator y=(x==1)?1:0; //Things to check when u r getting wrong answer // 1- check the flow of the code //2- If ur stuck read the problem once again //3- before submitting always check the output format of ur code //4- don't check standings until problem B is done //5- if u r thinking ur concept is correct but still u r getting wrong answer try to implement it in another way //6- By default, java interpret all numeral literals as 32-bit integer values. // If you want to explicitely specify that this is something bigger then 32-bit integer you should use suffix L for long values. example long a = 600851475143L //All the functions long pow(long a,long b,long m) { a%=m; long res=1L; while(b>0) { long temp=b&1; if(temp==1) res=(res*a)%m; a=(a*a)%m; b>>=1; } return res; } 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++) { // Initially, all elements are in // their own set. parent[i] = i; } } // Returns representative of x's set int find(int x) { // Finds the representative of the set // that x is an element of if (parent[x] != x) { // if x is not the parent of itself // Then x is not the representative of // his set, parent[x] = find(parent[x]); // so we recursively call Find on its parent // and move i's node directly under the // representative of this set } return parent[x]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { // Find representatives of two sets int xRoot = find(x), yRoot = find(y); // Elements are in the same set, no need // to unite anything. if (xRoot == yRoot) return; // If x's rank is less than y's rank if (rank[xRoot] < rank[yRoot]) // Then move x under y so that depth // of tree remains less parent[xRoot] = yRoot; // Else if y's rank is less than x's rank else if (rank[yRoot] < rank[xRoot]) // Then move y under x so that depth of // tree remains less parent[yRoot] = xRoot; else // if ranks are the same { // Then move y under x (doesn't matter // which one goes where) parent[yRoot] = xRoot; // And increment the result tree's // rank by 1 rank[xRoot] = rank[xRoot] + 1; } } } static void debug(String s) { System.out.println(s); } //collections.sort use merge sort instead of quick sort but arrays.sort use quicksort whose worst time complexity is O(n^2) static int[] sort(int[] a) { ArrayList list = new ArrayList<>(); for(int i=0;i= 1; j--){ tmp = Math.min(tmp+1, max[j-1]); right[j-1] = tmp; } for(int j = 0; j < n; j++){ System.out.print(Math.min(left[j], right[j]) + "" ""); } System.out.println(); } } } "," import java.io.*; import java.util.*; public class CODECHEF { 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(); } 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; } } static long MOD=1000000000; static class Pair{ long a; int b; Pair(long i,int j){ a=i; b=j; } } static long[] solve(int[] pos,long[] arr,int n,int k){ long[] ans=new long[n]; long[] left=new long[n]; long[] right=new long[n]; long min=Integer.MAX_VALUE; for(int i=0;i=0;i--){ min=Math.min(min+1,arr[i]); right[i]=min; } for(int i=0;i0){ int n=fs.nextInt(); int k=fs.nextInt(); int[] pos=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 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.io.PrintWriter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class a{ 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;tt=0;i--){ ans[i]=Math.min(ans[i],ans[i+1]+1); } for(int i=0;i l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i l=new ArrayList<>(); for (char 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; } } } ",1 4552d8a0,ec4c7e8e,"import java.util.*; import java.io.*; ////*************************************************************************** /* public class E_Gardener_and_Tree implements Runnable{ public static void main(String[] args) throws Exception { new Thread(null, new E_Gardener_and_Tree(), ""E_Gardener_and_Tree"", 1<<28).start(); } public void run(){ WRITE YOUR CODE HERE!!!! JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!! } } */ /////************************************************************************** public class D_Blue_Red_Permutation{ public static void main(String[] args) { FastScanner s= new FastScanner(); // PrintWriter out=new PrintWriter(System.out); //end of program //out.println(answer); //out.close(); StringBuilder res = new StringBuilder(); int t=s.nextInt(); int p=0; while(p red = new ArrayList(); ArrayList blue = new ArrayList(); for(int i=0;inumber){ check2=1; break; } number--; } if(check1==0 && check2==0){ res.append(""YES\n""); } else{ res.append(""NO\n""); } p++; } System.out.println(res); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { 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()); } } } ","import java.util.*; public class the_child_and_set { public static void main(String args[]) { Scanner in=new Scanner(System.in); int t=in.nextInt(); in.nextLine(); while(t--!=0) { int n=in.nextInt(); int arr[]=new int[n]; for(int i=0;ibi+(i+1)) { flag=false; break; } } } if(flag) System.out.println(""YES""); else System.out.println(""NO""); } } }",0 1410e423,da5cf40b,"import java.io.*; import java.util.*; public class C { 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))); int t = Integer.parseInt(br.readLine()); while(t --> 0) { int n = Integer.parseInt(br.readLine()); char[] lineA = br.readLine().toCharArray(); char[] lineB = br.readLine().toCharArray(); boolean[] a = new boolean[n]; boolean[] b = new boolean[n]; int ac = 0; int aic = 0; int bc = 0; int stay = 0; int flip = 0; for(int i = 0; i < n; i++) { if(lineA[i] == '1') { ac++; a[i] = true; }else aic++; if(lineB[i] == '1') { bc++; b[i] = true; } if(a[i] == b[i]) stay++; else flip++; } if(ac != bc && aic + 1 != bc) { pw.println(-1); }else { if(ac == aic+1) pw.println(Math.min(stay, flip)); else if(ac == bc) pw.println(flip); else pw.println(stay); } } pw.close(); } } ","/***** ---> :) Vijender Srivastava (: <--- *****/ import java.util.*; import java.lang.*; import java.io.*; public class Main { static FastReader sc =new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=(long)1e9+7; /* start */ public static void main(String [] args) { // int testcases = 1; int testcases = i(); while(testcases-->0) { solve(); } out.flush(); out.close(); } static void solve() { int n = i(); char c[] = inputC(); char d[] = inputC(); int x01=0,x10=0,x00=0,x11=0; for(int i=0;i 0) { if (y % 2 == 1) res = (res * x) ; y = y >> 1; x = (x * x); } return res; } 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 long[] sort(long a[]) { ArrayList arr = new ArrayList<>(); for(long i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } static int[] sort(int a[]) { ArrayList arr = new ArrayList<>(); for(Integer i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } //pair class private static class Pair implements Comparable { long first, second; public Pair(long f, long s) { first = f; second = s; } @Override public int compareTo(Pair p) { if (first > p.first) return 1; else if (first < p.first) return -1; else { if (second > p.second) return 1; else if (second < p.second) return -1; else return 0; } } } }",0 1c8bb204,7d7cf9a7,"import javax.print.DocFlavor; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class BST { static class pair implements Comparable{ int high; int idx; pair(int x , int y){ high = x; idx = y; } public String toString(){ return high + "" "" + idx; } @Override public int compareTo(Object o) { pair p = (pair)o; return high-p.high; } } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-->0){ int n = Integer.parseInt(br.readLine()); long [] arr = new long[n]; StringTokenizer st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { long tmp = Long.parseLong(st.nextToken()); arr[i] = tmp; } int h = 1; int v = 1; long minHor = arr[0]; long minVir = arr[1]; long sum0 = arr[0]; long sum1 = arr[1]; long total = (arr[0] + arr[1])*n; for (int i = 2; i < n; i++) { if(i%2==0){ h++; sum0 += arr[i]; minHor = Math.min(arr[i] , minHor); total = Math.min(total , minHor*(n-h+1)+(sum0-minHor)+minVir*(n-v+1)+(sum1-minVir)); }else { v++; sum1 += arr[i]; minVir = Math.min(arr[i] , minVir); total = Math.min(total , minHor*(n-h+1)+(sum0-minHor)+minVir*(n-v+1)+(sum1-minVir)); } } System.out.println(total); } } }","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;i0) { int n = sc.nextInt(); int a[] = new int[n]; int b[] = new int[n]; for(int i=0;i0){ int n=sc.nextInt(); int ar[]=new int[n]; int sum=0; for(int i=0;i0) { int n=in.nextInt(); int k=in.nextInt(); int a[]=new int[k]; int ans[]=new int[n]; int tem[]=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 0){ int n = sc.nextInt(); int k = sc.nextInt(); int[] a = new int[k]; int[] temp = new int[k]; for(int i=0;i=0;i--){ p = (p+1right[i])?right[i]:left[i]; System.out.print(kl+"" ""); } System.out.println(); } } } ",1 968c1e7e,a4e1511a,"import java.util.*; import java.io.*; public class E_Air_Conditioners{ public static void main(String[] args) { FastScanner s= new FastScanner(); StringBuilder res = new StringBuilder(); int t=s.nextInt(); int p=0; while(p> map = new HashMap>(); HashMap count1 = new HashMap (); for(int i=0;i obj = new ArrayList(); obj.add(pos[i]-1); map.put(temp[i],obj); count1.put(temp[i],1); } } int num=min; while(true){ if(!map.containsKey(num)){ break; } ArrayList obj2 = map.get(num); for(int i=0;i(num+1)|| ans[index+1]==0)){ ans[index+1]=num+1; if(map.containsKey(num+1)){ map.get(num+1).add(index+1); } else{ ArrayList object = new ArrayList(); object.add(index+1); map.put(num+1,object); } } if(index-1>=0 && (ans[index-1]>(num+1)|| ans[index-1]==0)){ ans[index-1]=num+1; if(map.containsKey(num+1)){ map.get(num+1).add(index-1); } else{ ArrayList object = new ArrayList(); object.add(index-1); map.put(num+1,object); } } } else if(ans[index]==num){ if(index+1(num+1)|| ans[index+1]==0)){ ans[index+1]=num+1; if(map.containsKey(num+1)){ map.get(num+1).add(index+1); } else{ ArrayList object = new ArrayList(); object.add(index+1); map.put(num+1,object); } } if(index-1>=0 && (ans[index-1]>(num+1)|| ans[index-1]==0)){ ans[index-1]=num+1; if(map.containsKey(num+1)){ map.get(num+1).add(index-1); } else{ ArrayList object = new ArrayList(); object.add(index-1); map.put(num+1,object); } } } } } num++; } for(int i=0;i0) { int n=sc.nextInt(),k=sc.nextInt(),i=0; long t[]=new long[n]; int a[]=new int[k]; for(i=0;i pq=new PriorityQueue<>(); for(i=0;i=0;i--) { if(t[i]!=0) pq.add(t[i]+i); if(pq.size()!=0) { long val=pq.peek()-i; if(ans[i]==0) ans[i]=val; else ans[i]=Math.min(val, ans[i]); } } pq.clear(); for(i=0;i 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 4e9c4bf9,f9e08a46,"import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class E1525D { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int[] arr = new int[n]; ArrayList 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.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Armchair { public static void main(String args[]){ Scanner in=new Scanner(System.in); int n=in.nextInt(); int arr[]=new int[n]; ArrayList list1=new ArrayList(); ArrayList list2=new ArrayList(); for(int i=0;i list1,ArrayList list2,long dp[][]){ for(int i=1;i<=list1.size();i++) dp[i][0]=Integer.MAX_VALUE; for(int i=1;i<=list1.size();i++){ for(int j=1;j<=list2.size();j++){ dp[i][j]=Math.min(Math.abs(list1.get(i-1)-list2.get(j-1))+dp[i-1][j-1],dp[i][j-1]); } } } }",0 80881cae,f8e7b886,"//package Codeforces; import java.io.*; import java.util.*; public class CP { static Scanner sc=new Scanner(System.in); public static void main(String[] args) throws IOException, CloneNotSupportedException { int t=sc.nextInt(); while(t-->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 { 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; // } } }","import java.util.*; import java.io.*; import java.time.*; import static java.lang.Math.*; @SuppressWarnings(""unused"") public class C { static boolean DEBUG = false; static Reader fs; static PrintWriter pw; static void solve() { int n = fs.nextInt(), k[] = fs.readArray(n), h[] = fs.readArray(n); int prev_h = h[0], prev_k = k[0]; // int ans = 0; ArrayList intervals = new ArrayList<>(); for (int i = 0; i < n; i++) { int start = k[i] - h[i] + 1; int end = k[i]; intervals.add(new pair(start, end)); } // pw.println(intervals); Collections.sort(intervals); ArrayList merged = new ArrayList<>(); merge(intervals, merged); long ans = 0; for(int i = 0 ; i < merged.size() ; i++) { ans += sum(merged.get(i).len()); } pw.println(ans); } static void merge(ArrayLista1, ArrayLista2) { int n = a1.size(); int index = 0; for(int i =1 ; i < n ; i++) { if(a1.get(index).s >= a1.get(i).f) { a1.get(index).s = max(a1.get(index).s, a1.get(i).s); } else { index++; a1.set(index, a1.get(i)); } } for(int i = 0 ; i <= index ; i++) { a2.add(a1.get(i)); } // pw.println(a1); } // int index = 0; // Stores index of last element // // in output array (modified arr[]) // // // Traverse all input Intervals // for (int i=1; i= arr[i].start) // { // // Merge previous and current Intervals // arr[index].end = Math.max(arr[index].end, arr[i].end); // } // else { // index++; // arr[index] = arr[i]; // } // } static boolean overlapping(pair p1, pair p2) { if((p2.f >= p1.f && p1.s >= p2.f) || (p2.s >= p1.f && p1.s >= p2.s)) { return true; } return false; } static pair merge(pair p1, pair p2) { return new pair(min(p1.f, p2.f), max(p1.s, p2.s)); } static long sum(long n) { return (n * (n + 1) / 2); } static class pair implements Comparable{ int f, s; pair(int f, int s) { this.f = f; this.s = s; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append(""{"" + f + "","" + s + ""}""); return sb.toString(); } @Override public int compareTo(pair o) { return f - o.f; } public int len() { return s - f + 1; } } public static void main(String[] args) throws IOException { Instant start = Instant.now(); if (args.length == 2) { System.setIn(new FileInputStream(new File(""D:\\program\\javaCPEclipse\\CodeForces\\src\\input.txt""))); // System.setOut(new PrintStream(new File(""output.txt""))); System.setErr(new PrintStream(new File(""D:\\program\\javaCPEclipse\\CodeForces\\src\\error.txt""))); DEBUG = true; } fs = new Reader(); pw = new PrintWriter(System.out); int t = fs.nextInt(); while (t-- > 0) { solve(); } Instant end = Instant.now(); if (DEBUG) { pw.println(Duration.between(start, end)); } pw.close(); } static void sort(int a[]) { ArrayList l = new ArrayList(); for (int x : a) l.add(x); Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } public static void print(long a, long b, long c, PrintWriter pw) { pw.println(a + "" "" + b + "" "" + c); return; } static class Reader { BufferedReader br; StringTokenizer st; public Reader() { 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; } int[][] read2Array(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] = nextInt(); } } return a; } } }",0 354d060f,e1c4f3db,"import java.util.*; import java.io.*; public class CodeForces { // 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 in = new FastReader(); OutputStream op = System.out; PrintWriter out = new PrintWriter(op); int t = in.nextInt(); for (int i = 1; i <= t; i++) { int n=in.nextInt(); int arr[]=new int[n]; for(int j = 0; j < n; j++) arr[j]=in.nextInt(); helper(n,arr,out); out.println(); } out.close(); } public static void helper(int n,int arr[],PrintWriter o) { int max=Integer.MIN_VALUE; for(int i=2;i=0;i--) { if(copy[i]=2){ copy[i-1]+=min; copy[i-2]+=2*min; }} return true; } }","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); } } }",0 6825260d,c972dd00,"import java.awt.Container; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class Main { public static int check(String a,String b) { int zero1 = 0,one0 = 0; for (int i = 0; i 0) { int n = input.nextInt(); String a = input.next(); String b = input.next(); int ans = Integer.MAX_VALUE; //no changes int noChange = check(a,b); if(noChange!=-1) { ans = Math.min(ans, noChange); } //1 - 1 boolean have = false; int index = -1; for (int i = 0; i 0) { int n = input.nextInt(); String a = input.next(); String b = input.next(); int ans = Integer.MAX_VALUE; //no changes int noChange = check(a,b); if(noChange!=-1) { ans = Math.min(ans, noChange); } //1 - 1 boolean have = false; int index = -1; for (int i = 0; i { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return (int)(first - ob.first); } } static class Tuple implements Comparable { int first, second,third; public Tuple(int first, int second, int third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int[] parent; int[] rank; //Size of the trees is used as the rank public DSU(int n) { parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) //finding through path compression { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public boolean union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return false; //if they are already connected we exit by returning false. // if a's parent is less than b's parent if(rank[a] < rank[b]) { //then move a under b parent[a] = b; } //else if rank of j's parent is less than i's parent else if(rank[a] > rank[b]) { //then move b under a parent[b] = a; } //if both have the same rank. else { //move a under b (it doesnt matter if its the other way around. parent[b] = a; rank[a] = 1 + rank[a]; } return true; } } static class Reader { 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) throws IOException { int[] a=new int[n]; for (int i=0; i 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long modPow(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPow(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n SieveList(int n) { boolean prime[] = new boolean[(int)(n+1)]; Arrays.fill(prime, true); List l = new ArrayList<>(); for (int p = 2; p*p<=n; p++) { if (prime[p] == true) { for(int i = p*p; i<=n; i += p) { prime[i] = false; } } } for (int p = 2; p<=n; p++) { if (prime[p] == true) { l.add(p); } } return l; } public static int countDivisors(int x) { int c = 0; for(int i = 1;i*i<=x;i++) { if(x%i == 0) { if(x/i != i) { c+=2; } else { c++; } } } return c; } public static long log2(long n) { long ans = (long)(log(n)/log(2)); return ans; } public static boolean isPow2(long n) { return (n != 0 && ((n & (n-1))) == 0); } public static boolean isSq(int x) { long s = (long)Math.round(Math.sqrt(x)); return s*s==x; } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(long a[], long x) { int l=-1, r=a.length; while(l+1>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void Sort(long[] a) { List l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); // Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i 0) { int n = sc.nextInt(); char[] a = sc.next().toCharArray(), b = sc.next().toCharArray(); int c00 = 0, c01 = 0, c10 = 0, c11 = 0; for(int i = 0;i { int x=0,y=0; int z=0; public Pair(int a, int b) { this.x=a; this.y=b; } public int compareTo(Pair o) { return this.x - o.x; } } public static int mod = (int) (1e9 + 7); static int ans = Integer.MAX_VALUE; public static void main(String hi[]) throws Exception { FastReader sc = new FastReader(); int t =sc.nextInt(); while(t-->0) { int n =sc.nextInt(); String a = sc.nextLine(),b=sc.nextLine(); int count1=0,count2=0,count3=0,count4=0; for(int i=0;i 0) { int n = in.nextInt(); int m = in.nextInt(); int x = in.nextInt(); List arr = new ArrayList<>(); for(int i = 0; i < n; i++) { arr.add(new Block(in.nextInt(), i)); } if(m == 1) { System.out.println(""YES""); for(int i = 0; i < n; i++) { System.out.print(1 + "" ""); } System.out.println(); continue; } arr.sort(Comparator.reverseOrder()); long[] towers = new long[m]; int ind = 0; boolean right = true; int[] ans = new int[n]; for(int i = 0; i < n; i++) { int nextInd; if(right) { if(ind == m - 1) { right = false; nextInd = ind - 1; } else { nextInd = ind + 1; } } else { if(ind == 0) { right = true; nextInd = ind + 1; } else { nextInd = ind - 1; } } if(towers[ind] <= towers[nextInd]) { towers[ind] += arr.get(i).weight; ans[arr.get(i).index] = ind + 1; } else { towers[nextInd] += arr.get(i).weight; ans[arr.get(i).index] = nextInd + 1; ind = nextInd; } } boolean success = true; for(int i = 0; i < m - 1; i++) { if(Math.abs(towers[i] - towers[i + 1]) > x) { success = false; break; } } if(success) { System.out.println(""YES""); for(int i = 0; i < n; i++) { System.out.print(ans[i] + "" ""); } System.out.println(); } else { System.out.println(""NO""); } } } static class Block implements Comparable { int weight; int index; public Block(int weight, int index) { this.weight = weight; this.index = index; } @Override public int compareTo(Block o) { return Integer.compare(this.weight, o.weight); } } 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.*; import java.io.*; public class C { public static void main(String[] args) { FastScanner sc = new FastScanner(); int T = sc.nextInt(); StringBuilder sb = new StringBuilder(); while(T-->0) { 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 1162c08f,f6ca6fc8,"import java.util.*; public class CodeForces1525C{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); ArrayList o=new ArrayList(), e=new ArrayList(); int n = sc.nextInt(),dp[][]=new int[n+1][n+1]; for(int i=1;i<=n;i++){ int x=sc.nextInt(); if(x==1)o.add(i); else e.add(i); } for(int i=1;i<=o.size();i++){ dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1)); for(int j=i+1;j<=e.size();j++) dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1))); } System.out.println(dp[o.size()][e.size()]); } } ","import java.util.*; import java.io.*; public class D { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n = sc.nextInt(); ArrayList o=new ArrayList(); ArrayList e=new ArrayList(); for(int i=1;i<=n;i++){ int x=sc.nextInt(); if(x==1)o.add(i); else e.add(i); } int dp[][]=new int[o.size()+1][e.size()+1]; for(int i=1;i<=o.size();i++){ dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1)); for(int j=i+1;j<=e.size();j++){ dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1))); } } System.out.println(dp[o.size()][e.size()]); } } ",1 4e87c35b,f1f600d9,"import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class TaskC { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } long ans = Long.MAX_VALUE; // sum so far + min*(n-k) long[] evenSum = new long[n]; long[] oddSum = new long[n]; int[] evenMin = new int[n]; int[] oddMin = new int[n]; evenSum[0] = arr[0]; oddSum[1] = arr[1]; evenMin[0] = arr[0]; oddMin[1] = arr[1]; for (int i = 2; i < n; i++) { if (i % 2 == 0) { evenSum[i] = evenSum[i - 2] + arr[i]; evenMin[i] = Math.min(evenMin[i - 2], arr[i]); } else { oddSum[i] = oddSum[i - 2] + arr[i]; oddMin[i] = Math.min(oddMin[i - 2], arr[i]); } } for (int i = 1; i < n; i++) { ans = Math.min(ans, compute(arr, i, evenSum, oddSum, evenMin, oddMin)); } out.println(ans); } private long compute(int[] arr, int i, long[] evenSum, long[] oddSum, int[] evenMin, int[] oddMin) { if (i % 2 == 0) { return evenSum[i] + (arr.length - (i / 2) - 1) * (long) evenMin[i] + oddSum[i - 1] + (arr.length - (i / 2)) * (long) oddMin[i - 1]; } else { return evenSum[i - 1] + (arr.length - (i / 2) - 1) * (long) evenMin[i - 1] + oddSum[i] + (arr.length - (i / 2) - 1) * (long) oddMin[i]; } } } } "," // import java.util.Vector; import java.util.*; import java.lang.Math; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import javax.management.Query; import java.io.*; import java.math.BigInteger; public class Main { static int mod = 1000000007; /* ======================DSU===================== */ static class dsu { static int parent[], n;// min[],value[]; static long size[]; dsu(int n) { parent = new int[n + 1]; size = new long[n + 1]; // min=new int[n+1]; // value=new int[n+1]; this.n = n; makeSet(); } static void makeSet() { for (int i = 1; i <= n; i++) { parent[i] = i; size[i] = 1; // min[i]=i; } } static int find(int a) { if (parent[a] == a) return a; else { return parent[a] = find(parent[a]);// Path Compression } } static void union(int a, int b) { int setA = find(a); int setB = find(b); if (setA == setB) return; if (size[setA] >= size[setB]) { parent[setB] = setA; size[setA] += size[setB]; } else { parent[setA] = setB; size[setB] += size[setA]; } } } /* ======================================================== */ static class Pair implements Comparator { long x; long y; // Constructor public Pair(long x, long y) { this.x = x; this.y = y; } public Pair() { } @Override public int compare(Main.Pair o1, Main.Pair o2) { return ((int) (o1.x - o2.x)); } } 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[] intArr(int n) { int res[] = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] longArr(int n) { long res[] = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } } static FastReader f = new FastReader(); static BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out)); static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int LowerBound(int a[], int x) { // x is the target value or key int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 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 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long power(long x, long y) { long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static int power(int x, int y) { int res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } 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)); } /* * ===========Modular Operations================== */ static long modInverse(long n, long p) { return power(n, p - 2, p); } static long modAdd(long a, long b) { return (a % mod + b % mod) % mod; } static long modMul(long a, long b) { return ((a % mod) * (b % mod)) % mod; } static long nCrModPFermat(int n, int r) { long p = 1000000007; if (r == 0) return 1; long[] fac = new long[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } /* * =============================================== */ static List removeDup(ArrayList list) { List newList = list.stream().distinct().collect(Collectors.toList()); return newList; } static void ruffleSort(long[] a) { 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 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); int temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } /* * ===========Dynamic prog Recur Section=========== */ static int DP[][]; static ArrayList> g; static int count = 0; static ArrayList bitMask(ArrayList ar, int n) { ArrayList ans = new ArrayList<>(); for (int mask = 0; mask <= Math.pow(2, n) - 1; mask++) { long sum = 0; for (int i = 0; i < n; i++) { if (((1 << i) & mask) > 0) { sum += ar.get(i); } } ans.add(sum); } return ans; } /* * ====================================Main================================= */ public static void main(String args[]) throws Exception { // File file = new File(""D:\\VS Code\\Java\\Output.txt""); // FileWriter fw = new FileWriter(""D:\\VS Code\\Java\\Output.txt""); Random rand = new Random(); int t = 1; t = f.nextInt(); int tc = 1; while (t-- != 0) { int n = f.nextInt(); int c[] = new int[n]; long minOdd = 0, minEven = 0; long sumEven = 0, sumOdd = 0; for (int i = 0; i < n ; i++) { c[i] = f.nextInt(); // if (i % 2 == 0) { // minEven = (c[minEven] > c[i]) ? i : minEven; // sumEven += c[i]; // } else { // minOdd = (minOdd > c[i]) ? i : minOdd; // sumOdd += c[i]; // } } minEven = c[0]; minOdd = c[1]; sumEven=c[0]; sumOdd=c[1]; long min=minEven*n + minOdd*n;//for k=2 int even=1,odd=1; for (int k = 3; k <= n; k++) { if(k%2==1){ sumEven+=c[k-1]; minEven=Math.min(minEven, c[k-1]); even++; }else{ sumOdd+=c[k-1]; minOdd=Math.min(minOdd, c[k-1]); odd++; } min=Math.min(min, sumEven-minEven+minEven*(n-even+1) + sumOdd-minOdd+minOdd*(n-odd+1)); } w.write(min+""\n""); } w.flush(); } }",0 05ca89ed,5b9a0551," import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class er106c { //By shwetank_verma public static void main(String[] args) { FastReader sc=new FastReader(); try{ int t=1; t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); long o=n,e=n; long maxo=Integer.MAX_VALUE; long maxe=Integer.MAX_VALUE; long ans=Long.MAX_VALUE; long temp=0; int a[]=new int[n]; for(int i=0;i b=new ArrayList<>(); static boolean seive(int n){ Arrays.fill(primes,true); primes[0]=primes[1]=false; for(int i=2;i*i<=n;i++){ if(primes[i]==true){ for(int p=i*i;p<=n;p+=i){ primes[p]=false; } } } if(n<1000007){ for(int i=2;i<=n;i++) { if(primes[i]) b.add(i); } return primes[n]; } return false; } static int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b); } static long GCD(long a,long b){ if(b==0) return a; return GCD(b,a%b); } static ArrayList segseive(int l,int r){ ArrayList isprime=new ArrayList(); boolean p[]=new boolean[r-l+1]; Arrays.fill(p, true); for(int i=0;b.get(i)*b.get(i)<=r;i++) { int currprime=b.get(i); int base=(l/currprime)*currprime; if(base>>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; } }","import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Main { public static void main(String[] args) { FastScanner in=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int t=in.nextInt(); while(t-->0) solve(in,out); out.close(); } static void solve(FastScanner in,PrintWriter out){ int n=in.nextInt(); long a[]=new long[n]; for (int i = 0; i < n; i++) { a[i]=in.nextLong(); } long odd=Integer.MAX_VALUE,even=Integer.MAX_VALUE; even=a[0]; long sum=a[0]; long ans=Long.MAX_VALUE; for (int i = 1; i < n; i++) { if(i%2==0) { ans=Math.min(ans,(n-i/2)*a[i] + odd*(n-i/2) +sum); even=Math.min(even,a[i]); } else { ans=Math.min(ans,(n-i/2)*a[i] + even*(n-i/2-1) +sum); odd=Math.min(odd,a[i]); } sum+=a[i]; } out.println(ans); } static class pair, V extends Comparable> implements Comparable> { public U x; public V y; public pair(U x, V y) { this.x = x; this.y = y; } public int compareTo(pair other) { int i = x.compareTo(other.x); if (i != 0) return i; return y.compareTo(other.y); } public String toString() { return x.toString() + "" "" + y.toString(); } public boolean equals(Object obj) { if (this.getClass() != obj.getClass()) return false; pair other = (pair) obj; return x.equals(other.x) && y.equals(other.y); } public int hashCode() { return 31 * x.hashCode() + y.hashCode(); } } static void sort(int[] a) { ArrayList l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i 0) solve(); out.println(sb); out.close(); } public static void solve() { // Road To Specialist Day 3 int n = scn.nextInt(), m = scn.nextInt(), x = scn.nextInt(); int[] a = new int[n], ans = new int[n]; for(int i = 0; i < n; i++) a[i] = scn.nextInt(); PriorityQueue pq = new PriorityQueue<>(); for(int i = 0; i < m; i++) pq.add(new Pair(0L, i)); for(int i = 0; i < n; i++) { int e = a[i]; Pair p = pq.poll(); p.value += e; pq.add(p); ans[i] = p.id + 1; } boolean check = false; long prev = pq.poll().value; while(!pq.isEmpty()) { long cur = pq.poll().value; if(Math.abs(cur - prev) > x) check = true; prev = cur; } if(check) sb.append(""NO""); else { sb.append(""YES\n""); for(int i : ans) sb.append(i + "" ""); } sb.append(""\n""); } static class Pair implements Comparable { int id; long value; public Pair(long value, int id) { this.id = id; this.value = value; } public int compareTo(Pair o) { return Long.compare(value, o.value); } } }","/*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 + ""]""; // } // } //-------------------------------------------------------- } ",0 b9595381,c4ca2ff3,"import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main{ static int dest1; static int dest2; public static void main(String args[]){ FastScanner in = new FastScanner(); int test=in.nextInt(); while(test-->0){ int n=in.nextInt(); int count[][]=new int[n][5]; int total[]=new int[n]; String words[]=new String[n]; for(int i=0;i0){ r+=ans[j]; j++; } max=Math.max(j,max); } System.out.println(max); } } public static int solve(int start[], int end[], int n) { int distance[][]=new int[n][n]; int r=n; int c=n; boolean visited[][]=new boolean[n][n]; int startx=start[0]; int starty=start[1]; int endx=end[0]; int endy=end[1]; Pair tmp=new Pair(startx,starty); Queue q=new LinkedList<>(); q.add(tmp); visited[startx][starty]=true; distance[startx][starty]=0; int dx[]={-2,-1,1,2,2,1,-1,-2}; int dy[]={1,2,2,1,-1,-2,-2,-1}; while(!q.isEmpty()) { Pair cell=q.poll(); int x=cell.x; int y=cell.y; int d=distance[x][y]; for(int i=0;i<8;i++) { int childx=x+dx[i]; int childy=y+dy[i]; if(valid(childx,childy,r,c,visited)) { visited[childx][childy]=true; distance[childx][childy]=d+1; q.add(new Pair(childx,childy)); } } } return distance[endx][endy]; } public static boolean valid(int x,int y,int r,int c,boolean visited[][]) { if(x<0||y<0||x>=r||y>=c) return false; if(visited[x][y]) return false; return true; } public static int knight(int i,int j){ if(i==dest1 && j==dest2) return 0; if(i<1 || j<1) return 0; if(i>8 || j>8) return 0; if(i<1 || j>8) return 0; if(i>8 || j<1) return 0; int min=0; if(i-1>=1 && j-2>=1) min+=1+knight(i-1,j-2); if(i-1>=1 && j+2<=8) min+=1+knight(i-1,j+2); if(i-2>=1 && j-1>=1) min+=1+knight(i-2,j-1); if(i-2>=1 && j+1<=8) min+=1+knight(i-2,j+1); if(i+1<=8 && j-2>=1) min+=1+knight(i+1,j-2); if(i+1<=8 && j+2<=8) min+=1+knight(i+1,j+2); if(i+2<=8 && j-1>=1) min+=1+knight(i+2,j-1); if(i+2<=8 && j+1<=8) min+=1+knight(i+2,j+1); return min; } } class FastScanner { java.io.BufferedReader br = new java.io.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()); } } class Pair { int x; int y; Pair(int i,int j) { x=i; y=j; } } ","/* 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 9291ca83,d6fb3b9e,"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); } } } ","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 6f393cfe,7a9c69d8,"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.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; } } } ",1 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 0fdc80c5,28c2d81a,"import java.io.*; import java.util.*; public class E { public static void main(String[] args) { FastScanner scan = new FastScanner(); int t = scan.nextInt(); for(int tt = 0;tt=0;i--) r[i] = Math.min(r[i], r[i+1]+1); StringBuilder s = new StringBuilder(); for(int i = 0;i0){ int n = fs.nextInt(), k = fs.nextInt(); int[] positions = fs.readArray(k), temps = fs.readArray(k); int[] forced = new int[n]; Arrays.fill(forced, Integer.MAX_VALUE/2); for(int i=0; i=0; i--) forced[i] = Math.min(forced[i], forced[i+1]+1); for(int i=0; i ""+a); } public static void debug(long a, long b){ System.out.println(""--> ""+ a +"" + "" + b); } public static void debug(char a, char b){ System.out.println(""--> ""+ a +"" + "" + b); } public static void debug(int[] array){ System.out.print(""Array--> ""); System.out.println(Arrays.toString(array)); } public static void debug(char[] array){ System.out.print(""Array--> ""); System.out.println(Arrays.toString(array)); } public static void debug(HashMap map){ System.out.print(""Map--> ""+map.toString()); } } } ",1 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;i0) { int n=Integer.parseInt(f.readLine()); String s=f.readLine(); String s2=f.readLine(); int[] arr=new int[4]; //0 00 1 01 2 10 3 11 for(int i=0;i=0){ int n=Integer.parseInt(br.readLine()); char a[]=br.readLine().toCharArray(); char b[]=br.readLine().toCharArray(); int x=func(a,b); int ind=-1; for(int i=0;i=0){ for(int i=0;i=1000000&&y>=1000000){ System.out.println(-1); } else System.out.println(Math.min(x,y)); } } } ",0 31f29772,d04e5afb,"import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.PriorityQueue; import java.util.StringTokenizer; import java.io.*; public class PhoenixTow { private static class MyScanner { BufferedReader br; StringTokenizer st; public 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()); } String nextLine(){ String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static class Pair implements Comparable { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair p) { return ((Integer)this.x).compareTo(p.x); } } public static void solution(int[] arr, int n, int m, int x) { ArrayList list = new ArrayList<>(); for(int i = 0; ix) {out.println(""NO""); return; } } sum[k-1]+=list.get(i).x; ans[list.get(i).y] = k; k++; if(k==(m+1)) k=1; } out.println(""YES""); for(int i = 0; i> a = new ArrayList<>(); for (int i = 0; i < n; ++i) { a.add(new Pair<>(in.nextInt(), i)); } Collections.sort(a); int[] ans = new int[n]; int[] sum = new int[m]; int j = 1; for (int i = 0; i < n; ++i) { ans[a.get(i).y] = j; sum[j - 1] += a.get(i).x; j++; if (j == m + 1) j = 1; } for (int i = 1; i < m; ++i) { if (Math.abs(sum[i - 1] - sum[i]) > k) { out.println(""NO""); } } out.println(""YES""); for (int e : ans) { out.print(e + "" ""); } out.println(); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(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 String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } 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); } } static class Pair implements Comparable> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int compareTo(Pair o) { int value = ((Comparable) x).compareTo(o.x); if (value != 0) return value; return ((Comparable) y).compareTo(o.y); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return x.equals(pair.x) && y.equals(pair.y); } public int hashCode() { return Objects.hash(x, y); } } } ",0 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 8f6421f3,bdfe8110,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Arrays; public class Main{ public static void main (String[] args){ FastReader s = new FastReader(); int t=1;t=s.ni(); for(int test=1;test<=t;test++){ int n=s.ni(),k=s.ni(); int position[]=s.readArray(k),temp[]=s.readArray(k); int ans[]=new int[n]; Arrays.fill(ans,Integer.MAX_VALUE/2); for(int i=0;i=0;i--){ ans[i]=Math.min(ans[i],ans[i+1]+1); } 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; i0){ 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(); } } ","import java.util.ArrayList; import java.util.Collections; 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; import java.util.TreeSet; import java.io.*; public class Div2 { private static class MyScanner { BufferedReader br; StringTokenizer st; public 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()); } String nextLine(){ String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static String solution(int [] arr, int n, String st) { ArrayList red = new ArrayList<>(); ArrayList blue = new ArrayList<>(); for(int i = 0; i=0; j--) { if(red.get(j)>cr) return ""NO""; cr--; } return ""YES""; } private static PrintWriter out = new PrintWriter(System.out); public static void main (String[] args) { MyScanner s = new MyScanner(); int t = s.nextInt(); for(int j = 0; j 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; } } } ","// package codechef; import java.io.*; import java.util.*; public class cp_2 { static int mod=998244353; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; public static void main(String[] args) throws IOException { long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); // int tc=1; // primeSet=new HashSet<>(); // sieveOfEratosthenes((int)1e5); while(tc-->0) { int n=sc.nextInt(); int arr[]=new int[n]; for (int i = 0; i < arr.length; i++) { arr[i]=sc.nextInt(); } String s=sc.next(); ArrayList blue=new ArrayList<>(); ArrayList red=new ArrayList<>(); for(int i=0;in-i) flag=false; } printYesNo(flag); } out.flush(); out.close(); System.gc(); } /* ...SOLUTION ENDS HERE...........SOLUTION ENDS HERE... */ 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 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(int n) { HashMap ans=new HashMap<>(); if(n%2==0) { ans.put(2, 0); while((n&1)==0) { n=n>>1; ans.put(2, ans.get(2)+1); } } for(int i=3;i*i<=n;i+=2) { if(n%i==0) { ans.put(i, 0); while(n%i==0) { n=n/i; ans.put(i, ans.get(i)+1); } } } if(n!=1) ans.put(n, ans.getOrDefault(n, 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,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 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; } ",1 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 2bd5c798,317baeaf,"import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; /** * * @Har_Har_Mahadev */ /** * Main , Solution , Remove Public */ public class A { public static void process() throws IOException { int n = sc.nextInt(); String s = sc.next(),t = sc.next(); int zs = 0, zt = 0; for(int i = 0; i 0) { // google(TTT++); process(); } out.flush(); // tr(System.currentTimeMillis()-s+""ms""); } private static boolean oj = System.getProperty(""ONLINE_JUDGE"") != null; private static void tr(Object... o) { if (!oj) System.err.println(Arrays.deepToString(o)); } 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 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 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; } public static long gcd(long a, long b) { if (a > b) a = (a + b) - (b = a); if (a == 0L) return b; return gcd(b % a, a); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int lower_bound(int[] arr, int x) { int low = 0, high = arr.length - 1, mid = -1; int ans = -1; while (low <= high) { mid = (low + high) / 2; if (arr[mid] > x) { high = mid - 1; } else { ans = mid; low = mid + 1; } } return ans; } public static int upper_bound(int[] arr, int x) { int low = 0, high = arr.length - 1, mid = -1; int ans = arr.length; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) { ans = mid; high = mid - 1; } else { low = mid + 1; } } return ans; } 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 void reverseArray(int[] a) { int n = a.length; int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void reverseArray(long[] a) { int n = a.length; long arr[] = new long[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } //custom multiset (replace with HashMap if needed) public static void push(TreeMap map, int k, int v) { //map[k] += v; if (!map.containsKey(k)) map.put(k, v); else map.put(k, map.get(k) + v); } public static void pull(TreeMap map, int k, int v) { //assumes map[k] >= v //map[k] -= v int lol = map.get(k); if (lol == v) map.remove(k); else map.put(k, lol - v); } // compress Big value to Time Limit public static int[] compress(int[] arr) { ArrayList ls = new ArrayList(); for (int x : arr) ls.add(x); Collections.sort(ls); HashMap map = new HashMap(); int boof = 1; //min value for (int x : ls) if (!map.containsKey(x)) map.put(x, boof++); int[] brr = new int[arr.length]; for (int i = 0; i < arr.length; i++) brr[i] = map.get(arr[i]); return brr; } // Fast Writer public static class FastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter() { out = null; } public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException(""FastWriter""); } } public FastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastWriter write(char c) { return write((byte) c); } public FastWriter write(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if (x == Integer.MIN_VALUE) { return write((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } 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; } public FastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("""" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if (x < 0) { write('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } write((long) x).write("".""); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; write((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastWriter writeln(char c) { return write(c).writeln(); } public FastWriter writeln(int x) { return write(x).writeln(); } public FastWriter writeln(long x) { return write(x).writeln(); } public FastWriter writeln(double x, int precision) { return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for (int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for (long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte) '\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for (char[] line : map) write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException(""innerflush""); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException(""flush""); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c) { return writeln(c); } public FastWriter println(int x) { return writeln(x); } public FastWriter println(long x) { return writeln(x); } public FastWriter println(double x, int precision) { return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } // Fast Inputs static 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[] readArray(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] readArrayLong(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public int[][] readArrayMatrix(int N, int M, int Index) { if (Index == 0) { int[][] res = new int[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) res[i][j] = (int) nextLong(); } return res; } int[][] res = new int[N][M]; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) res[i][j] = (int) nextLong(); } return res; } public long[][] readArrayMatrixLong(int N, int M, int Index) { if (Index == 0) { long[][] res = new long[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) res[i][j] = nextLong(); } return res; } long[][] res = new long[N][M]; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) res[i][j] = 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[] readArrayDouble(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; } } } } ","import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer st; int t = Integer.parseInt(br.readLine()); while (t --> 0) { int n = Integer.parseInt(br.readLine()); String a = br.readLine(); String b = br.readLine(); int alit = 0; int blit = 0; int ans = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { if (a.charAt(i) == '1') alit++; if (b.charAt(i) == '1') blit++; } if (alit == blit) { int count = 0; for (int i = 0; i < n; i++) if (a.charAt(i) != b.charAt(i)) count++; ans = Math.min(count, ans); } if (alit == n - blit + 1) { int count = 0; for (int i = 0; i < n; i++) if (a.charAt(i) == b.charAt(i)) count++; ans = Math.min(ans, count); } if (ans == Integer.MAX_VALUE) { pw.println(""-1""); } else { pw.println(ans); } } pw.close(); } }",1 ec607ca1,ed37ba7d," /* TO LEARN 1-segment trees 2-euler tour 3-fenwick tree and interval tree */ /* TO SOLVE uva 1103 program codeforces edu 102 D */ /* bit manipulation shit 1-Computer Systems: A Programmer's Perspective 2-hacker's delight 3-(02-03-bits-ints) 4-machine-basics 5-Bits Manipulation tutorialspoint */ /* TO WATCH 1-what is bitmasking by kartik arora youtube */ import java.util.*; import java.math.*; import java.io.*; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; public class A{ static InputStream inputStream = System.in; static FastScanner scan=new FastScanner(); public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); static boolean vis[]=new boolean[26]; static LinkedListedges[]; static int cnt=0; static ArrayListarr=new ArrayList(); static void dfs(int c,boolean vis2[]) { vis[c]=true; // vis2[c]=true; arr.add(c); for(int v:edges[c]) { if(!vis[v]) { dfs(v,vis2); } } } public static void main(String[] args) throws Exception { // scan=new FastScanner(""lifeguards.in""); //out = new PrintWriter(""lifeguards.out""); /* currently doing 1-digit dp 2-ds like fenwick and interval tree and sparse table */ /* READING 1-Everything About Dynamic Programming codeforces 2-DYNAMIC PROGRAMMING: FROM NOVICE TO ADVANCED topcoder 3-Introduction to DP with Bitmasking codefoces 4-Bit Manipulation hackerearth */ int tt=1; //System.out.println(2&0); /*for(int i=0;i<=70;i++) for(int j=0;j<=70;j++){ if((i&j)==i) System.out.println(i+"" ""+j+"" ""+(i&j)); }*/ //System.out.println(1^14); //System.out.println(15&6); tt=scan.nextInt(); outer:while(tt-->0) { int l=scan.nextInt(),n=scan.nextInt(); int arr[]=new int[n]; long t[]=new long[n]; int pos[]=new int[l+1]; Arrays.fill(pos,-1); TreeSettree=new TreeSet(); for(int i=0;i=1;i--) { if(pos[i]!=-1) { R[i]=t[pos[i]]; } R[i]=Math.min(R[i],R[i+1]+1); } for(int i=1;i<=l;i++) out.print(Math.min(L[i],R[i])+"" ""); out.println(); } out.close(); } static class special{ long x1,y1,x2,y2; //int id; special(long x1,long y1,long x2,long y2) { this.x1=x1; this.y1=y1; this.x2=x2; this.y2=y2; //this.id=id; } @Override public int hashCode() { int hash = 7; hash = 31 * hash + (int) x1; hash = 31 * hash + (int)x2; hash = 31 * hash + (int)y1; hash = 31 * hash + (int)y2; 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.x1 == x1 && t.y1 == y1&&t.x2==x2&&t.y2==y2; } } 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)(x-o.x); } } } ","import java.io.*; import java.util.*; import java.lang.*; public class codeforces { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; if (System.getProperty(""ONLINE_JUDGE"") == null) { long startTime = System.currentTimeMillis(); try { sc = new InputReader(new FileInputStream(""input.txt"")); out = new PrintWriter(new FileOutputStream(""output.txt"")); pr = new PrintWriter(new FileOutputStream(""error.txt"")); } catch (Exception ignored) { } int t = 1; int tt = t; t = sc.nextInt(); while (t-- > 0) { solve(); } long endTime = System.currentTimeMillis(); System.out.println(""Time: "" + (endTime - startTime) / tt + "" ms""); out.flush(); pr.flush(); } else { sc = new InputReader(inputStream); out = new PrintWriter(outputStream); pr = new PrintWriter(outputStream); int t = 1; t = sc.nextInt(); while (t-- > 0) { solve(); } out.flush(); } } public static void solve() { n = sc.nextInt(); for (int i = 0; i < n; i++) { dp[i] = inf; ans[i] = inf; } m = sc.nextInt(); for (int i = 0; i < m; i++) arr[i] = sc.nextInt() - 1; for (int i = 0; i < m; i++) { arr2[i] = sc.nextInt(); dp[(int)arr[i]] = arr2[i]; } temp = inf; for (int i = 0; i < n; i++) { temp = Math.min(temp, dp[i]); ans[i] = Math.min(ans[i], temp); temp++; } temp = inf; for (int i = (int)n - 1; i > -1; i--) { temp = Math.min(temp, dp[i]); ans[i] = Math.min(ans[i], temp); temp++; } for (int i = 0; i < n; i++) out.print(ans[i] + "" ""); out.println(""""); } /* * Set Iterator Iterator value = set.iterator(); Displaying the values after * iterating through the iterator * System.out.println(""The iterator values are: ""); while (value.hasNext()) { * System.out.println(value.next()); } */ /* * Map Iterator: for (Map.Entry entry : map.entrySet()){ * System.out.println(""Key => "" + entry.getKey() + "", Value => "" + * entry.getValue());} */ // Globals public static long n, m, temp; public static int template_array_size = (int) 1e6 + 16813; public static long[] arr = new long[template_array_size]; public static long[] arr2 = new long[template_array_size]; public static long[] dp = new long[template_array_size]; public static long[] ans = new long[template_array_size]; public static int inf = Integer.MAX_VALUE; public static int minf = Integer.MIN_VALUE; public static int mod = 1000000007; public static int ml = (int) 1e9; public static String s = """"; public static InputReader sc; public static PrintWriter out; public static PrintWriter pr; // Pair static class Pair { int first, second; Pair(int x, int y) { this.first = x; this.second = y; } } // FastReader Class 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()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } // Req Functions public static void fill(int[][] dp, int x) { for (int i = 0; i < dp.length; ++i) { for (int j = 0; j < dp[0].length; ++j) { dp[i][j] = x; } } } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } public static long nCr(int n, int k) { long ans = 1L; k = k > n - k ? n - k : k; int j = 1; for (; j <= k; j++, n--) { if (n % j == 0) { ans *= n / j; } else if (ans % j == 0) { ans = ans / j * n; } else { ans = (ans * n) / j; } } return ans; } public static String reverseString(String input) { StringBuilder str = new StringBuilder(""""); for (int i = input.length() - 1; i >= 0; i--) { str.append(input.charAt(i)); } return str.toString(); } public static int maxOf3(int x, int y, int z) { return Math.max(x, Math.max(y, z)); } public static int minof3(int x, int y, int z) { return Math.min(x, Math.min(y, z)); } public static long maxOf3(long x, long y, long z) { return Math.max(x, Math.max(y, z)); } public static long minof3(long x, long y, long z) { return Math.min(x, Math.min(y, z)); } public static void arrInput(int[] arr, int n) { for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); } public static void arrInput(long[] arr, int n) { for (int i = 0; i < n; i++) arr[i] = sc.nextLong(); } public static void arrInput(Pair[] pair, int n) { for (int i = 0; i < n; i++) pair[i] = new Pair(sc.nextInt(), sc.nextInt()); } public static int maxarrInput(int[] arr, int n) { int max = minf; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); max = Math.max(max, arr[i]); } return max; } public static long maxarrInput(long[] arr, int n) { long max = minf; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); max = Math.max(max, arr[i]); } return max; } public static int minarrInput(int[] arr, int n) { int min = inf; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); min = Math.max(min, arr[i]); } return min; } public static long minarrInput(long[] arr, int n) { long min = inf; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); min = Math.max(min, arr[i]); } return min; } public static int lowerBound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] >= x) r = m; else l = m; } return r; } public static int upperBound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] <= x) l = m; else r = m; } return l + 1; } public static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int[n1]; int R[] = new int[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++; } } public static void reversesort(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; reversesort(arr, l, m); reversesort(arr, m + 1, r); merge(arr, l, m, r); } } public 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++; } } public static void reversesort(long arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; reversesort(arr, l, m); reversesort(arr, m + 1, r); merge(arr, l, m, r); } } // debug public static boolean sysFlag = System.getProperty(""ONLINE_JUDGE"") == null; public static void debug(int[][] dp) { if (sysFlag) { for (int i = 0; i < dp.length; ++i) { pr.print(i + ""--> ""); for (int j = 0; j < dp[0].length; ++j) { pr.print(dp[i][j] + "" ""); } pr.println(""""); } } } public static void debug(long[][] dp) { if (sysFlag) { for (int i = 0; i < dp.length; ++i) { pr.print(i + ""--> ""); for (int j = 0; j < dp[0].length; ++j) { pr.print(dp[i][j] + "" ""); } pr.println(""""); } } } public static void debug(int x) { if (sysFlag) pr.println(""Int-Ele: "" + x); } public static void debug(String x) { if (sysFlag) pr.println(""String: "" + x); } public static void debug(char x) { if (sysFlag) pr.println(""Char: "" + x); } public static void debug(long x) { if (sysFlag) pr.println(""Long-Ele: "" + x); } public static void debug(int[] arr, int n) { if (sysFlag) { for (int i = 0; i < n; ++i) { pr.println(i + "" -> "" + arr[i]); } } } public static void debug(char[] arr) { if (sysFlag) { for (int i = 0; i < arr.length; ++i) { pr.println(i + "" -> "" + arr[i]); } } } public static void debug(long[] arr, int n) { if (sysFlag) { for (int i = 0; i < n; ++i) { pr.println(i + "" -> "" + arr[i]); } } } public static void debug(ArrayList list) { if (sysFlag) { for (int i = 0; i < list.size(); ++i) { pr.println(i + "" -> "" + list.get(i)); } } } public static void Ldebug(ArrayList list) { if (sysFlag) { for (int i = 0; i < list.size(); ++i) { pr.println(i + "" -> "" + list.get(i)); } } } public static void debugmapII(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapLI(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapLL(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapIL(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapSL(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapCL(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapSI(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapCI(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debug(Set set) { if (sysFlag) { Iterator value = set.iterator(); int i = 1; while (value.hasNext()) { pr.println((i++) + ""-> "" + value.next()); } } } } ",0 26e699de,f229aa7f,"//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 = 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 169e34bf,609af028,"import java.util.*; public class D{ static Scanner sc; public static void solve(){ int n=sc.nextInt(); Integer a[]=new Integer[n]; int flag; 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.io.*; import java.util.*; public class new1{ public static void main(String[] args) throws IOException{ FastReader s = new FastReader(); //long l1 = System.currentTimeMillis(); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); int t = s.nextInt(); for(int z = 0; z < t; z++) { int n = s.nextInt(); int[] arr = new int[n]; ArrayList blue = new ArrayList(); ArrayList red = new ArrayList(); for(int i = 0; i < n; i++) { arr[i] = s.nextInt(); } String str = s.next(); for(int i = 0; i < n; i++) { if(str.charAt(i) == 'B') blue.add(arr[i]); else red.add(arr[i]); } red.sort(null); blue.sort(null); int j = red.size() - 1; int k = blue.size() - 1; while(j >= 0 && red.get(j) > n) j--; boolean ans = true; // System.out.println(blue.toString()); // System.out.println(red.toString()); for(int m = n; m >= 1; m--) { if(j >= 0) j--; else if(k >= 0 && blue.get(k) >= m) k--; else ans = false; //System.out.println(m + "" "" + j + "" "" + k); while(j >= 0 && red.get(j) > m - 1) j--; } if(ans) System.out.println(""YES""); else System.out.println(""NO""); } //output.write((System.currentTimeMillis() - l1) + ""\n""); //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 3ff60986,d0b4b96c,"import java.io.*; import java.util.*; 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 int N = (int)2e5 +10; static ArrayList> adj= new ArrayList<>(N); static { for(int i=0; i()); } } static int[][] a = new int[2][N]; static long[][] dp = new long[2][N]; static void dfs(int v, int p){ dp[0][v] = 0; dp[1][v]=0; for(int u: adj.get(v)) { if(u!=p) { dfs(u, v); dp[0][v] += Math.max(Math.abs(a[0][v] - a[1][u]) + dp[1][u], Math.abs(a[0][v] - a[0][u]) + dp[0][u]); dp[1][v] += Math.max(Math.abs(a[1][v] - a[1][u]) + dp[1][u], Math.abs(a[1][v] - a[0][u]) + dp[0][u]); } } } public static void main(String[] args) throws IOException{ // write your code here FastReader sc = new FastReader(); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); for(int i=1; i<=n; i++) { a[0][i] = sc.nextInt(); a[1][i] = sc.nextInt(); adj.set(i, new ArrayList<>()); } for(int i=1; i> adj= new ArrayList<>(N); static { for(int i=0; i()); } } static int[][] a = new int[2][N]; static long[][] dp = new long[2][N]; static void dfs(int v, int p){ dp[0][v] = 0; dp[1][v]=0; for(int u: adj.get(v)) { if(u!=p) { dfs(u, v); dp[0][v] += Math.max(Math.abs(a[0][v] - a[1][u]) + dp[1][u], Math.abs(a[0][v] - a[0][u]) + dp[0][u]); dp[1][v] += Math.max(Math.abs(a[1][v] - a[1][u]) + dp[1][u], Math.abs(a[1][v] - a[0][u]) + dp[0][u]); } } } public static void main(String[] args) throws IOException{ // write your code here FastReader sc = new FastReader(); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); for(int i=1; i<=n; i++) { a[0][i] = sc.nextInt(); a[1][i] = sc.nextInt(); adj.set(i, new ArrayList<>()); } for(int i=1; i blue = new ArrayList<>(); List red = new ArrayList<>(); for (int i = 0; i < n; i++) { if (s.charAt(i) == 'B') { blue.add(a[i]); } else { red.add(a[i]); } } Collections.sort(blue); Collections.sort(red); for (int i = 0; i < blue.size(); i++) { if (blue.get(i) < i + 1) { result.append(""NO""); return; } } for (int i = 0; i < red.size(); i++) { if (red.get(i) > i + 1 + blue.size()) { result.append(""NO""); return; } } result.append(""YES""); } static class WithIdx implements Comparable { int val, idx; public WithIdx(int val, int idx) { this.val = val; this.idx = idx; } @Override public int compareTo(WithIdx o) { if (val == o.val) { return Integer.compare(idx, o.idx); } return Integer.compare(val, o.val); } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { 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(); } String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } int[] nextIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] nextLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } String[] nextStringArray(int n) { String[] res = new String[n]; for (int i = 0; i < n; i++) res[i] = nextToken(); return res; } } static class PrefixSums { long[] sums; public PrefixSums(long[] sums) { this.sums = sums; } public long sum(int fromInclusive, int toExclusive) { if (fromInclusive > toExclusive) throw new IllegalArgumentException(""Wrong value""); return sums[toExclusive] - sums[fromInclusive]; } public static PrefixSums of(int[] ar) { long[] sums = new long[ar.length + 1]; for (int i = 1; i <= ar.length; i++) { sums[i] = sums[i - 1] + ar[i - 1]; } return new PrefixSums(sums); } public static PrefixSums of(long[] ar) { long[] sums = new long[ar.length + 1]; for (int i = 1; i <= ar.length; i++) { sums[i] = sums[i - 1] + ar[i - 1]; } return new PrefixSums(sums); } } static class ADUtils { static void sort(int[] ar) { 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; } Arrays.sort(ar); } static void reverse(int[] arr) { int last = arr.length / 2; for (int i = 0; i < last; i++) { int tmp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = tmp; } } static void sort(long[] ar) { Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap long a = ar[index]; ar[index] = ar[i]; ar[i] = a; } Arrays.sort(ar); } } static class MathUtils { static long[] FIRST_PRIMES = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051}; static long[] primes(int to) { long[] all = new long[to + 1]; long[] primes = new long[to + 1]; all[1] = 1; int primesLength = 0; for (int i = 2; i <= to; i++) { if (all[i] == 0) { primes[primesLength++] = i; all[i] = i; } for (int j = 0; j < primesLength && i * primes[j] <= to && all[i] >= primes[j]; j++) { all[(int) (i * primes[j])] = primes[j]; } } return Arrays.copyOf(primes, primesLength); } static long modpow(long b, long e, long m) { long result = 1; while (e > 0) { if ((e & 1) == 1) { /* multiply in this bit's contribution while using modulus to keep * result small */ result = (result * b) % m; } b = (b * b) % m; e >>= 1; } return result; } static long submod(long x, long y, long m) { return (x - y + m) % m; } static long modInverse(long a, long m) { long g = gcdF(a, m); if (g != 1) { throw new IllegalArgumentException(""Inverse doesn't exist""); } else { // If a and m are relatively prime, then modulo // inverse is a^(m-2) mode m return modpow(a, m - 2, m); } } static public long gcdF(long a, long b) { while (b != 0) { long na = b; long nb = a % b; a = na; b = nb; } return a; } } } /* 5 3 2 3 8 8 2 8 5 10 1 */","import java.io.*; import java.util.*; 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; } } public static void main(String[] args) { FastReader obj = new FastReader(); PrintWriter out = new PrintWriter(System.out); int l = obj.nextInt(); while (l-- != 0) { int n = obj.nextInt(); int[] num = new int[n]; for (int i = 0; i < n; i++) num[i] = obj.nextInt(); Vector red = new Vector<>(); Vector blue = new Vector<>(); String s = obj.next(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'R') red.add(num[i]); else blue.add(num[i]); } Collections.sort(blue); Collections.sort(red); int c = 1, f = 0; for (int i = 0; i < blue.size(); i++) { if (blue.get(i) < c) { f = 1; break; } c++; } for (int i = 0; i < red.size(); i++) { if (red.get(i) > c) { f = 1; break; } c++; } if (f == 0) out.println(""YES""); else out.println(""NO""); } out.flush(); } } ",1 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 b5da712b,dd994c01," import java.io.*; import java.util.*; public class codeforces2 { static List primes; static final long X = 10000000000L; public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); // primes = sieveOfEratosthenes(100_000); // System.out.println(primes.toString()); int tc = sc.ni(); // int tc = 1; for (int rep = 0; rep < tc; rep++) { int N = sc.ni(); int[][] edges= new int[N-1][]; for (int i = 0; i < edges.length; i++) { edges[i] = new int[] {sc.ni() - 1, sc.ni() - 1}; } pw.println(solve(edges, N)); } pw.close(); } public static String solve(int[][] edges, int N) { Map> graph = new HashMap(); Map hash = new HashMap(); int idx = 0; for (int[] e : edges) { graph.putIfAbsent(e[0], new HashSet()); graph.get(e[0]).add(e[1]); graph.putIfAbsent(e[1], new HashSet()); graph.get(e[1]).add(e[0]); hash.put(getHash(e[0], e[1]), idx); idx++; } Queue q = new LinkedList(); //node, previous weight int[] initial = new int[2]; for (int node : graph.keySet()) { int len = graph.get(node).size(); if (len > 2) return ""-1""; if (len == 2) initial = new int[] {node, -1}; } q.offer(initial); int rotate = 0; // System.out.println(graph.toString()); int[] ret = new int[edges.length]; boolean[] vis = new boolean[N]; vis[initial[0]] = true; while (!q.isEmpty()) { int[] temp = q.poll(); int node = temp[0]; int old_color = temp[1]; if (old_color == -1) { int[] arr = new int[] {2, 11}; int p = 0; for (int neighbor : graph.get(node)) { ret[hash.get( getHash(node,neighbor) )] = arr[p]; q.offer(new int[] {neighbor, arr[p]}); p++; // System.out.println(); // System.out.println(neighbor + "" "" + graph.toString()); // System.out.println(Arrays.deepToString(edges)); vis[neighbor] = true; } } else if (old_color == 2) { int amt = 0; for (int neighbor : graph.get(node)) { if (vis[neighbor] == true) continue; vis[neighbor] = true; ret[hash.get( getHash(node,neighbor) )] = 11; q.offer(new int[] {neighbor, 11}); amt++; } if (amt >= 2) return ""-1""; } else { int amt = 0; for (int neighbor : graph.get(node)) { if (vis[neighbor] == true) continue; vis[neighbor] = true; ret[hash.get( getHash(node,neighbor) )] = 2; q.offer(new int[] {neighbor, 2}); amt++; } if (amt >= 2) return ""-1""; } } StringBuilder sb = new StringBuilder(); for (int x : ret) sb.append(x + "" ""); return sb.substring(0, sb.length() -1); } static long getHash(int u, int v) { if (u > v) { int temp = u; u = v; v = temp; } return u*X+v; } static boolean isSet(long n, int bit) { if (((1 << bit) & n) > 0) return true; return false; } static long nextPrime(long input){ long counter; input++; while(true){ int l = (int) Math.sqrt(input); counter = 0; for(long i = 2; i <= l; i ++){ if(input % i == 0) counter++; } if(counter == 0) return input; else{ input++; continue; } } } public static List sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; Arrays.fill(prime, true); for (int p = 2; p * p <= n; p++) { if (prime[p]) { for (int i = p * 2; i <= n; i += p) { prime[i] = false; } } } List primeNumbers = new LinkedList<>(); for (int i = 2; i <= n; i++) { if (prime[i]) { primeNumbers.add(i); } } return primeNumbers; } public static boolean perfectSquare(long n) { long lo = 0; long hi = n; while (lo < hi) { long k = (lo + hi) / 2; if (k * k < n) lo = k + 1; else hi = k; } return (lo * lo == n); } static Set divisors(int n) { Set set = new HashSet(); for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { // If divisors are equal, print only one if (n/i == i) set.add(i); else {// Otherwise print both set.add(i); set.add(n / i); } } } return set; } static Map primeFactorization(int x) { //first divide by 2 Map map = new HashMap(); if (x == 0) return map; int count = 0; while (x % 2 == 0) { x /=2; count++; } //insert 2 if (count > 0) map.put(2, count); for (int divisor = 3; divisor * divisor <= x; divisor += 2) { int cnt = 0; while (x % divisor == 0) { x /= divisor; cnt++; } if (cnt > 0) map.put(divisor, cnt); } if (x > 1) { map.put(x, 1); } return map; } static boolean isPrime(int n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 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); } // method to return LCM of two numbers static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } 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); } /* */ //printing methods /* */ //WOW! /* */ public static void printArr(PrintWriter pw, int[] arr) { StringBuilder sb = new StringBuilder(); for (int x : arr) { sb.append(x + """"); } sb.setLength(sb.length() - 1); pw.println(sb.toString()); } public static void printArr2d(PrintWriter pw, int[][] arr) { StringBuilder sb = new StringBuilder(); for (int[] row : arr) { for (int x : row) { sb.append(x + "" ""); } sb.setLength(sb.length() - 1); sb.append(""\n""); } sb.setLength(sb.length() - 1); pw.println(sb.toString()); } } 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[] 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; } } class UnionFind { int size; private int[] id; public UnionFind(int size) { this.size = size; id = new int[size]; for (int i = 0; i < id.length; i++) { id[i] = i; } } public int find(int a) { if (id[a] != a) { id[a] = find(id[a]); } return id[a]; } public void union(int a, int b) { int r1 = find(a); int r2 = find(b); if (r1 == r2) return; size--; id[r1] = r2; } @Override public String toString() { return Arrays.toString(id); } } class isSame { private long[] arr; TreeMap map = new TreeMap(); public isSame(long[] in) { arr = in; int prev = 0; for (int i = 1; i < arr.length; i++) { if (arr[i] == arr[i-1]) { } else { map.put(prev, i-1); prev = i; } } map.put(prev, arr.length - 1); } public boolean query(int l, int r) { Integer lower = map.floorKey(l); //should never happen i think if (lower == null) return false; return map.get(lower) >= r; } @Override public String toString() { return map.toString(); } }","//package codeforces.round766div2; import java.io.*; import java.util.*; import static java.lang.Math.*; public class C { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); solve(in.nextInt()); //solve(1); } /* General tips 1. It is ok to fail, but it is not ok to fail for the same mistakes over and over! 2. Train smarter, not harder! 3. If you find an answer and want to return immediately, don't forget to flush before return! */ /* Read before practice 1. Set a timer based on a problem's difficulty level: 45 minutes at your current target practice level; 2. During a problem solving session, focus! Do not switch problems or even worse switch to do something else; 3. If fail to solve within timer limit, read editorials to get as little help as possible to get yourself unblocked; 4. If after reading the entire editorial and other people's code but still can not solve, move this problem to to-do list and re-try in the future. 5. Keep a practice log about new thinking approaches, good tricks, bugs; Review regularly; */ /* Read before contests and lockout 1 v 1 Mistakes you've made in the past contests: 1. Tried to solve without going through given test examples -> wasting time on solving a different problem than asked; 2. Rushed to coding without getting a comprehensive sketch of your solution -> implementation bugs and WA; 3. Forgot about possible integer overflow; When stuck: 1. Understand problem statements? Walked through test examples? 2. Take a step back and think about other approaches? 3. Check rank board to see if you can skip to work on a possibly easier problem? 4. If none of the above works, take a guess? */ static int n; static List[] adj; static int[] ans; 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<>(); } ans = new int[n - 1]; for(int i = 0; i < n - 1; i++) { int u = in.nextInt(), v = in.nextInt(); adj[u].add(new int[]{v, i}); adj[v].add(new int[]{u, i}); } if(adj[1].size() > 2) out.println(-1); else { boolean can = false; if(adj[1].size() == 1) { ans[adj[1].get(0)[1]] = 2; if(dfs(adj[1].get(0)[0], 1, 2)) { can = true; } } else { ans[adj[1].get(0)[1]] = 2; ans[adj[1].get(1)[1]] = 3; if(dfs(adj[1].get(0)[0] ,1, 2) && dfs(adj[1].get(1)[0], 1, 3)) { can = true; } } if(can) { for(int v : ans) out.print(v + "" ""); out.println(); } else out.println(-1); } } out.close(); } static boolean dfs(int curr, int par, int prev) { if(adj[curr].size() - 1 > 1) return false; for(int[] next : adj[curr]) { if(next[0] == par) continue; ans[next[1]] = prev == 2 ? 3 : 2; if(!dfs(next[0], curr, prev == 2 ? 3 : 2)) { return false; } } return true; } 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; } List[] readGraphOneIndexed(int n, int m) { List[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); adj[u].add(v); adj[v].add(u); } return adj; } List[] readGraphZeroIndexed(int n, int m) { List[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u].add(v); adj[v].add(u); } return adj; } /* A more efficient way of building an undirected graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildUndirectedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]] = end2[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]] = end1[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building a directed graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildDirectedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]] = to[i]; idxForEachNode[from[i]]++; } return adj; } } }",0 624b8db5,eacc407e,"//created by Whiplash99 import java.io.*; import java.util.*; public class C { private static ArrayDeque[] edge; private static HashMap map; private static String getHash(int u, int v) { if(u>v) { int tmp=u; u=v; v=tmp; } return u+"" ""+v; } private static void DFS(int u, int p, int[] ans, int val) { for(int v:edge[u]) { if(v==p) continue; ans[map.get(getHash(u,v))]=val; DFS(v,u,ans,5-val); val=5-val; } } public static void main(String[] args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int i,N; int T=Integer.parseInt(br.readLine().trim()); StringBuilder sb=new StringBuilder(); while (T-->0) { N=Integer.parseInt(br.readLine().trim()); edge=new ArrayDeque[N]; for(i=0;i(); map=new HashMap<>(); int[] ans=new int[N-1]; int[] deg=new int[N]; for(i=0;i2) break; if(i[] adj; static int[] vals = { 2, 11 }; static int[] res; static void dfs(int u, int p, int par) { for (int[] nxt : adj[u]) { int v = nxt[0]; int idx = nxt[1]; if (v != p) { res[idx] = vals[par]; dfs(v, u, 1 ^ par); } } } 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(); res = new int[n - 1]; adj = new ArrayList[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList(); } for (int i = 0; i < n - 1; i++) { int u = sc.nextInt() - 1; int v = sc.nextInt() - 1; adj[u].add(new int[] { v, i }); adj[v].add(new int[] { u, i }); } boolean valid = true; int start = 0; for (int i = 0; i < n; i++) { if (adj[i].size() == 1) start = i; valid &= adj[i].size() <= 2; } dfs(start, -1, 0); if (valid) { for (int x : res) pw.print(x + "" ""); } else { pw.print(-1); } pw.println(); } pw.flush(); } 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 375489f2,a4c98ae1,"import java.io.*; import java.util.*; public class Main { static class FastInput { BufferedReader br; StringTokenizer st; public FastInput() { 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 long gcd(long a, long b) { if (b > a) return gcd(b, a); if (b == 0) return a; return gcd(b, a % b); } public static void plist(ArrayList arr) { for (int i = 0; i < arr.size(); i++) { System.out.print(arr.get(i) + "" ""); } System.out.println(); } static long[][] dp = new long[5005][5005]; static ArrayList filled = new ArrayList<>(); static ArrayList vacant = new ArrayList<>(); public static long calc(int i, int j){ if(i >= filled.size()) return 0; if(j >= vacant.size()) return Integer.MAX_VALUE; if(dp[i][j] != -1) return dp[i][j]; dp[i][j] = Math.min(Math.abs(filled.get(i) - vacant.get(j)) + calc(i+1, j+1), calc(i, j+1)); return dp[i][j]; } public static void run_case(FastInput s) { int n = s.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = s.nextInt(); if(arr[i] == 1){ filled.add(i); } else { vacant.add(i); } } for(int i = 0; i < filled.size(); i++){ for(int j = 0; j < vacant.size(); j++) dp[i][j] = -1; } System.out.println(calc(0, 0)); } public static void main(String[] args) { FastInput s = new FastInput(); run_case(s); } }","import java.io.*; import java.util.*; public class Main { static ArrayList one=new ArrayList<>(); static ArrayList zero=new ArrayList<>(); static long dp[][]= new long[5001][5001]; static long solve(int i,int j){ if (i==one.size())return 0; if (j==zero.size())return Integer.MAX_VALUE; if (dp[i][j]!=-1){ return dp[i][j]; } return dp[i][j]=Math.min(solve(i+1,j+1)+Math.abs(one.get(i)-zero.get(j)),solve(i,j+1)); } public static void main(String[] args) { FastScanner sc = new FastScanner(); // int t=sc.nextInt(); // while (t-->=1){ int n=sc.nextInt(); int a[]=sc.readArray(n); for (long i[]:dp){ Arrays.fill(i,-1); } for (int i=0;i 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 String sortString(String s) { char temp[] = s.toCharArray(); Arrays.sort(temp); return new String(temp); } static class Pair implements Comparable { String a; int b; public Pair(String a, int b) { this.a = a; this.b = b; } // to sort first part // public int compareTo(Pair other) { // if (this.a == other.a) return other.b > this.b ? -1 : 1; // else if (this.a > other.a) return 1; // else return -1; // } public int compareTo(Pair other) { // if (this.b == other.b) return 0; if (this.b > other.b) return 1; else return -1; } //sort on the basis of first part only // public int compareTo(Pair other) { // if (this.a == other.a) return 0; // else if (this.a > other.a) return 1; // else return -1; // } } static int[] frequency(String s){ int fre[]= new int[26]; for (int i=0;i0){ long lastBit=n&1; if (lastBit==1){ ans=(int)mul(ans,a); } a=(int)mul(a,a); n>>=1; } return ans; } static int[] find(int n, int start, int diff) { int a[] = new int[n]; a[0] = start; for (int i = 1; i < n; i++) a[i] = a[i - 1] + diff; return a; } static void swap(int a, int b) { int c = a; a = b; b = c; } static void printArray(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + "" ""); } } static boolean sorted(int a[]) { int n = a.length; boolean flag = true; for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) flag = false; } if (flag) return true; else return false; } public static int findlog(long n) { if (n == 0) return 0; if (n == 1) return 0; if (n == 2) return 1; double num = Math.log(n); double den = Math.log(2); if (den == 0) return 0; return (int) (num / den); } public static long gcd(long a, long b) { if (b % a == 0) return a; return gcd(b % a, a); } public static int gcdInt(int a, int b) { if (b % a == 0) return a; return gcdInt(b % a, a); } static void sortReverse(int[] a) { ArrayList l = new ArrayList<>(); for (int i : a) l.add(i); // Collections.sort.(l); Collections.sort(l, Collections.reverseOrder()); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } 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[] readArrayLong(long n) { long[] a = new long[(int) n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }",0 7d9b95c0,9d33ca79,"import java.util.*; import java.math.*; public class Main { public static class Edge{ int u; int v; // int wt; Edge(int u, int v){ this.u = u; this.v = v; // this.wt = wt; } } public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); StringBuilder sb = new StringBuilder(""""); for(int A=0 ; A[] graph = new ArrayList[n]; for(int i=0 ; i(); } String[] arr = new String[n-1]; for(int i=0 ; i 2) { flag = true; }else if(graph[i].size() == 1) src = i; } if(flag) { sb.append(-1 + ""\n""); }else { Map hm = new HashMap<>(); int count = 0; int val = 2; // System.out.println(src); while(count < n) { List e = graph[src]; int i=0; while(i < e.size() && hm.containsKey(src + "" "" + e.get(i).v)) i++; if(i < e.size()) { int nbr = e.get(i).v; // System.out.println(src + "" "" + nbr); hm.put(src + "" "" + nbr, val); hm.put(nbr + "" "" + src, val); val = 5 - val; src = nbr; } count++; } for(int i=0 ; i[]adj; static boolean vis []; static int edges[]; // we need to check that every path of length 1 or 2 must be a prime number // Idea--> we will only use 2 , 3 for the weight assignment // no assignment will be valid if there exists a node connected to 3 others 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(); vis= new boolean [n]; adj= new ArrayList[n]; TreeMap idx = new TreeMap<>(); TreeMap w= new TreeMap<>(); boolean notValid = false; for(int i =0;i(); } for(int i =1;i2||adj[v].size()>2)notValid=true; } if(notValid){ pw.println(-1); continue; } dfs(0,2,w); // vis[0]=true; // dfs(adj[0].get(0),2,w); // if(adj[0].size()==2)dfs(adj[0].get(1),3,w); for(int i =1;iweight){ vis[node]=true; int i =0; for(int x : adj[node]){ if(!vis[x]) { int min = Math.min(x , node); int max = Math.max(x , node); if(i%2==0){ weight.put(new Pair(min , max),w); dfs(x, 5-w,weight); } else{ weight.put(new Pair(min , max),5-w); dfs(x, w, weight); } i++; } } } static class Pair implements Comparable{ int x; int y ; Pair(int x , int y ){ this.x=x; this.y=y; } @Override public int compareTo(Pair o) { if(this.x==o.x)return this.y-o.y; return this.x-o.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 { 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 08cf0478,0f14b12d,"import java.util.*; import java.io.*; public class Main { static class Pair { int fi; int se; public Pair(int fi, int se) { this.fi = fi; this.se = se; } } public static void main(final String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int[][] arr = new int[5][n]; for(int i = 0; i < n; i++) { char[] s = sc.next().toCharArray(); int[] cnt = new int[5]; for(int j = 0; j < s.length; j++) { cnt[s[j]-'a']++; } for(int j = 0; j < 5; j++) arr[j][i] = cnt[j]-(s.length-cnt[j]); } int ans = 0; for(int i = 0; i < 5; i++) { Arrays.sort(arr[i]); int maxSum = 0; int words = 0; for(int j = arr[i].length-1; j >=0; j--) { maxSum += arr[i][j]; if(maxSum > 0) words++; } ans = Math.max(ans, words); } System.out.println(ans); } } static 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 jdk.nashorn.internal.parser.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.*; import java.util.*; import javax.management.Query; public class Test{ 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(); String [] words = new String[n]; int [] occ = new int[5]; int [] occWord = new int [5]; boolean [] found ; for(int i =0;i pq = new PriorityQueue<>(); for (String word : words){ pq.add(new Pair(word,occOfMaxChar(word, maxChar)-occOfOtherChar(word, maxChar))); } int res = 0; int curr = 0; int maxCharCount = 0; int otherCharCount =0; while(!pq.isEmpty()){ String word = pq.poll().x; maxCharCount +=occOfMaxChar(word, maxChar); otherCharCount += occOfOtherChar(word, maxChar); curr ++; if(maxCharCount >otherCharCount){ res = curr; } } maxRes = Math.max(maxRes, res); } System.out.println(maxRes);} } public static int occOfMaxChar (String s, int maxChar){ int occ = 0; for(int i =0 ;i gold ; public static long dp (int idx,int time){ if ( idx == n){ return 0; } if (memo[idx][time] != -1){ return memo[idx][time]; } long take = 0; if (3 * w*depth[idx] <= time){ take = values[idx]+ dp(idx+1, time-3*w*depth[idx]); } long leave = dp(idx+1, time); return memo[idx][time]=Math.max(take, leave); } static class Pair implements Comparable { String x; int y; public Pair (String x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Object o){ Pair p = (Pair) o; return p.y -y; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public boolean hasNext() { // TODO Auto-generated method stub return false; } 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 boolean ready() throws IOException { return br.ready(); } } } ",0 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 4a570de6,9852706b,"import java.io.*; import java.lang.*; import java.util.*; public class c { static class FastScanner { InputStreamReader is; BufferedReader br; StringTokenizer st; public FastScanner() { is = new InputStreamReader(System.in); br = new BufferedReader(is); } String next() throws Exception { while (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int ni() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } int[] readArray(int num) throws Exception { int arr[]=new int[num]; for(int i=0;i= 0) { double i= Math.sqrt(x); if(i%1!=0) { return false; } return ((i * i) == x); } return false; } public static int[] ia(int n) { int ar[]=new int[n]; return ar; } public static long[] la(int n) { long ar[]=new long[n]; return ar; } static class pair implements Comparable{ int ht; int id; pair(int ht, int id) { this.ht=ht; this.id=id; } public int compareTo(pair p) { return this.ht-p.ht; } } public static void main(String args[]) throws java.lang.Exception { FastScanner sc=new FastScanner(); int t=sc.ni(); while(t-->0) { int n=sc.ni(); int m=sc.ni(); int x=sc.ni(); int ar[]=ia(n); for(int i=0;i pq=new PriorityQueue<>(); for(int i=0;i0){ 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); } }",0 63a24497,6e5ab1d2,"import java.util.*; import java.lang.*; import java.io.*; public class Main { void solve() { int n = in.nextInt(); char[] a = in.nextLine().toCharArray(); char[] b = in.nextLine().toCharArray(); int ans = MAX; ans = Math.min(ans, operations(a, b)); ans = Math.min(ans, change(a, b, '1')); ans = Math.min(ans, change(a, b, '0')); if (ans == MAX)ans = -1; out.append(ans + ""\n""); } int operations(char[] a, char[] b) { int count01 = 0 , count10 = 0; int n = a.length; for (int i = 0 ; i < n; i++) { if (a[i] != b[i]) { if (a[i] == '0')count01++; else count10++; } } if (count01 != count10)return MAX; return count01 + count10; } int change(char[] a, char[] b , char ch) { int n = a.length; char[] c = new char[n]; for (int i = 0 ; i < n; i++)c[i] = a[i]; int index = -1; for (int i = 0; i < n; i++) { if (c[i] == '1' && b[i] == ch) { index = i; break; } } if (index == -1)return MAX; for (int i = 0 ; i < n; i++) { if (i == index)continue; c[i] = (char)( '0' + ('1' - c[i]) ); } int local = operations(c, b); if (local == MAX)return MAX; return 1 + local; } public static void main (String[] args) { // It happens - Syed Mizbahuddin Main sol = new Main(); int t = 1; t = in.nextInt(); while (t-- != 0) { sol.solve(); } System.out.print(out); } void println(T[] s) { if (err == null)return; err.println(Arrays.toString(s)); } void println(T s) { if (err == null)return; err.println(s); } void println(int s) { if (err == null)return; err.println(s); } void println(int[] a) { if (err == null)return; println(Arrays.toString(a)); } int[] array(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } return a; } int[] array1(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) { a[i] = in.nextInt(); } return a; } int max(int[] a) { int max = a[0]; for (int i = 0; i < a.length; i++)max = Math.max(max, a[i]); return max; } int min(int[] a) { int min = a[0]; for (int i = 0; i < a.length; i++)min = Math.min(min, a[i]); return min; } int count(int[] a, int x) { int count = 0; for (int i = 0; i < a.length; i++)if (x == a[i])count++; return count; } void printArray(int[] a) { for (int ele : a)out.append(ele + "" ""); out.append(""\n""); } static { try { System.setIn(new FileInputStream(""input.txt"")); System.setOut(new PrintStream(new FileOutputStream(""output.txt""))); err = new PrintStream(new FileOutputStream(""error.txt"")); } catch (Exception e) {} } static FastReader in; static StringBuilder out; static PrintStream err; final int MAX; final int MIN; int mod ; Main() { in = new FastReader(); out = new StringBuilder(); MAX = Integer.MAX_VALUE; MIN = Integer.MIN_VALUE; mod = (int)1e9 + 7; } 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; } } class Pair implements Comparable { int first , second; Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair b) { return this.first - b.first; } public String toString() { String s = ""{ "" + Integer.toString(first) + "" , "" + Integer.toString(second) + "" }""; return s; } } } ","import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CMenorah solver = new CMenorah(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class CMenorah { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); char[] a = in.next().toCharArray(); char[] b = in.next().toCharArray(); int ans = checkPairs(a, b, -1); ans = Math.min(ans, changePair(a, b, '1')); ans = Math.min(ans, changePair(a, b, '0')); if (ans == Integer.MAX_VALUE) ans = -1; out.println(ans); } int changePair(char[] a, char[] b, char t) { int index = -1; int n = a.length; for (int i = 0; i < n; ++i) { if (a[i] == '1' && b[i] == t) { index = i; break; } } return checkPairs(a, b, index); } int checkPairs(char[] a, char[] b, int changeStringIndex) { int n = a.length; int val = 0; char[] tmp = new char[n]; System.arraycopy(a, 0, tmp, 0, n); if (changeStringIndex != -1) { val = 1; for (int i = 0; i < n; ++i) { if (i == changeStringIndex) continue; tmp[i] = a[i] == '0' ? '1' : '0'; } } int _10 = 0, _01 = 0; for (int i = 0; i < n; ++i) { if (tmp[i] != b[i]) { if (tmp[i] == '0') _01++; else _10++; } } return _01 == _10 ? 2 * _01 + val : Integer.MAX_VALUE; } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(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 String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } 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 0c0af0ff,d6a8d884,"import java.util.*; import java.lang.*; import java.io.*; public class Main { PrintWriter out = new PrintWriter(System.out); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""""); String next() throws IOException { if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } long nl() throws IOException { return Long.parseLong(next()); } long mod=1000000007; void solve() throws IOException { for (int tc=ni();tc>0;tc--) { int n=ni(); int[]A=new int[n]; long[]T=new long[n]; A[0]=ni(); T[0]=A[0]; long total=0; for (int i=1;i0) { if ((p&1)==1) r=(r*a)%mod; p>>=1; a=(a*a)%mod; } return r; } public static void main(String[] args) throws IOException { new Main().solve(); } }","//Praise our lord and saviour qlf9 //DecimalFormat f = new DecimalFormat(""##.00""); import java.util.*; import java.io.*; import java.math.*; import java.text.*; public class C{ public static void main(String[] omkar) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(in.readLine()); StringBuilder sb = new StringBuilder(); int cases = Integer.parseInt(st.nextToken()); for(int i = 0; i < cases; i++) { solve(in, st, sb); } System.out.println(sb); } public static void solve(BufferedReader in, StringTokenizer st, StringBuilder sb) throws Exception { st = new StringTokenizer(in.readLine()); int n = Integer.parseInt(st.nextToken()); int[] arr = readArr(n, in, st); int[] mins = new int[n]; mins[0] = arr[0]; mins[1] = arr[1]; for(int i = 2; i < n; i++) { mins[i] = Math.min(arr[i], mins[i-2]); } long[] sums = new long[n]; sums[0] = (long)(arr[0]); for(int i = 1; i < n; i++) { sums[i] = sums[i-1]+(long)(arr[i]); } long minc = Long.MAX_VALUE; long temp; for(int i = 1; i < n; i++) { temp = sums[i]; temp += (long)(mins[i])*(long)(n-(i+2)/2); temp += (long)(mins[i-1])*(long)(n-(i+1)/2); minc = Math.min(minc, temp); } sb.append(minc+""\n""); } public static int[] readArr(int N, BufferedReader in, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(in.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } }",0 a7063d01,ac4d0fc5,"import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.lang.reflect.Array; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; 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); solve(in, out); out.close(); } static String reverse(String s) { return (new StringBuilder(s)).reverse().toString(); } static void sieveOfEratosthenes(int n, int factors[], ArrayList ar) { factors[1]=1; int p; for(p = 2; p*p <=n; p++) { if(factors[p] == 0) { ar.add(p); factors[p]=p; for(int i = p*p; i <= n; i += p) if(factors[i]==0) factors[i] = p; } } for(;p<=n;p++){ if(factors[p] == 0) { factors[p] = p; ar.add(p); } } } 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 void sort2(char 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 class SegTree { long tree[]; long lz[]; long r; long combine(long a, long b){ return Math.min(a,b); } void build(long a[], int v, int tl, int tr) { if (tl == tr) { tree[v] = a[tl]; } else { int tm = (tl + tr) / 2; build(a, v*2, tl, tm); build(a, v*2+1, tm+1, tr); tree[v] = combine(tree[2*v], tree[2*v+1]); } } void pointUpdate(int v, int tl, int tr, int pos, long val) { if(tl>pos||tr> 1); pointUpdate(v*2, tl, tm, pos, val); pointUpdate(v*2+1, tm+1, tr, pos, val); tree[v] = combine(tree[2*v],tree[2*v+1]); } // void push(int v, int tl, int tr){ // if(tl==tr){ // lz[v] = 0; // return; // } // tree[2*v] += lz[v]; // tree[2*v+1] += lz[v]; // lz[2*v] += lz[v]; // lz[2*v+1] += lz[v]; // lz[v] = 0; // } // void rangeUpdate(int v, int tl, int tr, int l, int r, long val) { // if(tl>r||tr=l&&tr<=r){ // tree[v] += val; // lz[v] += val; // return; // } // int tm = ((tl + tr) >> 1); // rangeUpdate(v*2, tl, tm, l, r, val); // rangeUpdate(v*2+1, tm+1, tr, l, r, val); // tree[v] = combine(tree[2*v],tree[2*v+1]); // } long get(int v, int tl, int tr, int l, int r, long val) { if(l>tr||rval){ return 0; } if (tl == tr) { tree[v] = Integer.MAX_VALUE; return 1; } int tm = ((tl + tr) >> 1); long al = get(2*v, tl, tm, l, r, val); long ar = get(2*v+1, tm+1, tr, l, r, val); tree[v] = combine(tree[2*v],tree[2*v+1]); return al+ar; } } static class BIT{ int n; long tree[]; long operate(long el, long val){ return el+val; } void update(int x, long val){ for(;x0;x-=(x&(-x))){ sum = operate(sum, tree[x]); } return sum; } } static int parent[]; static int rank[]; static long m = 0; static int find_set(int v) { if (v == parent[v]) return v; return find_set(parent[v]); } static void make_set(int v) { parent[v] = v; rank[v] = 1; } static void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (rank[a] < rank[b]){ int tmp = a; a = b; b = tmp; } parent[b] = a; // if (rank[a] == rank[b]) // rank[a]++; rank[a] += rank[b]; max1 = Math.max(max1,rank[a]); } } static int parent1[]; static int rank1[]; static int find_set1(int v) { if (v == parent1[v]) return v; return find_set1(parent1[v]); } static void make_set1(int v) { parent1[v] = v; rank1[v] = 1; } static void union_sets1(int a, int b) { a = find_set1(a); b = find_set1(b); if (a != b) { if (rank1[a] < rank1[b]){ int tmp = a; a = b; b = tmp; } parent1[b] = a; // if (rank1[a] == rank1[b]) // rank1[a]++; rank1[a] += rank1[b]; } } static long max1 = 0; static int count = 0; static int count1 = 0; static boolean possible; public static void solve(InputReader sc, PrintWriter pw){ int i, j = 0; // int t = 1; long mod = 1000000007; // int factors[] = new int[1000001]; // ArrayList ar = new ArrayList<>(); // sieveOfEratosthenes(1000000, factors, ar); // HashSet set = new HashSet<>(); // for(int x:ar){ // set.add(x); // } int t = sc.nextInt(); u: while (t-- > 0) { int n = sc.nextInt(); int e[][] = new int[n-1][2]; int x[] = new int[n]; int m = 0; for(i=0;i2) pw.println(-1); else{ if(n==2){ pw.println(2); } else if(n==3){ pw.println(2+"" ""+3); } else{ int d = 0; int ans[] = new int[n-1]; ArrayList ar[] = new ArrayList[n]; ArrayList ar1[] = new ArrayList[n]; for(i=0;i(); ar1[i] = new ArrayList<>(); } for(i=0;i ar[], ArrayList ar1[], int ans[], int par, int v){ if(ar[d].get(0)!=par){ ans[ar1[d].get(0)] = v; visit(ar[d].get(0), ar, ar1, ans, d, 5-v); return; } if(ar[d].size()==1) return; ans[ar1[d].get(1)] = v; visit(ar[d].get(1), ar, ar1, ans, d, 5-v); } static void KMPSearch(char pat[], char txt[], int pres[]){ int M = pat.length; int N = txt.length; int lps[] = new int[M]; int j = 0; computeLPSArray(pat, M, lps); int i = 0; while (i < N) { if (pat[j] == txt[i]) { j++; i++; } if (j == M) { pres[i-1] = 1; j = lps[j - 1]; } else if (i < N && pat[j] != txt[i]) { if (j != 0) j = lps[j - 1]; else i = i + 1; } } } static void computeLPSArray(char pat[], int M, int lps[]) { int len = 0; int i = 1; lps[0] = 0; while (i < M) { if (pat[i] == pat[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = len; i++; } } } } static long[][] matrixMult(long a[][], long b[][], long mod){ int n = a.length; int m = a[0].length; int p = b[0].length; long c[][] = new long[n][p]; for(int i=0;i pos){ for(int i=2;i*i1){ int y = pos.get(num); a[y]++; } } static void assignAnc(ArrayList ar[], int depth[], int sz[], int par[][] ,int curr, int parent, int d){ depth[curr] = d; sz[curr] = 1; par[curr][0] = parent; for(int v:ar[curr]){ if(parent==v) continue; assignAnc(ar, depth, sz, par, v, curr, d+1); sz[curr] += sz[v]; } } 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<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 void formArrayForBinaryLifting(int n, int par[][]){ for(int j=1;j<20;j++){ for(int i=0;i { long a; long b; Pair1(long a, long b) { this.a = a; this.b = b; } public int compareTo(Pair1 p) { if(a!=p.a) return (a { int a; int b; int c; Pair(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public int compareTo(Pair p) { if(b!=p.b) return (b-p.b); return (a-p.a); } } static class Pair2 implements Comparable { int a; int b; int c; Pair2(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public int compareTo(Pair2 p) { return a-p.a; } } 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; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } 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); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 9992768); 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()); } } }","import java.io.*; import java.util.*; public class Main { static boolean[] ret; static boolean[] updated; static ArrayList[] adjacencyList; static Edge[] edgeList; static class Edge { int start, end, number; public Edge (int _start, int _end, int _number) { start = _start; end = _end; number = _number; } } public static void dfs(int node) { updated[node] = true; for (int next : adjacencyList[edgeList[node].start]) { if (!updated[next]) { ret[next] = !ret[node]; dfs(next); } } for (int next : adjacencyList[edgeList[node].end]) { if (!updated[next]) { ret[next] = !ret[node]; dfs(next); } } } 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))); int numCases = Integer.parseInt(br.readLine()); for (int i = 0; i < numCases; i++) { int numVertices = Integer.parseInt(br.readLine()); int[] numEdges = new int[numVertices]; edgeList = new Edge[numVertices - 1]; adjacencyList = new ArrayList[numVertices]; for (int j = 0; j < numVertices; j++) { adjacencyList[j] = new ArrayList<>(); } for (int j = 0; j < numVertices - 1; j++) { StringTokenizer st = new StringTokenizer(br.readLine()); int a = Integer.parseInt(st.nextToken()) - 1; int b = Integer.parseInt(st.nextToken()) - 1; edgeList[j] = new Edge(a, b, j); numEdges[a]++; numEdges[b]++; adjacencyList[a].add(j); adjacencyList[b].add(j); } boolean good = true; for (int j = 0; j < numVertices; j++) { if (numEdges[j] > 2) { good = false; break; } } if (!good) { pw.println(-1); } else { ret = new boolean[numVertices - 1]; updated = new boolean[numVertices - 1]; dfs(0); for (boolean b : ret) { if (b) pw.print(5 + "" ""); else pw.print(2 + "" ""); } pw.println(); } } br.close(); pw.close(); } }",0 35bb6075,ed728769,"/* COLLECTIONS FRAMEWORK TUTORIAL * HashMap .add(key, value) .get(key) .containsKey(key) : true/false .size() */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static final long M = 1000000007; // use main for only io public static void main(String args[]) { FastReader io = new FastReader(); new Solver().solve(io); } } //class Pair{ // int key;String value; // // public Pair(int key, String value){ // this.key = key; // this.value = value; // } //} class SparseTable{ int dp[][] = new int[300005][20]; int log[] = new int[300005]; public SparseTable(long a[], int n){ for(int i =0;i R)return 0; int j = log[R - L + 1]; if(a[dp[L][j]] < a[dp[R - (1 << j) + 1][j]])return dp[L][j]; return dp[R-(1 << j) + 1][j]; } } class Solver { static final int M = 998244353; void solve(FastReader io) { int t = io.nextInt(); while(t-- > 0){ int n = io.nextInt(); String s[] = new String[n]; for(int i = 0;i < n;i++)s[i] = io.nextLine(); int ans = 0; for(int i = 0;i < 5;i++){ int count[] = new int[n]; for(int j = 0;j < n;j++){ int freq = 0; for(int k = 0;k < s[j].length();k++){ // System.out.println(s[j].charAt(k) - 'a'); if((s[j].charAt(k) - 'a') == i){ freq++; } } // System.out.println(i + "" "" + freq); count[j] = 2*freq - s[j].length(); } Arrays.sort(count); // for(int it : count)System.out.print(it + "" ""); // System.out.println(); int curr = 0; int j = n - 1; for(;j >= 0 && (curr + count[j] > 0);j--){ curr += count[j]; } ans = Math.max(ans, n - j - 1); } System.out.println(ans); } } // returns the first key greater than or equal to val int lower_bound(int a[], int val) { int low = 0, high = a.length - 1, ret = -1; while (low <= high) { int mid = (low + high) / 2; if (a[mid] < val) low = mid + 1; else { ret = mid; high = mid - 1; } } return ret; } // returns the first key strictly greater than val int upper_bound(int a[], int val) { int low = 0, high = a.length - 1, ret = -1; while (low <= high) { int mid = (low + high) / 2; if (a[mid] <= val) low = mid + 1; else { ret = mid; high = mid - 1; } } return ret; } long modexp(long n, int m) { if (m == 0) return 1; else if (m == 1) return n; else { long p = modexp(n, m / 2); if (m % 2 == 1) return (((p * p) % M) * n) % M; else return (p * p) % M; } } long exp(long n, long m) { if (m == 0) return 1; if (m == 1) return n; long p = exp(n, m / 2); if (m % 2 == 1) return p * p * n; else return p * p; } long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } long inv(long n) { return modexp(n, M - 2); } long lcm(long a, long b) { return a * b / gcd(a, b); } long factorial(long fact[], int n) { fact[0] = 1; fact[1] = 1; long prod = 1; for (int i = 2; i <= n; i++) { prod = (prod * i) % M; fact[i] = prod; } return prod; } boolean isPrime(long n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } } 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.*; import java.io.*; public class Solution { 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(); } } 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 dp[][]; static long f(int n , int m) { if(m == 0) return 1; if(dp[n][m] != -1) return dp[n][m]; if(n < 9) dp[n][m] = f(n+1,m-1); else dp[n][m] = f(0,m-1)+f(1,m-1); dp[n][m] %= 1000000007; return dp[n][m]; } 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); } public static void main(String []args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); // sc.nextLine(); while(t-- > 0) { int n = sc.nextInt(); sc.nextLine(); //String s = sc.nextLine(); int arr[][] = new int[26][n]; for(int i = 0 ; i < n ; i++) { String s = sc.nextLine(); for(int j = 0 ; j < 26 ; j++) { int cnt = 0; for(int k = 0 ; k < s.length() ; k++) { if(s.charAt(k)-'a' == j) cnt++; } arr[j][i] = 2*cnt-s.length(); } } int ans = 0; for(int i = 0 ; i < 26 ; i++) { Arrays.sort(arr[i]); int tot = 0; for(int j = n-1 ; j >= 0 ;j--) { tot += arr[i][j]; if(tot <= 0) break; ans = Math.max(ans,n-j); } } System.out.println(ans); } } }",0 a7e7f371,f8e7b886," import java.io.*; import java.util.*; public class cp { static int mod=(int)1e9+7; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; static int[] arInt; static long[] arLong; public static void main(String[] args) throws IOException { long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); // int tc=1; // primeSet=new HashSet<>(); // 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.util.*; import java.io.*; import java.time.*; import static java.lang.Math.*; @SuppressWarnings(""unused"") public class C { static boolean DEBUG = false; static Reader fs; static PrintWriter pw; static void solve() { int n = fs.nextInt(), k[] = fs.readArray(n), h[] = fs.readArray(n); int prev_h = h[0], prev_k = k[0]; // int ans = 0; ArrayList intervals = new ArrayList<>(); for (int i = 0; i < n; i++) { int start = k[i] - h[i] + 1; int end = k[i]; intervals.add(new pair(start, end)); } // pw.println(intervals); Collections.sort(intervals); ArrayList merged = new ArrayList<>(); merge(intervals, merged); long ans = 0; for(int i = 0 ; i < merged.size() ; i++) { ans += sum(merged.get(i).len()); } pw.println(ans); } static void merge(ArrayLista1, ArrayLista2) { int n = a1.size(); int index = 0; for(int i =1 ; i < n ; i++) { if(a1.get(index).s >= a1.get(i).f) { a1.get(index).s = max(a1.get(index).s, a1.get(i).s); } else { index++; a1.set(index, a1.get(i)); } } for(int i = 0 ; i <= index ; i++) { a2.add(a1.get(i)); } // pw.println(a1); } // int index = 0; // Stores index of last element // // in output array (modified arr[]) // // // Traverse all input Intervals // for (int i=1; i= arr[i].start) // { // // Merge previous and current Intervals // arr[index].end = Math.max(arr[index].end, arr[i].end); // } // else { // index++; // arr[index] = arr[i]; // } // } static boolean overlapping(pair p1, pair p2) { if((p2.f >= p1.f && p1.s >= p2.f) || (p2.s >= p1.f && p1.s >= p2.s)) { return true; } return false; } static pair merge(pair p1, pair p2) { return new pair(min(p1.f, p2.f), max(p1.s, p2.s)); } static long sum(long n) { return (n * (n + 1) / 2); } static class pair implements Comparable{ int f, s; pair(int f, int s) { this.f = f; this.s = s; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append(""{"" + f + "","" + s + ""}""); return sb.toString(); } @Override public int compareTo(pair o) { return f - o.f; } public int len() { return s - f + 1; } } public static void main(String[] args) throws IOException { Instant start = Instant.now(); if (args.length == 2) { System.setIn(new FileInputStream(new File(""D:\\program\\javaCPEclipse\\CodeForces\\src\\input.txt""))); // System.setOut(new PrintStream(new File(""output.txt""))); System.setErr(new PrintStream(new File(""D:\\program\\javaCPEclipse\\CodeForces\\src\\error.txt""))); DEBUG = true; } fs = new Reader(); pw = new PrintWriter(System.out); int t = fs.nextInt(); while (t-- > 0) { solve(); } Instant end = Instant.now(); if (DEBUG) { pw.println(Duration.between(start, end)); } pw.close(); } static void sort(int a[]) { ArrayList l = new ArrayList(); for (int x : a) l.add(x); Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } public static void print(long a, long b, long c, PrintWriter pw) { pw.println(a + "" "" + b + "" "" + c); return; } static class Reader { BufferedReader br; StringTokenizer st; public Reader() { 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; } int[][] read2Array(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] = nextInt(); } } return a; } } }",0 624b8db5,90f01508,"//created by Whiplash99 import java.io.*; import java.util.*; public class C { private static ArrayDeque[] edge; private static HashMap map; private static String getHash(int u, int v) { if(u>v) { int tmp=u; u=v; v=tmp; } return u+"" ""+v; } private static void DFS(int u, int p, int[] ans, int val) { for(int v:edge[u]) { if(v==p) continue; ans[map.get(getHash(u,v))]=val; DFS(v,u,ans,5-val); val=5-val; } } public static void main(String[] args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int i,N; int T=Integer.parseInt(br.readLine().trim()); StringBuilder sb=new StringBuilder(); while (T-->0) { N=Integer.parseInt(br.readLine().trim()); edge=new ArrayDeque[N]; for(i=0;i(); map=new HashMap<>(); int[] ans=new int[N-1]; int[] deg=new int[N]; for(i=0;i2) break; if(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; } } }",0 40132ebc,d783d815,"import java.util.ArrayList; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Collections; import java.util.StringTokenizer; import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue; import java.util.List; import java.util.HashMap; import java.util.LinkedList; import java.util.HashSet; import java.util.Stack; import java.util.Queue; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.sqrt; import static java.lang.Math.abs; import static java.lang.Math.random; import static java.lang.Integer.MAX_VALUE; import static java.lang.Integer.MIN_VALUE; import static java.util.Collections.reverseOrder; public final class Test1 { static int mod = 1000000007; static int mod1 = 998244353; public static void main(String[] args) { solve(); } static long gcd(int a, int b) {if (b == 0) return a; return gcd(b, a % b); } static long modAdd(long a, long b, long m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} static long modMul(long a, long b, long m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} static long modSub(long a, long b, long m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} static long modDiv(long a, long b, long m) {a = a % m; b = b % m; return (modMul(a, mminvprime(b, m), m) + m) % m;} //only for prime m static long mminvprime(long a, long b) {return expo(a, b - 2, b);} static long expo(long a, long b, long mod) {long res = 1; while (b > 0) {if ((b & 1) != 0)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;} static long setBits(int n) {int cnt = 0; while (n != 0) { cnt++; n = n & (n - 1); } return cnt;} static int[] sieve(int n) {int[] arr = new int[n + 1]; for (int i = 2; i <= n; i++)if (arr[i] == 0) {for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return arr;} static void debug(int[] nums) { for (int i = 0; i < nums.length; i++) System.out.println(nums[i] + "" ""); } static void debug(long[] nums) { for (int i = 0; i < nums.length; i++) System.out.println(nums[i] + "" ""); } static void debug(String[] nums) { for (int i = 0; i < nums.length; i++) System.out.println(nums[i] + "" ""); } static void reverse(int[] nums) { int start = 0, end = nums.length - 1; while (start < end) {int temp = nums[start]; nums[start] = nums[end]; nums[end] = temp; start++; end--; } } static void reverse(char[] nums) { int start = 0, end = nums.length - 1; while (start < end) {char temp = nums[start]; nums[start] = nums[end]; nums[end] = temp; start++; end--; } } 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 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); } // <------------------------- CODE START FROM HERE --------------------------------> public static void solve() { FastScanner sc = new FastScanner(); int t = sc.nextInt(); while (t-- != 0) { int n = sc.nextInt(); String[] arr = new String[n]; for (int i = 0; i < n; i++) arr[i] = sc.next(); int cnt[] = new int[5]; for (int i = 0; i < n; i++) { for (char ch : arr[i].toCharArray()) { cnt[ch - 'a']++; } } int res = 0; for (int i = 0; i < 5; i++) { res = Math.max(res, fun(arr, cnt, (char)( 'a' + i))); } System.out.println(res); } } static int fun(String[] arr, int[] cnt, char c) { int n = arr.length; int total = cnt[0] + cnt[1] + cnt[2] + cnt[3] + cnt[4] - cnt[c - 'a']; int letter_total = cnt[c - 'a']; PriorityQueuepq = new PriorityQueue<>(); for (int i = 0; i < n; i++) { int letter = 0; int ext = 0; for (char ch : arr[i].toCharArray()) { if (ch == c) letter++; else ext++; } pq.offer(new Pair(letter, ext)); } while (pq.size() > 0) { if (total < letter_total) return pq.size(); Pair temp = pq.poll(); total -= temp.ext; letter_total -= temp.letter; } return 0; } static class Pair implements Comparable { int letter, ext; Pair(int letter, int ext) { this.letter = letter; this.ext = ext; } public int compareTo(Pair p) { return (p.ext - p.letter) - (this.ext - this.letter); } } // <----------------------------------CODE END HERE------------------------> // System.out.println(); // int b[] = a.clone(); }","import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Collections; import java.util.PriorityQueue; import java.util.Scanner; import java.util.StringJoiner; import java.util.StringTokenizer; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.ArrayList; import java.util.List; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.HashSet; import static java.lang.System.out; import static java.util.stream.Collectors.joining; public class C { static FastScanner sc = new FastScanner(System.in); public static void main(String[] args) { try (PrintWriter out = new PrintWriter(System.out)) { int T = sc.nextInt(); for (int tt = 1; tt <= T; tt++) { int n = sc.nextInt(); String[] strings = new String[n]; PriorityQueue[] frequencies = new PriorityQueue[5]; for (int i = 0; i < 5; i++) frequencies[i] = new PriorityQueue<>(); for (int i = 0; i < n; i++) { strings[i] = sc.next(); int[] freq = new int[5]; for (char c : strings[i].toCharArray()) freq[c - 'a']++; for (int j = 0; j < 5; j++) { //if (freq[j] > 0) frequencies[j].add(new Pair(freq[j], strings[i].length())); } } /* for (int i = 0; i < 5; i++) { while (!frequencies[i].isEmpty()) System.out.print(frequencies[i].remove() + "", ""); System.out.println(); }*/ int ans = 0; for (PriorityQueue pq : frequencies) { long curlen = 0; long curfreq = 0; int cnt = 0; while (!pq.isEmpty()) { Pair pair = pq.remove(); curfreq += pair.freq; curlen += pair.len; if (curfreq >= curlen / 2 + 1) { cnt++; } else { break; } } ans = Math.max(ans, cnt); } System.out.println(ans); } } } static class Pair implements Comparable { int freq, len; public Pair(int freq, int len) { this.freq = freq; this.len = len; } @Override public int compareTo(Pair o) { /*if(freq == 0 && o.freq == 0) return len - o.len; if(freq == 0) return len - (o.len - o.freq); if(o.freq == 0) return o.len - (len - freq); int diff1 = freq * o.len; int diff2 = o.freq * len; return diff2 - diff1;*/ return -(o.len - 2*o.freq - len + 2*freq); } @Override public String toString() { return freq + "" "" + len; } } /* 1 5 cbdca d a d eb */ static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f), 32768); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } } ",0 04df7bb8,1ea771ea," 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.io.*; import java.util.*; public class CODECHEF { 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(); } 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; } } static long MOD=1000000000; static class Pair{ long a; int b; Pair(long i,int j){ a=i; b=j; } } static long[] solve(int[] pos,long[] arr,int n,int k){ long[] ans=new long[n]; long[] left=new long[n]; long[] right=new long[n]; long min=Integer.MAX_VALUE; for(int i=0;i=0;i--){ min=Math.min(min+1,arr[i]); right[i]=min; } for(int i=0;i0){ int n=fs.nextInt(); int k=fs.nextInt(); int[] pos=new int[k]; for(int i=0;i0){ int n = sc.nextInt(); int ans[] = new int[n]; for(int i=0;i cycle = new ArrayList(); int x = ask(i), y = ask(i); cycle.add(y); while(y != x){ y = ask(i); cycle.add(y); } for(int j=0;j cycle = new ArrayList<>(); int startCycleValue = ask(i + 1); int currValueAt = ask(i + 1); cycle.add(currValueAt); while (currValueAt != startCycleValue) { currValueAt = ask(i + 1); cycle.add(currValueAt); } int m = cycle.size(); for (int j = 0; j < m; j++) { permutation[cycle.get(j)] = cycle.get((j + 1) % m); } } } out.println(""! ""); for (int i = 0; i < n; i++) { out.print((permutation[i] + 1) + "" ""); } out.println(); out.flush(); } private static int ask(int i) { out.println(""? "" + i + "" ""); out.flush(); int value = sc.nextInt(); return value - 1; } public static FastReader sc; public static PrintWriter out; static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException end) { end.printStackTrace(); } } return str.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 end) { end.printStackTrace(); } return str; } } }",0 23cb8587,48801d9e,"import java.io.*; import java.util.*; public class C { static long mod = (long) (1e9 + 7); public static void main(String[] args) throws IOException { Scanner scn = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); StringBuilder sb = new StringBuilder(); int T = scn.ni(), tcs = 0; C: while (tcs++ < T) { int n = scn.ni(); tree = new ArrayList[n + 1]; range = new long[n + 1][2]; for (int i = 0; i <= n; i++) tree[i] = new ArrayList<>(); for (int i = 1; i <= n; i++) { range[i][0] = scn.nl(); range[i][1] = scn.nl(); } for (int i = 0; i < n - 1; i++) { int x = scn.ni(); int y = scn.ni(); tree[x].add(y); tree[y].add(x); } strg = new long[n + 1][2]; for (long a1[] : strg) Arrays.fill(a1, -1L); sb.append(Math.max(DFS(1, -1, 0), DFS(1, -1, 1))); sb.append(""\n""); } out.print(sb); out.close(); } static ArrayList tree[]; static long range[][], strg[][]; static long DFS(int u, int pa, int ok) { if (strg[u][ok] != -1) return strg[u][ok]; long tg = 0; for (int ch : tree[u]) { if (ch == pa) continue; long sg = 0; if (ok == 0) { sg = Math.max(DFS(ch, u, 0) + Math.abs(range[u][0] - range[ch][0]), DFS(ch, u, 1) + Math.abs(range[u][0] - range[ch][1])); } else { sg = Math.max(DFS(ch, u, 0) + Math.abs(range[u][1] - range[ch][0]), DFS(ch, u, 1) + Math.abs(range[u][1] - range[ch][1])); } tg += sg; } return strg[u][ok] = tg; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int ni() throws IOException { return Integer.parseInt(next()); } public long nl() throws IOException { return Long.parseLong(next()); } public int[] nia(int n) throws IOException { int a[] = new int[n]; String sa[] = br.readLine().split("" ""); for (int i = 0; i < n; i++) a[i] = Integer.parseInt(sa[i]); return a; } public long[] nla(int n) throws IOException { long a[] = new long[n]; String sa[] = br.readLine().split("" ""); for (int i = 0; i < n; i++) a[i] = Long.parseLong(sa[i]); return a; } public void sort(int[] a) { ArrayList l = new ArrayList<>(); for (int v : a) l.add(v); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } public void sort(long[] a) { ArrayList l = new ArrayList<>(); for (long v : a) l.add(v); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } } }"," import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class C { private static FastReader fr = new FastReader(); private static PrintWriter out=new PrintWriter(System.out); private static Random random = new Random(); private static long[][] dp; private static long calculate(List[] graph, int current, long[][] r, boolean[] stack, int use){ if(dp[current][use] != -1) return dp[current][use]; stack[current] = true; long max = 0; if(graph[current] != null){ for(int next : graph[current]){ if(!stack[next]){ stack[next] = true; long r1 = Math.abs(r[current][use] - r[next][0]) + calculate(graph, next, r, stack, 0); long r2 = Math.abs(r[current][use] - r[next][1]) + calculate(graph, next, r, stack, 1); max += Math.max(r1, r2); } } } stack[current] = false; dp[current][use] = max; return max; } public static void main(String[] args) throws IOException { StringBuilder sb = new StringBuilder(); // code goes here int t = fr.nextInt(); while (t-- > 0){ int n = fr.nextInt(); long[][] r = new long[n][2]; for(int i = 0; i < n; i++){ r[i] = fr.nextLongArray(2); } List[] graph = new ArrayList[n]; for(int i = 0; i < n - 1; i++){ int u = fr.nextInt(); int v = fr.nextInt(); if(graph[u - 1] == null) graph[u - 1] = new ArrayList<>(); if(graph[v - 1] == null) graph[v - 1] = new ArrayList<>(); graph[u - 1].add(v - 1); graph[v - 1].add(u - 1); } boolean[] stack = new boolean[n]; dp = new long[n][2]; for(int i = 0; i < dp.length; i++){ Arrays.fill(dp[i], -1); } long r1 = calculate(graph, 0, r, stack, 0); long r2 = calculate(graph, 0, r, stack, 1); sb.append(Math.max(r1, r2)).append(""\n""); } System.out.print(sb.toString()); } static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i{ A first; B second; public Pair(A first, B second){ this.first = first; this.second = second; } } static long mod(String num, long a) { // Initialize result long res = 0; // One by one process all digits of 'num' for (int i = 0; i < num.length(); i++) res = (res*10 + num.charAt(i) - '0') %a; return res; } static long binomialCoeff(long n, long k, long MOD) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); res %= MOD; } return res; } static long power(long x, long y, long p) { // Initialize result long res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p static long modInverse(long n, long p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static long nCrModPFermat(int n, int r, long p) { // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r long[] fac = new long[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } } ",0 0b27be88,3f4a5b64,"import java.util.*; import java.io.*; public class C { static class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;ir1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]> tm=new TreeMap<>(); for(int i=1;i<=m;i++) { ans.append(i+"" ""); int tmp=input.scanInt(); if(!tm.containsKey(tmp)) { Stack stck=new Stack<>(); stck.add(i); tm.put(tmp, stck); } else { Stack stck=tm.get(tmp); stck.add(i); } } for(int i=m;i stck=tm.get(min); int indx=stck.pop(); ans.append(indx+"" ""); if(stck.isEmpty()) { tm.remove(min); } tmp+=min; if(!tm.containsKey(tmp)) { stck=new Stack<>(); stck.add(indx); tm.put(tmp, stck); } else { stck=tm.get(tmp); stck.add(indx); } } ans.append(""\n""); } System.out.println(ans); } } ","import java.util.*; import java.io.*; public class C { static class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;ir1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]> tm=new TreeMap<>(); for(int i=1;i<=m;i++) { ans.append(i+"" ""); int tmp=input.scanInt(); if(!tm.containsKey(tmp)) { Stack stck=new Stack<>(); stck.add(i); tm.put(tmp, stck); } else { Stack stck=tm.get(tmp); stck.add(i); } } for(int i=m;i stck=tm.get(min); int indx=stck.pop(); ans.append(indx+"" ""); if(stck.isEmpty()) { tm.remove(min); } tmp+=min; if(!tm.containsKey(tmp)) { stck=new Stack<>(); stck.add(indx); tm.put(tmp, stck); } else { stck=tm.get(tmp); stck.add(indx); } } ans.append(""\n""); } System.out.println(ans); } } ",1 8f6421f3,ea2fc2bc,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Arrays; public class Main{ public static void main (String[] args){ FastReader s = new FastReader(); int t=1;t=s.ni(); for(int test=1;test<=t;test++){ int n=s.ni(),k=s.ni(); int position[]=s.readArray(k),temp[]=s.readArray(k); int ans[]=new int[n]; Arrays.fill(ans,Integer.MAX_VALUE/2); for(int i=0;i=0;i--){ ans[i]=Math.min(ans[i],ans[i+1]+1); } for(int i=0;i=0;i--) { dp[i]=Math.min(dp[i], dp[i+1]+1); } //Print(dp); /*for(int i=0;i0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList list=new ArrayList<>(); ArrayList space=new ArrayList<>(); for(int i=0;i0) { int n = sc.nextInt(); int a[] = new int[n]; for(int i=0;i a1 = new ArrayList<>(); ArrayList a2 = new ArrayList<>(); for (int i = 0; i < n; i++) { if(a[i] == 0) a1.add(i); else a2.add(i); } long dp[][] = new long[n+1][n+1]; for (int i = 0; i <= n; i++) { Arrays.fill(dp[i],-1); } out.write(find(0,0,a1,a2,dp)+""\n""); } out.close(); } private static long find(int i, int j, List a1, ArrayList a2, long[][] dp) { if(j == a2.size()) return 0; int req = a2.size()-j; int ava = a1.size()-i; if(ava 0){ int n = in.nextInt() ; int dp[][] = new int[n][5] ; String tt[] = new String[n] ; for (int i = 0; i list = new ArrayList<>() ; for (int j = 0; j 0){ ans++ ; max = max(max , ans) ; } else break; } } System.out.println(max); } out.flush(); out.close(); // } catch (Exception e) { // return; // } } static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } 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); } 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.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 11373c16,d8e4eb5e,"/* 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; } } public static void main (String[] args) throws java.lang.Exception { FastReader scan = new FastReader(); PrintWriter pw = new PrintWriter(System.out); int n = scan.nextInt(); ArrayList a = new ArrayList<>(); ArrayList b = new ArrayList<>(); for(int i=0;i 0) { int n = sc.nextInt(); TreeSet set = new TreeSet<>(); long prev = -1; boolean flag = true; for (int i = 0; i < n; i++) { long x = sc.nextInt(); if (i == 0) { set.add(x); prev = x; continue; } if (x > prev) { Long high = set.higher(prev); if (high == null) set.add(x); else if (high >= x) set.add(x); else flag = false; } else if (x < prev) { Long low = set.lower(prev); if (low == null) set.add(x); else if (low <= x) set.add(x); else flag = false; } prev = x; } System.out.println(flag ? ""YES"" : ""NO""); } } /*****************************************************************************/ static class CP { BufferedReader bufferedReader; StringTokenizer stringTokenizer; public CP() { bufferedReader = new BufferedReader(new InputStreamReader(System.in)); } int nextInt() { return Integer.parseInt(NNNN()); } long nextLong() { return Long.parseLong(NNNN()); } double nextDouble() { return Double.parseDouble(NNNN()); } String NNNN() { while (stringTokenizer == null || !stringTokenizer.hasMoreElements()) { try { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return stringTokenizer.nextToken(); } String nextLine() { String spl = """"; try { spl = bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); } return spl; } } /*****************************************************************************/ }","import java.util.*; import java.io.*; public class _724 { public static void main(String[] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); TreeSet set = new TreeSet<>(); long prev = -1; boolean ok = true; for (int i = 0; i < n; i++) { long x = sc.nextInt(); if (i == 0) { set.add(x); prev = x; continue; } if (x > prev) { Long high = set.higher(prev); if (high == null) set.add(x); else if (high >= x) set.add(x); else { ok = false; } } else if (x < prev) { Long low = set.lower(prev); if (low == null) set.add(x); else if (low <= x) set.add(x); else { ok = false; } } prev = x; } out.println(ok ? ""YES"" : ""NO""); } out.close(); } static void sort(int[] a) { ArrayList q = new ArrayList<>(); for (int i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static void sort(long[] a) { ArrayList q = new ArrayList<>(); for (long i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public 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()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }",1 873828f7,ac8acb97,"import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { 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 1f6b81b1,51151974,"import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class InterestingStory { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader sc = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); int t = sc.nextInt(); // int t = 1; while (t-- != 0) { solver.solve(sc, out); } out.close(); } static class Solver { public void solve(InputReader sc, PrintWriter out) { int n = sc.nextInt(); char[][] words = new char[n][]; for(int i = 0; i < n; i++) words[i] = sc.next().toCharArray(); int[][] arr = new int[n][]; for(int i = 0; i < n; i++) { arr[i] = new int[words[i].length]; for(int j = 0; j < arr[i].length; j++) arr[i][j] = words[i][j]-'a'; } int max = 0; for(int now = 0; now < 5; now++) { PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder()); for(int i = 0; i < n; i++) { int nowcnt = 0; int othercnt = 0; for(int j = 0; j < arr[i].length; j++) { if(arr[i][j]==now) nowcnt++; else othercnt++; } pq.add(nowcnt-othercnt); } int canTake = 0; int sum = 0; while(!pq.isEmpty()) { int nowAdd = pq.poll(); if(sum+nowAdd>0) { sum += nowAdd; canTake++; } else { break; } } max = Math.max(max,canTake); } out.println(max); } } static void sort(int[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 0; i < n; i++) { int idx = rand.nextInt(n); if (idx == i) continue; arr[i] ^= arr[idx]; arr[idx] ^= arr[i]; arr[i] ^= arr[idx]; } Arrays.sort(arr); } static void sort(long[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 0; i < n; i++) { int idx = rand.nextInt(n); if (idx == i) continue; arr[i] ^= arr[idx]; arr[idx] ^= arr[i]; arr[i] ^= arr[idx]; } Arrays.sort(arr); } static void sortDec(int[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 0; i < n; i++) { int idx = rand.nextInt(n); if (idx == i) continue; arr[i] ^= arr[idx]; arr[idx] ^= arr[i]; arr[i] ^= arr[idx]; } Arrays.sort(arr); int l = 0; int r = n - 1; while (l < r) { arr[l] ^= arr[r]; arr[r] ^= arr[l]; arr[l] ^= arr[r]; l++; r--; } } static void sortDec(long[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 0; i < n; i++) { int idx = rand.nextInt(n); if (idx == i) continue; arr[i] ^= arr[idx]; arr[idx] ^= arr[i]; arr[i] ^= arr[idx]; } Arrays.sort(arr); int l = 0; int r = n - 1; while (l < r) { arr[l] ^= arr[r]; arr[r] ^= arr[l]; arr[l] ^= arr[r]; l++; r--; } } static class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private 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 peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } 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 long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long 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 String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } 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; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public BigInteger readBigInteger() { try { return new BigInteger(nextString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return nextString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public int[] nextSortedIntArray(int n) { int array[] = nextIntArray(n); Arrays.sort(array); return array; } public int[] nextSumIntArray(int n) { int[] array = new int[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; ++i) array[i] = nextLong(); return array; } public long[] nextSumLongArray(int n) { long[] array = new long[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextSortedLongArray(int n) { long array[] = nextLongArray(n); Arrays.sort(array); return array; } } } ","import java.io.*; import java.util.*; public class Solution{ public static void main (String[] args) throws java.lang.Exception { FastReader sc = new FastReader(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int testCase = sc.nextInt(); while (testCase-->0){ 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 hs = new HashSet<>(); int minLen = Integer.MAX_VALUE; for(int i=0 ; i=0 ; j--) rev += str[i].charAt(j); if(hs.contains(rev)) { mark = true; break; } if(str[i].length() == 3) { String ans = rev.substring(1, rev.length()); if(hs.contains(ans)){ mark = true; break; } } if(str[i].length() < 3) { for(int k=0 ; k<26 ; k++) { String str1 = (char)(k + 'a') + rev; if(hs.contains(str1)) { mark= true; break; } } } hs.remove(str[i]); } if(!mark) gs.append(""NO"" + ""\n""); else gs.append(""YES"" + ""\n""); } } System.out.println(gs); } }","import java.util.*; public class Main { public static void main(String[] args){ Scanner scn = new Scanner(System.in); int t = scn.nextInt(); StringBuilder sb = new StringBuilder(""""); for(int A=0 ; A hm = new HashMap<>(); int min = 4; for(int i=0 ; i=0 ; j--) s += arr[i].charAt(j); if(hm.containsKey(s)) { flag = true; break; } if(arr[i].length() < 3) { for(int k=0 ; k<26 ; k++) { String str = (char)(k + 'a') + s; if(hm.containsKey(str)) { flag= true; break; } } } if(arr[i].length() == 3) { String str = s.substring(1, s.length()); if(hm.containsKey(str)){ flag = true; break; } } hm.remove(arr[i]); } if(flag) sb.append(""YES"" + ""\n""); else sb.append(""NO"" + ""\n""); } } System.out.println(sb); } }",1 d55c238c,ebce9e39,"import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class First { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); //int a = 1; int t; t = in.nextInt(); //t = 1; while (t > 0) { //out.print(""Case #""+(a++)+"": ""); solver.call(in,out); t--; } out.close(); } static class TaskA { public void call(InputReader in, PrintWriter out) { int n, m, x; n = in.nextInt(); m = in.nextInt(); x = in.nextInt(); int[] arr = new int[n]; answer[] array = new answer[n]; int[] ar = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); array[i] = new answer(arr[i],i); } long[] ans = new long[m]; Arrays.sort(array); int a = 0 , b = 0; while(true){ for (int i = 0; i < m; i++) { ar[b] = i+1; b++; if(b==n){ break; } } if(b==n){ break; } for (int i = m-1; i >= 0; i--) { ar[b] = i+1; b++; if(b==n){ break; } } if(b==n){ break; } } for (int i = 0; i < n; i++) { ans[ar[i]-1] += array[i].a; } for (int i = 0; i < m-1; i++) { if(Math.abs(ans[i]- ans[i+1])>x){ out.println(""NO""); return; } } out.println(""YES""); int[] answer = new int[n]; for (int i = 0; i < n; i++) { answer[array[i].b] = ar[i]; } for (int i = 0; i < n; i++) { out.print(answer[i]+"" ""); } out.println(); } } 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; int b; public answer(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return o.a - this.a; } @Override public boolean equals(Object o){ if(o instanceof answer){ answer c = (answer)o; return a == c.a && b == c.b; } return false; } } 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) { if(o.c==this.c){ return this.a - o.a; } return o.c - this.c; } } 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; i 0) { //out.print(""Case #""+(a++)+"": ""); solver.call(in,out); t--; } out.close(); } static class TaskA { public void call(InputReader in, PrintWriter out) { int n, m, x; n = in.nextInt(); m = in.nextInt(); x = in.nextInt(); int[] arr = new int[n]; answer[] array = new answer[n]; int[] ar = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); array[i] = new answer(arr[i],i); } long[] ans = new long[m]; Arrays.sort(array); int a = 0 , b = 0; while(true){ for (int i = 0; i < m; i++) { ar[b] = i+1; b++; if(b==n){ break; } } if(b==n){ break; } for (int i = m-1; i >= 0; i--) { ar[b] = i+1; b++; if(b==n){ break; } } if(b==n){ break; } } for (int i = 0; i < n; i++) { ans[ar[i]-1] += array[i].a; } for (int i = 0; i < m-1; i++) { if(Math.abs(ans[i]- ans[i+1])>x){ out.println(""NO""); return; } } out.println(""YES""); int[] answer = new int[n]; for (int i = 0; i < n; i++) { answer[array[i].b] = ar[i]; } for (int i = 0; i < n; i++) { out.print(answer[i]+"" ""); } out.println(); } } 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; int b; public answer(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return o.a - this.a; } @Override public boolean equals(Object o){ if(o instanceof answer){ answer c = (answer)o; return a == c.a && b == c.b; } return false; } } 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) { if(o.c==this.c){ return this.a - o.a; } return o.c - this.c; } } 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; i c = new ArrayList<>(n); for (int i = 0; i < n; i++) { c.add(inputReader.nextLong()); } writer.println(solve(n, c)); } writer.close(); } public long solve(int n, List c) { long[] minEven = new long[n]; long[] minOdd = new long[n]; long[] sumOdd = new long[n]; long[] sumEven = new long[n]; minEven[0] = Long.MAX_VALUE; minOdd[0] = Long.MAX_VALUE; for (int i = 0; i < n; i++) { if (i > 0) { minEven[i] = minEven[i - 1]; minOdd[i] = minOdd[i - 1]; sumOdd[i] = sumOdd[i - 1]; sumEven[i] = sumEven[i - 1]; } if (i % 2 == 0) { minEven[i] = Math.min(minEven[i], c.get(i)); sumEven[i] += c.get(i); } else { minOdd[i] = Math.min(minOdd[i], c.get(i)); sumOdd[i] += c.get(i); } } long best = Long.MAX_VALUE; for (int k = 1; k < n; k++) { int countOdd = (k + 1) / 2; int countEven = (k + 1) / 2; if (k % 2 == 0) { countEven++; } long oddResult = minOdd[k] * (n - countOdd) + sumOdd[k]; long evenResult = minEven[k] * (n - countEven) + sumEven[k]; long current = oddResult + evenResult; best = Math.min(best, current); } return best; } private 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()); } public long nextLong() { return Long.parseLong(next()); } } } ","import java.io.DataInputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class Main { private static void run() throws IOException { int n = in.nextInt(); long[] c = new long[n]; for (int i = 0; i < n; i++) { c[i] = in.nextInt(); } long ans = (c[0] + c[1]) * n; long sum = c[0] + c[1]; long[] min = {c[0], c[1]}; for (int i = 2; i < n; i++) { sum += c[i]; int index = i % 2; min[index] = Math.min(min[index], c[i]); int[] times = new int[2]; times[index] = n - (i / 2 + 1); times[index ^ 1] = n - ((i - 1) / 2 + 1); ans = Math.min(ans, sum + min[0] * times[0] + min[1] * times[1]); } out.println(ans); } 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 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(); } } }",0 0ca738ce,931faca2,"import java.io.*; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class G { public static void main(String[] args) { FastReader scan = new FastReader(); PrintWriter out = new PrintWriter(System.out); Task solver = new Task(); int t = 1; for(int tt = 1; tt <= t; tt++) solver.solve(tt, scan, out); out.close(); } static class Task { static int max = (int) (4e5), MOD = 998244353; static long[] fact = new long[max+1], invFact = new long[max+1], naturalInverse = new long[max+1]; public void solve(int testNumber, FastReader scan, PrintWriter out) { int n = scan.nextInt(), k = scan.nextInt(); Item[] lanterns = new Item[2 * n]; for(int i = 0; i < n; i++) { int l = scan.nextInt(), r = scan.nextInt(); lanterns[i * 2] = new Item(l, 0); lanterns[i * 2 + 1] = new Item(r, 1); } Arrays.sort(lanterns); precomp(); int have = 0; long ans = 0; for(Item x : lanterns) { if(x.start == 1) have--; else { ans = (ans + binomial(have, k - 1)) % MOD; have++; } } out.println(ans); } static class Item implements Comparable { int val; int start; public Item(int a, int b) { val = a; start = b; } @Override public int compareTo(Item item) { int ret = Integer.compare(val, item.val); if(ret == 0) ret = Integer.compare(start, item.start); return ret; } } static void precomp() { fact[0] = invFact[0] = invFact[1] = naturalInverse[0] = naturalInverse[1] = 1; for(int i = 1; i <= max; i++) { fact[i] = (fact[i-1]*i)%MOD; if(i == 1) continue; naturalInverse[i] = naturalInverse[MOD % i] * (MOD - MOD/i) % MOD; invFact[i] = (invFact[i-1]*naturalInverse[i])%MOD; } } static long binomial(int a, int b) { if(a < b) return 0; return ((fact[a]*invFact[b])%MOD*invFact[a-b])%MOD; } } 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.io.*; import java.math.*; public class Main { static FastReader sc=new FastReader(); static int dp[][][][]; static int mod=1000000007; static int mod1=998244353; static int max; static long bit[]; static long seg[]; static long fact[]; static long A[]; static long[] fac = new long[300001]; static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) { //CHECK FOR N=1 //CHECK FOR N=1 //StringBuffer sb=new StringBuffer(""""); int ttt=1; // ttt =i(); fac[0] = 1; for (int i = 1; i <= 300000; i++) fac[i] = fac[i - 1] * i % mod1; outer :while (ttt-- > 0) { int n=i(); int k=i(); Pair P[]=new Pair[2*n]; int c=0; 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) { 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; // } } //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=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 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 169e34bf,d9199dfd,"import java.util.*; public class D{ static Scanner sc; public static void solve(){ int n=sc.nextInt(); Integer a[]=new Integer[n]; int flag; 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 161b4a40,da5cf40b,"import java.util.*; import java.io.*; public class Main{ static final Random random=new Random(); static long mod=1000000007L; static HashMapmap=new HashMap<>(); static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } 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().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } int[] readIntArray(int n){ int[] res=new int[n]; for(int i=0;i 0){ solve(in); } out.close(); } catch (Exception e) { return; } } public static void solve( FastReader in){ int n=in.nextInt(); String s=in.next(); String t=in.next(); //int k=in.nextInt(); //long y=in.nextInt(); //long n=in.nextLong(); //int k=in.nextInt(); //long k=in.nextLong(); StringBuilder res=new StringBuilder(); char[] s1=s.toCharArray(); char[] t1=t.toCharArray(); int ans=n+2; int[] cnt={0,0}; for(int i=0;in){ res.append(""-1""); } else{ res.append(""""+ans); } //int ans=x.size()+y.size(); //res.append(""""+""Yes""); //res.append(""""+""""); System.out.println(res.toString()); } static int gcd(int a,int b){ if(b==0){ return a; } return gcd(b,a%b); } 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); } static void reversesort(int[] arr) { ArrayList ls = new ArrayList(); for(int x: arr) ls.add(x); Collections.sort(ls); Collections.reverse(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static void debug(String x){ System.out.println(x); } static < E > void print(E res) { System.out.println(res); } static String rString(String s){ StringBuilder sb=new StringBuilder(); sb.append(s); return sb.reverse().toString(); } }","/***** ---> :) Vijender Srivastava (: <--- *****/ import java.util.*; import java.lang.*; import java.io.*; public class Main { static FastReader sc =new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=(long)1e9+7; /* start */ public static void main(String [] args) { // int testcases = 1; int testcases = i(); while(testcases-->0) { solve(); } out.flush(); out.close(); } static void solve() { int n = i(); char c[] = inputC(); char d[] = inputC(); int x01=0,x10=0,x00=0,x11=0; for(int i=0;i 0) { if (y % 2 == 1) res = (res * x) ; y = y >> 1; x = (x * x); } return res; } 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 long[] sort(long a[]) { ArrayList arr = new ArrayList<>(); for(long i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } static int[] sort(int a[]) { ArrayList arr = new ArrayList<>(); for(Integer i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } //pair class private static class Pair implements Comparable { long first, second; public Pair(long f, long s) { first = f; second = s; } @Override public int compareTo(Pair p) { if (first > p.first) return 1; else if (first < p.first) return -1; else { if (second > p.second) return 1; else if (second < p.second) return -1; else return 0; } } } }",0 312d9460,e7024f3e,"//https://codeforces.com/contest/1547/problem/E //E. Air Conditioners import java.util.*; import java.io.*; public class CF_1547_E{ public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); StringBuilder sb = new StringBuilder(); StringTokenizer st, st1; int q = Integer.parseInt(br.readLine()); while(q-->0){ br.readLine(); st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); PriorityQueue right_side = new PriorityQueue(); int at[] = new int[n+1]; st = new StringTokenizer(br.readLine()); st1 = new StringTokenizer(br.readLine()); for(int i=0;i{ int a, t; Pair(int a, int t){ this.a = a; this.t = t; } public int compareTo(Pair A){ return (this.a+this.t-A.a-A.t); } }","import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { static String solve(int n, int k, int[] a, int[] t) { Pair[] pairs = new Pair[k]; for (int i = 0; i < k; i++) { pairs[i] = new Pair(a[i], t[i]); } Arrays.sort(pairs); int[] ret = new int[n + 1]; Arrays.fill(ret, Integer.MAX_VALUE); int pIdx = 0; int ct = pairs[pIdx].t; ret[pairs[pIdx].a] = ct; for (int i = pairs[pIdx].a + 1; i <= n; i++) { ct++; if (pIdx + 1 < k && pairs[pIdx + 1].a == i) { if (ct > pairs[pIdx + 1].t) { ct = pairs[pIdx + 1].t; } pIdx++; } ret[i] = ct; // System.out.println(Arrays.toString(ret)); } // System.out.println(); pIdx = k - 1; ct = pairs[pIdx].t; for (int i = pairs[pIdx].a - 1; i > 0; i--) { ct++; if (pIdx - 1 >= 0 && pairs[pIdx - 1].a == i) { if (ct > pairs[pIdx - 1].t) { ct = pairs[pIdx - 1].t; } pIdx--; } if (ct < ret[i]) { ret[i] = ct; } // System.out.println(Arrays.toString(ret)); } StringBuilder out = new StringBuilder(); for (int i = 1; i <= n; i++) { out.append(ret[i]).append("" ""); } // System.out.println(); // System.out.println(); return out.toString(); } static class Pair implements Comparable { int a, t; public Pair(int a, int t) { this.a = a; this.t = t; } @Override public int compareTo(Pair o) { return this.a - o.a; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); StringBuilder out = new StringBuilder(); int T = Integer.parseInt(st.nextToken()); while (T-- > 0) { st = new StringTokenizer(br.readLine()); st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] a = new int[k]; for (int i = 0; i < k; i++) { a[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(br.readLine()); int[] t = new int[k]; for (int i = 0; i < k; i++) { t[i] = Integer.parseInt(st.nextToken()); } out.append(solve(n, k, a, t)).append(""\n""); } System.out.println(out); } }",0 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 8f30bfc3,af1b152e,"import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.InputMismatchException; import java.util.List; import java.util.StringJoiner; public class Solution { private static List chairs; private static List folks; private static Long[] data; private static Long[][] cache; public static void main(String[] args) { Print print = new Print(); Scan scan = new Scan(); int n = scan.scanInt(); data = scan.scan1dLongArray(); chairs = new ArrayList<>(); folks = new ArrayList<>(); cache = new Long[n][n]; for (int i = 0; i < n; i++) { if (data[i] == 0) { chairs.add((long) i); } else { folks.add((long) i); } } print.printLine(Long.toString(solve(folks, chairs, 0, 0))); print.close(); } private static long solve(List folks, List chairs, int i, int j) { if (i == folks.size()) { return 0; } if (j == chairs.size()) { return Integer.MAX_VALUE; } if (cache[i][j] != null) { return cache[i][j]; } return cache[i][j] = Math .min(Math.abs(folks.get(i) - chairs.get(j)) + solve(folks, chairs, i + 1, j + 1), solve(folks, chairs, i, j + 1)); } static class Scan { private byte[] buf = new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in = System.in; } public int scan() { if (total < 0) { throw new InputMismatchException(); } if (index >= total) { index = 0; try { total = in.read(buf); } catch (IOException ignored) { } if (total <= 0) { return -1; } } return buf[index++]; } public int scanInt() { int integer = 0; int n = scan(); while (isWhiteSpace(n)) { n = scan(); } int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } else { throw new InputMismatchException(); } } return neg * integer; } public double scanDouble() { double doub = 0; int n = scan(); while (isWhiteSpace(n)) { n = scan(); } int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n) && n != '.') { if (n >= '0' && n <= '9') { doub *= 10; doub += n - '0'; n = scan(); } else { throw new InputMismatchException(); } } if (n == '.') { n = scan(); double temp = 1; while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { temp /= 10; doub += (n - '0') * temp; n = scan(); } else { throw new InputMismatchException(); } } } return doub * neg; } public Integer[] scan1dIntArray() { String[] s = this.scanString().split("" ""); Integer[] arr = new Integer[s.length]; for (int i = 0; i < s.length; i++) { arr[i] = Integer.parseInt(s[i]); } return arr; } public Integer[][] scan2dIntArray(int n, int m) { Integer[][] arr = new Integer[n][m]; for (int i = 0; i < n; i++) { String[] s = this.scanString().split("" ""); for (int j = 0; j < m; j++) { arr[i][j] = Integer.parseInt(s[j]); } } return arr; } public String[] scan1dStringArray() { return this.scanString().split("" ""); } public String[][] scan2dStringArray(int n, int m) { String[][] arr = new String[n][m]; for (int i = 0; i < n; i++) { String[] s = this.scanString().split("" ""); for (int j = 0; j < m; j++) { arr[i][j] = s[j]; } } return arr; } public char[] scan1dCharArray() { return this.scanString().toCharArray(); } public char[][] scan2dCharArray(int n, int m) { char[][] arr = new char[n][m]; for (int i = 0; i < n; i++) { char[] s = this.scanString().toCharArray(); for (int j = 0; j < m; j++) { arr[i][j] = s[j]; } } return arr; } public Long[] scan1dLongArray() { String[] s = this.scanString().split("" ""); Long[] arr = new Long[s.length]; for (int i = 0; i < s.length; i++) { arr[i] = Long.parseLong(s[i]); } return arr; } public Long[][] scan2dLongArray(int n, int m) { Long[][] arr = new Long[n][m]; for (int i = 0; i < n; i++) { String[] s = this.scanString().split("" ""); for (int j = 0; j < m; j++) { arr[i][j] = Long.parseLong(s[j]); } } return arr; } public String scanString() { StringBuilder sb = new StringBuilder(); int n = scan(); while (isWhiteSpace(n)) { n = scan(); } while (!isWhiteSpace(n)) { sb.append((char) n); n = scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if (n == '\n' || n == '\r' || n == '\t' || n == -1) { return true; } return false; } } static class Print { private final BufferedWriter bw; public Print() { bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(String str) { try { bw.append(str); } catch (IOException ignored) { } } public void printLine(Integer[] arr) { StringJoiner sj = new StringJoiner("" ""); for (Integer x : arr) { sj.add(Integer.toString(x)); } printLine(sj.toString()); } public void printLine(Integer[][] arr) { for (Integer[] x : arr) { StringJoiner sj = new StringJoiner("" ""); for (Integer y : x) { sj.add(Integer.toString(y)); } printLine(sj.toString()); } } public void printLine(String[] arr) { StringJoiner sj = new StringJoiner("" ""); for (String x : arr) { sj.add(x); } printLine(sj.toString()); } public void printLine(String[][] arr) { for (String[] x : arr) { StringJoiner sj = new StringJoiner("" ""); for (String y : x) { sj.add(y); } printLine(sj.toString()); } } public void printLine(char[] arr) { StringJoiner sj = new StringJoiner("" ""); for (char x : arr) { sj.add(Character.toString(x)); } printLine(sj.toString()); } public void printLine(char[][] arr) { for (char[] x : arr) { StringJoiner sj = new StringJoiner("" ""); for (char y : x) { sj.add(Character.toString(y)); } printLine(sj.toString()); } } public void printLine(Long[] arr) { StringJoiner sj = new StringJoiner("" ""); for (Long x : arr) { sj.add(Long.toString(x)); } printLine(sj.toString()); } public void printLine(Long[][] arr) { for (Long[] x : arr) { StringJoiner sj = new StringJoiner("" ""); for (Long y : x) { sj.add(Long.toString(y)); } printLine(sj.toString()); } } public void printLine(String str) { print(str); try { bw.append(""\n""); } catch (IOException ignored) { } } public void close() { try { bw.close(); } catch (IOException ignored) { } } } } ","import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.InputMismatchException; import java.util.List; import java.util.StringJoiner; public class D { private static List chairs; private static List folks; private static Long[] data; private static Long[][] cache; public static void main(String[] args) { Print print = new Print(); Scan scan = new Scan(); int n = scan.scanInt(); data = scan.scan1dLongArray(); chairs = new ArrayList<>(); folks = new ArrayList<>(); cache = new Long[n][n]; for (int i = 0; i < n; i++) { if (data[i] == 0) { chairs.add((long) i); } else { folks.add((long) i); } } print.printLine(Long.toString(solve(folks, chairs, 0, 0))); print.close(); } private static long solve(List folks, List chairs, int i, int j) { if (i == folks.size()) { return 0; } if (j == chairs.size()) { return Integer.MAX_VALUE; } if (cache[i][j] != null) { return cache[i][j]; } return cache[i][j] = Math .min(Math.abs(folks.get(i) - chairs.get(j)) + solve(folks, chairs, i + 1, j + 1), solve(folks, chairs, i, j + 1)); } static class Scan { private byte[] buf = new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in = System.in; } public int scan() { if (total < 0) { throw new InputMismatchException(); } if (index >= total) { index = 0; try { total = in.read(buf); } catch (IOException ignored) { } if (total <= 0) { return -1; } } return buf[index++]; } public int scanInt() { int integer = 0; int n = scan(); while (isWhiteSpace(n)) { n = scan(); } int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } else { throw new InputMismatchException(); } } return neg * integer; } public double scanDouble() { double doub = 0; int n = scan(); while (isWhiteSpace(n)) { n = scan(); } int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n) && n != '.') { if (n >= '0' && n <= '9') { doub *= 10; doub += n - '0'; n = scan(); } else { throw new InputMismatchException(); } } if (n == '.') { n = scan(); double temp = 1; while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { temp /= 10; doub += (n - '0') * temp; n = scan(); } else { throw new InputMismatchException(); } } } return doub * neg; } public Integer[] scan1dIntArray() { String[] s = this.scanString().split("" ""); Integer[] arr = new Integer[s.length]; for (int i = 0; i < s.length; i++) { arr[i] = Integer.parseInt(s[i]); } return arr; } public Integer[][] scan2dIntArray(int n, int m) { Integer[][] arr = new Integer[n][m]; for (int i = 0; i < n; i++) { String[] s = this.scanString().split("" ""); for (int j = 0; j < m; j++) { arr[i][j] = Integer.parseInt(s[j]); } } return arr; } public String[] scan1dStringArray() { return this.scanString().split("" ""); } public String[][] scan2dStringArray(int n, int m) { String[][] arr = new String[n][m]; for (int i = 0; i < n; i++) { String[] s = this.scanString().split("" ""); for (int j = 0; j < m; j++) { arr[i][j] = s[j]; } } return arr; } public char[] scan1dCharArray() { return this.scanString().toCharArray(); } public char[][] scan2dCharArray(int n, int m) { char[][] arr = new char[n][m]; for (int i = 0; i < n; i++) { char[] s = this.scanString().toCharArray(); for (int j = 0; j < m; j++) { arr[i][j] = s[j]; } } return arr; } public Long[] scan1dLongArray() { String[] s = this.scanString().split("" ""); Long[] arr = new Long[s.length]; for (int i = 0; i < s.length; i++) { arr[i] = Long.parseLong(s[i]); } return arr; } public Long[][] scan2dLongArray(int n, int m) { Long[][] arr = new Long[n][m]; for (int i = 0; i < n; i++) { String[] s = this.scanString().split("" ""); for (int j = 0; j < m; j++) { arr[i][j] = Long.parseLong(s[j]); } } return arr; } public String scanString() { StringBuilder sb = new StringBuilder(); int n = scan(); while (isWhiteSpace(n)) { n = scan(); } while (!isWhiteSpace(n)) { sb.append((char) n); n = scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if (n == '\n' || n == '\r' || n == '\t' || n == -1) { return true; } return false; } } static class Print { private final BufferedWriter bw; public Print() { bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(String str) { try { bw.append(str); } catch (IOException ignored) { } } public void printLine(Integer[] arr) { StringJoiner sj = new StringJoiner("" ""); for (Integer x : arr) { sj.add(Integer.toString(x)); } printLine(sj.toString()); } public void printLine(Integer[][] arr) { for (Integer[] x : arr) { StringJoiner sj = new StringJoiner("" ""); for (Integer y : x) { sj.add(Integer.toString(y)); } printLine(sj.toString()); } } public void printLine(String[] arr) { StringJoiner sj = new StringJoiner("" ""); for (String x : arr) { sj.add(x); } printLine(sj.toString()); } public void printLine(String[][] arr) { for (String[] x : arr) { StringJoiner sj = new StringJoiner("" ""); for (String y : x) { sj.add(y); } printLine(sj.toString()); } } public void printLine(char[] arr) { StringJoiner sj = new StringJoiner("" ""); for (char x : arr) { sj.add(Character.toString(x)); } printLine(sj.toString()); } public void printLine(char[][] arr) { for (char[] x : arr) { StringJoiner sj = new StringJoiner("" ""); for (char y : x) { sj.add(Character.toString(y)); } printLine(sj.toString()); } } public void printLine(Long[] arr) { StringJoiner sj = new StringJoiner("" ""); for (Long x : arr) { sj.add(Long.toString(x)); } printLine(sj.toString()); } public void printLine(Long[][] arr) { for (Long[] x : arr) { StringJoiner sj = new StringJoiner("" ""); for (Long y : x) { sj.add(Long.toString(y)); } printLine(sj.toString()); } } public void printLine(String str) { print(str); try { bw.append(""\n""); } catch (IOException ignored) { } } public void close() { try { bw.close(); } catch (IOException ignored) { } } } } ",1 18e2441c,24b20554," import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.Map; import java.util.HashMap; public class cf1515 { 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(); } static class Task { public void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- != 0) { int n = in.nextInt(); int m = in.nextInt(); int x = in.nextInt(); TreeMap> map = new TreeMap<>(); for (int i = 0; i < n; i++) { int j = in.nextInt(); if (!map.containsKey(j)) { map.put(j, new ArrayList()); } map.get(j).add(i); } out.println(""YES""); int[] ans = new int[n]; int sta = 0; for (int s : map.keySet()) { for (int i = 0; i < map.get(s).size(); i++) { ans[map.get(s).get(i)] = (sta++) % m + 1; } } for(int i=0;i 0) { int n=sr.nextInt(); int m=sr.nextInt(); int x=sr.nextInt(); TreeMap>h=new TreeMap<>(); for(int i=0;i()); h.get(a).add(i); } ans.append(""YES""); ans.append('\n'); int an[]=new int[n]; int q=0; for(int z:h.keySet()) { for(int i=0;i g[]; static long mod=(long)998244353,INF=Long.MAX_VALUE; // static boolean set[]; static int par[],partial[]; static int Days[],P[][]; static int sum=0,size[]; static int seg[],col[]; // static ArrayList A; static char X[][]; static boolean set[][]; static int D[],min[],A[]; static long dp[][]; // static HashSet visited,imposters; // static HashSet set; // 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 { /* * star,rope,TPST * BS,LST,MS,MQ */ int T=i(); outer:while(T-->0) { int N=i(); int f[]=new int[N+5]; int ask=ask(1); HashSet set=new HashSet<>(); int cnt=0; for(int i=1; i<=N; i++) { if(cnt+1==N) { set=new HashSet<>(); for( i=0; i<=N; i++)set.add(i); for( i=1; i<=N; i++)set.remove(f[i]); int a=-1; for(int v:set) { a=v; } for(i=1; i<=N; i++) { if(f[i]==0)f[i]=a; } break; } if(f[i]==0) { int last=0; set=new HashSet<>(); while(true) { int a=ask(i); f[last]=a; if(set.contains(a)) { break; } last=a; set.add(a); } cnt+=set.size(); } } for(int i=1; i<=N; i++) { ans.append(f[i]+"" ""); } System.out.println(""! ""+ans); ans=new StringBuilder(); } out.println(ans); out.close(); } static int ask(int a) { System.out.println(""? ""+a); // out.flush(); return i(); } static boolean f(String A,String B) { char X[]=A.toCharArray(),Y[]=B.toCharArray(); if(X[X.length-1]=='0' && A.length()==B.length()) { return A.equals(B); } else if(X[X.length-1]=='0') { for(int i=0; i ""+x.ch+"" ""+t); return true; } } } if(cnt==0)return true; return false; } static boolean f(Trie root) { if(root==null)return false; // int cnt=0; for(Trie x:root.A) { if(x!=null) { // cnt++; if(!f(x)) { return true; } } } return false; } static void insert(String X,Trie head) { Trie root=head; for(char x:X.toCharArray()) { int a=x-'a'; if(root.A[a]==null)root.A[a]=new Trie(); root=root.A[a]; root.ch=x; } root.ends=true; } static boolean search(String X,Trie head) { Trie root=head; for(char x:X.toCharArray()) { int a=x-'a'; if(root.A[a]==null)return false; root=root.A[a]; } return root.ends; } static boolean starts_with(String X,Trie head) { Trie root=head; for(char x:X.toCharArray()) { int a=x-'a'; if(root.A[a]==null)return false; root=root.A[a]; } return true; } static int lower(ArrayListA,int i) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; if(A.get(m)>i)r=m; else l=m; } return r; } static int upper(ArrayListA,int i) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; if(A.get(m)0)X.append(""0""); return X.toString(); } static long f(int i,int g,int x,int left) { if(x<=1)return left; if(i<0 || g==1)return left; // System.out.println(i+"" ""+g); if(dp[i][g]==0) { int next=min[i]; int new_gcd=(int)GCD(g,x); //System.out.println("" factor--> ""+x+"" gcd--> ""+g+"" new--> ""+new_gcd); long s=0; int c=0; for(int j=next+1; j<=i; j++) { if(A[i]%new_gcd==0) { s+=new_gcd; c--; } } long a=0,b=0; a=f(i,g,x-1,left); b=s+f(next,new_gcd,x-1,left-c); dp[i][g]=Math.max(a, b); } return dp[i][g]; } static boolean f(int A[],int B[],int x) { int c=0; int N=A.length; int l=0,r=x-1; for(int i=0; i=r && B[i]>=l) { r--; l++; c++; } if(c>=x)return true; } return false; } static boolean isPalindrome(int A[]) { int i=0,j=A.length-1; while(i A,int x) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; if(A.get(m)1) { int m=(l+r)/2; if(A[m]<=a)l=m; else r=m; } return A[l]; } static void build(int v,int tl,int tr,int A[]) { if(tl==tr) { seg[v]=A[tl]; return; } int tm=(tl+tr)/2; build(v*2,tl,tm,A); build(v*2+1,tm+1,tr,A); seg[v]=Math.min(seg[v*2], seg[v*2+1]); } static void update(int v,int tl,int tr,int index,int x) { if(tl==tr && tl==index) { seg[v]=x; return; } int tm=(tl+tr)/2; if(index<=tm)update(v*2,tl,tm,index,x); else update(v*2+1,tm+1,tr,index,x); seg[v]=Math.min(seg[v*2], seg[v*2+1]); } static int ask(int v,int tl,int tr,int l,int r) { // System.out.println(v); // if(v>100)return 0; if(l>r)return Integer.MAX_VALUE; if(tl==l && tr==r)return seg[v]; int tm=(tl+tr)/2; int a=ask(v*2,tl,tm,l,Math.min(tm, r)); // System.out.println(""for--> ""+(v)+"" tm--> ""+(tm+1)+"" tr--> ""+tr+"" l--> ""+Math.max(l, tm+1)+"" r--> ""+r); int b=ask(v*2+1,tm+1,tr,Math.max(l, tm+1),r); return Math.min(a, b); } 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 int ask(int a,int b) { System.out.println(""? ""+a+"" ""+b); return i(); } static long and(int i,int j) { System.out.println(""and ""+i+"" ""+j); return l(); } static long or(int i,int j) { System.out.println(""or ""+i+"" ""+j); return l(); } static boolean is_Sorted(int A[]) { int N=A.length; for(int i=1; i<=N; i++)if(A[i-1]!=i)return false; return true; } static boolean f(StringBuilder sb,String Y,String order) { StringBuilder res=new StringBuilder(sb.toString()); HashSet set=new HashSet<>(); for(char ch:order.toCharArray()) { set.add(ch); for(int i=0; i0) { x*=10; x+=a; } return x; } static int count(String X) { HashSet set=new HashSet<>(); for(char x:X.toCharArray())set.add(x-'0'); return set.size(); } // static void build(int v,int tl,int tr,long A[]) // { // if(tl==tr) // { // seg[v]=A[tl]; // } // else // { // int tm=(tl+tr)/2; // build(v*2,tl,tm,A); // build(v*2+1,tm+1,tr,A); // seg[v]=Math.min(seg[v*2], seg[v*2+1]); // } // } static int [] sub(int A[],int B[]) { int N=A.length; int f[]=new int[N]; for(int i=N-1; i>=0; i--) { if(B[i] 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 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(); Days[i]=-1; D[i]=Integer.MAX_VALUE; //D2[i]=INF; } } static long pow(long a,long b) { 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[] G = new ArrayList[(int)(3e5+10)]; static int[] dist = new int[(int)(3e5+10)]; static int[] a = new int[(int)(3e5+10)]; private static int[] nge(int n){ int[] edges = new int[n]; Arrays.fill(edges, -1); Stack st = new Stack<>(); st.push(0); for(int i = 1; i < n; i++){ while(!st.isEmpty() && a[i] >= a[st.peek()]){ edges[st.pop()] = i; } st.push(i); } //debug(""nge"", edges); return edges; } private static int[] nle(int n){ int[] edges = new int[n]; Arrays.fill(edges, -1); Stack st = new Stack<>(); st.push(0); for(int i = 1; i < n; i++){ while(!st.isEmpty() && a[i] <= a[st.peek()]){ edges[st.pop()] = i; } st.push(i); } //debug(""nle"", edges); return edges; } private static int[] pge(int n){ int[] edges = new int[n]; Arrays.fill(edges, -1); Stack st = new Stack<>(); st.push(n-1); for(int i = n-2; i >= 0; i--){ while(!st.isEmpty() && a[i] >= a[st.peek()]){ edges[st.pop()] = i; } st.push(i); } //debug(""pge"", edges); return edges; } private static int[] ple(int n){ int[] edges = new int[n]; Arrays.fill(edges, -1); Stack st = new Stack<>(); st.push(n-1); for(int i = n-2; i >= 0; i--){ while(!st.isEmpty() && a[i] <= a[st.peek()]){ edges[st.pop()] = i; } st.push(i); } //debug(""ple"", edges); return edges; } private static void buildGraph(int[] edges){ for(int i = 0; i < edges.length; i++){ if(edges[i] != -1){ // G[i].add(edges[i]); // G[edges[i]].add(i); G[min(i, edges[i])].add(max(i, edges[i])); } } } private static void bfs(int n){ dist[0] = 0; ArrayDeque q = new ArrayDeque<>(); q.add(0); while(!q.isEmpty()){ int front = q.pollFirst(); if(front == n-1){ break; } for(int adj : G[front]){ if(dist[adj] == (int)(1e9)){ dist[adj] = 1 + dist[front]; q.add(adj); } } } } public void run() { // InputStream is; // is = new FileInputStream(new File(""input.txt"")); sc = new InputReader(System.in); pw = new PrintWriter(System.out); int n = sc.nextInt(); a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); G[i] = new ArrayList(); } buildGraph(nge(n)); buildGraph(nle(n)); buildGraph(ple(n)); buildGraph(pge(n)); Arrays.fill(dist, (int)(1e9)); bfs(n); pw.println(dist[n-1]); // is.close(); pw.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[5]; private int curChar; private int numChars; private 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 String nextLine() { BufferedReader br = new BufferedReader(new InputStreamReader(stream)); String stock = """"; try { stock = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return stock; } 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 long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long 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 double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } 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 String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }","import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.util.Stack; // https://codeforces.com/contest/1407/problem/D public class Discrete_Centrifugal_Jumps { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] h = new int[n]; for (int i = 0; i < n; i++) h[i] = scan.nextInt(); System.out.println(getMinJump(h, n)); } static int getMinJump(int[] h, int n) { int[] rightG = new int[n], leftG = new int[n]; int[] rightS = new int[n], leftS = new int[n]; Arrays.fill(rightG, -1); Arrays.fill(rightS, -1); Arrays.fill(leftG, -1); Arrays.fill(leftS, -1); Stack stack = new Stack<>(); // next greater in the right segment int i = 0; while (i < n) { if (!stack.empty() && h[i] >= h[stack.peek()]) rightG[stack.pop()] = i; else stack.push(i++); } stack = new Stack<>(); // next smaller in the right segment i = 0; while (i < n) { if (!stack.empty() && h[i] <= h[stack.peek()]) rightS[stack.pop()] = i; else stack.push(i++); } stack = new Stack<>(); // next greater in left segment i = n-1; while (i >= 0) { if (!stack.empty() && h[i] >= h[stack.peek()]) leftG[stack.pop()] = i; else stack.push(i--); } stack = new Stack<>(); // next smaller in left segment i = n-1; while (i >= 0) { if (!stack.empty() && h[i] <= h[stack.peek()]) leftS[stack.pop()] = i; else stack.push(i--); } ArrayList[] jump = new ArrayList[n]; for (i = 0; i < n; i++) jump[i] = new ArrayList<>(); for (i = 0; i < n; i++) { // max(h[i+1] ... h[j-1]) < min(h[i], h[j]) if (rightG[i] != -1) jump[i].add(rightG[i]); if (leftG[i] != -1) jump[leftG[i]].add(i); // max(h[i], h[j]) < min(h[i+1] ... h[j]) if (rightS[i] != -1) jump[i].add(rightS[i]); if (leftS[i] != -1) jump[leftS[i]].add(i); } int[] dp = new int[n]; Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; for (int u = 0; u < n; u++) { for (int v: jump[u]) { dp[v] = Math.min(dp[v], dp[u] + 1); } } return dp[n-1]; } } ",0 69b8ffb2,c9159d9c,"import java.util.*; //CODE FORCES public class anshulvmc { public static void sort(int[] a) { ArrayList l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i bfs = new ArrayDeque<>(); // for(Node nn:nodes) { // if(nn.adj.size()<2) { // bfs.addLast(nn); // nn.dist=0; // } // } // // while(bfs.size()>0) { // Node nn = bfs.removeFirst(); // for(Node a : nn.adj) { // if(a.dist!=-1) continue; // a.usedDegree++; // if(a.adj.size() - a.usedDegree <= 1) { // a.dist = nn.dist+1; // bfs.addLast(a); // } // } // } // // int[] cs = new int[n+1]; // for(Node nn:nodes) { // cs[nn.dist]++; // } // for(int i=1;i adj = new ArrayList<>(); int dist = -1; int usedDegree = 0; } public static void cat_mice(int dest,int[] arr) { sort(arr); int time = dest; int timeleft = dest-1; int counter=0; for(int i=arr.length-1;i>=0;i--) { int val = arr[i]; int takes = time - val; if(takes <= timeleft) { timeleft -= takes; counter++; } } System.out.println(counter); } public static void minex(int n,int[] arr) { sort(arr); int ans=arr[0]; for(int i=0;i blue = new ArrayList<>(); ArrayList red = new ArrayList<>(); for(int i=0;i= i+1) { } else { return false; } } for(int i=0;i i+1 + blue.size()) { // System.out.println(red.get(i)+"" ""+(i+1 + blue.size())); return false; } } return true; } public static void main(String args[]) { Scanner scn = new Scanner(System.in); int test = scn.nextInt(); for(int i=0;i blue = new ArrayList<>(); ArrayList red = new ArrayList<>(); for(int j=0;jcounter){ counter++; } else{ ans=""NO""; break; } } if(ans==""NO""){ System.out.println(ans); } else{ int tempCounter = n+1; Collections.sort(red); for(int j=red.size()-1;j>=0;j--){ int current = red.get(j); if(current>=tempCounter){ ans=""NO""; break; } else{ tempCounter--; } } if(tempCounter-counter!=1) System.out.println(""NO""); else System.out.println(ans); } } return; } } ",0 99ccaa44,e7a997b5,"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 = 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 0c9d4def,1a6f8b20,"import java.io.BufferedReader; 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 C{ private static int maxWords = 0; private static int[][] words; private static int n; private static int[] netwrtchar(int index){ ArrayList list = new ArrayList<>(); for(int i=0; i0){ sum += f[index++]; count++; } return count; } public static void main(String[] args){ FS sc = new FS(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ n = sc.nextInt(); words = new int[n][5]; maxWords = 0; for(int i=0; i0) { int n=Integer.parseInt(br.readLine()); int ar[][]=new int [n][5]; int len[]=new int[n]; for(int i=0;i pq=new PriorityQueue(Collections.reverseOrder()); for(int i=0;i0) { int num=(int)(pq.poll()); if((sum+num)>0) { ct++; sum+=num; } } return ct; } } ",0 49b94994,558df7d4,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main1582D { public static void main(String[] args) { final FastScanner in = new FastScanner(System.in); final PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int[] a = new int[n]; for (int j = 0; j < n; j++) { a[j] = in.nextInt(); } int[] b = solution(a, n); for (int j = 0; j < n; j++) { out.print(b[j]); out.print("" ""); } out.println(); } out.flush(); out.close(); in.close(); } private static int[] solution(int[] a, int n) { int[] b = new int[n]; int start = 0; if (n % 2 == 1) { if (a[0] + a[1] != 0) { b[0] = -a[2]; b[1] = -a[2]; b[2] = a[0] + a[1]; } else if (a[0] + a[2] != 0) { b[0] = -a[1]; b[1] = a[0] + a[2]; b[2] = -a[1]; } else { b[0] = a[1] + a[2]; b[1] = -a[0]; b[2] = -a[0]; } start = 3; } else { b[0] = -a[1]; b[1] = a[0]; int gcd = gcd(b[0], b[1]); b[0] /= gcd; b[1] /= gcd; start = 2; } for (int i = start; i < n; i += 2) { b[i] = -a[i + 1]; b[i + 1] = a[i]; } return b; } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream)); } 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()); } double nextDouble() { return Double.parseDouble(next()); } int[] readIntArr(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = Integer.parseInt(next()); } return result; } long[] readLongArr(int n) { long[] result = new long[n]; for (int i = 0; i < n; i++) { result[i] = Long.parseLong(next()); } return result; } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } long nextLong() { return Long.parseLong(next()); } } } ","import java.io.*; import java.util.*; public class Pupsen { public static void main(String[] args) throws Exception { FastIO in = new FastIO(); int t = in.nextInt(); for (int tc=0; tc red = new ArrayList(); ArrayList blue = new ArrayList(); for(int i=0;inumber){ check2=1; break; } number--; } if(check1==0 && check2==0){ res.append(""YES\n""); } else{ res.append(""NO\n""); } p++; } System.out.println(res); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { 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()); } } } ","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 49e94e7e,fcc7e8fa,"import java.io.BufferedReader; import java.io.IOException; import java.lang.*; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.File; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class Main { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return (int)(first - ob.first); } } static class Tuple implements Comparable { int first, second,third; public Tuple(int first, int second, int third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int[] parent; int[] rank; //Size of the trees is used as the rank public DSU(int n) { parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) //finding through path compression { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public boolean union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return false; //if they are already connected we exit by returning false. // if a's parent is less than b's parent if(rank[a] < rank[b]) { //then move a under b parent[a] = b; } //else if rank of j's parent is less than i's parent else if(rank[a] > rank[b]) { //then move b under a parent[b] = a; } //if both have the same rank. else { //move a under b (it doesnt matter if its the other way around. parent[b] = a; rank[a] = 1 + rank[a]; } return true; } } static class Reader { 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) throws IOException { int[] a=new int[n]; for (int i=0; i 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long modPow(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPow(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n SieveList(int n) { boolean prime[] = new boolean[(int)(n+1)]; Arrays.fill(prime, true); List l = new ArrayList<>(); for (int p = 2; p*p<=n; p++) { if (prime[p] == true) { for(int i = p*p; i<=n; i += p) { prime[i] = false; } } } for (int p = 2; p<=n; p++) { if (prime[p] == true) { l.add(p); } } return l; } public static int countDivisors(int x) { int c = 0; for(int i = 1;i*i<=x;i++) { if(x%i == 0) { if(x/i != i) { c+=2; } else { c++; } } } return c; } public static long log2(long n) { long ans = (long)(log(n)/log(2)); return ans; } public static boolean isPow2(long n) { return (n != 0 && ((n & (n-1))) == 0); } public static boolean isSq(int x) { long s = (long)Math.round(Math.sqrt(x)); return s*s==x; } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(long a[], long x) { int l=-1, r=a.length; while(l+1>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void Sort(long[] a) { List l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); // Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i 0) { int n = sc.nextInt(); char[] a = sc.next().toCharArray(), b = sc.next().toCharArray(); int c00 = 0, c01 = 0, c10 = 0, c11 = 0; 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 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 99afdfb3,e152c1ff,"import java.util.Scanner; public class C_Set_or_Decrease{ static Scanner in=new Scanner(System.in); static int testCases,n; static long a[]; static long wantedSum; static void solve(){ if(n==1){ if(a[0]<=wantedSum ){ System.out.println(0); }else{ System.out.println((-wantedSum+a[0]) ); } return; } long sum=0; for(long i: a){ sum+=i; } if(sum<=wantedSum){ System.out.println(0); return; } sort(a,0,n-1); long step=Math.max(sum-wantedSum,0); for(int i=1;iwantedSum?(sum-wantedSum+i)/(i+1):0; step=Math.min(step,t+i); } System.out.println(step); //System.out.println(""sum after iterate: ""+sum); //System.out.println(""ans: ""+ans); } public static void main(String [] amit){ testCases=in.nextInt(); for(int t=0;t=right){ return; } int mid=(left+right)/2; sort(a,left,mid); sort(a,mid+1,right); merge(a,left,right,mid); } } "," import java.util.Scanner; public class C_Set_or_Decrease{ static Scanner in=new Scanner(System.in); static int testCases,n; static long a[]; static long wantedSum; static void solve(){ if(n==1){ if(a[0]<=wantedSum ){ System.out.println(0); }else{ System.out.println((-wantedSum+a[0]) ); } return; } long sum=0; for(long i: a){ sum+=i; } if(sum<=wantedSum){ System.out.println(0); return; } sort(a,0,n-1); long step=Math.max(sum-wantedSum,0); for(int i=1;iwantedSum?(sum-wantedSum+i)/(i+1):0; step=Math.min(step,t+i); } System.out.println(step); //System.out.println(""sum after iterate: ""+sum); //System.out.println(""ans: ""+ans); } public static void main(String [] amit){ testCases=in.nextInt(); for(int t=0;t=right){ return; } int mid=(left+right)/2; sort(a,left,mid); sort(a,mid+1,right); merge(a,left,right,mid); } }",1 5f3a196a,d76e3b9d,"import java.io.*; import java.util.*; public class MonstersAndSpells { public static PrintWriter out; public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(br.readLine()); out=new PrintWriter(System.out); int t=Integer.parseInt(st.nextToken()); while(t-->0) { st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken());//monsters int time[]=new int[n];//time int health[]=new int[n];//health st=new StringTokenizer(br.readLine()); for(int i=0;i{ int x, time; public State(int x, int time) { this.x=x;this.time=time; } public int compareTo(State o) { return x-o.x; } } } ","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 28820c82,8637bb90,"import java.util.*; import java.io.*; import java.math.*; import java.lang.*; public class MinimumGridPath { static int mod = 1000000007; public 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 void main(String[] args) throws Exception { Reader scn = new Reader(); PrintWriter pw = new PrintWriter(System.out); int t = scn.nextInt(); outer : while(t-->0){ int n = scn.nextInt(); long[] arr = new long[n]; for(int i=0; i list = new ArrayList<>(); for (int i=0; i list = new ArrayList<>(); for(int i=0; i= 0 && arr[mid-1] == arr[mid]){ ei = mid-1; }else{ return mid; } }else if(arr[mid] > x){ ei = mid - 1; }else{ si = mid+1; } } return si; } private static int upperBound(int[] arr, int x){ int n = arr.length, si = 0, ei = n - 1; while(si <= ei){ int mid = si + (ei - si)/2; if(arr[mid] == x){ if(mid+1 < n && arr[mid+1] == arr[mid]){ si = mid+1; }else{ return mid + 1; } }else if(arr[mid] > x){ ei = mid - 1; }else{ si = mid+1; } } return si; } private static int upperBound(ArrayList list, int x){ int n = list.size(), si = 0, ei = n - 1; while(si <= ei){ int mid = si + (ei - si)/2; if(list.get(mid) == x){ if(mid+1 < n && list.get(mid+1) == list.get(mid)){ si = mid+1; }else{ return mid + 1; } }else if(list.get(mid) > x){ ei = mid - 1; }else{ si = mid+1; } } return si; } // (x^y)%p in O(logy) private static long power(long x, long y){ long res = 1; x = x % mod; while(y > 0){ if ((y & 1) == 1){ res = (res * x) % mod; } y = y >> 1; x = (x * x) % mod; } return res; } public static boolean nextPermutation(int[] arr) { if(arr == null || arr.length <= 1){ return false; } int last = arr.length-2; while(last >= 0){ if(arr[last] < arr[last+1]){ break; } last--; } if (last < 0){ return false; } if(last >= 0){ int nextGreater = arr.length-1; for(int i=arr.length-1; i>last; i--){ if(arr[i] > arr[last]){ nextGreater = i; break; } } swap(arr, last, nextGreater); } reverse(arr, last+1, arr.length-1); return true; } private static void swap(int[] arr, int i, int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } private static void reverse(int[] arr, int i, int j){ while(i < j){ swap(arr, i++, j--); } } // TC- O(logmax(a,b)) private static int gcd(int a, int b) { if(a == 0){ return b; } return gcd(b%a, a); } private static long gcd(long a, long b) { if(a == 0L){ return b; } return gcd(b%a, a); } private static long lcm(long a, long b) { return a / gcd(a, b) * b; } // TC- O(logmax(a,b)) private static int lcm(int a, int b) { return a / gcd(a, b) * b; } private static long inv(long x){ return power(x, mod - 2); } private static long summation(long n){ return (n * (n + 1L)) >> 1; } }"," import java.util.*; import java.awt.Point; import java.io.*; import java.math.BigInteger; public class Solutions { static int MAX=Integer.MAX_VALUE; static int MIN=Integer.MIN_VALUE; //static ArrayList>list=new ArrayList>(); static FastScanner scr=new FastScanner(); static PrintStream out=new PrintStream(System.out); public static void main(String []args) { int T=scr.nextInt(); t:for(int tt=0;tt=0;i--) { b[index]=a[i]; } return b; } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i 0) { int n = sc.nextInt(); Node head = null; boolean notPossible = false; for (int i = 0; i < n; i++) { int x = sc.nextInt(); if (head == null) { head = new Node(x); } else { if ((head.next != null && x > head.next.value) || (head.prev != null && x < head.prev.value)) { notPossible = true; } else if ((head.next == null || x <= head.next.value) && x > head.value) { if (head.next != null && x == head.next.value) { head = head.next; continue; } Node temp = head.next; Node next = new Node(x); head.next = next; next.prev = head; next.next = temp; if (temp != null) { temp.prev = next; } head = next; } else if ((head.prev == null || x >= head.prev.value) && x < head.value) { if (head.prev != null && x == head.prev.value) { head = head.prev; continue; } Node temp = head.prev; Node prev = new Node(x); head.prev = prev; prev.next = head; prev.prev = temp; if (temp != null) { temp.next = prev; } head = prev; } } } if (notPossible) { out.println(""NO""); } else { out.println(""YES""); } } out.close(); } static class Node { int value; Node prev; Node next; Node(int value) { this.value = value; this.prev = null; this.next = null; } } }","import java.util.Scanner; public class D_724 { @SuppressWarnings(""resource"") public static void main(String[] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); for(int test = 0; test < t; test++){ int n = input.nextInt(); ListNode on = new ListNode(input.nextInt(), null, null); boolean good = true; for(int i = 1; i < n; i++){ int num = input.nextInt(); if(good){ int at = on.data; if(num > at){ if(on.next == null || num < on.next.data){ on.next = new ListNode(num, on, on.next); on = on.next; if(on.next != null){ on.next.prev = on; } }else if(num == on.next.data){ on = on.next; }else{ good = false; } }else if(num < at){ if(on.prev == null || num > on.prev.data){ on.prev = new ListNode(num, on.prev, on); on = on.prev; if(on.prev != null){ on.prev.next = on; } }else if(num == on.prev.data){ on = on.prev; }else{ good = false; } } } } if(good){ System.out.println(""YES""); }else{ System.out.println(""NO""); } } } static class ListNode{ int data; ListNode prev; ListNode next; ListNode(int data, ListNode prev, ListNode next){ this.data = data; this.prev = prev; this.next = next; } } } ",0 eea69e7f,f6ca6fc8,"import java.util.*; public class Solution { public static int minMoves(int[] input) { List people = new ArrayList(); List chairs = new ArrayList(); for (int i = 0; i < input.length; i++) { if (input[i] == 1) { people.add(i); } else { chairs.add(i); } } int[] memo = new int[chairs.size() + 1]; for (int p = 1; ((!people.isEmpty()) && (p <= people.size())); p++) { int prev = memo[p]; memo[p] = memo[p - 1] + Math.abs(people.get(p - 1) - chairs.get(p - 1)); for (int c = p + 1; c <= chairs.size(); c++) { int tmp = memo[c]; memo[c] = Math.min(memo[c - 1], prev + Math.abs(people.get(p - 1) - chairs.get(c - 1))); prev = tmp; } } return memo[memo.length - 1]; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] input = new int[n]; for (int i = 0; i < n; i++) { input[i] = sc.nextInt(); } System.out.println(Solution.minMoves(input)); } }","import java.util.*; import java.io.*; public class D { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n = sc.nextInt(); ArrayList o=new ArrayList(); ArrayList e=new ArrayList(); for(int i=1;i<=n;i++){ int x=sc.nextInt(); if(x==1)o.add(i); else e.add(i); } int dp[][]=new int[o.size()+1][e.size()+1]; for(int i=1;i<=o.size();i++){ dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1)); for(int j=i+1;j<=e.size();j++){ dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1))); } } System.out.println(dp[o.size()][e.size()]); } } ",0 584b0e9e,d9199dfd," import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; 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.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class D_Round_753_Div3 { public static int MOD = 1000000007; static int[][] dp; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // ""output.txt""))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int T = in.nextInt(); for (int z = 0; z < T; z++) { int n = in.nextInt(); int[] data = new int[n]; for (int i = 0; i < n; i++) { data[i] = in.nextInt(); } String line = in.next(); ArrayList blue = new ArrayList<>(); ArrayList red = new ArrayList<>(); for (int i = 0; i < n; i++) { if (line.charAt(i) == 'B') { blue.add(data[i]); } else { red.add(data[i]); } } Collections.sort(blue); Collections.sort(red); int st = 1; boolean ok = true; for (int i : blue) { if (i < st) { ok = false; break; } st++; } if (ok) { for (int i : red) { if (i > st) { ok = false; break; } st++; } } out.println(ok ? ""Yes"" : ""No""); } out.close(); } static int find(int v, int[] u) { if (v == u[v]) { return v; } return u[v] = find(u[v], u); } static int abs(int a) { return a < 0 ? -a : a; } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point { int x; int y; public Point(int start, int end) { this.x = start; this.y = end; } public String toString() { return x + "" "" + y; } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; data[index] %= MOD; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; result %= MOD; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return (val * val); } else { return (val * ((val * a))); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(""input.txt"")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }","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(); } } ",1 29bbcd8b,d4779c71," import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Stack; import java.util.StringTokenizer; public class D { private static final String INPUT_FILE_PATH = """"; void solve() { int n = in.nextInt(); int[] h = new int[n]; for (int i = 0; i < n; i++) h[i] = in.nextInt(); Stack increasing = new Stack(); Stack increasingIndices = new Stack(); Stack decreasing = new Stack(); Stack decreasingIndices = new Stack(); increasing.push(h[0]); increasingIndices.push(0); decreasing.push(h[0]); decreasingIndices.push(0); int[] dp = new int[n]; dp[0] = 0; for (int i = 1; i < n; i++) { dp[i] = dp[i - 1] + 1; while (!increasing.isEmpty() && increasing.peek() > h[i]) { dp[i] = Math.min(dp[i], 1 + dp[increasingIndices.peek()]); increasing.pop(); increasingIndices.pop(); } while (!decreasing.isEmpty() && decreasing.peek() < h[i]) { dp[i] = Math.min(dp[i], 1 + dp[decreasingIndices.peek()]); decreasing.pop(); decreasingIndices.pop(); } if (!increasing.isEmpty()) { dp[i] = Math.min(dp[i], 1 + dp[increasingIndices.peek()]); } if (!decreasing.isEmpty()) { dp[i] = Math.min(dp[i], 1 + dp[decreasingIndices.peek()]); } if (!increasing.isEmpty() && increasing.peek() == h[i]) { increasing.pop(); increasingIndices.pop(); } if (!decreasing.isEmpty() && decreasing.peek() == h[i]) { decreasing.pop(); decreasingIndices.pop(); } increasing.push(h[i]); increasingIndices.push(i); decreasing.push(h[i]); decreasingIndices.push(i); } out.println(dp[n - 1]); } private final InputReader in; private final PrintWriter out; private D(InputReader in, PrintWriter out) { this.in = in; this.out = out; } private static class InputReader { private BufferedReader br; private StringTokenizer st; public InputReader(InputStream inputStream) { this.br = new BufferedReader(new InputStreamReader(inputStream), 32768); this.st = null; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } public static void main(String[] args) throws Exception { InputStream inputStream = INPUT_FILE_PATH.isEmpty() ? System.in : new FileInputStream(new File(INPUT_FILE_PATH)); OutputStream outputStream = System.out; InputReader inputReader = new InputReader(inputStream); PrintWriter printWriter = new PrintWriter(outputStream); new D(inputReader, printWriter).solve(); printWriter.close(); } } ","import java.util.ArrayList; import java.util.Scanner; import java.util.Stack; public class D { static Scanner sc = new Scanner(System.in); static int[] height; static int[] dp; public static void main(String[] args) { int n = sc.nextInt(); height = new int[n]; dp = new int[n]; dp[0] = 0; for (int i = 0; i < n; i++) { height[i] = sc.nextInt(); } Stack rise = new Stack(); Stack fail = new Stack(); rise.push(0); fail.push(0); for (int i = 1; i < n; i++) { dp[i] = dp[i-1]+1; if (rise.isEmpty()) { rise.push(i); } else if (height[rise.peek()] < height[i]) { rise.push(i); } else { while (!rise.isEmpty() && height[rise.peek()] > height[i]) { rise.pop(); if (!rise.isEmpty()) { dp[i] = Math.min(dp[i], dp[rise.peek()] + 1); } } while (!rise.isEmpty() && height[rise.peek()] == height[i]) { rise.pop(); } rise.push(i); } if (fail.isEmpty()) { fail.push(i); } else if (height[fail.peek()] > height[i]) { fail.push(i); } else { while (!fail.isEmpty() && height[fail.peek()] < height[i]) { fail.pop(); if (!fail.isEmpty()){ dp[i] = Math.min(dp[i], dp[fail.peek()] + 1); } } while (!fail.isEmpty() && height[fail.peek()] == height[i]) { fail.pop(); } fail.push(i); } } System.out.println(dp[n - 1]); } } // a[i] // // // 6 7 6 2 //3 5 6 4 5 6 3",0 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; i0) { int n=Integer.parseInt(bu.readLine()); int k[]=new int[n+1],h[]=new int[n+1],i; String s[]=bu.readLine().split("" ""); for(i=1;i<=n;i++) k[i]=Integer.parseInt(s[i-1]); s=bu.readLine().split("" ""); for(i=1;i<=n;i++) h[i]=Integer.parseInt(s[i-1]); long max[]=new long[n+1]; for(i=1;i<=n;i++) { int j; long here=h[i]; for(j=i-1;j>=0;j--) { long diff=here-(k[i]-k[j]); if(diff<=0) {max[i]=max[j]+here*(here+1)/2; break;} if(diff0) { int n=Integer.parseInt(bu.readLine()); int k[]=new int[n+1],h[]=new int[n+1],i; String s[]=bu.readLine().split("" ""); for(i=1;i<=n;i++) k[i]=Integer.parseInt(s[i-1]); s=bu.readLine().split("" ""); for(i=1;i<=n;i++) h[i]=Integer.parseInt(s[i-1]); long max[]=new long[n+1]; for(i=1;i<=n;i++) { int j; long here=h[i]; for(j=i-1;j>=0;j--) { long diff=here-(k[i]-k[j]); if(diff<=0) {max[i]=max[j]+here*(here+1)/2; break;} if(diff Arrays.fill(a, 0)); /* List l1 = new ArrayList(); */ // Random rand = new Random(); int tst = sc.nextInt(); while(tst-->0) { int n=sc.nextInt(); int app[]=new int[n]; int h[]=new int[n]; for(int i=0;i=0; i--) { if(app[i]>=last) { last = Math.min(last,app[i] - h[i] + 1); } else { long s = app[end]-last+1; man += (s*(s+1))/2; end = i; last = app[i] - h[i] + 1;; } } long s = app[end]-last+1; man += (s*(s+1))/2; pw.println(man); } pw.flush(); } public static long eculidean_gcd(long a, long b) { if (a == 0) { x = 0; y = 1; return b; } long ans = eculidean_gcd(b % a, a); long x1 = x; x = y - (b / a) * x; y = x1; return ans; } public static int sum(int n) { int sum = 1; if (n == 0) return 0; while (n != 0) { sum = sum * n; n = n - 1; } return sum; } public static boolean isLsbOne(int n) { if ((n & 1) != 0) return true; return false; } public static pair helper(int arr[], int start, int end, int k, pair dp[][]) { if (start >= end) { if (start == end) return (new pair(arr[start], 0)); else return (new pair(0, 0)); } if (dp[start][end].x != -1 && dp[start][end].y != -1) { return dp[start][end]; } pair ans = new pair(0, Integer.MAX_VALUE); for (int i = start; i < end; i++) { pair x1 = helper(arr, start, i, k, dp); pair x2 = helper(arr, i + 1, end, k, dp); long tip = k * (x1.x + x2.x) + x1.y + x2.y; if (tip < ans.y) ans = new pair(x1.x + x2.x, tip); } return dp[start][end] = ans; } public static void debugger() { Random rand = new Random(); int tst = (int) (Math.abs(rand.nextInt()) % 2 + 1); pw.println(tst); while (tst-- > 0) { int n = (int) (Math.abs(rand.nextInt()) % 5 + 1); pw.println(n); for (int i = 0; i < n; i++) { pw.print((int) (Math.abs(rand.nextInt()) % 6 + 1) + "" ""); } pw.println(); } } static int UpperBound(long a[], long x) {// x is the key or target value int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } static int LowerBound(long a[], long x) { // x is the target value or key int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } static void recursion(int n) { if (n == 1) { pw.print(n + "" ""); return; } // pw.print(n+"" ""); gives us n to 1 recursion(n - 1); // pw.print(n+"" ""); gives us 1 to n } // ch.charAt(i)+"""" converts into a char sequence 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; } /* CREATED BY ME */ 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; } } public static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static boolean isPrime(long n) { if (n == 2) return true; long i = 2; while (i * i <= n) { if (n % i == 0) return false; i++; } return true; } 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 double max(double x, double 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 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 class pair { long x; long y; public pair(long a, long b) { x = a; y = b; } } public static class Comp implements Comparator { public int compare(pair a, pair b) { long ans = a.x - b.x; if (ans > 0) return 1; if (ans < 0) return -1; return 0; } } // modular exponentiation public static long fastExpo(long a, int n, int mod) { if (n == 0) return 1; else { if ((n & 1) == 1) { long x = fastExpo(a, n / 2, mod); return (((a * x) % mod) * x) % mod; } else { long x = fastExpo(a, n / 2, mod); return (((x % mod) * (x % mod)) % mod) % mod; } } } public static long modInverse(long n, int p) { return fastExpo(n, p - 2, p); } /* * public static void extract(ArrayList ar, int k, int d) { int c = 0; * for (int i = 1; i < k; i++) { int x = 0; boolean dm = false; while (x > 0) { * long dig = x % 10; x = x / 10; if (dig == d) { dm = true; break; } } if (dm) * ar.add(i); } } */ public static int[] prefixfuntion(String s) { int n = s.length(); int z[] = new int[n]; for (int i = 1; i < n; i++) { int j = z[i - 1]; while (j > 0 && s.charAt(i) != s.charAt(j)) j = z[j - 1]; if (s.charAt(i) == s.charAt(j)) j++; z[i] = j; } return z; } // counts the set(1) bit of a number public static long countSetBitsUtil(long x) { if (x <= 0) return 0; return (x % 2 == 0 ? 0 : 1) + countSetBitsUtil(x / 2); } //tells whether a particular index has which bit of a number public static int getIthBitsUtil(int x, int y) { return (x & (1 << y)) != 0 ? 1 : 0; } public static void swaper(long x, long y) { x = x ^ y; y = y ^ x; x = x ^ y; } public static double decimalPlaces(double sum) { DecimalFormat df = new DecimalFormat(""#.00""); String angleFormated = df.format(sum); double fin = Double.parseDouble(angleFormated); return fin; } //use collections.swap for swapping static boolean isSubSequence(String str1, String str2, int m, int n) { int j = 0; for (int i = 0; i < n && j < m; i++) if (str1.charAt(j) == str2.charAt(i)) j++; return (j == m); } static long sum(long n) { long s2 = 0, max = -1, min = 10; while (n > 0) { s2 = (n % 10); min = min(s2, min); max = max(s2, max); n = n / 10; } return max * min; } static long pow(long base, long power) { if (power == 0) { return 1; } long result = pow(base, power / 2); if (power % 2 == 1) { return result * result * base; } return result * result; } // return the hash value of a string static long compute_hash(String s) { long val = 0; long p = 31; long mod = (long) (1000000007); long pow = 1; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); val = (val + (int) (ch - 'a' + 1) * pow) % mod; pow = (pow * p) % mod; } return val; } } ","import java.util.*; import java.io.*; public class q3{ static FastScanner fs = new FastScanner(); static PrintWriter pw = new PrintWriter(System.out); static int[] sec; static int[] health; static int[] pp; public static void main(String[] args){ int T = fs.nextInt(); for (int tt=0;tt=0;i--){ int left = sec[i] - health[i] + 1, right = sec[i], top = health[i]; if (right < pp[0]){ sum += ((long)pp[2] + 1) * (long)pp[2] / 2; pp = new int[]{left, right, top}; } else if (left < pp[0]){ pp[0] = left; pp[2] = top + (pp[1] - right); } } sum += ((long)pp[2] + 1) * (long)pp[2] / 2; pw.println(sum); } pw.close(); } // ----------input function---------- 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 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 11c2ab99,bdfe8110,"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; 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; i0) { int n = in.nextInt(); int k = in.nextInt(); int[] a= in.readArray(k); int[] temp = in.readArray(k); int[] pre = new int[n]; Arrays.fill(pre,Integer.MAX_VALUE); int[] suf = new int[n]; Arrays.fill(suf,Integer.MAX_VALUE); for(int i = 0;i=0;i--){ min = Math.min(min,suf[i]); suf[i] = min; min++; } for(int i=0;i 0){ if(b%2 == 1) x=(x*y)%c; y = (y*y)%c; b = b>>1; } return x%c; } public static void debug(Object... o){ System.err.println(Arrays.deepToString(o)); } static int upper_bound(int[] arr,int n,int x){ int mid; int low=0; int high=n; while(low=arr[mid]) low=mid+1; else high=mid; } return low; } static int lower_bound(int[] arr,int n,int x){ int mid; int low=0; int high=n; while(low 0) { int n = sc.nextInt(); int k = sc.nextInt(); int[] arr = new int[k]; int[] t = new int[k]; HashMap hm = new HashMap<>(); for (int i = 0; i < k; i++) { arr[i] = sc.nextInt() - 1; } for (int i = 0; i < k; i++) { t[i] = sc.nextInt(); hm.put(arr[i], t[i]); } int[] left = new int[n]; int[] right = new int[n]; left[0] = hm.getOrDefault(0, -1); right[n - 1] = hm.getOrDefault(n - 1, -1); for (int i = 1; i < n; i++) { if (hm.containsKey(i)) { if (left[i - 1] < 0) { left[i] = hm.get(i); } else { left[i] = Math.min(hm.get(i), left[i - 1] + 1); } } else { left[i] = left[i - 1] < 0 ? -1 : left[i - 1] + 1; } } for (int i = n - 2; i >= 0; i--) { if (hm.containsKey(i)) { if (right[i + 1] < 0) { right[i] = hm.get(i); } else { right[i] = Math.min(hm.get(i), right[i + 1] + 1); } } else { right[i] = right[i + 1] < 0 ? -1 : right[i + 1] + 1; } } for (int i = 0; i < n; i++) { if (left[i] < 0) { pw.print(right[i] + "" ""); } else if (right[i] < 0) { pw.print(left[i] + "" ""); } else { pw.print(Math.min(left[i], right[i]) + "" ""); } } pw.println(); } pw.flush(); pw.close(); } // inclusive private static void rotate(int[] arr, int l, int r) { int temp = arr[r]; for (int i = r - 1; i >= l; i--) { arr[i + 1] = arr[i]; } arr[l] = temp; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream System) throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System)); } 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 boolean ready() throws IOException { return br.ready(); } public void waitForInput() { for (long i = 0; i < 3e9; i++) ; } } static class Pair { int a; int b; boolean asc; Pair(int a, int b, boolean asc) { this.a = a; this.b = b; this.asc = asc; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return a == pair.a && b == pair.b && asc == pair.asc; } @Override public int hashCode() { return Objects.hash(a, b, asc); } } } ",0 54488276,f4757480,"import java.io.*; import java.util.*; public class C { static int n; public static void main (String[] args) throws IOException { Kattio io = new Kattio(); int t = io.nextInt(); for (int ii=0; ii total) { ret++; } else { return ret; } } return ret; } static class Comp implements Comparator { char c; public Comp (char c) { this.c = c; } public int compare(String a, String b) { double cnt1 = 0; double cnt2 = 0; for (int i=0; i0){ 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; l0;t--) solve(); out.print(sb); } long MOD= 1000000007; void solve(){ int n= in.nextInt(); int k= in.nextInt(); long[] a=longArr(k); long[] t=longArr(k); long[] ans= new long[n]; for(int i=0;i=0;i--) { right[i]= min(right[i+1]+1,ans[i]); } for(int i=0;ikey) return i; while (i arr, long n) { arr.sort(new Comparator() { @Override public int compare(Pair p1, Pair p2) { return (int) (p1.first - p2.first); } }); } } 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 (Exception 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 (Exception e){ e.printStackTrace(); } return str; } } }","import java.io.*; import java.util.*; import java.lang.*; public class codeforces { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; if (System.getProperty(""ONLINE_JUDGE"") == null) { long startTime = System.currentTimeMillis(); try { sc = new InputReader(new FileInputStream(""input.txt"")); out = new PrintWriter(new FileOutputStream(""output.txt"")); pr = new PrintWriter(new FileOutputStream(""error.txt"")); } catch (Exception ignored) { } int t = 1; int tt = t; t = sc.nextInt(); while (t-- > 0) { solve(); } long endTime = System.currentTimeMillis(); System.out.println(""Time: "" + (endTime - startTime) / tt + "" ms""); out.flush(); pr.flush(); } else { sc = new InputReader(inputStream); out = new PrintWriter(outputStream); pr = new PrintWriter(outputStream); int t = 1; t = sc.nextInt(); while (t-- > 0) { solve(); } out.flush(); } } public static void solve() { n = sc.nextInt(); for (int i = 0; i < n; i++) { dp[i] = inf; ans[i] = inf; } m = sc.nextInt(); for (int i = 0; i < m; i++) arr[i] = sc.nextInt() - 1; for (int i = 0; i < m; i++) { arr2[i] = sc.nextInt(); dp[(int)arr[i]] = arr2[i]; } temp = inf; for (int i = 0; i < n; i++) { temp = Math.min(temp, dp[i]); ans[i] = Math.min(ans[i], temp); temp++; } temp = inf; for (int i = (int)n - 1; i > -1; i--) { temp = Math.min(temp, dp[i]); ans[i] = Math.min(ans[i], temp); temp++; } for (int i = 0; i < n; i++) out.print(ans[i] + "" ""); out.println(""""); } /* * Set Iterator Iterator value = set.iterator(); Displaying the values after * iterating through the iterator * System.out.println(""The iterator values are: ""); while (value.hasNext()) { * System.out.println(value.next()); } */ /* * Map Iterator: for (Map.Entry entry : map.entrySet()){ * System.out.println(""Key => "" + entry.getKey() + "", Value => "" + * entry.getValue());} */ // Globals public static long n, m, temp; public static int template_array_size = (int) 1e6 + 16813; public static long[] arr = new long[template_array_size]; public static long[] arr2 = new long[template_array_size]; public static long[] dp = new long[template_array_size]; public static long[] ans = new long[template_array_size]; public static int inf = Integer.MAX_VALUE; public static int minf = Integer.MIN_VALUE; public static int mod = 1000000007; public static int ml = (int) 1e9; public static String s = """"; public static InputReader sc; public static PrintWriter out; public static PrintWriter pr; // Pair static class Pair { int first, second; Pair(int x, int y) { this.first = x; this.second = y; } } // FastReader Class 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()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } // Req Functions public static void fill(int[][] dp, int x) { for (int i = 0; i < dp.length; ++i) { for (int j = 0; j < dp[0].length; ++j) { dp[i][j] = x; } } } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } public static long nCr(int n, int k) { long ans = 1L; k = k > n - k ? n - k : k; int j = 1; for (; j <= k; j++, n--) { if (n % j == 0) { ans *= n / j; } else if (ans % j == 0) { ans = ans / j * n; } else { ans = (ans * n) / j; } } return ans; } public static String reverseString(String input) { StringBuilder str = new StringBuilder(""""); for (int i = input.length() - 1; i >= 0; i--) { str.append(input.charAt(i)); } return str.toString(); } public static int maxOf3(int x, int y, int z) { return Math.max(x, Math.max(y, z)); } public static int minof3(int x, int y, int z) { return Math.min(x, Math.min(y, z)); } public static long maxOf3(long x, long y, long z) { return Math.max(x, Math.max(y, z)); } public static long minof3(long x, long y, long z) { return Math.min(x, Math.min(y, z)); } public static void arrInput(int[] arr, int n) { for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); } public static void arrInput(long[] arr, int n) { for (int i = 0; i < n; i++) arr[i] = sc.nextLong(); } public static void arrInput(Pair[] pair, int n) { for (int i = 0; i < n; i++) pair[i] = new Pair(sc.nextInt(), sc.nextInt()); } public static int maxarrInput(int[] arr, int n) { int max = minf; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); max = Math.max(max, arr[i]); } return max; } public static long maxarrInput(long[] arr, int n) { long max = minf; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); max = Math.max(max, arr[i]); } return max; } public static int minarrInput(int[] arr, int n) { int min = inf; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); min = Math.max(min, arr[i]); } return min; } public static long minarrInput(long[] arr, int n) { long min = inf; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); min = Math.max(min, arr[i]); } return min; } public static int lowerBound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] >= x) r = m; else l = m; } return r; } public static int upperBound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] <= x) l = m; else r = m; } return l + 1; } public static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int[n1]; int R[] = new int[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++; } } public static void reversesort(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; reversesort(arr, l, m); reversesort(arr, m + 1, r); merge(arr, l, m, r); } } public 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++; } } public static void reversesort(long arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; reversesort(arr, l, m); reversesort(arr, m + 1, r); merge(arr, l, m, r); } } // debug public static boolean sysFlag = System.getProperty(""ONLINE_JUDGE"") == null; public static void debug(int[][] dp) { if (sysFlag) { for (int i = 0; i < dp.length; ++i) { pr.print(i + ""--> ""); for (int j = 0; j < dp[0].length; ++j) { pr.print(dp[i][j] + "" ""); } pr.println(""""); } } } public static void debug(long[][] dp) { if (sysFlag) { for (int i = 0; i < dp.length; ++i) { pr.print(i + ""--> ""); for (int j = 0; j < dp[0].length; ++j) { pr.print(dp[i][j] + "" ""); } pr.println(""""); } } } public static void debug(int x) { if (sysFlag) pr.println(""Int-Ele: "" + x); } public static void debug(String x) { if (sysFlag) pr.println(""String: "" + x); } public static void debug(char x) { if (sysFlag) pr.println(""Char: "" + x); } public static void debug(long x) { if (sysFlag) pr.println(""Long-Ele: "" + x); } public static void debug(int[] arr, int n) { if (sysFlag) { for (int i = 0; i < n; ++i) { pr.println(i + "" -> "" + arr[i]); } } } public static void debug(char[] arr) { if (sysFlag) { for (int i = 0; i < arr.length; ++i) { pr.println(i + "" -> "" + arr[i]); } } } public static void debug(long[] arr, int n) { if (sysFlag) { for (int i = 0; i < n; ++i) { pr.println(i + "" -> "" + arr[i]); } } } public static void debug(ArrayList list) { if (sysFlag) { for (int i = 0; i < list.size(); ++i) { pr.println(i + "" -> "" + list.get(i)); } } } public static void Ldebug(ArrayList list) { if (sysFlag) { for (int i = 0; i < list.size(); ++i) { pr.println(i + "" -> "" + list.get(i)); } } } public static void debugmapII(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapLI(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapLL(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapIL(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapSL(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapCL(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapSI(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debugmapCI(HashMap map) { if (sysFlag) { for (Map.Entry entry : map.entrySet()) pr.println(""Key => "" + entry.getKey() + "", Value => "" + entry.getValue()); } } public static void debug(Set set) { if (sysFlag) { Iterator value = set.iterator(); int i = 1; while (value.hasNext()) { pr.println((i++) + ""-> "" + value.next()); } } } } ",0 089b7f00,fb20d298,"import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { 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;iset=new TreeSet<>(); set.add(a[0]); boolean flag=false; 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(); 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 (int j = 0; j < vertices-1; j++) { String st[] = bu.readLine().split("" ""); int u = Integer.parseInt(st[0]) - 1, v = Integer.parseInt(st[1]) - 1; connections[u].add(v); connections[v].add(u); } dfs(0, -1); out.append(Math.max(s[0][0], s[0][1]) + ""\n""); } System.out.print(out); } static int N = 100000; static int[][] a = new int[N][2]; static long[][] s = new long[N][2]; static ArrayList[] connections; public static void dfs(int n, int parent) { for (int child : connections[n]) { if (child != parent) { dfs(child, n); s[n][0] += Math.max(s[child][0] + Math.abs(a[n][0] - a[child][0]), s[child][1] + Math.abs(a[n][0] - a[child][1])); s[n][1] += Math.max(s[child][0] + Math.abs(a[n][1] - a[child][0]), s[child][1] + Math.abs(a[n][1] - a[child][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])); } } } ",1 45561f1f,cc9230d3,"/** * Created by Himanshu **/ import java.util.*; import java.io.*; import java.math.*; public class C1529 { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); Reader s = new Reader(); int t = s.i(); while (t-- > 0) { int n = s.i(); pairLong [] arr = new pairLong[n]; for (int i=0;i[] tree = new ArrayList[n+1]; for (int i=0;i<=n;i++) tree[i] = new ArrayList<>(); for (int i=0;i[] tree , pairLong [] arr , pairLong [] dp , int in , boolean [] vis) { if (dp[in] != null) return dp[in]; vis[in] = true; long a = 0L , b = 0L; for (int x : tree[in]) { if (!vis[x]) { pairLong y = value(tree,arr,dp,x,vis); a += Math.max(Math.abs(arr[in-1].first-arr[x-1].first) + y.first,Math.abs(arr[in-1].first-arr[x-1].second) + y.second); b += Math.max(Math.abs(arr[in-1].second-arr[x-1].first) + y.first,Math.abs(arr[in-1].second-arr[x-1].second) + y.second); } } dp[in] = new pairLong(a,b); return dp[in]; } private static void bfs(ArrayList[] tree , ArrayList even , ArrayList odd , int n) { Queue q = new LinkedList<>(); q.add(1); int in = 0; boolean [] vis = new boolean[n+1]; vis[1] = true; while (!q.isEmpty()) { Integer a = q.poll(); if (a == null) { q.add(null); in++; } else { if (in%2 == 0) even.add(a); else odd.add(a); for (int x : tree[a]) { if (!vis[x]) { q.add(x); vis[x] = true; } } } if (q.size() == 0) break; } } public static void shuffle(long[] arr) { int n = arr.length; Random rand = new Random(); for (int i = 0; i < n; i++) { long temp = arr[i]; int randomPos = i + rand.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = temp; } } private static int gcd(int a, int b) { if(b == 0) return a; return gcd(b,a%b); } public static long nCr(long[] fact, long[] inv, int n, int r, long mod) { if (n < r) return 0; return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod; } private static void factorials(long[] fact, long[] inv, long mod, int n) { fact[0] = 1; inv[0] = 1; for (int i = 1; i <= n; ++i) { fact[i] = (fact[i - 1] * i) % mod; inv[i] = power(fact[i], mod - 2, mod); } } private static long power(long a, long n, long p) { long result = 1; while (n > 0) { if (n % 2 == 0) { a = (a * a) % p; n /= 2; } else { result = (result * a) % p; n--; } } return result; } private static long power(long a, long n) { long result = 1; while (n > 0) { if (n % 2 == 0) { a = (a * a); n /= 2; } else { result = (result * a); n--; } } return result; } private static long query(long[] tree, int in, int start, int end, int l, int r) { if (start >= l && r >= end) return tree[in]; if (end < l || start > r) return 0; int mid = (start + end) / 2; long x = query(tree, 2 * in, start, mid, l, r); long y = query(tree, 2 * in + 1, mid + 1, end, l, r); return x + y; } private static void update(int[] arr, long[] tree, int in, int start, int end, int idx, int val) { if (start == end) { tree[in] = val; arr[idx] = val; return; } int mid = (start + end) / 2; if (idx > mid) update(arr, tree, 2 * in + 1, mid + 1, end, idx, val); else update(arr, tree, 2 * in, start, mid, idx, val); tree[in] = tree[2 * in] + tree[2 * in + 1]; } private static void build(int[] arr, long[] tree, int in, int start, int end) { if (start == end) { tree[in] = arr[start]; return; } int mid = (start + end) / 2; build(arr, tree, 2 * in, start, mid); build(arr, tree, 2 * in + 1, mid + 1, end); tree[in] = (tree[2 * in + 1] + tree[2 * in]); } static class Reader { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar, numChars; public Reader() { this(System.in); } public Reader(InputStream is) { mIs = is; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = mIs.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String s() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long l() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long 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 int i() { 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 double d() throws IOException { return Double.parseDouble(s()); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public int[] arr(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = i(); } return ret; } public long[] arrLong(int n) { long[] ret = new long[n]; for (int i = 0; i < n; i++) { ret[i] = l(); } return ret; } } static class pairLong implements Comparator { long first, second; pairLong() { } pairLong(long first, long second) { this.first = first; this.second = second; } @Override public int compare(pairLong p1, pairLong p2) { if (p1.first == p2.first) { if(p1.second > p2.second) return 1; else return -1; } if(p1.first > p2.first) return 1; else return -1; } } // static class pair implements Comparator { // int first, second; // // pair() { // } // // pair(int first, int second) { // this.first = first; // this.second = second; // } // // @Override // public int compare(pair p1, pair p2) { // if (p1.first == p2.first) return p1.second - p2.second; // return p1.first - p2.first; // } // } } ","import java.io.BufferedReader; import java.util.StringTokenizer; import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; public class C { static int[][] lr = new int[2][(int)2e5+10]; static long[][] dp = new long[2][(int)2e5+10]; static ArrayList> g; public static void main(String[] args) { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out, true); int cases = fr.nextInt(); for(int c = 0; c < cases; c++) { int nodes = fr.nextInt(); g = new ArrayList>(); for(int i = 1; i <= nodes; i++) { lr[0][i] = fr.nextInt(); lr[1][i] = fr.nextInt(); } for(int i = 0; i <= nodes; i++) { g.add(new ArrayList()); } for(int i = 0; i < nodes-1; i++) { int f = fr.nextInt(); int t = fr.nextInt(); g.get(f).add(t); g.get(t).add(f); } DFS(1, -1); out.write(Math.max(dp[0][1], dp[1][1]) + ""\n""); } out.close(); } static void DFS(int v, int p) { dp[0][v] = dp[1][v] = 0; for(Integer u : g.get(v)) { if (u == p) continue; DFS(u, v); dp[0][v] += Math.max(Math.abs(lr[0][v] - lr[1][u]) + dp[1][u], dp[0][u] + Math.abs(lr[0][v] - lr[0][u])); dp[1][v] += Math.max(Math.abs(lr[1][v] - lr[1][u]) + dp[1][u], dp[0][u] + Math.abs(lr[1][v] - lr[0][u])); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { this.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 1dab88fb,4138b081,"import java.util.*; public class Main { static class Edge{ public int node; public int index; public Edge(int n, int i){ node=n; index=i; } } static Scanner sc=new Scanner(System.in); public static void main(String[] args) { int test=sc.nextInt(); while(test-->0){ 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.io.*; import java.util.*; public class Contest1627C { static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { // reads in the next string while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { // reads in the next int return Integer.parseInt(next()); } public long nextLong() { // reads in the next long return Long.parseLong(next()); } public double nextDouble() { // reads in the next double return Double.parseDouble(next()); } } static InputReader r = new InputReader(System.in); static PrintWriter pw = new PrintWriter(System.out); static long mod = 1000000007; static ArrayList[] adj; static ArrayList[] num; static int[] ans; public static void main(String[] args) { int t = r.nextInt(); while (t > 0) { t--; int n = r.nextInt(); adj = new ArrayList[n]; num = new ArrayList[n]; for (int i = 0; i < n; i ++) { adj[i] = new ArrayList(); num[i] = new ArrayList(); } int[] deg = new int[n]; boolean flag = false; for (int i = 0; i < n - 1; i ++) { int a = r.nextInt()-1; int b = r.nextInt()-1; adj[a].add(b); adj[b].add(a); num[a].add(i); num[b].add(i); deg[a] ++; deg[b] ++; if (deg[a] > 2 || deg[b] > 2) { flag = true; } } if (flag) { pw.println(-1); continue; } ans = new int[n]; for (int i = 0; i < n; i ++) { if (deg[i] == 1) { dfs(i,3,-1); } } for (int i = 0; i < n - 1; i ++) { pw.println(ans[i]); } } pw.close(); } static void dfs(int node, int x, int p) { for (int j = 0; j < adj[node].size(); j ++) { int i = adj[node].get(j); if (i == p) { continue; } ans[num[node].get(j)] = x; dfs(i,5-x,node); } } }",0 595f5d6c,6653a758,"import java.math.BigInteger; //import static java.lang.Math.max; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; 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; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.Vector; import java.util.Scanner; public class ahh { //trihund static Scanner scn = new Scanner(System.in); static boolean vis[][]; 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 FastReader s = new FastReader(); static int MOD = 1000000007; public static void main(String[] args) { int n=scn.nextInt(),count=0; int arr[]=new int[n]; for(int i=0;izer=new ArrayList(),one=new ArrayList(); for(int i=0;ione,ArrayListzer,int i,int j,int count,long memo[][]) { if(count==0) return 0; if(i==one.size()||j==zer.size()) return Integer.MAX_VALUE; if(memo[i][j]!=-1) return memo[i][j]; long a=Integer.MAX_VALUE,b=Integer.MAX_VALUE; a=arm(one, zer, i+1, j+1,count-1,memo)+Math.abs(one.get(i)-zer.get(j)); b=arm(one, zer, i, j+1,count,memo); memo[i][j]=Math.min(a, b); return Math.min(a, b); } public static void fac(int n) { BigInteger b = new BigInteger(""1""); for (int i = 1; i <= n; i++) { b = b.multiply(BigInteger.valueOf(i)); } System.out.println(b); } static void ruffleSort(long[] a) { 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); } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } } class pm { int ini, fin; pm(int a, int b) { this.ini = a; this.fin = b; } } class surt implements Comparator { @Override public int compare(pm o1, pm o2) { // TODO Auto-generated method stub int a = o1.ini - o2.ini, b = o1.fin - o2.fin; if (a < 0) return -1; if (a == 0) { if (b < 0) return -1; else return 1; } else return 1; } } class pair { int x, y; pair(int a, int b) { this.x = a; this.y = b; } public int hashCode() { return x * 31 + y * 31; } public boolean equals(Object other) { if (this == other) return true; if (other instanceof pair) { pair pt = (pair) other; return pt.x == this.x && pt.y == this.y; } else return false; } } class sort implements Comparator { @Override public int compare(pair o1, pair o2) { // TODO Auto-generated method stub long a = o1.x - o2.x, b = o1.y - o2.y; if (b < 0) return -1; else if (a == 0) { if (a < 0) return -1; else return 1; } else return 1; } }","import java.util.*; public class D { public static void main(String[] args) { Scanner sc=new Scanner(System.in); ArrayList o=new ArrayList(), e=new ArrayList(); int n = sc.nextInt(),dp[][]=new int[n+1][n+1]; for(int i=1;i<=n;i++){ int x=sc.nextInt(); if(x==1)o.add(i); else e.add(i); } for(int i=1;i<=o.size();i++){ dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1)); for(int j=i+1;j<=e.size();j++) dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1))); } System.out.println(dp[o.size()][e.size()]); } }",0 0fd5b95a,41e72d4f,"//package codeforces; import java.io.PrintWriter; import java.util.*; public class codeforces { static int dp[][]=new int[5001][5001]; public static void main(String[] args) { Scanner s=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int t=1; for(int tt=0;tt z=new ArrayList<>(); ArrayList o=new ArrayList<>(); for(int i=0;i l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i { if (x[c] != y[c]) return(int)( x[c] - y[c]); return (int)-(x[1]+x[2] - y[1]-y[2]); }); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static int sol(int i,int j,ArrayList z,ArrayList o) { if(j==o.size()) { return 0; } int h=z.size()-i; int l=o.size()-j; if(i==z.size()) { return 10000000; } if(dp[i][j]!=-1) { //System.out.println(i+"" ""+j); return dp[i][j]; } int ans1=sol(i+1,j,z,o); int ans2=sol(i+1,j+1,z,o)+Math.abs(z.get(i)-o.get(j)); dp[i][j]=Math.min(ans1, ans2); return dp[i][j]; } } ","import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.StringTokenizer; public class ProblemD { public static void main(String[] args) throws IOException { final int INF = 20000000; InputStream in = System.in; InputReader scan = new InputReader(in); int n = scan.nextInt(); int occ = 0; List occPos = new ArrayList<>(); HashSet occPosSet = new HashSet<>(); for(int i=1;i<=n;i++) { int num = scan.nextInt(); if(num==1) { occ++; occPos.add(i); occPosSet.add(i); } } int[][] dp = new int[n+1][occ+1]; for(int i=0;i<=n;i++) { for(int j=0;j<=occ;j++) { dp[i][j] = 20000000; } } for(int i=1;i<=n;i++) { int k=1; for(int pos: occPos) { if(occPosSet.contains(i)) { dp[i][k] = dp[i-1][k]; } else { dp[i][k] = Math.min(dp[i-1][k], dp[i-1][k-1]+Math.abs(pos-i)); if(k==1) dp[i][k]=Math.min(dp[i][k],Math.abs(pos-i)); } k++; } } if(dp[n][occ]==INF) { System.out.println(0); } else { System.out.println(dp[n][occ]); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException ioe) { ioe.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } } ",0 9069684f,bf85ab7b,"import java.io.*; import java.util.*; public class Test { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static boolean[] vis; static ArrayList[] adj; static int[] l, r; static long[][] dp; @SuppressWarnings(""unchecked"") public static void main(String[] args) throws IOException { int T = readInt(); for (int t = 0; t < T; t++) { int n = readInt(); l = new int[n + 1]; r = new int[n + 1]; for (int i = 1; i <= n; i++) { l[i] = readInt(); r[i] = readInt(); } adj = new ArrayList[n + 1]; for (int i = 1; i <= n; i++) adj[i] = new ArrayList<>(); for (int i = 0; i < n - 1; i++) { int u = readInt(), v = readInt(); adj[u].add(v); adj[v].add(u); } dp = new long[n + 1][2]; vis = new boolean[n + 1]; vis[1] = true; dfs(1); System.out.println(Math.max(dp[1][0], dp[1][1])); } } static void dfs(int u) { for (int x : adj[u]) { if (!vis[x]) { vis[x] = true; dfs(x); dp[u][0] += Math.max(dp[x][0] + Math.abs(l[u] - l[x]), dp[x][1] + Math.abs(l[u] - r[x])); dp[u][1] += Math.max(dp[x][0] + Math.abs(r[u] - l[x]), dp[x][1] + Math.abs(r[u] - r[x])); } } } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static int readInt() throws IOException { return Integer.parseInt(next()); } } "," import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; public class ParsasHumongousTree { public static void main(String args[]) throws IOException { Reader scan = new Reader(); StringBuilder sb = new StringBuilder(); int t = scan.nextInt(); while (t-- > 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(); } } ",0 169e34bf,c9159d9c,"import java.util.*; public class D{ static Scanner sc; public static void solve(){ int n=sc.nextInt(); Integer a[]=new Integer[n]; int flag; 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.*; public class SolutionB { public static long gcd(long a, long b){ if(b==0){ return a; } return gcd(b, a%b); } public static long gcdSum(long b){ long a = 0; long temp = b; while(temp!=0){ a = a + temp%10; temp = temp/10; } return gcd(a,b); } public static class Pair{ Long a; Long b; public Pair(Long a, Long b) { this.a = a; this.b = b; } } public static long factorial (long n){ if(n==0) return 1; else if(n==1) return n; return n * factorial(n-1); } public static long lcm (long n){ if(n<3) return n; return lcmForBig(n,n-1); } private static long lcmForBig(long n, long l) { if(l==1) return n; n = (n * l) / gcd(n, l); return lcmForBig(n, l-1); } public static void main(String[] args){ Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int i =0;i blue = new ArrayList<>(); ArrayList red = new ArrayList<>(); for(int j=0;jcounter){ counter++; } else{ ans=""NO""; break; } } if(ans==""NO""){ System.out.println(ans); } else{ int tempCounter = n+1; Collections.sort(red); for(int j=red.size()-1;j>=0;j--){ int current = red.get(j); if(current>=tempCounter){ ans=""NO""; break; } else{ tempCounter--; } } if(tempCounter-counter!=1) System.out.println(""NO""); else System.out.println(ans); } } return; } } ",0 5288afb7,df594a00,"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) { bu.readLine(); String s[]=bu.readLine().split("" ""); int n=Integer.parseInt(s[0]),k=Integer.parseInt(s[1]); int a[]=new int[n],i,x,ac[]=new int[n],b[]=new int[k]; Arrays.fill(a,Integer.MAX_VALUE); s=bu.readLine().split("" ""); for(i=0;i pq=new PriorityQueue<>(); for(i=0;i(); for(i=n-1;i>=0;i--) { if(ac[i]!=0) pq.add(ac[i]+i); if(!pq.isEmpty()) a[i]=Math.min(a[i],pq.peek()-i); } for(i=0;i 0) { br.readLine(); String[] str = br.readLine().split("" ""); int n = Integer.parseInt(str[0]); int k = Integer.parseInt(str[1]); int[] a = new int[k]; int[] t = new int[k]; str = br.readLine().split("" ""); for(int i=0; i=0; i--) { right[i] = Math.min(right[i], minright); minright = right[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : right[i]+1; } 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.*; 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 a5d5a95f,c4ca2ff3,"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(); int Q = reader.nextInt(); outer: for (int q = 0; q < Q; q++) { int N = reader.nextInt(); int[][] scores = new int[5][N]; for (int i = 0; i < N; i++) { int[] occurs = new int[5]; String word = reader.next(); for (int j = 0; j < word.length(); j++) { occurs[word.charAt(j) - 'a']++; } for (int j = 0; j < 5; j++) { scores[j][i] = occurs[j] - (word.length() - occurs[j]) ; } } int bestCount = 0; for (int i = 0; i < 5; i++) { int[] curr = scores[i]; Arrays.sort(curr); int currentCount = 1; int currentScore = curr[curr.length - 1]; for (int j = curr.length - 2; j >= 0 && currentScore > 0; j--) { currentScore += curr[j]; currentCount++; } if (currentScore <= 0) currentCount--; bestCount = Math.max(currentCount, bestCount); } System.out.println(bestCount); } } } ","/* 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 1162c08f,6bcc5afd,"import java.util.*; public class CodeForces1525C{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); ArrayList o=new ArrayList(), e=new ArrayList(); int n = sc.nextInt(),dp[][]=new int[n+1][n+1]; for(int i=1;i<=n;i++){ int x=sc.nextInt(); if(x==1)o.add(i); else e.add(i); } for(int i=1;i<=o.size();i++){ dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1)); for(int j=i+1;j<=e.size();j++) dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1))); } System.out.println(dp[o.size()][e.size()]); } } ","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= 2; i--) { if(cur_h[i] < x) return false; int d = Math.min(h[i], cur_h[i]-x)/3; cur_h[i-1] += d; cur_h[i-2] += 2*d; } return cur_h[0] >= x && cur_h[1] >= x; } 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) { n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int max = Integer.MIN_VALUE; for(int i = 0; i < n; i++) { h[i] = Integer.parseInt(st.nextToken()); max = Math.max(max, h[i]); } int l = 0; int r = max; while(l < r) { int mid = l + (r-l+1)/2; if(check(mid)) l = mid; else r = mid-1; } writer.println(l); } writer.close(); br.close(); } } ","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; } } ",1 b7de5c19,c022c315,"import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); for (int i = 0; i < t; i++) { int n = scan.nextInt(); ArrayList> 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.*; import java.util.*; public class Main { // static boolean[] prime = new boolean[10000000]; final static long mod = 1000000007; public static void main(String[] args) { // sieve(); InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int[][] a = new int[n - 1][2]; ArrayList> g = new ArrayList<>(); for (int i = 0; i < n - 1; i++) { g.add(new ArrayList<>()); a[i][0] = in.nextInt() - 1; a[i][1] = in.nextInt() - 1; } g.add(new ArrayList<>()); for (int i = 0; i < n - 1; i++) { g.get(a[i][0]).add(new Data(a[i][1], i)); g.get(a[i][1]).add(new Data(a[i][0], i)); } if (!check(g)) { System.out.println(-1); continue; } int[] ans = new int[n - 1]; ans[0] = 2; for(int node: a[0]) { for(Data d: g.get(node)) { solve(g, d.a, d.ind, false, ans); } } for (int i : ans) System.out.print(i + "" ""); System.out.println(); } out.flush(); } private static void solve(ArrayList> g, int node, int edge, boolean b, int[] ans) { if (ans[edge] != 0) return; ans[edge] = b ? 2 : 3; for (Data d : g.get(node)) { solve(g, d.a, d.ind, !b, ans); } } private static boolean check(ArrayList> g) { for (ArrayList a : g) if (a.size() > 2) return false; return true; } static boolean prime(long k) { for (int i = 2; i * i <= k; i++) { if (k % i == 0) { return false; } } return true; } static long gcd(long a, long b) { if (a % b == 0) { return b; } else { return gcd(b, a % b); } } static void reverseArray(Integer[] a) { for (int i = 0; i < (a.length >> 1); i++) { Integer temp = a[i]; a[i] = a[a.length - 1 - i]; a[a.length - 1 - i] = temp; } } static Integer[] intInput(int n, InputReader in) { Integer[] a = new Integer[n]; for (int i = 0; i < a.length; i++) a[i] = in.nextInt(); return a; } static Long[] longInput(int n, InputReader in) { Long[] a = new Long[n]; for (int i = 0; i < a.length; i++) a[i] = in.nextLong(); return a; } static String[] strInput(int n, InputReader in) { String[] a = new String[n]; for (int i = 0; i < a.length; i++) a[i] = in.next(); return a; } // static void sieve() { // for (int i = 2; i * i < prime.length; i++) { // if (prime[i]) // continue; // for (int j = i * i; j < prime.length; j += i) { // prime[j] = true; // } // } // } } class Data { int a; int ind; Data(int val, int ind) { this.a = Math.abs(val); this.ind = ind; } } class compareVal implements Comparator { @Override public int compare(Data o1, Data o2) { // return (o1.val - o2.val == 0 ? o1.ind - o2.ind : o1.val - o2.val); return (o1.a - o2.a); } } 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()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }",0 921b6e4a,e7dce35b,"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(); } }","import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int n = nextInt(); int k = nextInt(); f = new int[n + 42]; rf = new int[n + 42]; f[0] = 1; 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[] a = new int[n * 2]; for (int i = 0; i < n; i++) { a[i] = nextInt() * 2; a[i + n] = nextInt() * 2 + 1; } Arrays.sort(a); int ans = 0; int curOpen = 0; for (int r = 0; r < 2 * n;) { int l = r; while (r < 2 * n && a[l] == a[r]) r++; int intersections = r - l; if (a[l] % 2 == 0) { ans += C(curOpen + intersections, k); if (ans >= mod) ans -= mod; ans += mod - C(curOpen, k); if (ans >= mod) ans -= mod; curOpen += intersections; } else { curOpen -= intersections; } } pw.println(ans); pw.close(); } static int mod = 998244353; static int mul(int a, int b) { return (int) ((long) a * (long) b % mod); } static int[] f; static int[] rf; static int C(int n, int k) { return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n - k], rf[k])); } 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 int inv(int a) { return pow(a, mod - 2); } static StringTokenizer st = new StringTokenizer(""""); static BufferedReader br; static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } } class Coordinates { int l; int r; public Coordinates(int l, int r) { this.l = l; this.r = r; } } class CoordinatesComparator implements Comparator { @Override public int compare(Coordinates o1, Coordinates o2) { if (o1.l == o2.l) return Integer.compare(o1.r, o2.r); return Integer.compare(o1.l, o2.l); } }",1 14b0fb8e,d221162a,"import java.io.*; import java.util.*; public class Solution { static long res; public static void main(String[] args) throws Exception { FastReader fr=new FastReader(); int n=fr.nextInt(); ArrayList oc=new ArrayList<>(); ArrayList em=new ArrayList<>(); res=Long.MAX_VALUE; for(int i=0;i oc,ArrayList em,int idx,int j,long dp[][]) { if(idx==oc.size()) return 0; long available=em.size()-j; long req=oc.size()-idx; if(available0&&j>0) { if(a.charAt(i-1)==b.charAt(j-1)) { lcs=a.charAt(i-1)+lcs; i--; j--; } else { if(dp[i-1][j]>dp[i][j-1]) i--; else j--; } } return lcs; } public static long facto(long n) { if(n==1||n==0) return 1; return n*facto(n-1); } public static long gcd(long n1,long n2) { if (n2 == 0) { return n1; } return gcd(n2, n1 % n2); } public static boolean isPali(String s) { int i=0; int j=s.length()-1; while(i<=j) { if(s.charAt(i)!=s.charAt(j)) return false; i++; j--; } return true; } public static String reverse(String s) { String res=""""; for(int i=0;i0) { int n = sc.nextInt(); int a[] = new int[n]; for(int i=0;i a1 = new ArrayList<>(); ArrayList a2 = new ArrayList<>(); for (int i = 0; i < n; i++) { if(a[i] == 0) a1.add(i); else a2.add(i); } long dp[][] = new long[n+1][n+1]; for (int i = 0; i <= n; i++) { Arrays.fill(dp[i],-1); } out.write(find(0,0,a1,a2,dp)+""\n""); } out.close(); } private static long find(int i, int j, List a1, ArrayList a2, long[][] dp) { if(j == a2.size()) return 0; int req = a2.size()-j; int ava = a1.size()-i; if(ava { public int compare(Struct a, Struct b) { return a.h - b.h; } } static class Struct{ int h,ind; Struct(int h,int ind){ this.h=h; this.ind=ind; } } public static void main(String[] args) throws java.lang.Exception { // your code goes here FastReader scn = new FastReader(); int t, k, i, j, l, f, max=0; t=scn.nextInt(); while(t-->0){ int n=scn.nextInt(); int m=scn.nextInt(); int x=scn.nextInt(); Struct a[]=new Struct[n]; for (i=0;im){ ms=1; } b[a[i].ind]=ms; ms++; } System.out.println(""YES""); for (i=0;i 0) solve(); out.println(sb); out.close(); } public static void solve() { // Road To Specialist Day 3 int n = scn.nextInt(), m = scn.nextInt(), x = scn.nextInt(); int[] a = new int[n], ans = new int[n]; for(int i = 0; i < n; i++) a[i] = scn.nextInt(); PriorityQueue pq = new PriorityQueue<>(); for(int i = 0; i < m; i++) pq.add(new Pair(0L, i)); for(int i = 0; i < n; i++) { int e = a[i]; Pair p = pq.poll(); p.value += e; pq.add(p); ans[i] = p.id + 1; } boolean check = false; long prev = pq.poll().value; while(!pq.isEmpty()) { long cur = pq.poll().value; if(Math.abs(cur - prev) > x) check = true; prev = cur; } if(check) sb.append(""NO""); else { sb.append(""YES\n""); for(int i : ans) sb.append(i + "" ""); } sb.append(""\n""); } static class Pair implements Comparable { int id; long value; public Pair(long value, int id) { this.id = id; this.value = value; } public int compareTo(Pair o) { return Long.compare(value, o.value); } } }",0 bf0df1d5,d1cd194e,"import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; import java.io.IOException; public class C_Phoenix_and_Towers { 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 class Pair implements Comparable { int id, h; public Pair(int id, int h) { this.id = id; this.h = h; } public int compareTo(Pair o) { return this.h - o.h; } } public static void main(String[] args) throws java.lang.Exception { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int x = sc.nextInt(); int tow[] = new int[n]; int ans[] = new int[n]; PriorityQueue pq = new PriorityQueue<>(); for (int i = 0; i < n; i++) { tow[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { ans[i] = i + 1; pq.add(new Pair(i + 1, tow[i])); } for (int i = m; i < n; i++) { Pair p = pq.poll(); p.h = p.h + tow[i]; ans[i] = p.id; pq.add(p); } System.out.println(""YES""); for (int i = 0; i < n; i++) { System.out.print(ans[i] + "" ""); } System.out.println(); } } }","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 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 464a03b8,cf27732e,"import java.util.*; public class Soltion{ public static void main(String []args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ 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 []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 22b41936,9cea10af," 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()); } } } ","//package currentContest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class P4 { static int dp[][]=new int[5000+1][5000+1]; public static void main(String[] args) { // TODO Auto-generated method stub FastReader sc=new FastReader(); int t=1; //t=sc.nextInt(); StringBuilder s=new StringBuilder(); while(t--!=0) { int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++) { P4.dp[i][j]=-1; } } ArrayList one=new ArrayList<>(); ArrayList zero=new ArrayList<>(); for(int i=0;i zero, ArrayList one) { //System.out.println(i+"" ""+j); // TODO Auto-generated method stub if(j==m) { return 0; } int av=n-i; int rem=m-j; if(av= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public int[] nia1(int n) { int a[] = new int[n+1]; for (int i = 1; i <=n; i++) { a[i] = ni(); } return a; } public long[] nla(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public long[] nla1(int n) { long a[] = new long[n+1]; for (int i = 1; i <= n; i++) { a[i] = nl(); } return a; } public Long[] nLa(int n) { Long a[] = new Long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public Long[] nLa1(int n) { Long a[] = new Long[n+1]; for (int i = 1; i <= n; i++) { a[i] = nl(); } return a; } public Integer[] nIa(int n) { Integer a[] = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public Integer[] nIa1(int n) { Integer a[] = new Integer[n+1]; for (int i = 1; i <= n; i++) { a[i] = ni(); } return a; } public String rs() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static class Key { private final int x, y; public Key(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Key)) return false; Key key = (Key) o; return x == key.x && y == key.y; } @Override public int hashCode() { int result = x; result = 31 * result + y; return result; } } static class Pair{ int x,y; Pair(int a,int b){ x=a;y=b; } // @Override // public int compareTo(Pair p) { // if(x==p.x) return y-p.y; // return x-p.x; // } } static void shuffleArray(long temp[]){ int n = temp.length; Random rnd = new Random(); for(int i=0; i s = new Stack<>(); int ng[] = new int[n]; for(int i = 0 ; i < n ; i++){ while(!s.isEmpty() && arr[s.peek()] <= arr[i]){ ng[s.pop()] = i; } s.add(i); } while(!s.isEmpty()){ ng[s.pop()] = n; } return ng; } static int[] nextS(int arr[]){ int n = arr.length; Stack s = new Stack<>(); int ns[] = new int[n]; for(int i = 0 ; i < n ; i++){ while(!s.isEmpty() && arr[s.peek()] >= arr[i]){ ns[s.pop()] = i; } s.add(i); } while(!s.isEmpty()){ ns[s.pop()] = n; } return ns; } static int[] prevG(int arr[]){ int n = arr.length; Stack s = new Stack<>(); int pg[] = new int[n]; for(int i = n-1 ; i >= 0 ; i--){ while(!s.isEmpty() && arr[s.peek()] <= arr[i]){ pg[s.pop()] = i; } s.add(i); } while(!s.isEmpty()){ pg[s.pop()] = -1; } return pg; } static int[] prevS(int arr[]){ int n = arr.length; Stack s = new Stack<>(); int ps[] = new int[n]; for(int i = n-1 ; i >= 0 ; i--){ while(!s.isEmpty() && arr[s.peek()] >= arr[i]){ ps[s.pop()] = i; } s.add(i); } while(!s.isEmpty()){ ps[s.pop()] = -1; } return ps; } public static void main(String [] args){ InputReader sc=new InputReader(System.in); int n = sc.ni(); int arr [] = sc.nia(n); int ng[] = nextG(arr); int ns [] = nextS(arr); int pg[] = prevG(arr); int ps[] = prevS(arr); int ans[]=new int[n]; Arrays.fill(ans,10000000); ans[n-1] = 0; for(int i = n -1 ; i >= 0 ; i --){ if(ns[i] != n){ ans[i] = Math.min(ans[i] , ans[ns[i]]+1); } if(ng[i] != n){ ans[i] = Math.min(ans[i] , ans[ng[i]]+1); } if(pg[i] != -1){ ans[pg[i]] = Math.min(ans[pg[i]] , ans[i]+1); } if(ps[i] != -1){ ans[ps[i]] = Math.min(ans[ps[i]] , ans[i]+1); } } w.println(ans[0]); w.close(); } }","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); } } } ",0 63118334,c392efe7,"import java.util.*; import java.io.*; public class MyClass { 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; } } static ArrayList edge[]; 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(); int a=fs.nextInt(); int b=fs.nextInt(); int da=fs.nextInt(); int db=fs.nextInt(); edge=new ArrayList[n+1]; for(int i=1;i<=n;i++) edge[i]=new ArrayList<>(); for(int i=1;i queue=new LinkedList<>(); queue.add(a); while(!queue.isEmpty()) { int node=queue.poll(); for(int v:edge[node]) { if(dist[v]==-1) { dist[v]=dist[node]+1; queue.add(v); } } } if(dist[b]<=da) { pw.println(""Alice""); continue; } int mx=0,mxvert=1; for(int i=1;i<=n;i++) { if(dist[i]>mx) { mx=dist[i]; mxvert=i; } } Arrays.fill(dist,-1); dist[mxvert]=0; queue.add(mxvert); while(!queue.isEmpty()) { int node=queue.poll(); for(int v:edge[node]) { if(dist[v]==-1) { dist[v]=dist[node]+1; queue.add(v); } } } for(int i=1;i<=n;i++) mx=Math.max(mx,dist[i]); db=Math.min(db,mx); if(db>2*da) pw.println(""Bob""); else pw.println(""Alice""); } pw.flush(); pw.close(); } }","import java.util.*; import java.io.*; import java.math.*; /** * * @Har_Har_Mahadev */ /** * Main , Solution , Remove Public */ public class D { static int visited[]; static int distance[]; private static ArrayList[] adj; private static void BFS(int node) { visited[node] = 1; Queue q = new LinkedList(); q.offer(node); distance[node] = 0; while(!q.isEmpty()) { int curr = q.poll(); for(int child : adj[curr]) { if(visited[child] == 0) { q.offer(child); distance[child] = distance[curr] + 1; visited[child] = 1; } } } } public static void process() throws IOException { int n = sc.nextInt(),a = sc.nextInt(),b = sc.nextInt(), da = sc.nextInt(),db = sc.nextInt(); adj = new ArrayList[n+1]; distance = new int[n+1]; visited = new int[n+1]; for(int i = 0; i<=n; i++)adj[i] = new ArrayList(); for(int i =1; i lis = new ArrayList(); for(int i = 1; i<=n; i++)lis.add(new Pair(distance[i], i)); Collections.sort(lis); Pair e = lis.get(n-1); distance = new int[n+1]; visited = new int[n+1]; BFS(e.y); int max = 0; for(int i = 1; i<=n; i++)max = max(max,distance[i]); if(max-da<=da) { System.out.println(""Alice""); return; } System.out.println(""Bob""); } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= private static void google(int tt) { System.out.print(""Case #"" + (tt) + "": ""); } static FastScanner sc; static PrintWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new PrintWriter(System.out); } else { sc = new FastScanner(100); out = new PrintWriter(""output.txt""); } int t = 1; t = sc.nextInt(); int TTT = 1; while (t-- > 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 debug(Object o) { System.out.println(o); } static void debug(Object x, Object y) { System.out.println(""("" + x + "" , "" + y + "")""); } static void println(Object o) { out.println(o); } static void println(int[] o) { for (int e : o) print(e + "" ""); println(); } static void println(long[] o) { for (long e : o) print(e + "" ""); println(); } 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); } static void reverseArray(int[] a) { int n = a.length; int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void reverseArray(long[] a) { int n = a.length; long arr[] = new long[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } } ",0 c57a973e,fa484fdd,"import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Air { public static void main(String[] args) { FastScanner sc = new FastScanner(); int T = sc.nextInt(); for(int tt=0; tt=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 3b10bd6d,f2cf6a74,"import java.util.*; public class B { //static int num = (int)Math.pow(10,6); static Scanner sc = null; public static void main(String[] args) { //System.out.println(""Enter :""); sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- >0){ 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;i0){ HashMap map=new HashMap<>(); int b=scn.nextInt(); int m=scn.nextInt(); int x=scn.nextInt(); int[] arr=new int[b]; PriorityQueue pq=new PriorityQueue<>(new pqc()); for(int i=0;i{ public int compare(Node n1,Node n2){ if(n1.datan2.data){ return 1; } else{ return 0; } } } } ","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) { 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.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 E { 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; 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(); } 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; } } static long MOD=1000000000; static class Pair{ long a; int b; Pair(long i,int j){ a=i; b=j; } } static long[] solve(int[] pos,long[] arr,int n,int k){ long[] ans=new long[n]; long[] left=new long[n]; long[] right=new long[n]; long min=Integer.MAX_VALUE; for(int i=0;i=0;i--){ min=Math.min(min+1,arr[i]); right[i]=min; } for(int i=0;i0){ int n=fs.nextInt(); int k=fs.nextInt(); int[] pos=new int[k]; for(int i=0;i0){ 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) { int n = Integer.parseInt(br.readLine()); String a = br.readLine(); String b = br.readLine(); int alit = 0; int blit = 0; int ans = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { if (a.charAt(i) == '1') alit++; if (b.charAt(i) == '1') blit++; } if (alit == blit) { int count = 0; for (int i = 0; i < n; i++) if (a.charAt(i) != b.charAt(i)) count++; ans = Math.min(count, ans); } if (alit == n - blit + 1) { int count = 0; for (int i = 0; i < n; i++) if (a.charAt(i) == b.charAt(i)) count++; ans = Math.min(ans, count); } if (ans == Integer.MAX_VALUE) { pw.println(""-1""); } else { pw.println(ans); } } pw.close(); } }","import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=Integer.valueOf(sc.nextLine()); while (t-->0){ int n=Integer.valueOf(sc.nextLine()); int ans=100001; String a=sc.nextLine(); String b=sc.nextLine(); HashSet listb=new HashSet<>(); ArrayList lista=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 int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } public static void main(String[] arg) throws IOException{ //Reader in=new Reader(); Scanner in=new Scanner(System.in); int n=in.nextInt(); int[] arr=new int[n]; ArrayList zero=new ArrayList(); ArrayList one=new ArrayList(); for(int i=0;i ones=new ArrayList<>(); ArrayList zero=new ArrayList<>(); for(int i=0;i=0) { cost+=min[i-1][curz-1]; } dp[i][curz]=cost; } minn=Integer.MAX_VALUE; for(int j=0;j0){ int n = fs.nextInt(), k = fs.nextInt(); int[] positions = fs.readArray(k), temps = fs.readArray(k); int[] forced = new int[n]; Arrays.fill(forced, Integer.MAX_VALUE/2); for(int i=0; i=0; i--) forced[i] = Math.min(forced[i], forced[i+1]+1); for(int i=0; i ""+a); } public static void debug(long a, long b){ System.out.println(""--> ""+ a +"" + "" + b); } public static void debug(char a, char b){ System.out.println(""--> ""+ a +"" + "" + b); } public static void debug(int[] array){ System.out.print(""Array--> ""); System.out.println(Arrays.toString(array)); } public static void debug(char[] array){ System.out.print(""Array--> ""); System.out.println(Arrays.toString(array)); } public static void debug(HashMap map){ System.out.print(""Map--> ""+map.toString()); } } } ","import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Arrays; public class Main{ public static void main (String[] args){ FastReader s = new FastReader(); int t=1;t=s.ni(); for(int test=1;test<=t;test++){ int n=s.ni(),k=s.ni(); int position[]=s.readArray(k),temp[]=s.readArray(k); int ans[]=new int[n]; Arrays.fill(ans,Integer.MAX_VALUE/2); for(int i=0;i=0;i--){ ans[i]=Math.min(ans[i],ans[i+1]+1); } for(int i=0;i