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
listlengths
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
ec9e0da5eefb3218f9b8a18c18e20c52
train_001.jsonl
1484499900
It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are n Balls overall. Each Ball has someone for whom he should bring a present according to some permutation p, pi ≠ i for all i.Unfortunately, Balls are quite clumsy. We know earlier that exactly k of them will forget to bring their gift. A Ball number i will get his present if the following two constraints will hold: Ball number i will bring the present he should give. Ball x such that px = i will bring his present. What is minimum and maximum possible number of kids who will not get their present if exactly k Balls will forget theirs?
256 megabytes
import javax.print.attribute.IntegerSyntax; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.StringTokenizer; /** * Created by Meepo on 3/11/2017. */ public class F { public static void main(String[] args) { new F().run(); } BufferedReader br; StringTokenizer st; PrintWriter out; public String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public void solve() throws IOException { int n = nextInt(); int k = nextInt(); int[] p = new int[n]; for (int i = 0; i < n; i++) { p[i] = nextInt() - 1; } int max = 0; int[] cnt = new int[n + 1]; for (int i = 0; i < n; i++) { if (p[i] == -1) { continue; } int x = i; int l = 0; while (p[x] != -1) { int lx = x; x = p[x]; p[lx] = -1; l++; } cnt[l]++; max += l / 2; } max = k < max ? 2 * k : Math.min(n, k + max); int rk = k; k = Math.min(k, n - k); if (k == 0) { out.println(rk + " " + max); return; } int[] best = new int[k + 1]; Arrays.fill(best, -1); best[0] = 0; int[] time = new int[k + 1]; for (int i = 1; i < cnt.length; i++) { if (cnt[i] == 0) { continue; } for (int j = 0; j < best.length - i; j++) { if (best[j] == -1) continue; if (time[j] != i) { time[j] = i; best[j] = 0; } } for (int j = 0; j < best.length - i; j++) {//// if (best[j] == -1 ||best[j] == cnt[i]) { continue; } if (time[j + i] != i || best[j] + 1 < best[i + j]) { time[j + i] = i; best[j + i] = best[j] + 1; } } if (best[k] != -1) { out.println(rk + " " + max); return; } } out.println((rk + 1) + " " + max); } public void run() { try { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
Java
["5 2\n3 4 1 5 2", "10 1\n2 3 4 5 6 7 8 9 10 1"]
1.5 seconds
["2 4", "2 2"]
NoteIn the first sample, if the third and the first balls will forget to bring their presents, they will be th only balls not getting a present. Thus the minimum answer is 2. However, if the first ans the second balls will forget to bring their presents, then only the fifth ball will get a present. So, the maximum answer is 4.
Java 8
standard input
[ "dp", "bitmasks", "greedy" ]
a0ddb8f02a91865dc65a214f1de111e3
The first line of input contains two integers n and k (2 ≤ n ≤ 106, 0 ≤ k ≤ n), representing the number of Balls and the number of Balls who will forget to bring their presents. The second line contains the permutation p of integers from 1 to n, where pi is the index of Ball who should get a gift from the i-th Ball. For all i, pi ≠ i holds.
2,600
You should output two values — minimum and maximum possible number of Balls who will not get their presents, in that order.
standard output
PASSED
e04d4861c47e07802d20cbc56f88dc34
train_001.jsonl
1484499900
It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are n Balls overall. Each Ball has someone for whom he should bring a present according to some permutation p, pi ≠ i for all i.Unfortunately, Balls are quite clumsy. We know earlier that exactly k of them will forget to bring their gift. A Ball number i will get his present if the following two constraints will hold: Ball number i will bring the present he should give. Ball x such that px = i will bring his present. What is minimum and maximum possible number of kids who will not get their present if exactly k Balls will forget theirs?
256 megabytes
import javax.print.attribute.IntegerSyntax; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.StringTokenizer; /** * Created by Meepo on 3/11/2017. */ public class F { public static void main(String[] args) { new F().run(); } BufferedReader br; StringTokenizer st; PrintWriter out; public String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public void solve() throws IOException { int n = nextInt(); int k = nextInt(); int[] p = new int[n]; for (int i = 0; i < n; i++) { p[i] = nextInt() - 1; } int max = 0; int[] cnt = new int[n + 1]; for (int i = 0; i < n; i++) { if (p[i] == -1) { continue; } int x = i; int l = 0; while (p[x] != -1) { int lx = x; x = p[x]; p[lx] = -1; l++; } cnt[l]++; max += l / 2; } max = k < max ? 2 * k : Math.min(n, k + max); int rk = k; k = Math.min(k, n - k); if (k == 0) { out.println(rk + " " + max); return; } boolean[] can = new boolean[k + 1]; can[0] = true; int[] time = new int[k + 1]; for (int i = 1; i < cnt.length; i++) { if (cnt[i] == 0) { continue; } for (int j = 0; j < i; j++) { int now = -1; for (int pos = j; pos < can.length; pos += i) { now--; if (can[pos]) { now = cnt[i]; } else { can[pos] = now >= 0; } } } if (can[k]) { out.println(rk + " " + max); return; } } out.println((rk + 1) + " " + max); } public void run() { try { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
Java
["5 2\n3 4 1 5 2", "10 1\n2 3 4 5 6 7 8 9 10 1"]
1.5 seconds
["2 4", "2 2"]
NoteIn the first sample, if the third and the first balls will forget to bring their presents, they will be th only balls not getting a present. Thus the minimum answer is 2. However, if the first ans the second balls will forget to bring their presents, then only the fifth ball will get a present. So, the maximum answer is 4.
Java 8
standard input
[ "dp", "bitmasks", "greedy" ]
a0ddb8f02a91865dc65a214f1de111e3
The first line of input contains two integers n and k (2 ≤ n ≤ 106, 0 ≤ k ≤ n), representing the number of Balls and the number of Balls who will forget to bring their presents. The second line contains the permutation p of integers from 1 to n, where pi is the index of Ball who should get a gift from the i-th Ball. For all i, pi ≠ i holds.
2,600
You should output two values — minimum and maximum possible number of Balls who will not get their presents, in that order.
standard output
PASSED
2491c0fd9281237324cba3d5fac653e3
train_001.jsonl
1484499900
It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are n Balls overall. Each Ball has someone for whom he should bring a present according to some permutation p, pi ≠ i for all i.Unfortunately, Balls are quite clumsy. We know earlier that exactly k of them will forget to bring their gift. A Ball number i will get his present if the following two constraints will hold: Ball number i will bring the present he should give. Ball x such that px = i will bring his present. What is minimum and maximum possible number of kids who will not get their present if exactly k Balls will forget theirs?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.math.BigInteger; import java.io.BufferedReader; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskF solver = new TaskF(); solver.solve(1, in, out); out.close(); } static class TaskF { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(), k = in.nextInt(); int[] perm = new int[n]; for (int i = 0; i < n; i++) perm[i] = in.nextInt() - 1; boolean[] vis = new boolean[n]; ArrayList<Integer> cycles = new ArrayList<>(); for (int i = 0; i < n; i++) { if (!vis[i]) { int c = i; int size = 0; do { vis[c] = true; c = perm[c]; size++; } while (c != i); cycles.add(size); } } int max = 0; int tk = k; for (int d : cycles) { int take = Math.min(tk, d / 2); max += take * 2; tk -= take; } for (int d : cycles) { if (d % 2 == 1) { if (tk > 0) { max++; tk--; } } } int min = k; // check if subset of cycles equals k if (!can(cycles, Math.min(k, n - k))) min++; out.println(min + " " + max); } public boolean can(ArrayList<Integer> items, int need) { if (need == 0) return true; for (int s : items) if (s == need) return true; ArrayList<Integer> d = new ArrayList<>(); for (int s : items) if (s <= need) d.add(s); Collections.sort(d); BigInteger mask = BigInteger.ONE; mask = mask.shiftLeft(need); for (int i = 0; i < d.size(); i++) { int j = i; while (j + 1 < d.size() && d.get(j + 1) == d.get(i)) j++; int amt = j - i + 1; int nxt = 1; while (amt > 0) { int w = Math.min(amt, nxt); amt -= w; nxt *= 2; mask = mask.or(mask.shiftRight(d.get(i) * w)); } i = j; } return mask.testBit(0); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["5 2\n3 4 1 5 2", "10 1\n2 3 4 5 6 7 8 9 10 1"]
1.5 seconds
["2 4", "2 2"]
NoteIn the first sample, if the third and the first balls will forget to bring their presents, they will be th only balls not getting a present. Thus the minimum answer is 2. However, if the first ans the second balls will forget to bring their presents, then only the fifth ball will get a present. So, the maximum answer is 4.
Java 8
standard input
[ "dp", "bitmasks", "greedy" ]
a0ddb8f02a91865dc65a214f1de111e3
The first line of input contains two integers n and k (2 ≤ n ≤ 106, 0 ≤ k ≤ n), representing the number of Balls and the number of Balls who will forget to bring their presents. The second line contains the permutation p of integers from 1 to n, where pi is the index of Ball who should get a gift from the i-th Ball. For all i, pi ≠ i holds.
2,600
You should output two values — minimum and maximum possible number of Balls who will not get their presents, in that order.
standard output
PASSED
777d947638108e2c5cd0d9e5d58586da
train_001.jsonl
1484499900
It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are n Balls overall. Each Ball has someone for whom he should bring a present according to some permutation p, pi ≠ i for all i.Unfortunately, Balls are quite clumsy. We know earlier that exactly k of them will forget to bring their gift. A Ball number i will get his present if the following two constraints will hold: Ball number i will bring the present he should give. Ball x such that px = i will bring his present. What is minimum and maximum possible number of kids who will not get their present if exactly k Balls will forget theirs?
256 megabytes
import java.io.*; import java.util.*; public class F { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; int get(int mask, int i) { return (mask >> i) & 1; } int[] f(int n, int k) { int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int mask = 0; mask < (1 << n); mask++) { if (Integer.bitCount(mask) == k) { int tot = 0; for (int i = 0; i < n; i++) { tot += get(mask, i) & get(mask, (i + 1) % n); } min = Math.min(min, tot); max = Math.max(max, tot); } } // return new int[] { min, max }; return new int[] { max }; } int getMin(int[] a, int k) { int[] cnt = new int[3]; for (int x : a) { cnt[0] += x / 2; cnt[1] += x % 2; cnt[2] += x / 2; } int ret = 0; for (int i = 0;; i++) { if (cnt[i] >= k) { return ret + k * i; } ret += cnt[i] * i; k -= cnt[i]; } } int getMax(int[] a, int k) { int sum = 0; for (int x : a) { sum += x; } int[] cnt = new int[sum + 1]; for (int x : a) { cnt[x]++; } BitsetLong bs = new BitsetLong(sum); bs.set(0); long[] data = bs.data; // for (int x : a) { for (int ii = 1; ii <= sum; ii++) { if (cnt[ii] == 0) { continue; } for (int rest = cnt[ii], p2 = 1; rest > 0; p2 <<= 1) { int use = Math.min(rest, p2); int x = ii * use; for (int i = data.length - 1; (i << 6) - x > -64; i--) { data[i] |= bs.getWord((i << 6) - x); } rest -= use; } } // } if (bs.get(k) == 1) { return k; } else { return k - 1; } } void solve() throws IOException { int n = nextInt(); int k = nextInt(); k = n - k; int[] p = new int[n]; for (int i = 0; i < n; i++) { p[i] = nextInt() - 1; } int[] arr = new int[n]; int ptr = 0; boolean[] vis = new boolean[n]; for (int i = 0; i < n; i++) { if (vis[i]) { continue; } vis[i] = true; int len = 1; for (int v = p[i]; v != i; v = p[v]) { vis[v] = true; len++; } arr[ptr++] = len; } arr = Arrays.copyOf(arr, ptr); out.println(n - getMax(arr, k) + " " + (n - getMin(arr, k))); } F() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); // for (int n = 1; n <= 10; n++) { // for (int k = 0; k <= n; k++) { // System.err.println(n + " " + k + " -> " // + Arrays.toString(f(n, k))); // } // System.err.println(); // } solve(); // List<Integer> tmp = new ArrayList<>(); // for (int i = 1, rest = 1_000_000; rest > 0; i++) { // int use = Math.min(rest, i); // tmp.add(use); // rest -= use; // } // int[] arr = new int[tmp.size()]; // for (int i = 0; i < tmp.size(); i++) { // arr[i] = tmp.get(i); // } // out.println(getMax(arr, 999999)); out.close(); } static class BitsetLong { static final int LOG = 6; static final int SIZE = 1 << LOG; static final int MASK = SIZE - 1; private long[] data; public BitsetLong(int n) { data = new long[(n >> LOG) + 2]; } void set(int i) { data[i >> LOG] |= 1L << (i & MASK); } void unset(int i) { data[i >> LOG] &= ~(1L << (i & MASK)); } void flip(int i) { data[i >> LOG] ^= 1L << (i & MASK); } int get(int i) { return (int) ((data[i >> LOG] >>> (i & MASK)) & 1); } /** * pads with zeroes if end of word is outside of range */ long getWord(int i) { if (i <= -SIZE) { return 0; } if (i < 0) { return data[0] << (-i); } int rem = i & MASK; int idx = i >> LOG; if (rem == 0) { return data[idx]; } long head = data[idx] >>> rem; long tail = data[idx + 1] & ((1L << rem) - 1); return head | (tail << (SIZE - rem)); } } public static void main(String[] args) throws IOException { new F(); } String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
Java
["5 2\n3 4 1 5 2", "10 1\n2 3 4 5 6 7 8 9 10 1"]
1.5 seconds
["2 4", "2 2"]
NoteIn the first sample, if the third and the first balls will forget to bring their presents, they will be th only balls not getting a present. Thus the minimum answer is 2. However, if the first ans the second balls will forget to bring their presents, then only the fifth ball will get a present. So, the maximum answer is 4.
Java 8
standard input
[ "dp", "bitmasks", "greedy" ]
a0ddb8f02a91865dc65a214f1de111e3
The first line of input contains two integers n and k (2 ≤ n ≤ 106, 0 ≤ k ≤ n), representing the number of Balls and the number of Balls who will forget to bring their presents. The second line contains the permutation p of integers from 1 to n, where pi is the index of Ball who should get a gift from the i-th Ball. For all i, pi ≠ i holds.
2,600
You should output two values — minimum and maximum possible number of Balls who will not get their presents, in that order.
standard output
PASSED
7d6ec4fc200c4d3af49103358d4f5826
train_001.jsonl
1484499900
It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are n Balls overall. Each Ball has someone for whom he should bring a present according to some permutation p, pi ≠ i for all i.Unfortunately, Balls are quite clumsy. We know earlier that exactly k of them will forget to bring their gift. A Ball number i will get his present if the following two constraints will hold: Ball number i will bring the present he should give. Ball x such that px = i will bring his present. What is minimum and maximum possible number of kids who will not get their present if exactly k Balls will forget theirs?
256 megabytes
/* * Code Author: Akshay Miterani * DA-IICT */ import java.io.*; import java.math.*; import java.util.*; public class Main { InputReader in; PrintWriter out; Main() throws IOException { in = new InputReader(System.in); out = new PrintWriter(System.out); //in = new InputReader(new FileInputStream("input_.txt")); //out = new PrintWriter(new File("output_.txt")); solve(); out.close(); } public static void main(String args[]) { new Thread(null, new Runnable() { public void run() { try { new Main(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } //--------------------My Code Starts Here---------------------- long mod=(long)1e9+9; double eps=1e-150; public void solve() throws IOException{ int n=in.nextInt(); int k=in.nextInt(); int a[]=new int[n+1]; for(int i=1;i<=n;i++){ a[i]=in.nextInt(); } int size[]=new int[n+1]; boolean v[]=new boolean[n+1]; for(int i=1;i<=n;i++){ if(!v[i]){ int my_size=1; v[i]=true; int cur=a[i]; while(cur!=i){ v[cur]=true; cur=a[cur]; my_size++; } size[my_size]++; } } int max_dou=0; for(int i=0;i<=n;i++){ max_dou+=(i/2)*size[i]; } int arr[]=new int[n+1]; int ptr=0; for(int i=1;i<=n;i++){ if(size[i]>0){ int val=size[i]; for(int j=0;(1<<j)<val;j++){ int tobe=val-(1<<j); if(tobe>=0){ arr[ptr++]=(1<<j)*i; val=tobe; } } if(val>0){ arr[ptr++]=val*i; } } } BigInteger b=BigInteger.ONE; for(int i=0;i<ptr;i++){ b=b.or(b.shiftLeft(arr[i])); } int min = (b.and(BigInteger.ONE.shiftLeft(k)).compareTo(BigInteger.ZERO)>0)?k:k+1; int max = Math.min(k, max_dou)*2; for(int i=max_dou+1;i<=k;i++){ max++; } if(max>n) max=n; out.println(min+" "+max); } //---------------------My Code Ends Here------------------------ static long modulo(long a,long b,long c) { long x=1; long y=a; while(b > 0){ if(b%2 == 1){ x=(x*y)%c; } y = (y*y)%c; // squaring the base b /= 2; } return x%c; } static class Pair implements Comparable<Pair>{ long x; long y; Pair(long xx,long yy){ x=xx; y=yy; } @Override public int compareTo(Pair o) { if(Long.compare(this.x, o.x)!=0) return Long.compare(this.x, o.x); else return Long.compare(this.y, o.y); } } public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static final class InputReader{ private final InputStream stream; private final byte[] buf=new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream){this.stream=stream;} private int read()throws IOException{ if(curChar>=numChars){ curChar=0; numChars=stream.read(buf); if(numChars<=0) return -1; } return buf[curChar++]; } public final int nextInt()throws IOException{return (int)nextLong();} public final long nextLong()throws IOException{ int c=read(); while(isSpaceChar(c)){ c=read(); if(c==-1) throw new IOException(); } boolean negative=false; if(c=='-'){ negative=true; 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 negative?(-res):(res); } public final int[] readIntBrray(int size)throws IOException{ int[] arr=new int[size]; for(int i=0;i<size;i++)arr[i]=nextInt(); return arr; } public final String next()throws IOException{ int c=read(); while(isSpaceChar(c))c=read(); StringBuilder res=new StringBuilder(); do{ res.append((char)c); c=read(); }while(!isSpaceChar(c)); return res.toString(); } public final String nextLine()throws IOException{ int c=read(); while(isSpaceChar(c))c=read(); StringBuilder res=new StringBuilder(); do{ res.append((char)c); c=read(); }while(c!='\n'&&c!=-1); return res.toString(); } private boolean isSpaceChar(int c){ return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1; } } }
Java
["5 2\n3 4 1 5 2", "10 1\n2 3 4 5 6 7 8 9 10 1"]
1.5 seconds
["2 4", "2 2"]
NoteIn the first sample, if the third and the first balls will forget to bring their presents, they will be th only balls not getting a present. Thus the minimum answer is 2. However, if the first ans the second balls will forget to bring their presents, then only the fifth ball will get a present. So, the maximum answer is 4.
Java 8
standard input
[ "dp", "bitmasks", "greedy" ]
a0ddb8f02a91865dc65a214f1de111e3
The first line of input contains two integers n and k (2 ≤ n ≤ 106, 0 ≤ k ≤ n), representing the number of Balls and the number of Balls who will forget to bring their presents. The second line contains the permutation p of integers from 1 to n, where pi is the index of Ball who should get a gift from the i-th Ball. For all i, pi ≠ i holds.
2,600
You should output two values — minimum and maximum possible number of Balls who will not get their presents, in that order.
standard output
PASSED
bde62e41139ea780d9d9597bc0a2003f
train_001.jsonl
1484499900
It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are n Balls overall. Each Ball has someone for whom he should bring a present according to some permutation p, pi ≠ i for all i.Unfortunately, Balls are quite clumsy. We know earlier that exactly k of them will forget to bring their gift. A Ball number i will get his present if the following two constraints will hold: Ball number i will bring the present he should give. Ball x such that px = i will bring his present. What is minimum and maximum possible number of kids who will not get their present if exactly k Balls will forget theirs?
256 megabytes
/* * Code Author: Akshay Miterani * DA-IICT */ import java.io.*; import java.math.*; import java.util.*; public class Main { InputReader in; PrintWriter out; Main() throws IOException { in = new InputReader(System.in); out = new PrintWriter(System.out); //in = new InputReader(new FileInputStream("input_.txt")); //out = new PrintWriter(new File("output_.txt")); solve(); out.close(); } public static void main(String args[]) { new Thread(null, new Runnable() { public void run() { try { new Main(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } //--------------------My Code Starts Here---------------------- long mod=(long)1e9+9; double eps=1e-150; public void solve() throws IOException{ int n=in.nextInt(); int k=in.nextInt(); int a[]=new int[n+1]; for(int i=1;i<=n;i++){ a[i]=in.nextInt(); } int size[]=new int[n+1]; boolean v[]=new boolean[n+1]; for(int i=1;i<=n;i++){ if(!v[i]){ int my_size=1; v[i]=true; int cur=a[i]; while(cur!=i){ v[cur]=true; cur=a[cur]; my_size++; } size[my_size]++; } } int max_dou=0; for(int i=0;i<=n;i++){ max_dou+=(i/2)*size[i]; } int arr[]=new int[n+1]; int ptr=0; for(int i=1;i<=n;i++){ if(size[i]>0){ int val=size[i]; for(int j=0;(1<<j)<val;j++){ int tobe=val-(1<<j); if(tobe>=0){ arr[ptr++]=(1<<j)*i; val=tobe; } } if(val>0){ arr[ptr++]=val*i; } } } BitSet dp=new BitSet(k+1); dp.set(0); for(int i=0;i<ptr;i++){ dp.or(shiftLeft(dp, arr[i])); } int min = dp.get(k)?k:k+1; int max = Math.min(k, max_dou)*2; for(int i=max_dou+1;i<=k;i++){ max++; } if(max>n) max=n; out.println(min+" "+max); } //---------------------My Code Ends Here------------------------ BitSet shiftLeft(BitSet a, int k) { long[] ar = a.toLongArray(); int d = k >> 6; int r = k & 63; long[] rs = new long[Math.min(20000, ar.length + d + 1)]; for (int i = d; i < rs.length; i++) { if (i - d < ar.length) { rs[i] = ar[i - d] << r; } if (i - d - 1 >= 0 && r > 0) { rs[i] |= ar[i - d - 1] >>> 64 - r; } } return BitSet.valueOf(rs); } static long modulo(long a,long b,long c) { long x=1; long y=a; while(b > 0){ if(b%2 == 1){ x=(x*y)%c; } y = (y*y)%c; // squaring the base b /= 2; } return x%c; } static class Pair implements Comparable<Pair>{ long x; long y; Pair(long xx,long yy){ x=xx; y=yy; } @Override public int compareTo(Pair o) { if(Long.compare(this.x, o.x)!=0) return Long.compare(this.x, o.x); else return Long.compare(this.y, o.y); } } public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static final class InputReader{ private final InputStream stream; private final byte[] buf=new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream){this.stream=stream;} private int read()throws IOException{ if(curChar>=numChars){ curChar=0; numChars=stream.read(buf); if(numChars<=0) return -1; } return buf[curChar++]; } public final int nextInt()throws IOException{return (int)nextLong();} public final long nextLong()throws IOException{ int c=read(); while(isSpaceChar(c)){ c=read(); if(c==-1) throw new IOException(); } boolean negative=false; if(c=='-'){ negative=true; 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 negative?(-res):(res); } public final int[] readIntBrray(int size)throws IOException{ int[] arr=new int[size]; for(int i=0;i<size;i++)arr[i]=nextInt(); return arr; } public final String next()throws IOException{ int c=read(); while(isSpaceChar(c))c=read(); StringBuilder res=new StringBuilder(); do{ res.append((char)c); c=read(); }while(!isSpaceChar(c)); return res.toString(); } public final String nextLine()throws IOException{ int c=read(); while(isSpaceChar(c))c=read(); StringBuilder res=new StringBuilder(); do{ res.append((char)c); c=read(); }while(c!='\n'&&c!=-1); return res.toString(); } private boolean isSpaceChar(int c){ return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1; } } }
Java
["5 2\n3 4 1 5 2", "10 1\n2 3 4 5 6 7 8 9 10 1"]
1.5 seconds
["2 4", "2 2"]
NoteIn the first sample, if the third and the first balls will forget to bring their presents, they will be th only balls not getting a present. Thus the minimum answer is 2. However, if the first ans the second balls will forget to bring their presents, then only the fifth ball will get a present. So, the maximum answer is 4.
Java 8
standard input
[ "dp", "bitmasks", "greedy" ]
a0ddb8f02a91865dc65a214f1de111e3
The first line of input contains two integers n and k (2 ≤ n ≤ 106, 0 ≤ k ≤ n), representing the number of Balls and the number of Balls who will forget to bring their presents. The second line contains the permutation p of integers from 1 to n, where pi is the index of Ball who should get a gift from the i-th Ball. For all i, pi ≠ i holds.
2,600
You should output two values — minimum and maximum possible number of Balls who will not get their presents, in that order.
standard output
PASSED
a1bd29ecc8146253256ca2ae10684cb5
train_001.jsonl
1484499900
It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are n Balls overall. Each Ball has someone for whom he should bring a present according to some permutation p, pi ≠ i for all i.Unfortunately, Balls are quite clumsy. We know earlier that exactly k of them will forget to bring their gift. A Ball number i will get his present if the following two constraints will hold: Ball number i will bring the present he should give. Ball x such that px = i will bring his present. What is minimum and maximum possible number of kids who will not get their present if exactly k Balls will forget theirs?
256 megabytes
/* * Code Author: Akshay Miterani * DA-IICT */ import java.io.*; import java.math.*; import java.util.*; public class Main { InputReader in; PrintWriter out; Main() throws IOException { in = new InputReader(System.in); out = new PrintWriter(System.out); //in = new InputReader(new FileInputStream("input_.txt")); //out = new PrintWriter(new File("output_.txt")); solve(); out.close(); } public static void main(String args[]) { new Thread(null, new Runnable() { public void run() { try { new Main(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } //--------------------My Code Starts Here---------------------- long mod=(long)1e9+9; double eps=1e-150; public void solve() throws IOException{ int n=in.nextInt(); int k=in.nextInt(); int a[]=new int[n+1]; for(int i=1;i<=n;i++){ a[i]=in.nextInt(); } int size[]=new int[n+1]; boolean v[]=new boolean[n+1]; for(int i=1;i<=n;i++){ if(!v[i]){ int my_size=1; v[i]=true; int cur=a[i]; while(cur!=i){ v[cur]=true; cur=a[cur]; my_size++; } size[my_size]++; } } int max_dou=0; for(int i=0;i<=n;i++){ max_dou+=(i/2)*size[i]; } int arr[]=new int[n+1]; int ptr=0; for(int i=1;i<=n;i++){ if(size[i]>0){ int val=size[i]; for(int j=0;(1<<j)<val;j++){ int tobe=val-(1<<j); if(tobe>=0){ arr[ptr++]=(1<<j)*i; val=tobe; } } if(val>0){ arr[ptr++]=val*i; } } } BitSet dp=new BitSet(k+1); dp.set(0); for(int i=0;i<ptr;i++){ dp.orShiftLeft(arr[i]); } int min = dp.get(k)?k:k+1; int max = Math.min(k, max_dou)*2; for(int i=max_dou+1;i<=k;i++){ max++; } if(max>n) max=n; out.println(min+" "+max); } //---------------------My Code Ends Here------------------------ static class BitSet { private int n; private int m; private long[] value; private long[] tmpValue; public BitSet(int n) { this.n = n; m = (n + 63) >> 6; value = new long[m]; tmpValue = new long[m]; } public void set(int bit) { value[bit >> 6] |= 1L << (bit & 63); } public boolean get(int bit) { return ((value[bit >> 6] >>> (bit & 63)) & 1) > 0; } public void orShiftLeft(int shift) { shiftLeftInternal(shift); for (int i = 0; i < m; ++i) { value[i] |= tmpValue[i]; } } public String toString() { StringBuffer sb = new StringBuffer(n); for (int i = n - 1; i >= 0; --i) { sb.append(get(i) ? '1' : '0'); } return sb.toString(); } private void shiftLeftInternal(int shift) { if (shift < 0) throw new IllegalArgumentException(); Arrays.fill(tmpValue, 0); int x = shift >> 6, y = shift & 63; for (int i = m - x - 1; i >= 0; --i) { if (y > 0 && i + x + 1 < m) { tmpValue[i + x + 1] |= value[i] >>> (64 - y); } tmpValue[i + x] |= value[i] << y; } } } static interface IntCollection { } static class IntArrayList implements IntCollection { private static final int[] EMPTY = {}; public int[] values; public int size; public IntArrayList() { values = EMPTY; clear(); } public IntArrayList(int capacity) { values = new int[Integer.highestOneBit(capacity) << 1]; clear(); } public void clear() { size = 0; } public void add(int value) { ensureCapacity(size + 1); values[size++] = value; } public int get(int idx) { if (idx >= size) throw new ArrayIndexOutOfBoundsException(); return values[idx]; } public void ensureCapacity(int capacity) { if (capacity <= values.length) return; int[] newValues = new int[Integer.highestOneBit(capacity) << 1]; for (int i = 0; i < size; ++i) { newValues[i] = values[i]; } values = newValues; } } static long modulo(long a,long b,long c) { long x=1; long y=a; while(b > 0){ if(b%2 == 1){ x=(x*y)%c; } y = (y*y)%c; // squaring the base b /= 2; } return x%c; } static class Pair implements Comparable<Pair>{ long x; long y; Pair(long xx,long yy){ x=xx; y=yy; } @Override public int compareTo(Pair o) { if(Long.compare(this.x, o.x)!=0) return Long.compare(this.x, o.x); else return Long.compare(this.y, o.y); } } public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static final class InputReader{ private final InputStream stream; private final byte[] buf=new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream){this.stream=stream;} private int read()throws IOException{ if(curChar>=numChars){ curChar=0; numChars=stream.read(buf); if(numChars<=0) return -1; } return buf[curChar++]; } public final int nextInt()throws IOException{return (int)nextLong();} public final long nextLong()throws IOException{ int c=read(); while(isSpaceChar(c)){ c=read(); if(c==-1) throw new IOException(); } boolean negative=false; if(c=='-'){ negative=true; 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 negative?(-res):(res); } public final int[] readIntBrray(int size)throws IOException{ int[] arr=new int[size]; for(int i=0;i<size;i++)arr[i]=nextInt(); return arr; } public final String next()throws IOException{ int c=read(); while(isSpaceChar(c))c=read(); StringBuilder res=new StringBuilder(); do{ res.append((char)c); c=read(); }while(!isSpaceChar(c)); return res.toString(); } public final String nextLine()throws IOException{ int c=read(); while(isSpaceChar(c))c=read(); StringBuilder res=new StringBuilder(); do{ res.append((char)c); c=read(); }while(c!='\n'&&c!=-1); return res.toString(); } private boolean isSpaceChar(int c){ return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1; } } }
Java
["5 2\n3 4 1 5 2", "10 1\n2 3 4 5 6 7 8 9 10 1"]
1.5 seconds
["2 4", "2 2"]
NoteIn the first sample, if the third and the first balls will forget to bring their presents, they will be th only balls not getting a present. Thus the minimum answer is 2. However, if the first ans the second balls will forget to bring their presents, then only the fifth ball will get a present. So, the maximum answer is 4.
Java 8
standard input
[ "dp", "bitmasks", "greedy" ]
a0ddb8f02a91865dc65a214f1de111e3
The first line of input contains two integers n and k (2 ≤ n ≤ 106, 0 ≤ k ≤ n), representing the number of Balls and the number of Balls who will forget to bring their presents. The second line contains the permutation p of integers from 1 to n, where pi is the index of Ball who should get a gift from the i-th Ball. For all i, pi ≠ i holds.
2,600
You should output two values — minimum and maximum possible number of Balls who will not get their presents, in that order.
standard output
PASSED
761f244bce46298b5e6806c44f0cacf8
train_001.jsonl
1484499900
It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are n Balls overall. Each Ball has someone for whom he should bring a present according to some permutation p, pi ≠ i for all i.Unfortunately, Balls are quite clumsy. We know earlier that exactly k of them will forget to bring their gift. A Ball number i will get his present if the following two constraints will hold: Ball number i will bring the present he should give. Ball x such that px = i will bring his present. What is minimum and maximum possible number of kids who will not get their present if exactly k Balls will forget theirs?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskF solver = new TaskF(); solver.solve(1, in, out); out.close(); } static class TaskF { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); int[] p = new int[n]; for (int i = 0; i < n; ++i) p[i] = in.nextInt() - 1; int[] cycles = new int[n]; int nCycles = 0; boolean[] mark = new boolean[n]; for (int i = 0; i < n; ++i) if (!mark[i]) { int j = i; int len = 0; do { mark[j] = true; j = p[j]; ++len; } while (j != i); cycles[nCycles++] = len; } cycles = Arrays.copyOf(cycles, nCycles); Arrays.sort(cycles); int doubles = 0; for (int x : cycles) doubles += x / 2; int maxKill; if (k <= doubles) { maxKill = 2 * k; } else { maxKill = Math.min(n, 2 * doubles + (k - doubles)); } int minKill = canRepresent(cycles, Math.min(k, n - k)) ? k : (k + 1); out.println(minKill + " " + maxKill); } private boolean canRepresent(int[] a, int s) { int[] cnt = new int[s + 1]; for (int x : a) { if (x <= s) ++cnt[x]; } boolean[] can = new boolean[s + 1]; can[0] = true; for (int v = 1; v <= s; ++v) { int am = cnt[v]; if (am == 0) continue; for (int from = 0; from < v; ++from) { int counter = -1; for (int got = from; got <= s; got += v) { --counter; if (can[got]) counter = am; else if (counter >= 0) { can[got] = true; } } } } return can[s]; } } 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 2\n3 4 1 5 2", "10 1\n2 3 4 5 6 7 8 9 10 1"]
1.5 seconds
["2 4", "2 2"]
NoteIn the first sample, if the third and the first balls will forget to bring their presents, they will be th only balls not getting a present. Thus the minimum answer is 2. However, if the first ans the second balls will forget to bring their presents, then only the fifth ball will get a present. So, the maximum answer is 4.
Java 8
standard input
[ "dp", "bitmasks", "greedy" ]
a0ddb8f02a91865dc65a214f1de111e3
The first line of input contains two integers n and k (2 ≤ n ≤ 106, 0 ≤ k ≤ n), representing the number of Balls and the number of Balls who will forget to bring their presents. The second line contains the permutation p of integers from 1 to n, where pi is the index of Ball who should get a gift from the i-th Ball. For all i, pi ≠ i holds.
2,600
You should output two values — minimum and maximum possible number of Balls who will not get their presents, in that order.
standard output
PASSED
67ce58267b1ea19539eaf6507b7261ba
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine()); String nn[] = scanner.nextLine().split(" "); long t = 0L; for(int j =0 ; j<nn.length;j++){ long x = Long.parseLong(nn[j]); t += x + (x-1)*j; } System.out.println(t); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 8
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
e6aa8687ef81bda1b5eba8bfb0dc5dcc
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Mohamed Seltene */ public class Main { public static void main(String[] args){ try { BufferedReader br; StringTokenizer st; br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); int question = Integer.parseInt(st.nextToken()); List<BigInteger> questions = new ArrayList<BigInteger>(); st = new StringTokenizer(br.readLine()); BigInteger clicks = BigInteger.ZERO; for(int i =0;i<question;i++){ questions.add(new BigInteger(st.nextToken())); } for(int i=0;i<question;i++){ clicks = clicks.add(questions.get(i)); BigInteger temp = BigInteger.ZERO; for(int j=0;j<i;j++) temp = temp.add(BigInteger.ONE); if(temp.compareTo(BigInteger.ZERO) > 0){ BigInteger b1 = temp.multiply(questions.get(i).subtract(BigInteger.ONE)); clicks = clicks.add(b1); } } System.out.println(clicks); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 8
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
f34f2aa065711549ef65241e96aa6d47
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.PrintWriter; import java.util.Scanner; public class A103{ public static void main(String [] args){ Scanner sc=new Scanner(System.in); PrintWriter pw=new PrintWriter(System.out); int n=sc.nextInt(); long a[]=new long [n]; for(int i=0;i<n;i++){ a[i]=sc.nextLong(); } long s=0; for(int ii=0;ii<n;ii++){s=s+a[ii]+(ii)*(a[ii]-1);} pw.print(s); pw.close(); sc.close(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
7816f536b58523f4554f687eee9a1ed8
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class TestingPants { private void solve() throws IOException { int n = nextInt(); long l = 0; for(int i=0;i<n;i++) { int t = nextInt(); l+= (long)t+(long)i*(long)(t-1); } writer.println(l); } public static void main(String[] args) { new TestingPants().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
9a01b310dd2656df548a2b46b6b73261
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class CodeForces { public void solve() throws IOException { int n=nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++){ arr[i]=nextLong(); } long count=0; for(int i=0;i<n;i++){ count+=arr[i]+i*(arr[i]-1); } writer.print(count); } public static void main(String[] args) { new CodeForces().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); // reader = new BufferedReader(new FileReader("input.txt")); tokenizer = null; writer = new PrintWriter(System.out); // writer = new PrintWriter(new BufferedWriter(new // FileWriter("output.txt"))); // long t=new Date().getTime(); solve(); // writer.println(t-new Date().getTime()); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
b92e2bd758b32a3d427a21a62ca4f306
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.StringTokenizer; public class Main { private static BufferedReader br; private static PrintWriter pw; private static StringTokenizer stk = new StringTokenizer(""); public static void main(String[] args) { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); (new Main()).solve(); pw.close(); } private void nline() { while (!stk.hasMoreElements()) { stk = new StringTokenizer(rline()); } } private String rline() { try { return br.readLine(); } catch (IOException e) { throw new RuntimeException("rline"); } } private boolean isready() { try { return br.ready(); } catch (IOException e) { throw new RuntimeException("isready"); } } private String ntok() { nline(); return stk.nextToken(); } private Integer ni() { return Integer.valueOf(ntok()); } private Double nd() { return Double.valueOf(ntok()); } private Long nl() { return Long.valueOf(ntok()); } private BigInteger nBI() { return new BigInteger(ntok()); } private void solve() { int n = ni(); long[] a = new long[n]; long[] dp = new long[n+1]; for (int i=0;i<n;i++) a[i]=ni(); dp[0]=0; for (int i=1;i<=n;i++) // 0 0+ dp[i]=dp[i-1]+(a[i-1]-1)*i+1; pw.println(dp[n]); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
d9f9f963760b810458f35ea5d6411d35
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class sample { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); int n = in.nextInt(); long[] var = new long[n]; for (int i = 0; i < n; i++) var[i] = in.nextInt(); long[] dp = new long[n]; dp[0] = var[0]; for (int i = 1; i < n; i++) dp[i] = dp[i - 1] + (var[i] - 1) * (long) (i) + var[i]; System.out.println(dp[n - 1]); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
809c5b92ecd6fd5c7f0fe55d9b1bdbb0
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.*; import java.util.Arrays; public class JavaApplication2 { /** * @param args */ public static void main(String[] args) { Scanner sc = new Scanner (System.in); int number = sc.nextInt(); sc.useDelimiter("\\s+"); long[] ch=new long[number+1]; long[] pre=new long[number+1]; pre[0]=0; int sum; for(int i=1;i<number+1;i++){ ch[i]=sc.nextInt(); } for(int i=1;i<(number+1);i++) { pre[i]=pre[i-1]+((ch[i]-1)*(i)); } System.out.print(pre[number]+number); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
f41ea4f30d86d400d78887e7f44a86cd
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.util.*; public class B { static Scanner in = new Scanner(new BufferedInputStream(System.in)); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int n = in.nextInt(); long ans = 0; for(int i = 1; i <= n; i++) { ans += 1 + (in.nextLong() - 1) * i; } out.println(ans); out.flush(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
3898293701ad1458765a554a25ce51e8
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; import static java.lang.Math.*; public class A implements Runnable { int [] a; long [] steps; long get(int i) { if (i == a.length) return 0; return (a[i] - 1) + steps[i + 1] + get(i + 1); } void solve() throws Exception { int n = nextInt(); a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } steps = new long[n + 1]; steps[n] = 1; for (int i = n - 1; i >= 0; i--) { steps[i] = steps[i + 1] + a[i] - 1; } out.println(get(0)); } public static void main(String[] args) { new Thread(new A()).start(); } public void run() { try { Locale.setDefault(Locale.US); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sTime(); solve(); debug("Time consumed: " + gTime()); gMemory(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } StringTokenizer tokenizer = new StringTokenizer(""); BufferedReader in; PrintWriter out; long time; void sTime() { time = System.currentTimeMillis(); } long gTime() { return System.currentTimeMillis() - time; } void gMemory() { debug("Memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + " kb"); } public void debug(Object o) { System.err.println(o); } boolean seekForToken() { while (!tokenizer.hasMoreTokens()) { String s = null; try { s = in.readLine(); } catch (Exception e) { e.printStackTrace(); } if (s == null) return false; tokenizer = new StringTokenizer(s); } return true; } String nextToken() { return seekForToken() ? tokenizer.nextToken() : null; } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } BigInteger nextBig() { return new BigInteger(nextToken()); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
52d2006521ae2f9b0eb9fe468e482e4f
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.math.BigInteger; import java.util.Scanner; public class A_103 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); BigInteger result = BigInteger.ZERO; for(int i=0;i<n;i++) { long c = in.nextLong(); result = result.add(BigInteger.valueOf(i * (c-1) + c)); } System.out.println(result); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
a6e4aa8b82266ac30943c28890ba835d
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long a[] = new long[n]; for(int i=0; i<n; i++) a[i] = sc.nextInt(); long ans = 0; for(int i=0; i<n; i++) { ans += (a[i] - 1) * (i+1) + 1; } System.out.println(ans); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
2339f09f507a5c52aa9b4d59143165b3
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.math.*; import java.util.*; import static java.lang.Math.*; public class Solution implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer st; boolean file = false; public static void main(String[] args) throws Exception { new Thread(new Solution()).start(); } private String next() throws Exception { if (st == null || !st.hasMoreElements()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } private int nextInt() throws Exception { return Integer.parseInt(next()); } private long nextLong() throws Exception { return Long.parseLong(next()); } private double nextDouble() throws Exception { return Double.parseDouble(next()); } public void run() { try { if (file) in = new BufferedReader(new FileReader("in.txt")); else in = new BufferedReader(new InputStreamReader(System.in)); if (file) out = new PrintWriter(new FileWriter("out.txt")); else out = new PrintWriter(System.out); solve(); } catch (Exception ex) { throw new RuntimeException(ex); } finally { out.close(); } } public void solve() throws Exception { long n = nextLong(), res = nextLong(); for(int i=1; i<n; i++){ long v = nextLong(); if(v == 1)res++; else { res += 1 + (v-1)*(i+1); } } out.println(res); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
5a0d381a037652a8d36995ce5c8f8062
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.*; public class SadPants { public static void main (String [] arg) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long [] q = new long [n]; for (int i = 0; i<n; ++i) q[i] = sc.nextLong(); long ans = 0; for (int i = 1; i<=n; ++i) { long wrong = q[i-1] - 1; ans += ((long)i)*wrong; } ans += n; System.out.println(ans); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
3af56c48b5634db771aa05b8349d5419
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.math.BigInteger; import java.util.Scanner; import java.util.Vector; public class Main { public static void main(String[] args) { int number_of_questions; Vector<BigInteger> number_of_answers = new Vector<BigInteger>(); Scanner scanner = new Scanner(System.in); number_of_questions = scanner.nextInt(); BigInteger counter = BigInteger.ZERO; for (int i = 0; i < number_of_questions ; i++) { BigInteger bi; bi = scanner.nextBigInteger(); counter = counter.add(bi); number_of_answers.add(bi); } for(int i = 1 ; i < number_of_answers.size(); i++){ if(number_of_answers.get(i) != BigInteger.ONE){ BigInteger x = number_of_answers.get(i).subtract(BigInteger.ONE) ; counter = counter.add(x.multiply(BigInteger.valueOf(i))); } } System.out.println(counter); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
c9ac97186465ff85b9f0a758fbc00844
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.PrintWriter; import java.util.Locale; import java.util.Scanner; public class A implements Runnable { private Scanner in; private PrintWriter out; private void solve() { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; ++i) { a[i] = in.nextInt(); } long ans = 0; for (int i = 0; i < n; ++i) { ans += 1L * (i + 1) * (a[i] - 1); } ans += n; out.println(ans); } @Override public void run() { Locale.setDefault(Locale.US); in = new Scanner(System.in); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } public static void main(String[] args) { new A().run(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
c388f2bc356a245a097d820d311975e8
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.*; public class cf103a { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long tot=0; for(int i=1; i<=n; i++) tot += in.nextLong()*i-(i-1); System.out.println(tot); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
f5c8052a5843809e5098249e127bd525
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; public class B_80_TestingPantsForSadness { public static void main(String[] args) { try { BufferedReader input = new BufferedReader(new InputStreamReader( System.in)); String line = input.readLine(); int n = Integer.parseInt(line); BigInteger clicks = BigInteger.ZERO; line = input.readLine(); String[] answers = line.split(" "); BigInteger product = null; for (int i = 0; i < n; i++) { int numAns = Integer.parseInt(answers[i].trim()); BigInteger p1 = new BigInteger(Integer.toString(numAns - 1)); BigInteger p2 = new BigInteger(Integer.toString(i + 1)); product = p1.multiply(p2); clicks = clicks.add(product.add(BigInteger.ONE)); } System.out.println(clicks); } catch (IOException e) { e.printStackTrace(); } } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
e0b2cda3a75503e92005c1e01e70a68a
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Ronex */ public class Main2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long[]tab = new long[n]; for (int i = 0; i < n; i++) { tab[i] = in.nextLong(); } long ans = 0; long level = 0; for (int i = 0; i < tab.length; i++) { ans += tab[i] + level * tab[i] - level; level++; } System.out.println(ans); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
1495608de6a128dd28d4eb8b6057201d
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.text.*; public class Main { Scanner cin = new Scanner(new BufferedInputStream(System.in)); int n; long sum = 0; long ar[] = new long[105]; void run(){ n = cin.nextInt(); Arrays.fill(ar, 0); for(int i = 1; i <= n ;i++){ ar[i] = cin.nextLong(); } int temp = 0; for(int i = 1; i <= n; i++){ temp = 0; for(int j = 1; j < i; j++) temp++; sum += temp * (ar[i] - 1) + ar[i]; } System.out.println(sum); } public static void main(String[] args) { new Main().run(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
99325bba18af8ade23a4df34421cbcee
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class TestingPantsForSadness { void run() { try { BufferedReader bfd = new BufferedReader(new InputStreamReader( System.in)); int n = Integer.parseInt(bfd.readLine()),i; long arr[]=new long[n]; long res[]=new long[n]; StringTokenizer tk = new StringTokenizer(bfd.readLine()); for(i=0;i<n;++i) arr[i] = Integer.parseInt(tk.nextToken()); res[arr.length-1] = 1; long total = 1; for(i=arr.length-2; i>=0; --i){ res[i] = res[i+1]+(arr[i+1]-1); total += res[i]; } for(i=0;i<n;++i) total += arr[i]-1; System.out.println(total); } catch (Exception e) { } } public static void main(String[] args) { new TestingPantsForSadness().run(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
8b6b6e39980d5fc5400e7bbcb6b8db3a
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.*; import java.math.*; import static java.lang.Character.isDigit; import static java.lang.Character.isLowerCase; import static java.lang.Character.isUpperCase; import static java.lang.Math.*; import static java.math.BigInteger.*; import static java.util.Arrays.*; import static java.util.Collections.*; import static java.lang.Character.isDigit; public class Main{ static void debug(Object...os){ System.err.println(deepToString(os)); } void run(){ int n=nextInt(); long res=n; for(int i=0;i<n;i++) { res += (i+1) * (nextLong()-1); } System.out.println(res); } int nextInt(){ try{ int c=System.in.read(); if(c==-1) return c; while(c!='-'&&(c<'0'||'9'<c)){ c=System.in.read(); if(c==-1) return c; } if(c=='-') return -nextInt(); int res=0; do{ res*=10; res+=c-'0'; c=System.in.read(); }while('0'<=c&&c<='9'); return res; }catch(Exception e){ return -1; } } long nextLong(){ try{ int c=System.in.read(); if(c==-1) return -1; while(c!='-'&&(c<'0'||'9'<c)){ c=System.in.read(); if(c==-1) return -1; } if(c=='-') return -nextLong(); long res=0; do{ res*=10; res+=c-'0'; c=System.in.read(); }while('0'<=c&&c<='9'); return res; }catch(Exception e){ return -1; } } double nextDouble(){ return Double.parseDouble(next()); } String next(){ try{ StringBuilder res=new StringBuilder(""); int c=System.in.read(); while(Character.isWhitespace(c)) c=System.in.read(); do{ res.append((char)c); }while(!Character.isWhitespace(c=System.in.read())); return res.toString(); }catch(Exception e){ return null; } } String nextLine(){ try{ StringBuilder res=new StringBuilder(""); int c=System.in.read(); while(c=='\r'||c=='\n') c=System.in.read(); do{ res.append((char)c); c=System.in.read(); }while(c!='\r'&&c!='\n'); return res.toString(); }catch(Exception e){ return null; } } public static void main(String[] args){ new Main().run(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
d485a261a161d937e952913726f0154d
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.util.*; public class test { public static void main(String[] args) throws Exception { int n; Scanner read = new Scanner(System.in); n = read.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = read.nextInt(); long ans = 0,s = 1; for (int x : a) { ans += s * (x - 1); s++; } ans += n; System.out.println(ans); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
3dc3eccf62bc0373bc8fabb092056d95
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; public class P103A { public static void main(String[] args) { Scanner inScanner = new Scanner(System.in); int n = inScanner.nextInt(); long sum = 0; for (long i = 0; i < n; i++) sum += (i + 1) * inScanner.nextLong() - i; System.out.println(sum); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
8ae938732d2dd6fb341afb12da9a38b4
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.awt.*; import java.awt.geom.*; import java.io.*; import java.math.*; import java.text.*; import java.util.*; public class B { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] list = new int[n]; StringTokenizer st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++) list[i] = Integer.parseInt(st.nextToken()); long ret = 0; for(int i = 0; i < n; i++) { if(i + 1 != n) ret += (list[i] - 1L) * (i+1); else ret += (list[i]) * (long)(i+1); } System.out.println(ret); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
5807704c946c4a7d04d512ccc7ce79f4
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.math.BigInteger; public class Test { /** * @param args * @throws IOException * @throws NumberFormatException */ public static void main(String[] args) throws NumberFormatException, IOException { // TODO Auto-generated method stub BufferedReader r=new BufferedReader(new InputStreamReader(System.in)); int n=new Integer(r.readLine()); long[] arr=new long[n]; String s=r.readLine(); String[]sp=s.split(" "); for (int i = 0; i < arr.length; i++) { arr[i]=new Integer(sp[i]); } long res2=0; for (int i = 0; i < arr.length; i++) { res2+=(arr[i]-1)*(i+1)+1; // System.out.println(res); } System.out.println(res2); // for(int i=0;i<100;i++)System.out.print(1000000000+" "); } } //3413422250
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
c78c80222087ef698bb2850a651cea12
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; public class A { private void work() { Scanner sc = new Scanner(new BufferedReader(new InputStreamReader( System.in))); while (sc.hasNextInt()) { int n = sc.nextInt(); long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) { a[i] = sc.nextInt(); a[i] = a[i] * i - i + 1 + a[i - 1] ; } System.out.println(a[n]); } } public static void main(String[] args) { new A().work(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
a0eb8a149fc7a6cc0b60b3bede71e772
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class A80 { static StringTokenizer st; static BufferedReader in; public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = nextInt(); long ans = 0; for (int i = 1; i <= n; i++) { int k = nextInt(); ans += (long)(i-1) * (k-1)+k; } System.out.println(ans); pw.close(); } private static int nextInt() throws IOException{ return Integer.parseInt(next()); } private static long nextLong() throws IOException{ return Long.parseLong(next()); } private static double nextDouble() throws IOException{ return Double.parseDouble(next()); } private static String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
7bda5e7150a59b64ac489373190912ec
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; public class Main { StreamTokenizer in; PrintWriter out; public static void main(String[] args) throws IOException { new Main().run(); } int nextInt() throws IOException { in.nextToken(); return (int)in.nval; } long nextLong() throws IOException { in.nextToken(); return (long)in.nval; } double nextDouble() throws IOException { in.nextToken(); return (double)in.nval; } String nextString() throws IOException{ in.nextToken(); return (String)in.sval; } void run() throws IOException { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); out.flush(); } void solve() throws IOException { int n = nextInt(); long x = 0; for (int i=0; i<n; i++){ long h = nextLong(); x += (h - 1) * (i+1); } x += n; out.print(x); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
53c4633df33eb8838ec87dfc03d6a2f1
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.*; public class A { private static Scanner in; public void run() { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } long ans = 0; for (int i = 0; i < n; i++) { ans += (i + 1L) * a[i] - i; } System.out.println(ans); } public static void main(String[] args) { Locale.setDefault(Locale.US); in = new Scanner(System.in); new A().run(); in.close(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
b2f58e523fad945db3e7083be91464df
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.util.*; public class TestingPantsForSadness { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); StringTokenizer st = new StringTokenizer(in.readLine()); long res = 0; for (int i = 0; i < n; i++) { int a = Integer.parseInt(st.nextToken()); res++; a--; res += (long)(i + 1)*a; } System.out.println(res); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
b98da720243ac49cef4d49b55de6baad
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
/** * Created by IntelliJ IDEA. * User: Taras_Brzezinsky * Date: 8/7/11 * Time: 2:02 PM * To change this template use File | Settings | File Templates. */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.FileReader; import java.util.StringTokenizer; import java.io.IOException; import java.util.Arrays; public class Pants extends Thread { public Pants() { this.input = new BufferedReader(new InputStreamReader(System.in)); this.output = new PrintWriter(System.out); this.setPriority(Thread.MAX_PRIORITY); } public void run() { try { int n = nextInt(); long []A = new long [n]; for (int i = 0; i < n; ++i) { A[i] = nextLong(); } long answer = 0; for (int i = 0; i < n; ++i) { answer += A[i]; answer += (A[i] - 1) * i; } output.println(answer); output.flush(); output.close(); } catch (Throwable e) { System.err.println(e.getMessage()); System.err.println(Arrays.deepToString(e.getStackTrace())); } } public static void main(String[] args) { new Pants().start(); } private String nextToken() throws IOException { while (tokens == null || !tokens.hasMoreTokens()) { tokens = new StringTokenizer(input.readLine()); } return tokens.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private BufferedReader input; private PrintWriter output; private StringTokenizer tokens = null; }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
cdc591a91fd16e613c6d4223e7763952
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; /** * Date 30.11.2011 * Time 13:48:42 * Author Woodey * $ */ public class A103 { private void Solution() throws IOException { int n = nextInt(); long ans = n; for (int i = 1; i <= n; i ++) { long a = nextInt(); ans += (a-1)*i; } System.out.println(ans); } public static void main(String[] args) { new A103().run(); } BufferedReader in; StringTokenizer tokenizer; public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; Solution(); in.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(in.readLine()); return tokenizer.nextToken(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
cd0dad426c286ead7cc56b1a4f1c9e9a
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner r = new Scanner(System.in); int n = r.nextInt(); long[] a = new long[n]; for(int i = 0; i < n; i++) a[i] = r.nextInt(); long res = a[0]; for(int i = 1; i < n; i++){ res += ((long)(i+1))*(a[i])-(long)(i); } System.out.println(res); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
05abe0666e6253d0729e524930025165
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; public class PlantingTest { public static void main(String[]args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); long[]array=new long[n]; for(int i=0;i<n;i++){ array[i]=sc.nextLong(); } long count=0; for(int j=0;j<n;j++){ count+=array[j]; } for(int k=1;k<n;k++){ if(array[k]>1){ count+=(array[k]-1)*k; } } System.out.println(count); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
bd6581564d27377ecaf9b6b108ac71ed
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; public class Problem80B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextLong(); } long max = 0; for (int i = 0; i < n; i++) { if(i==0) { max = a[0]; } else { if(i==n) { max += a[n]-1; } else { max += (i+1)*(a[i]-1)+1; } } } System.out.println(max); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
472717fc980c3b330e3088a9972135ce
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.util.*; public class Main { void solve() throws IOException { long n = nextLong(); long ans = 0; for(long i = 0; i < n - 1; ++i) ans += (i + 1) * (nextLong() - 1); ans += n * nextLong(); out.println(ans); } void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = System.out; solve(); in.close(); // out.close(); } BufferedReader in; PrintStream out; StringTokenizer tok; String nextToken() throws IOException { while(tok == null || !tok.hasMoreTokens()) tok = new StringTokenizer(in.readLine()); return tok.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public static void main(String[] args) throws IOException { new Main().run(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
fc182e9a73d4b0578194cccf25f847f6
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import static java.util.Arrays.*; import static java.lang.Math.*; import static java.math.BigInteger.*; import java.util.*; import java.math.*; import java.io.*; public class A implements Runnable { String file = "input"; boolean TEST = false; void solve() throws IOException { int n = nextInt(); long res = 0; for(int i = 0; i < n; i++) { res += (i + 1L) * (nextInt() - 1); } res += n; out.println(res); } String next() throws IOException { while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } void print(Object... o) { System.out.println(deepToString(o)); } void gcj(Object o) { String s = String.valueOf(o); out.println("Case #" + test + ": " + s); System.out.println("Case #" + test + ": " + s); } BufferedReader input; PrintWriter out; StringTokenizer st; int test; void init() throws IOException { if(TEST) input = new BufferedReader(new FileReader(file + ".in")); else input = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedOutputStream(System.out)); } public static void main(String[] args) throws IOException { new Thread(null, new A(), "", 1 << 20).start(); } public void run() { try { init(); if(TEST) { int runs = nextInt(); for(int i = 0; i < runs; i++) solve(); } else solve(); out.close(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
1883eca3790fa45b2b15b53d22633004
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.ArrayList; import java.util.List; import java.util.InputMismatchException; import java.math.BigInteger; import java.util.Iterator; import java.io.*; import java.util.Comparator; import java.util.Arrays; import java.util.Collection; /** * Generated by Contest helper plug-in * Actual solution is at the bottom */ public class Main { public static void main(String[] args) { InputReader in = new StreamInputReader(System.in); PrintWriter out = new PrintWriter(System.out); run(in, out); } public static void run(InputReader in, PrintWriter out) { Solver solver = new TaskA(); solver.solve(1, in, out); Exit.exit(in, out); } } abstract class InputReader { private boolean finished = false; public abstract int read(); 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 String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public void setFinished(boolean finished) { this.finished = finished; } public abstract void close(); } class StreamInputReader extends InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public StreamInputReader(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 void close() { try { stream.close(); } catch (IOException ignored) { } } } class Exit { private Exit() { } public static void exit(InputReader in, PrintWriter out) { in.setFinished(true); in.close(); out.close(); } } interface Solver { public void solve(int testNumber, InputReader in, PrintWriter out); } class IOUtils { public static long[] readLongArray(InputReader in, int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) array[i] = in.readLong(); return array; } } class TaskA implements Solver { public void solve(int testNumber, InputReader in, PrintWriter out) { int count = in.readInt(); long[] variants = IOUtils.readLongArray(in, count); long answer = 0; for (int i = 0; i < variants.length; i++) answer += 1 + (i + 1) * (variants[i] - 1); out.println(answer); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
34530482a8a9bbf3d9e5a2766e06f334
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; public class main { static int dir; public static void main(String[] args) { Scanner sc = new Scanner(System.in); long tiers = sc.nextLong(); long[] store = new long[(int) tiers]; for(long a=0;a<tiers;a++){ store[(int) a]=sc.nextInt(); } long total=0; for(long a=0;a<tiers;a++){ total+=(a+1)*(store[(int) a]-1)+1; } System.out.println(total); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
5b1e13cc7f6f3bb28215cedbcd1b4940
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.util.*; public class Main { static Scanner in; static int next() throws Exception {return in.nextInt();} static PrintWriter out; // static StreamTokenizer in; public static void main(String[] args) throws Exception { in = new Scanner(System.in); out = new PrintWriter(System.out); // in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); int n = next(); long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = next(); long answ = 0; for (int i = 0; i < n; i++) answ += (a[i] - 1L)*(i + 1); answ += n; out.println(answ); out.close(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
0164601d6493e100f4614efe259f22a9
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; /** * @author Alexander Grigoryev * Created on 07.08.11 */ public class Main { static Scanner in = new Scanner(System.in); public static void main(String[] args) { long n = in.nextInt() + 1, ans = 0; for(int i = 1; i < n; i++) { long x = in.nextLong(); ans += x * i - i + 1; } System.out.println(ans); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
8dd5f6b71fbae3a1d0e03e64549ca759
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.*; import java.io.*; public class a { static long mod = 1000000007; public static void main(String[] args) throws IOException { input.init(System.in); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int n = input.nextInt(); int[] data = new int[n]; for(int i = 0; i<n; i++) data[i] = input.nextInt(); long res = 0; for(int i = 0; i<n; i++) res += 1+(long)(i+1)*(data[i]-1); out.println(res); out.close(); } static class input { 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 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() ); } static String nextLine() throws IOException { return reader.readLine(); } } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
2b783b1c0ee15a3d0a4835669c71e700
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.*; import java.math.*; import java.io.*; public class B { private void solve() throws IOException { int n = nextInt(); long[] a = new long[n]; long res = 0; for (int i = 0; i < n; i++) { a[i] = nextInt(); } for (int i = 0; i < a.length; i++) { res += (i + 1) * (a[i] - 1); res++; } System.out.println(res); } public static void main(String[] args) { new B().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int[] readIntArray(int size) throws IOException { int[] res = new int[size]; for (int i = 0; i < size; i++) { res[i] = nextInt(); } return res; } long[] readLongArray(int size) throws IOException { long[] res = new long[size]; for (int i = 0; i < size; i++) { res[i] = nextLong(); } return res; } double[] readDoubleArray(int size) throws IOException { double[] res = new double[size]; for (int i = 0; i < size; i++) { res[i] = nextDouble(); } return res; } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } BigInteger nextBigInteger() throws IOException { return new BigInteger(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
967214a9c562e7d2fedf4258f69b1bdf
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; public class A103 { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); long sum = n; for(int x = 0;x<n;x++) { int k = in.nextInt(); sum+=(k-1L)*(x+1); } System.out.println(sum); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
f7fedfcf2aca94ac86e6c72e35ad57c9
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class A { BufferedReader in; StringTokenizer str; PrintWriter out; String SK; String next() throws IOException { while ((str == null) || (!str.hasMoreTokens())) { SK = in.readLine(); if (SK == null) return null; str = new StringTokenizer(SK); } return str.nextToken(); }; int nextInt() throws IOException { return Integer.parseInt(next()); }; double nextDouble() throws IOException { return Double.parseDouble(next()); }; long nextLong() throws IOException { return Long.parseLong(next()); }; void solve() throws IOException { int n = nextInt(); long a; long res = 0; for (int i = 1; i <= n; i++) { a = nextInt(); res += (a - 1) * ((long) i); } System.out.println(res + (long) n); }; void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new A().run(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
48f61a7e93575f07bc453a7c5a4a0005
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class A implements Runnable { public void solve() throws IOException { int n = in.nextInt(); long[] a = new long[n]; for ( int i = 0; i < n; i ++ ) { a[i] = in.nextLong(); } long r = n; for ( int i = 0; i < n; i ++ ) { r += ( a[i] - 1 ) * ( i + 1 ); } out.println( r ); } public Scanner in; public PrintWriter out; A() throws IOException { in = new Scanner(System.in); // in = new StreamTokenizer( new InputStreamReader( System.in ) ); out = new PrintWriter(System.out); } // int nextInt() throws IOException { // in.nextToken(); // return ( int ) in.nval; // } void check(boolean f, String msg) { if (!f) { out.close(); throw new RuntimeException(msg); } } void close() throws IOException { out.close(); } public void run() { try { solve(); close(); } catch (Exception e) { e.printStackTrace(out); out.flush(); throw new RuntimeException(e); } } public static void main(String[] args) throws IOException { new Thread(new A()).start(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
5549aa3c8e267d40c95c95040dc8e831
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; public class Prob103A { public static void main( String[] Args ) { Scanner scan = new Scanner( System.in ); int questions = scan.nextInt(); long ans = 0; for ( long i = 1; i < questions; i++ ) ans += i * ( scan.nextLong() - 1 ); ans += questions * scan.nextLong(); System.out.println( ans ); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
dac4621641e0f211555f359ec4292f2c
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; public class B_80 { /** * @param args */ public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); long[] mas=new long[n]; for(int i=0;i<n;i++) mas[i]=in.nextLong(); long sum=mas[n-1]; for(int i=n-2;i>=0;i--){ sum+=mas[i]-1; mas[i]=sum; } sum=0; for(int i=0;i<n;i++) sum+=mas[i]; System.out.println(sum); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
ccde271aa368d93361e4c7312e07ae3e
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; public class pr1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); long n=sc.nextLong(); long x,ans=0; for(int i=1;i<=n;i++){ x=sc.nextLong(); ans+=x+(i-1)*(x-1); } System.out.println(ans); }}
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
b09143306cd0d14128282c9a6350504f
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; public class pp { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); long sum=0; long op; long[] opt=new long[n]; if(n==1){ op=sc.nextLong(); System.out.println(op); return; } int n1=0; for(int i=0;i<n;i++){ op=sc.nextLong(); sum+=op+n1*(op-1); n1++; } System.out.println(sum); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
883dd70ef27df3e145b387e83e621c2c
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.util.*; public class TestingPantsforSadness { private void solve() throws IOException { int n = nextInt(); long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); long res = 0; for (int i = 0; i < n; i++) { res += (a[i] - 1) * (i + 1); res++; } System.out.println(res); } public static void main(String[] args) { new TestingPantsforSadness().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
71c008ee181bd3ea6e614c410d3d893b
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class A implements Runnable { void solve() { int n = nextInt(); long ans = 0; for (int i = 0; i < n; i++) { long x = nextInt(); ans += (x - 1) * (i + 1) + 1; } out.println(ans); } FastScanner sc; PrintWriter out; public void run() { Locale.setDefault(Locale.US); try { sc = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); sc.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } } int nextInt() { return sc.nextInt(); } String nextToken() { return sc.nextToken(); } long nextLong() { return sc.nextLong(); } double nextDouble() { return sc.nextDouble(); } BigInteger nextBigInteger() { return sc.nextBigInteger(); } class FastScanner extends BufferedReader { StringTokenizer st; boolean eof; String buf; String curLine; boolean createST; public FastScanner(String fileName) throws FileNotFoundException { this(fileName, true); } public FastScanner(String fileName, boolean createST) throws FileNotFoundException { super(new FileReader(fileName)); this.createST = createST; nextToken(); } public FastScanner(InputStream stream) { this(stream, true); } public FastScanner(InputStream stream, boolean createST) { super(new InputStreamReader(stream)); this.createST = createST; nextToken(); } String nextLine() { String ret = curLine; if (createST) { st = null; } nextToken(); return ret; } String nextToken() { if (!createST) { try { curLine = readLine(); } catch (Exception e) { eof = true; } return null; } while (st == null || !st.hasMoreTokens()) { try { curLine = readLine(); st = new StringTokenizer(curLine); } catch (Exception e) { eof = true; break; } } String ret = buf; buf = eof ? "-1" : st.nextToken(); return ret; } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } BigInteger nextBigInteger() { return new BigInteger(nextToken()); } public void close() { try { buf = null; st = null; curLine = null; super.close(); } catch (Exception e) { } } boolean isEOF() { return eof; } } public static void main(String[] args) { new A().run(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
bf1be951362c6725bc8e6729d6ebaa8c
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.util.*; public class A { void solve() throws Exception { int n = nextInt(); long[] a = new long[n]; long[] ans = new long[n + 1]; ans[0] = 0; for (int i = 0; i < n; i++) { a[i] = nextLong(); ans[i + 1] = ans[i] + (a[i] - 1L) * (1L + i) + 1L; } out.println(ans[n]); } void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); // in = new BufferedReader(new FileReader(filename + ".in")); // out = new PrintWriter(filename + ".out"); solve(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } BufferedReader in; StringTokenizer st; PrintWriter out; final String filename = new String("A").toLowerCase(); String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(nextToken()); } long nextLong() throws Exception { return Long.parseLong(nextToken()); } double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } public static void main(String[] args) { new A().run(); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
ece55289fc362c970dab02c865040bd0
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.StringTokenizer; public class A implements Runnable { // leave empty to read from stdin/stdout private static final String TASK_NAME_FOR_IO = ""; // file names private static final String FILE_IN = TASK_NAME_FOR_IO + ".in"; private static final String FILE_OUT = TASK_NAME_FOR_IO + ".out"; BufferedReader in; PrintWriter out; StringTokenizer tokenizer = new StringTokenizer(""); public static void main(String[] args) { new Thread(new A()).start(); } int n; long[] a; long answer; private void solve() throws IOException { n = nextInt(); a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } answer = a[0]; for (int i = 1; i < n; i++) { answer += 1L + (i + 1L) * (a[i] - 1L); } out.print(answer); } public void run() { long timeStart = System.currentTimeMillis(); boolean fileIO = TASK_NAME_FOR_IO.length() > 0; try { if (fileIO) { in = new BufferedReader(new FileReader(FILE_IN)); out = new PrintWriter(new FileWriter(FILE_OUT)); } else { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); } solve(); in.close(); out.close(); } catch (IOException e) { throw new IllegalStateException(e); } long timeEnd = System.currentTimeMillis(); if (fileIO) { System.out.println("Time spent: " + (timeEnd - timeStart) + " ms"); } } private String nextToken() throws IOException { while (!tokenizer.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } tokenizer = new StringTokenizer(line); } return tokenizer.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private BigInteger nextBigInt() throws IOException { return new BigInteger(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
5b0437c6b8e646c0a20f174b972c8f31
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; public class Trying0 { public static void main(String [] args){ Scanner sc = new Scanner (System.in); long n = sc.nextLong(); long [] b = new long [(int) n]; long count = 0; for (long a = 0; a < b.length; a++) { b[(int) a] = sc.nextLong(); } for (long a=0; a<b.length; a++){ count+=(a+1)*(b[(int) a]-1)+1; } System.out.print(count); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
0cfdea8d21a4321586c61b968df8098f
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.*; public class A { public static void main(String[] args) { new A(new FastScanner()); } public A(FastScanner in) { long res = 0; int N = in.nextInt(); int[] vs = new int[N]; for (int i=0; i<N; i++) vs[i] = in.nextInt(); for (int i=0; i<N; i++) res += (i+1L)*(vs[i]-1L)+1L; System.out.printf("%d%n", res); } } class FastScanner{ int nextInt(){ try{ int c=System.in.read(); if(c==-1) return c; while(c!='-'&&(c<'0'||'9'<c)){ c=System.in.read(); if(c==-1) return c; } if(c=='-') return -nextInt(); int res=0; do{ res*=10; res+=c-'0'; c=System.in.read(); }while('0'<=c&&c<='9'); return res; }catch(Exception e){ return -1; } } long nextLong(){ try{ int c=System.in.read(); if(c==-1) return -1; while(c!='-'&&(c<'0'||'9'<c)){ c=System.in.read(); if(c==-1) return -1; } if(c=='-') return -nextLong(); long res=0; do{ res*=10; res+=c-'0'; c=System.in.read(); }while('0'<=c&&c<='9'); return res; }catch(Exception e){ return -1; } } double nextDouble(){ return Double.parseDouble(next()); } String next(){ try{ StringBuilder res=new StringBuilder(""); int c=System.in.read(); while(Character.isWhitespace(c)) c=System.in.read(); do{ res.append((char)c); }while(!Character.isWhitespace(c=System.in.read())); return res.toString(); }catch(Exception e){ return null; } } String nextLine(){ try{ StringBuilder res=new StringBuilder(""); int c=System.in.read(); while(c=='\r'||c=='\n') c=System.in.read(); do{ res.append((char)c); c=System.in.read(); }while(c!='\r'&&c!='\n'); return res.toString(); }catch(Exception e){ return null; } } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
aae297e992b5f1abe8698d6d82c2309f
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class TestingPantsForSadness { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader buff = new BufferedReader(new InputStreamReader( System.in)); int n = Integer.parseInt(buff.readLine()); StringTokenizer st = new StringTokenizer(buff.readLine()); long[] list = new long[n]; long counter = 0; for (int i = 0; i < n; i++) { list[i] = Integer.parseInt(st.nextToken()); counter += (i + 1) * (list[i] - 1) + 1; } System.out.println(counter); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
4e65ce96248188e01bc51a6daa294dd1
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.math.BigInteger; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); BigInteger res = new BigInteger(n + ""); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); for (int i = 0; i < n; i++) { res = res.add(new BigInteger((long) (i + 1) * (a[i] - 1) + "")); } System.out.println(res); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
95b15292374813b51efa9bc171a38df2
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.io.*; public class A { public static void main(String [] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); long [] answers = new long[n]; String [] input = in.readLine().split(" "); for(int i = 0; i < n; i++) { answers[i] = Long.parseLong(input[i]); } long clicks = 0; for(int i = 0; i < n; i++) { clicks += answers[i] + i*(answers[i] - 1); } System.out.println(clicks); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
26f2dca41f3c9ba8208fb26438e00f9d
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.Scanner; public class B { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long sum = 0; for (int i = 0; i < n; i++) { long temp = in.nextLong(); sum += (temp - 1) * i + temp; } System.out.println(sum); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
11a746da3baf32fe4adf23b857ffd316
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); BigInteger res = BigInteger.ZERO; for (int i = 1; i <= n; ++i) res = res.add(BigInteger.valueOf(i).multiply(BigInteger.valueOf(in.nextInt()-1)).add(BigInteger.ONE)); System.out.println(res); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
fe838086d152adfcd79bd68b59a63f99
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
import java.util.*; public class sadpants{ public static void main(String[] args){ Scanner br = new Scanner(System.in); int n = br.nextInt(); long[] counts = new long[n]; long ans = 0; for(int i = 0;i<n;i++){ counts[i] = br.nextLong(); } for(int i = 0;i<n;i++){ if(counts[i] > 1){ for(int j = 0;j<i;j++){ counts[j]+=(counts[i]-1); } } } for(int i = 0;i<n;i++){ ans+=counts[i]; } System.out.println(ans); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
fe97fa25066c48700fc6aefd0020b380
train_001.jsonl
1312714800
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
256 megabytes
//package srms; import java.util.Scanner; /** * Created by IntelliJ IDEA. * User: Jarek * Date: 26.08.11 * Time: 18:08 * To change this template use File | Settings | File Templates. */ public class Vaganych { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] ar = new long[n + 1]; ar[0] = 0; for (int i = 1; i < ar.length; ++i) { long cnt = sc.nextLong(); ar[i] = ar[i - 1] + 1 + i * (cnt - 1); } System.out.println(ar[ar.length - 1]); } }
Java
["2\n1 1", "2\n2 2", "1\n10"]
2 seconds
["2", "5", "10"]
NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Java 6
standard input
[ "implementation", "greedy", "math" ]
c8531b2ab93993b2c3467595ad0679c5
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
1,100
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
standard output
PASSED
6d56190a33caeb3a0773b02c0e9fa4ed
train_001.jsonl
1371223800
It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) — one ant in each direction. No other ant movements will happen. Ants never interfere with each other.Scientists have put a colony of n ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.util.InputMismatchException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class TaskD { final int zero = 100; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int t = in.nextInt(); int[][] a = new int[2 * zero + 1][2 * zero + 1]; a[zero][zero] = n; boolean update = true; while (update) { update = false; for (int i = 1; i < 2 * zero; ++i) for (int j = 1; j < 2 * zero; ++j) if (a[i][j] >= 4) { a[i - 1][j] += a[i][j] >> 2; a[i + 1][j] += a[i][j] >> 2; a[i][j - 1] += a[i][j] >> 2; a[i][j + 1] += a[i][j] >> 2; a[i][j] %= 4; update = true; } } while (t-- > 0) { int x = in.nextInt(); int y = in.nextInt(); if (Math.abs(x) > zero || Math.abs(y) > zero) out.println(0); else out.println(a[x + zero][y + zero]); } } } class InputReader { BufferedReader in; StringTokenizer st; public InputReader(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); eat(""); } public int nextInt() { return Integer.parseInt(next()); } public String next() { while (!st.hasMoreTokens()) eat(nextLine()); return st.nextToken(); } public String nextLine() { try { String str = in.readLine(); if (str == null) throw new InputMismatchException(); return str; } catch (IOException e) { throw new InputMismatchException(); } } public void eat(String str) { st = new StringTokenizer(str); } }
Java
["1 3\n0 1\n0 0\n0 -1", "6 5\n0 -2\n0 -1\n0 0\n0 1\n0 2"]
1 second
["0\n1\n0", "0\n1\n2\n1\n0"]
NoteIn the first sample the colony consists of the one ant, so nothing happens at all.In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops.
Java 7
standard input
[ "dfs and similar" ]
e7a07efba27f2b1f9ec7c4a8fb997b00
First input line contains integers n (0 ≤ n ≤ 30000) and t (1 ≤ t ≤ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 ≤ xi, yi ≤ 109). Queries may coincide. It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end).
2,000
Print t integers, one per line — the number of ants at the corresponding junctions when the movement of the ants stops.
standard output
PASSED
47ceb3f1420979c35674edd69770f0d0
train_001.jsonl
1371223800
It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) — one ant in each direction. No other ant movements will happen. Ants never interfere with each other.Scientists have put a colony of n ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.util.InputMismatchException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class TaskD { final int zero = 65; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int t = in.nextInt(); int[][] a = new int[2 * zero + 1][2 * zero + 1]; a[zero][zero] = n; boolean update = true; while (update) { update = false; for (int i = 1; i < 2 * zero; ++i) for (int j = 1; j < 2 * zero; ++j) if (a[i][j] >= 4) { a[i - 1][j] += a[i][j] >> 2; a[i + 1][j] += a[i][j] >> 2; a[i][j - 1] += a[i][j] >> 2; a[i][j + 1] += a[i][j] >> 2; a[i][j] %= 4; update = true; } } while (t-- > 0) { int x = in.nextInt(); int y = in.nextInt(); if (Math.abs(x) > zero || Math.abs(y) > zero) out.println(0); else out.println(a[x + zero][y + zero]); } } } class InputReader { BufferedReader in; StringTokenizer st; public InputReader(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); eat(""); } public int nextInt() { return Integer.parseInt(next()); } public String next() { while (!st.hasMoreTokens()) eat(nextLine()); return st.nextToken(); } public String nextLine() { try { String str = in.readLine(); if (str == null) throw new InputMismatchException(); return str; } catch (IOException e) { throw new InputMismatchException(); } } public void eat(String str) { st = new StringTokenizer(str); } }
Java
["1 3\n0 1\n0 0\n0 -1", "6 5\n0 -2\n0 -1\n0 0\n0 1\n0 2"]
1 second
["0\n1\n0", "0\n1\n2\n1\n0"]
NoteIn the first sample the colony consists of the one ant, so nothing happens at all.In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops.
Java 7
standard input
[ "dfs and similar" ]
e7a07efba27f2b1f9ec7c4a8fb997b00
First input line contains integers n (0 ≤ n ≤ 30000) and t (1 ≤ t ≤ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 ≤ xi, yi ≤ 109). Queries may coincide. It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end).
2,000
Print t integers, one per line — the number of ants at the corresponding junctions when the movement of the ants stops.
standard output
PASSED
c497d7cc2f8a6c66c5ad566018621036
train_001.jsonl
1371223800
It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) — one ant in each direction. No other ant movements will happen. Ants never interfere with each other.Scientists have put a colony of n ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.util.InputMismatchException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class TaskD { final int zero = 69; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int t = in.nextInt(); int[][] a = new int[2 * zero + 1][2 * zero + 1]; a[zero][zero] = n; boolean update = true; while (update) { update = false; for (int i = 1; i < 2 * zero; ++i) for (int j = 1; j < 2 * zero; ++j) if (a[i][j] >= 4) { a[i - 1][j] += a[i][j] >> 2; a[i + 1][j] += a[i][j] >> 2; a[i][j - 1] += a[i][j] >> 2; a[i][j + 1] += a[i][j] >> 2; a[i][j] %= 4; update = true; } } while (t-- > 0) { int x = in.nextInt(); int y = in.nextInt(); if (Math.abs(x) > zero || Math.abs(y) > zero) out.println(0); else out.println(a[x + zero][y + zero]); } } } class InputReader { BufferedReader in; StringTokenizer st; public InputReader(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); eat(""); } public int nextInt() { return Integer.parseInt(next()); } public String next() { while (!st.hasMoreTokens()) eat(nextLine()); return st.nextToken(); } public String nextLine() { try { String str = in.readLine(); if (str == null) throw new InputMismatchException(); return str; } catch (IOException e) { throw new InputMismatchException(); } } public void eat(String str) { st = new StringTokenizer(str); } }
Java
["1 3\n0 1\n0 0\n0 -1", "6 5\n0 -2\n0 -1\n0 0\n0 1\n0 2"]
1 second
["0\n1\n0", "0\n1\n2\n1\n0"]
NoteIn the first sample the colony consists of the one ant, so nothing happens at all.In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops.
Java 7
standard input
[ "dfs and similar" ]
e7a07efba27f2b1f9ec7c4a8fb997b00
First input line contains integers n (0 ≤ n ≤ 30000) and t (1 ≤ t ≤ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 ≤ xi, yi ≤ 109). Queries may coincide. It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end).
2,000
Print t integers, one per line — the number of ants at the corresponding junctions when the movement of the ants stops.
standard output
PASSED
ba78f46f606064732b264a4e96d70d3b
train_001.jsonl
1371223800
It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) — one ant in each direction. No other ant movements will happen. Ants never interfere with each other.Scientists have put a colony of n ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops.
256 megabytes
import java.io.BufferedInputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.InputMismatchException; import java.util.Set; import java.util.TreeSet; public class Main { private final int SIZEN = 100; private int[][] map = new int[2 * SIZEN + 1][2 * SIZEN + 1]; public void dfs(int x, int y) { if(map[y][x] < 4) { return; } int m = map[y][x] / 4; map[y][x] %= 4; map[y][x - 1] += m; dfs(x - 1, y); map[y][x + 1] += m; dfs(x + 1, y); map[y - 1][x] += m; dfs(x, y - 1); map[y + 1][x] += m; dfs(x, y + 1); } public void foo() { MyScanner scan = new MyScanner(); int n = scan.nextInt(); map[SIZEN][SIZEN] = n; dfs(SIZEN, SIZEN); int t = scan.nextInt(); for(int i = 0;i < t;++i) { int x = scan.nextInt(); int y = scan.nextInt(); if(x < -SIZEN || x > SIZEN || y < -SIZEN || y > SIZEN) { System.out.println(0); } else { System.out.println(map[y + SIZEN][x + SIZEN]); } } } public static void main(String[] args) { new Main().foo(); } class Point implements Comparable<Point> { private int x; private int y; private int num; public Point(int aX, int aY, int aNum) { x = aX; y = aY; num = aNum; } public int compareTo(Point p) { return p.num - num; } } class MyScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; BufferedInputStream bis = new BufferedInputStream(System.in); public int read() { if (-1 == numChars) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = bis.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c; } } }
Java
["1 3\n0 1\n0 0\n0 -1", "6 5\n0 -2\n0 -1\n0 0\n0 1\n0 2"]
1 second
["0\n1\n0", "0\n1\n2\n1\n0"]
NoteIn the first sample the colony consists of the one ant, so nothing happens at all.In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops.
Java 7
standard input
[ "dfs and similar" ]
e7a07efba27f2b1f9ec7c4a8fb997b00
First input line contains integers n (0 ≤ n ≤ 30000) and t (1 ≤ t ≤ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 ≤ xi, yi ≤ 109). Queries may coincide. It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end).
2,000
Print t integers, one per line — the number of ants at the corresponding junctions when the movement of the ants stops.
standard output
PASSED
090554c83f2ed12ed0d4f8e5b2486108
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; public class G1183 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int Q = in.nextInt(); StringBuilder output = new StringBuilder(); for (int q=0; q<Q; q++) { int N = in.nextInt(); Candy[] candies = new Candy[N+1]; for (int n=0; n<=N; n++) { candies[n] = new Candy(); } for (int n=0; n<N; n++) { int a = in.nextInt(); int k = in.nextInt(); candies[a].count++; if (k == 0) { candies[a].keepCount++; } } Arrays.sort(candies, new Comparator<Candy>() { @Override public int compare(Candy o1, Candy o2) { return o2.count - o1.count; } }); int total = 0; int totalNoKeep = 0; Queue<Candy> queue = new PriorityQueue<>(new Comparator<Candy>(){ @Override public int compare(Candy o1, Candy o2) { int noKeep1 = o1.count - o1.keepCount; int noKeep2 = o2.count - o2.keepCount; return noKeep2 - noKeep1; } }); int idx = 0; for (int max = N; max > 0; max--) { while (idx < N && candies[idx].count == max) { queue.add(candies[idx]); idx++; } if (!queue.isEmpty()) { Candy candy = queue.poll(); total += max; totalNoKeep += Math.min(max, candy.count - candy.keepCount); } } output.append(total).append(' ').append(totalNoKeep).append('\n'); } System.out.print(output); } static class Candy { int count; int keepCount; } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
b194ef3a550e58728ecd8151ff7f0d9e
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); InputReader in = new InputReader(inputStream); // for(int i=4;i<=4;i++) { // InputStream uinputStream = new FileInputStream("leftout.in"); // String f = i+".in"; // InputStream uinputStream = new FileInputStream(f); // InputReader in = new InputReader(uinputStream); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("leftout.out"))); Task t = new Task(); t.solve(in, out); out.close(); // } } static class Task { int times = 0; public void solve(InputReader in, PrintWriter out) throws IOException { // //int xxx = in.nextInt(); // //while(xxx-->0) { // int n = in.nextInt(); // int k = in.nextInt(); // int a[] = in.readIntArray(n); // int b[] = in.readIntArray(n); // ArrayList<Integer>[] g = new ArrayList[n]; // for(int i=0;i<n;i++) g[i] = new ArrayList<Integer>(); // for(int i=0;i<n-1;i++) { // g[a[i]-1].add(a[i+1]-1); // g[b[i]-1].add(b[i+1]-1); // } // boolean vis[] = new boolean[n]; // int lvl[] = new int[n]; // int low[] = new int[n]; // int hash[] = new int[n]; // times = 0; // dfs(a[0]-1,g,vis,low,lvl,hash); // int cycles = 0; int cycle_nodes = 0; // DSU dsu = new DSU(n); // for(int i=0;i<n;i++) { // if(hash[low[i]]<i) { // dsu.union(hash[low[i]], i); // }else { // dsu.union(i,hash[low[i]]); // } // } // HashMap<Integer,Integer> s = new HashMap<Integer,Integer>(); // for(int i=0;i<n;i++) { // int tmp = dsu.find(i); // s.put(tmp, s.getOrDefault(tmp, 0)+1); // //low[i] = s.get(dsu.find(i)); // } // for(int i:s.keySet()) { // if(s.get(i)>1) { // cycles++; // cycle_nodes+=s.get(i); // } // } // if(k==3) { // //Dumper.print("here"); // } // if(n-cycle_nodes+cycles>=k) { // System.out.println("YES"); // //out.println("YES"); // int temp [] = new int[n]; // for(int i=0;i<n;i++) temp[i] = low[a[i]-1]; // for(int i=1;i<n;i++) { // if(temp[i]>temp[i-1]+1) temp[i] = temp[i-1]+1; // if(temp[i]>=k) temp[i] = temp[i-1]; // } // for(int i=0;i<n;i++) { // low[a[i]-1] = temp[i]; // } // for(int i=0;i<n;i++) { // System.out.print((char)('a'+low[i])); // //out.print((char)('a'+low[i])); // } // System.out.println(); // //out.println(); // } // else { // System.out.println("NO"); // //out.println("NO"); // } // //} int c = in.nextInt(); while(c-->0) { int n = in.nextInt(); HashMap<Integer,Integer> num = new HashMap<Integer,Integer>(); HashMap<Integer,Integer> ddlike = new HashMap<Integer,Integer>(); for(int i=0;i<n;i++) { int t = in.nextInt(); int l = in.nextInt(); num.put(t, num.getOrDefault(t, 0)+1); ddlike.put(t, ddlike.getOrDefault(t, 0)); if(l==1) ddlike.put(t, ddlike.get(t)+1); } ArrayList<pair> arr = new ArrayList<pair>(); for(int type:num.keySet()) { arr.add(new pair(num.get(type), num.get(type)-ddlike.get(type), ddlike.get(type))); } Collections.sort(arr, new Comparator<pair>() { public int compare(pair e1, pair e2) { return e1.num-e2.num; } }); PriorityQueue<pair> pq = new PriorityQueue<pair>(); int cur = arr.get(arr.size()-1).num; int p = arr.size()-1; int cnt = 0; int dlike = 0; while(cur>0&&(p>=0||!pq.isEmpty())) { while(p>=0&&arr.get(p).num>=cur) pq.add(arr.get(p--)); if(!pq.isEmpty()) { pair tmp = pq.poll(); cnt+=Math.min(tmp.num, cur); dlike+=Math.min(tmp.dlike, cur); } cur--; } out.println(cnt+" "+dlike); } } class pair implements Comparable<pair>{ int num, like, dlike; public pair(int b, int c, int d) { num=b;like=c;dlike=d; } public int compareTo(pair x) { return x.dlike-this.dlike; } } public int maxLength(List<String> arr) { int n = arr.size(); int w[] = new int[n]; int[][] a = new int[n][26]; for(int i=0;i<n;i++) { String tmp = arr.get(i); w[i] = tmp.length(); for(int j=0;j<tmp.length();j++) { a[i][tmp.charAt(j)-'a']++; if(a[i][tmp.charAt(j)-'a']>1) w[i]=0; } } boolean g[][] = new boolean[n][n]; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { boolean flag = false; for(int k=0;k<26;k++) { if(a[i][k]!=0&&a[j][k]!=0) flag = true; } if(!flag) g[i][j] = g[j][i] = true; } } int max = 0; for(int i=0;i<(1<<n);i++) { boolean flag = true; for(int j=0;j<n;j++) {if((i&(1<<j))==0) continue; if((i&(1<<j))==0||!flag) continue; for(int k=j+1;k<n;k++) { if((i&(1<<k))==0||!flag) continue; if(!g[j][k]) flag = false; } } if(!flag) continue; //Dumper.print(i); int s = 0; for(int j=0;j<n;j++) if((i&(1<<j))==(1<<j)) s+=w[j]; if(s>max) max = s; } return max; } int dfs(int cur, ArrayList<Integer>[] g, boolean vis[], int low[], int[] lvl, int hash[]) { lvl[cur] = low[cur] = times; hash[times] = cur; times++; int reach = low[cur]; vis[cur] = true; for(int to:g[cur]) { if(vis[to]) { reach = Math.min(reach, low[to]); }else { int tmp = dfs(to,g,vis,low,lvl,hash); reach = Math.min(reach, tmp); } } if(reach<low[cur]) low[cur] = reach; return low[cur]; } class edge implements Comparable<edge>{ int f,t,l,s; public edge(int a, int b, int c) { if(a<b){ f=a;t=b; }else { f=b;t=a; } l=c; } @Override public int compareTo(edge t) { return this.l-t.l; } } List<List<Integer>> convert(int arr[][]){ int n = arr.length; List<List<Integer>> ret = new ArrayList<>(); for(int i=0;i<n;i++) { ArrayList<Integer> tmp = new ArrayList<Integer>(); for(int j=0;j<arr[i].length;j++) tmp.add(arr[i][j]); ret.add(tmp); } return ret; } public int maximumSum(int[] arr) { int n = arr.length; int dp[][] = new int[n][2]; dp[0][0] = arr[0]; int ret = arr[0]; for(int i=1;i<n;i++) { dp[i][0] = dp[i-1][0]>=0?(dp[i-1][0]+arr[i]):arr[i]; int t1 = dp[i-1][0]; int t2 = (dp[i-1][1]<0?0:dp[i-1][1])+arr[i]; dp[i][1] = Math.max(t1,t2); ret = Math.max(ret,Math.max(dp[i][0], dp[i][1])); } return ret; } public List<Integer> findNumOfValidWords(String[] words, String[] puzzles) { List<Integer> ret = new ArrayList<Integer>(); HashMap<Integer,Integer>[] mp = new HashMap[26]; for(int i=0;i<26;i++) mp[i] = new HashMap<Integer,Integer>(); //String p = "abcdz"; //Dumper.print(enc(p)); for(int i=0;i<words.length;i++) { int v = enc(words[i]); for(int j=0;j<words[i].length();j++) { mp[words[i].charAt(j)-'a'].put(v, 1); } } for(int i=0;i<puzzles.length;i++) { int n = puzzles[i].length(); int t = 0; for(int j=1;j<1<<(n-1);j++) { int v = 0; for(int k=0;k<6;k++) { if((j&(1<<k))!=0) { v|=(1<<(puzzles[i].charAt(k)-'a')); } } v|=(1<<(puzzles[i].charAt(0)-'a')); //t+=search(v,mp[puzzles[i].charAt(0)-'a']); } ret.add(t); } return ret; } int search(int x, HashMap<Integer,Integer> mp, HashSet<Integer> hs) { if(mp.containsKey(x)) return mp.get(x); if(x==0) return 0; int v = 0; for(int i=0;i<7;i++) { if((x&(1<<i))!=0) { int t = x; t^=(1<<i); if(!hs.contains(t)) { hs.add(t); v+=search(t,mp,hs); } } } mp.put(x, v); return v; } int enc(String x) { char[] arr = x.toCharArray(); int v = 0; for(int i=0;i<arr.length;i++) { v|=1<<(arr[i]-'a'); } return v; } public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public int GCD(int a, int b) { if (b==0) return a; return GCD(b,a%b); } public long GCD(long a, long b) { if (b==0) return a; return GCD(b,a%b); } } static class ArrayUtils { static final long seed = System.nanoTime(); static final Random rand = new Random(seed); public static void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(int[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); int t = a[i]; a[i] = a[j]; a[j] = t; } } } static class BIT{ int arr[]; int n; public BIT(int a) { n=a; arr = new int[n]; } int sum(int p) { int s=0; while(p>0) { s+=arr[p]; p-=p&(-p); } return s; } void add(int p, int v) { while(p<n) { arr[p]+=v; p+=p&(-p); } } } static class DSU{ int[] arr; int[] sz; public DSU(int n) { arr = new int[n]; sz = new int[n]; for(int i=0;i<n;i++) arr[i] = i; Arrays.fill(sz, 1); } public int find(int a) { if(arr[a]!=a) arr[a] = find(arr[a]); return arr[a]; } public void union(int a, int b) { int x = find(a); int y = find(b); if(x==y) return; arr[y] = x; sz[x] += sz[y]; } public int size(int x) { return sz[find(x)]; } } static class MinHeap<Key> implements Iterable<Key> { private int maxN; private int n; private int[] pq; private int[] qp; private Key[] keys; private Comparator<Key> comparator; public MinHeap(int capacity){ if (capacity < 0) throw new IllegalArgumentException(); this.maxN = capacity; n=0; pq = new int[maxN+1]; qp = new int[maxN+1]; keys = (Key[]) new Object[capacity+1]; Arrays.fill(qp, -1); } public MinHeap(int capacity, Comparator<Key> c){ if (capacity < 0) throw new IllegalArgumentException(); this.maxN = capacity; n=0; pq = new int[maxN+1]; qp = new int[maxN+1]; keys = (Key[]) new Object[capacity+1]; Arrays.fill(qp, -1); comparator = c; } public boolean isEmpty() { return n==0; } public int size() { return n; } public boolean contains(int i) { if (i < 0 || i >= maxN) throw new IllegalArgumentException(); return qp[i] != -1; } public int peekIdx() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); return pq[1]; } public Key peek(){ if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); return keys[pq[1]]; } public int poll(){ if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); int min = pq[1]; exch(1,n--); down(1); assert min==pq[n+1]; qp[min] = -1; keys[min] = null; pq[n+1] = -1; return min; } public void update(int i, Key key) { if (i < 0 || i >= maxN) throw new IllegalArgumentException(); if (!contains(i)) { this.add(i, key); }else { keys[i] = key; up(qp[i]); down(qp[i]); } } private void add(int i, Key x){ if (i < 0 || i >= maxN) throw new IllegalArgumentException(); if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue"); n++; qp[i] = n; pq[n] = i; keys[i] = x; up(n); } private void up(int k){ while(k>1&&less(k,k/2)){ exch(k,k/2); k/=2; } } private void down(int k){ while(2*k<=n){ int j=2*k; if(j<n&&less(j+1,j)) j++; if(less(k,j)) break; exch(k,j); k=j; } } public boolean less(int i, int j){ if (comparator == null) { return ((Comparable<Key>) keys[pq[i]]).compareTo(keys[pq[j]]) < 0; } else { return comparator.compare(keys[pq[i]], keys[pq[j]]) < 0; } } public void exch(int i, int j){ int swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; qp[pq[i]] = i; qp[pq[j]] = j; } @Override public Iterator<Key> iterator() { // TODO Auto-generated method stub return null; } } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int zcurChar; private int znumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (znumChars == -1) throw new InputMismatchException(); if (zcurChar >= znumChars) { zcurChar = 0; try { znumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (znumChars <= 0) return -1; } return buf[zcurChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } static class Dumper { static void print_int_arr(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_char_arr(char[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_double_arr(double[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(int[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(boolean[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print(Object o) { System.out.println(o.toString()); } static void getc() { System.out.println("here"); } } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
d9f29bdd921d87e5d5feeb363604c90f
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); InputReader in = new InputReader(inputStream); // for(int i=4;i<=4;i++) { // InputStream uinputStream = new FileInputStream("leftout.in"); // String f = i+".in"; // InputStream uinputStream = new FileInputStream(f); // InputReader in = new InputReader(uinputStream); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("leftout.out"))); Task t = new Task(); t.solve(in, out); out.close(); // } } static class Task { int times = 0; public void solve(InputReader in, PrintWriter out) throws IOException { // //int xxx = in.nextInt(); // //while(xxx-->0) { // int n = in.nextInt(); // int k = in.nextInt(); // int a[] = in.readIntArray(n); // int b[] = in.readIntArray(n); // ArrayList<Integer>[] g = new ArrayList[n]; // for(int i=0;i<n;i++) g[i] = new ArrayList<Integer>(); // for(int i=0;i<n-1;i++) { // g[a[i]-1].add(a[i+1]-1); // g[b[i]-1].add(b[i+1]-1); // } // boolean vis[] = new boolean[n]; // int lvl[] = new int[n]; // int low[] = new int[n]; // int hash[] = new int[n]; // times = 0; // dfs(a[0]-1,g,vis,low,lvl,hash); // int cycles = 0; int cycle_nodes = 0; // DSU dsu = new DSU(n); // for(int i=0;i<n;i++) { // if(hash[low[i]]<i) { // dsu.union(hash[low[i]], i); // }else { // dsu.union(i,hash[low[i]]); // } // } // HashMap<Integer,Integer> s = new HashMap<Integer,Integer>(); // for(int i=0;i<n;i++) { // int tmp = dsu.find(i); // s.put(tmp, s.getOrDefault(tmp, 0)+1); // //low[i] = s.get(dsu.find(i)); // } // for(int i:s.keySet()) { // if(s.get(i)>1) { // cycles++; // cycle_nodes+=s.get(i); // } // } // if(k==3) { // //Dumper.print("here"); // } // if(n-cycle_nodes+cycles>=k) { // System.out.println("YES"); // //out.println("YES"); // int temp [] = new int[n]; // for(int i=0;i<n;i++) temp[i] = low[a[i]-1]; // for(int i=1;i<n;i++) { // if(temp[i]>temp[i-1]+1) temp[i] = temp[i-1]+1; // if(temp[i]>=k) temp[i] = temp[i-1]; // } // for(int i=0;i<n;i++) { // low[a[i]-1] = temp[i]; // } // for(int i=0;i<n;i++) { // System.out.print((char)('a'+low[i])); // //out.print((char)('a'+low[i])); // } // System.out.println(); // //out.println(); // } // else { // System.out.println("NO"); // //out.println("NO"); // } // //} int c = in.nextInt(); while(c-->0) { int n = in.nextInt(); int num[] = new int[n]; int dlike[] = new int[n]; int max = 0; for(int i=0;i<n;i++) { int t = in.nextInt()-1; int l = in.nextInt(); num[t]++; if(l==1) dlike[t]++; } ArrayList<Integer>[] arr = new ArrayList[n+1]; for(int i=0;i<=n;i++) arr[i] = new ArrayList<Integer>(); for(int i=0;i<n;i++) { if(num[i]>0) arr[num[i]].add(dlike[i]); if(num[i]>max) max = num[i]; } int cnt = 0; int ddlike = 0; PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder()); for(int i=max;i>0;i--) { pq.addAll(arr[i]); if(pq.isEmpty()) continue; cnt+=i; ddlike+=Math.min(i, pq.poll()); } out.println(cnt+" "+ddlike); } } class pair implements Comparable<pair>{ int num, like, dlike; public pair(int b, int c, int d) { num=b;like=c;dlike=d; } public int compareTo(pair x) { return x.dlike-this.dlike; } } public int maxLength(List<String> arr) { int n = arr.size(); int w[] = new int[n]; int[][] a = new int[n][26]; for(int i=0;i<n;i++) { String tmp = arr.get(i); w[i] = tmp.length(); for(int j=0;j<tmp.length();j++) { a[i][tmp.charAt(j)-'a']++; if(a[i][tmp.charAt(j)-'a']>1) w[i]=0; } } boolean g[][] = new boolean[n][n]; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { boolean flag = false; for(int k=0;k<26;k++) { if(a[i][k]!=0&&a[j][k]!=0) flag = true; } if(!flag) g[i][j] = g[j][i] = true; } } int max = 0; for(int i=0;i<(1<<n);i++) { boolean flag = true; for(int j=0;j<n;j++) {if((i&(1<<j))==0) continue; if((i&(1<<j))==0||!flag) continue; for(int k=j+1;k<n;k++) { if((i&(1<<k))==0||!flag) continue; if(!g[j][k]) flag = false; } } if(!flag) continue; //Dumper.print(i); int s = 0; for(int j=0;j<n;j++) if((i&(1<<j))==(1<<j)) s+=w[j]; if(s>max) max = s; } return max; } int dfs(int cur, ArrayList<Integer>[] g, boolean vis[], int low[], int[] lvl, int hash[]) { lvl[cur] = low[cur] = times; hash[times] = cur; times++; int reach = low[cur]; vis[cur] = true; for(int to:g[cur]) { if(vis[to]) { reach = Math.min(reach, low[to]); }else { int tmp = dfs(to,g,vis,low,lvl,hash); reach = Math.min(reach, tmp); } } if(reach<low[cur]) low[cur] = reach; return low[cur]; } class edge implements Comparable<edge>{ int f,t,l,s; public edge(int a, int b, int c) { if(a<b){ f=a;t=b; }else { f=b;t=a; } l=c; } @Override public int compareTo(edge t) { return this.l-t.l; } } List<List<Integer>> convert(int arr[][]){ int n = arr.length; List<List<Integer>> ret = new ArrayList<>(); for(int i=0;i<n;i++) { ArrayList<Integer> tmp = new ArrayList<Integer>(); for(int j=0;j<arr[i].length;j++) tmp.add(arr[i][j]); ret.add(tmp); } return ret; } public int maximumSum(int[] arr) { int n = arr.length; int dp[][] = new int[n][2]; dp[0][0] = arr[0]; int ret = arr[0]; for(int i=1;i<n;i++) { dp[i][0] = dp[i-1][0]>=0?(dp[i-1][0]+arr[i]):arr[i]; int t1 = dp[i-1][0]; int t2 = (dp[i-1][1]<0?0:dp[i-1][1])+arr[i]; dp[i][1] = Math.max(t1,t2); ret = Math.max(ret,Math.max(dp[i][0], dp[i][1])); } return ret; } public List<Integer> findNumOfValidWords(String[] words, String[] puzzles) { List<Integer> ret = new ArrayList<Integer>(); HashMap<Integer,Integer>[] mp = new HashMap[26]; for(int i=0;i<26;i++) mp[i] = new HashMap<Integer,Integer>(); //String p = "abcdz"; //Dumper.print(enc(p)); for(int i=0;i<words.length;i++) { int v = enc(words[i]); for(int j=0;j<words[i].length();j++) { mp[words[i].charAt(j)-'a'].put(v, 1); } } for(int i=0;i<puzzles.length;i++) { int n = puzzles[i].length(); int t = 0; for(int j=1;j<1<<(n-1);j++) { int v = 0; for(int k=0;k<6;k++) { if((j&(1<<k))!=0) { v|=(1<<(puzzles[i].charAt(k)-'a')); } } v|=(1<<(puzzles[i].charAt(0)-'a')); //t+=search(v,mp[puzzles[i].charAt(0)-'a']); } ret.add(t); } return ret; } int search(int x, HashMap<Integer,Integer> mp, HashSet<Integer> hs) { if(mp.containsKey(x)) return mp.get(x); if(x==0) return 0; int v = 0; for(int i=0;i<7;i++) { if((x&(1<<i))!=0) { int t = x; t^=(1<<i); if(!hs.contains(t)) { hs.add(t); v+=search(t,mp,hs); } } } mp.put(x, v); return v; } int enc(String x) { char[] arr = x.toCharArray(); int v = 0; for(int i=0;i<arr.length;i++) { v|=1<<(arr[i]-'a'); } return v; } public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public int GCD(int a, int b) { if (b==0) return a; return GCD(b,a%b); } public long GCD(long a, long b) { if (b==0) return a; return GCD(b,a%b); } } static class ArrayUtils { static final long seed = System.nanoTime(); static final Random rand = new Random(seed); public static void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(int[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); int t = a[i]; a[i] = a[j]; a[j] = t; } } } static class BIT{ int arr[]; int n; public BIT(int a) { n=a; arr = new int[n]; } int sum(int p) { int s=0; while(p>0) { s+=arr[p]; p-=p&(-p); } return s; } void add(int p, int v) { while(p<n) { arr[p]+=v; p+=p&(-p); } } } static class DSU{ int[] arr; int[] sz; public DSU(int n) { arr = new int[n]; sz = new int[n]; for(int i=0;i<n;i++) arr[i] = i; Arrays.fill(sz, 1); } public int find(int a) { if(arr[a]!=a) arr[a] = find(arr[a]); return arr[a]; } public void union(int a, int b) { int x = find(a); int y = find(b); if(x==y) return; arr[y] = x; sz[x] += sz[y]; } public int size(int x) { return sz[find(x)]; } } static class MinHeap<Key> implements Iterable<Key> { private int maxN; private int n; private int[] pq; private int[] qp; private Key[] keys; private Comparator<Key> comparator; public MinHeap(int capacity){ if (capacity < 0) throw new IllegalArgumentException(); this.maxN = capacity; n=0; pq = new int[maxN+1]; qp = new int[maxN+1]; keys = (Key[]) new Object[capacity+1]; Arrays.fill(qp, -1); } public MinHeap(int capacity, Comparator<Key> c){ if (capacity < 0) throw new IllegalArgumentException(); this.maxN = capacity; n=0; pq = new int[maxN+1]; qp = new int[maxN+1]; keys = (Key[]) new Object[capacity+1]; Arrays.fill(qp, -1); comparator = c; } public boolean isEmpty() { return n==0; } public int size() { return n; } public boolean contains(int i) { if (i < 0 || i >= maxN) throw new IllegalArgumentException(); return qp[i] != -1; } public int peekIdx() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); return pq[1]; } public Key peek(){ if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); return keys[pq[1]]; } public int poll(){ if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); int min = pq[1]; exch(1,n--); down(1); assert min==pq[n+1]; qp[min] = -1; keys[min] = null; pq[n+1] = -1; return min; } public void update(int i, Key key) { if (i < 0 || i >= maxN) throw new IllegalArgumentException(); if (!contains(i)) { this.add(i, key); }else { keys[i] = key; up(qp[i]); down(qp[i]); } } private void add(int i, Key x){ if (i < 0 || i >= maxN) throw new IllegalArgumentException(); if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue"); n++; qp[i] = n; pq[n] = i; keys[i] = x; up(n); } private void up(int k){ while(k>1&&less(k,k/2)){ exch(k,k/2); k/=2; } } private void down(int k){ while(2*k<=n){ int j=2*k; if(j<n&&less(j+1,j)) j++; if(less(k,j)) break; exch(k,j); k=j; } } public boolean less(int i, int j){ if (comparator == null) { return ((Comparable<Key>) keys[pq[i]]).compareTo(keys[pq[j]]) < 0; } else { return comparator.compare(keys[pq[i]], keys[pq[j]]) < 0; } } public void exch(int i, int j){ int swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; qp[pq[i]] = i; qp[pq[j]] = j; } @Override public Iterator<Key> iterator() { // TODO Auto-generated method stub return null; } } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int zcurChar; private int znumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (znumChars == -1) throw new InputMismatchException(); if (zcurChar >= znumChars) { zcurChar = 0; try { znumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (znumChars <= 0) return -1; } return buf[zcurChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } static class Dumper { static void print_int_arr(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_char_arr(char[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_double_arr(double[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(int[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(boolean[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print(Object o) { System.out.println(o.toString()); } static void getc() { System.out.println("here"); } } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
cebd4b217e18673c9e8216994735bd5a
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class q5 { static boolean check(int a) { String s=Integer.toString(a); int sum=0; for(int i=0;i<s.length();i++) { sum+=(s.charAt(i)-'0'); } return (sum%4==0); } public static void main(String[] args) throws IOException { Reader.init(System.in); PrintWriter out=new PrintWriter(System.out); int q=Reader.nextInt(); while(q>0) { q--; int n=Reader.nextInt(); int[] count=new int[n+1],count2=new int[n+1]; int ans2=0; for(int i=0;i<n;i++) { int a=Reader.nextInt(); count[a]++; int b=Reader.nextInt(); if(b==1) { count2[a]++; } } ArrayList<Integer> arr=new ArrayList<Integer>(); ArrayList<NoD> arr2=new ArrayList<NoD>(); ArrayList<Integer> add=new ArrayList<Integer>(); int lastinclude=n+1; for(int i=1;i<=n;i++) { if(count[i]>0) arr.add(count[i]); arr2.add(new NoD(count[i],count2[i])); } Collections.sort(arr);Collections.reverse(arr); Collections.sort(arr2,new Comparator<NoD>() { public int compare(NoD a, NoD b) { return b.a1-a.a1; } }); PriorityQueue<NoD> pq=new PriorityQueue<NoD>(new Comparator<NoD>() { public int compare(NoD a, NoD b) { return b.a2-a.a2; } }); int ans=0; for(int i=0;i<arr.size();i++) { int a=arr.get(i); if(lastinclude<=0) break; if(a>=lastinclude ) { ans+=lastinclude-1; lastinclude--; add.add(lastinclude); } else { ans+=a; lastinclude=a; add.add(lastinclude); } } int index=0; for(int i=0;i<add.size();i++) { int a=add.get(i); while(index<arr2.size()) { NoD b=arr2.get(index); if(b.a1>=a) { pq.add(b); index++; } else break; } NoD b=pq.poll(); ans2+=Math.min(a, b.a2); } out.println(ans+" "+ans2); } out.flush(); } } class NoD{ int a1, a2; NoD(int a,int b){ a1=a;a2=b; } } 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 nextLine() throws IOException{ return reader.readLine(); } static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
d58159712bad533ac2c2dc29e8850b4a
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.*; import java.util.*; public class Task { public static void main(String[] args) throws IOException { new Task().go(); } PrintWriter out; Reader in; BufferedReader br; Task() throws IOException { try { //br = new BufferedReader( new FileReader("input.txt") ); in = new Reader("input.txt"); out = new PrintWriter( new BufferedWriter(new FileWriter("output.txt")) ); } catch (Exception e) { //br = new BufferedReader( new InputStreamReader( System.in ) ); in = new Reader(); out = new PrintWriter( new BufferedWriter(new OutputStreamWriter(System.out)) ); } } void go() throws IOException { int t = 1; while (t > 0) { solve(); //out.println(); t--; } out.flush(); out.close(); } int inf = 2000000000; int mod = 1000000007; double eps = 0.000000001; int n; int m; ArrayList<Integer>[] g; void solve() throws IOException { int q = in.nextInt(); while (q > 0) { int n = in.nextInt(); HashMap<Integer, Pair> map = new HashMap<>(); HashMap<Integer, ArrayList<Integer>> inv = new HashMap<>(); for (int i = 0; i < n; i++) { int x = in.nextInt(); int f = in.nextInt(); Pair p = map.getOrDefault(x, new Pair(0, 0)); Pair np = new Pair(p.a + 1, p.b); if (f == 1) np.b++; map.put(x, np); } TreeSet<Pair> set = new TreeSet<>(); for (int k : map.keySet()) { Pair p = map.get(k); if (!inv.containsKey(p.a)) inv.put(p.a, new ArrayList<>()); inv.get(p.a).add(k); } int max = n; long ans = 0; long ansf = 0; while (max > 0) { if (inv.containsKey(max)) { for (int k : inv.get(max)) { Pair p = map.get(k); set.add(new Pair(-p.b, k)); } } Pair first = set.pollFirst(); if (first != null) { ans += max; ansf += Math.min(-first.a, max); } max--; } out.println(ans + " " + ansf); q--; } } class Pair implements Comparable<Pair> { int a; int b; Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair p) { if (a != p.a) return Integer.compare(a, p.a); else return Integer.compare(b, p.b); } } class Item { int a; int b; int c; Item(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } } class Reader { BufferedReader br; StringTokenizer tok; Reader(String file) throws IOException { br = new BufferedReader( new FileReader(file) ); } Reader() throws IOException { br = new BufferedReader( new InputStreamReader(System.in) ); } String next() throws IOException { while (tok == null || !tok.hasMoreElements()) tok = new StringTokenizer(br.readLine()); return tok.nextToken(); } int nextInt() throws NumberFormatException, IOException { return Integer.valueOf(next()); } long nextLong() throws NumberFormatException, IOException { return Long.valueOf(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.valueOf(next()); } String nextLine() throws IOException { return br.readLine(); } int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } ArrayList<Integer>[] nextGraph(int n, int m) throws IOException { ArrayList<Integer>[] g = new ArrayList[n]; for (int i = 0; i < n; i++) g[i] = new ArrayList<>(); for (int i = 0; i < m; i++) { int x = nextInt() - 1; int y = nextInt() - 1; g[x].add(y); g[y].add(x); } return g; } } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
c47c247e297bcfd3badd90816e41a597
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.*; import java.util.*; public class Main implements Runnable { public static void main (String[] args) {new Thread(null, new Main(), "_cf", 1 << 28).start();} public void run() { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); System.err.println(""); int q = fs.nextInt(); while(q-->0) { int n = fs.nextInt(); int[] a = new int[n]; int[][] data = new int[n][2]; for(int i = 0; i < n; i++) { a[i] = fs.nextInt(); data[i][0] = a[i]; data[i][1] = fs.nextInt(); } sort(a); long res = 0; ArrayList<Integer> list = new ArrayList<>(); for(int i = 0; i < n; i++) { int j = i; while(j < n && a[i] == a[j]) j++; int len = j-i; list.add(len); i = --j; } Collections.sort(list, Collections.reverseOrder()); int lim = Integer.MAX_VALUE; ArrayList<Integer> sizes = new ArrayList<>(); for(int i : list) { if(lim <= 0) break; if(i < lim) { sizes.add(i); res += i; lim = i-1; } else { res += lim; sizes.add(lim); lim--; } } HashMap<Integer, Integer> map = new HashMap<>(); ArrayList<Group> list2 = new ArrayList<>(); for(int i = 0; i < data.length; i++) { int v = data[i][0]; if(!map.containsKey(v)) { map.put(v, list2.size()); list2.add(new Group(v)); } int loc = map.get(v); list2.get(loc).freq++; if(data[i][1] == 1) list2.get(loc).count1++; } Collections.sort(list2); int[] cnt = new int[list2.size()]; for(int i = 0; i < list2.size(); i++ ) { cnt[i] = list2.get(i).count1; } ST st = new ST(cnt.length, cnt.clone()); long max = 0; int ptr = list2.size()-1; for(int i : sizes) { while(ptr >= 0 && list2.get(ptr).freq >= i) ptr--; ptr++; int idx = st.query(1, ptr, cnt.length-1); max += Math.min(cnt[idx], i); cnt[idx] = -1; st.kill(1, idx); } out.println(res + " " + max); } out.close(); } class Group implements Comparable<Group> { int type, freq, count1; Group(int t) { type = t; } public int compareTo(Group g) { return Integer.compare(freq, g.freq); } } class ST { int n; int[] lo, hi; int[] maxIdx, vals; ST(int a, int[] v) { n = 4*a+100; lo = new int[n]; hi = new int[n]; maxIdx = new int[n]; vals = v; init(1, 0, a-1); } void init(int p, int l, int r) { lo[p] = l; hi[p] = r; if(l == r) { maxIdx[p] = l; return; } int mid = (l+r)/2; int lf = 2*p, rg = 1+lf; init(lf, l, mid); init(rg, mid+1, r); if(vals[maxIdx[lf]] > vals[maxIdx[rg]]) maxIdx[p] = maxIdx[lf]; else maxIdx[p] = maxIdx[rg]; } int query(int p, int L, int R) { if(hi[p] < L || R < lo[p]) return -1; if(L <= lo[p] && hi[p] <= R) { return maxIdx[p]; } int lf = 2*p, rg = 1+lf; int k1 = query(lf, L, R); int k2 = query(rg, L, R); if(k1 == -1) return k2; if(k2 == -1) return k1; if(vals[k1] > vals[k2]) return k1; return k2; } void kill(int p, int idx) { if(lo[p] == hi[p]) { vals[idx] = -1; return; } int lf = 2*p, rg = 1+lf; if(lo[lf] <= idx && idx <= hi[lf]) kill(lf, idx); else kill(rg, idx); if(maxIdx[lf] == -1) maxIdx[p] = maxIdx[rg]; else if(maxIdx[rg] == -1) maxIdx[p] = maxIdx[lf]; else { if(vals[maxIdx[lf]] > vals[maxIdx[rg]]) maxIdx[p] = maxIdx[lf]; else maxIdx[p] = maxIdx[rg]; } } } void sort(int[] a) { Random rand = new Random(); int n = a.length; for(int i = 0; i < n; i++) { int x = rand.nextInt(n); int y = rand.nextInt(n); int t = a[x]; a[x] = a[y]; a[y] = t; } Arrays.sort(a); } int sum(int a) { int sum = 0; while(a > 0) { sum += a % 10; a /= 10; } return sum % 4; } class FastScanner { 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 FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } public char 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; } } public int[] nextIntArray(int n) { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
71548e52a8665636bc1acae66d6d811b
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.*; import java.util.*; public class Main { static class Scanner { BufferedReader buf; StringTokenizer tok; Scanner() { buf = new BufferedReader(new InputStreamReader(System.in)); } boolean hasNext() { while(tok == null || !tok.hasMoreElements()) { try { tok = new StringTokenizer(buf.readLine()); } catch(Exception e) { return false; } } return true; } String next() { if(hasNext()) return tok.nextToken(); return null; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } static class candy{ int like; int num; candy(int l, int n) { like = l; num = n; } } public static void main(String[] args) { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); int q = sc.nextInt(); while(q-- > 0) { int n = sc.nextInt(); HashMap<Integer,Integer> map = new HashMap<>(); List<candy> list = new ArrayList<>(); int idx = 0; for(int i = 0; i < n; i++) { int num = sc.nextInt(), f = sc.nextInt(); if(map.containsKey(num)) { candy c = list.get(map.get(num)); c.num++; c.like += f; } else { map.put(num,idx++); list.add(new candy(f,1)); } } Collections.sort(list,(a,b)->(a.num-b.num == 0 ? a.like-b.like : a.num-b.num)); int max = list.get(list.size()-1).num,sum = 0, like = 0; for(int i = list.size()-1; i >= 0; i--) { max = Math.min(list.get(i).num, max); if(max == 0) break; sum += max; int cur = i, max_like = list.get(i).like, max_idx = cur; while(cur > 0) { if(list.get(cur-1).num < max) break; cur--; if(list.get(cur).like > max_like) { max_idx = cur; max_like = list.get(cur).like; } } like += Math.min(max,max_like); max--; if(max_idx != i) list.remove(max_idx); } out.print(sum + " "); out.println(like); } out.close(); } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
028e627ddc816ed06fa251f61ad29ccc
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces1183 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int q = Integer.parseInt(st.nextToken()); int[][] results = new int[q][]; for (int i = 0; i < q; i++) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int[][] candies = new int[n][]; for (int j = 0; j < n; j++) { st = new StringTokenizer(br.readLine()); candies[j] = new int[]{Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())}; } int[] result = packTheBox(candies); results[i] = result; } PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8"), 512)); for (int i = 0; i < q; i++) { out.print(results[i][0]); out.print(" "); out.print(results[i][1]); out.println(); } out.flush(); } private static int[] packTheBox(int[][] candies) { Map<Integer, int[]> candyToCount = new HashMap<>(); for (int i = 0; i < candies.length; i++) { int[] count = candyToCount.get(candies[i][0]); if (count == null) { count = new int[2]; candyToCount.put(candies[i][0], count); } count[0] += 1; count[1] += candies[i][1]; } List<int[]> counts = new ArrayList<>(candyToCount.values()); Collections.sort(counts, (a, b) -> Integer.compare(b[0], a[0])); Queue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt(a -> -a[1])); int maxCandies = 0; int maxOnes = 0; int maxUniqueCount = Integer.MAX_VALUE; for (int i = 0; i < counts.size(); i++) { int[] count = counts.get(i); while (maxUniqueCount > count[0] && !queue.isEmpty()) { int[] peek = queue.poll(); maxCandies += maxUniqueCount; maxOnes += Math.min(peek[1], maxUniqueCount); maxUniqueCount--; } if (queue.isEmpty()) { maxUniqueCount = count[0]; } queue.add(count); } while (!queue.isEmpty() && maxUniqueCount > 0) { int[] peek = queue.poll(); maxCandies += maxUniqueCount; maxOnes += Math.min(peek[1], maxUniqueCount); maxUniqueCount--; } return new int[]{maxCandies, maxOnes}; } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
5b6d936117a42c66de963abaaff4b41a
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.AbstractCollection; import java.util.PriorityQueue; import java.util.InputMismatchException; import java.io.IOException; import java.util.Comparator; import java.util.TreeSet; import java.io.InputStream; /** * Built using CHelper plug-in Actual solution is at the top * * @author NMouad21 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); GCandyBoxHardVersion solver = new GCandyBoxHardVersion(); solver.solve(1, in, out); out.close(); } static class GCandyBoxHardVersion { public void solve(int testNumber, InputReader in, PrintWriter out) { int q = in.nextInt(); while (q-- > 0) { int n = in.nextInt(); int[][] cnt = new int[n + 1][2]; for (int i = 0; i < n; i++) { int a = in.nextInt(); int f = in.nextInt(); ++cnt[a][0]; if (f == 1) { ++cnt[a][1]; } } Arrays.sort(cnt, Comparator.comparingInt(x -> x[0])); ArrayUtils.reverse(cnt); int ans = 0, last = Integer.MAX_VALUE / 2; TreeSet<Integer> used = new TreeSet<>(); for (int[] tCnt : cnt) { int a = tCnt[0]; if (a == 0 || last == 0) { break; } last = Math.max(0, Math.min(last - 1, a)); ans += last; if (last != 0) { used.add(last); } } PriorityQueue<int[]> cntpq = new PriorityQueue<>(Comparator.comparingInt(x -> -x[1])); for (int[] tCnt : cnt) { if (tCnt[0] > 0) { cntpq.add(tCnt); } } int ansf = 0; while (!cntpq.isEmpty() && !used.isEmpty()) { int[] tCnt = cntpq.poll(); int a = tCnt[0]; int f = tCnt[1]; Integer target = used.floor(a); if (target != null) { if (target >= f) { ansf += f; } else { ansf += target; } used.remove(target); } } out.println(ans + " " + ansf); } } } static class ArrayUtils { public static <T> void reverse(T[] arr) { for (int i = 0; i < arr.length / 2; i++) { T temp = arr[i]; arr[i] = arr[arr.length - i - 1]; arr[arr.length - i - 1] = temp; } } } static class InputReader { private InputStream stream; private static final int DEFAULT_BUFFER_SIZE = 1 << 16; private static final int EOF = -1; private byte[] buf = new byte[DEFAULT_BUFFER_SIZE]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (this.numChars == EOF) { throw new UnknownError(); } else { if (this.curChar >= this.numChars) { this.curChar = 0; try { this.numChars = this.stream.read(this.buf); } catch (IOException ex) { throw new InputMismatchException(); } if (this.numChars <= 0) { return EOF; } } return this.buf[this.curChar++]; } } public int nextInt() { int c; for (c = this.read(); isSpaceChar(c); c = this.read()) { } byte sgn = 1; if (c == 45) { sgn = -1; c = this.read(); } int res = 0; while (c >= 48 && c <= 57) { res *= 10; res += c - 48; c = this.read(); if (isSpaceChar(c)) { return res * sgn; } } throw new InputMismatchException(); } public static boolean isSpaceChar(int c) { return c == 32 || c == 10 || c == 13 || c == 9 || c == EOF; } } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
3a4f796a9fb9f5f8d5d831ba4c57f0c5
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
// Working program using Reader Class import java.io.*; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static int gcd(int a, int b) { // Everything divides 0 if (a == 0 || b == 0) return 0; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } public static class Node implements Comparable<Node>{ int val; int count; int ones; Node(int xi){ val=xi; count=0; ones=0; } public int compareTo(Node n) { return n.count-this.count; } } public static class NodeComparator implements Comparator<Node>{ public int compare(Node n1,Node n2) { return n2.ones-n1.ones; } } public static void main(String[] args) throws IOException { //Reader scan=new Reader(); Reader scan=new Reader(); PrintWriter out=new PrintWriter(System.out); int q=scan.nextInt(); while(q-->0) { int n=scan.nextInt(); HashMap<Integer,Node> hm=new HashMap<Integer,Node>(); for(int i=0;i<n;i++) { int x=scan.nextInt(); int f=scan.nextInt(); if(!hm.containsKey(x)) { hm.put(x, new Node(x)); } hm.get(x).count++; if(f==1) { hm.get(x).ones++; } } ArrayList<Node> al=new ArrayList<Node>(); for(Map.Entry<Integer, Node> map : hm.entrySet()) { al.add(map.getValue()); } Collections.sort(al); int ans=0; int max=al.get(0).count; PriorityQueue<Node> tm=new PriorityQueue<Node>(new NodeComparator()); int i=0; int ones=0; while(max>0) { while(i<al.size()) { if(al.get(i).count==max) { tm.add(al.get(i)); } else { break; } i++; } if(!tm.isEmpty()) { ans+=max; ones+=Math.min(tm.poll().ones,max); } max--; } out.println(ans+" "+ones); } out.close(); } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
cd9b0ae3a0d35ffded77745159cf17cf
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Rustam Musin (t.me/musin_acm) */ 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); DKorobkaSKonfetamiLegkayaVersiya solver = new DKorobkaSKonfetamiLegkayaVersiya(); solver.solve(1, in, out); out.close(); } static class DKorobkaSKonfetamiLegkayaVersiya { public void solve(int testNumber, InputReader in, OutputWriter out) { int q = in.readInt(); while (q-- > 0) { int n = in.readInt(); int[] onesByType = new int[n]; int[] cnt = new int[n]; for (int i = 0; i < n; i++) { int type = in.readInt() - 1; onesByType[type] += in.readInt(); cnt[type]++; } List<Integer>[] onesBySize = new List[n + 1]; for (int i = 0; i <= n; i++) { onesBySize[i] = new ArrayList<>(); } for (int i = 0; i < n; i++) { onesBySize[cnt[i]].add(onesByType[i]); } List<Integer> availableOneCounts = new ArrayList<>(); int ans = 0; int ansOnes = 0; for (int size = n; size > 0; size--) { availableOneCounts.addAll(onesBySize[size]); if (!availableOneCounts.isEmpty()) { ans += size; int maxValue = Collections.max(availableOneCounts); availableOneCounts.remove((Integer) maxValue); ansOnes += Math.min(maxValue, size); } } out.printLine(ans, ansOnes); } } } 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 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 OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
9df2e6293a5d37049282f176a0ef16e2
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
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.PriorityQueue; import java.io.BufferedWriter; import java.util.Collection; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Queue; import java.util.Comparator; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Rustam Musin (t.me/musin_acm) */ 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); DKorobkaSKonfetamiLegkayaVersiya solver = new DKorobkaSKonfetamiLegkayaVersiya(); solver.solve(1, in, out); out.close(); } static class DKorobkaSKonfetamiLegkayaVersiya { public void solve(int testNumber, InputReader in, OutputWriter out) { int q = in.readInt(); while (q-- > 0) { int n = in.readInt(); int[] onesByType = new int[n]; int[] cnt = new int[n]; for (int i = 0; i < n; i++) { int type = in.readInt() - 1; onesByType[type] += in.readInt(); cnt[type]++; } List<Integer>[] onesBySize = new List[n + 1]; for (int i = 0; i <= n; i++) { onesBySize[i] = new ArrayList<>(); } for (int i = 0; i < n; i++) { onesBySize[cnt[i]].add(onesByType[i]); } Queue<Integer> availableOneCounts = new PriorityQueue<>(Comparator.reverseOrder()); int ans = 0; int ansOnes = 0; for (int size = n; size > 0; size--) { availableOneCounts.addAll(onesBySize[size]); if (!availableOneCounts.isEmpty()) { ans += size; ansOnes += Math.min(availableOneCounts.poll(), size); } } out.printLine(ans, ansOnes); } } } 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 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 OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
2bda9f46ce4c17ad1de6bdaea983869c
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
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.PriorityQueue; import java.io.BufferedWriter; import java.util.Collection; import java.util.HashMap; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.AbstractMap; import java.util.TreeMap; import java.util.Map; import java.util.Map.Entry; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Queue; import java.util.Comparator; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Rustam Musin (t.me/musin_acm) */ 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); DKorobkaSKonfetamiLegkayaVersiya solver = new DKorobkaSKonfetamiLegkayaVersiya(); solver.solve(1, in, out); out.close(); } static class DKorobkaSKonfetamiLegkayaVersiya { public void solve(int testNumber, InputReader in, OutputWriter out) { int q = in.readInt(); while (q-- > 0) { int n = in.readInt(); Map<Integer, Integer> onesByType = new HashMap<>(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.readInt(); onesByType.merge(a[i], in.readInt(), Integer::sum); } Map<Integer, Integer> cnt = new HashMap<>(); for (int x : a) { cnt.merge(x, 1, Integer::sum); } TreeMap<Integer, List<Integer>> onesBySize = new TreeMap<>(); for (Map.Entry<Integer, Integer> e : cnt.entrySet()) { onesBySize.computeIfAbsent(e.getValue(), k -> new ArrayList<>()) .add(onesByType.get(e.getKey())); } Queue<Integer> availableOneCounts = new PriorityQueue<>(Comparator.reverseOrder()); int ans = 0; int ansOnes = 0; for (int size = Integer.MAX_VALUE; size > 0; ) { if (onesBySize.containsKey(size)) { availableOneCounts.addAll(onesBySize.get(size)); onesBySize.remove(size); } if (availableOneCounts.isEmpty()) { if (onesBySize.isEmpty()) { break; } size = onesBySize.lastKey(); } else { ans += size; ansOnes += Math.min(availableOneCounts.poll(), size); size--; } } out.printLine(ans, ansOnes); } } } 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 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 OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
1eb6187c3a61a16c9b5b776c42f0d1df
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { IO io ; try { io = new IO("in.in", null); } catch (IOException e) { io = new IO(null, null); } int T = io.getNextInt(); ArrayList<item> A = new ArrayList<>(); HashMap<Integer,int[]> HM = new HashMap<>(); PriorityQueue<Integer> pq = new PriorityQueue<>(); for (int t = 0;t < T;t++) { int n = io.getNextInt(); A.clear(); HM.clear(); pq.clear(); for (int i = 0;i < n;i++) { int w = io.getNextInt(); int f = io.getNextInt(); if (!HM.containsKey(w)) HM.put(w,new int[2]); HM.get(w)[f]++; } for (int w : HM.keySet()) { int [] C = HM.get(w); A.add(new item(C[0] + C[1],C[1])); } Collections.sort(A); Collections.reverse(A); int i = 0; int upper = n; int w = 0,f = 0; while ((!pq.isEmpty() || i < A.size()) && upper > 0) { while (i < A.size() && A.get(i).n >= upper) { pq.add(-A.get(i).f); i++; } if (pq.isEmpty()) { int c = A.get(i).n; upper = Math.min(upper,c); } else { w += upper; int ef = -pq.poll(); ef = Math.min(ef,upper); f += ef; upper--; } } io.println(w + " " + f); } io.close(); } private static final int onebillion7 = 1000000007; } class item implements Comparable<item>{ int n,f; public item(int a,int b) { n = a; f = b; } @Override public int compareTo(item o) { if (n == o.n) return f - o.f; return n - o.n; } } class IO{ private BufferedReader br; private StringTokenizer st; private PrintWriter writer; private String inputFile,outputFile; public boolean hasMore() throws IOException{ if(st != null && st.hasMoreTokens()) return true; if(br != null && br.ready()) return true; return false; } public String getNext() throws FileNotFoundException, IOException{ while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String getNextLine() throws FileNotFoundException, IOException{ return br.readLine().trim(); } public int getNextInt() throws FileNotFoundException, IOException{ return Integer.parseInt(getNext()); } public long getNextLong() throws FileNotFoundException, IOException{ return Long.parseLong(getNext()); } public void print(double x,int num_digits) throws IOException{ writer.printf("%." + num_digits + "f" ,x); } public void println(double x,int num_digits) throws IOException{ writer.printf("%." + num_digits + "f\n" ,x); } public void print(Object o) throws IOException{ writer.print(o.toString()); } public void println(Object o) throws IOException{ writer.println(o.toString()); } public IO(String x,String y) throws FileNotFoundException, IOException{ inputFile = x; outputFile = y; if(x != null) br = new BufferedReader(new FileReader(inputFile)); else br = new BufferedReader(new InputStreamReader(System.in)); if(y != null) writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile))); else writer = new PrintWriter(new OutputStreamWriter(System.out)); } protected void close() throws IOException{ br.close(); writer.close(); } public void outputArr(Object [] A) throws IOException{ int L = A.length; for (int i = 0;i < L;i++) { if(i > 0) writer.print(" "); writer.print(A[i]); } writer.print("\n"); } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
c3968518c511d8cf2790c312e5dea2d6
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { PrintWriter out = new PrintWriter(System.out); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""); String next() throws IOException { if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } long nl() throws IOException { return Long.parseLong(next()); } void solve() throws IOException { int T=ni(); for (int t=0;t<T;t++) { int n=ni(); int[] A=new int[n+1]; int[] F=new int[n+1]; int[] B=new int[n+1]; int m=1; for (int x=0;x<n;x++) { int b=ni(); A[b]++; if (A[b]>m) m=A[b]; if (ni()==1) F[b]++; } ArrayList<Integer>[] C=new ArrayList[m+1]; for (int x=1;x<=m;x++) C[x]=new ArrayList(); for (int x=1;x<=n;x++) { if (A[x]==0) continue; C[A[x]].add(F[x]); } int ans=0; int good=0; PriorityQueue<Integer> D=new PriorityQueue(Collections.reverseOrder()); for (int x=m;x>0;x--) { if (C[x].size()>0) { for (int y=0;y<C[x].size();y++) { D.add(C[x].get(y)); } } if (D.size()==0) continue; ans+=x; int e=D.poll(); if (e>x) e=x; good+=e; } out.println(ans+" "+good); } out.flush(); } public static void main(String[] args) throws IOException { new Main().solve(); } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
37ad27b740b80009833923d923c51141
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.PriorityQueue; import java.util.InputMismatchException; import java.io.IOException; import java.util.Comparator; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author KharYusuf */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); GCandyBoxHardVersion solver = new GCandyBoxHardVersion(); solver.solve(1, in, out); out.close(); } static class GCandyBoxHardVersion { public void solve(int testNumber, FastReader s, PrintWriter w) { int q = s.nextInt(); while (q-- > 0) { int n = s.nextInt(), x, f[][] = new int[n + 1][2], fr[] = new int[n + 1], a1 = 0, a2 = 0, c = n; PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); for (int i = 0; i < n; i++) { x = s.nextInt(); f[x][0]++; if (s.nextInt() == 1) { f[x][1]++; } } func.sortbyColumn(f, 0); for (int i = n; i > 0; i--) { while (f[c][0] >= i) { pq.add(f[c--][1]); } if (pq.size() > 0) { a1 += i; a2 += Math.min(i, pq.poll()); } } w.println(a1 + " " + a2); } } } static class func { public static void sortbyColumn(int arr[][], final int col) { Arrays.sort(arr, new Comparator<int[]>() { public int compare(final int[] entry1, final int[] entry2) { if (entry1[col] > entry2[col]) return 1; if (entry1[col] < entry2[col]) return -1; return 0; } }); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
74920f4f7f9fc3a1ebf5f320a661486d
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.*; import java.util.*; public class G_CandyBox { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader inp = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); solver.solve(inp, out); out.close(); } private static class Solver { Comparator<Integer> fComparator = new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return -Integer.compare(o1, o2); } }; private void solve(InputReader inp, PrintWriter out) { int q = inp.nextInt(); for (int i = 0; i < q; i++) { int n = inp.nextInt(); Pair[] a = new Pair[n]; for (int j = 0; j < n; j++) a[j] = new Pair(); for (int j = 0; j < n; j++) { int val = inp.nextInt() - 1; int f = inp.nextInt(); a[val].candies++; if (f == 1) a[val].f++; } ArrayList<Pair>[] totals = new ArrayList[n+1]; for (int j = 0; j < n + 1; j++) totals[j] = new ArrayList<>(); for (Pair pair: a) totals[pair.candies].add(pair); int sum = 0, f = 0; PriorityQueue<Integer> pq = new PriorityQueue<>(fComparator); for (int j = n; j > 0; j--) { for (Pair p: totals[j]) pq.add(p.f); if (!pq.isEmpty()) { sum += j; f += Math.min(pq.poll(), j); } } out.println(sum + " " + f); } } } static class Pair implements Comparable<Pair> { int candies = 0; int f = 0; @Override public int compareTo(Pair o) { if (candies != o.candies) return Integer.compare(candies, o.candies); return Integer.compare(f, o.f); } } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; 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()); } } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
8d8569e202a7ab2c57141c94c1f9b7fb
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.*; public class Main { Main() throws IOException { Reader s = new Reader(); int q = s.nextInt(); for (int i = 0; i < q; i++) { int n = s.nextInt(); int[] num = new int[n]; int[] want = new int[n]; for (int j = 0; j < n; j++) { int a = s.nextInt() - 1; num[a]++; want[a] += s.nextInt(); } HashMap<Integer, ArrayList<Integer>> candies = new HashMap<>(); for (int j = 0; j < n; j++) { int a = num[j]; if (a == 0) continue; if (candies.containsKey(a)) candies.get(a).add(want[j]); else { ArrayList<Integer> list = new ArrayList<>(); list.add(want[j]); candies.put(a, list); } } for (Integer key : candies.keySet()) Collections.sort(candies.get(key)); int[] keys = new int[candies.keySet().size()]; ArrayList<Integer> meme = new ArrayList<>(candies.keySet()); for (int j = 0; j < candies.keySet().size(); j++) keys[j] = -meme.get(j); Arrays.sort(keys); for (int j = 0; j < keys.length; j++) keys[j] = -keys[j]; int total = 0, ans = 0, last = keys[0] + 10; for (int j = 0; j < keys.length; j++) { int key = keys[j]; if (key == 0) break; if (last <= key) continue; last = key; ArrayList<Integer> candy = candies.get(key); if (candy.isEmpty()) continue; total += key; ans += Math.min(candy.remove(candy.size() - 1), key); if (!candy.isEmpty() && (key > 0)) { keys[j]--; if (candies.containsKey(keys[j])) { candies.get(keys[j]).addAll(candy); Collections.sort(candies.get(keys[j])); } else candies.put(keys[j], candy); j--; } } System.out.println(total + " " + ans); } } long gcd(long a, long b) { while (b != 0) { long t = a; a = b; b = t % b; } return a; } public static void main(String[] args) { try { new Main(); } catch (IOException e) {} } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
526f9cd5a80b82f6fb59b6c5c895561d
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.*; import java.util.*; public class ProblemG { public static InputStream inputStream = System.in; public static OutputStream outputStream = System.out; public static void main(String[] args) { MyScanner scanner = new MyScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); int t = scanner.nextInt(); for (int p = 0; p < t; p++) { int n = scanner.nextInt(); Map<Integer, Pair<Integer, Integer>> map = new HashMap<>(); for (int i = 0; i < n; i++) { int x = scanner.nextInt(); int y = scanner.nextInt(); if (!map.containsKey(x)) { map.put(x, new Pair<>(0, 0)); } Pair<Integer, Integer> pair = map.get(x); pair.first++; pair.second += y; map.put(x, pair); } List<Pair<Integer, Integer>> countOfTypes = new ArrayList<>(map.values()); countOfTypes.sort(Comparator.comparingInt(pair -> pair.first)); Collections.reverse(countOfTypes); int count = 0; int good = 0; PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((a, b) -> Integer.compare(b, a)); int i = 0; int x = countOfTypes.get(i).first; while (i < countOfTypes.size()) { if (countOfTypes.get(i).first < x && priorityQueue.isEmpty()) { x = countOfTypes.get(i).first; } while (i < countOfTypes.size() && x == countOfTypes.get(i).first) { priorityQueue.add(countOfTypes.get(i).second); i++; } count += x; good += Math.min(priorityQueue.poll(), x); x--; } while (x > 0 && !priorityQueue.isEmpty()) { count += x; good += Math.min(priorityQueue.poll(), x); x--; } out.println(count + " " + good); } out.flush(); } private static class MyScanner { private BufferedReader bufferedReader; private StringTokenizer stringTokenizer; private MyScanner(InputStream inputStream) { bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); } private String next() { while (stringTokenizer == null || !stringTokenizer.hasMoreElements()) { try { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return stringTokenizer.nextToken(); } private int nextInt() { return Integer.parseInt(next()); } private long nextLong() { return Long.parseLong(next()); } private double nextDouble() { return Double.parseDouble(next()); } private String nextLine() { String str = ""; try { str = bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static class Pair<F, S> { private F first; private S second; public Pair() {} public Pair(F first, S second) { this.first = first; this.second = second; } } private static class Triple<F, S, T> { private F first; private S second; private T third; public Triple() {} public Triple(F first, S second, T third) { this.first = first; this.second = second; this.third = third; } } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
6a276f576d1f697c0d96ceec4e5711d8
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class practice2 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static FastReader scn = new FastReader(); public static void main(String[] args) { int t = scn.nextInt(); while(t-- > 0) run(); } static class node implements Comparable<node>{ int count; int poscount; public node(int c, int p){ count = c; poscount = p; } @Override public int compareTo(node node) { if(this.count==node.count) return node.poscount - this.poscount; return node.count - this.count; } } private static void run() { int n = scn.nextInt(); int[] count = new int[n+1]; int[] poscount = new int[n+1]; for(int i = 0; i<n;i++){ int a = scn.nextInt(); int b = scn.nextInt(); count[a]++; if (b==1) poscount[a]++; } PriorityQueue<node> pq = new PriorityQueue<>(); for(int i = 1; i<=n; i++){ if(count[i]!=0){ pq.add(new node(count[i],poscount[i])); } } long total = 0, fs = 0; int current = n; while(!pq.isEmpty() && current>0){ node nn = pq.remove(); if(nn.count>current){ nn.count = current; pq.add(nn); }else{ total += nn.count; fs += Math.min(nn.poscount,nn.count); current = Math.min(nn.count,current)-1; } } System.out.println(total + " " + fs); } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
ce1d383715eb2145e7dc3eb33985abea
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.InputMismatchException; import java.util.List; import java.util.Map; import java.util.PriorityQueue; public class Main { InputStream is; PrintWriter out; String INPUT = ""; private static final long M = 1000000007L; void solve() { StringBuffer sb = new StringBuffer(); int t = ni(); while (t-- > 0) { int n = ni(); int a[][] = na(n, 2); List<int[]> cnt = count(a); cnt.add(new int[] { 0, 0 }); PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Comparator.reverseOrder()); int ans[] = new int[] { 0, 0 }; int next = Integer.MAX_VALUE; for (int cnt0[] : cnt) { // tr(">", cnt0); int curTotal = cnt0[0] + cnt0[1]; if (pq.isEmpty() && next >= curTotal) { // pq must me empty ans[0] += curTotal; ans[1] += cnt0[1]; next = curTotal - 1; continue; } while (!pq.isEmpty() && next > curTotal) { ans[0] += next; ans[1] += Math.min(next, pq.poll()); next --; } if (pq.isEmpty() && next >= curTotal) { // pq must me empty ans[0] += curTotal; ans[1] += cnt0[1]; next = curTotal - 1; continue; } pq.add(cnt0[1]); } sb.append(ans[0]).append(' ').append(ans[1]).append(System.lineSeparator()); } System.out.println(sb.toString()); } private List<int[]> count(int[][] a) { Map<Integer, int[]> m = new HashMap<Integer, int[]>(); for (int a0[] : a) m.computeIfAbsent(a0[0], k -> new int[] { 0, 0 })[a0[1]]++; List<int[]> l = new ArrayList<int[]>(m.values()); l.sort(new Comparator<int[]>() { public int compare(int[] o1, int[] o2) { if (o1[0] + o1[1] == o2[0] + o2[1]) return o2[1] - o1[1]; return o2[0] + o2[1] - o1[0] - o1[1]; } }); return l; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private 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++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' // ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n) { if (!(isSpaceChar(b))) buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int[][] na(int n, int m) { int[][] a = new int[n][]; for (int i = 0; i < n; i++) a[i] = na(m); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
4f24618931ec0dfe933d8028d3e816c5
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) {new Main().run();} FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); void run(){ int q=in.nextInt(); for(int i=0;i<q;i++) { work(); } out.flush(); } long mod=1000000007; long gcd(long a,long b) { return b==0?a:gcd(b,a%b); } void work() { int n=in.nextInt(); int[] count1=new int[n+1]; int[] count2=new int[n+1]; for(int i=0;i<n;i++) { int a=in.nextInt(); int f=in.nextInt(); count1[a]++; if(f==1)count2[a]++; } ArrayList<int[]> list=new ArrayList<>(); for(int i=1;i<=n;i++) { if(count1[i]>0) list.add(new int[] {count1[i],count2[i]}); } Collections.sort(list,new Comparator<int[]>() { public int compare(int[] arr1,int[] arr2) { return arr2[0]-arr1[0]; } }); PriorityQueue<Integer> pq=new PriorityQueue<>(); int ret1=0; int ret2=0; for(int i=0,cur=1;cur>0;cur--) { if(pq.size()==0&&i==list.size()) break; if(pq.size()==0) { cur=list.get(i)[0]; } while(i<list.size()&&list.get(i)[0]==cur) { pq.add(-list.get(i)[1]); i++; } ret1+=cur; ret2+=Math.min(cur,-pq.poll()); } out.println(ret1+" "+ret2); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } public String next() { if(st==null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
dbd6c8979d44f4a9ac1b3e368499ae8a
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.io.*; import java.text.*; import java.util.*; import java.math.*; public class template { public static void main(String[] args) throws Exception { new template().run(); } public void run() throws Exception { FastScanner f = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int asdf = f.nextInt(); while(asdf-->0) { int n = f.nextInt(); int[] cntt = new int[n]; int[] cnt1 = new int[n]; for(int i = 0; i < n; i++) { int a= f.nextInt()-1; cntt[a]++; if(f.nextInt() == 1) cnt1[a]++; } Pair[] arr = new Pair[n]; for(int i = 0; i < n; i++) arr[i] = new Pair(cntt[i], cnt1[i]); Arrays.sort(arr, new Comparator<Pair>() { public int compare(Pair a, Pair b) { return -Integer.compare(a.a, b.a); } }); PriorityQueue<Pair> pq = new PriorityQueue<>(new Comparator<Pair>() { public int compare(Pair a, Pair b) { return -Integer.compare(a.b, b.b); } }); int totalt = 0, total1 = 0, prev = 2147483647; int j = 0; while(true) { if(pq.isEmpty()) { if(j == n) break; prev = arr[j].a; } while(j < n && arr[j].a >= prev) pq.add(arr[j++]); Pair p = pq.poll(); totalt += prev = Math.max(0, Math.min(prev, p.a)); total1 += Math.min(p.b, prev); prev--; } out.println(totalt + " " + total1); } /// out.flush(); } class Pair { int a, b; public Pair(int a, int b) { this.a = a; this.b = b; } } /// static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output
PASSED
542442785b6f15101e13b261743f0f35
train_001.jsonl
1561559700
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
256 megabytes
import java.util.*; import java.io.*; public class R1183G { FastScanner in; PrintWriter out; void solve() { int q=in.nextInt(); while (q-->0) { int n=in.nextInt(); Pair[] a=new Pair[n+1]; for(int i=1; i<=n; i++) a[i]=new Pair(0,0); for(int i=0; i<n; i++) { int t=in.nextInt(), f=in.nextInt(); a[t].add(1, f); } Queue<Pair>pq=new PriorityQueue<Pair>(); for(int i=1; i<=n; i++) pq.add(a[i]); int ans=0, ansf=0, max=Integer.MAX_VALUE; while (!pq.isEmpty()){ Pair b=pq.remove(); if (b.n==0) break; if (b.n==max) { pq.add(new Pair(b.n-1, b.f)); } else { max=b.n; ans+=max; ansf+=Math.min(max, b.f); } } System.out.println(ans+" "+ansf); } } void run() { try { in = new FastScanner(new File("CF.in")); out = new PrintWriter(new File("CF.out")); solve(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } class Pair implements Comparable<Pair> { int n; int f; public Pair(int n, int f) { this.n=n; this.f=f; } public void add(int n, int f){ this.n+=n; this.f+=f; } @Override public int compareTo(Pair p) { if (this.n!=p.n) return -this.n+p.n; else return -this.f+p.f; } } void runIO() { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] args) { new R1183G().runIO(); } }
Java
["3\n8\n1 0\n4 1\n2 0\n4 1\n5 1\n6 1\n3 0\n2 0\n4\n1 1\n1 1\n2 1\n2 1\n9\n2 0\n2 0\n4 1\n4 1\n4 1\n7 0\n7 1\n7 0\n7 1"]
2 seconds
["3 3\n3 3\n9 5"]
NoteIn the first query, you can include two candies of type $$$4$$$ and one candy of type $$$5$$$. All of them have $$$f_i = 1$$$ and you don't mind giving them away as part of the gift.
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
bcde53a1671a66eb16a37139380f4ae5
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 \le a_i \le n$$$, $$$0 \le f_i \le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.
2,000
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having $$$f_i = 1$$$ in a gift you can compose that contains the maximum possible number of candies.
standard output