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; } 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; } 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 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); } } } ",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 Pair { int a, b; public Pair(int a, int b) { this.a = a; this.b = b; } } static boolean vis[]; public static void dfs(ArrayList> 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; } 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; } 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; } 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; } 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,"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[]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(); } } } ",0 4e9c4bf9,7011024d,"import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class E1525D { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int[] arr = new int[n]; ArrayList occupied = new ArrayList<>(); for (int i = 0; i < n; i++) { arr[i] = scn.nextInt(); if (arr[i] == 1) occupied.add(i); } int[][] dp = new int[n + 1][occupied.size() + 1]; for (int[] row : dp) Arrays.fill(row, (int) 1e9); dp[0][0] = 0; for (int i = 1; i <= n; i++) { dp[i][0] = 0; for (int j = 1; j <= occupied.size(); j++) { dp[i][j] = dp[i - 1][j]; if (arr[i - 1] == 0) dp[i][j] = Math.min(dp[i][j], dp[i - 1][j - 1] + Math.abs(i - 1 - occupied.get(j - 1))); } } System.out.println(dp[n][occupied.size()]); } } ","import java.util.*; 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 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 { // 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. static int dp[][]; 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(available 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]); } } ",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]; } // For Fast Input ---- } ","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 { int id, h; public Pair(int id, int h) { this.id = id; this.h = 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; } } 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; } } } ","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""); } } } } ",0 35eb27da,64ce7b1e,"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{ 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"")); } } } ",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; } } ","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 MyWriter extends PrintWriter { private MyWriter(OutputStream outputStream) { super(outputStream); } void println(int[] a) { for (int v : a) { println(v); } } 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; } 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 { 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 pr(Object o) { System.out.println(o.toString()); } public static int inInt(String s) { return Integer.parseInt(s); } 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;w0) { 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,"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; } int bit[]=new int[(int)1e6]; /////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////// } ","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 MyWriter extends PrintWriter { private MyWriter(OutputStream outputStream) { super(outputStream); } void println(int[] a) { for (int v : a) { println(v); } } 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; } 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; } 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; } 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,"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); } // 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,"import java.util.*; import java.io.*; import java.math.BigInteger; import java.sql.Array; public class CP { static int mod = 1000000007; /* ***************************************************************************************************************************************************/ 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;i0) { 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(); } } } ","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(); } } ",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; } // 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""); } } } } ",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(); } } ","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; } } /*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; /* for (1/a)%mod = ( a^(mod-2) )%mod ----> use expo to calc -->(a^(mod-2)) */ 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 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); } } ","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() { }); 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; /* for (1/a)%mod = ( a^(mod-2) )%mod ----> use expo to calc -->(a^(mod-2)) */ 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 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 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(); } } ","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 { 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(); } } ","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,"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[]; public static void main(String[] args) throws Exception { ///scan=new FastScanner(""cownomics.in""); //out = new PrintWriter(""cownomics.out""); /* currently doing 1-digit dp 2-ds like fenwick and interval tree and sparse table */ /* READING 1-Everything About Dynamic Programming codeforces 2-DYNAMIC PROGRAMMING: FROM NOVICE TO ADVANCED topcoder 3-Introduction to DP with Bitmasking codefoces 4-Bit Manipulation hackerearth 5-read more about mobious and inculsion-exclusion */ int tt=1; tt=scan.nextInt(); int T=tt; /*Mapmap=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(); } 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); } 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; } public String toString() { return x + "" "" + y+"" ""+z; } } } ","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]; } } } ","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]; } } } ",1 1097b326,a60fba84,"import java.util.Scanner; public class Main { 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; 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(); } } ","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{ 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 int[] nia(int n){ int[] arr= new int[n]; int i=0; while(ires; 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;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 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(); } } ",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> 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<>(); System.out.println(""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) { System.out.print(ele + "" ""); } } ///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// static int ni() { return sc.nextInt(); } ///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// //-----------HritikScanner class for faster input----------// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// // Function to return gcd of a and b time complexity O(log(a+b)) // method to return LCM of two numbers ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// // method returns Nth power of A } ",1 3a318b43,90b71536,"import java.lang.reflect.Array; import java.util.*; import java.io.*; import java.util.regex.Pattern; public class B2 { 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() { }); } public static class Pair, V extends Comparable> implements Comparable> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } } // 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 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()); } } ",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); } //-----------PrintWriter for faster output--------------------------------- public static FastIO io = new FastIO(); //-----------MyScanner class for faster input---------- //-------------------------------------------------------- } ","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 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); } } ","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); 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)) { System.out.println(""NO""); return; } if(col[i] == 'B') { if(arr[i] > n) continue; cB[arr[i]]++; if(cB[arr[i]] > arr[i]) { System.out.println(""NO""); return; } } else { if(arr[i] < 1) continue; cR[arr[i]]++; if(cR[arr[i]] > (n-(arr[i]-1))) { System.out.println(""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) { System.out.println(""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))) { System.out.println(""NO""); return; } } // pa(psum1); System.out.println(""YES""); } ///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// static int[] nextIntArray(int n) { int[] arr = new int[n]; int i = 0; while (i < n) { arr[i++] = ni(); } return arr; } ///////////////////////////////////////////////////////////////////////////////// static int ni() { return sc.nextInt(); } ///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// //-----------HritikScanner class for faster input----------// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// // Function to return gcd of a and b time complexity O(log(a+b)) // method to return LCM of two numbers ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// // method returns Nth power of A } ",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); } } ","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{ static final Random random = new Random(); private void sort(int A[]) { int n = A.length; for(int i=0; i0){ int n=sc.nextInt(); int k=sc.nextInt(); int idx[]=new int[k]; for(int i=0;i=0;i--){ right[i]=Math.min(right[i+1]+1,arr[i]); } for(int i=0;i 0;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; 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; } } //Important //Dont't stick on same approach //Check long } ","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;i { int x; int y; pair(int x, int y) { this.x = x; this.y = 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; } } } ",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<>(); System.out.println(""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) { System.out.print(ele + "" ""); } } ///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// static int ni() { return sc.nextInt(); } ///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// //-----------HritikScanner class for faster input----------// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// // Function to return gcd of a and b time complexity O(log(a+b)) // method to return LCM of two numbers ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// // method returns Nth power of A } ",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-------------------------------------------- //----------------------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 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 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) // function to find first index >= x // function to find last index <= y // function to count elements within given range public int add(int a, int b){ a+=b; if(a>=MOD) a-=MOD; else if(a<0) a+=MOD; return a; } int[] fact= new int[MAXN]; int[] inv= new int[MAXN]; /** * return VALUE of lower bound for unsorted array */ /** * return VALUE of upper bound for unsorted array */ 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]; 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); } } } // public static class compareL implements Comparator{ // @Override // public int compare(Tuple t1, Tuple t2) { // return t2.l - t1.l; // } // } // fast input reader class; } ",0 1469697d,69c86b61,"import java.util.*; import java.io.*; public class Test { // global variables static final long INF = 1000000000000000000L; 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)); } } ","import java.util.*; import java.io.*; public class Test { // global variables static final long INF = 1000000000000000000L; 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,"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; } ","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(); } } ","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(); } } ",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(); } } ",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;i0){ 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))); } } ",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; } } 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); } } } // >>>>>>>>>>> Code Ends <<<<<<<<< // // --For Rounding--// // ----Greatest Common Divisor-----// // --- permutations and Combinations ---// static long fact[]; static long invfact[]; // ---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 ------// // --- Fast exponentiation ---// // >>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<< // } ",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; } } ",0 8d6f1bf5,dc281165,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Menorah { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t > 0) { int n = Integer.parseInt(br.readLine()); String source = br.readLine(); String destination = br.readLine(); int sameStatusOnes = 0; int sameStatusZeros = 0; int diffStatusOnes = 0; int diffStatusZeros = 0; for (int i = 0; i < n; i++) { char c1 = source.charAt(i); char c2 = destination.charAt(i); if (c1 == c2) { if (c1 == '0') { sameStatusZeros += 1; } else { sameStatusOnes += 1; } } else { if (c1 == '0') { diffStatusZeros += 1; } else { diffStatusOnes += 1; } } } int sameStatus = sameStatusOnes + sameStatusZeros; int diffStatus = diffStatusOnes + diffStatusZeros; //first case if (sameStatus == n) { System.out.println(0); } else if (diffStatus == n) { //second case if (diffStatus % 2 == 0 && diffStatusOnes == (n + 1) / 2) System.out.println(n); else System.out.println(-1); } else { int op1 = -1; int op2 = -1; if (sameStatus % 2 != 0 && sameStatusOnes == (sameStatus + 1) / 2) op1 = sameStatus; if (diffStatus % 2 == 0 && diffStatusOnes == (diffStatus + 1) / 2) op2 = diffStatus; if (op1 != -1 && op2 != -1) System.out.println(Integer.min(op1, op2)); else if (op1 != -1) System.out.println(op1); else if (op2 != -1) System.out.println(op2); else System.out.println(-1); } t--; } } } ","import 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""; 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); } } ",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); } } ","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;i0) { 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 int i() { return sc.nextInt(); } static String s() { return sc.next(); } 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) { 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 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); } // 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 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>() { }); // 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; } } ",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); } } ","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; } // 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=0; i--) { ans[i]=Math.min(ans[i],ans[i+1]+1); } for (int i=0; i0) { 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,"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); } } ","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}; } } ",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; } 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(); } } ","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; } 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(); } } ",1 26e699de,fcc7e8fa,"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 final Random random=new Random(); } ",0 9028caf7,e6b7a899,"import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=1; while(T-->0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList list=new ArrayList<>(); ArrayList space=new ArrayList<>(); for(int i=0;i ans=new ArrayList<>(); public static void main(String[] args){ PrintWriter out = new PrintWriter(System.out); InputReader sc=new InputReader(System.in); int N = sc.nextInt(); int[] A = sc.nextIntArray(N); ArrayList a1 = new ArrayList(); ArrayList a2 = new ArrayList(); for (int i = 0; i < A.length; i++) { if (A[i]==0) { a1.add(i); }else { a2.add(i); } } int[][] dp = new int[a1.size()+1][a2.size()+1]; for (int i = 0; i < dp.length; i++) { Arrays.fill(dp[i], Integer.MAX_VALUE/2); } dp[0][0] = 0; for (int i = 1; i <= a1.size() ; i++) { int pos1 = a1.get(i-1); for (int j = 0; j <= a2.size(); j++) { dp[i][j] = dp[i-1][j]; if (j-1>=0) { int pos2 = a2.get(j-1); dp[i][j] = Math.min(dp[i][j], dp[i-1][j-1] + Math.abs(pos1-pos2)); } } } System.out.println(dp[a1.size()][a2.size()]); } } ",0 b2590225,f7fc2e94,"import java.util.*; public class P3 { public static class tower implements Comparable{ ArrayList index; int size; tower(){ size = 0; index = new ArrayList<>(); } } 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 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 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]); } } } } ","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; } } } ",0 722e318f,ff1fc018,"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""); } } ","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;j0) { 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, 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 { public static FastReader obj = new FastReader(); public static PrintWriter out = new PrintWriter(System.out); //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 int i() { return obj.nextInt(); } 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 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() { }); } // 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) { 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; boolean LOCAL; //SUFFICIENT DRY RUN????LOGIC VERIFIED FOR ALL TEST CASES??? void solve(){ int t=scn.nextInt(); while(t-->0) { 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) { 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); } } // Use this instead of Arrays.sort() on an array of ints. Arrays.sort() is n^2 // worst case since it uses a version of quicksort. Although this would never // actually show up in the real world, in codeforces, people can hack, so // this is needed. } ","import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public final class C { public static void main(String[] args) { final FastScanner fs = new FastScanner(); final int t = fs.nextInt(); for (int test = 0; test < t; test++) { final int n = fs.nextInt(); final int[] arr = fs.nextIntArray(n); long evenSum = arr[0]; long oddSum = arr[1]; int minEven = arr[0]; int minOdd = arr[1]; long res = (long) n * arr[0] + (long) n * arr[1]; for (int i = 2; i < n; i++) { if (i % 2 == 0) { minEven = Math.min(minEven, arr[i]); evenSum += arr[i]; final long eC = n - i / 2 - 1; final long oC = n - i / 2; final long curr = evenSum + eC * minEven + oddSum + oC * minOdd; res = Math.min(res, curr); } else { minOdd = Math.min(minOdd, arr[i]); oddSum += arr[i]; final long c = n - i / 2 - 1; final long curr = oddSum + c * minOdd + evenSum + c * minEven; res = Math.min(res, curr); } } System.out.println(res); } } static final class Utils { private Utils() {} } } ",0 3d88acb8,b6023b26,"import java.util.*; import java.io.*; public class D { public static void main(String[] args) { FastScanner sc = new FastScanner(); int T = sc.nextInt(); while(T-->0) { int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } 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; } } ","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""); } } //Debugging Functions Starts //Debugging Functions END //---------------------- //IO FUNCTIONS STARTS static int i() { return in.nextInt(); } static long[] inputLong(int N) { long A[]=new long[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 ",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;i0){ 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); } } ",0 0588b869,9028caf7,"import java.util.*; import java.io.*; public class Solution { static final long mod=(long)1e9+7; /*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; } }*/ 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]; } ","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; } ",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(); } } ","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""); } } ",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;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); } // Use this to input code since it is faster than a Scanner } ","import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out = new PrintWriter(System.out); /****** CODE STARTS HERE *****/ //-------------------------------------------------------------------------------------------------------- int t = fs.nextInt(); while(t-->0) { 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 ***** //---------------------------------------------------------------------------------------------------------------- //----------- FastScanner class for faster input--------------------------- } ",1 80881cae,a7e7f371,"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); } // 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); 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> 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 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; } } // static long modInverse(long a, long m) // { // long g = gcd(a, m); // // return power(a, m - 2, m); // // } static PrintWriter out=new PrintWriter(System.out); } ",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(); } static final long mod = 1000000007; @SuppressWarnings(""FieldCanBeLocal"") private static Reader in; private static PrintWriter out; } ","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); } } } ",0 26e699de,e99c14b9,"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 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,"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 int i() { return sc.nextInt(); } static String s() { return sc.next(); } 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 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 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(); } } ",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 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; } } ",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()); } 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 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=Math.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{ static final Random random = new Random(); private void sort(int A[]) { int n = A.length; 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 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; } } ","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); } } } ","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; } } ",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 { 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 int mod = (int) (1e9 + 7); 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 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]); } } ",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 boolean prime[] = new boolean[2000009]; static int fac[] = new int[2000009]; } class twoval implements Comparable{ long myArray; int b; twoval(long aasjd,int r){ this.myArray = aasjd; this.b = r; } } ","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; } } } ",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 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; } } // 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 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 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; // } // } // } } ","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; } } } ",0 0ea5a836,3c74c140,"import java.util.*; import java.io.*; public class Solution { //static final long mod=(long)1e9+7; static final long mod=998244353L; /*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; } }*/ 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(); } } ","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; } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); 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) { 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); } } } } ","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,"import java.util.*; import java.io.*; public class Main{ //Fast IO class static long MOD=(long)1e9+7; //debug 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); } } ","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 static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static long mod = 1000000007; static int parent[]; static int rank[]; 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; // } // // } // } // } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); Clock clock = new Clock(); clock.start(); int t1=sc.nextInt(); for (int t = 0; 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{ 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]); } } } ",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 */ // 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(); } } ","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 final Random random=new Random(); } ",0 127af3a7,99ccaa44,"import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { notSitting(scanner); } } private static void notSitting(Scanner scanner) { int n = scanner.nextInt(); int[] num = new int[n - 1]; int[] ct = new int[n]; boolean[] visited = new boolean[n]; List[] 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;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; } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; 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; } void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i 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(); } } ","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[]; 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[] 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; } } } ","import java.util.*; import java.lang.*; import java.io.*; public class Template { // 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; } } 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]); } } } } ",0 856a8eda,c354b74f,"import java.io.*; import java.util.*; public class three{ public static class Pair implements Comparable{ int min; int idx; } public static void main(String[] args) throws Exception { MyScanner scn = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); /* int n = sc.nextInt(); // read input as integer long k = sc.nextLong(); // read input as long double d = sc.nextDouble(); // read input as double String str = sc.next(); // read input as String String s = sc.nextLine(); // read whole line as String int result = 3*n; out.println(result); // print via PrintWriter */ //The Code Starts here int t = scn.nextInt(); while(t-- > 0){ int n = scn.nextInt(); int m = scn.nextInt(); int x = scn.nextInt(); int arr[] = scn.nextIntArray(n); PriorityQueue pq = new PriorityQueue<>(); System.out.println(""YES""); for(int i=0;i { // long u; // long v; // public Pair(long u, long v) { // this.u = u; // this.v = v; // } // public int hashCode() { // int hu = (int) (u ^ (u >>> 32)); // int hv = (int) (v ^ (v >>> 32)); // return 31 * hu + hv; // } // public boolean equals(Object o) { // Pair other = (Pair) o; // return u == other.u && v == other.v; // } // public int compareTo(Pair other) { // return Long.compare(u, other.u) != 0 ? Long.compare(u, other.u) : Long.compare(v, other.v); // } // public String toString() { // return ""[u="" + u + "", v="" + v + ""]""; // } // } //-------------------------------------------------------- } ","import java.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 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 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 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 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; } } } ",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> { } 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; } } 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 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 { 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 } static int mod =(int)(1e9+7); static long mod(long x) { return ((x % mod + mod) % mod); } static long add(long x, long y) { return mod(mod(x) + mod(y)); } //Fast Power(logN) static void printArray(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + "" ""); } } } ","import java.io.*; import java.lang.*; import java.util.*; public class c { public static int[] ia(int n) { int ar[]=new int[n]; return ar; } static class pair implements Comparable{ int ht; int id; pair(int ht, int id) { this.ht=ht; this.id=id; } } 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); } } ",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(); } } ","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); } } } ",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 } ","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)); } } 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(); } // ch.charAt(i)+"""" converts into a char sequence // modular exponentiation /* * 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); } } */ // counts the set(1) bit of a number //tells whether a particular index has which bit of a number //use collections.swap for swapping // return the hash value of a string } ","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; } } } ",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 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 println(Object o) { System.out.println(o); } } ",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]); } } } ",1 1480a727,a7d1ce58,"import java.util.*; import java.io.*; public class Main { 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; } } ","import java.util.*; import java.io.*; public class AiseHi { static Scanner sc = new Scanner(System.in); public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); z : while(t-->0) { 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 boolean check(long n,long val) { // if(n == 0) return true; // // while(val<=n) { //// if(check(n-val,)) // } // } // static int ceil(int a,int b) { // return a/b + (a%b==0?0:1); // } // static int fac[] = new int[2000009]; // static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } } ",1 3bd7275e,8635bc10,"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); Arrays.sort(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); } } ","import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class C{ // static int N = 200010; static int n; static long k; static long[] a; static long[] s; // 求出a排序之后的前缀和 public static void main(String[] args){ // Scanner sc = new Scanner(System.in); FastScanner sc = new FastScanner(); int T = sc.nextInt(); while(T-- > 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; } } ",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 HashMap>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; } } ","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 } /* 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=0; i--) forced[i]=Math.min(forced[i], forced[i+1]+1); for (int i=0; i=0;i--) forced[i]=Math.min(forced[i], forced[i+1]+1); for (int i=0;i> dp= new HashMap<>(); // Calculating SPF (Smallest Prime Factor) for every // number till MAXN. // Time Complexity : O(nloglogn) // A O(log n) function returning primefactorization // by dividing by smallest prime factor at every step // function to find first index >= x // function to find last index <= y // function to count elements within given range 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]); } } // fast input reader class; } ","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; } } 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; } } 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) { 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]; } ","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) // A O(log n) function returning primefactorization // by dividing by smallest prime factor at every step // function to find first index >= x // function to find last index <= y // function to count elements within given range 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]); } } // fast input reader class; } ",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; } 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; } 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; } 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; } 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---------- } ","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 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); } } } ",0 3368f340,d8a171a3,"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;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); } ","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(); } } ","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[]; 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 { } static class myarr { int val,ii; myarr(int val,int ii) { this.val=val; this.ii=ii; } } static class mycom implements Comparator { } } ","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; } } } ",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; } } ","import java.io.*; import java.util.*; public class C { static int n; public static void main (String[] args) throws IOException { FastReader io = new FastReader(); int t = io.nextInt(); for (int ii=0; ii total) { ret++; } else { return ret; } } return ret; } static class Comp implements Comparator { char c; public Comp (char c) { this.c = c; } } } ",0 49e94e7e,d7a8434f,"import java.io.BufferedReader; import java.io.IOException; import java.lang.*; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.File; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class Main { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; //Count the number of coprime's upto N // Returns nCr % p using Fermat's little theorem. //Modular Operations for Addition and Multiplication. /* * * >= <= 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 void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int tt = sc.nextInt(); while(tt-- > 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); } } } ",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""); } } } } ","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 { 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(); } } ",0 953ac418,ba9b94f7,"import java.io.*; import java.lang.*; import java.util.*; public class E1578 { 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 q = Integer.parseInt(st.nextToken()); for(; q > 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); } } ","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; } } } ",0 07038b12,a18cb2c1,"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 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 primes=new ArrayList<>(); static ArrayList[] f1=new ArrayList[100001]; } ","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(); } } ",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(); } } */ } ",0 ac121776,eb6cfca7,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Solution { public static void main(String[] args) { MyScanner sc = new MyScanner(); int t = sc.nextInt(); while (t-- != 0) { int n = sc.nextInt(); long a[] = new long[n]; for(int i=0;i 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; } } } ","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; } } 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(); 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(); } } ",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 Pair[] pairs; ArrayList> 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; } } } ","import java.io.*; import java.util.*; public class Main { static class Task { int NN = 200005; int MOD = 1000000007; int INF = 2000000000; long INFINITY = 2000000000000000000L; int [] a; int [] b; List [] 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 Integer getInt(BufferedReader br) throws Exception { return Integer.parseInt(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,"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 int i() { return sc.nextInt(); } static String s() { return sc.next(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i0) { 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 = Math.abs(ss-cff) + arr[child].x; val2 = Math.abs(ss-css) + arr[child].y; if(val1 > val2) { arr[node].y+=val1; } else { arr[node].y+=val2; } } } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= 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 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; // } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////// } ","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 L[],R[],dp[][]; 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; i(); } } //Debugging Functions Starts //Debugging Functions END //---------------------- //IO FUNCTIONS STARTS static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } //IO FUNCTIONS END } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader ",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 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 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(); } /**************************************************************************************** *****************************************************************************************/ public int ni(){ return sc.nextInt(); }public char[] rl(){ return sc.nextLine().toCharArray(); } public void pl(Object s){ out.println(s); } public String next(){ return sc.next(); } void yes(){ pl(""YES""); }void no(){ pl(""NO""); } } ","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 { 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(); } } ",0 1dab88fb,f59d9b6e,"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.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(available[] 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 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""); } } ","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(); } } ",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 // } ",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> { } 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; } } } ",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; } } static class Block implements Comparable{ int i, w; public Block(int a, int b) { i=a; w=b; } } 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(); } } } ","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; } } ",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; } } ","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; } } ",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 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); } } } ","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; } } } ",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 void pn(Object o) { System.out.println(o); } static long[] takeLong(int n, FastScanner fs) { long[] arr=new long[n]; for(int i=0;i map; //static StringBuffer sb=new StringBuffer(""""); //static HashMap map; static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) { // StringBuffer sb=new StringBuffer(""""); int ttt=1; ttt =i(); outer :while (ttt-- > 0) { int n=i(); long A[]=inputL(n); long B[]=inputL(n); long C[]=new long[n]; for(int i=0;i=0;i--) { if(C[i]>min) { continue; } if(A[i]= 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(); } } } ","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(); } } } ",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 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; } } ",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); } } ","import java.io.*; import java.util.*; public class Main { static long mod = 1000000007; 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(); } } ",1 d1cd194e,dbffab11,"import java.util.*; import java.lang.*; import java.io.*; public class Template { // 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; } } 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(); } } } ","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{ } static class Node{ int sum; int build; Node(int sum ,int build) { this.sum=sum; this.build=build; } } } ",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; 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; static class Pair implements Comparable { long x; int y; Pair(long x, int y) { this.x = x; this.y = y; } /* @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; } */ } ///////////////////////////////////////////////////////////////////////////////////////////////////////// //custom multiset (replace with HashMap if needed) // compress Big value to Time Limit // Fast Writer // Fast Inputs } ","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; 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; static class Pair implements Comparable { long x; int y; Pair(long x, int y) { this.x = x; this.y = y; } /* @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; } */ } ///////////////////////////////////////////////////////////////////////////////////////////////////////// //custom multiset (replace with HashMap if needed) // compress Big value to Time Limit // Fast Writer // Fast Inputs } ",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; } } } ","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 pairr implements Comparable{ Integer value; Integer index; public pairr(Integer value, Integer index) { this.value = value; this.index = index; } } // Prints all prime numbers smaller than '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 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""); } // Function to precompute inverse of factorials // Function to calculate factorial of 1 to 200001 // Function to return nCr % p in O(1) time } ","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 { 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); } } ",0 0f14b12d,7d12d33c,"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 ; static class Pair implements Comparable { String x; int y; public Pair (String x, int y) { this.x = x; this.y = y; } } } ","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 ; static class Pair implements Comparable { String x; int y; public Pair (String x, int y) { this.x = x; this.y = y; } } } ",1 80881cae,ad4c7a20,"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); } // 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 protected Pair clone() throws CloneNotSupportedException { return new Pair(this.x,this.y); } } // Function to precompute inverse of factorials // Function to calculate factorial of 1 to 200001 // Function to return nCr % p in O(1) time } ",1 1ea771ea,ee270b2a,"import java.io.*; import java.util.*; public class CODECHEF { static long MOD=1000000000; 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); } } } ","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 ) { solve(); } System.out.println(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; i0){ 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 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; } } 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(); } 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 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(); } } ",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=0; i--) { ans[i]=Math.min(ans[i],ans[i+1]+1); } 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); } } } ",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; } 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; } 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; } // SIMPLE POWER FUNCTION=> } ","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 final Random random=new Random(); } ",0 3cf63146,bdfe8110,"import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.time.LocalDateTime; import java.util.*; public class B { static int tree[]; static int up[]; static int n; static int mod =1000000007; public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); int t=fs.nextInt(); outer: while(t-->0) { n=fs.nextInt(); int k=fs.nextInt(); int ac[]=fs.readArray(k); int temp[]=fs.readArray(k); int arr[]=new int[n]; Arrays.fill(arr, Integer.MAX_VALUE/2); for(int i=0;i=0;i--) { ans[i]=Math.min(Math.min(arr[i],left[i]),ans[i+1]+1); } // for(int i=0;i=0; i--) forced[i]=Math.min(forced[i], forced[i+1]+1); 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 print(int[] arr, PrintWriter out) { //for debugging only for (int x : arr) out.print(x + "" ""); out.println(); } // 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 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) //with mod //without mod //prime sieve //number of divisors //now adding next permutation function to java hehe //finding the value of NCR in O(RlogN) time and O(1) space //is vowel function //to sort the array with better method //sort long //for calculating binomialCoeff //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; } } //Triplet with int int int //Shortcut function //let's make one for calculating lcm basically //int version for gcd //long version for gcd //for ncr calculator(ignore this code) //swapping two elements in an array //for char array //reversing an array //SOME EXTRA DOPE FUNCTIONS //O(n) every single time remember that // 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) //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- //-------------------------------------------------------- } ",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 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(); } } ","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; // } } //------------------------------------------------------------------------------------------------------------------------------------------------ //for f/inding minimum in range //// for sum // // ----------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------------------- //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; } } //----------------------------------------------------------------------------------------------------------------------------------------------------------- } //------------------------------------------------------------------------------------------------------------------------------------------------------------ //----------------------------------------------------------------------------------------------------------------------------------------------------------- ",0 aa4b840f,e99c14b9,"import java.io.*; import java.util.*; public class Contest1615C { 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 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 FastReader scan; /*-------- I/O using short named function ---------*/ public static int ni() { return scan.nextInt(); } /*-------- for output of an array ---------------------*/ static void lPA(long 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(); } /*------------ for taking input faster ----------------*/ // Method to check if x is power of 2 //Method to return lcm of two numbers //Method to count digit of a number static final Random random = new Random(); //Method for sorting //Method for checking if a number is prime or not 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; } } 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); } } } ",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,"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 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; } } 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 Util{ private static Random random = new Random(); static long[] fact; public static int random(int min, int max) { return random.nextInt(max-min+1)+min; } 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); } } } ","import java.util.*; import java.io.*; public class Solution { static Scanner scn = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); public static void main(String[] HastaLaVistaLa) { int t = scn.nextInt(); while(t-- > 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; } } } ",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