filename_1
stringlengths
8
15
filename_2
stringlengths
8
12
code_1
stringlengths
591
24.4k
code_2
stringlengths
591
35.2k
labels
int64
0
1
notes
stringclasses
2 values
8f30bfc3
af1b152e
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.InputMismatchException; import java.util.List; import java.util.StringJoiner; public class Solution { private static List<Long> chairs; private static List<Long> folks; private static Long[] data; private static Long[][] cache; public static void main(String[] args) { Print print = new Print(); Scan scan = new Scan(); int n = scan.scanInt(); data = scan.scan1dLongArray(); chairs = new ArrayList<>(); folks = new ArrayList<>(); cache = new Long[n][n]; for (int i = 0; i < n; i++) { if (data[i] == 0) { chairs.add((long) i); } else { folks.add((long) i); } } print.printLine(Long.toString(solve(folks, chairs, 0, 0))); print.close(); } private static long solve(List<Long> folks, List<Long> chairs, int i, int j) { if (i == folks.size()) { return 0; } if (j == chairs.size()) { return Integer.MAX_VALUE; } if (cache[i][j] != null) { return cache[i][j]; } return cache[i][j] = Math .min(Math.abs(folks.get(i) - chairs.get(j)) + solve(folks, chairs, i + 1, j + 1), solve(folks, chairs, i, j + 1)); } static class Scan { private byte[] buf = new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in = System.in; } public int scan() { if (total < 0) { throw new InputMismatchException(); } if (index >= total) { index = 0; try { total = in.read(buf); } catch (IOException ignored) { } if (total <= 0) { return -1; } } return buf[index++]; } public int scanInt() { int integer = 0; int n = scan(); while (isWhiteSpace(n)) { n = scan(); } int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } else { throw new InputMismatchException(); } } return neg * integer; } public double scanDouble() { double doub = 0; int n = scan(); while (isWhiteSpace(n)) { n = scan(); } int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n) && n != '.') { if (n >= '0' && n <= '9') { doub *= 10; doub += n - '0'; n = scan(); } else { throw new InputMismatchException(); } } if (n == '.') { n = scan(); double temp = 1; while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { temp /= 10; doub += (n - '0') * temp; n = scan(); } else { throw new InputMismatchException(); } } } return doub * neg; } public Integer[] scan1dIntArray() { String[] s = this.scanString().split(" "); Integer[] arr = new Integer[s.length]; for (int i = 0; i < s.length; i++) { arr[i] = Integer.parseInt(s[i]); } return arr; } public Integer[][] scan2dIntArray(int n, int m) { Integer[][] arr = new Integer[n][m]; for (int i = 0; i < n; i++) { String[] s = this.scanString().split(" "); for (int j = 0; j < m; j++) { arr[i][j] = Integer.parseInt(s[j]); } } return arr; } public String[] scan1dStringArray() { return this.scanString().split(" "); } public String[][] scan2dStringArray(int n, int m) { String[][] arr = new String[n][m]; for (int i = 0; i < n; i++) { String[] s = this.scanString().split(" "); for (int j = 0; j < m; j++) { arr[i][j] = s[j]; } } return arr; } public char[] scan1dCharArray() { return this.scanString().toCharArray(); } public char[][] scan2dCharArray(int n, int m) { char[][] arr = new char[n][m]; for (int i = 0; i < n; i++) { char[] s = this.scanString().toCharArray(); for (int j = 0; j < m; j++) { arr[i][j] = s[j]; } } return arr; } public Long[] scan1dLongArray() { String[] s = this.scanString().split(" "); Long[] arr = new Long[s.length]; for (int i = 0; i < s.length; i++) { arr[i] = Long.parseLong(s[i]); } return arr; } public Long[][] scan2dLongArray(int n, int m) { Long[][] arr = new Long[n][m]; for (int i = 0; i < n; i++) { String[] s = this.scanString().split(" "); for (int j = 0; j < m; j++) { arr[i][j] = Long.parseLong(s[j]); } } return arr; } public String scanString() { StringBuilder sb = new StringBuilder(); int n = scan(); while (isWhiteSpace(n)) { n = scan(); } while (!isWhiteSpace(n)) { sb.append((char) n); n = scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if (n == '\n' || n == '\r' || n == '\t' || n == -1) { return true; } return false; } } static class Print { private final BufferedWriter bw; public Print() { bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(String str) { try { bw.append(str); } catch (IOException ignored) { } } public void printLine(Integer[] arr) { StringJoiner sj = new StringJoiner(" "); for (Integer x : arr) { sj.add(Integer.toString(x)); } printLine(sj.toString()); } public void printLine(Integer[][] arr) { for (Integer[] x : arr) { StringJoiner sj = new StringJoiner(" "); for (Integer y : x) { sj.add(Integer.toString(y)); } printLine(sj.toString()); } } public void printLine(String[] arr) { StringJoiner sj = new StringJoiner(" "); for (String x : arr) { sj.add(x); } printLine(sj.toString()); } public void printLine(String[][] arr) { for (String[] x : arr) { StringJoiner sj = new StringJoiner(" "); for (String y : x) { sj.add(y); } printLine(sj.toString()); } } public void printLine(char[] arr) { StringJoiner sj = new StringJoiner(" "); for (char x : arr) { sj.add(Character.toString(x)); } printLine(sj.toString()); } public void printLine(char[][] arr) { for (char[] x : arr) { StringJoiner sj = new StringJoiner(" "); for (char y : x) { sj.add(Character.toString(y)); } printLine(sj.toString()); } } public void printLine(Long[] arr) { StringJoiner sj = new StringJoiner(" "); for (Long x : arr) { sj.add(Long.toString(x)); } printLine(sj.toString()); } public void printLine(Long[][] arr) { for (Long[] x : arr) { StringJoiner sj = new StringJoiner(" "); for (Long y : x) { sj.add(Long.toString(y)); } printLine(sj.toString()); } } public void printLine(String str) { print(str); try { bw.append("\n"); } catch (IOException ignored) { } } public void close() { try { bw.close(); } catch (IOException ignored) { } } } }
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.InputMismatchException; import java.util.List; import java.util.StringJoiner; public class D { private static List<Long> chairs; private static List<Long> folks; private static Long[] data; private static Long[][] cache; public static void main(String[] args) { Print print = new Print(); Scan scan = new Scan(); int n = scan.scanInt(); data = scan.scan1dLongArray(); chairs = new ArrayList<>(); folks = new ArrayList<>(); cache = new Long[n][n]; for (int i = 0; i < n; i++) { if (data[i] == 0) { chairs.add((long) i); } else { folks.add((long) i); } } print.printLine(Long.toString(solve(folks, chairs, 0, 0))); print.close(); } private static long solve(List<Long> folks, List<Long> chairs, int i, int j) { if (i == folks.size()) { return 0; } if (j == chairs.size()) { return Integer.MAX_VALUE; } if (cache[i][j] != null) { return cache[i][j]; } return cache[i][j] = Math .min(Math.abs(folks.get(i) - chairs.get(j)) + solve(folks, chairs, i + 1, j + 1), solve(folks, chairs, i, j + 1)); } static class Scan { private byte[] buf = new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in = System.in; } public int scan() { if (total < 0) { throw new InputMismatchException(); } if (index >= total) { index = 0; try { total = in.read(buf); } catch (IOException ignored) { } if (total <= 0) { return -1; } } return buf[index++]; } public int scanInt() { int integer = 0; int n = scan(); while (isWhiteSpace(n)) { n = scan(); } int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } else { throw new InputMismatchException(); } } return neg * integer; } public double scanDouble() { double doub = 0; int n = scan(); while (isWhiteSpace(n)) { n = scan(); } int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n) && n != '.') { if (n >= '0' && n <= '9') { doub *= 10; doub += n - '0'; n = scan(); } else { throw new InputMismatchException(); } } if (n == '.') { n = scan(); double temp = 1; while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { temp /= 10; doub += (n - '0') * temp; n = scan(); } else { throw new InputMismatchException(); } } } return doub * neg; } public Integer[] scan1dIntArray() { String[] s = this.scanString().split(" "); Integer[] arr = new Integer[s.length]; for (int i = 0; i < s.length; i++) { arr[i] = Integer.parseInt(s[i]); } return arr; } public Integer[][] scan2dIntArray(int n, int m) { Integer[][] arr = new Integer[n][m]; for (int i = 0; i < n; i++) { String[] s = this.scanString().split(" "); for (int j = 0; j < m; j++) { arr[i][j] = Integer.parseInt(s[j]); } } return arr; } public String[] scan1dStringArray() { return this.scanString().split(" "); } public String[][] scan2dStringArray(int n, int m) { String[][] arr = new String[n][m]; for (int i = 0; i < n; i++) { String[] s = this.scanString().split(" "); for (int j = 0; j < m; j++) { arr[i][j] = s[j]; } } return arr; } public char[] scan1dCharArray() { return this.scanString().toCharArray(); } public char[][] scan2dCharArray(int n, int m) { char[][] arr = new char[n][m]; for (int i = 0; i < n; i++) { char[] s = this.scanString().toCharArray(); for (int j = 0; j < m; j++) { arr[i][j] = s[j]; } } return arr; } public Long[] scan1dLongArray() { String[] s = this.scanString().split(" "); Long[] arr = new Long[s.length]; for (int i = 0; i < s.length; i++) { arr[i] = Long.parseLong(s[i]); } return arr; } public Long[][] scan2dLongArray(int n, int m) { Long[][] arr = new Long[n][m]; for (int i = 0; i < n; i++) { String[] s = this.scanString().split(" "); for (int j = 0; j < m; j++) { arr[i][j] = Long.parseLong(s[j]); } } return arr; } public String scanString() { StringBuilder sb = new StringBuilder(); int n = scan(); while (isWhiteSpace(n)) { n = scan(); } while (!isWhiteSpace(n)) { sb.append((char) n); n = scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if (n == '\n' || n == '\r' || n == '\t' || n == -1) { return true; } return false; } } static class Print { private final BufferedWriter bw; public Print() { bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(String str) { try { bw.append(str); } catch (IOException ignored) { } } public void printLine(Integer[] arr) { StringJoiner sj = new StringJoiner(" "); for (Integer x : arr) { sj.add(Integer.toString(x)); } printLine(sj.toString()); } public void printLine(Integer[][] arr) { for (Integer[] x : arr) { StringJoiner sj = new StringJoiner(" "); for (Integer y : x) { sj.add(Integer.toString(y)); } printLine(sj.toString()); } } public void printLine(String[] arr) { StringJoiner sj = new StringJoiner(" "); for (String x : arr) { sj.add(x); } printLine(sj.toString()); } public void printLine(String[][] arr) { for (String[] x : arr) { StringJoiner sj = new StringJoiner(" "); for (String y : x) { sj.add(y); } printLine(sj.toString()); } } public void printLine(char[] arr) { StringJoiner sj = new StringJoiner(" "); for (char x : arr) { sj.add(Character.toString(x)); } printLine(sj.toString()); } public void printLine(char[][] arr) { for (char[] x : arr) { StringJoiner sj = new StringJoiner(" "); for (char y : x) { sj.add(Character.toString(y)); } printLine(sj.toString()); } } public void printLine(Long[] arr) { StringJoiner sj = new StringJoiner(" "); for (Long x : arr) { sj.add(Long.toString(x)); } printLine(sj.toString()); } public void printLine(Long[][] arr) { for (Long[] x : arr) { StringJoiner sj = new StringJoiner(" "); for (Long y : x) { sj.add(Long.toString(y)); } printLine(sj.toString()); } } public void printLine(String str) { print(str); try { bw.append("\n"); } catch (IOException ignored) { } } public void close() { try { bw.close(); } catch (IOException ignored) { } } } }
1
Plagiarised
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<Integer> oc=new ArrayList<>(); ArrayList<Integer> em=new ArrayList<>(); res=Long.MAX_VALUE; for(int i=0;i<n;i++) { int v=fr.nextInt(); if(v==1) oc.add(i); else em.add(i); } Collections.sort(oc); Collections.sort(em); long dp[][]=new long[5001][5001]; for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[i].length;j++) { dp[i][j]=-1; } } System.out.println(getMin(oc,em,0,0,dp)); } public static long getMin(ArrayList<Integer> oc,ArrayList<Integer> 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<req) return Integer.MAX_VALUE; if(dp[idx][j]!=-1) return dp[idx][j]; long ch1=getMin(oc,em,idx,j+1,dp); long ch2=getMin(oc,em,idx+1,j+1,dp)+Math.abs(em.get(j)-oc.get(idx)); return dp[idx][j]=Math.min(ch1,ch2); } public static String lcs(String a,String b) { int dp[][]=new int[a.length()+1][b.length()+1]; for(int i=0;i<=a.length();i++) { for(int j=0;j<=b.length();j++) { if(i==0||j==0) dp[i][j]=0; } } for(int i=1;i<=a.length();i++) { for(int j=1;j<=b.length();j++) { if(a.charAt(i-1)==b.charAt(j-1)) dp[i][j]=1+dp[i-1][j-1]; else dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]); } } int i=a.length(); int j=b.length(); String lcs=""; while(i>0&&j>0) { if(a.charAt(i-1)==b.charAt(j-1)) { lcs=a.charAt(i-1)+lcs; i--; j--; } else { if(dp[i-1][j]>dp[i][j-1]) i--; else j--; } } return lcs; } public static long facto(long n) { if(n==1||n==0) return 1; return n*facto(n-1); } public static long gcd(long n1,long n2) { if (n2 == 0) { return n1; } return gcd(n2, n1 % n2); } public static boolean isPali(String s) { int i=0; int j=s.length()-1; while(i<=j) { if(s.charAt(i)!=s.charAt(j)) return false; i++; j--; } return true; } public static String reverse(String s) { String res=""; for(int i=0;i<s.length();i++) { res+=s.charAt(i); } return res; } public static int bsearch(long suf[],long val) { int i=0; int j=suf.length-1; while(i<=j) { int mid=(i+j)/2; if(suf[mid]==val) return mid; else if(suf[mid]<val) j=mid-1; else i=mid+1; } return -1; } public static int[] getFreq(String s) { int a[]=new int[26]; for(int i=0;i<s.length();i++) { a[s.charAt(i)-'a']++; } return a; } public static boolean isPrime(int n) { for(int i=2;(i*i)<=n;i++) { if(n%i==0) return false; } return true; } } class Pair{ long i; long j; Pair(long num,long freq){ this.i=num; this.j=freq; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class taskd { public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); taskd sol = new taskd(); sol.solve(in, out); out.flush(); } void solve(FastScanner in, PrintWriter out) { int n = in.nextInt(); ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); for (int i = 0; i < n; i++) { int x = in.nextInt(); if (x == 1) { a.add(i); } else { b.add(i); } } long dp[][] = new long[a.size() + 5][b.size() + 5]; for (int i = a.size()-1; i >= 0; i--) { dp[i][b.size()] = Integer.MAX_VALUE; for (int j = b.size()-1; j >= 0; j--) { dp[i][j] = dp[i][j + 1]; dp[i][j] = Math.min(dp[i][j], Math.abs(a.get(i) - b.get(j)) + dp[i + 1][j + 1]); } } out.println(dp[0][0]); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
0
Non-plagiarised
72d4d693
ad2a1ae2
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; public class Codechef { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br=new BufferedReader(new FileReader("input.txt")); PrintStream out= new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } // Catch block to handle the exceptions catch (Exception e) { br=new BufferedReader(new InputStreamReader(System.in)); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int computeXOR(int n) { // If n is a multiple of 4 if (n % 4 == 0) return n; // If n%4 gives remainder 1 if (n % 4 == 1) return 1; // If n%4 gives remainder 2 if (n % 4 == 2) return n + 1; // If n%4 gives remainder 3 return 0; } public static void main (String[] args) throws java.lang.Exception { try{ FastReader sc=new FastReader(); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(System.out)); int t=sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int k[] = new int[n]; for(int i=0;i<n;i++) { k[i] = sc.nextInt(); } int h[] = new int[n]; for(int i=0;i<n;i++) { h[i] = sc.nextInt(); } long tail = k[n-1]; long span = h[n-1]; long ans = 0; for(int i=n-2;i>=0;i--) { if((tail-span)+1>k[i]) { ans+=(span*(span+1))/2; tail=k[i]; span=h[i]; } else if((tail-span)+1<=((k[i]-h[i])+1)) { continue; } else { span+=(((tail-span)+1) - ((k[i]-h[i])+1)); } } ans+=(span*(span+1))/2; System.out.println(ans); } }catch(Exception e){ return; } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.StringTokenizer; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; }} public static void main(String[] args) { FastReader sc = new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int r[]=new int[n]; int l[]=new int[n]; for(int i=0;i<n;i++) { r[i]=sc.nextInt(); } for(int i=0;i<n;i++) l[i]=r[i]-sc.nextInt()+1; long ans=0,min=l[n-1],max=r[n-1]; for(int i=n-2;i>=0;i--) { if(r[i]>=min) min=Math.min(min,l[i]); else { ans+=(max-min+1)*(max-min+2)/2; max=r[i]; min=l[i]; } } ans+=(max-min+1)*(max-min+2)/2; System.out.println(ans); } } }
0
Non-plagiarised
26e699de
90dc2b20
//package Task1; import java.util.Scanner; public class Menorah { static int MOD9= 1000000000; public static void main(String[] args){ Scanner sc= new Scanner(System.in); int numberTest=sc.nextInt(); while(numberTest-->0){ int n=sc.nextInt(); char[] s=new char[n+5]; char[] t=new char[n+5]; String ss=sc.next(); String tt=sc.next(); s=ss.toCharArray(); t=tt.toCharArray(); int cntax = 0, cntbx = 0, same = 0; int ans=MOD9; for(int i=0; i<n; i++){ if(s[i]=='1')cntax++; if(t[i]=='1')cntbx++; if(s[i]==t[i])same++; } if(same==n){ System.out.println(0); continue; } else if (cntax==0){ System.out.println(-1); continue; } if(cntax==cntbx){ ans=Math.min(ans,n-same); } if(n-cntax+1==cntbx)ans=Math.min(ans,same); if(ans<MOD9) System.out.println(ans); else System.out.println(-1); } } }
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<n;i++){ if(ch1[i]=='0'){ if(ch2[i]=='0'){ c00+=1; } else{ c01+=1; } } else{ if(ch2[i]=='0'){ c10+=1; } else{ c11+=1; } } } int ans = -1; if((c11-c00)==1 || c10==c01){ int s1 = (int)1e7; int s2 = (int)1e7; if((c11-c00)==1){ s1 = c11+c00; } if(c10==c01) s2 = c10+c01; ans = Math.min(s1,s2); } out.println(ans); } out.close(); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
0
Non-plagiarised
480de7be
f4757480
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; i<n; i++){ String w= io.getWord(); int len= w.length(); // count letters for(int j=0; j<len; j++) ps[w.charAt(j)-'a'][i]++; // calculate diffs letter-!letter // e.g. a-!a = a-(a+b+c+d+e-a) = a-(len-a) = a + (a-len) = 2a-len for(int k=0; k<5; k++) ps[k][i]+= ps[k][i]-len; } //sort diffs for(int k=0; k<5; k++) mergeSort(ps[k]); //calculate prefix sums of diffs (until they're non-positive) //start from the end as sort is ascending //pick largest index out of 5 letter at which sum of diffs is positive int max= 0; for(int k=0; k<5; k++){ if(ps[k][n-1]<=0) continue; if(max==0) max= 1; for(int i=2; i<=n; i++){ ps[k][n-i]+= ps[k][n-i+1]; if(ps[k][n-i]<=0) break; if(i>max) 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<n-1; l+=2*sz){ int m= Math.min(l + sz-1, n-1); int r= Math.min(l + 2*sz-1, n-1); int n1= m-l+1, n2= r-m; int L[] = new int[n1]; for (int i= 0; i<n1; i++) L[i]= arr[l+i]; int R[] = new int[n2]; for (int j= 0; j<n2; j++) R[j]= arr[m+1+j]; int i= 0, j= 0, k= l; for(;i<n1 && j<n2; k++){ if(L[i]<=R[j]){arr[k]= L[i]; i++;} else{arr[k] = R[j]; j++;} } for(;i<n1; i++, k++) arr[k]= L[i]; for(;j<n2; j++, k++) arr[k]= R[j]; } } } static class Kattio extends PrintWriter { private BufferedReader r; private String line, token; private StringTokenizer st; public Kattio(){this(System.in);} public Kattio(InputStream i){ super(new BufferedOutputStream(System.out)); r= new BufferedReader(new InputStreamReader(i)); } public Kattio(InputStream i, OutputStream o){ super(new BufferedOutputStream(o)); r= new BufferedReader(new InputStreamReader(i)); } public boolean isLineEnd(){ return !st.hasMoreTokens(); } public boolean hasMoreTokens(){ return peekToken()!=null; } public int getInt(){ return Integer.parseInt(nextToken()); } public double getDouble(){ return Double.parseDouble(nextToken()); } public long getLong(){ return Long.parseLong(nextToken()); } public String getWord(){ return nextToken(); } public String getLine(){ try{if(!st.hasMoreTokens()) return r.readLine(); }catch(IOException e){} return null; } private String peekToken(){ if(token==null) try { while(st==null || !st.hasMoreTokens()) { line= r.readLine(); if(line==null) return null; st= new StringTokenizer(line); } token= st.nextToken(); }catch(IOException e){} return token; } private String nextToken() { String ans= peekToken(); token= null; return ans; } } }
import java.util.*; 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; i<n; i++){ String w= io.getWord(); int len= w.length(); // count letters for(int j=0; j<len; j++) ps[w.charAt(j)-'a'][i]++; // calculate diffs letter-!letter // e.g. a-!a = a-(a+b+c+d+e-a) = a-(len-a) = a + (a-len) = 2a-len for(int k=0; k<5; k++) ps[k][i]+= ps[k][i]-len; } //sort diffs for(int k=0; k<5; k++) //mergeSort(ps[k]); Arrays.sort(ps[k]); //calculate prefix sums of diffs (until they're non-positive) //start from the end as sort is ascending //pick largest index out of 5 letter at which sum of diffs is positive int max= 0; for(int k=0; k<5; k++){ if(ps[k][n-1]<=0) continue; if(max==0) max= 1; for(int i=2; i<=n; i++){ ps[k][n-i]+= ps[k][n-i+1]; if(ps[k][n-i]<=0) break; if(i>max) 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<n-1; l+=2*sz){ int m= Math.min(l + sz-1, n-1); int r= Math.min(l + 2*sz-1, n-1); int n1= m-l+1, n2= r-m; int L[] = new int[n1]; for (int i= 0; i<n1; i++) L[i]= arr[l+i]; int R[] = new int[n2]; for (int j= 0; j<n2; j++) R[j]= arr[m+1+j]; int i= 0, j= 0, k= l; for(;i<n1 && j<n2; k++){ if(L[i]<=R[j]){arr[k]= L[i]; i++;} else{arr[k] = R[j]; j++;} } for(;i<n1; i++, k++) arr[k]= L[i]; for(;j<n2; j++, k++) arr[k]= R[j]; } } } static class Kattio extends PrintWriter { private BufferedReader r; private String line, token; private StringTokenizer st; public Kattio(){this(System.in);} public Kattio(InputStream i){ super(new BufferedOutputStream(System.out)); r= new BufferedReader(new InputStreamReader(i)); } public Kattio(InputStream i, OutputStream o){ super(new BufferedOutputStream(o)); r= new BufferedReader(new InputStreamReader(i)); } public boolean isLineEnd(){ return !st.hasMoreTokens(); } public boolean hasMoreTokens(){ return peekToken()!=null; } public int getInt(){ return Integer.parseInt(nextToken()); } public double getDouble(){ return Double.parseDouble(nextToken()); } public long getLong(){ return Long.parseLong(nextToken()); } public String getWord(){ return nextToken(); } public String getLine(){ try{if(!st.hasMoreTokens()) return r.readLine(); }catch(IOException e){} return null; } private String peekToken(){ if(token==null) try { while(st==null || !st.hasMoreTokens()) { line= r.readLine(); if(line==null) return null; st= new StringTokenizer(line); } token= st.nextToken(); }catch(IOException e){} return token; } private String nextToken() { String ans= peekToken(); token= null; return ans; } } }
1
Plagiarised
5d175166
d1391025
import java.io.*; import java.util.*; public class A { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static int[] res; static List<List<Integer>> graph; static boolean two = true; static HashMap<pr<Integer, Integer>, Integer> hm; static void solve() throws IOException { int n = sc.ni(); graph = new ArrayList<>(); res = new int[n-1]; hm = new HashMap<>(); for(int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } boolean f = false; for(int i = 0; i < n-1; i++) { int a = sc.ni(), b = sc.ni(); graph.get(a).add(b); graph.get(b).add(a); if(graph.get(a).size() > 2 || graph.get(b).size() > 2) f = true; hm.put(new pr<>(a, b), i); hm.put(new pr<>(b, a), i); } if(f) { w.p(-1); return; } int one = 0; for(int i = 0; i < n; i++) { if(graph.get(i).size() == 1) { one = i; break; } } dfs(one, -1); for(int i: res) { w.pr(i+" "); } w.pl(); } static void dfs(int at, int pt) { List<Integer> li = graph.get(at); if(pt != -1) { res[hm.get(new pr<>(at, pt))] = two?2:3; two = !two; } for(int to: li) { if(to == pt) continue; dfs(to, at); } } static class pr <T, V> { T a; V b; public pr(T a, V b) { this.a = a; this.b = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof pr)) return false; pr<?, ?> pr = (pr<?, ?>) o; return a.equals(pr.a) && b.equals(pr.b); } @Override public int hashCode() { return Objects.hash(a, b); } } }
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class C { static class scanner { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException {return Integer.parseInt(next());} static double nextDouble() throws IOException {return Double.parseDouble(next());} static long nextLong() throws IOException {return Long.parseLong(next());} } static long min(long a, long b) {return Math.min(a, b);} static long min(long a, long b, long c) {return min(a, min(b, c));} static int min(int a, int b) {return Math.min(a, b);} static int min(int a, int b, int c) {return min(a, min(b, c));} static long max(long a, long b) {return Math.max(a, b);} static long max(long a, long b, long c) {return max(a, max(b, c));} static int max(int a, int b) {return Math.max(a, b);} static int max(int a, int b, int c) {return max(a, max(b, c));} static int abs(int x) {return Math.abs(x);} static long abs(long x) {return Math.abs(x);} static long ceil(double x) {return (long) Math.ceil(x);} static long floor(double x) {return (long) Math.floor(x);} static int ceil(int x) {return (int) Math.ceil(x);} static int floor(int x) {return (int) Math.floor(x);} static double sqrt(double x) {return Math.sqrt(x);} static double cbrt(double x) {return Math.cbrt(x);} static long sqrt(long x) {return (long) Math.sqrt(x);} static long cbrt(long x) {return (long) Math.cbrt(x);} static int gcd(int a, int b) {if(b == 0)return a;return gcd(b, a % b);} static long gcd(long a, long b) {if(b == 0)return a;return gcd(b, a % b);} static double pow(double n, double power) {return Math.pow(n, power);} static long pow(long n, double power) {return (long) Math.pow(n, power);} static class Pair {int first, second;public Pair(int first, int second) {this.first = first;this.second = second;}} public static void main(String[] args) throws IOException { scanner.init(System.in); int t = 1; t = scanner.nextInt(); while (t-- > 0) { solve(); } } static void solve() throws IOException { int n = scanner.nextInt(); List<List<Pair>> tree = new ArrayList<>(); for (int i = 0; i < n; i++) { tree.add(new ArrayList<>()); } for (int i = 0; i < n-1; i++) { int u = scanner.nextInt()-1; int v = scanner.nextInt()-1; tree.get(u).add(new Pair(v, i)); tree.get(v).add(new Pair(u, i)); } int start = -1; for (int i = 0; i < n; i++) { if(tree.get(i).size() > 2) { System.out.println(-1); return; } else if(tree.get(i).size() == 1) { start = i; } } int[] res = new int[n-1]; Queue<Integer> q = new LinkedList<>(); q.add(start); int weight = 2, prev = -1; while (!q.isEmpty()) { int u = q.poll(); for(Pair v : tree.get(u)) { if(v.first != prev) { q.add(v.first); res[v.second] = weight; weight = 5 - weight; } } prev = u; } for(int i : res) { System.out.print(i + " "); } System.out.println(); } }
0
Non-plagiarised
5647d16e
ea09d197
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.*; /* -> 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<Integer>> list = new ArrayList<>(); int n = sc.nextInt(); for(int i = 0; i < n; i++){ list.add(new ArrayList<>()); } List<Pair> 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<String,Integer> map = new HashMap<>(); static void dfs(int curr, List<List<Integer>> list, int n, int par, int what){ List<Integer> 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<Integer>> 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<Integer> neighbours = list.get(curr); for(int e : neighbours){ if(e == par) continue; if(helper(e,list,n,curr)){ return true; } } return false; } /* Source: hu_tao Random stuff to try when stuck: - use bruteforcer - check for n = 1, n = 2, so on -if it's 2C then it's dp -for combo/probability problems, expand the given form we're interested in -make everything the same then build an answer (constructive, make everything 0 then do something) -something appears in parts of 2 --> model as graph -assume a greedy then try to show why it works -find way to simplify into one variable if multiple exist -treat it like fmc (note any passing thoughts/algo that could be used so you can revisit them) -find lower and upper bounds on answer -figure out what ur trying to find and isolate it -see what observations you have and come up with more continuations -work backwards (in constructive, go from the goal to the start) -turn into prefix/suffix sum argument (often works if problem revolves around adjacent array elements) -instead of solving for answer, try solving for complement (ex, find n-(min) instead of max) -draw something -simulate a process -dont implement something unless if ur fairly confident its correct -after 3 bad submissions move on to next problem if applicable -do something instead of nothing and stay organized -write stuff down Random stuff to check when wa: -if code is way too long/cancer then reassess -switched N/M -int overflow -switched variables -wrong MOD -hardcoded edge case incorrectly Random stuff to check when tle: -continue instead of break -condition in for/while loop bad Random stuff to check when rte: -switched N/M -long to int/int overflow -division by 0 -edge case for empty list/data structure/N=1 */ public static class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } public static void sort(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for (int x : arr) ls.add(x); Collections.sort(ls); for (int i = 0; i < arr.length; i++) arr[i] = ls.get(i); } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] sieve(int N) { boolean[] sieve = new boolean[N + 1]; for (int i = 2; i <= N; i++) sieve[i] = true; for (int i = 2; i <= N; i++) { if (sieve[i]) { for (int j = 2 * i; j <= N; j += i) { sieve[j] = false; } } } return sieve; } public static long power(long x, long y, long p) { long res = 1L; x = x % p; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; } return res; } public static void print(int[] arr, PrintWriter out) { //for debugging only for (int x : arr) out.print(x + " "); out.println(); } public static int log2(int a){ return (int)(Math.log(a)/Math.log(2)); } public static long ceil(long x, long y){ return (x + 0l + y - 1) / y; } static class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] readInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] readLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] readDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } // For Input.txt and Output.txt // FileInputStream in = new FileInputStream("input.txt"); // FileOutputStream out = new FileOutputStream("output.txt"); // PrintWriter pw = new PrintWriter(out); // Scanner sc = new Scanner(in); }
//some updates in import stuff import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; public class C{ static int mod = (int) (Math.pow(10, 9)+7); static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 }; static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 }; static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 }; static final double eps = 1e-10; static List<Integer> 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<Integer, Integer> 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<Integer, ArrayList<Pair>> 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<Pair> 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<ArrayList<Integer>> 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<Integer, ArrayList<Pair>> fuck){ //we will add stuff here int curr = start; int[] visited = new int[V+1]; visited[curr] = 1; int index = 0; while(true){ int make = 0; boolean last = true; for(int edge : edges.get(curr)){ if(visited[edge] == 1){ continue; }else{ make = edge; last = false; break; } } if(last){ break; } //now we have found make, we make and add it in map fuck.putIfAbsent(curr, new ArrayList<>()); fuck.putIfAbsent(make, new ArrayList<>()); fuck.get(curr).add(new Pair(make, index %2 == 0 ? 2 : 3)); fuck.get(make).add(new Pair(curr, index %2 == 0 ? 2 : 3)); visited[make] = 1; curr = make; index++; } } } //DSU (path and rank optimised) public static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; Arrays.fill(rank, 1); Arrays.fill(parent,-1); this.n = n; } public int find(int curr){ if(parent[curr] == -1) return curr; //path compression optimisation return parent[curr] = find(parent[curr]); } public void union(int a, int b){ int s1 = find(a); int s2 = find(b); if(s1 != s2){ if(rank[s1] < rank[s2]){ parent[s1] = s2; rank[s2] += rank[s1]; }else{ parent[s2] = s1; rank[s1] += rank[s2]; } } } } //with mod public static long powerMOD(long x, long y) { long res = 1L; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0){ x %= mod; res %= mod; res = (res * x)%mod; } // y must be even now y = y >> 1; // y = y/2 x%= mod; x = (x * x)%mod; // Change x to x^2 } return res%mod; } //without mod public static long power(long x, long y) { long res = 1L; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0){ res = (res * x); } // y must be even now y = y >> 1; // y = y/ x = (x * x); } return res; } public static class segmentTree{ public long[] arr; public long[] tree; public long[] lazy; segmentTree(long[] array){ int n = array.length; arr = new long[n]; for(int i= 0; i < n; i++) arr[i] = array[i]; tree = new long[4*n + 1]; lazy = new long[4*n + 1]; } public void build(int[]arr, int s, int e, int[] tree, int index){ if(s == e){ tree[index] = arr[s]; return; } //otherwise divide in two parts and fill both sides simply int mid = (s+e)/2; build(arr, s, mid, tree, 2*index); build(arr, mid+1, e, tree, 2*index+1); //who will build the current position dude tree[index] = Math.min(tree[2*index], tree[2*index+1]); } public int query(int sr, int er, int sc, int ec, int index, int[] tree){ if(lazy[index] != 0){ tree[index] += lazy[index]; if(sc != ec){ lazy[2*index+1] += lazy[index]; lazy[2*index] += lazy[index]; } lazy[index] = 0; } //no overlap if(sr > ec || sc > er) return Integer.MAX_VALUE; //found the index baby if(sr <= sc && ec <= er) return tree[index]; //finding the index on both sides hehehehhe int mid = (sc + ec)/2; int left = query(sr, er, sc, mid, 2*index, tree); int right = query(sr, er, mid+1, ec, 2*index + 1, tree); return Integer.min(left, right); } //now we will do point update implementation //it should be simple then we expected for sure public void update(int index, int indexr, int increment, int[] tree, int s, int e){ if(lazy[index] != 0){ tree[index] += lazy[index]; if(s != e){ lazy[2*index+1] = lazy[index]; lazy[2*index] = lazy[index]; } lazy[index] = 0; } //no overlap if(indexr < s || indexr > e) return; //found the required index if(s == e){ tree[index] += increment; return; } //search for the index on both sides int mid = (s+e)/2; update(2*index, indexr, increment, tree, s, mid); update(2*index+1, indexr, increment, tree, mid+1, e); //now update the current range simply tree[index] = Math.min(tree[2*index+1], tree[2*index]); } public void rangeUpdate(int[] tree , int index, int s, int e, int sr, int er, int increment){ //if not at all in the same range if(e < sr || er < s) return; //complete then also move forward if(s == e){ tree[index] += increment; return; } //otherwise move in both subparts int mid = (s+e)/2; rangeUpdate(tree, 2*index, s, mid, sr, er, increment); rangeUpdate(tree, 2*index + 1, mid+1, e, sr, er, increment); //update current range too na //i always forget this step for some reasons hehehe, idiot tree[index] = Math.min(tree[2*index], tree[2*index + 1]); } public void rangeUpdateLazy(int[] tree, int index, int s, int e, int sr, int er, int increment){ //update lazy values //resolve lazy value before going down if(lazy[index] != 0){ tree[index] += lazy[index]; if(s != e){ lazy[2*index+1] += lazy[index]; lazy[2*index] += lazy[index]; } lazy[index] = 0; } //no overlap case if(sr > e || s > er) return; //complete overlap if(sr <= s && er >= e){ tree[index] += increment; if(s != e){ lazy[2*index+1] += increment; lazy[2*index] += increment; } return; } //otherwise go on both left and right side and do your shit int mid = (s + e)/2; rangeUpdateLazy(tree, 2*index, s, mid, sr, er, increment); rangeUpdateLazy(tree, 2*index + 1, mid+1, e, sr, er, increment); tree[index] = Math.min(tree[2*index+1], tree[2*index]); return; } } //prime sieve public static void primeSieve(int n){ BitSet bitset = new BitSet(n+1); for(long i = 0; i < n ; i++){ if (i == 0 || i == 1) { bitset.set((int) i); continue; } if(bitset.get((int) i)) continue; primeNumbers.add((int)i); for(long j = i; j <= n ; j+= i) bitset.set((int)j); } } //number of divisors public static int countDivisors(long number){ if(number == 1) return 1; List<Integer> primeFactors = new ArrayList<>(); int index = 0; long curr = primeNumbers.get(index); while(curr * curr <= number){ while(number % curr == 0){ number = number/curr; primeFactors.add((int) curr); } index++; curr = primeNumbers.get(index); } if(number != 1) primeFactors.add((int) number); int current = primeFactors.get(0); int totalDivisors = 1; int currentCount = 2; for (int i = 1; i < primeFactors.size(); i++) { if (primeFactors.get(i) == current) { currentCount++; } else { totalDivisors *= currentCount; currentCount = 2; current = primeFactors.get(i); } } totalDivisors *= currentCount; return totalDivisors; } //now adding next permutation function to java hehe public static boolean next_permutation(int[] p) { for (int a = p.length - 2; a >= 0; --a) if (p[a] < p[a + 1]) for (int b = p.length - 1;; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } return false; } //finding the value of NCR in O(RlogN) time and O(1) space public static long getNcR(int n, int r) { long p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r > 0) { p *= n; k *= r; long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } return p; } //is vowel function public static boolean isVowel(char c) { return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U'); } //to sort the array with better method public static void sort(int[] a) { ArrayList<Integer> 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); } //sort long public static void sort(long[] a) { ArrayList<Long> 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); } //for calculating binomialCoeff public static int binomialCoeff(int n, int k) { int C[] = new int[k + 1]; // nC0 is 1 C[0] = 1; for (int i = 1; i <= n; i++) { // Compute next row of pascal // triangle using the previous row for (int j = Math.min(i, k); j > 0; j--) C[j] = C[j] + C[j - 1]; } return C[k]; } //Pair with int int public static class Pair{ public int a; public int b; Pair(int a , int b){ this.a = a; this.b = b; } @Override public String toString(){ return a + " -> " + b; } } //Triplet with int int int public static class Triplet{ public int a; public int b; public int c; Triplet(int a , int b, int c){ this.a = a; this.b = b; this.c = c; } @Override public String toString(){ return a + " -> " + b; } } //Shortcut function public static long lcm(long a , long b){ return a * (b/gcd(a,b)); } //let's make one for calculating lcm basically public static int lcm(int a , int b){ return (a * b)/gcd(a,b); } //int version for gcd public static int gcd(int a, int b){ if(b == 0) return a; return gcd(b , a%b); } //long version for gcd public static long gcd(long a, long b){ if(b == 0) return a; return gcd(b , a%b); } //for ncr calculator(ignore this code) public static long __gcd(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } //swapping two elements in an array public static void swap(int[] arr, int left , int right){ int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //for char array public static void swap(char[] arr, int left , int right){ char temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //reversing an array public static void reverse(int[] arr){ int left = 0; int right = arr.length-1; while(left <= right){ swap(arr, left,right); left++; right--; } } public static long expo(long a, long b, long mod) { long res = 1; while (b > 0) { if ((b & 1) == 1L) res = (res * a) % mod; //think about this one for a second a = (a * a) % mod; b = b >> 1; } return res; } //SOME EXTRA DOPE FUNCTIONS public static long mminvprime(long a, long b) { return expo(a, b - 2, b); } public static long mod_add(long a, long b, long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } public static long mod_sub(long a, long b, long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } public static long mod_mul(long a, long b, long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } public static long mod_div(long a, long b, long m) { a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m; } //O(n) every single time remember that public static long nCr(long N, long K , long mod){ long upper = 1L; long lower = 1L; long lowerr = 1L; for(long i = 1; i <= N; i++){ upper = mod_mul(upper, i, mod); } for(long i = 1; i <= K; i++){ lower = mod_mul(lower, i, mod); } for(long i = 1; i <= (N - K); i++){ lowerr = mod_mul(lowerr, i, mod); } // out.println(upper + " " + lower + " " + lowerr); long answer = mod_mul(lower, lowerr, mod); answer = mod_div(upper, answer, mod); return answer; } // ll *fact = new ll[2 * n + 1]; // ll *ifact = new ll[2 * n + 1]; // fact[0] = 1; // ifact[0] = 1; // for (ll i = 1; i <= 2 * n; i++) // { // fact[i] = mod_mul(fact[i - 1], i, MOD1); // ifact[i] = mminvprime(fact[i], MOD1); // } //ifact is basically inverse factorial in here!!!!!(imp) public static long combination(long n, long r, long m, long[] fact, long[] ifact) { long val1 = fact[(int)n]; long val2 = ifact[(int)(n - r)]; long val3 = ifact[(int)r]; return (((val1 * val2) % m) * val3) % m; } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //-------------------------------------------------------- }
0
Non-plagiarised
1071f735
cd835290
//package com.company; //import java.math.BigInteger; import java.util.*; //import java.util.Stack; import java.io.BufferedReader; import java.io.IOException; //import java.io.*; 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.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static class obj{ long a1; long a2; //int a3; obj(long a1,long a2){ this.a1=a1; this.a2=a2; // this.a3=a3; } } static class sortby implements Comparator<obj>{ public int compare(obj o1,obj o2){ return o1.a1>o2.a1?1:-1; } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static long power(long x,long y,long p){ String b1=""+y; long d=p; long reminderb=0; for(int i=0;i<b1.length();i++){ reminderb=(reminderb*10+b1.charAt(i)-'0')%d; } y=reminderb; long res=1; x=x%p; while(y>0){ if((y&1)>0) res=(res*x)%p; y=y>>1; x=(x*x)%p; } return res; } static boolean isComposite(int n) { // Corner cases if (n <= 1) System.out.println("False"); if (n <= 3) System.out.println("False"); // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return true; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return true; return false; } //static ArrayList<Integer>[] a; // static int[] vis; //static int[] col; /* static boolean dfs(int i,int c){ vis[i]=1; col[i]=c; //System.out.println(i+" "+c); for(int j:a[i]){ if(vis[j]==0) { if (dfs(j, c ^ 1) == false) return false; } else if(col[i]==col[j]) return false; } return true; }*/ static int[] m; static int solve(int p,long d){ if(p<1) return 0; if(m[p]!=0) return m[p]; if(p==1) return 1; long to=0; for(int i=1;i<=p-1;i++){ to+=solve(i,d); to=to%d; } for(int i=p;i>=2;i--){ to+=solve(p/i,d); to=to%d; } m[p]=(int)to; return m[p]; } static long[][] dis; // static long d=1000000007; public static long solve(long[] arr,int i,int j){ if(i>j){ return 0; } if(dis[i][j]!=-1) { return dis[i][j]; } if(i==j){ return arr[i]; } long c=0; c=Math.max(arr[i]+Math.min(arr[j-1]+solve(arr,i+1,j-1),arr[i+1]+solve(arr,i+2,j)),arr[j]+Math.min(solve(arr,i+1,j-1),solve(arr,i,j-2))); dis[i][j]=c; return c; } public static void main(String[] args) { FastReader s = new FastReader(); //InputStream inputStream = System.in; OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); //Scanner s=new Scanner(System.in); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); int m = s.nextInt(); int x=s.nextInt(); long[] a = new long[n]; for(int i=0;i<n;i++){ a[i]=s.nextInt(); } out.println("YES"); PriorityQueue<obj> p = new PriorityQueue<>(new sortby()); for (int i = 0; i <m; i++) { p.add(new obj(a[i],i+1)); out.print(i+1+" "); } for(int i=m;i<n;i++){ obj o=p.poll(); p.add(new obj(o.a1+a[i],o.a2)); out.print(o.a2+" "); } out.println(); } out.close(); }}
import java.util.*; public class CP { public static int[] swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; return arr; } static int isPowerOfTwo(int n) { if(n == 0 ) return 0; while(n != 1) { if(n % 2 != 0) return 0; n = n / 2; } return 1; } public static void printArr(int[] a){ for(int i=0; i<a.length; i++){ System.out.print(a[i]+1+" "); } System.out.println(); } public static class Pair implements Comparable<Pair> { int val; int idx; Pair(int a, int b){ this.val = a; this.idx = b; } public int compareTo(Pair o){ return this.val - o.val; } } public static void main(String[] args) { // CF1 // Scanner sc = new Scanner(System.in); // int t = sc.nextInt(); // for (int i = 0; i < t; i++) { // int n = sc.nextInt(); // int x = sc.nextInt(); // int[] arr = new int[n]; // int sum = 0; // for (int j = 0; j < n; j++) { // arr[j] = sc.nextInt(); // sum += arr[j]; // } // if (x == sum) { // System.out.println("NO"); // } else if (x > sum) { // System.out.println("YES"); // for (int j = 0; j < n; j++) { // System.out.print(arr[j] + " "); // } // System.out.println(); // } else {// x<sum // Arrays.sort(arr); // int curr = 0; // int k = 0; // while ((curr != x) && (k < n)) { // curr += arr[k]; // k++; // } // if(k==n) { // System.out.println("YES"); // for (int j = 0; j < n; j++) { // System.out.print(arr[j] + " "); // } // System.out.println(); // } // else if (curr == x) { // arr = swap(arr, k-1, n - 1); // System.out.println("YES"); // for (int j = 0; j < n; j++) { // System.out.print(arr[j] + " "); // } // System.out.println(); // }else{ // System.out.println("NO"); // } // } // } Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int m=sc.nextInt(); int x = sc.nextInt(); int[] arr = new int[n]; int[] ans = new int[n]; for (int j = 0; j < n; j++) { arr[j] = sc.nextInt(); } PriorityQueue<Pair> pq=new PriorityQueue<>(); for (int j = 0; j < m; j++) { pq.add(new Pair(arr[j],j)); ans[j]=j; } for (int j = m; j < n; j++) { Pair p=pq.remove(); pq.add(new Pair(p.val+arr[j],p.idx)); ans[j]=p.idx; } System.out.println("YES"); printArr(ans); } } }
1
Plagiarised
0c9d4def
d3415fc5
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; public class C{ private static int maxWords = 0; private static int[][] words; private static int n; private static int[] netwrtchar(int index){ ArrayList<Integer> list = new ArrayList<>(); for(int i=0; i<n; i++){ int sum = 0; for(int j=0; j<words[i].length; j++){ if(j==index) continue; sum += words[i][j]; } list.add(words[i][index] - sum); // f[i] = words[i][index] - sum; } Collections.sort(list, Collections.reverseOrder()); int[] f = new int[list.size()]; for(int i=0; i<list.size(); i++){ f[i] = list.get(i); } return f; } private static int maxWindow(int[] f){ int count = 0, sum = 0; int index = 0; while(index<f.length && sum+f[index]>0){ sum += f[index++]; count++; } return count; } public static void main(String[] args){ FS sc = new FS(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ n = sc.nextInt(); words = new int[n][5]; maxWords = 0; for(int i=0; i<n; i++){ String s = sc.next(); for(int j=0; j<s.length(); j++){ words[i][s.charAt(j)-'a']++; } } int maxWindow = 0; for(int i=0; i<5; i++){ int[] f = netwrtchar(i); int current = maxWindow(f); maxWindow = Math.max(maxWindow, current); } System.out.println(maxWindow); } pw.flush(); pw.close(); } static class FS{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(Exception ignored){ } } return st.nextToken(); } int[] nextArray(int n){ int[] a = new int[n]; for(int i = 0; i < n; i++){ a[i] = nextInt(); } return a; } long[] nextLongArray(int n){ long[] a = new long[n]; for(int i = 0; i < n; i++){ a[i] = nextLong(); } return a; } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } } }
import java.io.*; import java.util.*; import static java.lang.Math.max; import static java.lang.Math.min; public class C { Reader source; BufferedReader br; StringTokenizer in; PrintWriter out; public String nextToken() throws Exception { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } public void run() throws Exception { source = OJ ? new InputStreamReader(System.in) : new FileReader("C.in"); br = new BufferedReader(source); out = new PrintWriter(System.out); solve(); out.flush(); } public void sort(int[] a) { ArrayList<Integer> list = new ArrayList<>(); for (int i : a) list.add(i); Collections.sort(list); for (int i = a.length - 1, j = 0; i >= 0; i--, j++) a[j] = list.get(i); } public static void main(String[] args) throws Exception { new C().run(); } private boolean OJ = System.getProperty("ONLINE_JUDGE") != null; public void solve() throws Exception { int t = nextInt(); while (t-- > 0) { int n = 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 = nextToken(); int sz = s.length(); int a = 0, b = 0, c = 0, d = 0, e = 0; for (int j = 0; j < sz; j++) { if (s.charAt(j) == 'a') a++; if (s.charAt(j) == 'b') b++; if (s.charAt(j) == 'c') c++; if (s.charAt(j) == 'd') d++; if (s.charAt(j) == 'e') e++; } A[i] = a - b - c - d - e; B[i] = b - a - c - d - e; C[i] = c - a - b - d - e; D[i] = d - a - b - c - e; E[i] = e - a - b - c - d; } int ans = 0, sum; sum = 0; sort(A); for (int j = 0; j < n; j++) { sum += A[j]; if (sum <= 0) { ans = max(ans, j); break; } } if (sum > 0) ans = n; sum = 0; sort(B); for (int j = 0; j < n; j++) { sum += B[j]; if (sum <= 0) { ans = max(ans, j); break; } } if (sum > 0) ans = n; sum = 0; sort(C); for (int j = 0; j < n; j++) { sum += C[j]; if (sum <= 0) { ans = max(ans, j); break; } } if (sum > 0) ans = n; sum = 0; sort(D); for (int j = 0; j < n; j++) { sum += D[j]; if (sum <= 0) { ans = max(ans, j); break; } } if (sum > 0) ans = n; sum = 0; sort(E); for (int j = 0; j < n; j++) { sum += E[j]; if (sum <= 0) { ans = max(ans, j); break; } } if (sum > 0) ans = n; System.out.println(ans); } } }
0
Non-plagiarised
597195ee
5f3a196a
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class C_MonstersAndSpells_1700 { public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int[] times = new int[n]; int[] health = new int[n]; for(int i = 0; i < n; i++) { times[i] = sc.nextInt(); } for(int i = 0; i < n; i++) { health[i] = sc.nextInt(); } Point[] points = new Point[n]; for(int i = 0; i < n; i++) { points[i] = new Point(times[i]-health[i], times[i]); } Arrays.sort(points); long ans = 0; for(int i = 0; i < n; i++) { int j = i+1; int latestTime = points[i].time; while(j < n && points[j].startBy < latestTime) { latestTime = Math.max(latestTime, times[j]); j++; } long length = latestTime-points[i].startBy; ans += (length*(length + 1))/2; i = j - 1; } System.out.println(ans); } out.close(); } static class Point implements Comparable<Point> { Integer startBy; Integer time; Point(int startBy, int time) { this.startBy = startBy; this.time = time; } @Override public int compareTo(Point o) { return this.startBy.compareTo(o.startBy); } } public static PrintWriter out; public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Pair { public int x,y; public Pair(int x, int y) { this.x = x; this.y = y; } } }
import java.io.*; import java.util.*; public class MonstersAndSpells { public static PrintWriter out; public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(br.readLine()); out=new PrintWriter(System.out); int t=Integer.parseInt(st.nextToken()); while(t-->0) { st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken());//monsters int time[]=new int[n];//time int health[]=new int[n];//health st=new StringTokenizer(br.readLine()); for(int i=0;i<n;i++) { time[i]=Integer.parseInt(st.nextToken()); } st=new StringTokenizer(br.readLine()); for(int i=0;i<n;i++) { health[i]=Integer.parseInt(st.nextToken()); } State a[]=new State[n]; for(int i=0;i<n;i++) { a[i]=new State(time[i]-health[i], time[i]); } Arrays.sort(a); long ans=0; for(int i=0;i<n;i++) { int j=i+1; int max=a[i].time; while(j<n&&a[j].x<max) { max=Math.max(max, time[j]); j++; } ans+=((long)(max-a[i].x)*(long)(max-a[i].x+1))/2; i=j-1; } out.println(ans); } out.close(); } static class State implements Comparable<State>{ int x, time; public State(int x, int time) { this.x=x;this.time=time; } public int compareTo(State o) { return x-o.x; } } }
1
Plagiarised
3afcc566
3dd65549
//package codeforces.globalround18; import java.io.*; import java.util.*; import static java.lang.Math.*; //Think through the entire logic before jump into coding! //If you are out of ideas, take a guess! It is better than doing nothing! //Read both C and D, it is possible that D is easier than C for you! //Be aware of integer overflow! //If you find an answer and want to return immediately, don't forget to flush before return! 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 void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { int n = in.nextInt(); char[] a = in.next().toCharArray(), b = in.next().toCharArray(); int match0 = 0, match1 = 0, mismatch10 = 0, mismatch01 = 0; for(int i = 0; i < n; i++) { if(a[i] == b[i]) { if(a[i] == '0') match0++; else match1++; } else { if(a[i] == '0') mismatch01++; else mismatch10++; } } if(mismatch01 + mismatch10 == 0) out.println(0); else { if(match1 - match0 == 1 && mismatch01 == mismatch10) { out.println(min(match0 + match1, mismatch01 + mismatch10)); } else if(match1 - match0 == 1) { out.println(match0 + match1); } else if(mismatch01 == mismatch10) { out.println(mismatch01 + mismatch10); } else out.println(-1); } } out.close(); } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream("src/input.in")); out = new PrintWriter(new FileOutputStream("src/output.out")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } List<Integer>[] readGraphOneIndexed(int n, int m) { List<Integer>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); adj[u].add(v); adj[v].add(u); } return adj; } List<Integer>[] readGraphZeroIndexed(int n, int m) { List<Integer>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u].add(v); adj[v].add(u); } return adj; } /* A more efficient way of building a graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]] = end2[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]] = end1[i]; idxForEachNode[end2[i]]++; } return adj; } } }
import java.util.*; import java.io.*; public class codeforces { public static void main(String[] args) throws Exception { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); char[] a=sc.next().toCharArray(); char[] b=sc.next().toCharArray(); int e0=0; int e1=0; int o0=0; int o1=0; for(int i=0;i<n;i++) { if(a[i]!=b[i]) { if(a[i]=='1') { e1++; }else { e0++; } }else { if(a[i]=='1') { o1++; }else { o0++; } } } int ans=Integer.MAX_VALUE; if(e1==e0) { ans=Math.min(ans, e1+e0); } if(o1==o0+1) { ans=Math.min(ans, o1+o0); } // pw.println(e0+" "+e1+" "+o0+" "+o1); pw.println(ans==Integer.MAX_VALUE?-1:ans); } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
0
Non-plagiarised
4f7af821
f870e76b
import java.util.*; public class contestA { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); int inf = 1000300300; while (t-->0){ int n = scanner.nextInt(); int k = scanner.nextInt(); int[] c = new int[n]; Arrays.fill(c,inf); int[] a = new int[k]; int[] b = new int[k]; for(int i=0;i<k;++i) a[i] = scanner.nextInt() - 1; for(int i=0;i<k;++i) b[i] = scanner.nextInt(); for(int i=0;i<k;++i) c[a[i]] = b[i]; for(int i=1;i<n;++i){ c[i] = Math.min(c[i],c[i-1]+1); } for(int i=n-2;i>=0;--i){ c[i] = Math.min(c[i],c[i+1]+1); } for(int i=0;i<n;++i) System.out.print(c[i]+" "); System.out.println(); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.Vector; public class Main { static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(outputStream); public static void main(String[] args) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int k = in.nextInt(); int[] v1 = new int[k]; int[] v2 = new int[k]; for (int i = 0; i < k; i++) { v1[i] = in.nextInt(); } for (int i = 0; i < k; i++) { v2[i] = in.nextInt(); } //wejhfduiwehiofhw int[] res = new int[n + 2]; Arrays.fill(res, 2000000000); for (int i = 0; i < k; i++) { res[v1[i]] = v2[i]; } for (int i = 1; i <= n; i++) { int val = Math.min(res[i], res[i - 1] + 1); res[i] = val; } //ewhfowiejp //wedhciuwahidochqowi //wjdhoiqwnlidhqw for (int i = n; i >= 1; i--) { int val1 = Math.min(res[i], res[i + 1] + 1); res[i] = val1; // out.println(res[i]); } for (int i = 1; i <= n; i++) { int r = res[i]; out.print(r + " "); } out.println(); } out.close(); } private static void takeinput(int[] arr, int k) { for (int i = 0; i < k; i++) { arr[i] = in.nextInt(); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public String nextLine() throws IOException { return reader.readLine().trim(); } } }
0
Non-plagiarised
ba468e1f
d9199dfd
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<Integer> b = new ArrayList<>(); ArrayList<Integer> r = new ArrayList<>(); char c[] = fastReader.next().toCharArray(); for (int i = 0; i < n; i++) { if (c[i] == 'B') { b.add(a[i]); } else { r.add(a[i]); } } Collections.sort(b); Collections.sort(r); int sizeb = b.size(); boolean isValid = true; for (int i = 1 , j = 0; i <=sizeb; i++ , j++){ if (b.get(j) < i){ isValid =false; } } for (int i = sizeb+1 , j = 0; i <=n && j < r.size(); i++ , j++){ if (r.get(j) > i){ isValid =false; } } if (isValid){ System.out.println("YES"); }else{ System.out.println("NO"); } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Scanner; public class Simple{ public static void main(String args[]){ //System.out.println("Hello Java"); Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t>0){ int n = s.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = s.nextInt(); } String str = s.next(); //Arrays.sort(arr); ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for(int i=0;i<n;i++){ if(str.charAt(i)=='R'){ red.add(arr[i]); } else{ blue.add(arr[i]); } } Collections.sort(red); Collections.sort(blue); int start =1; boolean bool =true; for(int i=0;i<blue.size();i++){ if(blue.get(i)<start){ bool = false; break; } start++; } if(!bool){ System.out.println("NO"); } else{ for(int i=0;i<red.size();i++){ if(red.get(i)>start){ bool = false; break; } start++; } if(bool){ System.out.println("YES"); } else{ System.out.println("NO"); } } t--; } s.close(); } }
0
Non-plagiarised
22b41936
8f855b99
import java.io.*; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.util.Collections; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.*; public class Main1 { static ArrayList<Integer> list1 = new ArrayList<>() ; static ArrayList<Integer> 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 <N ; i++) { if (a[i] == 1)list2.add(i) ; else list1.add(i) ; } n = list1.size() ; m = list2.size() ; dp = new long[n][m] ; for(int i=0 ; i<n ; i++) for(int j=0 ; j<m ; j++) dp[i][j] = -1 ; System.out.println(solver(0 , 0 )); out.flush(); out.close(); // } // catch (Exception e){ // return; // } } static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } static ArrayList<Integer> list = new ArrayList<>(); static boolean A[] = new boolean[2 * 90000001]; static void seive(int n) { int maxn = n; //int maxn = 1000000 ; A[0] = A[1] = true; for (int i = 2; i * i <= maxn; i++) { if (!A[i]) { for (int j = i * i; j <= maxn; j += i) A[j] = true; } } for (int i = 2; i <= maxn; i++) if (!A[i]) list.add(i); } static int findLCA(int a, int b, int par[][], int depth[]) { if (depth[a] > depth[b]) { a = a ^ b; b = a ^ b; a = a ^ b; } int diff = depth[b] - depth[a]; for (int i = 19; i >= 0; i--) { if ((diff & (1 << i)) > 0) { b = par[b][i]; } } if (a == b) return a; for (int i = 19; i >= 0; i--) { if (par[b][i] != par[a][i]) { b = par[b][i]; a = par[a][i]; } } return par[a][0]; } static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } static void formArrayForBinaryLifting(int n, int par[][]) { for (int j = 1; j < 20; j++) { for (int i = 0; i < n; i++) { if (par[i][j - 1] == -1) continue; par[i][j] = par[par[i][j - 1]][j - 1]; } } } static void sort(int ar[]) { int n = ar.length; ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static void sort1(long ar[]) { int n = ar.length; ArrayList<Long> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static long ncr(long n, long r, long mod) { if (r == 0) return 1; long val = ncr(n - 1, r - 1, mod); val = (n * val) % mod; val = (val * modInverse(r, mod)) % mod; return val; } static long fast_pow(long base, long n, long M) { if (n == 0) return 1; if (n == 1) return base % M; long halfn = fast_pow(base, n / 2, M); if (n % 2 == 0) return (halfn * halfn) % M; else return (((halfn * halfn) % M) * base) % M; } static long modInverse(long n, long M) { return fast_pow(n, M - 2, M); } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } static class FastOutput implements AutoCloseable, Closeable, Appendable { private static final int THRESHOLD = 32 << 10; private final Writer os; private StringBuilder cache = new StringBuilder(THRESHOLD * 2); public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } private void afterWrite() { if (cache.length() < THRESHOLD) { return; } flush(); } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); afterWrite(); return this; } public FastOutput append(long c) { cache.append(c); afterWrite(); return this; } public FastOutput append(String c) { cache.append(c); afterWrite(); return this; } public FastOutput println() { return append(System.lineSeparator()); } public FastOutput flush() { try { // boolean success = false; // if (stringBuilderValueField != null) { // try { // char[] value = (char[]) stringBuilderValueField.get(cache); // os.write(value, 0, cache.length()); // success = true; // } catch (Exception e) { // } // } // if (!success) { os.append(cache); // } os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
import javax.print.DocFlavor; import java.util.*; import java.lang.*; import java.io.*; public class Solution { static int N = 5005; static int[] arr = new int[N]; static long[][] memo = new long[N][N]; static List<Integer> occupiedSeats = new ArrayList<>(); static List<Integer> emptySeats = new ArrayList<>(); static int n, occSize, empSize; public static void main(String[] args) throws java.lang.Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new FastReader(); int test = 1; for (int t = 0; t < test; t++) { solve(); } out.close(); } private static void solve() { n = sc.nextInt(); for (int i = 1; i <= n; i++) { arr[i] = sc.nextInt(); if (arr[i] == 1) { occupiedSeats.add(i); }else { emptySeats.add(i); } } occSize = occupiedSeats.size(); empSize = emptySeats.size(); for (long[] memset : memo) { Arrays.fill(memset, -1); } out.println(minimumTime(0, 0)); } private static long minimumTime(int occupied, int empty) { if (occupied == occSize) { return 0; } if (empty == empSize) { return Integer.MAX_VALUE; } if (memo[occupied][empty] != -1) { return memo[occupied][empty]; } long curr = Math.abs(occupiedSeats.get(occupied) - emptySeats.get(empty)) + minimumTime(occupied + 1, empty + 1); curr = Math.min(curr, minimumTime(occupied, empty + 1)); memo[occupied][empty] = curr; return curr; } public static FastReader sc; public static PrintWriter out; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
0
Non-plagiarised
d1cd194e
dbffab11
import java.util.*; import java.lang.*; import java.io.*; public class Template { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null ||!st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // static void solve(String s) // { //// Scanner sc = new Scanner(System.in); //// String s = sc.next(); // // int x[] = new int[2]; // x[0] = x[1] = -1; // // int ans = 0; // int n = s.length(); // for(int i=0;i<n;i++) // { // int c = s.charAt(i) - '0'; // if(c == 1 || c == 0) // { // x[(i%2) ^ c] = i; // } // int min = Math.min(x[0], x[1]); // ans += i - min; // //System.out.println(ans); // } // System.out.println(ans); // } // // public static void main(String args[]) // { // FastReader sc = new FastReader(); // //solve(); // //Scanner sc = new Scanner(System.in) // int testcases = sc.nextInt(); // nTest is the number of treasure hunts. // //// int testcases = 3; // while(testcases-- > 0) // { // String s = sc.next(); // solve(s); // // } // // } static class Pair implements Comparable<Pair> { int h; int ind; Pair(int h, int ind) { this.h = h; this.ind = ind; } @Override public int compareTo(Pair o) { return this.h - o.h; } } public static void main(String[] args) { FastReader fs=new FastReader(); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int n = fs.nextInt(); int m = fs.nextInt(); int x = fs.nextInt(); if(n < m) { System.out.println("NO"); continue; } Pair a[] = new Pair[n]; PriorityQueue<Pair> heap = new PriorityQueue<>(); for(int i=0;i<n;i++) { a[i] = new Pair(fs.nextInt(), i); } Arrays.sort(a); for(int i=1;i<=m;i++) { heap.add(new Pair(0, i)); } int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; int ans[] = new int[n]; int idx = 0; while(!heap.isEmpty() && idx < n) { Pair curr = heap.poll(); curr.h += a[idx].h; ans[a[idx].ind] = curr.ind; heap.add(new Pair(curr.h, curr.ind)); idx++; } // int towers[] = new int[m+1]; // int tower = 1; // boolean flag = false; // boolean inc = true; // for(int i=0;i<n;i++) // { // if(tower == m+1) // { // tower = m; // inc = false; // } // if(tower == 0) // { // tower = 1; // inc = true; // } // towers[tower] += a[i].h; // System.out.println(a[i].h +" THis" + tower); //// min = Math.min(min, towers[tower]); //// max = Math.max(max, towers[tower]); // ans[a[i].ind] = tower; //// if(Math.abs(max - min) > x) //// { //// System.out.println("NO" + a[i].ind+" "+a[i].h +" "+min +" "+max); //// flag = true; //// break; //// } // if(inc) // tower++; // else // tower--; // } // for(int i=1;i<=m;i++) // { // min = Math.min(min, towers[i]); // max = Math.max(max, towers[i]); // } // if(Math.abs(max - min) > x) // { // System.out.println("NO" + max+" "+min);// + a[i].ind+" "+a[i].h +" "+min +" "+max); // //flag = true; // continue; // } // if(flag) // continue; System.out.println("YES"); for(int i:ans) System.out.print(i+" "); System.out.println(); } } }
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class cses { static int mod=1000000007; static FastReader sc=new FastReader(); public static void main (String[] args) throws java.lang.Exception { long startTime=System.currentTimeMillis(); int t=sc.nextInt(); for(int y=0;y<t;++y) { int n=sc.nextInt(); int m=sc.nextInt(); int x=sc.nextInt(); int arr[]=arrayintinput(n); //PriorityQueue<Node> pq=new PriorityQueue<>(); int index[]=new int[n]; PriorityQueue<Node> pq =new PriorityQueue<Node>(new comp()); for(int i=0;i<m;++i) { pq.add(new Node(0,i+1)); } for(int i=0;i<n;++i) { Node temp=pq.remove(); temp.sum+=arr[i]; index[i]=temp.build; pq.add(new Node(temp.sum,temp.build)); } System.out.println("YES"); for(int i=0;i<n;++i) { System.out.print(index[i]+" "); } System.out.println(); } // getExecutionTime(startTime); } static int[] arrayintinput(int n) { int arr[]=new int[n]; for(int i=0;i<n;++i) { arr[i]=sc.nextInt(); } return arr; } static class comp implements Comparator<Node>{ public int compare(Node a,Node b) { return a.sum-b.sum; } } static class Node{ int sum; int build; Node(int sum ,int build) { this.sum=sum; this.build=build; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void getExecutionTime(long startTime) { final long endTime = System.currentTimeMillis(); System.out.println(); System.out.println("Time take is "+(endTime-startTime)+" ms"); } } // System.out.println("Case #"+(y+1)+": "+ans);
0
Non-plagiarised
680ba922
f3d7ce08
import java.util.*; import java.io.*; public class Solution { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static int parent(int a , int p[]) { if(a == p[a]) return a; return p[a] = parent(p[a],p); } static void union(int a , int b , int p[] , int size[]) { a = parent(a,p); b = parent(b,p); if(a == b) return; if(size[a] < size[b]) { int temp = a; a = b; b = temp; } p[b] = a; size[a] += size[b]; } static long getSum(int index , long BITree[]) { long sum = 0; // Iniialize result // index in BITree[] is 1 more than // the index in arr[] // index = index + 1; // Traverse ancestors of BITree[index] while(index>0) { // Add current element of BITree // to sum sum += BITree[index]; // Move index to parent node in // getSum View index -= index & (-index); } return sum; } // Updates a node in Binary Index Tree (BITree) // at given index in BITree. The given value // 'val' is added to BITree[i] and all of // its ancestors in tree. public static void updateBIT(int n, int index, long val , long BITree[]) { // index in BITree[] is 1 more than // the index in arr[] // index = index + 1; // Traverse all ancestors and add 'val' while(index <= n) { // Add 'val' to current node of BIT Tree BITree[index] += val; // Update index to that of parent // in update View index += index & (-index); } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int dp[][]; static int f(int pos , int take , int arr[]) { if(pos == -1) { if(take == 0) return 0; return -10000000; } if(dp[pos][take] != -1) return dp[pos][take]; if(pos+1-take == arr[pos]) dp[pos][take] = Math.max(dp[pos][take],1 + f(pos-1,take,arr)); dp[pos][take] = Math.max(dp[pos][take],f(pos-1,take,arr)); if(take > 0) dp[pos][take] = Math.max(dp[pos][take],f(pos-1,take-1,arr)); return dp[pos][take]; } public static void main(String []args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); sc.nextLine(); String a = sc.nextLine(); String b = sc.nextLine(); int same = 0 , zo = 0 , oz = 0 , oo = 0 , zz = 0; for(int i = 0 ; i < n ; i++) { if(a.charAt(i) == '0' && b.charAt(i) == '1') oz++; else if(a.charAt(i) == '1' && b.charAt(i) == '0') zo++; else if(a.charAt(i) == '1' && b.charAt(i) == '1') oo++; else zz++; } if(oz == zo || (zz == oo-1)) { int mx = Integer.MAX_VALUE; if(oz == zo) mx = Math.min(mx,2*oz); if(oo-1 == zz) mx = Math.min(mx,zz+oo); System.out.println(mx); } else { System.out.println(-1); } } } }
import javax.swing.plaf.IconUIResource; import java.lang.reflect.Array; import java.text.CollationElementIterator; import java.util.*; import java.io.*; //Timus judge id- 323935JJ public class Main { //---------------------------------------------------------------------------------------------- public static class Pair implements Comparable<Pair> { int x=0,y=0; int z=0; public Pair(int a, int b) { this.x=a; this.y=b; } public int compareTo(Pair o) { return this.x - o.x; } } public static int mod = (int) (1e9 + 7); static int ans = Integer.MAX_VALUE; public static void main(String hi[]) throws Exception { FastReader sc = new FastReader(); int t =sc.nextInt(); while(t-->0) { int n =sc.nextInt(); String a = sc.nextLine(),b=sc.nextLine(); int count1=0,count2=0,count3=0,count4=0; for(int i=0;i<n;i++) { if(a.charAt(i)=='0'&&b.charAt(i)=='0') count1++; else if(a.charAt(i)=='1'&&b.charAt(i)=='1') count2++; else if(a.charAt(i)=='1'&&b.charAt(i)=='0') count3++; else if(a.charAt(i)=='0'&&b.charAt(i)=='1') count4++; } int ans=Integer.MAX_VALUE; if(count3==count4) ans=Math.min(count3*2,ans); if(count2==count1+1) ans=Math.min(ans,2*count1+1); if(ans==Integer.MAX_VALUE) System.out.println(-1); else System.out.println(ans); } } static int find(int[] a,int x) { if(a[x]==-1) return x; else return find(a,a[x]); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // method to return LCM of two numbers static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcdl(long a, long b) { if (a == 0) return b; return gcdl(b % a, a); } // method to return LCM of two numbers static long lcml(long a, long b) { return (a / gcdl(a, b)) * b; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
0
Non-plagiarised
141effef
6653a758
// package codeforces; import java.util.*; public class ArmChairs { static int[]arr; static ArrayList<Integer>a; static ArrayList<Integer>b; static int dp[][]; public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=scn.nextInt(); } dp=new int[n+1][n+1]; a =new ArrayList<>(); b =new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]==0) { a.add(i); }else{ b.add(i); } } System.out.println(solve(0,0)); } public static int solve(int i,int j) { if(i==b.size()) { return 0; } if(j==a.size()) { return 100000000; } if(dp[i][j]!=0) { return dp[i][j]; } int x=Math.abs(a.get(j)-b.get(i))+solve(i+1,j+1); int y=solve(i,j+1); return dp[i][j]=Math.min(x, y); } }
import java.util.*; public class D { public static void main(String[] args) { Scanner sc=new Scanner(System.in); ArrayList<Integer> o=new ArrayList<Integer>(), e=new ArrayList<Integer>(); int n = sc.nextInt(),dp[][]=new int[n+1][n+1]; for(int i=1;i<=n;i++){ int x=sc.nextInt(); if(x==1)o.add(i); else e.add(i); } for(int i=1;i<=o.size();i++){ dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1)); for(int j=i+1;j<=e.size();j++) dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1))); } System.out.println(dp[o.size()][e.size()]); } }
0
Non-plagiarised
8c011cb9
e0f6aad6
import java.io.*; import java.util.*; /* polyakoff */ public class Main { static FastReader in; static PrintWriter out; static Random rand = new Random(); static final int oo = (int) 2e9 + 10; static final long OO = (long) 2e18 + 10; static final int MOD = 998244353; static void solve() { int n = in.nextInt(); String[] s = new String[n]; for (int i = 0; i < n; i++) { s[i] = in.next(); } HashSet<String> set = new HashSet<>(); for (int i = 0; i < n; i++) { if (s[i].charAt(0) == s[i].charAt(s[i].length() - 1)) { out.println("YES"); return; } if (s[i].length() == 2) { String t = new StringBuilder(s[i]).reverse().toString(); if (set.contains(t)) { out.println("YES"); return; } for (char c = 'a'; c <= 'z'; c++) { if (set.contains(t + c)) { out.println("YES"); return; } } set.add(s[i]); } else { String t = new StringBuilder(s[i]).reverse().toString(); if (set.contains(t) || set.contains(t.substring(0, 2))) { out.println("YES"); return; } set.add(s[i]); } } out.println("NO"); } public static void main(String[] args) { in = new FastReader(); out = new PrintWriter(System.out); int t = 1; t = in.nextInt(); while (t-- > 0) { solve(); } out.flush(); out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; FastReader() { this(System.in); } FastReader(String file) throws FileNotFoundException { this(new FileInputStream(file)); } FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } String nextLine() { String line; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } } }
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static ArrayList<Integer> primeupton(int n) { ArrayList<Integer> ans= new ArrayList<>(); boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) ans.add(i); } return ans; } public static Map<Integer,Integer> primeFactors(int n) { HashMap<Integer,Integer> map= new HashMap<>(); int count=0; while (n%2==0) { count++; n /= 2; } map.put(2,count); for (int i = 3; i <= Math.sqrt(n); i+= 2) { count=0; while (n%i == 0) { count++; n /= i; } map.put(i,count); } if (n > 2) map.put(n,1); return map; } public static String toBinary(int decimal){ StringBuilder ans= new StringBuilder(); while(decimal > 0){ ans.append(decimal%2) ; decimal = decimal/2; } return ans.reverse().toString(); } private static LinkedList[] adj; static class Sortbyroll implements Comparator<Pair> { @Override public int compare(Pair o1, Pair o2) { return o1.b-o2.b; } // Method // Sorting in ascending order of roll number } static class Pair { int a; int b; Pair(int a, int b) { this.a = a; this.b = b; } public int getB(){ return b; } } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int sum (int n){ return n*(n+1)/2; } public static void main(String[] args) throws Exception { // your code goes here FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); Set<String> list1= new HashSet<>(); Set<String> list2= new HashSet<>(); Set<String> list3= new HashSet<>(); int flag=0; for(int i=0;i<n;i++) { String s = sc.next(); if (flag == 1) { list1.add(s); } else { if (s.length() == 1) { flag = 1; } else if (s.length() == 2) { list2.add(s); StringBuilder x = new StringBuilder(); x.append(s.charAt(1)); x.append(s.charAt(0)); if (list3.contains(x.toString()) || list2.contains(x.toString())) { flag = 1; } } else { list1.add(s); list3.add(s.substring(0, 2)); StringBuilder x = new StringBuilder(); x.append(s.charAt(2)); x.append(s.charAt(1)); x.append(s.charAt(0)); if (list1.contains(x.toString())) flag = 1; StringBuilder y = new StringBuilder(); y.append(s.charAt(2)); y.append(s.charAt(1)); if (list2.contains(y.toString())) flag = 1; } } } if(flag!=1){ System.out.println("NO"); } else System.out.println("YES"); } } }
0
Non-plagiarised
5ee4d2f9
b38468e8
import java.util.*; /** * Created by Brandon on 4/29/2021, 11:04 AM. */ public class Main { static long mod = 998244353; public static void solve() { // string: //char** vals = new char* [n]; //for (int i = 0; i < n; i++) { // string str = ""; // cin >> str; // vals[i] = new char[n]; // for (int j = 0; j < n; j++) { // vals[i][j] = str[j]; // } //} int n; Scanner sc = new Scanner(System.in); n = Integer.parseInt(sc.next()); int[] arr = new int[n]; ArrayList<Integer> ones = new ArrayList<>(); for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(sc.next()); if (arr[i] == 1) { ones.add(i); } } if (ones.size() == 0) { System.out.println("0\n"); return; } int[] zeroes = new int[n - ones.size()]; int cur = 0; for (int i = 0; i < n; i++) { if (arr[i] == 0) { zeroes[cur] = i; cur++; } } int[][] dp = new int[ones.size()][n-ones.size()]; for (int i = 1; i < ones.size(); i++) { dp[i][0] = 100000000; } dp[0][0] = Math.abs(ones.get(0) - zeroes[0]); for (int i = 1; i < n - ones.size(); i++) { dp[0][i] = Math.min(dp[0][i - 1], Math.abs(ones.get(0) - zeroes[i])); } // dp[i][j] is number where you only have to move first i but max position you can put is j for (int i = 1; i < ones.size(); i++) { for (int j = 1; j < n - ones.size(); j++) { dp[i][j] = Math.min(dp[i][j - 1], dp[i-1][j-1] + Math.abs(zeroes[j] - ones.get(i))); } } System.out.println(dp[ones.size()-1][n-ones.size()-1] + "\n"); } public static void main(String[] args) { solve(); } }
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * 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; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); DArmchairs solver = new DArmchairs(); solver.solve(1, in, out); out.close(); } static class DArmchairs { static ArrayList<Integer> empty; static ArrayList<Integer> chair; static long[][] dp; public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] arr = in.nextIntArray(n); empty = new ArrayList<>(); chair = new ArrayList<>(); for (int i = 0; i < n; i++) { int a = arr[i]; if (a == 1) chair.add(i); else empty.add(i); } dp = new long[chair.size()][empty.size()]; for (long[] row : dp) Arrays.fill(row, -1); long ans = rec(0, 0); out.println(ans); } static long rec(int i, int j) { if (i == chair.size()) return 0; int req = chair.size() - i; int have = empty.size() - j; if (req > have) return Integer.MAX_VALUE; if (dp[i][j] != -1) return dp[i][j]; long opt1 = Math.abs(chair.get(i) - empty.get(j)) + rec(i + 1, j + 1); long opt2 = rec(i, j + 1); dp[i][j] = Math.min(opt1, opt2); return dp[i][j]; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(long i) { writer.println(i); } } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public int nextInt() { return Integer.parseInt(next()); } } }
0
Non-plagiarised
558df7d4
f0d91796
import java.io.*; import java.util.*; public class Pupsen { public static void main(String[] args) throws Exception { FastIO in = new FastIO(); int t = in.nextInt(); for (int tc=0; tc<t; tc++) { int n = in.nextInt(); int[] a = new int[n]; for (int i=0; i<n; i++) { a[i] = in.nextInt(); } int[] b = new int[n]; if (n%2==0) { for (int i=0; i<n-1; i+=2) { b[i] = -a[i+1]; b[i+1] = a[i]; } for (int i=0; i<n; i++) System.out.print(b[i]+" "); } else { if (a[0]+a[1]!=0) { b[0] = -a[2]; b[1] = -a[2]; b[2] = a[0]+a[1]; } else if (a[0]+a[2]!=0) { b[0] = -a[1]; b[2] = -a[1]; b[1] = a[0]+a[2]; } else { b[1] = -a[0]; b[2] = -a[0]; b[0] = a[1]+a[2]; } for (int i=3; i<n-1; i+=2) { b[i] = -a[i+1]; b[i+1] = a[i]; } for (int i=0; i<n; i++) System.out.print(b[i]+" "); } System.out.println(); } } static class FastIO { BufferedReader br; StringTokenizer st; PrintWriter pr; public FastIO() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } public String nextLine() throws IOException { String str = br.readLine(); return str; } } }
import java.io.*; import java.util.*; public class cf { 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(); } int ans[]=new int[n]; if(n%2==0){ for(int i=0;i<n;i=i+2){ ans[i]=-arr[i+1]; ans[i+1]=arr[i]; } } else{ if(arr[0]+arr[1]!=0){ ans[0]=-arr[2]; ans[1]=-arr[2]; ans[2]=arr[0]+arr[1]; } else{ if(arr[1]+arr[2]!=0){ ans[1]=-arr[0]; ans[2]=-arr[0]; ans[0]=arr[1]+arr[2]; } else{ ans[0]=-arr[1]; ans[2]=-arr[1]; ans[1]=arr[0]+arr[2]; } } for(int i=3;i<n;i=i+2){ ans[i]=-arr[i+1]; ans[i+1]=arr[i]; } } for(int j=0;j<n;j++){ System.out.print(ans[j]+" "); } System.out.println(); } } } ////////////////////////////////////////////////////////////// // LCM AND GCD /* public static int gcd(int a,int b){ if(b == 0){ return a; } return gcd(b,a%b); } public static int lcm(int a,int b){ return (a / gcd(a, b)) * b; }*/ /////////////////////////////////////////////////////////////////////////////////// //Iterator /*Iterator iterator = object.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next() + " "); }*/ /////////////////////////////////////////////////////////////////////////////////// class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } public char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
1
Plagiarised
4212c4b9
829d2024
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.PriorityQueue; import java.util.StringTokenizer; public class Solve { static int mod = 1000000000 + 7; static long fact[] = new long[2 * 100000 + 3]; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); // blocks int m = sc.nextInt(); // towers to build int x = sc.nextInt(); // maximum diff int heights[] = new int[n]; int sum = 0; PriorityQueue<Pair> pq = new PriorityQueue<Pair>(); for (int i = 0; i < n; i++) { heights[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { pq.add(new Pair(i + 1, 0)); } pw.println("YES"); for (int i = 0; i < n; i++) { Pair p = pq.poll(); p.value += heights[i]; pq.add(p); pw.print(p.tower + " "); } pw.println(); } pw.flush(); } static long gcd(long a, long b) { if (a == 0) return b; if (b == 0) return a; return gcd(b, a % b); } static long nC2(long n) { return n * (n - 1) / 2; } static long nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; // The array C is going to store last // row of pascal triangle at the end. // And last entry of last row is nCr int C[] = new int[r + 1]; C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (int i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (int j = Math.min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } static void sort(int[] a) { ArrayList<Integer> 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); } } class Pair implements Comparable<Pair> { int tower; int value = 0; Pair(int x, int y) { tower = x; value = y; } public int compareTo(Pair o) { return this.value - o.value; } } class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } } //creating a hash table //Hashtable<Integer, String> h = new Hashtable<Integer, String>(); // //Hashtable<Integer, String> h1 = new Hashtable<Integer, String>(); // //h.put(3, "Geeks"); //h.put(2, "forGeeks"); //h.put(1, "isBest"); //System.out.println(h.get(1)); //if (h.containsKey(1)) { // System.out.println(1111111); //} //if (h.containsValue("isBest")) { // System.out.println(1111111); //} // //// create a clone or shallow copy of hash table h //h1 = (Hashtable<Integer, String>) h.clone(); // //// checking clone h1 ////System.out.println("values in clone: " + h1); // //// clear hash table h //h.clear(); //Creating object of the // class linked list //LinkedList<String> ll // = new LinkedList<String>(); // //// Adding elements to the linked list //ll.add("A"); //ll.add("B"); //ll.addLast("C"); //ll.addFirst("D"); //ll.add(2, "E"); // //System.out.println(ll); // //ll.remove("B"); //ll.remove(3); //ll.removeFirst(); //ll.removeLast(); // //System.out.println(ll); //Set<Integer> set = new HashSet<Integer>(); //try { // for(int i = 0; i < 5; i++) { // set.add(count[i]); // } // System.out.println(set); // // TreeSet sortedSet = new TreeSet<Integer>(set); // System.out.println("The sorted list is:"); // System.out.println(sortedSet); // // System.out.println("The First element of the set is: "+ (Integer)sortedSet.first()); // System.out.println("The last element of the set is: "+ (Integer)sortedSet.last()); //} //catch(Exception e) {}
import java.util.*; import java.io.*; public class Main{ public static class Element implements Comparable<Element>{ public int key; public int value; Element(int k, int v) { key=k; value=v; } @Override public int compareTo(Element o) { if(this.value<o.value) return -1; if(this.value>o.value) return 1; return 0; } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int m=sc.nextInt(); int x=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;++i) arr[i]=sc.nextInt(); PriorityQueue<Element> pq=new PriorityQueue<>(); for(int i=1;i<=m;++i) { pq.add(new Element(i,0)); } System.out.println("YES"); for(int j=0;j<n;j++) { Element cur = pq.poll(); System.out.print(cur.key+" "); cur.value+= arr[j]; pq.add(cur); } System.out.println(); } } }
1
Plagiarised
810fd282
ec558d69
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<ArrayList<Integer>> adj=new ArrayList<>(N); static { for(int i=0;i<N;i++) adj.add(new ArrayList<>()); } 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<n;i++) { String[] ss2=br.readLine().split(" "); int u=Integer.parseInt(ss2[0]); int v=Integer.parseInt(ss2[1]); adj.get(u).add(v); adj.get(v).add(u); } dfs(1, -1); System.out.println(Math.max(dp[0][1], dp[1][1])); } } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; public class Main { static int N=(int)2e5+10; static int[][] A=new int[2][N]; static long[][] dp=new long[2][N]; static ArrayList<ArrayList<Integer>> links=new ArrayList<>(N); static { for (int i=0;i<N;i++) links.add(new ArrayList<>()); } static void dfs(int v,int p) { dp[0][v]=dp[1][v]=0; for (Integer link :links.get(v)) { if (link==p) continue; dfs(link,v); dp[0][v]+=Math.max(Math.abs(A[0][v]-A[0][link])+dp[0][link],Math.abs(A[0][v]-A[1][link])+dp[1][link]); dp[1][v]+=Math.max(Math.abs(A[1][v]-A[0][link])+dp[0][link],Math.abs(A[1][v]-A[1][link])+dp[1][link]); } } public static void main (String[] args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()),n,i; while (t-->0) { n=Integer.parseInt(br.readLine()); for (i=1;i<=n;i++) { String[] in=br.readLine().split(" "); A[0][i]=Integer.parseInt(in[0]); A[1][i]=Integer.parseInt(in[1]); links.set(i,new ArrayList<>()); } for (i=1;i<n;i++) { String[] in=br.readLine().split(" "); int a=Integer.parseInt(in[0]); int b=Integer.parseInt(in[1]); links.get(a).add(b); links.get(b).add(a); } dfs(1,-1); System.out.println(Math.max(dp[0][1],dp[1][1])); } } }
1
Plagiarised
1dab88fb
4138b081
import java.util.*; public class Main { static class Edge{ public int node; public int index; public Edge(int n, int i){ node=n; index=i; } } static Scanner sc=new Scanner(System.in); public static void main(String[] args) { int test=sc.nextInt(); while(test-->0){ solve(); } } static void solve(){ int n=sc.nextInt(); ArrayList<ArrayList<Edge>> graph= new ArrayList<ArrayList<Edge>>(); for(int i=0;i<n;i++){ graph.add(new ArrayList<>()); } 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<Edge> edges = graph.get(curNode); Edge next = edges.get(0); if (next.node == prevNode) { if (edges.size() == 1) { break; } else { next = edges.get(1); } } weight[next.index] = curWeight; prevNode = curNode; curNode = next.node; curWeight = 5 - curWeight; } for (int i = 0; i < n - 1; i++) { System.out.print(weight[i]); System.out.print(" "); } System.out.println(); } }
import java.io.*; import java.util.*; public class Contest1627C { static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { // reads in the next string while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { // reads in the next int return Integer.parseInt(next()); } public long nextLong() { // reads in the next long return Long.parseLong(next()); } public double nextDouble() { // reads in the next double return Double.parseDouble(next()); } } static InputReader r = new InputReader(System.in); static PrintWriter pw = new PrintWriter(System.out); static long mod = 1000000007; static ArrayList<Integer>[] adj; static ArrayList<Integer>[] 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<Integer>(); num[i] = new ArrayList<Integer>(); } int[] deg = new int[n]; boolean flag = false; for (int i = 0; i < n - 1; i ++) { int a = r.nextInt()-1; int b = r.nextInt()-1; adj[a].add(b); adj[b].add(a); num[a].add(i); num[b].add(i); deg[a] ++; deg[b] ++; if (deg[a] > 2 || deg[b] > 2) { flag = true; } } if (flag) { pw.println(-1); continue; } ans = new int[n]; for (int i = 0; i < n; i ++) { if (deg[i] == 1) { dfs(i,3,-1); } } for (int i = 0; i < n - 1; i ++) { pw.println(ans[i]); } } pw.close(); } static void dfs(int node, int x, int p) { for (int j = 0; j < adj[node].size(); j ++) { int i = adj[node].get(j); if (i == p) { continue; } ans[num[node].get(j)] = x; dfs(i,5-x,node); } } }
0
Non-plagiarised
46e9aed4
8ddb5587
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<Integer> fill= new ArrayList<Integer>(); ArrayList<Integer> unfilled= new ArrayList<>(); for(int i=0;i<n;i++){ int x =sc.nextInt(); if(x==1){ fill.add(i); }else{ unfilled.add(i); } } Collections.sort(fill); Collections.sort(unfilled); long[][] dp =new long[fill.size()+1][unfilled.size()+1]; for(int i=0;i<fill.size()+1;i++){ for(int j=0;j<unfilled.size()+1;j++){ dp[i][j]=Integer.MAX_VALUE; } } for(int i=0;i<unfilled.size()+1;i++){ dp[0][i]=0; } // for(int j=0;j<fill.size()+1;j++){ // dp[j][0]=0; // } for(int i=1;i<fill.size()+1;i++){ for(int j=1;j<unfilled.size()+1;j++){ dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(fill.get(i-1)-unfilled.get(j-1))); } } System.out.println(dp[fill.size()][unfilled.size()]); // for(int i=0;i<fill.size()+1;i++){ // for(int j=0;j<unfilled.size()+1;j++) // { // System.out.print(dp[i][j]+" "); // } // System.out.println(); // } } } class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } }
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.StringTokenizer; public class taskd { public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); taskd sol = new taskd(); sol.solve(in, out); out.flush(); } void solve(FastScanner in, PrintWriter out) { int n = in.nextInt(); ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); for (int i = 0; i < n; i++) { int x = in.nextInt(); if (x == 1) { a.add(i); } else { b.add(i); } } long dp[][] = new long[a.size() + 5][b.size() + 5]; for (int i = a.size()-1; i >= 0; i--) { dp[i][b.size()] = Integer.MAX_VALUE; for (int j = b.size()-1; j >= 0; j--) { dp[i][j] = dp[i][j + 1]; dp[i][j] = Math.min(dp[i][j], Math.abs(a.get(i) - b.get(j)) + dp[i + 1][j + 1]); } } out.println(dp[0][0]); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
0
Non-plagiarised
5c54f087
c6c7fa0c
import java.util.*; import java.lang.*; import java.io.*; public class Main { PrintWriter out = new PrintWriter(System.out); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""); String next() throws IOException { if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } long nl() throws IOException { return Long.parseLong(next()); } int n,a,b,da,db,dist,vert; ArrayList<Integer>A[]; void solve() throws IOException { for (int tc=ni();tc>0;tc--) { n=ni(); a=ni(); b=ni(); da=ni(); db=ni(); A=new ArrayList[n+1]; for (int i=1;i<=n;i++) A[i]=new ArrayList(); for (int i=1;i<n;i++) { int u=ni(),v=ni(); A[u].add(v); A[v].add(u); } dist=0; dfs1(a,0,0); if (dist<=da || db<=2*da) { out.println("Alice"); continue; } dist=0; vert=0; dfs2(1,0,0); dist=0; dfs2(vert,0,0); if (dist<=2*da) out.println("Alice"); else out.println("Bob"); } out.flush(); } void dfs2(int u,int p,int d) { if (d>dist) { dist=d; vert=u; } for (Integer v:A[u]) { if (v==p) continue; dfs2(v,u,d+1); } } void dfs1(int u,int p,int d) { if (u==b) dist=d; for (Integer v:A[u]) { if (v==p) continue; dfs1(v,u,d+1); } } public static void main(String[] args) throws IOException { new Main().solve(); } }
import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.PI; import static java.lang.System.in; import static java.lang.System.out; public class B { static ArrayList<ArrayList<Integer>> adj; static int dis[]; static void dfs(int s, int p, int l) { dis[s] = l; for(int i : adj.get(s)) { if(i==p) continue; dfs(i, s, l+1); } } public static void main(String[] args) throws Exception { Foster sc = new Foster(); PrintWriter p = new PrintWriter(out); int t = sc.nextInt(); while(t--!=0) { int n = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt(), da = sc.nextInt(), db = sc.nextInt(); adj = new ArrayList<>(); for(int i = 0; i <= n; i++) { adj.add(new ArrayList<>()); } for(int i = 1; i < n; i++) { int u = sc.nextInt(), v = sc.nextInt(); adj.get(u).add(v); adj.get(v).add(u); } dis = new int[n+1]; dfs(a, 0, 0); if(dis[b] <= da) //Alice reached in first move { p.println("Alice"); continue; } int farthest = 0, maxDis = 0; for(int i = 1; i <= n; i++) { if(maxDis < dis[i]) { maxDis = dis[i]; farthest = i; } } dfs(farthest, 0, 0); int diameter = 0; for(int i = 1; i <= n; i++) { diameter = Math.max(diameter, dis[i]); } if(db > 2*da && diameter > 2*da) { p.println("Bob"); } else { p.println("Alice"); } } p.close(); } /* */ /* 1. Check overflow in pow function or in general 2. Check indices of read array function 3. Think of an easier solution because the problems you solve are always easy 4. Check iterator of loop 5. If still doesn't work, then jump from the 729th floor 'coz "beta tumse na ho paayega" Move to top!! */ static class Foster { BufferedReader br = new BufferedReader(new InputStreamReader(in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] intArray(int n) // Check indices { int arr[] = new int[n]; for(int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } long[] longArray(int n) // Check indices { long arr[] = new long[n]; for(int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } int[] getBits(int n) //in Reverse Order { int a[] = new int[31]; for(int i = 0; i < 31; i++) { if(((1<<i) & n) != 0) a[i] = 1; } return a; } static long pow(long... a) { long mod = Long.MAX_VALUE; if(a.length == 3) mod = a[2]; long res = 1; while(a[1] > 0) { if((a[1] & 1) == 1) res = (res * a[0]) % mod; a[1] /= 2; a[0] = (a[0] * a[0]) % mod; } return res; } void print(Object... o) { for(Object next : o) { System.err.print(next + " "); } } void println(Object... o) { for(Object next : o) { System.err.print(next + " "); } System.err.println(); } void watch(Object...a) throws Exception { int i = 1; for (Object o: a) { boolean found = false; if (o.getClass().isArray()) { String type = o.getClass().getName().toString(); switch (type) { case "[I": { int[] test = (int[]) o; println(i + " : " + Arrays.toString(test)); break; } case "[[I": { int[][] obj = (int[][]) o; println(i + " : " + Arrays.deepToString(obj)); break; } case "[J": { long[] obj = (long[]) o; println(i + " : " + Arrays.toString(obj)); break; } case "[[J": { long[][] obj = (long[][]) o; println(i + " : " + Arrays.deepToString(obj)); break; } case "[D": { double[] obj = (double[]) o; println(i + " : " + Arrays.toString(obj)); break; } case "[[D": { double[][] obj = (double[][]) o; println(i + " : " + Arrays.deepToString(obj)); break; } case "[Ljava.lang.String": { String[] obj = (String[]) o; println(i + " : " + Arrays.toString(obj)); break; } case "[[Ljava.lang.String": { String[][] obj = (String[][]) o; println(i + " : " + Arrays.deepToString(obj)); break; } case "[C": { char[] obj = (char[]) o; println(i + " : " + Arrays.toString(obj)); break; } case "[[C": { char[][] obj = (char[][]) o; println(i + " : " + Arrays.deepToString(obj)); break; } default: { println(i + " : type not identified"); break; } } found = true; } if (o.getClass() == ArrayList.class) { println(i + " : LIST = " + o); found = true; } if (o.getClass() == TreeSet.class) { println(i + " : SET = " + o); found = true; } if (o.getClass() == TreeMap.class) { println(i + " : MAP = " + o); found = true; } if (o.getClass() == HashMap.class) { println(i + " : MAP = " + o); found = true; } if (o.getClass() == LinkedList.class) { println(i + " : LIST = " + o); found = true; } if (o.getClass() == PriorityQueue.class) { println(i + " : PQ = " + o); found = true; } if (!found) { println(i + " = " + o); } i++; } } } }
0
Non-plagiarised
921b6e4a
e7dce35b
import java.io.*; import java.util.*; public class D_Java { public static final int MOD = 998244353; public static int mul(int a, int b) { return (int)((long)a * (long)b % MOD); } int[] f; int[] rf; public int C(int n, int k) { return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n-k], rf[k])); } public static int pow(int a, int n) { int res = 1; while (n != 0) { if ((n & 1) == 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } static void shuffleArray(int[] a) { Random rnd = new Random(); for (int i = a.length-1; i > 0; i--) { int index = rnd.nextInt(i + 1); int tmp = a[index]; a[index] = a[i]; a[i] = tmp; } } public static int inv(int a) { return pow(a, MOD-2); } public void doIt() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tok.nextToken()); int k = Integer.parseInt(tok.nextToken()); f = new int[n+42]; rf = new int[n+42]; f[0] = rf[0] = 1; for (int i = 1; i < f.length; ++i) { f[i] = mul(f[i-1], i); rf[i] = mul(rf[i-1], inv(i)); } int[] events = new int[2*n]; for (int i = 0; i < n; ++i) { tok = new StringTokenizer(in.readLine()); int le = Integer.parseInt(tok.nextToken()); int ri = Integer.parseInt(tok.nextToken()); events[i] = le*2; events[i + n] = ri*2 + 1; } shuffleArray(events); Arrays.sort(events); int ans = 0; int balance = 0; for (int r = 0; r < 2*n;) { int l = r; while (r < 2*n && events[l] == events[r]) { ++r; } int added = r - l; if (events[l] % 2 == 0) { // Open event ans += C(balance + added, k); if (ans >= MOD) ans -= MOD; ans += MOD - C(balance, k); if (ans >= MOD) ans -= MOD; balance += added; } else { // Close event balance -= added; } } in.close(); System.out.println(ans); } public static void main(String[] args) throws IOException { (new D_Java()).doIt(); } }
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int n = nextInt(); int k = nextInt(); f = new int[n + 42]; rf = new int[n + 42]; f[0] = 1; rf[0] = 1; for (int i = 1; i < f.length; i++) { f[i] = mul(f[i - 1], i); rf[i] = mul(rf[i - 1], inv(i)); } int[] a = new int[n * 2]; for (int i = 0; i < n; i++) { a[i] = nextInt() * 2; a[i + n] = nextInt() * 2 + 1; } Arrays.sort(a); int ans = 0; int curOpen = 0; for (int r = 0; r < 2 * n;) { int l = r; while (r < 2 * n && a[l] == a[r]) r++; int intersections = r - l; if (a[l] % 2 == 0) { ans += C(curOpen + intersections, k); if (ans >= mod) ans -= mod; ans += mod - C(curOpen, k); if (ans >= mod) ans -= mod; curOpen += intersections; } else { curOpen -= intersections; } } pw.println(ans); pw.close(); } static int mod = 998244353; static int mul(int a, int b) { return (int) ((long) a * (long) b % mod); } static int[] f; static int[] rf; static int C(int n, int k) { return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n - k], rf[k])); } static int pow(int a, int n) { int res = 1; while (n != 0) { if ((n & 1) == 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } static int inv(int a) { return pow(a, mod - 2); } static StringTokenizer st = new StringTokenizer(""); static BufferedReader br; static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } } class Coordinates { int l; int r; public Coordinates(int l, int r) { this.l = l; this.r = r; } } class CoordinatesComparator implements Comparator<Coordinates> { @Override public int compare(Coordinates o1, Coordinates o2) { if (o1.l == o2.l) return Integer.compare(o1.r, o2.r); return Integer.compare(o1.l, o2.l); } }
1
Plagiarised
8be61667
ca3128ab
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=input.nextInt(); while(T-->0) { int n=input.nextInt(); int c[]=new int[n]; for(int i=0;i<n;i++) { c[i]=input.nextInt(); } long me=Integer.MAX_VALUE; long mo=Integer.MAX_VALUE; long se=0,so=0; long min=Long.MAX_VALUE; for(int i=1;i<=n;i++) { if(i%2==0) { me=Math.min(me,c[i-1]); se+=c[i-1]; } else { mo=Math.min(mo,c[i-1]); so+=c[i-1]; } if(i>=2) { long sum=0; long c1=i/2+i%2; long c2=(i-1)/2+(i-1)%2; if(i%2==0) { sum+=se; sum+=(me)*(n-c1); sum+=so; sum+=mo*(n-c2); } else { sum+=so; sum+=(mo)*(n-c1); sum+=se; sum+=me*(n-c2); } min=Math.min(min,sum); } } out.println(min); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
import java.io.*; import java.math.*; import java.util.*; public class test { static class Pair{ long x; long y; Pair(long x,long y){ this.x = x; this.y = y; } } static class Compare { void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.x!=p2.x) { return (int)(p1.x - p2.x); } else { return (int)(p1.y - p2.y); } } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + " " + arr[i].y + " "); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]) throws Exception { Scanner sc = new Scanner(); StringBuilder res = new StringBuilder(); int tc = sc.nextInt(); while(tc-->0) { int n = sc.nextInt(); long c[] = new long[n]; for(int i=0;i<n;i++) { c[i] = sc.nextLong(); } long min_odd = Integer.MAX_VALUE; long min_even = Integer.MAX_VALUE; long ans = Long.MAX_VALUE; long sum = 0; int cnt1 = n; int cnt2 = n; for(int i = 0; i < n; i++){ sum += c[i]; if(i % 2 == 0){ cnt1--; min_odd = Math.min(min_odd, c[i]); } else{ cnt2--; min_even = Math.min(min_even, c[i]); } if(i > 0){ long temp = sum + (min_odd * cnt1) + (min_even * cnt2); ans = Math.min(ans, temp); } } System.out.println(ans); } System.out.println(res); } }
0
Non-plagiarised
72d9eb5b
884f5678
import java.util.*; import java.io.*; public class D { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); while(T-- > 0) { int n = in.nextInt(); int[] a = new int[n]; for(int j=0;j<n;j++) a[j] = in.nextInt(); char[] s = in.next().toCharArray(); List<Integer> blue = new ArrayList<>(); List<Integer> red = new ArrayList<>(); for(int j=0;j<n;j++) { if(s[j] == 'B') blue.add(a[j]); else red.add(a[j]); } Collections.sort(blue); Collections.sort(red); boolean p = true; int cur = 1; for(int val : blue) { if(val<cur) { p = false; break; } else cur++; } for(int val : red) { if(val>cur) { p = false; break; } else cur++; } if(p) System.out.println("yes"); else System.out.println("no"); } } }
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<ArrayList<Integer>> adj; public static void main(String[] args) throws Exception{ class Element{ int x; char c; public Element(int y,char k) { x=y; c=k; } } int z=in.readInt(); while(z-->0) { int n=in.readInt(); int a[]=nia(n); char c[]=in.readString().toCharArray(); ArrayList<Integer> d=new ArrayList<>(); ArrayList<Integer> in=new ArrayList<>(); for(int i=0;i<n;i++) { if(c[i]=='R') { in.add(a[i]); } else { d.add(a[i]); } } String ans="YES"; Collections.sort(d); int k=1; for(int i:d) { if(i<k) { ans="NO"; } k++; } Collections.sort(in); for(int i=in.size()-1;i>=0;i--) { if(in.get(i)>n) { ans="NO"; break; } n--; } System.out.println(ans); } } static long ans(long l,long r,long x,long y) { long mid=(r-l)/2+l; long a=mid%x; long b=y%mid; long c=0; if(l>r) { return 0; } if(a==b) { return mid; } else if(a<b) { c=ans(l,mid-1,x,y); if(c==0) { c=ans(mid+1,r,x,y); } } else{ c=ans(l,mid-1,x,y); if(c==0) { c=ans(mid+1,r,x,y); } } return c; } static int[] nia(int n){ int[] arr= new int[n]; int i=0; while(i<n){ arr[i++]=in.readInt(); } return arr; } static long[] nla(int n){ long[] arr= new long[n]; int i=0; while(i<n){ arr[i++]=in.readLong(); } return arr; } static int[] nia1(int n){ int[] arr= new int[n+1]; int i=1; while(i<=n){ arr[i++]=in.readInt(); } return arr; } static long gcd(long a, long b) { if (b==0) return a; return gcd(b, a%b); } static class Scanner{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String readString() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } double readDouble() { return Double.parseDouble(readString()); } int readInt() { return Integer.parseInt(readString()); } long readLong() { return Long.parseLong(readString()); } } }
0
Non-plagiarised
31f29772
d04e5afb
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.PriorityQueue; import java.util.StringTokenizer; import java.io.*; public class PhoenixTow { private static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair p) { return ((Integer)this.x).compareTo(p.x); } } public static void solution(int[] arr, int n, int m, int x) { ArrayList<Pair> list = new ArrayList<>(); for(int i = 0; i<n; i++) { list.add(new Pair(arr[i], i)); } Collections.sort(list); long[] sum = new long[m]; int[] ans = new int[n]; int k = 1; for(int i = 0; i<list.size(); i++) { if(k<m) { if(sum[k-1]+list.get(i).x - sum[k]>x) {out.println("NO"); return; } } sum[k-1]+=list.get(i).x; ans[list.get(i).y] = k; k++; if(k==(m+1)) k=1; } out.println("YES"); for(int i = 0; i<n; i++) out.print(ans[i]+" "); out.println(); } private static PrintWriter out = new PrintWriter(System.out); public static void main (String[] args) { MyScanner s = new MyScanner(); int t = s.nextInt(); for(int j = 0; j<t ; j++) { int n = s.nextInt(); int m = s.nextInt(); int x = s.nextInt(); int[] arr = new int[n]; for(int i =0; i<n; i++) arr[i] = s.nextInt(); solution(arr,n,m,x); } out.flush(); out.close(); } }
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.Objects; import java.util.Collections; 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); CPhoenixAndTowers solver = new CPhoenixAndTowers(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class CPhoenixAndTowers { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(); ArrayList<Pair<Integer, Integer>> a = new ArrayList<>(); for (int i = 0; i < n; ++i) { a.add(new Pair<>(in.nextInt(), i)); } Collections.sort(a); int[] ans = new int[n]; int[] sum = new int[m]; int j = 1; for (int i = 0; i < n; ++i) { ans[a.get(i).y] = j; sum[j - 1] += a.get(i).x; j++; if (j == m + 1) j = 1; } for (int i = 1; i < m; ++i) { if (Math.abs(sum[i - 1] - sum[i]) > k) { out.println("NO"); } } out.println("YES"); for (int e : ans) { out.print(e + " "); } out.println(); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class Pair<U, V> implements Comparable<Pair<U, V>> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int compareTo(Pair<U, V> o) { int value = ((Comparable<U>) x).compareTo(o.x); if (value != 0) return value; return ((Comparable<V>) y).compareTo(o.y); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return x.equals(pair.x) && y.equals(pair.y); } public int hashCode() { return Objects.hash(x, y); } } }
0
Non-plagiarised
201e3463
de599e42
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.TreeSet; public class D { public static void main(String[] args) { CP sc =new CP(); int tt = sc.nextInt(); while (tt-- > 0) { int n = sc.nextInt(); TreeSet<Long> set = new TreeSet<>(); long prev = -1; boolean flag = true; for (int i = 0; i < n; i++) { long x = sc.nextInt(); if (i == 0) { set.add(x); prev = x; continue; } if (x > prev) { Long high = set.higher(prev); if (high == null) set.add(x); else if (high >= x) set.add(x); else flag = false; } else if (x < prev) { Long low = set.lower(prev); if (low == null) set.add(x); else if (low <= x) set.add(x); else flag = false; } prev = x; } System.out.println(flag ? "YES" : "NO"); } } /*****************************************************************************/ static class CP { BufferedReader bufferedReader; StringTokenizer stringTokenizer; public CP() { bufferedReader = new BufferedReader(new InputStreamReader(System.in)); } int nextInt() { return Integer.parseInt(NNNN()); } long nextLong() { return Long.parseLong(NNNN()); } double nextDouble() { return Double.parseDouble(NNNN()); } String NNNN() { while (stringTokenizer == null || !stringTokenizer.hasMoreElements()) { try { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return stringTokenizer.nextToken(); } String nextLine() { String spl = ""; try { spl = bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); } return spl; } } /*****************************************************************************/ }
import java.util.*; import java.io.*; public class _724 { public static void main(String[] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); TreeSet<Long> set = new TreeSet<>(); long prev = -1; boolean ok = true; for (int i = 0; i < n; i++) { long x = sc.nextInt(); if (i == 0) { set.add(x); prev = x; continue; } if (x > prev) { Long high = set.higher(prev); if (high == null) set.add(x); else if (high >= x) set.add(x); else { ok = false; } } else if (x < prev) { Long low = set.lower(prev); if (low == null) set.add(x); else if (low <= x) set.add(x); else { ok = false; } } prev = x; } out.println(ok ? "YES" : "NO"); } out.close(); } static void sort(int[] a) { ArrayList<Integer> q = new ArrayList<>(); for (int i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static void sort(long[] a) { ArrayList<Long> q = new ArrayList<>(); for (long i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1
Plagiarised
cdb801a1
ed610dc9
//package codeforces; import java.io.*; import java.util.*; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws java.lang.Exception { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = fr.ni(); while(t-->0) { int n = fr.ni(); long arr [] = new long[n]; for(int i = 0 ; i < n ; i++) arr[i]= fr.nl(); long even = arr[0]; long odd = arr[1]; long minEven = arr[0]; long minOdd = arr[1]; long ans = (minEven*n) + (minOdd*n); for(int i = 2 ; i < n ; i++) { if((i&1) == 0) { even += arr[i]; minEven = Math.min(minEven, arr[i]); long a = (i+2)/2; long b = (i+1)-a; long temp = (even + (minEven*(n - a))); temp += (odd + (minOdd*(n - b))); ans = Math.min(ans, temp); }else { odd += arr[i]; minOdd = Math.min(minOdd, arr[i]); long a = (i+2)/2; long b = (i+1)-a; long temp = (even + (minEven*(n - a))); temp += (odd + (minOdd*(n - b))); ans = Math.min(ans, temp); } } out.println(ans); } out.close(); } }
/* * akshaygupta26 */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.Random; import java.util.Arrays; import java.util.StringTokenizer; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Collections; import java.util.*; public class C { public static void main(String[] args) { FastReader sc=new FastReader(); StringBuffer ans=new StringBuffer(); int test=sc.nextInt(); while(test-->0) { int n=sc.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); } long nn=n; long ep=1; long op=1; long se=arr[0]; long so=arr[1]; long sume=se;long sumo=so; long minm = (se*nn) +(so*nn); for(int i=2;i<n;i++) { if(i%2 == 0) { ++ep; sume+=arr[i]; se=Math.min(se, arr[i]); } else { ++op; sumo+=arr[i]; so=Math.min(so, arr[i]); } long cost = (sume)+(se*(nn-ep)); cost+=((sumo)+(so*(nn-op))); minm=Math.min(cost, minm); } ans.append(minm+"\n"); } System.out.print(ans); } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
0
Non-plagiarised
bf0df1d5
d1cd194e
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; import java.io.IOException; public class C_Phoenix_and_Towers { public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static class Pair implements Comparable<Pair> { int id, h; public Pair(int id, int h) { this.id = id; this.h = h; } public int compareTo(Pair o) { return this.h - o.h; } } public static void main(String[] args) throws java.lang.Exception { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int x = sc.nextInt(); int tow[] = new int[n]; int ans[] = new int[n]; PriorityQueue<Pair> pq = new PriorityQueue<>(); for (int i = 0; i < n; i++) { tow[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { ans[i] = i + 1; pq.add(new Pair(i + 1, tow[i])); } for (int i = m; i < n; i++) { Pair p = pq.poll(); p.h = p.h + tow[i]; ans[i] = p.id; pq.add(p); } System.out.println("YES"); for (int i = 0; i < n; i++) { System.out.print(ans[i] + " "); } System.out.println(); } } }
import java.util.*; import java.lang.*; import java.io.*; public class Template { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null ||!st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // static void solve(String s) // { //// Scanner sc = new Scanner(System.in); //// String s = sc.next(); // // int x[] = new int[2]; // x[0] = x[1] = -1; // // int ans = 0; // int n = s.length(); // for(int i=0;i<n;i++) // { // int c = s.charAt(i) - '0'; // if(c == 1 || c == 0) // { // x[(i%2) ^ c] = i; // } // int min = Math.min(x[0], x[1]); // ans += i - min; // //System.out.println(ans); // } // System.out.println(ans); // } // // public static void main(String args[]) // { // FastReader sc = new FastReader(); // //solve(); // //Scanner sc = new Scanner(System.in) // int testcases = sc.nextInt(); // nTest is the number of treasure hunts. // //// int testcases = 3; // while(testcases-- > 0) // { // String s = sc.next(); // solve(s); // // } // // } static class Pair implements Comparable<Pair> { int h; int ind; Pair(int h, int ind) { this.h = h; this.ind = ind; } @Override public int compareTo(Pair o) { return this.h - o.h; } } public static void main(String[] args) { FastReader fs=new FastReader(); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int n = fs.nextInt(); int m = fs.nextInt(); int x = fs.nextInt(); if(n < m) { System.out.println("NO"); continue; } Pair a[] = new Pair[n]; PriorityQueue<Pair> heap = new PriorityQueue<>(); for(int i=0;i<n;i++) { a[i] = new Pair(fs.nextInt(), i); } Arrays.sort(a); for(int i=1;i<=m;i++) { heap.add(new Pair(0, i)); } int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; int ans[] = new int[n]; int idx = 0; while(!heap.isEmpty() && idx < n) { Pair curr = heap.poll(); curr.h += a[idx].h; ans[a[idx].ind] = curr.ind; heap.add(new Pair(curr.h, curr.ind)); idx++; } // int towers[] = new int[m+1]; // int tower = 1; // boolean flag = false; // boolean inc = true; // for(int i=0;i<n;i++) // { // if(tower == m+1) // { // tower = m; // inc = false; // } // if(tower == 0) // { // tower = 1; // inc = true; // } // towers[tower] += a[i].h; // System.out.println(a[i].h +" THis" + tower); //// min = Math.min(min, towers[tower]); //// max = Math.max(max, towers[tower]); // ans[a[i].ind] = tower; //// if(Math.abs(max - min) > x) //// { //// System.out.println("NO" + a[i].ind+" "+a[i].h +" "+min +" "+max); //// flag = true; //// break; //// } // if(inc) // tower++; // else // tower--; // } // for(int i=1;i<=m;i++) // { // min = Math.min(min, towers[i]); // max = Math.max(max, towers[i]); // } // if(Math.abs(max - min) > x) // { // System.out.println("NO" + max+" "+min);// + a[i].ind+" "+a[i].h +" "+min +" "+max); // //flag = true; // continue; // } // if(flag) // continue; System.out.println("YES"); for(int i:ans) System.out.print(i+" "); System.out.println(); } } }
0
Non-plagiarised
bdfe8110
c57a973e
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; public class E { public static void main(String[] args) { FastScanner fs=new FastScanner(); int T=fs.nextInt(); PrintWriter out=new PrintWriter(System.out); for (int tt=0; tt<T; tt++) { int n=fs.nextInt(), k=fs.nextInt(); int[] positions=fs.readArray(k), temps=fs.readArray(k); int[] forced=new int[n]; Arrays.fill(forced, Integer.MAX_VALUE/2); for (int i=0; i<k; i++) forced[positions[i]-1]=temps[i]; for (int i=1; i<n; i++) forced[i]=Math.min(forced[i], forced[i-1]+1); for (int i=n-2; i>=0; i--) forced[i]=Math.min(forced[i], forced[i+1]+1); for (int i=0; i<n; i++) out.print(forced[i]+" "); out.println(); } out.close(); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Air { public static void main(String[] args) { FastScanner sc = new FastScanner(); int T = sc.nextInt(); for(int tt=0; tt<T;tt++){ int n = sc.nextInt(), k=sc.nextInt(); int [] positions=new int[k], temp=new int[k]; for (int i=0;i<k;i++) positions[i]=sc.nextInt(); for (int i=0;i<k;i++) temp[i]=sc.nextInt(); int[] forced=new int[n]; Arrays.fill(forced, Integer.MAX_VALUE/2); for (int i=0;i<k;i++) forced[positions[i]-1]=temp[i]; for (int i=1;i<n;i++) forced[i]=Math.min(forced[i], forced[i-1]+1); for (int i=n-2;i>=0;i--) forced[i]=Math.min(forced[i], forced[i+1]+1); for (int i=0;i<n;i++) System.out.print(forced[i]+" "); System.out.println(); } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
1
Plagiarised
6be98ece
e1c4f3db
import java.util.*; public class CF763C { private static final int MAX = 1000000001; private static final int MIN = 0; public static final 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 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
Non-plagiarised
4138b081
f59d9b6e
import java.io.*; import java.util.*; public class Contest1627C { static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { // reads in the next string while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { // reads in the next int return Integer.parseInt(next()); } public long nextLong() { // reads in the next long return Long.parseLong(next()); } public double nextDouble() { // reads in the next double return Double.parseDouble(next()); } } static InputReader r = new InputReader(System.in); static PrintWriter pw = new PrintWriter(System.out); static long mod = 1000000007; static ArrayList<Integer>[] adj; static ArrayList<Integer>[] 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<Integer>(); num[i] = new ArrayList<Integer>(); } int[] deg = new int[n]; boolean flag = false; for (int i = 0; i < n - 1; i ++) { int a = r.nextInt()-1; int b = r.nextInt()-1; adj[a].add(b); adj[b].add(a); num[a].add(i); num[b].add(i); deg[a] ++; deg[b] ++; if (deg[a] > 2 || deg[b] > 2) { flag = true; } } if (flag) { pw.println(-1); continue; } ans = new int[n]; for (int i = 0; i < n; i ++) { if (deg[i] == 1) { dfs(i,3,-1); } } for (int i = 0; i < n - 1; i ++) { pw.println(ans[i]); } } pw.close(); } static void dfs(int node, int x, int p) { for (int j = 0; j < adj[node].size(); j ++) { int i = adj[node].get(j); if (i == p) { continue; } ans[num[node].get(j)] = x; dfs(i,5-x,node); } } }
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef{ public static class Edge{ int node; int index; Edge(int node, int index){ this.node = node; this.index = index; } } static Scanner scn = new Scanner(System.in); public static void main (String[] args) throws java.lang.Exception{ int t = scn.nextInt(); while(t-->0){ solve(); } } public static void solve(){ int n = scn.nextInt(); ArrayList<Edge>[]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){ ArrayList<Edge>edges = graph[curNode]; Edge next = edges.get(0); if(next.node == prevNode){ if(edges.size() == 1){ break; }else{ next = edges.get(1); } } weight[next.index] = curWeight; prevNode = curNode; curNode = next.node; curWeight = 5 - curWeight; } for(int i = 0; i < n - 1; i++){ System.out.print(weight[i]); System.out.print(" "); } System.out.println(); } }
0
Non-plagiarised
1162c08f
6653a758
import java.util.*; public class CodeForces1525C{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); ArrayList<Integer> o=new ArrayList<Integer>(), e=new ArrayList<Integer>(); 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<Integer> o=new ArrayList<Integer>(), e=new ArrayList<Integer>(); 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
Plagiarised
e431de28
eb6cfca7
import java.io.*; import java.lang.*; import java.util.*; public class MinGridPath { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0){ int n = s.nextInt(); long[] aa =new long[n]; for(int i=0;i<n;i++) aa[i]=s.nextLong(); long minEven = aa[0]; long minOdd = aa[1]; long sum = aa[0]+aa[1]; long best = n*minEven + n*minOdd; int numOdd = 1; int numEven = 1; for(int i=2; i<n; ++i) { if(i%2 == 0) { minEven = Math.min(aa[i], minEven); numEven++; }else { minOdd = Math.min(aa[i], minOdd); numOdd++; } sum += aa[i]; long score = sum; score += minEven*(n-numEven); score += minOdd*(n-numOdd); best = Math.min(best, score); } System.out.println(best); } } }
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.List; import java.util.Map.Entry; import java.util.TreeMap; public class Main { public Main() throws FileNotFoundException { // File file = Paths.get("input.txt").toFile(); // if (file.exists()) { // System.setIn(new FileInputStream(file)); // } long t = System.currentTimeMillis(); InputReader reader = new InputReader(); int ttt = reader.nextInt(); for (int tt = 0; tt < ttt; tt++) { int n=reader.nextInt(); long[] s=new long[n]; for(int i=0;i<n;i++) { s[i]=reader.nextLong(); } long smallest1=s[0]; long smallest2=s[1]; long val=n*s[0]+n*s[1]; int left1=n-1; int left2=n-1; long base=s[0]+s[1]; for(int i=2;i<n;i++) { if(i%2==0) { //left1 val=Math.min(val, base+left2*smallest2+left1*s[i]); base+=s[i]; smallest1=Math.min(smallest1, s[i]); left1--; }else { val=Math.min(val, base+left1*smallest1+left2*s[i]); base+=s[i]; smallest2=Math.min(smallest2, s[i]); left2--; //left2 } } System.out.println(val); } } static class InputReader { private byte[] buf = new byte[16384]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static void main(String[] args) throws FileNotFoundException { new Main(); } }
0
Non-plagiarised