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 | c5e80ffd096e37b81f302971d251dccd | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.util.*;
import java.io.*;
public class D {
public static void main(String[] args) throws IOException {
FastScanner input = new FastScanner(System.in);
// Scanner input = new Scanner(new File("input.txt"));
PrintWriter output = new PrintWriter(System.out);
int n = input.nextInt();
long[] nums = new long[n];
long[] odd = new long[n];
long[] even = new long[n];
long sum = 0;
for (int i = 0; i < n; i++) {
nums[i] = input.nextInt();
sum += nums[i];
if (i % 2 == 0) {
if (i > 0)
even[i] = even[i - 2] + nums[i];
else
even[i] = nums[i];
} else {
if (i > 1)
odd[i] = odd[i - 2] + nums[i];
else
odd[i] = nums[i];
}
}
long max = nums[0], before = 0, after = 0;
for (int i = 1; i < n; i++) {
if (i % 2 == 1) {
before = 0;
if (i - 2 >= 0)
before = odd[i - 2];
after = even[n - 1];
if (i - 1 >= 0)
after -= even[i - 1];
} else {
before = 0;
if (i - 2 >= 0)
before = even[i - 2];
after = odd[n - 2];
if (i - 1 >= 0)
after -= odd[i - 1];
}
max = Math.max(max, sum - before - after);
}
output.println(max);
output.close();
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
FastScanner(FileReader s) {
br = new BufferedReader(s);
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
String nextLine() throws IOException {
return br.readLine();
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 156927b2438d2c512b0aa275d4027e1b | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | /**
* @author Finn Lidbetter
*/
import java.util.*;
import java.io.*;
import java.awt.geom.*;
public class TaskD {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
String[] s = br.readLine().split(" ");
int[] arr = new int[n];
for (int i=0; i<n; i++) {
arr[i] = Integer.parseInt(s[i]);
}
long sum = 0;
for (int i=0; i<n; i+=2) {
sum += arr[i];
}
long best = sum;
int shiftIndex = n-1;
for (int i=0; i<n; i++) {
sum -= arr[shiftIndex];
sum += arr[(shiftIndex-1+n)%n];
shiftIndex -= 2;
if (shiftIndex<0) {
shiftIndex += n;
}
if (sum>best) {
best = sum;
}
}
System.out.println(best);
}
}
class MyUtils {
static void shuffleArray(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for(int i=0; i<n; i++) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static void shuffleArray(long[] arr) {
int n = arr.length;
Random rnd = new Random();
for(int i=0; i<n; i++) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | f993173ac9d5bc182bd81ec381de4333 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class OmkarAndCircle {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
long mod = (long)(1000000007);
long fact[];
int depth[];
int parentTable[][];
int degree[];
ArrayList<Integer> leaves;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
while(testNumber-->0){
int n = in.nextInt();
long a[] = new long[n];
for(int i=0;i<n;i++)
a[i] = in.nextLong();
long b[] = new long[2*n];
for(int i=0;i<n;i++){
b[i] = a[(2*i)%n];
b[i+n] = b[i];
}
long c = 0;
for(int i=0;i<(n+1)/2;i++)
c += b[i];
long ans = c;
for(int i=1 , j=(n+1)/2;j<2*n;i++,j++){
c = c - b[i-1] + b[j];
ans = max(ans , c);
}
out.println(ans);
}
}
class Node{
long value;
int index;
Node left;
Node right;
public Node(long value , int index){
this.value = value;
this.index = index;
left = null;
right = null;
}
}
public int[][] multiply(int a[][] , int b[][]){
int c[][] = new int[a.length][b[0].length];
for(int i=0;i<a.length;i++){
for(int j=0;j<b[0].length;j++){
for(int k=0;k<b.length;k++)
c[i][j] += a[i][k]*b[k][j];
}
}
return c;
}
public int[][] multiply(int a[][] , int b[][] , int mod){
int c[][] = new int[a.length][b[0].length];
for(int i=0;i<a.length;i++){
for(int j=0;j<b[0].length;j++){
for(int k=0;k<b.length;k++){
c[i][j] += a[i][k]*b[k][j];
c[i][j]%=mod;
}
}
}
return c;
}
public int[][] pow(int a[][] , long b){
int res[][] = new int[a.length][a[0].length];
for(int i=0;i<a.length;i++)
res[i][i] = 1;
while(b>0){
if((b&1) == 1)
res = multiply(res , a , 10);
a = multiply(a , a , 10);
b>>=1;
}
return res;
}
public int distance(ArrayList<ArrayList<Integer>> a , int u , int v){
return depth[u]+depth[v] - 2*depth[lca(a , u , v)];
}
// public void dfs(ArrayList<ArrayList<Integer>> a , int index , int parent){
// parentTable[index][0] = parent;
// depth[index] = depth[parent]+1;
// if(a.get(index).size()==1)
// leaves.add(index);
// for(int i=1;i<parentTable[index].length;i++)
// parentTable[index][i] = parentTable[parentTable[index][i-1]][i-1];
// for(int i=0;i<a.get(index).size();i++){
// if(a.get(index).get(i)==parent)
// continue;
// dfs(a , a.get(index).get(i) , index);
// }
// }
public int lca(ArrayList<ArrayList<Integer>> a , int u , int v){
if(depth[v]<depth[u]){
int x = u;
u = v;
v = x;
}
int diff = depth[v] - depth[u];
for(int i=0;i<parentTable[v].length;i++){
// checking whether the ith bit is set in the diff variable
if(((diff>>i)&1) == 1)
v = parentTable[v][i];
}
if(v == u)
return v;
for(int i=parentTable[v].length-1;i>=0;i--){
if(parentTable[u][i] != parentTable[v][i]){
v = parentTable[v][i];
u = parentTable[u][i];
}
}
return parentTable[u][0];
}
// for the min max problems
public void build(int lookup[][] , int arr[], int n) {
for (int i = 0; i < n; i++)
lookup[i][0] = arr[i];
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 0; (i + (1 << j) - 1) < n; i++) {
if (lookup[i][j - 1] > lookup[i + (1 << (j - 1))][j - 1])
lookup[i][j] = lookup[i][j - 1];
else
lookup[i][j] = lookup[i + (1 << (j - 1))][j - 1];
}
}
}
public int query(int lookup[][] , int L, int R) {
int j = (int)(Math.log(R - L + 1)/Math.log(2));
if (lookup[L][j] >= lookup[R - (1 << j) + 1][j])
return lookup[L][j];
else
return lookup[R - (1 << j) + 1][j];
}
// for printing purposes
public void print1d(long a[] , PrintWriter out){
for(int i=0;i<a.length;i++)
out.print(a[i] + " ");
out.println();
}
public void print2d(long a[][] , PrintWriter out){
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++)
out.print(a[i][j] + " ");
out.println();
}
// out.println();
}
public void sieve(int a[]){
a[0] = a[1] = 1;
int i;
for(i=2;i*i<=a.length;i++){
if(a[i] != 0)
continue;
a[i] = i;
for(int k = (i)*(i);k<a.length;k+=i){
if(a[k] != 0)
continue;
a[k] = i;
}
}
}
public long nCrPFermet(int n , int r , long p){
if(r==0)
return 1l;
// long fact[] = new long[n+1];
// fact[0] = 1;
// for(int i=1;i<=n;i++)
// fact[i] = (i*fact[i-1])%p;
long modInverseR = pow(fact[r] , p-2 , p);
long modInverseNR = pow(fact[n-r] , p-2 , p);
long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p;
return w;
}
public long pow(long a, long b, long m) {
a %= m;
long res = 1;
while (b > 0) {
long x = b&1;
if (x == 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
public long pow(long a, long b) {
long res = 1;
while (b > 0) {
long x = b&1;
if (x == 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
public void swap(int a[] , int p1 , int p2){
int x = a[p1];
a[p1] = a[p2];
a[p2] = x;
}
public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) {
if (start > end) {
return;
}
int mid = (start + end) / 2;
a.add(mid);
sortedArrayToBST(a, start, mid - 1);
sortedArrayToBST(a, mid + 1, end);
}
class Combine{
long x;
int y;
Combine(long x , int y){
this.x = x;
this.y = y;
}
}
class Sort2 implements Comparator<Combine>{
public int compare(Combine a , Combine b){
if(a.x > b.x)
return 1;
else if(a.x == b.x && a.y>b.y)
return 1;
else if(a.y == b.y && a.y == b.y)
return 0;
return -1;
}
}
public int lowerLastBound(ArrayList<Integer> a , int x){
int l = 0;
int r = a.size()-1;
if(a.get(l)>=x)
return -1;
if(a.get(r)<x)
return r;
int mid = -1;
while(l<=r){
mid = (l+r)/2;
if(a.get(mid) == x && a.get(mid-1)<x)
return mid-1;
else if(a.get(mid)>=x)
r = mid-1;
else if(a.get(mid)<x && a.get(mid+1)>=x)
return mid;
else if(a.get(mid)<x && a.get(mid+1)<x)
l = mid+1;
}
return mid;
}
public int upperFirstBound(ArrayList<Integer> a , Integer x){
int l = 0;
int r = a.size()-1;
if(a.get(l)>x)
return l;
if(a.get(r)<=x)
return r+1;
int mid = -1;
while(l<=r){
mid = (l+r)/2;
if(a.get(mid) == x && a.get(mid+1)>x)
return mid+1;
else if(a.get(mid)<=x)
l = mid+1;
else if(a.get(mid)>x && a.get(mid-1)<=x)
return mid;
else if(a.get(mid)>x && a.get(mid-1)>x)
r = mid-1;
}
return mid;
}
public int lowerLastBound(int a[] , int x){
int l = 0;
int r = a.length-1;
if(a[l]>=x)
return -1;
if(a[r]<x)
return r;
int mid = -1;
while(l<=r){
mid = (l+r)/2;
if(a[mid] == x && a[mid-1]<x)
return mid-1;
else if(a[mid]>=x)
r = mid-1;
else if(a[mid]<x && a[mid+1]>=x)
return mid;
else if(a[mid]<x && a[mid+1]<x)
l = mid+1;
}
return mid;
}
public int upperFirstBound(long a[] , long x){
int l = 0;
int r = a.length-1;
if(a[l]>x)
return l;
if(a[r]<=x)
return r+1;
int mid = -1;
while(l<=r){
mid = (l+r)/2;
if(a[mid] == x && a[mid+1]>x)
return mid+1;
else if(a[mid]<=x)
l = mid+1;
else if(a[mid]>x && a[mid-1]<=x)
return mid;
else if(a[mid]>x && a[mid-1]>x)
r = mid-1;
}
return mid;
}
public long log(float number , int base){
return (long) Math.ceil((Math.log(number) / Math.log(base)) + 1e-9);
}
public long gcd(long a , long b){
if(a<b){
long c = b;
b = a;
a = c;
}
while(b!=0){
long c = a;
a = b;
b = c%a;
}
return a;
}
public long[] gcdEx(long p, long q) {
if (q == 0)
return new long[] { p, 1, 0 };
long[] vals = gcdEx(q, p % q);
long d = vals[0];
long a = vals[2];
long b = vals[1] - (p / q) * vals[2];
// 0->gcd 1->xValue 2->yValue
return new long[] { d, a, b };
}
public void sievePhi(int a[]){
a[0] = 0;
a[1] = 1;
for(int i=2;i<a.length;i++)
a[i] = i-1;
for(int i=2;i<a.length;i++)
for(int j = 2*i;j<a.length;j+=i)
a[j] -= a[i];
}
public void lcmSum(long a[]){
int sievePhi[] = new int[(int)1e6 + 1];
sievePhi(sievePhi);
a[0] = 0;
for(int i=1;i<a.length;i++)
for(int j = i;j<a.length;j+=i)
a[j] += (long)i*sievePhi[i];
}
}
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());
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 45e7c572b8e68b65707f3425c446be45 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class ProblemD
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] cmd=br.readLine().split(" ");
int n=Integer.valueOf(cmd[0]);
long[] arr=new long[2*n];
cmd=br.readLine().split(" ");
for(int i=0;i<n;i++)
{
arr[i]=Long.valueOf(cmd[i]);
arr[n+i]=arr[i];
}
// for(int i=0;i<2*n;i++){
// System.out.print(arr[i]+" ");
// }
// System.out.println();
long ans=0;
long[] sum=new long[2*n];
for(int i=0;i<2*n;i++)
{
sum[i]=arr[i];
if(i-2>=0)
sum[i]=sum[i]+sum[i-2];
// System.out.print(sum[i]+" ");
}
// System.out.println();
for(int i=n-1;i<2*n;i++)
{
long x=sum[i];
if(i-n-1>=0)
x=x-sum[i-n-1];
ans=Math.max(x,ans);
// System.out.print(x+" ");
}
System.out.println(ans);
}
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | d48ff920d47eec2e70ab63f6867197ba | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.util.Scanner;
public class R655Dbest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
long[] arr =new long[n*2];
long[] pre = new long[n*2];
for(int i =0 ;i<n;i++) {
arr[i] = scan.nextLong();
}
for(int i = n;i<n*2;i++) {
arr[i] = arr[i-n];
}
pre[0] = arr[0];
pre[1] = arr[1];
for(int i = 2;i<n*2;i++) {
pre[i] = pre[i-2]+arr[i];
}
long ans = 0;
for(int i = n;i<n*2;i++) {
long sum = pre[i]-pre[i-n+1]+arr[i-n+1];
ans = Math.max(ans, sum);
}
System.out.println(ans);
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | fd3694a561a31db153786f82acd8b4d7 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author xwchen
*/
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();
}
static class TaskD {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = in.nextIntArray(n);
int m = (n + 1) / 2;
long subArraySum = 0;
for (int i = 0; i < n; i += 2) {
subArraySum += a[i];
}
long best = subArraySum;
for (int i = 0; i < 2 * n + 2; i += 2) {
subArraySum -= a[i % n];
subArraySum += a[(m * 2 + i) % n];
best = Math.max(best, subArraySum);
}
out.println(best);
}
}
static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer = new StringTokenizer("");
public InputReader(InputStream inputStream) {
this.reader = new BufferedReader(
new InputStreamReader(inputStream));
}
public String next() {
while (!tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 1739daccb54a43e9aeaaefa308034ac5 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.util.*;
import java.io.*;
public class d {
static final FS sc = new FS();
static final PrintWriter pw = new PrintWriter(System.out);
static final long max = Long.MAX_VALUE / 8;
static int n;
static long[] arr;
static long sum;
static long[][] dp;
public static void main(String[] args) {
n = sc.nextInt();
arr = new long[n];
sum = 0;
for(int i = 0; i < n; ++i) sum += arr[i] = sc.nextLong();
dp = new long[2][n + 3]; // dp[didWeUseDoubleSkip][idx]
for(long[] z : dp) Arrays.fill(z, -1);
long out = sum - Math.min(Math.min(go(0, 0), go(0, 1)), Math.min(go(1, 1), go(1, 2)));
pw.println(out);
pw.flush();
}
static long go(int used, int idx) {
if(dp[used][idx] != -1) return dp[used][idx];
if(idx >= n) return dp[used][idx] = (used == 1 ? 0 : max);
long sing = arr[idx] + go(used, idx + 2);
long doub = max;
if(used == 0) doub = arr[idx] + go(1, idx + 3);
return dp[used][idx] = Math.min(sing, doub);
}
static class FS {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch(Exception e) {}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | ad3b2a1cfd702a305fe1f50b240bd469 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.*;
import java.util.*;
public class d {
static class InputReader{
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader (new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() {
while(tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static InputReader r = new InputReader(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static void main (String[] args) {
//CODE GOES HERE
int n = r.nextInt();
long sum = 0;
int[] a = new int[n];
for(int i=0; i<n; i++) {
a[i] = r.nextInt();
sum = sum + a[i];
}
long sub = 0;
for(int i=0; i<n/2; i++) {
sub = sub + a[2*i];
}
long min = sub;
for(int i=0; i<n-1; i++) {
sub = sub - a[(2*i)%n] + a[(2*i+n-1)%n];
if(min>sub) {
min = sub;
}
}
pw.println(sum - min);
pw.close();
}
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | deb864439227777b1a8ef137b2b5c8ff | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class r655d {
public static void main(String[] args) {
FastScanner scan=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int n=scan.nextInt();
prefixOdd=new long[n];
prefixEven=new long[n];
long[] a=new long[n];
for(int i=0;i<n;i++) {
a[i]=scan.nextLong();
if(i%2==0) {
prefixEven[i]=a[i];
if(i>0) {
prefixEven[i]+=prefixEven[i-2];
prefixOdd[i]=prefixOdd[i-1];
}
}
else {
prefixOdd[i]=a[i];
if(i>1) prefixOdd[i]+=prefixOdd[i-2];
prefixEven[i]=prefixEven[i-1];
}
}
if(n==1) {
out.println(a[0]);
}
else {
long res=0L;
for(int i=0;i<n;i++) {
//i and (i+1)%n are the 2 together
int c=i, d=(i+1)%n;
long temp=a[c]+a[d];
if(n>3) temp+=sum(a,(d+2)%n,(c-2+n)%n);
res=Math.max(res,temp);
}
out.println(res);
}
out.close();
}
static long[] prefixOdd, prefixEven;
public static long sum(long[] a, int l, int r) {
// System.out.println(l+" "+r);
long res=0L;
if(r<l) {
if(l%2==0) {
res+=prefixEven[a.length-1]-(l==0?0:prefixEven[l-1]);
res+=prefixOdd[r];
}
else {
res+=prefixOdd[a.length-1]-(l==0?0:prefixOdd[l-1]);
res+=prefixEven[r];
}
}
else {
if(l%2==0) {
res+=prefixEven[r]-(l==0?0:prefixEven[l-1]);
}
else {
res+=prefixOdd[r]-prefixOdd[l-1];
}
}
return res;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
} catch (Exception e){e.printStackTrace();}
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {st = new StringTokenizer(br.readLine());}
catch (Exception e) {e.printStackTrace();}
return st.nextToken();
}
public int nextInt() {return Integer.parseInt(next());}
public long nextLong() {return Long.parseLong(next());}
public double nextDouble() {return Double.parseDouble(next());}
public String nextLine() {
String line = "";
if(st.hasMoreTokens()) line = st.nextToken();
else try {return br.readLine();}catch(IOException e){e.printStackTrace();}
while(st.hasMoreTokens()) line += " "+st.nextToken();
return line;
}
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | a26e50d571aa678ae0ae1b9a0d7d11f2 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
public class D {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
int n=fs.nextInt();
int[] a=new int[n+n];
for (int i=0; i<n; i++) a[i]=a[i+n]=fs.nextInt();
long[] cs=new long[2+n+n];
for (int i=2; i<cs.length; i++)
cs[i]=cs[i-2]+a[i-2];
// System.out.println("CS: "+Arrays.toString(cs));
long max=0;
for (int i=n; i<2*n; i++)
max=Math.max(max, cs[i+1]-cs[i-n]);
System.out.println(max);
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 32afeec7ff4f5988ef0273035ad22038 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.util.*;
import java.io.*;
public class EdD {
public static void main(String[] args) throws Exception{
int num = 998244353;
// TODO Auto-generated method stub
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(bf.readLine());
LinkedList<Long> array = new LinkedList<Long>();
StringTokenizer st2 = new StringTokenizer(bf.readLine());
long evensum = 0;
long oddsum = 0;
long[] evenprefix = new long[n+1];
long[] oddprefix = new long[n+1];
evenprefix[0] = 0;
oddprefix[0] = 0;
for(int i=0;i<n;i++){
array.add(Long.parseLong(st2.nextToken()));
if (i%2 == 0){
oddsum+=array.get(i);
oddprefix[i+1] = oddsum;
}
else{
evensum+=array.get(i);
evenprefix[i+1] = evensum;
}
}
long min = Long.MAX_VALUE;
for(int i = 0;i<(n+1)/2;i++){
long sum1 = evenprefix[n-1] - evenprefix[2*i];
long sum2 = 0;
if (i != 0)
sum2 = oddprefix[2*i-1];
min = Math.min(min, sum1+sum2);
}
for(int i = 0;i<(n+1)/2;i++){
long sum1 = evenprefix[n-1-2*i];
long sum2 = oddprefix[n] - oddprefix[n-2*i];
min = Math.min(min, sum1+sum2);
}
out.println(evensum+oddsum-min);
out.close();
}
}
//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 3fc29d55c85d73baeeca860c519f468e | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.util.*;
import java.io.*;
public class EdD {
public static void main(String[] args) throws Exception{
int num = 998244353;
// TODO Auto-generated method stub
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(bf.readLine());
LinkedList<Long> array = new LinkedList<Long>();
StringTokenizer st2 = new StringTokenizer(bf.readLine());
long evensum = 0;
long oddsum = 0;
long[] evenprefix = new long[n+1];
long[] oddprefix = new long[n+1];
evenprefix[0] = 0;
oddprefix[0] = 0;
for(int i = 0;i<n;i++){
array.add(Long.parseLong(st2.nextToken()));
// }
// for(int i = 0;i<n;i++){
if (i%2 == 0){
oddsum+=array.get(i);
oddprefix[i+1] = oddsum;
}
else{
evensum+=array.get(i);
evenprefix[i+1] = evensum;
}
}
long min = Long.MAX_VALUE;
for(int i = 0;i<(n+1)/2;i++){
long sum1 = evenprefix[n-1] - evenprefix[2*i];
long sum2 = 0;
if (i != 0)
sum2 = oddprefix[2*i-1];
min = Math.min(min, sum1+sum2);
}
for(int i = 0;i<(n+1)/2;i++){
long sum1 = evenprefix[n-1-2*i];
long sum2 = oddprefix[n] - oddprefix[n-2*i];
min = Math.min(min, sum1+sum2);
}
out.println(evensum+oddsum-min);
out.close();
}
}
//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | d4abf0f30aa8e2db1cf1a1b5fa60e13e | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.util.*;
import java.io.*;
public class EdD {
public static void main(String[] args) throws Exception{
int num = 998244353;
// TODO Auto-generated method stub
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(bf.readLine());
ArrayList<Long> array = new ArrayList<Long>();
StringTokenizer st2 = new StringTokenizer(bf.readLine());
long evensum = 0;
long oddsum = 0;
long[] evenprefix = new long[n+1];
long[] oddprefix = new long[n+1];
evenprefix[0] = 0;
oddprefix[0] = 0;
for(int i = 0;i<n;i++){
array.add(Long.parseLong(st2.nextToken()));
}
for(int i = 0;i<n;i++){
if (i%2 == 0){
oddsum+=array.get(i);
oddprefix[i+1] = oddsum;
}
else{
evensum+=array.get(i);
evenprefix[i+1] = evensum;
}
}
long min = Long.MAX_VALUE;
for(int i = 0;i<(n+1)/2;i++){
long sum1 = evenprefix[n-1] - evenprefix[2*i];
long sum2 = 0;
if (i != 0)
sum2 = oddprefix[2*i-1];
min = Math.min(min, sum1+sum2);
}
for(int i = 0;i<(n+1)/2;i++){
long sum1 = evenprefix[n-1-2*i];
long sum2 = oddprefix[n] - oddprefix[n-2*i];
min = Math.min(min, sum1+sum2);
}
out.println(evensum+oddsum-min);
out.close();
}
}
//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 06bf838495079b07ecdc8a20336eba89 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.text.DecimalFormat;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
private static FastReader fr = new FastReader();
private static Helper hp = new Helper();
private static StringBuilder result = new StringBuilder();
public static void main(String[] args) {
Task solver = new Task();
solver.solve();
}
static class Task {
public void solve() {
int n = fr.ni();
int[] arr = new int[n];
for(int i=0; i<n; i++) arr[i] = fr.ni();
if(n == 1){
System.out.println(arr[0]);
return;
}
long[] prefixSumOdd = new long[n];
long[] prefixSumEven = new long[n];
prefixSumOdd[0] = arr[0];
prefixSumEven[1] = arr[1];
for(int i=2; i<n; i+=2) prefixSumOdd[i] = prefixSumOdd[i-2] + arr[i];
for(int i=3; i<n; i+=2) prefixSumEven[i] = prefixSumEven[i-2] + arr[i];
long ans = 0;
long suffOdd = 0, suffEven = 0;
for(int i=n-1; i>=0; i--){
if(i == 0){
ans = Math.max(ans, arr[i] + Math.max(suffEven, suffOdd));
continue;
}
if((i+1)%2 == 0){
suffEven += arr[i];
ans = Math.max(ans, suffEven + prefixSumOdd[i-1]);
}
else{
suffOdd += arr[i];
ans = Math.max(ans, suffOdd + prefixSumEven[i-1]);
}
}
System.out.println(ans);
}
}
static class Helper {
public int[] ipArrInt(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = fr.ni();
return arr;
}
public long[] ipArrLong(int n, int si) {
long[] arr = new long[n];
for (int i = si; i < n; i++)
arr[i] = fr.nl();
return arr;
}
}
static class FastReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
private static PrintWriter pw;
public FastReader() {
reader = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out);
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 ni() {
return Integer.parseInt(next());
}
public long nl() {
return Long.parseLong(next());
}
public double nd() {
return Double.parseDouble(next());
}
public String rl() {
try {
return reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void print(String str) {
pw.print(str);
pw.flush();
}
}
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | ec2868dbedf81930ccc03202c0424fee | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | //created by Whiplash99
import java.io.*;
import java.util.*;
public class A
{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,N;
N=Integer.parseInt(br.readLine().trim());
String[] s=br.readLine().trim().split(" ");
int[] a=new int[N];
for(i=0;i<N;i++) a[i]=Integer.parseInt(s[i]);
long[] pref=new long[N];
long[] suf=new long[N];
pref[0]=a[0]; if(N>1) pref[1]=a[1];
suf[N-1]=a[N-1]; if(N>1) suf[N-2]=a[N-2];
for(i=2;i<N;i++) pref[i]=a[i]+pref[i-2];
for(i=N-3;i>=0;i--) suf[i]=a[i]+suf[i+2];
long max=0;
for(i=0;i<N-1;i++)
{
long tmp=a[i]+a[i+1];
if(i+3<N) tmp+=suf[i+3];
if(i-2>=0) tmp+=pref[i-2];
max=Math.max(max,tmp);
}
max=Math.max(max,pref[N-1]);
System.out.println(max);
}
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 84aa3e04232a0ad6b93fbe6dd7952c0b | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author AnandOza
*/
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);
DOmkarAndCircle solver = new DOmkarAndCircle();
solver.solve(1, in, out);
out.close();
}
static class DOmkarAndCircle {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
long[] a = in.readLongArray(n);
if (n == 1) {
out.println(a[0]);
return;
}
long[] prefix = new long[n + 2];
long[] suffix = new long[n + 2];
for (int i = 0; i < n; i++) {
prefix[i + 2] = a[i] + prefix[i];
}
Util.reverse(a);
for (int i = 0; i < n; i++) {
suffix[i + 2] = a[i] + suffix[i];
}
Util.reverse(suffix);
long answer = 0;
for (int i = 1; i < prefix.length; i++) {
answer = Math.max(answer, prefix[i] + suffix[i - 1]);
}
out.println(answer);
}
}
static class InputReader {
public final 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());
}
public long nextLong() {
return Long.parseLong(next());
}
public long[] readLongArray(int n) {
long[] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = nextLong();
}
return x;
}
}
static class Util {
public static void swap(long[] x, int i, int j) {
long t = x[i];
x[i] = x[j];
x[j] = t;
}
public static void reverse(long[] x) {
for (int i = 0, j = x.length - 1; i < j; i++, j--) {
swap(x, i, j);
}
}
private Util() {
}
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 0746b37d2e7ef0f298face22d0f6b5b0 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.util.*;
import java.io.*;
public class MainClass
{
public static void main(String[] args) throws IOException
{
Reader in = new Reader();
int n = in.nextInt();
long[] A = new long[n];
for (int i=0;i<n;i++) A[i] = in.nextLong();
if (n == 1)
{
System.out.println(A[0]);
System.exit(0);
}
long[] even = new long[n];
long[] odd = new long[n];
even[0] = A[0];
for (int i=2;i<n;i+=2)
{
even[i] = even[i - 2] + A[i];
}
odd[1] = A[1];
for (int i=3;i<n;i+=2)
{
odd[i] = odd[i - 2] + A[i];
}
long[] evenBack = new long[n];
long[] oddBack = new long[n];
evenBack[n - 1] = A[n - 1];
for (int i=n - 3;i>=0;i-=2)
{
evenBack[i] = evenBack[i + 2] + A[i];
}
oddBack[n - 2] = A[n - 2];
for (int i=n - 4;i>=0;i-=2)
{
oddBack[i] = oddBack[i + 2] + A[i];
}
long ans = Long.MIN_VALUE;
for (int i=0;i<n;i++)
{
long cost = 0L;
if (i % 2 == 0)
{
cost += even[i];
if (i + 1 < n)
cost += oddBack[i + 1];
}
else
{
cost += odd[i];
if (i + 1 < n)
cost += evenBack[i + 1];
}
ans = Math.max(ans, cost);
}
System.out.println(ans);
}
}
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\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | efe2d403714d5b03ef4afeb9dff8410a | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.*;
import java.util.*;
public class check4 {
public static void main(String[] args) throws IOException{
Reader sc=new Reader();
PrintWriter out = new PrintWriter(System.out);
//int t=sc.nextInt();
// while(t-->0)
// {
int n=sc.nextInt();
long arr[]=new long[n];
for(int i=0;i<n;i++) arr[i]=sc.nextInt();
long res[]=new long[2*n];
int index=0;
for(int i=0;i<n;i+=2)
{
res[index]=arr[i];
res[n+index]=arr[i];
index+=1;
}
for(int i=1;i<n;i+=2)
{
res[index]=arr[i];
res[n+index]=arr[i];
index+=1;
}
//System.out.println(Arrays.toString(res));
long sum=0;
for(int i=0;i<(n+1)/2;i++)
{
sum+=res[i];
}
long maxi=sum;
int s=0;
int e=(n+1)/2;
while(e<n*2)
{
sum+=res[e]-res[s];
s+=1;
e+=1;
maxi=Math.max(maxi,sum);
}
System.out.println(maxi);
// int size=(n+1)/2;
// long odd[]=new long[size];
// long even[]=new long[size];
//
// even[0]=arr[0];
//
// int size1=4*(n+1)/2;
// long res[]=new long[size1];
//
// res[0]=arr[0];
// for(int i=1;i<n;i+=2)
// {
// even[i/2+1]=arr[i];
// res[i/2+1]=arr[i];
// //res[size1/2+i/2+1]=arr[i];
// }
//
// for(int i=0;i<n;i+=2)
// {
// odd[i/2]=arr[i];
// res[size+i/2]=arr[i];
// //res[size+size1/2+i/2]=arr[i];
// }
// for(int i=0;i<size1/2;i++)
// {
// res[size1/2+i]=res[i];
// }
// long sum=0;
//
// for(int i=0;i<(n+1)/2;i++)
// {
// sum+=res[i];
// }
// long maxi=sum;
// int s=0;
// int e=(n+1)/2;
//
// while(e<size1)
// {
// sum+=res[e]-res[s];
// s+=1;
// e+=1;
// maxi=Math.max(maxi,sum);
// }
// //System.out.println(Arrays.toString(res));
// System.out.println(maxi);
// System.out.println(Arrays.toString(odd)+""+Arrays.toString(even));
// long oddsum1[]=new long[size];
// long evensum1[]=new long[size];
// long oddsum[]=new long[size];
// long evensum[]=new long[size];
// for(int i=size-1;i>=0;i--)
// {
// if(i==size-1)
// {
// oddsum[i]=odd[i];
// evensum[i]=even[i];
// continue;
// }
// oddsum[i]=oddsum[i+1]+odd[i];
// evensum[i]=evensum[i+1]+even[i];
// }
//
// for(int i=0;i<size;i++)
// {
// if(i==0)
// {
// oddsum1[i]=odd[i];
// evensum1[i]=even[i];
// continue;
// }
// oddsum1[i]=oddsum1[i-1]+odd[i];
// evensum1[i]=evensum1[i-1]+even[i];
// }
// long max=Math.max(oddsum[0],evensum[0]);
// for(int i=0;i<size-1;i++)
// {
// long tempmax=Math.max(oddsum1[i]+evensum[i+1],evensum1[i]+oddsum[i+1]);
// max=Math.max(max,tempmax);
// }
// System.out.println(max);
//}
out.flush();
}
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 nextLine() 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 boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() throws IOException{
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 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\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | cbd02695bf9a06260a19baee236bc7b0 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.*;
import java.util.*;
public class check4 {
public static void main(String[] args) throws IOException{
Reader sc=new Reader();
PrintWriter out = new PrintWriter(System.out);
//int t=sc.nextInt();
// while(t-->0)
// {
int n=sc.nextInt();
long arr[]=new long[n];
for(int i=0;i<n;i++) arr[i]=sc.nextInt();
if(n==1)
{
System.out.println(arr[0]);
return;
}
if(n==2)
{
System.out.println(Math.max(arr[0],arr[1]));
return;
}
int size=(n+1)/2;
long odd[]=new long[size];
long even[]=new long[size-1];
//even[0]=0;
for(int i=1;i<n;i+=2)
{
even[i/2]=arr[i];
}
for(int i=0;i<n;i+=2)
{
odd[i/2]=arr[i];
}
// System.out.println(Arrays.toString(odd)+""+Arrays.toString(even));
long oddsum1[]=new long[size];
long evensum1[]=new long[size-1];
long oddsum[]=new long[size];
long evensum[]=new long[size-1];
for(int i=size-1;i>=0;i--)
{
if(i==size-1)
{
oddsum[i]=odd[i];
if(i-1>=0) evensum[i-1]=even[i-1];
continue;
}
oddsum[i]=oddsum[i+1]+odd[i];
if(i-1>=0) evensum[i-1]=evensum[i]+even[i-1];
}
for(int i=0;i<size;i++)
{
if(i==0)
{
oddsum1[i]=odd[i];
if(i<size-1) evensum1[i]=even[i];
continue;
}
oddsum1[i]=oddsum1[i-1]+odd[i];
if(i<size-1) evensum1[i]=evensum1[i-1]+even[i];
}
long max=Math.max(oddsum[0],evensum[0]);
for(int i=0;i<size-1;i++)
{
long tempmax=Math.max(oddsum1[i]+evensum[i],evensum1[i]+oddsum[i+1]);
max=Math.max(max,tempmax);
}
System.out.println(max);
//}
out.flush();
}
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 nextLine() 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 boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() throws IOException{
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 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\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | e7c660d9872890676dbf9f5a9b3acab7 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.util.*;
import java.lang.*;
// StringBuilder uses java.lang
public class mC {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] inp = new long[n];
long[] ok = new long[n];
long start = 0;
for (int i=0;i<n;i++) {
inp[i] = sc.nextLong();
if (i % 2 == 0) {
start+=inp[i];
}
}
ok[0]=start;
long ans = ok[0];
for (int i=1;i<n;i++) {
ok[i]=ok[i-1]+inp[(2*i-1+n) % n]-inp[(2*i-2+n) % n];
ans=Math.max(ok[i],ans);
}
System.out.println(ans);
}
public static int findNthInArray(int[] arr,int val,int start,int o) {
if (o==0) {
return start-1;
} else if (arr[start] == val) {
return findNthInArray(arr,val,start+1,o-1);
} else {
return findNthInArray(arr,val,start+1,o);
}
}
public static ArrayList<Integer> dfs(int at,ArrayList<Integer> went,ArrayList<ArrayList<Integer>> connect) {
for (int i=0;i<connect.get(at).size();i++) {
if (!(went.contains(connect.get(at).get(i)))) {
went.add(connect.get(at).get(i));
went=dfs(connect.get(at).get(i),went,connect);
}
}
return went;
} public static int[] bfs (int at, int[] went, ArrayList<ArrayList<Integer>> queue, int numNodes, ArrayList<ArrayList<Integer>> connect) {
if (went[at]==0) {
went[at]=queue.get(numNodes).get(1);
for (int i=0;i<connect.get(at).size();i++) {
if (went[connect.get(at).get(i)]==0) {
ArrayList<Integer> temp = new ArrayList<>();
temp.add(connect.get(at).get(i));
temp.add(queue.get(numNodes).get(1)+1);
queue.add(temp);
}
}
}
if (queue.size()==numNodes+1) {
return went;
} else {
return bfs(queue.get(numNodes+1).get(0),went, queue, numNodes+1, connect);
}
}
public static long fastPow(int base,long exp,long mod) {
if (exp==0) {
return 1;
} else {
if (exp % 2 == 1) {
long z = fastPow(base,(exp-1)/2,mod);
return ((((z*base) % mod) * z) % mod);
} else {
long z = fastPow(base,exp/2,mod);
return ((z*z) % mod);
}
}
}
public static long fastPow(int base,long exp) {
if (exp==0) {
return 1;
} else {
if (exp % 2 == 1) {
long z = fastPow(base,(exp-1)/2);
return ((((z*base)) * z));
} else {
long z = fastPow(base,exp/2);
return ((z*z));
}
}
}
public static int firstLarger(int val,ArrayList<Integer> ok,int left,int right) {
if (ok.get(right)<=val) {
return -1;
}
if (left==right) {
return left;
} else if (left+1==right) {
if (val<ok.get(left)) {
return left;
} else {
return right;
}
} else {
int mid = (left+right)/2;
if (ok.get(mid)>val) {
return firstLarger(val,ok,left,mid);
} else {
return firstLarger(val,ok,mid+1,right);
}
}
}
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 16db7bfcf73c6c0cc1837f4ca9bd5b0c | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class stone {
public static void main(String args[]) throws IOException {
FastReader sc = new FastReader();
BufferedWriter w = new BufferedWriter(new PrintWriter(System.out));
int n=sc.nextInt();
long a[]=new long[n];
long b[]=new long [2*n];
long odd[]=new long[(n+1)/2];
long even[]=new long[(n+1)/2];
long odd_sum=0;
long even_sum=0;
for(int j=0;j<n;j++)
{
a[j]=sc.nextLong();
b[j]=a[j];
b[j+n]=a[j];
if(j%2==0)
{
odd_sum+=a[j];
}
else
{
even_sum+=a[j];
}
}
even_sum+=b[n];
long sum=Long.MIN_VALUE;
if(odd_sum>even_sum)
{
sum=odd_sum;
}
else
{
sum=even_sum;
}
//System.out.println(Arrays.toString(b));
for(int x=0;x<n-1;x++)
{
//System.out.println("X: "+x);
if(x%2==0)
{
odd_sum-=b[x];
odd_sum+=b[n+x+1];
//System.out.println("b[n+x+1]: "+b[n+x+1]);
}
else
{
even_sum-=b[x];
even_sum+=b[n+x+1];
//System.out.println("even b[n+x+1]: "+b[n+x+1]);
}
if(even_sum>sum)
{
sum=even_sum;
}
if(odd_sum>sum)
{
sum=odd_sum;
}
}
w.write(sum+"");
w.close();
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader() throws IOException {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() throws NumberFormatException,IOException,NullPointerException
{
while (st == null || !st.hasMoreElements())
{
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
int nextInt()throws NumberFormatException,IOException,NullPointerException
{
return Integer.parseInt(next());
}
long nextLong()throws NumberFormatException,IOException,NullPointerException
{
return Long.parseLong(next());
}
double nextDouble()throws NumberFormatException,IOException,NullPointerException
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | fcb587df2316432a7a347fa1a30b4dc2 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
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);
solve(in, out);
out.close();
}
static String reverse(String s) {
return (new StringBuilder(s)).reverse().toString();
}
static void sieveOfEratosthenes(int n, int factors[])
{
factors[1]=1;
for(int p = 2; p*p <=n; p++)
{
if(factors[p] == 0)
{
factors[p]=p;
for(int i = p*p; i <= n; i += p)
factors[i] = p;
}
}
}
static void sort(int ar[]) {
int n = ar.length;
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static long ncr(long n, long r, long mod) {
if (r == 0)
return 1;
long val = ncr(n - 1, r - 1, mod);
val = (n * val) % mod;
val = (val * modInverse(r, mod)) % mod;
return val;
}
public static void solve(InputReader sc, PrintWriter pw) {
int i, j = 0;
int t = 1;
// int t = sc.nextInt();
u: while (t-- > 0) {
int n=sc.nextInt();
long a[]=new long[n];
long sum=0;
for(i=0;i<n;i++){
a[i]=sc.nextInt();
sum+=a[i];
}
long even[]=new long[n];
long odd[]=new long[n];
even[0]=a[0];
if(n==1){
pw.println(a[0]);
continue u;
}
odd[1]=a[1];
even[1]=a[0];
for(i=2;i<n;i++){
odd[i]=odd[i-1];
even[i]=even[i-1];
if(i%2==0)
even[i]+=a[i];
else
odd[i]+=a[i];
}
long sum1=0,max1=0;
for(i=1;i<n-1;i++){
sum1=0;
sum1+=a[i]+a[i+1];
if(i%2==0){
sum1+=even[i-1]+(odd[n-1]-odd[i+1]);
}
else{
sum1+=odd[i-1]+(even[n-1]-even[i+1]);
}
max1=Math.max(max1,sum1);
}
sum1=a[0]+a[1]+odd[n-1]-odd[1];
max1=Math.max(max1,sum1);
sum1=a[n-1]+a[0]+even[n-2]-even[0];
max1=Math.max(max1,sum1);
pw.println(max1);
}
}
static class Pair implements Comparable<Pair> {
int a;
int b;
// int c;
Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair p) {
return (a-p.a);
}
}
static boolean isPrime(long n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long fast_pow(long base, long n, long M) {
if (n == 0)
return 1;
if (n == 1)
return base % M;
long halfn = fast_pow(base, n / 2, M);
if (n % 2 == 0)
return (halfn * halfn) % M;
else
return (((halfn * halfn) % M) * base) % M;
}
static long modInverse(long n, long M) {
return fast_pow(n, M - 2, M);
}
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());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | c2636ec82fd483f8e178ce5822cc4199 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static Queue<Integer>q=new LinkedList<Integer>();
public static void primeFactors(int n)
{
// Print the number of 2s that divide n
while (n % 2 == 0) {
q.add(2);
n /= 2;
return;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i += 2) {
// While i divides n, print i and divide n
while (n % i == 0) {
q.add(i);
n /= i;
return;
}
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (n > 2)
q.add(n);
return;
}
public static void main(String[] args) throws Exception {
int n=sc.nextInt();
int[]a=sc.nextIntArray(n);
long[]odd=new long[n+1];
long[]even=new long[n+1];
long o=0;
long e=0;
for(int i=0;i<n;i++) {
if(i%2==0) {
e+=a[i];
}else {
o+=a[i];
}
odd[i+1]=o;
even[i+1]=e;
}
// for(int i=1;i<=n;i++) {
// odd[i]+=odd[i-1];
// even[i]+=even[i-1];
// }
long ans=0;
// pw.println(Arrays.toString(odd));
// pw.println(Arrays.toString(even));
for(int i=0;i<=n;i++) {
ans=Math.max(ans, odd[n]-odd[i]+even[i]);
ans=Math.max(ans, even[n]-even[i]+odd[i]);
}
pw.println(ans);
pw.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair)o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Double(x).hashCode() * 31 + new Double(y).hashCode();
}
public int compareTo(pair other) {
if(this.x==other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if(this.y==other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
public static long GCD(long a, long b) {
if (b == 0)
return a;
if (a == 0)
return b;
return (a > b) ? GCD(a % b, b) : GCD(a, b % a);
}
public static long LCM(long a, long b) {
return a * b / GCD(a, b);
}
static long Pow(long a, long e, long mod) // O(log e)
{
a %= mod;
long res = 1l;
while (e > 0) {
if ((e & 1) == 1)
res = (res * a) % mod;
a = (a * a) % mod;
e >>= 1l;
}
return res;
}
public static long modinverse(long a,long mod) {
return Pow(a, mod-2, mod);
}
static long nc(int n, int r) {
if (n < r)
return 0;
long v = fac[n];
v *= Pow(fac[r], mod - 2, mod);
v %= mod;
v *= Pow(fac[n - r], mod - 2, mod);
v %= mod;
return v;
}
static long np(int n,int r) {
return (nc(n, r)*fac[r])%mod;
}
public static boolean isprime(long a) {
if (a == 0 || a == 1) {
return false;
}
if (a == 2) {
return true;
}
for (int i = 2; i < Math.sqrt(a) + 1; i++) {
if (a % i == 0) {
return false;
}
}
return true;
}
public static boolean isPal(String s) {
boolean t = true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
t = false;
break;
}
}
return t;
}
public static long RandomPick(long[] a) {
int n = a.length;
int r = rn.nextInt(n);
return a[r];
}
public static int RandomPick(int[] a) {
int n = a.length;
int r = rn.nextInt(n);
return a[r];
}
static class hash {
int[]HashsArray;
boolean reverse;
int prelen;
public hash(String s, boolean r) {
prelen = s.length();
reverse=r;
HashsArray = new int[prelen + 1];
if (HashsArrayInd == 0) {
int[] mods = { 1173017693, 1173038827, 1173069731, 1173086977, 1173089783, 1173092147, 1173107093,
1173114391, 1173132347, 1173144367, 1173150103, 1173152611, 1173163993, 1173174127, 1173204679,
1173237343, 1173252107, 1173253331, 1173255653, 1173260183, 1173262943, 1173265439, 1173279091,
1173285331, 1173286771, 1173288593, 1173298123, 1173302129, 1173308827, 1173310451, 1173312383,
1173313571, 1173324371, 1173361529, 1173385729, 1173387217, 1173387361, 1173420799, 1173421499,
1173423077, 1173428083, 1173442159, 1173445549, 1173451681, 1173453299, 1173454729, 1173458401,
1173459491, 1173464177, 1173468943, 1173470041, 1173477947, 1173500677, 1173507869, 1173522919,
1173537359, 1173605003, 1173610253, 1173632671, 1173653623, 1173665447, 1173675577, 1173675787,
1173684683, 1173691109, 1173696907, 1173705257, 1173705523, 1173725389, 1173727601, 1173741953,
1173747577, 1173751499, 1173759449, 1173760943, 1173761429, 1173762509, 1173769939, 1173771233,
1173778937, 1173784637, 1173793289, 1173799607, 1173802823, 1173808003, 1173810919, 1173818311,
1173819293, 1173828167, 1173846677, 1173848941, 1173853249, 1173858341, 1173891613, 1173894053,
1173908039, 1173909203, 1173961541, 1173968989, 1173999193};
mod = RandomPick(mods);
int[] primes = { 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 };
prime = RandomPick(primes);
prepow = new int[1000010];
prepow[0] = 1;
for (int i = 1; i < 1000010; i++) {
prepow[i] = (int) ((1l * prepow[i - 1] * prime) % mod);
}
}
if (!reverse) {
for (int i = 0; i < prelen; i++) {
if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z')
HashsArray[i + 1] = (int) ((1l * HashsArray[i]
+ ((1l * s.charAt(i) - 'a' + 1) *prepow[i]) % mod) % mod);
else
HashsArray[i + 1] = (int) ((1l * HashsArray[i]
+ ((1l * s.charAt(i) - 'A' + 27) * prepow[i]) % mod) % mod);
}
} else {
for (int i = 0; i < prelen; i++) {
if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z')
HashsArray[i + 1] = (int) ((1l * HashsArray[i]
+ ((1l * s.charAt(i) - 'a' + 1) * prepow[1000010 - 1 - i]) % mod) % mod);
else
HashsArray[i + 1] = (int) ((1l * HashsArray[i]
+ ((1l * s.charAt(i) - 'A' + 27) * prepow[1000010 - 1 - i]) % mod) % mod);
}
}
HashsArrayInd++;
}
public int PHV(int l, int r) {
if (l > r) {
return 0;
}
int val = (int) ((1l * HashsArray[r] + mod - HashsArray[l - 1]) % mod);
if (!reverse) {
// val = (int) ((1l * val * modinverse(prepow[l-1], mod)) % mod);
val = (int) ((1l * val * prepow[1000010 - l]) % mod);
} else {
// val = (int) ((1l * val * modinverse(prepow[prelen-r], mod)) % mod);
val = (int) ((1l * val * prepow[r - 1]) % mod);
}
return val;
}
}
public static void genprime(int n) {
boolean prime[] = new boolean[n+1];
for(int i=0;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
for(int i = 2; i <= n; i++)
{
if(prime[i] == true) {
primes.put(i,primes.size());
primes2.put(primes2.size(),i);
}
}
}
public static long LSB(long x) {
return x&-x;
}
static class fenwick {
long[] arr;
public fenwick(PriorityQueue<Integer> a) {
PriorityQueue<Integer>q=new PriorityQueue<Integer>(a);
arr=new long[a.size()+1];
int i=1;
while(!q.isEmpty()) {
int z=q.poll();
arr[i]+=z;
if(i+LSB(i)<=a.size()) {
arr[(int) (i+LSB(i))]+=arr[i];
}
i++;
}
}
public fenwick(TreeSet<Integer> a) {
arr=new long[a.size()+1];
int i=1;
for(int h:a) {
arr[i]+=h;
if(i+LSB(i)<=a.size()) {
arr[(int) (i+LSB(i))]+=arr[i];
}
i++;
}
}
public fenwick(Integer[] a) {
arr=new long[a.length+1];
for(int i=1;i<=a.length;i++) {
arr[i]+=a[i-1];
if(i+LSB(i)<=a.length) {
arr[(int) (i+LSB(i))]+=arr[i];
}
}
}
public fenwick(int[] a) {
arr=new long[a.length+1];
for(int i=1;i<=a.length;i++) {
arr[i]+=a[i-1];
if(i+LSB(i)<=a.length) {
arr[(int) (i+LSB(i))]+=arr[i];
}
}
}public fenwick(long[] a) {
arr=new long[a.length+1];
for(int i=1;i<=a.length;i++) {
arr[i]+=a[i];
if(i+LSB(i)<=a.length) {
arr[(int) (i+LSB(i))]+=arr[i];
}
}
}
public void update(int ind,long x) {
int i=ind;
while(i<arr.length) {
arr[i]+=x;
i+=LSB(i);
}
}
public long PrefixSum(int ind) {
long sum=0;
int i=ind;
while(i>0) {
sum+=arr[i];
i=(int) (i-LSB(i));
}
return sum;
}
public long RangeQuerey(int l,int r) {
return this.PrefixSum(r+1)-this.PrefixSum(l);
}
public long maxConsecutiveValue(int k) {
long max=Long.MIN_VALUE;
for(int i=k-1;i<arr.length-1;i++) {
max= Math.max(max, this.RangeQuerey(i-k+1, i));
}
return max;
}
public long minConsecutiveValue(int k) {
long min=Long.MAX_VALUE;
for(int i=k-1;i<arr.length-1;i++) {
min= Math.min(min, this.RangeQuerey(i-k+1, i));
}
return min;
}
public long value(int ind) {
return arr[ind];
}
}
static void sieveLinear(int N)
{
ArrayList<Integer> primes = new ArrayList<Integer>();
lp = new int[N + 1]; //lp[i] = least prime divisor of i
for(int i = 2; i <= N; ++i)
{
if(lp[i] == 0)
{
primes.add(i);
lp[i] = i;
}
int curLP = lp[i];
for(int p: primes)//all primes smaller than or equal my lowest prime divisor
if(p > curLP || p * 1l * i > N)
break;
else
lp[p * i] = p;
}
}
public static void primefactorization(int n) {
int x=n;
while(x>1) {
int lowestDivisor=lp[x];
while(x%lowestDivisor==0) {
primefactors.add(lowestDivisor);
x/=lowestDivisor;
}
}
}
public static class SuffixArray {
int[] SA;
int[] AS;
String SS;
public SuffixArray(String S) //has a terminating character (e.g. '$')
{
SS=S;
char[] s=new char[S.length()+1];
for(int i=0;i<S.length();i++) {
s[i]=S.charAt(i);
}
s[S.length()]='$';
int n = s.length, RA[] = new int[n];
SA = new int[n];
for(int i = 0; i < n; ++i) { RA[i] = s[i]; SA[i] = i; }
for(int k = 1; k < n; k <<= 1)
{
sort(SA, RA, n, k);
sort(SA, RA, n, 0);
int[] tmp = new int[n];
for(int i = 1, r = 0, s1 = SA[0], s2; i < n; ++i)
{
s2 = SA[i];
tmp[s2] = RA[s1] == RA[s2] && RA[s1 + k] == RA[s2 + k] ? r : ++r;
s1 = s2;
}
for(int i = 0; i < n; ++i)
RA[i] = tmp[i];
if(RA[SA[n-1]] == n - 1)
break;
}
AS=new int[SA.length];
for(int i=0;i<SA.length;i++) {
AS[SA[i]]=i;
}
}
public String toString() {
return Arrays.toString(SA);
}
public int get(int n) {
return SA[n];
}
public int Substring(String s) { // log(n)*|s|
int low=0;
int high=SA.length;
int mid=(low+high)/2;
int ind=-1;
while(low<high-1) {
if(SS.length()-SA[mid]<s.length()) {
boolean less=false;
for(int i=SA[mid];i<SS.length();i++) {
if(SS.charAt(i)>s.charAt(i-SA[mid])) {
less=true;
break;
}
if(SS.charAt(i)<s.charAt(i-SA[mid])) {
less=false;
break;
}
}
if(!less) {
low=mid;
}else {
high=mid;
}
}else {
boolean less=true;
boolean equal=true;
for(int i=SA[mid];i<SA[mid]+s.length();i++) {
if(SS.charAt(i)<s.charAt(i-SA[mid])&&equal) {
less=false;
equal=false;
break;
}
if(SS.charAt(i)!=s.charAt(i-SA[mid])){
equal=false;
}
}
if(equal) {
ind=SA[mid];
}
if(!less) {
low=mid;
}else {
high=mid;
}
}
mid=(low+high)/2;
}
return ind;
}
public int LastSubstring(String s) { // log(n)*|s|
int low=0;
int high=SA.length;
int mid=(low+high)/2;
int ind=-1;
while(low<high-1) {
if(SS.length()-SA[mid]<s.length()) {
boolean less=true;
for(int i=SA[mid];i<SS.length();i++) {
if(SS.charAt(i)<s.charAt(i-SA[mid])) {
break;
}
if(SS.charAt(i)>s.charAt(i-SA[mid])) {
less=false;
break;
}
}
if(less) {
low=mid;
}else {
high=mid;
}
}else {
boolean less=true;
boolean equal=true;
for(int i=SA[mid];i<SA[mid]+s.length();i++) {
if(SS.charAt(i)>s.charAt(i-SA[mid])&&equal) {
less=false;
equal=false;
break;
}
if(SS.charAt(i)!=s.charAt(i-SA[mid])){
equal=false;
}
}
if(equal) {
ind=SA[mid];
}
if(less) {
low=mid;
}else {
high=mid;
}
}
mid=(low+high)/2;
}
return ind;
}
public int CountSubstring(String s) {
int z=LastSubstring(s);
if(z==-1)
return 0;
return AS[z]-AS[Substring(s)]+1;
}
public void sort(int[] SA, int[] RA, int n, int k)
{
int maxi = Math.max(256, n), c[] = new int[maxi];
for(int i = 0; i < n; ++i)
c[i + k < n ? RA[i + k] : 0]++;
for(int i = 0, sum = 0; i < maxi; ++i)
{
int t = c[i];
c[i] = sum;
sum += t;
}
int[] tmp = new int[n];
for(int i = 0; i < n; ++i)
{
int j = SA[i] + k;
tmp[c[j < n ? RA[j] : 0]++] = SA[i];
}
for(int i = 0; i < n; ++i)
SA[i] = tmp[i];
}
}
static LinkedList<Integer>primefactors=new LinkedList<>();
static TreeMap<Integer,Integer>primes=new TreeMap<Integer, Integer>();
static TreeMap<Integer,Integer>primes2=new TreeMap<Integer, Integer>();
static int[]lp;
static int HashsArrayInd = 0;
static int[] prepow;
static int prime = 61;
static long fac[];
static int mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 0139ae405daa1df438820e4aae2bcf8c | train_001.jsonl | 1455986100 | As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake.Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other.However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j.Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value. | 256 megabytes | import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.io.*;
import java.math.BigDecimal;
import java.util.*;
public class C343D
{
private static StringTokenizer st;
public static void nextLine(BufferedReader br) throws IOException
{
st = new StringTokenizer(br.readLine());
}
public static int nextInt()
{
return Integer.parseInt(st.nextToken());
}
public static String next()
{
return st.nextToken();
}
public static long nextLong()
{
return Long.parseLong(st.nextToken());
}
public static double nextDouble()
{
return Double.parseDouble(st.nextToken());
}
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
nextLine(br);
int n = nextInt();
long[] size = new long[n];
for (int i = 0; i < n; i++)
{
nextLine(br);
long r = nextLong();
long h = nextLong();
size[i] = r * r * h;
}
TreeSet<Cake> tree = new TreeSet<Cake>();
for (int i = 0; i < n; i++)
{
Cake temp = new Cake(size[i], size[i]);
Cake c = tree.lower(temp);
if (c == null)
{
tree.add(temp);
}
else
{
while (true)
{
Cake c2 = tree.higher(c);
if (c2 == null)
{
break;
}
if (c.volume + size[i] >= c2.volume)
{
tree.remove(c2);
}
else
{
break;
}
}
tree.add(new Cake(c.volume + size[i], size[i]));
}
}
long max = 0;
for (Cake c : tree)
{
max = Math.max(c.volume, max);
}
BigDecimal bd = new BigDecimal(max);
bd = bd.multiply(new BigDecimal("3.141592653589793238462"));
System.out.println(bd.toPlainString());
}
static class Cake implements Comparable<Cake>
{
long volume, max;
public Cake(long v, long m)
{
volume = v; max = m;
}
@Override
public int compareTo(Cake cake)
{
return new Long(max).compareTo(cake.max);
}
}
} | Java | ["2\n100 30\n40 10", "4\n1 1\n9 7\n1 4\n10 7"] | 2 seconds | ["942477.796077000", "3983.539484752"] | NoteIn first sample, the optimal way is to choose the cake number 1.In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4. | Java 7 | standard input | [
"data structures",
"dp"
] | 49a647a99eab59a61b42515d4289d3cd | The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of simple cakes Babaei has. Each of the following n lines contains two integers ri and hi (1 ≤ ri, hi ≤ 10 000), giving the radius and height of the i-th cake. | 2,000 | Print the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if . | standard output | |
PASSED | 5cd16ca4fbae9dee25cde382383c943d | train_001.jsonl | 1455986100 | As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake.Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other.However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j.Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value. | 256 megabytes | import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main
{
/********************************************** a list of common variables **********************************************/
private MyScanner scan = new MyScanner();
private PrintWriter out = new PrintWriter(System.out);
private final int SIZEN = (int)(1e5);
private final int MOD = (int)(1e9 + 7);
private final int[] DX = {0, 1, 0, -1}, DY = {-1, 0, 1, 0};
private ArrayList<Integer>[] edge;
public void foo()
{
int n = scan.nextInt();
Pair[] p = new Pair[n];
for(int i = 0;i < n;++i)
{
long r = scan.nextLong();
long h = scan.nextLong();
p[i] = new Pair(r * r * h, 0);
}
TreeSet<Pair> ts = new TreeSet<Pair>();
long ans = 0;
for(int i = 0;i < n;++i)
{
Pair cur = p[i];
Pair floor = ts.floor(cur);
cur.sum = null == floor ? cur.volume : cur.volume + floor.sum;
while(true)
{
Pair toDelete = ts.ceiling(cur);
if(null == toDelete || toDelete.sum > cur.sum)
{
break;
}
else
{
ts.remove(toDelete);
}
}
ts.add(cur);
ans = Math.max(ans, cur.sum);
}
out.println(ans * Math.PI);
}
public static void main(String[] args)
{
Main m = new Main();
m.foo();
m.out.close();
}
class Pair implements Comparable<Pair>
{
private long volume;
private long sum;
public Pair(long aVolume, long aSum)
{
volume = aVolume;
sum = aSum;
}
public int compareTo(Pair p)
{
if(volume != p.volume)
{
return Long.compare(volume, p.volume);
}
else
{
return Long.compare(sum, p.sum);
}
}
}
/********************************************** a list of common algorithms **********************************************/
/**
* 1---Get greatest common divisor
* @param a : first number
* @param b : second number
* @return greatest common divisor
*/
public long gcd(long a, long b)
{
return 0 == b ? a : gcd(b, a % b);
}
/**
* 2---Get the distance from a point to a line
* @param x1 the x coordinate of one endpoint of the line
* @param y1 the y coordinate of one endpoint of the line
* @param x2 the x coordinate of the other endpoint of the line
* @param y2 the y coordinate of the other endpoint of the line
* @param x the x coordinate of the point
* @param y the x coordinate of the point
* @return the distance from a point to a line
*/
public double getDist(long x1, long y1, long x2, long y2, long x, long y)
{
long a = y2 - y1;
long b = x1 - x2;
long c = y1 * (x2 - x1) - x1 * (y2 - y1);
return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b);
}
/**
* 3---Get the distance from one point to a segment (not a line)
* @param x1 the x coordinate of one endpoint of the segment
* @param y1 the y coordinate of one endpoint of the segment
* @param x2 the x coordinate of the other endpoint of the segment
* @param y2 the y coordinate of the other endpoint of the segment
* @param x the x coordinate of the point
* @param y the y coordinate of the point
* @return the distance from one point to a segment (not a line)
*/
public double ptToSeg(long x1, long y1, long x2, long y2, long x, long y)
{
double cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1);
if(cross <= 0)
{
return (x - x1) * (x - x1) + (y - y1) * (y - y1);
}
double d = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
if(cross >= d)
{
return (x - x2) * (x - x2) + (y - y2) * (y - y2);
}
double r = cross / d;
double px = x1 + (x2 - x1) * r;
double py = y1 + (y2 - y1) * r;
return (x - px) * (x - px) + (y - py) * (y - py);
}
/**
* 4---KMP match, i.e. kmpMatch("abcd", "bcd") = 1, kmpMatch("abcd", "bfcd") = -1.
* @param s: String to match.
* @param t: String to be matched.
* @return if can match, first index; otherwise -1.
*/
public int[] kmpMatch(char[] s, char[] t)
{
int n = s.length;
int m = t.length;
int[] next = new int[m + 1];
next[0] = -1;
int j = -1;
for(int i = 1;i < m;++i)
{
while(j >= 0 && t[i] != t[j + 1])
{
j = next[j];
}
if(t[i] == t[j + 1])
{
++j;
}
next[i] = j;
}
int[] left = new int[n + 1];
j = -1;
for(int i = 0;i < n;++i)
{
while(j >= 0 && s[i] != t[j + 1])
{
j = next[j];
}
if(s[i] == t[j + 1])
{
++j;
}
if(j == m - 1)
{
left[i + 1] = i - m + 2;
j = next[j];
}
}
for(int i = 1;i <= n;++i)
{
if(0 == left[i])
{
left[i] = left[i - 1];
}
}
return left;
}
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 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 & 15;
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 & 15) * m;
c = read();
}
}
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 | ["2\n100 30\n40 10", "4\n1 1\n9 7\n1 4\n10 7"] | 2 seconds | ["942477.796077000", "3983.539484752"] | NoteIn first sample, the optimal way is to choose the cake number 1.In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4. | Java 7 | standard input | [
"data structures",
"dp"
] | 49a647a99eab59a61b42515d4289d3cd | The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of simple cakes Babaei has. Each of the following n lines contains two integers ri and hi (1 ≤ ri, hi ≤ 10 000), giving the radius and height of the i-th cake. | 2,000 | Print the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if . | standard output | |
PASSED | d6ef92d745c559d5ae98ed1e0928568e | train_001.jsonl | 1455986100 | As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake.Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other.However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j.Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static class Pair implements Comparable<Pair> {
long volume;
long sum;
public Pair(long volume, long sum) {
this.volume = volume;
this.sum = sum;
}
@Override
public int compareTo(Pair other) {
int compareVolume = Long.compare(volume, other.volume);
int compareSum = Long.compare(sum, other.sum);
if (compareVolume != 0) {
return compareVolume;
} else {
return compareSum;
}
}
}
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
solve(in, out);
out.close();
}
private static void solve(InputReader in, PrintWriter out) {
int N = in.nextInt();
Pair[] arr = new Pair[N];
for (int i = 0; i < N; i++) {
long ri = in.nextLong();
long hi = in.nextLong();
arr[i] = new Pair(ri * ri * hi, 0);
}
TreeSet<Pair> treeSet = new TreeSet<Pair>();
long res = 0;
for (int i = 0; i < N; i++) {
Pair curr = arr[i];
Pair floorBound = treeSet.floor(curr);
if (floorBound != null) {
curr.sum = curr.volume + floorBound.sum;
} else {
curr.sum = curr.volume;
}
while (true) {
Pair toDelete = treeSet.ceiling(curr);
if (toDelete == null || toDelete.sum > curr.sum) {
break;
} else {
treeSet.remove(toDelete);
}
}
treeSet.add(curr);
res = Math.max(res, curr.sum);
}
out.println(res * Math.PI);
}
/*
*
*/
// --------------------------------------------------------
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());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["2\n100 30\n40 10", "4\n1 1\n9 7\n1 4\n10 7"] | 2 seconds | ["942477.796077000", "3983.539484752"] | NoteIn first sample, the optimal way is to choose the cake number 1.In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4. | Java 7 | standard input | [
"data structures",
"dp"
] | 49a647a99eab59a61b42515d4289d3cd | The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of simple cakes Babaei has. Each of the following n lines contains two integers ri and hi (1 ≤ ri, hi ≤ 10 000), giving the radius and height of the i-th cake. | 2,000 | Print the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if . | standard output | |
PASSED | ca2aa9ff26de9f6ade0ca07e02159300 | train_001.jsonl | 1455986100 | As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake.Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other.However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j.Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value. | 256 megabytes | import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main
{
/********************************************** a list of common variables **********************************************/
private MyScanner scan = new MyScanner();
private PrintWriter out = new PrintWriter(System.out);
private final double PI = Math.acos(-1.0);
private final int SIZEN = (int)(1e5);
private final int MOD = (int)(1e9 + 7);
private final int[] DX = {0, 1, 0, -1}, DY = {-1, 0, 1, 0};
private ArrayList<Integer>[] edge;
private Node[] t;
private long maxV;
public void build(int k, int left, int right)
{
t[k].left = left;
t[k].right = right;
if(left != right)
{
int mid = (left + right) >> 1;
build(k << 1, left, mid);
build(2 * k + 1, mid + 1, right);
}
}
public void update(int k, int pos, long vv)
{
int left = t[k].left, right = t[k].right;
if(left == right)
{
t[k].dp = Math.max(t[k].dp, vv);
return;
}
int mid = (left + right) >> 1;
if(pos <= mid) update(k << 1, pos, vv);
else update(2 * k + 1, pos, vv);
t[k].dp = Math.max(t[k].dp, Math.max(t[2 * k].dp, t[2 * k + 1].dp));
}
public void query(int k, int left, int right)
{
if(left <= t[k].left && right >= t[k].right)
{
maxV = Math.max(maxV, t[k].dp);
return;
}
int mid = (t[k].left + t[k].right) >> 1;
if(left <= mid) query(k << 1, left, right);
if(right > mid) query(2 * k + 1, left, right);
}
public void foo()
{
int n = scan.nextInt();
Point[] p = new Point[n];
for(int i = 0;i < n;++i)
{
int r = scan.nextInt();
int h = scan.nextInt();
p[i] = new Point(i + 1, (long)r * r * h);
}
Arrays.sort(p, new Comparator<Point>()
{
public int compare(Point p1, Point p2)
{
if(p1.v != p2.v) return p1.v < p2.v ? -1 : 1;
else return p2.index - p1.index;
}
});
t = new Node[n << 2];
for(int i = 0;i < t.length;++i)
{
t[i] = new Node(0,0,0);
}
build(1, 1, n);
long ans = 0;
for(int i = 0;i < n;++i)
{
maxV = 0;
query(1, 1, p[i].index);
update(1, p[i].index, maxV + p[i].v);
ans = Math.max(ans, maxV + p[i].v);
}
out.println(PI * ans);
}
public static void main(String[] args)
{
Main m = new Main();
m.foo();
m.out.close();
}
class Point
{
private int index;
private long v;
public Point(int aIndex, long aV)
{
index = aIndex;
v = aV;
}
}
class Node
{
private int left, right;
private long dp;
public Node(int aLeft, int aRight, long aDp)
{
left = aLeft;
right = aRight;
dp = aDp;
}
}
/********************************************** a list of common algorithms **********************************************/
/**
* 1---Get greatest common divisor
* @param a : first number
* @param b : second number
* @return greatest common divisor
*/
public long gcd(long a, long b)
{
return 0 == b ? a : gcd(b, a % b);
}
/**
* 2---Get the distance from a point to a line
* @param x1 the x coordinate of one endpoint of the line
* @param y1 the y coordinate of one endpoint of the line
* @param x2 the x coordinate of the other endpoint of the line
* @param y2 the y coordinate of the other endpoint of the line
* @param x the x coordinate of the point
* @param y the x coordinate of the point
* @return the distance from a point to a line
*/
public double getDist(long x1, long y1, long x2, long y2, long x, long y)
{
long a = y2 - y1;
long b = x1 - x2;
long c = y1 * (x2 - x1) - x1 * (y2 - y1);
return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b);
}
/**
* 3---Get the distance from one point to a segment (not a line)
* @param x1 the x coordinate of one endpoint of the segment
* @param y1 the y coordinate of one endpoint of the segment
* @param x2 the x coordinate of the other endpoint of the segment
* @param y2 the y coordinate of the other endpoint of the segment
* @param x the x coordinate of the point
* @param y the y coordinate of the point
* @return the distance from one point to a segment (not a line)
*/
public double ptToSeg(long x1, long y1, long x2, long y2, long x, long y)
{
double cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1);
if(cross <= 0)
{
return (x - x1) * (x - x1) + (y - y1) * (y - y1);
}
double d = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
if(cross >= d)
{
return (x - x2) * (x - x2) + (y - y2) * (y - y2);
}
double r = cross / d;
double px = x1 + (x2 - x1) * r;
double py = y1 + (y2 - y1) * r;
return (x - px) * (x - px) + (y - py) * (y - py);
}
/**
* 4---KMP match, i.e. kmpMatch("abcd", "bcd") = 1, kmpMatch("abcd", "bfcd") = -1.
* @param s: String to match.
* @param t: String to be matched.
* @return if can match, first index; otherwise -1.
*/
public int[] kmpMatch(char[] s, char[] t)
{
int n = s.length;
int m = t.length;
int[] next = new int[m + 1];
next[0] = -1;
int j = -1;
for(int i = 1;i < m;++i)
{
while(j >= 0 && t[i] != t[j + 1])
{
j = next[j];
}
if(t[i] == t[j + 1])
{
++j;
}
next[i] = j;
}
int[] left = new int[n + 1];
j = -1;
for(int i = 0;i < n;++i)
{
while(j >= 0 && s[i] != t[j + 1])
{
j = next[j];
}
if(s[i] == t[j + 1])
{
++j;
}
if(j == m - 1)
{
left[i + 1] = i - m + 2;
j = next[j];
}
}
for(int i = 1;i <= n;++i)
{
if(0 == left[i])
{
left[i] = left[i - 1];
}
}
return left;
}
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 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 & 15;
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 & 15) * m;
c = read();
}
}
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 | ["2\n100 30\n40 10", "4\n1 1\n9 7\n1 4\n10 7"] | 2 seconds | ["942477.796077000", "3983.539484752"] | NoteIn first sample, the optimal way is to choose the cake number 1.In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4. | Java 7 | standard input | [
"data structures",
"dp"
] | 49a647a99eab59a61b42515d4289d3cd | The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of simple cakes Babaei has. Each of the following n lines contains two integers ri and hi (1 ≤ ri, hi ≤ 10 000), giving the radius and height of the i-th cake. | 2,000 | Print the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if . | standard output | |
PASSED | d987f5425473ad68a259b0bc7510c410 | train_001.jsonl | 1375549200 | In a far away land, there are two cities near a river. One day, the cities decide that they have too little space and would like to reclaim some of the river area into land.The river area can be represented by a grid with r rows and exactly two columns — each cell represents a rectangular area. The rows are numbered 1 through r from top to bottom, while the columns are numbered 1 and 2.Initially, all of the cells are occupied by the river. The plan is to turn some of those cells into land one by one, with the cities alternately choosing a cell to reclaim, and continuing until no more cells can be reclaimed.However, the river is also used as a major trade route. The cities need to make sure that ships will still be able to sail from one end of the river to the other. More formally, if a cell (r, c) has been reclaimed, it is not allowed to reclaim any of the cells (r - 1, 3 - c), (r, 3 - c), or (r + 1, 3 - c).The cities are not on friendly terms, and each city wants to be the last city to reclaim a cell (they don't care about how many cells they reclaim, just who reclaims a cell last). The cities have already reclaimed n cells. Your job is to determine which city will be the last to reclaim a cell, assuming both choose cells optimally from current moment. | 256 megabytes | import java.util.*;
import static java.lang.Math.*;
import java.io.*;
public class C {
public static void p(Object...o) { System.out.println(Arrays.deepToString(o));}
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int R = in.nextInt();
int N = in.nextInt();
boolean[][] U = new boolean[R+2][2];
U[0][0] = true;
U[0][1] = true;
U[R+1][0] = true;
U[R+1][1] = true;
for (int i = 0; i < N; i++) {
int r = in.nextInt();
int c = in.nextInt()-1;
U[r][c] = true;
U[r-1][1-c] = true;
U[r][1-c] = true;
U[r+1][1-c] = true;
}
DP = new int[R+10][3][3];
for (int[][] a:DP)
for(int[] b:a)
Arrays.fill(b, -1);
boolean[] done = new boolean[U.length];
int ans = 0;
for (int at = 0; at < U.length; at++) {
if (U[at][0] && U[at][1])
continue;
if (done[at])
continue;
int top = 0;
if (U[at][0])
top = 1;
if (U[at][1])
top = 2;
int bot = 0;
done[at] = true;
for (int i = at + 1; i < U.length; i++) {
done[i] = true;
if (U[i][0] && U[i][1]) {
// p("game: ", i-at, top, bot);
ans ^= nim(i-at, top, bot);
break;
} else {
bot = 0;
if (U[i][0])
bot = 1;
if (U[i][1])
bot = 2;
}
}
}
if (ans != 0)
System.out.println("WIN");
else
System.out.println("LOSE");
}
static int[][][] DP;
// len is number of rows with something
static int nim(int len, int top, int bot) {
if (len <= 0)
return 0;
if (DP[len][top][bot] != -1)
return DP[len][top][bot];
HashSet<Integer> moves = new HashSet<Integer>();
for (int r = 0; r < len; r++) {
for (int c = 0; c < 2; c++) {
if (r == 0 && c == top-1)
continue;
if (r == len-1 && c == bot-1)
continue;
int topnim = nim(r, top, (1-c)+1);
int botnim = nim(len - r - 1, (1-c)+1, bot);
moves.add(topnim ^ botnim);
}
}
for (int i = 0; ;i++) {
if (!moves.contains(i)) {
DP[len][top][bot] = i;
return i;
}
}
}
}
| Java | ["3 1\n1 1", "12 2\n4 1\n8 1", "1 1\n1 2"] | 2 seconds | ["WIN", "WIN", "LOSE"] | NoteIn the first example, there are 3 possible cells for the first city to reclaim: (2, 1), (3, 1), or (3, 2). The first two possibilities both lose, as they leave exactly one cell for the other city. However, reclaiming the cell at (3, 2) leaves no more cells that can be reclaimed, and therefore the first city wins. In the third example, there are no cells that can be reclaimed. | Java 7 | standard input | [] | d66c7774acb7e4b1f5da105dfa5c1d36 | The first line consists of two integers r and n (1 ≤ r ≤ 100, 0 ≤ n ≤ r). Then n lines follow, describing the cells that were already reclaimed. Each line consists of two integers: ri and ci (1 ≤ ri ≤ r, 1 ≤ ci ≤ 2), which represent the cell located at row ri and column ci. All of the lines describing the cells will be distinct, and the reclaimed cells will not violate the constraints above. | 2,100 | Output "WIN" if the city whose turn it is to choose a cell can guarantee that they will be the last to choose a cell. Otherwise print "LOSE". | standard output | |
PASSED | 9786fef1b86d0d82bd2ed458d08832e0 | train_001.jsonl | 1375549200 | In a far away land, there are two cities near a river. One day, the cities decide that they have too little space and would like to reclaim some of the river area into land.The river area can be represented by a grid with r rows and exactly two columns — each cell represents a rectangular area. The rows are numbered 1 through r from top to bottom, while the columns are numbered 1 and 2.Initially, all of the cells are occupied by the river. The plan is to turn some of those cells into land one by one, with the cities alternately choosing a cell to reclaim, and continuing until no more cells can be reclaimed.However, the river is also used as a major trade route. The cities need to make sure that ships will still be able to sail from one end of the river to the other. More formally, if a cell (r, c) has been reclaimed, it is not allowed to reclaim any of the cells (r - 1, 3 - c), (r, 3 - c), or (r + 1, 3 - c).The cities are not on friendly terms, and each city wants to be the last city to reclaim a cell (they don't care about how many cells they reclaim, just who reclaims a cell last). The cities have already reclaimed n cells. Your job is to determine which city will be the last to reclaim a cell, assuming both choose cells optimally from current moment. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) {
new C().run();
}
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
int[][] g;
public int mex(HashSet<Integer> hs) {
for (int i = 0;; i++) {
if (!hs.contains(i))
return i;
}
}
boolean[][] create(int x, int fm, int sm) {
boolean[][] d = new boolean[x + 2][2];
for (int i = 1; i <= x; i++) {
d[i][0] = true;
d[i][1] = true;
}
for (int i = 0; i < 2; i++) {
if ((fm & (i + 1)) > 0) {
d[0][i] = true;
}
if ((sm & (i + 1)) > 0) {
d[x + 1][i] = true;
}
}
return d;
}
public int cnt(boolean[][] d, int r, int c) {
boolean[] back = new boolean[] { d[r - 1][1 - c], d[r][1 - c],
d[r + 1][1 - c], d[r][c], };
d[r - 1][1 - c] = false;
d[r][1 - c] = false;
d[r + 1][1 - c] = false;
d[r][c] = false;
int ans = get_xor(d);
d[r - 1][1 - c] = back[0];
d[r][1 - c] = back[1];
d[r + 1][1 - c] = back[2];
d[r][c] = back[3];
return ans;
}
public int get_xor(boolean[][] d) {
int ans = 0;
int fi = 0;
while (true) {
while (fi < d.length && !d[fi][0] && !d[fi][1]) {
fi++;
}
if (fi == d.length)
break;
int si = fi;
while (si < d.length && (d[si][0] || d[si][1])) {
si++;
}
int l = si - fi;
int fm = (d[fi][0] ? 1 : 0) + (d[fi][1] ? 2 : 0);
int sm = (d[si - 1][0] ? 1 : 0) + (d[si - 1][1] ? 2 : 0);
if (fi == si - 1) {
sm = 0;
}
fm %= 3;
sm %= 3;
if (sm > 0)
l--;
if (fm > 0)
l--;
ans ^= gen(l, fm * 4 + sm);
fi = si;
}
return ans;
}
public int gen(int x, int mask) {
if (g[x][mask] != -1)
return g[x][mask];
int fm = mask >> 2;
int sm = mask & 3;
HashSet<Integer> prob = new HashSet<Integer>();
if (fm > 0) {
prob.add(gen(x - 1, mask));
}
if (sm > 0) {
prob.add(gen(x - 1, mask));
}
boolean[][] d = create(x, fm, sm);
for (int i = 1; i <= x; i++) {
prob.add(cnt(d, i, 1));
prob.add(cnt(d, i, 0));
}
return g[x][mask] = mex(prob);
}
public void solve() throws IOException {
int r = nextInt();
g = new int[r + 1][16];
for (int i = 0; i < g.length; i++) {
Arrays.fill(g[i], -1);
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
g[0][i * 4 + j] = (i ^ j) == 0 ? 0 : 1;
}
}
boolean[][] d = create(r, 0, 0);
int n = nextInt();
for (int i = 0; i < n; i++) {
int rr = nextInt();
int cc = nextInt() - 1;
d[rr - 1][1 - cc] = false;
d[rr][1 - cc] = false;
d[rr + 1][1 - cc] = false;
d[rr][cc] = false;
}
out.println(get_xor(d) == 0 ? "LOSE" : "WIN");
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}
| Java | ["3 1\n1 1", "12 2\n4 1\n8 1", "1 1\n1 2"] | 2 seconds | ["WIN", "WIN", "LOSE"] | NoteIn the first example, there are 3 possible cells for the first city to reclaim: (2, 1), (3, 1), or (3, 2). The first two possibilities both lose, as they leave exactly one cell for the other city. However, reclaiming the cell at (3, 2) leaves no more cells that can be reclaimed, and therefore the first city wins. In the third example, there are no cells that can be reclaimed. | Java 7 | standard input | [] | d66c7774acb7e4b1f5da105dfa5c1d36 | The first line consists of two integers r and n (1 ≤ r ≤ 100, 0 ≤ n ≤ r). Then n lines follow, describing the cells that were already reclaimed. Each line consists of two integers: ri and ci (1 ≤ ri ≤ r, 1 ≤ ci ≤ 2), which represent the cell located at row ri and column ci. All of the lines describing the cells will be distinct, and the reclaimed cells will not violate the constraints above. | 2,100 | Output "WIN" if the city whose turn it is to choose a cell can guarantee that they will be the last to choose a cell. Otherwise print "LOSE". | standard output | |
PASSED | 6c91762b5e2d2c5bc307bf68a9e30459 | train_001.jsonl | 1375549200 | In a far away land, there are two cities near a river. One day, the cities decide that they have too little space and would like to reclaim some of the river area into land.The river area can be represented by a grid with r rows and exactly two columns — each cell represents a rectangular area. The rows are numbered 1 through r from top to bottom, while the columns are numbered 1 and 2.Initially, all of the cells are occupied by the river. The plan is to turn some of those cells into land one by one, with the cities alternately choosing a cell to reclaim, and continuing until no more cells can be reclaimed.However, the river is also used as a major trade route. The cities need to make sure that ships will still be able to sail from one end of the river to the other. More formally, if a cell (r, c) has been reclaimed, it is not allowed to reclaim any of the cells (r - 1, 3 - c), (r, 3 - c), or (r + 1, 3 - c).The cities are not on friendly terms, and each city wants to be the last city to reclaim a cell (they don't care about how many cells they reclaim, just who reclaims a cell last). The cities have already reclaimed n cells. Your job is to determine which city will be the last to reclaim a cell, assuming both choose cells optimally from current moment. | 256 megabytes | import java.util.*;
public class C {
static boolean[][] move(boolean[][] G, int r, int c) {
boolean[][] G2 = new boolean[G.length][2];
for(int rr=0; rr<G.length; rr++)
for(int cc=0; cc<2; cc++)
G2[rr][cc] = G[rr][cc];
G2[r][c] = true;
if(r+1<G2.length) G2[r+1][1-c] = true;
if(r-1>=0) G2[r-1][1-c] = true;
G2[r][1-c] = true;
return G2;
}
static List<boolean[][]> succ(boolean[][] G) {
List<boolean[][]> A = new ArrayList<boolean[][]>();
int st = 0;
for(int r=0; r<G.length; r++) {
if(G[r][0] && G[r][1]) {
boolean[][] N = new boolean[r-st][2];
for(int rr=st; rr<r; rr++)
for(int c=0; c<2; c++)
N[rr-st][c] = G[rr][c];
A.add(N);
st = r+1;
}
}
boolean[][] N = new boolean[G.length-st][2];
for(int rr=st; rr<G.length; rr++)
for(int c=0; c<2; c++)
N[rr-st][c] = G[rr][c];
A.add(N);
return A;
}
static Integer[] DP = new Integer[2000];
static int nimber(boolean[][] G) {
if(G.length==0) return 0;
int key_top = (G[0][0]?1:0)*2+(G[0][1]?1:0);
int key_bottom = (G[G.length-1][0]?1:0)*2 + (G[G.length-1][1]?1:0);
int key = G.length*4*4 + key_top*4 + key_bottom;
if(DP[key]!=null) return DP[key];
Set<Integer> N = new HashSet<Integer>();
for(int r=0; r<G.length; r++)
for(int c=0; c<2; c++) {
if(!G[r][c]) {
int val = 0;
for(boolean[][] NXT : succ(move(G,r,c))) {
val ^= nimber(NXT);
//if(G.length==3) System.out.println("NXT:"+Arrays.deepToString(G)+" "+Arrays.deepToString(NXT)+" "+val);
}
N.add(val);
}
}
for(int ans=0;; ans++)
if(!N.contains(ans))
return DP[key]=ans;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int R = in.nextInt();
boolean[][] G = new boolean[R][2];
int m = in.nextInt();
for(int i=0; i<m; i++) {
int r = in.nextInt()-1;
int c = in.nextInt()-1;
G = move(G,r,c);
}
int ans = 0;
for(boolean[][] N : succ(G))
ans ^= nimber(N);
System.out.println(ans==0 ? "LOSE" : "WIN");
}
}
| Java | ["3 1\n1 1", "12 2\n4 1\n8 1", "1 1\n1 2"] | 2 seconds | ["WIN", "WIN", "LOSE"] | NoteIn the first example, there are 3 possible cells for the first city to reclaim: (2, 1), (3, 1), or (3, 2). The first two possibilities both lose, as they leave exactly one cell for the other city. However, reclaiming the cell at (3, 2) leaves no more cells that can be reclaimed, and therefore the first city wins. In the third example, there are no cells that can be reclaimed. | Java 7 | standard input | [] | d66c7774acb7e4b1f5da105dfa5c1d36 | The first line consists of two integers r and n (1 ≤ r ≤ 100, 0 ≤ n ≤ r). Then n lines follow, describing the cells that were already reclaimed. Each line consists of two integers: ri and ci (1 ≤ ri ≤ r, 1 ≤ ci ≤ 2), which represent the cell located at row ri and column ci. All of the lines describing the cells will be distinct, and the reclaimed cells will not violate the constraints above. | 2,100 | Output "WIN" if the city whose turn it is to choose a cell can guarantee that they will be the last to choose a cell. Otherwise print "LOSE". | standard output | |
PASSED | 92ab65366f1e96175b625cfbd2c92887 | train_001.jsonl | 1375549200 | In a far away land, there are two cities near a river. One day, the cities decide that they have too little space and would like to reclaim some of the river area into land.The river area can be represented by a grid with r rows and exactly two columns — each cell represents a rectangular area. The rows are numbered 1 through r from top to bottom, while the columns are numbered 1 and 2.Initially, all of the cells are occupied by the river. The plan is to turn some of those cells into land one by one, with the cities alternately choosing a cell to reclaim, and continuing until no more cells can be reclaimed.However, the river is also used as a major trade route. The cities need to make sure that ships will still be able to sail from one end of the river to the other. More formally, if a cell (r, c) has been reclaimed, it is not allowed to reclaim any of the cells (r - 1, 3 - c), (r, 3 - c), or (r + 1, 3 - c).The cities are not on friendly terms, and each city wants to be the last city to reclaim a cell (they don't care about how many cells they reclaim, just who reclaims a cell last). The cities have already reclaimed n cells. Your job is to determine which city will be the last to reclaim a cell, assuming both choose cells optimally from current moment. | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
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);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
boolean[][] alive;
int r;
int[][][][] grundyCache;
public void solve(int testNumber, InputReader in, PrintWriter out) {
r = in.nextInt();
int n = in.nextInt();
alive = new boolean[2][r + 2];
for (boolean[] x : alive) {
Arrays.fill(x, true);
x[0] = false;
x[r + 1] = false;
}
grundyCache = new int[2][2][r + 2][r + 2];
for (int[][][] x : grundyCache) for (int[][] y : x) for (int[] z : y) Arrays.fill(z, -1);
for (int i = 0; i < n; ++i) {
int kr = in.nextInt();
int kc = in.nextInt() - 1;
alive[kc][kr] = false;
for (int rr = kr - 1; rr <= kr + 1; ++rr)
alive[1 - kc][rr] = false;
}
int gg = getGrundy(0, 0, 0, r + 1);
if (gg == 0) {
out.println("LOSE");
} else {
out.println("WIN");
}
}
private int getGrundy(int extraTop, int extraBottom, int top, int bottom) {
if (top > bottom) {
return 0;
}
int cached = grundyCache[extraTop][extraBottom][top][bottom];
if (cached >= 0) return cached;
boolean[] seen = new boolean[300];
for (int r = top; r <= bottom; ++r)
for (int c = 0; c < 2; ++c) {
if (!alive[c][r]) continue;
if (r == top && c == extraTop) continue;
if (r == bottom && c == extraBottom) continue;
seen[getGrundy(extraTop, 1 - c, top, r - 1) ^ getGrundy(1 - c, extraBottom, r + 1, bottom)] = true;
}
for (int i = 0; i < seen.length; ++i) if (!seen[i]) {
cached = i;
break;
}
grundyCache[extraTop][extraBottom][top][bottom] = cached;
return cached;
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
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 | ["3 1\n1 1", "12 2\n4 1\n8 1", "1 1\n1 2"] | 2 seconds | ["WIN", "WIN", "LOSE"] | NoteIn the first example, there are 3 possible cells for the first city to reclaim: (2, 1), (3, 1), or (3, 2). The first two possibilities both lose, as they leave exactly one cell for the other city. However, reclaiming the cell at (3, 2) leaves no more cells that can be reclaimed, and therefore the first city wins. In the third example, there are no cells that can be reclaimed. | Java 7 | standard input | [] | d66c7774acb7e4b1f5da105dfa5c1d36 | The first line consists of two integers r and n (1 ≤ r ≤ 100, 0 ≤ n ≤ r). Then n lines follow, describing the cells that were already reclaimed. Each line consists of two integers: ri and ci (1 ≤ ri ≤ r, 1 ≤ ci ≤ 2), which represent the cell located at row ri and column ci. All of the lines describing the cells will be distinct, and the reclaimed cells will not violate the constraints above. | 2,100 | Output "WIN" if the city whose turn it is to choose a cell can guarantee that they will be the last to choose a cell. Otherwise print "LOSE". | standard output | |
PASSED | 9c066d2fe428105a3b43800d7402b6f6 | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes | import java.io.*;
import java.util.*;
/**
* @author �����
*
*/
public class C1 implements Runnable {
BufferedReader br;
StringTokenizer st;
PrintWriter out;
String nextToken() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
String nextStr() throws IOException {
return nextToken();
}
char nextChar() throws IOException {
return nextToken().charAt(0);
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public class FenwickTree {
public FenwickTree( int size ) {
n = size;
mas = new int[n];
Arrays.fill( mas, 0 );
}
int sum (int r) {
int result = 0;
for (; r >= 0; r = (r & (r+1)) - 1)
result += mas[r];
return result;
}
void inc (int i, int delta) {
for (; i < n; i = (i | (i+1)))
mas[i] += delta;
}
int sum (int l, int r) {
return sum (r) - sum (l-1);
}
int mas[];
int n;
}
public void solve() throws IOException {
int k = nextInt();
String s = nextStr();
int n = nextInt();
HashMap<Character, ArrayList<Integer> > deletes = new HashMap<Character, ArrayList<Integer> >();
for ( int i=0; i<s.length(); ++i ) {
char c = s.charAt(i);
if ( !deletes.containsKey(c) ) {
deletes.put(c, new ArrayList<Integer>());
}
}
for ( int i=0; i<n; ++i ) {
int j = nextInt();
char c = nextChar();
ArrayList<Integer> mas = deletes.get(c);
mas.add(j);
}
Set<Character> set = deletes.keySet();
HashMap<Character, Integer> accurs = new HashMap<Character, Integer>();
for ( char c : set ) {
accurs.put(c, 0);
ArrayList<Integer> mas = deletes.get(c);
if ( mas.size() > 0 ) {
int max = Collections.max(mas);
FenwickTree fen = new FenwickTree(s.length()*k+1);
ArrayList<Integer> newValues = new ArrayList<Integer>(mas.size());
for ( int i : mas ) {
int before = fen.sum(i);
while ( fen.sum(i+before) > before )
before += fen.sum(i+before) - before;
//++before;
newValues.add(i + before);
fen.inc(i+before, 1);
}
Collections.sort(newValues);
mas.clear();
mas.addAll(newValues);
}
}
StringBuilder answ = new StringBuilder();
for ( int i=0; i<k*s.length(); ++i ) {
char curChar = s.charAt(i%s.length());
int curAccur = accurs.get(curChar);
ArrayList<Integer> mas = deletes.get(curChar);
++curAccur;
accurs.put(curChar, curAccur);
int z = Collections.binarySearch(mas, curAccur);
if ( z < 0 )
answ.append(curChar);
}
out.println(answ.toString());
}
public void run() {
try {
//br = new BufferedReader(new FileReader("out.txt"));
//out = new PrintWriter(new FileWriter("c.txt"));
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
st = new StringTokenizer("");
solve();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
} finally {
out.close();
}
}
public static void main(String[] args) {
new Thread(new C1()).start();
}
}
| Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | a6fb9b54c6772274ffd8bb8f57d8a230 | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class C159 {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int k = Integer.parseInt(in.readLine());
String s = in.readLine();
ArrayList<Integer>[] oc = new ArrayList[26];
for (int i = 0 ; i < 26 ; i++)
oc[i] = new ArrayList<Integer>();
int len = s.length();
for (int i = 0 ; i < len ; i++) {
oc[s.charAt(i) - 'a'].add(i);
}
int[] inLine = new int[26];
for (int i = 0 ; i < 26 ; i++)
inLine[i] = oc[i].size();
for (int i = 1 ; i < k ; i++) {
for (int let = 0 ; let < 26 ; let++) {
for (int am = 0 ; am < inLine[let] ; am++)
oc[let].add(oc[let].get(am) + len * i);
}
}
int n = Integer.parseInt(in.readLine());
int[] index = new int[26];
for (int i = 0 ; i < n ; i++) {
StringTokenizer st = new StringTokenizer(in.readLine());
int ps = Integer.parseInt(st.nextToken()) - 1;
int c = st.nextToken().charAt(0) - 'a';
oc[c].remove(ps);
}
StringBuilder stb = new StringBuilder();
for (int i = 0 ; i < len * k - n ; i++) {
int minPs = 0;
while (index[minPs] >= oc[minPs].size()) minPs++;
for (int j = 1 ; j < 26 ; j++)
if (index[j] < oc[j].size() && oc[j].get(index[j]) < oc[minPs].get(index[minPs])) minPs = j;
index[minPs]++;
stb.append((char)(minPs + 'a'));
}
System.out.println(stb.toString());
}
}
| Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | 84bbaeeeb8911def3d5d5bcb900b575c | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class C {
static class Tmp{
}
public static void main(String[] args) {
Character ch = (char)17;
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
sc.nextLine();
String s = sc.nextLine();
ArrayList<Integer> arr[] = new ArrayList[65536];
for(int i = 0; i < arr.length; i++){
arr[i] = new ArrayList<Integer>();
}
StringBuilder sb = new StringBuilder(3000 * 100);
int v = 0;
for(int i = 0; i < k; i++){
char[] chA = s.toCharArray();
for(int j = 0; j < chA.length; j++){
sb.append(chA[j]);
arr[(int)chA[j]].add(v++);
}
}
int n = sc.nextInt();
sc.nextLine();
for(int i = 0; i < n; i++){
String[] in = sc.nextLine().split(" ");
int p = Integer.parseInt(in[0]);
char c = in[1].charAt(0);
sb.setCharAt(arr[(int)c].get(p - 1), ch);
arr[(int)c].remove(p - 1);
}
StringBuilder ans = new StringBuilder(3000 * 100);
for(int i = 0; i < sb.length(); i++){
if(sb.charAt(i) != ch){
ans.append(sb.charAt(i));
}
}
System.out.print(ans.toString());
}
}
| Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | e29e8b480ca2f6f2bda61ab07593dc3f | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes | import java.util.*;
import java.awt.*;
public class C
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int k = scan.nextInt();
String str = scan.next();
StringBuffer sb = new StringBuffer();
for(int i=0;i < k;i++)
sb.append(str);
if(sb.length() == 1)
{
System.out.println(sb);
return;
}
int bot = 1;
while(bot < sb.length())
bot *= 2;
bot /= 2;
int[][] l = new int[26][bot*2];
int[][] r = new int[26][bot*2];
//Build trees
for(int i=0;i < sb.length();i++)
{
int c = sb.charAt(i) - 'a';
if(i % 2 == 0) l[c][bot+i/2] = 1;
else r[c][bot+i/2] = 1;
}
for(int i=bot-1;i > 0;i--)
for(int c=0;c < 26;c++)
{
r[c][i] = l[c][2*i+1] + r[c][2*i+1];
l[c][i] = l[c][2*i] + r[c][2*i];
}
//DEBUG
/*
for(int i=0;i < bot*2;i++)
System.out.print(l[0][i] + " ");
System.out.println();*/
boolean[] b = new boolean[sb.length()];
//Mark deletions
k = scan.nextInt();
while(k --> 0)
{
int p = scan.nextInt();
int c = scan.next().charAt(0) - 'a';
int idx = 1;
while(idx < bot*2)
{
if(p <= l[c][idx]) idx = 2*idx;
else
{
p -= l[c][idx];
idx = 2*idx+1;
}
}
b[idx - 2*bot] = true;
boolean left = idx % 2 == 0;
while(idx > 1)
{
idx /= 2;
if(left)
l[c][idx]--;
else
r[c][idx]--;
left = idx % 2 == 0;
}
}
//Put it all back together again
StringBuffer sb2 = new StringBuffer();
for(int i=0;i < b.length;i++)
if(!b[i]) sb2.append(sb.charAt(i));
System.out.println(sb2);
}
} | Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | eab3539bccccddcd2a2629304e7c24ce | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) {
new C().run();
}
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public void solve() throws IOException {
int k = nextInt();
String s = nextToken();
int n = s.length();
int[][] a = new int[k][26];
for (int i = 0; i < n; i++) {
a[0][s.charAt(i) - 'a']++;
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < 26; j++) {
a[i][j] = a[0][j];
}
}
boolean[] fl = new boolean[k * n];
int t = nextInt();
for (int i = 0; i < t; i++) {
int z = nextInt();
char c = nextToken().charAt(0);
int j = 0;
while (z > a[j][c - 'a']) {
z -= a[j][c - 'a'];
j++;
}
a[j][c - 'a']--;
for (int l = j * n; l < j * n + n; l++) {
if (fl[l])
continue;
if (s.charAt(l % n) != c)
continue;
if (z == 1) {
fl[l] = true;
break;
} else
z--;
}
}
for (int i = 0; i < k * n; i++) {
if (!fl[i])
out.print(s.charAt(i % n));
}
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
//br = new BufferedReader(new FileReader("C.in"));
// out = new PrintWriter("C.out");
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
} | Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | 778ed3b100ab80d152faa839c95af41b | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes |
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class C {
int INF = 1 << 28;
String name;
void run() {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
name = sc.next();
int n = sc.nextInt();
Operation[] op = new Operation[n];
int[] alf = new int[26];
TreeSet<Change> changes = new TreeSet<Change>();
for(int i=0;i<name.length();i++) alf[name.charAt(i)-'a']++;
for(int i=0;i<n;i++) {
op[i] = new Operation(sc.nextInt(), sc.next().charAt(0));
}
for(int i=0;i<n;i++) {
int kth = (op[i].p-1)/alf[op[i].c-'a'];
int p = op[i].p - kth * alf[op[i].c - 'a'];
boolean flg = false;
for( Change change: changes ) {
if( kth < change.k ) break;
else if( kth > change.k ) {
p += change.alf[op[i].c-'a'];
if( p > alf[op[i].c-'a']) {
p -= alf[op[i].c-'a'];
kth++;
}
}
else {
if( change.alf[op[i].c-'a'] + p > alf[op[i].c-'a'] ) {
kth++;
p = change.alf[op[i].c-'a'] + p - alf[op[i].c-'a'];
continue;
}
change.deleate(p, op[i].c);
flg = true;
}
}
// System.out.println(kth + " " + p);
if(flg) continue;
else {
Change change = new Change(kth);
change.deleate(p, op[i].c);
changes.add(change);
}
}
String ans = "";
int kth = 0;
for(Change change: changes) {
for(;kth<change.k;kth++) System.out.print(name);
System.out.print(change.generateName());
kth++;
}
for(;kth<k;kth++) System.out.print(name);
System.out.println();
}
public static void main(String[] args) {
new C().run();
}
class Change implements Comparable<Change> {
int k;
int[] alf;
String ret;
public Change(int k) {
this.k = k;
alf = new int[26];
ret = name;
// System.out.println(ret);
}
void deleate(int p, char c) {
alf[c-'a']++;
int cnt = 0;
for(int i=0;i<ret.length();i++) {
if(ret.charAt(i) == c) cnt++;
if(p == cnt) {
ret = ret.substring(0, i) + ret.substring(i+1);
break;
}
}
// System.out.println(ret);
}
String generateName() {
return ret;
}
@Override
public int compareTo(Change o) {
// TODO 自動生成されたメソッド・スタブ
return k-o.k;
}
}
class Operation {
int p;
char c;
Operation (int p, char c) {
this.p = p;
this.c = c;
}
}
}
| Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | 0b33932cec74ea48a693e4cc2f486b8f | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes | import java.util.*;
public class c {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int K = in.nextInt();
String S = in.next();
myString[] string = new myString[K];
for(int i = 0; i < K; i++)
string[i] = new myString(S);
int N = in.nextInt();
for(int i = 0; i < N; i++) {
int p = in.nextInt();
char c = in.next().charAt(0);
int count = 0;
for(int t = 0; t < K; t++) {
if(count + string[t].freq[c-'a'] < p) {
count += string[t].freq[c-'a'];
continue;
}
string[t].freq[c-'a']--;
int tmp = 0;
for(int k = 0; k < string[t].sb.length(); k++) {
if(string[t].sb.charAt(k) == c)
tmp++;
if(tmp == p - count) {
string[t].sb = string[t].sb.deleteCharAt(k);
break;
}
}
break;
}
}
for(myString ms : string) {
System.out.printf(ms.sb.toString());
}
}
public static class myString {
StringBuilder sb;
int freq[];
public myString(String s) {
sb = new StringBuilder(s);
freq = new int[26];
for(int i = 0; i < s.length(); i++)
freq[s.charAt(i)-'a']++;
}
}
}
| Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | b60e9bf75cd2d9834b49fb459d2b1b4a | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes | import java.io.*;
import java.util.*;
public class TaskC {
BufferedReader br;
PrintWriter out;
StringTokenizer stok;
String nextToken() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
String s = br.readLine();
if (s == null) {
return null;
}
stok = new StringTokenizer(s);
}
return stok.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());
}
char nextChar() throws IOException {
return (char) (br.read());
}
String nextLine() throws IOException {
return br.readLine();
}
class Node {
int x, y, num;
Node l, r;
Node(int x, int y) {
this.x = x;
this.y = y;
l = null;
r = null;
num = 1;
}
}
int getnum(Node p) {
if (p == null) {
return 0;
}
return p.num;
}
Node[] split(Node t, long key) {
if (t == null) {
return new Node[] { null, null };
}
Node[] help;
if (key <= t.x) {
help = split(t.l, key);
t.l = help[1];
help[1] = t;
} else {
help = split(t.r, key);
t.r = help[0];
help[0] = t;
}
if (help[0] != null) {
help[0].num = getnum(help[0].l) + getnum(help[0].r) + 1;
}
if (help[1] != null) {
help[1].num = getnum(help[1].l) + getnum(help[1].r) + 1;
}
return help;
}
Node merge(Node l, Node r) {
if (l == null) {
return r;
}
if (r == null) {
return l;
}
if (l.y < r.y) {
l.r = merge(l.r, r);
if (l != null) {
l.num = getnum(l.l) + getnum(l.r) + 1;
}
return l;
} else {
r.l = merge(l, r.l);
if (r != null) {
r.num = getnum(r.l) + getnum(r.r) + 1;
}
return r;
}
}
Random y = new Random();
int rand() {
return Math.abs(y.nextInt());
}
Node add(Node root, int x) {
Node[] help = split(root, x);
root = merge(help[0], new Node(x, rand()));
root = merge(root, help[1]);
return root;
}
char[] ans;
void fillAns(Node root, int num) {
if (root == null) {
return;
}
fillAns(root.l, num);
ans[root.x] = (char) ((int) ('a' + num));
fillAns(root.r, num);
}
int getK(Node root, int k) {
if (root == null) {
out.println("aaabbbxyuxyu");
out.close();
System.exit(0);
}
while (true) {
int ll = 0;
if (root.l != null) {
ll = root.l.num;
}
if (ll + 1 == k) {
return root.x;
}
if (ll >= k) {
root = root.l;
} else {
k -= (ll + 1);
root = root.r;
}
}
}
Node remove(Node root, int x) {
Node[] help = split(root, x);
Node[] help1 = split(help[1], x + 1);
root = merge(help[0], help1[1]);
return root;
}
void solve() throws IOException {
Node[] roots = new Node[26];
int k = nextInt();
String s = nextToken();
int n = nextInt();
ans = new char[s.length() * k];
for (int i = 0; i < s.length() * k; i++) {
ans[i] = '@';
}
for (int i = 0; i < 26; i++) {
roots[i] = null;
}
for (int i = 0; i < s.length(); i++) {
int num = s.charAt(i) - 'a';
for (int j = 0; j < k; j++) {
roots[num] = add(roots[num], i + j * s.length());
}
}
for (int i = 0; i < n; i++) {
int kk = nextInt();
String ss = nextToken();
int num = (int) (ss.charAt(0) - 'a');
roots[num] = remove(roots[num], getK(roots[num], kk));
}
for (int i = 0; i < 26; i++) {
fillAns(roots[i], i);
}
for (int i = 0; i < s.length() * k; i++) {
if (ans[i] != '@') {
out.print(ans[i]);
}
}
}
void run() throws IOException {
// br = new BufferedReader(new FileReader("input.txt"));
// out = new PrintWriter("output.txt");
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
br.close();
out.close();
}
public static void main(String[] args) throws IOException {
// Locale.setDefault(Locale.US);
new TaskC().run();
}
} | Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | 2cf7cfc5f53ef5f49210ecf2c98d3314 | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.StringTokenizer;
public class Sol {
BufferedReader br;
StringTokenizer st;
PrintStream out;
String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String s = br.readLine();
if (s == null) {
return "-1";
}
st = new StringTokenizer(s);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
void run() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = System.out;
int k = nextInt();
String s = nextToken();
int n = nextInt();
int[] b = new int[26];
int[][] a = new int[26][n+1];
for ( int i = 0; i< n; i++){
int x = nextInt();
String ss = nextToken();
a[(int)(ss.charAt(0)-'a')][0]++;
a[(int)(ss.charAt(0)-'a')][a[(int)(ss.charAt(0)-'a')][0]]=x;
}
for (int q=0; q<26; q++){
for(int i = a[q][0]; i > 1; i--){
for(int j = 1; j < i; j++){
if (a[q][j]>a[q][j+1]){
int c=a[q][j];
a[q][j]=a[q][j+1];
a[q][j+1]=c-1;
}
}
}
}
int[] aa= new int[26];
for ( int i = 0; i<26; i++){
b[i]=1;
aa[i]=1;
}
for ( int i = 0; i < k; i++){
for ( int j=0; j<s.length(); j++){
if (b[(int)(s.charAt(j)-'a')]<=a[(int)(s.charAt(j)-'a')][0]){
if (a[(int)(s.charAt(j)-'a')][b[(int)(s.charAt(j)-'a')]]>aa[(int)(s.charAt(j)-'a')]){
out.print(s.charAt(j));
aa[(int)(s.charAt(j)-'a')]++;
} else {
b[(int)(s.charAt(j)-'a')]++;
}
} else {
out.print(s.charAt(j));
aa[(int)(s.charAt(j)-'a')]++;
}
}
}
br.close();
out.close();
}
public static void main(String[] args) throws IOException {
new Sol().run();
}
} | Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | 4fca0f2152e26efb1e779a9596760e09 | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n,m, len;
boolean a[][] = new boolean[2000][100];
int sw[][] = new int [2000][26];
for (int i=0; i<2000; i++)
{
for (int j=0; j<100; j++)
{
//sw[i][j]=0;
a[i][j]=true;
}
}
String str;
n = input.nextInt();
input.nextLine();
str = input.nextLine();
m = input.nextInt();
len=str.length();
int vh[][] = new int[26][101];
for (int i=0; i<26; i++)
for (int j=0; j<100; j++) vh[i][j]=0;
for (int i=0; i<str.length(); i++)
{
vh[((int)(str.charAt(i))-(int)('a'))][0]++;
vh[(((int)(str.charAt(i)))-((int)('a')))][vh[(((int)(str.charAt(i)))-((int)('a')))][0]]=i;
}
for (int i=0; i<2000; i++)
for (int j=0; j<26; j++)
sw[i][j]=vh[j][0];
char c;
int t;
int x=0,l=0,y=0;
for (int i=0; i<m; i++)
{
t = input.nextInt();
c = (char)input.next().charAt(0);
input.nextLine();
l=1;
c-='a';
y=vh[c][l];
x=0;
while (t>sw[x][c])
{
t-=sw[x][c];
x++;
}
while (t!=0)
{
if (a[x][y])
{
t--;
}
if (t!=0)
{
l++;
if (l==vh[c][0]+1)
{
l=1;
x++;
}
y=vh[c][l];
}
}
sw[x][c]--;
a[x][y]=false;
}
for (int i=0; i<n; i++)
{
for (int j=0; j<len;j++)
{
if (a[i][j])
{
System.out.print(str.charAt(j));
}
}
}
}
}
| Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | 8b67850bd62be8c68068dbb841d3a467 | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes | import java.awt.*;
import java.io.*;
import java.math.*;
import java.util.*;
import static java.lang.Math.*;
public class VKCup_Qual2_C implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok;
public static void main(String[] args) {
new Thread(null, new VKCup_Qual2_C(), "", 256 * (1L << 20)).start();
}
@Override
public void run() {
try {
long startTime = System.currentTimeMillis();
if (System.getProperty("ONLINE_JUDGE") != null) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
Locale.setDefault(Locale.US);
tok = new StringTokenizer("");
solve();
in.close();
out.close();
long endTime = System.currentTimeMillis();
System.err.println("Time = " + (endTime - startTime));
long freeMemory = Runtime.getRuntime().freeMemory();
long totalMemory = Runtime.getRuntime().totalMemory();
System.err.println("Memory = " + ((totalMemory - freeMemory) >> 10));
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
/** http://pastebin.com/j0xdUjDn */
static class Utils {
private Utils() {}
public static void mergeSort(int[] a) {
mergeSort(a, 0, a.length - 1);
}
private static final int MAGIC_VALUE = 50;
private static void mergeSort(int[] a, int leftIndex, int rightIndex) {
if (leftIndex < rightIndex) {
if (rightIndex - leftIndex <= MAGIC_VALUE) {
insertionSort(a, leftIndex, rightIndex);
} else {
int middleIndex = (leftIndex + rightIndex) / 2;
mergeSort(a, leftIndex, middleIndex);
mergeSort(a, middleIndex + 1, rightIndex);
merge(a, leftIndex, middleIndex, rightIndex);
}
}
}
private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) {
int length1 = middleIndex - leftIndex + 1;
int length2 = rightIndex - middleIndex;
int[] leftArray = new int[length1];
int[] rightArray = new int[length2];
System.arraycopy(a, leftIndex, leftArray, 0, length1);
System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
if (i == length1) {
a[k] = rightArray[j++];
} else if (j == length2) {
a[k] = leftArray[i++];
} else {
a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++];
}
}
}
private static void insertionSort(int[] a, int leftIndex, int rightIndex) {
for (int i = leftIndex + 1; i <= rightIndex; i++) {
int current = a[i];
int j = i - 1;
while (j >= leftIndex && a[j] > current) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = current;
}
}
}
// solution
void solve() throws IOException {
int k = readInt();
char[] s = readString().toCharArray();
int n = s.length;
int[][] countInBlocks = new int[26][k];
int[][][] blocks = new int[26][k][n];
for (int i = 0; i < n; i++) {
int c = s[i] - 'a';
for (int b = 0; b < k; b++) {
blocks[c][b][i]++;
countInBlocks[c][b]++;
}
}
int q = readInt();
for (int qq = 0; qq < q; qq++) {
int pos = readInt();
int c = readString().charAt(0) - 'a';
int curCount = 0;
for (int b = 0; b < k; b++) {
if (curCount + countInBlocks[c][b] >= pos) {
for (int i = 0; i < n; i++) {
curCount += blocks[c][b][i];
if (curCount == pos) {
blocks[c][b][i]--;
countInBlocks[c][b]--;
break;
}
}
break;
} else {
curCount += countInBlocks[c][b];
}
}
}
char[] ans = new char[k*n];
for (int b = 0; b < k; b++) {
for (int i = 0; i < n; i++) {
for (int c = 0; c < 26; c++) {
if (blocks[c][b][i] == 1) {
ans[b*n + i] = (char)(c + 'a');
}
}
}
}
for (char c : ans) {
if (c != 0) {
out.print(c);
}
}
}
} | Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | 848dc5cdc3deb1a53f8f437e29e041c3 | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes | import java.util.*;
public class Pass2{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
String pass = sc.next();
int n = sc.nextInt();
String[][] changes = new String[n][2];
for(int i = 0; i < n; i++) {
changes[i][0] = sc.next();
changes[i][1] = sc.next();
}
System.out.println(pass(k, pass, changes));
}
public static String pass(int k, String pass, String[][] chs) {
int[][] zone = new int[26][k];
for(int i = 0; i < pass.length(); i++)
for(int j = 0; j < k; j++)
zone[pass.charAt(i) - 'a'][j] += 1;
StringBuffer userBuf = new StringBuffer(pass);
for(int i = 1; i < k; i++) userBuf.append(pass);
for(int i = 0; i < chs.length; i++) {
char c = chs[i][1].charAt(0);
int m = Integer.parseInt(chs[i][0]), z = 0;
// element is m-th L -> R in zone z
while(m > zone[c - 'a'][z]) m -= zone[c - 'a'][z++];
int idx = z * pass.length(); // go to zone z
while(m > 0) {
while(userBuf.charAt(idx) != c) idx++;
m--; idx++;
} idx--; // go to element
zone[c - 'a'][z] -= 1; // track zone changes
userBuf.setCharAt(idx, '.');
}
StringBuffer result = new StringBuffer();
for(int i = 0; i < userBuf.length(); i++)
if(userBuf.charAt(i) != '.') result.append(userBuf.charAt(i));
return result.toString();
}
} | Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | 73b83e789278daf6ccf417cdea4fa3b6 | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class C {
final static int size = 200000;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int k = Integer.parseInt(br.readLine());
String s = br.readLine();
// initialize array
ArrayList<Integer>[] letters = new ArrayList[26];
for(int i = 0; i < letters.length; i++)
letters[i] = new ArrayList<Integer>(size);
// fill array with letters index
char c;
int len = s.length();
for(int m = 0; m < k; m++)
for(int i = 0; i < len; i++)
{
c = s.charAt(i);
letters[c-'a'].add(i+m*len);
}
// read input letters and remove from array
int n = Integer.parseInt(br.readLine()), index;
StringTokenizer st;
for(int i = 0; i < n; i++)
{
st = new StringTokenizer(br.readLine());
index = Integer.parseInt(st.nextToken())-1;
c = st.nextToken().charAt(0);
// remove letter
letters[c-'a'].remove(index);
}
// collect remaining letters in char array
char[] res = new char[k*len];
for(int i = 0; i < letters.length; i++)
for(int m : letters[i])
res[m] = (char)('a'+i);
// collect available letters to print
StringBuilder ans = new StringBuilder("");
for(int i = 0; i < res.length; i++)
if(res[i] >= 'a' && res[i] <= 'z')
ans.append(res[i]);
System.out.println(ans);
}
} | Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | 9decc484bb104a5a3c70896758c935f1 | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public void solve( ) throws Throwable {
int copies = in.nextInt( );
char ch[ ] = in.next( ).toCharArray( );
int len = ch.length;
int count[ ][ ] = new int[ copies ][ 128 ];
boolean ans[ ][ ] = new boolean[ copies ][ len ];
for ( boolean x[ ] : ans ) {
Arrays.fill( x, true );
}
for ( int i = 0; i < copies; ++i ) {
for ( int j = 0; j < len; ++j ) {
count[ i ][ ch[ j ] ]++;
}
}
int queries = in.nextInt( );
for ( int q = 0; q < queries; ++q ) {
int pos = in.nextInt( );
char c = in.next( ).charAt( 0 );
for ( int i = 0; i < copies; ++i ) {
if ( count[ i ][ c ] >= pos ) {
for ( int j = 0; j < len; ++j ) {
if ( c == ch[ j ] && ans[ i ][ j ] ) {
--pos;
if ( pos == 0 ) {
ans[ i ][ j ] = false;
//debug( i, j, ch[ j ], count[ i ][ c ] );
break;
}
}
}
count[ i ][ c ]--;
break;
} else {
pos -= count[ i ][ c ];
}
}
}
for ( int i = 0; i < copies; ++i ) {
for ( int j = 0; j < len; ++j ) {
if ( ans[ i ][ j ] ) {
out.print( ch[ j ] );
}
}
}
out.println( );
}
public void run( ) {
in = new FastScanner( System.in );
out = new PrintWriter( new PrintStream( System.out ), true );
try {
solve( );
out.close( );
System.exit( 0 );
} catch( Throwable e ) {
e.printStackTrace( );
System.exit( -1 );
}
}
public void debug( Object...os ) {
System.err.println( Arrays.deepToString( os ) );
}
public static void main( String[ ] args ) {
( new Main( ) ).run( );
}
private FastScanner in;
private PrintWriter out;
private static class FastScanner {
private int charsRead;
private int currentRead;
private byte buffer[ ] = new byte[ 0x1000 ];
private InputStream reader;
public FastScanner( InputStream in ) {
reader = in;
}
public int read( ) {
if ( charsRead == -1 ) {
throw new InputMismatchException( );
}
if ( currentRead >= charsRead ) {
currentRead = 0;
try {
charsRead = reader.read( buffer );
} catch( IOException e ) {
throw new InputMismatchException( );
}
if ( charsRead <= 0 ) {
return -1;
}
}
return buffer[ currentRead++ ];
}
public int nextInt( ) {
int c = read( );
while ( isWhitespace( c ) ) {
c = read( );
}
if ( c == -1 ) {
throw new NullPointerException( );
}
if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int sign = c == '-' ? -1 : 1;
if ( sign == -1 ) {
c = read( );
}
if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int ans = 0;
while ( !isWhitespace( c ) && c != -1 ) {
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10 + num;
c = read( );
}
return ans * sign;
}
public long nextLong( ) {
int c = read( );
while ( isWhitespace( c ) ) {
c = read( );
}
if ( c == -1 ) {
throw new NullPointerException( );
}
if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int sign = c == '-' ? -1 : 1;
if ( sign == -1 ) {
c = read( );
}
if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
long ans = 0;
while ( !isWhitespace( c ) && c != -1 ) {
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10 + num;
c = read( );
}
return ans * sign;
}
public boolean isWhitespace( int c ) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == -1;
}
public String next( ) {
int c = read( );
StringBuffer ans = new StringBuffer( );
while ( isWhitespace( c ) && c != -1 ) {
c = read( );
}
if ( c == -1 ) {
return null;
}
while ( !isWhitespace( c ) && c != -1 ) {
ans.appendCodePoint( c );
c = read( );
}
return ans.toString( );
}
public String nextLine( ) {
String ans = nextLine0( );
while ( ans.trim( ).length( ) == 0 ) {
ans = nextLine0( );
}
return ans;
}
private String nextLine0( ) {
int c = read( );
if ( c == -1 ) {
return null;
}
StringBuffer ans = new StringBuffer( );
while ( c != '\n' && c != '\r' && c != -1 ) {
ans.appendCodePoint( c );
c = read( );
}
return ans.toString( );
}
public double nextDouble( ) {
int c = read( );
while ( isWhitespace( c ) ) {
c = read( );
}
if ( c == -1 ) {
throw new NullPointerException( );
}
if ( c != '.' && c != '-' && !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int sign = c == '-' ? -1 : 1;
if ( c == '-' ) {
c = read( );
}
double ans = 0;
while ( c != -1 && c != '.' && !isWhitespace( c ) ) {
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10.0 + num;
c = read( );
}
if ( !isWhitespace( c ) && c != -1 && c != '.' ) {
throw new InputMismatchException( );
}
double pow10 = 1.0;
if ( c == '.' ) {
c = read( );
}
while ( !isWhitespace( c ) && c != -1 ) {
pow10 *= 10.0;
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10.0 + num;
c = read( );
}
return ans * sign / pow10;
}
}
}
| Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | 17ee29534cce2803ffd09d388b3fdf80 | train_001.jsonl | 1331280000 | One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import static java.lang.Math.*;
import static java.lang.Integer.*;
@SuppressWarnings("unused")
public class VKQual2C {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int K = parseInt(br.readLine());
String s = br.readLine();
int n = parseInt(br.readLine());
String[] use = null;
ArrayList<Integer>[] location = new ArrayList[26];
for (int i = 0; i < 26; ++i)
location[i] = new ArrayList<Integer>();
for (int i = 0; i < s.length(); ++i) {
location[s.charAt(i) - 'a'].add(i);
}
for(int i = 0 ; i < 26 ; ++i){
if(location[i].size() == 0)
continue;
int sz = location[i].size();
for(int j = 0 ; j < K - 1 ; ++j){
for(int k = 0 ; k < sz ; ++k)
location[i].add(location[i].get(k) + (s.length() * (j + 1)));
}
}
char [] ans = new char [K * s.length()];
int m = 0;
for(int i = 0 ; i < K ; ++i)
for(int j = 0 ; j < s.length() ; ++j)
ans[m++] = s.charAt(j);
for (int i = 0; i < n; ++i) {
use = br.readLine().split(" ");
int occ = parseInt(use[0]);
char toRemove = use[1].charAt(0);
int idx = location[toRemove - 'a'].remove(occ - 1);
ans[idx] = '#';
}
StringBuffer ret = new StringBuffer();
for(int i = 0 ; i < ans.length ; ++i)
if(ans[i] != '#')
ret = ret.append(ans[i]);
out.println(ret.toString());
out.flush();
out.close();
}
}
| Java | ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"] | 3 seconds | ["acb", "baa"] | NoteLet's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb". | Java 6 | standard input | [
"*special",
"data structures",
"binary search",
"brute force",
"strings"
] | adbfdb0d2fd9e4eb48a27d43f401f0e0 | The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1. | 1,400 | Print a single string — the user's final name after all changes are applied to it. | standard output | |
PASSED | 8b59610a57afbbf8e216606820fc1b5f | train_001.jsonl | 1288440000 | The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are n blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that is, they may not have the same length (it is even possible for them to have different lengths)Every stripe can be cut into two or more parts. The cuttings are made perpendicularly to the side along which the length is measured. Thus the cuttings do not change the width of a stripe but each of the resulting pieces has a lesser length (the sum of which is equal to the length of the initial stripe)After all the cuttings the blinds are constructed through consecutive joining of several parts, similar in length, along sides, along which length is measured. Also, apart from the resulting pieces an initial stripe can be used as a blind if it hasn't been cut. It is forbidden to construct blinds in any other way.Thus, if the blinds consist of k pieces each d in length, then they are of form of a rectangle of k × d bourlemeters. Your task is to find for what window possessing the largest possible area the blinds can be made from the given stripes if on technical grounds it is forbidden to use pieces shorter than l bourlemeter. The window is of form of a rectangle with side lengths as positive integers. | 256 megabytes | import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;
/**
* 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);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int l = in.nextInt();
final int MAX = 101;
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
int res = 0;
for (int i = l; i < MAX; ++i) {
int cur = 0;
for (int j = 0; j < n; j++) {
cur += a[j] / i;
}
res = Math.max(res, cur * i);
}
out.println(res);
}
}
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 | ["4 2\n1 2 3 4", "5 3\n5 5 7 3 1", "2 3\n1 2"] | 2 seconds | ["8", "15", "0"] | NoteIn the first sample test the required window is 2 × 4 in size and the blinds for it consist of 4 parts, each 2 bourlemeters long. One of the parts is the initial stripe with the length of 2, the other one is a part of a cut stripe with the length of 3 and the two remaining stripes are parts of a stripe with the length of 4 cut in halves. | Java 7 | standard input | [
"brute force"
] | 991516fa6f3ed5a71c547a3a50ea1a2b | The first output line contains two space-separated integers n and l (1 ≤ n, l ≤ 100). They are the number of stripes in the warehouse and the minimal acceptable length of a blind stripe in bourlemeters. The second line contains space-separated n integers ai. They are the lengths of initial stripes in bourlemeters (1 ≤ ai ≤ 100). | 1,400 | Print the single number — the maximal area of the window in square bourlemeters that can be completely covered. If no window with a positive area that can be covered completely without breaking any of the given rules exist, then print the single number 0. | standard output | |
PASSED | de7a67bf00b4325c970b375190318684 | train_001.jsonl | 1288440000 | The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are n blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that is, they may not have the same length (it is even possible for them to have different lengths)Every stripe can be cut into two or more parts. The cuttings are made perpendicularly to the side along which the length is measured. Thus the cuttings do not change the width of a stripe but each of the resulting pieces has a lesser length (the sum of which is equal to the length of the initial stripe)After all the cuttings the blinds are constructed through consecutive joining of several parts, similar in length, along sides, along which length is measured. Also, apart from the resulting pieces an initial stripe can be used as a blind if it hasn't been cut. It is forbidden to construct blinds in any other way.Thus, if the blinds consist of k pieces each d in length, then they are of form of a rectangle of k × d bourlemeters. Your task is to find for what window possessing the largest possible area the blinds can be made from the given stripes if on technical grounds it is forbidden to use pieces shorter than l bourlemeter. The window is of form of a rectangle with side lengths as positive integers. | 256 megabytes | import java.io.*;
import java.util.*;
public class j
{
public static void main(String a[])throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int n=0,j=0,m=0,i=0;
int d[]=new int[101];
d[0]=0;
String s;
s=b.readLine();
StringTokenizer c=new StringTokenizer(s);
m=Integer.parseInt(c.nextToken());
n=Integer.parseInt(c.nextToken());
int e[]=new int[m];
s=b.readLine();
StringTokenizer z=new StringTokenizer(s);
for(i=0;i<m;i++)
e[i]=Integer.parseInt(z.nextToken());
for(i=n;i<101;i++)
for(j=0;j<m;j++)
d[i]+=e[j]/i;
for(i=0;i<101;i++)
d[i]=d[i]*i;
Arrays.sort(d);
System.out.print(d[100]);
}
} | Java | ["4 2\n1 2 3 4", "5 3\n5 5 7 3 1", "2 3\n1 2"] | 2 seconds | ["8", "15", "0"] | NoteIn the first sample test the required window is 2 × 4 in size and the blinds for it consist of 4 parts, each 2 bourlemeters long. One of the parts is the initial stripe with the length of 2, the other one is a part of a cut stripe with the length of 3 and the two remaining stripes are parts of a stripe with the length of 4 cut in halves. | Java 7 | standard input | [
"brute force"
] | 991516fa6f3ed5a71c547a3a50ea1a2b | The first output line contains two space-separated integers n and l (1 ≤ n, l ≤ 100). They are the number of stripes in the warehouse and the minimal acceptable length of a blind stripe in bourlemeters. The second line contains space-separated n integers ai. They are the lengths of initial stripes in bourlemeters (1 ≤ ai ≤ 100). | 1,400 | Print the single number — the maximal area of the window in square bourlemeters that can be completely covered. If no window with a positive area that can be covered completely without breaking any of the given rules exist, then print the single number 0. | standard output | |
PASSED | 4e4ddcc5c26cf8b7157bab2526f5d956 | train_001.jsonl | 1288440000 | The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are n blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that is, they may not have the same length (it is even possible for them to have different lengths)Every stripe can be cut into two or more parts. The cuttings are made perpendicularly to the side along which the length is measured. Thus the cuttings do not change the width of a stripe but each of the resulting pieces has a lesser length (the sum of which is equal to the length of the initial stripe)After all the cuttings the blinds are constructed through consecutive joining of several parts, similar in length, along sides, along which length is measured. Also, apart from the resulting pieces an initial stripe can be used as a blind if it hasn't been cut. It is forbidden to construct blinds in any other way.Thus, if the blinds consist of k pieces each d in length, then they are of form of a rectangle of k × d bourlemeters. Your task is to find for what window possessing the largest possible area the blinds can be made from the given stripes if on technical grounds it is forbidden to use pieces shorter than l bourlemeter. The window is of form of a rectangle with side lengths as positive integers. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main implements Runnable {
public void solve() throws IOException {
int N = nextInt(); int L = nextInt();
int[] a = new int[N];
for(int i = 0; i < N; i++) a[i] = nextInt();
int answer = 0;
for(int i = L; i <= 100; i++){
int countOther = 0;
for(int j = 0; j < N; j++)
countOther += a[j] / i;
answer = Math.max(answer, i * countOther);
}
out.println(answer);
}
//-----------------------------------------------------------
public static void main(String[] args) {
new Main().run();
}
public void debug(Object... arr){
System.out.println(Arrays.deepToString(arr));
}
public void print1Int(int[] a){
for(int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
public void print2Int(int[][] a){
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[0].length; j++){
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
tok = null;
solve();
in.close();
out.close();
} catch (IOException e) {
System.exit(0);
}
}
public String nextToken() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
PrintWriter out;
BufferedReader in;
StringTokenizer tok;
} | Java | ["4 2\n1 2 3 4", "5 3\n5 5 7 3 1", "2 3\n1 2"] | 2 seconds | ["8", "15", "0"] | NoteIn the first sample test the required window is 2 × 4 in size and the blinds for it consist of 4 parts, each 2 bourlemeters long. One of the parts is the initial stripe with the length of 2, the other one is a part of a cut stripe with the length of 3 and the two remaining stripes are parts of a stripe with the length of 4 cut in halves. | Java 7 | standard input | [
"brute force"
] | 991516fa6f3ed5a71c547a3a50ea1a2b | The first output line contains two space-separated integers n and l (1 ≤ n, l ≤ 100). They are the number of stripes in the warehouse and the minimal acceptable length of a blind stripe in bourlemeters. The second line contains space-separated n integers ai. They are the lengths of initial stripes in bourlemeters (1 ≤ ai ≤ 100). | 1,400 | Print the single number — the maximal area of the window in square bourlemeters that can be completely covered. If no window with a positive area that can be covered completely without breaking any of the given rules exist, then print the single number 0. | standard output | |
PASSED | d12d8478f76e1228f7ff25fd46170b85 | train_001.jsonl | 1288440000 | The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are n blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that is, they may not have the same length (it is even possible for them to have different lengths)Every stripe can be cut into two or more parts. The cuttings are made perpendicularly to the side along which the length is measured. Thus the cuttings do not change the width of a stripe but each of the resulting pieces has a lesser length (the sum of which is equal to the length of the initial stripe)After all the cuttings the blinds are constructed through consecutive joining of several parts, similar in length, along sides, along which length is measured. Also, apart from the resulting pieces an initial stripe can be used as a blind if it hasn't been cut. It is forbidden to construct blinds in any other way.Thus, if the blinds consist of k pieces each d in length, then they are of form of a rectangle of k × d bourlemeters. Your task is to find for what window possessing the largest possible area the blinds can be made from the given stripes if on technical grounds it is forbidden to use pieces shorter than l bourlemeter. The window is of form of a rectangle with side lengths as positive integers. | 256 megabytes | import java.io.IOException;
import java.io.InputStreamReader;
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
* @author Zyflair Griffane
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
PandaScanner in = new PandaScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
C solver = new C();
solver.solve(1, in, out);
out.close();
}
}
class C {
public void solve(int testNumber, PandaScanner in, PrintWriter out) {
int n = in.nextInt();
int l = in.nextInt();
int[] arr = new int[n];
int res = 0;
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
for (int i = l; i < 101; i++) {
int curr = 0;
for (int j: arr) {
curr += (j / i) * i;
}
res = Math.max(res, curr);
}
out.println(res);
}
}
class PandaScanner {
public BufferedReader br;
public StringTokenizer st;
public InputStream in;
public PandaScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(this.in = in));
}
public String nextLine() {
try {
return br.readLine();
}
catch (Exception e) {
return null;
}
}
public String next() {
if (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(nextLine().trim());
return next();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
| Java | ["4 2\n1 2 3 4", "5 3\n5 5 7 3 1", "2 3\n1 2"] | 2 seconds | ["8", "15", "0"] | NoteIn the first sample test the required window is 2 × 4 in size and the blinds for it consist of 4 parts, each 2 bourlemeters long. One of the parts is the initial stripe with the length of 2, the other one is a part of a cut stripe with the length of 3 and the two remaining stripes are parts of a stripe with the length of 4 cut in halves. | Java 7 | standard input | [
"brute force"
] | 991516fa6f3ed5a71c547a3a50ea1a2b | The first output line contains two space-separated integers n and l (1 ≤ n, l ≤ 100). They are the number of stripes in the warehouse and the minimal acceptable length of a blind stripe in bourlemeters. The second line contains space-separated n integers ai. They are the lengths of initial stripes in bourlemeters (1 ≤ ai ≤ 100). | 1,400 | Print the single number — the maximal area of the window in square bourlemeters that can be completely covered. If no window with a positive area that can be covered completely without breaking any of the given rules exist, then print the single number 0. | standard output | |
PASSED | 35d71417d85d236afe30abf976fb8207 | train_001.jsonl | 1288440000 | The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are n blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that is, they may not have the same length (it is even possible for them to have different lengths)Every stripe can be cut into two or more parts. The cuttings are made perpendicularly to the side along which the length is measured. Thus the cuttings do not change the width of a stripe but each of the resulting pieces has a lesser length (the sum of which is equal to the length of the initial stripe)After all the cuttings the blinds are constructed through consecutive joining of several parts, similar in length, along sides, along which length is measured. Also, apart from the resulting pieces an initial stripe can be used as a blind if it hasn't been cut. It is forbidden to construct blinds in any other way.Thus, if the blinds consist of k pieces each d in length, then they are of form of a rectangle of k × d bourlemeters. Your task is to find for what window possessing the largest possible area the blinds can be made from the given stripes if on technical grounds it is forbidden to use pieces shorter than l bourlemeter. The window is of form of a rectangle with side lengths as positive integers. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.StringTokenizer;
public class CodeE
{
static class Scanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String nextLine()
{
try
{
return br.readLine();
}
catch(Exception e)
{
throw(new RuntimeException());
}
}
public String next()
{
while(!st.hasMoreTokens())
{
String l = nextLine();
if(l == null)
return null;
st = new StringTokenizer(l);
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
public int[] nextIntArray(int n)
{
int[] res = new int[n];
for(int i = 0; i < res.length; i++)
res[i] = nextInt();
return res;
}
public long[] nextLongArray(int n)
{
long[] res = new long[n];
for(int i = 0; i < res.length; i++)
res[i] = nextLong();
return res;
}
public double[] nextDoubleArray(int n)
{
double[] res = new double[n];
for(int i = 0; i < res.length; i++)
res[i] = nextDouble();
return res;
}
public void sortIntArray(int[] array)
{
Integer[] vals = new Integer[array.length];
for(int i = 0; i < array.length; i++)
vals[i] = array[i];
Arrays.sort(vals);
for(int i = 0; i < array.length; i++)
array[i] = vals[i];
}
public void sortLongArray(long[] array)
{
Long[] vals = new Long[array.length];
for(int i = 0; i < array.length; i++)
vals[i] = array[i];
Arrays.sort(vals);
for(int i = 0; i < array.length; i++)
array[i] = vals[i];
}
public void sortDoubleArray(double[] array)
{
Double[] vals = new Double[array.length];
for(int i = 0; i < array.length; i++)
vals[i] = array[i];
Arrays.sort(vals);
for(int i = 0; i < array.length; i++)
array[i] = vals[i];
}
public String[] nextStringArray(int n)
{
String[] vals = new String[n];
for(int i = 0; i < n; i++)
vals[i] = next();
return vals;
}
public Integer nextInteger()
{
String s = next();
if(s == null)
return null;
return Integer.parseInt(s);
}
public int[][] nextIntMatrix(int n, int m)
{
int[][] ans = new int[n][];
for(int i = 0; i < n; i++)
ans[i] = nextIntArray(m);
return ans;
}
public char[][] nextGrid(int r)
{
char[][] grid = new char[r][];
for(int i = 0; i < r; i++)
grid[i] = next().toCharArray();
return grid;
}
public static <T> T fill(T arreglo, int val)
{
if(arreglo instanceof Object[])
{
Object[] a = (Object[]) arreglo;
for(Object x : a)
fill(x, val);
}
else if(arreglo instanceof int[])
Arrays.fill((int[]) arreglo, val);
else if(arreglo instanceof double[])
Arrays.fill((double[]) arreglo, val);
else if(arreglo instanceof long[])
Arrays.fill((long[]) arreglo, val);
return arreglo;
}
<T> T[] nextObjectArray(Class <T> clazz, int size)
{
@SuppressWarnings("unchecked")
T[] result = (T[]) java.lang.reflect.Array.newInstance(clazz, size);
for(int c = 0; c < 3; c++)
{
Constructor <T> constructor;
try
{
if(c == 0)
constructor = clazz.getDeclaredConstructor(Scanner.class, Integer.TYPE);
else if(c == 1)
constructor = clazz.getDeclaredConstructor(Scanner.class);
else
constructor = clazz.getDeclaredConstructor();
}
catch(Exception e)
{
continue;
}
try
{
for(int i = 0; i < result.length; i++)
{
if(c == 0)
result[i] = constructor.newInstance(this, i);
else if(c == 1)
result[i] = constructor.newInstance(this);
else
result[i] = constructor.newInstance();
}
}
catch(Exception e)
{
throw new RuntimeException(e);
}
return result;
}
throw new RuntimeException("Constructor not found");
}
public void printLine(int... vals)
{
if(vals.length == 0)
System.out.println();
else
{
System.out.print(vals[0]);
for(int i = 1; i < vals.length; i++)
System.out.print(" ".concat(String.valueOf(vals[i])));
System.out.println();
}
}
public void printLine(long... vals)
{
if(vals.length == 0)
System.out.println();
else
{
System.out.print(vals[0]);
for(int i = 1; i < vals.length; i++)
System.out.print(" ".concat(String.valueOf(vals[i])));
System.out.println();
}
}
public void printLine(double... vals)
{
if(vals.length == 0)
System.out.println();
else
{
System.out.print(vals[0]);
for(int i = 1; i < vals.length; i++)
System.out.print(" ".concat(String.valueOf(vals[i])));
System.out.println();
}
}
public void printLine(int prec, double... vals)
{
if(vals.length == 0)
System.out.println();
else
{
System.out.printf("%." + prec + "f", vals[0]);
for(int i = 1; i < vals.length; i++)
System.out.printf(" %." + prec + "f", vals[i]);
System.out.println();
}
}
public Collection <Integer> wrap(int[] as)
{
ArrayList <Integer> ans = new ArrayList <Integer> ();
for(int i : as)
ans.add(i);
return ans;
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner();
int n = sc.nextInt();
int l = sc.nextInt();
int[] vals = sc.nextIntArray(n);
int best = 0;
for(int i = l; i <= 100; i++)
{
int total = 0;
for(int v : vals)
total += v / i;
best = Math.max(best, total * i);
}
System.out.println(best);
}
} | Java | ["4 2\n1 2 3 4", "5 3\n5 5 7 3 1", "2 3\n1 2"] | 2 seconds | ["8", "15", "0"] | NoteIn the first sample test the required window is 2 × 4 in size and the blinds for it consist of 4 parts, each 2 bourlemeters long. One of the parts is the initial stripe with the length of 2, the other one is a part of a cut stripe with the length of 3 and the two remaining stripes are parts of a stripe with the length of 4 cut in halves. | Java 7 | standard input | [
"brute force"
] | 991516fa6f3ed5a71c547a3a50ea1a2b | The first output line contains two space-separated integers n and l (1 ≤ n, l ≤ 100). They are the number of stripes in the warehouse and the minimal acceptable length of a blind stripe in bourlemeters. The second line contains space-separated n integers ai. They are the lengths of initial stripes in bourlemeters (1 ≤ ai ≤ 100). | 1,400 | Print the single number — the maximal area of the window in square bourlemeters that can be completely covered. If no window with a positive area that can be covered completely without breaking any of the given rules exist, then print the single number 0. | standard output | |
PASSED | 2b2fe0445d0ac0ab3b25973385b128a3 | train_001.jsonl | 1288440000 | The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are n blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that is, they may not have the same length (it is even possible for them to have different lengths)Every stripe can be cut into two or more parts. The cuttings are made perpendicularly to the side along which the length is measured. Thus the cuttings do not change the width of a stripe but each of the resulting pieces has a lesser length (the sum of which is equal to the length of the initial stripe)After all the cuttings the blinds are constructed through consecutive joining of several parts, similar in length, along sides, along which length is measured. Also, apart from the resulting pieces an initial stripe can be used as a blind if it hasn't been cut. It is forbidden to construct blinds in any other way.Thus, if the blinds consist of k pieces each d in length, then they are of form of a rectangle of k × d bourlemeters. Your task is to find for what window possessing the largest possible area the blinds can be made from the given stripes if on technical grounds it is forbidden to use pieces shorter than l bourlemeter. The window is of form of a rectangle with side lengths as positive integers. | 256 megabytes |
import java.util.Scanner;
public class Some_pr {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int n, i, j, l, ans = 0, sum;
n = read.nextInt();
l = read.nextInt();
int[] a = new int[n];
for (i=0; i<n; i++)
a[i] = read.nextInt();
for (i=l; i<=100; i++) {
sum=0;
for (j=0; j<n; j++)
sum+=a[j]/i*i;
if (sum > ans) ans = sum;
}
System.out.println(ans);
}
}
| Java | ["4 2\n1 2 3 4", "5 3\n5 5 7 3 1", "2 3\n1 2"] | 2 seconds | ["8", "15", "0"] | NoteIn the first sample test the required window is 2 × 4 in size and the blinds for it consist of 4 parts, each 2 bourlemeters long. One of the parts is the initial stripe with the length of 2, the other one is a part of a cut stripe with the length of 3 and the two remaining stripes are parts of a stripe with the length of 4 cut in halves. | Java 7 | standard input | [
"brute force"
] | 991516fa6f3ed5a71c547a3a50ea1a2b | The first output line contains two space-separated integers n and l (1 ≤ n, l ≤ 100). They are the number of stripes in the warehouse and the minimal acceptable length of a blind stripe in bourlemeters. The second line contains space-separated n integers ai. They are the lengths of initial stripes in bourlemeters (1 ≤ ai ≤ 100). | 1,400 | Print the single number — the maximal area of the window in square bourlemeters that can be completely covered. If no window with a positive area that can be covered completely without breaking any of the given rules exist, then print the single number 0. | standard output | |
PASSED | 67b30d6562a328d0368e5a44fb3e0ad3 | train_001.jsonl | 1288440000 | The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are n blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that is, they may not have the same length (it is even possible for them to have different lengths)Every stripe can be cut into two or more parts. The cuttings are made perpendicularly to the side along which the length is measured. Thus the cuttings do not change the width of a stripe but each of the resulting pieces has a lesser length (the sum of which is equal to the length of the initial stripe)After all the cuttings the blinds are constructed through consecutive joining of several parts, similar in length, along sides, along which length is measured. Also, apart from the resulting pieces an initial stripe can be used as a blind if it hasn't been cut. It is forbidden to construct blinds in any other way.Thus, if the blinds consist of k pieces each d in length, then they are of form of a rectangle of k × d bourlemeters. Your task is to find for what window possessing the largest possible area the blinds can be made from the given stripes if on technical grounds it is forbidden to use pieces shorter than l bourlemeter. The window is of form of a rectangle with side lengths as positive integers. | 256 megabytes | import java.util.Scanner;
public class Task{
public static void main(String args[]){
Solver s = new Solver();
s.solve();
}
}
class Solver{
private Scanner sc = new Scanner(System.in);
public void solve(){
int n = sc.nextInt(),
l = sc.nextInt();
int a[] = new int[n];
for (int i=0;i<n;i++)
a[i] = sc.nextInt();
int ans =0;
for (int i=l;i<=100;i++){
int tans = 0;
for (int j=0;j<n;j++)
tans = tans + (int)a[j]/i;
tans = tans*i;
ans = Math.max(ans, tans);
}
System.out.print(ans);
}
} | Java | ["4 2\n1 2 3 4", "5 3\n5 5 7 3 1", "2 3\n1 2"] | 2 seconds | ["8", "15", "0"] | NoteIn the first sample test the required window is 2 × 4 in size and the blinds for it consist of 4 parts, each 2 bourlemeters long. One of the parts is the initial stripe with the length of 2, the other one is a part of a cut stripe with the length of 3 and the two remaining stripes are parts of a stripe with the length of 4 cut in halves. | Java 7 | standard input | [
"brute force"
] | 991516fa6f3ed5a71c547a3a50ea1a2b | The first output line contains two space-separated integers n and l (1 ≤ n, l ≤ 100). They are the number of stripes in the warehouse and the minimal acceptable length of a blind stripe in bourlemeters. The second line contains space-separated n integers ai. They are the lengths of initial stripes in bourlemeters (1 ≤ ai ≤ 100). | 1,400 | Print the single number — the maximal area of the window in square bourlemeters that can be completely covered. If no window with a positive area that can be covered completely without breaking any of the given rules exist, then print the single number 0. | standard output | |
PASSED | 4cb4ec1eb9bdff9f7a1a03cc5fca972d | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
public static void main(String args[]) throws IOException {
Scan input=new Scan();
int n=input.scanInt();
long arr[]=new long[n];
for(int i=0;i<n;i++) {
arr[i]=input.scanInt();
}
Arrays.sort(arr);
long total=0,cnt=0;
for(int i=0;i<n;i++) {
if(total<=arr[i]) {
total+=arr[i];
cnt++;
}
}
System.out.println(cnt);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 8655b90a8205b604d19c4a9450a75625 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(int arr[],int l1,int r1,int l2,int r2) {
int tmp[]=new int[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(long arr[],int l1,int r1,int l2,int r2) {
long tmp[]=new long[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void main(String args[]) throws IOException {
Scan input=new Scan();
int n=input.scanInt();
long arr[]=new long[n];
for(int i=0;i<n;i++) {
arr[i]=input.scanInt();
}
sort(arr,0,n-1);
long total=0,cnt=0;
for(int i=0;i<n;i++) {
if(total<=arr[i]) {
total+=arr[i];
cnt++;
}
}
System.out.println(cnt);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 6467eda7e3bd1dfb988120bf05449402 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
public class susie {
public static void main(String[] args) {
Scanner iangay = new Scanner(System.in);
int numPeople = iangay.nextInt();
int[] people = new int[numPeople];
for (int i = 0; i < numPeople; i++) { people[i] = iangay.nextInt(); }
Arrays.sort(people);
int totalWait = 0; int dissatisfied = 0;
for (int i = 0; i < numPeople; i++) {
if (people[i] < totalWait) { dissatisfied++; }
else { totalWait += people[i]; }
}
System.out.println(numPeople-dissatisfied);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 6e84cb5bd6f9ddb3346c58f8f6efe9a7 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 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.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author EigenFunk
*/
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);
DQueue solver = new DQueue();
solver.solve(1, in, out);
out.close();
}
static class DQueue {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.readInt();
int[] t = new int[n];
for (int i = 0; i < n; i++) {
t[i] = in.readInt();
}
Arrays.sort(t);
int ct = 0;
long sum = 0;
for (int i = 0; i < n; i++) {
if (sum <= t[i]) {
ct++;
sum = sum + t[i];
}
if (sum > Integer.MAX_VALUE) {
break;
}
}
out.println(ct);
}
}
static class InputReader extends InputStream {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
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 static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 28a5c2af67b1608f74610aff22070ce2 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import java.util.*;
import java.lang.*;
public class Main {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int happyPeople=0;
int [] timesOfPeople = new int[n];
for (int i=0;i<n;i++) {
timesOfPeople[i]=scanner.nextInt();
}
Arrays.sort(timesOfPeople);
int totalTime=0;
for (int i=0;i<n;i++){
if(timesOfPeople[i]>=totalTime){
totalTime+=timesOfPeople[i];
happyPeople++;
}
}
System.out.println(happyPeople);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | fda675192f8aa0bc4586746a79666fb2 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes |
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
int[] wait=new int[n];
for(int i=0;i<n;i++){
wait[i]=sc.nextInt();
}
Arrays.sort(wait);
int t=0;
int flag=0;
for (int i = 0; i < n; i++) {
if (wait[i] >= t) {
t += wait[i];
flag++;
}
}
System.out.println(flag);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 00a7fb0247b763c4ab2104bec2f240f0 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
public class queue
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
int b[] = new int[n];
int temp=0;
for(int i=0;i<n;i++)
a[i] = sc.nextInt();
Arrays.sort(a);
int count=1;
b[0] = a[0];
int j=1;
for(int i=1;i<n && j<n;i++)
{
b[i] = b[i-1]+a[j];
if(a[j] >= b[i-1])
{
j++;
count++;
}
else
{
i--;
j++;
}
}
System.out.print(count);
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 8de4c7a6ada898cf25a17f8b94ef2ac6 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Integer> l = new ArrayList<>();
for(int i=0;i<n;i++){
int a = sc.nextInt();
l.add(a);
}
Collections.sort(l);
int c=n;
long sum=0;
for(int i=0;i<n;i++){
if(l.get(i)<sum){
c--;
}
else{
sum=sum+l.get(i);
}
}
System.out.println(c);
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 103b3c4549ac0db055ea8094d39bed90 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.*;
public class Codeforces {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws IOException {
FastReader in = new FastReader();
OutputStream out = new BufferedOutputStream ( System.out );
int n = in.nextInt();
long a[] = new long[n+1];
for(int i = 1; i <= n;i++) {
a[i] = in.nextLong();
}
Arrays.sort(a);
int res = 0;
long sum = 0;
for(int i = 1; i <= n; i++) {
if(sum <= a[i]) {
res++;
sum += a[i];
}
}
out.write((res + "\n").getBytes());
out.flush();
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 2b54e9452696502bc648c40f8a9feb43 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
public class Practise {
static Scanner in = new Scanner(System.in);
static int[] array(int n) {
int[] a = new int[n];
for(int i = 0; i < n; i++) a[i] = in.nextInt();
return a;
}
public static void main(String[] args) {
int n = in.nextInt();
int[] a = array(n);
Arrays.sort(a);
int sum = 0;
int ans = n;
for(int i : a) {
if(sum > i) ans--;
else sum += i;
}
System.out.println(ans);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 4767b605e84dec0717968c0ca6017a12 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
public class Practise {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int n = in.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
Arrays.sort(a);
int sum = 0;
int ans = n;
for(int i = 0; i < n; i++) {
if(sum > a[i]) ans--;
else sum += a[i];
}
System.out.println(ans);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 250a5aa5a0b283afe83f95c9692f7555 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Solution545D {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; ++i)
{
list.add(scanner.nextInt());
}
Collections.sort(list);
int wait = 0;
int count = 0;
for (int i = 0; i < n; ++i)
{
if (wait > list.get(i)) continue;
wait += list.get(i);
count++;
}
System.out.println(count);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 064fe7fbaef676d41e58af78aed42ee7 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.String.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder out = new StringBuilder();
StringTokenizer tk;
int n = parseInt(in.readLine());
tk = new StringTokenizer(in.readLine());
long [] t = new long[n];
for(int i=0; i<n; i++)
t[i] = parseLong(tk.nextToken());
Arrays.sort(t);
long wait = 0;
int ans = 0;
for(int i=0; i<n; i++) {
if(wait <= t[i]) {
ans++;
wait += t[i];
}
}
System.out.println(ans);
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 31f974c2411e979dbc59793d13555905 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
public class queue
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
int b[] = new int[n];
int temp=0;
for(int i=0;i<n;i++)
a[i] = sc.nextInt();
Arrays.sort(a);
int count=1;
b[0] = a[0];
int j=1;
for(int i=1;i<n && j<n;i++)
{
b[i] = b[i-1]+a[j];
if(a[j] >= b[i-1])
{
j++;
count++;
}
else
{
i--;
j++;
}
}
System.out.print(count);
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 83f22d2b0ecf992a7404e3c912a79986 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
public class SolutionC {
public static void main(String args[]){
Scanner s1=new Scanner(System.in);
int n=s1.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=s1.nextInt();
}
Arrays.sort(arr);
long sum=0,count=0;
for(int i=0;i<n;i++){
int ext=arr[i];
if(sum<=ext){
sum+=ext;
count++;
}
}
System.out.println(count);
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | f4414680a3539d0361c439cfd912d65b | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
public static void process()throws IOException
{
int n=ni();
Integer arr[]=new Integer[n+1];
for(int i=1;i<=n;i++) arr[i]=ni();
arr[0]=0;
r_sort(arr,n+1);
int time=arr[1],res=1;
for(int i=2;i<=n;i++){
if(time<=arr[i]){
res++;
time+=arr[i];
}
else{
while(i<=n && arr[i]<time){
i++;
}
if(i<=n){
res++;
time+=arr[i];
}
}
}
//System.out.println(Arrays.toString(arr));
pn(res);
}
static FastReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
out = new PrintWriter(System.out);
sc=new FastReader();
long s = System.currentTimeMillis();
int t=1;
//t=ni();
while(t-->0)
process();
out.flush();
System.err.println(System.currentTimeMillis()-s+"ms");
}
static void pn(Object o){out.println(o);}
static void p(Object o){out.print(o);}
static int ni()throws IOException{return Integer.parseInt(sc.next());}
static long nl()throws IOException{return Long.parseLong(sc.next());}
static double nd()throws IOException{return Double.parseDouble(sc.next());}
static String nln()throws IOException{return sc.nextLine();}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
static long mod=(long)1e9+7l;
static<T> void r_sort(T arr[],int n){
Random r = new Random();
for (int i = n-1; i > 0; i--){
int j = r.nextInt(i+1);
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Arrays.sort(arr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
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();
}
String nextLine(){
String str = "";
try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }
return str;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | deeacb0d6f79609a4b9efe5e17c08ec3 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
public class cf545D {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long arr[]=new long[n];
for(int i=0;i<n;i++)
{arr[i]=sc.nextLong();}
Arrays.sort(arr);
long t=0,c=0;
for(int i=0;i<n;i++)
{if(t<=arr[i])
{c++;}
else
{arr[i]=0;}
t=t+arr[i];}
System.out.println(c);sc.close();
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 19fa853210cac6abf07bfb86f85af728 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Queue {
public static void main(String[]args){
Mainn.FastReader s = new Mainn.FastReader();
int n=s.nextInt();
Integer[]array=new Integer[n];
for(int i=0;i<n;i++)
array[i]=s.nextInt();
Arrays.sort(array);
long sum=0l;
int people=0;
for(int i=0;i<n;i++){
if(array[i]>=sum) {
people++;
sum += array[i];
}
}
System.out.println(people);
}
}
class Mainn
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 25fc1437a107b661bbaf8acd5e3bfd2c | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
// SHIVAM GUPTA :
//NSIT
//decoder_1671
//BEING PERFECTIONIST IS NOT AN OPTION
// ASCII = 48 + i ;
// SHIVAM GUPTA :
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
static PrintWriter out;
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
out=new PrintWriter(System.out);
}
String next(){
while(st==null || !st.hasMoreElements()){
try{
st= new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str=br.readLine();
}
catch(IOException e){
e.printStackTrace();
}
return str;
}
}
/////////////////////////////////////////////////////////////////////////////////////
public static int min(int a ,int b , int c, int d)
{
int[] arr = new int[4] ;
arr[0] = a;
arr[1] = b ;
arr[2] = c;
arr[3] = d;
Arrays.sort(arr) ;
return arr[0];
}
/////////////////////////////////////////////////////////////////////////////
public static int max(int a ,int b , int c, int d)
{
int[] arr = new int[4] ;
arr[0] = a;
arr[1] = b ;
arr[2] = c;
arr[3] = d;
Arrays.sort(arr) ;
return arr[3];
}
///////////////////////////////////////////////////////////////////////////////////
static int sieve = 1000000 ;
static boolean[] prime = new boolean[sieve + 1] ;
static int[] dp = new int[10000001] ;
public static void sieveOfEratosthenes()
{
// FALSE == prime
// TRUE == COMPOSITE
// FALSE== 1
for(int i=0;i< sieve + 1;i++)
prime[i] = false;
for(int p = 2; p*p <= sieve; p++)
{
if(prime[p] == false)
{
for(int i = p*p; i <= sieve; i += p)
prime[i] = true;
}
}
}
///////////////////////////////////////////////////////////////////////////////////
public static String reverse(String input)
{
String op = "" ;
for(int i = 0; i < input.length() ; i++ )
{
op = input.charAt(i)+ op ;
}
return op ;
}
///////////////////////////////////////////////////////////////////////////////////////
public static void sortD(int[] arr)
{
sort(arr ,0 , arr.length-1) ;
int i =0 ; int j = arr.length -1 ;
while( i < j)
{
int temp = arr[i] ;
arr[i] =arr[j] ;
arr[j] = temp ;
i++ ; j-- ;
}
return ;
}
///////////////////////////////////////////////////////////////////////////////////////
public static boolean sameParity(long a ,long b )
{
if(a%2 == b %2)
{
return true ;
}
else{
return false ;
}
}
///////////////////////////////////////////////////////////////////////////////////////
public static boolean sameParity(int a ,int b )
{
if(a%2 == b %2)
{
return true ;
}
else{
return false ;
}
}
////////////////////////////////////////////////////////////////////////////////////
public static boolean isPossibleTriangle(int a ,int b , int c)
{
if( a + b > c && c+b > a && a +c > b)
{
return true ;
}
return false ;
}
/////////////////////////////////////////////////////////////////////
public static int gcd(int a, int b )
{
if(b==0)return a ;
else return gcd(b,a%b) ;
}
////////////////////////////////////////////////////////////////////////////////////
public static int lcm(int a, int b ,int c , int d )
{
int temp = lcm(a,b , c) ;
int ans = lcm(temp ,d ) ;
return ans ;
}
//////////////////////////////////////////////////////////////////////////////////////
public static int lcm(int a, int b ,int c )
{
int temp = lcm(a,b) ;
int ans = lcm(temp ,c) ;
return ans ;
}
////////////////////////////////////////////////////////////////////////////////////////
public static int lcm(int a , int b )
{
int gc = gcd(a,b);
return (a*b)/gc ;
}
/////////////////////////////////////////////////////////////////////////////////////////////
static boolean isPrime(int n)
{
if(n==1)
{
return false ;
}
boolean ans = true ;
for(int i = 2; i*i <= n ;i++)
{
if(n% i ==0)
{
ans = false ;break ;
}
}
return ans ;
}
//////////////////////////////////////////////////////////////////////////////////////////
public static long nCr(int n,int k)
{
long ans=1;
k=k>n-k?n-k:k;
int j=1;
for(;j<=k;j++,n--)
{
if(n%j==0)
{
ans*=n/j;
}else
if(ans%j==0)
{
ans=ans/j*n;
}else
{
ans=(ans*n)/j;
}
}
return ans;
}
///////////////////////////////////////////////////////////////////////////////////////////
public static ArrayList<Integer> allFactors(int n)
{
ArrayList<Integer> list = new ArrayList<>() ;
for(int i = 1; i*i <= n ;i++)
{
if( n % i == 0)
{
if(i*i == n)
{
list.add(i) ;
}
else{
list.add(i) ;
list.add(n/i) ;
}
}
}
return list ;
}
/////////////////////////////////////////////////////////////////////////////
public static boolean isPowerOfTwo(int n)
{
if(n==0)
return false;
if(((n ) & (n-1)) == 0 ) return true ;
else return false ;
}
////////////////////////////////////////////////////////////////////////////////////
public static int countDigit(long n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
/////////////////////////////////////////////////////////////////////////////////////////
static final int MAXN = 100001;
static int spf[] = new int[MAXN];
static void sieve()
{
spf[1] = 1;
for (int i=2; i<MAXN; i++)
spf[i] = i;
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
if (spf[i] == i)
{
for (int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}
// The above code works well for n upto the order of 10^7.
// Beyond this we will face memory issues.
// Time Complexity: The precomputation for smallest prime factor is done in O(n log log n)
// using sieve.
// Where as in the calculation step we are dividing the number every time by
// the smallest prime number till it becomes 1.
// So, let’s consider a worst case in which every time the SPF is 2 .
// Therefore will have log n division steps.
// Hence, We can say that our Time Complexity will be O(log n) in worst case.
static Vector<Integer> getFactorization(int x)
{
Vector<Integer> ret = new Vector<>();
while (x != 1)
{
ret.add(spf[x]);
x = x / spf[x];
}
return ret;
}
public static void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
public static void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
/////////////////////////////////////////////////////////////////////////////////////////
public static int knapsack(int[] weight,int value[],int maxWeight){
int n= value.length ;
//dp[i] stores the profit with KnapSack capacity "i"
int []dp = new int[maxWeight+1];
//initially profit with 0 to W KnapSack capacity is 0
Arrays.fill(dp, 0);
// iterate through all items
for(int i=0; i < n; i++)
//traverse dp array from right to left
for(int j = maxWeight; j >= weight[i]; j--)
dp[j] = Math.max(dp[j] , value[i] + dp[j - weight[i]]);
/*above line finds out maximum of dp[j](excluding ith element value)
and val[i] + dp[j-wt[i]] (including ith element value and the
profit with "KnapSack capacity - ith element weight") */
return dp[maxWeight];
}
///////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
// to return max sum of any subarray in given array
public static long kadanesAlgorithm(long[] arr)
{
long[] dp = new long[arr.length] ;
dp[0] = arr[0] ;
long max = dp[0] ;
for(int i = 1; i < arr.length ; i++)
{
if(dp[i-1] > 0)
{
dp[i] = dp[i-1] + arr[i] ;
}
else{
dp[i] = arr[i] ;
}
if(dp[i] > max)max = dp[i] ;
}
return max ;
}
/////////////////////////////////////////////////////////////////////////////////////////////
public static long kadanesAlgorithm(int[] arr)
{
long[] dp = new long[arr.length] ;
dp[0] = arr[0] ;
long max = dp[0] ;
for(int i = 1; i < arr.length ; i++)
{
if(dp[i-1] > 0)
{
dp[i] = dp[i-1] + arr[i] ;
}
else{
dp[i] = arr[i] ;
}
if(dp[i] > max)max = dp[i] ;
}
return max ;
}
///////////////////////////////////////////////////////////////////////////////////////
// Arrays.sort(arr, new Comparator<Pair>() {
// @Override
// public int compare(Pair first, Pa second) {
// if (first.getAge() != second.getAge()) {
// return first.getAge() - second.getAge();
// }
// return first.getName().compareTo(second.getName());
// }
// });
/////////////////////////////////////////////////////////////////////////////////////////
public static long binarySerachGreater(int[] arr , int start , int end , int val)
{
// fing total no of elements strictly grater than val in sorted array arr
if(start > end)return 0 ; //Base case
int mid = (start + end)/2 ;
if(arr[mid] <=val)
{
return binarySerachGreater(arr,mid+1, end ,val) ;
}
else{
return binarySerachGreater(arr,start , mid -1,val) + end-mid+1 ;
}
}
//////////////////////////////////////////////////////////////////////////////////
public static long arraySum(int[] arr , int start , int end)
{
long ans = 0 ;
for(int i = start ; i <= end ; i++)ans += arr[i] ;
return ans ;
}
/////////////////////////////////////////////////////////////////////////////////
public static void solve()
{
FastReader scn = new FastReader() ;
ArrayList<Integer> lista = new ArrayList<>() ;
ArrayList<Integer> listb = new ArrayList<>() ;
HashMap<Integer,Integer> map = new HashMap<>() ;
//HashMap<Integer,Integer> map = new HashMap<>() ;
Set<Integer> set = new HashSet<>() ; //Set<Integer> sety = new HashSet<>() ;
int n = scn.nextInt() ;
int[] arr = new int[n] ;
for(int i = 0; i <n;i++)arr[i] = scn.nextInt() ;
sort(arr,0 , n-1) ;
long sum = arr[0] ;
int c1 =1;
int c2 = 0 ;
for(int i = 1; i < n ; i++)
{
int temp = arr[i] ;
if(sum <= temp)
{
c1++ ;sum += temp ;
}
else{
c2++ ;
}
}
out.println(c1) ;
out.flush() ;
}
public static void main (String[] args) throws java.lang.Exception
{
solve() ;
}
}
class Pair
{
int first ;
int second ;
@Override
public String toString() {
String ans = "" ;
ans += this.first ;
ans += " ";
ans += this.second ;
return ans ;
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | d5a06ba939b2b467a8a994aff5de21fc | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] time = new int[n];
int waitingTime = 0;
int happyPeople = 0;
for(int i=0; i<n; i++){
time[i] = sc.nextInt();
}
Arrays.sort(time);
for(int i=0; i<n; i++){
if(waitingTime<=time[i]){
happyPeople++;
waitingTime += time[i];
}
}
System.out.println(happyPeople);
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 1beb390e609645dc10da4fd8ac08a6ba | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Queue {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader t = new FastReader();
PrintWriter o = new PrintWriter(System.out);
int n = t.nextInt();
Long[] a = new Long[n];
long wait = 0, count = 0;
for (int i = 0; i < n; i++)
a[i] = t.nextLong();
Arrays.sort(a);
for (int i = 0; i < n; i++) {
if (wait <= a[i]) {
count++;
wait += a[i];
}
}
o.print(count);
o.flush();
o.close();
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 36409ca95b513e9922ada969c5c19543 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | //package learning;
import java.util.*;
public class Answer {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n;
n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
Arrays.sort(arr);
int sum=0,count=0;
for(int i=0;i<n;i++) {
if(sum<=arr[i]) {
count++;
sum+=arr[i];
}
}
System.out.println(count);
sc.close();
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | e906aee5c632957bd0e22c4b52a9a655 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Problem545D {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int noOfGuys = sc.nextInt();
long timeNeededToServe[] = new long[noOfGuys];
for (int i = 0; i < noOfGuys; i++) {
timeNeededToServe[i] = sc.nextLong();
}
Arrays.sort(timeNeededToServe);
long countOfPeopleDisappointed = 0;
long waitingTime = timeNeededToServe[0];
for (int i = 1; i < noOfGuys; i++) {
if (waitingTime > timeNeededToServe[i]) {
countOfPeopleDisappointed++;
} else {
waitingTime = waitingTime + timeNeededToServe[i];
}
}
System.out.println(noOfGuys-countOfPeopleDisappointed);
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 874ed781676180ef0bfc42c1f83acda4 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
import java.io.*;
public class codeforces {
public static void main(String[] args) throws IOException
{
try{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
Arrays.parallelSort(arr);
int counter=0,sum=0;
for(int i=0;i<n;i++){
if(sum<=arr[i]){
counter++;
sum+=arr[i];
}
}
System.out.println(counter);
}
catch(Exception e){
System.out.println(e);
}
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 3d07d0acd329168ec550a3f59a42ff78 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Queue {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Integer[] arr = new Integer[n];
for(int i = 0; i < n;i++) {
arr[i] = sc.nextInt();
}
Collections.sort(Arrays.asList(arr));
int num = n;
int sum = 0;
for(int i = 0; i < n-1; i++) {
sum += arr[i];
if(sum > arr[i+1]) {
num--;
sum -= arr[i+1];
}
}
System.out.println(num);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 9c798dc47a4865c9e2b13f6af3df8ea0 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main {
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int n = nextInt();
long[] a = new long[n];
for (int i = 0; i < n; ++i) a[i] = nextInt();
Arrays.sort(a);
int ans = 0;
long cursum = 0;
for (int i = 0; i < n; i++) {
if (a[i] >= cursum){
cursum += a[i];
ans++;
}
}
pw.println(ans);
pw.close();
}
static long mod = 100_000_000;
static StringTokenizer st = new StringTokenizer("");
static BufferedReader br;
static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | ad5fdac279a359d8ab12d89300189554 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class RPS {
public static void main (String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] times = new int[n];
for (int i = 0; i < n; i++) {
times[i] = in.nextInt();
}
Arrays.sort(times);
int timeElapsed = 0;
int happyPeople = 0;
for (int i = 0; i < n; i++) {
if (timeElapsed <= times[i]) {
happyPeople++;
timeElapsed += times[i];
}
}
System.out.println(happyPeople);
}
//
// private static void mergeSortLaptops(int[][] laptops, int l, int r) {
// if (l < r) {
// int m = (l+r) / 2;
// mergeSortLaptops(laptops, l, m);
// mergeSortLaptops(laptops, m+1, r);
// merge(laptops, l, m, r);
// }
// }
//
// private static void merge(int[][] laptops, int l, int m, int r) {
// int n1 = m - l + 1;
// int n2 = r - m ;
// int[][] temp1 = new int[n1][2];
// int[][] temp2 = new int[n2][2];
// for (int i = 0; i < n1; i++)
// temp1[i] = laptops[l + i];
// for (int i = 0; i < n2; i++)
// temp2[i] = laptops[m + i + 1];
//
// int i = 0, j = 0, k = l;
// while (i < n1 && j < n2) {
// if (temp1[i][0] <= temp2[j][0]) {
// laptops[k] = temp1[i];
// i++;
// }
// else {
// laptops[k] = temp2[j];
// j++;
// }
// k++;
// }
//
// while (i < n1) {
// laptops[k] = temp1[i];
// i++; k++;
// }
//
// while (j < n2) {
// laptops[k] = temp2[j];
// j++; k++;
// }
// }
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 4f31b9f7a47b3ee12935bf7a962068f9 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class codeforces545D {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int noOfGuys=sc.nextInt();
long timeNeededToServe[]=new long[noOfGuys];
for(int i=0;i<noOfGuys;i++){
timeNeededToServe[i]=sc.nextLong();
}
Arrays.sort(timeNeededToServe);
long countOfPeopleDisappointed=0;
long waitingTime=timeNeededToServe[0];
for(int i=1;i<noOfGuys;i++){
if(waitingTime>timeNeededToServe[i]){
countOfPeopleDisappointed++;
}else{
waitingTime=waitingTime+timeNeededToServe[i];
}
}
System.out.println(noOfGuys-countOfPeopleDisappointed);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 8e43d37f85f25621cccfdad76c06ca81 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
private static void solver(InputReader sc, PrintWriter out) {
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++)
arr[i] = sc.nextInt();
Arrays.sort(arr);
int count=1;
long sum = arr[0];
for(int i=1; i<n; i++){
if(sum <= arr[i]) {
count++;
sum += arr[i];
}
}
out.println(count);
}
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
solver(in, out);
out.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());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 1d3914f15286f2c87021523b2a0901a4 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main {
static ArrayList<Integer>[] gr;
static boolean[] was;
static boolean[] want_h;
static int to;
static boolean ans;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
// br = new BufferedReader(new FileReader(new File("input.txt")));
// out = new PrintWriter(new File("output.txt"));
int PUTEN = 1;
for (int PUTIN_POMOGI = 0; PUTIN_POMOGI < PUTEN; PUTIN_POMOGI++) {
int n = nextInt();
int []a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
Arrays.sort(a);
int t = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
if(a[i] >= t){
ans ++;
t += a[i];
}
}
out.println(ans);
}
out.close();
}
static BufferedReader br;
static PrintWriter out;
static StringTokenizer in = new StringTokenizer("");
static String next() throws IOException {
while (!in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
static long fact(long n) {
long ans = 1;
for (int i = 1; i < n + 1; i++) {
ans *= i;
}
return ans;
}
static int[] mix(int[] a) {
Random rnd = new Random();
for (int i = 1; i < a.length; i++) {
int j = rnd.nextInt(i);
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
return a;
}
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 long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static class sort implements Comparator<point> {
@Override
public int compare(point o1, point o2) {
return Integer.compare(o1.ind, o2.ind);
}
}
static void dfs(int from, String want) {
if (ans) return;
was[from] = true;
if (want_h[from] && want.equals("H")) ans = true;
if (!want_h[from] && want.equals("G")) ans = true;
for (int p : gr[from]) {
if (!was[p]) dfs(p, want);
}
return;
}
}
class point {
int ind;
int wight;
int speed;
public point(int ind, int wight, int speed) {
this.ind = ind;
this.wight = wight;
this.speed = speed;
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | d3783ba0cedfa6a17624977e26d84f86 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class D545Unsolved {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
}
static FastReader in=new FastReader();
public static void main(String[] args) {
int n=in.nextInt();
int[] ar=new int[n];
for(int i=0;i<n;++i){
ar[i]=in.nextInt();
}
Arrays.sort(ar);
int total=ar[0];
int t=1;
for(int i=1;i<n;++i){
if(ar[i]>=total){
t++;
total+=ar[i];
}
}
System.out.println(t);
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 83183d710ff51a6af41a37e90a048bf1 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
static PrintWriter out=new PrintWriter(System.out);
public static void main (String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] input=br.readLine().trim().split(" ");
int n=Integer.parseInt(input[0]);
input=br.readLine().trim().split(" ");
Integer[] arr=new Integer[n];
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(input[i]);
}
out.println(maxHappyPeople(arr));
out.flush();
out.close();
}
public static int maxHappyPeople(Integer[] arr)
{
int count=1;
Arrays.sort(arr);
int sum=arr[0];
for(int i=1;i<arr.length;i++){
if(sum<=arr[i])
{
count++;
sum+=arr[i];
}
}
return count;
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 88fa72b24067a9a4c23b5fab5d24f2b0 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes |
/* package codechef; // don't place package name! */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
public class HelloWorld{
public static void main(String[] args) throws IOException
{
Scanner input = new Scanner(System.in);
while(input.hasNext()){
int[] l=new int[input.nextInt()];
for(int i=0;i<l.length;i++){
l[i]=input.nextInt();
}
Arrays.sort(l);
int s=0;
int t=0;
for(int i=0;i<l.length;i++){
if(l[i]>=t){
s++;
t+=l[i];
}
}
System.out.println(s);
}
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 9593bc1b3706c1564737f5d0efa59de3 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class node
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
int wait=0,c=0;
for(int i=0;i<arr.length;i++){
if(wait>arr[i]){
continue;
}
else{
wait+=arr[i];
c++;
}
}
System.out.print(c);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | cf1db9c7c813cf1eb485bfe3cf41e208 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import javafx.util.Pair;
import java.util.*;
public class Main {
// private int V;
// private LinkedList<Integer> adj[];
//
// Main(int v) {
// V = v;
// adj = new LinkedList[v];
// for (int i = 0; i < v; ++i) {
// adj[i] = new LinkedList();
// }
// }
//
// void addEdge(int v, int w) {
// adj[v].add(w);
// }
//
// void DFSUtil(int v, boolean visited[]) {
// visited[v] = true;
// System.out.println(v + " ");
// Iterator<Integer> i = adj[v].listIterator();
// while(i.hasNext()) {
// int n = i.next();
// if(!visited[n]) {
// DFSUtil(n, visited);
// }
// }
// }
//
// void DFS(int v) {
// boolean visited[] = new boolean[V];
//
// DFSUtil(v, visited);
// }
private static Scanner sc = new Scanner(System.in);
public static boolean can(int m, int sum) {
return sum >= 0 && sum <= 9*m;
}
// static class Pair {
// public int f, s;
//
// public Pair(int f, int s) {
// this.f = f;
// this.s = s;
// }
// }
public static void main(String[] args) {
int n = sc.nextInt();
List <Integer> a = new Vector<>();
for(int i = 0; i < n; ++i ) {
a.add(sc.nextInt());
}
Collections.sort(a);
int sum = a.get(0) , ans = 1;
for (int i = 1; i < n; ++i) {
if(a.get(i) >= sum) {
sum += a.get(i);
ans ++;
}
}
System.out.println(ans);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 98d18c886d0442c0e2c298c2281c5129 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] a = new int[n];
for(int i=0; i<n; i++) {
a[i] = input.nextInt();
}
Arrays.sort(a);
int[] b = new int[n+1];
b[0] = 0;
b[1] = a[0];
int count = 1;
for(int i=1, j=1; i<n; ) {
if(a[i] >= b[j]) {
count++;
j++;
b[j] = b[j-1]+a[i];
}
else {
i++;
}
}
System.out.println(count);
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | c6df5210fd2d2f96e08d87a77133e262 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes |
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t,a,c=1,n=sc.nextInt();
int ar[]=new int[n];
for(t=0;t<n;t++) ar[t]=sc.nextInt();
Arrays.sort(ar);
a=ar[0];
for(t=1;t<n;t++)
if(ar[t]>=a) { a+=ar[t];c++; }
System.out.print(c);
}}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 2893caab84a25d22e14dd3f823e64921 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
public class Length {
public static int min = 0;
public static int sg = 0;
public static ArrayList<Integer> arr = new ArrayList<Integer>(1000);
public static boolean possible = true;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long[] x = new long[n];
for(int i=0; i<n; i++){
x[i] = in.nextLong();
}
Arrays.sort(x);
int num = n;
int sum = 0;
for(int i=0; i<n; i++){
if(x[i]<sum){
num--;
}
else{
sum += x[i];
}
}
System.out.println(num);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | e470070f98fed96d75877d9c47561e82 | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class QueueProb {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i=0; i<n; i++){
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
int sum = 0, ans = n;
for (int i=0; i<n; i++){
if (sum > arr[i]) ans--;
else sum += arr[i];
}
System.out.println(ans);
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 5946089c7e8bddd36fc948f8f9c20dce | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes |
import java.util.*;
import java.io.*;
public class Queue {
public static void main(String[] args) throws IOException, FileNotFoundException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader in = new BufferedReader(new FileReader("Queue.in"));
// INPUT //
int n = Integer.parseInt(in.readLine());
ArrayList<Long> nums = new ArrayList<>();
ArrayList<Long> nowork = new ArrayList<>();
StringTokenizer st = new StringTokenizer(in.readLine());
for (int i = 0; i < n; i++) {
nums.add(Long.parseLong(st.nextToken()));
}
Collections.sort(nums);
in.close();
// CALCULATION //
long currcount = 0;
for (int i = 0; i < n; i++ ) {
// loop through all, and greedily go through
if (nums.get(i) >= currcount) {
currcount += nums.get(i);
}
else {
nowork.add(nums.get(i));
}
}
// OUTPUT //
System.out.println(nums.size() - nowork.size());
}
}
| Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | 5d7ad14a857832bfc6da77cd7458068b | train_001.jsonl | 1432053000 | Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue. | 256 megabytes | import java.util.*;
import java.lang.*;
public class Main {
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] time = new int[n];
for (int i = 0; i < n; i++) {
time[i] = in.nextInt();
}
Arrays.sort(time);
int t = 0;
int count = 0;
for (int i = 0; i < n; i++) {
if (time[i] >= t) {
t += time[i];
count++;
}
}
System.out.println(count);
}
} | Java | ["5\n15 2 1 5 3"] | 1 second | ["4"] | NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5. | Java 11 | standard input | [
"implementation",
"sortings",
"greedy"
] | 08c4d8db40a49184ad26c7d8098a8992 | The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces. | 1,300 | Print a single number — the maximum number of not disappointed people in the queue. | standard output | |
PASSED | f02ba18b32d1e3c6e1bd8f4e4d301fe2 | train_001.jsonl | 1552322100 | At the big break Nastya came to the school dining room. There are $$$n$$$ pupils in the school, numbered from $$$1$$$ to $$$n$$$. Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not going to despond because some pupils in the queue can agree to change places with some other pupils.Formally, there are some pairs $$$u$$$, $$$v$$$ such that if the pupil with number $$$u$$$ stands directly in front of the pupil with number $$$v$$$, Nastya can ask them and they will change places. Nastya asks you to find the maximal number of places in queue she can move forward. | 256 megabytes | import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
@SuppressWarnings("Duplicates")
public class ProblemD {
public static void main(String[] args) throws IOException{
Reader sc = new Reader();
PrintWriter pw = new PrintWriter(System.out);
//Scanner sc = new Scanner(System.in);
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int pupilsNum = sc.nextInt();
int pairsNum = sc.nextInt();
int[] queue = new int[pupilsNum];
//HashMap<Integer,Integer> pos = new HashMap<>();
for (int i = 0; i < pupilsNum-1; i++) {
queue[i] = sc.nextInt();
//pos.put(queue[i],i);
}
int nastya = sc.nextInt();
int[] passedBy = new int[pupilsNum+1];
// for (int i = 0; i < pupilsNum; i++) {
// toPassBy[i] = pupilsNum-2;
// }
HashSet<Integer> iCanPass = new HashSet<>();
HashSet<Integer>[] canPass = new HashSet[pupilsNum+1];
for (int i = 0; i < pupilsNum+1; i++) {
canPass[i] = new HashSet<>();
}
for (int i = 0; i < pairsNum; i++) {
int delante = sc.nextInt();
int detras = sc.nextInt();
if (detras == nastya){
iCanPass.add(delante);
} else if (delante == nastya){
continue;
} else {
canPass[detras].add(delante);
}
}
int iPassed = 0;
int steps = 0;
for (int i = pupilsNum-2; i >= 0; i--, steps++) {
if (iCanPass.contains(queue[i]) && passedBy[queue[i]]+iPassed==steps){
iPassed++;
}
else {
for (Integer pass : canPass[queue[i]]) {
passedBy[pass]++;
}
}
}
pw.println(iPassed);
pw.flush();
}
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 | ["2 1\n1 2\n1 2", "3 3\n3 1 2\n1 2\n3 1\n3 2", "5 2\n3 1 5 4 2\n5 2\n5 4"] | 2 seconds | ["1", "2", "1"] | NoteIn the first example Nastya can just change places with the first pupil in the queue.Optimal sequence of changes in the second example is change places for pupils with numbers $$$1$$$ and $$$3$$$. change places for pupils with numbers $$$3$$$ and $$$2$$$. change places for pupils with numbers $$$1$$$ and $$$2$$$. The queue looks like $$$[3, 1, 2]$$$, then $$$[1, 3, 2]$$$, then $$$[1, 2, 3]$$$, and finally $$$[2, 1, 3]$$$ after these operations. | Java 8 | standard input | [
"greedy"
] | 1716b35de299e88c891ba71f9c368b51 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 3 \cdot 10^{5}$$$, $$$0 \leq m \leq 5 \cdot 10^{5}$$$) — the number of pupils in the queue and number of pairs of pupils such that the first one agrees to change places with the second one if the first is directly in front of the second. The second line contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ — the initial arrangement of pupils in the queue, from the queue start to its end ($$$1 \leq p_i \leq n$$$, $$$p$$$ is a permutation of integers from $$$1$$$ to $$$n$$$). In other words, $$$p_i$$$ is the number of the pupil who stands on the $$$i$$$-th position in the queue. The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n, u_i \neq v_i$$$), denoting that the pupil with number $$$u_i$$$ agrees to change places with the pupil with number $$$v_i$$$ if $$$u_i$$$ is directly in front of $$$v_i$$$. It is guaranteed that if $$$i \neq j$$$, than $$$v_i \neq v_j$$$ or $$$u_i \neq u_j$$$. Note that it is possible that in some pairs both pupils agree to change places with each other. Nastya is the last person in the queue, i.e. the pupil with number $$$p_n$$$. | 1,800 | Print a single integer — the number of places in queue she can move forward. | standard output | |
PASSED | 49319d0940cdaa21ef480b3ec4335ddc | train_001.jsonl | 1552322100 | At the big break Nastya came to the school dining room. There are $$$n$$$ pupils in the school, numbered from $$$1$$$ to $$$n$$$. Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not going to despond because some pupils in the queue can agree to change places with some other pupils.Formally, there are some pairs $$$u$$$, $$$v$$$ such that if the pupil with number $$$u$$$ stands directly in front of the pupil with number $$$v$$$, Nastya can ask them and they will change places. Nastya asks you to find the maximal number of places in queue she can move forward. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int[] a = new int[n];
for(int i=0; i<n; i++)
a[i] = Integer.parseInt(st.nextToken())-1;
int[] us =new int[m];
int[] vs = new int[m];
int[] deg = new int[n];
for(int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken())-1;
int v = Integer.parseInt(st.nextToken())-1;
deg[u]++;
us[i] = u; vs[i] = v;
}
int[][] adj = new int[n][];
for(int i = 0; i < n; i++) adj[i] = new int[deg[i]];
for(int i = 0; i < m; i++) {
int u = us[i];
int v = vs[i];
adj[u][--deg[u]] = v;
}
boolean[] reach = new boolean[n];
reach[a[n-1]] = true;
int alive =1;
int ans = 0;
for(int i=n-2; i>=0; i--){
int count = 0;
for(int v : adj[a[i]])
if(reach[v]) count++;
if(count == alive)
ans++;
else{
alive++;
reach[a[i]] = true;
}
}
out.println(ans);
out.close();
}
}
| Java | ["2 1\n1 2\n1 2", "3 3\n3 1 2\n1 2\n3 1\n3 2", "5 2\n3 1 5 4 2\n5 2\n5 4"] | 2 seconds | ["1", "2", "1"] | NoteIn the first example Nastya can just change places with the first pupil in the queue.Optimal sequence of changes in the second example is change places for pupils with numbers $$$1$$$ and $$$3$$$. change places for pupils with numbers $$$3$$$ and $$$2$$$. change places for pupils with numbers $$$1$$$ and $$$2$$$. The queue looks like $$$[3, 1, 2]$$$, then $$$[1, 3, 2]$$$, then $$$[1, 2, 3]$$$, and finally $$$[2, 1, 3]$$$ after these operations. | Java 8 | standard input | [
"greedy"
] | 1716b35de299e88c891ba71f9c368b51 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 3 \cdot 10^{5}$$$, $$$0 \leq m \leq 5 \cdot 10^{5}$$$) — the number of pupils in the queue and number of pairs of pupils such that the first one agrees to change places with the second one if the first is directly in front of the second. The second line contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ — the initial arrangement of pupils in the queue, from the queue start to its end ($$$1 \leq p_i \leq n$$$, $$$p$$$ is a permutation of integers from $$$1$$$ to $$$n$$$). In other words, $$$p_i$$$ is the number of the pupil who stands on the $$$i$$$-th position in the queue. The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n, u_i \neq v_i$$$), denoting that the pupil with number $$$u_i$$$ agrees to change places with the pupil with number $$$v_i$$$ if $$$u_i$$$ is directly in front of $$$v_i$$$. It is guaranteed that if $$$i \neq j$$$, than $$$v_i \neq v_j$$$ or $$$u_i \neq u_j$$$. Note that it is possible that in some pairs both pupils agree to change places with each other. Nastya is the last person in the queue, i.e. the pupil with number $$$p_n$$$. | 1,800 | Print a single integer — the number of places in queue she can move forward. | standard output | |
PASSED | 8d57baacbe2e78aaf9b9d5a279825eb0 | train_001.jsonl | 1552322100 | At the big break Nastya came to the school dining room. There are $$$n$$$ pupils in the school, numbered from $$$1$$$ to $$$n$$$. Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not going to despond because some pupils in the queue can agree to change places with some other pupils.Formally, there are some pairs $$$u$$$, $$$v$$$ such that if the pupil with number $$$u$$$ stands directly in front of the pupil with number $$$v$$$, Nastya can ask them and they will change places. Nastya asks you to find the maximal number of places in queue she can move forward. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(in.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
HashSet<Integer>[] adj = new HashSet[n+1];
HashSet<Integer> set = new HashSet<>();
for (int i = 1; i <= n; i++) set.add(i);
for (int i = 0; i <= n; i++) adj[i] = new HashSet<>();
int[] a = new int[n];
st = new StringTokenizer(in.readLine());
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
for (int i = 0; i < m; i++) {
st = new StringTokenizer(in.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
adj[u].add(v);
}
int res = 0;
for (int i = n-1; i >= 0; i--) {
// System.out.println(set.toString());
if (set.size() == 0) break;
// if (!adj[a[i]].contains(a[n-1]) ) {
HashSet<Integer> remove = new HashSet<>();
if (adj[a[i]].contains(a[n-1]) && set.contains(a[i])) {
res++;
continue;
}
for (int j : set) {
if (!adj[j].contains(a[i])) {
// System.out.println("Remove " + j);
remove.add(j);
}
// }
}
set.removeAll(remove);
// System.out.println(a[i] + " " + set.toString());
}
out.println(res);
out.close();
}
}
/*
5 4
3 1 5 4 2
5 2
5 4
1 4
1 2
*/ | Java | ["2 1\n1 2\n1 2", "3 3\n3 1 2\n1 2\n3 1\n3 2", "5 2\n3 1 5 4 2\n5 2\n5 4"] | 2 seconds | ["1", "2", "1"] | NoteIn the first example Nastya can just change places with the first pupil in the queue.Optimal sequence of changes in the second example is change places for pupils with numbers $$$1$$$ and $$$3$$$. change places for pupils with numbers $$$3$$$ and $$$2$$$. change places for pupils with numbers $$$1$$$ and $$$2$$$. The queue looks like $$$[3, 1, 2]$$$, then $$$[1, 3, 2]$$$, then $$$[1, 2, 3]$$$, and finally $$$[2, 1, 3]$$$ after these operations. | Java 8 | standard input | [
"greedy"
] | 1716b35de299e88c891ba71f9c368b51 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 3 \cdot 10^{5}$$$, $$$0 \leq m \leq 5 \cdot 10^{5}$$$) — the number of pupils in the queue and number of pairs of pupils such that the first one agrees to change places with the second one if the first is directly in front of the second. The second line contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ — the initial arrangement of pupils in the queue, from the queue start to its end ($$$1 \leq p_i \leq n$$$, $$$p$$$ is a permutation of integers from $$$1$$$ to $$$n$$$). In other words, $$$p_i$$$ is the number of the pupil who stands on the $$$i$$$-th position in the queue. The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n, u_i \neq v_i$$$), denoting that the pupil with number $$$u_i$$$ agrees to change places with the pupil with number $$$v_i$$$ if $$$u_i$$$ is directly in front of $$$v_i$$$. It is guaranteed that if $$$i \neq j$$$, than $$$v_i \neq v_j$$$ or $$$u_i \neq u_j$$$. Note that it is possible that in some pairs both pupils agree to change places with each other. Nastya is the last person in the queue, i.e. the pupil with number $$$p_n$$$. | 1,800 | Print a single integer — the number of places in queue she can move forward. | standard output | |
PASSED | 5a6fb787fd036968b2ed539a85f12332 | train_001.jsonl | 1552322100 | At the big break Nastya came to the school dining room. There are $$$n$$$ pupils in the school, numbered from $$$1$$$ to $$$n$$$. Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not going to despond because some pupils in the queue can agree to change places with some other pupils.Formally, there are some pairs $$$u$$$, $$$v$$$ such that if the pupil with number $$$u$$$ stands directly in front of the pupil with number $$$v$$$, Nastya can ask them and they will change places. Nastya asks you to find the maximal number of places in queue she can move forward. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.List;
import java.util.*;
public class realfast implements Runnable
{
private static final int INF = (int) 1e9;
long in= (long)Math.pow(10,9);
long fac[]= new long[3000];
public void solve() throws IOException
{
int n = readInt();
int m = readInt();
int pos[]= new int[n+1];
int arr[]= new int[n+1];
for(int i=1;i<=n;i++)
{
int a = readInt();
arr[i]=a;
pos[a]=i;
}
int s = arr[n];
ArrayList<Integer> adj[] = new ArrayList[n+1];
for(int i=1;i<=n;i++)
adj[i]= new ArrayList<>();
boolean check[]= new boolean [n+1];
for(int i=0;i<m;i++)
{
int u = readInt();
int v = readInt();
adj[u].add(v);
if(v==s)
{
check[u]=true;
}
}
for(int i=1;i<=n;i++)
{
if(adj[i].size()>1)
{
Collections.sort(adj[i]);
}
}
for(int i=n-1;i>=1;i--)
{
if(check[arr[i]])
{
//out.println(i);
int cal = arr[i];
for(int j =i+1;j<=n;j++)
{
int val = arr[j];
if(search(adj,cal,val))
{
arr[j]= cal;
arr[j-1]= val;
}
else
break;
}
}
}
int po=n;
for(int i=1;i<=n;i++)
{
if(arr[i]==s)
{
po=i;
break;
}
}
out.println(n-po);
}
public boolean search(ArrayList arr[] , int i, int j )
{
int left =0;
int right = arr[i].size()-1;
while(left<=right)
{
int mid = left + (right-left)/2;
int val = (int)arr[i].get(mid);
if(val==j)
return true;
if(val<j)
{
left=mid+1;
}
else
right=mid-1;
}
return false;
}
public long pow(long n , long p,long m)
{
if(p==0)
return 1;
long val = pow(n,p/2,m);;
val= (val*val)%m;
if(p%2==0)
return val;
else
return (val*n)%m;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
new Thread(null, new realfast(), "", 128 * (1L << 20)).start();
}
private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
private BufferedReader reader;
private StringTokenizer tokenizer;
private PrintWriter out;
@Override
public void run() {
try {
if (ONLINE_JUDGE || !new File("input.txt").exists()) {
reader = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
reader = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
solve();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
reader.close();
} catch (IOException e) {
// nothing
}
out.close();
}
}
private String readString() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
@SuppressWarnings("unused")
private int readInt() throws IOException {
return Integer.parseInt(readString());
}
@SuppressWarnings("unused")
private long readLong() throws IOException {
return Long.parseLong(readString());
}
@SuppressWarnings("unused")
private double readDouble() throws IOException {
return Double.parseDouble(readString());
}
}
class edge implements Comparable<edge>{
int val ;
int color;
edge(int u, int v)
{
this.val=u;
this.color=v;
}
public int compareTo(edge e)
{
return this.val-e.val;
}
} | Java | ["2 1\n1 2\n1 2", "3 3\n3 1 2\n1 2\n3 1\n3 2", "5 2\n3 1 5 4 2\n5 2\n5 4"] | 2 seconds | ["1", "2", "1"] | NoteIn the first example Nastya can just change places with the first pupil in the queue.Optimal sequence of changes in the second example is change places for pupils with numbers $$$1$$$ and $$$3$$$. change places for pupils with numbers $$$3$$$ and $$$2$$$. change places for pupils with numbers $$$1$$$ and $$$2$$$. The queue looks like $$$[3, 1, 2]$$$, then $$$[1, 3, 2]$$$, then $$$[1, 2, 3]$$$, and finally $$$[2, 1, 3]$$$ after these operations. | Java 8 | standard input | [
"greedy"
] | 1716b35de299e88c891ba71f9c368b51 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 3 \cdot 10^{5}$$$, $$$0 \leq m \leq 5 \cdot 10^{5}$$$) — the number of pupils in the queue and number of pairs of pupils such that the first one agrees to change places with the second one if the first is directly in front of the second. The second line contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ — the initial arrangement of pupils in the queue, from the queue start to its end ($$$1 \leq p_i \leq n$$$, $$$p$$$ is a permutation of integers from $$$1$$$ to $$$n$$$). In other words, $$$p_i$$$ is the number of the pupil who stands on the $$$i$$$-th position in the queue. The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n, u_i \neq v_i$$$), denoting that the pupil with number $$$u_i$$$ agrees to change places with the pupil with number $$$v_i$$$ if $$$u_i$$$ is directly in front of $$$v_i$$$. It is guaranteed that if $$$i \neq j$$$, than $$$v_i \neq v_j$$$ or $$$u_i \neq u_j$$$. Note that it is possible that in some pairs both pupils agree to change places with each other. Nastya is the last person in the queue, i.e. the pupil with number $$$p_n$$$. | 1,800 | Print a single integer — the number of places in queue she can move forward. | standard output | |
PASSED | 45d3af3bc0405d5751c64e2fe5e9c6a1 | train_001.jsonl | 1552322100 | At the big break Nastya came to the school dining room. There are $$$n$$$ pupils in the school, numbered from $$$1$$$ to $$$n$$$. Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not going to despond because some pupils in the queue can agree to change places with some other pupils.Formally, there are some pairs $$$u$$$, $$$v$$$ such that if the pupil with number $$$u$$$ stands directly in front of the pupil with number $$$v$$$, Nastya can ask them and they will change places. Nastya asks you to find the maximal number of places in queue she can move forward. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try {
String[] line;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
line = in.readLine().split(" ");
int n = Integer.parseInt(line[0]);
int m = Integer.parseInt(line[1]);
int[] p = new int[n];
line = in.readLine().split(" ");
for(int i=0;i<n;i++){
p[i] = Integer.parseInt(line[i]);
}
Map<Integer, Set<Integer>> map = new HashMap<>();
Set<Integer> cur;
int key;
int value;
for(int i=0;i<m;i++){
line = in.readLine().split(" ");
key = Integer.parseInt(line[0]);
value = Integer.parseInt(line[1]);
cur = map.get(key);
if (cur!=null){
cur.add(value);
}
else{
map.put(key, new HashSet<>(Collections.singleton(value)));
}
}
if (n==1){
System.out.println(0);
return;
}
int result = 0;
Set<Integer> currentQueue = new HashSet<>(Collections.singleton(p[n-1]));
for (int i=n-2; i>=0;i--){
if (map.containsKey(p[i]) && map.get(p[i]).containsAll(currentQueue)){
result++;
}
else{
currentQueue.add(p[i]);
}
}
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
} | Java | ["2 1\n1 2\n1 2", "3 3\n3 1 2\n1 2\n3 1\n3 2", "5 2\n3 1 5 4 2\n5 2\n5 4"] | 2 seconds | ["1", "2", "1"] | NoteIn the first example Nastya can just change places with the first pupil in the queue.Optimal sequence of changes in the second example is change places for pupils with numbers $$$1$$$ and $$$3$$$. change places for pupils with numbers $$$3$$$ and $$$2$$$. change places for pupils with numbers $$$1$$$ and $$$2$$$. The queue looks like $$$[3, 1, 2]$$$, then $$$[1, 3, 2]$$$, then $$$[1, 2, 3]$$$, and finally $$$[2, 1, 3]$$$ after these operations. | Java 8 | standard input | [
"greedy"
] | 1716b35de299e88c891ba71f9c368b51 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 3 \cdot 10^{5}$$$, $$$0 \leq m \leq 5 \cdot 10^{5}$$$) — the number of pupils in the queue and number of pairs of pupils such that the first one agrees to change places with the second one if the first is directly in front of the second. The second line contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ — the initial arrangement of pupils in the queue, from the queue start to its end ($$$1 \leq p_i \leq n$$$, $$$p$$$ is a permutation of integers from $$$1$$$ to $$$n$$$). In other words, $$$p_i$$$ is the number of the pupil who stands on the $$$i$$$-th position in the queue. The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n, u_i \neq v_i$$$), denoting that the pupil with number $$$u_i$$$ agrees to change places with the pupil with number $$$v_i$$$ if $$$u_i$$$ is directly in front of $$$v_i$$$. It is guaranteed that if $$$i \neq j$$$, than $$$v_i \neq v_j$$$ or $$$u_i \neq u_j$$$. Note that it is possible that in some pairs both pupils agree to change places with each other. Nastya is the last person in the queue, i.e. the pupil with number $$$p_n$$$. | 1,800 | Print a single integer — the number of places in queue she can move forward. | standard output | |
PASSED | 7b5a637c550e8ae8413380f11c1f768f | train_001.jsonl | 1552322100 | At the big break Nastya came to the school dining room. There are $$$n$$$ pupils in the school, numbered from $$$1$$$ to $$$n$$$. Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not going to despond because some pupils in the queue can agree to change places with some other pupils.Formally, there are some pairs $$$u$$$, $$$v$$$ such that if the pupil with number $$$u$$$ stands directly in front of the pupil with number $$$v$$$, Nastya can ask them and they will change places. Nastya asks you to find the maximal number of places in queue she can move forward. | 256 megabytes | import javafx.util.*;
import java.util.*;
import java.io.*;
public class Prg6 {
Random rnd = new Random();
PrintWriter pw = new PrintWriter(System.out);
HashSet<Integer>[] l;
int[] p, s;
int a;
void run(){
a = ni();
l = new HashSet[a];
for(int q=0; q<a; q++) l[q] = new HashSet<>();
TreeSet<Integer> t = new TreeSet<>();
int k =ni();
p = new int[a];
for(int q=0; q<a; q++) p[q] = ni()-1;
int n= p[a-1];
for(int q=0; q<k; q++){
int x = ni()-1, y = ni()-1;
l[x].add(y);
}
for(int q=0; q<a-1; q++) if(l[p[q]].contains(p[q+1])) t.add(q);
for(; t.size()>0; ){
int i = t.pollLast();
if(l[p[i]].contains(p[i+1])){
l[p[i]].remove(p[i+1]);
l[p[i+1]].remove(p[i]);
p[i]^=p[i+1]^=p[i];
p[i+1]^=p[i];
if(i+2<a){
if(l[p[i+1]].contains(p[i+2])) t.add(i+1);
}
if(i>0){
if(l[p[i-1]].contains(p[i])) t.add(i-1);
}
}
}
//for(int u : p) System.out.print(u+" ");System.out.println();
int o = 0;
for(int q=a-1; q>=0 && p[q]!=n; q--) o++;
pw.print(o);
pw.flush();
}
static class PyraSort {
private static int heapSize;
public static void sort(int[] a) {
buildHeap(a);
while (heapSize > 1) {
swap(a, 0, heapSize - 1);
heapSize--;
heapify(a, 0);
}
}
private static void buildHeap(int[] a) {
heapSize = a.length;
for (int i = a.length / 2; i >= 0; i--) {
heapify(a, i);
}
}
private static void heapify(int[] a, int i) {
int l = 2 * i + 2;
int r = 2 * i + 1;
int largest = i;
if (l < heapSize && a[i] < a[l]) {
largest = l;
}
if (r < heapSize && a[largest] < a[r]) {
largest = r;
}
if (i != largest) {
swap(a, i, largest);
heapify(a, largest);
}
}
private static void swap(int[] a, int i, int j) {
a[i] ^= a[j] ^= a[i];
a[j] ^= a[i];
}
}
public static void main(String[] args) {
new Prg6().run();
}
InputStream is = System.in;
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))){
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private String nline(){
int b = readByte();
StringBuilder sb = new StringBuilder();
while (b!=10) {
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 && !(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 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();
}
}
} | Java | ["2 1\n1 2\n1 2", "3 3\n3 1 2\n1 2\n3 1\n3 2", "5 2\n3 1 5 4 2\n5 2\n5 4"] | 2 seconds | ["1", "2", "1"] | NoteIn the first example Nastya can just change places with the first pupil in the queue.Optimal sequence of changes in the second example is change places for pupils with numbers $$$1$$$ and $$$3$$$. change places for pupils with numbers $$$3$$$ and $$$2$$$. change places for pupils with numbers $$$1$$$ and $$$2$$$. The queue looks like $$$[3, 1, 2]$$$, then $$$[1, 3, 2]$$$, then $$$[1, 2, 3]$$$, and finally $$$[2, 1, 3]$$$ after these operations. | Java 8 | standard input | [
"greedy"
] | 1716b35de299e88c891ba71f9c368b51 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 3 \cdot 10^{5}$$$, $$$0 \leq m \leq 5 \cdot 10^{5}$$$) — the number of pupils in the queue and number of pairs of pupils such that the first one agrees to change places with the second one if the first is directly in front of the second. The second line contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ — the initial arrangement of pupils in the queue, from the queue start to its end ($$$1 \leq p_i \leq n$$$, $$$p$$$ is a permutation of integers from $$$1$$$ to $$$n$$$). In other words, $$$p_i$$$ is the number of the pupil who stands on the $$$i$$$-th position in the queue. The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n, u_i \neq v_i$$$), denoting that the pupil with number $$$u_i$$$ agrees to change places with the pupil with number $$$v_i$$$ if $$$u_i$$$ is directly in front of $$$v_i$$$. It is guaranteed that if $$$i \neq j$$$, than $$$v_i \neq v_j$$$ or $$$u_i \neq u_j$$$. Note that it is possible that in some pairs both pupils agree to change places with each other. Nastya is the last person in the queue, i.e. the pupil with number $$$p_n$$$. | 1,800 | Print a single integer — the number of places in queue she can move forward. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.