F1,F2,text_1,text_2,label 464a03b8,ff1fc018,"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;i Barr = new ArrayList(); List Rarr = new ArrayList(); for (int i = 0; i < n; i++) { if(color.charAt(i) == 'B')Barr.add(arr[i]); else Rarr.add(arr[i]); } Barr.sort(Comparator.naturalOrder()); Rarr.sort(Comparator.reverseOrder()); for (int i = 0; i < Barr.size(); i++) { if(Barr.get(i)< i + 1)return false; } for (int i = 0; i < Rarr.size(); i++) { int expect = n-i; if(Rarr.get(i) > expect)return false; } return true; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); for (int i = 0; i < n; i++) { int m = input.nextInt(); int[] arr = new int[m]; for(int j = 0;j 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 213340b3,35f0c004,"import java.util.*; import java.io.*; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void sort(int a[]){ // int -> long ArrayList arr=new ArrayList<>(); // Integer -> Long for(int i=0;i0){ res++; n/=2; } return res; } static int mod = (int)1e9+7; static int INF = Integer.MAX_VALUE; static PrintWriter out; static FastReader sc ; public static void main(String[] args) throws IOException { sc = new FastReader(); out = new PrintWriter(System.out); // primes(); // ================================ // int test = sc.nextInt(); while (test-- > 0) { int n = sc.nextInt(); String s = sc.nextLine(); String t = sc.nextLine(); solver(s,t, n); } // ================================ // // int n = sc.nextInt(); // solver(); // ================================ // out.flush(); } public static void solver(String s, String t, int n) { int diff = 0; int one = 0; int zero = 0; for(int i=0;i=0 && (n-diff-1)%2==0 && zero>0 && (n-diff-zero)==zero-1){ res = Math.min(res, (n-diff-1)+1); } out.println(res==INF?-1:res); } } ","import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=input.nextInt(); while(T-->0) { int n=input.nextInt(); String a=input.next(); String b=input.next(); int same1=0,same0=0,opp1=0,opp0=0; for(int i=0;i 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=input.nextInt(); int a[]=new int[n]; ArrayList list=new ArrayList<>(); ArrayList space=new ArrayList<>(); for(int i=0;i0){ 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 A734C { public static void main(String[] args) { JS scan = new JS(); int t = scan.nextInt(); loop:while(t-->0){ int n = scan.nextInt(); String[] arr= new String[n]; Integer[][] counts = new Integer[5][n]; for(int i = 0;i<5;i++){ for(int j = 0;j=0;j--){ extra+=counts[i][j]; if(extra>0)curr++; } best = Math.max(best,curr); } System.out.println(best); } } static class JS { public int BS = 1 << 16; public char NC = (char) 0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar() { 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 long nextLong() { num = 1; boolean neg = false; if (c == NC) c = nextChar(); for (; (c < '0' || c > '9'); c = nextChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = nextChar()) { res = (res << 3) + (res << 1) + c - '0'; num *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / num; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c > 32) { res.append(c); c = nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c != '\n') { res.append(c); c = nextChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = nextChar(); if (c == NC) return false; else if (c > 32) return true; } } } } ",0 1dab88fb,bac616ee,"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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; public class NotAssigning { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextArray(int n) { int[] a = new int[n]; for (int i=0; i> t, int cur, boolean mode, int[] w) { vis[cur] = true; for (Pair p : t.get(cur)) { if (!vis[p.a]) { if (mode) { w[p.b] = 3; } else { w[p.b] = 2; } dfs(t, p.a, !mode, w); } } } public static void solve(int n, int[] u, int[] v) { ArrayList> t = new ArrayList>(n); for (int i=0; i()); } for (int i=0; i 2) { System.out.println(""-1""); return; } if (t.get(i).size() == 1) { start = i; } } vis = new boolean[n]; int[] w = new int[n-1]; dfs(t, start, false, w); StringBuilder ans = new StringBuilder(); for (int i=0; i 0) { int n = in.nextInt(); int[] u = new int[n-1]; int[] v = new int[n-1]; for (int i=0; i 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 5be7547e,cb032314,"// package codeforces; import java.io.*; import java.util.*; public class practice { static FastScanner fs = new FastScanner(); public static void main(String[] args) { int t = 1; t = fs.nextInt(); for(int i=1;i<=t;i++) { solve(t); } } public static void dfs(int u, boolean visited[], ArrayList> g[], int ans[], int x) { visited[u] = true; for(ArrayList v:g[u]) { if(!visited[v.get(0)]) { ans[v.get(1)]=x; dfs(v.get(0),visited,g,ans,x^1); } } return; } @SuppressWarnings(""unused"") public static void solve(int tt) { int n = fs.nextInt(); @SuppressWarnings(""unchecked"") ArrayList> g[] = new ArrayList[n]; for(int i=0;i>(); int ans[] = new int[n]; boolean visited[] = new boolean[n]; int deg[] = new int[n]; for(int i=0;i L1=new ArrayList(); L1.add(v);L1.add(i); g[u].add(L1); ArrayList L2=new ArrayList(); L2.add(u);L2.add(i); g[v].add(L2); deg[u]++; deg[v]++; } // degree must be <=2 cause if greater than that then sum will be always non prime // there will be exactly 2 leafs, we start dfs from one of them int leaf=-1; boolean notcool=false; for(int i=0;i2) { System.out.println(-1); return; } } for(int i=0;i L = new ArrayList(); for(int i:a) { L.add(i); } Collections.sort(L); for(int i=0;i readList(int n) { ArrayList a = new ArrayList(); int x; for(int i=0;i[]grid = new List[n]; for (int i = 0; i < n; i++) { grid[i] = new ArrayList(); } Listlist = new ArrayList<>(); for (int i = 0; i < n - 1; i++) { int x = sc.nextInt() - 1; int y = sc.nextInt() - 1; grid[x].add(y); grid[y].add(x); degree[x]++; degree[y]++; list.add(new pair(Math.min(x,y),Math.max(x,y))); } int begin = 0; for(int i = 0;i < degree.length;i++){ if(degree[i] > 2){ System.out.println(-1); return; } if(degree[i] == 1){ begin = i; } } boolean[]used = new boolean[n]; int[]p = new int[]{5,2,11,2}; int idx = 0; HashMapmap = new HashMap<>(); while (!used[begin]){ used[begin] = true; for(int next : grid[begin]){ if(used[next]) continue; map.put(new pair(Math.min(begin,next),Math.max(begin,next)),p[idx % 4]); idx++; begin = next; } } StringBuilder bd = new StringBuilder(); for(pair pp : list){ bd.append(map.get(pp) + "" ""); } System.out.println(bd.toString().trim()); } public static void main(String[] args) { int n = sc.nextInt(); for(int i = 0;i < n;i++){ solve(); } } 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 occupied = new ArrayList<>(); for (int i = 0; i < n; i++) { arr[i] = scn.nextInt(); if (arr[i] == 1) occupied.add(i); } int[][] dp = new int[n + 1][occupied.size() + 1]; for (int[] row : dp) Arrays.fill(row, (int) 1e9); dp[0][0] = 0; for (int i = 1; i <= n; i++) { dp[i][0] = 0; for (int j = 1; j <= occupied.size(); j++) { dp[i][j] = dp[i - 1][j]; if (arr[i - 1] == 0) dp[i][j] = Math.min(dp[i][j], dp[i - 1][j - 1] + Math.abs(i - 1 - occupied.get(j - 1))); } } System.out.println(dp[n][occupied.size()]); } } ","import java.util.*; public class D { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); ArrayList occupied = new ArrayList<>(); ArrayList vacant = new ArrayList<>(); for (int i = 0; i < n; i++) { int x = scanner.nextInt(); if (x == 1) occupied.add(i); else vacant.add(i); } Solution Solution = new Solution(occupied, vacant); // System.out.println(Solution.tabulation()); System.out.println(Solution.memoization()); } } class Solution { ArrayList occupied, vacant; int x, y; public Solution(ArrayList occupied, ArrayList vacant) { this.occupied = occupied; this.vacant = vacant; x = occupied.size(); y = vacant.size(); } int tabulation() { return tabulation(x, y); } int tabulation(int x, int y) { int[][] dp = new int[x+1][y+1]; for (int i = 0; i <= x; i++) { Arrays.fill(dp[i], Integer.MAX_VALUE/2); } for (int i = 0; i <= x; i++) { dp[i][0] = 0; } for (int i = 0; i <= y; i++) { dp[0][i] = 0; } for (int i = 1; i <= x; i++) { for (int j = 1; j <= y; j++) { if(i == j) { dp[i][j] = dp[i-1][j-1] + Math.abs(occupied.get(i-1) - vacant.get(j-1)); } else { dp[i][j] = Math.min(dp[i][j-1], dp[i-1][j-1] + Math.abs(occupied.get(i-1) - vacant.get(j-1))); } } } return dp[x][y]; } int memoization() { int[][] dp = new int[x][y]; for (int i = 0; i < x; i++) { Arrays.fill(dp[i], -1); } return memoization(dp, x-1, y-1); } int memoization(int[][] dp, int n, int m) { if(n < 0) { return 0; } if(m < n) { return Integer.MAX_VALUE; } if(dp[n][m] != -1) { return dp[n][m]; } int first = memoization(dp, n, m-1); int second = memoization(dp, n-1, m-1) + Math.abs(occupied.get(n) - vacant.get(m)); dp[n][m] = Math.min(first, second); return dp[n][m]; } }",0 680ba922,6e207cbf,"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.*; 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); } } } } ",0 14b0fb8e,8ddb5587,"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;i a = new ArrayList<>(); ArrayList b = new ArrayList<>(); for (int i = 0; i < n; i++) { int x = in.nextInt(); if (x == 1) { a.add(i); } else { b.add(i); } } long dp[][] = new long[a.size() + 5][b.size() + 5]; for (int i = a.size()-1; i >= 0; i--) { dp[i][b.size()] = Integer.MAX_VALUE; for (int j = b.size()-1; j >= 0; j--) { dp[i][j] = dp[i][j + 1]; dp[i][j] = Math.min(dp[i][j], Math.abs(a.get(i) - b.get(j)) + dp[i + 1][j + 1]); } } out.println(dp[0][0]); } 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()); } long nextLong() { return Long.parseLong(next()); } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } } }",0 16a2a867,3951966f,"import java.util.*; import java.io.*; public class Armchairs{ public static void main(String[] args) { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out); Scanner sc= new Scanner (System.in); //Code From Here---- int t =fr.nextInt(); ArrayList chairs= new ArrayList<>(); ArrayList free= new ArrayList<>(); for (int i = 0; i < t; i++) { int state =fr.nextInt(); if(state==1) { chairs.add(i); } else{ free.add(i); } } int [][] dp=new int [t][t]; for (int[] is : dp) { Arrays.fill(is, -1); } int ans=solve(chairs,free,0,0,chairs.size(),dp); out.println(ans); out.flush(); sc.close(); } //This RadixSort() is for long method private static int solve(ArrayList chairs, ArrayList free, int i, int j, int size,int [][] dp) { if (dp[i][j]!=-1) { return dp[i][j]; } if (size==0) { return 0; } if (j==free.size()) { return 10000000; } int a=solve(chairs, free, i, j+1, size,dp); int b=Math.abs(chairs.get(i)-free.get(j))+solve(chairs, free, i+1, j+1, size-1,dp); dp[i][j]=Math.min(a, b); return dp[i][j]; } public static long[] radixSort(long[] f){ return radixSort(f, f.length); } public static long[] radixSort(long[] f, int n) { long[] to = new long[n]; { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>16&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>16&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>32&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>32&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>48&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>48&0xffff)]++] = f[i]; long[] d = f; f = to; to = d; } return f; } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } 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 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) >>> 1L; if (a[m] <= x) l = m; else r = m; } return l + 1; } static int upperBound(long[] arr, int start, int end, long k) { int N = end; while (start < end) { int mid = start + (end - start) / 2; if (k >= arr[mid]) { start = mid + 1; } else { end = mid; } } if (start < N && arr[start] <= k) { start++; } return start; } static long lowerBound(long[] arr, int start, int end, long k) { int N = end; while (start < end) { int mid = start + (end - start) / 2; if (k <= arr[mid]) { end = mid; } else { start = mid + 1; } } if (start < N && arr[start] < k) { start++; } return start; } // For Fast Input ---- 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.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; public class Armchairs { static ArrayList f; static ArrayList u; static int dp[][]; static int fun(int i, int j){ if(i == f.size()) return 0; if(j == u.size()) return 99999999; if(dp[i][j] != -1) return dp[i][j]; int ans1 = fun(i, j+1); int ans2 = fun(i+1, j+1) + Math.abs(f.get(i)-u.get(j)); return dp[i][j] = Math.min(ans1, ans2); } private static int solve(int n, int a[]) { for (int i = 0; i < n; i++) { if (a[i]==0) u.add(i); else f.add(i); } return fun(0,0); } public static void main(String[] args) throws IOException { Scanner s = new Scanner(); int t = 1; StringBuilder ans = new StringBuilder(); int count = 0; while (t-- > 0) { int n = s.nextInt(); int a[] = new int[n]; dp=new int[n][n]; for (int i = 0; i < n; i++) { a[i]=s.nextInt(); } f=new ArrayList<>(); u=new ArrayList<>(); for( int i=0; i= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static long norm(long a, long MOD) { return ((a % MOD) + MOD) % MOD; } public static long msub(long a, long b, long MOD) { return norm(norm(a, MOD) - norm(b, MOD), MOD); } public static long madd(long a, long b, long MOD) { return norm(norm(a, MOD) + norm(b, MOD), MOD); } public static long mMul(long a, long b, long MOD) { return norm(norm(a, MOD) * norm(b, MOD), MOD); } public static long mDiv(long a, long b, long MOD) { return norm(norm(a, MOD) / norm(b, MOD), MOD); } } ",0 bf0df1d5,ea899386,"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.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 0) { int n=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i b=new ArrayList<>(); ArrayList r=new ArrayList<>(); for(int i=0;i=cur)cur++; else{ ok=false; break; } //cur++; } for(int i:r){ if(i<=cur)cur++; else{ ok=false; break; } } if(ok)System.out.println(""YES""); else System.out.println(""NO""); } } catch (Exception e) { return; } } public static int lowerbound(long[] ar,int k) { int s=0; int e=ar.length; while (s !=e) { int mid = s+e>>1; if (ar[mid] 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 int upperbound(long[] arr, int k) { int s = 0; int e = arr.length; while (s != e) { int mid = s + e >> 1; if (arr[mid] <= k) { s = mid + 1; } else { e = mid; } } if (s == arr.length) { return -1; } return s; } public static long pow(long x,long y,long mod){ if(x==0)return 0l; if(y==0)return 1l; //(x^y)%mod if(y%2l==1l){ return ((x%mod)*(pow(x,y-1l,mod)%mod))%mod; } return pow(((x%mod)*(x%mod))%mod,y/2l,mod); } 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; } */ ","import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Codeforces { public static void main(String[] args) { FastReader fastReader = new FastReader(); int t = fastReader.nextInt(); while (t-- > 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 35eb27da,64ce7b1e,"/* 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 void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); sc.nextLine(); int a[][] = new int[26][26]; int b[][][] = new int[26][26][26]; boolean bl = false; String arr[] = new String[n]; for(int i = 0 ; i < n ; i++) { arr[i] = sc.nextLine(); if(arr[i].length() == 1) { bl = true; } else if(arr[i].length() == 2) { //a[arr[i].charAt(0)-'a'][arr[i].charAt(1)-'a'] = 1; if(arr[i].charAt(0)==arr[i].charAt(1)) bl = true; } else { //b[arr[i].charAt(0)-'a'][arr[i].charAt(1)-'a'][arr[i].charAt(2)-'a'] = 1; if(arr[i].charAt(0) == arr[i].charAt(2)) bl = true; } } if(bl) System.out.println(""YES""); else { for(int i = 0; i < n ; i++) { if(arr[i].length() == 2) { int p1 = arr[i].charAt(0)-'a'; int p2 = arr[i].charAt(1)-'a'; if(a[p2][p1] == 1) bl = true; for(int j = 0; j < 26 ; j++) { if(b[p2][p1][j] == 1) bl = true; } a[p1][p2] = 1; } else { int p1 = arr[i].charAt(0)-'a'; int p2 = arr[i].charAt(1)-'a'; int p3 = arr[i].charAt(2)-'a'; if(a[p3][p2] == 1) bl = true; if(b[p3][p2][p1] == 1) bl = true; b[p1][p2][p3] = 1; } } if(bl) System.out.println(""YES""); else System.out.println(""NO""); } } } }","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(); Set st = new HashSet(); String[] arr = new String[n]; boolean pos = false; for(int i = 0; i < n; i++) { String str = s.next(); st.add(str); arr[i] = str; if(str.length() == 1) pos = true; } if(pos) { System.out.println(""YES""); continue; } for(int i = 0; i < n; i++) { String str = arr[i]; st.remove(str); if(str.charAt(0) == str.charAt(str.length() - 1)) pos = true; if(str.length() == 3) { String str1 = Character.toString(str.charAt(1)) + Character.toString(str.charAt(0)); if(st.contains(str1)) pos = true; String str2 = Character.toString(str.charAt(2)) + str1; if(st.contains(str2)) pos = true; } else { String str1 = Character.toString(str.charAt(1)) + Character.toString(str.charAt(0)); if(st.contains(str1)) pos = true;; for(int j = 0; j < 26; j++) { char ch = (char) ((int)'a' + j); String str2 = Character.toString(ch) + str1; if(st.contains(str2)) pos = true;; } } } //System.out.println(st.toString()); if(pos) System.out.println(""YES""); else System.out.println(""NO""); //System.out.println(st.contains(""ba"")); } } } 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 5aebbb1b,e647bef7,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Learning { static LinkedList[] adj; public static void main(String[] args) throws Exception { FastInput in = new FastInput(); StringBuilder sb = new StringBuilder(); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int a = in.nextInt() - 1; int b = in.nextInt() - 1; int da = in.nextInt(); int db = in.nextInt(); adj = new LinkedList[n]; for (int i = 0; i < n; i++) { adj[i] = new LinkedList<>(); } for (int i = 0; i < n - 1; i++) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; adj[u].add(v); adj[v].add(u); } boolean f = solve(n, a, b, da, db); if (f) { sb.append(""Bob""); } else { sb.append(""Alice""); } sb.append(""\n""); } System.out.println(sb.toString()); } private static boolean solve(int n, int a, int b, int da, int db) { if (db <= da * 2) { return false; } Queue que = new LinkedList<>(); que.add(a); int[] dist = new int[n]; Arrays.fill(dist, -1); dist[a] = 0; while (!que.isEmpty()) { int t = que.poll(); for (int i : adj[t]) { if (dist[i] == -1) { que.add(i); dist[i] = dist[t] + 1; } } } if (dist[b] <= da) { return false; } int maxPath = 0; for (int i = 0; i < n; i++) { maxPath = Math.max(maxPath, dfs(i, new boolean[n])); } if (2 * da >= maxPath-1) { return false; } return true; } private static int dfs(int i, boolean[] vis) { vis[i] = true; int max = 0; for (int v : adj[i]) { if (!vis[v]) { max = Math.max(dfs(v, vis), max); } } return max + 1; } static long calculate(int[] arr, int x) { long val = 0L; long p = 1; for (int value : arr) { val += Math.abs(value - p); p *= x; } return val; } } class FastInput { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; String next() throws IOException { if (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } Integer nextInt() throws IOException { return Integer.parseInt(next()); } Long nextLong() throws IOException { return Long.parseLong(next()); } }"," import java.io.*; import java.util.*; public class Main { private static final boolean N_CASE = true; private List> g; private int a; private int b; private int da; private int db; private int max; private int ab; private int dfs(int u, int fa, int depth) { if (u == a) { ab = depth; } int m1 = 0, m2 = 0; for (int v : g.get(u)) { if (v != fa) { int m = dfs(v, u, depth + 1) + 1; if (m > m1) { m2 = m1; m1 = m; } else if (m > m2) { m2 = m; } } } max = Math.max(max, m1 + m2); int cmax = Math.max(m1, m2); max = Math.max(max, cmax + depth); return cmax; } private void solve() { int n = sc.nextInt(); a = sc.nextInt() - 1; b = sc.nextInt() - 1; da = sc.nextInt(); db = sc.nextInt(); g = createGraph(n); for (int i = 0; i < n - 1; ++i) { int u = sc.nextInt() - 1, v = sc.nextInt() - 1; g.get(u).add(v); g.get(v).add(u); } max = 0; dfs(b, -1, 0); db = Math.min(max, db); boolean win = true; if (ab > da) { if (db > da * 2) { win = false; } } out.println(win ? ""Alice"" : ""Bob""); } private void run() { int T = N_CASE ? sc.nextInt() : 1; for (int t = 0; t < T; ++t) { solve(); } } private static MyWriter out; private static MyScanner sc; private static class MyScanner { BufferedReader br; StringTokenizer st; private MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } int[][] nextIntArray(int n, int m) { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = sc.nextInt(); } } return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } long[][] nextLongArray(int n, int m) { long[][] a = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextLong(); } } return a; } List nextList(int n) { List list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(nextInt()); } return list; } List nextLongList(int n) { List list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(nextLong()); } return list; } char[] nextCharArray(int n) { return sc.next().toCharArray(); } char[][] nextCharArray(int n, int m) { char[][] c = new char[n][m]; for (int i = 0; i < n; i++) { String s = sc.next(); for (int j = 0; j < m; j++) { c[i][j] = s.charAt(j); } } return c; } } private static class MyWriter extends PrintWriter { private MyWriter(OutputStream outputStream) { super(outputStream); } void printArray(int[] a) { for (int i = 0; i < a.length; ++i) { print(a[i]); print(i == a.length - 1 ? '\n' : ' '); } } void printArray(long[] a) { for (int i = 0; i < a.length; ++i) { print(a[i]); print(i == a.length - 1 ? '\n' : ' '); } } void println(int[] a) { for (int v : a) { println(v); } } void print(List list) { for (int i = 0; i < list.size(); ++i) { print(list.get(i)); print(i == list.size() - 1 ? '\n' : ' '); } } void println(List list) { list.forEach(this::println); } } private List> createGraph(int n) { List> g = new ArrayList<>(); for (int i = 0; i < n; ++i) { g.add(new ArrayList<>()); } return g; } private void fill(int[][] a, int value) { for (int[] row : a) { fill(row, value); } } private void fill(int[] a, int value) { Arrays.fill(a, value); } public static void main(String[] args) { out = new MyWriter(new BufferedOutputStream(System.out)); sc = new MyScanner(); new Main().run(); out.close(); } }",0 30e0cc81,b55888de,"import java.io.*; import java.util.*; import java.math.BigInteger; import java.lang.*; public class Main { static class sortLong implements Comparator{ public int compare(long[] a,long[] b){ if(a[0] > b[0]) return 1;return -1; } } static class sortInt implements Comparator{ public int compare(int[] a,int[] b){ if(a[1] > b[1]) return 1;return -1; } } public static String[] F(BufferedReader bf) throws Exception { return (bf.readLine().split("" "")); } public static void pr(PrintWriter out,Object o) { out.println(o.toString());//out.flush(); } public static void prW(PrintWriter out,Object o) { out.print(o.toString());//out.flush(); } public static int intIn(String st) { return Integer.parseInt(st); } public static void pr(Object o) { System.out.println(o.toString()); } public static void prW(Object o) { System.out.print(o.toString()); } public static int inInt(String s) { return Integer.parseInt(s); } public static long in(String s) { return Long.parseLong(s); } static int[] toIntArray(String[] m) { int[] p=new int[m.length]; for(int o=0;o 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; } static long __gcd(long n1, long n2) { if(n1==0l) return n2; if(n2==0l) return n1; if(n1==1l || n2==1l) return 1l; // long gcd = 1; if(n1 == n2) return n1; if(n1>n2) return __gcd(n1%n2,n2); return __gcd(n1,n2%n1); } public static int F(String[] arr,char ch){ int[] nums = new int[arr.length]; for(int i=0;i=0;i--){ if((sum+nums[i]) <= 0){ return arr.length-1-i; } sum += nums[i]; } return arr.length; } public static void main (String[] args) throws Exception { BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out);;;// //int[] map=new int[1000001]; int yy=inInt(bf.readLine()); for(int w=0;w y) ? x : y; } int getMid(int s, int e) { return s + (e - s) / 2; } int RMQUtil(int ss, int se, int qs, int qe, int index) { if (qs <= ss && qe >= se) return st[index]; // If segment of this node is outside the given range if (se < qs || ss > qe) return Integer.MIN_VALUE; // If a part of this segment overlaps with the given range int mid = getMid(ss, se); return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1), RMQUtil(mid + 1, se, qs, qe, 2 * index + 2)); } // Return minimum of elements in range from index qs (query // start) to qe (query end). It mainly uses RMQUtil() int RMQ(int n, int qs, int qe) { // Check for erroneous input values return RMQUtil(0, n - 1, qs, qe, 0); } // A recursive function that constructs Segment Tree for // array[ss..se]. si is index of current node in segment tree st int constructSTUtil(int arr[], int ss, int se, int si) { // If there is one element in array, store it in current // node of segment tree and return if (ss == se) { st[si] = arr[ss]; return arr[ss]; } // If there are more than one elements, then recur for left and // right subtrees and store the minimum of two values in this node int mid = getMid(ss, se); st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1), constructSTUtil(arr, mid + 1, se, si * 2 + 2)); return st[si]; } void con(int arr[]) { // Allocate memory for segment tree //Height of segment tree int n = (arr.length); int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); //Maximum size of segment tree int max_size = 2 * (int) Math.pow(2, x) - 1; st = new int[max_size]; // allocate memory // Fill the allocated memory st constructSTUtil(arr, 0, n - 1, 0); } } static class DSU { int[] p;int[] sz;int op;int c;; int[] last; public void G(int n) { last=new int[n]; p=new int[n]; sz=new int[n];c=n; op=n; for(int h=0;hsz[y]) { p[y] = x; sz[x]+=sz[y]; last[x]=Math.max(last[x],last[y]); } else { p[x]=y;sz[y]+=sz[x]; last[y]=Math.max(last[y],last[x]); } c--; }} static long pow(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 0l; // 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; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int gcd(int a, int b,int o) { if (b == 0) return a; return gcd(b, a % b,o); } Geometric median public static double F(double[] x,double[] w) { double d1,d2; double S=0.00; for(double dp : w) S += dp; int k = 0; double sum = S - w[0]; // sum is the total weight of all `x[i] > x[k]` while(sum > S/2) { ++k; sum -= w[k]; } d1=x[k]; return d1; k = w.length-1; sum = S - w[k]; // sum is the total weight of all `x[i] > x[k]` while(sum > S/2) { --k; sum -= w[k]; } d2=x[k]; return new double[]{d1,d2}; } */","import java.lang.reflect.Array; import java.util.*; public class Rough { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-->0) { int n = sc.nextInt(); sc.nextLine(); String s[] = new String[n]; int f[][] = new int[n][5]; for (int i = 0; i < n; i++) { s[i] = sc.nextLine(); for (int j = 0; j < s[i].length(); j++) { f[i][s[i].charAt(j)-'a']++; } } int ans = 0; for ( int i = 0; i < 5; i++) { ArrayList al = new ArrayList<>(); for (int j = 0; j < n; j++) { int o = 0; for (int k = 0; k < 5; k++) { if(k != i) o+=f[j][k]; } al.add(f[j][i]-o); } Collections.sort(al,Collections.reverseOrder()); int max = 0; int x = 0; for (int j = 0; j < n; j++) { x+=al.get(j); if(x<=0)break; max++; } ans = Math.max(max,ans); } System.out.println(ans); } sc.close(); } }",0 05f939b3,e647bef7," //package cf; import java.io.*; import java.util.*; public class D_668 { static int p=1000000007; static int dia=Integer.MIN_VALUE; public static void main(String[] args) throws Exception{ BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), ""ASCII""), 512); FastReader sc=new FastReader(); int t=sc.nextInt(); StringBuilder sb=new StringBuilder(); while(t-->0) { int n=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); int da=sc.nextInt(); int db=sc.nextInt(); dia=Integer.MIN_VALUE; int dp[]=new int[(int) n+1]; List adj[]=new ArrayList[n+1]; for(int i=0;i<=n;i++) { adj[i]=new ArrayList<>(); } Set s=new HashSet<>(); for(int i=1;i=db||2*da>=dia||da>=dp[b]) sb.append(""Alice\n""); else sb.append(""Bob\n""); } System.out.println(sb.toString()); out.flush(); } public static int dfs(int dp[],List adj[],int i,int p,int d) { dp[i]=d; int last_max=0; for(int v:adj[i]) { if(p!=v&&dp[v]==0) { int max=1+dfs(dp,adj,v,i,d+1); dia=Math.max(dia,max+last_max); last_max=Math.max(last_max,max); } } return last_max; } public static int binary_Search_upper(int ar[],int x) { int res=-1; int l=0;int r=ar.length-1; while(l<=r) { int mid=(l+r)>>1; if(ar[mid]==x) { res=mid; l=mid+1; } else if(ar[mid]>x) { r=mid-1; } else { l=mid+1; } } return res; } public static int binary_Search_lower(int ar[],int x) { int res=-1; int l=0;int r=ar.length-1; while(l<=r) { int mid=(l+r)>>1; if(ar[mid]==x) { res=mid; r=mid-1; } else if(ar[mid]>x) { r=mid-1; } else { l=mid+1; } } return res; } int bit[]=new int[(int)1e6]; public void update(int n,int val,int i) { i++; while(i0) { sum+=bit[i]; i-=(i)&(-i); } return sum; } /////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////// static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } "," import java.io.*; import java.util.*; public class Main { private static final boolean N_CASE = true; private List> g; private int a; private int b; private int da; private int db; private int max; private int ab; private int dfs(int u, int fa, int depth) { if (u == a) { ab = depth; } int m1 = 0, m2 = 0; for (int v : g.get(u)) { if (v != fa) { int m = dfs(v, u, depth + 1) + 1; if (m > m1) { m2 = m1; m1 = m; } else if (m > m2) { m2 = m; } } } max = Math.max(max, m1 + m2); int cmax = Math.max(m1, m2); max = Math.max(max, cmax + depth); return cmax; } private void solve() { int n = sc.nextInt(); a = sc.nextInt() - 1; b = sc.nextInt() - 1; da = sc.nextInt(); db = sc.nextInt(); g = createGraph(n); for (int i = 0; i < n - 1; ++i) { int u = sc.nextInt() - 1, v = sc.nextInt() - 1; g.get(u).add(v); g.get(v).add(u); } max = 0; dfs(b, -1, 0); db = Math.min(max, db); boolean win = true; if (ab > da) { if (db > da * 2) { win = false; } } out.println(win ? ""Alice"" : ""Bob""); } private void run() { int T = N_CASE ? sc.nextInt() : 1; for (int t = 0; t < T; ++t) { solve(); } } private static MyWriter out; private static MyScanner sc; private static class MyScanner { BufferedReader br; StringTokenizer st; private MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } int[][] nextIntArray(int n, int m) { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = sc.nextInt(); } } return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } long[][] nextLongArray(int n, int m) { long[][] a = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextLong(); } } return a; } List nextList(int n) { List list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(nextInt()); } return list; } List nextLongList(int n) { List list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(nextLong()); } return list; } char[] nextCharArray(int n) { return sc.next().toCharArray(); } char[][] nextCharArray(int n, int m) { char[][] c = new char[n][m]; for (int i = 0; i < n; i++) { String s = sc.next(); for (int j = 0; j < m; j++) { c[i][j] = s.charAt(j); } } return c; } } private static class MyWriter extends PrintWriter { private MyWriter(OutputStream outputStream) { super(outputStream); } void printArray(int[] a) { for (int i = 0; i < a.length; ++i) { print(a[i]); print(i == a.length - 1 ? '\n' : ' '); } } void printArray(long[] a) { for (int i = 0; i < a.length; ++i) { print(a[i]); print(i == a.length - 1 ? '\n' : ' '); } } void println(int[] a) { for (int v : a) { println(v); } } void print(List list) { for (int i = 0; i < list.size(); ++i) { print(list.get(i)); print(i == list.size() - 1 ? '\n' : ' '); } } void println(List list) { list.forEach(this::println); } } private List> createGraph(int n) { List> g = new ArrayList<>(); for (int i = 0; i < n; ++i) { g.add(new ArrayList<>()); } return g; } private void fill(int[][] a, int value) { for (int[] row : a) { fill(row, value); } } private void fill(int[] a, int value) { Arrays.fill(a, value); } public static void main(String[] args) { out = new MyWriter(new BufferedOutputStream(System.out)); sc = new MyScanner(); new Main().run(); out.close(); } }",0 a4d6775d,cb87df79,"import java.io.*; import java.util.*; public class ArmChairs { public static int solution(int n, int[] arr) { ArrayList one = new ArrayList(); ArrayList zero = new ArrayList(); for (int i = 0; i < n; i++) { if (arr[i] == 1) { one.add(i); } else { zero.add(i); } } int[][] dp = new int[one.size() + 1][zero.size() + 1]; for (int i = 1; i <= one.size(); i++) { dp[i][i] = dp[i - 1][i - 1] + Math.abs(one.get(i - 1) - zero.get(i - 1)); for (int j = i + 1; j <= zero.size(); j++) { dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j - 1] + Math.abs(one.get(i - 1) - zero.get(j - 1))); } } return dp[one.size()][zero.size()]; } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split("" ""); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(s[i]); } log.write(Integer.toString(solution(n, arr)) + ""\n""); log.flush(); } }","import java.util.*; public class Longjumps { public static void main(String[] args){ Scanner sc=new Scanner(System.in); ArrayList o=new ArrayList(), e=new ArrayList(); int n = sc.nextInt(); 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 317a209c,6b97058e,"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 3088ca9c,7a9c69d8,"import java.util.*; import java.io.*; public class MyClass { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t>0) { int n = sc.nextInt(); sc.nextLine(); String [] str = new String[n]; int res = 0; for(int i=0;i=0;p--) { sum+=arr[p]; if(sum>0) { count++; } else { break; } } res=Math.max(count , res); } System.out.println(res); t--; } } }","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 6946f466,ce7027ee,"import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int testCases = sc.nextInt(); for (int t = 0; t < testCases; t++) { int n = sc.nextInt(); int m = sc.nextInt(); char c[][] = new char[n][m]; for (int i = 0; i < n; i++) { String str = sc.next(); c[i] = str.toCharArray(); } int count = 0; StringBuilder ans = new StringBuilder(); for (int i = 0; i < m - 2; i++) { for (int j = 0; j < n; j++) { if (c[j][i] == '0') { continue; } count++; if (j == n - 1) { c[j][i] = '0'; c[j][i + 1] = (c[j][i + 1] == '0' ? '1' : '0'); c[j - 1][i + 1] = (c[j - 1][i + 1] == '0' ? '1' : '0'); ans.append((j + 1) + "" "" + (i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j) + "" "" + (i + 2)) .append(""\n""); continue; } c[j][i] = '0'; c[j][i + 1] = (c[j][i + 1] == '0' ? '1' : '0'); c[j + 1][i + 1] = (c[j + 1][i + 1] == '0' ? '1' : '0'); ans.append((j + 1) + "" "" + (i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 2) + "" "" + (i + 2)) .append(""\n""); } } for (int i = 0; i < n - 2; i++) { for (int j = m - 2; j < m; j++) { if (c[i][j] == '0') { continue; } count++; if (j == m - 1) { c[i][j] = '0'; c[i + 1][j] = (c[i + 1][j] == '0' ? '1' : '0'); c[i + 1][j - 1] = (c[i + 1][j - 1] == '0' ? '1' : '0'); ans.append((i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j)) .append(""\n""); continue; } c[i][j] = '0'; c[i + 1][j] = (c[i + 1][j] == '0' ? '1' : '0'); c[i + 1][j + 1] = (c[i + 1][j + 1] == '0' ? '1' : '0'); ans.append((i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 2)) .append(""\n""); } } ArrayList al = new ArrayList<>(); int count_one = 0; for (int i = n - 2; i < n; i++) { for (int j = m - 2; j < m; j++) { if (c[i][j] == '0') { al.add((i + 1) + "" "" + (j + 1)); } else { count_one++; al.add(0, (i + 1) + "" "" + (j + 1)); } } } if (count_one == 0) { sb.append(count).append(""\n""); sb.append(ans).append(""\n""); continue; } if (count_one == 1) { ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(2)).append(""\n""); ans.append(al.get(0) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(3)).append(""\n""); count = count + 3; sb.append(count).append(""\n""); sb.append(ans).append(""\n""); continue; } if (count_one == 2) { ans.append(al.get(0) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); ans.append(al.get(1) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); count = count + 2; sb.append(count).append(""\n""); sb.append(ans).append(""\n""); continue; } if (count_one == 3) { ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(2)).append(""\n""); count = count + 1; sb.append(count).append(""\n""); sb.append(ans).append(""\n""); continue; } if (count_one == 4) { ans.append(al.get(1) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(2)).append(""\n""); ans.append(al.get(0) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(3)).append(""\n""); count = count + 4; sb.append(count).append(""\n""); sb.append(ans).append(""\n""); continue; } } System.out.print(sb); } } ","import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int testCases = sc.nextInt(); for (int t = 0; t < testCases; t++) { int n = sc.nextInt(); int m = sc.nextInt(); char c[][] = new char[n][m]; for (int i = 0; i < n; i++) { String str = sc.next(); c[i] = str.toCharArray(); } int count = 0; StringBuilder ans = new StringBuilder(); for (int i = 0; i < m - 2; i++) { for (int j = 0; j < n; j++) { if (c[j][i] == '0') { continue; } count++; if (j == n - 1) { c[j][i] = '0'; c[j][i + 1] = (c[j][i + 1] == '0' ? '1' : '0'); c[j - 1][i + 1] = (c[j - 1][i + 1] == '0' ? '1' : '0'); ans.append((j + 1) + "" "" + (i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j) + "" "" + (i + 2)) .append(""\n""); continue; } c[j][i] = '0'; c[j][i + 1] = (c[j][i + 1] == '0' ? '1' : '0'); c[j + 1][i + 1] = (c[j + 1][i + 1] == '0' ? '1' : '0'); ans.append((j + 1) + "" "" + (i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 2) + "" "" + (i + 2)) .append(""\n""); } } for (int i = 0; i < n - 2; i++) { for (int j = m - 2; j < m; j++) { if (c[i][j] == '0') { continue; } count++; if (j == m - 1) { c[i][j] = '0'; c[i + 1][j] = (c[i + 1][j] == '0' ? '1' : '0'); c[i + 1][j - 1] = (c[i + 1][j - 1] == '0' ? '1' : '0'); ans.append((i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j)) .append(""\n""); continue; } c[i][j] = '0'; c[i + 1][j] = (c[i + 1][j] == '0' ? '1' : '0'); c[i + 1][j + 1] = (c[i + 1][j + 1] == '0' ? '1' : '0'); ans.append((i + 1) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 1) + "" "" + (i + 2) + "" "" + (j + 2)) .append(""\n""); } } ArrayList al = new ArrayList<>(); int count_one = 0; for (int i = n - 2; i < n; i++) { for (int j = m - 2; j < m; j++) { if (c[i][j] == '0') { al.add((i + 1) + "" "" + (j + 1)); } else { count_one++; al.add(0, (i + 1) + "" "" + (j + 1)); } } } if (count_one == 0) { sb.append(count).append(""\n""); sb.append(ans).append(""\n""); continue; } if (count_one == 1) { ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(2)).append(""\n""); ans.append(al.get(0) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(3)).append(""\n""); count = count + 3; sb.append(count).append(""\n""); sb.append(ans).append(""\n""); continue; } if (count_one == 2) { ans.append(al.get(0) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); ans.append(al.get(1) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); count = count + 2; sb.append(count).append(""\n""); sb.append(ans).append(""\n""); continue; } if (count_one == 3) { ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(2)).append(""\n""); count = count + 1; sb.append(count).append(""\n""); sb.append(ans).append(""\n""); continue; } if (count_one == 4) { ans.append(al.get(1) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(2)).append(""\n""); ans.append(al.get(0) + "" "" + al.get(2) + "" "" + al.get(3)).append(""\n""); ans.append(al.get(0) + "" "" + al.get(1) + "" "" + al.get(3)).append(""\n""); count = count + 4; sb.append(count).append(""\n""); sb.append(ans).append(""\n""); continue; } } System.out.print(sb); } // } ",1 80881cae,9555b9d1,"//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.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashSet; import java.util.Scanner; import java.util.Set; /** * 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(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, Scanner in, PrintWriter out) { int T = in.nextInt(); while (T-- > 0) { solveOne(in, out); } } private void solveOne(Scanner in, PrintWriter out) { int N = in.nextInt(); int[] k = L.readIntArray(N, in), h = L.readIntArray(N, in); Set toRemove = new HashSet<>(); int prevCost = h[0], prevX = k[0]; for (int i = 1; i < N; i++) { for (int j = i; j < N; j++) { int currCost = h[j], currX = k[j]; if (currX - currCost + 1 <= prevX) { if (prevCost + (currX - prevX) > h[j]) h[j] = prevCost + (currX - prevX); } } prevX = k[i]; prevCost = h[i]; } int nextCost = h[N - 1], nextX = k[N - 1]; for (int i = N - 2; i >= 0; i--) { int currCost = h[i], currX = k[i]; if ((nextCost - currCost) >= (nextX - currX)) { toRemove.add(i); continue; } nextCost = currCost; nextX = currX; } long cost = 0; for (int i = N - 1; i >= 0; i--) if (!toRemove.contains(i)) cost += (h[i] * ((long) h[i] + 1)) / 2; out.println(cost); } } static class L { public static int[] readIntArray(int size, Scanner in) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = in.nextInt(); } return array; } } } ",0 24afd00e,810fd282,"/* JAI MATA DI */ import java.util.*; 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 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(); lr = new long[n][2]; for(int i =0 ; i < n ; i ++) { lr[i][0] = sc.nextLong(); lr[i][1] = sc.nextLong(); } adj = new ArrayList>(); for(int i = 0 ; i ()); for(int i = 0 ; i> adj; static void dfs(int u , int p ) { for(int child:adj.get(u)) { if(child == p) continue; dfs(child , u); } long left = lr[u][0] , right = lr[u][1]; long ansl = 0 , ansr = 0; for(int child:adj.get(u)) { if(child == p) continue; long leftc = lr[child][0] , rightc = lr[child][1]; ansl += Math.max( min[child] + Math.abs(left - leftc) , max[child] +Math.abs(left - rightc) ); } for(int child:adj.get(u)) { if(child == p) continue; long leftc = lr[child][0] , rightc = lr[child][1]; ansr += Math.max( min[child] + Math.abs(right - leftc) , max[child] +Math.abs(right - rightc) ); } min[u] = ansl; max[u] = ansr; } } ","import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; public class Main { static int N=(int)2e5+10; static long[][] dp=new long[2][N]; static int[][] A=new int[2][N]; static ArrayList> adj=new ArrayList<>(N); static { for(int i=0;i()); } public static void dfs(int v,int p) { dp[0][v]=dp[1][v]=0; for(Integer u:adj.get(v)) { if(u==p) continue; dfs(u, v); dp[0][v]+=Math.max(Math.abs(A[0][v]-A[1][u])+dp[1][u], dp[0][u]+Math.abs(A[0][v]-A[0][u])); dp[1][v]+=Math.max(Math.abs(A[1][v]-A[1][u])+dp[1][u], dp[0][u]+Math.abs(A[1][v]-A[0][u])); } } 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) { int n=Integer.parseInt(br.readLine()); for(int i=1;i<=n;i++) { String[] ss1=br.readLine().split("" ""); A[0][i]=Integer.parseInt(ss1[0]); A[1][i]=Integer.parseInt(ss1[1]); adj.set(i, new ArrayList<>()); } for(int i=1;i lt1=new ArrayList<>(); ArrayList lt0=new ArrayList<>(); for(int i=0;i() { @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; } } static int solve(int dp[][] , ArrayList one , ArrayList zero , int i,int j) { int n=one.size(); int m=zero.size(); if(i>=n) return 0; else if(j>=m) return Integer.MAX_VALUE; else if(dp[i][j]!=-1) return dp[i][j]; int val1 = Math.abs(one.get(i)-zero.get(j))+solve(dp, one ,zero ,i+1,j+1); int val2 = solve(dp , one , zero , i,j+1); dp[i][j]= Math.min(val1,val2); return dp[i][j]; } public static void main(String args[]) throws Exception { Scanner sc = new Scanner(); StringBuffer res = new StringBuffer(); int tc = 1; while(tc-->0) { int n = sc.nextInt(); ArrayList one = new ArrayList<>(); ArrayList zero = new ArrayList<>(); 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 a[] = new int[n]; int b[] = new int[n]; for(int i=0;i=1;i--){ if(arr[i]==0){ if(prev==Integer.MAX_VALUE-1000){ dp[i]=Math.min(prev,dp[i]); } else{ prev=prev+1; dp[i]=Math.min(prev,dp[i]); } } else{ prev=Math.min(prev+1,arr[i]); dp[i]=Math.min(prev,dp[i]); } } for(int i=1;i<=n;i++){ System.out.print(dp[i]+"" ""); } System.out.println(); } public static void main (String[] args) throws java.lang.Exception{ scan=new FastReader(); int t=scan.nextInt(); while(t-->0){ solve(); } } 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{ long wt; long idx; Pair(long x,long y){ this.wt=x; this.idx=y; } @Override public int compareTo(Pair x){ return (int)(this.wt-x.wt); } public String toString(){ return ""( ""+wt+"" ""+idx+"" )""; } } static void printLong(long []arr){ for(long x:arr)System.out.print(x+"" ""); } static void printInt(int []arr){ for(int x:arr)System.out.print(x+"" ""); } static void scanInt(int []arr){ for(int i=0;i 0){ if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } static long add(long a,long b,long mod){ a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod; } static long sub(long a, long b,long mod){ a = a % mod; b = b % mod; return (((a - b) % mod) + mod) % mod; } static long mul(long a, long b,long mod){ a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod; } static long mminvprime(long a, long b,long mod) { return power(a, b - 2,mod); } } "," 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.Collections; import java.util.HashSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.Vector; public class Main { static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(outputStream); public static void main(String[] args) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int k = in.nextInt(); int[] v1 = new int[k]; int[] v2 = new int[k]; for (int i = 0; i < k; i++) { v1[i] = in.nextInt(); } for (int i = 0; i < k; i++) { v2[i] = in.nextInt(); } //wejhfduiwehiofhw int[] res = new int[n + 2]; Arrays.fill(res, 2000000000); for (int i = 0; i < k; i++) { res[v1[i]] = v2[i]; } for (int i = 1; i <= n; i++) { int val = Math.min(res[i], res[i - 1] + 1); res[i] = val; } //ewhfowiejp //wedhciuwahidochqowi //wjdhoiqwnlidhqw for (int i = n; i >= 1; i--) { int val1 = Math.min(res[i], res[i + 1] + 1); res[i] = val1; // out.println(res[i]); } for (int i = 1; i <= n; i++) { int r = res[i]; out.print(r + "" ""); } out.println(); } out.close(); } private static void takeinput(int[] arr, int k) { for (int i = 0; i < k; i++) { arr[i] = in.nextInt(); } } 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 long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public String nextLine() throws IOException { return reader.readLine().trim(); } } } ",0 949502c2,9fc811f7,"import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class Template { static int mod = 1000000007; public static void main(String[] args) { FastScanner sc = new FastScanner(); int yo = sc.nextInt(); while (yo-- > 0) { int n = sc.nextInt(); int a = sc.nextInt()-1; int b = sc.nextInt()-1; int da = sc.nextInt(); int db = sc.nextInt(); List> list = new ArrayList<>(); for(int i = 0; i < n; i++) list.add(new ArrayList<>()); for(int i = 0; i < n-1; i++){ int x = sc.nextInt()-1; int y = sc.nextInt()-1; list.get(x).add(y); list.get(y).add(x); } for(int i = 0; i <= n; i++) depth[i] = 0; diam = 0; dfs(a,-1,list); if(2 * da >= min(diam, db) || depth[b] <= da){ out.println(""Alice""); } else { out.println(""Bob""); } } } static int[] depth = new int[200001]; static int diam = 0; static int dfs(int x, int p, List> list) { int len = 0; List ne = list.get(x); for(int y : ne) { if(y != p) { depth[y] = depth[x] + 1; int cur = 1 + dfs(y, x,list); diam = max(diam, cur + len); len = max(len, cur); } } return len; } public static class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } public static void sort(int[] arr) { ArrayList ls = new ArrayList(); for (int x : arr) ls.add(x); Collections.sort(ls); for (int i = 0; i < arr.length; i++) arr[i] = ls.get(i); } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] sieve(int N) { boolean[] sieve = new boolean[N + 1]; for (int i = 2; i <= N; i++) sieve[i] = true; for (int i = 2; i <= N; i++) { if (sieve[i]) { for (int j = 2 * i; j <= N; j += i) { sieve[j] = false; } } } return sieve; } public static long power(long x, long y, long p) { long res = 1L; x = x % p; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; } return res; } public static void print(int[] arr) { //for debugging only for (int x : arr) out.print(x + "" ""); out.println(); } static class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] readInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] readLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] readDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } // For Input.txt and Output.txt // FileInputStream in = new FileInputStream(""input.txt""); // FileOutputStream out = new FileOutputStream(""output.txt""); // PrintWriter pw = new PrintWriter(out); // Scanner sc = new Scanner(in); }","import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DTreeTag solver = new DTreeTag(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class DTreeTag { int diam = 0; public int dfs(ArrayList g[], int x, int depth[], int p) { int len = 0; for (int y : g[x]) { if (y != p) { depth[y] = depth[x] + 1; int cur = 1 + dfs(g, y, depth, x); diam = Math.max(diam, cur + len); len = Math.max(len, cur); } } return len; } public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int a = in.nextInt() - 1; int b = in.nextInt() - 1; int da = in.nextInt(); int db = in.nextInt(); int dis[] = new int[n]; ArrayList g[] = new ArrayList[n]; for (int i = 0; i < n; i++) g[i] = new ArrayList<>(); for (int i = 0; i < n - 1; i++) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; g[u].add(v); g[v].add(u); } diam = 0; dfs(g, a, dis, -1); int disb = dis[b]; if (2 * da >= Math.min(diam, db) || disb <= da) { out.println(""Alice""); } else { out.println(""Bob""); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } } ",1 4ea10951,66e74577,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static String f1(String s, int index) { StringBuffer sb = new StringBuffer(); for(int i =0;i 0) { int n = Integer.parseInt(br.readLine()); String s1 = br.readLine(); String s2 = br.readLine(); long res = solve(s1, s2); for(int i=0;i0) { int n = fr.ni(); long arr [] = new long[n]; for(int i = 0 ; i < n ; i++) arr[i]= fr.nl(); long even = arr[0]; long odd = arr[1]; long minEven = arr[0]; long minOdd = arr[1]; long ans = (minEven*n) + (minOdd*n); for(int i = 2 ; i < n ; i++) { if((i&1) == 0) { even += arr[i]; minEven = Math.min(minEven, arr[i]); long a = (i+2)/2; long b = (i+1)-a; long temp = (even + (minEven*(n - a))); temp += (odd + (minOdd*(n - b))); ans = Math.min(ans, temp); }else { odd += arr[i]; minOdd = Math.min(minOdd, arr[i]); long a = (i+2)/2; long b = (i+1)-a; long temp = (even + (minEven*(n - a))); temp += (odd + (minOdd*(n - b))); ans = Math.min(ans, temp); } } out.println(ans); } out.close(); } }","/* * akshaygupta26 */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.Random; import java.util.Arrays; import java.util.StringTokenizer; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Collections; import java.util.*; public class C { public static void main(String[] args) { FastReader sc=new FastReader(); StringBuffer ans=new StringBuffer(); int test=sc.nextInt(); while(test-->0) { int n=sc.nextInt(); long arr[]=new long[n]; for(int i=0;i0;tt--){ //int n = ni(); int n = ni(); int m =ni(); int x =ni(); int[] a = new int[n]; for(int i=0;i pq = new PriorityQueue<>(); for(int i=1;i<=m;i++) pq.add(new Pair(i)); out.println(""YES""); for(int i:a){ Pair r = pq.remove(); r.sum+=i; pq.add(r); out.print(r.id+"" ""); } out.println(); } } class Pair implements Comparable{ int id; long sum=0; public Pair(int i){ this.id=i; } @Override public int compareTo(Pair o) { return Long.compare(this.sum, o.sum); } } boolean issubset(int[] a,int k){ int n = a.length; boolean[][]dp = new boolean[n+1][k+1]; for(int i=0;i<=n;i++){ for(int j=0;j<=k;j++){ if(i==0) {dp[i][j] = false;continue;} if(j==0){dp[i][j]= true; continue;} if(a[i-1]<=j){ dp[i][j] = dp[i][j-a[i-1]] || dp[i-1][j]; }else{ dp[i][j] = dp[i-1][j]; } } } return dp[n][k]; } void ruffleSort(int[] a) { //ruffle int n=a.length; Random r=new Random(); for (int i=0; i al = new ArrayList<>(); for(int i=0;i use expo to calc -->(a^(mod-2)) */ long expo(long p,long q) /* (p^q)%mod */ { long z = 1; while (q>0) { if (q%2 == 1) { z = (z * p)%mod; } p = (p*p)%mod; q >>= 1; } return z; } void run()throws Exception { in=System.in; out = new PrintWriter(System.out); solve(); out.flush(); } private 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++]; } private int ni() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); int sgn = 1; if (c == '-') { sgn = -1; c = scan(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = scan(); } while (!isSpaceChar(c)); return res * sgn; } private long nl() throws IOException { long num = 0; int b; boolean minus = false; while ((b = scan()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = scan(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = scan(); } } private double nd() throws IOException{ return Double.parseDouble(ns()); } private String ns() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = scan(); } while (!isSpaceChar(c)); return res.toString(); } private String nss() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } private char nc() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); return (char) c; } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } private boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhiteSpace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } ","//Implemented By Aman Kotiyal Date:-02-May-2021 Time:-7:58:39 pm import java.io.*; import java.util.*; public class ques3 { public static void main(String[] args)throws Exception{ new ques3().run();} long mod=1000000000+7; void solve() throws Exception { for(int ii=ni();ii>0;ii--) { int n=ni(); int m=ni(); int x=ni(); long h[]=new long[n]; for (int i = 0; i pq=new PriorityQueue(new Comparator() { public int compare(long[] x, long[] y) { if(x[1]-y[1]>0)return 1; if(x[1]-y[1]<0)return -1; return 0; } }); for (int i = 1; i <=m; i++) pq.add(new long[] {i,0}); for(int i=0;ix) { out.println(""NO""); } else { out.println(""YES""); for (int i = 0; i < dp.length; i++) { out.print(dp[i]+"" ""); } out.println(); } } } /*FAST INPUT OUTPUT & METHODS BELOW*/ private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; private SpaceCharFilter filter; PrintWriter out; int min(int... ar){int min=Integer.MAX_VALUE;for(int i:ar)min=Math.min(min, i);return min;} long min(long... ar){long min=Long.MAX_VALUE;for(long i:ar)min=Math.min(min, i);return min;} int max(int... ar) {int max=Integer.MIN_VALUE;for(int i:ar)max=Math.max(max, i);return max;} long max(long... ar) {long max=Long.MIN_VALUE;for(long i:ar)max=Math.max(max, i);return max;} void reverse(int a[]){for(int i=0;i>1;i++){int tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}} 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;}} String reverse(String s){StringBuilder sb=new StringBuilder(s);sb.reverse();return sb.toString();} void shuffle(int a[]) { ArrayList al = new ArrayList<>(); for(int i=0;i use expo to calc -->(a^(mod-2)) */ long expo(long p,long q) /* (p^q)%mod */ { long z = 1; while (q>0) { if (q%2 == 1) { z = (z * p)%mod; } p = (p*p)%mod; q >>= 1; } return z; } void run()throws Exception { in=System.in; out = new PrintWriter(System.out); solve(); out.flush(); } private 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++]; } private int ni() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); int sgn = 1; if (c == '-') { sgn = -1; c = scan(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = scan(); } while (!isSpaceChar(c)); return res * sgn; } private long nl() throws IOException { long num = 0; int b; boolean minus = false; while ((b = scan()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = scan(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = scan(); } } private double nd() throws IOException{ return Double.parseDouble(ns()); } private String ns() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = scan(); } while (!isSpaceChar(c)); return res.toString(); } private String nss() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } private char nc() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); return (char) c; } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } private boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhiteSpace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } ",0 6f02c6d9,ff34fab2," 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.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; j0) { int n = sc.nextInt(), k = sc.nextInt(); int[] a = sc.nextIntArray(k); int[] t = sc.nextIntArray(k); int[] array = new int[n]; Arrays.fill(array, Integer.MAX_VALUE); for(int i = 0; i < k; i++) array[a[i] - 1] = t[i]; int[] pre = new int[n]; int[] post = new int[n]; int prev = (int)2e9; for(int i = 0; i < n; i++) prev = pre[i] = Math.min(prev + 1, array[i]); prev = (int)2e9; for(int i = n - 1; i >= 0; i--) prev = post[i] = Math.min(prev + 1, array[i]); for(int i = 0; i < n; i++) array[i] = Math.min(pre[i], post[i]); for(int i = 0; i < n; i++) pw.print(array[i] + (i == n - 1 ? ""\n"" : "" "")); } pw.flush(); } public static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) array[i] = new Integer(nextInt()); return array; } public long[] nextLongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public double[] nextDoubleArray(int n) throws IOException { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } public static int[] shuffle(int[] a) { int n = a.length; Random rand = new Random(); for (int i = 0; i < n; i++) { int tmpIdx = rand.nextInt(n); int tmp = a[i]; a[i] = a[tmpIdx]; a[tmpIdx] = tmp; } return a; } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } } ","import java.util.*; public class file { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t!=0) { sc.nextLine(); int n = sc.nextInt(); int k = sc.nextInt(); int[] position = new int[k]; for(int i = 0;i < k;i++) { position[i] = sc.nextInt(); } int[] temprature = new int[k]; for(int i = 0;i < k;i++) { temprature[i] = sc.nextInt(); } int[] left = new int[n]; int[] right = new int[n]; int c[] = new int[n]; Arrays.fill(c, Integer.MAX_VALUE); for(int i = 0;i < k;i++) { c[position[i]-1] = temprature[i]; } long min = Integer.MAX_VALUE - 1; for(int i = 0;i < n;i++) { min = Math.min(min+1, c[i]); left[i] = (int)min; } min = Integer.MAX_VALUE - 1; for(int i = n-1;i >= 0;i--) { min = Math.min(min+1, c[i]); right[i] = (int)min; } for(int i = 0;i < n;i++) { System.out.print(Math.min(left[i], right[i])+"" ""); } System.out.println(); t--; } } }",0 01c915a2,480de7be," /* TO LEARN 1-segment trees 2-euler tour */ /* TO SOLVE uva 1103 codeforces 722 kavi on pairing duty */ /* 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.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 final long p=29; static final long mod=(long)1e9+9; static int n,m; static String spotty[],plain[]; static boolean check(int l) { Map>hashes=new HashMap>(); HashSethashes2=new HashSet(); long pow_p[]=new long[m]; pow_p[0]=1; for(int i=1;imap=new HashMap(); for(int i=1;i<=333333334;i++) { map.put(i+2*i,new Pair(i,2*i)); map.put(i+(2*(i+1)),new Pair(i,i+1)); map.put(i+1+(2*i),new Pair(i+1,i)); }*/ outer:while(tt-->0) { int n=scan.nextInt(); int cnt[][]=new int[n][5]; String arr[]=new String[n]; int sumlens=0; for(int i=0;itmp=new ArrayList(); int sumall=0; for(int i=0;iTHAT) { // if(to==3) // out.println(""AHA ""+(n-i)); res=Math.max(res,n-i); break; } THIS-=tmp.get(i).x; THAT-=tmp.get(i).y; } } out.println(res); } out.close(); } static class special{ boolean bool; int n; //int id; special(boolean bool,int n) { this.bool=bool; this.n=n; } @Override public int hashCode() { int hash = 7; hash = 31 * hash + (int) n; return hash; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; special t = (special)o; return t.bool == bool && t.n == n; } } static long binexp(long a,long n) { if(n==0) return 1; long res=binexp(a,n/2); if(n%2==1) return res*res*a; else return res*res; } static long powMod(long base, long exp, long mod) { if (base == 0 || base == 1) return base; if (exp == 0) return 1; if (exp == 1) return (base % mod+mod)%mod; long R = (powMod(base, exp/2, mod) % mod+mod)%mod; R *= R; R %= mod; if ((exp & 1) == 1) { return (base * R % mod+mod)%mod; } else return (R %mod+mod)%mod; } static double dis(double x1,double y1,double x2,double y2) { return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } static long mod(long x,long y) { if(x<0) x=x+(-x/y+1)*y; return x%y; } public static long pow(long b, long e) { long r = 1; while (e > 0) { if (e % 2 == 1) r = r * b ; b = b * b; e >>= 1; } return r; } private static void sort(long[] arr) { List list = new ArrayList<>(); for (long object : arr) list.add(object); Collections.sort(list); //Collections.reverse(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } private static void sort2(long[] arr) { List list = new ArrayList<>(); for (Long object : arr) list.add(object); Collections.sort(list); Collections.reverse(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } static class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } static class Pair implements Comparable{ public long x, y,z; public Pair(long x1, long y1,long z1) { x=x1; y=y1; z=z1; } public Pair(long x1, long y1) { x=x1; y=y1; } @Override public int hashCode() { return (int)(x + 31 * y); } public String toString() { return x + "" "" + y+"" ""+z; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; Pair t = (Pair)o; return t.x == x && t.y == y&&t.z==z; } public int compareTo(Pair o) { return (int)((o.y-o.x)-(y-x)); } } } ","import java.util.StringTokenizer; import java.io.*; public class CF_1551c{ public static final void main(String[] args){ Kattio io= new Kattio(); int t= io.getInt(); while(t-->0){ int n= io.getInt(); int[][] ps= new int[5][n]; for(int i=0; imax) max= i; } } io.println(max); } io.close(); } // using mergeSort to avoid Java quicksort TLE hacks static void mergeSort(int arr[]){ int n= arr.length; for (int sz= 1; sz<=n-1; sz=2*sz){ for (int l= 0; l 0) { int n = sc.nextInt(); int[] h = new int[n]; int i, b = MAX, e = MIN; for (i = 0; i < n; i++) { h[i] = sc.nextInt(); if (b > h[i]) b = h[i]; if (e < h[i]) e = h[i]; } int[] s = new int[n]; int c, d, ans = b; // System.out.println(b + "" "" + e); while (b <= e) { c = (b + e) / 2; // System.out.println(c); // sc.next(); for (i = 0; i < n; i++) { s[i] = 0; } for (i = n - 1; i >= 2; i--) { if (h[i] + s[i] < c) { e = c - 1; break; } else { d = Math.min(h[i], h[i] + s[i] - c) / 3; s[i - 1] += d; s[i - 2] += 2 * d; } } // System.out.println(i); if (i == 1) { if (h[i] + s[i] < c || h[i - 1] + s[i - 1] < c) { e = c - 1; } else { ans = c; b = c + 1; } } } System.out.println(ans); } } }","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 2e1109d7,e7000ac2,"import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DArmchairs solver = new DArmchairs(); solver.solve(1, in, out); out.close(); } static class DArmchairs { ArrayList[] arr; long[][] dp; public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); int[] a = in.readArray(n); int mx = 5001; dp = new long[mx][mx]; for (int i = 0; i < mx; ++i) { Arrays.fill(dp[i], -1); } arr = new ArrayList[2]; for (int i = 0; i < 2; ++i) { arr[i] = new ArrayList<>(); } for (int i = 0; i < n; ++i) { arr[a[i]].add(i); } out.println(go(0, 0)); } long go(int i, int j) { if (i == arr[1].size()) return 0; if (j == arr[0].size()) return (long) 1e9; if (dp[i][j] != -1) return dp[i][j]; long pick = Math.abs(arr[0].get(j) - arr[1].get(i)) + go(i + 1, j + 1); long leave = go(i, j + 1); dp[i][j] = Math.min(leave, pick); return dp[i][j]; } } 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } } ","import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DArmchairs solver = new DArmchairs(); solver.solve(1, in, out); out.close(); } static class DArmchairs { ArrayList[] arr; long[][] dp; public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); int[] a = in.readArray(n); int mx = 5001; dp = new long[mx][mx]; for (int i = 0; i < mx; ++i) { Arrays.fill(dp[i], -1); } arr = new ArrayList[2]; for (int i = 0; i < 2; ++i) { arr[i] = new ArrayList<>(); } for (int i = 0; i < n; ++i) { arr[a[i]].add(i); } out.println(go(0, 0)); } long go(int i, int j) { if (i == arr[1].size()) return 0; if (j == arr[0].size()) return (long) 1e9; if (dp[i][j] != -1) return dp[i][j]; long pick = Math.abs(arr[0].get(j) - arr[1].get(i)) + go(i + 1, j + 1); long leave = go(i, j + 1); dp[i][j] = Math.min(leave, pick); return dp[i][j]; } } 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } } ",1 1097b326,a60fba84,"// package com.company; import java.util.Scanner; public class Main { public static void solution1(){ Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- != 0){ int n, k1, k2, w, b; n = scanner.nextInt(); k1 = scanner.nextInt(); k2 = scanner.nextInt(); w = scanner.nextInt(); b = scanner.nextInt(); if (k1 < k2){ int temp = k1; k1 = k2; k2 = temp; } int white_dominoes = k2 + (k1 - k2) / 2; int black_dominoes = (n - k1) + (k1 - k2) / 2; if (white_dominoes >= w && black_dominoes >= b) { System.out.println(""YES""); } else System.out.println(""NO""); } scanner.close(); } public static void solution2(){ Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); scanner.nextLine(); while (t-- != 0) { String s; s = scanner.nextLine(); boolean flag = true; int first_ones = -1, last_zeroes = -1; for (int i=0; i0; i--){ if (s.charAt(i) == '0' && s.charAt(i-1) == '0'){ last_zeroes = i-1; break; } } if (first_ones != -1 && last_zeroes != -1 && first_ones < last_zeroes) flag = false; if (flag) System.out.println(""YES""); else System.out.println(""NO""); } scanner.close(); } public static void solution3(){ Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0){ int n; n = scanner.nextInt(); int[] costs = new int[n]; for (int i=0; i 0){ if (b%2 == 1){ ans *= a; } a *= a; b >>= 1; } return ans; } public static void main(String[] args) { solution3(); } }","/*input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 */ import java.util.*; import java.lang.*; import java.io.*; public class Main { static PrintWriter out; static int MOD = 1000000007; static FastReader scan; /*-------- I/O using short named function ---------*/ public static String ns(){return scan.next();} public static int ni(){return scan.nextInt();} public static long nl(){return scan.nextLong();} public static double nd(){return scan.nextDouble();} public static String nln(){return scan.nextLine();} public static void p(Object o){out.print(o);} public static void ps(Object o){out.print(o + "" "");} public static void pn(Object o){out.println(o);} /*-------- for output of an array ---------------------*/ static void iPA(int arr []){ StringBuilder output = new StringBuilder(); for(int i=0; i0){ int n = ni(); long arr[] = new long[n]; lIA(arr); long ans = (long)(n*(arr[0] + arr[1])); long sum = arr[0] + arr[1]; long emin = arr[0], omin = arr[1]; for(int i=2; i one = new ArrayList(); ArrayList zero = new ArrayList(); for (int i = 0; i < n; i++) { if (arr[i] == 1) { one.add(i); } else { zero.add(i); } } int[][] dp = new int[one.size() + 1][zero.size() + 1]; for (int i = 1; i <= one.size(); i++) { dp[i][i] = dp[i - 1][i - 1] + Math.abs(one.get(i - 1) - zero.get(i - 1)); for (int j = i + 1; j <= zero.size(); j++) { dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j - 1] + Math.abs(one.get(i - 1) - zero.get(j - 1))); } } return dp[one.size()][zero.size()]; } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split("" ""); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(s[i]); } log.write(Integer.toString(solution(n, arr)) + ""\n""); log.flush(); } } ","import java.util.*; public class MyClass { public static void main(String args[]) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int a[]=new int[n]; ArrayList lt1=new ArrayList<>(); ArrayList lt0=new ArrayList<>(); for(int i=0;i 0) { int n = 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.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class JaiShreeRam{ static Scanner in=new Scanner(); static long mod = 1000000007; static ArrayList> adj; public static void main(String[] args) throws Exception{ class Element{ int x; char c; public Element(int y,char k) { x=y; c=k; } } int z=in.readInt(); while(z-->0) { int n=in.readInt(); int a[]=nia(n); char c[]=in.readString().toCharArray(); ArrayList d=new ArrayList<>(); ArrayList in=new ArrayList<>(); for(int i=0;i=0;i--) { if(in.get(i)>n) { ans=""NO""; break; } n--; } System.out.println(ans); } } static long ans(long l,long r,long x,long y) { long mid=(r-l)/2+l; long a=mid%x; long b=y%mid; long c=0; if(l>r) { return 0; } if(a==b) { return mid; } else if(ares; public static void main(String[] args) { Scanner input = new Scanner(System.in); int test = input.nextInt(); while(test-->0){ int n = input.nextInt(); int m = input.nextInt(); char arr[][] = new char[n][m]; res = new ArrayList<>(); for(int i =0;i= 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 String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(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; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int mod = (int)(1e9+7); public static long pow(long a,long b) { long ans = 1; while(b> 0) { if((b & 1)==1){ ans = (ans*a) % mod; } a = (a*a) % mod; b = b>>1; } return ans; } static void sort(int[] a) { ArrayList l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i0) { // out.printLine(""UAShd""); int n = in.nextInt(),m = in.nextInt(); char[][] arr = new char[n][m]; for(int i=0;i0) // res.deleteCharAt(res.length()-1); out.printLine(nops); if(nops>0) out.print(res); } out.flush(); out.close(); } }",0 3dd65549,f229aa7f,"import java.util.*; import java.io.*; public class codeforces { public static void main(String[] args) throws Exception { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); char[] a=sc.next().toCharArray(); char[] b=sc.next().toCharArray(); int e0=0; int e1=0; int o0=0; int o1=0; for(int i=0;i { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + "" "" + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + "" "" + y + "" "" + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }","import java.util.*; import java.io.*; import java.math.*; public class cf { static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); char[] a = sc.next().toCharArray(); char[] b = sc.next().toCharArray(); int x = 0, y = 0, lit = 0,lit2 = 0; for (int i = 0; i < n; i++) { if (a[i] == '1') lit++; if (b[i] == '1') lit2++; if (a[i] == b[i]) x++; else y++; } if(lit == lit2 || n - lit + 1 == lit2) { if (lit == lit2 && n - lit + 1 == lit2) { pw.println(Math.min(x,y)); }else if(lit == lit2) { pw.println(y); }else { pw.println(x); } }else { pw.println(-1); } } pw.close(); } public static class tuble implements Comparable { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + "" "" + y + "" "" + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) return this.z - other.z; return this.y - other.y; } else { return this.x - other.x; } } } public static class pair implements Comparable { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + "" "" + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Integer(x).hashCode() * 31 + new Integer(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }",0 44428e63,9debf95c,"import java.util.*; import java.io.*; public class C_Minimum_Grid_Path{ public static void main(String[] args) { FastScanner s= new FastScanner(); StringBuilder res = new StringBuilder(); int t=s.nextInt(); int p=0; while(p 0) { if ((p & 1) > 0) { ans = (ans * a) % mod; } a = (a * a) % mod; p = p >> 1; } return ans % mod; } static long mod = 1000000007; static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } static long fast_powerNumbers(long a, long n) { if (n == 1) { return a; } long ans = fast_powerNumbers(a, n / 2); if (n % 2 == 0) { return (ans * ans); } else { return ((ans * ans) * (a)); } } static void dfs_helper(int[][] arr, int i, int j, int team, int n, int m) { arr[i][j] = team; if (i - 1 >= 0 && arr[i - 1][j] == 1) { dfs(arr, i - 1, j, team, n, m); } if (j - 1 >= 0 && arr[i][j - 1] == 1) { dfs(arr, i, j - 1, team, n, m); } if (i + 1 < n && arr[i + 1][j] == 1) { dfs(arr, i + 1, j, team, n, m); } if (j + 1 < m && arr[i][j + 1] == 1) { dfs(arr, i, j + 1, team, n, m); } } static void dfs(int[][] arr, int i, int j, int team, int n, int m) { dfs_helper(arr, i, j, team, n, m); } static int parent[]; static int rank[]; static int find(int i) { if (parent[i] == -1) { parent[i] = i; return i; } if (parent[i] == i) { return i; } else { parent[i] = find(parent[i]); } return parent[i]; } static void unite(int s1, int s2) { if (rank[s1] > rank[s2]) { parent[s2] = s1; rank[s1] += rank[s2]; } else { parent[s1] = s2; rank[s2] += rank[s1]; } } public static long arr[]; public static int arr1[]; // static void seive(int n) { // arr = new int[n + 1]; // arr[0] = arr[1] = 1; // for (int i = 4; i <= n; i = i + 2) { // arr[i] = 1; // } // for (int i = 3; i * i <= n; i = i + 2) { // if (arr[i] == 0) { // for (int j = i * i; j <= n; j = j + i) { // arr[j] = 1; // } // // } // } // } static void seive(int n) { arr1=new int[n+1]; arr1[0]=arr1[1]=1; for (int i = 4; i <=n ; i+=2) { arr1[i]=1; } for (int i = 3; i*i <=n ; i+=2) { if (arr1[i] == 0) { for (int j = i*i; j <=n ; j+=i) { arr1[j]=1; } } } } public static boolean ccw(Point a,Point b,Point c) { long s1=(c.x-b.x)*(b.y-a.y) ; long s2 = (c.y-b.y)*(b.x-a.x); if(s1rank[p2]) { parent[p2]=p1; rank[p1]+=rank[p2]; // System.out.println(arr[p2]); sum[p1]+=sum[p2]; //sum[p2]+=sum[p1]; } else { parent[p1]=p2; rank[p2]+=rank[p1]; //System.out.println(arr[p1]); sum[p2]+=sum[p1]; //sum[p1]+=sum[p2]; } } } class Gaph { HashMap> hm; Gaph() { hm = new HashMap<>(); } Gaph(int n) { hm = new HashMap<>(); for (int i = 0; i < n; i++) { hm.put(i, new ArrayList()); } } // function for adding an edge................................................. public void addEdge(int a, int b, boolean isDir) { if (isDir) { if (hm.containsKey(a)) { hm.get(a).add(b); } else { hm.put(a, new ArrayList<>(Arrays.asList(b))); } } else { if (hm.containsKey(a)) { hm.get(a).add(b); } else if (!hm.containsKey(a)) { hm.put(a, new ArrayList<>(Arrays.asList(b))); } if (hm.containsKey(b)) { hm.get(b).add(a); } else if (!hm.containsKey(b)) { hm.put(b, new ArrayList<>(Arrays.asList(a))); } } } } // out.println(al.toString().replaceAll(""[\\[|\\]|,]"","""")); ",0 18e2441c,43b10dec," 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) { solve(); } } static void solve() { int n = ni(); int m = ni(), x = ni(); Map> map = new TreeMap<>(); pl(""YES""); for(int i = 0; i < n; i++) { int num = ni(); if(!map.containsKey(num)) { map.put(num, new ArrayList()); } map.get(num).add(i); } int[] ans = new int[n]; int q = 0; for(int block : map.keySet()) { for(int i = 0; i < map.get(block).size(); i++) { ans[map.get(block).get(i)] = (q++)%m+1; } } for(int ele : ans) { p(ele + "" ""); } pl(); } ///////////////////////////////////////////////////////////////////////////////// static int[] nextIntArray(int n) { int[] arr = new int[n]; int i = 0; while (i < n) { arr[i++] = ni(); } return arr; } static long[] nextLongArray(int n) { long[] arr = new long[n]; int i = 0; while (i < n) { arr[i++] = nl(); } return arr; } static int[] nextIntArray1(int n) { int[] arr = new int[n + 1]; int i = 1; while (i <= n) { arr[i++] = ni(); } return arr; } ///////////////////////////////////////////////////////////////////////////////// static int ni() { return sc.nextInt(); } static long nl() { return sc.nextLong(); } static double nd() { return sc.nextDouble(); } ///////////////////////////////////////////////////////////////////////////////// static void pl() { pw.println(); } static void p(Object o) { pw.print(o); } static void pl(Object o) { pw.println(o); } static void psb(StringBuilder sb) { pw.print(sb); } static void pa(Object arr[]) { for (Object o : arr) { p(o); } pl(); } static void pa(int arr[]) { for (int o : arr) { p(o); } pl(); } static void pa(long arr[]) { for (long o : arr) { p(o); } pl(); } static void pa(double arr[]) { for (double o : arr) { p(o); } pl(); } static void pa(char arr[]) { for (char o : arr) { p(o); } pl(); } static void pa(List list) { for (Object o : list) { p(o); } pl(); } static void pa(Object[][] arr) { for (int i = 0; i < arr.length; ++i) { for (Object o : arr[i]) { p(o); } pl(); } } static void pa(int[][] arr) { for (int i = 0; i < arr.length; ++i) { for (int o : arr[i]) { p(o); } pl(); } } static void pa(long[][] arr) { for (int i = 0; i < arr.length; ++i) { for (long o : arr[i]) { p(o); } pl(); } } static void pa(char[][] arr) { for (int i = 0; i < arr.length; ++i) { for (char o : arr[i]) { p(o); } pl(); } } static void pa(double[][] arr) { for (int i = 0; i < arr.length; ++i) { for (double o : arr[i]) { p(o); } pl(); } } ///////////////////////////////////////////////////////////////////////////////// static void print(Object s) { System.out.println(s); } ///////////////////////////////////////////////////////////////////////////////// //-----------HritikScanner class for faster input----------// static class HritikScanner { BufferedReader br; StringTokenizer st; public HritikScanner() { 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 num, row,col; Pair(int num, int row, int col) { this.num = num; this.row = row; this.col = col; } public int get1() { return num; } public int get2() { return row; } public int compareTo(Pair A) { return this.num - A.num; } public String toString() { return num +"" ""+ row+"" ""+col; } } ////////////////////////////////////////////////////////////////// // Function to return gcd of a and b time complexity O(log(a+b)) 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 long lcm(int a, int b) { return (a / gcd(a, b)) * b; } ////////////////////////////////////////////////////////////////// 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 (long i = 5; i * i <= n; i = i + 6) { if (n % i == 0 || n % (i + 2) == 0) { return false; } } return true; } ////////////////////////////////////////////////////////////////// static boolean isPowerOfTwo(long n) { if (n == 0) { return false; } return (long) (Math.ceil((Math.log(n) / Math.log(2)))) == (long) (Math.floor(((Math.log(n) / Math.log(2))))); } public static long getFact(int n) { long ans = 1; while (n > 0) { ans *= n; ans %= MOD; n--; } return ans; } public static long pow(long n, int pow) { if (pow == 0) { return 1; } long temp = pow(n, pow / 2) % MOD; temp *= temp; temp %= MOD; if (pow % 2 == 1) { temp *= n; } temp %= MOD; return temp; } public static long nCr(int n, int r) { long ans = 1; int temp = n - r; while (n > temp) { ans *= n; ans %= MOD; n--; } ans *= pow(getFact(r) % MOD, MOD - 2) % MOD; ans %= MOD; return ans; } ////////////////////////////////////////////////////////////////// // method returns Nth power of A static double nthRoot(int A, int N) { // intially guessing a random number between // 0 and 9 double xPre = Math.random() % 10; // smaller eps, denotes more accuracy double eps = 0.001; // initializing difference between two // roots by INT_MAX double delX = 2147483647; // xK denotes current value of x double xK = 0.0; // loop untill we reach desired accuracy while (delX > eps) { // calculating current value from previous // value by newton's method xK = ((N - 1.0) * xPre + (double) A / Math.pow(xPre, N - 1)) / (double) N; delX = Math.abs(xK - xPre); xPre = xK; } return xK; } } ",1 3a318b43,90b71536,"import java.lang.reflect.Array; import java.util.*; import java.io.*; import java.util.regex.Pattern; public class B2 { public static int getBit(int pos,int num){ int no = 1 << pos; return no & num; } public static boolean palindrome(String s){ StringBuilder s1 = new StringBuilder(); for(int i=s.length();i>=0;i--){ s1.append(s.charAt(i)); } String s2 = s1.toString(); if(s.equals(s2)){ return true; } return false; } public static void permutation(ArrayList arr,int l, int r,String ans,HashMap map,char[] temp){ if(l>r){ //System.out.println(ans); arr.add(ans); return; } for(char c:temp) { if (map.get(c) > 0) { map.put(c, map.get(c) - 1); permutation(arr, l + 1, r, ans + c, map,temp); map.put(c, map.get(c) + 1); } } } public static long[] maxBeauty(HashMap[] arr, long[] lv,long[] rv,int start,int parent,boolean[] vis){ vis[start] = true; long[] ans = new long[2]; // System.out.println(start); ArrayList list = new ArrayList(arr[start].keySet()); for(int node:list){ if(vis[node]==false) { long[] curr = maxBeauty(arr,lv,rv,node,start,vis); ans[0] += Math.max(curr[0] + Math.abs(lv[start]-lv[node]),curr[1] + Math.abs(lv[start]-rv[node])); ans[1] += Math.max(curr[0] + Math.abs(rv[start]-lv[node]),curr[1] + Math.abs(rv[start]-rv[node])); } } return ans; } public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int tc = Integer.parseInt(br.readLine()); while (tc > 0) { int n = Integer.parseInt(br.readLine()); boolean[] vis = new boolean[n+1]; HashMap[] arr = new HashMap[n+1]; for(int i=0;i<=n;i++){ arr[i] = new HashMap(); } long[] lv = new long[n+1]; long[] rv = new long[n+1]; for(int i=1;i<=n;i++){ String[] s1 = br.readLine().split("" ""); lv[i] = Long.valueOf(s1[0]); rv[i] = Long.valueOf(s1[1]); } 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 { public boolean isSpaceChar(int ch); } } 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); } public static void sortbyColumn(int arr[][], int col) { Arrays.sort(arr, new Comparator() { @Override public int compare(final int[] entry1, final int[] entry2) { if (entry1[col] > entry2[col]) return 1; else return -1; } }); } static long func(long a[],int size,int s){ long max1=a[s]; long maxc=a[s]; for(int i=s+1;i, 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 String toString() { return String.format(""(%s, %s)"", x.toString(), y.toString()); } } static class MultiSet> { public int sz = 0; public TreeMap t; public MultiSet() { t = new TreeMap<>(); } public void add(U x) { t.put(x, t.getOrDefault(x, 0) + 1); sz++; } public void remove(U x) { if (t.get(x) == 1) t.remove(x); else t.put(x, t.get(x) - 1); sz--; } } static void shuffle(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static long myceil(long a, long b){ return (a+b-1)/b; } static long C(long n,long r){ long count=0,temp=n; long ans=1; long num=n-r+1,div=1; while(num<=n){ ans*=num; //ans+=MOD; ans%=MOD; ans*=mypow(div,MOD-2); //ans+=MOD; ans%=MOD; num++; div++; } ans+=MOD; return ans%MOD; } static long fact(long a){ long i,ans=1; for(i=1;i<=a;i++){ ans*=i; ans%=MOD; } return ans%=MOD; } static void sieve(int n){ is_sieve[0]=1; is_sieve[1]=1; int i,j,k; for(i=2;in||j<0){ break; } is_sieve[j]=1; sieve[j]=i; } } } } // static void calc(int n){ // int i,j; // dp[n-1]=0; // if(n>1) // dp[n-2]=1; // for(i=n-3;i>=0;i--){ // long ind=n-i-1; // dp[i]=((ind*(long)mypow(10,ind-1))%MOD+dp[i+1])%MOD; // } // } static long mypow(long x,long y){ long temp; if( y == 0) return 1; temp = mypow(x, y/2); if (y%2 == 0) return (temp*temp)%MOD; else return ((x*temp)%MOD*(temp)%MOD)%MOD; } static long dist[],dp[][],left[],right[]; static int visited[],isit[]; static ArrayList>> adj[],li; //static int dp[][][]; static int MOD=1000000007; static char ch[]; static int[] sieve,is_sieve; static TreeSet tr; static long mat[][]; // static void bfs(int node,int par,Pair p[],long taken){ // LinkedList li=new LinkedList<>(); // li.add(node); // while(!li.isEmpty()){ // int x=li.pollFirst(); // long lowNode=p[x-1].x; // long highNode=p[x-1].y; // int left=0,right=0; // visited[x]=1; // for(Pair> pp:adj[x]){ // long max=0; // if(selected[pp.x]==0){ // max=Math.max(Math.abs(lowNode-pp.y.y),Math.abs(highNode-pp.y.x)); // if(max==Math.abs(lowNode-pp.y.y)){ // left++; // }else{ // right++; // } // }else{ // max=Math.max(Math.abs(lowNode-selected[pp.x]),Math.abs(highNode-selected[pp.x])); // if(max==Math.abs(lowNode-selected[pp.x])){ // left++; // }else{ // right++; // } // } // if(visited[pp.x]==0) // li.add(pp.x); // } // if(left>=right){ // selected[x]=lowNode; // }else{ // selected[x]=highNode; // } // } // } static void dfs(int node,int par, Pair p[]){ for(Pair> pp:adj[node]){ if(pp.x!=par){ //sum+=Math.abs(selected[node]-selected[pp.x]); dfs(pp.x,node,p); //System.out.println(node+"" ""+pp.x); long x=Math.abs(p[node].x-p[pp.x].x); long y=Math.abs(p[node].x-p[pp.x].y); long z=Math.abs(p[node].y-p[pp.x].x); long w=Math.abs(p[node].y-p[pp.x].y); left[node]+=Math.max(x+left[pp.x],y+right[pp.x]); right[node]+=Math.max(z+left[pp.x],w+right[pp.x]); } } } public static void main(String args[]){ InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter w = new PrintWriter(outputStream); int t,i,j,tno=0,tte; t=in.nextInt(); //t=1; //tte=t; while(t-->0){ //sum=0; int n=in.nextInt(); adj=new ArrayList[n+1]; left=new long[n+1]; right=new long[n+1]; visited=new int[n+1]; for(i=0;i(); } Pair p[]=new Pair[n+1]; for(i=1;i<=n;i++){ p[i]=new Pair<>(in.nextLong(),in.nextLong()); } for(i=0;i(v,p[v])); adj[v].add(new Pair<>(u,p[u])); } //bfs(1,-1,p,Long.MAX_VALUE); dfs(1,-1,p); // for(i=0;i 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 6bcc5afd,f6ca6fc8,"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 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 7c11511f,ca8f11a4,"import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws Exception { int tc = io.nextInt(); for (int i = 0; i < tc; i++) { solve(); } io.close(); } private static void solve() throws Exception { int n = io.nextInt(); String a = io.next(); String b = io.next(); int zz = 0; int zo = 0; int oo = 0; int oz = 0; for (int i = 0; i < a.length(); i++) { if (a.charAt(i) == '1' && b.charAt(i) == '0') { oz++; } if (a.charAt(i) == '0' && b.charAt(i) == '0') { zz++; } if (a.charAt(i) == '1' && b.charAt(i) == '1') { oo++; } if (a.charAt(i) == '0' && b.charAt(i) == '1') { zo++; } } int ans = Integer.MAX_VALUE; if (oz == zo) { ans = Math.min(ans, oz + zo); } if (oo - 1 == zz) { ans = Math.min(ans, oo + zz); } if (ans == Integer.MAX_VALUE) { ans = -1; } io.println(ans); } static void sort(int[] a) { ArrayList l = new ArrayList<>(a.length); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } //-----------PrintWriter for faster output--------------------------------- public static FastIO io = new FastIO(); //-----------MyScanner class for faster input---------- static class FastIO extends PrintWriter { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar, numChars; // standard input public FastIO() { this(System.in, System.out); } public FastIO(InputStream i, OutputStream o) { super(o); stream = i; } // file input public FastIO(String i, String o) throws IOException { super(new FileWriter(o)); stream = new FileInputStream(i); } // throws InputMismatchException() if previously detected end of file private int nextByte() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars == -1) return -1; // end of file } return buf[curChar++]; } // to read in entire lines, replace c <= ' ' // with a function that checks whether c is a line break public String next() { int c; do { c = nextByte(); } while (c <= ' '); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > ' '); return res.toString(); } public String nextLine() { int c; do { c = nextByte(); } while (c < '\n'); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n'); return res.toString(); } public int nextInt() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public long nextLong() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public double nextDouble() { return Double.parseDouble(next()); } } //-------------------------------------------------------- }"," import java.util.*; import java.util.stream.Collectors; import java.io.*; import java.math.*; public class GR18_C2 { public static FastScanner sc; public static StringBuilder sb ;public static int MOD= 1000000007; public static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() throws IOException { return br.readLine(); } } public static void solve(int t) throws IOException { int n=sc.nextInt(); String a=sc.next(); String b=sc.next(); int _00=0; int _01=0; int _10=0; int _11=0; for(int i=0;i= 0; i--) { if(changed[i] < x) { return false; } else if(i > 1){ int change = Math.min(changed[i]-x, arr[i])/3; changed[i-1] = changed[i-1]+change; changed[i-2] = changed[i-2] +change*2; } } return true; } } ","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[] arr = new long[n]; StringTokenizer tokenizer = new StringTokenizer(in.readLine()); for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(tokenizer.nextToken()); } long low = 0; long high = 1000000000; low--; while (low < high) { long mid = low + (high - low + 1) / 2; boolean works = test(arr, mid); if (works) { low = mid; } else { high = mid - 1; } //System.out.println(mid + "" "" + works); } System.out.println(low); } in.close(); out.close(); } public static boolean test(long[] arr, long k) { long[] h = new long[arr.length]; for (int i = 0; i < arr.length; i++) { h[i] = arr[i]; } for (int i = h.length - 1; i - 2 >= 0; i--) { if (h[i] < k) break; long d = Math.min((h[i]-k) / 3, arr[i]/3); h[i] -= 3 * d; h[i - 1] += d; h[i-+ 2] += 2 * d; } for (int i = 0; i < h.length; i++) { if (h[i] < k) return false; } //System.out.println(Arrays.toString(h)); return true; } }",0 0df4050e,f5fde094,"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()); } }"," //package eround101; import java.util.*; import java.io.*; import java.lang.*; import java.util.StringTokenizer; public class Solution { static HritikScanner sc = new HritikScanner(); static PrintWriter pw = new PrintWriter(System.out, true); final static int MOD = 1000000007; static StringBuilder sb = new StringBuilder(); public static void main(String[] args) { int t = ni(); while (t-- > 0) { solve(); } } static void solve() { int n = ni(); int[] arr = nextIntArray(n); char[] col = sc.next().toCharArray(); int[] cB = new int[n+1]; int[] cR = new int[n+1]; for(int i = 0; i < n; i++) { // pl((col[i] == 'R' && arr[i] > n)); // pl((col[i] == 'B' && arr[i] < 1)); if((col[i] == 'R' && arr[i] > n)||(col[i] == 'B' && arr[i] < 1)) { pl(""NO""); return; } if(col[i] == 'B') { if(arr[i] > n) continue; cB[arr[i]]++; if(cB[arr[i]] > arr[i]) { pl(""NO""); return; } } else { if(arr[i] < 1) continue; cR[arr[i]]++; if(cR[arr[i]] > (n-(arr[i]-1))) { pl(""NO""); return; } } // pa(cB); // pa(cR); } // pa(cB); // pa(cR); int[] psum = new int[n+1]; for(int i = 1; i<= n; i++) { psum[i] = psum[i-1]+cB[i]; if(psum[i] > i) { pl(""NO""); return; } } // pa(psum); int[] psum1 = new int[n+1]; psum1[n] = cR[n]; for(int i = n-1; i>= 0; i--) { psum1[i] = psum1[i+1]+cR[i]; if(psum1[i] > (n-(i-1))) { pl(""NO""); return; } } // pa(psum1); pl(""YES""); } ///////////////////////////////////////////////////////////////////////////////// static class FenwickTree { // Binary Index Tree int[] tree; static int size; public FenwickTree(int size) { this.size = size; tree = new int[size + 5]; } public void add(int i, int val) { while (i <= size) { tree[i] += val; i += i & -i; // adding the decimal value of the last set bit. } } public int sum(int i) { int res = 0; while (i >= 1) { res += tree[i]; i -= i & -i; // deleting the last set bit } return res; } public int sum(int l, int r) { return sum(r) - sum(l - 1); } } ///////////////////////////////////////////////////////////////////////////////// static int[] nextIntArray(int n) { int[] arr = new int[n]; int i = 0; while (i < n) { arr[i++] = ni(); } return arr; } static long[] nextLongArray(int n) { long[] arr = new long[n]; int i = 0; while (i < n) { arr[i++] = nl(); } return arr; } static int[] nextIntArray1(int n) { int[] arr = new int[n + 1]; int i = 1; while (i <= n) { arr[i++] = ni(); } return arr; } ///////////////////////////////////////////////////////////////////////////////// static int ni() { return sc.nextInt(); } static long nl() { return sc.nextLong(); } static double nd() { return sc.nextDouble(); } ///////////////////////////////////////////////////////////////////////////////// static void pl() { pw.println(); } static void p(Object o) { pw.print(o + "" ""); } static void pl(Object o) { pw.println(o); } static void psb(StringBuilder sb) { pw.print(sb); } static void pa(Object arr[]) { for (Object o : arr) { p(o); } pl(); } static void pa(int arr[]) { for (int o : arr) { p(o); } pl(); } static void pa(long arr[]) { for (long o : arr) { p(o); } pl(); } static void pa(double arr[]) { for (double o : arr) { p(o); } pl(); } static void pa(char arr[]) { for (char o : arr) { p(o); } pl(); } static void pa(List list) { for (Object o : list) { p(o); } pl(); } static void pa(Object[][] arr) { for (int i = 0; i < arr.length; ++i) { for (Object o : arr[i]) { p(o); } pl(); } } static void pa(int[][] arr) { for (int i = 0; i < arr.length; ++i) { for (int o : arr[i]) { p(o); } pl(); } } static void pa(long[][] arr) { for (int i = 0; i < arr.length; ++i) { for (long o : arr[i]) { p(o); } pl(); } } static void pa(char[][] arr) { for (int i = 0; i < arr.length; ++i) { for (char o : arr[i]) { p(o); } pl(); } } static void pa(double[][] arr) { for (int i = 0; i < arr.length; ++i) { for (double o : arr[i]) { p(o); } pl(); } } ///////////////////////////////////////////////////////////////////////////////// static void print(Object s) { System.out.println(s); } ///////////////////////////////////////////////////////////////////////////////// //-----------HritikScanner class for faster input----------// static class HritikScanner { BufferedReader br; StringTokenizer st; public HritikScanner() { 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 num, row, col; Pair(int num, int row, int col) { this.num = num; this.row = row; this.col = col; } public int get1() { return num; } public int get2() { return row; } public int compareTo(Pair A) { return this.num - A.num; } public String toString() { return num + "" "" + row + "" "" + col; } } ////////////////////////////////////////////////////////////////// // Function to return gcd of a and b time complexity O(log(a+b)) 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 long lcm(long a, long b) { return (a / gcd(a, b)) * b; } ////////////////////////////////////////////////////////////////// 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 (long i = 5; i * i <= n; i = i + 6) { if (n % i == 0 || n % (i + 2) == 0) { return false; } } return true; } ////////////////////////////////////////////////////////////////// static boolean isPowerOfTwo(long n) { if (n == 0) { return false; } return (long) (Math.ceil((Math.log(n) / Math.log(2)))) == (long) (Math.floor(((Math.log(n) / Math.log(2))))); } public static long getFact(int n) { long ans = 1; while (n > 0) { ans *= n; ans %= MOD; n--; } return ans; } public static long pow(long n, int pow) { if (pow == 0) { return 1; } long temp = pow(n, pow / 2) % MOD; temp *= temp; temp %= MOD; if (pow % 2 == 1) { temp *= n; } temp %= MOD; return temp; } public static long nCr(int n, int r) { long ans = 1; int temp = n - r; while (n > temp) { ans *= n; ans %= MOD; n--; } ans *= pow(getFact(r) % MOD, MOD - 2) % MOD; ans %= MOD; return ans; } ////////////////////////////////////////////////////////////////// // method returns Nth power of A static double nthRoot(int A, int N) { // intially guessing a random number between // 0 and 9 double xPre = Math.random() % 10; // smaller eps, denotes more accuracy double eps = 0.001; // initializing difference between two // roots by INT_MAX double delX = 2147483647; // xK denotes current value of x double xK = 0.0; // loop untill we reach desired accuracy while (delX > eps) { // calculating current value from previous // value by newton's method xK = ((N - 1.0) * xPre + (double) A / Math.pow(xPre, N - 1)) / (double) N; delX = Math.abs(xK - xPre); xPre = xK; } return xK; } } ",0 6e7cd58b,cfba313d,"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.io.*; import java.util.*; public class C {//Any Class Name static class Code { private void solve(InputReader in, OutputWriter out) throws IOException { ArrayOpn o= new ArrayOpn(); int t = in.readInt(); while(t -- > 0) { int n=in.readInt(); int[][] freq= new int[5][n]; for(int i=0; i=0; j--) { temp+=freq[i][j]; if(temp<=0) { break; } count++; } ans=Math.max(ans, count); } out.printLine(ans); } out.close(); } } static class ArrayOpn{ private int[] aI(InputReader in, int n) { int A[]= new int[n]; for(int i=0; i{ private final F first; //first member of pair private final S second; //second member of pair private Pair(F first, S second) { this.first = first; this.second = second; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; if(!first.equals(pair.first)) return false; return second.equals(pair.second); } @Override public int hashCode() { return 31 * first.hashCode() + second.hashCode(); } @Override public String toString() { return ""("" + first +"", ""+ second +"")""; } public static Pair of(F a, S b) { return new Pair<>(a,b); } public F getFirst() { return first; } public S getSecond() { return second; } } // Comparator for using in Sorting On integers static class PairCompare implements Comparator> { @Override public int compare(Pair p1, Pair p2) { int diff = Integer.compare(p1.first, p2.first); if(diff == 0) { return Integer.compare(p1.second, p2.second); } else return diff; } } public static void main(String[] args) throws IOException { //initialize InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); Code solver = new Code(); solver.solve(in, out); out.flush(); out.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private InputReader(InputStream stream) { this.stream = stream; } private 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++]; } private String readString() { 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(); } private int readInt() { 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; } private long readLong() { 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; } private double readDouble() { 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, readInt()); } 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, readInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } private boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public String next() { return readString(); } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; private OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter( outputStream))); } private OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } private void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } writer.flush(); } private void printLine(Object... objects) { print(objects); writer.println(); writer.flush(); } private void printLine(int i) { writer.println(i); } private void close() { writer.close(); } private void flush() { writer.flush(); } } static class IOUtils { private static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.readInt(); return array; } private static long[] readLongArray(InputReader in, int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) array[i] = in.readLong(); return array; } private static char[] readCharArray(InputReader in, int size) { char[] array = new char[size]; for (int i = 0; i < size; i++) array[i] = in.readCharacter(); return array; } private static char[][] readTable(InputReader in, int rowCount, int columnCount) { char[][] table = new char[rowCount][]; for (int i = 0; i < rowCount; i++) table[i] = readCharArray(in, columnCount); return table; } } static class ArrayUtils { private static void fill(int[][] array, int value) { for (int[] row : array) Arrays.fill(row, value); } } static class MiscUtils { private static final int[] DX4 = {1, 0, -1, 0}; private static final int[] DY4 = {0, -1, 0, 1}; private static boolean isValidCell(int row, int column, int rowCount, int columnCount) { return row >= 0 && row < rowCount && column >= 0 && column < columnCount; } } }",1 2f8c3bf3,a3abac01,"import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int k=sc.nextInt(); int idx[]=new int[k]; for(int i=0;i=0;i--){ right[i]=Math.min(right[i+1]+1,arr[i]); } for(int i=0;i 0;T--) start(); // System.out.print(sb); } void start() { int n = s.nextInt(); int m = s.nextInt(); long arr[] = longArr(m); long arr2[] = longArr(m); long ans [] = new long[n]; Arrays.fill(ans,Integer.MAX_VALUE); for(int i = 0; i=0; i--) { right[i] = Math.min(right[i+1]+1, ans[i]); } StringBuffer x = new StringBuffer(); for(int i =0; i0) { m/=2; y++; } return y; } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } long power(long x, long y, long p) { long res = 1; // Initialize result // Update x if it is more // than or equal to p x = x % p; while (y > 0) { // If y is odd, multiply // x with the result if ((y & 1) > 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } int lower_bound(int [] arr , int key) { int i = 0; int j = arr.length-1; //if(arr[i] > key)return -1; if(arr[j] < key)return -1; while(i 0) { 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); } } } } ",0 0017d438,9852706b,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class A { static List [] adj; static ArrayList temp; static int mod = (int) 1e9+7; static boolean[] vis = new boolean[(int)1e5]; static List l = new ArrayList<>(); static final Reader s = new Reader(); public static void main(String[] args) throws IOException { int t = s.nextInt(); // int t=1; while(t-->0) { int n = s.nextInt(); int m = s.nextInt(); int d = s.nextInt(); System.out.println(""YES""); PriorityQueue pq = new PriorityQueue<>(); for(int i=1;i<=m;i++){ pq.add(new Pair(i)); } for(int i=0;i { int id; long sum=0; public Pair(int id) { this.id=id; } public int compareTo(Pair o) { return Long.compare(sum, o.sum); } } //Important //Dont't stick on same approach //Check long static void dfs(int i){ vis[i]=true; l.add(i); List Y = adj[i]; for(int x:Y){ if(!vis[x])dfs(x); } } static long gcd(long a, long b) throws IOException { return (b == 0) ? a : gcd(b, a % b); } static int gcd(int a, int b) throws IOException { return (b == 0) ? a : gcd(b, a % b); } static void sortr(int[] a) { ArrayList l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l, Collections.reverseOrder()); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(int[] a) { ArrayList l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(long[] a) { ArrayList l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class Reader { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; String next() { while(st==null||!st.hasMoreTokens()) { try { st=new StringTokenizer(in.readLine()); } catch(Exception e) {} } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }","import java.io.BufferedReader; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static int modulo=998244353; public static void main(String[] args) { FastScanner in = new FastScanner(); int test=in.nextInt(); while(test-->0){ int n=in.nextInt(); int m=in.nextInt(); int x=in.nextInt(); PriorityQueue pq=new PriorityQueue<>(); int arr[]=new int[n]; for(int i=1;i<=m;i++){ pq.add(new pair(i,0)); } System.out.println(""YES""); for(int i=0;ik;i--){ num*=i; den*=j; j++; } return num/den; } public static int countDiff(int arr[],int n,int diff){ int sum=0; for(int i=0;i { int x; int y; pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(pair o) { return (int)(y-o.y); } }",1 0c173033,6b83b22e," import java.io.*; import java.util.*; public class M { static Scanner scanner=new Scanner(System.in); public static void main(String[] args) { int t=scanner.nextInt(); while(t-->0) { int n=scanner.nextInt(); int a[]=new int [n]; int b[]=new int [n]; for(int i=0;i0) { int n = sc.nextInt(); int a[] = new int[n]; int b[] = new int[n]; for(int i=0;i> indices = new HashMap>(); for(int i=0;i obj = new LinkedList(); obj.add(i); indices.put(array[i],obj); } } long tower[]= new long[m]; int ans[]= new int[n]; Arrays.sort(array); int k=0; for(int i=0;i0) { int n = sc.nextInt(); int m = sc.nextInt(); long x = sc.nextLong(); long[] arr = new long[n]; for(int i = 0; i < n; i++) { arr[i] = sc.nextLong(); } int[] res = new int[n]; PriorityQueue q = new PriorityQueue<>(); for(int i = 0; i < m; i++) { q.add(new Pair(i+1, 0)); } for(int i = 0; i < n; i++) { Pair p = q.poll(); res[i] = p.i; q.add(new Pair(p.i, p.w + arr[i])); } sb.append(""YES\n""); for(int i = 0; i < n; i++) { sb.append(res[i]+"" ""); } sb.replace(sb.length()-1, sb.length(), ""\n""); } PrintWriter pw = new PrintWriter(System.out); pw.println(sb.toString().trim()); pw.flush(); } static class Pair implements Comparable{ int i; long w; public Pair(int i, long w) { this.i = i; this.w = w; } public String toString() { return i+"" ""+w; } @Override public int compareTo(Pair p) { return Long.compare(w, p.w); } } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } } ",0 8535bdf7,e86eb5b0,"import java.util.*; import java.io.*; import java.lang.*; public class Problem { public static void dfs(int u, int parent, ArrayList> graph,long[][] dp,Pair[] arr) { ArrayList adja=graph.get(u); for(int i=0;i0) { int n =Integer.parseInt(br.readLine().trim()); Pair[] arr=new Pair[n+1]; for(int i=1;i> graph=new ArrayList>(n+1); for(int i=0;i()); } for(int i=0;i[] adj; static Long[][] a; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); int t = Integer.parseInt(s); while (t-- > 0) { s = br.readLine(); int n = Integer.parseInt(s); l = new int[n]; r = new int[n]; for (int i = 0; i < n; i++) { s = br.readLine(); String[] num = s.split("" ""); l[i] = Integer.parseInt(num[0]); r[i] = Integer.parseInt(num[1]); } adj = new ArrayList[n]; for (int i = 0; i < n; i++) adj[i] = new ArrayList<>(); for (int i = 1; i < n; i++) { s = br.readLine(); String[] num = s.split("" ""); int u = Integer.parseInt(num[0]) - 1, v = Integer.parseInt(num[1]) - 1; adj[u].add(v); adj[v].add(u); } a = new Long[n][2]; dfs(0, 0); System.out.println(Math.max(a[0][0], a[0][1])); } } private static void dfs(int cur, int fa) { a[cur][0] = a[cur][1] = 0L; for (int i = 0; i < adj[cur].size(); i++) { int p = adj[cur].get(i); if (p == fa) continue; dfs(p, cur); a[cur][0] += Math.max(a[p][0] + Math.abs(l[cur] - l[p]), a[p][1] + Math.abs(l[cur] - r[p])); a[cur][1] += Math.max(a[p][0] + Math.abs(r[cur] - l[p]), a[p][1] + Math.abs(r[cur] - r[p])); } } } ",0 83b44c9c,ed610dc9,"import java.util.Scanner; public class MinimumGridPath { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int test = scanner.nextInt(); StringBuilder sb = new StringBuilder(); for (int t = 0; t < test; t++) { int n = scanner.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = scanner.nextLong(); } long minx = arr[0]; long miny = arr[1]; long min = minx * n + miny * n; long sumx = arr[0]; long sumy = arr[1]; for(int i=2; i0) { int n=sc.nextInt(); long arr[]=new long[n]; for(int i=0;i 0) { int n = Integer.parseInt(br.readLine()); String[] str = br.readLine().split("" ""); int[] a = new int[n]; int k = 0; ArrayList pos = new ArrayList<>(); for(int i=0; i pos = new ArrayList(); st = new StringTokenizer(br.readLine()); 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 0) { solve(); } } static void solve() { int n = ni(); int m = ni(), x = ni(); Map> map = new TreeMap<>(); pl(""YES""); for(int i = 0; i < n; i++) { int num = ni(); if(!map.containsKey(num)) { map.put(num, new ArrayList()); } map.get(num).add(i); } int[] ans = new int[n]; int q = 0; for(int block : map.keySet()) { for(int i = 0; i < map.get(block).size(); i++) { ans[map.get(block).get(i)] = (q++)%m+1; } } for(int ele : ans) { p(ele + "" ""); } pl(); } ///////////////////////////////////////////////////////////////////////////////// static int[] nextIntArray(int n) { int[] arr = new int[n]; int i = 0; while (i < n) { arr[i++] = ni(); } return arr; } static long[] nextLongArray(int n) { long[] arr = new long[n]; int i = 0; while (i < n) { arr[i++] = nl(); } return arr; } static int[] nextIntArray1(int n) { int[] arr = new int[n + 1]; int i = 1; while (i <= n) { arr[i++] = ni(); } return arr; } ///////////////////////////////////////////////////////////////////////////////// static int ni() { return sc.nextInt(); } static long nl() { return sc.nextLong(); } static double nd() { return sc.nextDouble(); } ///////////////////////////////////////////////////////////////////////////////// static void pl() { pw.println(); } static void p(Object o) { pw.print(o); } static void pl(Object o) { pw.println(o); } static void psb(StringBuilder sb) { pw.print(sb); } static void pa(Object arr[]) { for (Object o : arr) { p(o); } pl(); } static void pa(int arr[]) { for (int o : arr) { p(o); } pl(); } static void pa(long arr[]) { for (long o : arr) { p(o); } pl(); } static void pa(double arr[]) { for (double o : arr) { p(o); } pl(); } static void pa(char arr[]) { for (char o : arr) { p(o); } pl(); } static void pa(List list) { for (Object o : list) { p(o); } pl(); } static void pa(Object[][] arr) { for (int i = 0; i < arr.length; ++i) { for (Object o : arr[i]) { p(o); } pl(); } } static void pa(int[][] arr) { for (int i = 0; i < arr.length; ++i) { for (int o : arr[i]) { p(o); } pl(); } } static void pa(long[][] arr) { for (int i = 0; i < arr.length; ++i) { for (long o : arr[i]) { p(o); } pl(); } } static void pa(char[][] arr) { for (int i = 0; i < arr.length; ++i) { for (char o : arr[i]) { p(o); } pl(); } } static void pa(double[][] arr) { for (int i = 0; i < arr.length; ++i) { for (double o : arr[i]) { p(o); } pl(); } } ///////////////////////////////////////////////////////////////////////////////// static void print(Object s) { System.out.println(s); } ///////////////////////////////////////////////////////////////////////////////// //-----------HritikScanner class for faster input----------// static class HritikScanner { BufferedReader br; StringTokenizer st; public HritikScanner() { 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 num, row,col; Pair(int num, int row, int col) { this.num = num; this.row = row; this.col = col; } public int get1() { return num; } public int get2() { return row; } public int compareTo(Pair A) { return this.num - A.num; } public String toString() { return num +"" ""+ row+"" ""+col; } } ////////////////////////////////////////////////////////////////// // Function to return gcd of a and b time complexity O(log(a+b)) 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 long lcm(int a, int b) { return (a / gcd(a, b)) * b; } ////////////////////////////////////////////////////////////////// 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 (long i = 5; i * i <= n; i = i + 6) { if (n % i == 0 || n % (i + 2) == 0) { return false; } } return true; } ////////////////////////////////////////////////////////////////// static boolean isPowerOfTwo(long n) { if (n == 0) { return false; } return (long) (Math.ceil((Math.log(n) / Math.log(2)))) == (long) (Math.floor(((Math.log(n) / Math.log(2))))); } public static long getFact(int n) { long ans = 1; while (n > 0) { ans *= n; ans %= MOD; n--; } return ans; } public static long pow(long n, int pow) { if (pow == 0) { return 1; } long temp = pow(n, pow / 2) % MOD; temp *= temp; temp %= MOD; if (pow % 2 == 1) { temp *= n; } temp %= MOD; return temp; } public static long nCr(int n, int r) { long ans = 1; int temp = n - r; while (n > temp) { ans *= n; ans %= MOD; n--; } ans *= pow(getFact(r) % MOD, MOD - 2) % MOD; ans %= MOD; return ans; } ////////////////////////////////////////////////////////////////// // method returns Nth power of A static double nthRoot(int A, int N) { // intially guessing a random number between // 0 and 9 double xPre = Math.random() % 10; // smaller eps, denotes more accuracy double eps = 0.001; // initializing difference between two // roots by INT_MAX double delX = 2147483647; // xK denotes current value of x double xK = 0.0; // loop untill we reach desired accuracy while (delX > eps) { // calculating current value from previous // value by newton's method xK = ((N - 1.0) * xPre + (double) A / Math.pow(xPre, N - 1)) / (double) N; delX = Math.abs(xK - xPre); xPre = xK; } return xK; } } ",1 175201f9,40cc90ae,"import java.util.*; import java.io.*; public class Sol{ /* ->check n=1, int overflow , array bounds , all possibilites(dont stuck on 1 approach) ->Problem = Observation(constraints(m<=n/3 or k<=min(100,n)) + Thinking + Technique (seg_tree,binary lift,rmq,bipart,dp,connected comp etc) ->solve or leave it (- tutorial improves you in minimal way -) */ public static void main (String []args) { //precomp(); int times=ni();while(times-->0){solve();}out.close();} static void solve(){ int n=ni(); int sum[]=new int[5]; PriorityQueue p[]=new PriorityQueue[5];for(int i=0;i<5;i++)p[i]=new PriorityQueue(); for(int i=0;i0 && sum[i]<=0){cmax--;sum[i]-=p[i].poll();} max=Math.max(max,cmax); } out.println(max); return; } //-----------------Utility-------------------------------------------- static long gcd(long a,long b){if(b==0)return a; return gcd(b,a%b);} static int Max=Integer.MAX_VALUE; static long mod=1000000007; //static int v(char c){return (int)(c-'a');} public static long power(long x, long y ) { //0^0 = 1 long res = 1L; x = x%mod; while(y > 0) { if((y&1)==1) res = (res*x)%mod; y >>= 1; x = (x*x)%mod; } return res; } static class Pair implements Comparable{ int id;int value;Pair next; public Pair(int id,int value) { this.id=id;this.value=value;next=null; } @Override public int compareTo(Pair p){return Long.compare(value,p.value);} } //----------------------I/O--------------------------------------------- static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static FastReader in=new FastReader(inputStream); static PrintWriter out=new PrintWriter(outputStream); static class FastReader { BufferedReader br; StringTokenizer st; FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } /*static int ni() { try { boolean in = false; int res = 0; for (;;) { int b = System.in.read() - '0'; if (b >= 0) { in = true; res = 10 * res + b; } else if (in) return res; } } catch (IOException e) { throw new Error(e); } }*/ static int ni(){return in.nextInt();} static long nl(){return in.nextLong();} static double nd(){return in.nextDouble();} static String ns(){return in.nextLine();} }","import javax.swing.text.html.parser.Entity; import java.io.*; import java.util.*; import java.math.BigInteger; public class Main{ public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } // main solver static class Task{ double eps= 0.00000001; static final int MAXN = 1010; static final int MOD= 1000000007; // stores smallest prime factor for every number static int spf[] = new int[MAXN]; static boolean[] prime; // Calculating SPF (Smallest Prime Factor) for every // number till MAXN. // Time Complexity : O(nloglogn) public void sieve() { spf[1] = 1; for (int i=2; i= x public int lowerIndex(List arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) >= x) h = mid - 1; else l = mid + 1; } return l; } public int lowerIndex(int[] arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] >= x) h = mid - 1; else l = mid + 1; } return l; } // function to find last index <= y public int upperIndex(List arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) <= y) l = mid + 1; else h = mid - 1; } return h; } public int upperIndex(int[] arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } // function to count elements within given range public int countInRange(List arr, int n, int x, int y) { // initialize result int count = 0; count = upperIndex(arr, n, y) - lowerIndex(arr, n, x) + 1; return count; } public int _gcd(int a, int b) { if(b == 0) { return a; } else { return _gcd(b, a % b); } } public int add(int a, int b){ a+=b; if(a>=MOD) a-=MOD; else if(a<0) a+=MOD; return a; } public int mul(int a, int b){ long res= (long)a*(long)b; return (int)(res%MOD); } public int power(int a, int b) { int ans=1; while(b>0){ if((b&1)!=0) ans= mul(ans,a); b>>=1; a= mul(a,a); } return ans; } int[] fact= new int[MAXN]; int[] inv= new int[MAXN]; public int Ckn(int n, int k){ if(k<0 || n<0) return 0; return mul(mul(fact[n],inv[k]),inv[n-k]); } public int inverse(int a){ return power(a,MOD-2); } public void preprocess() { fact[0]=1; for(int i=1;i=0;i--){ inv[i]= mul(inv[i+1],i+1); } } /** * return VALUE of lower bound for unsorted array */ public int lowerBoundNormalArray(int[] arr, int x){ TreeSet set= new TreeSet<>(); for(int num: arr) set.add(num); return set.lower(x); } /** * return VALUE of upper bound for unsorted array */ public int upperBoundNormalArray(int[] arr, int x){ TreeSet set= new TreeSet<>(); for(int num: arr) set.add(num); return set.higher(x); } public void debugArr(int[] arr){ for(int i: arr) out.print(i+"" ""); out.println(); } public int rand(){ int min=0, max= MAXN; int random_int = (int)Math.floor(Math.random()*(max-min+1)+min); return random_int; } InputReader in; PrintWriter out; static int mod = 1000000007; int ans = 0; ArrayListg[] = new ArrayList[101]; int cnt[][] = new int[101][101]; int dp[][] = new int[101][101]; int Add(int a, int b){ a+=b; while (a>=mod)a-=mod; return a; } int Mul(int a, int b){ return (int)(((long)a * b) % mod); } void dfs(int node, int par, int root, int depth){ cnt[root][depth]++; for (int v : g[node]){ if (v != par){ dfs(v, node, root, depth+1); } } } public void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- > 0){ int n = in.nextInt(); String[] str = new String[n]; ArrayList diff[] = new ArrayList[5]; for (int i = 0; i < 5; i++) diff[i] = new ArrayList<>(); for (int i = 0; i < n; i++){ str[i] = in.nextToken(); int[] cnt = new int[5]; for (int j = 0; j < str[i].length(); j++){ cnt[str[i].charAt(j) - 'a']++; } for (int j = 0; j < 5; j++){ diff[j].add(cnt[j] * 2 - str[i].length()); } } int ans = 0; for (int i = 0; i < 5; i++){ Collections.sort(diff[i]); Collections.reverse(diff[i]); int cur = 0, x = 0; for (int j = 0; j < diff[i].size(); j++){ cur+=diff[i].get(j); if (cur <= 0){ break; } x++; } ans = Math.max(ans, x); } out.println(ans); } } class SEG { int n; int[] segs; public SEG (int[] a){ this.n= a.length; segs= new int[4*this.n]; build(a,0,0,this.n-1); } public void build(int[] a, int root, int l, int r){ if(l==r){ segs[root]=a[l]; return; } int m= (l+r)/2; build(a,2*root+1,l,m); build(a,2*root+2,m+1,r); segs[root]= _gcd(segs[2*root+1], segs[2*root+2]); } public int query(int root, int l, int r, int lq, int rq){ if(lq<=l && rq>=r) return segs[root]; if(lq>r || rq m= new HashMap<>(); public long base=0; public long totalValue=0; private int M= 1000000007; private long addMod(long a, long b){ a+=b; if(a>=M) a-=M; return a; } public void reset(){ m= new HashMap<>(); base=0; totalValue=0; } public void update(long add){ base= base+ add; } public void add(long key, long val){ long newKey= key-base; m.put(newKey, addMod(m.getOrDefault(newKey,(long)0),val)); } } static class Tuple implements Comparable{ int x, y, z; public Tuple(int x, int y, int z){ this.x= x; this.y= y; this.z=z; } @Override public int compareTo(Tuple o){ return this.x-o.x; } } static class Pair implements Comparable{ public int x; public int y; public Pair(int x, int y){ this.x= x; this.y= y; } @Override public int compareTo(Pair o) { return this.x-o.x; } } // public static class compareL implements Comparator{ // @Override // public int compare(Tuple t1, Tuple t2) { // return t2.l - t1.l; // } // } // fast input reader class; static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { String line = null; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble(){ return Double.parseDouble(nextToken()); } public long nextLong(){ return Long.parseLong(nextToken()); } public int[] nextIntArr(int n){ int[] arr= new int[n]; for(int i=0;i nextIntList(int n){ List arr= new ArrayList<>(); for(int i=0;i> nextIntMatList(int n, int m){ List> mat= new ArrayList<>(); for(int i=0;i temp= new ArrayList<>(); for(int j=0;j k = new ArrayList((int)n + 1); k.add(0L); ArrayList h = new ArrayList((int) n + 1); h.add(0L); ArrayList dp = new ArrayList((int) n + 1); dp.add(0L); for (int i = 1; i <= n; i++) { long k_ = s.nextLong(); k.add(k_); } for (int i = 1; i <= n; i++) { long h_ = s.nextLong(); h.add(h_); dp.add(0L); } for (int i = 1; i <= n; i++) { long var = k.get(i) - h.get(i) + 1; for (int j = i - 1; (j > 0 && k.get(j) >= var); j--) { long var2 = k.get(j) - h.get(j) + 1; if (var2 <= var) { var = var2; } } int j = i - 1; for (; j > 0; j--) { if (k.get(j) < var) { break; } } long var3 = k.get(i) - var + 1; if (j != 0) { dp.set(i, (dp.get(j) + (var3 * (var3 + 1)) / 2)); } else { dp.set(i, ((var3 * (var3 + 1)) / 2)); } } System.out.println(dp.get((int) n)); } } ","import java.util.*; import java.io.*; public class Test { // global variables static final long INF = 1000000000000000000L; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() throws FileNotFoundException { 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; public static void main(String[] args) throws IOException { if (System.getProperty(""ONLINE_JUDGE"") == null) { PrintStream ps = new PrintStream(new File(""output.txt"")); InputStream is = new FileInputStream(""input.txt""); System.setIn(is); System.setOut(ps); } long t = 1; s = new FastReader(); t = s.nextLong(); for (int i = 1; i <= t; i++) solve(); } public static void solve() throws IOException { long n = s.nextLong(); ArrayList k = new ArrayList((int)n + 1); k.add(0L); ArrayList h = new ArrayList((int) n + 1); h.add(0L); ArrayList dp = new ArrayList((int) n + 1); dp.add(0L); for (int i = 1; i <= n; i++) { long k_ = s.nextLong(); k.add(k_); } for (int i = 1; i <= n; i++) { long h_ = s.nextLong(); h.add(h_); dp.add(0L); } for (int i = 1; i <= n; i++) { long var = k.get(i) - h.get(i) + 1; for (int j = i - 1; (j > 0 && k.get(j) >= var); j--) { long var2 = k.get(j) - h.get(j) + 1; if (var2 <= var) { var = var2; } } int j = i - 1; for (; j > 0; j--) { if (k.get(j) < var) { break; } } long var3 = k.get(i) - var + 1; if (j != 0) { dp.set(i, (dp.get(j) + (var3 * (var3 + 1)) / 2)); } else { dp.set(i, ((var3 * (var3 + 1)) / 2)); } } System.out.println(dp.get((int) n)); } } ",1 2db37cb1,875ed4c8,"/* ""Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up."" Just have Patience + 1... */ import java.util.*; import java.lang.*; import java.io.*; public class Solution { static int MAX = 100005; static Map> graph = new HashMap<>(); static int[] value = new int[MAX]; public static void main(String[] args) throws java.lang.Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new FastReader(); int test = sc.nextInt(); for (int t = 0; t < test; t++) { solve(); } out.close(); } private static void solve() { int n = sc.nextInt(); graph.clear(); for (int i = 1; i <= n; i++) { graph.put(i, new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { int u = sc.nextInt(); int v = sc.nextInt(); graph.get(u).add(new int[]{v, i}); graph.get(v).add(new int[]{u, i}); } int node = 0; for (int i = 1; i <= n; i++) { int childrens = graph.get(i).size(); if (childrens > 2) { out.println(-1); return; } if (childrens == 1) { node = i; } } dfs(node, 0, 2); for (int i = 0; i < n - 1; i++) { out.print(value[i] + "" ""); } out.println(); } private static void dfs(int currNode, int parent, int prime) { int addPrime = prime == 2 ? 3 : 2; for (int[] adjacentNode : graph.get(currNode)) { if (adjacentNode[0] == parent) { continue; } value[adjacentNode[1]] = prime; dfs(adjacentNode[0], currNode, addPrime); } } 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; } } } ","import java.util.*; import java.lang.*; import java.io.*; public class Main { static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc = new FastReader(); int t = sc.nextInt(); while(t-->0){ solve(sc); } } public static void solve(FastReader sc){ int n = sc.nextInt(); ArrayList> graph = new ArrayList>(); for(int i = 0;i()); } for(int i = 0;i2){ out.println(-1);out.flush();return; }else if(graph.get(i).size()==1){ start=i; } } int val=2; int [] wgt = new int[n-1]; int curr = graph.get(start).get(0).node; wgt[graph.get(start).get(0).idx] = val; val=5; while(true){ ArrayList list = graph.get(curr); if(list.size()==1){ break; }else{ for(Edge el : list){ if(wgt[el.idx]==0){ wgt[el.idx] = val; val = 7-val; curr = el.node; } } } } for(int el : wgt){ out.print(el + "" ""); } out.println(); out.flush(); } static class Edge { int node; int idx; Edge(int src, int nbr) { this.node = src; this.idx = nbr; } } /* int [] arr = new int[n]; for(int i = 0;i= 0 && freq[alpha - 'a'] <= freqSum) { //System.out.println(freq[alpha - 'a'] + "" "" + freqSum); freqSum -= res[k--]; } return k + 1; } public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); freq = new int[7]; strs = new String[n]; for (int i = 0; i < n; i++) strs[i] = in.nextLine(); for (String str : strs) for (char c : str.toCharArray()) freq[c - 'a']++; int max = 0; // int x = solve('d'); // out.println(x); for (char c = 'a'; c < 'f'; c++) max = Math.max(max, solve(c)); // int[] arr = countAlpha('d'); // Arrays.sort(arr); // // for (int i : arr) // out.println(i); out.println(max); } out.close(); } private static class FastReader { BufferedReader br; StringTokenizer st; 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 str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } ","import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; // نورت الكود يا كبير اتفضل // يا رب Accepted public class InterestingStory { private static int[] freq; private static String[] strs; private static int[] countAlpha(char alpha) { int[] count = new int[strs.length]; for (int i = 0; i < strs.length; i++) for (char c : strs[i].toCharArray()) count[i] += c == alpha ? -1 : 1; return count; } private static int solve(char alpha) { int[] res = countAlpha(alpha); Arrays.sort(res); int freqSum = 0; for (int j : freq) freqSum += j; freqSum -= freq[alpha - 'a']; int k = res.length - 1; while (k >= 0 && freq[alpha - 'a'] <= freqSum) freqSum -= res[k--]; return k + 1; } public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); freq = new int[5]; strs = new String[n]; for (int i = 0; i < n; i++) strs[i] = in.nextLine(); for (String str : strs) for (char c : str.toCharArray()) freq[c - 'a']++; int max = 0; for (char c = 'a'; c < 'f'; c++) max = Math.max(max, solve(c)); out.println(max); } out.close(); } private static class FastReader { BufferedReader br; StringTokenizer st; 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 str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }",1 44428e63,c850e422,"import java.util.*; import java.io.*; public class C_Minimum_Grid_Path{ public static void main(String[] args) { FastScanner s= new FastScanner(); StringBuilder res = new StringBuilder(); int t=s.nextInt(); int p=0; while(p 0) { int n = in.nextInt(); long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = in.nextLong(); } long result = Long.MAX_VALUE; for (int i = 0; i < 2; i++) { long x = 0; long y = 0; long minX = Long.MAX_VALUE; long minY = Long.MAX_VALUE; long prefix = 0; for (int j = 0; j < n; j++) { if (j % 2 == i) { if (j > 0) { result = Math.min(result, (n - x) * a[j] + (n - y) * minY + prefix); } ++x; prefix += a[j]; minX = Math.min(minX, a[j]); } else { if (j > 0) { result = Math.min(result, (n - y) * a[j] + (n - x) * minX + prefix); } ++y; prefix += a[j]; minY = Math.min(minY, a[j]); } } } out.println(result); } out.close(); } static void shuffle(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } static void shuffle(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Tuple implements Comparable { int a; int b; int c; public Tuple(int a, int b) { this.a = a; this.b = b; this.c = 0; } public Tuple(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public int getA() { return a; } public int getB() { return b; } public int getC() { return c; } public int compareTo(Tuple other) { if (this.a == other.a) { if (this.b == other.b) return Long.compare(this.c, other.c); return Long.compare(this.b, other.b); } return Long.compare(this.a, other.a); } @Override public int hashCode() { return Arrays.deepHashCode(new Integer[] {a, b, c}); } @Override public boolean equals(Object o) { if (!(o instanceof Tuple)) return false; Tuple pairo = (Tuple) o; return (this.a == pairo.a && this.b == pairo.b && this.c == pairo.c); } @Override public String toString() { return String.format(""(%d %d %d) "", this.a, this.b, this.c); } } } ",0 4dc0247e,875ed4c8,"import java.io.*; import java.util.*; public class ComdeFormces { public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub FastReader sc=new FastReader(); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int t=sc.nextInt(); while(t--!=0) { int n=sc.nextInt(); // int m=sc.nextInt(); ArrayList> ar=new ArrayList<>(); ArrayList arr=new ArrayList<>(); int c[]=new int[n]; for(int i=0;i<=n;i++) { ar.add(new ArrayList<>()); } for(int i=0;i2) { ans=false; break; } } if(ans) { for(int i=1;i<=n;i++) { if(ar.get(i).size()==1) { boolean vis[]=new boolean[n+1]; dfs(ar,2,i,vis,c); break; } } for(int i=0;i>ar ,int val,int src,boolean vis[],int c[]) { if(vis[src])return; vis[src]=true; for(int i=0;i divisors(int n){ ArrayList ar=new ArrayList<>(); for (int i=2; i<=Math.sqrt(n); i++){ if (n%i == 0){ if (n/i == i) { ar.add(i); } else { ar.add(i); ar.add(n/i); } } } return ar; } static int primeDivisor(int n){ ArrayList ar=new ArrayList<>(); int cnt=0; boolean pr=false; while(n%2==0) { pr=true; n/=2; } if(pr)ar.add(2); for(int i=3;i*i<=n;i+=2) { pr=false; while(n%i==0) { n/=i; pr=true; } if(pr)ar.add(i); } if(n>2) ar.add(n); return ar.size(); } static int mdlg(int num, int base) { int val=((int)(Math.ceil(Math.log(num)/Math.log(base)))); int val2=(int)Math.pow(base,val); // System.out.println(num+"" ""+Math.pow(base,val)); if(num/val2==0)return val; return val+1; // return val; } static String rev(String s) { char temp[]=s.toCharArray(); for(int i=0;imid)min=mid+1; else if(an=2;i--) { if(a[i]+val[i]>el) { if(val[i]>=el) { long df=a[i]; a[i]-=df; val[i-1]+=(df/3); val[i-2]+=2*(df/3); a[i]+=df%3; } else { long df=a[i]+val[i]-el; val[i-1]+=(df/3); val[i-2]+=2*(df/3); a[i]-=df; a[i]+=df%3; } } } for(int i=0;i> arr, int val,int el) { int s=0; int e=arr.size()-1; int ptr=0; if(val==1)ptr=1; while(s<=e) { int m=s+(e-s)/2; if(arr.get(m).get(ptr)==el)return m; else if(arr.get(m).get(ptr) arr,int el) { int start=0; int end=arr.size()-1; while(start<=end) { int mid=start+(end-start)/2; if(arr.get(mid).a==el)return mid; else if(arr.get(mid).aarr.size()-1)return -2; return -1; } static String eval(String s, long bs, long be,long k) { String ans=""""; for(int i=s.length()-1;i>=0;i--) { if(s.charAt(i)=='a') { ans=""a""+ans; } else if(be>0){ if(be>k) { for(int j=0;j>=1; if(tval==0)cnt[i][j]=cnt[i-1][j]+1; else { cnt[i][j]=cnt[i-1][j]; } j++; } } return cnt; } static long find(int s,long a[]) { if(s>=a.length)return -1; long num=a[s]; for(int i=s;i a, int k) { if(a.size()<1 )return 0; int i=a.size()-1; long ans=0; while(i>=0) { int temp=k; int prev=0; while(temp--!=0 && i>=0) { ans+=Math.abs(a.get(i)-prev); if(temp==0 || i==0)ans+=a.get(i); prev=a.get(i); i--; } } return ans; } static int bs(long a[] ,long num) { int start=0; int end=a.length-1; while(start<=end) { int mid=start+(end-start)/2; if(a[mid]==num) { return mid; } else if(a[mid]n-r)r=n-r; int ans=1; for(int i=0;i=end)return a[end]; int mid=start+(end-start)/2; int min1=mergesort(a,start,mid); int min2=mergesort(a,mid+1,end); merge(a,start,mid,end); return Math.min(min1, min2); } static void merge(int []a, int start,int mid,int end) { int ptr1=start; int ptr2=mid+1; int b[]=new int[end-start+1]; int i=0; while(ptr1<=mid && ptr2<=end) { if(a[ptr1]<=a[ptr2]) { b[i]=a[ptr1]; ptr1++; i++; } else { b[i]=a[ptr2]; ptr2++; i++; } } while(ptr1<=mid) { b[i]=a[ptr1]; ptr1++; i++; } while(ptr2<=end) { b[i]=a[ptr2]; ptr2++; i++; } for(int j=start;j<=end;j++) { a[j]=b[j-start]; } } public static class FastReader { BufferedReader b; StringTokenizer s; public FastReader() { b=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(s==null ||!s.hasMoreElements()) { try { s=new StringTokenizer(b.readLine()); } catch(IOException e) { e.printStackTrace(); } } return s.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""""; try { str=b.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } boolean hasNext() { if (s != null && s.hasMoreTokens()) { return true; } String tmp; try { b.mark(1000); tmp = b.readLine(); if (tmp == null) { return false; } b.reset(); } catch (IOException e) { return false; } return true; } } public static class pair{ int a; int b; public pair(int a,int b) { this.a=a; this.b=b; } public int compareTo(pair b) { return b.b-this.b; } public int compareToo(pair b) { if(this.a!=b.a)return this.a-b.a; else { return b.b-this.b; } } } static long pow(long a, long pw) { long temp; if(pw==0)return 1; temp=pow(a,pw/2); if(pw%2==0)return temp*temp; return a*temp*temp; } static int pow(int a, int pw) { int temp; if(pw==0)return 1; temp=pow(a,pw/2); if(pw%2==0)return temp*temp; return a*temp*temp; } } ","import java.util.*; import java.lang.*; import java.io.*; public class Main { static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc = new FastReader(); int t = sc.nextInt(); while(t-->0){ solve(sc); } } public static void solve(FastReader sc){ int n = sc.nextInt(); ArrayList> graph = new ArrayList>(); for(int i = 0;i()); } for(int i = 0;i2){ out.println(-1);out.flush();return; }else if(graph.get(i).size()==1){ start=i; } } int val=2; int [] wgt = new int[n-1]; int curr = graph.get(start).get(0).node; wgt[graph.get(start).get(0).idx] = val; val=5; while(true){ ArrayList list = graph.get(curr); if(list.size()==1){ break; }else{ for(Edge el : list){ if(wgt[el.idx]==0){ wgt[el.idx] = val; val = 7-val; curr = el.node; } } } } for(int el : wgt){ out.print(el + "" ""); } out.println(); out.flush(); } static class Edge { int node; int idx; Edge(int src, int nbr) { this.node = src; this.idx = nbr; } } /* int [] arr = new int[n]; for(int i = 0;i0) dp[i][i] = dp[i-1][i-1] + Math.abs(arr1[i]-arr2[i]); for(int j=i+1;j0) { dp[j][i] = Math.min(dp[j-1][i],dp[j-1][i-1]+Math.abs(arr2[j]-arr1[i])); } else { dp[j][i] = Math.min(dp[j-1][i],Math.abs(arr2[j]-arr1[i])); } } } System.out.println(dp[m2-1][m1-1]); } public static void main(String[] args) { MyScanner scanner = new MyScanner(); int n = scanner.nextInt(); int[] arr = new int[n]; for(int i=0;i 0) { --t; int n = in.nextInt(); int arr[] = new int[n]; List ones = new ArrayList(); List zero = new ArrayList<>(); for(int i = 0;i ones,List zero,int x,int y) { if(x < 0) return 0; if(y<0) return Integer.MAX_VALUE; if(dp[x][y]!=-1) return dp[x][y]; return dp[x][y] = Math.min(findans(ones, zero, x, y-1),findans(ones, zero, x-1, y-1) + (long)Math.abs(ones.get(x)-zero.get(y))); } static long gcd(long a, long b) { if (a == 0) return b; else return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static void sort(long[] a) { ArrayList l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } } class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }",0 71e0f2e5,b226c49a,"import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for(int t=0;t[] adj = new LinkedList[V]; for(int v=0;v 2){ System.out.println(""-1""); return; } } int[] weight = new int[V-1]; // int curNode = start; int prevNode = -1; visited[start] = true; for(int v=0;v 0) A.go(); // out.println(); out.flush(); } // >>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<< // static class pair { int x, y; pair(int x, int y) { this.x = x; this.y = y; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x; result = prime * result + y; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; pair other = (pair) obj; if (x != other.x) return false; if (y != other.y) return false; return true; } } static void go() throws Exception { int n=sc.nextInt(); ArrayList> aa=new ArrayList<>(); for(int i=0;i()); } LinkedHashMap map=new LinkedHashMap<>(); for(int i=0;iv) { int temp=v; v=u;u=temp; } map.put(new pair(u,v),0); } for(ArrayList i : aa) { if(i.size()>2) { out.println(-1); return; } } boolean vis[]=new boolean[n]; dfs(aa,0,vis,2,map); for(pair i : map.keySet()) { out.print(map.get(i)+"" ""); } out.println(); } static void dfs(ArrayList> aa,int src,boolean vis[],int see,LinkedHashMap map) { vis[src]=true; for(int i: aa.get(src)) { if(vis[i]==false) { int x=src,y=i; if(x>y) { int temp=x; x=y; y=temp; } pair temp=new pair(x,y); if(see==2) { map.put(temp,5); see=5; }else { map.put(temp,2); see=2; } dfs(aa,i,vis,see,map); } } } static long lcm(long a, long b) { return a * b / gcd(a, b); } // >>>>>>>>>>> Code Ends <<<<<<<<< // // --For Rounding--// static double round(double value, int places) { if (places < 0) throw new IllegalArgumentException(); BigDecimal bd = new BigDecimal(Double.toString(value)); bd = bd.setScale(places, RoundingMode.HALF_UP); return bd.doubleValue(); } // ----Greatest Common Divisor-----// static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } // --- permutations and Combinations ---// static long fact[]; static long invfact[]; static long ncr(int n, int k) { if (k < 0 || k > n) { return 0; } long x = fact[n]; long y = fact[k]; long yy = fact[n - k]; long ans = (x / y); ans = (ans / yy); return ans; } // ---sieve---// static int prime[] = new int[1000006]; // static void sieve() { // Arrays.fill(prime, 1); // prime[0] = 0; // prime[1] = 0; // for (int i = 2; i * i <= 1000005; i++) { // if (prime[i] == 1) // for (int j = i * i; j <= 1000005; j +)= i) { // prime[j] = 0; // } // } // } // ---- Manual sort ------// static void sort(long[] a) { ArrayList aa = new ArrayList<>(); for (long i : a) { aa.add(i); } Collections.sort(aa); for (int i = 0; i < a.length; i++) a[i] = aa.get(i); } static void sort(int[] a) { ArrayList aa = new ArrayList<>(); for (int i : a) { aa.add(i); } Collections.sort(aa); for (int i = 0; i < a.length; i++) a[i] = aa.get(i); } // --- Fast exponentiation ---// static long pow(long x, long y) { long res = 1l; while (y != 0) { if (y % 2 == 1) { res = (x * res); } y /= 2; x = (x * x); } return res; } // >>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<< // 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()); } int[] intArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); return a; } long[] longArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = sc.nextLong(); return a; } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }",0 7d7cf9a7,e45446bc,"import java.util.*; import java.io.*; import java.math.*; public class Coder { static int n; static long c[]; static StringBuilder str = new StringBuilder(""""); static void solve() { long mne=c[0]; long mno=c[1]; long ans=(c[0]+c[1])*n; long se=c[0]; long so=c[1]; long ecnt=1,ocnt=1; for(int i=2;i0) { n=Integer.parseInt(bf.readLine().trim()); c=new long[n]; String s[]=bf.readLine().trim().split(""\\s+""); for(int i=0;i 0) { String[] scn = (br.readLine()).trim().split("" ""); int n = Integer.parseInt(scn[0]); long[] arr = new long[n]; scn = (br.readLine()).trim().split("" ""); for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(scn[i]); } long min; long hor = arr[0], ver = arr[1]; long min1 = 0, min2 = 0; min = (hor + ver) * n; long x = 0, y = 0; for (int i = 2; i < n; i++) { if (i % 2 == 0) { x += 1; if (arr[i] >= hor) { min1 += arr[i]; } else { min1 += hor; hor = arr[i]; } } else { y += 1; if (arr[i] >= ver) { min2 += arr[i]; } else { min2 += ver; ver = arr[i]; } } long pro = (n - x) * hor + (n - y) * ver; min = Math.min(min, min1 + min2 +pro); } sb.append(min); sb.append(""\n""); } System.out.println(sb); return; } public static void sort(long[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int idx = (int) Math.random() * n; long temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } public static void print(long[] dp) { for (long ele : dp) { System.out.print(ele + "" ""); } System.out.println(); } public static void print(long[][] dp) { for (long[] a : dp) { for (long ele : a) { System.out.print(ele + "" ""); } System.out.println(); } } } ",0 8d6f1bf5,dc281165,"//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--; } } } ","/** * 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 3666e0e8,ac7187d8," import java.io.*; import java.lang.*; import java.util.*; public class C1499 { public static void main(String[] args) throws IOException{ StringBuffer ans = new StringBuffer(); StringTokenizer st; BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(f.readLine()); int t = Integer.parseInt(st.nextToken()); for(int i = 0; i < t; i++){ st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); long op = Long.MAX_VALUE; long[] arr = new long[n]; st = new StringTokenizer(f.readLine()); for(int x = 0; x < n; x++){ arr[x] = Integer.parseInt(st.nextToken()); } long sum = arr[0]; long min = arr[0]; long min1 = arr[1]; long howMany = 1; long howMany1 = 0; long osum = sum; for(int x = 1; x < n; x++){ osum+=arr[x]; if(x % 2 != 0){ sum+= (n - howMany1) *arr[x]; sum+=( min *(n-howMany)); min1 = Math.min(arr[x], min1); howMany1++; }else{ sum+= (n - howMany) *arr[x]; sum+=( min1 *(n-howMany1)); min = Math.min(arr[x], min); howMany++; } //System.out.println(min1 + "" "" + min1); //System.out.println(sum); op = Math.min(op, sum); sum = osum; } ans.append(op); ans.append(""\n""); } f.close(); System.out.println(ans); } public static class point implements Comparable{ int x; int y; public point(int x,int y){ this.x = x; this.y = y; } public String toString(){ return(x + "" "" + y); } public boolean equals(Object x){ point y = ((point)(x)); if (this.x == y.x && this.y == y.y){ return true; } return false; } public int hashCode(){ return Objects.hash(x,y); } public int compareTo(point other){ if(this.x < other.x){ return -1; }else if(this.x == other.x && this.y == other.y){ return 0; } return 1; } } }","import java.io.*; import java.util.*; public class C { public static void main (String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(f.readLine()); int t = Integer.parseInt(st.nextToken()); while (t-->0) { st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(f.readLine()); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(st.nextToken()); } solve(n, arr); } } static void solve(long n, long[] arr) { long minEven = Integer.MAX_VALUE; long minOdd = arr[0]; long evenSum = 0; long oddSum = arr[0]; long finans = Long.MAX_VALUE; long oddAns, evenAns; long oddcount=1; long evencount=0; for (int k = 1; k < n; k++) { if (k%2==1) { evenSum+=arr[k]; evencount++; minEven = Math.min(minEven, arr[k]); } else { oddSum+=arr[k]; oddcount++; minOdd = Math.min(minOdd, arr[k]); } oddAns = oddSum+(n-oddcount)*minOdd; evenAns = evenSum+(n-evencount)*minEven; finans = Math.min(finans, oddAns+evenAns); } System.out.println(finans); } } ",0 69b2fd22,9028caf7,"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;i0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList list=new ArrayList<>(); ArrayList space=new ArrayList<>(); 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.io.*; import java.util.*; public class B { public static void main(String[] args)throws IOException { FastScanner scan = new FastScanner(); PrintWriter output = new PrintWriter(System.out); int t = scan.nextInt(); for(int tt = 0;tt list = new ArrayList<>(); for(int i:arr) list.add(i); Collections.sort(list); for(int i = 0;i :) 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 int mod=10000007; static StringBuilder sb=new StringBuilder(); /* start */ public static void main(String [] args) { int t = i(); while(t-->0) { int n = i(); int a[] = input(n); char c[] = inputC(); ArrayList b = new ArrayList<>(); ArrayList r = new ArrayList<>(); for(int i=0;in-i) { is = false; break; } } out.println(is==true?""YES"":""NO""); } out.close(); } /* end */ 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 i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; 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; } //pair class private static class Pair implements Comparable { int first, second; public Pair(int f, int 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; } } } }","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 54eb12ca,949502c2,"import java.io.*; import java.util.*; public class huge implements Runnable { private boolean console=false; ArrayList adj[]; int maxi,maxc; int dis[]; int vis[]; Queue q; public void solve() { int i; int n=in.ni(); maxi=0; maxc=0; int a=in.ni()-1,b=in.ni()-1; int da=in.ni(); int db=in.ni(); adj=new ArrayList[n]; for(i=0;i=d||da>=db) ans=0; q= new LinkedList(); vis=new int[n]; q.add(a); dis=new int[n]; dis[a]=0; int cu=0; while(!q.isEmpty()||vis[b]==0) { int v=q.poll(); vis[v]=1; bfs(v); } if(dis[b]<=da) ans=0; if(ans==1) out.println(""Bob""); else out.println(""Alice""); } public void bfs(int v) { for(int node:adj[v]) { if(vis[node]==0) { q.add(node); dis[node]=dis[v]+1; } } } public void dfs(int v,int cur) { cur++; if(cur>maxc) { maxc=cur; maxi=v; } vis[v]=1; for(int node:adj[v]) { if(vis[node]==0) dfs(node,cur); } } @Override public void run() { try { init(); } catch (FileNotFoundException e) { e.printStackTrace(); } int t= in.ni(); while (t-->0) { solve(); out.flush(); } } private FastInput in; private PrintWriter out; public static void main(String[] args) throws Exception { new huge().run(); } private void init() throws FileNotFoundException { InputStream inputStream = System.in; OutputStream outputStream = System.out; try { if (!console && System.getProperty(""user.name"").equals(""sachan"")) { outputStream = new FileOutputStream(""/home/sachan/Desktop/output.txt""); inputStream = new FileInputStream(""/home/sachan/Desktop/input.txt""); } } catch (Exception ignored) { } out = new PrintWriter(outputStream); in = new FastInput(inputStream); } static class FastInput { InputStream obj; public FastInput(InputStream obj) { this.obj = obj; } byte inbuffer[] = new byte[1024]; int lenbuffer = 0, ptrbuffer = 0; int readByte() { if (lenbuffer == -1) throw new InputMismatchException(); if (ptrbuffer >= lenbuffer) { ptrbuffer = 0; try { lenbuffer = obj.read(inbuffer); } catch (IOException e) { throw new InputMismatchException(); } } if (lenbuffer <= 0) return -1;return inbuffer[ptrbuffer++]; } String ns() { int b = skip();StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b);b = readByte(); }return sb.toString();} int ni() { int num = 0, b;boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true;b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; }b = readByte(); }} long nl() { long num = 0;int b;boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true;b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10L + (b - '0'); } else { return minus ? -num : num; }b = readByte(); } } boolean isSpaceChar(int c) { return (!(c >= 33 && c <= 126)); } int skip() { int b;while ((b = readByte()) != -1 && isSpaceChar(b)) ;return b; } float nf() {return Float.parseFloat(ns());} double nd() {return Double.parseDouble(ns());} char nc() {return (char) skip();} } } ","import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class Template { static int mod = 1000000007; public static void main(String[] args) { FastScanner sc = new FastScanner(); int yo = sc.nextInt(); while (yo-- > 0) { int n = sc.nextInt(); int a = sc.nextInt()-1; int b = sc.nextInt()-1; int da = sc.nextInt(); int db = sc.nextInt(); List> list = new ArrayList<>(); for(int i = 0; i < n; i++) list.add(new ArrayList<>()); for(int i = 0; i < n-1; i++){ int x = sc.nextInt()-1; int y = sc.nextInt()-1; list.get(x).add(y); list.get(y).add(x); } for(int i = 0; i <= n; i++) depth[i] = 0; diam = 0; dfs(a,-1,list); if(2 * da >= min(diam, db) || depth[b] <= da){ out.println(""Alice""); } else { out.println(""Bob""); } } } static int[] depth = new int[200001]; static int diam = 0; static int dfs(int x, int p, List> list) { int len = 0; List ne = list.get(x); for(int y : ne) { if(y != p) { depth[y] = depth[x] + 1; int cur = 1 + dfs(y, x,list); diam = max(diam, cur + len); len = max(len, cur); } } return len; } public static class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } public static void sort(int[] arr) { ArrayList ls = new ArrayList(); for (int x : arr) ls.add(x); Collections.sort(ls); for (int i = 0; i < arr.length; i++) arr[i] = ls.get(i); } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] sieve(int N) { boolean[] sieve = new boolean[N + 1]; for (int i = 2; i <= N; i++) sieve[i] = true; for (int i = 2; i <= N; i++) { if (sieve[i]) { for (int j = 2 * i; j <= N; j += i) { sieve[j] = false; } } } return sieve; } public static long power(long x, long y, long p) { long res = 1L; x = x % p; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; } return res; } public static void print(int[] arr) { //for debugging only for (int x : arr) out.print(x + "" ""); out.println(); } static class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] readInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] readLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] readDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } // For Input.txt and Output.txt // FileInputStream in = new FileInputStream(""input.txt""); // FileOutputStream out = new FileOutputStream(""output.txt""); // PrintWriter pw = new PrintWriter(out); // Scanner sc = new Scanner(in); }",0 11c2ab99,e6a6e318,"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--) { 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; } } } ",1 35f0c004,efa38999,"import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=input.nextInt(); while(T-->0) { int n=input.nextInt(); String a=input.next(); String b=input.next(); int same1=0,same0=0,opp1=0,opp0=0; for(int i=0;i 0) { int n = reader.nextInt(); String a = reader.nextLine(); String b = reader.nextLine(); ans = -1; int even_demand = 0; int odd_demand = 0; int same_ones = 0, same_zeros = 0; int diff_ones = 0, diff_zeros = 0; for(int i=0; i0){ int n=sc.nextInt(); int[][] ct=new int[n][5]; int[] len=new int[n]; for (int i=0;i=0;j--){ sum+=diff[j]; if (sum>0) { inc++; }else { break; } } mx=Math.max(mx,inc); } System.out.println(mx); } } } ","import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; public class Practice { static HashMap map = new HashMap<>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-->0) { int n = sc.nextInt(); int[][] occurances = new int[5][n]; for(int i=0;i=0;j--){ tmpSum+=occurances[i][j]; if(tmpSum>0) tmpAns++; else break; } ans = Math.max(ans, tmpAns); } System.out.println(ans); } } } ",1 5a81e159,866a2d52," /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; public class C_CF { public static void main(String[] args) { FastScanner57 fs = new FastScanner57(); PrintWriter pw = new PrintWriter(System.out); int t = fs.ni(); //int t = 1; for (int tc = 0; tc < t; tc++) { int n = fs.ni(); long[][] lr = new long[n][2]; for (int i = 0; i < n; i++) { lr[i][0] = fs.nl(); lr[i][1] = fs.nl(); } List> list = new ArrayList(); for (int i = 0; i < n;i ++) { List temp = new ArrayList(); list.add(temp); } for (int i = 0; i < n-1; i++) { int a = fs.ni()-1, b = fs.ni()-1; list.get(a).add(b); list.get(b).add(a); } Long[][] dp = new Long[n][2]; pw.println(recur(0,0,-1,new long[] {0,0},dp,lr,list)); } pw.close(); } // 0 -> left was chosen // 1 -> right was chosen public static long recur(int ind, int p,int prev, long[] v, Long[][] dp, long[][] lr,List> list) { long last = v[0]; long ls = 0L; long rs = 0L; if (p==1) { last = v[1]; } if (ind!=0) ls += (long)Math.abs(last-lr[ind][0]); if (ind!=0) rs += (long)Math.abs(last-lr[ind][1]); if (dp[ind][p]!=null) return dp[ind][p]; long[] cur = lr[ind]; List temp = list.get(ind); for (int val : temp) { if (prev==val) continue; ls += recur(val,0,ind,cur,dp,lr,list); rs += recur(val,1,ind,cur,dp,lr,list); } return dp[ind][p] = Math.max(ls,rs); } public static void sort(long[] a) { List list = new ArrayList(); for (int i = 0; i < a.length; i++) { list.add(a[i]); } Collections.sort(list); for (int i = 0; i < a.length; i++) { a[i] = list.get(i); } } public static long gcd(long n1, long n2) { if (n2 == 0) { return n1; } return gcd(n2, n1 % n2); } } class UnionFind16 { int[] id; public UnionFind16(int size) { id = new int[size]; for (int i = 0; i < size; i++) { id[i] = i; } } public int find(int p) { int root = p; while (root != id[root]) { root = id[root]; } while (p != root) { int next = id[p]; id[p] = root; p = next; } return root; } public void union(int p, int q) { int a = find(p), b = find(q); if (a == b) { return; } id[b] = a; } } class FastScanner57 { BufferedReader br; StringTokenizer st; public FastScanner57() { br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } int[] intArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) { ret[i] = ni(); } return ret; } long nl() { return Long.parseLong(next()); } long[] longArray(int N) { long[] ret = new long[N]; for (int i = 0; i < N; i++) { ret[i] = nl(); } return ret; } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } ","import java.io.*; import java.util.*; public class P1529C { public static final int MAX = 100000; private static final List[] adjacency = new List[MAX]; private static final int[][] pair = new int[MAX][2]; public static void main(String[] args) { SpeedScanner in = new SpeedScanner(); for(int i = 0; i < MAX; ++i) adjacency[i] = new ArrayList<>(); int t = in.nextInt(); while(t-- != 0) { int nv = in.nextInt(); for(int i = 0; i < nv; ++i) { pair[i][0] = in.nextInt(); pair[i][1] = in.nextInt(); } for(int i = 1; i < nv; ++i) { int x = in.nextInt() - 1, y = in.nextInt() - 1; adjacency[x].add(y); adjacency[y].add(x); } long[] alts = dfsTraverse(0, -1); System.out.println(alts[0] >= alts[1] ? alts[0] : alts[1]); for(int i = 0; i < nv; ++i) adjacency[i].clear(); } } private static long[] dfsTraverse(int vertex, int parent) { long sumDiffL = 0, sumDiffR = 0; for(int child: adjacency[vertex]) { if(child == parent) continue; long[] temp = dfsTraverse(child, vertex); sumDiffL += Math.max(Math.abs(pair[vertex][0] - pair[child][0]) + temp[0], Math.abs(pair[vertex][0] - pair[child][1]) + temp[1]); sumDiffR += Math.max(Math.abs(pair[vertex][1] - pair[child][0]) + temp[0], Math.abs(pair[vertex][1] - pair[child][1]) + temp[1]); } return new long[]{sumDiffL, sumDiffR}; } } class SpeedScanner { BufferedReader in; StringTokenizer splitter; public SpeedScanner() { in = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(splitter == null || !splitter.hasMoreElements()) try { splitter = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } return splitter.nextToken(); } String nextLine() { String s = """"; try { s = in.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } ",0 3d245498,7838095f,"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(); int[] a = new int[n]; int[] ans = new int[n]; int sum = 0; a[0] = in.nextInt(); for (int i = 1; i < n; i++) { a[i] = in.nextInt(); if (a[i] * sum <= 0) { ans[i] = 1; } else { ans[i] = -1; } sum += ans[i] * a[i]; } if (sum == 0) { sum -= ans[1] * a[1]; ans[1] = -ans[1]; sum += ans[1] * a[1]; } int d = gcd(Math.abs(sum), Math.abs(a[0])); if (a[0] * sum < 0) { ans[0] = Math.abs(sum) / d; } else { ans[0] = -Math.abs(sum) / d; } for (int i = 1; i < n; i++) { ans[i] *= Math.abs(a[0]) / d; } print_array(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 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.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(); int[] a = new int[n]; int[] ans = new int[n]; int sum = 0; a[0] = in.nextInt(); for (int i = 1; i < n; i++) { a[i] = in.nextInt(); // checking is the multiply of the element at i index and sum is less then or equal 0 if (a[i] * sum <= 0) { ans[i] = 1; } else { ans[i] = -1; } sum += ans[i] * a[i]; } if (sum == 0) { sum -= ans[1] * a[1]; ans[1] = -ans[1]; sum += ans[1] * a[1]; } // storing the greatest available divisor int d = gcd(Math.abs(sum), Math.abs(a[0])); /** * if the the value of a[0] gets smaller then zero we will divide it with divisor and keep it absolute positive * else we make it negative and divide it with divisor * then will store in index 0 */ if (a[0] * sum < 0) { ans[0] = Math.abs(sum) / d; } else { ans[0] = -Math.abs(sum) / d; } /** * then for each value of array we will divide the divisor with the absolute value of index 0 */ for (int i = 1; i < n; i++) { ans[i] *= (double) Math.abs(a[0]) / d; } print_array(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(); } /** * Here we are finding greatest common divisor of a[1], so that we divide the maximum value of sum as the sum might cross 20000 or more * @param a * @param b * @return greatest common divisor */ 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; } @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(); } 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 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; } 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(); } } } ",1 26e699de,fcc7e8fa,"//package Task1; import java.util.Scanner; public class Menorah { static int MOD9= 1000000000; public static void main(String[] args){ Scanner sc= new Scanner(System.in); int numberTest=sc.nextInt(); while(numberTest-->0){ int n=sc.nextInt(); char[] s=new char[n+5]; char[] t=new char[n+5]; String ss=sc.next(); String tt=sc.next(); s=ss.toCharArray(); t=tt.toCharArray(); int cntax = 0, cntbx = 0, same = 0; int ans=MOD9; for(int i=0; i 0) { solver.call(in,out); t--; } out.close(); } static class TaskA { public void call(InputReader in, PrintWriter out) { int n, _00 = 0, _01 = 0, _11 = 0, _10 = 0; n = in.nextInt(); char[] s = in.next().toCharArray(); char[] s1 = in.next().toCharArray(); for (int i = 0; i < n; i++) { if(s[i]==s1[i]){ if(s[i]=='0'){ _00++; } else{ _11++; } } else{ if(s[i]=='0'){ _01++; } else{ _10++; } } } int ans = Integer.MAX_VALUE; if(_10 ==_01){ ans = 2*_01; } if(_11 == _00 + 1){ ans = Math.min(ans, 2*_00 + 1); } if(ans == Integer.MAX_VALUE){ out.println(-1); } else{ out.println(ans); } } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class answer implements Comparable{ int a, b; public answer(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return o.a - this.a; } } static class arrayListClass { ArrayList arrayList2 ; public arrayListClass(ArrayList arrayList) { this.arrayList2 = arrayList; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] a) { ArrayList l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i0) { int n=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 b2590225,f7fc2e94,"//package com.company; import java.util.*; public class P3 { public static class tower implements Comparable{ ArrayList index; int size; tower(){ size = 0; index = new ArrayList<>(); } @Override public int compareTo(tower a) { if(this.size > a.size) { return 1; } else if (this.size < a.size) { return -1; } else { return 0; } } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t>0){ t--; int n = scan.nextInt(); int m = scan.nextInt(); int x = scan.nextInt(); ArrayList arr = new ArrayList<>(); HashMap map = new HashMap<>(); for(int i = 0;i towers = new PriorityQueue<>(); for(int i = 0;i 0){ tower curr = towers.poll(); for(Integer p : curr.index){ ans[p] = count; } count++; } for(int i = 0;i { int h,index; height(int hi,int i) { h=hi; index=i; } public int compareTo(height a) { return this.h-a.h; } public String toString() { return ""(""+this.h+"",""+this.index+"") ""; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc=new FastReader(); int T=sc.nextInt(); while(T-->0) { int n=sc.nextInt(),m=sc.nextInt(),x=sc.nextInt(),i; ArrayList arr=new ArrayList<>(); int ans[]=new int[n]; PriorityQueue tower=new PriorityQueue(); for(i=0;i 0){ int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } ",1 bf85ab7b,e48933fa," 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(); } } ","import java.io.*; import java.util.*; /* */ public class C { static FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); Node nodes[]=new Node[n]; for(int i=0;i adj=new ArrayList<>(); Node(int id,int l,int r){ this.id=id; this.l=l; this.r=r; } } static void ruffleSort(int a[]) { ArrayList al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al,Collections.reverseOrder()); for(int i=0;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.util.*; public class mentor1 { public static boolean solve(int n, String color, int[] arr){ List Barr = new ArrayList(); List Rarr = new ArrayList(); for (int i = 0; i < n; i++) { if(color.charAt(i) == 'B')Barr.add(arr[i]); else Rarr.add(arr[i]); } Barr.sort(Comparator.naturalOrder()); Rarr.sort(Comparator.reverseOrder()); for (int i = 0; i < Barr.size(); i++) { if(Barr.get(i)< i + 1)return false; } for (int i = 0; i < Rarr.size(); i++) { int expect = n-i; if(Rarr.get(i) > expect)return false; } return true; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); for (int i = 0; i < n; i++) { int m = input.nextInt(); int[] arr = new int[m]; for(int j = 0;j { 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[] temp = new LinkedList[vertices+1]; for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>(); for(int i = 0; i < edges; i++) { int x = sc.nextInt(); int y = sc.nextInt(); temp[x].add(y); } return temp; } static LinkedList[] graphUD(int vertices, int edges) { LinkedList[] temp = new LinkedList[vertices+1]; for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>(); for(int i = 0; i < edges; i++) { int x = sc.nextInt(); int y = sc.nextInt(); temp[x].add(y); temp[y].add(x); } return temp; } static void printG(LinkedList[] temp) { for(LinkedList aa:temp) System.out.println(aa); } static long cal(long val, long pow, long mod) { if(pow == 0) return 1; long res = cal(val, pow/2, mod); long ret = (res*res)%mod; if(pow%2 == 0) return ret; return (val*ret)%mod; } static long gcd(long a, long b) { return b == 0 ? a:gcd(b, a%b); } } static int mod = (int) 1e9; static LinkedList[] temp, idx; static long inf = (long) Long.MAX_VALUE; // static long inf = Long.MAX_VALUE; // static int max; public static void main(String[] args) { int t = sc.nextInt(); // int t = 1; StringBuilder ret = new StringBuilder(); while(t-- > 0) { int n = sc.nextInt(); String a = sc.next(), b = sc.next(); int _00 = 0, _01 = 0, _10 = 0, _11 = 0; for(int i = 0; i < n; i++) { if(a.charAt(i) == '0' && b.charAt(i) == '0') _00++; if(a.charAt(i) == '0' && b.charAt(i) == '1') _01++; if(a.charAt(i) == '1' && b.charAt(i) == '0') _10++; if(a.charAt(i) == '1' && b.charAt(i) == '1') _11++; } int ans = Integer.MAX_VALUE; if(_10 == _01) ans = _01 + _10; if(_10 > 0) { int n00 = _10 - 1; int n01 = _11; int n10 = _00 + 1; int n11 = _01; if (n01 == n10) { ans = Math.min(ans, 1 + n01 + n10); } } if(_11 > 0) { int n00 = _10; int n01 = _11 - 1; int n10 = _00; int n11 = _01 + 1; if (n01 == n10) { ans = Math.min(ans, 1 + n01 + n10); } } ret.append(ans == Integer.MAX_VALUE ? -1 : ans); ret.append(""\n""); } System.out.println(ret); } } ",0 1dab88fb,7b5f64b7,"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.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; } } public static FastReader obj = new FastReader(); public static PrintWriter out = new PrintWriter(System.out); public static void sort(long[] a) { ArrayList arr = new ArrayList<>(); for (int i = 0; i < a.length; i++) arr.add(a[i]); Collections.sort(arr); for (int i = 0; i < arr.size(); i++) a[i] = arr.get(i); } public static void revsort(long[] a) { ArrayList arr = new ArrayList<>(); for (int i = 0; i < a.length; i++) arr.add(a[i]); Collections.sort(arr, Collections.reverseOrder()); for (int i = 0; i < arr.size(); i++) a[i] = arr.get(i); } //Cover the small test cases like for n=1 . public static class pair { int a; int b; pair(int x, int y) { a = x; b = y; } } public static long l() { return obj.nextLong(); } public static int i() { return obj.nextInt(); } public static String s() { return obj.next(); } public static long[] l(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = l(); return arr; } public static int[] i(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = i(); return arr; } public static long ceil(long a, long b) { return (a + b - 1) / b; } public static void p(long val) { out.println(val); } public static void p(String s) { out.println(s); } public static void pl(long[] arr) { for (int i = 0; i < arr.length; i++) { out.print(arr[i] + "" ""); } out.println(); } public static void p(int[] arr) { for (int i = 0; i < arr.length; i++) { out.print(arr[i] + "" ""); } out.println(); } public static void sortpair(ArrayList arr) { //ascending just change return 1 to return -1 and vice versa to get descending. //compare based on value of pair.a arr.sort(new Comparator() { public int compare(pair o1, pair o2) { long val = o1.a - o2.a; if (val == 0) return 0; else if (val > 0) return 1; else return -1; } }); } // Take of the small test cases such as when n=1,2 etc. // remember in case of fenwick tree ft is 1 based but our array should be 0 based. // in fenwick tree when we update some index it doesn't change the value to val but it // adds the val value in it so remember to add val-a[i] instead of just adding val. //in case of finding the inverse mod do it (biexpo(a,mod-2)%mod + mod )%mod public static ArrayList> adj; public static int[] ans; public static void main(String[] args) { int len = i(); while (len-- != 0) { int n = i(); adj=new ArrayList<>(n+1); ans=new int[n]; int ok=0; for(int i=0;i<=n;i++)adj.add(new ArrayList<>()); for(int i=0;i2)ok=1; if(adj.get(b).size()>2)ok=1; } if(ok==1)out.println(-1); else { int[] vis=new int[n+1]; if(adj.get(1).size()==2) { vis[adj.get(1).get(1).a]=1; dfs(1,vis,11); vis[adj.get(1).get(1).a]=0; vis[1]=0; dfs(1,vis,2); } else { dfs(1,vis,11); } for(int i=0;i= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static int 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.io.*; import java.util.*; public class _1615c { FastScanner scn; PrintWriter w; PrintStream fs; long MOD = 1000000007; int MAX = 200005; long mul(long x, long y) {long res = x * y; return (res >= MOD ? res % MOD : res);} long power(long x, long y) {if (y < 0) return 1; long res = 1; x %= MOD; while (y!=0) {if ((y & 1)==1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;} void ruffleSort(int[] a) {int n=a.length;Random r=new Random();for (int i=0; i list = new ArrayList<>();for (int i=0; i0) { int n=scn.nextInt(); String s1= scn.next(); String s2 = scn.next(); int op1=0,op0=0,one=0,zr=0; 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; } 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; } 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; } void swap(int[] arr, int i, int j){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;} long nextPowerOf2(long v){if (v == 0) return 1;v--;v |= v >> 1;v |= v >> 2;v |= v >> 4;v |= v >> 8;v |= v >> 16;v |= v >> 32;v++;return v;} int gcd(int a, int b) {if(a == 0){return b;}return gcd(b%a, a);} // TC- O(logmax(a,b)) 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);}int i = last + 1, j = arr.length - 1;while(i < j){swap(arr, i++, j--);}return true;} public static void main(String[] args) { new _1615c().runIO(); } } ",0 0c1143f7,960a4c0f,"import java.io.*; import java.util.*; public class E { public static void main(String[] args) { // TODO Auto-generated method stub FastScanner sc = new FastScanner(); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int[] a = new int[n]; for(int i = 0 ; i< n ; i++) { a[i] = sc.nextInt(); } long oddMin = a[1]; long evenMin = a[0]; long res = (n * oddMin) + (n * evenMin); long oddSum = a[1]; long evenSum = a[0]; for(int i = 2 ;i < n ; i++) { if(i % 2 == 1) { oddSum += a[i]; oddMin = Math.min(oddMin, a[i]); } else { evenSum += a[i]; evenMin = Math.min(evenMin, a[i]); } int odd = (i + 1) / 2; int even = (i / 2) + 1; long minCostOdd = oddSum + oddMin*(n - odd); long minCostEven = evenSum + evenMin*(n - even); res = Math.min(res, minCostOdd + minCostEven); } System.out.println(res); } } 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 = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } if(check(arr)) System.out.println(""YES""); else System.out.println(""NO""); } } static boolean check(int[] arr) { int n = arr.length; TreeSet set = new TreeSet<>(); set.add(arr[0]); for(int i = 1; i < n; i++) { set.add(arr[i]); if(arr[i-1] == arr[i]) continue; Integer x = set.lower(arr[i]); if(x != null && x == arr[i-1]) continue; x = set.higher(arr[i]); if(x != null && x == arr[i-1]) continue; return false; } return true; } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } } ","import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; //import javafx.util.*; public final class B { static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList> g; static long mod=1000000007; static int D1[],D[],par[]; static boolean set[]; static long INF=Long.MAX_VALUE; public static void main(String args[])throws IOException { int T=i(); while(T-->0) { int N=i(); long A[]=inputLong(N); boolean f=true; TreeSet set=new TreeSet<>(); for(int i=0; i=2) { if(A[i]A[i-1]) { if(set.floor(A[i]-1)!=A[i-1])f=false; } } set.add(A[i]); } if(f)System.out.println(""YES""); else System.out.println(""NO""); } } static int f2(int l,int r,int index) { while(r-l>1) { int m=(l+r)/2; if(ask(index,m)==index)r=m; else l=m; } return r; } static long fact(long N) { long num=1L; while(N>=1) { num=((num%mod)*(N%mod))%mod; N--; } return num; } static boolean reverse(long A[],int l,int r) { while(l l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i>(); for(int i=0; i<=N; i++) { D[i]=N+1; g.add(new ArrayList()); } } static long pow(long a,long b) { long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1< l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i A) { for(int a:A)System.out.print(a+"" ""); System.out.println(); } //Debugging Functions END //---------------------- //IO FUNCTIONS STARTS static HashMap Hash(int A[]) { HashMap mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i{ // int index; long a; // pair(long a,int index) // { // this.a=a; // this.index=index; // } // public int compareTo(pair X) // { // if(this.a>X.a)return 1; // if(this.a==X.a)return this.index-X.index; // return -1; // } //} //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } //gey double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }",0 32ff22fe,f0d91796,"import java.io.*; import java.util.*; public class MyClass { public static void pA(int[]a){ for(int i=0;i0){ int n=Integer.parseInt(br.readLine()); String s[]=br.readLine().split("" ""); int arr[]=new int[n]; int a[]=new int[n]; for(int i=0;i 0){ int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } ",0 36f1b52a,558df7d4,"/* ""Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up."" */ import java.util.*; import java.lang.*; import java.io.*; public class Solution { public static void main(String[] args) throws java.lang.Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new FastReader(); int test = sc.nextInt(); for (int t = 0; t < test; t++) { solve(); } out.close(); } private static void solve() { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int[] res = new int[n]; if (n % 2 == 0) { for (int i = 1; i < n; i += 2) { res[i] = arr[i - 1]; res[i - 1] = -arr[i]; } }else { for (int i = 4; i < n; i += 2) { res[i] = arr[i - 1]; res[i - 1] = -arr[i]; } if (arr[0] + arr[1] != 0) { res[0] = -arr[2]; res[1] = -arr[2]; res[2] = arr[0] + arr[1]; }else if (arr[0] + arr[2] != 0) { res[0] = -arr[1]; res[2] = -arr[1]; res[1] = arr[0] + arr[2]; }else { res[1] = -arr[0]; res[2] = -arr[0]; res[0] = arr[1] + arr[2]; } } for (int i = 0; i < n; i++) { out.print(res[i] + "" ""); } out.println(); } 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; } } }","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; tc0){ int n=scanInt(); int []arr=new int[n]; int low=Integer.MAX_VALUE; int high=0; for (int i=0;i=2;i--){ if(temp[i] 0) { solve(); } out.flush(); out.close(); } static boolean bs(int[] arr, int a){ // out.println("" mid1 "" + Arrays.toString(arr) + "" "" + a); int[] curr = Arrays.copyOf(arr, arr.length); for(int i = arr.length - 1; i >= 2; i--){ if(arr[i] < a){ return false; } int min = Math.min(arr[i] - a, curr[i]); min = min/3; arr[i] -= 3*min; arr[i - 1] += min; arr[i-2] += 2*min; } for(int i: arr){ if(i < a){ return false; } } return true; } public static void solve() { int n = sc.nextInt(); int[] arr = new int[n]; int max = Integer.MIN_VALUE; for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); max = Math.max(max, arr[i]); } int l = 0, r = max, ans = 0; while(l <= r){ int mid = (l+r)/2; // out.println("" l r "" + l + "" "" + r); int[] aa = Arrays.copyOf(arr, arr.length); if(bs(aa, mid)){ // out.println(Arrays.toString(arr) + "" "" + Arrays.toString(aa) + "" mid "" + mid ); ans = mid; l = mid+1; }else{ r = mid-1; } } out.println(ans); } public static long leftShift(long a) { return (long) Math.pow(2, a); } public static void reverse(int[] arr) { Arrays.sort(arr); int n = arr.length; for (int i = 0; i < arr.length; i++) { int temp = arr[i]; arr[i] = arr[n - 1 - i]; arr[n - 1 - i] = temp; } return; } public static int lower_bound(ArrayList ar, int k) { int s = 0, e = ar.size(); while (s != e) { int mid = s + e >> 1; if (ar.get(mid) <= k) { s = mid + 1; } else { e = mid; } } return Math.abs(s) - 1; } public static int upper_bound(ArrayList ar, int k) { int s = 0; int e = ar.size(); while (s != e) { int mid = s + e >> 1; if (ar.get(mid) < k) { s = mid + 1; } else { e = mid; } } if (s == ar.size()) { return -1; } return s; } static class Kioken { // FileInputStream br = new FileInputStream(""input.txt""); BufferedReader br; StringTokenizer st; Kioken(String filename) { try { FileReader fr = new FileReader(filename); br = new BufferedReader(fr); st = new StringTokenizer(""""); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } Kioken() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""""); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } public boolean hasNext() { String next = null; try { next = br.readLine(); } catch (Exception e) { } if (next == null || next.length() == 0) { return false; } st = new StringTokenizer(next); return true; } } }",0 0588b869,9028caf7,"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 list1 = new ArrayList<>() ; static ArrayList list2 = new ArrayList<>() ; static int n , m ; static long dp[][] ; static long solver(int i , int j ){ // i = empty chairs if (j == m)return 0 ; int tt1 = n-i ; int tt2 = m-j ; if (n-i < m-j)return Long.MAX_VALUE/2 ; if ( dp[i][j] != -1 )return dp[i][j] ; long a = solver(i+1 , j) ; long b = abs( list1.get(i) - list2.get(j)) + solver(i+1 , j+1) ; return dp[i][j] = min(a , b) ; } public static void main(String[] args) throws IOException { // try { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int N = in.nextInt() ; int a[] = in.readArray(N) ; for (int i = 0; i list = new ArrayList<>(); static boolean A[] = new boolean[2 * 90000001]; static void seive(int n) { int maxn = n; //int maxn = 1000000 ; A[0] = A[1] = true; for (int i = 2; i * i <= maxn; i++) { if (!A[i]) { for (int j = i * i; j <= maxn; j += i) A[j] = true; } } for (int i = 2; i <= maxn; i++) if (!A[i]) list.add(i); } static int findLCA(int a, int b, int par[][], int depth[]) { if (depth[a] > depth[b]) { a = a ^ b; b = a ^ b; a = a ^ b; } int diff = depth[b] - depth[a]; for (int i = 19; i >= 0; i--) { if ((diff & (1 << i)) > 0) { b = par[b][i]; } } if (a == b) return a; for (int i = 19; i >= 0; i--) { if (par[b][i] != par[a][i]) { b = par[b][i]; a = par[a][i]; } } return par[a][0]; } static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } static void formArrayForBinaryLifting(int n, int par[][]) { for (int j = 1; j < 20; j++) { for (int i = 0; i < n; i++) { if (par[i][j - 1] == -1) continue; par[i][j] = par[par[i][j - 1]][j - 1]; } } } static void sort(int ar[]) { int n = ar.length; ArrayList a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static void sort1(long ar[]) { int n = ar.length; ArrayList a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static long ncr(long n, long r, long mod) { if (r == 0) return 1; long val = ncr(n - 1, r - 1, mod); val = (n * val) % mod; val = (val * modInverse(r, mod)) % mod; return val; } static long fast_pow(long base, long n, long M) { if (n == 0) return 1; if (n == 1) return base % M; long halfn = fast_pow(base, n / 2, M); if (n % 2 == 0) return (halfn * halfn) % M; else return (((halfn * halfn) % M) * base) % M; } static long modInverse(long n, long M) { return fast_pow(n, M - 2, M); } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } static class FastOutput implements AutoCloseable, Closeable, Appendable { private static final int THRESHOLD = 32 << 10; private final Writer os; private StringBuilder cache = new StringBuilder(THRESHOLD * 2); public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } private void afterWrite() { if (cache.length() < THRESHOLD) { return; } flush(); } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); afterWrite(); return this; } public FastOutput append(long c) { cache.append(c); afterWrite(); return this; } public FastOutput append(String c) { cache.append(c); afterWrite(); return this; } public FastOutput println() { return append(System.lineSeparator()); } public FastOutput flush() { try { // boolean success = false; // if (stringBuilderValueField != null) { // try { // char[] value = (char[]) stringBuilderValueField.get(cache); // os.write(value, 0, cache.length()); // success = true; // } catch (Exception e) { // } // } // if (!success) { os.append(cache); // } os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } } ","import javax.print.DocFlavor; import java.util.*; import java.lang.*; import java.io.*; public class Solution { static int N = 5005; static int[] arr = new int[N]; static long[][] memo = new long[N][N]; static List occupiedSeats = new ArrayList<>(); static List emptySeats = new ArrayList<>(); static int n, occSize, empSize; public static void main(String[] args) throws java.lang.Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new FastReader(); int test = 1; for (int t = 0; t < test; t++) { solve(); } out.close(); } private static void solve() { n = sc.nextInt(); for (int i = 1; i <= n; i++) { arr[i] = sc.nextInt(); if (arr[i] == 1) { occupiedSeats.add(i); }else { emptySeats.add(i); } } occSize = occupiedSeats.size(); empSize = emptySeats.size(); for (long[] memset : memo) { Arrays.fill(memset, -1); } out.println(minimumTime(0, 0)); } private static long minimumTime(int occupied, int empty) { if (occupied == occSize) { return 0; } if (empty == empSize) { return Integer.MAX_VALUE; } if (memo[occupied][empty] != -1) { return memo[occupied][empty]; } long curr = Math.abs(occupiedSeats.get(occupied) - emptySeats.get(empty)) + minimumTime(occupied + 1, empty + 1); curr = Math.min(curr, minimumTime(occupied, empty + 1)); memo[occupied][empty] = curr; return curr; } public static FastReader sc; public static PrintWriter out; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }",0 584b0e9e,722e318f," 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.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.LongStream; public class TaskB { static long mod = 1000000007; static FastScanner scanner; static final StringBuilder result = new StringBuilder(); public static void main(String[] args) { // 2 : 1000000000 scanner = new FastScanner(); int T = scanner.nextInt(); for (int t = 0; t < T; t++) { solve(t + 1); result.append(""\n""); } System.out.println(result); } static void solve(int t) { int n = scanner.nextInt(); int[] a = scanner.nextIntArray(n); String s = scanner.nextToken(); List 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 */",0 99bc7da3,ced4639e,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Codeforces { static int mod =1000000007; static Set set; 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[]=new int[n]; // set=new HashSet<>(); for(int i=0;in) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long pow(long a,long b) { long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i0) { int n=fs.nextInt(); int arr[]=fs.readArray(n); int l=0, r=1000000000; while(l1;i--) { if(brr[i]+arr[i]=min)&&(arr[1]+brr[1]>=min); } static long pow(long a,long b) { if(b<0) return 1; long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } static long nck(int n,int k) { if(k>n) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { // return fact[(int)n]; long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i0) { int n = fs.nextInt(); int[] h = fs.readArray(n); int l=1, r=(int)1e9; int ans = -1; while(l<=r) { int mid = (l+r)/2; if(isPossible(mid, h, n)) { ans = mid; l = mid+1; }else { r= mid-1; } } out.println(ans); } out.close(); } static boolean isPossible(int min, int[] h, int n) { int[] c = new int[n]; for(int i=n-1; i>=2; i--) { if(h[i]+c[i]-min<0)return false; int x = Math.min(h[i], h[i]+c[i]-min) / 3; c[i-1] += x; c[i-2] += 2*x; } return (h[0]+c[0]>=min&&h[1]+c[1]>=min); } //****** CODE ENDS HERE ***** //---------------------------------------------------------------------------------------------------------------- static void sort(int[] a) { ArrayList l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i0) { int n=sc.nextInt(); int k[]=new int[n]; int h[]=new int[n]; for(int i=0;i interval=new ArrayList(); ArrayList act=new ArrayList(); for(int i=0;i { int x,y; Pair(int a,int b) { this.x=a; this.y=b; } // @Override // public boolean equals(Object obj) // { // if(obj instanceof Pair) // { // Pair p=(Pair)obj; // return p.x==this.x && p.y==this.y; // } // return false; // } // @Override // public int hashCode() // { // return Math.abs(x)+500*Math.abs(y); // } // @Override // public String toString() // { // return ""(""+x+"" ""+y+"")""; // } @Override protected Pair clone() throws CloneNotSupportedException { return new Pair(this.x,this.y); } @Override public int compareTo(Pair a) { long t=(this.x-a.x); if(t!=0) return t>0?1:-1; else return (int)(this.y-a.y); } // public void swap() // { // this.y=this.y+this.x; // this.x=this.y-this.x; // this.y=this.y-this.x; // } } }"," 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 316c9955,e7370eec,"import java.io.DataInputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.LinkedList; public class C { private static boolean[] visited; private static Node[] nodes; private static void run() throws IOException { int n = in.nextInt(); nodes = new Node[n]; for (int i = 0; i < n; i++) { nodes[i] = new Node(); nodes[i].index = i; nodes[i].l = in.nextInt(); nodes[i].r = in.nextInt(); } for (int i = 0; i < n - 1; i++) { int x = in.nextInt() - 1; int y = in.nextInt() - 1; nodes[x].next.add(y); nodes[y].next.add(x); } visited = new boolean[n]; dfs(0); out.println(Math.max(nodes[0].ans_l, nodes[0].ans_r)); } private static void dfs(int now) { visited[now] = true; for (int next : nodes[now].next) { if (visited[next]) continue; dfs(next); nodes[now].ans_l += Math.max(nodes[next].ans_l + Math.abs(nodes[now].l - nodes[next].l), nodes[next].ans_r + Math.abs(nodes[now].l - nodes[next].r)); nodes[now].ans_r += Math.max(nodes[next].ans_l + Math.abs(nodes[now].r - nodes[next].l), nodes[next].ans_r + Math.abs(nodes[now].r - nodes[next].r)); } } static class Node { int index; long l, r; long ans_l, ans_r; LinkedList next = new LinkedList<>(); } public static void main(String[] args) throws IOException { in = new Reader(); out = new PrintWriter(new OutputStreamWriter(System.out)); int t = in.nextInt(); for (int i = 0; i < t; i++) { run(); } out.flush(); in.close(); out.close(); } private static int gcd(int a, int b) { if (a == 0 || b == 0) return 0; while (b != 0) { int tmp; tmp = a % b; a = b; b = tmp; } return a; } static final long mod = 1000000007; static long pow_mod(long a, long b) { long result = 1; while (b != 0) { if ((b & 1) != 0) result = (result * a) % mod; a = (a * a) % mod; b >>= 1; } return result; } private static long multiplied_mod(long... longs) { long ans = 1; for (long now : longs) { ans = (ans * now) % mod; } return ans; } @SuppressWarnings(""FieldCanBeLocal"") private static Reader in; private static PrintWriter out; private static int[] read_int_array(int len) throws IOException { int[] a = new int[len]; for (int i = 0; i < len; i++) { a[i] = in.nextInt(); } return a; } private static long[] read_long_array(int len) throws IOException { long[] a = new long[len]; for (int i = 0; i < len; i++) { a[i] = in.nextLong(); } return a; } private static void print_array(int[] array) { for (int now : array) { out.print(now); out.print(' '); } out.println(); } private static void print_array(long[] array) { for (long now : array) { out.print(now); out.print(' '); } out.println(); } static class Reader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { final byte[] buf = new byte[1024]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextSign() throws IOException { byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() throws IOException { int b; // noinspection ALL while ((b = read()) != -1 && isSpaceChar(b)) { ; } return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } }","//package codeforces.round722div2; import java.io.*; import java.util.*; public class C { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); solve(in.nextInt()); //solve(1); } static int n; static List[] adj; static long[] l, r; static void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { n = in.nextInt(); adj = new List[n + 1]; for(int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } l = new long[n + 1]; r = new long[n + 1]; for(int i = 1; i <= n; i++) { l[i] = in.nextInt(); r[i] = in.nextInt(); } for(int i = 0; i < n - 1; i++) { int u = in.nextInt(); int v = in.nextInt(); adj[u].add(v); adj[v].add(u); } long[] ans = dfs(1, 0); out.println(Math.max(ans[0], ans[1])); } out.close(); } static long[] dfs(int curr, int par) { long[] ans = new long[2]; for(int next : adj[curr]) { if(next == par) continue; long[] nextAns = dfs(next, curr); ans[0] += Math.max(nextAns[0] + Math.abs(l[curr] - l[next]), nextAns[1] + Math.abs(l[curr] - r[next])); ans[1] += Math.max(nextAns[0] + Math.abs(r[curr] - l[next]), nextAns[1] + Math.abs(r[curr] - r[next])); } return ans; } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream(""src/input.in"")); out = new PrintWriter(new FileOutputStream(""src/output.out"")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } } }",0 26e699de,e99c14b9,"//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 al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i 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.io.*; import java.util.*; public class ArmChairs { public static int solution(int n, int[] arr) { ArrayList one = new ArrayList(); ArrayList zero = new ArrayList(); for (int i = 0; i < n; i++) { if (arr[i] == 1) { one.add(i); } else { zero.add(i); } } int[][] dp = new int[one.size() + 1][zero.size() + 1]; for (int i = 1; i <= one.size(); i++) { dp[i][i] = dp[i - 1][i - 1] + Math.abs(one.get(i - 1) - zero.get(i - 1)); for (int j = i + 1; j <= zero.size(); j++) { dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j - 1] + Math.abs(one.get(i - 1) - zero.get(j - 1))); } } return dp[one.size()][zero.size()]; } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split("" ""); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(s[i]); } log.write(Integer.toString(solution(n, arr)) + ""\n""); log.flush(); } }",1 2120328e,b790ef12,"/***** ---> :) 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 int mod=10000007; static StringBuilder sb=new StringBuilder(); /* start */ public static void main(String [] args) { int t = i(); while(t-->0) { int n = i(); int a[] = input(n); char c[] = inputC(); ArrayList b = new ArrayList<>(); ArrayList r = new ArrayList<>(); for(int i=0;in-i) { is = false; break; } } out.println(is==true?""YES"":""NO""); } out.close(); } /* end */ 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 i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; 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; } //pair class private static class Pair implements Comparable { int first, second; public Pair(int f, int 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; } } } }","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 6e207cbf,e19376ca,"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); } } } } ","import java.io.*; import java.util.*; public class new1{ public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } public static int count(int[] arr, int[] dest) { int n = arr.length; int count = 0; for(int i = 0; i < n; i++) { if(arr[i] != dest[i]) count++; } return count; } public static void main(String[] args) throws IOException{ BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); FastReader s = new FastReader(); int t = s.nextInt(); for(int z = 0; z < t; z++) { int n = s.nextInt(); String str1 = s.next(); String str2 = s.next(); int count1 = 0; int count2 = 0; for(int i = 0; i < n; i++) { if(str1.charAt(i) == '1') count1++; if(str2.charAt(i) == '1') count2++; } if(!(count1 == count2 || n - count1 + 1 == count2)) { System.out.println(-1); continue; } int[] arr = new int[n]; int[] dest = new int[n]; for(int i = 0; i < n; i++) { arr[i] = str1.charAt(i) - '0'; dest[i] = str2.charAt(i) - '0'; } int ans1 = count(arr, dest); int ans2 = Integer.MAX_VALUE; int flag = 0; for(int i = 0; i < n; i++) { if(flag == 0 && arr[i] == 1 && dest[i] == 1) { flag = 1; } else { arr[i] = 1 - arr[i]; } } //System.out.println(ans1); if(flag == 1) { ans2 = Math.min(ans2, count(arr, dest) + 1); } else { for(int i = 0; i < n; i++) { if(str1.charAt(i) == '1' && flag == 0) { flag = 1; arr[i] = 1; break; } } } if(flag == 1) ans2 = Math.min(ans2, count(arr, dest) + 1); int ans = Integer.MAX_VALUE; if(count1 == count2) ans = Math.min(ans, ans1); if(n - count1 + 1 == count2) ans = Math.min(ans, ans2); System.out.println(ans); } //output.flush(); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; }}",0 9028caf7,a4e39423,"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 a = new ArrayList<>(); ArrayList b = new ArrayList<>(); for(int i = 0; i < n; i++){ if(arr[i] == 1){ a.add(i); }else{ b.add(i); } } if(a.size() == 0){ System.out.println(""0""); return; } int [][] dp = new int[a.size()][b.size()]; for(int i = 0; i < a.size(); i++){ for(int j = i; j < b.size(); j++) { if (j == 0) { dp[i][j] = Math.abs(a.get(i) - b.get(j)); } else if (i == 0) { dp[i][j] = Math.min(dp[i][j - 1], Math.abs(a.get(i) - b.get(j))); } else if (i == j) { dp[i][j] = dp[i - 1][j - 1] + Math.abs(a.get(i) - b.get(j)); } else { dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j - 1] + Math.abs(a.get(i) - b.get(j))); } } } System.out.println(dp[a.size() - 1][b.size() - 1]); } static int readInt() throws IOException { return Integer.parseInt(br.readLine()); } static long readLong() throws IOException { return Long.parseLong(br.readLine()); } static int[] readIntarray() throws IOException { String[] _a = br.readLine().split("" ""); int[] _res = new int[_a.length]; for (int i = 0; i < _a.length; i++) { _res[i] = Integer.parseInt(_a[i]); } return _res; } static long[] readLongarray() throws IOException { String[] _a = br.readLine().split("" ""); long[] _res = new long[_a.length]; for (int i = 0; i < _a.length; i++) { _res[i] = Long.parseLong(_a[i]); } return _res; } } ",0 43855bfe,ddcadc64,"import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; public class O4 { private static String solve(int n, int[] a) { if (n == 2) { return a[1] + "" "" + (-a[0]); } int res[] = new int[n]; if (n % 2 == 0) { diagonalAlignment(a, res, n); } else { diagonalAlignment(a, res, n - 3); if ( a[n - 3] + a[n - 2] != 0) { res[n - 1] = a[n - 3] + a[n - 2]; res[n - 2] = -a[n - 1]; res[n - 3] = -a[n - 1]; } else if (a[n - 1] + a[n - 2] != 0){ res[n - 3] = a[n - 1] + a[n - 2]; res[n - 2] = -a[n - 3]; res[n - 1] = -a[n - 3]; } else { res[n - 2] = a[n - 1] + a[n - 3]; res[n - 1] = -a[n - 2]; res[n - 3] = -a[n - 2]; } } return formattedArray(res); } private static void diagonalAlignment(int[] a, int[] res, int n) { for (int i = 0; i < n; i += 2) { res[i + 1] = -a[i]; res[i] = a[i + 1]; } } public static void main(String[] args) throws IOException { Scanner s = new Scanner(); int t = 1; t = s.nextInt(); StringBuilder ans = new StringBuilder(); int count = 0; while (t-- > 0) { int n = s.nextInt(); int a[] = new int[n]; getInputs(s, a, n); ans.append(solve(n, a)).append(""\n""); } System.out.println(ans.toString()); } static class Scanner { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Scanner() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Scanner(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 norm(long a, long MOD) { return ((a % MOD) + MOD) % MOD; } public static long msub(long a, long b, long MOD) { return norm(norm(a, MOD) - norm(b, MOD), MOD); } public static long madd(long a, long b, long MOD) { return norm(norm(a, MOD) + norm(b, MOD), MOD); } public static long mMul(long a, long b, long MOD) { return norm(norm(a, MOD) * norm(b, MOD), MOD); } public static long mDiv(long a, long b, long MOD) { return norm(norm(a, MOD) / norm(b, MOD), MOD); } public static String formattedArray(int a[]) { StringBuilder res = new StringBuilder(""""); for (int e : a) res.append(e).append("" ""); return res.toString().trim(); } private static void getInputs(Scanner s, int[] a, int n) throws IOException { for (int i = 0; i < n; i++) { a[i] = s.nextInt(); } } } ","import java.util.Scanner; public class D { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int n = scn.nextInt(); int[] arr = new int[n]; int[] ans = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scn.nextInt(); } if (n % 2 != 0) { if (arr[n - 2] + arr[n - 3] == 0) { ans[n - 3] = -2 * arr[n - 1]; ans[n - 2] = -arr[n - 1]; ans[n - 1] = arr[n - 2] + arr[n - 3] * 2; } else { ans[n - 3] = -arr[n - 1]; ans[n - 2] = -arr[n - 1]; ans[n - 1] = arr[n - 2] + arr[n - 3]; } n -= 3; } for (int i = 0; i < n; i += 2) { int div = 1; if (arr[i] % 2 == 0 && arr[i + 1] % 2 == 0) div = 2; ans[i] = arr[i + 1] / div; ans[i + 1] = -arr[i] / div; } for (int v : ans) { sb.append(v); sb.append("" ""); } sb.append(""\n""); } System.out.println(sb); } } ",0 7279bcb3,cfba313d,"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(); } } ","import java.io.*; import java.util.*; public class C {//Any Class Name static class Code { private void solve(InputReader in, OutputWriter out) throws IOException { ArrayOpn o= new ArrayOpn(); int t = in.readInt(); while(t -- > 0) { int n=in.readInt(); int[][] freq= new int[5][n]; for(int i=0; i=0; j--) { temp+=freq[i][j]; if(temp<=0) { break; } count++; } ans=Math.max(ans, count); } out.printLine(ans); } out.close(); } } static class ArrayOpn{ private int[] aI(InputReader in, int n) { int A[]= new int[n]; for(int i=0; i{ private final F first; //first member of pair private final S second; //second member of pair private Pair(F first, S second) { this.first = first; this.second = second; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; if(!first.equals(pair.first)) return false; return second.equals(pair.second); } @Override public int hashCode() { return 31 * first.hashCode() + second.hashCode(); } @Override public String toString() { return ""("" + first +"", ""+ second +"")""; } public static Pair of(F a, S b) { return new Pair<>(a,b); } public F getFirst() { return first; } public S getSecond() { return second; } } // Comparator for using in Sorting On integers static class PairCompare implements Comparator> { @Override public int compare(Pair p1, Pair p2) { int diff = Integer.compare(p1.first, p2.first); if(diff == 0) { return Integer.compare(p1.second, p2.second); } else return diff; } } public static void main(String[] args) throws IOException { //initialize InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); Code solver = new Code(); solver.solve(in, out); out.flush(); out.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private InputReader(InputStream stream) { this.stream = stream; } private 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++]; } private String readString() { 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(); } private int readInt() { 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; } private long readLong() { 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; } private double readDouble() { 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, readInt()); } 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, readInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } private boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public String next() { return readString(); } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; private OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter( outputStream))); } private OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } private void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } writer.flush(); } private void printLine(Object... objects) { print(objects); writer.println(); writer.flush(); } private void printLine(int i) { writer.println(i); } private void close() { writer.close(); } private void flush() { writer.flush(); } } static class IOUtils { private static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.readInt(); return array; } private static long[] readLongArray(InputReader in, int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) array[i] = in.readLong(); return array; } private static char[] readCharArray(InputReader in, int size) { char[] array = new char[size]; for (int i = 0; i < size; i++) array[i] = in.readCharacter(); return array; } private static char[][] readTable(InputReader in, int rowCount, int columnCount) { char[][] table = new char[rowCount][]; for (int i = 0; i < rowCount; i++) table[i] = readCharArray(in, columnCount); return table; } } static class ArrayUtils { private static void fill(int[][] array, int value) { for (int[] row : array) Arrays.fill(row, value); } } static class MiscUtils { private static final int[] DX4 = {1, 0, -1, 0}; private static final int[] DY4 = {0, -1, 0, 1}; private static boolean isValidCell(int row, int column, int rowCount, int columnCount) { return row >= 0 && row < rowCount && column >= 0 && column < columnCount; } } }",1 a4e39423,ba8800f5,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class _109D { static BufferedReader br; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); int n = readInt(); int arr[] = readIntarray(); ArrayList a = new ArrayList<>(); ArrayList b = new ArrayList<>(); for(int i = 0; i < n; i++){ if(arr[i] == 1){ a.add(i); }else{ b.add(i); } } if(a.size() == 0){ System.out.println(""0""); return; } int [][] dp = new int[a.size()][b.size()]; for(int i = 0; i < a.size(); i++){ for(int j = i; j < b.size(); j++) { if (j == 0) { dp[i][j] = Math.abs(a.get(i) - b.get(j)); } else if (i == 0) { dp[i][j] = Math.min(dp[i][j - 1], Math.abs(a.get(i) - b.get(j))); } else if (i == j) { dp[i][j] = dp[i - 1][j - 1] + Math.abs(a.get(i) - b.get(j)); } else { dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j - 1] + Math.abs(a.get(i) - b.get(j))); } } } System.out.println(dp[a.size() - 1][b.size() - 1]); } static int readInt() throws IOException { return Integer.parseInt(br.readLine()); } static long readLong() throws IOException { return Long.parseLong(br.readLine()); } static int[] readIntarray() throws IOException { String[] _a = br.readLine().split("" ""); int[] _res = new int[_a.length]; for (int i = 0; i < _a.length; i++) { _res[i] = Integer.parseInt(_a[i]); } return _res; } static long[] readLongarray() throws IOException { String[] _a = br.readLine().split("" ""); long[] _res = new long[_a.length]; for (int i = 0; i < _a.length; i++) { _res[i] = Long.parseLong(_a[i]); } return _res; } } ","import java.io.*; import java.util.*; import java.text.*; import java.math.*; public class Main { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int ar[] = new int[n]; for(int i = 0; i < n; i++){ ar[i] = Integer.parseInt(st.nextToken()); } ArrayList ones = new ArrayList(); ArrayList zeroes = new ArrayList(); for(int i = 0; i < n; i++){ if(ar[i] == 1) ones.add(i); else zeroes.add(i); } int r = ones.size(); int c = zeroes.size(); int time[][] = new int[r][c]; System.out.println(calculateTime(time, r, c, 0, 0, ones, zeroes)); } public static int calculateTime(int time[][], int r, int c, int currR, int currC, ArrayList ones, ArrayList zeroes){ // System.out.println(currR + "" "" + currC); if(currR == r) return 0; if(currC == c) return (int)1e9; if(time[currR][currC] != 0) return time[currR][currC]; return time[currR][currC] = Math.min((calculateTime(time, r, c, currR + 1, currC + 1, ones, zeroes) + Math.abs(ones.get(currR) - zeroes.get(currC))), calculateTime(time, r, c, currR, currC + 1, ones, zeroes)); } }",0 af2b138c,e45446bc,"import java.util.*; import java.lang.*; import java.io.*; public class ecf106 { //HashMap hm=new HashMap<>(); //ArrayList list=new ArrayList<>(); //HashSet hs=new HashSet<>(); //Stack st=new Stack<>(); //Queue q=new LinkedList<>(); //PriorityQueue pq=new PriorityQueue<>(); // static ArrayList> gp=new ArrayList<>(); //int[] arr=new int[n]; //System.out.println(""YES""); //1 hr- atleast 2 qs d-2 //Mt_do 3 qs d-2 //Mt_do 4 qs d-3 //StringBuilder sb=new StringBuilder(); public static void main(String[] args) throws Exception{ InputStreamReader ip=new InputStreamReader(System.in); BufferedReader br = new BufferedReader(ip); int t= Integer.parseInt(br.readLine()); while(t-->0){ int n=Integer.parseInt(br.readLine());//Integer.parseInt(strs[0]); String[] strs=(br.readLine()).trim().split("" ""); // int n=Integer.parseInt(strs[0]),k=Integer.parseInt(strs[1]);//,k=Integer.parseInt(strs[2]); // strs=(br.readLine()).trim().split("" ""); long[] arr=new long [n]; for(int i=0;i=hor){ //v.v.imp donot update horizontal min1+=arr[i]; }else{ min1+=hor; hor=arr[i]; } }else{ y++; if(arr[i]>=vert){ //v.v.imp donot update vertical min2+=arr[i]; }else{ min2+=vert; vert=arr[i]; } } long rem=(n-x)*hor+(n-y)*vert; ans=Math.min(ans, min1+min2+rem); } System.out.println(ans); } } } // public static void main(String[] args) throws Exception{ // InputStreamReader ip=new InputStreamReader(System.in); // BufferedReader br = new BufferedReader(ip); // int t= Integer.parseInt(br.readLine()); // while(t-->0){ // // int n=Integer.parseInt(br.readLine());//Integer.parseInt(strs[0]); // // String[] strs=(br.readLine()).trim().split("" ""); // // int n=Integer.parseInt(strs[0]),k=Integer.parseInt(strs[1]);//,k=Integer.parseInt(strs[2]); // // strs=(br.readLine()).trim().split("" ""); // String str=(br.readLine()).trim(); // char[] arr=str.toCharArray(); // int n=arr.length; // int i=0, l=0, r=n; // for (i=1; i=0; i--) // if (arr[i]==arr[i+1] && arr[i]=='1') // r=i; // if (l<=r) // System.out.println(""YES""); // else // System.out.println(""NO""); // // int cz=0,co=0; // // int lz=0; // // int n=str.length(); // // boolean fg1=true; // // for(int i=0;i0 && arr[i-1]>arr[i]){fg1=false; } // // } // // if(co==n || cz==n || fg1 || co==1 ||cz==1){ // // System.out.println(""YES""); continue; // // } // // // String s1=""""; // // // String s2=""""; // // StringBuilder sb1=new StringBuilder(); // // StringBuilder sb2=new StringBuilder(); // // for(int i=0;i0 && arr1[i-1]>arr1[i]){fg2=false; break;} // // } // // for(int i=0;i0 && arr2[i-1]>arr2[i]){fg3=false;} // // } // // if(fg2 ||fg3){ // // System.out.println(""YES""); // // }else{ // // System.out.println(""NO""); // // } // } // } // } // int az=0,ao=0; // ArrayList list=new ArrayList<>(); // for(int i=0;i1 && v2>1){fl=false; break;} // } // if(fl){System.out.println(""YES"");} // else{System.out.println(""NO"");} // } // } // } //c1 000001111 // boolean fg=true; // int last=-2; // for(int i=0;i<=lz;i++){ // if(arr[i]=='0'){ if((last+1)==i){fg=false; break;} last=i; } // } // if(fg){System.out.println(""YES""); continue;} // //c2 11111 // fg=true; // last=-2; // for(int i=0;i0){ // // int n=Integer.parseInt(br.readLine());//Integer.parseInt(strs[0]); // String[] strs=(br.readLine()).trim().split("" ""); // int n=Integer.parseInt(strs[0]),k1=Integer.parseInt(strs[1]),k2=Integer.parseInt(strs[2]);//,k=Integer.parseInt(strs[2]); // strs=(br.readLine()).trim().split("" ""); // int w=Integer.parseInt(strs[0]),b=Integer.parseInt(strs[1]); // int tw=k1+k2; // int tb=(2*n)-tw; // if((tw/2)>=w && (tb/2)>=b){ // System.out.println(""YES""); // }else{ // System.out.println(""NO""); // } // } // } // } "," import java.util.*; import java.lang.*; import java.io.*; public class C { // *** ++ // +=-==+ +++=- // +-:---==+ *+=----= // +-:------==+ ++=------== // =-----------=++========================= // +--:::::---:-----============-=======+++==== // +---:..:----::-===============-======+++++++++ // =---:...---:-===================---===++++++++++ // +----:...:-=======================--==+++++++++++ // +-:------====================++===---==++++===+++++ // +=-----======================+++++==---==+==-::=++**+ // +=-----================---=======++=========::.:-+***** // +==::-====================--: --:-====++=+===:..-=+***** // +=---=====================-... :=..:-=+++++++++===++***** // +=---=====+=++++++++++++++++=-:::::-====+++++++++++++*****+ // +=======++++++++++++=+++++++============++++++=======+****** // +=====+++++++++++++++++++++++++==++++==++++++=:... . .+**** // ++====++++++++++++++++++++++++++++++++++++++++-. ..-+**** // +======++++++++++++++++++++++++++++++++===+====:. ..:=++++ // +===--=====+++++++++++++++++++++++++++=========-::....::-=++* // ====--==========+++++++==+++===++++===========--:::....:=++* // ====---===++++=====++++++==+++=======-::--===-:. ....:-+++ // ==--=--====++++++++==+++++++++++======--::::...::::::-=+++ // ===----===++++++++++++++++++++============--=-==----==+++ // =--------====++++++++++++++++=====================+++++++ // =---------=======++++++++====+++=================++++++++ // -----------========+++++++++++++++=================+++++++ // =----------==========++++++++++=====================++++++++ // =====------==============+++++++===================+++==+++++ // =======------==========================================++++++ // created by : Nitesh Gupta public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while (t-- > 0) { String[] scn = (br.readLine()).trim().split("" ""); int n = Integer.parseInt(scn[0]); long[] arr = new long[n]; scn = (br.readLine()).trim().split("" ""); for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(scn[i]); } long min; long hor = arr[0], ver = arr[1]; long min1 = 0, min2 = 0; min = (hor + ver) * n; long x = 0, y = 0; for (int i = 2; i < n; i++) { if (i % 2 == 0) { x += 1; if (arr[i] >= hor) { min1 += arr[i]; } else { min1 += hor; hor = arr[i]; } } else { y += 1; if (arr[i] >= ver) { min2 += arr[i]; } else { min2 += ver; ver = arr[i]; } } long pro = (n - x) * hor + (n - y) * ver; min = Math.min(min, min1 + min2 +pro); } sb.append(min); sb.append(""\n""); } System.out.println(sb); return; } public static void sort(long[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int idx = (int) Math.random() * n; long temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } public static void print(long[] dp) { for (long ele : dp) { System.out.print(ele + "" ""); } System.out.println(); } public static void print(long[][] dp) { for (long[] a : dp) { for (long ele : a) { System.out.print(ele + "" ""); } System.out.println(); } } } ",1 10bbe9de,48993e07,"import java.io.*; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { SolveQue solveQue = new SolveQue(); solveQue.ques(); } } class SolveQue { 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; } } private static final FastScanner fs = new FastScanner(); private static final Scanner sc = new Scanner(System.in); private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static final long MOD = (long) (1e9 + 7); private static PrintWriter out = new PrintWriter(System.out); private static BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); private static final int iInf = Integer.MAX_VALUE - (int) (1e6); private static final long lInf = (long)(1e17); private static int[] phi; /** Optimal(Maximum,Minimum) Answers * 1. Binary search * 2. Prefix Suffix * 3. Greedy (sorting searching) * 4. DP **/ long[] getP(long[] arr,int n) { long mn = lInf; long[] P = new long[n]; Arrays.fill(P,lInf); long cnt = 0; for(int i = 0 ; i < n; i++){ long curr = arr[i]; if(mn + cnt < curr){ P[i] = mn + cnt; } else { mn = arr[i]; P[i] = arr[i]; cnt = 0; } cnt++; } return P; } long[] getS(long[] arr,int n) { long mn = lInf; long[] S = new long[n]; Arrays.fill(S,lInf); long cnt = 0; for(int i = n - 1; i >= 0; i--){ long curr = arr[i]; if(mn + cnt < curr){ S[i] = mn + cnt; }else{ mn = arr[i]; S[i] = arr[i]; cnt = 0; } cnt++; } return S; } void solve(int T) throws IOException { int n = fs.nextInt(); int k = fs.nextInt(); int[] a = new int[k]; long[] t = new long[k]; long[] arr = new long[n]; Arrays.fill(arr,lInf); for (int i = 0; i < k; i++) { a[i] = fs.nextInt(); a[i]--; } for (int i = 0; i < k; i++) { t[i] = fs.nextLong(); arr[a[i]] = t[i]; } // Arrays.fill(arr,lInf); long[] P = getP(arr, n); // Arrays.fill(arr,lInf); long[] S = getS(arr, n); for(int i = 0 ; i < n ; i++){ System.out.print(Math.min(P[i],S[i]) + "" ""); } System.out.println(); } void ques() throws IOException { int t = 1; // t = sc.nextInt(); t = fs.nextInt(); // t = Integer.parseInt(br.readLine()); int tt = 1; while (t-- > 0) { solve(tt); tt++; } System.gc(); } }","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 void main(String arg[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while (t-- > 0) { br.readLine(); String s[]=br.readLine().split("" ""); int n=Integer.parseInt(s[0]); int k=Integer.parseInt(s[1]); long a[]=new long[n]; long b[]=new long[n]; long c[]=new long[n]; Arrays.fill(c,Integer.MAX_VALUE); String s1[]=br.readLine().split("" ""); s=br.readLine().split("" ""); for(int i=0;i=0;i--){ p=Math.min(p+1,c[i]); b[i]=p; } for(int i=0;i min = new Stack(); Stack max = new Stack(); int[] a = new int[n]; int[] dp = new int[n]; StringTokenizer st = new StringTokenizer(in.readLine()); a[0] = pint(st); min.add(0); max.add(0); for(int i = 1; i < n; i++) { a[i] = pint(st); int h = dp[i - 1] + 1; while(!max.isEmpty() && a[i] > a[max.peek()]) { int k = a[max.peek()]; h = Math.min(h, dp[max.pop()] + 1); while(!max.isEmpty() && a[max.peek()] == k) {max.pop();} } if(!max.isEmpty()) { h = Math.min(h, dp[max.peek()] + 1); } while(!min.isEmpty() && a[i] < a[min.peek()]) { int k = a[min.peek()]; h = Math.min(h, dp[min.pop()] + 1); while(!min.isEmpty() && a[min.peek()] == k) {min.pop();} } if(!min.isEmpty()) { h = Math.min(h, dp[min.peek()] + 1); } dp[i] = h; min.add(i); max.add(i); } System.out.println(dp[n - 1]); } static int[] resize(int[] a) { int[] r = new int[a.length * 2]; for(int i = 0; i < a.length; i++) { r[i] = a[i]; } return r; } static int pint(BufferedReader in) throws IOException {return Integer.parseInt(in.readLine());} static int pint(StringTokenizer st) {return Integer.parseInt(st.nextToken());} }","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 aa8091b0,ac180326,"import java.util.*; import java.io.*; public class AiseHi { static Scanner sc = new Scanner(System.in); static int mod = (int)(1e9+7); public static void main (String[] args) { PrintWriter out = new PrintWriter(System.out); int t = 1; t = sc.nextInt(); z : while(t-->0) { int n = sc.nextInt(); int m = sc.nextInt(); int x = sc.nextInt(); PriorityQueue myQueue = new PriorityQueue<>(); for(int i=1;i<=m;i++) { myQueue.add(new twoval(0,i)); } List arrayList = new ArrayList<>(); int myArray[] = new int[n]; for(int i=0;i=0;i--) { twoval p = myQueue.poll(); long aasjd = p.myArray; int idx = p.b; aasjd += arrayList.get(i).myArray; ans[arrayList.get(i).b] = idx; myQueue.add(new twoval(aasjd,idx)); } long dasdaknw[] = new long[m]; long min = Long.MAX_VALUE, max = Long.MIN_VALUE; for(int i=0;ix) { out.write(""NO\n""); continue; } out.write(""YES\n""); for(int aasjd : ans) out.write(aasjd+"" ""); out.write(""\n""); } out.close(); } static int ceil(int myArray,int b) { return myArray/b + (myArray%b==0?0:1); } static boolean prime[] = new boolean[2000009]; static int fac[] = new int[2000009]; static void sieve() { prime[0] = true; prime[1] = true; int max = 1000000; for(int i=2;i*i<=max;i++) { if(!prime[i]) { for(int j=i*i;j<=max;j+=i) { prime[j] = true; fac[j] = i; } } } } static int gcd(int myArray,int b) { if(b==0) return myArray; return gcd(b,myArray%b); } } class twoval implements Comparable{ long myArray; int b; twoval(long aasjd,int r){ this.myArray = aasjd; this.b = r; } public int compareTo(twoval o) { return (int) (this.myArray - o.myArray); } } ","//This code is written by प्रविण शंखपाळ //package wizard; 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.text.DecimalFormat; import java.util.Collections; import java.util.Comparator; import java.util.Arrays; import java.util.ArrayList; import java.util.LinkedList; import java.util.Stack; import java.util.Queue; import java.util.PriorityQueue; import java.util.List; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.TreeSet; import java.util.Map; import java.util.HashMap; import java.util.Scanner; import java.util.Set; import java.util.StringTokenizer; import java.util.Vector; public class Dobby { public static void main(String[] args) { try { FastReader fr = new FastReader(); PrintWriter pt = new PrintWriter(System.out); int t = fr.nextInt(); while (t > 0) { int n = fr.nextInt(), m = fr.nextInt(), x = fr.nextInt(); ArrayList pp = new ArrayList<>(); int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = fr.nextInt(); Pair pr = new Pair(A[i], i); pp.add(pr); } Collections.sort(pp); Collections.reverse(pp); int ps[] = new int[n]; int pk[] = new int[n]; Arrays.fill(ps, 0); Arrays.fill(pk, 0); int index = 0; for (int i = 0; i < n; i++) { if (pk[index] < x) { pk[index] += pp.get(i).a; } ps[pp.get(i).b] = index + 1; index++; index = index == m ? 0 : index; } pt.println(""YES""); for (int i = 0; i < n; i++) { pt.print(ps[i] + "" ""); } pt.println(); t--; } pt.close(); } catch ( Exception e) { return; } } static void merge(long arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; long L[] = new long[n1]; long R[] = new long[n2]; for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void sort(long arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; sort(arr, l, m); sort(arr, m + 1, r); merge(arr, l, m, r); } } static class Pair implements Comparable { int a, b; Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair o) { // TODO Auto-generated method stub if (this.a != o.a) return Integer.compare(this.a, o.a); else return Integer.compare(this.b, o.b); // return 0; } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair) o; return p.a == a && p.b == b; } return false; } } static int binarySearch(int arr[], int first, int last, int key) { int mid = (first + last) / 2; while (first <= last) { if (arr[mid] < key) { first = mid + 1; } else if (arr[mid] == key) { return mid; } else { last = mid - 1; } mid = (first + last) / 2; } return -1; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } ",0 692a4496,f7006f16,"import java.io.*; import java.util.*; public class c { public static BufferedReader in; public static void main(String[] args) throws Exception { 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()); int[] p = new int[n]; Arrays.fill(p, -1); for (int i = 0; i < n; i++) { if (p[i] != -1) continue; ArrayList cycle = new ArrayList<>(); cycle.add(query(i)); while (cycle.size() == 1 || !cycle.get(0).equals(cycle.get(cycle.size() - 1))) { cycle.add(query(i)); } cycle.remove(cycle.size() - 1); for(int j = 0; j < cycle.size() - 1; j++){ p[cycle.get(j)] = cycle.get(j + 1); } p[cycle.get(cycle.size() - 1)] = cycle.get(0); //System.out.println(Arrays.toString(p)); } StringBuilder b = new StringBuilder(); b.append(""! ""); for (int i = 0; i < n; i++) { b.append((p[i] + 1) + (i < n - 1 ? "" "" : ""\n"")); } System.out.print(b); System.out.flush(); } in.close(); out.close(); } public static int query(int i) throws Exception { System.out.println(""? "" + (i + 1)); System.out.flush(); int k = Integer.parseInt(in.readLine()); return k - 1; } } ","import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class C { private static StringTokenizer st; private static BufferedReader br; private static PrintWriter out; static boolean[]used; static int[]p; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int t = nextInt(); while (t --> 0) { int n = nextInt(); used = new boolean[n+1]; p = new int[n+1]; for (int i = 1; i <= n; i++) { if (!used[i]) { go(i); } } System.out.print(""! ""); for (int i = 1; i <= n; i++) { System.out.print(p[i]+"" ""); } System.out.println(); } out.close(); } private static void go(int i) throws IOException { List inCycle = new ArrayList<>(); int first = -1; while (true) { System.out.println(""? "" + i); int x = nextInt(); used[x] = true; if (x == first) { break; } if (first == -1) { first = x; } inCycle.add(x); } List numbers = new ArrayList<>(); for (int j = 0; j < inCycle.size(); j++) { if (inCycle.get(j) == i) { for (int k = j; k < inCycle.size(); k++) { numbers.add(inCycle.get(k)); } for (int k = 0; k < j; k++) { numbers.add(inCycle.get(k)); } break; } } if (numbers.size() == 1) { p[numbers.get(0)] = numbers.get(0); } else { for (int j = 0; j < numbers.size()-1; j++) { p[numbers.get(j)] = numbers.get(j+1); } p[numbers.get(numbers.size()-1)] = numbers.get(0); } } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } private static String next() throws IOException { while (st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } }",0 2ef4c176,fa1bf524,"import java.io.*; import java.util.*; public class Template { static int mod = 1000000007; public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int yo = sc.nextInt(); while (yo-- > 0) { int n = sc.nextInt(); Map map = new HashMap<>(); for(int i = 0; i < n; i++) { map.put(i, new Pair(sc.nextLong(),sc.nextLong())); } List> list = new ArrayList<>(); for(int i = 0; i < n; i++) { list.add(new ArrayList<>()); } for(int i = 0; i < n-1; i++) { int x = sc.nextInt()-1; int y = sc.nextInt()-1; list.get(x).add(y); list.get(y).add(x); } // l -> 0, r -> 1 for(int i = 0; i < 1e5+3; i++) { for(int j = 0; j < 2; j++) { dp[i][j] = -1; } } long a1 = dfs(map,list,0,0,-1); long a2 = dfs(map,list,1,0,-1); long ans = Math.max(a1, a2); out.println(ans); } out.close(); } // static Map dp = new HashMap<>(); static long[][] dp = new long[(int)1e5+10][2]; private static long dfs(Map map, List> list, int x, int node, int parent) { if(dp[node][x] != -1) { return dp[node][x]; } List neighbours = list.get(node); long ans1 = 0; if(x == 0) { ans1 = map.get(node).x; } else { ans1 = map.get(node).y; } long uAns = 0; for(int e : neighbours) { if(e == parent) continue; long ua1 = dfs(map,list,0,e,node); long ua2 = dfs(map,list,1,e,node); // consider 0 long a1 = ua1 + Math.abs(map.get(e).x-ans1); // consider 1 long a2 = ua2 + Math.abs(map.get(e).y-ans1); uAns += Math.max(a1, a2); } return dp[node][x] = uAns; } static class Pair { long x; long y; public Pair(long x, long y) { this.x = x; this.y = y; } } static void ruffleSort(int[] a) { int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n), temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] sieve(int N) { boolean[] sieve = new boolean[N]; for (int i = 2; i < N; i++) sieve[i] = true; for (int i = 2; i < N; i++) { if (sieve[i]) { if (i * i < 0) continue; for (int j = i * i; j < N; j += i) { sieve[j] = false; } } } return sieve; } static long pow(int a, long b) { if (b == 0) { return 1; } if (b == 1) { return a; } if (b % 2 == 0) { long ans = pow(a, b / 2); return ans * ans; } else { long ans = pow(a, (b - 1) / 2); return a * ans * ans; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } // For Input.txt and Output.txt // FileInputStream in = new FileInputStream(""input.txt""); // FileOutputStream out = new FileOutputStream(""output.txt""); // PrintWriter pw = new PrintWriter(out); // Scanner sc = new Scanner(in); } ","import java.util.*; import java.io.*; public class Solution { static LinkedList graph[]; static long key[][]; static long value[][]; public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); StringBuffer out=new StringBuffer(); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); String input[]; OUTER: while(t--!=0) { int n=Integer.parseInt(br.readLine()); value=new long[n][2]; key=new long[n][2]; for(int i=0; i 0) { int n = in.nextInt(); Integer[] k = intInput(n, in), h = intInput(n, in); long ans = 0; int a = k[n - 1]; int current = k[n - 1]; for (int i = n - 1; i >= 0; i--) { if (current > k[i]) { ans += sum(a - current + 1); a = k[i]; current = k[i]-h[i]+1; }else { current = Math.min(current, k[i] - h[i]+1); } } ans += sum(a - current + 1); out.println(ans); } out.flush(); } static long sum(long a) { return a * (a + 1) / 2; } 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 val; int ind; boolean positive; Data(int val, int ind) { this.val = Math.abs(val); this.ind = ind; this.positive = val >= 0; } } 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.val - o2.val); } } class compareInd implements Comparator { @Override public int compare(Data o1, Data o2) { return o1.ind - o2.ind == 0 ? o1.val - o2.val : o1.ind - o2.ind; } } 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; } }","import java.util.*; import java.io.*; public class hmm { static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws Exception { int t =sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int []k = sc.nextIntArray(n); int h[]=sc.nextIntArray(n); long mana = 0; pair cur = new pair(k[n-1]-h[n-1]+1,k[n-1]); for(int i=n-1;i>=0;i--) { int s = k[i]; int start = s-h[i]+1; if(s>=cur.x) { cur.x = Math.min(start, cur.x); } else { long x = cur.y - cur.x +1; mana += x*(x+1)/2; cur.x = start; cur.y = s; } } long x = cur.y - cur.x +1; mana += x*(x+1)/2; pw.println(mana); } pw.close(); } // -------------- stuff ------------------------------ static class pair { int x ; int y; public pair(int n,int c) { x= n; y = c; } } 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 int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } } }",0 0ea5a836,3c74c140,"import java.util.*; import java.io.*; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //static final long mod=(long)1e9+7; static final long mod=998244353L; public static long pow(long a,long p) { long res=1; while(p>0) { if(p%2==1) { p--; res*=a; res%=mod; } else { a*=a; a%=mod; p/=2; } } return res; } static class Pair { int u,v; Pair(int u,int v) { this.u=u; this.v=v; } } /*static class Pair implements Comparable { int v,l; Pair(int v,int l) { this.v=v; this.l=l; } public int compareTo(Pair p) { return l-p.l; } }*/ static long gcd(long a,long b) { if(b%a==0) return a; return gcd(b%a,a); } public static class comp implements Comparator { public int compare(Pair p1,Pair p2) { if(p1.v>p2.v) return 1; return -1; } } public static void upd1(int a[],int i) { for(;i0;i-=(i&(-i))) s+=a[i]; return s; } public static long q2(long a[],int i) { long s=0; for(;i>0;i-=(i&(-i))) s+=a[i]; return s; } public static void main(String args[])throws Exception { FastReader fs=new FastReader(); PrintWriter pw=new PrintWriter(System.out); int tc=fs.nextInt(); while(tc-->0) { int n=fs.nextInt(); char[][] c=new char[n][]; for(int i=0;i list[]=new ArrayList[5]; for(int i=0;i<5;i++) list[i]=new ArrayList<>(); for(int i=0;i=0;j--) { ans=Math.max(ans,n-1-j); s+=list[i].get(j); if(s<=0) break; } if(s>0) ans=n; } pw.println(ans); } pw.flush(); pw.close(); } }"," //package com.company; import java.math.*; import java.util.*; import java.lang.*; import java.io.*; public final class Main { FastReader s; public static void main (String[] args) throws java.lang.Exception { new Main().run(); } void run() { s = new FastReader(); solve(); } StringBuffer sb; // int counter; void solve() { sb = new StringBuffer(); for(int T = s.nextInt();T > 0;T--) { start(); } // System.out.print(sb); } void start() { int n = s.nextInt(); int mat[][] = new int[n][5]; for(int i = 0; i x = new ArrayList<>(); for(int j = 0; j=0) { int u = x.get(l); if(s+u>0) { s+=u; cnt++; } else break; l--; } return cnt; } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } long power(long x, long y, long p) { long res = 1; // Initialize result // Update x if it is more // than or equal to p x = x % p; while (y > 0) { // If y is odd, multiply // x with the result if ((y & 1) > 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } int lower_bound(int [] arr , int key) { int i = 0; int j = arr.length-1; //if(arr[i] > key)return -1; if(arr[j] < key)return -1; while(i[] 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) { int n = sc.nextInt(); pair = new int[n+1][2]; dp = new long[n+1][2]; for(int i=1;i<=n;i++){ pair[i][0]=sc.nextInt(); pair[i][1]=sc.nextInt(); } getAdj(n, n-1, sc); output.append(solver(n)).append(""\n""); } out.println(output); // _______________________________ // int n = sc.nextInt(); // out.println(solver()); // ________________________________ out.flush(); } static int[][] pair; static long[][] dp; public static long solver(int n) { dfs(1, 0); return Math.max(dp[1][0], dp[1][1]); } static void dfs(int node, int parent){ for(Integer child: adj[node]){ if(child!=parent){ dfs(child, node); long left1 = Math.abs(pair[node][0]-pair[child][0]) + dp[child][0]; long right1 = Math.abs(pair[node][0]-pair[child][1]) + dp[child][1]; long left2 = Math.abs(pair[node][1]-pair[child][0]) + dp[child][0]; long right2 = Math.abs(pair[node][1]-pair[child][1]) + dp[child][1]; dp[node][0] += Math.max(left1, right1); dp[node][1] += Math.max(left2, right2); } } } }","/* * akshaygupta26 */ import java.io.*; import java.util.*; public class E { static long dp[][]; static long val[][]; static ArrayList arr[]; public static void main(String[] args) { FastReader sc=new FastReader(); StringBuffer ans=new StringBuffer(); int test=sc.nextInt(); while(test-->0) { int n=sc.nextInt(); arr=new ArrayList[n+1]; val=new long[n+1][2]; dp=new long[n+1][2]; for(int i=0;i<=n;i++) Arrays.fill(dp[i], -1); for(int i=1;i<=n;i++) { val[i][0]=sc.nextLong();val[i][1]=sc.nextLong(); } for(int i=0;i<=n;i++) arr[i]=new ArrayList<>(); 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.io.*; import java.util.*; public class Codeforces { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); int t=Integer.parseInt(bu.readLine()); while(t-->0) { int n=Integer.parseInt(bu.readLine()); g=new ArrayList[n]; int i; for(i=0;i(); String st[]=bu.readLine().split("" ""); a[i][0]=Integer.parseInt(st[0]); a[i][1]=Integer.parseInt(st[1]); s[i][0]=s[i][1]=0; } for(i=0;i g[]; static int N=100000,a[][]=new int[N][2]; static long s[][]=new long[N][2]; static void dfs(int n,int p) { for(int x:g[n]) if(x!=p) { dfs(x,n); s[n][0]+=Math.max(s[x][0]+Math.abs(a[x][0]-a[n][0]),s[x][1]+Math.abs(a[x][1]-a[n][0])); s[n][1]+=Math.max(s[x][0]+Math.abs(a[x][0]-a[n][1]),s[x][1]+Math.abs(a[x][1]-a[n][1])); } } } ",0 0ee2f8f1,587307f2,"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 arr[]=new int[n]; int min=Integer.MAX_VALUE;int max=Integer.MIN_VALUE; for(int i=0;i=2;i--){ if(tmp[i]=min && tmp[0]>=min; } }","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 arr[]=new int[n]; int min=Integer.MAX_VALUE;int max=Integer.MIN_VALUE; for(int i=0;i=2;i--){ if(tmp[i]=min && tmp[0]>=min; } }",1 5bf01190,79b9b897," // Main Code at the Bottom import java.util.*; import java.io.*; public class Main{ //Fast IO class static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { boolean env=System.getProperty(""ONLINE_JUDGE"") != null; //env=true; if(!env) { try { br=new BufferedReader(new FileReader(""src\\input.txt"")); } catch (FileNotFoundException e) { e.printStackTrace(); } } else 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; //debug static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); //Global variables and functions //Main function(The main code starts from here) public static void main (String[] args) throws java.lang.Exception { int test=1; test=sc.nextInt(); while(test-->0) { int n=sc.nextInt(); long a[]=new long[n]; for(int i=0;i0) { int n=scanner.nextInt(); long a[]=new long [n]; for(int i=0;i0) 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) { if ((p & 1) > 0) { ans = (ans * a) % mod; } a = (a * a) % mod; p = p >> 1; } return ans % mod; } static long mod = 1000000007; static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } static long fast_powerNumbers(long a, long n) { if (n == 1) { return a; } long ans = fast_powerNumbers(a, n / 2); if (n % 2 == 0) { return (ans * ans); } else { return ((ans * ans) * (a)); } } static void dfs_helper(int[][] arr, int i, int j, int team, int n, int m) { arr[i][j] = team; if (i - 1 >= 0 && arr[i - 1][j] == 1) { dfs(arr, i - 1, j, team, n, m); } if (j - 1 >= 0 && arr[i][j - 1] == 1) { dfs(arr, i, j - 1, team, n, m); } if (i + 1 < n && arr[i + 1][j] == 1) { dfs(arr, i + 1, j, team, n, m); } if (j + 1 < m && arr[i][j + 1] == 1) { dfs(arr, i, j + 1, team, n, m); } } static void dfs(int[][] arr, int i, int j, int team, int n, int m) { dfs_helper(arr, i, j, team, n, m); } static int parent[]; static int rank[]; static int find(int i) { if (parent[i] == -1) { parent[i] = i; return i; } if (parent[i] == i) { return i; } else { parent[i] = find(parent[i]); } return parent[i]; } static void unite(int s1, int s2) { if (rank[s1] > rank[s2]) { parent[s2] = s1; rank[s1] += rank[s2]; } else { parent[s1] = s2; rank[s2] += rank[s1]; } } public static long arr[]; public static int arr1[]; // static void seive(int n) { // arr = new int[n + 1]; // arr[0] = arr[1] = 1; // for (int i = 4; i <= n; i = i + 2) { // arr[i] = 1; // } // for (int i = 3; i * i <= n; i = i + 2) { // if (arr[i] == 0) { // for (int j = i * i; j <= n; j = j + i) { // arr[j] = 1; // } // // } // } // } static void seive(int n) { arr1=new int[n+1]; arr1[0]=arr1[1]=1; for (int i = 4; i <=n ; i+=2) { arr1[i]=1; } for (int i = 3; i*i <=n ; i+=2) { if (arr1[i] == 0) { for (int j = i*i; j <=n ; j+=i) { arr1[j]=1; } } } } public static boolean ccw(Point a,Point b,Point c) { long s1=(c.x-b.x)*(b.y-a.y) ; long s2 = (c.y-b.y)*(b.x-a.x); if(s1rank[p2]) { parent[p2]=p1; rank[p1]+=rank[p2]; // System.out.println(arr[p2]); sum[p1]+=sum[p2]; //sum[p2]+=sum[p1]; } else { parent[p1]=p2; rank[p2]+=rank[p1]; //System.out.println(arr[p1]); sum[p2]+=sum[p1]; //sum[p1]+=sum[p2]; } } } class Gaph { HashMap> hm; Gaph() { hm = new HashMap<>(); } Gaph(int n) { hm = new HashMap<>(); for (int i = 0; i < n; i++) { hm.put(i, new ArrayList()); } } // function for adding an edge................................................. public void addEdge(int a, int b, boolean isDir) { if (isDir) { if (hm.containsKey(a)) { hm.get(a).add(b); } else { hm.put(a, new ArrayList<>(Arrays.asList(b))); } } else { if (hm.containsKey(a)) { hm.get(a).add(b); } else if (!hm.containsKey(a)) { hm.put(a, new ArrayList<>(Arrays.asList(b))); } if (hm.containsKey(b)) { hm.get(b).add(a); } else if (!hm.containsKey(b)) { hm.put(b, new ArrayList<>(Arrays.asList(a))); } } } } // out.println(al.toString().replaceAll(""[\\[|\\]|,]"","""")); ",0 476e94d3,9310ad0c,"import java.util.*; import java.io.*; import java.math.BigInteger; public class code2{ public static int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } //@SuppressWarnings(""unchecked"") public static void main(String[] arg) throws IOException{ //Reader in=new Reader(); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(System.in); int t=in.nextInt(); while(t-- > 0){ int n=in.nextInt(); long[] k=new long[n]; long[] h=new long[n]; for(int i=0;ir){ //out.println(m); res[i]=res[i-1]+(m*(m+1))/2; power[i]=h[i]; } else{ res[i]=Long.MAX_VALUE; for(int j=i-1;j>=0;j--){ if(power[j]+k[i]-k[j] >= h[i]){ long x=power[j]+k[i]-k[j]; if(x>=h[i]){ if(k[i]-h[i]+1>k[j]){ power[i]=h[i]; res[i]=res[j]+(m*(m+1))/2; } else{ power[i]=x; long c=(x*(x+1))/2; long d=(power[j]*(power[j]+1))/2; res[i]=res[j]+c-d; } break; } } } } l=Math.min(l,lc); r=k[i]; } out.println(res[n-1]); } out.flush(); } } ","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) throws Exception { FastReader sc = new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long k[]=new long[n]; for(int i=0;i al=new ArrayList<>(); long csp=h[n-1],idx=k[n-1]-h[n-1]; for(int i=n-2;i>=0;i--) { if(idx0) { 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 prime = new ArrayList<>(); public static void main(String[] args) throws IOException { Scanner in=new Scanner(System.in); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringBuffer out = new StringBuffer(); int T = in.nextInt(); OUTER: while(T-->0) { int n=in.nextInt(), k=in.nextInt(); int a[]=new int[k]; for(int i=0; i=0; i--) { if(ans[i]!=0) { temp=Math.min(temp, ans[i]); } right[i]=temp; temp+=1; } for(int i=0; i hi = new Stack<>(); Stack lo = new Stack<>(); hi.push(0); lo.push(0); for(int i = 1; ih[i]){ dp[i] = Math.min(dp[i], dp[lo.peek()]+1); lo.pop(); } if(!lo.isEmpty()){ dp[i] = Math.min(dp[i], dp[lo.peek()]+1); if(h[i] == h[lo.peek()]) lo.pop(); } hi.push(i); lo.push(i); } System.out.println(dp[n-1]); } } ","import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Vector; import java.util.InputMismatchException; import java.io.IOException; import java.util.Stack; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DDiscreteCentrifugalJumps solver = new DDiscreteCentrifugalJumps(); solver.solve(1, in, out); out.close(); } static class DDiscreteCentrifugalJumps { public void solve(int testNumber, InputReader s, PrintWriter w) { int n = s.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = s.nextInt(); int[] dp = new int[n]; for (int i = 0; i < n; i++) dp[i] = i; Stack dec = new Stack<>(); dec.push(0); Stack inc = new Stack<>(); inc.push(0); for (int i = 1; i < n; i++) { while (!dec.isEmpty() && a[dec.peek()] < a[i]) { dp[i] = Math.min(dp[i], dp[dec.peek()] + 1); dec.pop(); } if (!dec.isEmpty()) { dp[i] = Math.min(dp[i], dp[dec.peek()] + 1); if (a[dec.peek()] == a[i]) dec.pop(); } dec.push(i); while (!inc.isEmpty() && a[inc.peek()] > a[i]) { dp[i] = Math.min(dp[i], dp[inc.peek()] + 1); inc.pop(); } if (!inc.isEmpty()) { dp[i] = Math.min(dp[i], dp[inc.peek()] + 1); if (a[inc.peek()] == a[i]) inc.pop(); } inc.push(i); } w.println(dp[n - 1]); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } } ",1 1162c08f,6653a758,"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 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()]); } }",1 1410e423,e90a198b,"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(); } } ","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 f229aa7f,fcc7e8fa,"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(); } } }","import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; 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); TaskA solver = new TaskA(); int t; t = in.nextInt(); //t = 1; while (t > 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; i[] edges = new List[n]; for (int i = 0; i < n; i++) { edges[i] = new ArrayList<>(); } for (int i = 0; i < n - 1; i++) { int u = scanner.nextInt() - 1; int v = scanner.nextInt() - 1; edges[u].add(new int[]{v, i}); edges[v].add(new int[]{u, i}); ct[u]++; ct[v]++; } int start = -1; for (int i = 0; i < n; i++) { if (ct[i] >= 3) { System.out.println(-1); return; } else if (ct[i] == 1) { start = i; } } visited[start] = true; boolean odd = false; for (int i = 0; i < n - 1; i++) { for (int[] e : edges[start]) { if (!visited[e[0]]) { visited[e[0]] = true; num[e[1]] = odd ? 3 : 2; odd = !odd; start = e[0]; break; } } } for (int i = 0; i < n - 1; i++) { System.out.print(num[i]); System.out.print("" ""); } System.out.println(); } } ","import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); while(tc>0) { tc--; int ans = 0; int n = sc.nextInt(); int in[] = new int[n]; int wx[] = new int[n-1]; int wy[] = new int[n-1]; ArrayList> arr = new ArrayList>(); HashMap h = new HashMap(); HashSet h2 = new HashSet(); for(int i=0;i()); } for(int i=0;i2 || in[y]>2) { ans = -1; } else if(ans!=-1) { arr.get(x).add(y); arr.get(y).add(x); wx[i] = x; wy[i] = y; } } if(ans == -1) { System.out.println(""-1""); } else { int vis[] = new int[n]; ArrayDeque q = new ArrayDeque(); q.add(0); vis[0] = 1; while(q.size()>0) { int x = q.removeFirst(); for(int i=0;i 0) { int n = sc.nextInt(); int k = sc.nextInt(); int[] a = new int[k]; int[] temp = new int[n]; for (int i = 0; i < k; i++) a[i] = sc.nextInt(); for (int i = 0; i < k; i++) temp[a[i] - 1] = sc.nextInt(); int[] left = new int[n]; int[] right = new int[n]; Arrays.fill(left, Integer.MAX_VALUE - 1); Arrays.fill(right, Integer.MAX_VALUE - 1); if (temp[0] > 0) left[0] = temp[0]; for (int i = 1; i < n; i++) { left[i] = Math.min(left[i - 1] + 1, left[i]); if (temp[i] > 0) left[i] = Math.min(left[i], temp[i]); } if (temp[n - 1] > 0) right[n - 1] = temp[n - 1]; for (int i = n - 2; i >= 0; i--) { right[i] = Math.min(right[i + 1] + 1, right[i]); if (temp[i] > 0) right[i] = Math.min(right[i], temp[i]); } for (int i = 0; i < n; i++) out.print(Math.min(left[i], right[i]) + "" ""); out.println(); } out.flush(); out.close(); } } ","import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.time.LocalDateTime; import java.util.*; public class B { static int tree[]; static int up[]; static int n; static int mod =1000000007; public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); int t=fs.nextInt(); outer: while(t-->0) { n=fs.nextInt(); int k=fs.nextInt(); int ac[]=fs.readArray(k); int temp[]=fs.readArray(k); int arr[]=new int[n]; Arrays.fill(arr, Integer.MAX_VALUE/2); for(int i=0;i=0;i--) { ans[i]=Math.min(Math.min(arr[i],left[i]),ans[i+1]+1); } // for(int i=0;in) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long pow(long a,long b) { long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i 0;T--) { start(); } // System.out.print(sb); } void start() { int n = s.nextInt(); int mat[][] = new int[n][5]; for(int i = 0; i x = new ArrayList<>(); for(int j = 0; j=0) { int u = x.get(l); if(s+u>0) { s+=u; cnt++; } else break; l--; } return cnt; } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } long power(long x, long y, long p) { long res = 1; // Initialize result // Update x if it is more // than or equal to p x = x % p; while (y > 0) { // If y is odd, multiply // x with the result if ((y & 1) > 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } int lower_bound(int [] arr , int key) { int i = 0; int j = arr.length-1; //if(arr[i] > key)return -1; if(arr[j] < key)return -1; while(i0;t--) solve(); out.print(sb); } void solve(){ int n = in.nextInt(); int[][] mat = new int[n][5]; for(int i = 0; i x = new ArrayList<>(); for(int j = 0; j=0) { int u = x.get(l); if(s+u>0) { s+=u; cnt++; } else break; l--; } return cnt; } long 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; x = (x * x) % p; } return res; } int upper_bound(long[] arr, int key) { int i=0, j=arr.length-1; if (arr[j]<=key) return j+1; if(arr[i]>key) return i; while (i arr, long n) { arr.sort(new Comparator() { @Override public int compare(Pair p1, Pair p2) { return (int) (p2.first - p1.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; } } }",1 522458fc,702694f5,"import java.io.*; import java.util.*; /* polyakoff */ public class Main { static FastReader in; static PrintWriter out; static Random rand = new Random(); static final int oo = (int) 2e9 + 10; static final long OO = (long) 2e18 + 10; static final int MOD = 998244353; static int ask(int i) { out.println(""? "" + (i + 1)); out.flush(); return in.nextInt() - 1; } static void solve() { int n = in.nextInt(); int[] p = new int[n]; Arrays.fill(p, -1); for (int i = 0; i < n; i++) { if (p[i] == -1) { int first = ask(i); ArrayList cycle = new ArrayList<>(); while (true) { int x = ask(i); cycle.add(x); if (x == first) break; } int sz = cycle.size(); int from = -1; for (int j = 0; j < sz; j++) { if (cycle.get(j) == i) { from = j; break; } } p[i] = cycle.get((from + 1) % sz); for (int j = (from + 1) % sz; j != from; j = (j + 1) % sz) { p[cycle.get(j)] = cycle.get((j + 1) % sz); } } } out.print(""!""); for (int i = 0; i < n; i++) { out.print("" "" + (p[i] + 1)); } out.println(); out.flush(); } public static void main(String[] args) { in = new FastReader(); out = new PrintWriter(System.out); int t = 1; t = in.nextInt(); while (t-- > 0) { solve(); } out.flush(); out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; FastReader() { this(System.in); } FastReader(String file) throws FileNotFoundException { this(new FileInputStream(file)); } FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } String nextLine() { String line; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } } } ","import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; //import java.text.DecimalFormat; import java.util.*; //import B.Node; public class Codeforces { static int mod= 998244353; static char arr[]; static class Node{ int l,r,c,ind; Node(){ } Node(int l,int r,int c){ this.l=l; this.r=r; this.c=c; } Node(int l,int r,int c,int d){ this.l=l; ind=d; this.r=r; this.c=c; } } public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); // int t=fs.nextInt(); int t=fs.nextInt(); outer:while(t-->0) { int n=fs.nextInt(); int p[]=new int[n]; Arrays.fill(p, -1); for(int i=0;i list=new ArrayList<>(); Set set=new HashSet<>(); while(true) { System.out.println(""? ""+(i+1)); int cur=fs.nextInt(); list.add(cur); if(set.contains(cur)) break; set.add(cur); } for(int j=0;j>1; } return res; } static int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } static long nck(int n,int k) { if(k>n) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { // return fact[(int)n]; long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i[] temp = new LinkedList[vertices+1]; for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>(); for(int i = 0; i < edges; i++) { int x = sc.nextInt(); int y = sc.nextInt(); temp[x].add(y); } return temp; } static LinkedList[] graphUD(int vertices, int edges) { LinkedList[] temp = new LinkedList[vertices+1]; for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>(); for(int i = 0; i < edges; i++) { int x = sc.nextInt(); int y = sc.nextInt(); temp[x].add(y); temp[y].add(x); } return temp; } static void printG(LinkedList[] temp) { for(LinkedList aa:temp) System.out.println(aa); } static long cal(long val, long pow, long mod) { if(pow == 0) return 1; long res = cal(val, pow/2, mod); long ret = (res*res)%mod; if(pow%2 == 0) return ret; return (val*ret)%mod; } static long gcd(long a, long b) { return b == 0 ? a:gcd(b, a%b); } } static int mod = (int) 1e9; static LinkedList[] temp, idx; static long inf = (long) Long.MAX_VALUE; // static long inf = Long.MAX_VALUE; // static int max; public static void main(String[] args) { int t = sc.nextInt(); // int t = 1; StringBuilder ret = new StringBuilder(); while(t-- > 0) { int n = sc.nextInt(); String a = sc.next(), b = sc.next(); int _00 = 0, _01 = 0, _10 = 0, _11 = 0; for(int i = 0; i < n; i++) { if(a.charAt(i) == '0' && b.charAt(i) == '0') _00++; if(a.charAt(i) == '0' && b.charAt(i) == '1') _01++; if(a.charAt(i) == '1' && b.charAt(i) == '0') _10++; if(a.charAt(i) == '1' && b.charAt(i) == '1') _11++; } int ans = Integer.MAX_VALUE; if(_10 == _01) ans = _01 + _10; if(_10 > 0) { int n00 = _10 - 1; int n01 = _11; int n10 = _00 + 1; int n11 = _01; if (n01 == n10) { ans = Math.min(ans, 1 + n01 + n10); } } if(_11 > 0) { int n00 = _10; int n01 = _11 - 1; int n10 = _00; int n11 = _01 + 1; if (n01 == n10) { ans = Math.min(ans, 1 + n01 + n10); } } ret.append(ans == Integer.MAX_VALUE ? -1 : ans); ret.append(""\n""); } System.out.println(ret); } } ",0 0922b7e7,d1cd194e,"import java.io.*; import java.util.*; public class PhoenixAndTowers { public static void main(String[] args) throws IOException { // BufferedReader in = new BufferedReader(new FileReader(""PhoenixAndTowers.in"")); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(""PhoenixAndTowers.out""))); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int T = Integer.parseInt(in.readLine()); for (int i = 0; i < T; i++) { StringTokenizer st = new StringTokenizer(in.readLine()); int N = Integer.parseInt(st.nextToken()); int M = Integer.parseInt(st.nextToken()); int X = Integer.parseInt(st.nextToken()); PriorityQueue towers = new PriorityQueue(); for (int j = 1; j <= M; j++) towers.add(new Tower(j, 0)); out.println(""YES""); st = new StringTokenizer(in.readLine()); for (int j = 0; j < N; j++) { Tower t = towers.remove(); t.size += Integer.parseInt(st.nextToken()); towers.add(t); out.print(t.idx + "" ""); } out.println(); } out.close(); in.close(); } public static class Tower implements Comparable { int idx, size; public Tower(int idx, int size) { this.idx = idx; this.size = size; } @Override public int compareTo(Tower o) { return size - o.size; } } }","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 929b98f0,bf85ab7b," import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.*; public class Main { public static long[] ans(List> edges, long[][] range, int root, boolean[] visited, PrintStream out) { if (visited[root]) { return new long[2]; } visited[root] = true; long[] ans = new long[2]; for (int x : edges.get(root)) { if (!visited[x]) { long[] temp = ans(edges, range, x, visited, out); ans[0] += Math.max(Math.abs(range[root][0] - range[x][0]) + temp[0], Math.abs(range[root][0] - range[x][1]) + temp[1]); ans[1] += Math.max(Math.abs(range[root][1] - range[x][0]) + temp[0], Math.abs(range[root][1] - range[x][1]) + temp[1]); } } return ans; } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintStream out = new PrintStream(System.out); int t = Integer.parseInt(reader.readLine()); while (t-->0) { int n = Integer.parseInt(reader.readLine()); long[][] range = new long[n][2]; for (int i = 0; i < n; i++) { String[] input = reader.readLine().split("" ""); range[i][0] = Integer.parseInt(input[0]); range[i][1] = Integer.parseInt(input[1]); } List> edges = new ArrayList<>(); for (int i = 0; i < n; i++) { edges.add(new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { String[] input = reader.readLine().split("" ""); int u = Integer.parseInt(input[0]) - 1, v = Integer.parseInt(input[1]) - 1; edges.get(u).add(v); edges.get(v).add(u); } int root = 0; for (int i = 0; i < n; i++) { if (edges.get(i).size() > 1) { root = i; break; } if (edges.get(i).size() == 1) { root = i; } } long[] ans = ans(edges, range, root, new boolean[n], out); out.println(Math.max(ans[0], ans[1])); } out.close(); } } "," 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 856a8eda,c354b74f,"/*input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 */ import java.io.*; import java.util.*; public class three{ public static class Pair implements Comparable{ int min; int idx; @Override public int compareTo(Pair o) { return min - o.min; } } public static void main(String[] args) throws Exception { MyScanner scn = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); /* int n = sc.nextInt(); // read input as integer long k = sc.nextLong(); // read input as long double d = sc.nextDouble(); // read input as double String str = sc.next(); // read input as String String s = sc.nextLine(); // read whole line as String int result = 3*n; out.println(result); // print via PrintWriter */ //The Code Starts here int t = scn.nextInt(); while(t-- > 0){ int n = scn.nextInt(); int m = scn.nextInt(); int x = scn.nextInt(); int arr[] = scn.nextIntArray(n); PriorityQueue pq = new PriorityQueue<>(); System.out.println(""YES""); for(int i=0;i { // long u; // long v; // public Pair(long u, long v) { // this.u = u; // this.v = v; // } // public int hashCode() { // int hu = (int) (u ^ (u >>> 32)); // int hv = (int) (v ^ (v >>> 32)); // return 31 * hu + hv; // } // public boolean equals(Object o) { // Pair other = (Pair) o; // return u == other.u && v == other.v; // } // public int compareTo(Pair other) { // return Long.compare(u, other.u) != 0 ? Long.compare(u, other.u) : Long.compare(v, other.v); // } // public String toString() { // return ""[u="" + u + "", v="" + v + ""]""; // } // } //-------------------------------------------------------- } ","//Some of the methods are copied from GeeksforGeeks Website import java.util.*; import java.lang.*; import java.io.*; public class Main { static Reader sc=new Reader(); // static FastReader sc=new FastReader(System.in); static class PairComparator implements Comparator{ // Overriding compare()method of Comparator // for descending order of cgpa public int compare(Pair s1, Pair s2) { if (s1.x < s2.x) return -1; else if (s1.x > s2.x) return 1; return 0; } } public static void main (String[] args) throws java.lang.Exception { // try{ /* int n=sc.nextInt(); ArrayList al=new ArrayList<>(); ArrayList al=new ArrayList<>(); Set set=new HashSet<>(); Collections.sort(al,Collections.reverseOrder()); long n=sc.nextLong(); for(int i=0;i0) { int n=sc.nextInt(); int m=sc.nextInt(); int x=sc.nextInt(); PriorityQueue pq=new PriorityQueue<>(m, new PairComparator()); for(int i=1;i<=m;i++) { Pair p=new Pair(0,i); pq.add(p); } out.println(""YES""); int a[]=new int[n]; for(int i=0;i map=new HashMap<>(); for(int i=0;i> hmap=map.entrySet(); for(Map.Entry data : hmap) { } Iterator it = set.iterator(); while(it.hasNext()) { int x=it.next(); } */ static void print(int a[]) { int n=a.length; for(int i=0;i al) { int si=al.size(); for(int i=0;i al) { int si=al.size(); for(int i=0;i>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static class Graph { int v; ArrayList list[]; Graph(int v) { this.v=v; list=new ArrayList[v+1]; for(int i=1;i<=v;i++) list[i]=new ArrayList(); } void addEdge(int a, int b) { this.list[a].add(b); } } static void DFS(Graph g, boolean[] visited, int u) { visited[u]=true; int v=0; for(int i=0;i l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static PrintWriter out=new PrintWriter(System.out); static int int_max=Integer.MAX_VALUE; static int int_min=Integer.MIN_VALUE; static long long_max=Long.MAX_VALUE; static long long_min=Long.MIN_VALUE; } // Thank You !",0 55ab9b5d,c6113c06,"import java.io.*; import java.util.*; public class Main { static int solve(char ch, char[][] str, int N) { int[] a = new int[N]; for (int i = 0; i < N; i++) { int c1 = 0, c2 = 0; for (char x : str[i]) { if (x == ch) c1++; else c2++; } a[i] = c1 - c2; } int sum = 0, count = N; for (int x : a) sum += x; Arrays.sort(a); for (int i = 0; i < N; i++) { if (sum > 0) break; count--; sum -= a[i]; } return count; } public static void main(String[] args) throws IOException { // System.out.println(""===== input =====""); // BufferedReader br = new BufferedReader(new FileReader(""input/input1.txt"")); // BufferedReader br2 = new BufferedReader(new FileReader(""input/input1.txt"")); // String s; // while ((s = br2.readLine()) != null) { // System.out.println(s); // } // System.out.println(""===== output =====""); 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) { int n = Integer.parseInt(br.readLine().trim()); char[][] str = new char[n][]; for (int i = 0; i < n; i++) { str[i] = br.readLine().trim().toCharArray(); } int ans = 0; for (char ch = 'a'; ch <= 'e'; ch++) { ans = Math.max(ans, solve(ch, str, n)); } out.append(ans).append(""\n""); } System.out.println(out); } } ","//created by Whiplash99 import java.io.*; import java.util.*; public class D { private static int solve(char ch, char[][] str, int N) { int[] a=new int[N]; for(int i=0;i0) break; count--; sum-=a[i]; } return count; } 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()); char[][] str=new char[N][]; for(i=0;i 0) { int n = Integer.parseInt(br.readLine()); String[] str = br.readLine().split("" ""); int[] a = new int[n]; int k = 0; ArrayList pos = new ArrayList<>(); for(int i=0; i pos=new ArrayList<>(n); for(int i=0;i al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i cycle = new ArrayList<>(); int ans = query(i, in); int now = query(i, in); cycle.add(now); while (now != ans) { now = query(i, in); cycle.add(now); } for (int j = 0; j < cycle.size(); j++) { arr[cycle.get(j)] = cycle.get((j + 1) % cycle.size()) + 1; vis[cycle.get(j)] = true; } } out.print(""! ""); out.println(arr); out.flush(); } int query(int ind, InputReader in) { System.out.println(""? "" + (ind + 1)); return in.nextInt() - 1; } } static class InputReader { BufferedReader reader; 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()); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void println(int[] array) { print(array); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } } ",0 25597bcb,86232d21,"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.LinkedList; import java.util.List; import java.util.Map; import java.util.StringTokenizer; import java.util.TreeSet; public class PhoenixAndTowers { // Template for CF public static class ListComparator implements Comparator> { @Override public int compare(List l1, List l2) { for (int i = 0; i < l1.size(); ++i) { if (l1.get(i).compareTo(l2.get(i)) != 0) { return l1.get(i).compareTo(l2.get(i)); } } return 0; } } public static class Pair { int first; int second; public Pair(int first, int second) { this.first = first; this.second = second; } public int getFirst() { return first; } public int getSecond() { return second; } public void setFirst(int a) { first = a; } @Override public String toString() { return first + "" "" + second; } } public static void main(String[] args) throws IOException { // Check for int overflow!!!! // Should you use a long to store the sum or smthn? BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int T = Integer.parseInt(f.readLine()); for (int i = 0; i < T; i++) { StringTokenizer st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); st = new StringTokenizer(f.readLine()); List list = new ArrayList<>(); for (int j = 0; j < n; j++) { int a = Integer.parseInt(st.nextToken()); list.add(a); } ArrayList copy = new ArrayList<>(list); Collections.sort(list); TreeSet> set = new TreeSet<>(new ListComparator()); for (int j = 1; j <= m; j++) { List temp = new ArrayList<>(); temp.add(0); temp.add(j); set.add(temp); // System.out.println(temp); } // System.out.println(set); Map> map = new HashMap<>(); for (int j = n - 1; j >= 0; j--) { if (map.containsKey(list.get(j))) { map.get(list.get(j)).addLast(set.first().get(1)); } else { map.put(list.get(j), new LinkedList<>()); map.get(list.get(j)).addLast(set.first().get(1)); } List temp = new ArrayList<>(); temp.add(set.first().get(0) + list.get(j)); temp.add(set.pollFirst().get(1)); set.add(temp); } // System.out.println(set); if (set.last().get(0) - set.first().get(0) > x) { out.println(""NO""); } else { out.println(""YES""); for (int j = 0; j < n; j++) { out.print(map.get(copy.get(j)).pollFirst() + "" ""); } out.println(); } } out.close(); } }","import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.exit; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.PriorityQueue; import java.util.StringTokenizer; public class C { static class Token implements Comparable { final int i, h; Token(int i, int h) { this.i = i; this.h = h; } public int compareTo(Token o) { return h - o.h; } } static void solve() throws Exception { int tests = scanInt(); for (int test = 0; test < tests; test++) { int n = scanInt(), m = scanInt(); scanInt(); PriorityQueue pq = new PriorityQueue<>(); int h[] = new int[m]; int ans[] = new int[n]; for (int i = 0; i < m; i++) { pq.add(new Token(i, 0)); } for (int i = 0; i < n; i++) { Token cur = pq.remove(); pq.add(new Token(cur.i, h[cur.i] += scanInt())); ans[i] = cur.i; } out.println(""YES""); for (int i = 0; i < n; i++) { out.print((ans[i] + 1) + "" ""); } out.println(); } } static int scanInt() throws IOException { return parseInt(scanString()); } static long scanLong() throws IOException { return parseLong(scanString()); } static String scanString() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; public static void main(String[] args) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } }",0 086f0f90,4a570de6,"import java.io.*; import java.util.*; import java.util.concurrent.ConcurrentHashMap; public class Main { public static void main(String[] args) { FastScanner sc = new FastScanner(); int t=sc.nextInt(); while (t-->=1) { int n = sc.nextInt(),m=sc.nextInt(), x = sc.nextInt(); int a[] = sc.readArray(n); PriorityQueue pq=new PriorityQueue<>(); 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 { int a; int b; public Pair(int 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.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;i 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 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)); } }",0 55ab9b5d,ae4d5b49,"import java.io.*; import java.util.*; public class Main { static int solve(char ch, char[][] str, int N) { int[] a = new int[N]; for (int i = 0; i < N; i++) { int c1 = 0, c2 = 0; for (char x : str[i]) { if (x == ch) c1++; else c2++; } a[i] = c1 - c2; } int sum = 0, count = N; for (int x : a) sum += x; Arrays.sort(a); for (int i = 0; i < N; i++) { if (sum > 0) break; count--; sum -= a[i]; } return count; } public static void main(String[] args) throws IOException { // System.out.println(""===== input =====""); // BufferedReader br = new BufferedReader(new FileReader(""input/input1.txt"")); // BufferedReader br2 = new BufferedReader(new FileReader(""input/input1.txt"")); // String s; // while ((s = br2.readLine()) != null) { // System.out.println(s); // } // System.out.println(""===== output =====""); 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) { int n = Integer.parseInt(br.readLine().trim()); char[][] str = new char[n][]; for (int i = 0; i < n; i++) { str[i] = br.readLine().trim().toCharArray(); } int ans = 0; for (char ch = 'a'; ch <= 'e'; ch++) { ans = Math.max(ans, solve(ch, str, n)); } out.append(ans).append(""\n""); } System.out.println(out); } } ","import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException{ //BufferedReader f = new BufferedReader(new FileReader(""uva.in"")); BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int t = Integer.parseInt(f.readLine()); while(t-- > 0) { int n = Integer.parseInt(f.readLine()); int[][] occ = new int[n][6]; for(int i = 0; i < n; i++) { char[] temp = f.readLine().toCharArray(); for(char j: temp) { occ[i][j-'a']++; } occ[i][5] = occ[i][0]+occ[i][1]+occ[i][2]+occ[i][3]+occ[i][4]; } int max = 0; for(int i = 0; i < 5; i++) { int[] temp = new int[n]; for(int j = 0; j < n; j++) { temp[j] = occ[j][i]-(occ[j][5]-occ[j][i]); } Arrays.sort(temp); int j; int cur = 0; for(j = n-1; j >= 0; j--) { if(cur+temp[j] <= 0) { break; } cur += temp[j]; } max = Math.max(max, n-j-1); } out.println(max); } f.close(); out.close(); } }",0 3e1442a3,d0e27497,"import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); // long mod = 1_000_000_007L; // long mod = 998_244_353L; int t = sc.nextInt(); for ( int zzz=0; zzz> adj = new HashMap<>(); HashMap, Integer> inv = new HashMap<>(); boolean f = false; for ( int i=0; i s = adj.get(u); if ( s.size()>1 ) f = true; s.add(v); adj.put(u, s); } else { HashSet s = new HashSet<>(); s.add(v); adj.put(u, s); } if ( adj.containsKey(v) ) { HashSet s = adj.get(v); if ( s.size()>1 ) f = true; s.add(u); adj.put(v, s); } else { HashSet s = new HashSet<>(); s.add(u); adj.put(v, s); } HashSet si = new HashSet<>(); si.add(u); si.add(v); inv.put(si, i); } if ( f ) { System.out.println(-1); continue; } String[] ans = new String[n-1]; boolean g = false; ArrayDeque q = new ArrayDeque<>(); q.addLast(1); boolean[] seen = new boolean[n+1]; while ( q.size()>0 ) { int v = q.removeLast(); seen[v] = true; HashSet s = adj.get(v); for ( int e : s ) { if ( seen[e] ) continue; HashSet st = new HashSet<>(); st.add(v); st.add(e); int pos = inv.get(st); if ( g ) { ans[pos] = ""3""; } else { ans[pos] = ""2""; } g = !g; q.addLast(e); break; } } g = true; q.addLast(1); while ( q.size()>0 ) { int v = q.removeLast(); seen[v] = true; HashSet s = adj.get(v); for ( int e : s ) { if ( seen[e] ) continue; HashSet st = new HashSet<>(); st.add(v); st.add(e); int pos = inv.get(st); if ( g ) { ans[pos] = ""3""; } else { ans[pos] = ""2""; } g = !g; q.addLast(e); break; } } System.out.println(String.join("" "", ans)); } } } ","import java.util.*; import java.io.*; public class Main { static FastScanner sc = new FastScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = (long) 1e9 + 7; public static void main(String[] args) throws Exception { int t = sc.nextInt(); for(int i = 0; i < t; i++) solve(); pw.flush(); } static ArrayList> map; static int[] ans; public static void solve() { sb.setLength(0); int n = sc.nextInt(); ans = new int[n-1]; map = new ArrayList<>(); for(int i = 0; i < n; i++){ map.add(new ArrayList<>()); } int[] cnt = new int[n]; for(int i = 0; i < n-1; i++){ int[] e = sc.nextIntArray(2); e[0]--; e[1]--; cnt[e[0]]++; cnt[e[1]]++; //edge.add(e); map.get(e[0]).add(new int[]{e[1],i}); map.get(e[1]).add(new int[]{e[0],i}); } for(int i = 0; i < n; i++){ if(cnt[i] > 2){ pw.println(-1); return; } } for(int i = 0; i < n; i++){ if(cnt[i] == 1){ ArrayDeque dq = new ArrayDeque<>(); boolean[] used = new boolean[n]; used[i] = true; dq.add(new int[]{i,0}); while(dq.size() > 0){ int[] now = dq.poll(); int u = now[0]; int c = now[1]; //System.err.println(u + "" "" + c + "" ""+ map.get(u).size()); for(int[] next : map.get(u)){ int v = next[0]; int ei = next[1]; //System.err.println(u + "" "" + v + "" "" + c + "" "" + ei); if(!used[v]){ used[v] = true; //System.err.println(u + "" "" + v + "" "" + c); ans[ei] = c % 2 == 0 ? 3 : 2; dq.add(new int[]{v,c+1}); } } } for(int v : ans){ if(v == 0){ pw.println(-1); return; } sb.append(v).append("" ""); } pw.println(sb.toString().trim()); return; } } pw.println(-1); } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public FastScanner(FileReader in) { reader = new BufferedReader(in); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(""\n""); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String[] nextArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } ",0 83b44c9c,e431de28,"import java.util.Scanner; public class MinimumGridPath { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int test = scanner.nextInt(); StringBuilder sb = new StringBuilder(); for (int t = 0; t < test; t++) { int n = scanner.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = scanner.nextLong(); } long minx = arr[0]; long miny = arr[1]; long min = minx * n + miny * n; long sumx = arr[0]; long sumy = arr[1]; for(int i=2; i0){ int n = s.nextInt(); long[] aa =new long[n]; for(int i=0;i one = new ArrayList(); ArrayList zero = new ArrayList(); for (int i = 0; i < n; i++) { if (arr[i] == 1) { one.add(i); } else { zero.add(i); } } int[][] dp = new int[one.size() + 1][zero.size() + 1]; for (int i = 1; i <= one.size(); i++) { dp[i][i] = dp[i - 1][i - 1] + Math.abs(one.get(i - 1) - zero.get(i - 1)); for (int j = i + 1; j <= zero.size(); j++) { dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j - 1] + Math.abs(one.get(i - 1) - zero.get(j - 1))); } } return dp[one.size()][zero.size()]; } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split("" ""); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(s[i]); } log.write(Integer.toString(solution(n, arr)) + ""\n""); log.flush(); } } ","import java.util.*; public class Longjumps { public static void main(String[] args){ Scanner sc=new Scanner(System.in); ArrayList o=new ArrayList(), e=new ArrayList(); int n = sc.nextInt(); 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 034030f3,9e4ddc38,"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.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Try2{ public static void main(String[] args) throws IOException { FastScanner fs = new FastScanner(); // int t =fs.nextInt(); int t=1; while (t-- > 0) { int n = fs.nextInt(); int a[] = new int[n]; int countOne = 0; ArrayList aa = new ArrayList(); for(int i =0;i 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.io.*; import java.util.*; public class A734C { public static void main(String[] args) { JS scan = new JS(); int t = scan.nextInt(); loop:while(t-->0){ int n = scan.nextInt(); String[] arr= new String[n]; Integer[][] counts = new Integer[5][n]; for(int i = 0;i<5;i++){ for(int j = 0;j=0;j--){ extra+=counts[i][j]; if(extra>0)curr++; } best = Math.max(best,curr); } System.out.println(best); } } static class JS { public int BS = 1 << 16; public char NC = (char) 0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar() { 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 long nextLong() { num = 1; boolean neg = false; if (c == NC) c = nextChar(); for (; (c < '0' || c > '9'); c = nextChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = nextChar()) { res = (res << 3) + (res << 1) + c - '0'; num *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / num; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c > 32) { res.append(c); c = nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c != '\n') { res.append(c); c = nextChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = nextChar(); if (c == NC) return false; else if (c > 32) return true; } } } } ",1 80e6c4db,f22a4243,"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_Set_or_Decrease{ 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 list = new ArrayList(); long sum=0; for(int i=0;i(yo1-n-5);i--){ // System.out.println(""i ""+i); long num=first-i; long start=-1; long end=n-1; long mid=0; while(start 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.IOException; import java.io.InputStreamReader; import java.util.*; public class C { public static void main(String[] args) { FastScanner fs=new FastScanner(); int t=fs.nextInt(); while(t-->0) { int n; long k,sum=0L,temp=0L,min=Integer.MAX_VALUE; n=fs.nextInt(); k=fs.nextLong(); int[] arr=fs.readArray(n); for(int i=0;i s=new Stack<>(); for(int i=1;ik){ min=Math.min(min,temp+(sum-k+temp)/(temp+1)); sum-=s.peek(); s.pop(); temp++; } if(sum>k){ temp+=(sum-k+temp)/(temp+1); } System.out.println(Math.min(min, temp)); } } public static void kThLexString(String st,int k, int n) { // Set to store the unique substring Set z = new HashSet(); for(int i = 0; i < n; i++) { // String to create each substring String pp = """"; for(int j = i; j < i + k; j++) { if (j >= n) break; pp += st.charAt(j); // Adding to set z.add(pp); } } // Converting set into a list Vector fin = new Vector(); fin.addAll(z); // Sorting the strings int the list // into lexicographical order Collections.sort(fin); // Printing kth substring System.out.print(fin.get(k - 1)); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(int[] a) { ArrayList l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i 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 hmm { static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws Exception { int t =sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int []k = sc.nextIntArray(n); int h[]=sc.nextIntArray(n); long mana = 0; pair cur = new pair(k[n-1]-h[n-1]+1,k[n-1]); for(int i=n-1;i>=0;i--) { int s = k[i]; int start = s-h[i]+1; if(s>=cur.x) { cur.x = Math.min(start, cur.x); } else { long x = cur.y - cur.x +1; mana += x*(x+1)/2; cur.x = start; cur.y = s; } } long x = cur.y - cur.x +1; mana += x*(x+1)/2; pw.println(mana); } pw.close(); } // -------------- stuff ------------------------------ static class pair { int x ; int y; public pair(int n,int c) { x= n; y = c; } } 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 int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } } }",1 018c15bd,378bb1ca,"import java.util.*; import java.util.Map.Entry; import java.io.*; import java.math.*; import static java.util.stream.Collectors.*; import static java.util.Map.Entry.*; /* Name of the class has to be ""Main"" only if the class is public. */ public class Hey { public static void main (String[] args) throws IOException { final long mod=(long) (1e9+7); Reader s=new Reader(); PrintWriter pt=new PrintWriter(System.out); // BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int T=s.nextInt(); // int T=Integer.parseInt(br.readLine()); // int T=1; while(T-->0) { int n=s.nextInt(); int c[]=new int[n]; for(int i=0;i=arr[i-1]==false) { return false; } } return true; } static int setBit(int S, int j) { return S | 1 << j; } static int clearBit(int S, int j) { return S & ~(1 << j); } static int toggleBit(int S, int j) { return S ^ 1 << j; } static boolean isOn(int S, int j) { return (S & 1 << j) != 0; } static int turnOnLastZero(int S) { return S | S + 1; } static int turnOnLastConsecutiveZeroes(int S) { return S | S - 1; } static int turnOffLastBit(int S) { return S & S - 1; } static int turnOffLastConsecutiveBits(int S) { return S & S + 1; } static int lowBit(int S) { return S & -S; } static int setAll(int N) { return (1 << N) - 1; } static int modulo(int S, int N) { return (S & N - 1); } //S%N, N is a power of 2 static boolean isPowerOfTwo(int S) { return (S & S - 1) == 0; } static boolean isWithin(long x, long y, long d, long k) { return x*k*x*k + y*k*y*k <= d*d; } static long modFact(long n, long p) { if (n >= p) return 0; long result = 1; for (int i = 1; i <= n; i++) result = (result * i) % p; return result; } static int sum(int[] arr, int n) { int inc[]=new int[n+1]; int dec[]=new int[n+1]; inc[0] = arr[0]; dec[0] = arr[0]; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (arr[j] > arr[i]) { dec[i] = max(dec[i], inc[j] + arr[i]); } else if (arr[i] > arr[j]) { inc[i] = max(inc[i], dec[j] + arr[i]); } } } return max(inc[n - 1], dec[n - 1]); } static long nc2(long a) { return a*(a-1)/2; } public static int numberOfprimeFactors(int n) { // Print the number of 2s that divide n HashSet hs = new HashSet(); while (n%2==0) { hs.add(2); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n while (n%i == 0) { hs.add(i); n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n > 2) hs.add(n); return hs.size(); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static void reverse(int arr[],int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } static void reverse(long arr[],int start, int end) { long temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } 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 int p2(int n) { int k=0; while(n>1) { if(n%2!=0) return k; n/=2; k++; } return k; } static boolean isp2(int n) { while(n>1) { if(n%2==1) return false; n/=2; } return true; } static int binarySearch(int arr[], int first, int last, int key){ int mid = (first + last)/2; while( first <= last ){ if ( arr[mid] < key ){ first = mid + 1; }else if ( arr[mid] == key ){ return mid; }else{ last = mid - 1; } mid = (first + last)/2; } return -1; } static void print(int a[][]) { for(int i=0;i y)? x : y; } static int search(Pair[] p, Pair pair) { int l=0, r=p.length; while (l <= r) { int m = l + (r - l) / 2; if (p[m].compareTo(pair)==0) return m; if (p[m].compareTo(pair)<0) l = m + 1; else r = m - 1; } return -1; } static void pa(int a[]) { 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 modInverse(long n, long p) { return power(n, p-2, p); } static long nCrModP(long n, long r, long p) { if(r>n) return 0; if (r == 0) return 1; long[] fac = new long[(int) (n+1)]; fac[0] = 1; for (int i = 1 ;i <= n; i++) fac[i] = fac[i-1] * i % p; return (fac[(int) n]* modInverse(fac[(int) r], p) % p * modInverse(fac[(int) (n-r)], p) % p) % p; } static String reverse(String str) { return new StringBuffer(str).reverse().toString(); } static long fastpow(long x, long y, long m) { if (y == 0) return 1; long p = fastpow(x, y / 2, m) % m; p = (p * p) % m; if (y % 2 == 0) return p; else return (x * p) % m; } static boolean isPerfectSquare(long l) { return Math.pow((long)Math.sqrt(l),2)==l; } static void merge(long[] arr, int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long [n1]; long R[] = new long [n2]; /*Copy data to temp arrays*/ for (int i=0; i{ int a; int b; Pair(int a,int b){ this.a=a; this.b=b; } public int compareTo(Pair p){ if(a>p.a) return 1; if(a==p.a) return (b-p.b); return -1; } } 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[128]; // 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(); } } }","//package Current; import java.util.*; public class test { static final long mod = (long) 1e9 + 7; static class pair { int x, y; public pair(int x, int y) { this.x = x; this.y = y; } } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = 1; t = sc.nextInt(); while (t-- > 0) { // Start code int n = sc.nextInt(); long[] cost = new long[n]; long[] a = new long[n]; for (int i = 0; i < n; i++) { cost[i] = sc.nextLong(); a[i] = cost[i]; if (i > 1) a[i] = Math.min(a[i], a[i - 2]); } long sum = cost[0]; long res = Long.MAX_VALUE; for (int i = 1; i < n; i++) { sum += cost[i]; long cur = sum - a[i] - a[i - 1]; int p = (i + 1) / 2; int q = (i + 1) - p; int x = n - p + 1; int y = n - q + 1; cur += (a[i - 1] * x + a[i] * y); res = Math.min(res, cur); } println(res); } sc.close(); } static void print(Object o) { System.out.print(o + "" ""); } static void println(Object o) { System.out.println(o); } static void swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static ArrayList factorial(int num) { ArrayList fac = new ArrayList<>(); fac.add((long) 0); fac.add((long) 1); for (int i = 2; i < num; i++) { fac.add((fac.get(i - 1) * i) % mod); } return fac; } static long ncr(long x, long y, ArrayList fac) { if (y >= x) return (long) 1; long res = fac.get((int) x); long z = (fac.get((int) y) * fac.get((int) (x - y))) % mod; z = modInv(z); res = (res * z) % mod; return res; } static long modInv(long x) { return modExpo(x, mod - 2); } static long modExpo(long x, long y) { long res = 1; x = x % mod; while (y > 0) { if (y % 2 == 1) res = (res * x) % mod; y = y / 2; x = (x * x) % mod; } return res; } static int lowerBound(int n, long[] arr, long value) { int res = (int) 1e7; int l = 0, r = n - 1; while (l <= r) { int mid = l + (r - l) / 2; if (arr[mid] >= value) { res = mid; r = mid - 1; } else l = mid + 1; } return res; } } ",0 0951d079,71a4f6d2,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; import java.util.StringTokenizer; public class D { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader file = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(file.readLine()); int[] heights = new int[n]; StringTokenizer st = new StringTokenizer(file.readLine()); for(int i = 0; i < n; i++) { heights[i] = Integer.parseInt(st.nextToken()); } int[] dp = new int[n]; dp[0] = 0; Stack high = new Stack<>(); Stack low = new Stack<>(); high.push(0); low.push(0); for(int i = 1; i < n; i++) { dp[i] = dp[i-1]+1; while(!high.isEmpty() && heights[high.peek()] < heights[i]) { dp[i] = Math.min(dp[i], dp[high.peek()]+1); high.pop(); } if(!high.isEmpty()) { dp[i] = Math.min(dp[i], dp[high.peek()]+1); if(heights[high.peek()] == heights[i]) high.pop(); } while(!low.isEmpty() && heights[low.peek()] > heights[i]) { dp[i] = Math.min(dp[i], dp[low.peek()]+1); low.pop(); } if(!low.isEmpty()) { dp[i] = Math.min(dp[i], dp[low.peek()]+1); if(heights[low.peek()] == heights[i]) low.pop(); } high.push(i); low.push(i); } System.out.println(dp[n-1]); } } ","import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Vector; import java.util.InputMismatchException; import java.io.IOException; import java.util.Stack; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DDiscreteCentrifugalJumps solver = new DDiscreteCentrifugalJumps(); solver.solve(1, in, out); out.close(); } static class DDiscreteCentrifugalJumps { public void solve(int testNumber, InputReader s, PrintWriter w) { int n = s.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = s.nextInt(); int[] dp = new int[n]; for (int i = 0; i < n; i++) dp[i] = i; Stack dec = new Stack<>(); dec.push(0); Stack inc = new Stack<>(); inc.push(0); for (int i = 1; i < n; i++) { while (!dec.isEmpty() && a[dec.peek()] < a[i]) { dp[i] = Math.min(dp[i], dp[dec.peek()] + 1); dec.pop(); } if (!dec.isEmpty()) { dp[i] = Math.min(dp[i], dp[dec.peek()] + 1); if (a[dec.peek()] == a[i]) dec.pop(); } dec.push(i); while (!inc.isEmpty() && a[inc.peek()] > a[i]) { dp[i] = Math.min(dp[i], dp[inc.peek()] + 1); inc.pop(); } if (!inc.isEmpty()) { dp[i] = Math.min(dp[i], dp[inc.peek()] + 1); if (a[inc.peek()] == a[i]) inc.pop(); } inc.push(i); } w.println(dp[n - 1]); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } } ",1 1480a727,a7d1ce58,"/* * Everything is Hard * Before Easy * Jai Mata Dii */ 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 long mod = (long)(1e9+7); // static long mod = 998244353; // static Scanner sc = new Scanner(System.in); static FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); public static void main (String[] args) { int ttt = 1; ttt = sc.nextInt(); z :for(int tc=1;tc<=ttt;tc++){ int n = sc.nextInt(); long d[] = new long[n]; for(int i=0;i0;i--) { long dif = d[i] - d[i-1]; long nxt = 0; if(i+1= cur) { req[i] = 0; } else { req[i] = cur-dif; } // System.out.println(i+"" ""+req[i]); } long ans = 0, prev = 0; for(int i=0;iprev) { ans = ans + sumFrom(prev, req[i]); ans = ans + sumFrom(req[i], req[i] + (d[i]-d[i-1])); prev = req[i] + d[i] - d[i-1]; } else { ans = ans + sumFrom(prev, prev + (d[i]-d[i-1])); prev = prev + d[i] - d[i-1]; } } } } out.write(ans+""\n""); } out.close(); } private static long sumFrom(long l, long cur) { long is1 = (cur*(cur+1))/2; long is2 = (l*(l+1))/2; return is1 - is2; } static long pow(long a, long b){long ret = 1;while(b>0){if(b%2 == 0){a = (a*a)%mod;b /= 2;}else{ret = (ret*a)%mod;b--;}}return ret%mod;} static long gcd(long a,long b){if(b==0) return a; return gcd(b,a%b); } private static void sort(int[] a) {List k = new ArrayList<>();for(int val : a) k.add(val);Collections.sort(k);for(int i=0;i[] tre2){for(int i=0;i();}} private static void init(List[] tre2){for(int i=0;i();}} private static void sort(long[] a) {List k = new ArrayList<>();for(long val : a) k.add(val);Collections.sort(k);for(int i=0;i0) { int n = sc.nextInt(); long dsasds[] = new long[n]; for(int i=0;i0;i--) { long dif = dsasds[i] - dsasds[i-1]; long nxt = 0; if(i+1= cur) { dsasdsuyuiyubtrtr[i] = 0; } else { dsasdsuyuiyubtrtr[i] = cur-dif; } // System.out.println(i+"" ""+dsasdsuyuiyubtrtr[i]); } long dsasdscsacsauyuiyubtrtr = 0, kokopmom = 0; for(int i=0;ikokopmom) { dsasdscsacsauyuiyubtrtr = dsasdscsacsauyuiyubtrtr + sumFrom(kokopmom, dsasdsuyuiyubtrtr[i]); dsasdscsacsauyuiyubtrtr = dsasdscsacsauyuiyubtrtr + sumFrom(dsasdsuyuiyubtrtr[i], dsasdsuyuiyubtrtr[i] + (dsasds[i]-dsasds[i-1])); kokopmom = dsasdsuyuiyubtrtr[i] + dsasds[i] - dsasds[i-1]; } else { dsasdscsacsauyuiyubtrtr = dsasdscsacsauyuiyubtrtr + sumFrom(kokopmom, kokopmom + (dsasds[i]-dsasds[i-1])); kokopmom = kokopmom + dsasds[i] - dsasds[i-1]; } } } } System.out.println(dsasdscsacsauyuiyubtrtr); } } private static long sumFrom(long l, long cur) { long is1 = (cur*(cur+1))/2; long is2 = (l*(l+1))/2; return is1 - is2; } private static long solve(int i, int k, int pre, int a[], int dsasds[]) { if(i == a.length) { int dsasdsuyuiyubtrtr = pre*(l-dsasds[i-1]); return dsasdsuyuiyubtrtr; } if(dp[i][k]!=-1) return dp[i][k]; long cans = Integer.MAX_VALUE; if(k>0) { cans = 1L*pre*(dsasds[i]-dsasds[i-1]) + solve(i+1, k-1, pre, a, dsasds); } cans = Math.min(cans, 1L*pre*(dsasds[i]-dsasds[i-1]) + solve(i+1, k, a[i], a, dsasds)); return dp[i][k] = cans; } private static boolean same(char[] a, char[] b) { for(int i=0;i val, long mid) { for(int i=0;i=mid) return false; mid = mid + 1; } } return true; } static long fac[] = new long[100006]; static void precompute() { int n = 100005; fac[0] = 1; for(int i=1;i<=n;i++) fac[i] = (i * fac[i-1])%mod; } static long power(long base, long p, long mod2) { long dsasdscsacsauyuiyubtrtr = 1; while(p!=0) { if(p%2==0) { base = (base*base)%mod2; p/=2; } else { dsasdscsacsauyuiyubtrtr = (dsasdscsacsauyuiyubtrtr*base)%mod2; p-=1; } } return (int) (dsasdscsacsauyuiyubtrtr%mod2); } private static boolean checkl(char[] s, char t[]) { int i=0, j =0; while(im) { dsasdsuyuiyu = mid-1; } else { ret = mid; l = mid+1; } } return ret; } private static int find(int[] a, int l, int dsasdsuyuiyu, long m) { int ret = -1; while(l<=dsasdsuyuiyu) { int mid = (l+dsasdsuyuiyu)/2; if(a[mid]>=m) { ret = mid; dsasdsuyuiyu = mid-1; } else l = mid+1; } return ret; } private static boolean find(String ini, String a) { return !KMPSearch(ini,a); } static boolean KMPSearch(String pat, String txt) { 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.charAt(j) == txt.charAt(i)) { j++; i++; } if (j == M) { j = lps[j - 1]; return true; } else if (i < N && pat.charAt(j) != txt.charAt(i)) { if (j != 0) j = lps[j - 1]; else i = i + 1; } } return false; } static void computeLPSArray(String pat, int M, int lps[]) { int len = 0; int i = 1; lps[0] = 0; // lps[0] is always 0 // the loop calculates lps[i] for i = 1 to M-1 while (i < M) { if (pat.charAt(i) == pat.charAt(len)) { len++; lps[i] = len; i++; } else // (pat[i] != pat[len]) { // This is tricky. Consider the example. // AAACAAAA and i = 7. The idea is similar // to search step. if (len != 0) { len = lps[len - 1]; // Also, note that we do not increment // i here } else // if (len == 0) { lps[i] = len; i++; } } } } // private static boolean check(long n,long val) { // if(n == 0) return true; // // while(val<=n) { //// if(check(n-val,)) // } // } private static int find(int[] a, TreeMap ts, int lp) { int ret = 1; int idx = -1; for(int i=a.length-1;i>=0;i--) { if(a[i] == lp) { idx = i; break; } } idx--; int kokopmom = lp; while(idx>=0) { if((kokopmom - a[idx]) >= lp) { ret++; kokopmom = a[idx]; } idx--; } return ret; } private static void reverse(char[] s) { char a[] = new char[s.length]; for(int i=0;isize[fv]) { par[fv] = fu; size[fu] += size[fv]; } else { par[fu] = fv; size[fv] += size[fu]; } return true; } else return false; } } class pair{ long val; char c; pair(long val, char c){ this.val = val; this.c = c; } }",1 3bd7275e,8635bc10,"// \(≧▽≦)/ // Terminus // TODO : get good import java.io.*; import java.util.*; public class tank { static final FastScanner fs = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int t = fs.nextInt(); while(t-- > 0) run_case(); out.close(); } static void run_case() { int n = fs.nextInt(); long k = fs.nextLong(); int[] a = fs.readArray(n); ruffleSort(a); long sum = 0; for(int i: a) sum += i; if(sum <= k) { out.println(0); return; } long ans = sum - k, dif; for (int i = 1; i < n; i++) { sum -= a[n - i]; sum += a[0]; //out.println(sum); if(sum <= k) { ans = Math.min(ans, i); continue; } dif = sum - k; if(dif % (i + 1) == 0) { ans = Math.min(ans, dif / (i + 1) + i); } else { ans = Math.min(ans, dif / (i + 1) + 1 + i); } } out.println(ans); } static void ruffleSort(int[] a) { int n=a.length; Random r=new Random(); for (int i=0; i 0){ n = sc.nextInt(); k = sc.nextLong(); // a = sc.readArray(n); a = new long[n + 1]; for(int i = 1; i <= n; i++) a[i] = sc.nextInt(); System.out.println(slover()); } } static long slover(){ Arrays.sort(a); // for(int i = 1; i <= n; i++) System.out.print(a[i] + "" ""); // System.out.println(); s = new long[n + 1]; for(int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i]; // System.out.println(s[n]); long res = (long)1e10; // 能枚举进行赋值操作的元素个数 for(int i = 1; i <= n; i++){ // long selected = (n - i) * a[1]; // 最小的替换(n - i) long t; if(s[i] + (n-i)*a[1] - k <= 0) t = 0; else t = (long) Math.ceil((s[i] + (n-i) * a[1] - k) * 1.0 / (n-i+1)); // if(selected + s[i] <= k){ // t = 0; // }else{ // // System.out.println(""get in""); // t = (long)Math.ceil((s[i] + selected - k) * 1.0 / (1 + n - i)); // } res = Math.min(res, n - i + t); // System.out.println((n - i) + "" -> "" + (s[i] + selected - k) + "" "" + (1 + n - i) + "" ->"" + res); } return res; } 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 3088ca9c,f6ed6826,"import java.util.*; import java.io.*; public class MyClass { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t>0) { int n = sc.nextInt(); sc.nextLine(); String [] str = new String[n]; int res = 0; for(int i=0;i=0;p--) { sum+=arr[p]; if(sum>0) { count++; } else { break; } } res=Math.max(count , res); } System.out.println(res); t--; } } }"," import java.util.*; import java.io.*; public class Solution { static FastScanner scr=new FastScanner(); // static Scanner scr=new Scanner(System.in); static PrintStream out=new PrintStream(System.out); static StringBuilder sb=new StringBuilder(); static class pair{ int x;int y; pair(int x,int y){ this.x=x;this.y=y; } } static class triplet{ int x; long y; int z; triplet(int x,long y,int z){ this.x=x; this.y=y; this.z=z; } } 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(); } long gcd(long a,long b){ if(b==0) { return a; } return gcd(b,a%b); } int[] sort(int a[]) { int multiplier = 1, len = a.length, max = Integer.MIN_VALUE; int b[] = new int[len]; int bucket[]; for (int i = 0; i < len; i++) if (max < a[i]) max = a[i]; while (max / multiplier > 0) { bucket = new int[10]; for (int i = 0; i < len; i++) bucket[(a[i] / multiplier) % 10]++; for (int i = 1; i < 10; i++) bucket[i] += (bucket[i - 1]); for (int i = len - 1; i >= 0; i--) b[--bucket[(a[i] / multiplier) % 10]] = a[i]; for (int i = 0; i < len; i++) a[i] = b[i]; multiplier *= 10; } return a; } long modPow(long base,long exp) { if(exp==0) { return 1; } if(exp%2==0) { long res=(modPow(base,exp/2)); return (res*res); } return (base*modPow(base,exp-1)); } int []reverse(int a[]){ int b[]=new int[a.length]; int index=0; for(int i=a.length-1;i>=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>hm=new HashMap<>(); static void solve() { int n=scr.nextInt(); int count[]=new int[5]; int store[][]=new int[n][5]; int max=MIN; int length[]=new int[n]; for(int i=0;ia=new ArrayList<>(); for(int i=0;i0) { ans++; }else { break; } } max=Math.max(max, ans); } out.println(max); } static int MAX = Integer.MAX_VALUE; static int MIN = Integer.MIN_VALUE; public static void main(String []args) { int t=scr.nextInt(); while(t-->0) { solve(); } // solve(); // out.println(sb); } } ",0 4d279121,a3e272af,"import java.util.*; import java.io.*; @SuppressWarnings(""unchecked"") 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 m = input.nextInt(); int x = input.nextInt(); int arr[] = new int[n]; for(int i = 0; i < n; i++) arr[i] = input.nextInt(); PriorityQueue queue = new PriorityQueue(); out.println(""YES""); int res[] = new int[n]; for(int i = 1; i <= m; i++) { queue.add(new Pair(0, i)); } for(int i = 0; i < n; i++) { Pair p = queue.remove(); out.print(p.second()+"" ""); queue.add(new Pair(p.first()+arr[i], p.second())); } out.println(); out.flush(); } } } class Pair implements Comparable { public int x; public int y; public Pair(int x, int y) { this.x = x; this.y = y; } public int first() { return x; } public int second() { return y; } public int compareTo(Pair p) { if(this.first() == p.first()) { return 0; }else if(this.first() > p.first()) { return 1; }else { return -1; } } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while(st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); }catch(Exception e) { System.out.println(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String getString() { return next(); } }"," 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 9debf95c,ac596a43,"// A Computer is Like a mischievous genie. // It will give you exactly what you ask for, // but not always what you want // A code by Rahul Verma import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; public class Main { static class Clock { protected long start, stop; public void start() { start = System.currentTimeMillis(); } public void stop() { stop = System.currentTimeMillis(); } public String getTime() { return ((stop - start) + "" ms""); } } public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String[] nextSArray() { String sr[] = null; try { sr = br.readLine().trim().split("" ""); } catch (IOException e) { e.printStackTrace(); } return sr; } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long powmodulo(long a, long p) { if (p == 0) { return 1 % mod; } if (p == 1) { return a % mod; } long ans = 1; while (p > 0) { if ((p & 1) > 0) { ans = (ans * a) % mod; } a = (a * a) % mod; p = p >> 1; } return ans % mod; } static long mod = 1000000007; static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } static long fast_powerNumbers(long a, long n) { if (n == 1) { return a; } long ans = fast_powerNumbers(a, n / 2); if (n % 2 == 0) { return (ans * ans); } else { return ((ans * ans) * (a)); } } static void dfs_helper(int[][] arr, int i, int j, int team, int n, int m) { arr[i][j] = team; if (i - 1 >= 0 && arr[i - 1][j] == 1) { dfs(arr, i - 1, j, team, n, m); } if (j - 1 >= 0 && arr[i][j - 1] == 1) { dfs(arr, i, j - 1, team, n, m); } if (i + 1 < n && arr[i + 1][j] == 1) { dfs(arr, i + 1, j, team, n, m); } if (j + 1 < m && arr[i][j + 1] == 1) { dfs(arr, i, j + 1, team, n, m); } } static void dfs(int[][] arr, int i, int j, int team, int n, int m) { dfs_helper(arr, i, j, team, n, m); } static int parent[]; static int rank[]; static int find(int i) { if (parent[i] == -1) { parent[i] = i; return i; } if (parent[i] == i) { return i; } else { parent[i] = find(parent[i]); } return parent[i]; } static void unite(int s1, int s2) { if (rank[s1] > rank[s2]) { parent[s2] = s1; rank[s1] += rank[s2]; } else { parent[s1] = s2; rank[s2] += rank[s1]; } } public static long arr[]; public static int arr1[]; // static void seive(int n) { // arr = new int[n + 1]; // arr[0] = arr[1] = 1; // for (int i = 4; i <= n; i = i + 2) { // arr[i] = 1; // } // for (int i = 3; i * i <= n; i = i + 2) { // if (arr[i] == 0) { // for (int j = i * i; j <= n; j = j + i) { // arr[j] = 1; // } // // } // } // } static void seive(int n) { arr1=new int[n+1]; arr1[0]=arr1[1]=1; for (int i = 4; i <=n ; i+=2) { arr1[i]=1; } for (int i = 3; i*i <=n ; i+=2) { if (arr1[i] == 0) { for (int j = i*i; j <=n ; j+=i) { arr1[j]=1; } } } } public static boolean ccw(Point a,Point b,Point c) { long s1=(c.x-b.x)*(b.y-a.y) ; long s2 = (c.y-b.y)*(b.x-a.x); if(s1rank[p2]) { parent[p2]=p1; rank[p1]+=rank[p2]; // System.out.println(arr[p2]); sum[p1]+=sum[p2]; //sum[p2]+=sum[p1]; } else { parent[p1]=p2; rank[p2]+=rank[p1]; //System.out.println(arr[p1]); sum[p2]+=sum[p1]; //sum[p1]+=sum[p2]; } } } class Gaph { HashMap> hm; Gaph() { hm = new HashMap<>(); } Gaph(int n) { hm = new HashMap<>(); for (int i = 0; i < n; i++) { hm.put(i, new ArrayList()); } } // function for adding an edge................................................. public void addEdge(int a, int b, boolean isDir) { if (isDir) { if (hm.containsKey(a)) { hm.get(a).add(b); } else { hm.put(a, new ArrayList<>(Arrays.asList(b))); } } else { if (hm.containsKey(a)) { hm.get(a).add(b); } else if (!hm.containsKey(a)) { hm.put(a, new ArrayList<>(Arrays.asList(b))); } if (hm.containsKey(b)) { hm.get(b).add(a); } else if (!hm.containsKey(b)) { hm.put(b, new ArrayList<>(Arrays.asList(a))); } } } } // out.println(al.toString().replaceAll(""[\\[|\\]|,]"","""")); ","// A Computer is Like a mischievous genie. // It will give you exactly what you ask for, // but not always what you want // A code by Rahul Verma import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; public class Main { static class Clock { protected long start, stop; public void start() { start = System.currentTimeMillis(); } public void stop() { stop = System.currentTimeMillis(); } public String getTime() { return ((stop - start) + "" ms""); } } public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String[] nextSArray() { String sr[] = null; try { sr = br.readLine().trim().split("" ""); } catch (IOException e) { e.printStackTrace(); } return sr; } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long powmodulo(long a, long p) { if (p == 0) { return 1 % mod; } if (p == 1) { return a % mod; } long ans = 1; while (p > 0) { if ((p & 1) > 0) { ans = (ans * a) % mod; } a = (a * a) % mod; p = p >> 1; } return ans % mod; } static long mod = 1000000007; static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } static long fast_powerNumbers(long a, long n) { if (n == 1) { return a; } long ans = fast_powerNumbers(a, n / 2); if (n % 2 == 0) { return (ans * ans); } else { return ((ans * ans) * (a)); } } static void dfs_helper(int[][] arr, int i, int j, int team, int n, int m) { arr[i][j] = team; if (i - 1 >= 0 && arr[i - 1][j] == 1) { dfs(arr, i - 1, j, team, n, m); } if (j - 1 >= 0 && arr[i][j - 1] == 1) { dfs(arr, i, j - 1, team, n, m); } if (i + 1 < n && arr[i + 1][j] == 1) { dfs(arr, i + 1, j, team, n, m); } if (j + 1 < m && arr[i][j + 1] == 1) { dfs(arr, i, j + 1, team, n, m); } } static void dfs(int[][] arr, int i, int j, int team, int n, int m) { dfs_helper(arr, i, j, team, n, m); } static int parent[]; static int rank[]; static int find(int i) { if (parent[i] == -1) { parent[i] = i; return i; } if (parent[i] == i) { return i; } else { parent[i] = find(parent[i]); } return parent[i]; } static void unite(int s1, int s2) { if (rank[s1] > rank[s2]) { parent[s2] = s1; rank[s1] += rank[s2]; } else { parent[s1] = s2; rank[s2] += rank[s1]; } } public static long arr[]; public static int arr1[]; // static void seive(int n) { // arr = new int[n + 1]; // arr[0] = arr[1] = 1; // for (int i = 4; i <= n; i = i + 2) { // arr[i] = 1; // } // for (int i = 3; i * i <= n; i = i + 2) { // if (arr[i] == 0) { // for (int j = i * i; j <= n; j = j + i) { // arr[j] = 1; // } // // } // } // } static void seive(int n) { arr1=new int[n+1]; arr1[0]=arr1[1]=1; for (int i = 4; i <=n ; i+=2) { arr1[i]=1; } for (int i = 3; i*i <=n ; i+=2) { if (arr1[i] == 0) { for (int j = i*i; j <=n ; j+=i) { arr1[j]=1; } } } } public static boolean ccw(Point a,Point b,Point c) { long s1=(c.x-b.x)*(b.y-a.y) ; long s2 = (c.y-b.y)*(b.x-a.x); if(s1rank[p2]) { parent[p2]=p1; rank[p1]+=rank[p2]; // System.out.println(arr[p2]); sum[p1]+=sum[p2]; //sum[p2]+=sum[p1]; } else { parent[p1]=p2; rank[p2]+=rank[p1]; //System.out.println(arr[p1]); sum[p2]+=sum[p1]; //sum[p1]+=sum[p2]; } } } class Gaph { HashMap> hm; Gaph() { hm = new HashMap<>(); } Gaph(int n) { hm = new HashMap<>(); for (int i = 0; i < n; i++) { hm.put(i, new ArrayList()); } } // function for adding an edge................................................. public void addEdge(int a, int b, boolean isDir) { if (isDir) { if (hm.containsKey(a)) { hm.get(a).add(b); } else { hm.put(a, new ArrayList<>(Arrays.asList(b))); } } else { if (hm.containsKey(a)) { hm.get(a).add(b); } else if (!hm.containsKey(a)) { hm.put(a, new ArrayList<>(Arrays.asList(b))); } if (hm.containsKey(b)) { hm.get(b).add(a); } else if (!hm.containsKey(b)) { hm.put(b, new ArrayList<>(Arrays.asList(a))); } } } } // out.println(al.toString().replaceAll(""[\\[|\\]|,]"","""")); ",1 bdfe8110,c57a973e,"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=0;i--) forced[i]=Math.min(forced[i], forced[i+1]+1); for (int i=0;i> dp= new HashMap<>(); // Calculating SPF (Smallest Prime Factor) for every // number till MAXN. // Time Complexity : O(nloglogn) public void sieve() { spf[1] = 1; for (int i=2; i getFactorization(int x) { if(dp.containsKey(x)) return dp.get(x); Set ret = new HashSet<>(); while (x != 1) { if(spf[x]!=2) ret.add(spf[x]); x = x / spf[x]; } dp.put(x,ret); return ret; } // function to find first index >= x public int lowerIndex(List arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) >= x) h = mid - 1; else l = mid + 1; } return l; } // function to find last index <= y public int upperIndex(List arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) <= y) l = mid + 1; else h = mid - 1; } return h; } // function to count elements within given range public int countInRange(List arr, int n, int x, int y) { // initialize result int count = 0; count = upperIndex(arr, n, y) - lowerIndex(arr, n, x) + 1; return count; } ArrayListA = new ArrayList<>(); ArrayListB = new ArrayList<>(); ArrayList> L = new ArrayList>(); public void solve(InputReader in, PrintWriter out) { int n= in.nextInt(); int[] A= new int[n]; final int INF= 1000000000; List pos= new ArrayList<>(); for (int i = 0; i < n; i++){ A[i]= in.nextInt(); if (A[i] == 1) pos.add(i); } int cnt = pos.size(); int[][] dp= new int[cnt][n+1]; for(int[] temp: dp) Arrays.fill(temp,INF); if (cnt == 0){ out.println(0); return; } for (int i = n - 1; i >= 0; i--){ dp[cnt - 1][i] = dp[cnt - 1][i + 1]; if (A[i] != 1){ dp[cnt - 1][i] = Math.min(dp[cnt - 1][i], Math.abs(i - pos.get(pos.size()-1))); } } for (int i = cnt - 2; i >= 0; i--){ for (int j = n - 1; j >= 0; j--){ dp[i][j] = dp[i][j + 1]; if (A[j] != 1){ dp[i][j] = Math.min(dp[i][j], Math.abs(j - pos.get(i)) + dp[i + 1][j + 1]); } } } out.println(dp[0][0]); } void backtrack(PrintWriter out, int i, List considered, int used, int sum, int cur, int g){ if (used == considered.size()){ int percentage = (cur * 100) / sum; if (percentage >= g){ considered.add(percentage); L.add(considered); } return; } if (i == B.size()){ return; } for (int j = i; j < B.size(); j++){ List au = new ArrayList<>(considered); au.add(j + 1); backtrack(out, j + 1, au, used, sum, cur + B.get(j), g); } } public int _gcd(int a, int b) { if(b == 0) { return a; } else { return _gcd(b, a % b); } } } static class Tuple implements Comparable{ int x, y, z; public Tuple(int x, int y, int z){ this.x= x; this.y= y; this.z=z; } @Override public int compareTo(Tuple o){ return this.x-o.x; } } static class Pair implements Comparable{ public int x; public int y; public Pair(int x, int y){ this.x= x; this.y= y; } @Override public int compareTo(Pair o) { return this.x-o.x; } } // fast input reader class; static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { String line = null; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble(){ return Double.parseDouble(nextToken()); } public long nextLong(){ return Long.parseLong(nextToken()); } } } ","import java.io.*; import java.util.*; public class D { public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws IOException { readInput(); out.close(); } static List o1, o0; static int[][] dp; static int solve(int i, int j) { if (i >= dp.length) return 0; if (j >= dp[0].length) return Integer.MAX_VALUE/10; if (dp[i][j] == -1) { dp[i][j] = Integer.min(solve(i,j+1), solve(i+1,j+1) + Math.abs(o1.get(i)-o0.get(j))); } return dp[i][j]; } public static void readInput() throws IOException { // br = new BufferedReader(new FileReader("".in"")); // out = new PrintWriter(new FileWriter("".out"")); int n; n = Integer.parseInt(br.readLine()); int[] a= new int[n]; StringTokenizer st = new StringTokenizer(br.readLine()); o1 = new ArrayList(); o0 = new ArrayList(); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(st.nextToken()); if (a[i] == 1) o1.add(i); else o0.add(i); } if (o1.size() == 0) { out.println(0); return; } dp = new int[o1.size()][o0.size()]; for (int[] x: dp) Arrays.fill(x, -1); out.println(solve(0,0)); } } ",0 3b5cec19,8f31b279,"import java.util.*; import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Test{ static class Pair implements Comparable{ 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{ 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=0;i--){ p = Math.min(p+1, a[i]); r[i] = p; } for(int i=0;i 0) { bf.readLine(); String s[] = bf.readLine().trim().split(""\\s+""); n = Integer.parseInt(s[0]); k = Integer.parseInt(s[1]); s = bf.readLine().trim().split(""\\s+""); pos = new int[k]; temp = new int[k]; a = new long[n]; for(int i=0;i= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static int mod = (int) (1e9 + 7); public static class pair implements Comparator { int x; int y; public pair() { } public pair(int x, int y) { this.x = x; this.y = y; } @Override public int compare(pair o1, pair o2) { return o1.y - o2.y; } } public static long modularpow(long a, long b) { long res = 1; if (b == 0) return res; else { while (b > 0) { if (b % 2 == 1) { res *= a; res %= mod; } a = a * a; a %= mod; b /= 2; } return res % mod; } } public static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } public static String binary(int a) { String s1 = """"; while (a > 0) { s1 = a % 2 + s1; a /= 2; } return s1; } public static int Lower_Bound(long a[], int l, int r, long k) { while (r - l > 1) { int mid = l + (r - l) / 2; if (a[mid] <= k) l = mid; else r = mid; } return l; } public static int Upper_Bound(int a[], int l, int r, int k) { while (r - l > 1) { int mid = (l + r) / 2; if (a[mid] <= k) l = mid; else r = mid; } return l + 1; } public static void main(String arg[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while (t-- > 0) { br.readLine(); String s[]=br.readLine().split("" ""); int n=Integer.parseInt(s[0]); int k=Integer.parseInt(s[1]); long a[]=new long[n]; long b[]=new long[n]; long c[]=new long[n]; Arrays.fill(c,Integer.MAX_VALUE); String s1[]=br.readLine().split("" ""); s=br.readLine().split("" ""); for(int i=0;i=0;i--){ p=Math.min(p+1,c[i]); b[i]=p; } for(int i=0;i list1 = new ArrayList<>() ; static ArrayList list2 = new ArrayList<>() ; static int n , m ; static long dp[][] ; static long solver(int i , int j ){ // i = empty chairs if (j == m)return 0 ; int tt1 = n-i ; int tt2 = m-j ; if (n-i < m-j)return Long.MAX_VALUE/2 ; if ( dp[i][j] != -1 )return dp[i][j] ; long a = solver(i+1 , j) ; long b = abs( list1.get(i) - list2.get(j)) + solver(i+1 , j+1) ; return dp[i][j] = min(a , b) ; } public static void main(String[] args) throws IOException { // try { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int N = in.nextInt() ; int a[] = in.readArray(N) ; for (int i = 0; i list = new ArrayList<>(); static boolean A[] = new boolean[2 * 90000001]; static void seive(int n) { int maxn = n; //int maxn = 1000000 ; A[0] = A[1] = true; for (int i = 2; i * i <= maxn; i++) { if (!A[i]) { for (int j = i * i; j <= maxn; j += i) A[j] = true; } } for (int i = 2; i <= maxn; i++) if (!A[i]) list.add(i); } static int findLCA(int a, int b, int par[][], int depth[]) { if (depth[a] > depth[b]) { a = a ^ b; b = a ^ b; a = a ^ b; } int diff = depth[b] - depth[a]; for (int i = 19; i >= 0; i--) { if ((diff & (1 << i)) > 0) { b = par[b][i]; } } if (a == b) return a; for (int i = 19; i >= 0; i--) { if (par[b][i] != par[a][i]) { b = par[b][i]; a = par[a][i]; } } return par[a][0]; } static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } static void formArrayForBinaryLifting(int n, int par[][]) { for (int j = 1; j < 20; j++) { for (int i = 0; i < n; i++) { if (par[i][j - 1] == -1) continue; par[i][j] = par[par[i][j - 1]][j - 1]; } } } static void sort(int ar[]) { int n = ar.length; ArrayList a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static void sort1(long ar[]) { int n = ar.length; ArrayList a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static long ncr(long n, long r, long mod) { if (r == 0) return 1; long val = ncr(n - 1, r - 1, mod); val = (n * val) % mod; val = (val * modInverse(r, mod)) % mod; return val; } static long fast_pow(long base, long n, long M) { if (n == 0) return 1; if (n == 1) return base % M; long halfn = fast_pow(base, n / 2, M); if (n % 2 == 0) return (halfn * halfn) % M; else return (((halfn * halfn) % M) * base) % M; } static long modInverse(long n, long M) { return fast_pow(n, M - 2, M); } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } static class FastOutput implements AutoCloseable, Closeable, Appendable { private static final int THRESHOLD = 32 << 10; private final Writer os; private StringBuilder cache = new StringBuilder(THRESHOLD * 2); public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } private void afterWrite() { if (cache.length() < THRESHOLD) { return; } flush(); } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); afterWrite(); return this; } public FastOutput append(long c) { cache.append(c); afterWrite(); return this; } public FastOutput append(String c) { cache.append(c); afterWrite(); return this; } public FastOutput println() { return append(System.lineSeparator()); } public FastOutput flush() { try { // boolean success = false; // if (stringBuilderValueField != null) { // try { // char[] value = (char[]) stringBuilderValueField.get(cache); // os.write(value, 0, cache.length()); // success = true; // } catch (Exception e) { // } // } // if (!success) { os.append(cache); // } os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } } ","import java.io.*; import java.util.*; public class A{ public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } // main solver static class Task{ double eps= 0.00000001; static final int MAXN = 10000001; // stores smallest prime factor for every number static int spf[] = new int[MAXN]; Map> dp= new HashMap<>(); // Calculating SPF (Smallest Prime Factor) for every // number till MAXN. // Time Complexity : O(nloglogn) public void sieve() { spf[1] = 1; for (int i=2; i getFactorization(int x) { if(dp.containsKey(x)) return dp.get(x); Set ret = new HashSet<>(); while (x != 1) { if(spf[x]!=2) ret.add(spf[x]); x = x / spf[x]; } dp.put(x,ret); return ret; } // function to find first index >= x public int lowerIndex(List arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) >= x) h = mid - 1; else l = mid + 1; } return l; } // function to find last index <= y public int upperIndex(List arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) <= y) l = mid + 1; else h = mid - 1; } return h; } // function to count elements within given range public int countInRange(List arr, int n, int x, int y) { // initialize result int count = 0; count = upperIndex(arr, n, y) - lowerIndex(arr, n, x) + 1; return count; } ArrayListA = new ArrayList<>(); ArrayListB = new ArrayList<>(); ArrayList> L = new ArrayList>(); public void solve(InputReader in, PrintWriter out) { int n= in.nextInt(); int[] A= new int[n]; final int INF= 1000000000; List pos= new ArrayList<>(); for (int i = 0; i < n; i++){ A[i]= in.nextInt(); if (A[i] == 1) pos.add(i); } int cnt = pos.size(); int[][] dp= new int[cnt][n+1]; for(int[] temp: dp) Arrays.fill(temp,INF); if (cnt == 0){ out.println(0); return; } for (int i = n - 1; i >= 0; i--){ dp[cnt - 1][i] = dp[cnt - 1][i + 1]; if (A[i] != 1){ dp[cnt - 1][i] = Math.min(dp[cnt - 1][i], Math.abs(i - pos.get(pos.size()-1))); } } for (int i = cnt - 2; i >= 0; i--){ for (int j = n - 1; j >= 0; j--){ dp[i][j] = dp[i][j + 1]; if (A[j] != 1){ dp[i][j] = Math.min(dp[i][j], Math.abs(j - pos.get(i)) + dp[i + 1][j + 1]); } } } out.println(dp[0][0]); } void backtrack(PrintWriter out, int i, List considered, int used, int sum, int cur, int g){ if (used == considered.size()){ int percentage = (cur * 100) / sum; if (percentage >= g){ considered.add(percentage); L.add(considered); } return; } if (i == B.size()){ return; } for (int j = i; j < B.size(); j++){ List au = new ArrayList<>(considered); au.add(j + 1); backtrack(out, j + 1, au, used, sum, cur + B.get(j), g); } } public int _gcd(int a, int b) { if(b == 0) { return a; } else { return _gcd(b, a % b); } } } static class Tuple implements Comparable{ int x, y, z; public Tuple(int x, int y, int z){ this.x= x; this.y= y; this.z=z; } @Override public int compareTo(Tuple o){ return this.x-o.x; } } static class Pair implements Comparable{ public int x; public int y; public Pair(int x, int y){ this.x= x; this.y= y; } @Override public int compareTo(Pair o) { return this.x-o.x; } } // fast input reader class; static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { String line = null; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble(){ return Double.parseDouble(nextToken()); } public long nextLong(){ return Long.parseLong(nextToken()); } } } ",0 6b97058e,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 4be01508,b7de5c19,"import java.util.*; import java.io.*; public class q3{ static FastScanner fs = new FastScanner(); static PrintWriter pw = new PrintWriter(System.out); static List> list; public static void main(String[] args){ int tt = fs.nextInt(); for (int t=0;t(); for (int i=0;i()); for (int i=0;i 2){ pw.println(-1); return; } else if (list.get(i).size() == 1){ start = i; } } int[] ans = new int[n-1]; int prev = -1; int cur = start; int curWeight = 2; while (true){ Edge now = list.get(cur).get(0); if (now.node == prev){ if (list.get(cur).size() == 1){ break; } now = list.get(cur).get(1); } ans[now.index] = 5 - curWeight; curWeight = ans[now.index]; prev = cur; cur = now.node; } for (int i : ans){ pw.printf(""%d "", i); } pw.println(); } static class Edge { int node; int index; public Edge(int node, int index){ this.node = node; this.index = index; } } // ----------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()); } } } ","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 3dd65549,c1638a45,"import java.util.*; import java.io.*; public class codeforces { public static void main(String[] args) throws Exception { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); char[] a=sc.next().toCharArray(); char[] b=sc.next().toCharArray(); int e0=0; int e1=0; int o0=0; int o1=0; for(int i=0;i { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + "" "" + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + "" "" + y + "" "" + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }","import java.io.*; import java.util.*; public class qC { 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()); char[] curr = br.readLine().toCharArray(); char[] sol = br.readLine().toCharArray(); // int oddP = 0; // int evenP = 0; // int even1 = 0; // int odd1 = 0; // int even0 = 0; // int odd0 = 0; // boolean alreadySolved = true; // for(int i = 0;i < N;i++) { // if(curr[i] == sol[i]) { // evenP++; // if(curr[i] == '1') even1++; // else even0++; // } // else { // oddP++; // if(curr[i] == '1') odd1++; // else odd0++; // alreadySolved = false; // } // } // // if(alreadySolved) { // System.out.println(0); // continue; // } // int minSwaps = Integer.MAX_VALUE; // if(N % 2 == 1) { // if(evenP % 2 == 1 && Math.abs(odd1 - odd0) <= 1 && odd1 > 0 && odd0 > 0) { // minSwaps = Math.min(minSwaps, oddP); // } // if(oddP % 2 == 0 && Math.abs(even1 - even0) <= 1 && even1 > 0 && even1 > 0) { // minSwaps = Math.min(minSwaps, evenP); // } // } // else { // if(evenP % 2 == 0 && Math.abs(odd1 - odd0) <= 1 && odd1 > 0 && odd0 > 0) { // minSwaps = Math.min(minSwaps, oddP); // } // if(oddP % 2 == 1 && Math.abs(even1 - even0) <= 1 && even1 > 0 && even1 > 0) { // minSwaps = Math.min(minSwaps, evenP); // } // } // System.out.println((minSwaps == Integer.MAX_VALUE) ? -1: minSwaps); int curr1 = 0; int sol1 = 0; int mismatch = 0; for(int i = 0;i < N;i++) { if(curr[i] == '1') curr1++; if(sol[i] == '1') sol1++; if(curr[i] != sol[i]) mismatch++; } int minAns = Integer.MAX_VALUE; //even operations if(curr1 == sol1 && mismatch % 2 == 0) { minAns = Math.min(mismatch, minAns); } //odd operations for(int i = 0;i < N;i++) { if(curr[i] == '1') { int tempcurr1 = N - curr1 + 1; int tempmismatch; if(sol[i] == '0') { tempmismatch = N - mismatch; } else { tempmismatch = N - mismatch - 1; } if(tempcurr1 == sol1 && tempmismatch % 2 == 0) { minAns = Math.min(minAns, tempmismatch + 1); } } } System.out.println((minAns == Integer.MAX_VALUE) ? -1 : minAns); } } } ",0 6e207cbf,76ad805a,"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); } } } } ","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 3368f340,d8a171a3,"//package Codeforces; import java.io.*; import java.util.*; public class Menorah { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); while (t-->0){ int n = sc.nextInt(); char[] a = sc.next().toCharArray(); char[] b = sc.next().toCharArray(); int a1=0, b1=0; for(int i=0;i list = new ArrayList<>(); for(int i:arr) list.add(i); Collections.sort(list); 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 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 a8f7c8b7,c1638a45," import java.util.Scanner; public class Menorah { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); char initial[] = sc.next().toCharArray(); char desired[] = sc.next().toCharArray(); int lit1 = 0, lit2 = 0; int ans = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { if (initial[i] == '1') { ++lit1; } if (desired[i] == '1') { ++lit2; } } if (lit1 == lit2) { int count = 0; for (int i = 0; i < n; i++) { if (initial[i] != desired[i]) { ++count; } } ans = Math.min(count, ans); } if (lit2 == (n - lit1 + 1)) { int count = 0; for (int i = 0; i < n; i++) { if (initial[i] == desired[i]) { ++count; } } ans = Math.min(ans, count); } if (ans == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(ans); } } } }","import java.io.*; import java.util.*; public class qC { 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()); char[] curr = br.readLine().toCharArray(); char[] sol = br.readLine().toCharArray(); // int oddP = 0; // int evenP = 0; // int even1 = 0; // int odd1 = 0; // int even0 = 0; // int odd0 = 0; // boolean alreadySolved = true; // for(int i = 0;i < N;i++) { // if(curr[i] == sol[i]) { // evenP++; // if(curr[i] == '1') even1++; // else even0++; // } // else { // oddP++; // if(curr[i] == '1') odd1++; // else odd0++; // alreadySolved = false; // } // } // // if(alreadySolved) { // System.out.println(0); // continue; // } // int minSwaps = Integer.MAX_VALUE; // if(N % 2 == 1) { // if(evenP % 2 == 1 && Math.abs(odd1 - odd0) <= 1 && odd1 > 0 && odd0 > 0) { // minSwaps = Math.min(minSwaps, oddP); // } // if(oddP % 2 == 0 && Math.abs(even1 - even0) <= 1 && even1 > 0 && even1 > 0) { // minSwaps = Math.min(minSwaps, evenP); // } // } // else { // if(evenP % 2 == 0 && Math.abs(odd1 - odd0) <= 1 && odd1 > 0 && odd0 > 0) { // minSwaps = Math.min(minSwaps, oddP); // } // if(oddP % 2 == 1 && Math.abs(even1 - even0) <= 1 && even1 > 0 && even1 > 0) { // minSwaps = Math.min(minSwaps, evenP); // } // } // System.out.println((minSwaps == Integer.MAX_VALUE) ? -1: minSwaps); int curr1 = 0; int sol1 = 0; int mismatch = 0; for(int i = 0;i < N;i++) { if(curr[i] == '1') curr1++; if(sol[i] == '1') sol1++; if(curr[i] != sol[i]) mismatch++; } int minAns = Integer.MAX_VALUE; //even operations if(curr1 == sol1 && mismatch % 2 == 0) { minAns = Math.min(mismatch, minAns); } //odd operations for(int i = 0;i < N;i++) { if(curr[i] == '1') { int tempcurr1 = N - curr1 + 1; int tempmismatch; if(sol[i] == '0') { tempmismatch = N - mismatch; } else { tempmismatch = N - mismatch - 1; } if(tempcurr1 == sol1 && tempmismatch % 2 == 0) { minAns = Math.min(minAns, tempmismatch + 1); } } } System.out.println((minAns == Integer.MAX_VALUE) ? -1 : minAns); } } } ",0 018c9543,d1dbc56a,"import java.io.*; import java.util.*; public class Main { public static int n; public static int a[] = new int[12]; public static int b[] = new int[12]; public static boolean f(int p) { if (p > n) return false; for (int i = 0; i < p; i++) { b[p] = a[p] + b[i]; for (int j = 0; j < p; j++) { if (b[j] == b[p]) return true; } if (f(p + 1)) { return true; } // b[p] = b[i] - a[p]; // for (int j = 0; j < p; j++) { // if (b[j] == b[p]) // return true; // } // if (f(p + 1)) { // return true; // } } return false; } public static void main(String[] args) throws Exception { int T = r.readInt(); for (int t = 0; t < T; t++) { n = r.readInt(); for (int i = 1; i <= n; i++) { a[i] = r.readInt(); } boolean ans = false; if (n == 1) { ans = a[1] == 0; } else { b[0] = 1; ans = f(1); } if (ans) { System.out.println(""YES""); } else { System.out.println(""NO""); } } } static public InputReader r = new InputReader(System.in); static public OutputWriter w = new OutputWriter(System.out); static public 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 readInt() { 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[] readIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = this.readInt(); } return array; } public long[] readLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = this.readLong(); } return array; } public long readLong() { 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 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); } } static public class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } } // 1 // 10 // 1 2 4 8 16 32 64 128 256 512","import java.util.*; import java.io.*; public class D { static ArrayList set = new ArrayList(); static int[] a; static int test; public static void main(String[] args) throws IOException{ Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int q=0;q(); if(b.length!=0) { if(subset(b,0)) yes = true; } } if(yes) { System.out.println(""YES""); }else { System.out.println(""NO""); } } } static boolean subset(int[] a,int i) { // System.out.println(set); if(i>=a.length) { if(binary(new int[set.size()],0)) return true; return false; } set.add(a[i]); if(subset(a,i+1))return true; set.remove(set.size()-1); if(subset(a,i+1))return true; return false; } static boolean binary(int[] b,int i) { if(i==b.length) { int sum=0; for(int j=0;j0) { int n = sc.nextInt(), k = sc.nextInt(); int[] a = sc.nextIntArray(k); int[] t = sc.nextIntArray(k); int[] array = new int[n]; Arrays.fill(array, Integer.MAX_VALUE); for(int i = 0; i < k; i++) array[a[i] - 1] = t[i]; int[] pre = new int[n]; int[] post = new int[n]; int prev = (int)2e9; for(int i = 0; i < n; i++) prev = pre[i] = Math.min(prev + 1, array[i]); prev = (int)2e9; for(int i = n - 1; i >= 0; i--) prev = post[i] = Math.min(prev + 1, array[i]); for(int i = 0; i < n; i++) array[i] = Math.min(pre[i], post[i]); for(int i = 0; i < n; i++) pw.print(array[i] + (i == n - 1 ? ""\n"" : "" "")); } pw.flush(); } public static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) array[i] = new Integer(nextInt()); return array; } public long[] nextLongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public double[] nextDoubleArray(int n) throws IOException { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } public static int[] shuffle(int[] a) { int n = a.length; Random rand = new Random(); for (int i = 0; i < n; i++) { int tmpIdx = rand.nextInt(n); int tmp = a[i]; a[i] = a[tmpIdx]; a[tmpIdx] = tmp; } return a; } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } } ","import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.StringTokenizer; public class Main { static FastReader fr; static int arrForIndexSort[]; 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{ int first; int second; Pair(int first, int second){ this.first = first; this.second = second; } @Override public boolean equals(Object b) { Pair a = (Pair)b; if(this.first == a.first && this.second==a.second) { return true; } return false; } } class PairSorter implements Comparator{ public int compare(Pair a, Pair b) { if(a.first!=b.first) { return a.first-b.first; } return a.second-b.second; } } class IndexSorter implements Comparator{ public int compare(Integer a, Integer b) { //desc if(arrForIndexSort[b]==arrForIndexSort[a]) { return b-a; } return arrForIndexSort[b]-arrForIndexSort[a]; } } class ListSorter implements Comparator{ public int compare(List a, List b) { return b.size()-a.size(); } } static class DisjointSet{ int[] dsu; public DisjointSet(int n) { makeSet(n); } public void makeSet(int n) { dsu = new int[n+1]; //*** 1 Based indexing *** for(int i=1;i<=n;i++) { dsu[i] = -1; } } public int find(int i) { while(dsu[i] > 0) { i = dsu[i]; } return i; } public void union(int i, int j) { int iRep = find(i); int jRep = find(j); if(iRep == jRep) { return; } if(dsu[iRep]>dsu[jRep]) { dsu[jRep] += dsu[iRep]; dsu[iRep] = jRep; } else { dsu[iRep] += dsu[jRep]; dsu[jRep] = iRep; } } } public static void main(String[] args) { fr = new FastReader(); int T = 1; T = fr.nextInt(); int t1 = T; while (T-- > 0) { solve(); } } public static void solve() { int n=fr.nextInt(); int k=fr.nextInt(); int[] a = new int[k]; int[] map = new int[n]; int[] temp = new int[n]; Arrays.fill(temp, Integer.MAX_VALUE); Arrays.fill(map, Integer.MAX_VALUE); int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for(int i=0;i=0;i--) { int airConditioner = map[i]; if(airConditioner0) { int n=sc.nextInt(); int m=sc.nextInt(); int x=sc.nextInt(); PriorityQueue pq=new PriorityQueue<>(new myComp()); myarr a[]=new myarr[n]; int b[]=new int[n]; // int c[]=new int[m]; for(int i=0;i { public int compare(pair p1,pair p2) { if(p1.b==p2.b) return 0; else if(p1.b { public int compare(myarr a,myarr b) { if(a.val==b.val) return 0; else if(a.val 0) { int n = fr.nextInt(), m = fr.nextInt(), x = fr.nextInt(); ArrayList pp = new ArrayList<>(); int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = fr.nextInt(); Pair pr = new Pair(A[i], i); pp.add(pr); } Collections.sort(pp); Collections.reverse(pp); int ps[] = new int[n]; int pk[] = new int[n]; Arrays.fill(ps, 0); Arrays.fill(pk, 0); int index = 0; for (int i = 0; i < n; i++) { if (pk[index] < x) { pk[index] += pp.get(i).a; } ps[pp.get(i).b] = index + 1; index++; index = index == m ? 0 : index; } pt.println(""YES""); for (int i = 0; i < n; i++) { pt.print(ps[i] + "" ""); } pt.println(); t--; } pt.close(); } catch ( Exception e) { return; } } static void merge(long arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; long L[] = new long[n1]; long R[] = new long[n2]; for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void sort(long arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; sort(arr, l, m); sort(arr, m + 1, r); merge(arr, l, m, r); } } static class Pair implements Comparable { int a, b; Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair o) { // TODO Auto-generated method stub if (this.a != o.a) return Integer.compare(this.a, o.a); else return Integer.compare(this.b, o.b); // return 0; } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair) o; return p.a == a && p.b == b; } return false; } } static int binarySearch(int arr[], int first, int last, int key) { int mid = (first + last) / 2; while (first <= last) { if (arr[mid] < key) { first = mid + 1; } else if (arr[mid] == key) { return mid; } else { last = mid - 1; } mid = (first + last) / 2; } return -1; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } ",0 3f939694,54488276,"import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; import java.io.*; public class Main { public static void main(String args[])throws Exception{ Input sc=new Input(); StringBuilder sb=new StringBuilder(); int t=sc.readInt(); for(int f=0;f lst=new ArrayList<>(); int a[]=new int[n]; for(int j=0;j(s2+v2)){ s1+=v1; s2+=v2; co++; } //System.out.println(lst.get(j).a+"" ""+lst.get(j).b); } ans=Math.max(co,ans); } // count all 'a' in each string sb.append(ans+""\n""); } System.out.print(sb); } } class pair implements Comparable{ int a; int b; pair(int a,int b){ this.a=a; this.b=b; } @Override public int compareTo(pair o) { // if(this.a!=o.a){ // return o.a-this.a; // }else // return this.b-o.b; return -(this.a-this.b)+(o.a-o.b); } } class Input{ BufferedReader br; StringTokenizer st; Input(){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""""); } public int[] readArray() throws Exception{ st=new StringTokenizer(br.readLine()); int a[]=new int[st.countTokens()]; for(int i=0;i 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; 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;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 ba468e1f,fb312dc6,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Codeforces { public static void main(String[] args) { FastReader fastReader = new FastReader(); int t = fastReader.nextInt(); while (t-- > 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; } } } ","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.StringTokenizer; public class D { static class Pair{ int val; char c; Pair(int a,char b){ this.val=a; this.c=b; } } public static void main(String[] args) { FastScanner sc=new FastScanner(); int t=sc.nextInt(); PrintWriter pw=new PrintWriter(System.out); while(t-->0) { int n=sc.nextInt(); int[] a=sc.readArray(n); char[] s=sc.next().toCharArray(); boolean ok=true; ArrayList blues=new ArrayList<>(); ArrayList reds=new ArrayList<>(); for(int i=0;i(start++)){ ok=false; break; } } if(ok){ pw.println(""YES""); } else { pw.println(""NO""); } } pw.flush(); } 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 0; q--){ st = new StringTokenizer(f.readLine()); st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); int[] a = new int[k]; st = new StringTokenizer(f.readLine()); for(int i = 0; i < k; i++){ a[i] = Integer.parseInt(st.nextToken()) - 1; } int[] arr = new int[n]; int[] b = new int[k]; st = new StringTokenizer(f.readLine()); Arrays.fill(arr, Integer.MAX_VALUE-1); for(int i = 0; i < k; i++){ b[i] = Integer.parseInt(st.nextToken()); arr[a[i]] = b[i]; } int[] l = new int[n]; int[] r = new int[n]; for(int i = 0; i < n; i++){ if(i == 0) l[i] = arr[i]; else l[i] = Math.min(l[i-1]+1, arr[i]); } for(int i = n-1; i > -1; i--){ if(i == n-1) r[i] = arr[i]; else r[i] = Math.min(r[i+1]+1, arr[i]); } for(int i = 0; i < n; i++) ans.append(Math.min(l[i], r[i])).append("" ""); ans.append(""\n""); } f.close(); System.out.println(ans); } public static class point implements Comparable{ int x; int y; public point(int x,int y){ this.x = x; this.y = y; } public String toString(){ return(x + "" "" + y); } public boolean equals(Object x){ point y = ((point)(x)); if (this.x == y.x && this.y == y.y){ return true; } return false; } public int hashCode(){ return Objects.hash(x,y); } public int compareTo(point other){ if(this.x > other.x || (this.x == other.x && this.y > other.y)){ return 1; }else if(this.x == other.x && other.y == this.y){ return 0; } return -1; } } }","import java.util.*; import java.io.*; public class _1547_E { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(in.readLine()); while(t-- > 0) { in.readLine(); StringTokenizer line1 = new StringTokenizer(in.readLine()); int n = Integer.parseInt(line1.nextToken()); int k = Integer.parseInt(line1.nextToken()); int[] x = new int[n]; line1 = new StringTokenizer(in.readLine()); StringTokenizer line2 = new StringTokenizer(in.readLine()); TreeSet after = new TreeSet(); for(int i = 0; i < k; i++) { int ai = Integer.parseInt(line1.nextToken()) - 1; int ti = Integer.parseInt(line2.nextToken()); x[ai] = ti; after.add(new AC(ti, ai)); } TreeSet before = new TreeSet(); int[] res = new int[n]; for(int i = 0; i < n; i++) { if(x[i] > 0) { after.remove(new AC(x[i], i)); before.add(new AC(x[i], n - i)); } AC be = before.size() == 0 ? null : before.first(); AC af = after.size() == 0 ? null : after.first(); if(be == null) { res[i] = af.d + af.t - i; }else if(af == null) { res[i] = be.d + be.t - (n - i); }else { res[i] = Math.min(af.d + af.t - i, be.d + be.t - (n - i)); } } StringBuilder sb = new StringBuilder(); for(int i = 0; i < n; i++) { sb.append(res[i]); sb.append(' '); } out.println(sb.toString()); } in.close(); out.close(); } static class AC implements Comparable { int t, d; AC(int tt, int dd) { t = tt; d = dd; } @Override public int compareTo(AC o) { if(t + d == o.t + o.d) { return d - o.d; } return t + d - (o.t + o.d); } } } ",0 07038b12,a18cb2c1,"//Siddhartha Bose import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.time.LocalDate; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class h { public static int r1=0; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long[][] f=new long[501][501]; public static void main(String[] args) { OutputStream outputStream =System.out; PrintWriter out =new PrintWriter(outputStream); FastReader s=new FastReader(); int t=s.nextInt(); while(t>0) { int n=s.nextInt(); for(int i=1;i<=n;i++) { vis[i]=0; deg[i]=0; ans[i]=0; f1[i]=new ArrayList<>(); } for(int i=1;i<=n-1;i++) { int x=s.nextInt(); int y=s.nextInt(); f1[x].add(new pair1(y,i)); f1[y].add(new pair1(x,i)); deg[x]++; deg[y]++; } // dfs(1); int node=-1; boolean p=false; for(int i=1;i<=n;i++) { if(deg[i]>2) { p=true; } if(deg[i]==1) { node=i; } } if(p) { out.println(-1); }else { dfs(node,2); // System.out.println(); for(int i=1;i<=n-1;i++) { out.print(ans[i]+"" ""); } out.println(); } t--; } out.close(); } static int[] ans=new int[100001]; static int[] deg=new int[100001]; static void dfs(int node,int v) { vis[node]=1; // deg[node]++; for(int i=0;i{ private int a; private int b; private int d; private int c; pair3(int a,int b,int c,int d){ this.a=a; this.d=d; this.b=b; this.c=c; } @Override public int compareTo(pair3 c) { if(c.b-c.a!=this.b-this.a) { return Integer.compare(c.b-c.a, this.b-this.a); }else { return Integer.compare(this.c, c.c); } } } static class pair2 implements Comparable{ private int a; private int b; private int d; private int c; pair2(int a,int b,int c,int d){ this.a=a; this.d=d; this.b=b; this.c=c; } public int compareTo(pair2 c) { if(c.b!=this.b) { return Integer.compare(c.b, this.b); }else { if(c.c!=this.c) { return Integer.compare(this.c, c.c); }else { return Integer.compare(this.a, c.a); } } } } static boolean r(int k) { for(int i=2;i<=k/2;i++) { if(k%i==0) { return false; } } return true; } public static int[] is_prime=new int[100001]; public static ArrayList primes=new ArrayList<>(); public static void sieve() { long maxN=100; for(long i=1;i<=maxN;i++) { is_prime[(int) i]=1; } is_prime[0]=0; is_prime[1]=0; for(long i=2;i*i<=maxN;i++) { if(is_prime[(int) i]!=0) { primes.add( i); long c=1; for(long j=i*i;j<=maxN;j+=i) { is_prime[(int) j]++; } } } for(long i=2;i<=maxN;i++) { is_prime[(int) i]=is_prime[(int) (i-1)]+is_prime[(int) i]; } // int count=0; // for(long i=1;i<=maxN;i++) { // if(is_prime[(int) i]==1) { // count++; // } // if(is_prime[count]==1) { // arr[(int) i]=1; // }else { // arr[(int)i]=0; // } // } } public static long[] merge_sort(long[] A, int start, int end) { if (end > start) { int mid = (end + start) / 2; long[] v = merge_sort(A, start, mid); long[] o = merge_sort(A, mid + 1, end); return (merge(v, o)); } else { long[] y = new long[1]; y[0] = A[start]; return y; } } static ArrayList[] f1=new ArrayList[100001]; public static long[] merge(long a[], long b[]) { // int count=0; long[] temp = new long[a.length + b.length]; int m = a.length; int n = b.length; int i = 0; int j = 0; int c = 0; while (i < m && j < n) { if (a[i] < b[j]) { temp[c++] = a[i++]; } else { temp[c++] = b[j++]; } } while (i < m) { temp[c++] = a[i++]; } while (j < n) { temp[c++] = b[j++]; } return temp; } }","import java.util.*; import java.io.*; public class C1627 { 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(); int[][] edges = new int[n - 1][2]; int[] deg = new int[n]; boolean valid = true; ArrayList[] adjList = new ArrayList[n]; for (int i = 0; i < n; i++) { adjList[i] = new ArrayList<>(); } for (int i = 0; i < n - 1; i++) { int u = sc.nextInt() - 1; int v = sc.nextInt() - 1; edges[i] = new int[]{u, v}; deg[u]++; deg[v]++; valid &= deg[u] <= 2 && deg[v] <= 2; adjList[u].add(i); adjList[v].add(i); } if (!valid) { pw.println(-1); continue; } int root = -1; for (int i = 0; i < n; i++) { if (adjList[i].size() == 1) root = i; } int[] ans = new int[n - 1]; int curColor = 2; int par = -1; while (true) { int nxt = -1; for (int e : adjList[root]) { int other = edges[e][0] ^ edges[e][1] ^ root; if (other != par) { ans[e] = curColor; curColor = 5 - curColor; nxt = other; break; } } if (nxt == -1) break; par = root; root = nxt; } for (int x : ans) { pw.print(x + "" ""); } pw.println(); } 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 69c97258,db01da00,"import java.util.Scanner; import java.util.ArrayList; import java.lang.Math; public class fell { private static final Scanner cin = new Scanner(System.in); private static final int maxVal = 1000000000; public static void main(String[] args) { int n = cin.nextInt(); ArrayList a = new ArrayList<>(); ArrayList b = new ArrayList<>(); for (int i = 0; i < n; i++) { int t = cin.nextInt(); if (t == 0) a.add(i); else b.add(i); } if (a.size() == n) { System.out.println(0); return; } int[][] dp = new int[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (i == 0) dp[i][j] = 0; else dp[i][j] = maxVal; } for (int i = 0; i < a.size(); i++) { for (int j = 0; j < Math.min(i + 1, b.size() + 1); j++) { dp[i + 1][j] = Math.min(dp[i + 1][j], dp[i][j]); if (j == b.size()) continue; dp[i + 1][j + 1] = Math.min(dp[i + 1][j + 1], dp[i][j] + Math.abs(a.get(i) - b.get(j))); } } System.out.println(dp[a.size()][b.size()]); } }","import java.util.*; import java.lang.*; import java.io.*; public class Main { static int func(List ones, List zeroes, int idO, int idZ, int o, int z, Integer[][] memo) { if(idZ == zeroes.size() || idO == ones.size()) { return 0; } if(memo[idO][idZ] != null) { return memo[idO][idZ]; } if(o == z) { return memo[idO][idZ] = Math.abs(ones.get(idO) - zeroes.get(idZ)) + func(ones, zeroes, idO + 1, idZ + 1, o - 1, z - 1, memo); } else { return memo[idO][idZ] = Math.min( Math.abs(ones.get(idO) - zeroes.get(idZ)) + func(ones, zeroes, idO + 1, idZ + 1, o - 1, z - 1, memo), func(ones, zeroes, idO, idZ + 1, o, z - 1, memo) ); } } public static void main (String[] args) { // Scanner scan = new Scanner(System.in); FastScanner scan = new FastScanner(); int n = scan.nextInt(); int[] arr = scan.readArray(n); List ones = new ArrayList<>(); List zeroes = new ArrayList<>(); for(int i = 0; i < n; i++) { if(arr[i] == 0) { zeroes.add(i); } else { ones.add(i); } } int o = ones.size(); int z = zeroes.size(); Integer[][] memo = new Integer[o + 1][z + 1]; int ans = func(ones, zeroes, 0, 0, o, z, memo); System.out.println(ans); } /* int n = scan.nextInt(); int[] arr = scan.readArray(n); int n = scan.nextInt(); int m = scan.nextInt(); int[][] arr = new int[n][m]; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { arr[i][j] = scan.nextInt(); } } */ static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }",0 ac121776,eb6cfca7,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Solution { 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; } } static class HashMultiSet implements Iterable { private final HashMap map; private int size; public HashMultiSet(){map=new HashMap<>(); size=0;} public void clear(){map.clear(); size=0;} public int size(){return size;} public int setSize(){return map.size();} public boolean contains(T a){return map.containsKey(a);} public boolean isEmpty(){return size==0;} public Integer get(T a){return map.getOrDefault(a,0);} public void add(T a, int count) { int cur=get(a);map.put(a,cur+count); size+=count; if(cur+count==0) map.remove(a); } public void addOne(T a){add(a,1);} public void remove(T a, int count){add(a,Math.max(-get(a),-count));} public void removeOne(T a){remove(a,1);} public void removeAll(T a){remove(a,Integer.MAX_VALUE-10);} public Iterator iterator() { return new Iterator<>() { private final Iterator iter = map.keySet().iterator(); private int count = 0; private T curElement; public boolean hasNext(){return iter.hasNext()||count>0;} public T next() { if(count==0) { curElement=iter.next(); count=get(curElement); } count--; return curElement; } }; } } 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; } } } private static boolean try1(String s, int index,int prev){ int n = s.length(); for(int i = index;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 9756f13a,ea899386,"import java.util.*; import java.io.*; public class PhoenixAndTowers { public static void main(String [] args) throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(in.readLine()); for (int test = 0; test < t; test++){ String [] data = in.readLine().split("" ""); int n = Integer.parseInt(data[0]); int m = Integer.parseInt(data[1]); int x = Integer.parseInt(data[2]); data = in.readLine().split("" ""); intPair [] blocks = new intPair[n]; for (int i = 0; i < n; i++){ blocks[i] = new intPair(i, Integer.parseInt(data[i])); } Arrays.sort(blocks); TreeSet towers = new TreeSet<>(); // intPair [] towers = new intPair[m]; for (int i = 0; i < m; i++){ // towers[i] = new intPair(i+1, 0); towers.add(new intPair(i+1, 0)); } int [] newIdx = new int[n]; for (int i = n - 1; i >= 0; i--){ //Arrays.sort(towers); intPair low = towers.pollFirst(); //towers[0].value += blocks[i].value; low.value += blocks[i].value; newIdx[blocks[i].idx] = low.idx; towers.add(low); } int min = n*x; int max = 0; for (intPair tower : towers){ if (tower.value < min){ min = tower.value; } if (tower.value > max){ max = tower.value; } } if (max - min <= x){ out.write(""YES\n""); for (int i = 0; i < n; i++){ if (i != 0){ out.write("" "" + newIdx[i]); }else { out.write("""" + newIdx[i]); } } out.write(""\n""); } else { out.write(""NO\n""); } } out.close(); } static class intPair implements Comparable{ int idx; int value; intPair(int idx, int value){ this.idx = idx; this.value = value; } @Override public int compareTo(PhoenixAndTowers.intPair o) { return this.value != o.value? this.value - o.value: this.idx - o.idx; } } } ","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 list = new ArrayList<>(); for(int i:arr) list.add(i); Collections.sort(list); 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 03b3c5af,d74028ea,"import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; public class TaskA { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); int testCount = 1; testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class Solver { public void fastSort(int[] a){ Random random = new Random(); for(int i=0;i> adj; long[][] dp; public void dfs(int vertex,int parent){ // dp[0][vertex] = dp[1][vertex] = 0; for(int i=0;i(); for(int i=0;i()); } for(int i=0;i{ int f; int s; public Pair(int a,int b){ this.f = a; this.s = b; } public int compareTo(Pair other){ if(other.f==this.f){ return (int)(this.s-other.s); } else return this.f-other.f; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(InputStream stream){ br = new BufferedReader(new InputStreamReader(stream)); } public String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } public int nextInt(){ return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } public String nextLine(){ String str = """"; try{ str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } public int[] nextArray(int n) { int[] a=new int[n]; for (int i=0; i [] g; long [][] dp; long rec(int node, int prev, int index) { if(dp[node][index] != -1){ return dp[node][index]; } long ret = 0; int val = index==0?a[node]:b[node]; for(int adj: g[node]) { if(adj == prev) { continue; } ret += Math.max((long)(rec(adj, node, 0) + (long)(Math.abs(val - a[adj]))), (long)(rec(adj, node, 1) + (long)(Math.abs(val - b[adj])))); } return dp[node][index] = ret; } public void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while(t-->0) { int n = in.nextInt(); a = new int[n + 1]; b = new int[n + 1]; g = new ArrayList[n + 1]; dp = new long[n + 1][2]; for(int i=1;i<=n;++i) { a[i] = in.nextInt(); b[i] = in.nextInt(); g[i] = new ArrayList<>(); dp[i][0] = dp[i][1] = -1; } for(int i=1;i[] 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.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; public class C { private static final boolean TEST_MODE = true; private static Node[] tree; private static long[] lWt, rWt; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = getInt(br); for (int t=0; t q = new LinkedList<>(); q.add(1); // Constraint: Every node that is in the queue has parent already assigned. while (!q.isEmpty()) { int currId = q.poll(); Node currNode = tree[currId]; // Add all neighbours as children. for (int nbrId : currNode.nbrs) { if (nbrId == currNode.parent) { continue; } tree[nbrId].parent = currId; currNode.addChild(nbrId); q.add(nbrId); } // currNode.nbrs.clear(); } } private static void getMaxBeauty() throws Exception { Stack stack = new Stack<>(); stack.add(1); while (!stack.isEmpty()) { int currId = stack.pop(); Node curr = tree[currId]; // Is leaf ? if (curr.children.size() == 0) { lWt[currId] = 0; rWt[currId] = 0; continue; } // Are all children processed ? boolean allChildrenProcessed = (lWt[curr.children.get(0)] >= 0); if (allChildrenProcessed) { lWt[currId] = 0; rWt[currId] = 0; // Compute lwt and rwt maximized against each child. for (int childId : curr.children) { Node childNode = tree[childId]; long ll = Math.abs(curr.left - childNode.left) + lWt[childId]; long lr = Math.abs(curr.left - childNode.right) + rWt[childId]; lWt[currId] += Math.max(ll, lr); long rl = Math.abs(curr.right - childNode.left) + lWt[childId]; long rr = Math.abs(curr.right - childNode.right) + rWt[childId]; rWt[currId] += Math.max(rl, rr); } } else { // Add all unvisited children. stack.add(currId); stack.addAll(curr.children); } } long res = Math.max(lWt[1], rWt[1]); System.out.println(res); } private static int getLeftId(int nodeId) { return tree[nodeId].left; } private static int getRightId(int nodeId) { return tree[nodeId].right; } private static Integer getInt(BufferedReader br) throws Exception { return Integer.parseInt(br.readLine()); } private static Long getLong(BufferedReader br) throws Exception { return Long.parseLong(br.readLine()); } private static int[] getIntArray(int N, BufferedReader br) throws Exception { String s = br.readLine(); String[] tokens = s.split("" ""); int[] result = new int[N]; for (int i=0; i nbrs = new ArrayList<>(); List children = new ArrayList<>(); int left, right; public Node(int l, int r) { this.left = l; this.right = r; } public Node(String s) { String[] tokens = s.split("" ""); this.left = Integer.parseInt(tokens[0]); this.right = Integer.parseInt(tokens[1]); } public void addNbr(int nbr) { nbrs.add(nbr); } public void addChild(int child) { children.add(child); } }",0 2120328e,6de04ee2,"/***** ---> :) 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 int mod=10000007; static StringBuilder sb=new StringBuilder(); /* start */ public static void main(String [] args) { int t = i(); while(t-->0) { int n = i(); int a[] = input(n); char c[] = inputC(); ArrayList b = new ArrayList<>(); ArrayList r = new ArrayList<>(); for(int i=0;in-i) { is = false; break; } } out.println(is==true?""YES"":""NO""); } out.close(); } /* end */ 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 i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; 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; } //pair class private static class Pair implements Comparable { int first, second; public Pair(int f, int 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; } } } }","import java.util.*; public class Solution{ 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 l=new Vector<>(), r=new Vector<>(); for(int i=0;i n - i) ok = false; System.out.print((ok ? ""YES"" : ""NO"")+'\n'); } } }",1 529d9642,8ad1ad84,"import java.util.*; import java.io.*; import java.math.*; /** * * @Har_Har_Mahadev */ /** * Main , Solution , Remove Public */ public class C { private static ArrayList[] adj; private static ArrayList lis; private static Pair[] arr; public static void process() throws IOException { int n = sc.nextInt(); lis = new ArrayList(); for(int i = 0; i(); for(int i = 1; i val2) { arr[node].x+=val1; } else { arr[node].x+=val2; } // second val1 = abs(ss-cff) + arr[child].x; val2 = abs(ss-css) + arr[child].y; if(val1 > val2) { arr[node].y+=val1; } else { arr[node].y+=val2; } } } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353; private static int N = 0; 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 { long x, y; Pair(long x, long y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Long.compare(this.x, o.x); } // @Override // public boolean equals(Object o) { // if (this == o) return true; // if (!(o instanceof Pair)) return false; // Pair key = (Pair) o; // return x == key.x && y == key.y; // } // // @Override // public int hashCode() { // int result = x; // result = 31 * result + y; // return result; // } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static void println(Object o) { out.println(o); } static void println() { out.println(); } static void print(Object o) { out.print(o); } static void pflush(Object o) { out.println(o); out.flush(); } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static long sqrt(long z) { long sqz = (long) Math.sqrt(z); while (sqz * 1L * sqz < z) { sqz++; } while (sqz * 1L * sqz > z) { sqz--; } return sqz; } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static int lower_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System.in)); } FastScanner(int a) throws FileNotFoundException { br = new BufferedReader(new FileReader(""input.txt"")); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) throws IOException { int[] A = new int[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextInt(); } return A; } long[] readArrayLong(int n) throws IOException { long[] A = new long[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextLong(); } return A; } } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } } ","import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; //import javafx.util.*; public final class B { static 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); static int D1[],D2[],par[]; static boolean set[]; static int value[]; static long INF=Long.MAX_VALUE; static int N,M; static long L[],R[],dp[][]; static int s=1; public static void main(String args[])throws IOException { int T=i(); outer:while(T-->0) { int N=i(); setGraph(N); for(int i=1; i<=N; i++) { L[i]=l(); R[i]=l(); } for(int i=1; iA[i+1])return false; } return true; } static int f1(int x,ArrayList A) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; int a=A.get(m); if(aend)return 0; int a=0,b=0,c=0; if(d>1)a=f(st+d-1,end,d-1); b=f(st+d,end,d); c=f(st+d+1,end,d+1); return value[st]+max(a,b,c); } static int max(int a,int b,int c) { return Math.max(a, Math.max(c, b)); } static int value(char X[],char Y[]) { int c=0; for(int i=0; i<7; i++) { if(Y[i]=='1' && X[i]=='0')return -1; if(X[i]=='1' && Y[i]=='0')c++; } return c; } static boolean isValid(int i,int j) { if(i<0 || i>=N)return false; if(j<0 || j>=M)return false; return true; } static long fact(long N) { long num=1L; while(N>=1) { num=((num%mod)*(N%mod))%mod; N--; } return num; } static boolean reverse(long A[],int l,int r) { while(l l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i(); } } static long pow(long a,long b) { long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1< l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i A) { for(int a:A)System.out.print(a+"" ""); System.out.println(); } //Debugging Functions END //---------------------- //IO FUNCTIONS STARTS static HashMap Hash(int A[]) { HashMap mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i { int x,index; boolean right; node(int x,boolean right,int index) { this.x=x; this.right=right; this.index=index; } public int compareTo(node X) { return this.x-X.x; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } //gey double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }",0 3d06b643,930dd1f1,"import java.io.*; import java.util.*; public class ArmChairs { public static int solution(int n, int[] arr) { ArrayList one = new ArrayList(); ArrayList zero = new ArrayList(); for (int i = 0; i < n; i++) { if (arr[i] == 1) { one.add(i); } else { zero.add(i); } } int[][] dp = new int[one.size() + 1][zero.size() + 1]; for (int i = 1; i <= one.size(); i++) { dp[i][i] = dp[i - 1][i - 1] + Math.abs(one.get(i - 1) - zero.get(i - 1)); for (int j = i + 1; j <= zero.size(); j++) { dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j - 1] + Math.abs(one.get(i - 1) - zero.get(j - 1))); } } return dp[one.size()][zero.size()]; } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split("" ""); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(s[i]); } log.write(Integer.toString(solution(n, arr)) + ""\n""); log.flush(); } } ","import java.util.*; import java.io.*; public class Armchairs { public static int val(int b[],int m,int c[],int n,int dp[][]) { if(dp[m][n]>-1) { return dp[m][n]; } else if(m==0) { return 0; } else if(n==0) { return 2099999999; } else { return dp[m][n]=Math.min(val(b,m-1,c,n-1,dp)+Math.abs(b[m-1]-c[n-1]),val(b,m,c,n-1,dp)); } } public static void process()throws IOException { int n=I(); int a[]=Ai(n); ArrayList arr=new ArrayList(); ArrayList arr1=new ArrayList(); for(int i=0;i0) {process();}out.flush();out.close();}catch(Exception e){return;}}} //*-----------------------------------------------------------------------------------------------------------------------------------*// ",0 4138b081,f729338f,"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); } } }","import java.io.*; import java.util.*; public class Solution{ public static class Edge { int index; int u, v; Edge(int u, int v, int index) { this.index=index; this.u = u; this.v = v; } } public static void main (String[] args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); int t=Integer.parseInt(br.readLine()); while(t-->0) { int n=Integer.parseInt(br.readLine()); ArrayList graph[] = new ArrayList[n]; for(int i=0; i(); for(int i=0; i2) { poss = false; } else if(graph[i].size() == 1) start_i = i; } if(!poss) { sb.append(""-1\n""); continue; } int wts[] = new int[n-1]; ArrayList edges = new ArrayList<>(); Edge first = graph[start_i].get(0); wts[first.index] = 2; edges = graph[first.v]; while(edges.size()>1) { Edge firste = edges.get(0); Edge sece = edges.get(1); if(wts[firste.index] != 0 ) //second not visited { wts[sece.index] = 5 - wts[firste.index]; edges = graph[sece.v]; } else //first not visited { wts[firste.index] = 5 - wts[sece.index]; edges = graph[firste.v]; } } for(int i=0; i{ 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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class D { static class Pair{ int val; char c; Pair(int a,char b){ this.val=a; this.c=b; } } public static void main(String[] args) { FastScanner sc=new FastScanner(); int t=sc.nextInt(); PrintWriter pw=new PrintWriter(System.out); while(t-->0) { int n=sc.nextInt(); int[] a=sc.readArray(n); char[] s=sc.next().toCharArray(); boolean ok=true; ArrayList blues=new ArrayList<>(); ArrayList reds=new ArrayList<>(); for(int i=0;i(start++)){ ok=false; break; } } if(ok){ pw.println(""YES""); } else { pw.println(""NO""); } } pw.flush(); } 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){ 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(); } } ","/* 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(); } } ",1 83b44c9c,8d9871a9,"import java.util.Scanner; public class MinimumGridPath { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int test = scanner.nextInt(); StringBuilder sb = new StringBuilder(); for (int t = 0; t < test; t++) { int n = scanner.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = scanner.nextLong(); } long minx = arr[0]; long miny = arr[1]; long min = minx * n + miny * n; long sumx = arr[0]; long sumy = arr[1]; for(int i=2; i 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;i[] 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 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.util.*; import java.io.*; public class C_1551 { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int[] a = new int[n]; int[] b = new int[n]; int[] c = new int[n]; int[] d = new int[n]; int[] e = new int[n]; for(int i = 0; i < n; i++) { String s = sc.next(); int[] cnt = new int[5]; for(int j = 0; j < s.length(); j++) cnt[s.charAt(j) - 'a']++; a[i] = 2 * cnt[0] - s.length(); b[i] = 2 * cnt[1] - s.length(); c[i] = 2 * cnt[2] - s.length(); d[i] = 2 * cnt[3] - s.length(); e[i] = 2 * cnt[4] - s.length(); } Arrays.sort(a); Arrays.sort(b); Arrays.sort(c); Arrays.sort(d); Arrays.sort(e); int max = 0; int sum = 0; for(int i = n - 1; i >= 0; i--) { sum += a[i]; if(sum <= 0) break; max = n - i; } sum = 0; for(int i = n - 1; i >= 0; i--) { sum += b[i]; if(sum <= 0) break; max = Math.max(max, n - i); } sum = 0; for(int i = n - 1; i >= 0; i--) { sum += c[i]; if(sum <= 0) break; max = Math.max(max, n - i); } sum = 0; for(int i = n - 1; i >= 0; i--) { sum += d[i]; if(sum <= 0) break; max = Math.max(max, n - i); } sum = 0; for(int i = n - 1; i >= 0; i--) { sum += e[i]; if(sum <= 0) break; max = Math.max(max, n - i); } pw.println(max); } 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); } } } ",0 d2a4c1fb,dcdaf666,"import java.util.*; public class CodeForcesRound { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i 0) { int n = fs.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = fs.nextLong(); } long[] ps = new long[n]; ps[0] = arr[0]; for (int i = 1; i < n; i++) { ps[i] = ps[i - 1] + arr[i]; } long min = arr[0] * n + arr[1] * n; int even = 0; int odd = 1; for (int i = 2; i < n; i++) { if (i % 2 == 0) { if (arr[i] < arr[even]) { even = i; } } else { if (arr[i] < arr[odd]) { odd = i; } } int tmp = i + 1; int oddCount = tmp / 2; int evenCount = (tmp + 1) / 2; long local = ps[i]; local += (n - oddCount) * arr[odd]; local += (n - evenCount) * arr[even]; min = Math.min(min, local); } out.println(min); } } // if these strings are both balanced then as one or zero goes out of the window one or zero must come in // 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(); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } } ",0 25597bcb,5fcbdd19,"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.LinkedList; import java.util.List; import java.util.Map; import java.util.StringTokenizer; import java.util.TreeSet; public class PhoenixAndTowers { // Template for CF public static class ListComparator implements Comparator> { @Override public int compare(List l1, List l2) { for (int i = 0; i < l1.size(); ++i) { if (l1.get(i).compareTo(l2.get(i)) != 0) { return l1.get(i).compareTo(l2.get(i)); } } return 0; } } public static class Pair { int first; int second; public Pair(int first, int second) { this.first = first; this.second = second; } public int getFirst() { return first; } public int getSecond() { return second; } public void setFirst(int a) { first = a; } @Override public String toString() { return first + "" "" + second; } } public static void main(String[] args) throws IOException { // Check for int overflow!!!! // Should you use a long to store the sum or smthn? BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int T = Integer.parseInt(f.readLine()); for (int i = 0; i < T; i++) { StringTokenizer st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); st = new StringTokenizer(f.readLine()); List list = new ArrayList<>(); for (int j = 0; j < n; j++) { int a = Integer.parseInt(st.nextToken()); list.add(a); } ArrayList copy = new ArrayList<>(list); Collections.sort(list); TreeSet> set = new TreeSet<>(new ListComparator()); for (int j = 1; j <= m; j++) { List temp = new ArrayList<>(); temp.add(0); temp.add(j); set.add(temp); // System.out.println(temp); } // System.out.println(set); Map> map = new HashMap<>(); for (int j = n - 1; j >= 0; j--) { if (map.containsKey(list.get(j))) { map.get(list.get(j)).addLast(set.first().get(1)); } else { map.put(list.get(j), new LinkedList<>()); map.get(list.get(j)).addLast(set.first().get(1)); } List temp = new ArrayList<>(); temp.add(set.first().get(0) + list.get(j)); temp.add(set.pollFirst().get(1)); set.add(temp); } // System.out.println(set); if (set.last().get(0) - set.first().get(0) > x) { out.println(""NO""); } else { out.println(""YES""); for (int j = 0; j < n; j++) { out.print(map.get(copy.get(j)).pollFirst() + "" ""); } out.println(); } } out.close(); } }","import java.io.*; import java.util.*; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = in.nextInt(); for (int t1 = 0; t1 < t; t1++) { int n = in.nextInt(), m = in.nextInt(), x = in.nextInt(); ArrayList list = new ArrayList<>(); for (int i = 1; i <= n; i++) list.add(new Pair(in.nextInt(), i)); pw.println(solve(list, n, m, x)); } pw.close(); } static StringBuilder solve(ArrayList arr, int n, int m, int x) { Stack stack = new Stack<>(); HashMap map = new HashMap<>(); PriorityQueue pq = new PriorityQueue<>(); Collections.sort(arr); for (int i = m; i < n; i++) stack.push(arr.get(i)); for (int i = 0, j = 1; i < m; i++, j++) { Pair p = arr.get(i); pq.add(new Pair(p.f, j)); map.put(p.s, j); } while (!stack.isEmpty()) { Pair val = stack.pop(); Pair p = pq.remove(); map.put(val.s, p.s); pq.add(new Pair(p.f + val.f, p.s)); } // debug(pq); int min = pq.remove().f; while (!pq.isEmpty()) { int val = pq.remove().f; if ((val - min) > x) return new StringBuilder().append(""NO""); } StringBuilder sb = new StringBuilder(); sb.append(""YES\n""); for (int i = 1; i <= n; i++) { sb.append(map.get(i) + "" ""); } return sb; } static class Pair implements Comparable { int f, s; Pair(int f, int s) { this.f = f; this.s = s; } @Override public int compareTo(Pair p) { return Integer.compare(this.f, p.f); } @Override public String toString() { return f + "" "" + s; } } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }",0 643e22cc,bd7281dc,"import java.io.*; import java.util.*; public class C { public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws IOException { readInput(); out.close(); } static class Tower implements Comparable{ int i, w; public Tower(int a, int b) { i=a; w=b; } public int compareTo(Tower o) {return w - o.w;} } static class Block implements Comparable{ int i, w; public Block(int a, int b) { i=a; w=b; } public int compareTo(Block o) {return o.w-w;} } static PriorityQueue towers; static PriorityQueue blocks; static int[] par; static void solve() { while (!blocks.isEmpty()) { Block h = blocks.poll(); Tower t = towers.poll(); par[h.i] = t.i; t.w += h.w; towers.add(t); } List res = new ArrayList(towers); Collections.sort(res); boolean works = true; for (int i =1 ; i < m; i++) { if (Math.abs(res.get(i-1).w - res.get(i).w) > x) works = false; } if (works) { out.println(""YES""); for (int i = 0; i < n; i++) out.print(par[i] + 1 + "" ""); out.println(); } else out.println(""NO""); } static int n,m,x; public static void readInput() throws IOException { // br = new BufferedReader(new FileReader("".in"")); // out = new PrintWriter(new FileWriter("".out"")); int t = Integer.parseInt(br.readLine()); while (t-->0) { StringTokenizer st= new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); x = Integer.parseInt(st.nextToken()); towers = new PriorityQueue(); blocks = new PriorityQueue(); par = new int[n]; st = new StringTokenizer(br.readLine()); for (int i= 0 ; i < n; i++) { blocks.add(new Block(i,Integer.parseInt(st.nextToken()))); } for (int i =0 ; i < m; i++) { towers.add(new Tower(i, 0)); } solve(); } } } ","/* bts songs to dance to: I need U Run ON Filter I'm fine */ 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 x1515C { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int M = Integer.parseInt(st.nextToken()); int X = Integer.parseInt(st.nextToken()); int[] arr = readArr(N, infile, st); ArrayList ls = new ArrayList(); for(int i=0; i < N; i++) ls.add(new Unit(arr[i], i)); Collections.sort(ls); int[] res = new int[N]; PriorityQueue pq = new PriorityQueue(Comparator.reverseOrder()); for(int i=1; i <= M; i++) pq.add(new Unit(0, i)); for(Unit add: ls) { Unit tower = pq.poll(); tower.height += add.height; res[add.id] = tower.id; pq.add(tower); } int min = pq.poll().height; int max = min; while(pq.size() > 0) max = max(max, pq.poll().height); if(max-min > X) sb.append(""NO\n""); else { sb.append(""YES\n""); for(int x: res) sb.append(x+"" ""); sb.append(""\n""); } } System.out.print(sb); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } } class Unit implements Comparable { public int id; public int height; public Unit(int h, int i) { height = h; id = i; } public int compareTo(Unit oth) { return oth.height-height; } }",0 63bfa731,7974ffba," import java.util.*; import java.io.*; public class D { static ArrayList adj[] = new ArrayList[(int)1e5+7]; static int diameter = 0; static int[] depth = new int[(int)1e5 + 7]; public static void main(String[] args) { FastReader in = new FastReader(); int t = in.nextInt(); while(t-- > 0){ int n = in.nextInt(); int a = in.nextInt(), b = in.nextInt(), da = in.nextInt(), db = in.nextInt(); for(int i = 1; i <= n; i++){ adj[i] = new ArrayList<>(); } for(int i = 1; i <=n; i++){ adj[i].clear(); } for(int i = 0; i < n- 1; i++){ int u = in.nextInt(); int v = in.nextInt(); adj[u].add(v); adj[v].add(u); } diameter = 0; depth[a] = 0; dfs(a, -1); System.out.println(2 * da >= Math.min(diameter, db) || depth[b] <= da ? ""Alice"" : ""Bob""); } } static int dfs(int node, int parent){ int len = 0; for(int x : adj[node]){ if(x != parent){ depth[x] = depth[node] + 1; int cur = 1 + dfs(x, node); diameter = Math.max(diameter, cur + len); len = Math.max(len, cur); // System.out.print(""x "" + x + "" node "" + node + "" par "" + parent); // System.out.println("" cur "" + cur + "" len "" + len + "" diam "" + diameter); } } return len; } static long getDigitSum(long n) { long sum = 0; while (n > 0) { sum = sum + n % 10; n = n/10; } return sum; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n){ int[] a = new int[n]; for(int i = 0; i < n; i++){ a[i] = nextInt(); } return a; } } } ","import java.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; } } } ",1 04df7bb8,85125ecb," 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.Arrays; import java.util.Hashtable; public class AirConditionersUpsolve{ static class InputReader { private static final int DEFAULT_BUFFER_SIZE = 1 << 16; // Change this to increase your input size // private static final InputStream DEFAULT_STREAM = System.in;private static final int MAX_DECIMAL_PRECISION = 21;private int c;private byte[] buf;private int bufferSize, bufIndex, numBytesRead;private InputStream stream;private static final byte EOF = -1;private static final byte NEW_LINE = 10;private static final byte CARRIAGE_RETURN = 13;private static final byte SPACE = 32;private static final byte DASH = 45;private static final byte DOT = 46;private char[] charBuffer;private static byte[] bytes = new byte[58];private static int[] ints = new int[58];private static char[] chars = new char[128];static { char ch = ' ';int value = 0;byte _byte = 0;for (int i = 48; i < 58; i++) bytes[i] = _byte++;for (int i = 48; i < 58; i++) ints[i] = value++;for (int i = 32; i < 128; i++) chars[i] = ch++; }private static final double[][] doubles = {{0.0d, 0.00d, 0.000d, 0.0000d, 0.00000d, 0.000000d, 0.0000000d, 0.00000000d, 0.000000000d, 0.0000000000d, 0.00000000000d, 0.000000000000d, 0.0000000000000d, 0.00000000000000d, 0.000000000000000d, 0.0000000000000000d, 0.00000000000000000d, 0.000000000000000000d, 0.0000000000000000000d, 0.00000000000000000000d, 0.000000000000000000000d}, {0.1d, 0.01d, 0.001d, 0.0001d, 0.00001d, 0.000001d, 0.0000001d, 0.00000001d, 0.000000001d, 0.0000000001d, 0.00000000001d, 0.000000000001d, 0.0000000000001d, 0.00000000000001d, 0.000000000000001d, 0.0000000000000001d, 0.00000000000000001d, 0.000000000000000001d, 0.0000000000000000001d, 0.00000000000000000001d, 0.000000000000000000001d}, {0.2d, 0.02d, 0.002d, 0.0002d, 0.00002d, 0.000002d, 0.0000002d, 0.00000002d, 0.000000002d, 0.0000000002d, 0.00000000002d, 0.000000000002d, 0.0000000000002d, 0.00000000000002d, 0.000000000000002d, 0.0000000000000002d, 0.00000000000000002d, 0.000000000000000002d, 0.0000000000000000002d, 0.00000000000000000002d, 0.000000000000000000002d}, {0.3d, 0.03d, 0.003d, 0.0003d, 0.00003d, 0.000003d, 0.0000003d, 0.00000003d, 0.000000003d, 0.0000000003d, 0.00000000003d, 0.000000000003d, 0.0000000000003d, 0.00000000000003d, 0.000000000000003d, 0.0000000000000003d, 0.00000000000000003d, 0.000000000000000003d, 0.0000000000000000003d, 0.00000000000000000003d, 0.000000000000000000003d}, {0.4d, 0.04d, 0.004d, 0.0004d, 0.00004d, 0.000004d, 0.0000004d, 0.00000004d, 0.000000004d, 0.0000000004d, 0.00000000004d, 0.000000000004d, 0.0000000000004d, 0.00000000000004d, 0.000000000000004d, 0.0000000000000004d, 0.00000000000000004d, 0.000000000000000004d, 0.0000000000000000004d, 0.00000000000000000004d, 0.000000000000000000004d}, {0.5d, 0.05d, 0.005d, 0.0005d, 0.00005d, 0.000005d, 0.0000005d, 0.00000005d, 0.000000005d, 0.0000000005d, 0.00000000005d, 0.000000000005d, 0.0000000000005d, 0.00000000000005d, 0.000000000000005d, 0.0000000000000005d, 0.00000000000000005d, 0.000000000000000005d, 0.0000000000000000005d, 0.00000000000000000005d, 0.000000000000000000005d}, {0.6d, 0.06d, 0.006d, 0.0006d, 0.00006d, 0.000006d, 0.0000006d, 0.00000006d, 0.000000006d, 0.0000000006d, 0.00000000006d, 0.000000000006d, 0.0000000000006d, 0.00000000000006d, 0.000000000000006d, 0.0000000000000006d, 0.00000000000000006d, 0.000000000000000006d, 0.0000000000000000006d, 0.00000000000000000006d, 0.000000000000000000006d}, {0.7d, 0.07d, 0.007d, 0.0007d, 0.00007d, 0.000007d, 0.0000007d, 0.00000007d, 0.000000007d, 0.0000000007d, 0.00000000007d, 0.000000000007d, 0.0000000000007d, 0.00000000000007d, 0.000000000000007d, 0.0000000000000007d, 0.00000000000000007d, 0.000000000000000007d, 0.0000000000000000007d, 0.00000000000000000007d, 0.000000000000000000007d}, {0.8d, 0.08d, 0.008d, 0.0008d, 0.00008d, 0.000008d, 0.0000008d, 0.00000008d, 0.000000008d, 0.0000000008d, 0.00000000008d, 0.000000000008d, 0.0000000000008d, 0.00000000000008d, 0.000000000000008d, 0.0000000000000008d, 0.00000000000000008d, 0.000000000000000008d, 0.0000000000000000008d, 0.00000000000000000008d, 0.000000000000000000008d}, {0.9d, 0.09d, 0.009d, 0.0009d, 0.00009d, 0.000009d, 0.0000009d, 0.00000009d, 0.000000009d, 0.0000000009d, 0.00000000009d, 0.000000000009d, 0.0000000000009d, 0.00000000000009d, 0.000000000000009d, 0.0000000000000009d, 0.00000000000000009d, 0.000000000000000009d, 0.0000000000000000009d, 0.00000000000000000009d, 0.000000000000000000009d}}; public InputReader() { this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE); } public InputReader(int bufferSize) { this(DEFAULT_STREAM, bufferSize); } public InputReader(InputStream stream) { this(stream, DEFAULT_BUFFER_SIZE); } public InputReader(InputStream stream, int bufferSize) { if (stream == null || bufferSize <= 0) throw new IllegalArgumentException();buf = new byte[bufferSize];charBuffer = new char[128];this.bufferSize = bufferSize;this.stream = stream; } private byte read() throws IOException { if (numBytesRead == EOF) throw new IOException();if (bufIndex >= numBytesRead) { bufIndex = 0;numBytesRead = stream.read(buf);if (numBytesRead == EOF) return EOF; }return buf[bufIndex++]; } private int readJunk(int token) throws IOException { if (numBytesRead == EOF) return EOF; do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > token) return 0;bufIndex++; } numBytesRead = stream.read(buf);if (numBytesRead == EOF) return EOF;bufIndex = 0; } while (true); } public byte nextByte() throws IOException {return (byte) nextInt(); } public int nextInt() throws IOException { if (readJunk(DASH - 1) == EOF) throw new IOException();int sgn = 1, res = 0;c = buf[bufIndex];if (c == DASH) { sgn = -1;bufIndex++; } do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1);res += ints[buf[bufIndex++]]; } else { bufIndex++;return res * sgn; } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return res * sgn;bufIndex = 0; } while (true); } public long nextLong() throws IOException { if (readJunk(DASH - 1) == EOF) throw new IOException();int sgn = 1;long res = 0L;c = buf[bufIndex];if (c == DASH) { sgn = -1;bufIndex++; } do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1);res += ints[buf[bufIndex++]]; } else { bufIndex++;return res * sgn; } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return res * sgn;bufIndex = 0; } while (true); } private void doubleCharBufferSize() { char[] newBuffer = new char[charBuffer.length << 1];for (int i = 0; i < charBuffer.length; i++) newBuffer[i] = charBuffer[i];charBuffer = newBuffer; } public String nextLine() throws IOException { try { c = read(); } catch (IOException e) { return null; }if (c == NEW_LINE) return """";if (c == EOF) return null;int i = 0;charBuffer[i++] = (char) c;do { while (bufIndex < numBytesRead) { if (buf[bufIndex] != NEW_LINE && buf[bufIndex] != CARRIAGE_RETURN) { if (i == charBuffer.length) doubleCharBufferSize();charBuffer[i++] = (char) buf[bufIndex++]; } else { if(buf[bufIndex] == CARRIAGE_RETURN) bufIndex++;bufIndex++;return new String(charBuffer, 0, i); } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return new String(charBuffer, 0, i);bufIndex = 0; } while (true); } public String nextString() throws IOException { if (numBytesRead == EOF) return null;if (readJunk(SPACE) == EOF) return null;for (int i = 0; ; ) { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { if (i == charBuffer.length) doubleCharBufferSize();charBuffer[i++] = (char) buf[bufIndex++]; } else {bufIndex++;return new String(charBuffer, 0, i); } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return new String(charBuffer, 0, i);bufIndex = 0; } } public double nextDoubleFast() throws IOException {c = read();int sgn = 1;while (c <= SPACE) c = read();if (c == DASH) { sgn = -1;c = read(); }double res = 0.0;while (c > DOT) { res *= 10.0;res += ints[c];c = read(); }if (c == DOT) { int i = 0;c = read();while (c > SPACE && i < MAX_DECIMAL_PRECISION) { res += doubles[ints[c]][i++];c = read(); } }return res * sgn; } public void close() throws IOException {stream.close(); } } // region variables static InputReader sc = new InputReader(); static OutputStream outputStream = System.out; static PrintWriter w = new PrintWriter(outputStream); // endregion private static void initiateIO() throws IOException {if (System.getProperty(""ONLINE_JUDGE"") == null) { try { w = new PrintWriter(""output.txt"");sc = new InputReader(new FileInputStream(""input.txt"")); } catch (Exception e) { throw new IOException(); }} } public static void main(String[] args) throws IOException { initiateIO(); int t = sc.nextInt(); while(t-- > 0) { solve(); } w.close(); } static void solve() throws IOException { int n = sc.nextInt(); int k = sc.nextInt(); int[] ac = new int[n+1]; Arrays.fill(ac, Integer.MAX_VALUE); int[] pos = new int[k+1]; for(int i = 1; i <= k; i++) { pos[i] = sc.nextInt(); } for(int i = 1; i <= k; i++) { int posi = pos[i]; int temp = sc.nextInt(); ac[posi] = temp; } long[] prev = new long[n+1]; long temp = Integer.MAX_VALUE; for(int i = 1; i <= n; i++) { temp = Math.min(temp+1, ac[i]); prev[i] = temp; } long[] next = new long[n+1]; temp = Integer.MAX_VALUE; for(int i = n; i > 0; i--) { temp = Math.min(temp+1, ac[i]); next[i] = temp; } for(int i = 1; i <= n; i++) { w.print(Math.min(prev[i], next[i])+"" ""); } w.println(); } }",1 646b3abc,8261bd1c,"import java.util.*; import java.io.*; public class Main { static FastScanner sc = new FastScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = (long) 1e9 + 7; public static void main(String[] args) throws Exception { int n = sc.nextInt(); for(int i = 0; i < n; i++) solve(); pw.flush(); } public static void solve() { int n = sc.nextInt(); char[] a = sc.next().toCharArray(); char[] b = sc.next().toCharArray(); boolean isFirst = false; boolean allOk = true; int a1 = 0; int a0 = 0; int b1 = 0; int b0 = 0; int[][] cnt1 = new int[2][2]; for(int i = 0; i < n; i++){ if(a[i] != b[i]){ allOk = false; } if(a[i] == '1'){ a1++; isFirst = true; }else{ a0++; } if(b[i] == '1'){ b1++; }else{ b0++; } cnt1[a[i]-'0'][b[i]-'0']++; } if(allOk){ pw.println(0); return; }else if(!isFirst){ pw.println(-1); return; }else if(a1 != b1 && (a0+1) != b1){ pw.println(-1); return; }else{ int same = cnt1[1][1] - cnt1[0][0] == 1 ? cnt1[1][1] + cnt1[0][0] : Integer.MAX_VALUE; int odd = cnt1[1][0] - cnt1[0][1] == 0 ? cnt1[1][0] + cnt1[0][1] : Integer.MAX_VALUE; int min = Math.min(same,odd); pw.println(min == Integer.MAX_VALUE ? -1 : min); } } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public FastScanner(FileReader in) { reader = new BufferedReader(in); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(""\n""); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String[] nextArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } ","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 Cgr18A { 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 { final int INF = (int) 1e9; public void solve(InputReader sc, PrintWriter out) { int n = sc.nextInt(); char[] a = sc.next().toCharArray(); char[] b = sc.next().toCharArray(); boolean isAllZeroA = isAllZero(a); boolean isAllZeroB = isAllZero(b); if(isAllZeroA && isAllZeroB) { out.println(0); return; } if(isAllZeroA || isAllZeroB) { out.println(-1); return; } int eq = 0; for(int i = 0; i< n;i++) if(a[i]==b[i]) eq++; int eqHasOne = 0; int notEqHasOne = 0; int noteq = n-eq; for (int i = 0; i < n; i++) { if(a[i]==b[i] && a[i]=='1') eqHasOne++; if(a[i]!=b[i] && a[i]=='1') notEqHasOne++; } int l = solveIt(new int[] {eq,noteq},new int[] {eqHasOne,notEqHasOne},0); int r = solveIt(new int[] {eq,noteq},new int[] {eqHasOne,notEqHasOne},1); if(l==INF && r==INF) { out.println(-1); } else { out.println(Math.min(l,r)); } } private boolean isAllZero(char[] a) { for(char c : a) if(c=='1') return false; return true; } private int solveIt(int[] arr,int[] hasOne, int minusPointer) { if(arr[0]==0 && arr[1]==0) throw new RuntimeException(); int cnt = 0; int max = arr[0] + arr[1]; while(arr[1]!=0) { int minus = minusPointer; int plus = 1-minus; if(hasOne[minusPointer]<1) return INF; int eqHasOne = hasOne[0]; int notEqHasOne = hasOne[1]; int eqHasZero = arr[0] - eqHasOne; int notEqHasZero = arr[1] - notEqHasOne; if(minusPointer==0) { int newNotEqHasOne = eqHasZero; int newEqHasOne = notEqHasZero + 1; hasOne[0] = newEqHasOne; hasOne[1] = newNotEqHasOne; } else { int newEqHasOne = notEqHasZero; int newNotEqHasOne = eqHasZero + 1; hasOne[0] = newEqHasOne; hasOne[1] = newNotEqHasOne; } int minusValue = arr[minus]-1; int plusValue = arr[plus]+1; arr[minus] = plusValue; arr[plus] = minusValue; minusPointer = 1-minusPointer; cnt++; if(cnt>max+10) break; } if(arr[1]!=0) return INF; return cnt; } } static void sort(int[] arr) { ArrayList al = new ArrayList(); for (int i : arr) al.add(i); Collections.sort(al); int idx = 0; for (int i : al) arr[idx++] = i; } static void sort(long[] arr) { ArrayList al = new ArrayList(); for (long i : arr) al.add(i); Collections.sort(al); int idx = 0; for (long i : al) arr[idx++] = i; } static void sortDec(int[] arr) { ArrayList al = new ArrayList(); for (int i : arr) al.add(i); Collections.sort(al, Collections.reverseOrder()); int idx = 0; for (int i : al) arr[idx++] = i; } static void sortDec(long[] arr) { ArrayList al = new ArrayList(); for (long i : arr) al.add(i); Collections.sort(al, Collections.reverseOrder()); int idx = 0; for (long i : al) arr[idx++] = i; } 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; } } } ",0 01b911ac,a195911e,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Arrays; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.TreeSet; import java.util.TreeMap; import java.util.PriorityQueue; import java.util.Collections; import java.util.Stack; import java.math.BigInteger; import java.util.LinkedList; import java.util.Iterator; public class First { public static void main(String[] args) { FastScanner fs = new FastScanner(); int T = fs.nextInt(); for (int tt = 0; tt < T; tt++) { solve(fs); } } static void solve(FastScanner fs) { int n=fs.nextInt(); long[] times=takeLong(n, fs); long[] damage=takeLong(n, fs); long reqTime=times[n-1]-damage[n-1]+1; long ans=0; for(int i=n-1;i>=0;) { reqTime=times[i]-damage[i]+1; long time=times[i]; i--; while(i>=0 && times[i]>=reqTime) { long thisReqTime=times[i]-damage[i]+1; reqTime=Math.min(reqTime, thisReqTime); i--; } long x=time-reqTime+1; // pn(x); ans+=(x*(x+1)/2); } pn(ans); } static long MOD=(long)(1e9+7); static boolean check(String x, String ans) { if(x.length()!=ans.length()) return x.length()>ans.length(); if(x.compareTo(ans) <= -1) return false; else return true; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } static void pn(Object o) { System.out.println(o); } static void p(Object o) { System.out.print(o); } static void flush() { System.out.flush(); } static void debugInt(int[] arr) { for(int i=0;i l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i>1); } static String getBinaryFromDecimal(int x) { StringBuilder sb=new StringBuilder(); for(int i=0;i<31;++i) { if((x&1)==0) sb.append('0'); else sb.append('1'); x=x>>1; } return sb.reverse().toString(); } static boolean[] sieve() { int size=(int)(1e5+5); boolean[] isPrime=new boolean[size]; Arrays.fill(isPrime, true); for(int i=2;i { public int compare(String s1, String s2) { if(s1.length()!=s2.length()) return s2.length()-s1.length(); if(s1.compareTo(s2) <= -1) return 1; else return -1; } }","import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc=new FastReader(); static int dp[]; static boolean v[]; // static int mod=998244353;; static int mod=1000000007; static int max; static int bit[]; //static long fact[]; // static long A[]; static HashMap 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 079ad09e,20012377,"import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; 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); EAirConditioners solver = new EAirConditioners(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class EAirConditioners { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(), q = in.nextInt(); int[] a = in.readArray(q); long[] t = in.readLongArray(q); long[] pref = new long[n + 2]; long[] suff = new long[n + 2]; Arrays.fill(pref, (long) 1e14); Arrays.fill(suff, (long) 1e14); for (int i = 0; i < q; ++i) { pref[a[i]] = t[i] - a[i]; suff[a[i]] = t[i] + a[i]; } for (int i = 1; i <= n; ++i) { pref[i] = Math.min(pref[i], pref[i - 1]); } for (int i = n; i >= 1; --i) { suff[i] = Math.min(suff[i], suff[i + 1]); } for (int i = 1; i <= n; ++i) { out.print(Math.min(pref[i] + i, suff[i] - i) + "" ""); } 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 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 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 int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } public long[] readLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) array[i] = nextLong(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }","import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; 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); EAirConditioners solver = new EAirConditioners(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class EAirConditioners { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(), q = in.nextInt(); int[] a = in.readArray(q); long[] t = in.readLongArray(q); long[] pref = new long[n + 2]; long[] suff = new long[n + 2]; Arrays.fill(pref, (long) 1e14); Arrays.fill(suff, (long) 1e14); for (int i = 0; i < q; ++i) { pref[a[i]] = t[i] - a[i]; suff[a[i]] = t[i] + a[i]; } for (int i = 1; i <= n; ++i) { pref[i] = Math.min(pref[i], pref[i - 1]); } for (int i = n; i >= 1; --i) { suff[i] = Math.min(suff[i], suff[i + 1]); } for (int i = 1; i <= n; ++i) { out.print(Math.min(pref[i] + i, suff[i] - i) + "" ""); } 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 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 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 int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } public long[] readLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) array[i] = nextLong(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } } ",1 6653a758,6bcc5afd,"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()]); } }","import java.util.*; public class MyClass { public static void main(String args[]) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int a[]=new int[n]; ArrayList lt1=new ArrayList<>(); ArrayList lt0=new ArrayList<>(); for(int i=0;i=0; 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) { int nrLands = scn.nextInt(); int[] toRightLands = new int[nrLands]; Arrays.fill(toRightLands, (int) (1e9 + 1e8)); int[] toLeftLands = new int[nrLands]; Arrays.fill(toLeftLands, (int) (1e9 + 1e8)); int nrAcs = scn.nextInt(); AC[] acs = new AC[nrAcs]; for (int i = 0; i < nrAcs; i++) { acs[i] = new AC(); acs[i].myPos = scn.nextInt() - 1; } for (int i = 0; i < nrAcs; i++) { acs[i].myTemp = scn.nextInt(); } Arrays.sort(acs); int nextAc = 1; AC lastChosen = acs[0]; for (int i = lastChosen.myPos; i < nrLands; i++) { if (nextAc < acs.length && i == acs[nextAc].myPos) { if (acs[nextAc].myTemp < lastChosen.myTemp + i - lastChosen.myPos) { lastChosen = acs[nextAc]; } nextAc++; } toRightLands[i] = lastChosen.myTemp + i - lastChosen.myPos; } int nextAc1 = acs.length - 2; AC lastChosen1 = acs[acs.length - 1]; for (int i = lastChosen1.myPos; i >= 0; i--) { if (nextAc1 >= 0 && i == acs[nextAc1].myPos) { if (acs[nextAc1].myTemp < lastChosen1.myTemp - i + lastChosen1.myPos) { lastChosen1 = acs[nextAc1]; } nextAc1--; } toLeftLands[i] = lastChosen1.myTemp - i + lastChosen1.myPos; } for (int i = 0; i < nrLands; i++) { System.out.print(Integer.min(toLeftLands[i], toRightLands[i]) + "" ""); } System.out.println(); } } static class AC implements Comparable { int myPos, myTemp; @Override public int compareTo(AC o) { return Integer.compare(myPos, o.myPos); } } } ",0 0df4050e,6f02c6d9,"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.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; } } } ",1 d1cd194e,dbffab11,"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(); } } } ","/* 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 cses { static int mod=1000000007; static FastReader sc=new FastReader(); public static void main (String[] args) throws java.lang.Exception { long startTime=System.currentTimeMillis(); int t=sc.nextInt(); for(int y=0;y pq=new PriorityQueue<>(); int index[]=new int[n]; PriorityQueue pq =new PriorityQueue(new comp()); for(int i=0;i{ public int compare(Node a,Node b) { return a.sum-b.sum; } } static class Node{ int sum; int build; Node(int sum ,int build) { this.sum=sum; this.build=build; } } 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 getExecutionTime(long startTime) { final long endTime = System.currentTimeMillis(); System.out.println(); System.out.println(""Time take is ""+(endTime-startTime)+"" ms""); } } // System.out.println(""Case #""+(y+1)+"": ""+ans); ",0 2b9dfed3,351c9206,"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 E { public static void process() throws IOException { int n = sc.nextInt(),k = sc.nextInt(); int index[] = sc.readArray(k); long t[] = sc.readArrayLong(k); PriorityQueue q = new PriorityQueue(); for(int i = 0; i=1) { while(!q.isEmpty() && q.peek().y > i) { q.poll(); } if(q.isEmpty())break; Pair e = q.peek(); ans[i]=min(ans[i],e.x-(n-i)); i--; } for( i = 1; i<=n; i++)out.print(ans[i]+"" ""); out.println(); } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353; private static int N = 0; private static void google(int tt) { System.out.print(""Case #"" + (tt) + "": ""); } static FastScanner sc; static FastWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new FastWriter(System.out); } else { sc = new FastScanner(""input.txt""); out = new FastWriter(""output.txt""); } long s = System.currentTimeMillis(); int t = 1; t = sc.nextInt(); int TTT = 1; while (t-- > 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 { long x; int y; Pair(long x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Long.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 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 E { public static void process() throws IOException { int n = sc.nextInt(),k = sc.nextInt(); int index[] = sc.readArray(k); long t[] = sc.readArrayLong(k); PriorityQueue q = new PriorityQueue(); for(int i = 0; i=1) { while(!q.isEmpty() && q.peek().y > i) { q.poll(); } if(q.isEmpty())break; Pair e = q.peek(); ans[i]=min(ans[i],e.x-(n-i)); i--; } for( i = 1; i<=n; i++)out.print(ans[i]+"" ""); out.println(); } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353; private static int N = 0; private static void google(int tt) { System.out.print(""Case #"" + (tt) + "": ""); } static FastScanner sc; static FastWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new FastWriter(System.out); } else { sc = new FastScanner(""input.txt""); out = new FastWriter(""output.txt""); } long s = System.currentTimeMillis(); int t = 1; t = sc.nextInt(); int TTT = 1; while (t-- > 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 { long x; int y; Pair(long x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Long.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; } } } } ",1 0922b7e7,c34bb733,"import java.io.*; import java.util.*; public class PhoenixAndTowers { public static void main(String[] args) throws IOException { // BufferedReader in = new BufferedReader(new FileReader(""PhoenixAndTowers.in"")); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(""PhoenixAndTowers.out""))); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int T = Integer.parseInt(in.readLine()); for (int i = 0; i < T; i++) { StringTokenizer st = new StringTokenizer(in.readLine()); int N = Integer.parseInt(st.nextToken()); int M = Integer.parseInt(st.nextToken()); int X = Integer.parseInt(st.nextToken()); PriorityQueue towers = new PriorityQueue(); for (int j = 1; j <= M; j++) towers.add(new Tower(j, 0)); out.println(""YES""); st = new StringTokenizer(in.readLine()); for (int j = 0; j < N; j++) { Tower t = towers.remove(); t.size += Integer.parseInt(st.nextToken()); towers.add(t); out.print(t.idx + "" ""); } out.println(); } out.close(); in.close(); } public static class Tower implements Comparable { int idx, size; public Tower(int idx, int size) { this.idx = idx; this.size = size; } @Override public int compareTo(Tower o) { return size - o.size; } } }","//package pack; import java.io.*; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.*; import java.io.*; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.*; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.math.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Vector; import static java.lang.Math.sqrt; import static java.lang.Math.floor; public class topcoder { static class pair{ int first; int second; public pair(int first, int second) { this.first = first; this.second = second; } } static class Compare{ static void compare(ArrayListarr, int n) { Collections.sort(arr,new Comparator() { public int compare(pair p1, pair p2) { return p1.first-p2.first; } }); } } static class pairr implements Comparable{ Integer value; Integer index; public pairr(Integer value, Integer index) { this.value = value; this.index = index; } @Override public int compareTo(pairr o) { return value-o.value; } } static class Key { public K1 key1; public K2 key2; public Key(K1 key1, K2 key2) { this.key1 = key1; this.key2 = key2; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Key key = (Key) o; if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) { return false; } if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) { return false; } return true; } @Override public int hashCode() { int result = key1 != null ? key1.hashCode() : 0; result = 31 * result + (key2 != null ? key2.hashCode() : 0); return result; } @Override public String toString() { return ""["" + key1 + "", "" + key2 + ""]""; } } public static int sumOfDigits (long n) { int sum = 0; while(n > 0) { sum += n%10; n /= 10; } return sum; } public static long binary_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return ar[s]; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return binary_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return binary_search(s,mid,num,ar); } return -1; } public static int index_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return s; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return index_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return index_search(s,mid,num,ar); } return -1; } public static void swap(int []ar, int i, int j) { for(int k= j; k >= i; k--) { int temp = ar[k]; ar[k] = ar[k+1]; ar[k+1] = temp; } } public static boolean digit_exists(long n) { while(n > 0) { if(n%10 == 9) return true; n = n/10; } return false; } public static int log(int n) { int c = 0; while(n > 0) { c++; n /=2; } return c; } public static int findOr(int[]bits){ int or=0; for(int i=0;i<32;i++){ or=or<<1; if(bits[i]>0) or=or+1; } return or; } static void simpleSieve(int limit, Vector prime) { // Create a boolean array ""mark[0..n-1]"" and initialize // all entries of it as true. A value in mark[p] will // finally be false if 'p' is Not a prime, else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; for (int p=2; p*pl) { // Compute all primes smaller than or equal // to square root of n using simple sieve int limit = (int) (floor(sqrt(n))+1); Vector prime = new Vector<>(); simpleSieve(limit, prime); // Divide the range [0..n-1] in different segments // We have chosen segment size as sqrt(n). int low = limit; int high = 2*limit; // While all segments of range [0..n-1] are not processed, // process one segment at a time while (low < n) { if (high >= n) high = n; // To mark primes in current range. A value in mark[i] // will finally be false if 'i-low' is Not a prime, // else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; // Use the found primes by simpleSieve() to find // primes in current range for (int i = 0; i < prime.size(); i++) { // Find the minimum number in [low..high] that is // a multiple of prime.get(i) (divisible by prime.get(i)) // For example, if low is 31 and prime.get(i) is 3, // we start with 33. int loLim = (int) (floor(low/prime.get(i)) * prime.get(i)); if (loLim < low) loLim += prime.get(i); /* Mark multiples of prime.get(i) in [low..high]: We are marking j - low for j, i.e. each number in range [low, high] is mapped to [0, high-low] so if range is [50, 100] marking 50 corresponds to marking 0, marking 51 corresponds to 1 and so on. In this way we need to allocate space only for range */ for (int j=loLim; j 0) { power++; k /=2 ; } long check = (long)Math.pow(2, power-1); if(k1 == check) { return power; } // System.out.println(power); long f = (long)Math.pow(2, power-1); long rem = k1-f; return find_indexNum(rem); } public static void sortPair(ArrayListl, int n) { n = l.size(); Compare obj = new Compare(); obj.compare(l, n); } public static int add(long n, long num, long a, int i) { //System.out.println(num); if(num > n)return -1; if(num == n) { return i; } if(a < 2050)return -1; long temp = num+a; if(temp <= n) { return add(n,temp,a,i+1); }else if(temp > n){ a /= 10; return add(n,num,a,i); } return -1; } public static void shuffle(int []array, int num,int t_index, boolean []vis, int m ) { for(int i = 0; i < m; i++) { if(vis[i] == false) { int temp = array[i]; if(i < t_index) { vis[i] = true; } array[i] = num; array[t_index] = temp; // System.out.println(array[t_index]+"" ""+array[i]); break; } } } public static void rotate(int []arr,int j, int times, int m) { if(j == 0) { int temp1 = arr[0]; arr[0] = arr[times]; arr[times] = temp1; }else { int temp = arr[j]; int z = arr[0]; arr[0] = arr[times]; arr[j] = z; arr[times] = temp; } } public static void recur(int i,int A, int B,int []dp,int []metal, int n, boolean took,int ind) { if(i-A <= 0 && i-B <= 0)return; int count = 0; for(int j = 1; j <= n; j++) { if(dp[j] >= metal[j]) { count++; } } if(count == n)return; if(i-A >= 0 && i-B >= 0 && dp[i] > 0 && dp[i] > metal[i]) { dp[i]--; dp[i-A]++; dp[i-B]++; } if(ind == 6) { // System.out.println(Arrays.toString(dp)); } recur(i-A,A,B,dp,metal,n,took,ind); recur(i-B,A,B,dp,metal,n,took,ind); } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static boolean[] getSieve(int n) { boolean[] isPrime = new boolean[n+1]; for (int i = 2; i <= n; i++) isPrime[i] = true; for (int i = 2; i*i <= n; i++) if (isPrime[i]) for (int j = i; i*j <= n; j++) isPrime[i*j] = false; return isPrime; } public static int gcd(int a, int b) { int tmp = 0; while(b != 0) { tmp = b; b = a%b; a = tmp; } return a; } public static long gcd(long a, long b) { long tmp = 0; while(b != 0) { tmp = b; b = a%b; a = tmp; } return a; } public static boolean poss( int r, int c, int k, int n, int m,boolean [][]vis) { if(k < 0)return false; if(r > n || c > m || r < 1 || c < 1)return false; if(r == n && c == m && k == 0) { // System.out.println(k); return true; } if(vis[r][c] == true) { return false; } vis[r][c] = true; return poss(r+1,c,k-c,n,m,vis) || poss(r,c+1,k-r,n,m,vis); } public static void dfs(LinkedList[]list, HashMapmap, int parent, int n) { Stackst = new Stack<>(); } public static boolean pos(int n) { int i = 1; boolean pos = false; while(i*i <= n) { if(i*i*2 == n || i*i*4 == n) { pos = true; break; } i++; } if(pos)return true; return false; } public static void main(String args[])throws IOException{ // System.setIn(new FileInputStream(""Case.txt"")); BufferedReader ob = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(ob.readLine()); while( t--> 0) { StringTokenizer st = new StringTokenizer(ob.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); int []ar = new int[n]; st = new StringTokenizer(ob.readLine()); PriorityQueuepq = new PriorityQueue<>(); for(int i = 0; i < n; i++) { ar[i] = Integer.parseInt(st.nextToken()); pq.add(ar[i]); } PriorityQueuep = new PriorityQueue<>(); for(int i =0; i < n; i++) { p.add(new pairr(ar[i],i)); } int []ans = new int[n]; for(int i = 0; i < n; i++) { pairr a = p.poll(); ans[a.index] = (i%m)+1; } System.out.println(""YES""); for(int i = 0; i < n; i++ ) { System.out.print(ans[i]+"" ""); } System.out.println(); } } } ",0 3a12e509,47310b0e,"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.io.*; import java.util.*; public class Codeforces { public static void main(String[] args) throws IOException{ try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { int t = Integer.parseInt(br.readLine()); while (t-- > 0) { int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split("" ""); String c = br.readLine(); ArrayList blue = new ArrayList<>(); ArrayList red = new ArrayList<>(); for (int i = 0; i < n; i++) { if (c.charAt(i) == 'B') blue.add(Integer.parseInt(s[i])); else red.add(Integer.parseInt(s[i])); } Collections.sort(blue); Collections.sort(red); int p1 = 0, p2 = 0, num = 1, flag = 1; while (num <= n) { if (p1 < blue.size()) { if (blue.get(p1) < num) { flag = 0; break; } p1++; } else if (p2 < red.size()) { if (red.get(p2) > num) { flag = 0; break; } p2++; } num++; } if (flag == 1) System.out.println(""YES""); else System.out.println(""NO""); } } } }",0 51cc7026,51d88c51," import java.util.*; import java.math.*; import java.io.*; import java.lang.*; public class C_Balanced_Stone_Heaps { 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[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(next()); } return a; } } public static int t, n; public static int H[]; private static long startTime = System.currentTimeMillis(); public static void main(String[] args) { FastReader sc =new FastReader(); t = sc.nextInt(); while (t-->0) { n = sc.nextInt(); H = new int[n]; int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { H[i] = sc.nextInt(); max = Math.max(H[i], max); min = Math.min(H[i], min); } int mid = 0; while (min < max) { mid = min + (max-min+1)/2; if(check(mid)) { min = mid; } else max = mid-1; } System.out.println(min); } } public static boolean check(int x){ int S[] = Arrays.copyOf(H, H.length); for (int i = n-1; i >= 2; i--) { if(S[i]=2){ // S[i]-=move*3; S[i-1]+=(move); S[i-2]+=2*(move); } } return S[0]>= x && S[1] >= x; } } "," 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 boolean check(int a[],int value) { int b[]= new int[a.length]; for (int i = a.length-1; i >=2; i--) { if(a[i]+b[i] 0) { int n = input.nextInt(); int a[]= new int[n]; int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int i = 0; i < n; i++) { a[i] = input.nextInt(); min = Math.min(min, a[i]); max = Math.max(max, a[i]); } int low = min; int high = max; int ans = 0; while(low<=high) { int mid = low+(high-low)/2; if(check(a, mid)) { ans = mid; low = mid+1; } else { high = mid-1; } } result.append(ans+""\n""); } System.out.println(result); } 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()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } } ",0 0f14b12d,7d12d33c,"//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(); } } } ","//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]; 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(); } } } ",1 80881cae,ad4c7a20,"//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.io.*; import java.util.*; public class Practice { // static final long mod=7420738134811L; static int mod=1000000007; static final int size=501; static FastReader sc=new FastReader(System.in); // static Reader sc=new Reader(); // static Scanner sc=new Scanner(System.in); static PrintWriter out=new PrintWriter(System.out); static long[] factorialNumInverse; static long[] naturalNumInverse; static int[] sp; static long[] fact; static ArrayList pr; public static void main(String[] args) throws IOException, CloneNotSupportedException { // System.setIn(new FileInputStream(""input.txt"")); // System.setOut(new PrintStream(""output.txt"")); // factorial(mod); // InverseofNumber(mod); // InverseofFactorial(mod); // make_seive(); int t=1; t=sc.nextInt(); for(int i=1;i<=t;i++) solve(i); out.close(); out.flush(); // System.out.flush(); // System.exit(0); } static void solve(int CASENO) throws IOException, CloneNotSupportedException { int n=sc.nextInt(); int k[]=new int[n]; int h[]=new int[n]; for(int i=0;i interval=new ArrayList(); ArrayList act=new ArrayList(); for(int i=0;i { int x,y; Pair(int a,int b) { this.x=a; this.y=b; } @Override public boolean equals(Object obj) { if(obj instanceof Pair) { Pair p=(Pair)obj; return p.x==this.x && p.y==this.y; } return false; } @Override public int hashCode() { return Math.abs(x)+500*Math.abs(y); } @Override public String toString() { return ""(""+x+"" ""+y+"")""; } @Override protected Pair clone() throws CloneNotSupportedException { return new Pair(this.x,this.y); } @Override public int compareTo(Pair a) { long t=(this.x-a.x); if(t!=0) return t>0?1:-1; else return (int)(this.y-a.y); } public void swap() { this.y=this.y+this.x; this.x=this.y-this.x; this.y=this.y-this.x; } } static class Tuple implements Cloneable, Comparable { int x,y,z; Tuple(int a,int b,int c) { this.x=a; this.y=b; this.z=c; } public boolean equals(Object obj) { if(obj instanceof Tuple) { Tuple p=(Tuple)obj; return p.x==this.x && p.y==this.y && p.z==this.z; } return false; } @Override public int hashCode() { return (this.x+501*this.y); } @Override public String toString() { return ""(""+x+"",""+y+"",""+z+"")""; } @Override protected Tuple clone() throws CloneNotSupportedException { return new Tuple(this.x,this.y,this.z); } @Override public int compareTo(Tuple a) { int x=this.z-a.z; if(x!=0) return x; int X= this.x-a.x; if(X!=0) return X; return a.y-this.y; } } static void arraySort(int arr[]) { ArrayList a=new ArrayList(); for (int i = 0; i < arr.length; i++) { a.add(arr[i]); } Collections.sort(a,Comparator.reverseOrder()); for (int i = 0; i < arr.length; i++) { arr[i]=a.get(i); } } static void arraySort(long arr[]) { ArrayList a=new ArrayList(); for (int i = 0; i < arr.length; i++) { a.add(arr[i]); } Collections.sort(a); for (int i = 0; i < arr.length; i++) { arr[i]=a.get(i); } } static HashSet primeFactors(int n) { HashSet ans=new HashSet(); if(n%2==0) { ans.add(2); while((n&1)==0) n=n>>1; } for(int i=3;i*i<=n;i+=2) { if(n%i==0) { ans.add(i); while(n%i==0) n=n/i; } } if(n!=1) ans.add(n); return ans; } static void make_seive() { sp=new int[size]; pr=new ArrayList(); for (int i=2; i adj; Node(int ver) { vertex=ver; dis=-1; adj=new HashSet(); } @Override public String toString() { return vertex+"" ""; } } static class Edge { Node to; int cost; Edge(Node t,int c) { this.to=t; this.cost=c; } @Override public String toString() { return ""(""+to.vertex+"",""+cost+"") ""; } } static long power(long x, long y) { if(x<=0) return 1; long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res%mod; } static long binomialCoeff(long n, long k) { if(n n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (long i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } static class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } public void printarray(int arr[]) { for (int i = 0; i < arr.length; i++) System.out.print(arr[i]+"" ""); System.out.println(); } } } ",1 1ea771ea,ee270b2a," 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){ int n = scanner.nextInt(), k = scanner.nextInt(); int[] a = new int[k]; for(int i=0;i=0;i--){ R[i] = Math.min(min+1,R[i]); min = R[i]; } for(int i=0;i0){ int n=sc.nextInt(); int[][] ct=new int[n][5]; int[] len=new int[n]; for (int i=0;i=0;j--){ sum+=diff[j]; if (sum>0) { inc++; }else { break; } } mx=Math.max(mx,inc); } System.out.println(mx); } } } ","/* 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(); } } ",1 46e9aed4,ee4f7b06," 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(); } } "," 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 } ",0 3368f340,402aff07,"//package Codeforces; import java.io.*; import java.util.*; public class Menorah { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); while (t-->0){ int n = sc.nextInt(); char[] a = sc.next().toCharArray(); char[] b = sc.next().toCharArray(); int a1=0, b1=0; for(int i=0;i 0){ int n = s.nextInt(); String a = s.next(); String b = s.next(); int a1 = 0, b1 = 0; for (char c: a.toCharArray()){ if (c == '1') a1++; } for (char c: b.toCharArray()){ if (c == '1') b1++; } int ans = Integer.MAX_VALUE; int res = 0; for (int i = 0; i < n; i++) { if (a.charAt(i) != b.charAt(i)) res++; } if (a1 == b1) ans = Math.min(ans, res); if (b1 == n-a1+1) ans = Math.min(ans, n-res); if (ans == Integer.MAX_VALUE){ System.out.println(""-1""); } else { System.out.println(ans); } } } }",0 0889dcfb,22138fad,"import java.io.*; import java.util.*; import static java.lang.Math.*; public class C{ static class Event implements Comparable{ long i; int index; boolean start; public Event(long a,int c, boolean b) { i=a; index = c; start=b; } public int compareTo(Event o){ if (o.i != i) return (int)(i-o.i); if (start == o.start) return 0; return start ? -1:1; } } public static void main(String[] args) throws IOException{ // br = new BufferedReader(new FileReader("".in"")); // out = new PrintWriter(new FileWriter("".out"")); // new Thread(null, new (), ""fisa balls"", 1<<28).start(); int t= readInt(); while(t-->0) { int n =readInt(); long[] k = new long[n], h = new long[n]; for (int i = 0; i < n; i++) k[i]=readInt(); for (int i = 0; i < n; i++) h[i]=readInt(); List e = new ArrayList(); for (int i = 0 ; i { int time; int monsterNum; boolean isMonster; public Event(int time, int monsterNum, boolean isMonster) { this.time = time; this.monsterNum = monsterNum; this.isMonster = isMonster; } @Override public int compareTo(Event o) { if(time == o.time) { if(!(isMonster ^ o.isMonster)) { return monsterNum - o.monsterNum; } if(o.isMonster) { return -1; } return 1; } return time - o.time; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); for(int test = 0; test < t; test++) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int[] k = new int[n]; int[] h = new int[n]; st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++) k[i] = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++) h[i] = Integer.parseInt(st.nextToken()); int[] latestStart = new int[n]; for(int i = 0; i < n; i++) { latestStart[i] = k[i] - h[i] + 1; } PriorityQueue pq = new PriorityQueue(); for(int i = 0; i < n; i++) { pq.add(new Event(k[i],i,true)); pq.add(new Event(latestStart[i],i,false)); } long manaUsed = 0; int lastEvent = 0; HashMap waiting = new HashMap(); long currentSpell = 1; //pw.println(""NEW GAME""); while(!pq.isEmpty()) { Event nextEvent = pq.poll(); if(!waiting.isEmpty() && nextEvent.time > lastEvent) { manaUsed += rangeSum(currentSpell+1, currentSpell + nextEvent.time - lastEvent); currentSpell = currentSpell + nextEvent.time - lastEvent; } if(waiting.isEmpty()) { currentSpell = 1; manaUsed++; } //pw.println(manaUsed + "" "" + currentSpell + "" "" + nextEvent.monsterNum + "" "" + nextEvent.isMonster); if(nextEvent.isMonster) { waiting.remove(nextEvent.monsterNum); } if(!nextEvent.isMonster) { waiting.put(nextEvent.monsterNum,nextEvent); } lastEvent = nextEvent.time; } pw.println(manaUsed); } pw.close(); } } ",0 35857a5a,ab7507bf,"import java.io.PrintWriter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class solution { public static void main(String args[]) throws java.lang.Exception{ FastScanner s=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int t=s.nextInt(); for(int tt=0;ttsum2) { ans=n; break; }else { ArrayList f=new ArrayList<>(); for(int j=0;j0 && f.get(f.size()-1)>0) { sum2-=f.get(f.size()-1); f.remove(f.size()-1); //out.println(sum2); } if(sum[i]>sum2) { ans=Math.max(ans,f.size()); } //out.println(ans+"" Ayush""); } } out.println(ans); } out.close(); } public static int modulus(int a,int b) { int ans=a%b; if(ans<0) { ans=b+ans; } return ans; } static void sort(long [] a) { ArrayList l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i { if(x[c]>y[c]) { return 1; }else { return -1; } }); } public static void printb(boolean ans) { if(ans) { System.out.println(""Yes""); }else { System.out.println(""No""); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } double nextDouble() { return Double.parseDouble(next()); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i{ int a , b; Pair(int x , int y){ a=x; b=y; } public int compareTo(Pair o) { return a != o.a ? a - o.a : b - o.b; } } } ","import java.util.*; import java.io.*; public class C1551 { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); char[][] arr = new char[n][]; for (int i = 0; i < arr.length; i++) { arr[i] = sc.next().toCharArray(); } int[][] cnt = new int[n][5]; for (int i = 0; i < cnt.length; i++) { for (char c : arr[i]) { cnt[i][c - 'a']++; } } int fans = 0; for (int letter = 0; letter < 5; letter++) { ArrayList al = new ArrayList(); for (int i = 0; i < n; i++) { al.add(2 * cnt[i][letter] - arr[i].length); } Collections.sort(al, Collections.reverseOrder()); int sum = 0; int ans = 0; for (int x : al) { sum += x; if (sum > 0) { ans++; } else { break; } } fans = Math.max(ans, fans); } pw.println(fans); } pw.close(); } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader f) { br = new BufferedReader(f); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextIntArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(next()); } return arr; } } } ",0 bdfe8110,e6a6e318,"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=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; } } } ",1 016510dc,f1540246,"import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; public class TaskC { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class Solver { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(),m = in.nextInt(); int[][] a = new int[n][m]; for(int i=0;i ans = new ArrayList<>(); for(int i=0;i<=n-2;i++){ for(int j=0;j<=m-2;j++) { int cnt = 0; if (a[i][j] == 1) cnt++; if (a[i + 1][j] == 1) cnt++; if(a[i][j+1]==1) cnt++; if(a[i+1][j+1]==1) cnt++; if(cnt==4){ a[i][j] = 0; a[i+1][j] = 0; a[i+1][j+1] = 0; ans.add(new Pair((i+1),(j+1))); ans.add(new Pair((i+2),(j+1))); ans.add(new Pair((i+2),(j+2))); // out.println(); cnt = 1; } if(cnt==1){ boolean flag = false; for(int x=0;x<2;x++){ for(int y=0;y<2;y++){ if(a[i+x][j+y]==1){ // out.print((i+1+x)+"" ""+(j+1+y)+"" ""); ans.add(new Pair((i+1+x),(j+1+y))); int cnt2 = 1; for(int sx=0;sx<2;sx++){ for(int sy=0;sy<2;sy++){ if(a[i+sx][j+sy]!=1){ // out.print((i+sx+1)+"" ""+(j+1+sy)+"" ""); a[i+sx][j+sy]=1; ans.add(new Pair((i+sx+1),(j+sy+1))); cnt2++; if(cnt2==3){ a[i+x][j+y]=0; flag = true; break; } } } if(flag) break; } if(flag) break; } if(flag) break; } if(flag) break; } // out.println(); cnt = 2; } if(cnt==2){ boolean flag = false; for(int x=0;x<2;x++){ for(int y=0;y<2;y++){ if(a[i+x][j+y]==1){ // out.print((i+1+x)+"" ""+(j+1+y)+"" ""); ans.add(new Pair((i+1+x),(j+1+y))); int cnt2 = 1; for(int sx=0;sx<2;sx++){ for(int sy=0;sy<2;sy++){ if(a[i+sx][j+sy]!=1){ // out.print((i+sx+1)+"" ""+(j+1+sy)+"" ""); cnt2++; ans.add(new Pair((i+sx+1),(j+1+sy))); a[i+sx][j+sy]=1; if(cnt2==3){ a[i+x][j+y]=0; flag = true; break; } } } if(flag) break; } if(flag) break; } if(flag) break; } if(flag) break; } // out.println(); cnt = 3; } if(cnt==3){ for(int x=0;x<2;x++){ for(int y=0;y<2;y++){ if(a[i+x][j+y]==1){ a[i+x][j+y]=0; ans.add(new Pair((i+1+x),(j+1+y))); // out.print((i+1+x)+"" ""+(j+1+y)+"" ""); } } } // out.println(); } } } out.println((ans.size())/3); for(int i=0;i> list = new ArrayList<>(); for(int i = 0; i < n - 1; i++) { for(int j = 0; j < m - 1; j++) { list.addAll(change(g, i, j)); } } out.println(list.size()); for(int i = 0; i < list.size(); i++) { for(int j = 0; j < 6; j++) { out.print(list.get(i).get(j) + "" ""); } out.println(); } } out.close(); } static List> change(char[][] g, int i, int j) { List> list = new ArrayList<>(); int cnt = 0; cnt += g[i][j] - '0'; cnt += g[i][j + 1] - '0'; cnt += g[i + 1][j] - '0'; cnt += g[i + 1][j + 1] - '0'; if(cnt != 0) { if(cnt == 4) { List op = new ArrayList<>(); op.add(i + 1); op.add(j + 1 + 1); op.add(i + 1 + 1); op.add(j + 1); op.add(i + 1 + 1); op.add(j + 1 + 1); list.add(op); g[i][j + 1] = '0'; g[i + 1][j] = '0'; g[i + 1][j + 1] = '0'; cnt = 1; } if(cnt == 1) { List op = new ArrayList<>(); int zeroCnt = 0; for(int k1 = 0; k1 < 2; k1++) { for(int k2 = 0; k2 < 2; k2++) { if(g[i + k1][j + k2] == '0' && zeroCnt < 2) { op.add(i + k1 + 1); op.add(j + k2 + 1); zeroCnt++; } else if(g[i + k1][j + k2] == '1'){ op.add(i + k1 + 1); op.add(j + k2 + 1); } } } list.add(op); zeroCnt = 0; for(int k1 = 0; k1 < 2; k1++) { for(int k2 = 0; k2 < 2; k2++) { if(g[i + k1][j + k2] == '0' && zeroCnt < 2) { g[i + k1][j + k2] = '1'; zeroCnt++; } else if(g[i + k1][j + k2] == '1'){ g[i + k1][j + k2] = '0'; } } } cnt = 2; } if(cnt == 2) { List op = new ArrayList<>(); int oneCnt = 0; for(int k1 = 0; k1 < 2; k1++) { for(int k2 = 0; k2 < 2; k2++) { if(g[i + k1][j + k2] == '0') { op.add(i + k1 + 1); op.add(j + k2 + 1); } else if(oneCnt < 1){ op.add(i + k1 + 1); op.add(j + k2 + 1); oneCnt++; } } } list.add(op); oneCnt = 0; for(int k1 = 0; k1 < 2; k1++) { for(int k2 = 0; k2 < 2; k2++) { if(g[i + k1][j + k2] == '0') { g[i + k1][j + k2] = '1'; } else if(oneCnt < 1){ g[i + k1][j + k2] = '0'; oneCnt++; } } } cnt = 3; } if(cnt == 3) { List op = new ArrayList<>(); for(int k1 = 0; k1 < 2; k1++) { for(int k2 = 0; k2 < 2; k2++) { if(g[i + k1][j + k2] == '1') { op.add(i + k1 + 1); op.add(j + k2 + 1); } } } list.add(op); } for(int k1 = 0; k1 < 2; k1++) { for(int k2 = 0; k2 < 2; k2++) { g[i + k1][j + k2] = '0'; } } } return list; } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream(""src/input.in"")); out = new PrintWriter(new FileOutputStream(""src/output.out"")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } } } ",0 317a209c,548ffb07,"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 2eb89317,fcc7e8fa,"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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; 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); TaskA solver = new TaskA(); int t; t = in.nextInt(); //t = 1; while (t > 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) { n=fs.nextInt(); int k=fs.nextInt(); int ac[]=fs.readArray(k); int temp[]=fs.readArray(k); int arr[]=new int[n]; Arrays.fill(arr, Integer.MAX_VALUE/2); for(int i=0;i=0;i--) { ans[i]=Math.min(Math.min(arr[i],left[i]),ans[i+1]+1); } // for(int i=0;in) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long pow(long a,long b) { long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i=0; i--) forced[i]=Math.min(forced[i], forced[i+1]+1); for (int i=0; i l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i Give your 100%, that's it! -> Rules 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 N = 100001; int yo = sc.nextInt(); while (yo-- > 0) { List> list = new ArrayList<>(); int n = sc.nextInt(); for(int i = 0; i < n; i++){ list.add(new ArrayList<>()); } List res = new ArrayList<>(); for(int i = 0; i < n-1; i++){ int u = sc.nextInt()-1; int v = sc.nextInt()-1; res.add(new Pair(u+1,v+1)); list.get(v).add(u); list.get(u).add(v); } boolean ok = helper(0,list,n,-1); if(ok){ out.println(-1); continue; } map.clear(); dfs(0,list,n,-1,-1); for(Pair p : res){ int x = p.x; int y = p.y; out.print(map.get(x + "" "" + y) + "" "" ); } out.println(); } out.close(); } static Map map = new HashMap<>(); static void dfs(int curr, List> list, int n, int par, int what){ List neighbours = list.get(curr); if(what == -1){ boolean three = true; for(int e : neighbours){ String str1 = (curr+1) + "" "" + (e+1); String str2 = (e+1) + "" "" + (curr+1); // out.println(str1); if(three){ map.put(str1,3); map.put(str2,3); three = false; } else { map.put(str1,2);map.put(str2,2); } dfs(e,list,n,curr,map.get(str1)); } } else { for(int e : neighbours){ if(e == par) continue; String str1 = (curr+1) + "" "" + (e+1); String str2 = (e+1) + "" "" + (curr+1); if(what == 2){ map.put(str1,3);map.put(str2,3); } else { map.put(str1,2); map.put(str2,2); } dfs(e,list,n,curr,map.get(str1)); } } } static boolean helper(int curr, List> list, int n, int par){ if(par != -1){ if(list.get(curr).size() >= 3){ return true; } } else { if(list.get(curr).size() > 2){ return true; } } List neighbours = list.get(curr); for(int e : neighbours){ if(e == par) continue; if(helper(e,list,n,curr)){ return true; } } return false; } /* 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); }","//some updates in import stuff 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.*; public class C{ static int mod = (int) (Math.pow(10, 9)+7); static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 }; static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 }; static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 }; static final double eps = 1e-10; static List primeNumbers = new ArrayList<>(); public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); //code below int test = sc.nextInt(); while(test --> 0){ int n = sc.nextInt(); int[][] data = new int[n-1][2]; Map count = new HashMap<>(); Graph g = new Graph(n+1); for(int i= 0; i < n-1; i++){ int x = sc.nextInt(); int y = sc.nextInt(); data[i][0] = x; data[i][1] = y; g.addEdge(x, y); count.putIfAbsent(x, 0); count.put(x, count.get(x)+1); count.putIfAbsent(y, 0); count.put(y, count.get(y)+1); } //we will count int one = 0; int two = 0; boolean flag = true; int start = 0; for(int i : count.keySet()){ if(count.get(i) == 1){ start = i; one++; }else if(count.get(i) == 2){ two++; }else{ flag = false; break; } } if(one != 2){ flag = false; } if(!flag){ out.println(-1); continue; } //now we know this question is solvable for sure //we do something really cool Map> fuck = new HashMap<>(); g.addData(start, fuck); for(int i = 0; i < n-1; i++){ int x = data[i][0]; int y = data[i][1]; //search for the answer ArrayList temp = fuck.get(x); for(Pair curr : temp){ if(curr.a == y){ out.print(curr.b + "" ""); break; } } } out.println(); } out.close(); } //Updation Required //Fenwick Tree (customisable) //Segment Tree (customisable) //-----CURRENTLY PRESENT-------// //Graph //DSU //powerMODe //power //Segment Tree (work on this one) //Prime Sieve //Count Divisors //Next Permutation //Get NCR //isVowel //Sort (int) //Sort (long) //Binomial Coefficient //Pair //Triplet //lcm (int & long) //gcd (int & long) //gcd (for binomial coefficient) //swap (int & char) //reverse //Fast input and output //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //GRAPH (basic structure) public static class Graph{ public int V; public ArrayList> edges; //2 -> [0,1,2] (current) Graph(int V){ this.V = V; edges = new ArrayList<>(V+1); for(int i= 0; i <= V; i++){ edges.add(new ArrayList<>()); } } public void addEdge(int from , int to){ edges.get(from).add(to); edges.get(to).add(from); } public void addData(int start, Map> fuck){ //we will add stuff here int curr = start; int[] visited = new int[V+1]; visited[curr] = 1; int index = 0; while(true){ int make = 0; boolean last = true; for(int edge : edges.get(curr)){ if(visited[edge] == 1){ continue; }else{ make = edge; last = false; break; } } if(last){ break; } //now we have found make, we make and add it in map fuck.putIfAbsent(curr, new ArrayList<>()); fuck.putIfAbsent(make, new ArrayList<>()); fuck.get(curr).add(new Pair(make, index %2 == 0 ? 2 : 3)); fuck.get(make).add(new Pair(curr, index %2 == 0 ? 2 : 3)); visited[make] = 1; curr = make; index++; } } } //DSU (path and rank optimised) public static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; Arrays.fill(rank, 1); Arrays.fill(parent,-1); this.n = n; } public int find(int curr){ if(parent[curr] == -1) return curr; //path compression optimisation return parent[curr] = find(parent[curr]); } public void union(int a, int b){ int s1 = find(a); int s2 = find(b); if(s1 != s2){ if(rank[s1] < rank[s2]){ parent[s1] = s2; rank[s2] += rank[s1]; }else{ parent[s2] = s1; rank[s1] += rank[s2]; } } } } //with mod public static long powerMOD(long x, long y) { long res = 1L; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0){ x %= mod; res %= mod; res = (res * x)%mod; } // y must be even now y = y >> 1; // y = y/2 x%= mod; x = (x * x)%mod; // Change x to x^2 } return res%mod; } //without mod public static long power(long x, long y) { long res = 1L; 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/ x = (x * x); } return res; } public static class segmentTree{ public long[] arr; public long[] tree; public long[] lazy; segmentTree(long[] array){ int n = array.length; arr = new long[n]; for(int i= 0; i < n; i++) arr[i] = array[i]; tree = new long[4*n + 1]; lazy = new long[4*n + 1]; } public void build(int[]arr, int s, int e, int[] tree, int index){ if(s == e){ tree[index] = arr[s]; return; } //otherwise divide in two parts and fill both sides simply int mid = (s+e)/2; build(arr, s, mid, tree, 2*index); build(arr, mid+1, e, tree, 2*index+1); //who will build the current position dude tree[index] = Math.min(tree[2*index], tree[2*index+1]); } public int query(int sr, int er, int sc, int ec, int index, int[] tree){ if(lazy[index] != 0){ tree[index] += lazy[index]; if(sc != ec){ lazy[2*index+1] += lazy[index]; lazy[2*index] += lazy[index]; } lazy[index] = 0; } //no overlap if(sr > ec || sc > er) return Integer.MAX_VALUE; //found the index baby if(sr <= sc && ec <= er) return tree[index]; //finding the index on both sides hehehehhe int mid = (sc + ec)/2; int left = query(sr, er, sc, mid, 2*index, tree); int right = query(sr, er, mid+1, ec, 2*index + 1, tree); return Integer.min(left, right); } //now we will do point update implementation //it should be simple then we expected for sure public void update(int index, int indexr, int increment, int[] tree, int s, int e){ if(lazy[index] != 0){ tree[index] += lazy[index]; if(s != e){ lazy[2*index+1] = lazy[index]; lazy[2*index] = lazy[index]; } lazy[index] = 0; } //no overlap if(indexr < s || indexr > e) return; //found the required index if(s == e){ tree[index] += increment; return; } //search for the index on both sides int mid = (s+e)/2; update(2*index, indexr, increment, tree, s, mid); update(2*index+1, indexr, increment, tree, mid+1, e); //now update the current range simply tree[index] = Math.min(tree[2*index+1], tree[2*index]); } public void rangeUpdate(int[] tree , int index, int s, int e, int sr, int er, int increment){ //if not at all in the same range if(e < sr || er < s) return; //complete then also move forward if(s == e){ tree[index] += increment; return; } //otherwise move in both subparts int mid = (s+e)/2; rangeUpdate(tree, 2*index, s, mid, sr, er, increment); rangeUpdate(tree, 2*index + 1, mid+1, e, sr, er, increment); //update current range too na //i always forget this step for some reasons hehehe, idiot tree[index] = Math.min(tree[2*index], tree[2*index + 1]); } public void rangeUpdateLazy(int[] tree, int index, int s, int e, int sr, int er, int increment){ //update lazy values //resolve lazy value before going down if(lazy[index] != 0){ tree[index] += lazy[index]; if(s != e){ lazy[2*index+1] += lazy[index]; lazy[2*index] += lazy[index]; } lazy[index] = 0; } //no overlap case if(sr > e || s > er) return; //complete overlap if(sr <= s && er >= e){ tree[index] += increment; if(s != e){ lazy[2*index+1] += increment; lazy[2*index] += increment; } return; } //otherwise go on both left and right side and do your shit int mid = (s + e)/2; rangeUpdateLazy(tree, 2*index, s, mid, sr, er, increment); rangeUpdateLazy(tree, 2*index + 1, mid+1, e, sr, er, increment); tree[index] = Math.min(tree[2*index+1], tree[2*index]); return; } } //prime sieve public static void primeSieve(int n){ BitSet bitset = new BitSet(n+1); for(long i = 0; i < n ; i++){ if (i == 0 || i == 1) { bitset.set((int) i); continue; } if(bitset.get((int) i)) continue; primeNumbers.add((int)i); for(long j = i; j <= n ; j+= i) bitset.set((int)j); } } //number of divisors public static int countDivisors(long number){ if(number == 1) return 1; List primeFactors = new ArrayList<>(); int index = 0; long curr = primeNumbers.get(index); while(curr * curr <= number){ while(number % curr == 0){ number = number/curr; primeFactors.add((int) curr); } index++; curr = primeNumbers.get(index); } if(number != 1) primeFactors.add((int) number); int current = primeFactors.get(0); int totalDivisors = 1; int currentCount = 2; for (int i = 1; i < primeFactors.size(); i++) { if (primeFactors.get(i) == current) { currentCount++; } else { totalDivisors *= currentCount; currentCount = 2; current = primeFactors.get(i); } } totalDivisors *= currentCount; return totalDivisors; } //now adding next permutation function to java hehe public static boolean next_permutation(int[] p) { for (int a = p.length - 2; a >= 0; --a) if (p[a] < p[a + 1]) for (int b = p.length - 1;; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } return false; } //finding the value of NCR in O(RlogN) time and O(1) space public static long getNcR(int n, int r) { long p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r > 0) { p *= n; k *= r; long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } return p; } //is vowel function public static boolean isVowel(char c) { return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U'); } //to sort the array with better method 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 l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i 0; j--) C[j] = C[j] + C[j - 1]; } return C[k]; } //Pair with int int public static class Pair{ public int a; public int b; Pair(int a , int b){ this.a = a; this.b = b; } @Override public String toString(){ return a + "" -> "" + b; } } //Triplet with int int int public static class Triplet{ public int a; public int b; public int c; Triplet(int a , int b, int c){ this.a = a; this.b = b; this.c = c; } @Override public String toString(){ return a + "" -> "" + b; } } //Shortcut function public static long lcm(long a , long b){ return a * (b/gcd(a,b)); } //let's make one for calculating lcm basically public static int lcm(int a , int b){ return (a * b)/gcd(a,b); } //int version for gcd public static int gcd(int a, int b){ if(b == 0) return a; return gcd(b , a%b); } //long version for gcd public static long gcd(long a, long b){ if(b == 0) return a; return gcd(b , a%b); } //for ncr calculator(ignore this code) public static long __gcd(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } //swapping two elements in an array public static void swap(int[] arr, int left , int right){ int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //for char array public static void swap(char[] arr, int left , int right){ char temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //reversing an array public static void reverse(int[] arr){ int left = 0; int right = arr.length-1; while(left <= right){ swap(arr, left,right); left++; right--; } } public static long expo(long a, long b, long mod) { long res = 1; while (b > 0) { if ((b & 1) == 1L) res = (res * a) % mod; //think about this one for a second a = (a * a) % mod; b = b >> 1; } return res; } //SOME EXTRA DOPE FUNCTIONS public static long mminvprime(long a, long b) { return expo(a, b - 2, b); } public static long mod_add(long a, long b, long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } public static long mod_sub(long a, long b, long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } public static long mod_mul(long a, long b, long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } public static long mod_div(long a, long b, long m) { a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m; } //O(n) every single time remember that public static long nCr(long N, long K , long mod){ long upper = 1L; long lower = 1L; long lowerr = 1L; for(long i = 1; i <= N; i++){ upper = mod_mul(upper, i, mod); } for(long i = 1; i <= K; i++){ lower = mod_mul(lower, i, mod); } for(long i = 1; i <= (N - K); i++){ lowerr = mod_mul(lowerr, i, mod); } // out.println(upper + "" "" + lower + "" "" + lowerr); long answer = mod_mul(lower, lowerr, mod); answer = mod_div(upper, answer, mod); return answer; } // ll *fact = new ll[2 * n + 1]; // ll *ifact = new ll[2 * n + 1]; // fact[0] = 1; // ifact[0] = 1; // for (ll i = 1; i <= 2 * n; i++) // { // fact[i] = mod_mul(fact[i - 1], i, MOD1); // ifact[i] = mminvprime(fact[i], MOD1); // } //ifact is basically inverse factorial in here!!!!!(imp) public static long combination(long n, long r, long m, long[] fact, long[] ifact) { long val1 = fact[(int)n]; long val2 = ifact[(int)(n - r)]; long val3 = ifact[(int)r]; return (((val1 * val2) % m) * val3) % m; } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------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; } } //-------------------------------------------------------- }",0 90dc2b20,d8a171a3,"import java.util.*; import java.io.*; public class C1615{ static FastScanner fs = null; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); while (t-->0) { int n = fs.nextInt(); String a = fs.next(); String b = fs.next(); char ch1[] = a.toCharArray(); char ch2[] = b.toCharArray(); int c00 = 0; int c01 = 0; int c10 = 0; int c11 = 0; for(int i=0;i l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i list = new ArrayList<>(); for(int i:arr) list.add(i); Collections.sort(list); for(int i = 0;i q = new PriorityQueue<>(); for (int i = 0; i < n; i++) { q.add(in.nextInt()); } for (int i = 0; i < n; i++) { data[i] = q.poll(); total += data[i]; } Arrays.sort(data); long result = Long.max(0, total - k); long cur = 0; int num = 0; for (int i = n - 1; i > 0 && total > k + cur; i--) { num++; cur += data[i] - data[0]; long left = Long.max(0, total - cur - k); left /= (num + 1); if (total - cur - (left * (num + 1)) > k) { left++; } //System.out.println(num + "" "" + left + "" "" + cur + "" "" + total); result = Long.min(result, left + num); } out.println(result); } out.close(); } static long cal(long msg, long k) { if (msg <= k) { return msg * (msg + 1L) / 2L; } long re = k * (k + 1L) / 2L; long left = 2 * k - 1 - msg; long other = (k - 1) * k / 2; other -= left * (left + 1) / 2; //System.out.println(other); return re + other; } static long cal2(long msg, long k) { if (msg <= k) { return BigInteger.valueOf(msg).multiply(BigInteger.valueOf(msg + 1)).shiftRight(1).longValue(); } BigInteger re = BigInteger.valueOf(k).multiply(BigInteger.valueOf(k + 1)).shiftRight(1); long left = 2 * k - 1 - msg; BigInteger other = BigInteger.valueOf(k).multiply(BigInteger.valueOf(k - 1)).shiftRight(1); other = other.subtract(BigInteger.valueOf(left).multiply(BigInteger.valueOf(left + 1)).shiftRight(1)); return re.add(other).longValue(); } static int find(int index, int[] u) { if (u[index] != index) { return u[index] = find(u[index], u); } return index; } 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; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; 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) % MOD; } else { return (val * ((val * a) % MOD)) % MOD; } } 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.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 ; o=psum[n]) { System.out.println(0); continue; } long ans = Integer.MAX_VALUE; long x = 0; long a1 = arr[0]; for(int y = 0 ; y0) { x++; } // if(x<0) { // continue; // } ans = Math.min(ans, Math.max(x, 0) + y); } ans = Math.max(ans, 0); System.out.println(ans); } // }catch(Exception e) { // return; // } } //------------------------------------------------------------------------------------------------------------------------------------------------ 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 ; i0) { x/=2; a++; } return a; } public static long ncr(long[] fac, int n , int r , long m) { return fac[n]*(modInverse(fac[r], m))%m *(modInverse(fac[n-r], m))%m; } public static void build(int [][] seg,char []arr,int idx, int lo , int hi) { if(lo == hi) { // seg[idx] = arr[lo]; seg[idx][(int)arr[lo]-'a'] = 1; 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(int i = 0 ; i<27;i++) { seg[idx][i] = seg[2*idx+1][i] + seg[2*idx + 2][i]; } } //for f/inding minimum in range public static void query(int[][]seg,int[]ans,int idx , int lo , int hi , int l , int r) { if(lo>=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 aa4b840f,e99c14b9,"import java.io.*; import java.util.*; public class Contest1615C { 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; public static void main(String[] args) { int t = r.nextInt(); while (t > 0) { t--; int n = r.nextInt(); String a = r.next(); String b = r.next(); int sum1 = 0; int sum2 = 0; for (int i = 0; i < n; i ++) { sum1 += (a.charAt(i) == '1'?1:0); sum2 += (b.charAt(i) == '1'?1:0); } if (sum1!=sum2 && sum1+sum2 != n+1) { pw.println(-1); continue; } int[][] count = new int[2][2]; for (int i = 0; i < n; i ++) { count[(int)a.charAt(i)-(int)'0'][(int)b.charAt(i)-(int)'0']++; } int min = 10000000; if (count[0][1] == count[1][0]) { min = Math.min(min, count[0][1]*2); } if (count[1][1] == count[0][0] + 1) { min = Math.min(min, count[1][1] + count[0][0]); } pw.println(min == 10000000 ? -1:min); } pw.close(); } }","import java.io.*; import java.util.*; /* */ public class A{ static FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); int t=sc.nextInt(); for(int tt=0;tt al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i ss = new Stack<>(); boolean hachu = true; for(int i = a; i <= b; i++) { if(s.charAt(i) == ')' && ss.isEmpty()) {hachu = false; break;} if(s.charAt(i) == '(') ss.add('('); else ss.pop(); } return ss.empty() && hachu; } static String reverseOfString(String a) { StringBuilder ssd = new StringBuilder(); for(int i = a.length() - 1; i >= 0; i--) { ssd.append(a.charAt(i)); } return ssd.toString(); } static char[] reverseOfChar(char a[]) { char b[] = new char[a.length]; int j = 0; for(int i = a.length - 1; i >= 0; i--) { b[i] = a[j]; j++; } return b; } static boolean isPalindrome(char a[]) { boolean hachu = true; for(int i = 0; i <= a.length / 2; i++) { if(a[i] != a[a.length - 1 - i]) { hachu = false; break; } } return hachu; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long powermod(long x, long y, long mod){ long ans = 1; x = x % mod; if (x == 0) return 0; int i = 1; while (y > 0){ if ((y & 1) != 0) ans = (ans * x) % mod; y = y >> 1; x = (x * x) % mod; } return ans; } static long power(long x, long y){ long ans = 1; if (x == 0) return 0; int i = 1; while (y > 0){ if ((y & 1) != 0) ans = (ans * x); y = y >> 1; x = (x * x); } return ans; } static boolean check(String a) { boolean hachu = true; for(int i = 0; i < a.length(); i++) { if(a.charAt(0) != a.charAt(i)) {hachu = false; break;} } return hachu; } public static class Pair implements Comparable { public int index; public int value; public Pair(int index, int value) { this.index = index; this.value = value; } @Override public int compareTo(Pair other) { //multiplied to -1 as the author need descending sort order return -1 * Integer.valueOf(this.value).compareTo(other.value); } } static boolean equalString(int i, int j, int arr[], String b) { int brr[] = new int[26]; for(int k = i; k <= j; k++) brr[b.charAt(k) - 'a']++; for(int k = 0; k < 26; k++) { if(arr[k] != brr[k]) return false; } return true; } static boolean cequalArray(String a, String b) { int count[] = new int[26]; int count1[] = new int[26]; for(int i = 0; i < a.length(); i++) count[a.charAt(i) - 'a']++; for(int i = 0; i < a.length(); i++) count1[b.charAt(i) - 'a']++; for(int i = 0; i < 26; i++) if(count[i] != count1[i]) return false; return true; } static boolean isPrime(long d) { if(d == 1) return true; for(int i = 2; i <= (long)Math.sqrt(d); i++) { if(d % i == 0) return false; } return true; } public static void main(String[] args) throws Exception{ FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); int arr[] = new int[k]; int temp[] = new int[k]; for(int i = 0; i < k; i++) arr[i] = sc.nextInt(); for(int i = 0; i < k; i++) temp[i] = sc.nextInt(); long brr[] = new long[n]; Arrays.fill(brr, Integer.MAX_VALUE); for(int i = 0; i < k; i++) brr[arr[i] - 1] = temp[i]; for(int i = 1; i < n; i++) { brr[i] = Math.min(brr[i], brr[i - 1] + 1); } for(int i = n - 2; i >= 0; i--) { brr[i] = Math.min(brr[i], brr[i + 1] + 1); } for(long e: brr) out.print(e + "" ""); out.println(); } out.close(); } }","import java.util.*; import java.lang.*; import java.io.*; public class cf { static PrintWriter out; static int MOD = 1000000007; static FastReader scan; /*-------- I/O using short named function ---------*/ public static String ns() { return scan.next(); } public static int ni() { return scan.nextInt(); } public static long nl() { return scan.nextLong(); } public static double nd() { return scan.nextDouble(); } public static String nln() { return scan.nextLine(); } public static void p(Object o) { out.print(o); } public static void ps(Object o) { out.print(o + "" ""); } public static void pn(Object o) { out.println(o); } /*-------- for output of an array ---------------------*/ static void iPA(int arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + "" ""); out.println(output); } static void lPA(long arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + "" ""); out.println(output); } static void sPA(String arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + "" ""); out.println(output); } static void dPA(double arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + "" ""); out.println(output); } /*-------------- for input in an array ---------------------*/ static void iIA(int arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = ni(); } static void lIA(long arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = nl(); } static void sIA(String arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = ns(); } static void dIA(double arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = nd(); } /*------------ for taking input faster ----------------*/ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = """"; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static ArrayList sieveOfEratosthenes(int n) { // Create a boolean array // ""prime[0..n]"" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } ArrayList arr = new ArrayList<>(); // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } // Method to check if x is power of 2 static boolean isPowerOfTwo(int x) { return x != 0 && ((x & (x - 1)) == 0); } //Method to return lcm of two numbers static int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } //Method to count digit of a number static int countDigit(long n) { String sex = Long.toString(n); return sex.length(); } static void reverse(int a[]) { int i, k, t; int n = a.length; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static final Random random = new Random(); //Method for sorting static void ruffleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = random.nextInt(n); int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static void sortadv(long[] a) { ArrayList l = new ArrayList<>(); for (long value : a) { l.add(value); } Collections.sort(l); for (int i = 0; i < l.size(); i++) a[i] = l.get(i); } //Method for checking if a number is prime or not static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static void main(String[] args) throws java.lang.Exception { OutputStream outputStream = System.out; out = new PrintWriter(outputStream); scan = new FastReader(); //for fast output sometimes StringBuilder sb = new StringBuilder(); int t = ni(); while (t-- != 0) { int n=ni(); int k=ni(); int[] a=new int[k]; int[] temp=new int[k]; iIA(a); iIA(temp); long dp[]=new long[n]; Arrays.fill(dp,Integer.MAX_VALUE); for(int i=0;i=0;i--){ dp[i]=Math.min(dp[i+1]+1,dp[i]); } lPA(dp); //pn(""""); } out.flush(); out.close(); } } ",1 98950986,b9595381,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class ReverseString { static class pair implements Comparable{ String s; int contribution; pair(String s , int contribution){ this.s = s; this.contribution = contribution; } @Override public int compareTo(pair pair) { return this.contribution-pair.contribution; } public String toString(){ return s + "" "" + contribution; } public int getValue(){ return contribution; } } static int factorOfLetter(String s , char c){ int sum = 0; for (int i = 0; i < s.length(); i++) { if(s.charAt(i)==c)sum++; } int rem = Math.abs(sum-s.length()); return sum-rem; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while (t-->0){ int n = Integer.parseInt(br.readLine()); int max = Integer.MIN_VALUE; String [] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = br.readLine(); } for (int i = 0; i < 5; i++) { int word = 0; ArrayList tmp = new ArrayList<>(); for (int j = 0; j < arr.length; j++) { tmp.add(new pair(arr[j] , factorOfLetter(arr[j] , (char)(i+97)))); } Collections.sort(tmp); //System.out.println(tmp); int acc = 0; for (int j = tmp.size()-1; j >=0 ; j--) { acc += tmp.get(j).contribution; if (acc<=0)break; else word++; } max = Math.max(max , word); } System.out.println(max); } } }","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; } } ",0 0d4d22e0,d20e861b," import java.util.*; public class CodeForces { static ArrayListres; public static void main(String[] args) { Scanner input = new Scanner(System.in); int test = input.nextInt(); while(test-->0){ int n = input.nextInt(); int m = input.nextInt(); char arr[][] = new char[n][m]; res = new ArrayList<>(); for(int i =0;i ans = new ArrayList<>(); public static void add(int a,int b,int c,int d,int e,int f){ int[] tem=new int[6]; tem[0]=a;tem[1]=b;tem[2]=c;tem[3]=d;tem[4]=e;tem[5]=f; ans.add(tem); } public static void helper(int[][] arr,int r,int c){ for(int k=0;k<12;k++){ if(arr[r][c]==1){ add(r,c,r+1,c,r,c+1); arr[r][c]=1-arr[r][c]; arr[r+1][c]=1-arr[r+1][c]; arr[r][c+1]=1-arr[r][c+1]; } if(arr[r][c+1]==1){ add(r,c+1,r+1,c+1,r,c); arr[r][c+1]=1-arr[r][c+1]; arr[r+1][c+1]=1-arr[r+1][c+1]; arr[r][c]=1-arr[r][c]; } if(arr[r+1][c]==1){ add(r+1,c+1,r,c,r+1,c); arr[r+1][c+1]=1-arr[r+1][c+1]; arr[r][c]=1-arr[r][c]; arr[r+1][c]=1-arr[r+1][c]; } if(arr[r+1][c+1]==1){ add(r+1,c+1,r+1,c,r,c+1); arr[r+1][c+1]=1-arr[r+1][c+1]; arr[r+1][c]=1-arr[r+1][c]; arr[r][c+1]=1-arr[r][c+1]; } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int m = sc.nextInt(); ans.clear(); int[][] arr=new int[n][m]; for(int i=0;i one = new ArrayList(); ArrayList zero = new ArrayList(); for (int i = 0; i < n; i++) { if (arr[i] == 1) { one.add(i); } else { zero.add(i); } } int[][] dp = new int[one.size() + 1][zero.size() + 1]; for (int i = 1; i <= one.size(); i++) { dp[i][i] = dp[i - 1][i - 1] + Math.abs(one.get(i - 1) - zero.get(i - 1)); for (int j = i + 1; j <= zero.size(); j++) { dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j - 1] + Math.abs(one.get(i - 1) - zero.get(j - 1))); } } return dp[one.size()][zero.size()]; } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split("" ""); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(s[i]); } log.write(Integer.toString(solution(n, arr)) + ""\n""); log.flush(); } }","import java.util.*; public class 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)); } }",0 26e699de,3dd65549,"//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; i0) { 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 307ef2cb,565f77b7,"import java.util.PriorityQueue; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Arrays; import java.util.Random; import java.io.FileWriter; import java.io.PrintWriter; /* Solution Created: 18:16:56 02/05/2021 Custom Competitive programming helper. */ public class Main { public static void solve() { int n = in.nextInt(), m = in.nextInt(), x = in.nextInt(); Pair[] a = new Pair[n]; for(int i = 0; i heights = new PriorityQueue<>(); for(int i = 0; ix) { out.println(""NO""); return; } out.println(""YES""); out.printlnArray(ans); } static class Pair implements Comparable{ int idx; long val; public Pair(int idx, long val) { this.idx = idx; this.val = val; } public String toString() { return this.idx+"" ""+this.val; } @Override public int compareTo(Main.Pair o) { return Long.compare(this.val, o.val); } } public static void main(String[] args) { in = new Reader(); out = new Writer(); int t = in.nextInt(); while(t-->0) solve(); out.exit(); } static Reader in; static Writer out; static class Reader { static BufferedReader br; static StringTokenizer st; public Reader() { this.br = new BufferedReader(new InputStreamReader(System.in)); } public Reader(String f){ try { this.br = new BufferedReader(new FileReader(f)); } catch (IOException e) { e.printStackTrace(); } } public int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public double[] nd(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[] nca() { return next().toCharArray(); } public String[] ns(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int nextInt() { ensureNext(); return Integer.parseInt(st.nextToken()); } public double nextDouble() { ensureNext(); return Double.parseDouble(st.nextToken()); } public Long nextLong() { ensureNext(); return Long.parseLong(st.nextToken()); } public String next() { ensureNext(); return st.nextToken(); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); return null; } } private void ensureNext() { if (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } } } static class Util{ private static Random random = new Random(); static long[] fact; public static void initFactorial(int n, long mod) { fact = new long[n+1]; fact[0] = 1; for (int i = 1; i < n+1; i++) fact[i] = (fact[i - 1] * i) % mod; } public static long modInverse(long a, long MOD) { long[] gcdE = gcdExtended(a, MOD); if (gcdE[0] != 1) return -1; // Inverted doesn't exist long x = gcdE[1]; return (x % MOD + MOD) % MOD; } public static long[] gcdExtended(long p, long q) { if (q == 0) return new long[] { p, 1, 0 }; long[] vals = gcdExtended(q, p % q); long tmp = vals[2]; vals[2] = vals[1] - (p / q) * vals[2]; vals[1] = tmp; return vals; } public static long nCr(int n, int r, long MOD) { if (r == 0) return 1; return (fact[n] * modInverse(fact[r], MOD) % MOD * modInverse(fact[n - r], MOD) % MOD) % MOD; } public static long nCr(int n, int r) { return (fact[n]/fact[r])/fact[n-r]; } public static long nPr(int n, int r, long MOD) { if (r == 0) return 1; return (fact[n] * modInverse(fact[n - r], MOD) % MOD) % MOD; } public static long nPr(int n, int r) { return fact[n]/fact[n-r]; } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static boolean[] getSieve(int n) { boolean[] isPrime = new boolean[n+1]; for (int i = 2; i <= n; i++) isPrime[i] = true; for (int i = 2; i*i <= n; i++) if (isPrime[i]) for (int j = i; i*j <= n; j++) isPrime[i*j] = false; return isPrime; } public static int gcd(int a, int b) { int tmp = 0; while(b != 0) { tmp = b; b = a%b; a = tmp; } return a; } public static long gcd(long a, long b) { long tmp = 0; while(b != 0) { tmp = b; b = a%b; a = tmp; } return a; } public static int random(int min, int max) { return random.nextInt(max-min+1)+min; } public static void dbg(Object... o) { System.out.println(Arrays.deepToString(o)); } public static void reverse(int[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { int tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(int[] s) { reverse(s, 0, s.length-1); } public static void reverse(long[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { long tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(long[] s) { reverse(s, 0, s.length-1); } public static void reverse(float[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { float tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(float[] s) { reverse(s, 0, s.length-1); } public static void reverse(double[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { double tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(double[] s) { reverse(s, 0, s.length-1); } public static void reverse(char[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { char tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(char[] s) { reverse(s, 0, s.length-1); } public static void reverse(T[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { T tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(T[] s) { reverse(s, 0, s.length-1); } public static void shuffle(int[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); int t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(long[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); long t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(float[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); float t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(double[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); double t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(char[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); char t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(T[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); T t = s[i]; s[i] = s[j]; s[j] = t; } } public static void sortArray(int[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(long[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(float[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(double[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(char[] a) { shuffle(a); Arrays.sort(a); } public static > void sortArray(T[] a) { Arrays.sort(a); } } static class Writer { private PrintWriter pw; public Writer(){ pw = new PrintWriter(System.out); } public Writer(String f){ try { pw = new PrintWriter(new FileWriter(f)); } catch (IOException e) { e.printStackTrace(); } } public void printArray(int[] a) { 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); } } }",0 81a9086f,baebdc56," import java.io.*; import java.util.*; public class D { static long mod=998244353; static long[] facts, factInvs; public static void main(String[] args)throws IOException { FastReader f=new FastReader(); StringBuffer sb = new StringBuffer(); int n=f.nextInt(); int k=f.nextInt(); Time time[]=new Time[n]; for(int i=0;i pq=new PriorityQueue<>(); for(int i=0;i=0; i--) factInvs[i]=mul(factInvs[i+1], i+1); } static long getNcR(int n, int k) { return mul(facts[n], mul(factInvs[k], factInvs[n-k])); } static long mul(long a, long b) { return a*b%mod; } static long modInv(long x) { return exp(x, mod-2); } static long exp(long base, long e) { if (e==0) return 1; long half=exp(base, e/2); if (e%2==0) return mul(half, half); else return mul(half, mul(half, base)); } static class Time implements Comparable