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 | 7dd6d535dc31a08a80131042d7c7ee8b | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Housni Abdellatif
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, Scanner sc, PrintWriter out) {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; ++i) {
a[i] = sc.nextInt();
}
long ans = 0;
loop:
for (int i = 0; i < n; ) {
int max = a[i];
for (int j = i + 1; j < n; ++j) {
if ((a[j] ^ a[i]) < 0) {
i = j;
ans += max;
continue loop;
}
max = Math.max(max, a[j]);
}
ans += max;
break;
}
out.println(ans);
}
}
}
}
| Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | d29c0dafa982a0bcce9b31cd34c6e880 | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class solution {
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 void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
for(int i=0;i<t;i++){
int n = Integer.parseInt(br.readLine());
long a[] = new long[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j=0;j<n;j++){
a[j] = Long.parseLong(st.nextToken());
}
Long max = a[0];
ArrayList<Long>A = new ArrayList<>();
for(int j=1;j<n;j++){
if(a[j]*a[j-1] <0){
A.add(max);
max = a[j];
}
else{
max = Math.max(max,a[j]);
}
}
A.add(max);
long ans = 0;
for(int j=0;j<A.size();j++){
ans+=A.get(j);
}
System.out.println(ans);
}
}
} | Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | b16c297cd690a81e992bfd32f1a6de78 | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.Random;
import java.io.FileWriter;
import java.io.PrintWriter;
/*
Solution Created: 18:21:56 21/04/2020
Custom Competitive programming helper.
*/
public class Main {
public static void solve(Reader in, Writer out) {
int t = in.nextInt();
while(t-->0) {
int n = in.nextInt();
int[] a = in.na(n);
ArrayList<Integer> fix = new ArrayList<Integer>();
fix.add(a[0]);
boolean hasPos = false, hasNeg = false;
if(a[0]>0) hasPos = true;
else hasNeg = true;
for(int i = 1; i<n; i++) {
int l = fix.get(fix.size()-1);
int c = a[i];
if(c>0) hasPos = true;
else hasNeg = true;
if(l>0) {
if(c>0) fix.set(fix.size()-1, Math.max(l, c));
else fix.add(c);
}else {
if(c<0) fix.set(fix.size()-1, Math.max(l, c));
else fix.add(c);
}
}
if(((!hasPos)||!(hasNeg)) && fix.size()!=1) out.println(-1);
else {
n = fix.size();
long s = 0;
for(Integer i : fix) s+=i;
out.println(s);
}
}
}
public static void main(String[] args) {
Reader in = new Reader();
Writer out = new Writer();
solve(in, out);
out.exit();
}
static class Reader {
static BufferedReader br;
static StringTokenizer st;
private int charIdx = 0;
private String s;
public Reader() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(String f){
try {
this.br = new BufferedReader(new FileReader(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public double[] nd(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++)
a[i] = nextDouble();
return a;
}
public char nextChar() {
if (s == null || charIdx >= s.length()) {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
s = st.nextToken();
charIdx = 0;
}
return s.charAt(charIdx++);
}
public long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[] nca() {
return next().toCharArray();
}
public String[] nS(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++)
a[i] = next();
return a;
}
public int nextInt() {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
return Integer.parseInt(st.nextToken());
}
public double nextDouble() {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
return Double.parseDouble(st.nextToken());
}
public Long nextLong() {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
return Long.parseLong(st.nextToken());
}
public String next() {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
return st.nextToken();
}
public static void readLine() {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
}
}
}
static class Util{
private static Random random = new Random();
static long[] fact;
public static void initFactorial(int n, long mod) {
fact = new long[n+1];
fact[0] = 1;
for (int i = 1; i < n+1; i++) fact[i] = (fact[i - 1] * i) % mod;
}
public static long modInverse(long a, long MOD) {
long[] gcdE = gcdExtended(a, MOD);
if (gcdE[0] != 1) return -1; // Inverted doesn't exist
long x = gcdE[1];
return (x % MOD + MOD) % MOD;
}
public static long[] gcdExtended(long p, long q) {
if (q == 0) return new long[] { p, 1, 0 };
long[] vals = gcdExtended(q, p % q);
long tmp = vals[2];
vals[2] = vals[1] - (p / q) * vals[2];
vals[1] = tmp;
return vals;
}
public static long nCr(int n, int r, long MOD) {
if (r == 0) return 1;
return (fact[n] * modInverse(fact[r], MOD) % MOD * modInverse(fact[n - r], MOD) % MOD) % MOD;
}
public static long nCr(int n, int r) {
return (fact[n]/fact[r])/fact[n-r];
}
public static long nPr(int n, int r, long MOD) {
if (r == 0) return 1;
return (fact[n] * modInverse(fact[n - r], MOD) % MOD) % MOD;
}
public static long nPr(int n, int r) {
return fact[n]/fact[n-r];
}
public static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static int lowerBound(int[] a, int v) {
int l = 0, h = a.length;
while(l<h) {
int mid = (l+h)/2;
if(v<=a[mid]) h = mid;
else l = mid+1;
}
return l;
}
public static int lowerBound(long[] a, long v) {
int l = 0, h = a.length;
while(l<h) {
int mid = (l+h)/2;
if(v<=a[mid]) h = mid;
else l = mid+1;
}
return l;
}
public static int upperBound(int[] a, int v) {
int l = 0, h = a.length;
while(l<h) {
int mid = (l+h)/2;
if(a[mid]<=v) l = mid+1;
else h = mid;
}
return l;
}
public static int upperBound(long[] a, long v) {
int l = 0, h = a.length;
while(l<h) {
int mid = (l+h)/2;
if(a[mid]<=v) l = mid+1;
else h = mid;
}
return l;
}
public static boolean[] getSieve(int n) {
boolean[] isPrime = new boolean[n+1];
for (int i = 2; i <= n; i++) isPrime[i] = true;
for (int i = 2; i*i <= n; i++) if (isPrime[i])
for (int j = i; i*j <= n; j++) isPrime[i*j] = false;
return isPrime;
}
public static int gcd(int a, int b) {
int tmp = 0;
while(b != 0) {
tmp = b;
b = a%b;
a = tmp;
}
return a;
}
public static long gcd(long a, long b) {
long tmp = 0;
while(b != 0) {
tmp = b;
b = a%b;
a = tmp;
}
return a;
}
public static int random(int min, int max) {
return random.nextInt(max-min+1)+min;
}
public static void dbg(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public static void reverse(int[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
int tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(int[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(long[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
long tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(long[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(float[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
float tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(float[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(double[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
double tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(double[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(char[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
char tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(char[] s) {
reverse(s, 0, s.length-1);
}
public static <T> void reverse(T[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
T tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static <T> void reverse(T[] s) {
reverse(s, 0, s.length-1);
}
public static void shuffle(int[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
int t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(long[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
long t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(float[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
float t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(double[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
double t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(char[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
char t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static <T> void shuffle(T[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
T t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void sortArray(int[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(long[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(float[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(double[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(char[] a) {
shuffle(a);
Arrays.sort(a);
}
public static <T extends Comparable<T>> void sortArray(T[] a) {
Arrays.sort(a);
}
}
static class Writer {
private PrintWriter pw;
public Writer(){
pw = new PrintWriter(System.out);
}
public Writer(String f){
try {
pw = new PrintWriter(new FileWriter(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public void printArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void printArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void print(Object o) {
pw.print(o.toString());
}
public void println(Object o) {
pw.println(o.toString());
}
public void println() {
pw.println();
}
public void flush() {
pw.flush();
}
public void exit() {
pw.close();
}
}
}
| Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | a71beb35672ee60604bb13b07a8a8c30 | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i = 0; i < t; i++) {
int n = sc.nextInt();
long inp[] = new long[n];
for(int j = 0; j < n; j++) {
inp[j] = sc.nextLong();
}
int p = 0;
long sum = 0;
while(p < n)
{
long neg = Long.MIN_VALUE, pos = Long.MIN_VALUE;
while(p < n && inp[p] < 0)
{
if(inp[p] > neg)
neg = inp[p];
p++;
}
sum += neg == Long.MIN_VALUE ? 0 : neg;
while(p < n && inp[p] > 0)
{
if(inp[p] > pos)
pos = inp[p];
p++;
}
sum += pos == Long.MIN_VALUE ? 0 : pos;
}
System.out.println(sum);
}
}
} | Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | 2c115341786d9612ce97403cfd9f7aa0 | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.*;
/* Name of the class has to be "Main" only if the class is public*/
public class cf_1343C
{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Node {
long pp;
long a, b;
Node(long x, long y) {
a = x;
b = y;
pp = a * b;
}
}
static class Comp implements Comparator<Node> {
public int compare(Node o1, Node o2) {
if (o1.pp > o2.pp) {
return 1;
} else {
return -1;
}
}
}
static int gcd(int x, int y)
{
if(y==0) return x;
else return gcd(y,x%y);
}
static boolean isprime(int x)
{
for(int i=2;i<=Math.sqrt(x);i++)
{
if(x%i==0) return false;
}
return true;
}
static boolean isPowerOfTwo (int x)
{
return x!=0 && ((x&(x-1)) == 0);
}
public static void main(String[] args) {
FastReader sc=new FastReader();
PrintWriter out=new PrintWriter(System.out);
//your code starts here
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++) a[i]=sc.nextInt();
long best=0;int sign=a[0]/Math.abs(a[0]);
ArrayDeque<Integer> st=new ArrayDeque<>();
st.push(a[0]);
for(int i=1;i<n;i++)
{
int sgn=a[i]/Math.abs(a[i]);
if(sgn==sign)
{
if(a[i]>st.peek())
{
st.pop(); st.push(a[i]);
}
}
else st.push(a[i]);
sign=sgn; sgn=0;
}
//out.println(st.toString());
for(Integer i:st) best+=i;
out.println(best);
}
out.close();
}
}
| Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | 9cb2381f000f335d5c05093592aaa830 | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.*;
/* Name of the class has to be "Main" only if the class is public*/
public class cf_1343C
{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Node {
long pp;
long a, b;
Node(long x, long y) {
a = x;
b = y;
pp = a * b;
}
}
static class Comp implements Comparator<Node> {
public int compare(Node o1, Node o2) {
if (o1.pp > o2.pp) {
return 1;
} else {
return -1;
}
}
}
static int gcd(int x, int y)
{
if(y==0) return x;
else return gcd(y,x%y);
}
static boolean isprime(int x)
{
for(int i=2;i<=Math.sqrt(x);i++)
{
if(x%i==0) return false;
}
return true;
}
static boolean isPowerOfTwo (int x)
{
return x!=0 && ((x&(x-1)) == 0);
}
public static void main(String[] args) {
FastReader sc=new FastReader();
PrintWriter out=new PrintWriter(System.out);
//your code starts here
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++) a[i]=sc.nextInt();
long best=0;
int sign=a[0]/Math.abs(a[0]);
long nmax=Long.MIN_VALUE,pmax=Long.MAX_VALUE;
if(sign==-1) nmax=a[0]; else pmax=a[0];
for(int i=1;i<n;i++)
{
int sgn=a[i]/Math.abs(a[i]);
if(sgn==-1)
{
if(sign==sgn)
{
nmax=Math.max(a[i],nmax);
}
else
{
sign=sgn;
best+=pmax;pmax=Long.MAX_VALUE;
nmax=a[i];
}
}
else
{
if(sign==sgn)
{
pmax=Math.max(a[i],pmax);
}
else
{
sign=sgn;
best+=nmax;nmax=Long.MIN_VALUE;
pmax=a[i];
}
}
}
if(pmax==Long.MAX_VALUE) best+=nmax;
if(nmax==Long.MIN_VALUE) best+=pmax;
out.println(best);
}
out.close();
}
}
| Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | ce79bf7b46a68ae83867700c073c767d | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Q1 {
public static void main(String[] args) {
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
while (t-- > 0) {
int N = in.nextInt();
int arr[] = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = in.nextInt();
}
long sum=0;
long flag=arr[0]>0?1:0,max=0,min=0;
for(int i =0;i<N;i++){
if(flag==1 && arr[i]>0)
max=Math.max(max,arr[i]);
else if(flag==0 && arr[i]<0){
if(min==0)
min=arr[i];
else
min=Math.max(min,arr[i]);
} else if(flag==1){
sum+=max;
max=0; min=arr[i];
flag=flag^1;
}else{
sum+=min;
min=0; max=arr[i];
flag=flag^1;
}
}
sum+=max+min;
out.println(sum);
}
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public int[] shuffle(int[] arr) {
Random r = new Random();
for (int i = 1, j; i < arr.length; i++) {
j = r.nextInt(i);
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
return arr;
}
public InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public char nextChar() {
return next().charAt(0);
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
| Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | 831885c427b48c3e04b1e3b0218633b0 | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 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 Main {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main (String[] args) throws java.lang.Exception {
FastReader sc = new FastReader() ;
int t = sc.nextInt() ;
while(t-->0) {
int n = sc.nextInt() ;
int a[] = new int[n] ;
long sum = 0 ;
for(int i = 0 ; i < n ; i++) {
a[i] = sc.nextInt() ;
}
int j ;
int curr_max ;
for(int i = 0 ; i < n ; i++) {
curr_max = a[i] ;
for(j = i ; j < n && isSameSign(a[i], a[j]) ; j++) {
curr_max = Math.max(curr_max, a[j]) ;
}
sum += curr_max ;
i = j - 1 ;
}
System.out.println(sum);
}
}
static boolean isSameSign(int a, int b) {
if((a < 0 && b < 0) ||(a > 0 && b > 0)) return true ;
return false ;
}
} | Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | 8a17137dc954e358184901639b565d38 | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class Sequence {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int cases = sc.nextInt();
for(int i=1; i<=cases; i++){
int elements = sc.nextInt();
int[] num = new int[elements];
long sum =0L;
for(int j=0; j<elements; j++){
num[j] = sc.nextInt();
}
int temp = num[0];
if (elements==1)
sum = temp;
else {
for (int k = 0; k < elements - 1; k++) {
if (sameSign(num[k], num[k + 1])) {
temp = findMax(temp, num[k + 1]);
if (k + 1 == elements - 1)
sum += temp;
} else {
sum += temp;
temp = num[k + 1];
if (k + 1 == elements - 1)
sum += temp;
}
}
}
System.out.println(sum);
}
}
public static int findMax(int a, int b){
return (a>=b)?a:b;
}
public static boolean sameSign(int a, int b){
return (a>0 && b>0) || (a<0 && b<0);
}
} | Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | 23e1fc50cad9cc0961db6cbcaa0a7eae | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
long n, mi = Long.MIN_VALUE, ma = 0;
int flag = 0, flag1 = 0;
n = in.nextLong();
long sum = 0, a;
for (int i = 0; i < n; ++i) {
a = in.nextInt();
if (a < 0) {
flag = 1;
if (flag1 == 1) {
sum += ma;
ma = 0;
flag1 = 0;
}
if (a > mi) {
mi = a;
}
} else {
flag1 = 1;
if (flag == 1) {
sum += mi;
mi = Long.MIN_VALUE;
flag = 0;
}
if (a > ma)
ma = a;
}
}
if (flag == 1)
sum += mi;
if (flag1 == 1)
sum += ma;
System.out.println(sum);
}
}
} | Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | 991471350f5f4bd49d43d857cf1bea6e | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class AlternatingSubsequence {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while (t-- > 0) {
int n = scn.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = scn.nextInt();
}
ArrayList<Long> vals = new ArrayList<>();
long maxSoFar = arr[0];
for(int i = 1; i < n; i++) {
if((arr[i]/Math.abs(arr[i]))*-1 == arr[i-1]/Math.abs(arr[i-1])) {
vals.add(maxSoFar);
maxSoFar = arr[i];
} else {
maxSoFar = Math.max(arr[i], maxSoFar);
}
}
vals.add(maxSoFar);
long result = 0;
for(long x: vals) result+=x;
System.out.println(result);
}
}
}
| Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | 1a55380831a0dfee37c68c95089aaaaf | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution {
public static void main(String args[])
{
FastReader sc=new FastReader();
int t=sc.nextInt();
int i=0,j=0,k=0;
for (int testcase=0;testcase<t;testcase++)
{
long n=sc.nextInt();
long arr[]=new long[(int) n];
for (i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
i=0;
long temp=arr[0];
long sum=0;
while (i<n)
{
if (temp<0)
{
long max=Integer.MIN_VALUE;
while (temp<0 && i<arr.length)
{
if (max<temp)
{
max=temp;
}
i++;
if (i<arr.length)
temp=arr[i];
}
sum+=max;
//System.out.println(max);
//System.out.println(sum);
}
else
{
long max=Integer.MIN_VALUE;
while (temp>0 && i<arr.length)
{
if (max<temp)
{
max=temp;
}
i++;
if (i<arr.length)
temp=arr[i];
}
//System.out.println(max);
sum+=max;
//System.out.println(sum);
}
}
System.out.println(sum);
}
}
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 | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | 9d99b32df26cf5317d4216d98d203784 | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.StringTokenizer;
public class Main
{
static int MAXN=201;
static int spf[] = new int[MAXN];
static int ans[]=new int[MAXN];
static int id[]=new int[MAXN];
static int sz[]=new int[MAXN];
static HashSet s=new HashSet();
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static void sieves()
{
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 (long i=3; i*i<MAXN; i++)
{
if (spf[(int)i] == i)
{
for (long j=i*i; j<MAXN; j+=i)
if (spf[(int)j]==j)
spf[(int)j] = (int)i;
}
}
}
static void getFactorizations(int x)
{
Vector<Integer> ret = new Vector<>();
HashSet w=new HashSet();
while (x != 1 && spf[x]!=1)
{
if(!w.contains(spf[x]) && spf[x]!=1)
{
w.add(spf[x]);
ans[spf[x]]++;
}
x = x / spf[x];
}
}
public static void main(String[] args)
{
FastReader sc=new FastReader();
int T=sc.nextInt();
while(T-->0){
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int pos=0,neg=0;
long sum=Long.MIN_VALUE;
int maxx=Integer.MIN_VALUE;
int tr=0;
int i=0;
if(a[0]<0){
while(i<n){
if(a[i]<0){
maxx=Math.max(maxx,a[i]);
i++;tr=1;neg=1;
}
else if(a[i]>0)break;
}}else{
if(tr==0){
while(i<n){
if(a[i]>0){
maxx=Math.max(maxx,a[i]);
i++;tr=1;pos=1;
}
else if(a[i]<0)break;
}
}
}
sum=Math.max(sum,maxx);
for(;i<n;){
long max=Long.MIN_VALUE;
if(pos<neg){
tr=0;
while(i<n){
if(a[i]>0){
max=Math.max(max,a[i]);
i++;tr=1;
}
else if(a[i]<0){break;}
}
if(tr==1){
pos++;
sum+=max;
}else break;
}else if(pos>neg){
tr=0;
while(i<n){
if(a[i]<0){
max=Math.max(max,a[i]);
i++;tr=1;
}
else if(a[i]>0){break;}
}
if(tr==1){
neg++;
sum+=max;
}else break;
}else{
tr=0;
while(i<n){
if(a[i]>0){
max=Math.max(max,a[i]);
i++;tr=1;
}
else if(a[i]<0){break;}
}
if(tr==1){
pos++;
sum+=max;
}else {
tr=0;
while(i<n){
if(a[i]<0){
max=Math.max(max,a[i]);
i++;tr=1;
}
else if(a[i]>0){break;}
}
if(tr==1){
neg++;
sum+=max;
}else break;
}
}
//System.out.println(i+" "+sum+" "+max);
}
System.out.println(sum);
}
}
public static int root(int i)
{
while(i!=id[i])
i=id[i];
return i;
}
public static void union(int p,int q)
{
int i=root(p);
int j=root(q);
//System.out.println(i+" "+j);
if(sz[i]<sz[j])
{
id[i]=j;
sz[j]+=sz[i];
}
else
{
id[j]=i;
sz[i]+=sz[j];
}
//max=Math.max(max,Math.max(sz[i],sz[j]));
}
public static void sieven(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*2; i <= n; i += p)
prime[i] = false;
}
}
for(int p=2; p<=n;p++)
if(prime[p]==true)
s.add(p);
}
public static long gcd(long a,long b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
static long power(long x, long y, int p)
{
long res = 1;
x = x % p;
while (y > 0)
{
if((y & 1)==1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static class Pair
{
int x;
int y;
public Pair(int x, int y)
{
this.x = x;
this.y = y;
}
}
static class Compare
{
static void compare(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.x>p2.x)
{
return 1;
}
else if(p2.x>p1.x)
{
return -1;
}
else
{
return 0;
}
}
});
}
}
}
| Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | 0ac0867886e10ac1bd03169f2cebd85a | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | // I know stuff but probably my rating tells otherwise...
// Kaafi din baad aaye hai... Swagat nhi kariyega???
// Kya hua, code samajhne ki koshish kar rhe ho?? Mat karo,
// mujhe bhi samajh nhi aata kya likha hai
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class _1343C {
static void Mangni_ke_bail_ke_dant_na_dekhal_jye() {
t = ni();
while(t -- >0){
n = ni();
long ar[] = new long[n];
for(int i=0;i<n;i++)ar[i] = ni();
long ans = 0;
int i = 0;
while(i<n){
long temp_max = ar[i];
int j = i + 1;
while(j<n && temp_max * ar[j] > 0){
temp_max = max(temp_max, ar[j++]);
}
ans+=temp_max;
i = j;
}
pl(ans);
}
}
//----------------------------------------The main code ends here------------------------------------------------------
/*-------------------------------------------------------------------------------------------------------------------*/
//-----------------------------------------Rest's all dust-------------------------------------------------------------
static int mod9 = 1_000_000_007;
static int n, m, l, k, t;
static AwesomeInput input;
static PrintWriter pw;
static long power(long a, long b) {
long x = max(a, b);
if (b == 0) return 1;
if ((b & 1) == 1) return a * power(a * a, b >> 1);
return power(a * a, b >> 1);
}
// The Awesome Input Code is a fast IO method //
static class AwesomeInput {
private InputStream letsDoIT;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private AwesomeInput(InputStream incoming) {
this.letsDoIT = incoming;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = letsDoIT.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
private long ForLong() {
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;
}
private String ForString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
boolean isSpaceChar(int ch);
}
}
// functions to take input//
static int ni() {
return (int) input.ForLong();
}
static String ns() {
return input.ForString();
}
static long nl() {
return input.ForLong();
}
//functions to give output
static void pl() {
pw.println();
}
static void p(Object o) {
pw.print(o + " ");
}
static void pws(Object o) {
pw.print(o + "");
}
static void pl(Object o) {
pw.println(o);
}
// Fast Sort is Radix Sort
public static int[] fastSort(int[] f) {
int n = f.length;
int[] to = new int[n];
{
int[] b = new int[65537];
for (int i = 0; i < n; i++) b[1 + (f[i] & 0xffff)]++;
for (int i = 1; i <= 65536; i++) b[i] += b[i - 1];
for (int i = 0; i < n; i++) to[b[f[i] & 0xffff]++] = f[i];
int[] d = f;
f = to;
to = d;
}
{
int[] b = new int[65537];
for (int i = 0; i < n; i++) b[1 + (f[i] >>> 16)]++;
for (int i = 1; i <= 65536; i++) b[i] += b[i - 1];
for (int i = 0; i < n; i++) to[b[f[i] >>> 16]++] = f[i];
int[] d = f;
f = to;
to = d;
}
return f;
}
public static void main(String[] args) { //threading has been used to increase the stack size.
try {
input = new AwesomeInput(System.in);
pw = new PrintWriter(System.out, true);
input = new AwesomeInput(new FileInputStream("/home/saurabh/Desktop/input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("/home/saurabh/Desktop/output.txt")), true);
} catch (Exception e) {
}
new Thread(null, null, "AApan_gand_hawai_dusar_ke_kare_dawai", 1 << 25) //the last parameter is stack size desired.
{
public void run() {
try {
double s = System.currentTimeMillis();
Mangni_ke_bail_ke_dant_na_dekhal_jye();
//System.out.println(("\nExecution Time : " + ((double) System.currentTimeMillis() - s) / 1000) + " s");
pw.flush();
pw.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}.start();
}
} | Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | 1531bee46a4af75ff8c2ad007329172b | train_002.jsonl | 1587479700 | Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases. | 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 Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t-- >0){
long sum=0;
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];
String s[]=br.readLine().split(" ");
for(int i=0;i<n;i+=1){
a[i]=Integer.parseInt(s[i]);
//System.out.print(a[i]+" ");
}
for(int i=0;i<n;){
int max=Integer.MIN_VALUE;
if(a[i]<0){
while(i<n && a[i]<0){
//System.out.println(a[i]);
if(a[i]>max){
max=a[i];
}
i++;
}
sum+=max;
}
else{
while(i<n && a[i]>0){
if(a[i]>max){
max=a[i];
}
i++;
}
sum+=max;
//System.out.println(sum);
}
}
System.out.println(sum);
}
}
} | Java | ["4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000"] | 1 second | ["2\n-1\n6\n-2999999997"] | NoteIn the first test case of the example, one of the possible answers is $$$[1, 2, \underline{3}, \underline{-1}, -2]$$$.In the second test case of the example, one of the possible answers is $$$[-1, -2, \underline{-1}, -3]$$$.In the third test case of the example, one of the possible answers is $$$[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$$$.In the fourth test case of the example, one of the possible answers is $$$[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$$$. | Java 11 | standard input | [
"dp",
"two pointers",
"greedy"
] | 39480cdf697fc9743dc9665f989077d7 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9, a_i \ne 0$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,200 | For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $$$a$$$. | standard output | |
PASSED | c66027b95d77e9b5d7e7a0dbb0915ead | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
import java.io.*;
public class a {
public static void main(String[] arg) throws IOException {
new a();
}
public a() throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int[] vs = new int[n];
for(int i = 0; i < n; i++) vs[i] = in.nextInt();
int max = vs[0];
for(int i = 1; i < n; i++) max = Math.max(max, vs[i]);
int ans = 0;
for(int i : vs) ans += max-i;
out.println(ans);
in.close(); out.close();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
st = new StringTokenizer("");
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public String next() throws IOException {
while(!st.hasMoreElements()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public void close() throws IOException {
br.close();
}
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 25a138c0fdd2618f9481c6b023f26d2b | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
public class HolidayOfEquality {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
long noOfCitizens = sc.nextLong();
long max = 0;
long ans = 0;
long arr[] = new long[(int) noOfCitizens];
for(int i = 0 ;i <noOfCitizens ;i++)
arr[i] = sc.nextLong();
Arrays.sort(arr);
max = arr[(int) (noOfCitizens-1)];
for(int i = 0 ;i <noOfCitizens-1;i++)
ans += Math.abs(max-arr[i]);
System.out.print(ans);
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | e1ca9a3e4c08aea645e24039dd93d3d7 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes |
//package codeforces;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int [] in = new int [n];
int max = in[0];
int min = in[0];
int d=0;
int sum = 0;
for (int y = 0 ; y < n ; y++)
{
in[y]= input.nextInt();
}
if (in.length < 2)
{
System.out.println(Math.abs(max-min));
}
else
{
for (int i = 0 ;i<in.length ; i++)
{
if (in[i]>= max)
{
max = in[i];
}
else
{
min = in[i];
}
}
for (int i = 0 ; i < in.length ; i ++)
{
sum = sum + (max - in[i]);
}
System.out.println(sum);
}
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 614b143186898494e7b48a9882890a44 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes |
import java.util.*;
import java.io.*;
public class A {
public static void main(String arg[]) {
FastScanner sc = new FastScanner(System.in);
int n = sc.nextInt();
int x [] = new int [n];
int sum= 0,o=0;
while (n-->0){
x[o++] = sc.nextInt();
}
int max = -1;
for(int i =0;i<o;++i){
if(x[i]>max){
max = x[i];
}
}
for(int i =0;i<x.length;++i){
sum+=max-x[i];
}
System.out.println(sum);
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.valueOf(next());
}
double nextd() {
return Double.valueOf(next());
}
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 46397e8f68731f10b4c1911a226064c6 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes |
import java.util.*;
import java.io.*;
public class A {
public static void main(String arg[]) {
FastScanner sc = new FastScanner(System.in);
int n = sc.nextInt();
int x [] = new int [n];
int sum= 0,o=0;
while (n-->0){
x[o++] = sc.nextInt();
}
for(int i =0;i<o;++i){
if(x[i]>n){
n = x[i];
}
}
for(int i =0;i<x.length;++i){
sum+=n-x[i];
}
System.out.println(sum);
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.valueOf(next());
}
double nextd() {
return Double.valueOf(next());
}
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 8fcdddffb6415fe82248c641a17997af | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes |
import java.util.*;
import java.io.*;
public class A {
public static void main(String arg[]) {
FastScanner sc = new FastScanner(System.in);
int n = sc.nextInt();
int x [] = new int [n];
int sum= 0,o=0;
while (n-->0){
x[o++] = sc.nextInt();
}
Arrays.sort(x);
for(int i =0;i<x.length-1;++i){
sum+=x[o-1]-x[i];
}
System.out.println(sum);
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.valueOf(next());
}
double nextd() {
return Double.valueOf(next());
}
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | ae895df54f63eafe77a4040e248583d4 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String n1 = input.nextLine();
String num = input.nextLine();
int n=Integer.parseInt(n1);
if (n==0){
System.out.println("0");
}
String a[] = num.split(" ");
int arr[] = new int[a.length];
for (int i = 0; i <a.length ; i++) {
arr[i] = Integer.parseInt(a[i]);
}
Arrays.sort(arr);
int result = 0;
for (int i = 0; i <arr.length ; i++) {
result = result + arr[arr.length-1] - arr[i];
}
System.out.println(result);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 0380b101112f10b2087f6dbaf62d887d | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Question758A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] arr = new long[n];
long max=0;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
max=Math.max(max,arr[i]);
}
long count=0;
for (int i = 0; i <arr.length; i++) {
count+=max-arr[i];
}
System.out.println(count);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 48888aa23d9c6b7be85a61fe8efc97e7 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Question758A {
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 count=0;
for (int i = 0; i <arr.length-1; i++) {
count+=arr[arr.length-1]-arr[i];
}
System.out.println(count);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 9e4fce132706a15fb90b7c7a147b6638 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Arrays;
import java.util.Scanner;
public class TestApplication {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] anArray = new int[n];
int maxValue = Integer.MIN_VALUE;
//input array and caluclate max value
for (int i = 0; i < n; i++)
anArray[i] = scanner.nextInt();
Arrays.sort(anArray);
maxValue = anArray[n - 1];
//counter
int counter = 0;
for (int x : anArray)
counter += maxValue - x;
System.out.println(counter);
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 250fda88c4bba204ae51072aa71c6735 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
public class TestApplication {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] anArray = new int[n];
int maxValue = Integer.MIN_VALUE;
//input array and caluclate max value
for (int i = 0; i < n; i++) {
int a = scanner.nextInt();
if (a > maxValue) {
maxValue = a;
}
anArray[i] = a;
}
//counter
int counter = 0;
for (int x : anArray)
counter += maxValue - x;
System.out.println(counter);
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | f8f9badfc177b1a53d4d34ca5f6de059 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n,i,max;
n = in.nextInt();
int arr[]= new int[n],sum = 0;
for (i=0; i<n; i++)
arr[i] = in.nextInt();
Arrays.sort(arr);
max = arr[n-1];
for (i=0; i<n-1; i++)
sum += Math.abs(max - arr[i]);
System.out.print(sum);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 6fc2348c059af3b1db1b57aa5e5f1785 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n,i,max;
n = in.nextInt();
int arr[]= new int[n],sum = 0;
for (i=0; i<n; i++)
arr[i] = in.nextInt();
Arrays.sort(arr);
max = arr[n-1];
for (i=0; i<n-1; i++)
sum += Math.abs(max - arr[i]);
System.out.print(sum);
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 7436f6ea0b1178eac55d0b2c7fa71478 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
import java.io.*;
public class Holiday{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int numofloop = sc.nextInt();
int[] arrnum = new int[numofloop];
int i=0,j=0,sum=0;
sc.nextLine();
while(numofloop>0){
arrnum[i] = sc.nextInt();
i++;
numofloop--;
}
Arrays.sort(arrnum);
while(arrnum[j]!=arrnum[i-1]){
sum = sum + (arrnum[i-1]-arrnum[j]);
j++;
}
System.out.println(sum);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 32918b6c0cf27907e132ad5e07c09117 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
public class HolidayOfEquality {
public static void main(String[] args) {
Scanner in= new Scanner(System.in);
int n,sum=0,max,max_sum;
n=in.nextInt();
int a[]=new int[n];
for(int i=0; i < n ; i++)
{
a[i]=in.nextInt();
sum+=a[i];
}
Arrays.sort(a);
max = a[n-1]; max_sum = max*n;
System.out.println(max_sum-sum);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 7e9463f35451326d8ca96e45b82692fd | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
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();
int max=arr[0];
for(int i=1;i<n;i++){
if(arr[i]>max)
max=arr[i];
}
int c=0;
for(int i=0;i<n;i++)
c+=(max-arr[i]);
System.out.println(c);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 4be6f3e7462a94bc38db699e68814321 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
int max=a[0];
for(int i=1;i<n;i++){
if(a[i]>max)
max=a[i];
}
int c=0;
for(int i=0;i<n;i++)
c+=(max-a[i]);
System.out.println(c);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 672e9caba7cf2941d7743704f1d0c100 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes |
import java.util.Scanner;
public class Pockets {
public static void main(String[] args) {
int n = 0;
int []a = new int[101];
int max = 0;
int total = 0;
int i;
Scanner in = new Scanner(System. in);
n = in.nextInt();
for (i = 0; i < n; i++) {
a[i] = in.nextInt();
}
for(i = 0; i < n; i++) {
if (a[i] > max) {
max = a[i];
}
}
for (i = 0; i < n; i++) {
total = total + max - a[i];
}
System.out.println(total);
}} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | a534dc226d6ce7ad6ad676aa60104575 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.Scanner;
import java.util.Arrays;
public class HolidayOfEquality{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int numOfCitizens = Integer.parseInt(input.nextLine().trim());
String[] parts = input.nextLine().split("\\s+");
int[] welfare = new int[parts.length];
for(int i = 0; i < parts.length; i++){
welfare[i] = Integer.parseInt(parts[i]);
}
Arrays.sort(welfare);
int max = welfare[parts.length - 1];
int sum = 0;
for(int i = 0; i < parts.length; i++){
if(max > welfare[i]){
sum += (max - welfare[i]);
}
}
System.out.print(sum);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 97109c9cc23f8940a3e4ae49c7a64f17 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.io.*;
import java.util.*;
public class soultion
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int i,max=0,cnt=0;
int[] a=new int[n];
for(i=0;i<n;i++)
a[i]=s.nextInt();
for(i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
}
for(i=0;i<n;i++)
{
cnt+=max-a[i];
}
System.out.println(cnt);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | e8711e91ab713a84cc2ca629d77e2d6c | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int[] a=new int[n];
int sum=0,i,max=a[0];
for(i=0;i<n;i++)
a[i]=s.nextInt();
for(i=0;i<n;i++)
if(max<a[i])
max=a[i];
for(i=0;i<n;i++)
{
if(max>a[i])
{
sum+=max-a[i];
}
}
System.out.println(sum);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | fd3522789c7f17de00d8643603244ad5 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
public class HolidayOfEquality {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int all[] = new int[n];
int sum = 0;
for(int i = 0 ; i<n; i++) {
all[i]=sc.nextInt();
}
Arrays.sort(all); //�Ѥp��j
// System.out.println(all[0]);
for(int i = n-1 ; i>=0; i--) {
sum+=all[n-1]-all[i];
}
System.out.println(sum);
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 55369a963307f9e5ead9679d4987b5bc | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
public class HolidayOfEquality {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int all[]= new int[n];
int sum = 0;
int max = -1;
for(int i =0 ; i<n; i++) {
int input = sc.nextInt();
if(input>max) {
max = input;
}
sum+=input;
}
System.out.println(max*n-sum);
/* int n = sc.nextInt();
int all[] = new int[n];
int sum = 0;
for(int i = 0 ; i<n; i++) {
all[i]=sc.nextInt();
}
Arrays.sort(all); //�Ѥp��j
// System.out.println(all[0]);
for(int i = n-1 ; i>=0; i--) {
sum+=all[n-1]-all[i];
}
System.out.println(sum);*/
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 2006f405fa9c88f3aec444bde2eb89a0 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int[] a =new int[105];
int ans=0,aim=0;
for(int i=0;i<n;i++){
a[i]=cin.nextInt();
if(aim<a[i]) aim=a[i];
}
for(int i=0;i<n;i++){
ans+=aim-a[i];
}
System.out.println(ans);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | bbc3ccc7ecca324d0f608b153f8845b9 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
public class prob{
public static void main(String args[]){
Scanner in =new Scanner(System.in);
int n=in.nextInt();
int a[]=new int[n];
int max=-1;
for(int k=0;k<n;k++){
a[k]=in.nextInt();
if(a[k]>max) max=a[k];
}
int count=0;
for(int k=0;k<n;k++){
count+=max-a[k];
}
System.out.println(count);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 6b6a37b3d7c87a4d04b150285c4ee8e9 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
// Your code here!
BufferedReader ac = new BufferedReader(new InputStreamReader(System.in));
String b = ac.readLine();
int len = Integer.parseInt(b);
String c[] = new String[len];
if (len > 100 | len < 1){
System.exit(0);
}else{
String a = ac.readLine();
c = a.split(" ");
}
int d[] = new int[c.length];
for(int k = 0; k<c.length;k++){
d[k] = Integer.parseInt(c[k]);
}
Arrays.sort(d);
int last =d[d.length-1];
int res = 0;
for(int j = 0; j<d.length;j++){
int ini = d[j];
res = res + (last-ini);
}
System.out.println(res);
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 0209d97d7a71f359d51e4dd1eba91522 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | /* package whatever; // 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 Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
Arrays.sort(a);
long sum=0;
for(int i=0;i<n-1;i++)
sum=sum+(a[n-1]-a[i]);
System.out.println(sum);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | bae94526984e68ac344ad55e6964c322 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
public class Train {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int n= kb.nextInt();
int[] nm = new int[n];
for(int i=0;i<n;++i){
int l=kb.nextInt();
nm[i]=l;
}
Arrays.sort(nm);
int res=0;
for(int i=0;i<n-1;i++){
res+=(nm[n-1]-nm[i]);
}
System.out.println(res);
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 3b9da86d9dccdbcdbc919066cde9ac00 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
public class abcd {
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] inp = new int[N];
for (int i = 0;i<N;i++){
inp[i] = sc.nextInt();
}
int m = max(inp);
int s = 0;
for (int i = 0;i<N;i++){
int a = m - inp[i];
s=s+a;
}
System.out.println(s);
}
public static int max(int[] A){
for (int i = 0; i<A.length-1;i++){
if (A[i]>A[i+1]){
int x = A[i];
int y = A[i+1];
A[i+1] = x;
A[i] = y;
}
}
return A[A.length - 1];
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 0fdd04691d75ea2ec874615ea4b09e39 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.Objects;
import java.util.Scanner;
public class HelloWorld
{
public static void main(String[] args)
{
Scanner in = new Scanner (System.in);
int n= in.nextInt(),k=0,m=0;
int [] money=new int [n];
for (int i =0;i<n;i++)
{
Scanner in2 = new Scanner (System.in);
money [i]= in.nextInt();
}
for (int i =0;i<n;i++)
{
k=Math.max(k, money [i]);
}
for (int i =n-1;i>-1;i--)
{
m=m+ k-money[i];
}
if (n!=1){
System.out.println(m);
}
else {
System.out.println(0);
}
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 2fc18ec4d4edbc8ca5a6495fcb67b690 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
public class holidayOfEquality {
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();
}
if(arr.length==1){
System.out.println("0");
}
else
{
int max=arr[0];
for(int i=1;i<n;i++){
if(arr[i]>max){
max=arr[i];
}
}
int total=0;
for(int i=0;i<n;i++){
total+=max-arr[i];
}
System.out.println(total);
}
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 46e1ed7b6023a8b67bca67d262edd0c8 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author Elizabeth Gonzalez
*/
public class Main {
public static void main(String[] args) {
Scanner leer=new Scanner(new BufferedInputStream(System.in));
PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out),true);
int n=leer.nextInt();
int citizens[]=new int[n];
for (int i = 0; i < n; i++) {
citizens[i]=leer.nextInt();
}
Arrays.sort(citizens);
int mayor=citizens[citizens.length-1];
int total=0;
for (int i = 0; i < n-1; i++) {
total+=(mayor-citizens[i]);
}
out.println(total);
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 9e0f72792736d60369f919ca70d3a72a | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.io.PrintWriter;
import static java.lang.System.out;
import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;
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.ArrayList;
import java.util.StringTokenizer;
/**
*
* @author fatsu
*/
public class JavaApplication1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Random rand = new Random();
Scanner read = new Scanner(System.in);
int citizens = read.nextInt();
ArrayList<Integer> wealth = new ArrayList<Integer>();
int max = 0;
for(int n = citizens; n>0; n--){
int add = read.nextInt();
if(add > max){
max = add;
}
wealth.add(add);
}
int sum = 0;
for(int kek : wealth){
if(kek < max){
sum += max-kek;
}
}
//out.print(sum);
System.out.println(sum);
out.close();
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 4d289b90ec6915f5cf82ce7fd665a5b2 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.Scanner;
public class HolidayOfEquality {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
int max=0;
int total=0;
for(int i=0; i<n; i++){
a[i]=sc.nextInt();
}
max=a[0];
for(int i=0;i<n;i++){
if(a[i]>max)
max=a[i];
}
for(int i=0;i<n;i++){
total=total+Math.abs(max-a[i]);
}
System.out.println(total);
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 64b97a2399699114eaf5f57e2fdfae74 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.Scanner;
public class Class1{
public static void main(String[] args){
int n;
Scanner in= new Scanner(System.in);
n =in.nextInt();
int[] a = new int[1000000];
int i;
for(i=0;i<n;i++)
a[i]=in.nextInt();
int l=a[0];
for(i=0;i<n;i++){
if(a[i]>l)
l=a[i];
}
for(i=0;i<n;i++){
if(a[i]<=l)
a[i]=l-a[i];
}
int count=0;
for(i=0;i<n;i++){
count=count+a[i];
}
System.out.print(count);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | d3406623afca059ead116155bd49d4e4 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
public class main1
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int no = in.nextInt();
int[] arr = new int[no];
int max = 0, counter = 0;
for (int i = 0; i < no; i++) {
arr[i] = in.nextInt();
if (arr[i] > max) {
max = arr[i];
}
}
for (int i = 0; i < no; i++) {
if (arr[i] < max) {
counter += max - arr[i];
}
}
System.out.println(counter);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 5918ca7fb7eaa62f4edca36cd74df7bc | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int no=in.nextInt();
int arr[]=new int[no];
for(int i=0;i<no;i++)
arr[i]=in.nextInt();
Arrays.sort(arr);
int max=arr[no-1];
int count=0;
for(int j=0;j<no;j++)
{
count=count+(max-arr[j]);
}
System.out.println(count);
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 9bd1697cd31d52f0f76d4d2acd732121 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes |
/*
* File: Breakout.java
* -------------------
* Name:
* Section Leader:
*
* This file will eventually implement the game of Breakout.
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;
public class BlankClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int max = 0;
int count = 0;
int n = sc.nextInt();
int mas[] = new int[n];
for (int i = 0; i < n; i++) {
mas[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
if (mas[i] > max) {
max = mas[i];
}
}
for (int i = 0; i < n; i++) {
count += max - mas[i];
}
System.out.println(count);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | fd6b90816d23df267d58ecc3bb1d888e | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, Scanner in, PrintWriter out) {
long n = in.nextInt(), h = 0, ans = 0;
long[] t = new long[(int) n];
for (int i = 0; i < n; i++) {
t[i] = in.nextInt();
if (t[i] > h) h = t[i];
}
if (n == 1) {
out.println(0);
return;
}
for (int i = t.length - 1; i >= 0; i--) {
ans += h - t[i];
}
out.println(ans);
}
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 8040a1e5a9fbab8faf09cf88e8fdf1e9 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
import java.io.*;
public class burles
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
int maxx = Integer.MIN_VALUE;
int i;
for(i=0;i<n;i++)
{
arr[i] = in.nextInt();
maxx = Math.max(maxx, arr[i]);
}
//System.out.println(maxx);
int sum = 0;
for(i=0;i<n;i++)
sum =sum + (maxx - arr[i]);
System.out.println(sum);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | dc0a838d55fe54f8dd27128fc4ddd326 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.*;
import java.io.*;
public class burles
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
int maxx = Integer.MIN_VALUE;
for(int i=0;i<n;i++)
{
arr[i] = in.nextInt();
maxx = Math.max(maxx, arr[i]);
}
//System.out.println(maxx);
int sum = 0;
for(int i=0;i<n;i++)
sum =sum + (maxx - arr[i]);
System.out.println(sum);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 04f45baf61ef29d0e43cf764b3fc6615 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes |
import java.util.Scanner;
public class BMain{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int count = s.nextInt();
int max = s.nextInt();
int sum = max;
for (int i =0 ;i< count-1 ;i++){
int n = s.nextInt();
sum += n;
if (n>max){
max = n;
}
}
System.out.print(count * max - sum);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 7a6733d9f2576d681a03acfc155af0fe | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input= new Scanner(System.in);
int n = input.nextInt();
int a[]= new int[n];
int max=-1;
for(int i=0;i<n;i++){
a[i]=input.nextInt();
max=Math.max(max, a[i]);
}
int answer=0;
if(n==0){
System.out.println(0);
}else{
for(int i=0;i<n;i++){
answer+=max-a[i];
}
System.out.println(answer);
}
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 24211ebf27905fe4b9486826ef8f6471 | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++) {
arr[i]=input.nextInt();
}
int max=arr[0];
int min=arr[0];
int sum=0;
for(int i=0;i<n;i++) {
if(arr[i]>max) {
max=arr[i];
}
}
for(int j=0;j<n;j++) {
sum=sum+max-arr[j];
}
System.out.print(sum);
}
} | Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 44ff2d6daa9da3d4e6b867a4029510dc | train_002.jsonl | 1484838300 | In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Codeforces392a {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int people = in.nextInt();
int ar[] = new int[people];
for(int i=0; people>i;i++){
ar[i]=in.nextInt();
}
Arrays.sort(ar);
int sum=0;
for(int i=0; people>i;i++){
sum += ar[people-1]-ar[i];
}
System.out.println(sum);
in.close();
}
}
| Java | ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"] | 1 second | ["10", "1", "4", "0"] | NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles. | Java 8 | standard input | [
"implementation",
"math"
] | a5d3c9ea1c9affb0359d81dae4ecd7c8 | The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen. | 800 | In the only line print the integer S — the minimum number of burles which are had to spend. | standard output | |
PASSED | 4f63cd1f3a88f889f3879a634e4f0ef4 | train_002.jsonl | 1350370800 | There are two decks of cards lying on the table in front of you, some cards in these decks lay face up, some of them lay face down. You want to merge them into one deck in which each card is face down. You're going to do it in two stages.The first stage is to merge the two decks in such a way that the relative order of the cards from the same deck doesn't change. That is, for any two different cards i and j in one deck, if card i lies above card j, then after the merge card i must also be above card j.The second stage is performed on the deck that resulted from the first stage. At this stage, the executed operation is the turning operation. In one turn you can take a few of the top cards, turn all of them, and put them back. Thus, each of the taken cards gets turned and the order of these cards is reversed. That is, the card that was on the bottom before the turn, will be on top after it.Your task is to make sure that all the cards are lying face down. Find such an order of merging cards in the first stage and the sequence of turning operations in the second stage, that make all the cards lie face down, and the number of turns is minimum. | 256 megabytes | import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class H {
public static ArrayList<Integer> solve(int[] ar) {
ArrayList<Integer> res = new ArrayList<Integer>();
for (int i = 0; i < ar.length; i++) {
int j = i;
for (; j < ar.length && ar[j] == ar[i]; j++)
;
i = j - 1;
if (j == ar.length && ar[i] == 0)
break;
res.add((i + 1));
}
return res;
}
public static void main(String[] args) throws IOException {
// Scanner sc = new Scanner(System.in);
Scanner sc = new Scanner(new FileReader("input.txt"));
FileWriter fw = new FileWriter("output.txt");
int len1 = sc.nextInt();
int[] ar1 = new int[len1];
for (int i = 0; i < len1; i++)
ar1[i] = sc.nextInt();
int len2 = sc.nextInt();
int[] ar2 = new int[len2];
for (int i = 0; i < len2; i++)
ar2[i] = sc.nextInt();
int[] pos1 = new int[len1 + len2];
int[] res1 = new int[len1 + len2];
int[] pos2 = new int[len1 + len2];
int[] res2 = new int[len1 + len2];
int idx1 = 1, idx2 = 0;
pos1[0] = ar1[0];
res1[0] = 1;
int pos1Idx = 1;
int cur = ar1[0];
while (pos1Idx < pos1.length) {
for (; idx1 < ar1.length && ar1[idx1] == cur; idx1++) {
pos1[pos1Idx] = cur;
res1[pos1Idx++] = idx1 + 1;
}
for (; idx2 < ar2.length && ar2[idx2] == cur; idx2++) {
pos1[pos1Idx] = cur;
res1[pos1Idx++] = idx2 + len1 + 1;
}
cur = 1 - cur;
}
idx1 = 0;
idx2 = 1;
pos2[0] = ar2[0];
res2[0] = len1 + 1;
int pos2Idx = 1;
cur = ar2[0];
while (pos2Idx < pos2.length) {
for (; idx2 < ar2.length && ar2[idx2] == cur; idx2++) {
pos2[pos2Idx] = cur;
res2[pos2Idx++] = idx2 + len1 + 1;
}
for (; idx1 < ar1.length && ar1[idx1] == cur; idx1++) {
pos2[pos2Idx] = cur;
res2[pos2Idx++] = idx1 + 1;
}
cur = 1 - cur;
}
ArrayList<Integer> tmp1 = solve(pos1);
ArrayList<Integer> tmp2 = solve(pos2);
if (tmp1.size() < tmp2.size()) {
for (int i = 0; i < pos1.length; i++) {
System.out.print(res1[i] + " ");
fw.write(res1[i] + " ");
}
System.out.println();
fw.write("\n");
System.out.println(tmp1.size());
fw.write(tmp1.size() + "\n");
for (Integer ii : tmp1) {
System.out.print(ii + " ");
fw.write(ii + " ");
}
System.out.println();
fw.write("\n");
} else {
for (int i = 0; i < pos2.length; i++) {
System.out.print(res2[i] + " ");
fw.write(res2[i] + " ");
}
System.out.println();
fw.write("\n");
System.out.println(tmp2.size());
fw.write(tmp2.size() + "\n");
for (Integer ii : tmp2) {
System.out.print(ii + " ");
fw.write(ii + " ");
}
System.out.println();
fw.write("\n");
}
fw.flush();
fw.close();
sc.close();
}
}
| Java | ["3\n1 0 1\n4\n1 1 1 1", "5\n1 1 1 1 1\n5\n0 1 0 1 0"] | 2 seconds | ["1 4 5 6 7 2 3\n3\n5 6 7", "6 1 2 3 4 5 7 8 9 10\n4\n1 7 8 9"] | null | Java 7 | input.txt | [
"constructive algorithms",
"greedy"
] | f1a312a21d600cf9cfff4afeca9097dc | The first input line contains a single integer n — the number of cards in the first deck (1 ≤ n ≤ 105). The second input line contains n integers, separated by single spaces a1, a2, ..., an (0 ≤ ai ≤ 1). Value ai equals 0, if the i-th card is lying face down, and 1, if the card is lying face up. The cards are given in the order from the topmost one to the bottommost one. The third input line contains integer m — the number of cards in the second deck (1 ≤ m ≤ 105). The fourth input line contains m integers, separated by single spaces b1, b2, ..., bm (0 ≤ bi ≤ 1). Value bi equals 0, if the i-th card is lying face down, and 1, if the card is lying face up. The cards are given in the order from the topmost to the bottommost. | 2,000 | In the first line print n + m space-separated integers — the numbers of the cards in the order, in which they will lie after the first stage. List the cards from top to bottom. The cards from the first deck should match their indexes from 1 to n in the order from top to bottom. The cards from the second deck should match their indexes, increased by n, that is, numbers from n + 1 to n + m in the order from top to bottom. In the second line print a single integer x — the minimum number of turn operations you need to make all cards in the deck lie face down. In the third line print x integers: c1, c2, ..., cx (1 ≤ ci ≤ n + m), each of them represents the number of cards to take from the top of the deck to perform a turn operation. Print the operations in the order, in which they should be performed. If there are multiple optimal solutions, print any of them. It is guaranteed that the minimum number of operations doesn't exceed 6·105. | output.txt | |
PASSED | 54ae7fe4c68732fafb1824541a0c7a17 | train_002.jsonl | 1350370800 | There are two decks of cards lying on the table in front of you, some cards in these decks lay face up, some of them lay face down. You want to merge them into one deck in which each card is face down. You're going to do it in two stages.The first stage is to merge the two decks in such a way that the relative order of the cards from the same deck doesn't change. That is, for any two different cards i and j in one deck, if card i lies above card j, then after the merge card i must also be above card j.The second stage is performed on the deck that resulted from the first stage. At this stage, the executed operation is the turning operation. In one turn you can take a few of the top cards, turn all of them, and put them back. Thus, each of the taken cards gets turned and the order of these cards is reversed. That is, the card that was on the bottom before the turn, will be on top after it.Your task is to make sure that all the cards are lying face down. Find such an order of merging cards in the first stage and the sequence of turning operations in the second stage, that make all the cards lie face down, and the number of turns is minimum. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class H implements Runnable {
private InputReader in;
private PrintWriter out;
public static void main(String[] args) {
new H().run();
}
public H() {
//in = new InputReader(System.in); out = new PrintWriter(System.out);
try {in = new InputReader(new FileInputStream("input.txt")); out = new PrintWriter(new FileOutputStream("output.txt"));}
catch (Exception e) {}
}
int[] order;
int[] lengths;
int solve(int[] d1, int[] d2, int initial) {
int n = d1.length;
int m = d2.length;
int oi = 0;
order = new int[n+m];
lengths = new int[n+m];
int current = initial;
int flips = 0;
int a = 0;
int b = 0;
while (a < n || b < m) {
if (a < n && d1[a] == current) {
order[a+b] = a+1;
a += 1;
}
else if (b < m && d2[b] == current) {
order[a+b] = n+b+1;
b += 1;
}
else {
current = 1-current;
lengths[flips++] = a+b;
}
}
if (current == 1) {
lengths[flips++] = n+m;
}
return flips;
}
public void run() {
int n = in.readInt();
int[] d1 = new int[n];
for (int i = 0; i < n; i++)
d1[i] = in.readInt();
int m = in.readInt();
int[] d2 = new int[m];
for (int i = 0; i < m; i++)
d2[i] = in.readInt();
int s1 = solve(d1, d2, 0);
int s2 = solve(d1, d2, 1);
if (s1 < s2) {
solve(d1, d2, 0);
}
else {
solve(d1, d2, 1);
}
for (int i = 0; i < n+m; i++)
out.print(order[i]+" ");
out.println();
out.println(Math.min(s1,s2));
for (int i = 0; i < Math.min(s1,s2); i++)
out.print(lengths[i]+" ");
out.println();
out.close();
}
private static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1000];
private int curChar, numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public BigInteger readBigInteger() {
try {
return new BigInteger(readString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
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 < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
}
}
| Java | ["3\n1 0 1\n4\n1 1 1 1", "5\n1 1 1 1 1\n5\n0 1 0 1 0"] | 2 seconds | ["1 4 5 6 7 2 3\n3\n5 6 7", "6 1 2 3 4 5 7 8 9 10\n4\n1 7 8 9"] | null | Java 7 | input.txt | [
"constructive algorithms",
"greedy"
] | f1a312a21d600cf9cfff4afeca9097dc | The first input line contains a single integer n — the number of cards in the first deck (1 ≤ n ≤ 105). The second input line contains n integers, separated by single spaces a1, a2, ..., an (0 ≤ ai ≤ 1). Value ai equals 0, if the i-th card is lying face down, and 1, if the card is lying face up. The cards are given in the order from the topmost one to the bottommost one. The third input line contains integer m — the number of cards in the second deck (1 ≤ m ≤ 105). The fourth input line contains m integers, separated by single spaces b1, b2, ..., bm (0 ≤ bi ≤ 1). Value bi equals 0, if the i-th card is lying face down, and 1, if the card is lying face up. The cards are given in the order from the topmost to the bottommost. | 2,000 | In the first line print n + m space-separated integers — the numbers of the cards in the order, in which they will lie after the first stage. List the cards from top to bottom. The cards from the first deck should match their indexes from 1 to n in the order from top to bottom. The cards from the second deck should match their indexes, increased by n, that is, numbers from n + 1 to n + m in the order from top to bottom. In the second line print a single integer x — the minimum number of turn operations you need to make all cards in the deck lie face down. In the third line print x integers: c1, c2, ..., cx (1 ≤ ci ≤ n + m), each of them represents the number of cards to take from the top of the deck to perform a turn operation. Print the operations in the order, in which they should be performed. If there are multiple optimal solutions, print any of them. It is guaranteed that the minimum number of operations doesn't exceed 6·105. | output.txt | |
PASSED | a73efa1b27810bebf792e4095db9cb1e | train_002.jsonl | 1350370800 | There are two decks of cards lying on the table in front of you, some cards in these decks lay face up, some of them lay face down. You want to merge them into one deck in which each card is face down. You're going to do it in two stages.The first stage is to merge the two decks in such a way that the relative order of the cards from the same deck doesn't change. That is, for any two different cards i and j in one deck, if card i lies above card j, then after the merge card i must also be above card j.The second stage is performed on the deck that resulted from the first stage. At this stage, the executed operation is the turning operation. In one turn you can take a few of the top cards, turn all of them, and put them back. Thus, each of the taken cards gets turned and the order of these cards is reversed. That is, the card that was on the bottom before the turn, will be on top after it.Your task is to make sure that all the cards are lying face down. Find such an order of merging cards in the first stage and the sequence of turning operations in the second stage, that make all the cards lie face down, and the number of turns is minimum. | 256 megabytes | import java.io.*;
import java.util.*;
public class P234H
{
public static StringTokenizer st;
public static void nextLine(BufferedReader br) throws IOException
{
st = new StringTokenizer(br.readLine());
}
public static String next()
{
return st.nextToken();
}
public static int nextInt()
{
return Integer.parseInt(st.nextToken());
}
public static long nextLong()
{
return Long.parseLong(st.nextToken());
}
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
nextLine(br);
PrintWriter pw = new PrintWriter("output.txt");
int n = nextInt();
int[] a = new int[n];
nextLine(br);
for (int i = 0; i < n; i++) a[i] = nextInt();
nextLine(br);
int m = nextInt();
int[] b = new int[m];
nextLine(br);
for (int i = 0; i < m; i++) b[i] = nextInt();
int[] ab = new int[n+m];
int pos = n+m-1;
int p1 = n-1, p2 = m-1;
int start = 0;
ArrayList<Integer> flops = new ArrayList<Integer>();
if (a[p1] == 1 && b[p2] == 1)
{
start = 1;
flops.add(n+m);
}
while (pos >= 0)
{
if (p1 >= 0 && a[p1] == start)
{
ab[pos] = p1+1;
p1--;
pos--;
}
else if (p2 >= 0 && b[p2] == start)
{
ab[pos] = p2 + n+1;
p2--;
pos--;
}
else
{
start = 1-start;
flops.add(pos+1);
}
}
StringBuffer sb = new StringBuffer();
sb.append(ab[0]);
for (int i = 1; i < n+m; i++)
{
sb.append(" ");
sb.append(ab[i]);
}
sb.append("\n");
sb.append(flops.size() + "\n");
for (int i = 0; i < flops.size(); i++)
{
if (i != 0) sb.append(" ");
sb.append(flops.get(flops.size() - i - 1));
}
sb.append("\n");
pw.print(sb);
pw.close();
}
} | Java | ["3\n1 0 1\n4\n1 1 1 1", "5\n1 1 1 1 1\n5\n0 1 0 1 0"] | 2 seconds | ["1 4 5 6 7 2 3\n3\n5 6 7", "6 1 2 3 4 5 7 8 9 10\n4\n1 7 8 9"] | null | Java 7 | input.txt | [
"constructive algorithms",
"greedy"
] | f1a312a21d600cf9cfff4afeca9097dc | The first input line contains a single integer n — the number of cards in the first deck (1 ≤ n ≤ 105). The second input line contains n integers, separated by single spaces a1, a2, ..., an (0 ≤ ai ≤ 1). Value ai equals 0, if the i-th card is lying face down, and 1, if the card is lying face up. The cards are given in the order from the topmost one to the bottommost one. The third input line contains integer m — the number of cards in the second deck (1 ≤ m ≤ 105). The fourth input line contains m integers, separated by single spaces b1, b2, ..., bm (0 ≤ bi ≤ 1). Value bi equals 0, if the i-th card is lying face down, and 1, if the card is lying face up. The cards are given in the order from the topmost to the bottommost. | 2,000 | In the first line print n + m space-separated integers — the numbers of the cards in the order, in which they will lie after the first stage. List the cards from top to bottom. The cards from the first deck should match their indexes from 1 to n in the order from top to bottom. The cards from the second deck should match their indexes, increased by n, that is, numbers from n + 1 to n + m in the order from top to bottom. In the second line print a single integer x — the minimum number of turn operations you need to make all cards in the deck lie face down. In the third line print x integers: c1, c2, ..., cx (1 ≤ ci ≤ n + m), each of them represents the number of cards to take from the top of the deck to perform a turn operation. Print the operations in the order, in which they should be performed. If there are multiple optimal solutions, print any of them. It is guaranteed that the minimum number of operations doesn't exceed 6·105. | output.txt | |
PASSED | be44fc5deeea135943b08b9dbf9019fa | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.*;
import java.lang.reflect.*;
import java.util.*;
public class C {
public C () {
int N = sc.nextInt();
int M = sc.nextInt();
Integer [][] G = new Integer [N][3]; int [] D = new int[N];
for (@SuppressWarnings("unused") int i : rep(M)) {
int x = sc.nextInt() - 1, y = sc.nextInt() - 1;
G[x][D[x]++] = y;
G[y][D[y]++] = x;
}
boolean [] R = new boolean[N];
boolean [] V = new boolean[N];
Queue<Integer> Q = new LinkedList<Integer>();
for (int i : rep(N)) {
Q.add(i);
V[i] = true;
}
while(!Q.isEmpty()) {
int U = Q.poll();
V[U] = false;
int Z = 0, W = -1;
for (int j : rep(D[U]))
if (R[U] == R[G[U][j]])
++Z;
else
W = G[U][j];
if (Z > 1) {
R[U] = !R[U];
if (W >= 0 && !V[W]) {
Z = 0;
for (int k : rep(D[W]))
if (R[G[W][k]] == R[W])
++Z;
if (Z > 1) {
Q.add(W);
V[W] = true;
}
}
}
}
char [] res = new char [N];
for (int i : rep(N))
res[i] = R[i] ? '1' : '0';
cprint(res);
}
////////////////////////////////////////////////////////////////////////////////////
static Iterable<Integer> rep(final int S, final int T) {
return new Iterable<Integer>() {
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
int i = S;
public void remove() { throw new UnsupportedOperationException(); }
public Integer next() { return i++; }
public boolean hasNext() { return i < T; }
};
}
};
}
static Iterable<Integer> rep(int N) { return rep(0, N); }
static Iterable<Integer> req(int S, int T) { return rep(S, T+1); }
////////////////////////////////////////////////////////////////////////////////////
static MyScanner sc;
static class MyScanner {
public String next() {
readToken();
return new String(b, T[0], T[1] - T[0]);
}
public char nextChar() {
readToken();
return (char)b[T[0]];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
readToken();
return calc(T[0], T[1]);
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
readLine();
return new String(b, T[0], T[1] - T[0]);
}
public String [] next(int N) {
String [] res = new String [N];
for (int i = 0; i < N; ++i)
res[i] = next();
return res;
}
public Integer [] nextInt(int N) {
Integer [] res = new Integer [N];
for (int i = 0; i < N; ++i)
res[i] = nextInt();
return res;
}
public Long [] nextLong(int N) {
Long [] res = new Long [N];
for (int i = 0; i < N; ++i)
res[i] = nextLong();
return res;
}
public Double [] nextDouble(int N) {
Double [] res = new Double [N];
for (int i = 0; i < N; ++i)
res[i] = nextDouble();
return res;
}
public String [] nextLine(int N) {
String [] res = new String [N];
for (int i = 0; i < N; ++i)
res[i] = nextLine();
return res;
}
public String [] nextStrings() {
readLine();
int s = T[0], c = 0;
String [] res = new String[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = new String(b, s, i - s);
s = i+1;
}
res[c] = new String (b, s, T[1] - s);
return res;
}
public char [] nextChars() {
readToken();
int c = 0;
char [] res = new char[T[1] - T[0]];
for (int i = T[0]; i < T[1]; ++i)
res[c++] = (char)b[i];
return res;
}
public Integer [] nextInts() {
readLine();
int s = T[0], c = 0;
Integer [] res = new Integer[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = (int)calc(s, i);
s = i+1;
}
res[c] = (int)calc(s, T[1]);
return res;
}
public Long [] nextLongs() {
readLine();
int s = T[0], c = 0;
Long [] res = new Long[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = calc(s, i);
s = i+1;
}
res[c] = calc(s, T[1]);
return res;
}
public Double [] nextDoubles() {
readLine();
int s = T[0], c = 0;
Double [] res = new Double[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = Double.parseDouble(new String(b, s, i - s));
s = i+1;
}
res[c] = Double.parseDouble(new String(b, s, T[1] - s));
return res;
}
String [][] nextStrings(int N) {
String [][] res = new String [N][];
for (int i = 0; i < N; ++i)
res[i] = nextStrings();
return res;
}
char [][] nextChars(int N) {
char [][] res = new char [N][];
for (int i = 0; i < N; ++i)
res[i] = nextChars();
return res;
}
Integer [][] nextInts(int N) {
Integer [][] res = new Integer [N][];
for (int i = 0; i < N; ++i)
res[i] = nextInts();
return res;
}
Long [][] nextLongs(int N) {
Long [][] res = new Long [N][];
for (int i = 0; i < N; ++i)
res[i] = nextLongs();
return res;
}
Double [][] nextDoubles(int N) {
Double [][] res = new Double [N][];
for (int i = 0; i < N; ++i)
res[i] = nextDoubles();
return res;
}
//////////////////////////////////////////////
private final static int MAX_SZ = 12000000;
private final byte [] b = new byte[MAX_SZ];
private final int [] T = new int [2];
private int cur = 0;
MyScanner () {
try {
InputStream is = new BufferedInputStream(System.in);
while (is.available() == 0)
Thread.sleep(1);
start();
int off = 0, c;
while ((c = is.read(b, off, MAX_SZ - off)) != -1)
off += c;
} catch (Exception e) {
throw new Error(e);
}
}
private void readLine() {
int c;
for (c = cur; b[c] != LF && b[c] != CR && b[c] != 0; ++c);
T[0] = cur; T[1] = c;
for (cur = c; b[cur] == LF || b[cur] == CR; ++cur);
}
private void readToken() {
int c;
for (c = cur; b[c] != SPACE && b[c] != LF && b[c] != CR && b[c] != 0; ++c);
T[0] = cur; T[1] = c;
for (cur = c; b[cur] == SPACE || b[cur] == LF || b[cur] == CR; ++cur);
}
private int count(int s, int e) {
int cnt = 1;
for (int i = s; i < e; ++i)
if (b[i] == SPACE)
++cnt;
return cnt;
}
private long calc(int s, int e) {
long res = 0, f = 1;
for (int i = s; i < e; ++i)
if (b[i] == '-')
f = -1;
else
res = 10 * res + b[i] - '0';
return res * f;
}
private static final char LF = '\n';
private static final char CR = '\r';
private static final char SPACE = ' ';
}
static void print(Object o, Object... a) {
printDelim(" ", o, a);
}
static void cprint(Object o, Object... a) {
printDelim("", o, a);
}
static void printDelim (String delim, Object o, Object... a) {
pw.println(build(delim, o, a));
}
static void exit (Object o, Object... a) {
print(o, a);
exit();
}
static void exit () {
pw.close();
System.out.flush();
System.err.println("------------------");
System.err.println("Time: " + ((millis() - t) / 1000.0));
System.exit(0);
}
void NO() {
throw new Error("NO!");
}
////////////////////////////////////////////////////////////////////////////////////
static String build(String delim, Object o, Object... a) {
StringBuilder b = new StringBuilder();
append(b, o, delim);
for (Object p : a)
append(b, p, delim);
return b.toString().trim();
}
static void append(StringBuilder b, Object o, String delim) {
if (o.getClass().isArray()) {
int L = Array.getLength(o);
for (int i = 0; i < L; ++i)
append(b, Array.get(o, i), delim);
} else if (o instanceof Iterable<?>) {
for (Object p : (Iterable<?>)o)
append(b, p, delim);
} else
b.append(delim).append(o);
}
////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
sc = new MyScanner ();
new C();
exit();
}
static void start() {
t = millis();
}
static PrintWriter pw = new PrintWriter(System.out);
static long t;
static long millis() {
return System.currentTimeMillis();
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | f4fc428030faac626c16b1c5bfcfdb6a | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes |
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
public class C {
final int MOD = (int)1e9 + 7;
final double eps = 1e-12;
final int INF = (int)1e9;
public C () {
int N = sc.nextInt();
int M = sc.nextInt();
Integer [][] E = new Integer [N][3]; int [] NE = new int[N];
for (int i = 0; i < M; ++i) {
int x = sc.nextInt() - 1, y = sc.nextInt() - 1;
E[x][NE[x]++] = y;
E[y][NE[y]++] = x;
}
BitSet R = new BitSet();
Queue<Integer> Q = new LinkedList<Integer>();
BitSet V = new BitSet();
for (int i = 0; i < N; ++i) {
Q.add(i);
V.set(i);
}
while(!Q.isEmpty()) {
int U = Q.poll(); V.clear(U);
int Z = 0, W = -1;
for (int j = 0; j < NE[U]; ++j)
if (R.get(U) == R.get(E[U][j]))
++Z;
else
W = E[U][j];
if (Z > 1) {
R.flip(U);
if (W >= 0 && !V.get(W)) {
Z = 0;
for (int k = 0; k < NE[W]; ++k)
if (R.get(E[W][k]) == R.get(W))
++Z;
if (Z > 1) {
Q.add(W);
V.set(W);
}
}
}
}
char [] res = new char [N];
for (int i = 0; i < N; ++i)
res[i] = R.get(i) ? '1' : '0';
//cprint(res);
pw.println(new String(res));
}
////////////////////////////////////////////////////////////////////////////////////
static MyScanner sc;
static class MyScanner {
public String next() {
readToken();
return new String(b, T[0], T[1] - T[0]);
}
public char nextChar() {
readToken();
return (char)b[T[0]];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
readToken();
return calc(T[0], T[1]);
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
readLine();
return new String(b, T[0], T[1] - T[0]);
}
public String [] next(int N) {
String [] res = new String [N];
for (int i = 0; i < N; ++i)
res[i] = next();
return res;
}
public Integer [] nextInt(int N) {
Integer [] res = new Integer [N];
for (int i = 0; i < N; ++i)
res[i] = nextInt();
return res;
}
public Long [] nextLong(int N) {
Long [] res = new Long [N];
for (int i = 0; i < N; ++i)
res[i] = nextLong();
return res;
}
public Double [] nextDouble(int N) {
Double [] res = new Double [N];
for (int i = 0; i < N; ++i)
res[i] = nextDouble();
return res;
}
public String [] nextLine(int N) {
String [] res = new String [N];
for (int i = 0; i < N; ++i)
res[i] = nextLine();
return res;
}
public String [] nextStrings() {
readLine();
int s = T[0], c = 0;
String [] res = new String[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = new String(b, s, i - s);
s = i+1;
}
res[c] = new String (b, s, T[1] - s);
return res;
}
public char [] nextChars() {
readToken();
int c = 0;
char [] res = new char[T[1] - T[0]];
for (int i = T[0]; i < T[1]; ++i)
res[c++] = (char)b[i];
return res;
}
public Integer [] nextInts() {
readLine();
int s = T[0], c = 0;
Integer [] res = new Integer[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = (int)calc(s, i);
s = i+1;
}
res[c] = (int)calc(s, T[1]);
return res;
}
public Long [] nextLongs() {
readLine();
int s = T[0], c = 0;
Long [] res = new Long[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = calc(s, i);
s = i+1;
}
res[c] = calc(s, T[1]);
return res;
}
public Double [] nextDoubles() {
readLine();
int s = T[0], c = 0;
Double [] res = new Double[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = Double.parseDouble(new String(b, s, i - s));
s = i+1;
}
res[c] = Double.parseDouble(new String(b, s, T[1] - s));
return res;
}
String [][] nextStrings(int N) {
String [][] res = new String [N][];
for (int i = 0; i < N; ++i)
res[i] = nextStrings();
return res;
}
char [][] nextChars(int N) {
char [][] res = new char [N][];
for (int i = 0; i < N; ++i)
res[i] = nextChars();
return res;
}
Integer [][] nextInts(int N) {
Integer [][] res = new Integer [N][];
for (int i = 0; i < N; ++i)
res[i] = nextInts();
return res;
}
Long [][] nextLongs(int N) {
Long [][] res = new Long [N][];
for (int i = 0; i < N; ++i)
res[i] = nextLongs();
return res;
}
Double [][] nextDoubles(int N) {
Double [][] res = new Double [N][];
for (int i = 0; i < N; ++i)
res[i] = nextDoubles();
return res;
}
//////////////////////////////////////////////
private final static int MAX_SZ = 12000000;
private final byte [] b = new byte[MAX_SZ];
private final int [] T = new int [2];
private int cur = 0;
MyScanner () {
try {
InputStream is = new BufferedInputStream(System.in);
while (is.available() == 0)
Thread.sleep(1);
start();
int off = 0, c;
while ((c = is.read(b, off, MAX_SZ - off)) != -1)
off += c;
} catch (Exception e) {
throw new Error(e);
}
}
private void readLine() {
int c;
for (c = cur; b[c] != LF && b[c] != CR && b[c] != 0; ++c);
T[0] = cur; T[1] = c;
for (cur = c; b[cur] == LF || b[cur] == CR; ++cur);
}
private void readToken() {
int c;
for (c = cur; b[c] != SPACE && b[c] != LF && b[c] != CR && b[c] != 0; ++c);
T[0] = cur; T[1] = c;
for (cur = c; b[cur] == SPACE || b[cur] == LF || b[cur] == CR; ++cur);
}
private int count(int s, int e) {
int cnt = 1;
for (int i = s; i < e; ++i)
if (b[i] == SPACE)
++cnt;
return cnt;
}
private long calc(int s, int e) {
long res = 0, f = 1;
for (int i = s; i < e; ++i)
if (b[i] == '-')
f = -1;
else
res = 10 * res + b[i] - '0';
return res * f;
}
private static final char LF = '\n';
private static final char CR = '\r';
private static final char SPACE = ' ';
}
static void print(Object o, Object... a) {
printDelim(" ", o, a);
}
static void cprint(Object o, Object... a) {
printDelim("", o, a);
}
static void printDelim (String delim, Object o, Object... a) {
pw.println(build(delim, o, a));
}
static void exit (Object o, Object... a) {
print(o, a);
exit();
}
static void exit () {
pw.close();
System.out.flush();
System.err.println("------------------");
System.err.println("Time: " + ((millis() - t) / 1000.0));
System.exit(0);
}
void NO() {
throw new Error("NO!");
}
////////////////////////////////////////////////////////////////////////////////////
static String build(String delim, Object o, Object... a) {
StringBuilder b = new StringBuilder();
append(b, o, delim);
for (Object p : a)
append(b, p, delim);
return b.toString().trim();
}
static void append(StringBuilder b, Object o, String delim) {
if (o.getClass().isArray()) {
int L = Array.getLength(o);
for (int i = 0; i < L; ++i)
append(b, Array.get(o, i), delim);
} else if (o instanceof Iterable<?>) {
for (Object p : (Iterable<?>)o)
append(b, p, delim);
} else
b.append(delim).append(o);
}
////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
sc = new MyScanner ();
new C();
exit();
}
static void start() {
t = millis();
}
static PrintWriter pw = new PrintWriter(System.out);
static long t;
static long millis() {
return System.currentTimeMillis();
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | 63a0da323e08d8ee94db0dfed33b9829 | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes |
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
public class C {
final int MOD = (int)1e9 + 7;
final double eps = 1e-12;
final int INF = (int)1e9;
public C () {
int N = sc.nextInt();
int M = sc.nextInt();
Integer [][] E = new Integer [N][3]; int [] NE = new int[N];
for (int i = 0; i < M; ++i) {
int x = sc.nextInt() - 1, y = sc.nextInt() - 1;
E[x][NE[x]++] = y;
E[y][NE[y]++] = x;
}
BitSet R = new BitSet();
Queue<Integer> Q = new LinkedList<Integer>();
BitSet V = new BitSet();
for (int i = 0; i < N; ++i) {
Q.add(i);
V.set(i);
}
while(!Q.isEmpty()) {
int U = Q.poll(); V.clear(U);
int Z = 0, W = -1;
for (int j = 0; j < NE[U]; ++j)
if (R.get(U) == R.get(E[U][j]))
++Z;
else
W = E[U][j];
if (Z > 1) {
R.flip(U);
if (W >= 0 && !V.get(W)) {
Z = 0;
for (int k = 0; k < NE[W]; ++k)
if (R.get(E[W][k]) == R.get(W))
++Z;
if (Z > 1) {
Q.add(W);
V.set(W);
}
}
}
}
char [] res = new char [N];
for (int i = 0; i < N; ++i)
res[i] = R.get(i) ? '1' : '0';
cprint(res);
}
////////////////////////////////////////////////////////////////////////////////////
static MyScanner sc;
static class MyScanner {
public String next() {
readToken();
return new String(b, T[0], T[1] - T[0]);
}
public char nextChar() {
readToken();
return (char)b[T[0]];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
readToken();
return calc(T[0], T[1]);
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
readLine();
return new String(b, T[0], T[1] - T[0]);
}
public String [] next(int N) {
String [] res = new String [N];
for (int i = 0; i < N; ++i)
res[i] = next();
return res;
}
public Integer [] nextInt(int N) {
Integer [] res = new Integer [N];
for (int i = 0; i < N; ++i)
res[i] = nextInt();
return res;
}
public Long [] nextLong(int N) {
Long [] res = new Long [N];
for (int i = 0; i < N; ++i)
res[i] = nextLong();
return res;
}
public Double [] nextDouble(int N) {
Double [] res = new Double [N];
for (int i = 0; i < N; ++i)
res[i] = nextDouble();
return res;
}
public String [] nextLine(int N) {
String [] res = new String [N];
for (int i = 0; i < N; ++i)
res[i] = nextLine();
return res;
}
public String [] nextStrings() {
readLine();
int s = T[0], c = 0;
String [] res = new String[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = new String(b, s, i - s);
s = i+1;
}
res[c] = new String (b, s, T[1] - s);
return res;
}
public char [] nextChars() {
readToken();
int c = 0;
char [] res = new char[T[1] - T[0]];
for (int i = T[0]; i < T[1]; ++i)
res[c++] = (char)b[i];
return res;
}
public Integer [] nextInts() {
readLine();
int s = T[0], c = 0;
Integer [] res = new Integer[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = (int)calc(s, i);
s = i+1;
}
res[c] = (int)calc(s, T[1]);
return res;
}
public Long [] nextLongs() {
readLine();
int s = T[0], c = 0;
Long [] res = new Long[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = calc(s, i);
s = i+1;
}
res[c] = calc(s, T[1]);
return res;
}
public Double [] nextDoubles() {
readLine();
int s = T[0], c = 0;
Double [] res = new Double[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = Double.parseDouble(new String(b, s, i - s));
s = i+1;
}
res[c] = Double.parseDouble(new String(b, s, T[1] - s));
return res;
}
String [][] nextStrings(int N) {
String [][] res = new String [N][];
for (int i = 0; i < N; ++i)
res[i] = nextStrings();
return res;
}
char [][] nextChars(int N) {
char [][] res = new char [N][];
for (int i = 0; i < N; ++i)
res[i] = nextChars();
return res;
}
Integer [][] nextInts(int N) {
Integer [][] res = new Integer [N][];
for (int i = 0; i < N; ++i)
res[i] = nextInts();
return res;
}
Long [][] nextLongs(int N) {
Long [][] res = new Long [N][];
for (int i = 0; i < N; ++i)
res[i] = nextLongs();
return res;
}
Double [][] nextDoubles(int N) {
Double [][] res = new Double [N][];
for (int i = 0; i < N; ++i)
res[i] = nextDoubles();
return res;
}
//////////////////////////////////////////////
private final static int MAX_SZ = 12000000;
private final byte [] b = new byte[MAX_SZ];
private final int [] T = new int [2];
private int cur = 0;
MyScanner () {
try {
InputStream is = new BufferedInputStream(System.in);
while (is.available() == 0)
Thread.sleep(1);
start();
int off = 0, c;
while ((c = is.read(b, off, MAX_SZ - off)) != -1)
off += c;
} catch (Exception e) {
throw new Error(e);
}
}
private void readLine() {
int c;
for (c = cur; b[c] != LF && b[c] != CR && b[c] != 0; ++c);
T[0] = cur; T[1] = c;
for (cur = c; b[cur] == LF || b[cur] == CR; ++cur);
}
private void readToken() {
int c;
for (c = cur; b[c] != SPACE && b[c] != LF && b[c] != CR && b[c] != 0; ++c);
T[0] = cur; T[1] = c;
for (cur = c; b[cur] == SPACE || b[cur] == LF || b[cur] == CR; ++cur);
}
private int count(int s, int e) {
int cnt = 1;
for (int i = s; i < e; ++i)
if (b[i] == SPACE)
++cnt;
return cnt;
}
private long calc(int s, int e) {
long res = 0, f = 1;
for (int i = s; i < e; ++i)
if (b[i] == '-')
f = -1;
else
res = 10 * res + b[i] - '0';
return res * f;
}
private static final char LF = '\n';
private static final char CR = '\r';
private static final char SPACE = ' ';
}
static void print(Object o, Object... a) {
printDelim(" ", o, a);
}
static void cprint(Object o, Object... a) {
printDelim("", o, a);
}
static void printDelim (String delim, Object o, Object... a) {
pw.println(build(delim, o, a));
}
static void exit (Object o, Object... a) {
print(o, a);
exit();
}
static void exit () {
pw.close();
System.out.flush();
System.err.println("------------------");
System.err.println("Time: " + ((millis() - t) / 1000.0));
System.exit(0);
}
void NO() {
throw new Error("NO!");
}
////////////////////////////////////////////////////////////////////////////////////
static String build(String delim, Object o, Object... a) {
StringBuilder b = new StringBuilder();
append(b, o, delim);
for (Object p : a)
append(b, p, delim);
return b.toString().trim();
}
static void append(StringBuilder b, Object o, String delim) {
if (o.getClass().isArray()) {
int L = Array.getLength(o);
for (int i = 0; i < L; ++i)
append(b, Array.get(o, i), delim);
} else if (o instanceof Iterable<?>) {
for (Object p : (Iterable<?>)o)
append(b, p, delim);
} else
b.append(delim).append(o);
}
////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
sc = new MyScanner ();
new C();
exit();
}
static void start() {
t = millis();
}
static PrintWriter pw = new PrintWriter(System.out);
static long t;
static long millis() {
return System.currentTimeMillis();
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | ffd0c8da1798fa9c542122c863f21798 | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes |
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
public class C {
final int MOD = (int)1e9 + 7;
final double eps = 1e-12;
final int INF = (int)1e9;
public C () {
int N = sc.nextInt();
int M = sc.nextInt();
Integer [][] E = new Integer [N][3]; int [] NE = new int[N];
for (int i = 0; i < M; ++i) {
int x = sc.nextInt() - 1, y = sc.nextInt() - 1;
E[x][NE[x]++] = y;
E[y][NE[y]++] = x;
}
// BitSet R = new BitSet();
// BitSet V = new BitSet();
boolean [] R = new boolean[N];
boolean [] V = new boolean[N];
Queue<Integer> Q = new LinkedList<Integer>();
for (int i = 0; i < N; ++i) {
Q.add(i);
V[i] = true; // V.set(i);
}
while(!Q.isEmpty()) {
int U = Q.poll(); V[U] = false; // V.clear(U);
int Z = 0, W = -1;
for (int j = 0; j < NE[U]; ++j)
// if (R.get(U) == R.get(E[U][j]))
if (R[U] == R[E[U][j]])
++Z;
else
W = E[U][j];
if (Z > 1) {
R[U] = !R[U]; //R.flip(U);
if (W >= 0 && !V[W]) { // !V.get(W)) {
Z = 0;
for (int k = 0; k < NE[W]; ++k)
// if (R.get(E[W][k]) == R.get(W))
if (R[E[W][k]] == R[W])
++Z;
if (Z > 1) {
Q.add(W);
V[W] = true; //V.set(W);
}
}
}
}
char [] res = new char [N];
for (int i = 0; i < N; ++i)
res[i] = /* R.get(i) */ R[i] ? '1' : '0';
//cprint(res);
pw.println(new String(res));
}
////////////////////////////////////////////////////////////////////////////////////
static MyScanner sc;
static class MyScanner {
public String next() {
readToken();
return new String(b, T[0], T[1] - T[0]);
}
public char nextChar() {
readToken();
return (char)b[T[0]];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
readToken();
return calc(T[0], T[1]);
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
readLine();
return new String(b, T[0], T[1] - T[0]);
}
public String [] next(int N) {
String [] res = new String [N];
for (int i = 0; i < N; ++i)
res[i] = next();
return res;
}
public Integer [] nextInt(int N) {
Integer [] res = new Integer [N];
for (int i = 0; i < N; ++i)
res[i] = nextInt();
return res;
}
public Long [] nextLong(int N) {
Long [] res = new Long [N];
for (int i = 0; i < N; ++i)
res[i] = nextLong();
return res;
}
public Double [] nextDouble(int N) {
Double [] res = new Double [N];
for (int i = 0; i < N; ++i)
res[i] = nextDouble();
return res;
}
public String [] nextLine(int N) {
String [] res = new String [N];
for (int i = 0; i < N; ++i)
res[i] = nextLine();
return res;
}
public String [] nextStrings() {
readLine();
int s = T[0], c = 0;
String [] res = new String[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = new String(b, s, i - s);
s = i+1;
}
res[c] = new String (b, s, T[1] - s);
return res;
}
public char [] nextChars() {
readToken();
int c = 0;
char [] res = new char[T[1] - T[0]];
for (int i = T[0]; i < T[1]; ++i)
res[c++] = (char)b[i];
return res;
}
public Integer [] nextInts() {
readLine();
int s = T[0], c = 0;
Integer [] res = new Integer[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = (int)calc(s, i);
s = i+1;
}
res[c] = (int)calc(s, T[1]);
return res;
}
public Long [] nextLongs() {
readLine();
int s = T[0], c = 0;
Long [] res = new Long[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = calc(s, i);
s = i+1;
}
res[c] = calc(s, T[1]);
return res;
}
public Double [] nextDoubles() {
readLine();
int s = T[0], c = 0;
Double [] res = new Double[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = Double.parseDouble(new String(b, s, i - s));
s = i+1;
}
res[c] = Double.parseDouble(new String(b, s, T[1] - s));
return res;
}
String [][] nextStrings(int N) {
String [][] res = new String [N][];
for (int i = 0; i < N; ++i)
res[i] = nextStrings();
return res;
}
char [][] nextChars(int N) {
char [][] res = new char [N][];
for (int i = 0; i < N; ++i)
res[i] = nextChars();
return res;
}
Integer [][] nextInts(int N) {
Integer [][] res = new Integer [N][];
for (int i = 0; i < N; ++i)
res[i] = nextInts();
return res;
}
Long [][] nextLongs(int N) {
Long [][] res = new Long [N][];
for (int i = 0; i < N; ++i)
res[i] = nextLongs();
return res;
}
Double [][] nextDoubles(int N) {
Double [][] res = new Double [N][];
for (int i = 0; i < N; ++i)
res[i] = nextDoubles();
return res;
}
//////////////////////////////////////////////
private final static int MAX_SZ = 12000000;
private final byte [] b = new byte[MAX_SZ];
private final int [] T = new int [2];
private int cur = 0;
MyScanner () {
try {
InputStream is = new BufferedInputStream(System.in);
while (is.available() == 0)
Thread.sleep(1);
start();
int off = 0, c;
while ((c = is.read(b, off, MAX_SZ - off)) != -1)
off += c;
} catch (Exception e) {
throw new Error(e);
}
}
private void readLine() {
int c;
for (c = cur; b[c] != LF && b[c] != CR && b[c] != 0; ++c);
T[0] = cur; T[1] = c;
for (cur = c; b[cur] == LF || b[cur] == CR; ++cur);
}
private void readToken() {
int c;
for (c = cur; b[c] != SPACE && b[c] != LF && b[c] != CR && b[c] != 0; ++c);
T[0] = cur; T[1] = c;
for (cur = c; b[cur] == SPACE || b[cur] == LF || b[cur] == CR; ++cur);
}
private int count(int s, int e) {
int cnt = 1;
for (int i = s; i < e; ++i)
if (b[i] == SPACE)
++cnt;
return cnt;
}
private long calc(int s, int e) {
long res = 0, f = 1;
for (int i = s; i < e; ++i)
if (b[i] == '-')
f = -1;
else
res = 10 * res + b[i] - '0';
return res * f;
}
private static final char LF = '\n';
private static final char CR = '\r';
private static final char SPACE = ' ';
}
static void print(Object o, Object... a) {
printDelim(" ", o, a);
}
static void cprint(Object o, Object... a) {
printDelim("", o, a);
}
static void printDelim (String delim, Object o, Object... a) {
pw.println(build(delim, o, a));
}
static void exit (Object o, Object... a) {
print(o, a);
exit();
}
static void exit () {
pw.close();
System.out.flush();
System.err.println("------------------");
System.err.println("Time: " + ((millis() - t) / 1000.0));
System.exit(0);
}
void NO() {
throw new Error("NO!");
}
////////////////////////////////////////////////////////////////////////////////////
static String build(String delim, Object o, Object... a) {
StringBuilder b = new StringBuilder();
append(b, o, delim);
for (Object p : a)
append(b, p, delim);
return b.toString().trim();
}
static void append(StringBuilder b, Object o, String delim) {
if (o.getClass().isArray()) {
int L = Array.getLength(o);
for (int i = 0; i < L; ++i)
append(b, Array.get(o, i), delim);
} else if (o instanceof Iterable<?>) {
for (Object p : (Iterable<?>)o)
append(b, p, delim);
} else
b.append(delim).append(o);
}
////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
sc = new MyScanner ();
new C();
exit();
}
static void start() {
t = millis();
}
static PrintWriter pw = new PrintWriter(System.out);
static long t;
static long millis() {
return System.currentTimeMillis();
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | 24734e41289b76b582acf2630010f756 | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.*;
import java.lang.reflect.*;
import java.util.*;
public class C {
public C () {
int N = sc.nextInt();
int M = sc.nextInt();
Integer [][] G = new Integer [N][3]; int [] D = new int[N];
for (@SuppressWarnings("unused") int i : rep(M)) {
int x = sc.nextInt() - 1, y = sc.nextInt() - 1;
G[x][D[x]++] = y;
G[y][D[y]++] = x;
}
boolean [] R = new boolean[N];
boolean [] V = new boolean[N];
Queue<Integer> Q = new LinkedList<Integer>();
for (int i : rep(N)) {
Q.add(i);
V[i] = true;
}
while(!Q.isEmpty()) {
int U = Q.poll();
V[U] = false;
int Z = 0, W = -1;
for (int j : rep(D[U]))
if (R[U] == R[G[U][j]])
++Z;
else
W = G[U][j];
if (Z > 1) {
R[U] = !R[U];
if (W >= 0 && !V[W]) {
Z = 0;
for (int k : rep(D[W]))
if (R[G[W][k]] == R[W])
++Z;
if (Z > 1) {
Q.add(W);
V[W] = true;
}
}
}
}
char [] res = new char [N];
for (int i : rep(N))
res[i] = R[i] ? '1' : '0';
cprint(res);
}
////////////////////////////////////////////////////////////////////////////////////
static int [] rep(int N) { return rep(0, N); }
static int [] rep(int S, int T) { int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
static int [] req(int S, int T) { return rep(S, T+1); }
////////////////////////////////////////////////////////////////////////////////////
static MyScanner sc;
static class MyScanner {
public String next() {
readToken();
return new String(b, T[0], T[1] - T[0]);
}
public char nextChar() {
readToken();
return (char)b[T[0]];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
readToken();
return calc(T[0], T[1]);
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
readLine();
return new String(b, T[0], T[1] - T[0]);
}
public String [] next(int N) {
String [] res = new String [N];
for (int i = 0; i < N; ++i)
res[i] = next();
return res;
}
public Integer [] nextInt(int N) {
Integer [] res = new Integer [N];
for (int i = 0; i < N; ++i)
res[i] = nextInt();
return res;
}
public Long [] nextLong(int N) {
Long [] res = new Long [N];
for (int i = 0; i < N; ++i)
res[i] = nextLong();
return res;
}
public Double [] nextDouble(int N) {
Double [] res = new Double [N];
for (int i = 0; i < N; ++i)
res[i] = nextDouble();
return res;
}
public String [] nextLine(int N) {
String [] res = new String [N];
for (int i = 0; i < N; ++i)
res[i] = nextLine();
return res;
}
public String [] nextStrings() {
readLine();
int s = T[0], c = 0;
String [] res = new String[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = new String(b, s, i - s);
s = i+1;
}
res[c] = new String (b, s, T[1] - s);
return res;
}
public char [] nextChars() {
readToken();
int c = 0;
char [] res = new char[T[1] - T[0]];
for (int i = T[0]; i < T[1]; ++i)
res[c++] = (char)b[i];
return res;
}
public Integer [] nextInts() {
readLine();
int s = T[0], c = 0;
Integer [] res = new Integer[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = (int)calc(s, i);
s = i+1;
}
res[c] = (int)calc(s, T[1]);
return res;
}
public Long [] nextLongs() {
readLine();
int s = T[0], c = 0;
Long [] res = new Long[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = calc(s, i);
s = i+1;
}
res[c] = calc(s, T[1]);
return res;
}
public Double [] nextDoubles() {
readLine();
int s = T[0], c = 0;
Double [] res = new Double[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = Double.parseDouble(new String(b, s, i - s));
s = i+1;
}
res[c] = Double.parseDouble(new String(b, s, T[1] - s));
return res;
}
String [][] nextStrings(int N) {
String [][] res = new String [N][];
for (int i = 0; i < N; ++i)
res[i] = nextStrings();
return res;
}
char [][] nextChars(int N) {
char [][] res = new char [N][];
for (int i = 0; i < N; ++i)
res[i] = nextChars();
return res;
}
Integer [][] nextInts(int N) {
Integer [][] res = new Integer [N][];
for (int i = 0; i < N; ++i)
res[i] = nextInts();
return res;
}
Long [][] nextLongs(int N) {
Long [][] res = new Long [N][];
for (int i = 0; i < N; ++i)
res[i] = nextLongs();
return res;
}
Double [][] nextDoubles(int N) {
Double [][] res = new Double [N][];
for (int i = 0; i < N; ++i)
res[i] = nextDoubles();
return res;
}
//////////////////////////////////////////////
private final static int MAX_SZ = 12000000;
private final byte [] b = new byte[MAX_SZ];
private final int [] T = new int [2];
private int cur = 0;
MyScanner () {
try {
InputStream is = new BufferedInputStream(System.in);
while (is.available() == 0)
Thread.sleep(1);
start();
int off = 0, c;
while ((c = is.read(b, off, MAX_SZ - off)) != -1)
off += c;
} catch (Exception e) {
throw new Error(e);
}
}
private void readLine() {
int c;
for (c = cur; b[c] != LF && b[c] != CR && b[c] != 0; ++c);
T[0] = cur; T[1] = c;
for (cur = c; b[cur] == LF || b[cur] == CR; ++cur);
}
private void readToken() {
int c;
for (c = cur; b[c] != SPACE && b[c] != LF && b[c] != CR && b[c] != 0; ++c);
T[0] = cur; T[1] = c;
for (cur = c; b[cur] == SPACE || b[cur] == LF || b[cur] == CR; ++cur);
}
private int count(int s, int e) {
int cnt = 1;
for (int i = s; i < e; ++i)
if (b[i] == SPACE)
++cnt;
return cnt;
}
private long calc(int s, int e) {
long res = 0, f = 1;
for (int i = s; i < e; ++i)
if (b[i] == '-')
f = -1;
else
res = 10 * res + b[i] - '0';
return res * f;
}
private static final char LF = '\n';
private static final char CR = '\r';
private static final char SPACE = ' ';
}
static void print(Object o, Object... a) {
printDelim(" ", o, a);
}
static void cprint(Object o, Object... a) {
printDelim("", o, a);
}
static void printDelim (String delim, Object o, Object... a) {
pw.println(build(delim, o, a));
}
static void exit (Object o, Object... a) {
print(o, a);
exit();
}
static void exit () {
pw.close();
System.out.flush();
System.err.println("------------------");
System.err.println("Time: " + ((millis() - t) / 1000.0));
System.exit(0);
}
void NO() {
throw new Error("NO!");
}
////////////////////////////////////////////////////////////////////////////////////
static String build(String delim, Object o, Object... a) {
StringBuilder b = new StringBuilder();
append(b, o, delim);
for (Object p : a)
append(b, p, delim);
return b.toString().trim();
}
static void append(StringBuilder b, Object o, String delim) {
if (o.getClass().isArray()) {
int L = Array.getLength(o);
for (int i = 0; i < L; ++i)
append(b, Array.get(o, i), delim);
} else if (o instanceof Iterable<?>) {
for (Object p : (Iterable<?>)o)
append(b, p, delim);
} else
b.append(delim).append(o);
}
////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
sc = new MyScanner ();
new C();
exit();
}
static void start() {
t = millis();
}
static PrintWriter pw = new PrintWriter(System.out);
static long t;
static long millis() {
return System.currentTimeMillis();
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | 60e87a1fe90f2065bbf75cfbf7736342 | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.*;
import java.lang.reflect.*;
import java.util.*;
public class C {
final int MOD = (int)1e9 + 7;
final double eps = 1e-12;
final int INF = (int)1e9;
public C () {
int N = sc.nextInt();
int M = sc.nextInt();
Integer [][] E = new Integer [N][3]; int [] NE = new int[N];
for (int i = 0; i < M; ++i) {
int x = sc.nextInt() - 1, y = sc.nextInt() - 1;
E[x][NE[x]++] = y;
E[y][NE[y]++] = x;
}
int [] R = new int [N];
Queue<Integer> Q = new LinkedList<Integer>();
boolean [] V = new boolean [N];
for (int i = 0; i < N; ++i) {
Q.add(i);
V[i] = true;
}
while(!Q.isEmpty()) {
int U = Q.poll();
V[U] = false;
int Z = 0;
for (int j = 0; j < NE[U]; ++j)
if (R[U] == R[E[U][j]])
++Z;
if (Z > 1) {
R[U] = 1 - R[U];
for (int j = 0; j < NE[U]; ++j) {
int X = E[U][j];
if (!V[X]) {
Z = 0;
for (int k = 0; k < NE[X]; ++k)
if (R[E[X][k]] == R[X])
++Z;
if (Z > 1) {
Q.add(X);
V[X] = true;
}
}
}
}
}
char [] res = new char [N];
for (int i = 0; i < N; ++i)
res[i] = R[i] == 0 ? '0' : '1';
cprint(res);
}
////////////////////////////////////////////////////////////////////////////////////
static MyScanner sc;
static class MyScanner {
public String next() {
newLine();
return line[index++];
}
public char nextChar() {
return next().charAt(0);
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
line = null;
return readLine();
}
public String [] nextStrings() {
line = null;
return readLine().split(" ");
}
public char [] nextChars() {
return next().toCharArray();
}
public Integer [] nextInts() {
String [] L = nextStrings();
Integer [] res = new Integer [L.length];
for (int i = 0; i < L.length; ++i)
res[i] = Integer.parseInt(L[i]);
return res;
}
public Long [] nextLongs() {
String [] L = nextStrings();
Long [] res = new Long [L.length];
for (int i = 0; i < L.length; ++i)
res[i] = Long.parseLong(L[i]);
return res;
}
public Double [] nextDoubles() {
String [] L = nextStrings();
Double [] res = new Double [L.length];
for (int i = 0; i < L.length; ++i)
res[i] = Double.parseDouble(L[i]);
return res;
}
public String [] next (int N) {
String [] res = new String [N];
for (int i = 0; i < N; ++i)
res[i] = sc.next();
return res;
}
public Integer [] nextInt (int N) {
Integer [] res = new Integer [N];
for (int i = 0; i < N; ++i)
res[i] = sc.nextInt();
return res;
}
public Long [] nextLong (int N) {
Long [] res = new Long [N];
for (int i = 0; i < N; ++i)
res[i] = sc.nextLong();
return res;
}
public Double [] nextDouble (int N) {
Double [] res = new Double [N];
for (int i = 0; i < N; ++i)
res[i] = sc.nextDouble();
return res;
}
public String [][] nextStrings (int N) {
String [][] res = new String [N][];
for (int i = 0; i < N; ++i)
res[i] = sc.nextStrings();
return res;
}
public Integer [][] nextInts (int N) {
Integer [][] res = new Integer [N][];
for (int i = 0; i < N; ++i)
res[i] = sc.nextInts();
return res;
}
public Long [][] nextLongs (int N) {
Long [][] res = new Long [N][];
for (int i = 0; i < N; ++i)
res[i] = sc.nextLongs();
return res;
}
public Double [][] nextDoubles (int N) {
Double [][] res = new Double [N][];
for (int i = 0; i < N; ++i)
res[i] = sc.nextDoubles();
return res;
}
//////////////////////////////////////////////
private boolean eol() {
return index == line.length;
}
private String readLine() {
try {
return r.readLine();
} catch (Exception e) {
throw new Error(e);
}
}
private final BufferedReader r;
MyScanner () {
this(new BufferedReader(new InputStreamReader(System.in)));
}
MyScanner(BufferedReader r) {
try {
this.r = r;
while (!r.ready())
Thread.sleep(1);
start();
} catch (Exception e) {
throw new Error(e);
}
}
private String [] line;
private int index;
private void newLine() {
if (line == null || eol()) {
line = readLine().split(" ");
index = 0;
}
}
}
static void print(Object o, Object... a) {
printDelim(" ", o, a);
}
static void cprint(Object o, Object... a) {
printDelim("", o, a);
}
static void printDelim (String delim, Object o, Object... a) {
pw.println(build(delim, o, a));
}
static void exit (Object o, Object... a) {
print(o, a);
exit();
}
static void exit () {
pw.close();
System.out.flush();
System.err.println("------------------");
System.err.println("Time: " + ((millis() - t) / 1000.0));
System.exit(0);
}
void NO() {
throw new Error("NO!");
}
////////////////////////////////////////////////////////////////////////////////////
static String build(String delim, Object o, Object... a) {
StringBuilder b = new StringBuilder();
append(b, o, delim);
for (Object p : a)
append(b, p, delim);
return b.toString().trim();
}
static void append(StringBuilder b, Object o, String delim) {
if (o.getClass().isArray()) {
int L = Array.getLength(o);
for (int i = 0; i < L; ++i)
append(b, Array.get(o, i), delim);
} else if (o instanceof Iterable<?>) {
for (Object p : (Iterable<?>)o)
append(b, p, delim);
} else
b.append(delim).append(o);
}
////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
sc = new MyScanner ();
new C();
exit();
}
static void start() {
t = millis();
}
static PrintWriter pw = new PrintWriter(System.out);
static long t;
static long millis() {
return System.currentTimeMillis();
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | 0c28237d627e95484d671f83c8f8e70a | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.*;
import java.lang.reflect.*;
import java.util.*;
public class C {
public C () {
int N = sc.nextInt();
int M = sc.nextInt();
Integer [][] G = new Integer [N][3]; int [] D = new int[N];
for (@SuppressWarnings("unused") int i : rep(M)) {
int x = sc.nextInt() - 1, y = sc.nextInt() - 1;
G[x][D[x]++] = y;
G[y][D[y]++] = x;
}
boolean [] R = new boolean[N];
boolean [] V = new boolean[N];
Queue<Integer> Q = new LinkedList<Integer>();
for (int i : rep(N)) {
Q.add(i);
V[i] = true;
}
while(!Q.isEmpty()) {
int U = Q.poll();
V[U] = false;
int Z = 0, W = -1;
for (int j : rep(D[U]))
if (R[U] == R[G[U][j]])
++Z;
else
W = G[U][j];
if (Z > 1) {
R[U] = !R[U];
if (W >= 0 && !V[W]) {
Z = 0;
for (int k : rep(D[W]))
if (R[G[W][k]] == R[W])
++Z;
if (Z > 1) {
Q.add(W);
V[W] = true;
}
}
}
}
char [] res = new char [N];
for (int i : rep(N))
res[i] = R[i] ? '1' : '0';
pw.println(new String(res));
}
////////////////////////////////////////////////////////////////////////////////////
static int [] rep(int N) { return rep(0, N); }
static int [] rep(int S, int T) { int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
static int [] req(int S, int T) { return rep(S, T+1); }
////////////////////////////////////////////////////////////////////////////////////
static MyScanner sc;
static class MyScanner {
public String next() {
readToken();
return new String(b, T[0], T[1] - T[0]);
}
public char nextChar() {
readToken();
return (char)b[T[0]];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
readToken();
return calc(T[0], T[1]);
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
readLine();
return new String(b, T[0], T[1] - T[0]);
}
public String [] next(int N) {
String [] res = new String [N];
for (int i = 0; i < N; ++i)
res[i] = next();
return res;
}
public Integer [] nextInt(int N) {
Integer [] res = new Integer [N];
for (int i = 0; i < N; ++i)
res[i] = nextInt();
return res;
}
public Long [] nextLong(int N) {
Long [] res = new Long [N];
for (int i = 0; i < N; ++i)
res[i] = nextLong();
return res;
}
public Double [] nextDouble(int N) {
Double [] res = new Double [N];
for (int i = 0; i < N; ++i)
res[i] = nextDouble();
return res;
}
public String [] nextLine(int N) {
String [] res = new String [N];
for (int i = 0; i < N; ++i)
res[i] = nextLine();
return res;
}
public String [] nextStrings() {
readLine();
int s = T[0], c = 0;
String [] res = new String[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = new String(b, s, i - s);
s = i+1;
}
res[c] = new String (b, s, T[1] - s);
return res;
}
public char [] nextChars() {
readToken();
int c = 0;
char [] res = new char[T[1] - T[0]];
for (int i = T[0]; i < T[1]; ++i)
res[c++] = (char)b[i];
return res;
}
public Integer [] nextInts() {
readLine();
int s = T[0], c = 0;
Integer [] res = new Integer[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = (int)calc(s, i);
s = i+1;
}
res[c] = (int)calc(s, T[1]);
return res;
}
public Long [] nextLongs() {
readLine();
int s = T[0], c = 0;
Long [] res = new Long[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = calc(s, i);
s = i+1;
}
res[c] = calc(s, T[1]);
return res;
}
public Double [] nextDoubles() {
readLine();
int s = T[0], c = 0;
Double [] res = new Double[count(T[0], T[1])];
for (int i = T[0]; i < T[1]; ++i)
if (b[i] == SPACE) {
res[c++] = Double.parseDouble(new String(b, s, i - s));
s = i+1;
}
res[c] = Double.parseDouble(new String(b, s, T[1] - s));
return res;
}
String [][] nextStrings(int N) {
String [][] res = new String [N][];
for (int i = 0; i < N; ++i)
res[i] = nextStrings();
return res;
}
char [][] nextChars(int N) {
char [][] res = new char [N][];
for (int i = 0; i < N; ++i)
res[i] = nextChars();
return res;
}
Integer [][] nextInts(int N) {
Integer [][] res = new Integer [N][];
for (int i = 0; i < N; ++i)
res[i] = nextInts();
return res;
}
Long [][] nextLongs(int N) {
Long [][] res = new Long [N][];
for (int i = 0; i < N; ++i)
res[i] = nextLongs();
return res;
}
Double [][] nextDoubles(int N) {
Double [][] res = new Double [N][];
for (int i = 0; i < N; ++i)
res[i] = nextDoubles();
return res;
}
//////////////////////////////////////////////
private final static int MAX_SZ = 12000000;
private final byte [] b = new byte[MAX_SZ];
private final int [] T = new int [2];
private int cur = 0;
MyScanner () {
try {
InputStream is = new BufferedInputStream(System.in);
while (is.available() == 0)
Thread.sleep(1);
start();
int off = 0, c;
while ((c = is.read(b, off, MAX_SZ - off)) != -1)
off += c;
} catch (Exception e) {
throw new Error(e);
}
}
private void readLine() {
int c;
for (c = cur; b[c] != LF && b[c] != CR && b[c] != 0; ++c);
T[0] = cur; T[1] = c;
for (cur = c; b[cur] == LF || b[cur] == CR; ++cur);
}
private void readToken() {
int c;
for (c = cur; b[c] != SPACE && b[c] != LF && b[c] != CR && b[c] != 0; ++c);
T[0] = cur; T[1] = c;
for (cur = c; b[cur] == SPACE || b[cur] == LF || b[cur] == CR; ++cur);
}
private int count(int s, int e) {
int cnt = 1;
for (int i = s; i < e; ++i)
if (b[i] == SPACE)
++cnt;
return cnt;
}
private long calc(int s, int e) {
long res = 0, f = 1;
for (int i = s; i < e; ++i)
if (b[i] == '-')
f = -1;
else
res = 10 * res + b[i] - '0';
return res * f;
}
private static final char LF = '\n';
private static final char CR = '\r';
private static final char SPACE = ' ';
}
static void print(Object o, Object... a) {
printDelim(" ", o, a);
}
static void cprint(Object o, Object... a) {
printDelim("", o, a);
}
static void printDelim (String delim, Object o, Object... a) {
pw.println(build(delim, o, a));
}
static void exit (Object o, Object... a) {
print(o, a);
exit();
}
static void exit () {
pw.close();
System.out.flush();
System.err.println("------------------");
System.err.println("Time: " + ((millis() - t) / 1000.0));
System.exit(0);
}
void NO() {
throw new Error("NO!");
}
////////////////////////////////////////////////////////////////////////////////////
static String build(String delim, Object o, Object... a) {
StringBuilder b = new StringBuilder();
append(b, o, delim);
for (Object p : a)
append(b, p, delim);
return b.toString().trim();
}
static void append(StringBuilder b, Object o, String delim) {
if (o.getClass().isArray()) {
int L = Array.getLength(o);
for (int i = 0; i < L; ++i)
append(b, Array.get(o, i), delim);
} else if (o instanceof Iterable<?>) {
for (Object p : (Iterable<?>)o)
append(b, p, delim);
} else
b.append(delim).append(o);
}
////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
sc = new MyScanner ();
new C();
exit();
}
static void start() {
t = millis();
}
static PrintWriter pw = new PrintWriter(System.out);
static long t;
static long millis() {
return System.currentTimeMillis();
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | 13d31327102fb1da6021772cb41031ca | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solver {
int n;
Vector<Integer>[] g;
int[] clr;
boolean isBad(int num) {
int cnt = 0;
for (int i = 0; i < g[num].size(); i++) {
int v = g[num].elementAt(i);
if (clr[v] == clr[num]) cnt++;
}
return cnt > 1;
}
void solve() throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
in.nextToken();
int n = (int)in.nval;
in.nextToken();
int m = (int)in.nval;
g = new Vector[n];
clr = new int[n];
for (int i = 0; i < n; i++) g[i] = new Vector<Integer>();
Vector<Integer> bad = new Vector<Integer>();
boolean[] used = new boolean[n];
for (int i = 0; i < m; i++) {
in.nextToken();
int u = (int)in.nval;
in.nextToken();
int v = (int)in.nval;
u--;
v--;
g[u].add(v);
g[v].add(u);
}
for (int i = 0; i < n; i++) {
if (isBad(i)) {
used[i] = true;
bad.add(i);
}
}
while (!bad.isEmpty()) {
int v = bad.lastElement();
bad.removeElementAt(bad.size() - 1);
used[v] = false;
if (!isBad(v)) continue;
clr[v] = 1 - clr[v];
for (int i = 0; i < g[v].size(); i++) {
int w = g[v].elementAt(i);
if (isBad(w) && !used[w]) {
used[w] = true;
bad.add(w);
}
}
}
for (int i = 0; i < n; i++) {
out.print(clr[i]);
}
out.close();
}
public static void main(String[] args) throws IOException {
(new Solver()).solve();
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | 5e2a1d0ff840b66ac98f4801d96927c7 | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solver {
int n;
Vector<Integer>[] g;
int[] clr;
StringTokenizer st = new StringTokenizer("");
BufferedReader in;
PrintWriter out;
String nextToken() throws IOException {
while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
boolean isBad(int num) {
int cnt = 0;
for (int i = 0; i < g[num].size(); i++) {
int v = g[num].elementAt(i);
if (clr[v] == clr[num]) cnt++;
}
return cnt > 1;
}
void solve() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int n = nextInt();
int m = nextInt();
g = new Vector[n];
clr = new int[n];
for (int i = 0; i < n; i++) g[i] = new Vector<Integer>();
//Vector<Integer> bad = new Vector<Integer>();
Queue<Integer> bad = new LinkedList<Integer>();
boolean[] used = new boolean[n];
for (int i = 0; i < m; i++) {
int u = nextInt() - 1;
int v = nextInt() - 1;
g[u].add(v);
g[v].add(u);
}
for (int i = 0; i < n; i++) {
if (isBad(i)) {
used[i] = true;
bad.add(i);
}
}
while (!bad.isEmpty()) {
int v = bad.poll();
used[v] = false;
if (!isBad(v)) continue;
clr[v] = 1 - clr[v];
for (int i = 0; i < g[v].size(); i++) {
int w = g[v].elementAt(i);
if (isBad(w) && !used[w]) {
used[w] = true;
bad.add(w);
}
}
}
for (int i = 0; i < n; i++) {
out.print(clr[i]);
}
out.close();
}
public static void main(String[] args) throws IOException {
(new Solver()).solve();
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | f9b0f2c309341906c8a75788fcc79f95 | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solver {
int n;
Vector<Integer>[] g;
int[] clr;
boolean isBad(int num) {
int cnt = 0;
for (int i = 0; i < g[num].size(); i++) {
int v = g[num].elementAt(i);
if (clr[v] == clr[num]) cnt++;
}
return cnt > 1;
}
void solve() throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
in.nextToken();
int n = (int)in.nval;
in.nextToken();
int m = (int)in.nval;
g = new Vector[n];
clr = new int[n];
for (int i = 0; i < n; i++) g[i] = new Vector<Integer>();
//Vector<Integer> bad = new Vector<Integer>();
Queue<Integer> bad = new LinkedList<Integer>();
boolean[] used = new boolean[n];
for (int i = 0; i < m; i++) {
in.nextToken();
int u = (int)in.nval;
in.nextToken();
int v = (int)in.nval;
u--;
v--;
g[u].add(v);
g[v].add(u);
}
for (int i = 0; i < n; i++) {
if (isBad(i)) {
used[i] = true;
bad.add(i);
}
}
while (!bad.isEmpty()) {
int v = bad.poll();
used[v] = false;
if (!isBad(v)) continue;
clr[v] = 1 - clr[v];
for (int i = 0; i < g[v].size(); i++) {
int w = g[v].elementAt(i);
if (isBad(w) && !used[w]) {
used[w] = true;
bad.add(w);
}
}
}
for (int i = 0; i < n; i++) {
out.print(clr[i]);
}
out.close();
}
public static void main(String[] args) throws IOException {
(new Solver()).solve();
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | 9b5c9d8c7705748f5be411f3baf0127c | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.awt.geom.*;
import static java.lang.Math.*;
public class Solution implements Runnable {
int n, m;
int g[][];
int [] whites, blacks, color;
void paint (int v, int c) {
for (int i = 0; i < g[v].length; i++) {
int to = g[v][i];
if (color[v] == 0)
whites[to]--;
else if (color[v] == 1)
blacks[to]--;
if (c == 0)
whites[to]++;
else
blacks[to]++;
}
color[v] = c;
}
void dfs (int v, int pre) {
if (whites[v] == 2) {
paint (v, 1);
for (int i = 0; i < g[v].length; i++) {
int to = g[v][i];
if (to == pre) continue;
if (color[to] == 1 && blacks[to] == 2)
dfs (to, v);
}
} else if (blacks[v] == 2) {
paint (v, 0);
for (int i = 0; i < g[v].length; i++) {
int to = g[v][i];
if (to == pre) continue;
if (color[to] == 0 && whites[to] == 2)
dfs (to, v);
}
}
}
public void solve () throws Exception {
n = sc.nextInt();
m = sc.nextInt();
g = new int [n][];
int degree[] = new int [n];
int edges [][] = new int [m][2];
for (int i = 0; i < m; i++) {
int v1 = sc.nextInt() - 1;
int v2 = sc.nextInt() - 1;
edges[i][0] = v1;
edges[i][1] = v2;
degree[v1]++;
degree[v2]++;
}
for (int i = 0; i < n; i++) {
g[i] = new int [degree[i]];
degree[i] = 0;
}
for (int i = 0; i < m; i++) {
int v1 = edges[i][0];
int v2 = edges[i][1];
g[v1][degree[v1]++] = v2;
g[v2][degree[v2]++] = v1;
}
whites = new int [n];
blacks = new int [n];
color = new int [n];
Arrays.fill(color, -1);
for (int i = 0; i < n; i++) {
if (whites[i] == 0) {
paint (i, 0);
} else if (blacks[i] == 0) {
paint (i, 1);
} else if (whites[i] == 1) {
paint (i, 0);
} else if (blacks[i] == 1) {
paint (i, 1);
}
for (int j = 0; j < g[i].length; j++) {
int to = g[i][j];
dfs (to, i);
}
}
for (int i = 0; i < n; i++)
out.print(color[i]);
}
BufferedReader in;
PrintWriter out;
FastScanner sc;
static Throwable uncaught;
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new FastScanner(in);
solve();
} catch (Throwable t) {
Solution.uncaught = t;
} finally {
out.close();
}
}
public static void main(String[] args) throws Throwable {
Thread t = new Thread(null, new Solution(), "", (1 << 26));
t.start();
t.join();
if (uncaught != null) {
throw uncaught;
}
}
}
class FastScanner {
BufferedReader reader;
StringTokenizer strTok;
public FastScanner(BufferedReader reader) {
this.reader = reader;
}
public String nextToken() throws IOException {
while (strTok == null || !strTok.hasMoreTokens()) {
strTok = new StringTokenizer(reader.readLine());
}
return strTok.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());
}
} | Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | cfe326bac5a4bc60988ca22a9dccbb4b | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.*;
import static java.lang.Math.*;
import java.util.*;
import static java.util.Arrays.fill;
import static java.util.Arrays.sort;
public class Main {
public static void main(String[] args) {
if(new File("input.txt").exists())
try {
System.setIn(new FileInputStream("input.txt"));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
new Thread(null, new Runnable() {
public void run() {
try {
new Main().run();
} catch (IOException e) {
e.printStackTrace();
}
}
}, "kek", 1 << 23).start();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
Locale.setDefault(Locale.US);
solve();
in.close();
out.close();
}
int n, m;
int type[];
int deg[];
int neigh[][];
boolean atQueue[];
Queue qu;
void add(int a, int b){
neigh[a][deg[a]++] = b;
}
int calcSum(int v){
int ret = 0;
for(int i = 0; i < deg[v]; i++)
if(type[v] == type[neigh[v][i]]) ret++;
return ret;
}
void solve() throws IOException{
n = nextInt();
qu = new Queue(n * 10);
m = nextInt();
type = new int[n];
deg = new int[n];
neigh = new int[n][3];
atQueue = new boolean[n];
for(int i = 0; i < m; i++){
int a = nextInt() - 1;
int b = nextInt() - 1;
add(a, b);
add(b, a);
}
for(int i = 0; i < n; i++)
if(deg[i] > 1){
qu.push(i);
atQueue[i] = true;
}
while(!qu.isEmpty()){
int e = qu.pop();
atQueue[e] = false;
if(calcSum(e) > 1)
type[e] = 1 - type[e];
for(int i = 0; i < deg[e]; i++){
int ne = neigh[e][i];
if(!atQueue[ne] && calcSum(ne) > 1){
qu.push(ne);
atQueue[ne] = true;
}
}
}
for(int i = 0; i < n; i++)
out.print(type[i]);
out.println();
}
class Queue{
int m[];
int h, t;
Queue(int sz){
m = new int[sz];
h = t = 0;
}
void push(int x){
m[t++] = x;
}
int pop(){
return m[h++];
}
boolean isEmpty(){
return h == t;
}
}
String nextToken() throws IOException{
while(!st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
long nextLong() throws IOException{
return Long.parseLong(nextToken());
}
int nextInt() throws IOException{
return Integer.parseInt(nextToken());
}
double nextDouble() throws IOException{
return Double.parseDouble(nextToken());
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | e3eb7aafb1682663ec9f0eaa64bf534a | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class C {
ArrayList<Integer>[] arr;
int[] col;
void dfs(int v, int num) {
if (col[v] != 0) {
return;
}
col[v] = num;
for (int k = 0; k < arr[v].size(); k++) {
int to = arr[v].get(k);
dfs(to, 3 - num);
}
}
void count() throws IOException {
int n = nextInt();
arr = new ArrayList[n];
col = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = new ArrayList<>();
}
int m = nextInt();
for (int i = 0; i < m; i++) {
int st = nextInt() - 1;
int en = nextInt() - 1;
arr[st].add(en);
arr[en].add(st);
}
ArrayList<Integer> ar = new ArrayList<>();
for (int i = 0; i < n; i++) {
ar.add(i);
}
for (int i = 0; i < n; i++) {
int v = ar.get(i);
if (col[v] == 0) {
dfs(v, 1);
}
}
while (true) {
boolean was = false;
ar = new ArrayList<>();
for (int i = 0; i < n; i++) {
ar.add(i);
}
Collections.shuffle(ar);
for (int i = 0; i < n; i++) {
int sum = 0;
int v = ar.get(i);
for (int k = 0; k < arr[v].size(); k++) {
int to = arr[v].get(k);
if (col[to] == col[v]) {
sum++;
}
}
if (sum > 1) {
col[v] = 3 - col[v];
was = true;
}
}
if (!was) {
break;
}
}
for (int i = 0; i < n; i++) {
out.print(col[i] - 1);
}
out.println();
}
BufferedReader in;
PrintWriter out;
StringTokenizer str;
String readline;
String nextToken() throws IOException {
while (str == null || !str.hasMoreTokens()) {
readline = in.readLine();
if (readline == null) {
return null;
} else {
str = new StringTokenizer(readline);
}
}
return str.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());
}
void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
count();
out.close();
}
public static void main(String[] args) throws IOException {
new C().run();
}
} | Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | 227d080d7e460262e5498e89cc1dda01 | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
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 vadimmm
*/
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 {
private ArrayList<Integer>[] graph;
private boolean[] color;
private int vertexCounter, edgeCounter;
public void solve(int testNumber, InputReader in, PrintWriter out) {
vertexCounter = in.nextInt();
edgeCounter = in.nextInt();
graph = new ArrayList[vertexCounter];
for (int i = 0; i < vertexCounter; ++i)
graph[i] = new ArrayList<Integer>();
color = new boolean[vertexCounter];
int v, u;
for (int i = 0; i < edgeCounter; ++i) {
v = in.nextInt() - 1;
u = in.nextInt() - 1;
graph[v].add(u);
graph[u].add(v);
}
boolean good = false;
while (!good) {
good = true;
for (int vertex = 0; vertex < vertexCounter; ++vertex) {
int degree = 0;
for (int x : graph[vertex])
if (color[x] == color[vertex])
++degree;
if (degree > 1) {
good = false;
color[vertex] = !color[vertex];
}
}
}
for (boolean right : color)
out.print(right ? 0 : 1);
}
}
class InputReader {
private static BufferedReader bufferedReader;
private static StringTokenizer stringTokenizer;
public InputReader(InputStream inputStream) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
stringTokenizer = null;
}
public String next() {
while(stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
try {
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return stringTokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | a36247c8437178765c2d8c8b7ca861c1 | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.util.ArrayDeque;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Queue;
import java.util.Collection;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Niyaz Nigmatullin
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, FastScanner in, FastPrinter out) {
int n = in.nextInt();
int m = in.nextInt();
int[] from = new int[m];
int[] to = new int[m];
for (int i = 0; i < m; i++) {
from[i] = in.nextInt() - 1;
to[i] = in.nextInt() - 1;
}
int[][] edges = GraphUtils.getEdgesUndirectedUnweighted(n, from, to);
Queue<Integer> q = new ArrayDeque<Integer>();
int[] color = new int[n];
for (int i = 0; i < n; i++) {
q.add(i);
}
boolean[] inq = new boolean[n];
Arrays.fill(inq, true);
while (!q.isEmpty()) {
int v = q.poll();
inq[v] = false;
int cnt = 0;
for (int j : edges[v]) {
if (color[v] == color[j]) {
++cnt;
}
}
if (cnt < 2) {
continue;
}
color[v] ^= 1;
for (int j : edges[v]) {
if (inq[j]) {
continue;
}
cnt = 0;
for (int k : edges[j]) {
if (color[k] == color[j]) {
++cnt;
}
}
if (cnt > 1) {
inq[j] = true;
q.add(j);
}
}
}
for (int i = 0; i < n; i++) {
out.print(color[i]);
}
out.println();
}
}
class FastScanner extends BufferedReader {
boolean isEOF;
public FastScanner(InputStream is) {
super(new InputStreamReader(is));
}
public int read() {
try {
int ret = super.read();
if (isEOF && ret < 0) {
throw new InputMismatchException();
}
isEOF = ret == -1;
return ret;
} catch (IOException e) {
throw new InputMismatchException();
}
}
static boolean isWhiteSpace(int c) {
return c >= -1 && c <= 32;
}
public int nextInt() {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int ret = 0;
while (!isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new NumberFormatException("digit expected " + (char) c
+ " found");
}
ret = ret * 10 + c - '0';
c = read();
}
return ret * sgn;
}
}
class FastPrinter extends PrintWriter {
public FastPrinter(OutputStream out) {
super(out);
}
public FastPrinter(Writer out) {
super(out);
}
}
class GraphUtils {
public static int[][] getEdgesUndirectedUnweighted(int n, int[] v, int[] u) {
int[][] edges = new int[n][];
int[] deg = getDegreeUndirected(n, v, u);
for (int i = 0; i < n; i++) {
edges[i] = new int[deg[i]];
}
int m = v.length;
Arrays.fill(deg, 0);
for (int i = 0; i < m; i++) {
edges[v[i]][deg[v[i]]++] = u[i];
edges[u[i]][deg[u[i]]++] = v[i];
}
return edges;
}
public static int[] getDegreeUndirected(int n, int[] v, int[] u) {
int[] deg = new int[n];
for (int i : v) {
deg[i]++;
}
for (int i : u) {
deg[i]++;
}
return deg;
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | e3e23087adc1befc6a7354f946231bff | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
}
class Task {
public void solve(int testNumber, InputReader in, OutputWriter out) {
n = in.readInt();
m = in.readInt();
g = new ArrayList[n];
for (int i = 0; i < n; i++)
g[i] = new ArrayList<Integer>();
for (int i = 0; i < m; i++){
int a = in.readInt() - 1, b = in.readInt() - 1;
g[a].add(b);
g[b].add(a);
}
color = new int[n];
count = new int[n];
Arrays.fill(color, 0);
Arrays.fill(count, 0);
was = new boolean[n];
Arrays.fill(was, false);
for (int i = 0; i < n; i++)
if (!was[i])
dfs(i, 0);
for (int i : color)
out.print(i);
}
void dfs(int v, int clr){
if (was[v])
return ;
was[v] = true;
color[v] = clr;
for (int to : g[v]){
dfs(to, clr ^ 1);
count[v] += color[to] == clr ? 1 : 0;
}
if (count[v] > 1)
color[v] = clr ^ 1;
}
int n, m;
int[] color, count;
boolean[] was;
ArrayList<Integer>[] g;
}
class InputReader {
public InputReader(InputStream inputStream){
in = new BufferedReader(new InputStreamReader(inputStream));
}
public InputReader(Reader reader){
in = new BufferedReader(reader);
}
public String nextLine(){
String res = null;
try {
res = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
public String readWord(){
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(nextLine());
return tokenizer.nextToken();
}
public int readInt(){
return Integer.parseInt(readWord());
}
private StringTokenizer tokenizer;
private BufferedReader in;
}
class OutputWriter {
public OutputWriter(OutputStream outputStream){
out = new PrintWriter(new OutputStreamWriter(outputStream));
}
public OutputWriter(Writer writer){
out = new PrintWriter(writer);
}
public void print(Object...args){
int size = args.length;
for (int i = 0; i < size; i++)
out.print(args[i]);
}
public void close(){
out.close();
}
private PrintWriter out;
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | 2d034d87406e5f1672e9e2290ee51e71 | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class M {
boolean res[];
int g[];
List<List<Integer>> ll;
int n, m;
PriorityQueue<Integer> pq;
private void solve() throws IOException {
n = nextInt();
m = nextInt();
g = new int[n];
res = new boolean[n];
ll = new ArrayList<>(n);
for (int i = 0; i < n; ++i)
ll.add(new ArrayList<Integer>(3));
for (int i = 0; i < m; ++i) {
int u = nextInt() - 1;
int v = nextInt() - 1;
++g[u];
++g[v];
ll.get(u).add(v);
ll.get(v).add(u);
}
pq = new PriorityQueue<>();
for (int i = 0; i < n; ++i)
if (g[i] >= 2)
pq.add(i);
while (!pq.isEmpty()) {
int v = pq.poll();
if (g[v] < 2)
continue;
inverse(v);
}
for (int i = 0; i < n; ++i)
print(res[i] ? '1' : '0');
}
private void inverse(int v) {
res[v] = !res[v];
g[v] = 3 - g[v];
for (Integer i : ll.get(v))
if (res[i] == res[v]) {
++g[i];
if (g[i] >= 2)
pq.add(i);
} else
--g[i];
}
public static void main(String[] args) {
new M().run();
}
public void run() {
try {
if (isFileIO) {
pw = new PrintWriter(new File("output.out"));
br = new BufferedReader(new FileReader("input.in"));
} else {
pw = new PrintWriter(System.out);
br = new BufferedReader(new InputStreamReader(System.in));
}
solve();
pw.close();
br.close();
} catch (IOException e) {
System.err.println("IO Error");
}
}
private void print(Object o) {
pw.print(o);
}
private void println(Object o) {
pw.println(o);
}
private void println() {
pw.println();
}
int[] nextIntArray(int n) throws IOException {
int arr[] = new int[n];
for (int i = 0; i < n; ++i)
arr[i] = Integer.parseInt(nextToken());
return arr;
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(br.readLine());
}
return tokenizer.nextToken();
}
private BufferedReader br;
private StringTokenizer tokenizer;
private PrintWriter pw;
private final boolean isFileIO = false;
} | Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | b88a5f8e7eacced164cd91da7a53aec1 | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.*;
import java.util.*;
public class C implements Runnable {
public static void main(String[] args) {
new Thread(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 n = nextInt();
ArrayList<Integer>[] e = new ArrayList[n];
for (int i = 0; i < e.length; i++) {
e[i] = new ArrayList<Integer>();
}
int[] type = new int[n];
int[] degree = new int[n];
int m = nextInt();
for (int i = 0; i < m; i++) {
int u = nextInt() - 1;
int v = nextInt() - 1;
e[u].add(v);
e[v].add(u);
}
for (int i = 0; i < degree.length; i++) {
degree[i] = e[i].size();
}
Queue<Integer> q = new ArrayDeque<Integer>();
for (int i = 0; i < degree.length; i++) {
if (degree[i] >= 2)
q.add(i);
}
// for (int i = 0; i < 3 * m; i++) {
while (q.size() > 0) {
int u = q.poll();
if (degree[u] < 2)
continue;
type[u] = 1 - type[u];
degree[u] = 0;
for (int v : e[u]) {
if (type[v] == type[u]) {
degree[v]++;
if (degree[v] >= 2)
q.add(v);
degree[u]++;
} else {
degree[v]--;
}
}
}
if (q.size() > 0) {
out.println(-1);
return;
}
for (int i = 0; i < degree.length; i++) {
out.print(type[i]);
}
}
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 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | b06f0e97fb2f3fddbd91f63a7123333f | train_002.jsonl | 1360769400 | Dima came to the horse land. There are n horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
InputReader reader;
PrintWriter writer;
Main() {
reader = new InputReader();
writer = new PrintWriter(System.out);
}
public static void main(String[] args) {
new Main().run();
}
public void run() {
try {
int n = reader.nextInt();
int m = reader.nextInt();
int[] degree = new int[n];
int[][] graph = new int[n][3];
for (int i = 0; i < m; ++ i) {
int a = reader.nextInt() - 1;
int b = reader.nextInt() - 1;
graph[a][degree[a] ++] = b;
graph[b][degree[b] ++] = a;
}
int[] color = new int[n];
int[] conflict = degree.clone();
Queue <Integer> queue = new LinkedList <Integer>();
for (int i = 0; i < n; ++ i) {
if (conflict[i] > 1) {
queue.offer(i);
}
}
boolean[] visit = new boolean[n];
Arrays.fill(visit, true);
while (!queue.isEmpty()) {
int u = queue.poll();
visit[u] = false;
if (conflict[u] <= 1) {
continue;
}
color[u] ^= 1;
conflict[u] = degree[u] - conflict[u];
for (int i = 0; i < degree[u]; ++ i) {
int v = graph[u][i];
conflict[v] += color[u] == color[v] ? 1 : -1;
if (conflict[v] > 1 && !visit[v]) {
visit[v] = true;
queue.offer(v);
}
}
}
for (int i = 0; i < n; ++ i) {
writer.print(color[i]);
}
writer.println();
} catch (IOException ex) {
}
writer.close();
}
}
class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = new StringTokenizer("");
}
String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
Integer nextInt() throws IOException {
return Integer.parseInt(next());
}
}
| Java | ["3 3\n1 2\n3 2\n3 1", "2 1\n2 1", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["100", "00", "0110000000"] | null | Java 7 | standard input | [
"greedy",
"graphs"
] | 7017f2c81d5aed716b90e46480f96582 | The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs. Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi. Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. | 2,200 | Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. | standard output | |
PASSED | 24ccea37acc1f7e0624f10ba48e92283 | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 256 megabytes | import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
private static boolean local = false;
public static void main(String[] args) throws Exception {
InputStream inputStream = System.in;
if (local) {
inputStream = new FileInputStream("/home/dhaya/projects/codeforces/src/in4");
}
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
try {
Task t = new Task();
t.solve(in, out);
} finally {
if (local) {
inputStream.close();
}
}
out.close();
}
private static class Task {
private void solve(Scanner in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
char[][] letters = new char[n][];
for (int i = 0; i < n; i++) {
letters[i] = in.next().toCharArray();
}
Set<Integer> removedCols = new HashSet<>();
solve1(letters, 0, n-1, 0, m-1, removedCols);
out.println(removedCols.size());
//out.println(removedCols);
}
private void solve1(char[][] letters,
int rstart,
int rend,
int colStart,
int colEnd,
Set<Integer> removedCols) {
if (rstart == rend || colStart > colEnd) return;
if (removedCols.contains(colStart)) {
solve1(letters, rstart, rend, colStart + 1, colEnd, removedCols);
return;
}
// atleast 2 rows
char toCmp = letters[rend][colStart];
int row = rend - 1;
do {
char curr = letters[row][colStart];
if (curr > toCmp) {
// not sorted
removedCols.add(colStart);
solve1(letters, rstart, rend, colStart+1, colEnd, removedCols);
} else if (curr == toCmp) {
int eqStart = row + 1;
while (row >= rstart && letters[row][colStart] == curr) {
row--;
}
solve1(letters, row + 1, eqStart, colStart + 1, colEnd, removedCols);
} else {
row--;
}
toCmp = curr;
} while (row >= rstart);
}
}
}
| Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | 4d2baf94460bb87ea6d364d85eb75c10 | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Mouna Cheikhna
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
ARemovingColumns solver = new ARemovingColumns();
solver.solve(1, in, out);
out.close();
}
static class ARemovingColumns {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int count = 0;
String[] arr = new String[n];
for (int i = 0; i < n; i++) {
arr[i] = in.next();
}
for (int col = 0; col < arr[0].length(); col++) {
boolean isSorted = true;
for (int i = 0; i < n - 1; i++) {
if (arr[i].substring(0, col + 1).compareTo(arr[i + 1].substring(0, col + 1)) > 0) {
isSorted = false;
break;
}
}
if (!isSorted) {
count++;
removeCol(arr, col);
col--;
}
}
out.println(count);
}
private void removeCol(String[] arr, int j) {
for (int i = 0; i < arr.length; i++) {
String s = arr[i];
arr[i] = s.substring(0, Math.max(0, j)) + s.substring(Math.min(s.length(), j + 1), s.length());
}
}
}
}
| Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | a6ed8ca2d23302b5b8b64c6774e3c293 | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
/**
*
* @author Saju
*
*/
public class Main {
private static int dx[] = { 1, 0, -1, 0 };
private static int dy[] = { 0, -1, 0, 1 };
private static final long INF = Long.MAX_VALUE;
private static final int INT_INF = Integer.MAX_VALUE;
private static final long NEG_INF = Long.MIN_VALUE;
private static final int NEG_INT_INF = Integer.MIN_VALUE;
private static final double EPSILON = 1e-10;
private static final int MAX = 10000007;
private static final long MOD = 1000000007;
private static final int MAXN = 100007;
private static final int MAXA = 10000009;
private static final int MAXLOG = 22;
public static void main(String[] args) throws IOException {
InputReader in = new InputReader(System.in);
// Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
// InputReader in = new InputReader(new FileInputStream("src/test.in"));
// PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("src/test.out")));
/*
*/
int n = in.nextInt();
int m = in.nextInt();
StringBuilder[] sbs = new StringBuilder[n];
for(int i = 0; i < n; i++) {
String s = in.next();
sbs[i] = new StringBuilder(s);
}
int cnt = 0;
for(int j = 0; j < m; j++) {
if(!sorted(j, n, sbs)) {
if(!isOk(j, n, m, sbs)) {
cnt++;
for(int i = 0; i < n; i++) {
sbs[i].setCharAt(j, '*');
}
// System.out.println(j);
}
}
}
out.println(cnt);
// for(int i = 0; i < n; i++) {
// System.out.println(sbs[i]);
// }
in.close();
out.flush();
out.close();
System.exit(0);
}
private static boolean sorted(int j, int n, StringBuilder[] strs) {
String newS[] = new String[n];
for(int i = 0; i < n; i++) {
newS[i] = new String(strs[i].substring(0, j + 1).toString());
}
Arrays.sort(newS);
for(int i = 0; i < n; i++) {
if(!newS[i].equals(strs[i].substring(0, j + 1).toString())) {
return false;
}
}
return true;
}
private static boolean isOk(int j, int n, int m, StringBuilder[] strs) {
char ch[] = new char[n];
for(int i = 0; i < n; i++) {
ch[i] = strs[i].charAt(j);
}
Arrays.sort(ch);
for(int i = 0; i < n; i++) {
if(ch[i] != strs[i].charAt(j)) {
return false;
}
}
return true;
}
private static boolean isPalindrome(String str) {
StringBuilder sb = new StringBuilder();
sb.append(str);
String str1 = sb.reverse().toString();
return str.equals(str1);
}
private static String getBinaryStr(int n, int j) {
String str = Integer.toBinaryString(n);
int k = str.length();
for (int i = 1; i <= j - k; i++) {
str = "0" + str;
}
return str;
}
private static String getBinaryStr(long n, int j) {
String str = Long.toBinaryString(n);
int k = str.length();
for (int i = 1; i <= j - k; i++) {
str = "0" + str;
}
return str;
}
private static long bigMod(long n, long k, long m) {
long ans = 1;
while (k > 0) {
if ((k & 1) == 1) {
ans = (ans * n) % m;
}
n = (n * n) % m;
k >>= 1;
}
return ans;
}
private static long ceil(long n, long x) {
long div = n / x;
if(div * x != n) {
div++;
}
return div;
}
private static int ceil(int n, int x) {
int div = n / x;
if(div * x != n) {
div++;
}
return div;
}
private static int abs(int x) {
if (x < 0) {
return -x;
}
return x;
}
private static long abs(long x) {
if(x < 0) {
return -x;
}
return x;
}
private static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
private static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
private static int log(long x, int base) {
return (int) (Math.log(x) / Math.log(base));
}
private static long min(long a, long b) {
if (a < b) {
return a;
}
return b;
}
private static int min(int a, int b) {
if (a < b) {
return a;
}
return b;
}
private static long max(long a, long b) {
if (a < b) {
return b;
}
return a;
}
private static int max(int a, int b) {
if (a < b) {
return b;
}
return a;
}
private static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (IOException e) {
return null;
}
return tokenizer.nextToken();
}
public String nextLine() {
String line = null;
try {
tokenizer = null;
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public boolean hasNext() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public int[] nextIntArr(int n) {
int arr[] = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArr(int n) {
long arr[] = new long[n];
for(int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public int[] nextIntArr1(int n) {
int arr[] = new int[n + 1];
for(int i = 1; i <= n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArr1(int n) {
long arr[] = new long[n + 1];
for(int i = 1; i <= n; i++) {
arr[i] = nextLong();
}
return arr;
}
public void close() {
try {
if(reader != null) {
reader.close();
}
}
catch(Exception e) {
}
}
}
}
| Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | 099edc22b31735e651716c7aaa15dba3 | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF {
FastScanner in;
PrintWriter out;
void solve() {
int n = in.nextInt();
int m = in.nextInt();
char[][] a = new char[n][];
for (int i = 0; i < n; i++) {
a[i] = in.next().toCharArray();
}
ArrayList<Integer> from = new ArrayList<>();
ArrayList<Integer> to = new ArrayList<>();
from.add(0);
to.add(n - 1);
int ans = 0;
for (int col = 0; col < m; col++) {
boolean ok = true;
for (int i = 0; i < from.size(); i++) {
int f = from.get(i);
int t = to.get(i);
for (int x = f; x < t; x++) {
if (a[x][col] > a[x + 1][col] ){
ok = false;
}
}
}
if (ok) {
ArrayList<Integer> from2 = new ArrayList<>();
ArrayList<Integer> to2 = new ArrayList<>();
for (int i = 0; i < from.size(); i++) {
int f = from.get(i);
int t = to.get(i);
int curF = f;
for (int x = f; x < t; x++) {
if (a[x][col] < a[x + 1][col] ){
from2.add(curF);
to2.add(x);
curF = x + 1;
}
}
from2.add(curF);
to2.add(t);
}
from = from2;
to = to2;
} else {
ans++;
}
}
out.println(ans);
}
void runIO() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
void run() {
try {
in = new FastScanner(new File("test.in"));
out = new PrintWriter(new File("test.out"));
solve();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) {
new CF().runIO();
}
} | Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | 9270c9c8bbd9fdc092ea7fbd5b421db9 | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
public class Codeforces {
static double Phi = (1 + Math.sqrt(5)) / 2.0;
static int MAX = -1 >>> 1;
static long MODL = 1_000_000_007L;
static int MOD = 1_000_000_007;
static void no() {println("NO");}
static void yes() {println("YES");}
static void main2() throws Exception {
int n = ni();
int m = ni();
String[] arr = new String[n];
for(int i = 0; i < n; i++) {
arr[i] = line();
}
int count = 0;
while(true) {
boolean isFound = false;
int cFound = 0;
for(int i = 0; i < n - 1; i++) {
for(int j = 0; j < m; j++) {
if(arr[i].charAt(j) > arr[i + 1].charAt(j)) {
isFound = true;
cFound = j;
break;
} else if(arr[i].charAt(j) == arr[i + 1].charAt(j)) {
} else {
break;
}
}
}
if(isFound) {
++count;
for(int i = 0; i < n; i++) {
arr[i] = arr[i].substring(0, cFound) + arr[i].substring(cFound + 1);
}
--m;
// for(String e : arr) {
// println(e);
// }
// println("");
} else {
break;
}
}
println(count);
}
// /$$
// | $$
// /$$$$$$$| $$ /$$$$$$ /$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$$
// /$$_____/| $$ |____ $$ /$$_____//$$_____/ /$$__ $$ /$$_____/
// | $$ | $$ /$$$$$$$| $$$$$$| $$$$$$ | $$$$$$$$| $$$$$$
// | $$ | $$ /$$__ $$ \____ $$\____ $$| $$_____/ \____ $$
// | $$$$$$$| $$| $$$$$$$ /$$$$$$$//$$$$$$$/| $$$$$$$ /$$$$$$$/
// \_______/|__/ \_______/|_______/|_______/ \_______/|_______/
static class Pair <L, R> {
L first;
R second;
Pair(L first, R second) {
this.first = first;
this.second = second;
}
Pair(Pair<L, R> p) {
this.first = p.first;
this.second = p.second;
}
public boolean equals(Object p2) {
if (p2 instanceof Pair) {
return ((Pair) p2).first.equals(first) && ((Pair) p2).second.equals(second);
}
return false;
}
public String toString() {
return "(first=" + first.toString() + ",second=" + second.toString() + ")";
}
}
static class DisjointSet {
int[] arr;
int[] size;
boolean enableSize = true;
DisjointSet(int n) {
arr = new int[n + 1];
this.enableSize = enableSize;
if (this.enableSize) size = new int[n + 1];
makeSet();
}
void makeSet() {
for (int i = 1; i < arr.length; i++) {
arr[i] = i;
if (enableSize) size[i] = 1;
}
}
void union(int i, int j) {
i = find(i);
j = find(j);
if (i == j)
return;
arr[j] = arr[i];
if (enableSize) {
size[i] += size[j];
size[j] = size[i];
}
}
int find(int i) {
if (i == arr[i]) return i;
return arr[i] = find(arr[i]);
}
int getSize(int i) {
if (enableSize) return size[find(i)];
return 0;
}
public String toString() {
return Arrays.toString(arr);
}
}
// /$$ /$$ /$$
// | $$ |__/| $$
// /$$ /$$ /$$$$$$ /$$| $$ /$$$$$$$
// | $$ | $$|_ $$_/ | $$| $$ /$$_____/
// | $$ | $$ | $$ | $$| $$| $$$$$$
// | $$ | $$ | $$ /$$| $$| $$ \____ $$
// | $$$$$$/ | $$$$/| $$| $$ /$$$$$$$/
// \______/ \___/ |__/|__/|_______/
static int diff(int a, int b) {
return abs(a - b);
}
static long diff(long a, long b) {
return abs(a - b);
}
static boolean isSqrt(double a) {
double sr = Math.sqrt(a);
return ((sr - Math.floor(sr)) == 0);
}
static long abs(long a) {
return Math.abs(a);
}
static int abs(int a) {
return Math.abs(a);
}
static int min(int ... arr) {
int min = Integer.MAX_VALUE;
for (int var : arr)
min = Math.min(min, var);
return min;
}
static long min(long ... arr) {
long min = Long.MAX_VALUE;
for (long var : arr)
min = Math.min(min, var);
return min;
}
static long max(long ... arr) {
long max = Long.MIN_VALUE;
for (long var : arr)
max = Math.max(max, var);
return max;
}
static int max(int... arr) {
int max = Integer.MIN_VALUE;
for (int var : arr)
max = Math.max(max, var);
return max;
}
static long modInverse(long a) {
long[] g = gcde(a, MODL);
long x = g[1], y = g[2];
if (g[0] != 1)
return 0;
else {
// m is added to handle negative x
return (g[1] % MODL + MODL) % MODL;
}
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long[] gcde(long a, long b) {
// Base Case
if (a == 0) {
return new long[] {b, 0, 1};
}
long[] gcd = gcde(b % a, a);
long x1 = gcd[1], y1 = gcd[2]; // To store results of recursive call
// Update x and y using results of recursive
// call
gcd[1] = y1 - (b / a) * x1;
gcd[2] = x1;
return gcd;
}
// ____________________________________________________________________________
//| |
//| /$$$$$$ /$$$$$$ /$$$$$$ /$$ /$$$$$$ /$$$$$$ |
//| |_ $$_/ /$$__ $$ /$$__ $$ | $$ /$$__ $$ /$$__ $$ |
//| | $$ | $$ \ $$ | $$ \__//$$$$$$ /$$ /$$| $$ \__/| $$ \__/ |
//| | $$ | $$ | $$ | $$$$$$|_ $$_/ | $$ | $$| $$$$ | $$$$ |
//| | $$ | $$ | $$ \____ $$ | $$ | $$ | $$| $$_/ | $$_/ |
//| | $$ | $$ | $$ /$$ \ $$ | $$ /$$| $$ | $$| $$ | $$ |
//| /$$$$$$| $$$$$$/ | $$$$$$/ | $$$$/| $$$$$$/| $$ | $$ |
//| |______/ \______/ \______/ \___/ \______/ |__/ |__/ |
//|____________________________________________________________________________|
private static byte[] scannerByteBuffer = new byte[1024]; // Buffer of Bytes
private static int scannerIndex;
private static InputStream scannerIn;
private static int scannerTotal;
private static BufferedWriter printerBW;
private static boolean DEBUG = false;
private static int next() throws IOException { // Scan method used to scan buf
if (scannerTotal < 0)
throw new InputMismatchException();
if (scannerIndex >= scannerTotal) {
scannerIndex = 0;
scannerTotal = scannerIn.read(scannerByteBuffer);
if (scannerTotal <= 0)
return -1;
}
return scannerByteBuffer[scannerIndex++];
}
static int ni() throws IOException {
int integer = 0;
int n = next();
while (isWhiteSpace(n)) // Removing startPointing whitespaces
n = next();
int neg = 1;
if (n == '-') { // If Negative Sign encounters
neg = -1;
n = next();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = next();
} else
throw new InputMismatchException();
}
return neg * integer;
}
static long nl() throws IOException {
long integer = 0;
int n = next();
while (isWhiteSpace(n)) // Removing startPointing whitespaces
n = next();
int neg = 1;
if (n == '-') { // If Negative Sign encounters
neg = -1;
n = next();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = next();
} else
throw new InputMismatchException();
}
return neg * integer;
}
static String line() throws IOException {
StringBuilder sb = new StringBuilder();
int n = next();
while (isWhiteSpace(n))
n = next();
while (!isNewLine(n)) {
sb.append((char) n);
n = next();
}
return sb.toString();
}
private static boolean isNewLine(int n) {
return n == '\n' || n == '\r' || n == -1;
}
private static boolean isWhiteSpace(int n) {
return n == ' ' || isNewLine(n) || n == '\t';
}
static int[] nia(int n) throws Exception {
if (n < 0)
throw new Exception("Array size should be non negative");
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = ni();
return array;
}
static int[][] n2dia(int r, int c) throws Exception {
if (r < 0 || c < 0)
throw new Exception("Array size should be non negative");
int[][] array = new int[r][c];
for (int i = 0; i < r; i++)
array[i] = nia(c);
return array;
}
static long[] nla(int n) throws Exception {
if (n < 0)
throw new Exception("Array size should be non negative");
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nl();
return array;
}
static float[] nfa(int n) throws Exception {
if (n < 0)
throw new Exception("Array size should be non negative");
float[] array = new float[n];
for (int i = 0; i < n; i++)
array[i] = nl();
return array;
}
static double[] nda(int n) throws Exception {
if (n < 0)
throw new Exception("Array size should be non negative");
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nl();
return array;
}
static <T> void print(T ... str) {
try {
for (T ele : str)
printerBW.append(ele.toString());
if (DEBUG)
flush();
} catch (IOException e) {
System.out.println(e.toString());
}
}
static <T> void println(T ... str) {
if (str.length == 0) {
print('\n');
return;
}
for (T ele : str)
print(ele, '\n');
}
static void flush() throws IOException {
printerBW.flush();
}
static void close() {
try {
printerBW.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
public static void main(String[] args) throws Exception {
long startPointTime = System.currentTimeMillis();
scannerIn = System.in;
printerBW = new BufferedWriter(new OutputStreamWriter(System.out));
if (args.length > 0 && args[0].equalsIgnoreCase("debug")
|| args.length > 1 && args[1].equalsIgnoreCase("debug"))
DEBUG = true;
main2();
long endTime = System.currentTimeMillis();
float totalProgramTime = endTime - startPointTime;
if (args.length > 0 && args[0].equalsIgnoreCase("time") || args.length > 1 && args[1].equalsIgnoreCase("time"))
print("Execution time is " + totalProgramTime + " (" + (totalProgramTime / 1000) + "s)");
close();
}
}
| Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | 12fce31e36b09e89a2c719632dd007eb | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 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.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author fintech
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
RemovingColumns solver = new RemovingColumns();
solver.solve(1, in, out);
out.close();
}
static class RemovingColumns {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
String[] data = new String[n];
for (int i = 0; i < n; ++i) data[i] = in.next();
boolean[] eq = new boolean[n - 1];
Arrays.fill(eq, true);
int res = 0;
for (int j = 0; j < m; ++j) {
boolean ok = true;
for (int i = 0; i + 1 < n; ++i)
if (eq[i] && data[i].charAt(j) > data[i + 1].charAt(j)) ok = false;
if (!ok) {
++res;
continue;
}
for (int i = 0; i + 1 < n; ++i)
if (data[i].charAt(j) < data[i + 1].charAt(j)) eq[i] = false;
}
out.println(res);
}
}
}
| Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | 2f4d8b3c6a96fc7b0c6127f2f55d711f | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 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.List;
import java.util.StringTokenizer;
public class Main {
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter writer;
static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
static boolean eof = false;
static String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static void main(String[] args) throws IOException {
tokenizer = null;
// reader = new BufferedReader(new FileReader("grave.in"));
// writer = new PrintWriter(new FileWriter("grave.out"));
reader = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-1"));
writer = new PrintWriter(System.out);
banana();
reader.close();
writer.close();
}
static String s[];
static int counter = 0;
static void dfs(int beg, int end, int col) {
}
static void banana() throws IOException {
int n = nextInt();
int m = nextInt();
s = new String[n];
for (int i = 0; i < n; ++i)
s[i] = nextToken();
List<Integer> beg = new ArrayList<Integer>();
List<Integer> end = new ArrayList<Integer>();
beg.add(0);
end.add(n - 1);
for (int j = 0; j < m; ++j) {
if (beg.size() == 0)
break;
List<Integer> newBeg = new ArrayList<Integer>();
List<Integer> newEnd = new ArrayList<Integer>();
boolean removed = false;
for (int i = 0; i < beg.size(); ++i) {
int x = beg.get(i);
int y = end.get(i);
if (x == y)
continue;
for (int l = x; l <= y-1; ++l)
if (s[l].charAt(j) > s[l+1].charAt(j))
removed = true;
int cur = x;
int endCur = x;
for (int k = x; k <= y - 1; ++k) {
if (s[k].charAt(j) == s[k + 1].charAt(j))
++endCur;
else {
newBeg.add(cur);
newEnd.add(endCur);
cur = endCur = k + 1;
}
}
newBeg.add(cur);
newEnd.add(endCur);
}
if (!removed) {
beg = newBeg;
end = newEnd;
}
else
++counter;
}
System.out.println(counter);
}
} | Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | c2712ea4c79e8822852916530627c932 | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 256 megabytes | // practice with rainboy
import java.io.*;
import java.util.*;
public class CF497A extends PrintWriter {
CF497A() { super(System.out, true); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF497A o = new CF497A(); o.main(); o.flush();
}
void main() {
int n = sc.nextInt();
int m = sc.nextInt();
byte[][] cc = new byte[n][];
for (int i = 0; i < n; i++)
cc[i] = sc.next().getBytes();
boolean[] gt = new boolean[n];
gt[0] = true;
int ans = 0;
for (int j = 0; j < m; j++) {
boolean bad = false;
for (int i = 0; i < n; i++)
if (!gt[i] && cc[i][j] < cc[i - 1][j]) {
bad = true;
break;
}
if (bad)
ans++;
else
for (int i = 0; i < n; i++)
if (!gt[i] && cc[i][j] > cc[i - 1][j])
gt[i] = true;
}
println(ans);
}
}
| Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | 073ec3555e971474f0eeac22b2e78487 | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Template {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA{
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
String[] data = new String[n];
for (int i = 0; i < n; ++i) data[i] = in.next();
boolean[] eq = new boolean[n - 1];
Arrays.fill(eq, true);
int res = 0;
for (int j = 0; j < m; ++j) {
boolean ok = true;
for (int i = 0; i + 1 < n; ++i)
if (eq[i] && data[i].charAt(j) > data[i + 1].charAt(j)) ok = false;
if (!ok) {
++res;
continue;
}
for (int i = 0; i + 1 < n; ++i)
if (data[i].charAt(j) < data[i + 1].charAt(j)) eq[i] = false;
}
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() {
if (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 double nextDouble() {
return Double.parseDouble(next());
}
}
| Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | 9865cb272a4718dec5222edf3fb49ba6 | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Task task = new Task();
System.out.println(task.solve());
}
}
class Task {
private int n, m;
boolean [][] correct = new boolean[105][105] ;
boolean [] deleted = new boolean [105] ;
Scanner in = new Scanner(System.in);
char[][] a = new char[105][105];
public int solve() {
n = in.nextInt();
m = in.nextInt();
for (int i = 0; i < n; i++) {
String word = in.next();
for (int j = 0; j < m ; j++) {
a[i][j] = word.charAt(j);
}
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < n - 1; i++) {
if (a[i][j] > a[i + 1][j]) {
if (checked(i+1)) {
continue;
}
deleted[j] = true;
correct[i][j] = false;
break;
}
if (a[i][j] < a[i + 1][j]) {
correct[i + 1][j] = true;
}
}
}
int ans = 0 ;
for(int i = 0 ; i < m ; i++){
if(deleted[i]){
ans ++ ;
}
}
return ans ;
}
private boolean checked(int i){
for(int j = 0 ; j < m ; j++){
if(correct[i][j] && !deleted[j]){
return true ;
}
}
return false ;
}
}
| Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | a0672af116179e9012fb446508b1ac16 | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 256 megabytes |
import java.util.*;
import java.io.*;
public class A {
public static long time = 0;
public static void main(String[] args) throws Exception {
time = System.currentTimeMillis();
IN = System.in;
OUT = System.out;
in = new BufferedReader(new InputStreamReader(IN));
out = new PrintWriter(OUT, FLUSH);
solveOne();
out.flush();
}
public static void solveOne() throws Exception {
int n1 = ni();
int m1 = ni();
String[] l1 = new String[n1];
for (int i = 0 ; i < n1; i++){
l1[i] = nx();
// px(l1[i]);
}
int ans = 0;
int[] dp1 = new int[n1];
dp1[0] = n1;
for (int idx = 0; idx < m1; idx++){
boolean good = true;
int[] next = new int[n1];
for (int i = 0; i < n1; i++){
int ptr = i;
int cnt = 1;
for (int j = 0; j < dp1[i];j ++){
if (j < dp1[i] - 1){
if (l1[i + j].charAt(idx) > l1[i + j + 1].charAt(idx) ){
good = false;
}
else if (l1[i + j].charAt(idx) == l1[i + j + 1].charAt(idx) ){
cnt++;
}
else {
ptr = i + j + 1;
cnt = 1;
}
}
next[ptr] = cnt;
}
}
if (good){
dp1 = next;
}
else {
ans ++;
}
// px(dp1);
}
out.println(ans);
}
public static void solveTwo() throws Exception {
}
public static void solveThree() throws Exception {
}
public static BufferedReader in;
public static StringTokenizer st;
public static InputStream IN;
public static OutputStream OUT;
public static String nx() throws Exception {
for (;st == null || !st.hasMoreTokens();){
String k1 = in.readLine();
if (k1 == null) return null;
st = new StringTokenizer(k1);
}
return st.nextToken();
}
public static int ni () throws Exception {
return Integer.parseInt(nx());
}
public static long nl() throws Exception{
return Long.parseLong(nx());
}
public static double nd() throws Exception{
return Double.parseDouble(nx());
}
public static void px(Object ... l1){
System.out.println(Arrays.deepToString(l1));
}
public static boolean FLUSH = false;
public static PrintWriter out;
public static void p(Object ... l1){
for (int i = 0; i < l1.length; i++){
if (i != 0) out.print(' ');
out.print(l1[i].toString());
}
}
public static void pn(Object ... l1){
for (int i = 0; i < l1.length; i++){
if (i != 0) out.print(' ');
out.print(l1[i].toString());
}
out.println();
}
public static void pn(Collection l1){
boolean first = true;
for (Object e: l1){
if (first) first = false;
else out.print(' ');
out.print(e.toString());
}
out.println();
}
}
| Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | fc0181ceca2d26d8ecf7e219460554ee | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.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);
ProblemARemovingColumns solver = new ProblemARemovingColumns();
solver.solve(1, in, out);
out.close();
}
static class ProblemARemovingColumns {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.readInt();
int m = in.readInt();
char[][] a = new char[n][m];
for (int i = 0; i < n; i++) a[i] = in.readLine().toCharArray();
boolean[] unused = new boolean[m];
int ans = 0;
l1:
for (int i = 0; i < m; i++) {
for (int j = 0; j < n - 1; j++) {
for (int k = 0; k < m; k++) {
if (unused[k]) continue;
if (a[j][k] > a[j + 1][k]) {
unused[k] = true;
ans++;
continue l1;
} else if (a[j][k] < a[j + 1][k]) break;
}
}
}
out.println(ans);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | 37ce76005df007d46eb71d0ed27f01ec | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 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
*/
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);
ProblemARemovingColumns solver = new ProblemARemovingColumns();
solver.solve(1, in, out);
out.close();
}
static class ProblemARemovingColumns {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.readInt();
int m = in.readInt();
char[][] a = new char[n][m];
for (int i = 0; i < n; i++) a[i] = in.readLine().toCharArray();
boolean[] eq = new boolean[n];
Arrays.fill(eq, true);
int ans = 0;
for (int j = 0; j < m; j++) {
boolean valid = true;
for (int i = 0; i < n - 1; i++) {
if (eq[i] && a[i][j] > a[i + 1][j]) valid = false;
}
if (!valid) {
ans++;
continue;
}
for (int i = 0; i < n - 1; i++) {
if (a[i][j] != a[i + 1][j]) eq[i] = false;
}
}
out.println(ans);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | 11d2bd0fb42d11812eff0f572025507d | train_002.jsonl | 1418833800 | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class RemovingColumns {
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter w = new PrintWriter(System.out);
StringTokenizer st1 = new StringTokenizer(br.readLine());
int n = ip(st1.nextToken());
int m = ip(st1.nextToken());
char a[][] = new char[n][m];
for(int i=0;i<n;i++)
a[i] = br.readLine().toCharArray();
boolean colRem[] = new boolean[m];
int ans = 0;
for(int j=0;j<m;j++){
// System.out.println(breaks(a,j,colRem,n));
if(breaks(a,j,colRem,n)){
colRem[j] = true;
ans++;
}
}
w.println(ans);
w.close();
}
public static boolean breaks(char a[][],int col,boolean colRem[],int n){
String checks[] = new String[n];
for(int i=0;i<n;i++)
checks[i] = new String("");
for(int i=0;i<n;i++){
for(int j=0;j<=col;j++){
if(colRem[j]!=true)
checks[i] += a[i][j];
}
}
for(int i=0;i<n-1;i++){
//System.out.println(checks[i] + " " + checks[i+1]);
if(checks[i].compareTo(checks[i+1]) > 0 )
return true;
}
return false;
}
public static int ip(String s){
return Integer.parseInt(s);
}
}
| Java | ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"] | 2 seconds | ["0", "2", "4"] | NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | Java 8 | standard input | [
"greedy"
] | a45cfc2855f82f162133930d9834a9f0 | The first line contains two integers — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. | 1,500 | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | standard output | |
PASSED | 5ba8e006299575f81bd5c74364ce4d5b | train_002.jsonl | 1581604500 | Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $$$a$$$ of $$$n$$$ non-negative integers.Dark created that array $$$1000$$$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) and replaces all missing elements in the array $$$a$$$ with $$$k$$$.Let $$$m$$$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $$$|a_i - a_{i+1}|$$$ for all $$$1 \leq i \leq n - 1$$$) in the array $$$a$$$ after Dark replaces all missing elements with $$$k$$$.Dark should choose an integer $$$k$$$ so that $$$m$$$ is minimized. Can you help him? | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
public static class pair{
int x;
int y;
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t-->0){
int n=Integer.parseInt(br.readLine());
int[] arr=new int[n];
String line=br.readLine();
String[] strs=line.trim().split(" ");
long sum=0;
int count=0;
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(strs[i]);
}
int max=Integer.MIN_VALUE;
int min=Integer.MAX_VALUE;
for(int i=0;i<n;i++){
if(arr[i]!=-1){
if(i-1>=0&&arr[i-1]==-1){
max=Math.max(max,arr[i]);
min=Math.min(min,arr[i]);
}
else if(i+1<n&&arr[i+1]==-1){
max=Math.max(max,arr[i]);
min=Math.min(min,arr[i]);
}
}
}
if(min==Integer.MAX_VALUE){
System.out.println("0 0");
continue;
}
int val=(min+max)/2;
int[] a1=new int[n];
int[] a2=new int[n];
for(int i=0;i<n;i++){
if(arr[i]==-1){
a1[i]=val;
a2[i]=val+1;
}else{
a1[i]=arr[i];
a2[i]=arr[i];
}
}
int max1=Integer.MIN_VALUE;
int max2=Integer.MIN_VALUE;
for(int i=0;i<n-1;i++){
max1=Math.max(max1,Math.abs(a1[i]-a1[i+1]));
max2=Math.max(max2,Math.abs(a2[i]-a2[i+1]));
}
if(max1<max2)
System.out.println(max1+" "+val);
else
System.out.println(max2+" "+(val+1));
}
}
public static int comb(int val){
long v=1;
for(int i=1;i<=val;i++){
v*=i;
}
long v2=1;
for(int i=1;i<=val-2;i++){
v2*=i;
}
long a=v/v2;
a=a/2;
return (int)a;
}
public static class Comp implements Comparator<pair>{
public int compare(pair a,pair b){
if(a.x!=b.x){
return b.x-a.x;
}else{
return a.y-b.y;
}
}
}
public static int gcd(int a,int b){
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int lcm(int a,int b){
int x=Math.max(a,b);
int y=Math.min(a,b);
long ans=x;
while(ans%y!=0){
ans+=x;
}
if(ans>Integer.MAX_VALUE){
return -1;
}
return (int)ans;
}
public static long fact(int n){
long ans=1;
for(int i=1;i<=n;i++){
ans*=i;
}
return ans;
}
} | Java | ["7\n5\n-1 10 -1 12 -1\n5\n-1 40 35 -1 35\n6\n-1 -1 9 -1 3 -1\n2\n-1 -1\n2\n0 -1\n4\n1 -1 3 -1\n7\n1 -1 7 5 2 -1 5"] | 2 seconds | ["1 11\n5 35\n3 6\n0 42\n0 0\n1 2\n3 4"] | NoteIn the first test case after replacing all missing elements with $$$11$$$ the array becomes $$$[11, 10, 11, 12, 11]$$$. The absolute difference between any adjacent elements is $$$1$$$. It is impossible to choose a value of $$$k$$$, such that the absolute difference between any adjacent element will be $$$\leq 0$$$. So, the answer is $$$1$$$.In the third test case after replacing all missing elements with $$$6$$$ the array becomes $$$[6, 6, 9, 6, 3, 6]$$$. $$$|a_1 - a_2| = |6 - 6| = 0$$$; $$$|a_2 - a_3| = |6 - 9| = 3$$$; $$$|a_3 - a_4| = |9 - 6| = 3$$$; $$$|a_4 - a_5| = |6 - 3| = 3$$$; $$$|a_5 - a_6| = |3 - 6| = 3$$$. So, the maximum difference between any adjacent elements is $$$3$$$. | Java 8 | standard input | [
"binary search",
"greedy",
"ternary search"
] | 8ffd80167fc4396788b745b53068c9d3 | The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^{5}$$$) — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-1 \leq a_i \leq 10 ^ {9}$$$). If $$$a_i = -1$$$, then the $$$i$$$-th integer is missing. It is guaranteed that at least one integer is missing in every test case. It is guaranteed, that the sum of $$$n$$$ for all test cases does not exceed $$$4 \cdot 10 ^ {5}$$$. | 1,500 | Print the answers for each test case in the following format: You should print two integers, the minimum possible value of $$$m$$$ and an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) that makes the maximum absolute difference between adjacent elements in the array $$$a$$$ equal to $$$m$$$. Make sure that after replacing all the missing elements with $$$k$$$, the maximum absolute difference between adjacent elements becomes $$$m$$$. If there is more than one possible $$$k$$$, you can print any of them. | standard output | |
PASSED | 052cfec6a85aa8fe3f38fa5f75be1308 | train_002.jsonl | 1581604500 | Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $$$a$$$ of $$$n$$$ non-negative integers.Dark created that array $$$1000$$$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) and replaces all missing elements in the array $$$a$$$ with $$$k$$$.Let $$$m$$$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $$$|a_i - a_{i+1}|$$$ for all $$$1 \leq i \leq n - 1$$$) in the array $$$a$$$ after Dark replaces all missing elements with $$$k$$$.Dark should choose an integer $$$k$$$ so that $$$m$$$ is minimized. Can you help him? | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.io.*;
import java.math.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws IOException
{
Reader s=new Reader();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pt=new PrintWriter(System.out);
int T=s.nextInt();
while(T-->0)
{
int n=s.nextInt();
long arr[]=new long[n];
long sum=0,k=0;
for(int i=0;i<n;i++) {
arr[i]=s.nextLong();
if(arr[i]==-1)
k++;
}
long max=Long.MIN_VALUE;
long min=Long.MAX_VALUE;
for(int i=1;i<n-1;i++)
{
if(arr[i]!=-1&& (arr[i-1]==-1|| arr[i+1]==-1)) {
max=Math.max(max, arr[i]);
min=Math.min(min, arr[i]);
}
}
if(arr[0]!=-1&&arr[1]==-1)
{
max=Math.max(max, arr[0]);
min=Math.min(min, arr[0]);
}
if(arr[n-1]!=-1&&arr[n-2]==-1)
{
max=Math.max(max, arr[n-1]);
min=Math.min(min, arr[n-1]);
}
if(k==n) {
pt.println("0 0");
continue;
}
long m=(max+min)/2;
replace(arr, m);
long ma=maxabs(arr);
pt.println(ma+" "+(m));
}
pt.close();
}
static long maxabs(long[] arr)
{
long max=Integer.MIN_VALUE;
for(int i=1;i<arr.length;i++)
{
max=Math.max(Math.abs(arr[i]-arr[i-1]), max);
}
return max;
}
static void replace(long arr[], long n)
{
for(int i=0;i<arr.length;i++)
if(arr[i]==-1)
arr[i]=n;
}
static boolean isPerfectSquare(long l)
{
return Math.pow((long)Math.sqrt(l),2)==l;
}
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()
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);
}
}
static class Pair implements Comparable<Pair>{
int a;
int b;
Pair(int a,int b){
this.a=a;
this.b=b;
}
public int compareTo(Pair p){
if(a>p.a)
return 1;
if(a==p.a)
return (b-p.b);
return -1;
}
}
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 | ["7\n5\n-1 10 -1 12 -1\n5\n-1 40 35 -1 35\n6\n-1 -1 9 -1 3 -1\n2\n-1 -1\n2\n0 -1\n4\n1 -1 3 -1\n7\n1 -1 7 5 2 -1 5"] | 2 seconds | ["1 11\n5 35\n3 6\n0 42\n0 0\n1 2\n3 4"] | NoteIn the first test case after replacing all missing elements with $$$11$$$ the array becomes $$$[11, 10, 11, 12, 11]$$$. The absolute difference between any adjacent elements is $$$1$$$. It is impossible to choose a value of $$$k$$$, such that the absolute difference between any adjacent element will be $$$\leq 0$$$. So, the answer is $$$1$$$.In the third test case after replacing all missing elements with $$$6$$$ the array becomes $$$[6, 6, 9, 6, 3, 6]$$$. $$$|a_1 - a_2| = |6 - 6| = 0$$$; $$$|a_2 - a_3| = |6 - 9| = 3$$$; $$$|a_3 - a_4| = |9 - 6| = 3$$$; $$$|a_4 - a_5| = |6 - 3| = 3$$$; $$$|a_5 - a_6| = |3 - 6| = 3$$$. So, the maximum difference between any adjacent elements is $$$3$$$. | Java 8 | standard input | [
"binary search",
"greedy",
"ternary search"
] | 8ffd80167fc4396788b745b53068c9d3 | The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^{5}$$$) — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-1 \leq a_i \leq 10 ^ {9}$$$). If $$$a_i = -1$$$, then the $$$i$$$-th integer is missing. It is guaranteed that at least one integer is missing in every test case. It is guaranteed, that the sum of $$$n$$$ for all test cases does not exceed $$$4 \cdot 10 ^ {5}$$$. | 1,500 | Print the answers for each test case in the following format: You should print two integers, the minimum possible value of $$$m$$$ and an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) that makes the maximum absolute difference between adjacent elements in the array $$$a$$$ equal to $$$m$$$. Make sure that after replacing all the missing elements with $$$k$$$, the maximum absolute difference between adjacent elements becomes $$$m$$$. If there is more than one possible $$$k$$$, you can print any of them. | standard output | |
PASSED | 106c87b10cbbfae3ce180a165f0086e5 | train_002.jsonl | 1581604500 | Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $$$a$$$ of $$$n$$$ non-negative integers.Dark created that array $$$1000$$$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) and replaces all missing elements in the array $$$a$$$ with $$$k$$$.Let $$$m$$$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $$$|a_i - a_{i+1}|$$$ for all $$$1 \leq i \leq n - 1$$$) in the array $$$a$$$ after Dark replaces all missing elements with $$$k$$$.Dark should choose an integer $$$k$$$ so that $$$m$$$ is minimized. Can you help him? | 256 megabytes | import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(in.readLine());
for(int t = 0; t < T; t++){
int n = Integer.parseInt(in.readLine());
int[] a = new int[n+1];
StringTokenizer st = new StringTokenizer(in.readLine());
for(int i = 0; i < n; i++){
a[i] = Integer.parseInt(st.nextToken());
}
int max = 0;
int min = 1000000001;
int maxAdjDiff = 0;
for(int i = 0; i < n; i++){
if(a[i] != -1){
if(i >= 1 && a[i-1] != -1){
maxAdjDiff = Math.max(maxAdjDiff, Math.abs(a[i]-a[i-1]));
}
if((i >= 1 && a[i-1] == -1) || a[i+1] == -1){
max = Math.max(max, a[i]);
min = Math.min(min, a[i]);
}
}
}
System.out.print(Math.max(maxAdjDiff,((max-min+1)/2)));
System.out.println(" "+(max+min)/2);
}
}
public static boolean okcheck(int[] a){
return true;
}
}
| Java | ["7\n5\n-1 10 -1 12 -1\n5\n-1 40 35 -1 35\n6\n-1 -1 9 -1 3 -1\n2\n-1 -1\n2\n0 -1\n4\n1 -1 3 -1\n7\n1 -1 7 5 2 -1 5"] | 2 seconds | ["1 11\n5 35\n3 6\n0 42\n0 0\n1 2\n3 4"] | NoteIn the first test case after replacing all missing elements with $$$11$$$ the array becomes $$$[11, 10, 11, 12, 11]$$$. The absolute difference between any adjacent elements is $$$1$$$. It is impossible to choose a value of $$$k$$$, such that the absolute difference between any adjacent element will be $$$\leq 0$$$. So, the answer is $$$1$$$.In the third test case after replacing all missing elements with $$$6$$$ the array becomes $$$[6, 6, 9, 6, 3, 6]$$$. $$$|a_1 - a_2| = |6 - 6| = 0$$$; $$$|a_2 - a_3| = |6 - 9| = 3$$$; $$$|a_3 - a_4| = |9 - 6| = 3$$$; $$$|a_4 - a_5| = |6 - 3| = 3$$$; $$$|a_5 - a_6| = |3 - 6| = 3$$$. So, the maximum difference between any adjacent elements is $$$3$$$. | Java 8 | standard input | [
"binary search",
"greedy",
"ternary search"
] | 8ffd80167fc4396788b745b53068c9d3 | The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^{5}$$$) — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-1 \leq a_i \leq 10 ^ {9}$$$). If $$$a_i = -1$$$, then the $$$i$$$-th integer is missing. It is guaranteed that at least one integer is missing in every test case. It is guaranteed, that the sum of $$$n$$$ for all test cases does not exceed $$$4 \cdot 10 ^ {5}$$$. | 1,500 | Print the answers for each test case in the following format: You should print two integers, the minimum possible value of $$$m$$$ and an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) that makes the maximum absolute difference between adjacent elements in the array $$$a$$$ equal to $$$m$$$. Make sure that after replacing all the missing elements with $$$k$$$, the maximum absolute difference between adjacent elements becomes $$$m$$$. If there is more than one possible $$$k$$$, you can print any of them. | standard output | |
PASSED | 5d52130907f02847a4c76ba40b9b3e48 | train_002.jsonl | 1581604500 | Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $$$a$$$ of $$$n$$$ non-negative integers.Dark created that array $$$1000$$$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) and replaces all missing elements in the array $$$a$$$ with $$$k$$$.Let $$$m$$$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $$$|a_i - a_{i+1}|$$$ for all $$$1 \leq i \leq n - 1$$$) in the array $$$a$$$ after Dark replaces all missing elements with $$$k$$$.Dark should choose an integer $$$k$$$ so that $$$m$$$ is minimized. Can you help him? | 256 megabytes | import java.util.*;
import java.util.Arrays;
public class solve
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int t=in.nextInt();
while(t!=0)
{
t--;
int n=in.nextInt();
int m=n;
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=in.nextInt();
}
int b[]=new int[n];
int c=0;
if((a[0]!=-1)&&(a[1]==-1))
{
b[0]=a[0];
c++;
}
for(int i=1;i<n-1;i++)
{
if(a[i]!=-1)
{
if((a[i-1]==-1)||(a[i+1]==-1))
{
b[c]=a[i];
c++;
}
}
}
if((a[n-1]!=-1)&&(a[n-2]==-1))
{
b[c]=a[n-1];
c++;
}
int max=b[0],min=b[0];
for(int i=1;i<c;i++)
{
if(b[i]>max)
max=b[i];
if(b[i]<min)
min=b[i];
}
int assg=(max+min)/2;
for(int i=0;i<n;i++)
{
if(a[i]==-1)
a[i]=assg;
}
int diff=0;
for(int i=0;i<n-1;i++)
{
if(Math.abs(a[i+1]-a[i])>diff)
diff=Math.abs(a[i+1]-a[i]);
}
System.out.println(diff+" "+assg);
}
}
} | Java | ["7\n5\n-1 10 -1 12 -1\n5\n-1 40 35 -1 35\n6\n-1 -1 9 -1 3 -1\n2\n-1 -1\n2\n0 -1\n4\n1 -1 3 -1\n7\n1 -1 7 5 2 -1 5"] | 2 seconds | ["1 11\n5 35\n3 6\n0 42\n0 0\n1 2\n3 4"] | NoteIn the first test case after replacing all missing elements with $$$11$$$ the array becomes $$$[11, 10, 11, 12, 11]$$$. The absolute difference between any adjacent elements is $$$1$$$. It is impossible to choose a value of $$$k$$$, such that the absolute difference between any adjacent element will be $$$\leq 0$$$. So, the answer is $$$1$$$.In the third test case after replacing all missing elements with $$$6$$$ the array becomes $$$[6, 6, 9, 6, 3, 6]$$$. $$$|a_1 - a_2| = |6 - 6| = 0$$$; $$$|a_2 - a_3| = |6 - 9| = 3$$$; $$$|a_3 - a_4| = |9 - 6| = 3$$$; $$$|a_4 - a_5| = |6 - 3| = 3$$$; $$$|a_5 - a_6| = |3 - 6| = 3$$$. So, the maximum difference between any adjacent elements is $$$3$$$. | Java 8 | standard input | [
"binary search",
"greedy",
"ternary search"
] | 8ffd80167fc4396788b745b53068c9d3 | The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^{5}$$$) — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-1 \leq a_i \leq 10 ^ {9}$$$). If $$$a_i = -1$$$, then the $$$i$$$-th integer is missing. It is guaranteed that at least one integer is missing in every test case. It is guaranteed, that the sum of $$$n$$$ for all test cases does not exceed $$$4 \cdot 10 ^ {5}$$$. | 1,500 | Print the answers for each test case in the following format: You should print two integers, the minimum possible value of $$$m$$$ and an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) that makes the maximum absolute difference between adjacent elements in the array $$$a$$$ equal to $$$m$$$. Make sure that after replacing all the missing elements with $$$k$$$, the maximum absolute difference between adjacent elements becomes $$$m$$$. If there is more than one possible $$$k$$$, you can print any of them. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.