exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
sequencelengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
09aa461995d6511bf61b8e8181e11f70
train_110.jsonl
1638110100
While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.OutputStream; import java.util.Arrays; import java.io.Writer; import java.io.IOException; import java.util.InputMismatchException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Nipuna Samarasekara */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, FastScanner in, FastPrinter out) { int t = in.nextInt(); boolean[] isPrime = MathUtils.genPrimesBoolean(1000007); while (t-- > 0) { int n = in.nextInt(), e = in.nextInt(); int[] a = in.readIntArray(n); int[] onesl = new int[n]; int[] onesr = new int[n]; for (int i = 0; i < n; i++) { if (a[i] == 1) { onesl[i] = 1; if (i - e >= 0) { onesl[i] += onesl[i - e]; } } } for (int i = n - 1; i >= 0; i--) { if (a[i] == 1) { onesr[i] = 1; if (i + e < n) { onesr[i] += onesr[i + e]; } } } long ans = 0; for (int i = 0; i < n; i++) { if (isPrime[a[i]]) { long left = 0; long right = 0; if (i - e >= 0) { left = onesl[i - e]; } if (i + e < n) { right = onesr[i + e]; } ans += (left + 1) * (right + 1) - 1; } } out.println(ans); } } } static class MathUtils { public static boolean[] genPrimesBoolean(int n) { boolean[] isPrime = new boolean[n]; Arrays.fill(isPrime, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i < n; i++) { if (!isPrime[i]) { continue; } for (int j = i * i; j < n; j += i) { isPrime[j] = false; } } return isPrime; } } static class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } } static class FastScanner extends BufferedReader { public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); // if (isEOF && ret < 0) { // throw new InputMismatchException(); // } // isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (c >= 0 && !isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } }
Java
["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"]
2 seconds
["2\n0\n4\n0\n5\n0"]
NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
Java 8
standard input
[ "binary search", "dp", "implementation", "number theory", "schedules", "two pointers" ]
32130f939336bb6f2deb4dfa5402867d
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,400
For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions.
standard output
PASSED
a6eb027d75bbdd08151f9bcf5ffa00c0
train_110.jsonl
1638110100
While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; /** * @author Naitik * */ public class Main { static FastReader sc=new FastReader(); static long dp[][][]; //static int v[][]; // static int mod=998244353;; static int mod=1000000007; static int max; static int bit[]; // static int seg[]; //static long fact[]; // static long A[]; // static TreeMap<Integer,Integer> map; //static StringBuffer sb=new StringBuffer(""); static HashMap<Integer,Integer> map; static PrintWriter out=new PrintWriter(System.out); static TreeSet<Long> set=new TreeSet<Long>(); public static void main(String[] args) { // StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); boolean prime[] = new boolean[2000000+1]; for(int i=0;i<=2000000;i++) prime[i] = true; for(int p = 2; p*p <=2e6; p++) { // If prime[p] is not changed, then it is a prime if(prime[p] == true) { // Update all multiples of p for(int i = p*p; i <= 2e6; i += p) prime[i] = false; } } outer :while (ttt-- > 0) { int n=i(); int e=i(); int A[]=input(n); ArrayList<Integer> l=new ArrayList<Integer>(); ArrayList<Integer> a=new ArrayList<Integer>(); ArrayList<Integer> b=new ArrayList<Integer>(); long ans=0; for(int i=1;i<=e;i++) { l.clear(); a.clear(); b.clear(); for(int j=i;j<=n;j+=e) { l.add(A[j-1]); } // System.out.println(l); int c=0; for(int j=0;j<l.size();j++) { int y=l.get(j); if(y!=1) { if(prime[y]) a.add(c); c=0; } else { c++; } } c=0; for(int j=l.size()-1;j>=0;j--) { int y=l.get(j); if(y!=1) { if(prime[y]) b.add(c); c=0; } else { c++; } } Collections.reverse(b); //System.out.println(a.size()+" "+b.size()); for(int j=0;j<a.size();j++) { long t=b.get(j); long t1=a.get(j); ans+=t1*(t+1); ans+=t; } } System.out.println(ans); } //System.out.println(sb.toString()); out.close(); //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 } static class Pair implements Comparable<Pair> { int x; int y; int z; Pair(int x,int y){ this.x=x; this.y=y; // this.z=z; } @Override public int compareTo(Pair o) { if(this.x>o.x) return -1; else if(this.x<o.x) return 1; else { if(this.y>o.y) return 1; else if(this.y<o.y) return -1; else return 0; } } // public int hashCode() // { // final int temp = 14; // int ans = 1; // ans =x*31+y*13; // return ans; // } // @Override // public boolean equals(Object o) // { // if (this == o) { // return true; // } // if (o == null) { // return false; // } // if (this.getClass() != o.getClass()) { // return false; // } // Pair other = (Pair)o; // if (this.x != other.x || this.y!=other.y) { // return false; // } // return true; // } // /* FOR TREE MAP PAIR USE */ // public int compareTo(Pair o) { // if (x > o.x) { // return 1; // } // if (x < o.x) { // return -1; // } // if (y > o.y) { // return 1; // } // if (y < o.y) { // return -1; // } // return 0; // } } //static int find(int A[],int a) { // if(A[a]<0) // return a; // return A[a]=find(A, A[a]); //} static int find(int A[],int a) { if(A[a]==a) return a; return find(A, A[a]); } //FENWICK TREE static void update(int i, int x){ for(; i < bit.length; i += (i&-i)) bit[i] += x; } static int sum(int i){ int ans = 0; for(; i > 0; i -= (i&-i)) ans += bit[i]; return ans; } //END static void add(int v) { if(!map.containsKey(v)) { map.put(v, 1); } else { map.put(v, map.get(v)+1); } } static void remove(int v) { if(map.containsKey(v)) { map.put(v, map.get(v)-1); if(map.get(v)==0) map.remove(v); } } public static int upper(int A[],int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { ans=mid; l=mid+1; } else { u=mid-1; } } return ans; } public static int lower(int A[],int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { l=mid+1; } else { ans=mid; u=mid-1; } } return ans; } static int[] copy(int A[]) { int B[]=new int[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long[] copy(long A[]) { long B[]=new long[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void reverse(long A[]) { int n=A.length; long B[]=new long[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void reverse(int A[]) { int n=A.length; int B[]=new int[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static int[][] input(int n,int m){ int A[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=i(); } } return A; } static char[][] charinput(int n,int m){ char A[][]=new char[n][m]; for(int i=0;i<n;i++) { String s=s(); for(int j=0;j<m;j++) { A[i][j]=s.charAt(j); } } return A; } static int nextPowerOf2(int n) { n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } static int highestPowerof2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static long highestPowerof2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long [] prefix(long A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] prefix(int A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] suffix(long A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static long [] suffix(int A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static void fill(int dp[]) { Arrays.fill(dp, -1); } static void fill(int dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(int dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(int dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static void fill(long dp[]) { Arrays.fill(dp, -1); } static void fill(long dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(long dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(long dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long power(long x, long y, long p) { if(y==0) return 1; if(x==0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Integer> hash(int A[]){ HashMap<Integer,Integer> map=new HashMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static HashMap<Long,Integer> hash(long A[]){ HashMap<Long,Integer> map=new HashMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Integer,Integer> tree(int A[]){ TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Long,Integer> tree(long A[]){ TreeMap<Long,Integer> map=new TreeMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"]
2 seconds
["2\n0\n4\n0\n5\n0"]
NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
Java 8
standard input
[ "binary search", "dp", "implementation", "number theory", "schedules", "two pointers" ]
32130f939336bb6f2deb4dfa5402867d
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,400
For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions.
standard output
PASSED
c6ee6780fc3a07bbb758b99cd7ba1f8c
train_110.jsonl
1638110100
While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers.
256 megabytes
import java.io.*; import java.util.*; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter pr = new PrintWriter(System.out); static String readLine() throws IOException { return br.readLine(); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(readLine()); return st.nextToken(); } static int readInt() throws IOException { return Integer.parseInt(next()); } static long readLong() throws IOException { return Long.parseLong(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readChar() throws IOException { return next().charAt(0); } static class Pair implements Comparable<Pair> { int f, s; Pair(int f, int s) { this.f = f; this.s = s; } public int compareTo(Pair other) { if (this.f != other.f) return this.f - other.f; return this.s - other.s; } } final static int A = 1000000; static boolean prime[] = new boolean[A + 1]; static void solve() throws IOException { int n = readInt(), e = readInt(), a[] = new int[n + 1]; for (int i = 1; i <= n; ++i) a[i] = readInt(); long ans = 0, dp1[] = new long[n + 1], dp2[] = new long[n + 1]; for (int start = 1; start <= n; ++start) { int end = 0; for (int i = start; i <= n; i += e) { if (a[i] == 1 && i - e >= 1) dp1[i] = dp1[i - e] + 1; else if (a[i] == 1) dp1[i] = 1; else dp1[i] = 0; end = i; } for (int i = end; i >= start; i -= e) { if (a[i] == 1 && i + e <= n) dp2[i] = dp2[i + e] + 1; else if (a[i] == 1) dp2[i] = 1; else dp2[i] = 0; } for (int i = start; i <= end; i += e) { if (!prime[a[i]]) continue; long mul1 = 1; if (i - e >= 1) mul1 = dp1[i - e] + 1; long mul2 = 1; if (i + e <= n) mul2 = dp2[i + e] + 1; ans += mul1*mul2 - 1; } if (start%e == 0) break; } pr.println(ans); } public static void main(String[] args) throws IOException { //solve(); Arrays.fill(prime, true); prime[0] = prime[1] = false; for (int i = 2; i <= A; ++i) { if (!prime[i]) continue; for (int j = 2*i; j <= A; j += i) prime[j] = false; } for (int t = readInt(); t > 0; --t) solve(); pr.close(); } }
Java
["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"]
2 seconds
["2\n0\n4\n0\n5\n0"]
NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
Java 8
standard input
[ "binary search", "dp", "implementation", "number theory", "schedules", "two pointers" ]
32130f939336bb6f2deb4dfa5402867d
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,400
For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions.
standard output
PASSED
389755b2a1a69ecb6fd622e1c08a93f3
train_110.jsonl
1638110100
While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers.
256 megabytes
import java.util.*; import java.io.*; public class Complex_Market_Analysis { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void shuffle(int[] a) { Random r = new Random(); for (int i = 0; i <= a.length - 2; i++) { int j = i + r.nextInt(a.length - i); swap(a, i, j); } Arrays.sort(a); } public static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void main(String[] args) { // TODO Auto-generated method stub FastReader t = new FastReader(); PrintWriter o = new PrintWriter(System.out); int test = t.nextInt(); while (test-- > 0) { int n = t.nextInt(); int k = t.nextInt(); int[] a = new int[n]; long ans = 0; for (int i = 0; i < n; ++i) a[i] = t.nextInt(); for (int i = 0; i < n; ++i) { if (isPrime(a[i])) { long left = 1, right = 1; int l = i - k, r = i + k; while (l >= 0 && a[l] == 1) { ++left; l -= k; } while (r < n && a[r] == 1) { ++right; r += k; } ans += left * right - 1; } } o.println(ans); } o.flush(); o.close(); } private static boolean isPrime(int n) { if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.sqrt(n); i += 2) if (n % i == 0) return false; return true; } }
Java
["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"]
2 seconds
["2\n0\n4\n0\n5\n0"]
NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
Java 8
standard input
[ "binary search", "dp", "implementation", "number theory", "schedules", "two pointers" ]
32130f939336bb6f2deb4dfa5402867d
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,400
For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions.
standard output
PASSED
73903d9e96aa8709e437417836ec959d
train_110.jsonl
1638110100
While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers.
256 megabytes
import java.io.*;import java.util.*;import java.math.*;import static java.lang.Math.*;import static java. util.Map.*;import static java.util.Arrays.*;import static java.util.Collections.*; import static java.lang.System.*; public class Main { public void tq()throws Exception { st=new StringTokenizer(bq.readLine()); int tq=i(); sb=new StringBuilder(2000000); boolean s[]=si(1000000); o: while(tq-->0) { int n=i(); int e=i(); int ar[]=ari(n); long c=0l; int p[]=new int[n]; for(int x=0;x<e;x++) { int o=0; int en=-1; for(int y=x;y<n;y+=e) { en=y; if(ar[y]==1) { o++; p[y]=o; } else { if(!s[ar[y]]) { o++; p[y]=o; } o=0; } } o=0; for(int y=en;y>=0;y-=e) { if(ar[y]==1)o++; else { if(!s[ar[y]]) { o++; c+=1l*p[y]*o-1; } o=0; } } } sl(c); } p(sb); } void f(){out.flush();} int di[][]={{-1,0},{1,0},{0,-1},{0,1}}; int de[][]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}}; long mod=1000000007l;int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;long maxl=Long.MAX_VALUE, minl=Long. MIN_VALUE;BufferedReader bq=new BufferedReader(new InputStreamReader(in));StringTokenizer st; StringBuilder sb;public static void main(String[] a)throws Exception{new Main().tq();}int[] so(int ar[]) {Integer r[]=new Integer[ar.length];for(int x=0;x<ar.length;x++)r[x]=ar[x];sort(r);for(int x=0;x<ar.length; x++)ar[x]=r[x];return ar;}long[] so(long ar[]){Long r[]=new Long[ar.length];for(int x=0;x<ar.length;x++) r[x]=ar[x];sort(r);for(int x=0;x<ar.length;x++)ar[x]=r[x];return ar;} char[] so(char ar[]) {Character r[]=new Character[ar.length];for(int x=0;x<ar.length;x++)r[x]=ar[x];sort(r);for(int x=0;x<ar.length;x++) ar[x]=r[x];return ar;}void s(String s){sb.append(s);}void s(int s){sb.append(s);}void s(long s){sb. append(s);}void s(char s){sb.append(s);}void s(double s){sb.append(s);}void ss(){sb.append(' ');}void sl (String s){sb.append(s);sb.append("\n");}void sl(int s){sb.append(s);sb.append("\n");}void sl(long s){sb .append(s);sb.append("\n");}void sl(char s) {sb.append(s);sb.append("\n");}void sl(double s){sb.append(s) ;sb.append("\n");}void sl(){sb.append("\n");}int l(int v){return 31-Integer.numberOfLeadingZeros(v);} long l(long v){return 63-Long.numberOfLeadingZeros(v);}int sq(int a){return (int)sqrt(a);}long sq(long a) {return (long)sqrt(a);}long gcd(long a,long b){while(b>0l){long c=a%b;a=b;b=c;}return a;}int gcd(int a,int b) {while(b>0){int c=a%b;a=b;b=c;}return a;}boolean pa(String s,int i,int j){while(i<j)if(s.charAt(i++)!= s.charAt(j--))return false;return true;}boolean[] si(int n) {boolean bo[]=new boolean[n+1];bo[0]=true;bo[1] =true;for(int x=4;x<=n;x+=2)bo[x]=true;for(int x=3;x*x<=n;x+=2){if(!bo[x]){int vv=(x<<1);for(int y=x*x;y<=n; y+=vv)bo[y]=true;}}return bo;}long mul(long a,long b,long m) {long r=1l;a%=m;while(b>0){if((b&1)==1) r=(r*a)%m;b>>=1;a=(a*a)%m;}return r;}int i()throws IOException{if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());return Integer.parseInt(st.nextToken());}long l()throws IOException {if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());return Long.parseLong(st.nextToken());}String s()throws IOException {if (!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());return st.nextToken();} double d()throws IOException{if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());return Double. parseDouble(st.nextToken());}void p(Object p){out.print(p);}void p(String p){out.print(p);}void p(int p) {out.print(p);}void p(double p){out.print(p);}void p(long p){out.print(p);}void p(char p){out.print(p);}void p(boolean p){out.print(p);}void pl(Object p){out.println(p);}void pl(String p){out.println(p);}void pl(int p) {out.println(p);}void pl(char p){out.println(p);}void pl(double p){out.println(p);}void pl(long p){out. println(p);}void pl(boolean p) {out.println(p);}void pl(){out.println();}void s(int a[]){for(int e:a) {sb.append(e);sb.append(' ');}sb.append("\n");} void s(long a[]) {for(long e:a){sb.append(e);sb.append(' ') ;}sb.append("\n");}void s(int ar[][]){for(int a[]:ar){for(int e:a){sb.append(e);sb.append(' ');}sb.append ("\n");}} void s(char a[]) {for(char e:a){sb.append(e);sb.append(' ');}sb.append("\n");}void s(char ar[][]) {for(char a[]:ar){for(char e:a){sb.append(e);sb.append(' ');}sb.append("\n");}}int[] ari(int n)throws IOException {int ar[]=new int[n];if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());for(int x=0; x<n;x++)ar[x]=Integer.parseInt(st.nextToken());return ar;}int[][] ari(int n,int m)throws IOException {int ar[][]=new int[n][m];for(int x=0;x<n;x++){if (!st.hasMoreTokens())st=new StringTokenizer (bq.readLine());for(int y=0;y<m;y++)ar[x][y]=Integer.parseInt(st.nextToken());}return ar;}long[] arl (int n)throws IOException {long ar[]=new long[n];if(!st.hasMoreTokens()) st=new StringTokenizer(bq.readLine()) ;for(int x=0;x<n;x++)ar[x]=Long.parseLong(st.nextToken());return ar;}long[][] arl(int n,int m)throws IOException {long ar[][]=new long[n][m];for(int x=0;x<n;x++) {if(!st.hasMoreTokens()) st=new StringTokenizer(bq.readLine());for(int y=0;y<m;y++)ar[x][y]=Long.parseLong(st.nextToken());}return ar;} String[] ars(int n)throws IOException {String ar[] =new String[n];if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());for(int x=0;x<n;x++)ar[x]=st.nextToken();return ar;}double[] ard (int n)throws IOException {double ar[] =new double[n];if(!st.hasMoreTokens())st=new StringTokenizer (bq.readLine());for(int x=0;x<n;x++)ar[x]=Double.parseDouble(st.nextToken());return ar;}double[][] ard (int n,int m)throws IOException{double ar[][]=new double[n][m];for(int x=0;x<n;x++) {if(!st.hasMoreTokens()) st=new StringTokenizer(bq.readLine());for(int y=0;y<m;y++) ar[x][y]=Double.parseDouble(st.nextToken());} return ar;}char[] arc(int n)throws IOException{char ar[]=new char[n];if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());for(int x=0;x<n;x++)ar[x]=st.nextToken().charAt(0);return ar;}char[][] arc(int n,int m)throws IOException {char ar[][]=new char[n][m];for(int x=0;x<n;x++){String s=bq.readLine(); for(int y=0;y<m;y++)ar[x][y]=s.charAt(y);}return ar;}void p(int ar[]) {StringBuilder sb=new StringBuilder (2*ar.length);for(int a:ar){sb.append(a);sb.append(' ');}out.println(sb);}void p(int ar[][]) {StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length);for(int a[]:ar){for(int aa:a){sb.append(aa); sb.append(' ');}sb.append("\n");}p(sb);}void p(long ar[]){StringBuilder sb=new StringBuilder (2*ar.length);for(long a:ar){ sb.append(a);sb.append(' ');}out.println(sb);} void p(long ar[][]) {StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length);for(long a[]:ar){for(long aa:a){sb.append(aa); sb.append(' ');}sb.append("\n");}p(sb);}void p(String ar[]){int c=0;for(String s:ar)c+=s.length()+1; StringBuilder sb=new StringBuilder(c);for(String a:ar){sb.append(a);sb.append(' ');}out.println(sb);} void p(double ar[]) {StringBuilder sb=new StringBuilder(2*ar.length);for(double a:ar){sb.append(a); sb.append(' ');}out.println(sb);}void p (double ar[][]){StringBuilder sb=new StringBuilder(2* ar.length*ar[0].length);for(double a[]:ar){for(double aa:a){sb.append(aa);sb.append(' ');}sb.append("\n") ;}p(sb);}void p(char ar[]) {StringBuilder sb=new StringBuilder(2*ar.length);for(char aa:ar){sb.append(aa); sb.append(' ');}out.println(sb);}void p(char ar[][]){StringBuilder sb=new StringBuilder(2*ar.length*ar[0] .length);for(char a[]:ar){for(char aa:a){sb.append(aa);sb.append(' ');}sb.append("\n");}p(sb);}void pl (int... ar){for(int e:ar)p(e+" ");pl();}void pl(long... ar){for(long e:ar)p(e+" ");pl();} }
Java
["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"]
2 seconds
["2\n0\n4\n0\n5\n0"]
NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
Java 8
standard input
[ "binary search", "dp", "implementation", "number theory", "schedules", "two pointers" ]
32130f939336bb6f2deb4dfa5402867d
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,400
For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions.
standard output
PASSED
6b704c2aee09fde937c0dad9ab20df93
train_110.jsonl
1638110100
While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class C { FastScanner in; PrintWriter out; boolean systemIO = true; public static void quickSort(int[] a, int from, int to) { if (to - from <= 1) { return; } int i = from; int j = to - 1; int x = a[from + (new Random()).nextInt(to - from)]; while (i <= j) { while (a[i] < x) { i++; } while (a[j] > x) { j--; } if (i <= j) { int t = a[i]; a[i] = a[j]; a[j] = t; i++; j--; } } quickSort(a, from, j + 1); quickSort(a, j + 1, to); } public long gcd(long x, long y) { if (y == 0) { return x; } if (x == 0) { return y; } return gcd(y, x % y); } public boolean prime(long x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) { return false; } } return true; } public long pow(long x, long p) { if (p == 0) { return 1; } long t = pow(x, p / 2); t *= t; t %= mod; if (p % 2 == 1) { t *= x; t %= mod; } return t; } public class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } @Override public int compareTo(Pair o) { if (x > o.x) { return 1; } if (x < o.x) { return -1; } if (y > o.y) { return 1; } if (y < o.y) { return -1; } return 0; } } public class Fenvik2D { long[][] t; int n, m; public Fenvik2D(int n, int m) { t = new long[n][m]; this.n = n; this.m = m; } public int sum(int x1, int y1, int x2, int y2) { return sum(x2, y2) - sum(x1 - 1, y2) - sum(x2, y1 - 1) + sum(x1 - 1, y1 - 1); } public int sum(int x, int y) { int result = 0; for (int i = x; i >= 0; i = (i & (i + 1)) - 1) { for (int j = y; j >= 0; j = (j & (j + 1)) - 1) { result += t[i][j]; } } return result; } public void add(int x, int y, int delta) { for (int i = x; i < n; i = (i | (i + 1))) { for (int j = y; j < m; j = (j | (j + 1))) { t[i][j] += delta; } } } } public int[] prefF(int[] a) { int[] p = new int[a.length]; for (int i = 1; i < p.length; i++) { int j = p[i - 1]; while (j > 0) { if (a[j] == a[i]) { break; } if (j == 0) { break; } j = p[j - 1]; } if (a[i] == a[j]) { p[i] = j + 1; } } return p; } public class Tree { long[] sum; long[] min; int[] alive; int pow; long inf = Long.MAX_VALUE / 2; public Tree(long[] a, long[] b) { pow = 1; while (pow < a.length) { pow *= 2; } sum = new long[2 * pow]; min = new long[2 * pow]; for (int i = 0; i < min.length; i++) { min[i] = inf; } alive = new int[2 * pow]; for (int i = 0; i < b.length; i++) { sum[pow + i] = a[i]; min[pow + i] = a[i] + b[i]; alive[pow + i] = 1; } for (int i = pow - 1; i > 0; i--) { merge(i); } } public void merge(int v) { sum[v] = sum[2 * v] + sum[2 * v + 1]; min[v] = inf; if (min[2 * v] < min[v]) { min[v] = min[2 * v]; } if (min[2 * v + 1] + sum[2 * v] < min[v]) { min[v] = min[2 * v + 1] + sum[2 * v]; } alive[v] = alive[2 * v] + alive[2 * v + 1]; } public void add(long a, long b, int c) { add(1, 0, pow, a, b, c - 1); } private void add(int v, int l, int r, long a, long b, int c) { if (v >= pow) { sum[v] = a; min[v] = a + b; alive[v] = 1; return; } int m = (l + r) / 2; if (c < m) { add(2 * v, l, m, a, b, c); } else { add(2 * v + 1, m, r, a, b, c); } merge(v); } public int update(long x) { return update(1, 0, pow, x); } private int update(int v, int l, int r, long x) { if (v >= pow) { if (x > min[v]) { sum[v] = 0; min[v] = inf; alive[v] = 0; return 1; } if (alive[v] == 1 && x >= sum[v]) { return 1; } return 0; } int m = (l + r) / 2; if (sum[2 * v] > x) { int ans = update(2 * v, l, m, x); merge(v); return ans; } int ans = alive[2 * v] + update(2 * v + 1, m, r, x - sum[2 * v]); if (x > min[2 * v]) { update(2 * v, l, m, x); } merge(v); return ans; } } public class MyStack { int[] st; int sz; public MyStack(int max) { st = new int[max]; sz = 0; } public int size() { return sz; } public boolean isEmpty() { return sz == 0; } public int peek() { return st[sz - 1]; } public int pop() { return st[--sz]; } public void add(int x) { st[sz++] = x; } public void clear() { sz = 0; } } public long[][] mult(long[][] a, long[][] b) { long[][] c = new long[a.length][b[0].length]; for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[0].length; j++) { for (int k = 0; k < b.length; k++) { c[i][j] += a[i][k] * b[k][j]; c[i][j] %= mod; } } } return c; } int mod = 998244353; public long[][] pow(long p, long[][] m) { if (p == 0) { long[][] matrix = new long[m.length][m.length]; for (int i = 0; i < matrix.length; i++) { matrix[i][i] = 1; } return matrix; } if (p % 2 == 1) { return mult(m, pow(p - 1, m)); } long[][] cur = pow(p / 2, m); return mult(cur, cur); } public void add(HashMap<Integer, Integer> map, int x) { if (map.containsKey(x)) { map.put(x, map.get(x) + 1); } else { map.put(x, 1); } } public class Point implements Comparable<Point> { long x; long y; int n; public Point(long x, long y, int n) { this.x = x; this.y = y; this.n = n; } public void norm() { long gcd = gcd(Math.abs(x), Math.abs(y)); x /= gcd; y /= gcd; } public int quater() { if (x > 0 && y >= 0) { return 0; } if (x <= 0 && y > 0) { return 1; } if (x < 0) { return 2; } return 3; } public long mod2() { return x * x + y * y; } @Override public int compareTo(Point o) { if (quater() != o.quater()) { return quater() - o.quater(); } int s = sign(cp(o, this)); if (s != 0) { return s; } return sign(mod2() - o.mod2()); } } public long cp(Point a, Point b) { return a.x * b.y - a.y * b.x; } public int sign(long x) { if (x > 0) { return 1; } if (x < 0) { return -1; } return 0; } public class Vector { double x; double y; public Vector(double x, double y) { this.x = x; this.y = y; } public double norm() { return Math.sqrt(norm2()); } public double norm2() { return x * x + y * y; } public String toString() { return x + " " + y; } public Vector negative() { return new Vector(-x, -y); } public Vector add(Vector v) { return new Vector(x + v.x, y + v.y); } public Vector subtract(Vector v) { return new Vector(x - v.x, y - v.y); } } public double dp(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } public double cp(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } public class Polygon { Vector[] v; double s; public Polygon(Vector[] v) { this.v = v; s = Math.abs(s()); } public double s() { double ans = 0; for (int i = 1; i < v.length - 1; i++) { Vector a = v[i].subtract(v[0]); Vector b = v[i + 1].subtract(v[0]); ans += cp(a, b); } return ans / 2; } public boolean intersect(Vector o, double r) { for (int i = 0; i < v.length; i++) { if (intersectSeg(v[i], v[(i + 1) % v.length], o, r)) { return true; } } boolean inside = false; for (int i = 0; i < v.length; i++) { inside ^= intersectRay(v[i], v[(i + 1) % v.length], o); } if (inside) { System.err.println(o + " " + s); return true; } return false; } } public double sq(double x) { return x * x; } public boolean intersectSeg(Vector a, Vector b, Vector o, double r) { Vector ao = o.subtract(a); Vector bo = o.subtract(b); if (ao.norm2() <= r * r * (1 + eps) || bo.norm2() <= r * r * (1 + eps)) { return true; } Vector ab = b.subtract(a); if (sq(Math.abs(cp(ao, bo))) > ab.norm2() * r * r * (1 + eps)) { return false; } if (dp(ao, ab) < -eps || dp(bo, ab) > eps) { return false; } return true; } public boolean intersectRay(Vector a, Vector b, Vector o) { if (a.y == b.y) { return false; } if (o.y == Math.max(a.y, b.y) && o.x < Math.min(a.x, b.x)) { return true; } if (o.y == Math.min(a.y, b.y)) { return false; } if ((o.y - a.y) * (o.y - b.y) < 0) { if (a.y < b.y) { if (cp(b.subtract(a), o.subtract(a)) > 0) { return true; } } else { if (cp(b.subtract(a), o.subtract(a)) < 0) { return true; } } } return false; } double eps = 0; Random random = new Random(566); public class SegmentTreeSet { int pow; long[] sum; long[] max; long[] min; long[] delta; boolean[] flag; public SegmentTreeSet(long[] a) { pow = 1; while (pow < a.length) { pow *= 2; } flag = new boolean[2 * pow]; sum = new long[2 * pow]; max = new long[2 * pow]; min = new long[2 * pow]; delta = new long[2 * pow]; for (int i = 0; i < a.length; i++) { sum[pow + i] = a[i]; max[pow + i] = a[i]; min[pow + i] = a[i]; } for (int i = pow - 1; i > 0; i--) { sum[i] = sum[2 * i] + sum[2 * i + 1]; max[i] = max[2 * i]; min[i] = min[2 * i + 1]; } } public int get(int v, int tl, int tr, int l, int r) { push(v, tl, tr); if (l >= tr || r <= tl) { return 0; } if (l <= tl && r >= tr) { if (money >= sum[v]) { money -= sum[v]; return tr - tl; } if (money < min[v]) { return 0; } } int tm = (tl + tr) / 2; return get(2 * v, tl, tm, l, r) + get(2 * v + 1, tm, tr, l, r); } public void set(int v, int tl, int tr, int l, int r, long x) { push(v, tl, tr); if (l >= tr || r <= tl) { return; } if (l <= tl && r >= tr) { if (x >= max[v]) { delta[v] = x; flag[v] = true; push(v, tl, tr); } else if (v < pow) { int tm = (tl + tr) / 2; push(2 * v + 1, tm, tr); if (x >= max[2 * v + 1]) { delta[2 * v + 1] = x; flag[2 * v + 1] = true; push(2 * v + 1, tm, tr); set(2 * v, tl, tm, l, r, x); } else { push(2 * v, tl, tm); set(2 * v + 1, tm, tr, l, r, x); } sum[v] = (sum[2 * v] + sum[2 * v + 1]); max[v] = max[2 * v]; min[v] = min[2 * v + 1]; } return; } int tm = (tl + tr) / 2; set(2 * v, tl, tm, l, r, x); set(2 * v + 1, tm, tr, l, r, x); sum[v] = (sum[2 * v] + sum[2 * v + 1]); max[v] = max[2 * v]; min[v] = min[2 * v + 1]; } public void push(int v, int tl, int tr) { if (flag[v]) { if (v < pow) { flag[2 * v] = true; flag[2 * v + 1] = true; delta[2 * v] = delta[v]; delta[2 * v + 1] = delta[v]; } flag[v] = false; sum[v] = delta[v] * (tr - tl); max[v] = delta[v]; min[v] = delta[v]; } } public int f(int a, int b) { return a + b; } } long money = 0; public long[] diofant(long a, long b) { long[] ans = new long[2]; if (b == 0) { ans[0] = 1; ans[1] = 0; return ans; } long div = a / b; long mod = a % b; long[] get = diofant(b, mod); ans[0] = get[1]; ans[1] = get[0] - (get[1] * div); return ans; } public class Arr implements Comparable<Arr> { int[] a; public Arr(int[] a) { this.a = a; } @Override public int compareTo(Arr o) { for (int i = 0; i < comp.length; i++) { if (a[comp[i]] != o.a[comp[i]]) { return a[comp[i]] - o.a[comp[i]]; } } return 0; } } int[] comp; public void dfs(int v) { boolean flag = false; if (!used[v]) { used[v] = true; flag = true; ans.add(v); } for (int x = 0; x < sz; x++) { if (!used[x] && g[v][x]) { used[x] = flag; dfs(x); } } } public int components() { int c = 0; for (int i = 0; i < sz; i++) { if (!used[i]) { dfs(i); c++; } } return c; } int max = 500; boolean[][] g = new boolean[2 * max][2 * max]; boolean[] used = new boolean[2 * max]; int[][] value = new int[max][max]; int sz = -1; ArrayList<Integer> ans = new ArrayList<>(); public void solve() { boolean[] prime = new boolean[1000001]; for (int i = 2; i < prime.length; i++) { prime[i] = true; } for (int i = 2; i * i < prime.length; i++) { if (prime[i]) { for (int j = i * i; j < prime.length; j += i) { prime[j] = false; } } } for (int qwerty = in.nextInt(); qwerty > 0; qwerty--) { int n = in.nextInt(); int[] a = new int[n]; int e = in.nextInt(); for (int i = 0; i < a.length; i++) { a[i] = in.nextInt(); } long ans = 0; for (int i0 = 0; i0 < e; i0++) { int prevones = 0; int curones = 0; boolean wasprime = false; for (int i = i0; i < a.length; i += e) { if (a[i] == 1) { curones++; if (wasprime) { ans += prevones + 1; } } else if (prime[a[i]]) { prevones = curones; wasprime = true; curones = 0; ans += prevones; } else { wasprime = false; prevones = curones = 0; } } } out.println(ans); } } public void run() { try { if (systemIO) { in = new FastScanner(System.in); out = new PrintWriter(System.out); } else { in = new FastScanner(new File("input.txt")); out = new PrintWriter(new File("output.txt")); } solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } 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()); } } // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA public static void main(String[] arg) { long time = System.currentTimeMillis(); new C().run(); System.err.println(System.currentTimeMillis() - time); } }
Java
["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"]
2 seconds
["2\n0\n4\n0\n5\n0"]
NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
Java 8
standard input
[ "binary search", "dp", "implementation", "number theory", "schedules", "two pointers" ]
32130f939336bb6f2deb4dfa5402867d
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,400
For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions.
standard output
PASSED
82fee8addde81b58c1b1138eff7b5aed
train_110.jsonl
1638110100
While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; 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; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, FastScanner in, PrintWriter out) { final int N = (int) (1e6 + 100); boolean[] isPrime = new boolean[N]; Arrays.fill(isPrime, true); isPrime[0] = false; isPrime[1] = false; for (int i = 2; i < N; i++) { if (isPrime[i]) { for (int j = i + i; j < N; j += i) { isPrime[j] = false; } } } int numTests = in.nextInt(); for (int test = 0; test < numTests; test++) { int n = in.nextInt(); int e = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } long ans = 0; for (int s = 0; s < n && s < e; s++) { List<Integer> b = new ArrayList<>(); for (int i = s; i < n; i += e) { b.add(a[i]); } int[] l = new int[b.size()]; int[] r = new int[b.size()]; for (int i = 0; i < b.size(); i++) { if (i > 0) { l[i] = l[i - 1]; } if (b.get(i) == 1) { l[i] += 1; } else { l[i] = 0; } } for (int i = b.size() - 1; i >= 0; i--) { if (i + 1 < b.size()) { r[i] = r[i + 1]; } if (b.get(i) == 1) { r[i] += 1; } else { r[i] = 0; } } for (int i = 0; i < b.size(); i++) { if (isPrime[b.get(i)]) { long L = i > 0 ? l[i - 1] : 0; long R = i + 1 < b.size() ? r[i + 1] : 0; ans += (L + 1) * (R + 1) - 1; } } } out.println(ans); } } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { try { in = new BufferedReader(new InputStreamReader(stream, "UTF-8")); } catch (Exception e) { throw new AssertionError(); } } public String next() { while (st == null || !st.hasMoreTokens()) { try { String rl = in.readLine(); if (rl == null) { return null; } st = new StringTokenizer(rl); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"]
2 seconds
["2\n0\n4\n0\n5\n0"]
NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
Java 8
standard input
[ "binary search", "dp", "implementation", "number theory", "schedules", "two pointers" ]
32130f939336bb6f2deb4dfa5402867d
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,400
For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions.
standard output
PASSED
d6ad7c7cac003bcccd3cd79432a5df67
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException; import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter; import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Array;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;import java.security.AccessControlException;import java.util.*; import java.util.Arrays;import java.util.Collection;import java.util.Comparator; import java.util.List;import java.util.Map;import java.util.Objects;import java.util.TreeMap; import java.util.function.Function;import java.util.stream.Collectors;import java.util.stream.IntStream; import java.util.stream.LongStream;import java.util.stream.Stream;public class _A { static public void main(final String[] args) throws IOException{A._main(args);} static class A extends Solver{public A(){}@Override public void solve()throws IOException {int n=sc.nextInt();sc.nextLine();long[]a=Arrays.stream(sc.nextLine().trim().split("\\s+")).filter($s2 ->!$s2.isEmpty()).mapToLong(Long::parseLong).toArray();Comparator<long[]>cmp=(x, y)->x[0]!=y[0]?Long.compare(x[0],y[0]):Long.compare(x[1],y[1]);TreeMap<Integer,TreeSet<long[]>> map=new TreeMap<>();List<long[]>listi=Datas.listi(a);for(long[]v:listi){int k=0;while(v[0] %2==0){k++;v[0]/=2;}if(!map.containsKey(k)){map.put(k,new TreeSet<>(cmp));}map.get(k).add(v); }long res=Arrays.stream(a).sum();for(int k:map.keySet()){long[]probe=map.get(k).pollLast(); long max=probe[0]*(1<<k);long sum=0;for(Map.Entry<Integer,TreeSet<long[]>>entry: map.entrySet()){if(!entry.getValue().isEmpty()){sum+=entry.getValue().stream().mapToLong(v ->v[0]).sum();max*=(1L<<entry.getKey()*entry.getValue().size());}}sum+=max;if(res <sum){res=sum;}map.get(k).add(probe);}pw.println(res);}static public void _main(String[] args)throws IOException{new A().run();}}static class Datas{final static String SPACE =" ";public static TreeMap<Integer,Integer>mapc(final int[]a){return IntStream.range(0, a.length).collect(()->new TreeMap<Integer,Integer>(),(res,i)->{res.put(a[i],res.getOrDefault(a[i], 0)+1);},Map::putAll);}public static TreeMap<Long,Integer>mapc(final long[]a){return IntStream.range(0,a.length).collect(()->new TreeMap<Long,Integer>(),(res,i)->{res.put(a[i], res.getOrDefault(a[i],0)+1);},Map::putAll);}public static<T>TreeMap<T,Integer>mapc(final T[]a,Comparator<T>cmp){return IntStream.range(0,a.length).collect(cmp!=null?()-> new TreeMap<T,Integer>(cmp):()->new TreeMap<T,Integer>(),(res,i)->{res.put(a[i], res.getOrDefault(a[i],0)+1);},Map::putAll);}public static<T>TreeMap<T,Integer>mapc(final T[]a){return mapc(a,null);}public static TreeMap<Integer,Integer>mapc(final IntStream a){return a.collect(()->new TreeMap<Integer,Integer>(),(res,v)->{res.put(v,res.getOrDefault(v, 0)+1);},Map::putAll);}public static TreeMap<Long,Integer>mapc(final LongStream a) {return a.collect(()->new TreeMap<Long,Integer>(),(res,v)->{res.put(v,res.getOrDefault(v, 0)+1);},Map::putAll);}public static<T>TreeMap<T,Integer>mapc(final Stream<T>a,Comparator<T> cmp){return a.collect(cmp!=null?()->new TreeMap<T,Integer>(cmp):()->new TreeMap<T, Integer>(),(res,v)->{res.put(v,res.getOrDefault(v,0)+1);},Map::putAll);}public static <T>TreeMap<T,Integer>mapc(final Stream<T>a){return mapc(a,null);}public static<T> TreeMap<T,Integer>mapc(final Collection<T>a,Comparator<T>cmp){return mapc(a.stream(), cmp);}public static<T>TreeMap<T,Integer>mapc(final Collection<T>a){return mapc(a.stream()); }public static TreeMap<Integer,List<Integer>>mapi(final int[]a){return IntStream.range(0, a.length).collect(()->new TreeMap<Integer,List<Integer>>(),(res,i)->{if(!res.containsKey(a[i])) {res.put(a[i],Stream.of(i).collect(Collectors.toList()));}else{res.get(a[i]).add(i); }},Map::putAll);}public static TreeMap<Long,List<Integer>>mapi(final long[]a){return IntStream.range(0,a.length).collect(()->new TreeMap<Long,List<Integer>>(),(res,i) ->{if(!res.containsKey(a[i])){res.put(a[i],Stream.of(i).collect(Collectors.toList())); }else{res.get(a[i]).add(i);}},Map::putAll);}public static<T>TreeMap<T,List<Integer>> mapi(final T[]a,Comparator<T>cmp){return IntStream.range(0,a.length).collect(cmp !=null?()->new TreeMap<T,List<Integer>>(cmp):()->new TreeMap<T,List<Integer>>(), (res,i)->{if(!res.containsKey(a[i])){res.put(a[i],Stream.of(i).collect(Collectors.toList())); }else{res.get(a[i]).add(i);}},Map::putAll);}public static<T>TreeMap<T,List<Integer>> mapi(final T[]a){return mapi(a,null);}public static TreeMap<Integer,List<Integer>> mapi(final IntStream a){int[]i=new int[]{0};return a.collect(()->new TreeMap<Integer, List<Integer>>(),(res,v)->{if(!res.containsKey(v)){res.put(v,Stream.of(i[0]).collect(Collectors.toList())); }else{res.get(v).add(i[0]);}i[0]++;},Map::putAll);}public static TreeMap<Long,List<Integer>> mapi(final LongStream a){int[]i=new int[]{0};return a.collect(()->new TreeMap<Long, List<Integer>>(),(res,v)->{if(!res.containsKey(v)){res.put(v,Stream.of(i[0]).collect(Collectors.toList())); }else{res.get(v).add(i[0]);}i[0]++;},Map::putAll);}public static<T>TreeMap<T,List<Integer>> mapi(final Stream<T>a,Comparator<T>cmp){int[]i=new int[]{0};return a.collect(cmp !=null?()->new TreeMap<T,List<Integer>>(cmp):()->new TreeMap<T,List<Integer>>(), (res,v)->{if(!res.containsKey(v)){res.put(v,Stream.of(i[0]).collect(Collectors.toList())); }else{res.get(v).add(i[0]);}i[0]++;},Map::putAll);}public static<T>TreeMap<T,List<Integer>> mapi(final Stream<T>a){return mapi(a,null);}public static<T>TreeMap<T,List<Integer>> mapi(final Collection<T>a,Comparator<T>cmp){return mapi(a.stream(),cmp);}public static<T>TreeMap<T,List<Integer>>mapi(final Collection<T>a){return mapi(a.stream()); }public static List<int[]>listi(final int[]a){return IntStream.range(0,a.length).mapToObj(i ->new int[]{a[i],i}).collect(Collectors.toList());}public static List<long[]>listi(final long[]a){return IntStream.range(0,a.length).mapToObj(i->new long[]{a[i],i}).collect(Collectors.toList()); }public static<T>List<Pair<T,Integer>>listi(final T[]a){return IntStream.range(0, a.length).mapToObj(i->new Pair<T,Integer>(a[i],i)).collect(Collectors.toList()); }public static List<int[]>listi(final IntStream a){int[]i=new int[]{0};return a.mapToObj(v ->new int[]{v,i[0]++}).collect(Collectors.toList());}public static List<long[]>listi(final LongStream a){int[]i=new int[]{0};return a.mapToObj(v->new long[]{v,i[0]++}).collect(Collectors.toList()); }public static<T>List<Pair<T,Integer>>listi(final Stream<T>a){int[]i=new int[]{0}; return a.map(v->new Pair<T,Integer>(v,i[0]++)).collect(Collectors.toList());}public static<T>List<Pair<T,Integer>>listi(final Collection<T>a){int[]i=new int[]{0};return a.stream().map(v->new Pair<T,Integer>(v,i[0]++)).collect(Collectors.toList());}public static String join(final int[]a){return Arrays.stream(a).mapToObj(Integer::toString).collect(Collectors.joining(SPACE)); }public static String join(final long[]a){return Arrays.stream(a).mapToObj(Long::toString).collect(Collectors.joining(SPACE)); }public static<T>String join(final T[]a){return Arrays.stream(a).map(v->Objects.toString(v)).collect(Collectors.joining(SPACE)); }public static<T>String join(final T[]a,final Function<T,String>toString){return Arrays.stream(a).map(v->toString.apply(v)).collect(Collectors.joining(SPACE));}public static<T>String join(final Collection<T>a){return a.stream().map(v->Objects.toString(v)).collect(Collectors.joining(SPACE)); }public static<T>String join(final Collection<T>a,final Function<T,String>toString) {return a.stream().map(v->toString.apply(v)).collect(Collectors.joining(SPACE)); }public static<T>String join(final Stream<T>a){return a.map(v->Objects.toString(v)).collect(Collectors.joining(SPACE)); }public static<T>String join(final Stream<T>a,final Function<T,String>toString){ return a.map(v->toString.apply(v)).collect(Collectors.joining(SPACE));}public static <T>String join(final IntStream a){return a.mapToObj(Integer::toString).collect(Collectors.joining(SPACE)); }public static<T>String join(final LongStream a){return a.mapToObj(Long::toString).collect(Collectors.joining(SPACE)); }public static List<Integer>list(final int[]a){return Arrays.stream(a).mapToObj(Integer::valueOf).collect(Collectors.toList()); }public static List<Integer>list(final IntStream a){return a.mapToObj(Integer::valueOf).collect(Collectors.toList()); }public static List<Long>list(final long[]a){return Arrays.stream(a).mapToObj(Long::valueOf).collect(Collectors.toList()); }public static List<Long>list(final LongStream a){return a.mapToObj(Long::valueOf).collect(Collectors.toList()); }public static<T>List<T>list(final Stream<T>a){return a.collect(Collectors.toList()); }public static<T>List<T>list(final Collection<T>a){return a.stream().collect(Collectors.toList()); }public static<T>List<T>list(final T[]a){return Arrays.stream(a).collect(Collectors.toList()); }public static String yesNo(final boolean res){return res?"YES":"NO";}public static String dump(Object obj){String res="";if(obj!=null){Class cl=obj.getClass();String cls=cl.getName();if(cls.startsWith("[")){res+="[";for(int i=0;;i++){try{Object o =Array.get(obj,i);String s=dump(o);if(i>0){res+=", ";}res+=s;}catch(ArrayIndexOutOfBoundsException ex){break;}}res+="]";}else if(Collection.class.isAssignableFrom(cl)){@SuppressWarnings("unchecked")final Object s=((Collection)obj).stream().map(v->dump(v)).collect(Collectors.joining(", ", "[","]"));res+=s.toString();}else if(Map.class.isAssignableFrom(cl)){@SuppressWarnings("unchecked")final Object s=((Map)obj).entrySet().stream().map(v->dump(v)).collect(Collectors.joining(", ", "{","}"));res+=s.toString();}else if(Character.class.isInstance(obj)|| Integer.class.isInstance(obj) || Long.class.isInstance(obj)|| Float.class.isInstance(obj)|| Double.class.isInstance(obj)|| String.class.isInstance(obj) ){res+=Objects.toString(obj);}else if(Map.Entry.class.isInstance(obj)){res+=dump(((Map.Entry)obj).getKey()) +"="+dump(((Map.Entry)obj).getValue());}else if(Stream.class.isInstance(obj)){@SuppressWarnings("unchecked") final Object s=((Stream)obj).map(v->dump(v)).collect(Collectors.joining(", ","[", "]"));res+=s.toString();}else{res+=Stream.concat(Arrays.stream(obj.getClass().getFields()).map(v ->{String name=v.getName();String val;try{Object o=v.get(obj);if(o!=null && v.isAnnotationPresent(Dump.class)) {Dump an=v.getAnnotation(Dump.class);Class ocl=o.getClass();val="{";for(String fn:an.fields()) {try{Field f=ocl.getField(fn);val+=fn+"="+dump(f.get(o))+", ";}catch(NoSuchFieldException nsfex){try{@SuppressWarnings("unchecked")final Method m=ocl.getMethod(fn);val+=fn+"="+dump(m.invoke(o)) +", ";}catch(NoSuchMethodException | IllegalArgumentException | InvocationTargetException nsmex){}}}if(val.endsWith(", ")){val=val.substring(0,val.length()-2);}val+="}";} else{val=dump(o);}}catch(IllegalAccessException ex){val="N/A";}return name+"="+val; }),Arrays.stream(obj.getClass().getMethods()).filter(m->m.isAnnotationPresent(Getter.class)).map(m ->{String name=m.getName();String val;try{Object o=m.invoke(obj);if(o!=null && m.isAnnotationPresent(Dump.class)) {Dump an=m.getAnnotation(Dump.class);Class ocl=o.getClass();val="{";for(String fn:an.fields()) {try{Field f=ocl.getField(fn);val+=fn+"="+dump(f.get(o))+", ";}catch(NoSuchFieldException nsfex){try{@SuppressWarnings("unchecked")final Method m1=ocl.getMethod(fn);val+=fn+"="+dump(m1.invoke(o)) +", ";}catch(NoSuchMethodException | IllegalArgumentException | InvocationTargetException nsmex){}}}if(val.endsWith(", ")){val=val.substring(0,val.length()-2);}val+="}";}else {val=dump(o);}}catch(IllegalAccessException | InvocationTargetException ex){val= "N/A";}return name+"="+val;})).collect(Collectors.joining(", ","{"+obj.getClass().getName() +": ","}"));}}if(res.length()==0){res="<null>";}return res;}}@Retention(RetentionPolicy.RUNTIME) public @interface Dump{String[]fields();}@Retention(RetentionPolicy.RUNTIME)public @interface Getter{}static class Pair<K,V>{private K k;private V v;public Pair(final K t,final V u){this.k=t;this.v =u;}public K getKey(){return k;}public V getValue(){return v;}public void setKey(K t){k=t;}public void setValue(V t){v=t;}}static class MyScanner{private StringBuilder cache=new StringBuilder();int cache_pos=0;private int first_linebreak=-1;private int second_linebreak=-1;private StringBuilder sb=new StringBuilder();private InputStream is=null;public MyScanner(final InputStream is){this.is=is;}public String charToString(final int c){return String.format("'%s'",c=='\n'?"\\n":(c=='\r'?"\\r":String.valueOf((char)c))); }public int get(){int res=-1;if(cache_pos<cache.length()){res=cache.charAt(cache_pos); cache_pos++;if(cache_pos==cache.length()){cache.delete(0,cache_pos);cache_pos=0; }}else{try{res=is.read();}catch(IOException ex){throw new RuntimeException(ex);} }return res;}private void unget(final int c){if(cache_pos==0){cache.insert(0,(char)c); }else{cache_pos--;}}public String nextLine(){sb.delete(0,sb.length());int c;boolean done=false;boolean end=false;while((c=get())!=-1){if(check_linebreak(c)){done=true; if(c==first_linebreak){if(!end){end=true;}else{cache.append((char)c);break;}}else if(second_linebreak!=-1 && c==second_linebreak){break;}}if(end && c!=first_linebreak && c!=second_linebreak){cache.append((char)c);break;}if(!done){sb.append((char)c); }}return!done && sb.length()==0?null:sb.toString();}private boolean check_linebreak(int c){if(c=='\n'|| c=='\r'){if(first_linebreak==-1){first_linebreak=c;}else if(c!=first_linebreak && second_linebreak==-1){second_linebreak=c;}return true;}return false;}public int nextInt(){return Integer.parseInt(next());}public long nextLong(){return Long.parseLong(next()); }public boolean hasNext(){boolean res=false;int c;while((c=get())!=-1){if(!check_linebreak(c) && c!=' '&& c!='\t'){res=true;unget(c);break;}}return res;}public String next(){ sb.delete(0,sb.length());boolean started=false;int c;while((c=get())!=-1){if(check_linebreak(c) || c==' '|| c=='\t'){if(started){unget(c);break;}}else{started=true;sb.append((char)c); }}return sb.toString();}public int nextChar(){return get();}public boolean eof() {int c=get();boolean res=false;if(c!=-1){unget(c);}else{res=true;}return res;}public double nextDouble(){return Double.parseDouble(next());}}static abstract class Solver {protected String nameIn=null;protected String nameOut=null;protected boolean singleTest =false;protected MyScanner sc=null;protected PrintWriter pw=null;private int current_test =0;private int count_tests=0;protected int currentTest(){return current_test;}protected int countTests(){return count_tests;}private void process()throws IOException{if(!singleTest) {count_tests=sc.nextInt();sc.nextLine();for(current_test=1;current_test<=count_tests; current_test++){solve();pw.flush();}}else{count_tests=1;current_test=1;solve();} }abstract protected void solve()throws IOException;public void run()throws IOException {boolean done=false;try{if(nameIn!=null){if(new File(nameIn).exists()){try(FileInputStream fis=new FileInputStream(nameIn);PrintWriter pw0=select_output();){select_output(); done=true;sc=new MyScanner(fis);pw=pw0;process();}}else{throw new RuntimeException("File " +new File(nameIn).getAbsolutePath()+" does not exist!");}}}catch(IOException | AccessControlException ex){}if(!done){try(PrintWriter pw0=select_output();){sc=new MyScanner(System.in); pw=pw0;process();}}}private PrintWriter select_output()throws FileNotFoundException {if(nameOut!=null){return new PrintWriter(nameOut);}return new PrintWriter(System.out); }}static abstract class Tester{static public int getRandomInt(final int min,final int max){return(min+(int)Math.floor(Math.random()*(max-min+1)));}static public long getRandomLong(final long min,final long max){return(min+(long)Math.floor(Math.random() *(max-min+1)));}static public double getRandomDouble(final double min,final double maxExclusive){return(min+Math.random()*(maxExclusive-min));}abstract protected void testOutput(final List<String>output_data);abstract protected void generateInput(); abstract protected String inputDataToString();private boolean break_tester=false; protected void beforeTesting(){}protected void breakTester(){break_tester=true;} public boolean broken(){return break_tester;}}}
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
da128b5a7d5def6a212c2706024212f2
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class main { public static void main(String[] args) { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } try{ FastReader sc = new FastReader(); StringBuilder str = new StringBuilder(""); int num = sc.nextInt(); while(num--> 0){ int n=sc.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } int count=0; for(int i=0;i<n;i++){ while(arr[i]%2==0){ arr[i]/=2; count++; } } Arrays.sort(arr); long sum=0; for(int i=0;i<count;i++){ arr[n-1]*=2; } for(long s:arr){ sum+=s; } System.out.println(sum); } System.out.println(str); } catch (Exception e){ return; } } public static long gcd(long a,long b){ if (b == 0) return a; return gcd(b, a % b); } public static long lcm(long a, long b) { return (a / gcd(a, 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; } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
58bcb416aef2d199d28d3d4c799c20eb
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; public class snackDown3 { public static class FastReader { BufferedReader b; StringTokenizer s; public FastReader() { b=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(s==null ||!s.hasMoreElements()) { try { s=new StringTokenizer(b.readLine()); } catch(IOException e) { e.printStackTrace(); } } return s.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=b.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } boolean hasNext() { if (s != null && s.hasMoreTokens()) { return true; } String tmp; try { b.mark(1000); tmp = b.readLine(); if (tmp == null) { return false; } b.reset(); } catch (IOException e) { return false; } return true; } } public static class pair{ char a; int b; public pair(char a,int b) { this.a=a; this.b=b; } } public static class pair2{ int a; int b; public pair2(int a,int b) { this.a=a; this.b=b; } public int compareTo(pair2 b) { return this.b-b.b; } } public static void main (String[] args) throws Exception { // your code goes here // int a[][][]=new int[3][3][3]; FastReader sc=new FastReader(); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); // OutputStream log = new BufferedOutputStream ( System.out ); int t=sc.nextInt(); while(t--!=0) { int n=sc.nextInt(); long a[]=new long[n]; long b[]=new long[n]; for(int i=0;i<n;i++) { a[i]=sc.nextLong(); } int i=0; long ans=-1; for(int k=0;k<a.length;k++) { for(int j=0;j<a.length;j++) { b[j]=a[j]; } i=0; while(i<b.length) { if(i!=k && b[i]%2==0 && b[i]!=0) { while(b[i]%2==0) { b[k]*=2; b[i]/=2; } } i++; } long sum=0; for(int j=0;j<a.length;j++) { sum+=b[j]; } ans=Math.max(ans, sum); } log.write(ans+"\n"); log.flush(); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
f3310bf06a266882fcf7bbcefc6bbe01
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static long mod = (int)1e9+7; // static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; while(t-->0) { int n = sc.nextInt(); long arr[] = sc.readArrayLong(n); long ans = 0; for(int i=0;i<n;i++) { long sum = 0; for(int j=0;j<n;j++) { if(i == j) { continue; } while(arr[j]%2==0) { arr[j] /= 2L; arr[i] *= 2L; if(arr[j] == 1) { break; } } } for(long x:arr) { sum += x; } ans = Math.max(ans , sum); } System.out.println(ans); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } static void reverse_sorted(int[] arr) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<arr.length;i++) { list.add(arr[i]); } Collections.sort(list , Collections.reverseOrder()); for(int i=0;i<arr.length;i++) { arr[i] = list.get(i); } } 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(ArrayList<Integer> list, int x) {// x is the key or target value int l=-1,r=list.size(); while(l+1<r) { int m=(l+r)>>>1; if(list.get(m)<=x) l=m; else r=m; } return l+1; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static class Queue_Pair implements Comparable<Queue_Pair> { int first , second; public Queue_Pair(int first, int second) { this.first=first; this.second=second; } public int compareTo(Queue_Pair o) { return Integer.compare(o.first, first); } } static void leftRotate(int arr[], int d, int n) { for (int i = 0; i < d; i++) leftRotatebyOne(arr, n); } static void leftRotatebyOne(int arr[], int n) { int i, temp; temp = arr[0]; for (i = 0; i < n - 1; i++) arr[i] = arr[i + 1]; arr[n-1] = temp; } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } static boolean palindrome_array(char arr[], int n) { // Initialise flag to zero. int flag = 0; // Loop till array size n/2. for (int i = 0; i <= n / 2 && n != 0; i++) { // Check if first and last element are different // Then set flag to 1. if (arr[i] != arr[n - i - 1]) { flag = 1; break; } } // If flag is set then print Not Palindrome // else print Palindrome. if (flag == 1) return false; else return true; } static boolean allElementsEqual(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]==arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } static boolean allElementsDistinct(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]!=arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } public static void reverse(int[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } public static void reverse_Long(long[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily long temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static boolean isReverseSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { return false; } } return true; } static int[] rearrangeEvenAndOdd(int arr[], int n) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); int[] array = list.stream().mapToInt(i->i).toArray(); return array; } static long[] rearrangeEvenAndOddLong(long arr[], int n) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); long[] array = list.stream().mapToLong(i->i).toArray(); return array; } static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (long i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static long getSum(long n) { long sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcdLong(long a, long b) { if (b == 0) return a; return gcdLong(b, a % b); } static void swap(int i, int j) { int temp = i; i = j; j = temp; } static int countDigit(int n) { return (int)Math.floor(Math.log10(n) + 1); } } class Pair { int first; int second; Pair(int first , int second) { this.first = first; this.second = second; } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
a2dd3ae9a0df6e63e6e9d4d879e92989
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // System.out.println(sc.nextByte()); PrintWriter pw=new PrintWriter(System.out); divideandmult(sc,pw); pw.close(); } private static void divideandmult(Scanner scanner,PrintWriter printWriter){ int testcase=scanner.nextInt(); for(int i=0;i<testcase;i++){ int size=scanner.nextInt(); long[] ar=new long[size]; for(int j=0;j<size;j++){ long z=scanner.nextLong(); ar[j]=z; if(z%2!=0); } long max=0; long sum=0; long power=0; for(int j=0;j<size;j++){ long a =ar[j]; while (a%2==0){ power+=1; a=a/2; } max=Math.max(max,a); sum+=a; } sum-=max; long num= (long) (max*Math.pow(2,power)); System.out.println(num+sum); } }}
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
886bc6b7b9c8b4fa222dd0c7835e325f
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; public class DivideAndMultiply { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int test=1;test<=t;test++) { int n=sc.nextInt(); long[] ar=new long[n]; for(int i=0;i<n;i++) { ar[i]=sc.nextInt(); } int c=0; for(int i=0;i<n;i++) { if(ar[i]%2==0) { while(ar[i]%2!=1) { ar[i]=ar[i]/2; c++; } } } Arrays.sort(ar); for(int k=1;k<=c;k++) { ar[n-1]=ar[n-1]*2; } long sum=0; for(int i=0;i<n;i++) { sum=sum+ar[i]; } System.out.println(sum); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
b31b85ffb0c543d769334019e5326936
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.math.*; //import java.io.*; public class Experiment { static Scanner in=new Scanner(System.in); // static BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); public static void sort(int ar[],int l,int r) { if(l<r) { int m=l+(r-l)/2; sort(ar,l,m); sort(ar,m+1,r); merge(ar,l,m,r); } } public static void merge(int ar[],int l,int m,int r) { int n1=m-l+1;int n2=r-m; int L[]=new int[n1]; int R[]=new int[n2]; for(int i=0;i<n1;i++) L[i]=ar[l+i]; for(int i=0;i<n2;i++) R[i]=ar[m+i+1]; int i=0;int j=0;int k=l; while(i<n1 && j<n2) { if(L[i]<=R[j]) { ar[k++]=L[i++]; }else { ar[k++]=R[j++]; } } while(i<n1) ar[k++]=L[i++]; while(j<n2) ar[k++]=R[j++]; } public static int[] sort(int ar[]) { sort(ar,0,ar.length-1); //Array.sort uses O(n^2) in its worst case ,so better use merge sort return ar; } public static void func(int ar[],int maxeven,int maxodd) { long p=1;int n=ar.length;int k=0; for(int i=0;i<n;i++) { while(ar[i]%2==0) { p*=2;k=1; ar[i]=ar[i]/2; } } sort(ar); long sum=0; for(int i=0;i<n-1;i++) sum+=ar[i]; if(k==0) { sum+=ar[n-1]; System.out.println(sum);return;} System.out.println(ar[n-1]*p+ sum); } public static void main(String[] args) { int t=in.nextInt(); while(t!=0) { //long x=in.nextLong(); int n=in.nextInt(); int ar[]=new int[n]; int maxeven=Integer.MIN_VALUE;int j=0; int maxodd=Integer.MIN_VALUE; for(int i=0;i<n;i++) { ar[i]=in.nextInt(); if((ar[i]%2==0) && ar[i]>maxeven) { maxeven=ar[i]; } if((ar[i]%2!=0) && ar[i]>maxodd) { maxodd=ar[i]; } } func(ar,maxeven,maxodd); t--; } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
0beb858ed5d2e3e513d2dc435d4323a6
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import javax.swing.*; import javax.swing.plaf.basic.BasicLookAndFeel; import java.util.Arrays; import java.util.Scanner; public class first { public static long[] a = new long[20]; public static long[] num = new long[20]; public static long Pow(long a,long b) { long ans = 1; for(int i = 1; i <= b; i++)ans*=a; return ans; } public static void main(String[] args) { Scanner read = new Scanner(System.in); int t = read.nextInt(); while(t > 0) { int n = read.nextInt(); for(int i = 1; i <= n; i++) { a[i] = read.nextLong(); long now = a[i]; while(now%2==0) { now /= 2; num[i]++; } } long minn = 0; int pos = 0; for(int i = 1; i <= n; i++) { if(a[i] / Pow(2,num[i]) > minn) { pos = i; minn = a[i]/Pow(2,num[i]); } } minn = 0; for(int i = 1; i <= n; i++) if(i!=pos)minn += num[i]; long ans = 0; for(int i = 1; i <= n; i++) { if(i==pos)ans += Pow(2,minn)*a[i]; else ans += a[i]/Pow(2,num[i]); a[i] = 0; num[i] = 0; } System.out.println(ans); t--; } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
ed3200b0b79e00a8a3f87beb81310c02
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
// हर हर महादेव import java.util.*; import java.lang.*; import java.io.*; import java.math.BigInteger; import java.text.DecimalFormat; public final class Solution { static int inf = Integer.MAX_VALUE; static long mod = 1000000000 + 7; static void ne(Scanner sc, BufferedWriter op) throws Exception { int n=sc.nextInt(); long[] arr= new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); } int k=0; for(int i=0;i<n;i++){ while(arr[i]%2==0){ arr[i]/=2; k++; } } sort(arr); arr[n-1]=arr[n-1]*(long)Math.pow(2,k); long sum=0; for(int i=0;i<n;i++){ sum+=arr[i]; } op.write(sum+"\n"); } static long gcd(long a, long b){ if(a==0L) return b; return gcd(b%a,a); } public static void main(String[] args) throws Exception { BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out)); // Reader sc = new Reader(); Scanner sc= new Scanner(System.in); int t = sc.nextInt(); while (t-->0){ ne(sc, op); } // ne(sc,op); op.flush(); } static void print(Object o) { System.out.println(String.valueOf(o)); } static int[] toIntArr(String s){ int[] val= new int[s.length()]; for(int i=0;i<s.length();i++){ val[i]=s.charAt(i)-'a'; } return val; } static void sort(int[] arr){ ArrayList<Integer> list= new ArrayList<>(); for(int i=0;i<arr.length;i++){ list.add(arr[i]); } Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i]=list.get(i); } } static void sort(long[] arr){ ArrayList<Long> list= new ArrayList<>(); for(int i=0;i<arr.length;i++){ list.add(arr[i]); } Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i]=list.get(i); } } } // return -1 to put no ahed in array class pair { long xx; int yy; pair(long xx, int yy ) { this.xx = xx; this.yy = yy; } } class sortY implements Comparator<pair> { public int compare(pair p1, pair p2) { if (p1.yy > p2.yy) { return 1; } else if (p1.yy == p2.yy) { if (p1.xx > p2.xx) { return 1; } else if (p1.xx < p2.xx) { return -1; } return 0; } return -1; } } class sortX implements Comparator<pair> { public int compare(pair p1, pair p2) { if (p1.xx > p2.xx) { return 1; } else if (p1.xx == p2.xx) { if (p1.yy > p2.yy) { return 1; } else if (p1.yy < p2.yy) { return -1; } return 0; } return -1; } } class debug { static void print1d(long[] arr) { System.out.println(); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } static void print1d(int[] arr) { System.out.println(); for (int i = 0; i < arr.length; i++) { System.out.println(i+" = "+arr[i]); } } static void print1d(boolean[] arr) { System.out.println(); for (int i = 0; i < arr.length; i++) { System.out.println(i + "= " + arr[i]); } } static void print2d(int[][] arr) { System.out.println(); int n = arr.length; int n2 = arr[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < n2; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } static void print2d(long[][] arr) { System.out.println(); int n = arr.length; int n2 = arr[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < n2; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } static void printPair(ArrayList<pair> list) { if(list.size()==0){ System.out.println("empty list"); return; } System.out.println(); for(int i=0;i<list.size();i++){ System.out.println(list.get(i).xx+"-"+list.get(i).yy); } } } 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(); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
a41f74f2a23cd11fcafd555fa85294e0
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
/* JAIKARA SHERAWAALI DA BOLO SACHE DARBAR KI JAI HAR HAR MAHADEV JAI BHOLENAATH Rohit Kumar NIT JAMSHEDPUR */ import java.util.*; import java.io.*; public class Main { static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null||!st.hasMoreTokens()){ try{ st=new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken();} long nextLong(){ return Long.parseLong(next());} int nextInt(){ return Integer.parseInt(next());} } static <t>void print(t o){ System.out.println(o); } static boolean islapindrome(String s){ int fhalf[]=new int[26]; int shalf[]=new int[26]; int i=0,j=s.length()-1; while(i<j){ fhalf[s.charAt(i)-'a']++; shalf[s.charAt(j)-'a']++; i++; j--; } for(int k=0;k<26;k++){ if(fhalf[k]!=shalf[k])return false; } return true; } static int mod=(int)1e9+7; public static void main (String args[]) throws IOException{ try{ System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long arr[]=new long[n]; long cop[]=new long[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); cop[i]=arr[i]; } long ans=0; if(n==1) { System.out.println(arr[0]); continue; } int p=0; for(int i=0;i<arr.length;i++) { if(arr[i]%2==0) { while(arr[i]%2==0) { p++; arr[i]/=2; } } } Arrays.sort(arr); // System.out.print(p+" "); // System.out.println(); arr[arr.length-1]=(long)Math.pow(2,p)*arr[arr.length-1]; long ans1=0; for(int i=0;i<n;i++) { ans1+=arr[i]; } System.out.println(ans1); } } void sieveOfEratosthenes(int n) { 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; } } for(int i = 2; i <= n; i++) { if(prime[i] == true) System.out.print(i + " "); } } static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } static boolean isprime(int n) { if(n<=1) return false; if(n<=3) return true; if(n%2==0||n%3==0) return false; for(int i=5;i*i<=n;i=i+6) { if(n%i==0||n%(i+2)==0) return false; } return true; } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
557c36ca2396b6d379333243e2f9ef8a
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.*; public class DivideAndMultiply { static InputReader reader = new InputReader(System.in); public static void main(String[] args) { int t = reader.nextInt(); for (int i = 0; i < t; i++) { int n = reader.nextInt(); int[] number = new int[n]; int count = 0; for (int j = 0; j < n; j++) { int temp = reader.nextInt(); while (temp % 2 == 0) { temp /= 2; count++; } number[j] = temp; } Arrays.sort(number); long res = (long) (number[number.length - 1] * Math.pow(2, count)); for (int j = 0; j < number.length - 1; j++) { res += number[j]; } System.out.println(res); } } 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 String nextLine() { String str; try { str = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return str; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
030d688d186a31b3d614489e3bc70837
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.*; public class DivideAndMultiply { static InputReader reader = new InputReader(System.in); public static void main(String[] args) { int t = reader.nextInt(); for (int i = 0; i < t; i++) { int n = reader.nextInt(); int[] number = new int[n]; int count = 0; for (int j = 0; j < n; j++) { int temp = reader.nextInt(); while (temp % 2 == 0) { temp /= 2; count++; } number[j] = temp; } Arrays.sort(number); long res = 0; for (int j = 0; j < number.length - 1; j++) { res += number[j]; } res += number[number.length - 1] * Math.pow(2, count); System.out.println(res); } } 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 String nextLine() { String str; try { str = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return str; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
a3980921a2aa175bd1e690e652373a2d
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.util.*; /** * @author atulanand */ public class Solution { static class Result { public long solve(int[] arr) { int pow = 0; for (int k = 0; k < arr.length; k++) { while (arr[k] % 2 == 0) { arr[k] /= 2; pow++; } } Arrays.sort(arr); long res = (long) (arr[arr.length - 1] * Math.pow(2, pow)); for (int i = 0; i + 1 < arr.length; i++) { res += arr[i]; } return res; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = inputInt(br); Result result = new Result(); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int[] spec = inputIntArray(br); sb.append(result.solve(inputIntArray(br))).append("\n"); } System.out.println(sb); } public static char[] inputCharArrayW(BufferedReader br) throws IOException { String[] spec = inputString(br).split(" "); char[] arr = new char[spec.length - 1]; for (int i = 1; i < spec.length; i++) arr[i - 1] = spec[i].toCharArray()[0]; return arr; } public static char[][] inputCharGrid(BufferedReader br, int rows) throws IOException { char[][] grid = new char[rows][]; for (int i = 0; i < grid.length; i++) { grid[i] = inputString(br).toCharArray(); } return grid; } public static int[][] inputIntGrid(BufferedReader br, int rows) throws IOException { int[][] grid = new int[rows][]; for (int i = 0; i < grid.length; i++) { grid[i] = inputIntArrayW(br); } return grid; } public static char[] inputCharArray(BufferedReader br) throws IOException { return inputString(br).toCharArray(); } public static String inputString(BufferedReader br) throws IOException { return br.readLine().trim(); } public static int inputInt(BufferedReader br) throws IOException { return Integer.parseInt(inputString(br)); } public static long inputLong(BufferedReader br) throws IOException { return Long.parseLong(inputString(br)); } public static int[] inputIntArray(BufferedReader br) throws IOException { String[] spec = inputString(br).split(" "); int[] arr = new int[spec.length]; for (int i = 0; i < spec.length; i++) arr[i] = Integer.parseInt(spec[i]); return arr; } public static int[] inputIntArrayW(BufferedReader br) throws IOException { String[] spec = inputString(br).split(" "); int[] arr = new int[spec.length - 1]; for (int i = 1; i < spec.length; i++) arr[i - 1] = Integer.parseInt(spec[i]) - 1; return arr; } public static long[] inputLongArray(BufferedReader br) throws IOException { String[] spec = inputString(br).split(" "); long[] arr = new long[spec.length]; for (int i = 0; i < spec.length; i++) arr[i] = Long.parseLong(spec[i]); return arr; } private String stringify(char[] chs) { StringBuilder sb = new StringBuilder(); for (char ch : chs) { sb.append(ch); } return sb.toString(); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
d2a38e663b85387790fc423267c5000a
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { private static boolean arr[] = sieve(1000001); private static ArrayList<Long> primes = new ArrayList<>(); private static int freq[] = new int[200005]; private static int mod = (int) (1e9 + 7); private static final int IMAX = 2147483647; private static final int IMIN = -2147483648; private static final long LMAX = 9223372036854775807L; private static final long LMIN = -9223372036854775808L; private static FastScanner c; private static PrintWriter pw; // **********************************Code Begins From // Here*************************************** // DecimalFormat df = new DecimalFormat("#.000000000000"); // map.put(key,map.getOrDefault(key,0)+1); public static void solve() { int n = c.i(); long a[] = c.longArray(n), k = 0, sum = 0; for (int i = 0; i < n; i++) while (a[i] % 2 == 0) { a[i] /= 2; k++; } sort(a); while (k > 0) { a[n - 1] *= 2; k--; } for (long e : a) sum += (long) e; pn(sum); } public static void main(String[] args) throws FileNotFoundException { // Scanner sc = new Scanner(System.in); c = new FastScanner(); pw = new PrintWriter(System.out); int tc = 1; tc = c.i(); long start = System.currentTimeMillis(); for (int t = 0; t < tc; t++) { // p("Case #" + (t + 1) + ": "); solve(); } long end = System.currentTimeMillis(); if (System.getProperty("os.name").equals("Mac OS X")) { pn("The Program takes " + (end - start) + "ms"); } // for (long i = 0; i < arr.length; i++) { // if (arr[(int) i]) { // primes.add(i); // } // } pw.close(); } // ArrayList<Integer> al = new ArrayList<>(); // Set<Integer> set = new TreeSet<>(); // Pair<Integer, Integer> pair; // Map<Integer, Integer> map = new HashMap<>(); // for(Map.Entry<Integer,Integer> e:map.entrySet())pn(e.getKey()+" // "+e.getValue()); // LinkedList<Integer> ll = new LinkedList<>(); // math util private static int cei(double d) { return (int) Math.ceil(d); } private static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } private static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } private static int abs(int a, int b) { return (int) Math.abs(a - b); } private static int abs(int a) { return (int) Math.abs(a); } private static long abs(long a) { return (long) Math.abs(a); } private static long abs(long a, long b) { return (long) Math.abs(a - b); } private static int max(int a, int b) { if (a > b) { return a; } else { return b; } } private static int min(int a, int b) { if (a > b) { return b; } else { return a; } } private static long max(long a, long b) { if (a > b) { return a; } else { return b; } } private static long min(long a, long b) { if (a > b) { return b; } else { return a; } } private static int pow(int base, int exp) { int result = 1; while (exp != 0) { if ((exp & 1) == 1) result *= base; exp >>= 1; base *= base; } return result; } // array util private static void sortList(ArrayList<Integer> al) { Collections.sort(al); } private static void sort(int[] a) { Arrays.parallelSort(a); } private static void sort(long[] a) { Arrays.parallelSort(a); } private static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } private static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } private static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } // output private static void pn(Object o) { pw.println(o); } private static void p(Object o) { pw.print(o); } private static void pry() { pn("Yes"); } private static void pY() { pn("YES"); } private static void prn() { pn("No"); } private static void pN() { pn("NO"); } private static void flush() { pw.flush(); } private static void watch(int[] a) { for (int e : a) p(e + " "); pn(""); } private static void watchList(ArrayList<Integer> al) { for (int e : al) p(e + " "); pn(""); } private static Set<Integer> putSet(int[] a) { Set<Integer> set = new TreeSet<>(); for (int e : a) set.add(e); return set; } private static boolean[] sieve(int n) { boolean[] isPrime = new boolean[n + 1]; Arrays.fill(isPrime, true); isPrime[0] = false; isPrime[1] = false; for (int i = 2; i * i <= n; i++) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } return isPrime; } private static class Pair<U, V> implements Comparable<Pair<U, V>> { public final U first; public V second; public <U, V> Pair<U, V> makePair(U first, V second) { return new Pair<U, V>(first, second); } public Pair(U first, V second) { this.first = first; this.second = second; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Pair pair = (Pair) o; return !(first != null ? !first.equals(pair.first) : pair.first != null) && !(second != null ? !second.equals(pair.second) : pair.second != null); } @Override public int hashCode() { int result = first != null ? first.hashCode() : 0; result = 31 * result + (second != null ? second.hashCode() : 0); return result; } public Pair<V, U> swap() { return makePair(second, first); } @Override public String toString() { return "(" + first + "," + second + ")"; } @SuppressWarnings({ "unchecked" }) public int compareTo(Pair<U, V> o) { int value = ((Comparable<U>) first).compareTo(o.first); if (value != 0) { return value; } return ((Comparable<V>) second).compareTo(o.second); } } private static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() throws FileNotFoundException { if (System.getProperty("os.name").equals("Mac OS X")) { // Input is a file br = new BufferedReader(new FileReader("input.txt")); // PrintWriter class prints formatted representations // of objects to a text-output stream. PrintStream pw = new PrintStream(new FileOutputStream("output.txt")); System.setOut(pw); } else { // Input is System.in br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int i() { return Integer.parseInt(next()); } int[] intArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) ret[i] = i(); return ret; } long l() { return Long.parseLong(next()); } long[] longArray(int n) { long[] ret = new long[n]; for (int i = 0; i < n; i++) ret[i] = l(); return ret; } double d() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
e093f5545de066b53b9a5bb0db9a5cee
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { private static boolean arr[] = sieve(1000001); private static ArrayList<Long> primes = new ArrayList<>(); private static int freq[] = new int[200005]; private static int mod = (int) (1e9 + 7); private static final int IMAX = 2147483647; private static final int IMIN = -2147483648; private static final long LMAX = 9223372036854775807L; private static final long LMIN = -9223372036854775808L; private static FastScanner c; private static PrintWriter pw; private static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() throws FileNotFoundException { if (System.getProperty("os.name").equals("Mac OS X")) { // Input is a file br = new BufferedReader(new FileReader("input.txt")); // PrintWriter class prints formatted representations // of objects to a text-output stream. PrintStream pw = new PrintStream(new FileOutputStream("output.txt")); System.setOut(pw); } else { // Input is System.in br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int i() { return Integer.parseInt(next()); } int[] intArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) ret[i] = i(); return ret; } long l() { return Long.parseLong(next()); } long[] longArray(int n) { long[] ret = new long[n]; for (int i = 0; i < n; i++) ret[i] = l(); return ret; } double d() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // **********************************Code Begins From // Here*************************************** // DecimalFormat df = new DecimalFormat("#.000000000000"); // map.put(key,map.getOrDefault(key,0)+1); public static void solve() { int n = c.i(); long a[] = c.longArray(n), ans = 0; for (int i = 0; i < n; i++) { long b[] = copy(a), sum = 0; for (int j = 0; j < n; j++) { if (i == j) continue; while ((b[j] & 1) == 0) { b[i] <<= 1; b[j] >>= 1; } } for (long e : b) sum += e; // pn(Arrays.toString(b) + " " + sum); ans = max(ans, sum); } pn(ans); } public static void main(String[] args) throws FileNotFoundException { // Scanner sc = new Scanner(System.in); c = new FastScanner(); pw = new PrintWriter(System.out); int tc = 1; tc = c.i(); long start = System.currentTimeMillis(); for (int t = 0; t < tc; t++) { // p("Case #" + (t + 1) + ": "); solve(); } long end = System.currentTimeMillis(); if (System.getProperty("os.name").equals("Mac OS X")) { pn("The Program takes " + (end - start) + "ms"); } // for (long i = 0; i < arr.length; i++) { // if (arr[(int) i]) { // primes.add(i); // } // } pw.close(); } // ArrayList<Integer> al = new ArrayList<>(); // Set<Integer> set = new TreeSet<>(); // Pair<Integer, Integer> pair; // Map<Integer, Integer> map = new HashMap<>(); // for(Map.Entry<Integer,Integer> e:map.entrySet())pn(e.getKey()+" // "+e.getValue()); // LinkedList<Integer> ll = new LinkedList<>(); // math util private static int cei(double d) { return (int) Math.ceil(d); } private static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } private static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } private static int abs(int a, int b) { return (int) Math.abs(a - b); } private static int abs(int a) { return (int) Math.abs(a); } private static long abs(long a) { return (long) Math.abs(a); } private static long abs(long a, long b) { return (long) Math.abs(a - b); } private static int max(int a, int b) { if (a > b) { return a; } else { return b; } } private static int min(int a, int b) { if (a > b) { return b; } else { return a; } } private static long max(long a, long b) { if (a > b) { return a; } else { return b; } } private static long min(long a, long b) { if (a > b) { return b; } else { return a; } } private static int pow(int base, int exp) { int result = 1; while (exp != 0) { if ((exp & 1) == 1) result *= base; exp >>= 1; base *= base; } return result; } // array util private static void sortList(ArrayList<Integer> al) { Collections.sort(al); } private static void sort(int[] a) { Arrays.parallelSort(a); } private static void sort(long[] a) { Arrays.parallelSort(a); } private static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } private static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } // output private static void pn(Object o) { pw.println(o); } private static void p(Object o) { pw.print(o); } private static void pry() { pn("Yes"); } private static void pY() { pn("YES"); } private static void prn() { pn("No"); } private static void pN() { pn("NO"); } private static void flush() { pw.flush(); } private static void watch(int[] a) { for (int e : a) p(e + " "); pn(""); } private static void watchList(ArrayList<Integer> al) { for (int e : al) p(e + " "); pn(""); } private static Set<Integer> putSet(int[] a) { Set<Integer> set = new TreeSet<>(); for (int e : a) set.add(e); return set; } private static boolean[] sieve(int n) { boolean[] isPrime = new boolean[n + 1]; Arrays.fill(isPrime, true); isPrime[0] = false; isPrime[1] = false; for (int i = 2; i * i <= n; i++) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } return isPrime; } private static class Pair<U, V> implements Comparable<Pair<U, V>> { public final U first; public V second; public <U, V> Pair<U, V> makePair(U first, V second) { return new Pair<U, V>(first, second); } public Pair(U first, V second) { this.first = first; this.second = second; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Pair pair = (Pair) o; return !(first != null ? !first.equals(pair.first) : pair.first != null) && !(second != null ? !second.equals(pair.second) : pair.second != null); } @Override public int hashCode() { int result = first != null ? first.hashCode() : 0; result = 31 * result + (second != null ? second.hashCode() : 0); return result; } public Pair<V, U> swap() { return makePair(second, first); } @Override public String toString() { return "(" + first + "," + second + ")"; } @SuppressWarnings({ "unchecked" }) public int compareTo(Pair<U, V> o) { int value = ((Comparable<U>) first).compareTo(o.first); if (value != 0) { return value; } return ((Comparable<V>) second).compareTo(o.second); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
c7f7be27a3af59f4339f71941e3bd497
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.InputMismatchException; public class DivideMultiply { public static void main(String[] args) { int t = nextInt(); StringBuilder res = new StringBuilder(); while (t-- > 0) { int n = nextInt(); long max = 0; long total = 0, multi2 = 0; for (int i = 0; i < n; i++) { int num = nextInt(); while ((num & 1) == 0) { num >>= 1; multi2++; } if (num > max) { total += max; max = num; } else { total += num; } } total += max << multi2; res.append(total).append("\n"); } System.out.print(res); } static InputStream is = System.in; static byte[] inbuf = new byte[1 << 20]; static int lenbuf = 0, ptrbuf = 0; static int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } static int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } static double nextDouble() { return Double.parseDouble(next()); } static char nextChar() { return (char) skip(); } static String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } static String nextLine() throws IOException { int b = skip(); StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b) && b != '\n') { // when next, () sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } static char[] nextCharArray(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } static int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } static long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
3d8cea85b1e3c2126d6001495e17d68f
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { // -- static variables --- // static FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static int mod = (int) 1000000007; public static void main(String[] args) throws Exception { int t=sc.nextInt(); while(t-->0) Main.go(); // out.println(); out.flush(); } // >>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<< // static class pair{ int x;int y; pair(int x,int y){ this.x=x; this.y=y; } } // static int ask(int l ,int r) { // System.out.println("? "+l+" "+r); // int x=sc.nextInt(); // return x; // } static void go() throws Exception { int n=sc.nextInt(); long a[]=sc.longArray(n); if(n==1) { out.println(a[0]); return; } long max=Long.MIN_VALUE; for(int i=0;i<n;i++) { long sum=0; long m=a[i]; for(int j=0;j<n;j++) { if(i!=j) { long temp=a[j]; while(temp%2==0) { temp/=2; m*=2; } sum+=temp; } } sum+=m; max=Math.max(max,sum); } out.println(max); } static long lcm(long a,long b) { return a*b/gcd(a,b); } // >>>>>>>>>>> Code Ends <<<<<<<<< // // --For Rounding--// static double round(double value, int places) { if (places < 0) throw new IllegalArgumentException(); BigDecimal bd = new BigDecimal(Double.toString(value)); bd = bd.setScale(places, RoundingMode.HALF_UP); return bd.doubleValue(); } // ----Greatest Common Divisor-----// static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } // --- permutations and Combinations ---// static long fact[]; static long invfact[]; static long ncr(int n, int k) { if (k < 0 || k > n) { return 0; } long x = fact[n]; long y = fact[k]; long yy = fact[n - k]; long ans = (x / y); ans = (ans / yy); return ans; } // ---sieve---// static int prime[] = new int[1000006]; // static void sieve() { // Arrays.fill(prime, 1); // prime[0] = 0; // prime[1] = 0; // for (int i = 2; i * i <= 1000005; i++) { // if (prime[i] == 1) // for (int j = i * i; j <= 1000005; j += i) { // prime[j] = 0; // } // } // } // ---- Manual sort ------// static void sort(long[] a) { ArrayList<Long> aa = new ArrayList<>(); for (long i : a) { aa.add(i); } Collections.sort(aa); for (int i = 0; i < a.length; i++) a[i] = aa.get(i); } static void sort(int[] a) { ArrayList<Integer> aa = new ArrayList<>(); for (int i : a) { aa.add(i); } Collections.sort(aa); for (int i = 0; i < a.length; i++) a[i] = aa.get(i); } // --- Fast exponentiation ---// static long pow(long x, long y) { long res = 1l; while (y != 0) { if (y % 2 == 1) { res = (x * res); } y /= 2; x = (x * x); } return res; } // >>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<< // static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] intArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); return a; } long[] longArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = sc.nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
4aa37b29b0628be91b51801bbb8057c1
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class A1592 { 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[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } System.out.println(solve(arr, n)); t--; } } private static long solve(int[] arr, int n) { long max = 0; for (int i = 0; i < n; i++) { long temp = arr[i]; long sum = 0; for (int j = 0; j < n; j++) { if (j == i) continue; int temp1 = arr[j]; if (arr[j] % 2 == 0) { while (temp1 % 2 == 0) { temp *= 2; temp1 /= 2; } } sum += temp1; } sum += temp; if (sum > max) max = sum; } return max; } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
23818e2e30196088872fd93ed963606e
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.Arrays; import java.util.Scanner; /** * * @author eslam */ public class DivideAndMultiply { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); for (int i = 0; i < t; i++) { int n = input.nextInt(); long a[] = new long[n]; long sum = 0; for (int j = 0; j < n; j++) { a[j] = input.nextInt(); } Arrays.sort(a); int p2 = n - 1; while (p2 >= 0) { long ma = 0; long b []= new long[n]; for (int j = 0; j < n; j++) { b[j] = a[j]; } int p1 = 0; while (p1 < n) { if (p1 == p2) { p1++; continue; } if (b[p1] % 2 == 1) { p1++; continue; } b[p1] /= 2; b[p2] *= 2; } p2--; for (int j = 0; j < n; j++) { ma += b[j]; } sum = Math.max(sum, ma); } System.out.println(sum); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
addb6a9caf25ce4bfd5201eead4fd66b
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
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.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Akash Deep Vishwakarma | LinkedIn : https://www.linkedin.com/in/vishdeep01/ */ 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); ADivideAndMultiply solver = new ADivideAndMultiply(); solver.solve(1, in, out); out.close(); } static class ADivideAndMultiply { public void solve(int testNumber, InputReader in, OutputWriter out) { int t = in.readInt(); while (t-- > 0) { int n = in.readInt(); long[] a = in.readLongArray(n); long temp = 1; for (int i = 0; i < n; i++) { while (a[i] % 2 == 0) { a[i] /= 2; temp *= 2; } } Arrays.sort(a); a[a.length - 1] *= temp; long sum = 0; for (long i : a) sum += i; out.printLine(sum); } } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public long[] readLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = readLong(); } return array; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
98005d44b7bf9438895ca3301e194f7e
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.io.*; public class DivideAndMultiply { static long t = 0; public static void main(String[] args) throws IOException { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(r.readLine()); int T = Integer.parseInt(st.nextToken()); for (int i = 0; i < T; i++) { st = new StringTokenizer(r.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(r.readLine()); ArrayList<Long> nums = new ArrayList<Long>(); long[] sums = new long[n]; for (int j = 0; j < n; j++) nums.add(Long.parseLong(st.nextToken())); Collections.sort(nums); ArrayList<Long> tempNums = new ArrayList<Long>(); for (long j : nums) tempNums.add(j); for (int j = 0; j < n; j++) { t = nums.remove(j); for (int x = 0; x < nums.size(); x++) nums.set(x, divideTwo(nums.get(x))); nums.add(t); long sum = 0; for (long x : nums) sum += x; sums[j] = sum; nums.removeAll(nums); for (long x : tempNums) nums.add(x); } long max = Long.MIN_VALUE; for (long j : sums) { if (j > max) max = j; } pw.println(max); } pw.close(); r.close(); } static long divideTwo(long n) { while (n % 2 == 0) { n /= 2l; t *= 2l; } return n; } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
ebf6854c66415f09dde7c8315bdaff85
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
/***** ---> :) Vijender Srivastava (: <--- *****/ import java.util.*; import java.lang.*; import java.io.*; public class Main { static FastReader sc =new FastReader(); static PrintWriter out=new PrintWriter(System.out); static int mod=10000007; /* start */ public static void main(String [] args) { int t = i(); while(t-->0) { int n = i(); long a[] = inputL(n); long ans = 1; for(int i=0;i<n;i++) { while(a[i]%2==0) { ans *= 2; a[i]/=2; } } Arrays.sort(a); a[n-1]*=ans; pl(sum(a)); } out.close(); } /* end */ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void p(Object o) { out.print(o); } static void pl(Object o) { out.println(o); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static long[] putL(long a[]) { long A[]=new long[a.length]; for(int i=0;i<a.length;i++) { A[i]=a[i]; } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x) ; y = y >> 1; x = (x * x); } return res; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } //pair class private static class Pair implements Comparable<Pair> { int first, second; public Pair(int f, int s) { first = f; second = s; } @Override public int compareTo(Pair p) { if (first > p.first) return 1; else if (first < p.first) return -1; else { if (second > p.second) return 1; else if (second < p.second) return -1; else return 0; } } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
120eeb5fa7e44e7468b4d7886d876258
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class A{ public static void main(String[] args){ FastReader sc = new FastReader(); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int a[]=sc.fastArray(n); int cnt=0; for(int i=0;i<n;i++) { while((a[i]&1)==0) { a[i]/=2; cnt++; } } sort(a); long res=(long)Math.pow(2,cnt); res*=a[n-1]; for(int i=0;i<n-1;i++) res+=a[i]; System.out.println(res); } } static class pair { int x;int y; pair(int x,int y){ this.x=x; this.y=y; } } static void sort(int a[]) { ArrayList<Integer> aa= new ArrayList<>(); for(int i:a)aa.add(i); Collections.sort(aa); int j=0; for(int i:aa)a[j++]=i; } static ArrayList<Integer> primeFac(int n){ ArrayList<Integer>ans = new ArrayList<Integer>(); int lp[]=new int [n+1]; Arrays.fill(lp, 0); //0-prime for(int i=2;i<=n;i++) { if(lp[i]==0) { for(int j=i;j<=n;j+=i) { if(lp[j]==0) lp[j]=i; } } } int fac=n; while(fac>1) { ans.add(lp[fac]); fac=fac/lp[fac]; } print(ans); return ans; } static ArrayList<Long> prime_in_given_range(long l,long r){ ArrayList<Long> ans= new ArrayList<>(); int n=(int)Math.sqrt(r)+1; int prime[]=sieve_of_Eratosthenes(n); long res[]=new long [(int)(r-l)+1]; for(int i=0;i<=r-l;i++) { res[i]=i+l; } for(int i=0;i<prime.length;i++) { if(prime[i]==1) { System.out.println(2); for(int j=Math.max((int)i*i, (int)(l+i-1)/i*i);j<=r;j+=i) { res[j-(int)l]=0; } } } for(long i:res) if(i!=0)ans.add(i); return ans; } static int [] sieve_of_Eratosthenes(int n) { int prime[]=new int [n]; Arrays.fill(prime, 1); // 1-prime | 0-not prime prime[0]=prime[1]=0; for(int i=2;i<n;i++) { if(prime[i]==1) { for(int j=i*i;j<n;j+=i) { prime[j]=0; } } } return prime; } static long binpow(long a,long b) { long res=1; if(b==0)return a; if(a==0)return 1; while(b>0) { if((b&1)==1) { res*=a; } a*=a; b>>=1; } return res; } static void print(int a[]) { System.out.println(a.length); for(int i:a) { System.out.print(i+" "); } System.out.println(); } static void print(long a[]) { System.out.println(a.length); for(long i:a) { System.out.print(i+" "); } System.out.println(); } static void print(ArrayList<Integer> a) { System.out.println(a.size()); for(long i:a) { System.out.print(i+" "); } System.out.println(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } int [] fastArray(int n) { int a[]=new int [n]; for(int i=0;i<n;i++) { a[i]=nextInt(); } return a; } 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; } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
2bb30d5f28d3b71f02345cdc5bf74e7f
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
//import java.io.IOException; import java.util.*; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.InputMismatchException; import java.util.List; public class DividandMultipl { static InputReader inputReader=new InputReader(System.in); static void solve() { int n=inputReader.nextInt(); long arr[]=new long[n]; for (int i=0;i<n;i++) { arr[i]=inputReader.nextLong(); } long p2=1; for (int i=0;i<n;i++) { while (arr[i]%2==0) { p2=p2*2; arr[i]=arr[i]/2; } } Arrays.sort(arr); long sum=0; for (int i=0;i<n-1;i++) { sum+=arr[i]; } sum+=arr[n-1]*p2; out.println(sum); } static PrintWriter out=new PrintWriter((System.out)); public static void main(String args[])throws IOException { int t=inputReader.nextInt(); while(t-->0) { solve(); } out.close(); } static void sortDec(int arr[]) { int len=arr.length; List<Integer>list=new ArrayList<>(); for(int ele:arr) { list.add(ele); } Collections.sort(list,Collections.reverseOrder()); for(int i=0;i<len;i++) { arr[i] = list.get(i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
82b330257c7d460725a35b9e6f074a2a
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class Main { static class Pair { int l,r,x; public Pair(int l,int r,int x) { this.l=l; this.r=r; this.x=x; } } static int mod=1000000007; public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public static void solve(int testNumber, InputReader in, OutputWriter out) { int t=in.readInt(); while(t-->0) { int n=in.readInt(); long arr[]=in.nextLongArray(n); long sum=0; long tp=1; for(int i=0;i<n;i++) { while(arr[i]%2==0) { arr[i]/=2; tp*=2; } } CP.sort(arr); arr[n-1]=arr[n-1]*tp; for(int i=0;i<n;i++) { sum+=arr[i]; } out.printLine(sum); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int arraySize) { int[] array = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = readInt(); } return array; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isNewLine(int c) { return c == '\n'; } public String nextLine() { int c = read(); StringBuilder result = new StringBuilder(); do { result.appendCodePoint(c); c = read(); } while (!isNewLine(c)); return result.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sign = 1; if (c == '-') { sign = -1; c = read(); } long result = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } result *= 10; result += c & 15; c = read(); } while (!isSpaceChar(c)); return result * sign; } public long[] nextLongArray(int arraySize) { long array[] = new long[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextLong(); } return array; } public double nextDouble() { double ret = 0, div = 1; byte c = (byte) read(); while (c <= ' ') { c = (byte) read(); } boolean neg = (c == '-'); if (neg) { c = (byte) read(); } do { ret = ret * 10 + c - '0'; } while ((c = (byte) read()) >= '0' && c <= '9'); if (c == '.') { while ((c = (byte) read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class CP { static boolean isPrime(long n) { if (n <= 1) return false; if (n == 2 || n == 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; (long) i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } static String addChar(String s, int n, String ch) { String res =s+String.join("", Collections.nCopies(n, ch)); return res; } static int ifnotPrime(int[] prime, int x) { return (prime[x / 64] & (1 << ((x >> 1) & 31))); } static void makeComposite(int[] prime, int x) { prime[x / 64] |= (1 << ((x >> 1) & 31)); } static ArrayList<Integer> bitWiseSieve(int n) { ArrayList<Integer> al = new ArrayList<>(); int prime[] = new int[n / 64 + 1]; for (int i = 3; i * i <= n; i += 2) { if (ifnotPrime(prime, i) == 0) for (int j = i * i, k = i << 1; j < n; j += k) makeComposite(prime, j); } al.add(2); for (int i = 3; i <= n; i += 2) if (ifnotPrime(prime, i) == 0) al.add(i); return al; } public static long[] sort(long arr[]){ List<Long> list = new ArrayList<>(); for(long n : arr){list.add(n);} Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i] = list.get(i); } return arr; } static ArrayList<Integer> sieve(long size) { ArrayList<Integer> pr = new ArrayList<Integer>(); boolean prime[] = new boolean[(int) size]; for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i * i < prime.length; i++) { if (prime[i]) { for (int j = i * i; j < prime.length; j += i) { prime[j] = false; } } } for (int i = 2; i < prime.length; i++) if (prime[i]) pr.add(i); return pr; } static ArrayList<Integer> segmented_sieve(int l, int r, ArrayList<Integer> primes) { ArrayList<Integer> al=new ArrayList<>(); if (l == 1) ++l; int max = r - l + 1; int arr[] = new int[max]; for (int p : primes) { if (p * p <= r) { int i = (l / p) * p; if (i < l) i += p; for (; i <= r; i += p) { if (i != p) { arr[i - l] = 1; } } } } for (int i = 0; i < max; ++i) { if (arr[i] == 0) { al.add(l+i); } } return al; } static boolean isfPrime(long n, int iteration) { if (n == 0 || n == 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; Random rand = new Random(); for (int i = 0; i < iteration; i++) { long r = Math.abs(rand.nextLong()); long a = r % (n - 1) + 1; if (modPow(a, n - 1, n) != 1) return false; } return true; } static long modPow(long a, long b, long c) { long res = 1; for (int i = 0; i < b; i++) { res *= a; res %= c; } return res % c; } private static long binPower(long a, long l, long mod) { long res = 0; while (l > 0) { if ((l & 1) == 1) { res = mulmod(res, a, mod); ; l >>= 1; } a = mulmod(a, a, mod); } return res; } private static long mulmod(long a, long b, long c) { long x = 0, y = a % c; while (b > 0) { if (b % 2 == 1) { x = (x + y) % c; } y = (y * 2L) % c; b /= 2; } return x % c; } static long binary_Expo(long a, long b) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res *= a; --b; } a *= a; b /= 2; } return res; } static long Modular_Expo(long a, long b) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res = (res * a) % 1000000007 ; --b; } a = (a * a) % 1000000007; b /= 2; } return res%1000000007; } static int i_gcd(int a, int b) { while (true) { if (b == 0) return a; int c = a; a = b; b = c % b; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long ceil_div(long a, long b) { return (a + b - 1) / b; } static int getIthBitFromInt(int bits, int i) { return (bits >> (i - 1)) & 1; } private static TreeMap<Long, Long> primeFactorize(long n) { TreeMap<Long, Long> pf = new TreeMap<>(Collections.reverseOrder()); long cnt = 0; long total = 1; for (long i = 2; (long) i * i <= n; ++i) { if (n % i == 0) { cnt = 0; while (n % i == 0) { ++cnt; n /= i; } pf.put(cnt, i); //total*=(cnt+1); } } if (n > 1) { pf.put(1L, n); //total*=2; } return pf; } static long upper_Bound(long a[], long x) { long l = -1, r = a.length; while (l + 1 < r) { long m = (l + r) >>> 1; if (a[(int) m] <= x) l = m; else r = m; } return l + 1; } static int lower_Bound(ArrayList<Long> a, long x) { int l = 0, r = a.size()-1,ans=-1,mid=0; while(l<=r) { mid=(l+r)>>1; if(a.get(mid)<=x) { ans=mid; l=mid+1; } else { r=mid-1; } } return ans; } static int lower_Bound(int a[],int x) { int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[(int) 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 int bsh(int a[],int t) { int ans =-1; int i = 0, j = a.length - 1; while (i <= j) { int mid = i + (j - i) / 2; if (a[mid] > t) { ans = mid; //System.out.print("uppr "+a[ans]); j=mid-1; } else{ //System.out.print("less "+a[mid]); i=mid+1; } } return ans; } static int bsl(int a[],int t) { int ans =-1; int i = 0, j = a.length-1; while (i <= j) { int mid = i + (j - i) / 2; if (a[mid] == t) { ans = mid; break; } else if (a[mid] > t) { j = mid - 1; } else { ans=mid; i = mid + 1; } } return ans; } static int upper_Bound(ArrayList<Long> a, long x) //closest to the right { int l = 0, r = a.size()-1,ans=-1,mid=0; while(l<=r) { mid=(l+r)>>1; if(a.get(mid)>=x) { ans=mid; r=mid-1; } else { l=mid+1; } } return ans; } static boolean isSquarefactor(int x, int factor, int target) { int s = (int) Math.round(Math.sqrt(x)); return factor * s * s == target; } static boolean isSquare(int x) { int s = (int) Math.round(Math.sqrt(x)); return x * x == s; } static void sort(int a[]) // heap sort { PriorityQueue<Integer> q = new PriorityQueue<>(); for (int i = 0; i < a.length; i++) q.add(a[i]); for (int i = 0; i < a.length; i++) a[i] = q.poll(); } static void shuffle(int[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); fast_swap(in, idx, i); } } static boolean isPalindrome(String s) { StringBuilder sb=new StringBuilder(s); sb.reverse(); if(s.equals(sb.toString())) { return true; } return false; } static int[] computeLps(String pat) { int len = 0, i = 1, m = pat.length(); int lps[] = new int[m]; lps[0] = 0; while (i < m) { if (pat.charAt(i) == pat.charAt(len)) { ++len; lps[i] = len; ++i; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = len; ++i; } } } return lps; } static void kmp(String s, String pat,int[] prf,int[] st) { int n = s.length(), m = pat.length(); int lps[] = computeLps(pat); int i = 0, j = 0; while (i < n) { if (s.charAt(i) == pat.charAt(j)) { i++; j++; if (j == m) { ++prf[(i-j)+m]; ++st[(i-j)+1]; j=lps[j - 1]; } } else { if (j != 0) { j = lps[j - 1]; } else { i++; } } } } static void reverse_ruffle_sort(int a[]) { shuffle(a); Arrays.sort(a); for (int l = 0, r = a.length - 1; l < r; ++l, --r) fast_swap(a, l, r); } static void ruffle_sort(int a[]) { shuffle(a); Arrays.sort(a); } static int getMax(int arr[], int n) { int mx = arr[0]; for (int i = 1; i < n; i++) if (arr[i] > mx) mx = arr[i]; return mx; } static ArrayList<Long> primeFactors(long n) { ArrayList<Long> al = new ArrayList<>(); al.add(1L); while (n % 2 == 0) { if(!al.contains(2L)) { al.add(2L); } n /= 2L; } for (int i = 3; (long) i * i <= n; i += 2) { while ((n % i == 0)) { if(!al.contains((long)i)) { al.add((long) i); } n /= i; } } if (n > 2) { if(!al.contains(n)) { al.add(n); } } return al; } static int[] z_function(String s) { int n = s.length(), z[] = new int[n]; for (int i = 1, l = 0, r = 0; i < n; ++i) { if (i <= r) z[i] = Math.min(z[i - l], r - i + 1); while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i])) ++z[i]; if (i + z[i] - 1 > r) { l = i; r = i + z[i] - 1; } } return z; } static void swap(int a[], int idx1, int idx2) { a[idx1] += a[idx2]; a[idx2] = a[idx1] - a[idx2]; a[idx1] -= a[idx2]; } static void fast_swap(int[] a, int idx1, int idx2) { if (a[idx1] == a[idx2]) return; a[idx1] ^= a[idx2]; a[idx2] ^= a[idx1]; a[idx1] ^= a[idx2]; } public static void fast_sort(long[] array) { ArrayList<Long> copy = new ArrayList<>(); for (long i : array) copy.add(i); Collections.sort(copy); for (int i = 0; i < array.length; i++) array[i] = copy.get(i); } static int divCount(int n) { boolean hash[] = new boolean[n + 1]; Arrays.fill(hash, true); for (int p = 2; p * p < n; p++) if (hash[p] == true) for (int i = p * 2; i < n; i += p) hash[i] = false; int total = 1; for (int p = 2; p <= n; p++) { if (hash[p]) { int count = 0; if (n % p == 0) { while (n % p == 0) { n = n / p; count++; } total = total * (count + 1); } } } return total; } static long binomialCoeff(long n,long k) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (int i = 0; i < k; ++i) { res = (res*(n - i)); res /= (i + 1); } return res; } static long c(long fact[],long n,long k) { if(k>n) return 0; long res=fact[(int)n]; res= (int) ((res * Modular_Expo(fact[(int)k], mod - 2))%mod); res= (int) ((res * Modular_Expo(fact[(int)n - (int)k], mod - 2))%mod); return res%mod; } public static ArrayList<Long> getFactors(long x) { ArrayList<Long> facts=new ArrayList<>(); for(long i=2;i*i<=x;++i) { if(x%i==0) { facts.add(i); if(i!=x/i) { facts.add(x/i); } } } return facts; } public static HashMap<Integer, Integer> sortMap(HashMap<Integer, Integer> map) { List<Map.Entry<Integer,Integer>> list=new LinkedList<>(map.entrySet()); Collections.sort(list,(o1,o2)->o2.getValue()-o1.getValue()); HashMap<Integer,Integer> temp=new LinkedHashMap<>(); for(Map.Entry<Integer,Integer> i:list) { temp.put(i.getKey(),i.getValue()); } return temp; } public static long lcm(long l,long l2) { long val=CP.gcd(l,l2); return (l*l2)/val; } } static void py() { System.out.println("YES"); } static void pn() { System.out.println("NO"); } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } public void printLine(int i) { writer.println(i); } public void printLine(char c) { writer.println(c); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
d72f645397d91d7d2311e778f893c70c
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; import java.util.*; /*AUTHOR - ELDIIAR DZHUNUSOV */ public class a { static int mod= 1000000007 ; static final FastReader in = new FastReader(); static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { long t = in.nextInt(); for (long i = 1; i <= t; i++) { new Solver(); } out.flush(); out.close(); } static class Solver{ Solver(){ int n = in.nextInt(); long[] v1 = new long[n]; long[] v2 = new long[n]; for (int i = 0; i < n; i++) { v1[i] = in.nextInt(); v2[i] = v1[i]; } long maxSum = 0; for (int i = 0; i < n; i++) { long localMax = 0; long[] arr = Arrays.copyOf(v1,v1.length); for (int j = 0; j < n; j++) { if(i==j){ continue; } while(arr[j]>1 && arr[j]%2==0){ arr[j]/=2; arr[i]*=2; } } for (int j = 0; j < n; j++) { localMax+=arr[j]; } maxSum = Math.max(localMax,maxSum); } out.println(maxSum); } } // Collections.sort(arrayList); // sort 1d // sort(arr, 0, arr.length - 1); // Sort 2d by the first index // Arrays.sort(arr, Comparator.comparingDouble(o -> o[0])); public static long lBinSearch(long l, long r, long... numbers){ //Assuming that we know that the elements exists there for sure, but we want to find the most optimal one //After some point all numbers will be considered as "Good" //We need to find most left "Good" number while(l<r){ long m = l + (r-l)/2; if(checkerForBinarySearch(numbers[0],numbers[1],numbers[2],m)){ r = m; }else{ l = m+1; } } return l; } // public static long rBinSearch(int l, int r, int... numbers){ // //Assuming that we know that the elements exists there for sure, but we want to find the most optimal one // //After some point all numbers will be considered as "Bad" // //We need to find most Right "Good" number // while(l<r){ // int m = l + (r-l+1)/2; // if(checkerForBinarySearch(numbers)){ // l = m; // }else{ // r = m-1; // } // } // return l; // } public static boolean checkerForBinarySearch(long... nums){ // out.println(Arrays.toString(nums)); double a = nums[0]; double b = nums[1]; double c = nums[2]; double d = nums[3]; BigDecimal num1 = BigDecimal.valueOf((a*2 + b*3 + c*4 +d*5)); BigDecimal num2 = BigDecimal.valueOf(a+ b + c + d); num1 = num1.divide(num2, MathContext.DECIMAL128); // System.out.println(num1); return num1.compareTo(BigDecimal.valueOf(3.5))>=0; // out.println(div1 + " " + nums[3]); // // return Double.compare(((a*2 + b*3 + c*4 +d*5)/ (a+ b + c + d)),3.5)>=0; } public static void swap(int[] arr, int i, int j){ arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; } static boolean isPrime(long n) { if(n==1)return false ; for(long i = 2L; i*i <= n ;i++){ if(n% i ==0){return false; } } return true ; } static boolean isPrime(int n) { if(n==1)return false ; for(int i = 2; i*i <= n ;i++){ if(n% i ==0){return false ; } } return true ; } public static int gcd(int a, int b ) { if(b==0)return a ;else return gcd(b,a%b) ; } public static long gcd(long a, long b ) { if(b==0)return a ;else return gcd(b,a%b) ; } static boolean isPower(int n, int p){ if(p==0) return n==1; return (Double.compare(Math.pow(n,1.0/p),(int)Math.pow(n,1.0/p))==0); } static boolean isPower(long n, long p){ if(p==0L) return n==1L; return (Double.compare(Math.pow(n,1.0/p),(long)Math.pow(n,1.0/p))==0); } public static long nCr(int n,int k) { long ans=1L; k=k>n-k?n-k:k; int j=1; for(;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } public static ArrayList<Integer> allFactors(int n) { ArrayList<Integer> list = new ArrayList<>() ; for(int i = 1; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n)list.add(i) ; else{ list.add(i) ;list.add(n/i) ; } } } return list ; } public static ArrayList<Long> allFactors(long n) { ArrayList<Long> list = new ArrayList<>() ; for(long i = 1L; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) list.add(i) ; else{ list.add(i) ; list.add(n/i) ; } } } return list ; } public static void reverseArray(int[] arr){ int i = 0; int j = arr.length-1; while(i<j){ swap(arr,i,j); i++; j--; } } 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; } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
58273046d96a162b3eed9e14fbab383d
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
// HOPE //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////// ////////// /////// ////////// /////// RRRRRRRRRRRRR YY YY AA NN NN ////////// /////// RR RR YY YY AA AA NN NN NN ////////// /////// RR RR YY YY AA AA NN NN NN ////////// /////// RRRRRRRRRR YY YY AAAAAAAAAAAA NN NN NN ////////// /////// RR RR YY AAAAAAAAAAAAAA NN NN NN ////////// /////// RR RR YY AA AA NN NNNN ////////// /////// RR RR YY AA AA NN NN ////////// /////// RR RR_____________________________________________________________________________ ////////// //////// ////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Codeforces1 { static PrintWriter out = new PrintWriter(System.out); static final int mod=1_000_000_007; 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()); } int[] readIntArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } long[] readLongArray(int n){ long[] a = new long[n]; for(int i=0;i<n;i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //Try seeing general case public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); int[] a = s.readIntArray(n); out.println(find(n, a)); } out.close(); } public static long find(int n, int[] a) { if (n == 1) return a[0]; int count = 0; for(int i=0;i<n;i++) { while((a[i]&1)==0) { count++; a[i] /= 2; } } sort(a); long sum = 0; for(int i=0;i<n-1;i++) { sum += a[i]; } sum += a[n-1]*Math.pow(2, count); return sum; } /////////////////////////////////////////////////THE END/////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static int gcd(int x,int y) { return y==0?x:gcd(y,x%y); } public static long gcd(long x,long y) { return y==0L?x:gcd(y,x%y); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static long pow(long a,long b) { if(b==0L) return 1L; long tmp=1; while(b>1L) { if((b&1L)==1) tmp*=a; a*=a; b>>=1; } return (tmp*a); } public static long modPow(long a,long b,long mod) { if(b==0L) return 1L; long tmp=1; while(b>1L) { if((b&1L)==1L) tmp*=a; a*=a; a%=mod; tmp%=mod; b>>=1; } return (tmp*a)%mod; } static long mul(long a, long b) { return a*b%mod; } static long fact(int n) { long ans=1; for (int i=2; i<=n; i++) ans=mul(ans, i); return ans; } static long fastPow(long base, long exp) { if (exp==0) return 1; long half=fastPow(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static void debug(int ...a) { for(int x: a) out.print(x+" "); out.println(); } static void debug(long ...a) { for(long x: a) out.print(x+" "); out.println(); } static void debugMatrix(int[][] a) { for(int[] x:a) out.println(Arrays.toString(x)); } static void debugMatrix(long[][] a) { for(long[] x:a) out.println(Arrays.toString(x)); } static void reverseArray(int[] a) { int n = a.length; int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void reverseArray(long[] a) { int n = a.length; long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void sort(int[] a) { ArrayList<Integer> ls = new ArrayList<>(); for(int x: a) ls.add(x); Collections.sort(ls); for(int i=0;i<a.length;i++) a[i] = ls.get(i); } static void sort(long[] a) { ArrayList<Long> ls = new ArrayList<>(); for(long x: a) ls.add(x); Collections.sort(ls); for(int i=0;i<a.length;i++) a[i] = ls.get(i); } static class Pair{ int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public String toString() { return "Pair{" + "x=" + x + ", y=" + y + '}'; } } static class MyCmp implements Comparator<Pair> { public int compare(Pair p1, Pair p2) { return p1.x-p2.x; } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
ef64567a5770ae374ea8c5c9a185156c
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.util.*; public class cp { public static void main(String[] args) throws IOException { //Your Solve // Reader s = new Reader(); FastReader s = new FastReader(); // Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int p = 0;p < t;p++) { int n = s.nextInt(); int arr[] = new int[n]; long ans = 0; for(int i = 0;i < n;i++) { arr[i] = s.nextInt(); } // Arrays.sort(arr); int power2 = 0; // for(int i = n-1;i >= 0;i--) { // if(arr[i] % 2 == 1) { // int temp = arr[n-1]; // arr[n-1] = arr[i]; // arr[i] = temp; // break; // } // } for(int i = 0;i < n;i++) { while(arr[i] % 2== 0) { arr[i] /= 2; power2++; } } Arrays.sort(arr); for(int i = 0;i < n-1;i++) { ans += arr[i]; } ans += Math.pow(2, power2)*arr[n-1]; System.out.println(ans); } } static long binomialCoeff(long n, long k) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } public static boolean palindrome(Vector<Integer> v) { int start = 0;int end = v.size()-1; while(start<end) { if(v.get(start) != v.get(end)) { return false; } start++; end--; } return true; } static void shuffleArray(int[] ar) { // If running on Java 6 or older, use `new Random()` on RHS here Random rnd = new Random(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap int a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } /* Iterative Function to calculate (x^y) in O(log y) */ static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long getPairsCount(int n, long sum,long arr[]) { HashMap<Long, Integer> hm = new HashMap<>(); // Store counts of all elements in map hm for (int i = 0; i < n; i++) { // initializing value to 0, if key not found if (!hm.containsKey(arr[i])) hm.put(arr[i], 0); hm.put(arr[i], hm.get(arr[i]) + 1); } long twice_count = 0; // iterate through each element and increment the // count (Notice that every pair is counted twice) for (int i = 0; i < n; i++) { if (hm.get(sum - arr[i]) != null) twice_count += hm.get(sum - arr[i]); // if (arr[i], arr[i]) pair satisfies the // condition, then we need to ensure that the // count is decreased by one such that the // (arr[i], arr[i]) pair is not considered if (sum - arr[i] == arr[i]) twice_count--; } // return the half of twice_count return twice_count / 2; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >( hm.entrySet()); // Sort the list using lambda expression Collections.sort( list, (i1, i2) -> i1.getValue().compareTo(i2.getValue())); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } public static String palin(String str) { StringBuilder ans = new StringBuilder(); for(int i = str.length()-1;i >= 0 ;i--) { ans.append(str.charAt(i)); } return ans.toString(); } public static String palin(StringBuilder str) { StringBuilder ans = new StringBuilder(); for(int i = str.length()-1;i >= 0 ;i--) { ans.append(str.charAt(i)); } return ans.toString(); } public static int getInt(char ch) { int ans = 0; if(ch >= '0' && ch <= '9') { ans = ch-'0'; }else if(ch >= 'A' && ch <= 'Z') { ans = ch-'A' + 10; }else if(ch >= 'a' && ch <= 'z') { ans = ch-'a' + 36; }else if(ch == '-') { ans = 62; }else { ans = 63; } return ans; } public static int getZeroes(int base64) { int count = 0; int iters = 0; while(iters != 6) { if(base64%2 == 0) { count++; } base64 /= 2; iters++; } return count; } static int lower_bound(long array[], long key,long start,long end) { // Initialize starting index and // ending index long low = start, high = end; long mid; // Till high does not crosses low while (low < high) { // Find the index of the middle element mid = low + (high - low) / 2; // If key is less than or equal // to array[mid], then find in // left subarray if (key <= array[(int)mid]) { high = mid; } // If key is greater than array[mid], // then find in right subarray else { low = mid + 1; } } // If key is greater than last element which is // array[n-1] then lower bound // does not exists in the array if (low < end && array[(int)low] < key) { low++; } // Returning the lower_bound index return (int)low; } static int upper_bound(long array[], long key,long start,long end) { // Initialize starting index and // ending index long low = start, high = end; long mid; // Till high does not crosses low while (low < high) { // Find the index of the middle element mid = low + (high - low) / 2; // If key is less than or equal // to array[mid], then find in // left subarray if (key >= array[(int)mid]) { low = mid+1; } // If key is greater than array[mid], // then find in right subarray else { high = mid; } } // If key is greater than last element which is // array[n-1] then lower bound // does not exists in the array if (low < end && array[(int)low] <= key) { low++; } // Returning the lower_bound index return (int)low; } public static long search1(long x,long k) { long start = 1,end = k; long mid = start + (end-start)/2; while(start < end) { mid = start + (end-start)/2; if(x>mid*(mid+1)/2) { start = mid+1; }else { end = mid; } } if(start<end && mid*(mid+1)/2 <= x) { start++; } return start; } public static long search2(long x,long k) { long start = 1,end = k; long mid = start + (end-start)/2; while(start < end) { mid = start + (end-start)/2; if(x>mid*(2*k-mid-1)/2) { start = mid+1; }else { end = mid; } } if(start<end && mid*(2*k-mid-1)/2 <= x) { start++; } return start; } public static int countGreater(int arr[], int k,int l, int r) { int leftGreater = r+1; int R = r; while (l<=r) { int m = l + (r - l) / 2; if (arr[m] >= k) { leftGreater = m; r = m - 1; } else { l = m + 1; } } return (R-leftGreater+1); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } 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(); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
32a0d89f812c844a619410f04545cbf0
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
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.*; import java.util.stream.Collectors; public class coding { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st == null || !st.hasMoreElements()) { try { String str = br.readLine(); if(str == null) throw new InputMismatchException(); st = new StringTokenizer(str); } 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(); } if(str == null) throw new InputMismatchException(); return str; } } public static <K, V> K getKey(Map<K, V> map, V value) { for (Map.Entry<K, V> entry: map.entrySet()) { if (value.equals(entry.getValue())) { return entry.getKey(); } } return null; } public static void main(String args[] ) throws Exception { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int arr[]=new int[n]; int brr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } long sum=0,sum1=0; for(int i=0;i<n;i++) { while(arr[i]%2==0) { sum++; arr[i]=arr[i]/2; } } Arrays.sort(arr); for(int i=0;i<n-1;i++) { sum1=sum1+arr[i]; } sum1=sum1 + arr[n-1]*(long)Math.pow(2, sum); System.out.println(sum1); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
55c50f4e5b9477127e9b1496c87f0246
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.io.*; public class CP { static final int MOD = (int) (1e9) + 7;// ((a + b) % MOD + MOD) % MOD public static double roundOff(double num, int precision) { double round = Math.round(num * Math.pow(10, precision)); return round / (Math.pow(10, precision)); } public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); int t = sc.Int(); for (; t > 0; t--) { int n = sc.Int(); long arr[] = new long[n]; long c = 1; for (int i = 0; i < n; i++) { arr[i] = sc.Long(); for (; arr[i] % 2 == 0;) { c *= 2; arr[i] /= 2; } } Arrays.sort(arr); arr[n - 1] *= c; long ans = 0; for (long v : arr) { ans += v; } System.out.println(ans); } sc.close(); } } class FastReader { private BufferedReader br; private StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { for (; st == null || !st.hasMoreTokens();) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int Int() throws IOException { return Integer.parseInt(next()); } public long Long() throws IOException { return Long.parseLong(next()); } public double Double() throws IOException { return Double.parseDouble(next()); } public float Float() throws IOException { return Float.parseFloat(next()); } public char Char() throws IOException { return next().charAt(0); } public void close() throws IOException { br.close(); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
2c5b7df08a786250c7c3389c81da56da
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int testCases = scan.nextInt(); while (testCases-- != 0) { int n = scan.nextInt(); if (n == 1) { System.out.println(scan.nextInt()); } else { int largestOdd = 0; int sum = 0; int power = 0; while (n-- != 0) { int num = scan.nextInt(); while (num % 2 == 0) { power++; num /= 2; } if (num > largestOdd) { sum += largestOdd; largestOdd = num; } else { sum += num; } } System.out.println(sum + largestOdd * (long) Math.pow(2, power)); } } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
834bf2e879ff9f330078fd8931ba636d
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int testCases = scan.nextInt(); while (testCases-- != 0) { int n = scan.nextInt(); if (n == 1) { System.out.println(scan.nextInt()); } else { int largestOdd = 0; int sum = 0; int power = 0; while (n-- != 0) { int num = scan.nextInt(); while (num % 2 == 0) { power++; num /= 2; } if (num > largestOdd) { sum += largestOdd; largestOdd = num; } else { sum += num; } } System.out.println(sum + largestOdd * (long) Math.pow(2, power)); } } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
84cc1cfdfe8b09d9019c76ba37a688b3
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.io.*; // problem A public class divideAndMultiply { public static void main(String[] args) { int t = r.nextInt(); for (int i = 0; i < t; i++) { new Solve(); } pw.close(); } static class Solve { Solve() { int n = r.nextInt(); int[] a = new int[n]; int ans = 0, x = 0, mx = 0; for (int i = 0; i < n; i++) { a[i] = r.nextInt(); while (a[i] % 2 == 0) { x++; a[i] /= 2; } mx = Math.max(a[i], mx); ans += a[i]; } ans -= mx; pw.println(ans + mx * (long) Math.pow(2, x)); } public static int[] copy(int[] arr) { int[] arr2 = new int[arr.length]; for (int i = 0; i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } static InputReader r = new InputReader(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
45a0e3bd16218875b809afd923430fbc
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import javax.sql.rowset.BaseRowSet; import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codeforces { static int seive[]=new int[1000+1]; static void Seive() { for(int j=1;j<seive.length;j++) seive[j]=j; int color=1; for(int j=2;j<seive.length;j=j+2) seive[j]=1; color=2; for(int j=3;j<seive.length;j++) { if(seive[j]==j) { for(int z=2*j;z<seive.length;z+=j) { if(seive[z]==z) seive[z]=color; } color++; } } } static long gcd(long a,long b) { if(a==0) return b; return gcd(b%a,a); } static long sum1(long b[],int n,int arr[]) { long sum=0; for(int j=0;j<n;j++) sum+=(Math.abs(arr[j]-b[j])); sum=2*sum; return sum; } static long sum(long even[],long odd[],int n) { long s=0; for(int j=0;j<even.length;j++) s+=even[j]; for(int j=0;j<odd.length;j++) s+=odd[j]; return s; } public static void main(String[] args) throws java.lang.Exception { /* your code goes here */ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(buf.readLine()); StringBuilder sb = new StringBuilder(); for (int i = 0; i < t; i++) { int n=Integer.parseInt(buf.readLine()); long arr[]=new long[n]; String st2[]=(buf.readLine()).split(" "); long m=1; for(int j=0;j<n;j++) { arr[j] = Long.parseLong(st2[j]); while(arr[j]%2==0) { arr[j]/=2; m*=2; } } Arrays.sort(arr); long sum=0; for(int j=0;j<n-1;j++) sum+=arr[j]; arr[n-1]=m*arr[n-1]; sum+=arr[n-1]; sb.append(sum+"\n"); } System.out.println(sb); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
e4f9d49753e64df10664de141a1a05c2
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.lang.*; public class a { // static int mod = 998244353; static int mod = 1000000007; public static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws Exception { Reader scn = new Reader(); PrintWriter pw = new PrintWriter(System.out); int t = scn.nextInt(); outer : while(t-->0){ int n = scn.nextInt(); long[] arr = new long[n]; for(int i=0; i<n; i++){ arr[i] = scn.nextLong(); } long pow = 1L; for(int i=0; i<n; i++){ while(arr[i] % 2 == 0){ arr[i] /= 2; pow *= 2L; } } sort(arr); long ans = 0L; for(int i=0; i<n-1; i++){ ans += arr[i]; } ans += (arr[n-1] * pow); pw.println(ans); } pw.close(); } public static class Pair{ int x; int y; Pair(int x, int y){ this.x = x; this.y = y; } } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } private static void sort(long[] arr) { List<Long> list = new ArrayList<>(); for(int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } // private static void sort(Pair[] arr) { // List<Pair> list = new ArrayList<>(); // for(int i=0; i<arr.length; i++){ // list.add(arr[i]); // } // Collections.sort(list); // collections.sort uses nlogn in backend // for (int i = 0; i < arr.length; i++){ // arr[i] = list.get(i); // } // } private static void reverseSort(int[] arr){ List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list, Collections.reverseOrder()); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } private static void reverseSort(long[] arr){ List<Long> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list, Collections.reverseOrder()); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } private static void reverseSort(ArrayList<Integer> list){ Collections.sort(list, Collections.reverseOrder()); } private static int lowerBound(int[] arr, int x){ int n = arr.length, si = 0, ei = n - 1; while(si <= ei){ int mid = si + (ei - si)/2; if(arr[mid] == x){ if(mid-1 >= 0 && arr[mid-1] == arr[mid]){ ei = mid-1; }else{ return mid; } }else if(arr[mid] > x){ ei = mid - 1; }else{ si = mid+1; } } return si; } private static int upperBound(int[] arr, int x){ int n = arr.length, si = 0, ei = n - 1; while(si <= ei){ int mid = si + (ei - si)/2; if(arr[mid] == x){ if(mid+1 < n && arr[mid+1] == arr[mid]){ si = mid+1; }else{ return mid + 1; } }else if(arr[mid] > x){ ei = mid - 1; }else{ si = mid+1; } } return si; } private static int upperBound(ArrayList<Integer> list, int x){ int n = list.size(), si = 0, ei = n - 1; while(si <= ei){ int mid = si + (ei - si)/2; if(list.get(mid) == x){ if(mid+1 < n && list.get(mid+1) == list.get(mid)){ si = mid+1; }else{ return mid + 1; } }else if(list.get(mid) > x){ ei = mid - 1; }else{ si = mid+1; } } return si; } // (x^y)%p in O(logy) private static long power(long x, long y){ long res = 1; x = x % mod; while(y > 0){ if ((y & 1) == 1){ res = (res * x) % mod; } y = y >> 1; x = (x * x) % mod; } return res; } public static boolean nextPermutation(int[] arr) { if(arr == null || arr.length <= 1){ return false; } int last = arr.length-2; while(last >= 0){ if(arr[last] < arr[last+1]){ break; } last--; } if (last < 0){ return false; } if(last >= 0){ int nextGreater = arr.length-1; for(int i=arr.length-1; i>last; i--){ if(arr[i] > arr[last]){ nextGreater = i; break; } } swap(arr, last, nextGreater); } reverse(arr, last+1, arr.length-1); return true; } private static void swap(int[] arr, int i, int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } private static void reverse(int[] arr, int i, int j){ while(i < j){ swap(arr, i++, j--); } } private static String reverseStr(String s){ StringBuilder sb = new StringBuilder(s); return sb.reverse().toString(); } // TC- O(logmax(a,b)) private static int gcd(int a, int b) { if(a == 0){ return b; } return gcd(b%a, a); } private static long gcd(long a, long b) { if(a == 0L){ return b; } return gcd(b%a, a); } private static long lcm(long a, long b) { return a / gcd(a, b) * b; } // TC- O(logmax(a,b)) private static int lcm(int a, int b) { return a / gcd(a, b) * b; } private static long inv(long x){ return power(x, mod - 2); } private static long summation(long n){ return (n * (n + 1L)) >> 1; } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
7cde6b23266b01e907bc9dba9c668c47
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class A { static int ans=0; public static void main(String[] args) { FastScanner fs=new FastScanner(); int t=fs.nextInt(); while(t-->0) { int n,nE=0; n=fs.nextInt(); int[] arr=fs.readArray(n); long sum=0L,ans=Long.MIN_VALUE; for(int i=0;i<n;i++) { while(arr[i]%2==0) { nE++; arr[i]=arr[i]/2; } } sort(arr); ans=arr[n-1]*(long)Math.pow(2,nE); for(int i=0;i<n-1;i++) sum+=arr[i]; ans+=sum; System.out.println(ans); } } 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); } void shuffleArray(long[] arr){ int n = arr.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = arr[i]; int randomPos = i + rnd.nextInt(n-i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } 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()); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
f7cc99aed6454601895b9f0ec4a54689
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; public class Solution{ public static void main(String[] args) { Scanner sc= new Scanner(System.in); int T=sc.nextInt(); for(int t=0;t<T;t++) { int n=sc.nextInt(); int[] arr= new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); int[] pow= new int[n]; int[] rem= new int[n]; int powSum=0; int remSum=0; for(int i=0;i<n;i++) { int[] temp= getPowAndRem(arr[i]); //System.out.println(temp[0]+", "+temp[1]); pow[i]=temp[0]; powSum+=pow[i]; rem[i]=temp[1]; remSum+=rem[i]; } long maxm=0; for(int i=0;i<n;i++) { long cur=(long)arr[i]*(long)(Math.pow(2,powSum-pow[i]))+(long)(remSum-rem[i]); maxm=Math.max(maxm,cur); } System.out.println(maxm); } } public static int[] getPowAndRem(int num) { int[] ans= new int[2]; int n=1; int count=0; while(num>1&&num%2==0) { num/=2; count++; } ans[0]=count; ans[1]=num; return ans; } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
b43afdd7ba7f5d2805f0099cf99f4252
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); // BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=1; t=sc.nextInt(); //int t=Integer.parseInt(br.readLine()); while(--t>=0){ int n=sc.nextInt(); long a[]=new long[n]; for(int i=0;i<n;i++)a[i]=sc.nextLong(); Arrays.sort(a); long sum=0; int x=-1; for(int i=0;i<n-1;i++){ while(a[i]%2==0){ a[i]=a[i]/2l; a[n-1]=a[n-1]*2l; } sum=sum+a[i]; } sum=sum+a[n-1]; long max=0; for(int i=0;i<n-1;i++){ if(a[i]%2!=0&&a[i]>=max){ x=i; max=a[i]; } } // long sum1=0; if(x>=0&&x<(n-1)){ while(a[n-1]%2==0){ a[x]=a[x]*2l; a[n-1]=a[n-1]/2l; } } long sum1=0; for(int i=0;i<n;i++){ sum1=sum1+a[i]; } System.out.println(Math.max(sum1,sum)); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
719cb9b901f70d6af816396428d7d6d2
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class SolutionA extends Thread { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static final FastReader scanner = new FastReader(); private static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { new Thread(null, new SolutionA(), "Main", 1 << 28).start(); } public void run() { int t = scanner.nextInt(); for (int i = 0; i < t; i++) { solve(); } out.close(); } private static void solve() { int n = scanner.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = scanner.nextInt(); } int amountMult = 0; for (int i = 0; i < n; i++) { while (a[i] % 2 == 0) { a[i] /= 2; amountMult++; } } Arrays.sort(a); for (int i = 0; i < amountMult; i++) { a[n-1] *= 2; } long sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; } out.println(sum); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
36ef818ad8a88021e434893d9de90481
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
// Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.*; public class HelloWorld { static Scanner scan = new Scanner(System.in); public static void main(String[] args) { int t = scan.nextInt(); for(int i =0;i<t;i++){ solve(); } } public static void solve(){ int n = scan.nextInt(); int[] arr = new int[n]; Set<Integer> set = new HashSet<>(); int max =Integer.MIN_VALUE; for(int i =0;i<n;i++){ arr[i]=scan.nextInt(); max = Math.max(max,arr[i]); set.add(arr[i]); } if(n==1){ System.out.println(arr[0]); } else{ int m =0; long s=0; for(int i =0;i<n;i++){ while(arr[i] % 2 ==0){ arr[i] = arr[i]/2; m++; } } Arrays.sort(arr); for(int i =0;i<n-1;i++){ s+=arr[i]; } s = s+ (long)(Math.pow(2,m)) * arr[n-1]; System.out.println(s); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
26659ca8034f454238e74bf6860c3dd0
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import javax.sql.rowset.spi.SyncResolver; import java.io.ObjectInputStream.GetField; import java.lang.*; /* Name of the class has to be "Main" only if the class is public. */ public class codeforces { public static int pow(int n) { int out=0; while(n%2==0){ n/=2; out++; } return out; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int[] arr=new int[n]; int oddmax=-1; int evenmax=-2; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); if(arr[i]%2==0){ if(evenmax<arr[i]){ evenmax=arr[i]; } }else{ if(oddmax<arr[i]){ oddmax=arr[i]; } } } int sum=0; int outsum=0; int[] lowestform=new int[n]; for(int i=0;i<n;i++){ int power=pow(arr[i]); //System.out.println("power "+power); sum+=power; lowestform[i]=arr[i]/(int)Math.pow(2, power); outsum+=lowestform[i]; } Arrays.sort(lowestform); long out=lowestform[n-1]*(long)Math.pow(2, sum)+(outsum-lowestform[n-1]); System.out.println(out); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
2a35ba6c3be4846e40728dac6ab7491e
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; public class solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } int i=0; long count=0;long res=0; for(i=0;i<n;i++) { while(arr[i]%2==0) {count++; arr[i]=arr[i]/2; } } Arrays.sort(arr); for(int k=0;k<n-1;k++) { res=res+arr[k]; } res+= arr[n-1]*(long)Math.pow(2,count) ; System.out.println(res); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
957fad7b0561a4f65fae64241e1fae18
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
// ceil using integer division: ceil(x/y) = (x+y-1)/y import java.util.*; import java.lang.*; import java.io.*; public class Solution { public static void main(String[] args) throws IOException { Reader.init(System.in); int t = Reader.nextInt(); while(t-->0){ int n = Reader.nextInt(); long[] arr = new long[n]; long count = 1; for(int i = 0;i<n;i++){ arr[i] = Reader.nextLong(); while(arr[i]%2==0){ count*=2; arr[i]/=2; } } Arrays.parallelSort(arr); long sum = 0; for(int i = 0;i<n-1;i++){ sum += arr[i]; } sum += arr[n-1]*count; System.out.println(sum); } } public static int powOf2JustSmallerThanN(int n) { n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return n ^ (n >> 1); } public static int mergeSortAndCount(int[] arr, int l, int r) { int count = 0; if (l < r) { int m = (l + r) / 2; count += mergeSortAndCount(arr, l, m); count += mergeSortAndCount(arr, m + 1, r); count += mergeAndCount(arr, l, m, r); } return count; } public static int mergeAndCount(int[] arr, int l, int m, int r) { int[] left = Arrays.copyOfRange(arr, l, m + 1); int[] right = Arrays.copyOfRange(arr, m + 1, r + 1); int i = 0, j = 0, k = l, swaps = 0; while (i < left.length && j < right.length) { if (left[i] <= right[j]) arr[k++] = left[i++]; else { arr[k++] = right[j++]; swaps += (m + 1) - (l + i); } } while (i < left.length) arr[k++] = left[i++]; while (j < right.length) arr[k++] = right[j++]; return swaps; } public static void reverseArray(int[] arr,int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } public static long gcd(long a, long b){ if(a==0){ return b; } return gcd(b%a,a); } public static long lcm(long a, long b){ if(a>b) return a/gcd(b,a) * b; return b/gcd(a,b) * a; } public static long largeExponentMod(long x,long y,long mod){ // computing (x^y) % mod x%=mod; long ans = 1; while(y>0){ if((y&1)==1){ ans = (ans*x)%mod; } x = (x*x)%mod; y = y >> 1; } return ans; } public static boolean[] numOfPrimesInRange(long L, long R){ boolean[] isPrime = new boolean[(int) (R-L+1)]; Arrays.fill(isPrime,true); long lim = (long) Math.sqrt(R); for (long i = 2; i <= lim; i++){ for (long j = Math.max(i * i, (L + i - 1) / i * i); j <= R; j += i){ isPrime[(int) (j - L)] = false; } } if (L == 1) isPrime[0] = false; return isPrime; } public static ArrayList<Long> primeFactors(long n){ ArrayList<Long> factorization = new ArrayList<>(); while(n%2==0){ factorization.add(2L); n/=2; } while(n%3==0){ factorization.add(3L); n/=3; } while(n%5==0){ factorization.add(5L); n/=5; } int[] increments = {4, 2, 4, 2, 4, 6, 2, 6}; int i = 0; for (long d = 7; d * d <= n; d += increments[i++]) { while (n % d == 0) { factorization.add(d); n /= d; } if (i == 8) i = 0; } if (n > 1) factorization.add(n); return factorization; } } class DSU { int[] size, parent; int n; public DSU(int n){ this.n = n; size = new int[n]; parent = new int[n]; for(int i = 0;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int u){ if(parent[u]==u){ return u; } return parent[u] = find(parent[u]); } public void merge(int u, int v){ u = find(u); v = find(v); if(u!=v){ if(size[u]>size[v]){ parent[v] = u; size[u] += size[v]; } else{ parent[u] = v; size[v] += size[u]; } } } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static String nextLine() throws IOException { return reader.readLine(); } 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() ); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
667abab61814d0ed69697de722d00996
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0){ int n = sc.nextInt(); long[] arr = new long[n]; int count = 0; for(int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } for(int i = 0; i < n; i++){ while(arr[i] % 2 == 0){ arr[i] /= 2; count++; } } Arrays.sort(arr); long ans = 0; for(int i = 0; i < n - 1; i++){ ans += arr[i]; } ans += arr[n - 1] * Math.pow(2, count); System.out.println(ans); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
ac21e2c71c44ba1908ed5dc8bf50c41d
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-->0){ int n = scn.nextInt(); long[] arr = new long[n]; for(int i = 0; i<n; i++){ arr[i] = scn.nextLong(); } long temp = 1; for (int i = 0; i < n; i++) { while(arr[i] %2 == 0){ temp*= 2; arr[i] /= 2; } } Arrays.sort(arr); arr[n - 1] *= temp; long ans = 0; for (long i : arr) { ans = ans + i; } System.out.println(ans); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
818b4df2254100eaf1506374accd0874
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; public class DivideAndMultiply{ public static void main(String args[])throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int testCases = Integer.parseInt(br.readLine()); StringBuilder result = new StringBuilder(); StringTokenizer str; while(testCases-- > 0){ int n = Integer.parseInt(br.readLine()); str = new StringTokenizer(br.readLine()); int nums[] = new int[n]; long count = 0; for(int i = 0; i < n; i++){ nums[i] = Integer.parseInt(str.nextToken()); while(nums[i] % 2 == 0){ nums[i] /= 2; count++; } } Arrays.sort(nums); long sum = 0; for(int i = 0; i < n - 1; i++){ sum += nums[i]; } sum += (nums[n - 1] * Math.pow(2, count)); result.append(sum + "\n"); } System.out.println(result); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
a613d20608e55ef13012512aa96be9d5
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.Scanner; public class Trial2 { public static boolean check(long a) { if((a==2)||(a==4)||(a==8)||(a==16)){ return false; } return true; } public static int find(long a){ int d = 1; while(a%2 == 0){ d = d*2; a = a/2; } return d; } public static long sea(long a){ while (a%2 == 0) { a = a/2; } return a; } public static void main(String[] args) { // B ka 66 // W ka 87 // G ka 71 Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int z = 0; z < t; z++) { int n = sc.nextInt(); long[] arr = new long[n]; arr[0] = sc.nextInt(); long max_n = arr[0]; int max_ind = 0; for(int i =1; i<n; i++){ arr[i] = sc.nextInt(); if(check(arr[i])){ if((check(max_n))&&(sea(arr[i])>sea(max_n))){ max_n = arr[i]; max_ind = i; } else if(!(check(max_n))){ max_n = arr[i]; max_ind = i; } } } long sum = 0; for(int i =0; i<n; i++){ if(i != max_ind){ int m = find(arr[i]); arr[i] = arr[i]/m; arr[max_ind] = arr[max_ind]*m; sum = sum + arr[i]; } } sum = sum + arr[max_ind]; System.out.println(sum); } sc.close(); } } // String fin = ""; // for (int i = 0; i < size; i++) { // char c = (char) arr[i]; // fin = fin + c; // } // System.out.println(fin); // 1,4,9,16,25,36,49,64,81 // 8,27,
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
e63e89908be2b1eff5028735b148371d
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
//package com.codeforces.Practise; import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class DivideAndMultiply { //Don't Confuse //Experience is the name of the game // You won't fail until you stop trying....... // you can solve one problem by many approaches. when you stuck you are going to learn something new okk // Everything is easy. you feel its hard because of you don't know, and you not understand it very well. //// How to Improve Your problem-solving skill ??( By practise ). ***simple /// ==>> Solve problems slightly above of your level (to know some logic and how to approach cp problems) // Otherwise You will stay as it is okk. Learn from other code as well. ///////////////////////////////////////////////////////////////////////// /// How to Solve Problem in CP ? (you need to come up with brainstorm) ( if you feel problem is hard then your approach is wrong ) // ==>> Step01 :- Understanding problem statement clearly. Then Only Move Forward (Because Everything is mentioned in problem statement). // Step02 :- Think a lot about Solution. if you are confident that your solution might be correct Then only Move Forward // Step03 :- First think of brute force then move to optimal approach // Step04 :- Finally Code ( there is no any sense to code. if you not follow about steps okk) /////////////////////////////////////////////////////////////////////////// public static void main(String[] args)throws IOException { Reader scan=new Reader(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t=scan.nextInt(); while (t-->0) { int n=scan.nextInt(); long[] arr=new long[n]; for (int i = 0; i < n; i++) { arr[i]=scan.nextLong(); } long k=0; for (int i = 0; i < n; i++) { while (arr[i]%2==0){ arr[i]=arr[i]/2; k++; } } Arrays.sort(arr); long ans= (long) Math.pow(2,k); arr[n-1]=ans*arr[n-1]; long sum=0; for (int i = 0; i < n; i++) { sum+=arr[i]; } bw.write(sum+""); bw.newLine(); bw.flush(); } } //FAST READER static class Reader { public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public Reader() { in = new BufferedInputStream(System.in, BS); } public Reader(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+(cur < 0 ? -1*nextLong()/num : nextLong()/num); } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
c4c86b4af1e2798599370e677a695a25
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; public class solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } int i=0; long count=0;long res=0; for(i=0;i<n;i++) { while(arr[i]%2==0) {count++; arr[i]=arr[i]/2; } } Arrays.sort(arr); for(int k=0;k<n-1;k++) { res=res+arr[k]; } res+= arr[n-1]*(long)Math.pow(2,count) ; System.out.println(res); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
bf8b79f921f5b37b893dfe7f032b8376
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
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.*; import java.util.Arrays; import java.util.Collections; public class divide_and_multiply { public static void main(String args[]) throws IOException { // long start = System.currentTimeMillis(); int u = 0; BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); // System.out.println("Enter t"); int t = Integer.parseInt(br.readLine()); do { // System.out.println("Enter n"); int n = Integer.parseInt(br.readLine()); long[] input = Arrays.stream(br.readLine().split("\\s+")).mapToLong(Long::parseLong).toArray(); //sort_longarray(input); //long a = input[n-1]; int p =0; for(int i=0;i<n;i++) { long b = input[i]; while(b%2 ==0) { b = b/2; p++; } input[i] =b ; } sort_longarray(input); input[n-1] = input[n-1]*(long)Math.pow(2,p); long sum = 0; for(int i=0;i<n;i++) { sum += input[i]; } System.out.println(sum); u++; } while(u<t); // long end = System.currentTimeMillis(); // System.out.println("time taken is "+ (end - start) + "ms"); } public static void sort_longarray(long[] input) { ArrayList<Long> a = new ArrayList<Long>(); for(int i = 0;i<input.length;i++) { a.add(input[i]); } Collections.sort(a); for(int i=0;i<a.size();i++) { input[i] = a.get(i); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
30454e436eafe6f714e6676347c08753
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
// Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.*; import java.io.*; public class HelloWorld { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); int k=0; for(int i=0;i<arr.length;i++){ long val=arr[i]; if(val%2==0){ while(val%2==0){ val=val/2; arr[i]=val; k++; } } } //35184372088846 Arrays.sort(arr); //System.out.print(arr[14]); while(k!=0){ arr[n-1]*=2; //System.out.print(arr[14]+" "); k--; } long sum=0; for(int i=0;i<arr.length;i++){ //System.out.print(arr[i]+ " "); sum+=arr[i]; } System.out.println(sum); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
a00c52400393f3b3c795d9795a5bedd7
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; public class Divide_and_Multiply { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int testCases = Integer.parseInt(reader.readLine()); for (int i = 0; i < testCases; i++) { int n = Integer.parseInt(reader.readLine()); String[] line = reader.readLine().split(" "); int[] array = new int[n]; int[] divide2 = new int[n]; int[] countArr = new int[n]; int total = 0; for (int j = 0; j < n; j++) { int count = 0; array[j] = Integer.parseInt(line[j]); int num = array[j]; while (num % 2 == 0) { num = num / 2; count++; } divide2[j] = num; countArr[j] = count; } int max = divide2[0]; int maxIndex = 0; for (int j = 0; j < n; j++) { if (max < divide2[j]) { max = divide2[j]; maxIndex = j; } } for (int j = 0; j < n; j++) { total += countArr[j]; } long bigNum = max * (long) Math.pow(2, total); for (int j = 0; j < n; j++) { if (j != maxIndex) { bigNum += divide2[j]; } } System.out.println(bigNum); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
062dc23bb87e320f68cde1036f928e5d
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.text.DecimalFormat; import java.util.*; public class Codeforces { static int mod= 1000000007; public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); int t=fs.nextInt(); outer:while(t-->0) { int n=fs.nextInt(); long arr[]=fs.lreadArray(n); if(n==1) { out.println(arr[0]); continue ; } long p=0; for(int i=0;i<n;i++) { while(arr[i]%2==0) { arr[i]/=2; p++; } } Arrays.sort(arr); long mul=1; while(p-->0) { mul*=2; } arr[n-1]*= mul; long sum=0; for(int i=0;i<n;i++) sum+=arr[i]; out.println(sum); } out.close(); } static long pow(long a,long b) { if(b<=0) return 1; long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } static long nck(int n,int k) { if(k>n) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { // return fact[(int)n]; long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); int temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } // Use this to input code since it is faster than a Scanner 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[] lreadArray(int n) { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } 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()); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
03379ea28a1191b8debdd2adc69b20c0
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.Arrays; import java.util.StringTokenizer; public class Codeforces1609A { private static InputReader in = new InputReader(System.in); private static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); BigInteger[] arr = new BigInteger[n]; for (int i = 0; i < n; i++) { arr[i] = BigInteger.valueOf(in.nextInt()); } int cnt = 0; for (int i = 0; i < n; i++) { while (arr[i].mod(BigInteger.TWO).equals(BigInteger.ZERO)) { arr[i] = arr[i].divide(BigInteger.TWO); cnt++; } } Arrays.sort(arr); for (int i = 0; i < cnt; i++) { arr[n-1] = arr[n-1].multiply(BigInteger.TWO); } BigInteger res = BigInteger.ZERO; for (int i = 0; i < n; i++) { res = res.add(arr[i]); } out.println(res); } out.flush(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
770a617a78dcf17425e74cfbd73d7bab
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class devidee { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int[] arr= new int[n]; for(int i=0; i<n; i++){ arr[i] = sc.nextInt(); } int count=0; for(int i=0; i<n; i++){ while(arr[i]%2==0&&arr[i]>1){ arr[i]/=2; count++; } } Arrays.sort(arr); long sum =0; for(int i=0; i<n-1; i++) sum+=arr[i]; sum = (long) (sum + arr[n-1]*Math.pow(2,count)); System.out.println(sum); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
503c6a26c2a3438be999098773bc6232
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class A { public static void main(String[] args) throws java.lang.Exception { try { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int arr[]=new int[n]; long p=1,sum=0; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); while(arr[i]%2==0) { p*=2; arr[i]/=2; } } Arrays.sort(arr); long q=arr[n-1]*p; for(int i=0;i<n-1;i++) { sum+=arr[i]; } sum+=q; System.out.println(sum); } } catch (Exception e) { } finally { return; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
0509155a4627cba70804837b49913f22
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static long mod = (int)1e9+7; static PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; while(t-->0) { int n=sc.nextInt(); int a[]=sc.readArray(n); long count=1; long sum=0; for(int i=0;i<n;i++) { while(a[i]%2==0) { a[i]/=2; count*=2; } } radixSort2(a); for(int i=0;i<n-1;i++) { sum+=a[i]; // out.print(a[i]+" "); } // out.println(count); sum+=a[n-1]*count; out.println(sum); } out.flush(); } static int divisible(int n) { int count=0; while(n%2==0) { count++; n/=2; } return count; } static int findfrequencies(int a[],int n) { int count=0; for(int i=0;i<a.length;i++) { if(a[i]==n) { count++; } } return count; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } static int[] EvenOddArragement(int a[]) { ArrayList<Integer> list=new ArrayList<>(); for(int i=0;i<a.length;i++) { if(a[i]%2==0) { list.add(a[i]); } } for(int i=0;i<a.length;i++) { if(a[i]%2!=0) { list.add(a[i]); } } for(int i=0;i<a.length;i++) { a[i]=list.get(i); } return a; } static int gcd(int a, int b) { while (b != 0) { int t = a; a = b; b = t % b; } return a; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static int DigitSum(int n) { int r=0,sum=0; while(n>=0) { r=n%10; sum=sum+r; n=n/10; } return sum; } static boolean checkPerfectSquare(int number) { double sqrt=Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static boolean isPowerOfTwo(int n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static boolean isPrime2(int n) { if (n <= 1) { return false; } if (n == 2) { return true; } if (n % 2 == 0) { return false; } for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) { if (n % i == 0) { return false; } } return true; } static class P implements Comparable<P> { int i, j; public P(int i, int j) { this.i=i; this.j=j; } public int compareTo(P o) { return Integer.compare(i, o.i); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
3e30611aefd0ab23bc2c00e6ca9971f7
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
//package com.company; import java.io.*; import java.util.*; public class Main { static long mod =(long)(1e9+7); public static void main(String[] args) throws IOException { FastScanner reader = new FastScanner(); int t = reader.nextInt(); while(t-- > 0){ int n = reader.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i] = reader.nextInt(); long sum = 0; long evenMul = 1; for(int i=0;i<n;i++) { while(arr[i] % 2 == 0){ evenMul *= 2; arr[i] /=2; } } Arrays.sort(arr); for(int i=0;i<n-1;i++) sum += arr[i]; sum += arr[n-1] * evenMul; System.out.println(sum); } } static class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} } static class UnionFind { private int[] par; private int[] rank; public UnionFind(int n) { par = new int[n]; rank = new int[n]; for (int i = 0; i < n; i++) { par[i] = i; rank[i] = 0; } } public int par(int x) { return par[x]; } public int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } public boolean isSame(int x, int y) { return find(x)==find(y); } public boolean union(int x, int y) { x = find(x); y = find(y); if (x == y) { return false; } if (rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) { rank[x]++; } } return true; } } static long getSum(int x2, int y2, int x1, int y1,long[][] A){ return A[x2][y2] - A[x1][y2] - A[x2][y1] + A[x1][y1]; } // cumulative sum // for(int i=1; i<=n; i++){ // for(int j=1; j<=m; j++){ // sum[i][j] = reader.nextLong(); // sum[i][j] += sum[i][j-1] + sum[i-1][j] - sum[i-1][j-1]; // } // } static long fastPowMod(long x, long n, long mod) { if(n == 0) return 1; x %= mod; long half = fastPowMod(x, n / 2, mod); long res = half * half % mod; if(n % 2 != 0) { return res * x % mod; } return res; } public static int[][] nextIntMatrix(int height, int width,FastScanner reader) throws IOException { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = reader.nextInt(); } return mat; } static class Score{ int total; int id; public Score(int id) { this.total = 0; this.id = id; } } static int dfs(int start, ArrayList<ArrayList<Integer> > adj,boolean[] vis){ int cnt = 1; vis[start] = true; for(int i=0;i<adj.get(start).size();i++){ int next = adj.get(start).get(i); if(vis[next] == false){ cnt += dfs(next,adj,vis); } } return cnt; } static void swap(int[] nums, int i ,int j){ int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } // 1 2 4 9
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
703dcee3f2cb19b0bc61e23896d06ad1
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; public class A { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); for(int t = 0; t < T; t++) { int n = Integer.parseInt(br.readLine()); long[] arr = new long[n]; String[] line = br.readLine().split(" "); int p = 0; for(int i = 0; i < n; i++) { arr[i] = Integer.parseInt(line[i]); while(arr[i]%2 == 0) { arr[i] /= 2; ++p; } } Arrays.sort(arr); while(p > 0) { arr[n-1] *= 2; p--; } long s = 0; for(int i = 0; i < n; i++) s += arr[i]; System.out.println(s); } br.close(); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
f03f18f2dae554abd322affb8ceb5abe
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.Locale; import java.util.StringTokenizer; import java.util.stream.IntStream; public class E { private void solve() { int t = readInt(); for(int q = 0; q < t; q++) { int n = readInt(); long[] arr = new long[n]; long count = 0; for(int i = 0; i < n; i++) { arr[i] = readLong(); } for(int i = 0; i < n; i++) { while(arr[i] % 2 == 0 && arr[i] != 0) { count++; arr[i] = arr[i]/2; } } int max = 0; for(int i = 0; i < n; i++) { if(arr[i] > arr[max]) max = i; } arr[max] = arr[max] * binpow(2, count); long sum = 0; for (int i = 0; i < n; i++) { sum+=arr[i]; } out.println(sum); } } ////////////////////////////////////////////////////////////////// long sqrtLong(long x) { long root = (long) Math.sqrt(x); while (root * root > x) --root; while ((root + 1) * (root + 1) <= x) ++root; return root; } int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } int lcm(int a, int b) { return a / gcd(a, b) * b; } int phi(int n) { int result = n; for (int i = 2; i * i <= n; ++i) if (n % i == 0) { while (n % i == 0) n /= i; result -= result / i; } if (n > 1) result -= result / n; return result; } int factmod(int n, int p) { int res = 1; while (n > 1) { res = (res * ((n / p) % 2 == 1 ? p - 1 : 1)) % p; for (int i = 2; i <= n % p; ++i) res = (res * i) % p; n /= p; } return res % p; } long binpow(long a, long n) { long res = 1; while (n != 0) { if ((n & 1) != 0) res *= a; a *= a; n >>= 1; } return res; } String solPyatnashki() { int a[] = new int[16]; for (int i = 0; i < 16; ++i) a[i] = readInt(); int inv = 0; for (int i = 0; i < 16; ++i) if (a[i] != 0) for (int j = 0; j < i; ++j) if (a[j] > a[i]) ++inv; for (int i = 0; i < 16; ++i) if (a[i] == 0) inv += 1 + i / 4; return ((inv & 1) != 0 ? "No Solution" : "Solution Exists"); } private static int binarySearch(int[] sortedArray, int valueToFind, int low, int high) { int index = -1; while (low <= high) { int mid = (low + high) / 2; if (sortedArray[mid] < valueToFind) { low = mid + 1; } else if (sortedArray[mid] > valueToFind) { high = mid - 1; } else if (sortedArray[mid] == valueToFind) { index = mid; break; } } return index; } boolean isPrime(int number) { return IntStream.rangeClosed(2, (int)sqrtLong(number) + 1).noneMatch(i -> number % i == 0) || number == 2; } ////////////////////////////////////////////////////////////////// private boolean yesNo(boolean yes) { return yesNo(yes, "YES", "NO"); } private boolean yesNo(boolean yes, String yesString, String noString) { out.println(yes ? yesString : noString); return yes; } ////////////////////////////////////////////////////////////////// private long readLong() { return Long.parseLong(readString()); } private int[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; ++i) a[i] = readInt(); return a; } private int readInt() { return Integer.parseInt(readString()); } private String readString() { while (!tok.hasMoreTokens()) { String nextLine = readLine(); if (null == nextLine) return null; tok = new StringTokenizer(nextLine); } return tok.nextToken(); } private String readLine() { try { return in.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } ////////////////////////////////////////////////////////////////// private BufferedReader in; private StringTokenizer tok; private PrintWriter out; private void initFileIO(String inputFileName, String outputFileName) throws FileNotFoundException { in = new BufferedReader(new FileReader(inputFileName)); out = new PrintWriter(outputFileName); } private void initConsoleIO() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } private void initIO() throws IOException { Locale.setDefault(Locale.US); String fileName = ""; if (!fileName.isEmpty()) { initFileIO(fileName + ".in", fileName + ".out"); } else { if (new File("input.txt").exists()) { initFileIO("input.txt", "output.txt"); } else { initConsoleIO(); } } tok = new StringTokenizer(""); } ////////////////////////////////////////////////////////////////// private void run() { try { long timeStart = System.currentTimeMillis(); initIO(); solve(); out.close(); long timeEnd = System.currentTimeMillis(); System.err.println("Time(ms) = " + (timeEnd - timeStart)); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void main(String[] args) { new E().run(); } /////////////////////////////////////////////////////////////////// }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
caab1d0949d51e4d939205654f441ab8
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; import java.time.Duration; import java.time.Instant; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.StringTokenizer; import java.util.TreeSet; @SuppressWarnings("unused") public class A { static boolean DEBUG = false; public static void main(String[] args) throws IOException { Instant start = Instant.now(); if (args.length == 2) { System.setIn(new FileInputStream(new File("D:\\program\\javaCPEclipse\\CodeForces\\src\\input.txt"))); // System.setOut(new PrintStream(new File("output.txt"))); System.setErr(new PrintStream(new File("D:\\program\\javaCPEclipse\\CodeForces\\src\\error.txt"))); DEBUG = true; } Reader fs = new Reader(); PrintWriter pw = new PrintWriter(System.out); int t = fs.nextInt(); while (t-- > 0) { int n = fs.nextInt(); int a[] = fs.readArray(n); Arrays.sort(a); int b[] = Arrays.copyOfRange(a, 0, n); int maxx = Integer.MIN_VALUE; int cnt = 0; for (int i = 0; i < n; i++) { while (a[i] != 0) { if (a[i] % 2 == 0) { cnt++; a[i] >>= 1; continue; } break; } } long ans = 0; Arrays.sort(a); for(int i = 0 ; i < n - 1 ; i++) { ans += a[i]; } ans += a[n-1]*Math.pow(2, cnt); pw.println(ans); } Instant end = Instant.now(); if (DEBUG) { pw.println(Duration.between(start, end)); } pw.close(); } public static void print(long a, long b, long c, PrintWriter pw) { pw.println(a + " " + b + " " + c); return; } static class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[][] read2Array(int n, int m) { int a[][] = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextInt(); } } return a; } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
fb3fa4bf52038efc1689c7299da7b304
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.lang.Math.ceil; import static java.util.Arrays.sort; public class C { public static void main(String[] args) throws IOException { int t = ri(); while (t-- > 0) { int n = ri(); long a[] = rla(n); long ans = 0; for (int j = 0; j < n; j++) { long sum = 0; long cnt = 0; for (int i = 0 ; i < n ; i++){ if (i == j) continue; long temp = a[i]; while (temp%2 == 0){ cnt++; temp/=2; } sum+=temp; } sum+= (long) Math.pow(2,cnt)*a[j]; ans =Math.max(ans,sum); } prln(ans); } close(); } static boolean check(int[] a, int low, int high, int val) { while (low <= high) { if (a[low] != a[high] && a[low] != val && a[high] != val) { return false; } if (a[low] == val) { low++; } if (a[high] == val) { high--; } if (a[high] == a[low]) { high--; low++; } } return true; } static BufferedReader __i = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __o = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random __r = new Random(); // references // IBIG = 1e9 + 7 // IMAX ~= 2e9 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 9223372036854775807L; // math util static int minof(int a, int b, int c) { return min(a, min(b, c)); } static int minof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static long minof(long a, long b, long c) { return min(a, min(b, c)); } static long minof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static int maxof(int a, int b, int c) { return max(a, max(b, c)); } static int maxof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static long maxof(long a, long b, long c) { return max(a, max(b, c)); } static long maxof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static int powi(int a, int b) { if (a == 0) return 0; int ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static long powl(long a, int b) { if (a == 0) return 0; long ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static int fli(double d) { return (int) d; } static int cei(double d) { return (int) ceil(d); } static long fll(double d) { return (long) d; } static long cel(double d) { return (long) ceil(d); } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static int[] exgcd(int a, int b) { if (b == 0) return new int[]{1, 0}; int[] y = exgcd(b, a % b); return new int[]{y[1], y[0] - y[1] * (a / b)}; } static long[] exgcd(long a, long b) { if (b == 0) return new long[]{1, 0}; long[] y = exgcd(b, a % b); return new long[]{y[1], y[0] - y[1] * (a / b)}; } static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static long mix(long x) { x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31); } // array util static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static void resetBoolean(boolean[] vis, int n) { for (int i = 0; i < n; i++) { vis[i] = false; } } static void setMinusOne(int[][] matrix) { int row = matrix.length; int col = matrix[0].length; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { matrix[i][j] = -1; } } } // input static void r() throws IOException { input = new StringTokenizer(rline()); } static int ri() throws IOException { return Integer.parseInt(rline().split(" ")[0]); } static long rl() throws IOException { return Long.parseLong(rline()); } static double rd() throws IOException { return Double.parseDouble(rline()); } static int[] ria(int n) throws IOException { int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a; } static void ria(int[] a) throws IOException { int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni(); } static int[] riam1(int n) throws IOException { int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a; } static void riam1(int[] a) throws IOException { int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; } static long[] rla(int n) throws IOException { long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a; } static void rla(long[] a) throws IOException { int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nl(); } static double[] rda(int n) throws IOException { double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a; } static void rda(double[] a) throws IOException { int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nd(); } static char[] rcha() throws IOException { return rline().toCharArray(); } static void rcha(char[] a) throws IOException { int n = a.length, i = 0; for (char c : rline().toCharArray()) a[i++] = c; } static String rline() throws IOException { return __i.readLine(); } static String n() { return input.nextToken(); } static int rni() throws IOException { r(); return ni(); } static int ni() { return Integer.parseInt(n()); } static long rnl() throws IOException { r(); return nl(); } static long nl() { return Long.parseLong(n()); } static double rnd() throws IOException { r(); return nd(); } static double nd() { return Double.parseDouble(n()); } // output static void pr(int i) { __o.print(i); } static void prln(int i) { __o.println(i); } static void pr(long l) { __o.print(l); } static void prln(long l) { __o.println(l); } static void pr(double d) { __o.print(d); } static void prln(double d) { __o.println(d); } static void pr(char c) { __o.print(c); } static void prln(char c) { __o.println(c); } static void pr(char[] s) { __o.print(new String(s)); } static void prln(char[] s) { __o.println(new String(s)); } static void pr(String s) { __o.print(s); } static void prln(String s) { __o.println(s); } static void pr(Object o) { __o.print(o); } static void prln(Object o) { __o.println(o); } static void prln() { __o.println(); } static void pryes() { prln("yes"); } static void pry() { prln("Yes"); } static void prY() { prln("YES"); } static void prno() { prln("no"); } static void prn() { prln("No"); } static void prN() { prln("NO"); } static boolean pryesno(boolean b) { prln(b ? "yes" : "no"); return b; } ; static boolean pryn(boolean b) { prln(b ? "Yes" : "No"); return b; } static boolean prYN(boolean b) { prln(b ? "YES" : "NO"); return b; } static void prln(int... a) { for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i) ; if (a.length > 0) prln(a[a.length - 1]); else prln(); } static void prln(long... a) { for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i) ; if (a.length > 0) prln(a[a.length - 1]); else prln(); } static void prln(double... a) { for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i) ; if (a.length > 0) prln(a[a.length - 1]); else prln(); } static <T> void prln(Collection<T> c) { int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i) ; if (n >= 0) prln(iter.next()); else prln(); } static void h() { prln("hlfd"); flush(); } static void flush() { __o.flush(); } static void close() { __o.close(); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
607321ec10a5953c391a8f49acb4f26a
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.io.*; public class A { public void prayGod() throws IOException { int t = nextInt(); while (t-- > 0) { int n = nextInt(); long[] a = nextLongArray(n); sort(a); long ret = 0; for (int i = 0; i < n; i++) { long[] curr = new long[n]; for (int j = 0; j < n; j++) { curr[j] = a[j]; } long ans = 0; for (int j = 0; j < n; j++) { if (j == i) continue; while (curr[j] % 2 == 0) { curr[j] /= 2; curr[i] *= 2; } } for (int j = 0; j < n; j++) { ans += curr[j]; } ret = Math.max(ret, ans); } out.println(ret); } } public long gcd(long x, long y) { if (y == 0) return x; return gcd(y, x % y); } public void printVerdict(boolean verdict) { if (verdict) out.println(VERDICT_YES); else out.println(VERDICT_NO); } static final String VERDICT_YES = "Yes"; static final String VERDICT_NO = "No"; static final boolean RUN_TIMING = true; static final boolean AUTOFLUSH = false; static final boolean FILE_INPUT = false; static final boolean FILE_OUTPUT = false; static int iinf = 0x3f3f3f3f; static long inf = (long) 1e18 + 10; static long mod = (long) 1e9 + 7; static char[] inputBuffer = new char[1 << 20]; static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), AUTOFLUSH); // int data-type public int nextInt() throws IOException { return Integer.parseInt(next()); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } public void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } // long data-type public long nextLong() throws IOException { return Long.parseLong(next()); } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } public static void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } public void sort(long[] a) { shuffle(a); Arrays.sort(a); } // double data-type public double nextDouble() throws IOException { return Double.parseDouble(next()); } public double[] nextDoubleArray(int n) throws IOException { double[] arr = new double[n]; for (int i = 0; i < n; i++) arr[i] = nextDouble(); return arr; } public static void printArray(double[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } // Generic type public <T> void sort(T[] a) { shuffle(a); Arrays.sort(a); } public static <T> void printArray(T[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } public String next() throws IOException { int len = 0; int c; do { c = in.read(); } while (Character.isWhitespace(c) && c != -1); if (c == -1) { throw new NoSuchElementException("Reached EOF"); } do { inputBuffer[len] = (char) c; len++; c = in.read(); } while (!Character.isWhitespace(c) && c != -1); while (c != '\n' && Character.isWhitespace(c) && c != -1) { c = in.read(); } if (c != -1 && c != '\n') { in.unread(c); } return new String(inputBuffer, 0, len); } public String nextLine() throws IOException { int len = 0; int c; while ((c = in.read()) != '\n' && c != -1) { if (c == '\r') { continue; } inputBuffer[len] = (char) c; len++; } return new String(inputBuffer, 0, len); } public boolean hasNext() throws IOException { String line = nextLine(); if (line.isEmpty()) { return false; } in.unread('\n'); in.unread(line.toCharArray()); return true; } public void shuffle(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public void shuffle(long[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public void shuffle(Object[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public static void main(String[] args) throws IOException { if (FILE_INPUT) in = new PushbackReader(new BufferedReader(new FileReader(new File("output.txt"))), 1 << 20); if (FILE_OUTPUT) out = new PrintWriter(new FileWriter(new File("output.txt"))); long time = 0; time -= System.nanoTime(); new A().prayGod(); time += System.nanoTime(); if (RUN_TIMING) System.err.printf("%.3f ms%n", time / 1000000.0); out.flush(); in.close(); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
57d325bc5f94a2b74aca122e99523e73
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.math.*; import java.util.*; import java.io.*; public class main { static Map<String,Integer> map=new HashMap<>(); static int[] parent; static int[] size; public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter pw = new PrintWriter(System.out); // FileReader fr=new FileReader("/sagar.txt"); // InputStreamReader isr=new InputStreamReader(fr); // BufferedReader isr=new BufferedReader(fr); // FileWriter fw=new FileWriter("ProblemA1_ans.txt"); // BufferedWriter bw=new BufferedWriter(fw); final int mod=1000000007; // int test_case =Integer.parseInt(isr.read()); int test_case=in.nextInt(); // int test_case=1; StringBuilder sb=new StringBuilder(); // long mod=998244353; long[] fact=new long[100001]; fact[0]=1; fact[1]=1; fact[2]=1; for(int i=3;i<fact.length;i++) { fact[i]=((fact[i-1])%mod*(i%mod))%mod; //System.out.println(fact[i]); } for(int t1=0;t1<test_case;t1++) { int n=in.nextInt(); long[] a=in.nextlongArray(n); long[] b=a.clone(); long[] count=new long[n]; Arrays.sort(a); long sum=0; long total=0; for(int i=0;i<n;i++) { while(a[i]%2==0) { a[i]/=2; count[i]++; total++; } sum+=a[i]; } long max=0; // System.out.println(sum+" "+total); for(int i=0;i<n;i++) { if(total!=0) { // sum-=a[i]; long temp=a[i]; for(int j=0;j<total;j++) { a[i]*=2; } // sum+=a[i]*total; max=Math.max(max,sum+a[i]-temp); // System.out.println(i+" "+max); }else{ max=Math.max(max,sum); } } System.out.println(max); // int c=0; // for(int i=0;i<n;i++) // { // if(a[i]%2==0) // { // c++; // } // } // boolean f=(c==n); // for(int i=0;i<n-1;i++) // { // while(a[i]%2==0) // { // a[i]=a[i]/2; // a[n-1]*=2; // } // } // long sum=0; // for(int i=0;i<n;i++) // { // sum+=a[i]; // } // if(!f) // { // long temp=0; // for(int i=n-1;i>=0;i--) // { // if(b[i]%2!=0) // { // temp=b[i]; // b[i]=0; // break; // } // } // for(int i=0;i<n;i++) // { // while(b[i]!=0&&b[i]%2==0) // { // b[i]=b[i]/2; // temp*=2; // } // } // long sum1=temp; // // System.out.println(sum1); // for(int i=0;i<n;i++) // { // sum1+=b[i]; // } // sum=Math.max(sum1,sum); // } } pw.flush(); pw.close(); } static void solve(int[] a,List<Integer> ls,int n,boolean[] f,int ans,int c,int r,int k) { if(k==n) { ls.add(ans); max=Math.max(ans,max); return ; } for(int i=0;i<n;i++) { if(f[i]) { continue; } f[i]=true; for(int j=0;j<n;j++) { if(f[j]) { continue; } int ans1=(GCD(a[i],a[j])*r); ans+=ans1; f[j]=true; solve(a,ls,n,f,ans,0,r+1,k+2); ans-=ans1; f[j]=false; } f[i]=false; } } static int lower(long[] ages, long target) { if (ages == null || ages.length == 0) { return 0; } int l = 0; int r = ages.length - 1; if (target <= ages[0]) { return 0; } if (target > ages[r]) { return -1; } while (l < r) { int m = l + (r - l ) / 2 ; if (ages[m] >= target) { r = m; }else { l = m + 1; } } return r; } static int upper(long[] ages, long target) { if (ages == null || ages.length == 0) { return 0; } int l = 0; int r = ages.length - 1; if (target < ages[0]) { return -1; } if (target >= ages[r]) { return r; } while (l < r - 1) { int m = l + (r - l ) / 2 ; if (ages[m] <= target) { l = m; }else { r = m - 1; } } return ages[r] <= target ? r : l; } static int LowerBound(long a[], long x) { // x is the target value or key int l=0,r=a.length; while(l<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m+1; } if(l<a.length&&a[l]<x) { l++; } return l; } static int UpperBound(long a[], long x) {// x is the key or target value int l=0,r=a.length; while(l<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m+1; else r=m; } if(l<a.length&&a[l]<=x) { l++; } return l; } static int max=0; static void ProblemA2(boolean f,int c,int min,char key,Map<Character,List<Character>> map,char target,char temp,A2 a2,Set<Character> set) { // System.out.println(temp); if(map.containsKey(key)) { set.add(key); for(Character value:map.get(key)) { c++; if(set.contains(value)) { return; } // System.out.println(value+" "+target); if(value==target) { f=f||true; min=Math.min(c,min); // System.out.println(f+" "+min); a2.f=f||a2.f; a2.min=Math.min(min,a2.min); }else if(value==temp){ f=f||false; a2.f=f||a2.f; a2.min=Math.min(min,a2.min); break; }else{ ProblemA2(f,c,min,value,map,target,temp,a2,set); // return a2; } // set.remove(value); c--; } set.remove(key); } // System.out.println(a2.f+" "+a2.min); } static class A2{ boolean f; int min=0; A2(boolean f,int min) { this.f=f; this.min=min; } } static boolean backtrack(int i,int j,int[][] a,int[][] gol,int n,Set<String> set,int parentx,int parenty) { System.out.println(i+" "+j); if(Arrays.equals(a,gol)) { return true; } boolean f=false; int[] dx={-1,0,0,1}; int[] dy={0,-1,1,0}; set.add(i+"root"+j); for(int k=0;k<dx.length;k++) { if((i+dx[k])>=0&&(i+dx[k])<n&&(j+dy[k])>=0&&(j+dy[k])<n) { if(i+dx[k]==parentx&&j+dy[k]==parenty||set.contains((i+dx[k])+"root"+(j+dy[k]))) { continue; } // System.out.println((i+dx[k])+" "+(j+dy[k])); int temp=a[i][j]; a[i][j]=a[i+dx[k]][j+dy[k]]; a[i+dx[k]][j+dy[k]]=temp; f=backtrack(i+dx[k],j+dy[k],a,gol,n,set,i,j); if(f) { System.out.println(temp+" "+a[i][j]); return f; } temp=a[i][j]; a[i][j]=a[i+dx[k]][j+dy[k]]; a[i+dx[k]][j+dy[k]]=temp; } } // visited[i][j]=false; return f; } static int find_parent(int p) { if(p==parent[p]) { return p; } return parent[p]=find_parent(parent[p]); } static void set_union(int x,int y) { if(size[x]<size[y]) { swap(x,y); } parent[y]=x; size[x]+=size[y]; } static boolean bipartite(int i,List<List<Integer>> ls,boolean[] visited,int[] color, int x) { color[i]=x; visited[i]=true; for(int x1:ls.get(i)) { // System.out.println(visited[x1]+" "+color[x1]==x); if(visited[x1]&&color[x1]==x) { return false; } if(!visited[x1]) { return bipartite(x1,ls,visited,color,(x==-1?1:-1)); } } return true; } static boolean isDirectedCycle(List<Integer> stack,boolean[] visited,List<List<Integer>> ls) { int i=stack.get(stack.size()-1); visited[i]=true; for(int x:ls.get(i)) { stack.add(x); if(!visited[x]&&isDirectedCycle(stack,visited,ls)) { return true; } if(stack.contains(x)) { return true; } stack.remove(stack.size()-1); } return false; } static boolean isCyclic(int i,List<List<Integer>> ls,boolean[] visited,int parent) { visited[i]=true; for(int x:ls.get(i)) { if(x!=parent){ if(visited[x]) { return true; } if(!visited[x]&&isCyclic(x,ls,visited,i)) { return true; } } } return false; } static void dfs(List<List<Integer>> ls,boolean[] f,List<Integer> stack) { int x=stack.remove(stack.size()-1); System.out.print(x+"->"); f[x]=true; for(int x1:ls.get(x)) { if(!f[x1]) { stack.add(x1); dfs(ls,f,stack); } } } static int factorial(int n,int mod) { long res = 1; for (int i=2; i<=n; i++) res=((res%mod)*(i%mod))%mod; return (int)res; } static int length(long l) { int c=0; while(l>0) { c++; l/=2; } return c; } static int CeilIndex(int A[], int l, int r, int key) { while (r - l > 1) { int m = l + (r - l) / 2; if (A[m] <= key) r = m; else l = m; } return r; } static int LongestIncreasingSubsequenceLength(int A[], int size) { // Add boundary case, when array size is one int[] tailTable = new int[size]; int len; // always points empty slot tailTable[0] = A[0]; len = 1; for (int i = 1; i < size; i++) { if (A[i] > tailTable[0]) tailTable[0] = A[i]; else if (A[i] <= tailTable[len - 1]) tailTable[len++] = A[i]; else tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]; } return len; } static int[] next(int arr[], int n) { Stack<Integer> S = new Stack<>(); int[] next=new int[n]; for (int i = n-1; i >= 0; i--) { while (!S.empty() && arr[S.peek()] <= arr[i]) { S.pop(); } if (S.empty()) { next[i]=-1; } else { next[i]=S.peek(); } S.push(i); } return next; } static int[] back(int arr[], int n) { Stack<Integer> S = new Stack<>(); int[] back=new int[n]; for (int i = 0; i < n; i++) { while (!S.empty() && arr[S.peek()] < arr[i]) { S.pop(); } if (S.empty()) { back[i]=-1; } else { back[i]=S.peek(); } S.push(i); } return back; } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public long[] nextlongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static long mod = 1000000007; public static int d; public static int p; public static int q; public static int[] suffle(int[] a,Random gen) { int n = a.length; for(int i=0;i<n;i++) { int ind = gen.nextInt(n-i)+i; int temp = a[ind]; a[ind] = a[i]; a[i] = temp; } return a; } public static void swap(int a, int b){ int temp = a; a = b; b = temp; } public static HashSet<Integer> primeFactorization(int n) { HashSet<Integer> a =new HashSet<Integer>(); for(int i=2;i*i<=n;i++) { while(n%i==0) { a.add(i); n/=i; } } if(n!=1) a.add(n); return a; } public static void sieve(boolean[] isPrime,int n) { for(int i=1;i<n;i++) isPrime[i] = true; isPrime[0] = false; isPrime[1] = false; for(int i=2;i*i<n;i++) { if(isPrime[i] == true) { for(int j=(2*i);j<n;j+=i) isPrime[j] = false; } } } public static int GCD(int a,int b) { if(b==0) return a; else return GCD(b,a%b); } public static long GCD(long a,long b) { if(b==0) return a; else return GCD(b,a%b); } public static void extendedEuclid(int A,int B) { if(B==0) { d = A; p = 1 ; q = 0; } else { extendedEuclid(B, A%B); int temp = p; p = q; q = temp - (A/B)*q; } } public static long LCM(long a,long b) { return (a*b)/GCD(a,b); } public static int LCM(int a,int b) { return (a*b)/GCD(a,b); } public static int binaryExponentiation(int x,int n) { int result=1; while(n>0) { if(n % 2 ==1) result=result * x; x=x*x; n=n/2; } return result; } int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); return binarySearch(arr, mid + 1, r, x); } return -1; } public static long binaryExponentiation(long x,long n) { long result=1; while(n>0) { if(n % 2 ==1) result=result * x; x=x*x; n=n/2; } return result; } public static int modularExponentiation(int x,int n,int M) { int result=1; while(n>0) { if(n % 2 ==1) result=(result * x)%M; x=(x*x)%M; n=n/2; } return result; } public static long modularExponentiation(long x,long n,long M) { long result=1; while(n>0) { if(n % 2 ==1) result=(result * x)%M; x=(x*x)%M; n=n/2; } return result; } public static int modInverse(int A,int M) { return modularExponentiation(A,M-2,M); } public static long modInverse(long A,long M) { return modularExponentiation(A,M-2,M); } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n%2 == 0 || n%3 == 0) return false; for (int i=5; i*i<=n; i=i+6) { if (n%i == 0 || n%(i+2) == 0) return false; } return true; } static class Pair { int x; int y; } static class pair implements Comparable<pair> { Integer x, y; pair(int x,int y) { this.x=x; this.y=y; } public int compareTo(pair o) { int result = x.compareTo(o.x); if(result==0) result = y.compareTo(o.y); return result; } public String toString() { return x+" "+y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair)o; return (Math.abs(p.x-x)==0 && Math.abs(p.y-y)==0); } return false; } public int hashCode() { return new Long(x).hashCode()*31 + new Long(y).hashCode(); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
cae869b18fc4dfe087e7c08d5d8b1c7b
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.util.*; public class Solution { static PrintWriter out = new PrintWriter((System.out)); static Kioken sc = new Kioken(); public static void main(String[] args) { int t = 1; t = sc.nextInt(); while (t-- > 0) { solve(); } out.close(); } public static void solve() { int n = sc.nextInt(); int[] arr = new int[n]; List<Integer> odd = new ArrayList<>(); List<Integer> even = new ArrayList<>(); long[] power = new long[n]; int cnt = 0; for(int i = 0 ; i < n; i++){ arr[i] = sc.nextInt(); if(arr[i]%2 == 0){ int val = arr[i]; while(val%2 == 0){ val = val/2; cnt++; } power[i] = val; }else{ power[i] = arr[i]; } } long sum = 0L; for(long i: power){ sum += i; } // out.println(Arrays.toString(power) + " " + cnt); long ans = Integer.MIN_VALUE; for(int i =0; i < n; i++){ if(arr[i]%2 == 0){ long sum1 = sum - power[i]; long val = arr[i]; long left_cnt = (long)Math.pow(2, cnt)/(long)(val/power[i]); sum1 += (val*left_cnt); // out.println(" ans " + sum1 + " " + left_cnt); ans = Math.max(ans, sum1); }else{ long val = arr[i]; val = val*(long)Math.pow(2, cnt); long sum1 = sum - power[i] + val; ans = Math.max(ans, sum1); } } out.println(ans); // Collections.sort(odd); // Collections.sort(even); // long sum1 = 0L; // if(odd.size() > 0){ // long val = odd.get(odd.size() - 1); // long sum = 0L; // for(int i: even){ // while(i%2 == 0){ // i = i/2; // val = val*2; // } // sum += i; // } // for(int i = 0; i < odd.size() - 1; i++){ // sum += odd.get(i); // } // sum1 = sum + val; // // out.println(sum + val); // } // long sum2 = 0L; // if(even.size() > 0){ // long val = even.get(even.size() - 1); // long sum = 0L; // for(int i = 0; i < even.size()-1; i++){ // int e = even.get(i); // while(e%2 == 0){ // val = val*2; // e = e/2; // } // sum += e; // } // // adding odd // for(int x: odd){ // sum += x; // } // // out.println(val + sum); // sum2 = sum + val; // } // out.println(Math.max(sum1, sum2)); } public static long leftShift(long a){ return (long)Math.pow(2, a); } public static int lower_bound(ArrayList<Integer> ar, int k) { int s = 0, e = ar.size(); while (s != e) { int mid = s + e >> 1; if (ar.get(mid) <= k) { s = mid + 1; } else { e = mid; } } return Math.abs(s) - 1; } public static int upper_bound(ArrayList<Integer> ar, int k) { int s = 0; int e = ar.size(); while (s != e) { int mid = s + e >> 1; if (ar.get(mid) < k) { s = mid + 1; } else { e = mid; } } if (s == ar.size()) { return -1; } return s; } static class Kioken { // FileInputStream br = new FileInputStream("input.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } public boolean hasNext() { String next = null; try { next = br.readLine(); } catch (Exception e) { } if (next == null || next.length() == 0) { return false; } st = new StringTokenizer(next); return true; } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
38c73a47da1f59b065807b22fe64ae03
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; public class Solution{ public static int log2(int N){ int result = (int)(Math.log(N) / Math.log(2)); return result; } public static void main(String[] args){ Scanner in=new Scanner(System.in); int t=in.nextInt(); for(int i=0;i<t;i++){ int n=in.nextInt(); int[] arr=new int[n]; int index=0; int max=0; int count=0; for(int j=0;j<n;j++){ int a=in.nextInt(); while(a%2==0){ a=a/2; count+=1; } arr[j]=a; if(a>=max){ max=a; index=j; } } long sum=0; for(int k=0;k<n;k++){ if(k==index){ sum+=arr[k]*Math.pow(2,count); } else{ sum+=arr[k]; } } System.out.println(sum); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
5b790056798bf0becc3a6a6320ada807
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class ProblemA { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(f.readLine()); int t = Integer.parseInt(st.nextToken()); for(int i = 0; i < t; i++) { st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(f.readLine()); int[] vals = new int[n]; for(int j = 0; j < n; j++) { vals[j] = Integer.parseInt(st.nextToken()); } Arrays.sort(vals); int count = 0; for(int j = 0; j < n; j++) { int x = vals[j]; while(x%2==0) { x/=2; count++; } } long sum = 0; for(int k = 0; k < n; k++) { int add = 0; int x = vals[k]; while(x%2==0) { x/=2; add++; } long max = (long) (vals[k]*Math.pow(2, count-add)); for(int j = 0; j < n; j++) { if(j==k) continue; int total = 0; int y = vals[j]; while(y%2==0) { y/=2; total++; } if(vals[j]%2==0) { max+=(long)(vals[j]/(Math.pow(2, total))); } else { max+=vals[j]; } } sum = Math.max(max, sum); } System.out.println(sum); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
933c355f2333a936df54fe8e8f419bcd
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
/* بسم الله الرحمن الرحيم /$$$$$ /$$$$$$ /$$ /$$ /$$$$$$ |__ $$ /$$__ $$ |$$ |$$ /$$__ $$ | $$| $$ \ $$| $$|$$| $$ \ $$ | $$| $$$$$$$$| $$ / $$/| $$$$$$$$ / $$ | $$| $$__ $$ \ $$ $$/ | $$__ $$ | $$ | $$| $$ | $$ \ $$$/ | $$ | $$ | $$$$$$/| $$ | $$ \ $/ | $$ | $$ \______/ |__/ |__/ \_/ |__/ |__/ /$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$ | $$__ $$| $$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$$ /$$$| $$_____/| $$__ $$ | $$ \ $$| $$ \ $$| $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$$$ /$$$$| $$ | $$ \ $$ | $$$$$$$/| $$$$$$$/| $$ | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$ | $$$$$$$/ | $$____/ | $$__ $$| $$ | $$| $$|_ $$| $$__ $$| $$__ $$| $$ $$$| $$| $$ $$$| $$| $$__/ | $$__ $$ | $$ | $$ \ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$| $$\ $ | $$| $$ | $$ \ $$ | $$ | $$ | $$| $$$$$$/| $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$| $$ \/ | $$| $$$$$$$$| $$ | $$ |__/ |__/ |__/ \______/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/ |__/|________/|__/ |__/ */ import java.util.*; import java.lang.*; import java.io.*; public class Deltix281121A { public static void main(String[] args) throws java.lang.Exception { // your code goes here //try { // Scanner sc=new Scanner(System.in); FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); } Arrays.sort(arr); long ans=Long.MIN_VALUE; for(int i=0;i<n;i++){ long csum=0l; long val1=arr[i]; for(int j=0;j<n;j++){ long val=arr[j]; if(j!=i){ while(arr[j]%2==0){ arr[j]=arr[j]/2; arr[i]=arr[i]*2; } csum+=arr[j]; } arr[j]=val; } csum+=arr[i]; ans=Math.max(ans,csum); arr[i]=val1; } System.out.println(ans); /*continue; boolean odd=false; int oi=n; long sum=0l; int ei=n; boolean even=false; for(int i=n-1;i>=0;i--){ if(arr[i]%2==1 && !odd){ odd=true; oi=i; } else{ if(arr[i]%2==0 && !even){ even=true; ei=i; } } sum+=Long.valueOf(arr[i]); } if(!even){ System.out.println(sum); continue; } if(odd){ for(int i=0;i<n;i++){ if(i!=oi){ long val=arr[i]; while(val%2==0){ val=val/2; arr[oi]*=2; } arr[i]=val; } } sum=0l; for(int i=0;i<n;i++){ sum+=Long.valueOf(arr[i]); } System.out.println(sum); } else{ sum=0l; //System.out.println(ei); for(int i=0;i<n;i++){ if(i!=ei){ long val=arr[i]; while(val%2==0){ val=val/2; arr[ei]*=2; } arr[i]=val; } } sum=0l; for(int i=0;i<n;i++){ sum+=Long.valueOf(arr[i]); } System.out.println(sum); } /*long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); }*/ } /*} catch (Exception e) { return; }*/ } public static int lowerbound(int[] ar,int k) { int s=0; int e=ar.length; while (s !=e) { int mid = s+e>>1; if (ar[mid] <k) { s=mid+1; } else { e=mid; } } if(s==ar.length) { return -1; } return s; } public static class pair { int ff; int ss; pair(int ff, int ss) { this.ff = ff; this.ss = ss; } } static int BS(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] < element) { low = mid + 1; } else { high = mid; } } if (arr[low] == element) { return low; } else if (arr[high] == element) { return high; } return -1; } static int lower_bound(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] < element) { low = mid + 1; } else { high = mid; } } if (arr[low] >= element) { return low; } else if (arr[high] >= element) { return high; } return -1; } static int upper_bound(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] <= element) { low = mid + 1; } else { high = mid; } } if (arr[low] > element) { return low; } else if (arr[high] > element) { return high; } return -1; } public static int upperbound(long[] arr, int k) { int s = 0; int e = arr.length; while (s != e) { int mid = s + e >> 1; if (arr[mid] <= k) { s = mid + 1; } else { e = mid; } } if (s == arr.length) { return -1; } return s; } public static long pow(long x,long y,long mod){ if(x==0)return 0l; if(y==0)return 1l; //(x^y)%mod if(y%2l==1l){ return ((x%mod)*(pow(x,y-1l,mod)%mod))%mod; } return pow(((x%mod)*(x%mod))%mod,y/2l,mod); } public static long gcd_long(long a, long b) { // a/b,a-> dividant b-> divisor if (b == 0) return a; return gcd_long(b, a % b); } public static int gcd_int(int a, int b) { // a/b,a-> dividant b-> divisor if (b == 0) return a; return gcd_int(b, a % b); } public static int lcm(int a, int b) { int gcd = gcd_int(a, b); return (a * b) / gcd; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble(){ return Double.valueOf(Integer.parseInt(next())); } String nextLine() { String s = ""; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } Long nextLong() { return Long.parseLong(next()); } } public static boolean contains(String main, String Substring) { boolean flag=false; if(main==null && main.trim().equals("")) { return flag; } if(Substring==null) { return flag; } char fullstring[]=main.toCharArray(); char sub[]=Substring.toCharArray(); int counter=0; if(sub.length==0) { flag=true; return flag; } for(int i=0;i<fullstring.length;i++) { if(fullstring[i]==sub[counter]) { counter++; } else { counter=0; } if(counter==sub.length) { flag=true; return flag; } } return flag; } } /* * public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){ * return true; } if(n<1 || m<1 || k<0){ return false; } boolean * tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; } * return false; } */
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
bc3933422c1227c40f0ff4b1031957ab
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Stack; public class Main { static long MOD = 998244353l; int re = 0; public static void main(String[] args) throws Exception { // FileInputStream fis = new FileInputStream(new // File("/Users/goudezhao/Downloads/A-2/tester/input_0.txt")); var sc = new FastScanner(); // var sc = new FastScanner(fis); // var pw = new FastPrintStream("/Users/goudezhao/Downloads/A-2/tester/0.out"); var pw = new FastPrintStream(); solve(sc, pw); sc.close(); pw.flush(); pw.close(); } public static void solve(FastScanner sc, FastPrintStream pw) { int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); long a[] = new long[n]; Arrays.setAll(a, j -> sc.nextLong()); Arrays.sort(a); if (n == 1) { pw.println(a[0]); continue; } long sum = 0; int count = 0; for (int j = 0; j < n; j++) { while (a[j] % 2 == 0) { a[j] /= 2; count++; } } Arrays.sort(a); for (int j=0;j<count;j++) { a[n-1]*=2; } for (int j = 0; j < n; j++) { sum += a[j]; } pw.println(sum); } } public static long anothertoTen(long ano, int another) { long ten = 0; long now = 1; long temp = ano; while (temp > 0) { long i = temp % 10; ten += now * i; now *= another; temp /= 10; } return ten; } public static long tentoAnother(long ten, int another) { Stack<Long> stack = new Stack<Long>(); while (ten > 0) { stack.add(ten % another); ten /= another; } long re = 0; while (!stack.isEmpty()) { long pop = stack.pop(); re = re * 10 + pop; } return re; } // 2C5 = 5*4/(2*1) public static long XCY(long x, long y) { long temp = 1; for (int i = 0; i < x; i++) { temp = (temp * (y - i)) % MOD; } long tempx = 1; for (int i = 2; i <= x; i++) { tempx = (tempx * i) % MOD; } tempx = modpow(tempx, (long) MOD - 2); temp = (temp * tempx) % MOD; return temp; } static long modpow(long N, Long K) { return BigInteger.valueOf(N).modPow(BigInteger.valueOf(K), BigInteger.valueOf(MOD)).longValue(); } static long modpow(long N, Long K, long mod) { return BigInteger.valueOf(N).modPow(BigInteger.valueOf(K), BigInteger.valueOf(mod)).longValue(); } public static long gcd(long a, long b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } public static int gcd(int a, int b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } } class Range { long start = 0; long end = 0; } class Point implements Comparable { int x; int y; public int compareTo(Object p) { Point t = (Point) p; if (this.x < t.x) { return -1; } if (this.x > t.x) { return 1; } return t.y - this.y; } } class PointY implements Comparable { int index; long a; long b; public int compareTo(Object p) { PointY t = (PointY) p; if (this.a > t.a) { return -1; } if (this.a < t.a) { return 1; } return 0; } } class FastPrintStream implements AutoCloseable { private static final int BUF_SIZE = 1 << 15; private final byte[] buf = new byte[BUF_SIZE]; private int ptr = 0; private final java.lang.reflect.Field strField; private final java.nio.charset.CharsetEncoder encoder; private java.io.OutputStream out; public FastPrintStream(java.io.OutputStream out) { this.out = out; java.lang.reflect.Field f; try { f = java.lang.String.class.getDeclaredField("value"); f.setAccessible(true); } catch (NoSuchFieldException | SecurityException e) { f = null; } this.strField = f; this.encoder = java.nio.charset.StandardCharsets.US_ASCII.newEncoder(); } public FastPrintStream(java.io.File file) throws java.io.IOException { this(new java.io.FileOutputStream(file)); } public FastPrintStream(java.lang.String filename) throws java.io.IOException { this(new java.io.File(filename)); } public FastPrintStream() { this(System.out); try { java.lang.reflect.Field f = java.io.PrintStream.class.getDeclaredField("autoFlush"); f.setAccessible(true); f.set(System.out, false); } catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException e) { // ignore } } public FastPrintStream println() { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = (byte) '\n'; return this; } public FastPrintStream println(java.lang.Object o) { return print(o).println(); } public FastPrintStream println(java.lang.String s) { return print(s).println(); } public FastPrintStream println(char[] s) { return print(s).println(); } public FastPrintStream println(char c) { return print(c).println(); } public FastPrintStream println(int x) { return print(x).println(); } public FastPrintStream println(long x) { return print(x).println(); } public FastPrintStream println(double d, int precision) { return print(d, precision).println(); } private FastPrintStream print(byte[] bytes) { int n = bytes.length; if (ptr + n > BUF_SIZE) { internalFlush(); try { out.write(bytes); } catch (java.io.IOException e) { throw new RuntimeException(); } } else { System.arraycopy(bytes, 0, buf, ptr, n); ptr += n; } return this; } public FastPrintStream print(java.lang.Object o) { return print(o.toString()); } public FastPrintStream print(java.lang.String s) { if (strField == null) { return print(s.getBytes()); } else { try { return print((byte[]) strField.get(s)); } catch (IllegalAccessException e) { return print(s.getBytes()); } } } public FastPrintStream print(char[] s) { try { return print(encoder.encode(java.nio.CharBuffer.wrap(s)).array()); } catch (java.nio.charset.CharacterCodingException e) { byte[] bytes = new byte[s.length]; for (int i = 0; i < s.length; i++) { bytes[i] = (byte) s[i]; } return print(bytes); } } public FastPrintStream print(char c) { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = (byte) c; return this; } public FastPrintStream print(int x) { if (x == 0) { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = '0'; return this; } int d = len(x); if (ptr + d > BUF_SIZE) internalFlush(); if (x < 0) { buf[ptr++] = '-'; x = -x; d--; } int j = ptr += d; while (x > 0) { buf[--j] = (byte) ('0' + (x % 10)); x /= 10; } return this; } public FastPrintStream print(long x) { if (x == 0) { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = '0'; return this; } int d = len(x); if (ptr + d > BUF_SIZE) internalFlush(); if (x < 0) { buf[ptr++] = '-'; x = -x; d--; } int j = ptr += d; while (x > 0) { buf[--j] = (byte) ('0' + (x % 10)); x /= 10; } return this; } public FastPrintStream print(double d, int precision) { if (d < 0) { print('-'); d = -d; } d += Math.pow(10, -d) / 2; print((long) d).print('.'); d -= (long) d; for (int i = 0; i < precision; i++) { d *= 10; print((int) d); d -= (int) d; } return this; } private void internalFlush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (java.io.IOException e) { throw new RuntimeException(e); } } public void flush() { try { out.write(buf, 0, ptr); out.flush(); ptr = 0; } catch (java.io.IOException e) { throw new RuntimeException(e); } } public void close() { try { out.close(); } catch (java.io.IOException e) { throw new RuntimeException(e); } } private static int len(int x) { int d = 1; if (x >= 0) { d = 0; x = -x; } int p = -10; for (int i = 1; i < 10; i++, p *= 10) if (x > p) return i + d; return 10 + d; } private static int len(long x) { int d = 1; if (x >= 0) { d = 0; x = -x; } long p = -10; for (int i = 1; i < 19; i++, p *= 10) if (x > p) return i + d; return 19 + d; } } class FastScanner implements AutoCloseable { private final java.io.InputStream in; private final byte[] buf = new byte[2048]; private int ptr = 0; private int buflen = 0; public FastScanner(java.io.InputStream in) { this.in = in; } public FastScanner() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) return true; ptr = 0; try { buflen = in.read(buf); } catch (java.io.IOException e) { throw new RuntimeException(e); } return buflen > 0; } private int readByte() { return hasNextByte() ? buf[ptr++] : -1; } public boolean hasNext() { while (hasNextByte() && !(32 < buf[ptr] && buf[ptr] < 127)) ptr++; return hasNextByte(); } private StringBuilder nextSequence() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); for (int b = readByte(); 32 < b && b < 127; b = readByte()) { sb.appendCodePoint(b); } return sb; } public String next() { return nextSequence().toString(); } public String next(int len) { return new String(nextChars(len)); } public char nextChar() { if (!hasNextByte()) throw new java.util.NoSuchElementException(); return (char) readByte(); } public char[] nextChars() { StringBuilder sb = nextSequence(); int l = sb.length(); char[] dst = new char[l]; sb.getChars(0, l, dst, 0); return dst; } public char[] nextChars(int len) { if (!hasNext()) throw new java.util.NoSuchElementException(); char[] s = new char[len]; int i = 0; int b = readByte(); while (32 < b && b < 127 && i < len) { s[i++] = (char) b; b = readByte(); } if (i != len) { throw new java.util.NoSuchElementException( String.format("Next token has smaller length than expected.", len)); } return s; } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) throw new NumberFormatException(); while (true) { if ('0' <= b && b <= '9') { n = n * 10 + b - '0'; } else if (b == -1 || !(32 < b && b < 127)) { return minus ? -n : n; } else throw new NumberFormatException(); b = readByte(); } } public int nextInt() { return Math.toIntExact(nextLong()); } public double nextDouble() { return Double.parseDouble(next()); } public void close() { try { in.close(); } catch (java.io.IOException e) { throw new RuntimeException(e); } } } /** * @verified https://atcoder.jp/contests/practice2/tasks/practice2_j */ class SegTree<S> { final int MAX; final int N; final java.util.function.BinaryOperator<S> op; final S E; final S[] data; @SuppressWarnings("unchecked") public SegTree(int n, java.util.function.BinaryOperator<S> op, S e) { this.MAX = n; int k = 1; while (k < n) k <<= 1; this.N = k; this.E = e; this.op = op; this.data = (S[]) new Object[N << 1]; java.util.Arrays.fill(data, E); } public SegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e) { this(dat.length, op, e); build(dat); } private void build(S[] dat) { int l = dat.length; System.arraycopy(dat, 0, data, N, l); for (int i = N - 1; i > 0; i--) { data[i] = op.apply(data[i << 1 | 0], data[i << 1 | 1]); } } public void set(int p, S x) { exclusiveRangeCheck(p); data[p += N] = x; p >>= 1; while (p > 0) { data[p] = op.apply(data[p << 1 | 0], data[p << 1 | 1]); p >>= 1; } } public S get(int p) { exclusiveRangeCheck(p); return data[p + N]; } public S prod(int l, int r) { if (l > r) { throw new IllegalArgumentException(String.format("Invalid range: [%d, %d)", l, r)); } inclusiveRangeCheck(l); inclusiveRangeCheck(r); S sumLeft = E; S sumRight = E; l += N; r += N; while (l < r) { if ((l & 1) == 1) sumLeft = op.apply(sumLeft, data[l++]); if ((r & 1) == 1) sumRight = op.apply(data[--r], sumRight); l >>= 1; r >>= 1; } return op.apply(sumLeft, sumRight); } public S allProd() { return data[1]; } public int maxRight(int l, java.util.function.Predicate<S> f) { inclusiveRangeCheck(l); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (l == MAX) return MAX; l += N; S sum = E; do { l >>= Long.numberOfTrailingZeros(l); if (!f.test(op.apply(sum, data[l]))) { while (l < N) { l = l << 1; if (f.test(op.apply(sum, data[l]))) { sum = op.apply(sum, data[l]); l++; } } return l - N; } sum = op.apply(sum, data[l]); l++; } while ((l & -l) != l); return MAX; } public int minLeft(int r, java.util.function.Predicate<S> f) { inclusiveRangeCheck(r); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (r == 0) return 0; r += N; S sum = E; do { r--; while (r > 1 && (r & 1) == 1) r >>= 1; if (!f.test(op.apply(data[r], sum))) { while (r < N) { r = r << 1 | 1; if (f.test(op.apply(data[r], sum))) { sum = op.apply(data[r], sum); r--; } } return r + 1 - N; } sum = op.apply(data[r], sum); } while ((r & -r) != r); return 0; } private void exclusiveRangeCheck(int p) { if (p < 0 || p >= MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d).", p, 0, MAX)); } } private void inclusiveRangeCheck(int p) { if (p < 0 || p > MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d].", p, 0, MAX)); } } // **************** DEBUG **************** // private int indent = 6; public void setIndent(int newIndent) { this.indent = newIndent; } @Override public String toString() { return toSimpleString(); } public String toDetailedString() { return toDetailedString(1, 0); } private String toDetailedString(int k, int sp) { if (k >= N) return indent(sp) + data[k]; String s = ""; s += toDetailedString(k << 1 | 1, sp + indent); s += "\n"; s += indent(sp) + data[k]; s += "\n"; s += toDetailedString(k << 1 | 0, sp + indent); return s; } private static String indent(int n) { StringBuilder sb = new StringBuilder(); while (n-- > 0) sb.append(' '); return sb.toString(); } public String toSimpleString() { StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = 0; i < N; i++) { sb.append(data[i + N]); if (i < N - 1) sb.append(',').append(' '); } sb.append(']'); return sb.toString(); } } class DSU { private int n; private int[] parentOrSize; public DSU(int n) { this.n = n; this.parentOrSize = new int[n]; Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n) || !(0 <= b && b < n)) { return -1; } int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; return x; } boolean same(int a, int b) { if (!(0 <= a && a < n) || !(0 <= b && b < n)) { return false; } return leader(a) == leader(b); } int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) { return -1; } return -parentOrSize[leader(a)]; } ArrayList<ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); for (int i = 0; i < n; i++) { result.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } return result; } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
f1e8e6142055338697e9249b100fbfe2
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.util.*; public class AA{ public static void main(String[] args) throws Exception { BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int t = Integer.parseInt(buffer.readLine()); while (t-- > 0) { int n = Integer.parseInt(buffer.readLine()); String [] inp = buffer.readLine().split(" "); long [] arr = new long[n]; long sum = 0, pow = 1, max = 0; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(inp[i]); while (arr[i] % 2 == 0) { arr[i] /= 2; pow *= 2; } max = Math.max(max, arr[i]); } for (int i = 0; i < n; i++) { if (max == arr[i]) { arr[i] *= pow; max = -1; } sum += arr[i]; } sb.append(sum+"\n"); } System.out.println(sb); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
fd2f45498f23b0b27d256029c215a943
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { private static FS sc = new FS(); private static class FS { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } private static class extra { static int[] intArr(int size) { int[] a = new int[size]; for(int i = 0; i < size; i++) a[i] = sc.nextInt(); return a; } static long[] longArr(int size) { Scanner scc = new Scanner(System.in); long[] a = new long[size]; for(int i = 0; i < size; i++) a[i] = sc.nextLong(); return a; } static long intSum(int[] a) { long sum = 0; for(int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } static long longSum(long[] a) { long sum = 0; for(int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } static LinkedList[] graphD(int vertices, int edges) { LinkedList<Integer>[] temp = new LinkedList[vertices+1]; for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>(); for(int i = 0; i < edges; i++) { int x = sc.nextInt(); int y = sc.nextInt(); temp[x].add(y); } return temp; } static LinkedList[] graphUD(int vertices, int edges) { LinkedList<Integer>[] temp = new LinkedList[vertices+1]; for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>(); for(int i = 0; i < edges; i++) { int x = sc.nextInt(); int y = sc.nextInt(); temp[x].add(y); temp[y].add(x); } return temp; } static void printG(LinkedList[] temp) { for(LinkedList<Integer> aa:temp) System.out.println(aa); } static long cal(long val, long pow, long mod) { if(pow == 0) return 1; long res = cal(val, pow/2, mod); long ret = (res*res)%mod; if(pow%2 == 0) return ret; return (val*ret)%mod; } static long gcd(long a, long b) { return b == 0 ? a:gcd(b, a%b); } } static int mod = 998244353; static LinkedList<Integer>[] temp, idx; static long inf = (long) Long.MAX_VALUE; // static long inf = Long.MAX_VALUE; static int max; public static void main(String[] args) { int t = sc.nextInt(); // int t = 1; StringBuilder ret = new StringBuilder(); while(t-- > 0) { int n = sc.nextInt(); long[] a = extra.longArr(n); long max = 1; for(int i = 0; i < n; i++) { while(a[i]%2 == 0) { a[i] /= 2; max *= 2; } } int idx = 0; for(int i = 0; i < n; i++) { if(a[i] > a[idx]) idx = i; } a[idx] *= max; long sum = 0; for(long aa:a) sum += aa; ret.append(sum +"\n"); } System.out.println(ret); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
48b0227c2c1006156a1e6fd512a7a03a
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; /** * @author Mubtasim Shahriar */ public class DeltixAutumn2021 { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader sc = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); int t = sc.nextInt(); // int t = 1; while (t-- != 0) { solver.solve(sc, out); } out.close(); } static class Solver { public void solve(InputReader sc, PrintWriter out) { int n = sc.nextInt(); long[] arr = sc.nextLongArray(n); sort(arr); long[] lessarr = new long[n]; for(int i = 0; i < n; i++) { long now = arr[i]; while(now%2==0) now /= 2; lessarr[i] = now; } int maxid = 0; for(int i = 1; i < n; i++) { if(lessarr[i]>lessarr[maxid]) maxid = i; } for(int i = 0; i < n; i++) { if(i==maxid) continue; while(arr[i]%2==0) { arr[i] /= 2; arr[maxid] *= 2; } } long sum = 0; for(long l : arr) sum += l; out.println(sum); } } static void sort(int[] arr) { ArrayList<Integer> al = new ArrayList(); for (int i : arr) al.add(i); Collections.sort(al); int idx = 0; for (int i : al) arr[idx++] = i; } static void sort(long[] arr) { ArrayList<Long> al = new ArrayList(); for (long i : arr) al.add(i); Collections.sort(al); int idx = 0; for (long i : al) arr[idx++] = i; } static void sortDec(int[] arr) { ArrayList<Integer> al = new ArrayList(); for (int i : arr) al.add(i); Collections.sort(al, Collections.reverseOrder()); int idx = 0; for (int i : al) arr[idx++] = i; } static void sortDec(long[] arr) { ArrayList<Long> al = new ArrayList(); for (long i : arr) al.add(i); Collections.sort(al, Collections.reverseOrder()); int idx = 0; for (long i : al) arr[idx++] = i; } static class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) { return -1; } } return buf[curChar]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public BigInteger readBigInteger() { try { return new BigInteger(nextString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return nextString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public int[] nextSortedIntArray(int n) { int array[] = nextIntArray(n); Arrays.sort(array); return array; } public int[] nextSumIntArray(int n) { int[] array = new int[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; ++i) array[i] = nextLong(); return array; } public long[] nextSumLongArray(int n) { long[] array = new long[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextSortedLongArray(int n) { long array[] = nextLongArray(n); Arrays.sort(array); return array; } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
d4031edf12c7dda0b6406204f0efac8d
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.math.*; import java.io.*; import java.util.stream.Collectors; import java.text.DecimalFormat; public class A{ static FastScanner scan=new FastScanner(); public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); public static void main(String[] args) throws Exception { int tt=1; tt=scan.nextInt(); // scan=new FastScanner("clumsy.in"); //out=new PrintWriter("clumsy.out"); outer:while(tt-->0) { int n=scan.nextInt(); long arr[]=new long[n]; long res=0,max=0; int id=-1; for(int i=0;i<n;i++) { arr[i]=scan.nextLong(); if(arr[i]>max&&arr[i]%2!=0) { max=arr[i]; id=i; } } Set<long[]>st=new HashSet<long[]>(); while(true){ int cnt=0; max=0; for(int i=0;i<n;i++) { if(arr[i]==1) cnt++; } if(cnt==n-1) break; id=-1; for(int i=0;i<n;i++) { if(arr[i]>max&&arr[i]%2!=0) { max=arr[i]; id=i; } } if(id==-1) { for(int i=0;i<n;i++) { if(arr[i]>max) { max=arr[i]; id=i; } } } /*that:while(true) {*/ // System.out.println(Arrays.toString(arr)); that:for(int i=0;i<n;i++) { if(i!=id&&arr[i]%2==0) { while(arr[i]%2==0) { arr[i]/=2; arr[id]*=2; } // continue that; } } //System.out.println(Arrays.toString(arr)); if(st.contains(arr)) break; long nw=0; for(int i=0;i<n;i++)nw+=arr[i]; res=Math.max(res,nw); st.add(arr); } //} long nw=0; for(int i=0;i<n;i++)nw+=arr[i]; out.println(Math.max(res,nw)); } out.close(); } static class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } static class Pair implements Comparable<Pair>{ public long x, y,z; public Pair(long x1, long y1,long z1) { x=x1; y=y1; z=z1; } public Pair(long x1, long y1) { x=x1; y=y1; } @Override public int hashCode() { return (int)(x + 31 * y); } public String toString() { return x + " " + y+" "+z; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; Pair t = (Pair)o; return t.x == x && t.y == y&&t.z==z; } public int compareTo(Pair o) { //if(o.x==x) // return (int)(y-o.y); return Long.compare(o.x,x); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
66460c87d0381e1a0756465df351b0f1
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.util.concurrent.LinkedBlockingDeque; import javax.security.auth.login.AccountExpiredException; import javax.swing.text.Segment; import java.io.*; import java.math.*; import java.sql.Array; import java.sql.ResultSet; import java.sql.SQLException; public class Main { private static class MyScanner { private static final int BUF_SIZE = 2048; BufferedReader br; private MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } private boolean isSpace(char c) { return c == '\n' || c == '\r' || c == ' '; } String next() { try { StringBuilder sb = new StringBuilder(); int r; while ((r = br.read()) != -1 && isSpace((char)r)); if (r == -1) { return null; } sb.append((char) r); while ((r = br.read()) != -1 && !isSpace((char)r)) { sb.append((char)r); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } static class Reader{ BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static void rvrs(int[] arr) { int i =0 , j = arr.length-1; while(i>=j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static void rvrs(long[] arr) { int i =0 , j = arr.length-1; while(i>=j) { long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static long mod_mul( long mod , long... a) { long ans = a[0]%mod; for(int i = 1 ; i<a.length ; i++) { ans = (ans * (a[i]%mod))%mod; } return ans; } static long mod_sum(long mod , long... a) { long ans = 0; for(long e:a) { ans = (ans + e)%mod; } return ans; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2, m); } static long lcm(long a , long b) { return (a*b)/gcd(a, b); } static int lcm(int a , int b) { return (int)((a*b)/gcd(a, b)); } static long power(long x, long y, long m){ if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (int)((p * (long)p) % m); if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); } static class Combinations{ private long[] z; private long[] z1; private long[] z2; public Combinations(long N , long mod) { z = new long[(int)N+1]; z1 = new long[(int)N+1]; z[0] = 1; for(int i =1 ; i<=N ; i++) z[i] = (z[i-1]*i)%mod; z2 = new long[(int)N+1]; z2[0] = z2[1] = 1; for (int i = 2; i <= N; i++) z2[i] = z2[(int)(mod % i)] * (mod - mod / i) % mod; z1[0] = z1[1] = 1; for (int i = 2; i <= N; i++) z1[i] = (z2[i] * z1[i - 1]) % mod; } long fac(long n) { return z[(int)n]; } long invrsNum(long n) { return z2[(int)n]; } long invrsFac(long n) { return invrsFac((int)n); } long ncr(long N, long R, long mod) { if(R<0 || R>N ) return 0; long ans = ((z[(int)N] * z1[(int)R]) % mod * z1[(int)(N - R)]) % mod; return ans; } } static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } void makeSet() { for (int i = 0; i < n; i++) { parent[i] = i; } } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } } } static int max(int... a ) { int max = a[0]; for(int e:a) max = Math.max(max, e); return max; } static long max(long... a ) { long max = a[0]; for(long e:a) max = Math.max(max, e); return max; } static int min(int... a ) { int min = a[0]; for(int e:a) min = Math.min(e, min); return min; } static long min(long... a ) { long min = a[0]; for(long e:a) min = Math.min(e, min); return min; } static int[] KMP(String str) { int n = str.length(); int[] kmp = new int[n]; for(int i = 1 ; i<n ; i++) { int j = kmp[i-1]; while(j>0 && str.charAt(i) != str.charAt(j)) { j = kmp[j-1]; } if(str.charAt(i) == str.charAt(j)) j++; kmp[i] = j; } return kmp; } /************************************************ Query **************************************************************************************/ /***************************************** Sparse Table ********************************************************/ static class SparseTable{ private long[][] st; SparseTable(long[] arr){ int n = arr.length; st = new long[n][25]; log = new int[n+2]; build_log(n+1); build(arr); } private void build(long[] arr) { int n = arr.length; for(int i = n-1 ; i>=0 ; i--) { for(int j = 0 ; j<25 ; j++) { int r = i + (1<<j)-1; if(r>=n) break; if(j == 0 ) st[i][j] = arr[i]; else st[i][j] = Math.min(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] ); } } } public long gcd(long a ,long b) { if(a == 0) return b; return gcd(b%a , a); } public long query(int l ,int r) { int w = r-l+1; int power = log[w]; return Math.min(st[l][power],st[r - (1<<power) + 1][power]); } private int[] log; void build_log(int n) { log[1] = 0; for(int i = 2 ; i<=n ; i++) { log[i] = 1 + log[i/2]; } } } /******************************************************** Segement Tree *****************************************************/ /** static class SegmentTree{ long[] tree; long[] arr; int n; SegmentTree(long[] arr){ this.n = arr.length; tree = new long[4*n+1]; this.arr = arr; buildTree(0, n-1, 1); } void buildTree(int s ,int e ,int index ) { if(s == e) { tree[index] = arr[s]; return; } int mid = (s+e)/2; buildTree( s, mid, 2*index); buildTree( mid+1, e, 2*index+1); tree[index] = Math.min(tree[2*index] , tree[2*index+1]); } long query(int si ,int ei) { return query(0 ,n-1 , si ,ei , 1 ); } private long query( int ss ,int se ,int qs , int qe,int index) { if(ss>=qs && se<=qe) return tree[index]; if(qe<ss || se<qs) return (long)(1e17); int mid = (ss + se)/2; long left = query( ss , mid , qs ,qe , 2*index); long right= query(mid + 1 , se , qs ,qe , 2*index+1); return Math.min(left, right); } public void update(int index , int val) { arr[index] = val; for(long e:arr) System.out.print(e+" "); update(index , 0 , n-1 , 1); } private void update(int id ,int si , int ei , int index) { if(id < si || id>ei) return; if(si == ei ) { tree[index] = arr[id]; return; } if(si > ei) return; int mid = (ei + si)/2; update( id, si, mid , 2*index); update( id , mid+1, ei , 2*index+1); tree[index] = Math.min(tree[2*index] ,tree[2*index+1]); } } */ /* ***************************************************************************************************************************************************/ // static MyScanner sc = new MyScanner(); // only in case of less memory static Reader sc = new Reader(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) throws IOException { int tc = 1; tc = sc.nextInt(); for(int i = 1 ; i<=tc ; i++) { // sb.append("Case #" + i + ": " ); // During KickStart && HackerCup TEST_CASE(); } System.out.println(sb); } static void TEST_CASE() { int n = sc.nextInt(); long[] arr = new long[n]; for(int i = 0 ; i<n ; i++) arr[i] = sc.nextLong(); long mul = 1; for(int i =0 ; i<n ; i++) { while(arr[i]%2 == 0) { arr[i] /= 2; mul *=2; } } sort(arr); arr[n-1] *= mul; long sum = 0; for(long e:arr) sum += e; sb.append(sum +"\n"); } } /*******************************************************************************************************************************************************/ /** */
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
b1b258f7c12ae034a64f1d9185c2eeab
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.util.*; public class DeltixA { public static void main(String[] args) { JS scan = new JS(); int t = scan.nextInt(); while(t-->0){ int n = scan.nextInt(); long[] arr = new long[n]; long sum = 0; for(int i= 0;i<n;i++){ arr[i] = scan.nextInt(); sum+=arr[i]; } Arrays.sort(arr); int count = 0; int maxG = n-1; for(int i = 0; i<n;i++){ while(arr[i]%2==0){ count++; arr[i]/=2; } } Arrays.sort(arr); arr[n-1] = (long) (arr[n-1]*Math.pow(2,count)); sum = 0; for(int i =0;i<n;i++){ sum+=arr[i]; } System.out.println(sum); } } static class JS { public int BS = 1 << 16; public char NC = (char) 0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public long nextLong() { num = 1; boolean neg = false; if (c == NC) c = nextChar(); for (; (c < '0' || c > '9'); c = nextChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = nextChar()) { res = (res << 3) + (res << 1) + c - '0'; num *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / num; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c > 32) { res.append(c); c = nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c != '\n') { res.append(c); c = nextChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = nextChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
012c477a42510d44238e98b6355ad1fd
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.util.*; public class A1609 { public static void main(String[] args) { FastScanner sc = new FastScanner("in.txt"); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextLong(); } int k = 0; long sum = 0; long max = 0; for (int i = 0; i < n; i++) { while (a[i] % 2 == 0) { a[i] /= 2; k++; } sum += a[i]; max = Math.max(max, a[i]); } sum = sum - max + max * (long) Math.pow(2, k); out.println(sum); } out.close(); } static final Random random = new Random(); static void ruffleSort(int[] a) { int n = a.length; 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 class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public 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(); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
2b501480518171c7c267234223aa3662
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Math.max; import static java.lang.System.exit; import static java.util.Arrays.fill; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class cf { static long mod = (long) (1e9 + 7); static void solve() throws Exception { int tests = scanInt(); // int tests = 1; for (int test = 0; test < tests; test++) { // int n = scanInt(), k = scanInt(); // String l = scanString(); // out.println(); // continue test; int n = scanInt(); long []arr = arrL(n); long count = 0; long sum = 0; long maximum = 0; for(int i = 0; i < n; i++){ while(arr[i] > 0 && (arr[i] % 2 == 0)){ arr[i] = arr[i]/2; count++; } maximum = Math.max(maximum, arr[i]); sum += arr[i]; } sum -= maximum; long ans = sum + (long)Math.pow(2, count)*maximum; out.println(ans); } } static class pair { long x, y; pair(long ar, long ar2) { x = ar; y = ar2; } } static class pairChar { Character key; int val; pairChar(Character key, int val) { this.key = key; this.val = val; } } static int[] arrI(int N) throws Exception { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = scanInt(); } return A; } static long[] arrL(int N) throws Exception { long A[] = new long[N]; for (int i = 0; i < A.length; i++) A[i] = scanLong(); return A; } static void sort(long[] a) // check for long { 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); } static void sortReverse(long[] a) // check for long { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); int n = a.length; for (int i = 0; i < n; i++) a[n - i - 1] = l.get(i); } 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 void sortReverse(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); int n = a.length; for (int i = 0; i < n; i++) a[n - i - 1] = l.get(i); } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static class DescendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return b - a; } } static class AscendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return a - b; } } static boolean isPalindrome(char X[]) { int l = 0, r = X.length - 1; while (l <= r) { if (X[l] != X[r]) return false; l++; r--; } return true; } static long fact(long N) { long num = 1L; while (N >= 1) { num = ((num % mod) * (N % mod)) % mod; N--; } return num; } static long pow(long a, long b) { long mod = 1000000007; long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) pow = (pow * x) % mod; x = (x * x) % mod; b /= 2; } return pow; } static long toggleBits(long x)// one's complement || Toggle bits { int n = (int) (Math.floor(Math.log(x) / Math.log(2))) + 1; return ((1 << n) - 1) ^ x; } static int countBits(long a) { return (int) (Math.log(a) / Math.log(2) + 1); } static boolean isPrime(long N) { if (N <= 1) return false; if (N <= 3) return true; if (N % 2 == 0 || N % 3 == 0) return false; for (int i = 5; i * i <= N; i = i + 6) if (N % i == 0 || N % (i + 2) == 0) return false; return true; } static long GCD(long a, long b) { if (b == 0) { return a; } else return GCD(b, a % b); } static HashMap<Integer, Integer> getHashMap(int A[]) { HashMap<Integer, Integer> mp = new HashMap<>(); for (int a : A) { int f = mp.getOrDefault(a, 0) + 1; mp.put(a, f); } return mp; } public static Map<Character, Integer> mapSortByValue(Map<Character, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Character, Integer>> list = new LinkedList<Map.Entry<Character, Integer>>(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() { public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) { return o1.getValue() - o2.getValue(); } }); // put data from sorted list to hashmap Map<Character, Integer> temp = new LinkedHashMap<Character, Integer>(); for (Map.Entry<Character, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } public static Map<Character, Integer> mapSortByKey(Map<Character, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Character, Integer>> list = new LinkedList<Map.Entry<Character, Integer>>(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() { public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) { return o1.getKey() - o2.getKey(); } }); // put data from sorted list to hashmap Map<Character, Integer> temp = new LinkedHashMap<Character, Integer>(); for (Map.Entry<Character, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static int scanInt() throws IOException { return parseInt(scanString()); } static long scanLong() throws IOException { return parseLong(scanString()); } static String scanString() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; public static void main(String[] args) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
9a71223a513a4db8d406cb27d40f2c69
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.InterruptedIOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Scanner; import javax.swing.plaf.synth.SynthOptionPaneUI; public class Main { private static long gcd(long a, long b) { while (b > 0) { long temp = b; b = a % b; // % is remainder a = temp; } return a; } private static long lcm(long a, long b) { return a * (b / gcd(a, b)); } static long binpow(long a,long b) { long M=1000000007; long res=1; while(b>0) { if(b%2==1) { res=(res*a)%M; } a=(a*a)%M; b/=2; } return res; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc1=new Scanner(System.in); int t=sc1.nextInt(); while(t-->0) { int n=sc1.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) { arr[i]=sc1.nextLong(); } int i=0; long mul=1; while(i<n) { long v=arr[i]; while(v%2==0) { v=v/2; mul*=2; } arr[i]=v; i++; } Arrays.sort(arr); arr[n-1]*=mul; long sum=0; for(int j=0;j<n;j++) { sum+=arr[j]; } System.out.println(sum); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
f90446149adc5be5133a7995271fc3a9
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.util.*; public class Checking { static FastScanner fs; static FastWriter fw; static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null; private static final int[][] kdir = new int[][]{{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}}; private static final int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; private static final int iMax = (int) (1e9 + 100), iMin = (int) (-1e9 - 100); private static final long lMax = (long) (1e18 + 100), lMin = (long) (-1e18 - 100); private static final int mod1 = (int) (1e9 + 7); private static final int mod2 = 998244353; public static void main(String[] args) throws IOException { fs = new FastScanner(); fw = new FastWriter(); int t = 1; t = fs.nextInt(); while (t-- > 0) { solve(); } fw.out.close(); } private static void solve() { int n = fs.nextInt(); long[] arr = new long[(int)n]; for(int i = 0; i<n;i++){ arr[i] = fs.nextLong(); } long mul = 0; for(int i = 0; i<n; i++){ while (arr[i] %2 == 0){ mul++; arr[i] = arr[i]/2; } } Arrays.sort(arr); long last = arr[n-1]; last = last * (long)Math.pow(2, mul); long sum = 0; for(int i = 0; i<n-1; i++){ sum+= arr[i]; } sum+= last; fw.out.println(sum); } private static class UnionFind { private final int[] parent; private final int[] rank; UnionFind(int n) { parent = new int[n + 5]; rank = new int[n + 5]; for (int i = 0; i <= n; i++) { parent[i] = i; rank[i] = 0; } } private int find(int i) { if (parent[i] == i) return i; return parent[i] = find(parent[i]); } private void union(int a, int b) { a = find(a); b = find(b); if (a != b) { if (rank[a] < rank[b]) { int temp = a; a = b; b = temp; } parent[b] = a; if (rank[a] == rank[b]) rank[a]++; } } } // private static class Calc_nCr { // private final long[] fact; // private final long[] invfact; // private final int p; // // Calc_nCr(int n, int prime) { // fact = new long[n + 5]; // invfact = new long[n + 5]; // p = prime; // // fact[0] = 1; // for (int i = 1; i <= n; i++) { // fact[i] = (i * fact[i - 1]) ; // } // // invfact[n] = pow(fact[n], p - 2); // for (int i = n - 1; i >= 0; i--) { // invfact[i] = (invfact[i + 1] * (i + 1)) ; // } // } // // private long nCr(int n, int r) { // if (r > n || n < 0 || r < 0) return 0; // return (((fact[n] * invfact[r]) % p) * invfact[n - r]) % p; // } // } private static long gcd(long a, long b) { return (b == 0 ? a : gcd(b, a % b)); } private static long lcm(long a, long b) { return ((a * b) / gcd(a, b)); } private static long pow(long a, long b) { long result = 1; while (b > 0) { if ((b & 1L) == 1) { result = (result * a) ; } a = (a * a) ; b >>= 1; } return result; } private static long ceilDiv(long a, long b) { return ((a + b - 1) / b); } private static long getMin(long... args) { long min = lMax; for (long arg : args) min = Math.min(min, arg); return min; } private static long getMax(long... args) { long max = lMin; for (long arg : args) max = Math.max(max, arg); return max; } private static boolean isPalindrome(String s, int l, int r) { int i = l, j = r; while (j - i >= 1) { if (s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } private static List<Integer> primes(int n) { boolean[] primeArr = new boolean[n + 5]; Arrays.fill(primeArr, true); for (int i = 2; (i * i) <= n; i++) { if (primeArr[i]) { for (int j = i * i; j <= n; j += i) { primeArr[j] = false; } } } List<Integer> primeList = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (primeArr[i]) primeList.add(i); } return primeList; } private static int noOfSetBits(long x) { int cnt = 0; while (x != 0) { x = x & (x - 1); cnt++; } return cnt; } private static boolean isPerfectSquare(long num) { long sqrt = (long) Math.sqrt(num); return ((sqrt * sqrt) == num); } private static class Pair<U, V> { private final U first; private final V second; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return first.equals(pair.first) && second.equals(pair.second); } @Override public int hashCode() { return Objects.hash(first, second); } @Override public String toString() { return "(" + first + ", " + second + ")"; } private Pair(U ff, V ss) { this.first = ff; this.second = ss; } } private static void randomizeIntArr(int[] arr, int n) { Random r = new Random(); for (int i = (n - 1); i > 0; i--) { int j = r.nextInt(i + 1); swapInIntArr(arr, i, j); } } private static void randomizeLongArr(long[] arr, int n) { Random r = new Random(); for (int i = (n - 1); i > 0; i--) { int j = r.nextInt(i + 1); swapInLongArr(arr, i, j); } } private static void swapInIntArr(int[] arr, int a, int b) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } private static void swapInLongArr(long[] arr, int a, int b) { long temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } private static int[] readIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = fs.nextInt(); return arr; } private static long[] readLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = fs.nextLong(); return arr; } private static List<Integer> readIntList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(fs.nextInt()); return list; } private static List<Long> readLongList(int n) { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(fs.nextLong()); return list; } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws IOException { if (checkOnlineJudge) this.br = new BufferedReader(new FileReader("src/input.txt")); else this.br = new BufferedReader(new InputStreamReader(System.in)); this.st = new StringTokenizer(""); } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException err) { err.printStackTrace(); } } return st.nextToken(); } public String nextLine() { if (st.hasMoreTokens()) { return st.nextToken("").trim(); } try { return br.readLine().trim(); } catch (IOException err) { err.printStackTrace(); } return ""; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } private static class FastWriter { PrintWriter out; FastWriter() throws IOException { if (checkOnlineJudge) out = new PrintWriter(new FileWriter("src/output.txt")); else out = new PrintWriter(System.out); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
62751c7cfd28a5a87b768ff9c3494374
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; public class DividedandMultiply { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int arr[]=new int[n]; long count=0,sum=0; long mul=1; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); while(arr[i]%2==0) { arr[i]=arr[i]/2; count++; } } Arrays.sort(arr); long last=arr[n-1]; for(int i=0;i<n-1;i++) { sum=sum+arr[i]; } long ans=((long)Math.pow(2,count)*last)+sum; System.out.println(ans); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
ed26016b6858798135b2272e6fab9918
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.util.*; public class Main { private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private FastWriter wr; private Reader rd; public final int MOD = 1000000007; /************************************************** FAST INPUT IMPLEMENTATION *********************************************/ class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader( new InputStreamReader(System.in)); } public Reader(String path) throws FileNotFoundException { br = new BufferedReader( new InputStreamReader(new FileInputStream(path))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public int ni() throws IOException { return rd.nextInt(); } public long nl() throws IOException { return rd.nextLong(); } public void yOrn(boolean flag){ if(flag){ wr.println("YES"); }else { wr.println("NO"); } } char nc() throws IOException { return rd.next().charAt(0); } public String ns() throws IOException { return rd.nextLine(); } public Double nd() throws IOException { return rd.nextDouble(); } public int[] nai(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } public long[] nal(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nl(); } return arr; } /************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/ public static class FastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter() { out = null; } public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastWriter write(char c) { return write((byte) c); } public FastWriter write(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if (x == Integer.MIN_VALUE) { return write((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if (x < 0) { write('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } write((long) x).write("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; write((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastWriter writeln(char c) { return write(c).writeln(); } public FastWriter writeln(int x) { return write(x).writeln(); } public FastWriter writeln(long x) { return write(x).writeln(); } public FastWriter writeln(double x, int precision) { return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for (int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for (long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte) '\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for (char[] line : map) write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c) { return writeln(c); } public FastWriter println(int x) { return writeln(x); } public FastWriter println(long x) { return writeln(x); } public FastWriter println(double x, int precision) { return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } /********************************************************* USEFUL CODE **************************************************/ boolean[] SAPrimeGenerator(int n) { // TC-N*LOG(LOG N) //Create Prime Marking Array and fill it with true value boolean[] primeMarker = new boolean[n + 1]; Arrays.fill(primeMarker, true); primeMarker[0] = false; primeMarker[1] = false; for (int i = 2; i <= n; i++) { if (primeMarker[i]) { // we start from 2*i because i*1 must be prime for (int j = 2 * i; j <= n; j += i) { primeMarker[j] = false; } } } return primeMarker; } private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } class Pair<F, S> { private F first; private S second; Pair(F first, S second) { this.first = first; this.second = second; } public F getFirst() { return first; } public S getSecond() { return second; } @Override public String toString() { return "Pair{" + "first=" + first + ", second=" + second + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<F, S> pair = (Pair<F, S>) o; return first == pair.first && second == pair.second; } @Override public int hashCode() { return Objects.hash(first, second); } } class PairThree<F, S, X> { private F first; private S second; private X third; PairThree(F first, S second,X third) { this.first = first; this.second = second; this.third=third; } public F getFirst() { return first; } public void setFirst(F first) { this.first = first; } public S getSecond() { return second; } public void setSecond(S second) { this.second = second; } public X getThird() { return third; } public void setThird(X third) { this.third = third; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PairThree<?, ?, ?> pair = (PairThree<?, ?, ?>) o; return first.equals(pair.first) && second.equals(pair.second) && third.equals(pair.third); } @Override public int hashCode() { return Objects.hash(first, second, third); } } public static void main(String[] args) throws IOException { new Main().run(); } public void run() throws IOException { if (oj) { rd = new Reader(); wr = new FastWriter(System.out); } else { File input = new File("input.txt"); File output = new File("output.txt"); if (input.exists() && output.exists()) { rd = new Reader(input.getPath()); wr = new FastWriter(output.getPath()); } else { rd = new Reader(); wr = new FastWriter(System.out); oj = true; } } long s = System.currentTimeMillis(); solve(); wr.flush(); tr(System.currentTimeMillis() - s + "ms"); } /*************************************************************************************************************************** *********************************************************** MAIN CODE ****************************************************** ****************************************************************************************************************************/ boolean[] sieve; public void solve() throws IOException { int t = 1; t = ni(); while (t-- > 0) { go(); } } /********************************************************* MAIN LOGIC HERE ****************************************************/ long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int lower_bound(long array[], long key) { int low = 0, high = array.length; int mid; while (low < high) { mid = low + (high - low) / 2; if (key <= array[mid]) { high = mid; } else { low = mid + 1; } } if (low < array.length && array[low] < key) { low++; } return low; } public void go() throws IOException { int n=ni(); int[] arr=nai(n); int counter=0; int max=Integer.MIN_VALUE; int max_index=0; for(int i=0;i<n;i++){ int temp=arr[i]; while (temp%2==0 && temp!=0){ counter++; temp/=2; } arr[i]=temp; if(max<temp){ max_index=i; max=temp; } } long ans=0; for(int i=0;i<n;i++){ int temp=arr[i]; if(i==max_index){ ans+=(long)Math.pow(2,counter)*Math.max(temp,1); }else { ans+=temp; } } wr.println(ans); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
b2c1f9e1d93979bb0ea277e78b0be98e
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.io.*; public class Main { // when can't think of anything -->> // 1. In sorting questions try to think about all possibilities like sorting from start, end, middle. // 2. Two pointers, brute force. // 3. In graph query questions try to solve it reversely or try to process all the queries in a single parse. // 4. If order does not matter then just sort the data if constraints allow. It'll never harm. // 5. In greedy problems if you are just overwhelmed by the possibilities and stuck, try to code whatever you come up with. // 6. Think like a robot, just consider all the possibilities not probabilities if you still can't solve. // 7. Try to solve it from back or reversely. public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter writer = new PrintWriter(System.out); 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(); } long ans = 0; for(int i = 0; i < n; i++)ans = Math.max(ans, calc(arr,i)); writer.println(ans); } writer.flush(); writer.close(); } private static long calc(int arr[], int j) { long ans = arr[j]; int n = arr.length; int arr2[] = Arrays.copyOf(arr, n); for(int i = 0; i < n; i++) { if(i==j)continue; while(arr2[i]%2==0) { ans*=2; arr2[i]/=2; } } for(int i = 0; i < n; i++) { if(i==j)continue; ans+=arr2[i]; } return ans; } private static boolean isPrime(int c) { for (int i = 2; i*i <= c; i++) { if(c%i==0)return false; } return true; } private static int find(int a , int arr[]) { if(arr[a] != a) return arr[a] = find(arr[a], arr); return arr[a]; } private static void union(int a, int b, int arr[]) { int aa = find(a,arr); int bb = find(b, arr); arr[aa] = bb; } private static int gcd(int a, int b) { if(a==0)return b; return gcd(b%a, a); } public static int[] readIntArray(int size, FastReader s) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = s.nextInt(); } return array; } 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; } } } class Pair implements Comparable<Pair>{ int a; int b; Pair(int a, int b){ this.a = a; this.b = b; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null || obj.getClass()!= this.getClass()) return false; Pair pair = (Pair) obj; return (pair.a == this.a && pair.b == this.b); } @Override public int hashCode() { return Objects.hash(a,b); } @Override public int compareTo(Pair o) { if(this.a == o.a) { return Integer.compare(this.b, o.b); }else { return Integer.compare(this.a, o.a); } } @Override public String toString() { return this.a + " " + this.b; } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
c08af4b49f987ecec0d23ebd0e57f42e
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.DataInputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.*; import java.util.function.Consumer; import java.util.stream.Collectors; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.Math.pow; import java.math.*; public class DivideAndMultiply { @SuppressWarnings("FieldCanBeLocal") private static Reader in; private static PrintWriter out; private static String YES = "YES"; private static String NO = "NO"; private static void solve() throws IOException{ //Your Code Goes Here; long n = in.nextLong(); long[] arr = new long[(int) n]; long count = 0, maximum = 0, sum = 0; for(int i = 0; i < n; i++){ arr[i] = in.nextLong(); while(arr[i] % 2 == 0){ count++; arr[i] /= 2; } maximum = max(maximum, arr[i]); sum += arr[i]; } sum -= maximum; long total = (long) (sum + maximum * pow(2, count)); System.out.println(total); } public static void main(String[] args) throws IOException, InterruptedException { in = new Reader(); out = new PrintWriter(new OutputStreamWriter(System.out)); int T = in.nextInt(); while(T --> 0){ solve(); } out.flush(); in.close(); out.close(); } static final Random random = new Random(); static final int mod = 1_000_000_007; private static boolean check(long[] arr, long n){ long ch = arr[0]; for(int i = 0; i < n; i++){ if(ch != arr[i]){ return false; } } return true; } //This function gives the max occuring of any element in an array;(HashMap version) private static long maxFreqHashMap(long[] arr, long n){ Map<Long, Long> hp = new HashMap<Long, Long>(); for(int i = 0; i < n; i++) { long key = arr[i]; if(hp.containsKey(key)) { long freq = hp.get(key); freq++; hp.put(key, freq); } else { hp.put(key, 1L); } } long max_count = 0, res = 1, count = 0; for(Map.Entry<Long, Long> val : hp.entrySet()) { if (max_count < val.getValue()) { res = Math.toIntExact(val.getKey()); max_count = Math.toIntExact(val.getValue()); count = max_count; } } return count; } //This function gives the max occuring of any element in an array; this also known as the "MOORE's Algorithm" private static long maxFreq(long []arr, long n) { // using moore's voting algorithm long res = 0; long count = -1; for(int i = 1; i < n; i++) { if(arr[i] == arr[(int) res]) { count++; } else { count--; } if(count == 0) { res = i; count = 1; } } return arr[(int) res]; /* you've to add below code in the solve() long freq = maxFreq(arr, n); int count = 0; for(int i = 0; i < n; i++){ if(arr[i] == freq){ count++; } } */ } private static int LCSubStr(char[] X, char[] Y, int m, int n) { int[][] LCStuff = new int[m + 1][n + 1]; int result = 0; for (int i = 0; i <= m; i++){ for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) { LCStuff[i][j] = 0; } else if (X[i - 1] == Y[j - 1]) { LCStuff[i][j] = LCStuff[i - 1][j - 1] + 1; result = Integer.max(result, LCStuff[i][j]); } else { LCStuff[i][j] = 0; } } } return result; } private static long longCusBsearch(long[] arr, long n, long h){ long ans = h; long l = 1; long r = h; while(l <= r){ long mid = (l + r) / 2; long ok = 0; for(long i = 0; i < n; i++){ if(i == n - 1) { ok += mid; } else{ long x = arr[(int) (i + 1)] - arr[(int) i]; if(x >= mid) { ok += mid; } else{ ok += x; } } } if(ok >= h){ ans = mid; r = mid - 1; } else{ l = mid + 1; } } return ans; } public static int intCusBsearch(int[] arr, int n, int h){ int ans = h, l = 1, r = h; while(l <= r){ int mid = (l + r) / 2; int ok = 0; for(int i = 0; i < n; i++){ if(i == n - 1){ ok += mid; } else{ int x = arr[i + 1] - arr[i]; if(x >= mid){ ok += mid; } else{ ok += x; } } } if(ok >= h){ ans = mid; r = mid - 1; } else{ l = mid + 1; } } return ans; } /* Method to check if x is power of 2*/ private static boolean isPowerOfTwo (int x) { /* First x in the below expression is for the case when x is 0 */ return x!=0 && ((x&(x-1)) == 0); } //Taken From "Second Thread" static void ruffleSort(int[] a){ int n = a.length;//shuffles, then sort; for(int i=0; i<n; i++){ int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } sort(a); } private static void longRuffleSort(long[] a){ long n = a.length; for(long i = 0;i<n;i++){ long oi = random.nextInt((int) n), temp = a[(int) oi]; a[(int) oi] = a[(int) i]; a[(int) i] = temp; } longSort(a); } private static int gcd(int a, int b){ if(a == 0 || b == 0){ return 0; } while(b != 0){ int temp; temp = a % b; a = b; b = temp; } return a; } private static long gcd(long a, long b){ if(a == 0 || b == 0){ return 0; } while(b != 0){ long temp; temp = a % b; a = b; b = temp; } return a; } private static int lowestCommonMultiple(int a, int b){ return (a / gcd(a, b) * b); } /* The Below func: the for loop runs in sqrt times. The second if is used to if the divisors are equal to print only one of them otherwise we're printing both; */ private static void pDivisors(int n){ for(int i=1;i<Math.sqrt(n);i++){ if(n % i == 0){ if(n / i == i){ System.out.println(i + " "); } else{ System.out.println(i + " " + (n / i) + " "); } } } } private static void returnNothing(){return;} private static int numOfdigitsinN(int a){return (int) (Math.floor(Math.log10(a)) + 1);} //prime Num till N: it takes any number and prints all the prime till that //num; private static boolean isPrime(int n){ if(n <= 1) return false; if(n <= 3) return true; //This is checked so that we can skip middle five number in below loop: if(n % 2 == 0 || n % 3 == 0) return false; for(int i = 5; i * i <= n; i = i + 6){ if(n % i == 0 || n % (i + 2) == 0){ return false; } } return true; } //below func is to print the isPrime func(); private static void printTheIsPrimeFunc(int n){ for(int i=2;i<=n;i++){ if(isPrime(i)) System.out.println(i + " "); } } public static boolean primeFinder(int n){ int m = 0; int flag = 0; m = n / 2; if(n == 0 ||n == 1){ return false; } else{ for(int i=2;i<=m;i++){ if(n % i == 0){ return false; } } return true; } } private static boolean evenOdd(int num){ //System.out.println((num & 1) == 0 ? "EVEN" : "ODD"); return (num & 1) == 0 ? true : false; } private static boolean[] sieveOfEratosthenes(long n) { boolean prime[] = new boolean[(int)n + 1]; for (int i = 0; i <= n; i++) { prime[i] = true; } for (long p = 2; p * p <= n; p++) { if (prime[(int)p] == true) { for (long i = p * p; i <= n; i += p) prime[(int)i] = false; } } return prime; } private static int log2(int n){ int result = (int) (Math.log(n) / Math.log(2)); return result; } private static long add(long a, long b){ return (a + b) % mod; } private static long lcm(long a, long b){ return (a / gcd((int) a, (int) b) * b); } private 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); } private static void longSort(long[] a){ ArrayList<Long> l = new ArrayList<Long>(); for(long i:a) l.add(i); Collections.sort(l); for(int i=0;i<a.length;i++)a[i] = l.get(i); } public static int[][] prefixsum( int n , int m , int arr[][] ){ int prefixsum[][] = new int[n+1][m+1]; for( int i = 1 ;i <= n ;i++) { for( int j = 1 ; j<= m ; j++) { int toadd = 0; if( arr[i-1][j-1] == 1) { toadd = 1; } prefixsum[i][j] = toadd + prefixsum[i][j-1] + prefixsum[i-1][j] - prefixsum[i-1][j-1]; } } return prefixsum; } //call this method when you want to read an integer array; private static int[] readArray(int len) throws IOException{ int[] a = new int[len]; for(int i=0;i<len;i++)a[i] = in.nextInt(); return a; } //call this method when you want to read an Long array; private static long[] readLongArray(int len) throws IOException{ long[] a = new long[len]; for(int i=0;i<len;i++) a[i] = in.nextLong(); return a; } //call this method to print the integer array; private static void printArray(int[] array){ for(int now : array) out.print(now);out.print(' ');out.close(); } //call this method to print the long array; private static void printLongArray(long[] array){ for(long now:array) out.print(now); out.print(' '); out.close(); } /*Another way of Reading input...collected from a online blog from here => https://codeforces.com/contest/1625/submission/144334744;*/ private static class Reader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; Reader() {//Constructor; din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException {//To take user input for String values; final byte[] buf = new byte[1024]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextSign() throws IOException {//For taking the signs like plus or minus ... byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() throws IOException { int b; // noinspection ALL while ((b = read()) != -1 && isSpaceChar(b)) { ; } return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } /* A class representing a Mutable Multiset (Collectied From TechieDelight)*/ private static class Multiset<E> { /* List to store distinct values */ private List<E> values; /* List to store counts of distinct values */ private List<Integer> frequency; private final String ERROR_MSG = "Count cannot be negative: "; /* Constructor */ public Multiset() { values = new ArrayList<>(); frequency = new ArrayList<>(); } /** * Adds an element to this multiset specified number of times * * @param `element` The element to be added * @param `count` The number of times * @return The previous count of the element */ public int add(E element, int count) { if (count < 0) { throw new IllegalArgumentException(ERROR_MSG + count); } int index = values.indexOf(element); int prevCount = 0; if (index != -1) { prevCount = frequency.get(index); frequency.set(index, prevCount + count); } else if (count != 0) { values.add(element); frequency.add(count); } return prevCount; } /** * Adds specified element to this multiset * * @param `element` The element to be added * @return true always */ public boolean add(E element) { return add(element, 1) >= 0; } /** * Adds all elements in the specified collection to this multiset * * @param `c` Collection containing elements to be added * @return true if all elements are added to this multiset */ boolean addAll(Collection<? extends E> c) { for (E element: c) { add(element, 1); } return true; } /** * Adds all elements in the specified array to this multiset * * @param `arr` An array containing elements to be added */ public void addAll(E... arr) { for (E element: arr) { add(element, 1); } } /** * Performs the given action for each element of the Iterable, * including duplicates * * @param `action` The action to be performed for each element */ public void forEach(Consumer<? super E> action) { List<E> all = new ArrayList<>(); for (int i = 0; i < values.size(); i++) { for (int j = 0; j < frequency.get(i); j++) { all.add(values.get(i)); } all.forEach(action); } } /** * Removes a single occurrence of the specified element from this multiset * * @param `element` The element to removed * @return true if an occurrence was found and removed */ public boolean remove(Object element) { return remove(element, 1) > 0; } /** * Removes a specified number of occurrences of the specified element * from this multiset * * @param `element` The element to removed * @param `count` The number of occurrences to be removed * @return The previous count */ public int remove(Object element, int count) { if (count < 0) { throw new IllegalArgumentException(ERROR_MSG + count); } int index = values.indexOf(element); if (index == -1) { return 0; } int prevCount = frequency.get(index); if (prevCount > count) { frequency.set(index, prevCount - count); } else { values.remove(index); frequency.remove(index); } return prevCount; } /** * Check if this multiset contains at least one occurrence of the * specified element * * @param `element` The element to be checked * @return true if this multiset contains at least one occurrence * of the element */ public boolean contains(Object element) { return values.contains(element); } /** * Check if this multiset contains at least one occurrence of each element * in the specified collection * * @param `c` The collection of elements to be checked * @return true if this multiset contains at least one occurrence * of each element */ public boolean containsAll(Collection<?> c) { return values.containsAll(c); } /** * Update the frequency of an element to the specified count or * add element to this multiset if not present * * @param `element` The element to be updated * @param `count` The new count * @return The previous count */ public int setCount(E element, int count) { if (count < 0) { throw new IllegalArgumentException(ERROR_MSG + count); } if (count == 0) { remove(element); } int index = values.indexOf(element); if (index == -1) { return add(element, count); } int prevCount = frequency.get(index); frequency.set(index, count); return prevCount; } /** * Find the frequency of an element in this multiset * * @param `element` The element to be counted * @return The frequency of the element */ public int count(Object element) { int index = values.indexOf(element); return (index == -1) ? 0 : frequency.get(index); } /** * @return A view of the set of distinct elements in this multiset */ public Set<E> elementSet() { return values.stream().collect(Collectors.toSet()); } /** * @return true if this multiset is empty */ public boolean isEmpty() { return values.size() == 0; } /** * @return Total number of elements in this multiset, including duplicates */ public int size() { int size = 0; for (Integer i: frequency) { size += i; } return size; } @Override public String toString() { StringBuilder sb = new StringBuilder("["); for (int i = 0; i < values.size(); i++) { sb.append(values.get(i)); if (frequency.get(i) > 1) { sb.append(" x ").append(frequency.get(i)); } if (i != values.size() - 1) { sb.append(", "); } } return sb.append("]").toString(); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
a703e52ce71d5765901c3149bf17a732
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces { 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){ try { FastReader fastReader = new FastReader(); int T = fastReader.nextInt(); while (T-- > 0) { int n = fastReader.nextInt(); long a[] = new long[n]; for(int i=0; i<n; i++){ a[i] = fastReader.nextInt(); } int count = 0; for(int i=0; i<n; i++){ if(a[i]%2==0){ while(a[i]%2 == 0){ count++; a[i] /= 2; } } } Arrays.sort(a); a[n-1] *= Math.pow(2, count); long sum = 0; for(int i=0; i<n; i++){ sum+= a[i]; } System.out.println(sum); } } catch (Exception e){ return; } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
fc379f2bf0c8e60c0cfdc35dbcbd9ca1
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; public class practice { public static void main(String[] args) { Scanner scan = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int t = scan.nextInt(); while (t --> 0) { int n = scan.nextInt(); int arr[] = new int[n]; int k = 0; for (int i = 0; i < n; i++) { arr[i] = scan.nextInt(); while ((arr[i] & 1) == 0) { arr[i] /= 2; k++; } } long max = Arrays.stream(arr).max().getAsInt(); long sum = Arrays.stream(arr).sum() - max; sb.append((max * (long) Math.pow(2, k) + sum) + "\n"); } System.out.println(sb.toString().trim()); scan.close(); } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
a0387adaa81bd805842221e3bbcb7f44
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int p,q; public static void main(String[] args) { FastReader sc = new FastReader(); int t=sc.nextInt(); while(t--!=0) { int n=sc.nextInt(); long a[]=new long[n]; long sum=0,max=0; long ans=0; for(int i=0;i<n;i++) { a[i]=sc.nextLong(); while(a[i]%2==0) { a[i]/=2; sum++; } max=Math.max(max,a[i]); ans+=a[i]; } ans-=max; ans+=max*(long)Math.pow(2,sum); System.out.println(ans); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
98f3173642d06803a0fda6911c64da27
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner lec = new Scanner(System.in); int casoPrueba=lec.nextInt(); int mayorImpar=0; int contador=0; int tamanoArreglo=0; for (int i=0; i<casoPrueba; i++) { mayorImpar=0; contador=0; tamanoArreglo = lec.nextInt(); long [] arreglo = new long [tamanoArreglo]; for (int j=0; j<tamanoArreglo; j++) { arreglo[j]=lec.nextInt(); } for (int h=0; h<tamanoArreglo; h++) { while(arreglo[h]%2==0) { arreglo[h]=arreglo[h]/2; contador++; } if (arreglo[h]>arreglo[mayorImpar]) { mayorImpar=h; } } if (mayorImpar!=-1) { for (int k=0; k<contador; k++) { arreglo[mayorImpar]=arreglo[mayorImpar]*2; } } else { for (int k=0; k<contador; k++) { arreglo[0]=arreglo[0]*2; } } long total=0; for (int q=0; q < tamanoArreglo;q++) { total=arreglo[q]+total; } System.out.println(total); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
aa1236ed66f48dc7c3cba214eb228112
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; public class solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } int i=0; long count=0;long res=0; for(i=0;i<n;i++) { while(arr[i]%2==0) {count++; arr[i]=arr[i]/2; } } Arrays.sort(arr); for(int k=0;k<n-1;k++) { res=res+arr[k]; } res+= arr[n-1]*(long)Math.pow(2,count) ; System.out.println(res); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
a0fd9dda5c67b40739afd390d8e9b83b
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int t=in.nextInt(); for(int i=1;i<=t;i++) { long ans=0; int x=in.nextInt(); long[] a=new long[x]; for(int j=0;j<x;j++) { a[j]=in.nextInt(); } int time=0; Arrays.sort(a); for(int j=0;j<=x-1;j++) { while(a[j]%2==0) { a[j]/=2; time++; } } Arrays.sort(a); for(int j=0;j<x-1;j++) { ans+=a[j]; } a[x-1]=(long) (a[x-1]* Math.pow(2,time)); ans+=a[x-1]; System.out.println(ans); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
a6f6e8ccc579045b237144f0d9e52608
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class DivideandMultiply { static final int MOD = (int) 1e9 + 7; public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); while(t-- >0){ int n=fs.nextInt(); long []a=new long[n]; long s=1; for(int i=0;i<n;i++) a[i]=fs.nextLong(); for(int i=0;i<n;i++){ while(a[i]%2==0){ a[i]/=2; s*=2; } } Arrays.sort(a); a[n-1]*=s;s=0; for(int i=0;i<n;i++) s+=a[i]; out.println(s); } out.close(); } 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[] readArray(int n) {int[] a=new int[n];for (int i=0; i<n; i++) a[i]=nextInt();return a; } long[] readLongArray(int n) {long[] a=new long[n];for (int i=0; i<n; i++) a[i]=nextLong();return a; } int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
0787732b98068a8126896202828ba4e6
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.Math.log; import static java.lang.System.out; import static java.lang.Integer.MAX_VALUE; import static java.lang.Integer.MIN_VALUE; public class Codechef { static class Pair{ int pop; int special; String post; Pair(int pop,int spe,String post){ this.pop = pop; this.special = spe; this.post = post; } } static int garr[] ; static FastReader fs; static boolean isPrimes[]; static int countprimes[]; static int max_seive = 1000000; public static void main(String args[]) throws Exception{ try{ System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch(Exception e){ } fs =new FastReader(); int tt = 1; tt = fs.nextInt(); while(tt--!=0) solve(); } public static void solve(){ int n = fs.nextInt(); int arr[]= new int[n]; int max_odd = -1; int max_even = -1; int max_even_ind =-1,max_odd_ind=-1; int k = 0; long ans = 0; long maxi =0; for(int i =0;i<n;i++){ int yo = fs.nextInt(); while(yo%2==0){ yo/=2; k++; } maxi = Math.max(maxi,yo); ans+=yo; } ans-=maxi; out.println(ans+maxi*(long)Math.pow(2,k)); } public static boolean isPallindrome(String val){ StringBuilder sb =new StringBuilder(val); return val.equals(sb.reverse().toString()); } public static void seive(){ countprimes = new int[max_seive]; isPrimes = new boolean[max_seive]; Arrays.fill(isPrimes,true); isPrimes[0] = false; isPrimes[1] = false; for(int i =2;i*i<=max_seive;i++){ if(isPrimes[i]){ for(int j =i*i;j<max_seive;j+=i){ isPrimes[j] = false; } } } int count =0; for(int i = 2;i<max_seive;i++){ if(isPrimes[i]){ count++; } countprimes[i] = count; } } static int gcd(int a,int b){ return (b==0)?a: gcd(b,a%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; } } static int[] readArray(int n){ int arr[]= new int[n]; for(int i=0;i<arr.length;i++){ arr[i] = fs.nextInt(); } return arr; } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
597fc26b9eaa69c82c24a013804ec9ec
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
import java.util.Scanner; public class DivideAndMultiply { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int i = 0; while (i < t) { int length = sc.nextInt(); int[] a = new int[length]; for (int j = 0; j < length; j++) { a[j] = sc.nextInt(); } long temp = 1; for (int k = 0; k < length; k++){ while (a[k] % 2 == 0) { a[k] /= 2; temp *= 2; } } Sort(a, 0, a.length - 1); int maxSum = 0; long lastNo = a[length - 1]; lastNo *= temp; for (int p = 0; p < length - 1; p++) { maxSum += a[p]; } System.out.println(maxSum + lastNo); i++; } } public static void Merge (int[] a, int l, int m, int h) { int n1 = m - l + 1; int n2 = h - m; int[] L = new int[n1]; int[] R = new int[n2]; for (int i = 0; i < n1; i++) { L[i] = a[i + l]; } for (int j = 0; j < n2; j++) { R[j] = a[j + m + 1]; } int i = 0; int j = 0; int k = l; while (i < L.length && j < R.length) { if (L[i] < R[j]) { a[k++] = L[i++]; } else { a[k++] = R[j++]; } } for (; i < L.length; i++) { a[k++] = L[i]; } for(; j < R.length; j++) { a[k++] = R[j]; } } public static void Sort (int[] a, int l, int h) { if (l < h) { int mid = (l + h) / 2; Sort(a, l, mid); Sort(a, mid + 1, h); Merge(a, l, mid, h); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output
PASSED
ee577aa11d6179613f80f8548589bc63
train_110.jsonl
1638110100
William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
256 megabytes
//package Contest2; import java.io.*; import java.util.*; public class DivideAndMultiply { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int cases = scan.nextInt(); for (int t = 0; t < cases; t++) { int len = scan.nextInt(); long[] arr = new long[len]; for (int i = 0; i < len; i++) arr[i] = scan.nextInt(); int power = 0; for (int i = 0; i < len; i++) { while (arr[i] % 2 == 0) { arr[i] /= 2; power++; } } int maxInd = 0; for (int i = 1; i < len; i++) if (arr[i] > arr[maxInd]) maxInd = i; while (power-->0) arr[maxInd] *= 2; long sum = 0; for (long n : arr) sum += n; System.out.println(sum); } } }
Java
["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"]
1 second
["50\n46\n10\n26\n35184372088846"]
NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.
Java 11
standard input
[ "greedy", "implementation", "math", "number theory" ]
f5de1e9b059bddf8f8dd46c18ce12683
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i &lt; 16)$$$, the contents of William's array.
900
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
standard output