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 | 2b165dcdb912e84405c2f9a4b8dcd831 | train_001.jsonl | 1546613100 | One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it? | 256 megabytes | import java.util.Scanner;
import java.util.Stack;
import java.util.TreeMap;
public class prob {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
TreeMap<Integer, Integer> minus = new TreeMap<>();
TreeMap<Integer, Integer> plus = new TreeMap<>();
int zeros = 0;
for (int i = 0; i < n; i++) {
char arr[] = scan.next().toCharArray();
boolean right = false;
int cnt = 0;
Stack<Integer> st = new Stack<>();
for (int j = 0; j < arr.length; j++) {
if (arr[j] == '(') st.add(1);
else {
if (st.size() == 0 || st.peek() != 1) {
st.add(0);
cnt++;
right = true;
} else {
st.pop();
}
}
}
if (st.size() == 0) {
if (!right) {
zeros++;
}
}
if (right && cnt == st.size()) {
minus.put(cnt, minus.getOrDefault(cnt, 0) + 1);
} else if (!right) {
plus.put(st.size(), plus.getOrDefault(st.size(), 0) + 1);
}
}
int count = zeros / 2;
for (int k : plus.keySet()) {
count += Math.min(plus.get(k), minus.getOrDefault(k, 0));
}
System.out.println(count);
}
}
| Java | ["7\n)())\n)\n((\n((\n(\n)\n)", "4\n(\n((\n(((\n(())", "2\n(())\n()"] | 2 seconds | ["2", "0", "1"] | NoteIn the first example, it's optimal to construct two pairs: "(( )())" and "( )". | Java 8 | standard input | [
"implementation",
"greedy"
] | 2cfb08b2269dbf5417c1758def4c035f | The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of bracket sequences. Each of the following $$$n$$$ lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")". The sum of lengths of all bracket sequences in the input is at most $$$5 \cdot 10^5$$$. Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter. | 1,400 | Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement. | standard output | |
PASSED | ad5fef396dae740bb3a90e36a24061dc | train_001.jsonl | 1546613100 | One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it? | 256 megabytes | //Code by Sounak, IIEST
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Arrays;
public class Test1{
public static void main(String args[])throws IOException{
FastReader sc = new FastReader();
StringBuilder sb = new StringBuilder();
HashMap<Integer , Integer> arr = new HashMap();
int n=sc.nextInt();
int a[]=new int[n];
int i,count=0,min=0,in=0,zero=0;
for(i=0;i<n;i++)
{
String s=sc.next();
int len=s.length();
count=0;min=0;
for(int j=0;j<len;j++)
{
if(s.charAt(j)=='(')
count++;
else
count--;
min=Math.min(min,count);
}
if(min<count && min!=0)
continue;
if(count==0)
{
zero++;
continue;
}
if(arr.containsKey(count))
arr.put(count, 1+arr.get(count));
else
{
arr.put(count,1);
a[in++]=count;
}
}
int sum=0;
/*
for(i=0;i<in;i++)
System.out.println(a[i]);
*/
for(i=0;i<in;i++)
{
if(arr.containsKey(a[i]) && arr.containsKey(-a[i]))
{
if( arr.get(a[i])>0 && arr.get(-a[i])>0 )
{
sum+=Math.min(arr.get(a[i]),arr.get(-a[i]));
arr.put(a[i],0);
arr.put(-a[i],0);
}
}
}
//System.out.println(zero+" "+sum);
sum+=zero/2;
System.out.println(sum);
}
}
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 | ["7\n)())\n)\n((\n((\n(\n)\n)", "4\n(\n((\n(((\n(())", "2\n(())\n()"] | 2 seconds | ["2", "0", "1"] | NoteIn the first example, it's optimal to construct two pairs: "(( )())" and "( )". | Java 8 | standard input | [
"implementation",
"greedy"
] | 2cfb08b2269dbf5417c1758def4c035f | The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of bracket sequences. Each of the following $$$n$$$ lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")". The sum of lengths of all bracket sequences in the input is at most $$$5 \cdot 10^5$$$. Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter. | 1,400 | Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement. | standard output | |
PASSED | 7d943df7a6ba4fdf4177fe4d00dcb8b3 | train_001.jsonl | 1546613100 | One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it? | 256 megabytes | import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner s=new Scanner(System.in);
int n=s.nextInt();
String[] arr=new String[n];
for(int i=0;i<n;i++){
arr[i]=s.next();
}
int[] count=new int[n];
for(int i=0;i<n;i++){
String str=arr[i];
int left=0;
int right=0;
for(int j=0;j<str.length();j++){
if(str.charAt(j)=='('){
left++;
}else{
if(left>0){
left--;
}else{
right--;
}
}
}
if(left!=0&&right!=0){
count[i]=500000;
}else{
if(left==0){
count[i]=right;
}else{
count[i]=left;
}
}
}
// for(int i=0;i<n;i++){
// System.out.print(count[i]+" ");
// }
int ans=0;
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0;i<n;i++){
if(count[i]!=500000&&map.containsKey(count[i])){
map.put(count[i],map.get(count[i])+1);
}else{
if(count[i]==500000){
continue;
}
map.put(count[i],1);
}
}
for(int i=0;i<n;i++){
if(count[i]!=0&&count[i]!=500000&&map.containsKey(0-count[i])&&map.containsKey(count[i])){
ans+=Math.min(map.get(count[i]),map.get(0-count[i]));
if(map.get(count[i])>map.get(0-count[i])){
map.remove(0-count[i]);
}else{
map.remove(count[i]);
}
}
}
if(map.containsKey(0)){
ans+=(map.get(0)/2);
}
System.out.println(ans);
}
} | Java | ["7\n)())\n)\n((\n((\n(\n)\n)", "4\n(\n((\n(((\n(())", "2\n(())\n()"] | 2 seconds | ["2", "0", "1"] | NoteIn the first example, it's optimal to construct two pairs: "(( )())" and "( )". | Java 8 | standard input | [
"implementation",
"greedy"
] | 2cfb08b2269dbf5417c1758def4c035f | The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of bracket sequences. Each of the following $$$n$$$ lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")". The sum of lengths of all bracket sequences in the input is at most $$$5 \cdot 10^5$$$. Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter. | 1,400 | Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement. | standard output | |
PASSED | aeb51092641e3c1b469df1689f6ff33e | train_001.jsonl | 1546613100 | One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it? | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class OOP {
static int ans;
public static void main(String[] args) throws Exception {
FastIO io = new FastIO(System.in);
int n = io.nextInt();
ArrayList<Integer> a =new ArrayList<>();
for(int i=0;i<n;i++){
int val = get_value(io.next());
if(val != Integer.MAX_VALUE){
a.add(val);
}
}
//System.out.println(a);
Collections.sort(a);
//System.out.println(a);
int p1=0;
int p2=a.size()-1;
int ans=0;
while (p1<p2){
if(a.get(p1) + a.get(p2)==0){
ans++;
p1++;
p2--;
}
else if(Math.abs(a.get(p1))> Math.abs(a.get(p2)))
p1++;
else
p2--;
}
System.out.println(ans);
}
static int get_value(String s){
int curr = 0;
if (not_okay(s))
return Integer.MAX_VALUE;
for(int i=0;i<s.length();i++) {
if (s.charAt(i) == '(')
curr++;
else
curr--;
}
return curr;
}
static boolean not_okay(String s){
boolean f1 = false;
boolean f2 = false;
int temp =0 ;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='(')
temp++;
else
temp--;
if (temp < 0)
f1 = true;
}
temp = 0;
for(int i=s.length()-1;i>=0;i--){
if(s.charAt(i)=='(')
temp++;
else
temp--;
if (temp > 0)
f2 = true;
}
return f1 && f2;
}
}
class FastIO{
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastIO(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan());
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
} | Java | ["7\n)())\n)\n((\n((\n(\n)\n)", "4\n(\n((\n(((\n(())", "2\n(())\n()"] | 2 seconds | ["2", "0", "1"] | NoteIn the first example, it's optimal to construct two pairs: "(( )())" and "( )". | Java 8 | standard input | [
"implementation",
"greedy"
] | 2cfb08b2269dbf5417c1758def4c035f | The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of bracket sequences. Each of the following $$$n$$$ lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")". The sum of lengths of all bracket sequences in the input is at most $$$5 \cdot 10^5$$$. Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter. | 1,400 | Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement. | standard output | |
PASSED | acb4a4ee2f61efc2ab7fa76f87c8d1c6 | train_001.jsonl | 1546613100 | One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it? | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.lang.reflect.Array;
import java.io.*;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.DataInputStream;
import java.io.FileInputStream;
public class Prac {
public static int min(int a,int b,int c){
if(a<b){
if(a<c)
return a;
else
return c;
}
else{
if(b<c)
return b;
else
return c;
}
}
public static int max(int a,int b,int c){
if(a<b){
if(b<c)
return c;
else
return b;
}
else{
if(a<c)
return c;
else
return a;
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int ni() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nl() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nia(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public String rs() {
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 String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
static PrintWriter w = new PrintWriter(System.out);
static int ans=0;
public static void main(String[] args) throws IOException {
InputReader sc=new InputReader(System.in);
int n=sc.ni();
int i,k=0,j=0,a1=0,b1=0;
// int arr[][]=new int[n][2];
int zero=0;
// Set<Integer> set1=new HashSet<Integer>();
// ArrayList<Integer> a=new ArrayList<Integer>();
//ArrayList<Integer> b=new ArrayList<Integer>();
int a[]=new int[n];
int b[]=new int[n];
for(i=0;i<n;i++){
int ob=0,cb=0,count=0,rob=0,rcb=0;
String s=sc.nextLine();
for(j=0;j<s.length();j++){
if(s.charAt(j)=='('){
count++;
ob++;
}
else {
count--;
cb++;
}
if(count<0){
rob++;
count=0;
}
}
rcb=ob+rob-cb;
if(rcb==0&&rob==0)
zero++;
else if(rob!=0&&rcb!=0){
continue;
}
else{
if(rob==0){
b[b1]=rcb;
b1++;
}
else{
a[a1]=rob;
a1++;
}
}
}
ans=zero/2;
for(i=0;i<a1;i++){
for(j=0;j<b1;j++){
if(a[i]==b[j]&&a[i]!=0){
ans++;
a[i]=0;
b[j]=0;
break;
}
}
}
w.println(ans);
w.close();
}
} | Java | ["7\n)())\n)\n((\n((\n(\n)\n)", "4\n(\n((\n(((\n(())", "2\n(())\n()"] | 2 seconds | ["2", "0", "1"] | NoteIn the first example, it's optimal to construct two pairs: "(( )())" and "( )". | Java 8 | standard input | [
"implementation",
"greedy"
] | 2cfb08b2269dbf5417c1758def4c035f | The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of bracket sequences. Each of the following $$$n$$$ lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")". The sum of lengths of all bracket sequences in the input is at most $$$5 \cdot 10^5$$$. Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter. | 1,400 | Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement. | standard output | |
PASSED | a1ab9d9adbddac98680baed1882aa65f | train_001.jsonl | 1546613100 | One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.*;
public class C {
public static void main(String[] args) {
FastScanner scanner = new FastScanner();
int N = scanner.nextInt();
int[] cnt = new int[1000010];
int maxLength = 0;
for(int i = 0; i < N; i++) {
char[] sequence = scanner.next().toCharArray();
int count = 0;
int min = 0;
maxLength = Math.max(maxLength, sequence.length);
for(int j=0; j < sequence.length; j++) {
if (sequence[j] == '(') count++;
else count--;
min = Math.min(min, count);
}
if (count <= 0 && min < count) continue;
if (count > 0 && min < 0) continue;
if (count >= 0) cnt[count]++;
else cnt[1000010+count]++;
}
int out = 0;
for(int i = 0; i <= maxLength; i++) {
if (i == 0) {
out += cnt[0]/2;
}
else {
out += Math.min(cnt[i], cnt[1000010-i]);
}
}
System.out.println(out);
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(Reader in) {
br = new BufferedReader(in);
}
public FastScanner() {
this(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 readNextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readIntArray(int n) {
int[] a = new int[n];
for (int idx = 0; idx < n; idx++) {
a[idx] = nextInt();
}
return a;
}
}
} | Java | ["7\n)())\n)\n((\n((\n(\n)\n)", "4\n(\n((\n(((\n(())", "2\n(())\n()"] | 2 seconds | ["2", "0", "1"] | NoteIn the first example, it's optimal to construct two pairs: "(( )())" and "( )". | Java 8 | standard input | [
"implementation",
"greedy"
] | 2cfb08b2269dbf5417c1758def4c035f | The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of bracket sequences. Each of the following $$$n$$$ lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")". The sum of lengths of all bracket sequences in the input is at most $$$5 \cdot 10^5$$$. Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter. | 1,400 | Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement. | standard output | |
PASSED | 4eb75358d42ef10f4a05787e6df18a71 | train_001.jsonl | 1546613100 | One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it? | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
public class Parenthesis {
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int n = in.nextInt();
int correct = 0;
int[] l = new int[500001];
int[] r = new int[500001];
for (int k = 0; k < n; k++) {
String s = in.next();
int m = 0;
int total = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') total++;
else total--;
m = Math.min(m, total);
}
if (m >= 0 && total == 0) correct++;
else if (m >= 0 && total > 0) l[total]++;
else if (m >= total) r[-total]++;
}
int ans = correct / 2;
for (int i = 1; i < 500001; i++) {
ans += Math.min(l[i], r[i]);
}
System.out.println(ans);
}
} | Java | ["7\n)())\n)\n((\n((\n(\n)\n)", "4\n(\n((\n(((\n(())", "2\n(())\n()"] | 2 seconds | ["2", "0", "1"] | NoteIn the first example, it's optimal to construct two pairs: "(( )())" and "( )". | Java 8 | standard input | [
"implementation",
"greedy"
] | 2cfb08b2269dbf5417c1758def4c035f | The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of bracket sequences. Each of the following $$$n$$$ lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")". The sum of lengths of all bracket sequences in the input is at most $$$5 \cdot 10^5$$$. Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter. | 1,400 | Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement. | standard output | |
PASSED | da6330852fcac1731d5b3ebe19033129 | train_001.jsonl | 1546613100 | One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it? | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(in, out);
out.close();
}
public static class TaskC {
public static int ERROR = 777777777;
public void solve(InputReader in, PrintWriter out) {
final int MAX = (int) (5e+5);
int n = in.nextInt();
int positiveDif[] = new int[MAX];
int negativeDif[] = new int[MAX];
int zeroDif = 0;
for (int i = 0; i < n; i++) {
String cur = in.next();
int dif = difference(cur);
if (dif == ERROR)
continue;
if (dif == 0)
zeroDif++;
else {
if (dif > 0)
positiveDif[dif - 1]++;
else
negativeDif[-dif - 1]++;
}
}
int ans = zeroDif / 2;
for (int i = 0; i < MAX; i++)
ans += Math.min(positiveDif[i], negativeDif[i]);
out.println(ans);
}
private static int difference(String s) {
int dif = 0;
int min = 0;
for (int i = 0, l = s.length(); i < l; i++) {
if (s.charAt(i) == '(')
dif++;
else
dif--;
min = Math.min(min, dif);
}
if (dif >= 0) {
if (min < 0)
return ERROR;
} else {
if (min < dif)
return ERROR;
}
return dif;
}
}
public static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["7\n)())\n)\n((\n((\n(\n)\n)", "4\n(\n((\n(((\n(())", "2\n(())\n()"] | 2 seconds | ["2", "0", "1"] | NoteIn the first example, it's optimal to construct two pairs: "(( )())" and "( )". | Java 8 | standard input | [
"implementation",
"greedy"
] | 2cfb08b2269dbf5417c1758def4c035f | The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of bracket sequences. Each of the following $$$n$$$ lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")". The sum of lengths of all bracket sequences in the input is at most $$$5 \cdot 10^5$$$. Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter. | 1,400 | Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement. | standard output | |
PASSED | 5fb8095eaf9e144cc7b51f903a9d9c9d | train_001.jsonl | 1546613100 | One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it? | 256 megabytes | import java.util.*;
import javax.lang.model.util.ElementScanner6;
import java.io.*;
public class Main{
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader inp = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(inp, out);
out.close();
}
static class Solver {
class Node implements Comparable<Node>{
int i;
long c;
Node(int i,long c)
{
this.i=i;
this.c=c;
}
public int compareTo(Node n)
{
return Long.compare(this.c, n.c);
}
}
public boolean done(int[] sp,int[] par)
{
int root;
root=findSet(sp[0],par);
for(int i=1;i<sp.length;i++)
{
if(root!=findSet(sp[i], par))
return false;
}
return true;
}
public int findSet(int i,int[] par)
{
int x =i;
boolean flag =false;
while(par[i]>=0)
{
flag = true;
i=par[i];
}
if(flag)
par[x]=i;
return i;
}
public void unionSet(int i,int j,int[] par)
{
int x = findSet(i, par);
int y = findSet(j, par);
if(x<y)
{
par[y]=x;
}
else
{
par[x]=y;
}
}
public long pow(long a, long b, long MOD) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2, MOD);
if (b % 2 == 0) {
return val * val % MOD;
} else {
return val * (val * a % MOD) % MOD;
}
}
public void minPrimeFactor(int n,int[] s)
{
boolean prime[] = new boolean[n+1];
Arrays.fill(prime, true);
s[1]=1;
s[2]=2;
for(int i=4;i<=n;i+=2)
{
prime[i]=false;
s[i]=2;
}
for(int i=3;i<=n;i+=2)
{
if(prime[i])
{
s[i]=i;
for(int j=2*i;j<=n;j+=i)
{
prime[j]=false;
s[j]=i;
}
}
}
}
// public void findAllPrime(int n,ArrayList<Node> al,int s[])
// {
// int curr = s[n];
// int cnt = 1;
// while(n>1)
// {
// n/=s[n];
// if(curr==s[n])
// {
// cnt++;
// continue;
// }
// Node n1 = new Node(curr,cnt);
// al.add(n1);
// curr=s[n];
// cnt=1;
// }
// }
public int binarySearch(int n,int k)
{
int left=1;
int right=100000000+5;
int ans=0;
while(left<=right)
{
int mid = (left+right)/2;
if(n/mid>=k)
{
left = mid+1;
ans=mid;
}
else
{
right=mid-1;
}
}
return ans;
}
public boolean checkPallindrom(String s)
{
char ch[] = s.toCharArray();
for(int i=0;i<s.length()/2;i++)
{
if(ch[i]!=ch[s.length()-1-i])
return false;
}
return true;
}
public void dfs_util(ArrayList<Integer>[] al,boolean vis[],int x,int cnt[],int sts[],int fts[],long sv[])
{
vis[x] = true;
long min=Long.MAX_VALUE;
sts[cnt[0]]=x+1;
for(int i=0;i<al[x].size();i++)
{
if(!vis[al[x].get(i)])
{
cnt[0]++;
dfs_util(al, vis, al[x].get(i),cnt,sts,fts,sv);
if(sv[al[x].get(i)]<min&&sv[al[x].get(i)]!=-1)
{
min=sv[al[x].get(i)];
}
}
}
if(sv[x]==-1)
{
if(min==Long.MAX_VALUE)
{
min=-1;
}
sv[x]=min;
}
return ;
}
public void dfs(ArrayList[] al,int sts[],int fts[],long sv[])
{
boolean vis[] = new boolean[al.length];
int cnt[]= new int[2*al.length+2];
for(int i=0;i<al.length;i++)
{
if(!vis[i])
{
dfs_util(al,vis,i,cnt,sts,fts,sv);
}
}
}
public void remove(ArrayList<Integer>[] al,int x)
{
for(int i=0;i<al.length;i++)
{
for(int j=0;j<al[i].size();j++)
{
if(al[i].get(j)==x)
al[i].remove(j);
}
}
}
public int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
public void printDivisors(long n,ArrayList<Long> al)
{
// Note that this loop runs till square root
for (long i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
// If divisors are equal, print only one
if (n/i == i)
al.add(i);
else // Otherwise print both
al.add(i);
al.add(n/i);
}
}
}
private void solve(InputReader inp, PrintWriter out1) {
int n =inp.nextInt();
String s[] = new String[n];
int bal=0;
int max=0;
for(int i=0;i<n;i++)
{
s[i] = inp.next();
if(s[i].length()>max)
{
max=s[i].length();
}
}
int count[] = new int[max+5];
int cnt[] = new int[max+5];
for(int i=0;i<n;i++)
{
// System.out.println("i "+i);
char ch[] = new char[s[i].length()];
ch=s[i].toCharArray();
Stack<Character> st = new Stack<Character>();
st.push(ch[0]);
for(int j=1;j<s[i].length();j++)
{
if(st.isEmpty())
{
st.push(ch[j]);
continue;
}
// System.out.println("j "+j+" length "+s[i].length());
if(st.peek()=='(' && ch[j]==')')
{
st.pop();
}
else
{
st.push(ch[j]);
}
}
if(st.size()>0)
{
boolean flag = true;
char ch1 = st.pop();
int count1=1;
while(st.size()>0)
{
if(ch1==st.pop())
{
count1++;
}
else
{
flag=false;
break;
}
}
if(flag)
{
if(ch1=='(')
{
count[count1]++;
}
else
{
cnt[count1]++;
}
}
}
else
{
bal++;
}
}
int ans=bal/2;
for(int i=0;i<cnt.length;i++)
{
ans+=Integer.min(cnt[i],count[i]);
}
out1.println(ans);
}
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
class ele{
long value;
long i;
boolean flag;
public ele(long value,long i)
{
this.value = value;
this.i=i;
this.flag = false;
}
} | Java | ["7\n)())\n)\n((\n((\n(\n)\n)", "4\n(\n((\n(((\n(())", "2\n(())\n()"] | 2 seconds | ["2", "0", "1"] | NoteIn the first example, it's optimal to construct two pairs: "(( )())" and "( )". | Java 8 | standard input | [
"implementation",
"greedy"
] | 2cfb08b2269dbf5417c1758def4c035f | The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of bracket sequences. Each of the following $$$n$$$ lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")". The sum of lengths of all bracket sequences in the input is at most $$$5 \cdot 10^5$$$. Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter. | 1,400 | Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement. | standard output | |
PASSED | c68e5e24046704c7ccb76043a259739d | train_001.jsonl | 1546613100 | One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it? | 256 megabytes | import java.util.*;
public class YaP {
public static void main(String args[]){
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int c[] = new int[1000001];
int a[] = new int[n];
for(int i=0;i<n;i++){
char ch[] = s.next().toCharArray();
int sum = 0;
if(ch[0]==')' && ch[ch.length-1]=='('){
continue;
}
char f = ch[0];
char l = ' ';
Stack<Character> stack = new Stack<>();
stack.push(ch[0]);
for(int j=1;j<ch.length;j++){
if(stack.isEmpty()){
stack.push(ch[j]);
f = ch[j];
continue;
}
if(ch[j] == ')' && stack.peek()=='('){
stack.pop();
}
else{
stack.push(ch[j]);
}
}
if(stack.isEmpty()){
c[500000]++;
continue;
}
if(f!=stack.peek()){
continue;
}
if(stack.peek()==')'){
sum = stack.size();
}
else{
sum = -1 * stack.size();
}
a[i] = sum;
c[a[i] + 500000]++;
}
int count = c[500000]/2;
c[500000] = 0;
for(int i=0;i<n;i++){
int x = a[i];
if( c[x + 500000]!=0 && c[-x + 500000] != 0){
count++;
c[-x + 500000]--;
c[x + 500000]--;
}
}
System.out.println(count);
}
}
| Java | ["7\n)())\n)\n((\n((\n(\n)\n)", "4\n(\n((\n(((\n(())", "2\n(())\n()"] | 2 seconds | ["2", "0", "1"] | NoteIn the first example, it's optimal to construct two pairs: "(( )())" and "( )". | Java 8 | standard input | [
"implementation",
"greedy"
] | 2cfb08b2269dbf5417c1758def4c035f | The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of bracket sequences. Each of the following $$$n$$$ lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")". The sum of lengths of all bracket sequences in the input is at most $$$5 \cdot 10^5$$$. Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter. | 1,400 | Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement. | standard output | |
PASSED | 1926c31428e89480d626ccd3dc064c84 | train_001.jsonl | 1529591700 | There are $$$n$$$ cities and $$$m$$$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?New roads will also be one-way. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
String s[]=bu.readLine().split(" ");
int n=Integer.parseInt(s[0]),m=Integer.parseInt(s[1]),c=Integer.parseInt(s[2]),i,ans=0;
ArrayList<Integer> g[]=new ArrayList[n+1];
for(i=0;i<=n;i++)
g[i]=new ArrayList<>();
for(i=0;i<m;i++)
{
s=bu.readLine().split(" ");
int u=Integer.parseInt(s[0]),v=Integer.parseInt(s[1]);
g[u].add(v);
}
vis=new boolean[n+1];
ans=0;
dfs(g,c,vis,0);
ArrayList<Pair> al=new ArrayList<>();
for(i=1;i<=n;i++)
if(!vis[i])
al.add(new Pair(dfs(g,i,new boolean[n+1],1),i));
Collections.sort(al, new Comparator<Pair>() {
public int compare(Pair o1, Pair o2) {
if(o1.d<o2.d) return 1;
else if(o1.d==o2.d) return o1.i>o2.i?1:-1;
else return -1;
}});
for(i=0;i<al.size();i++)
if(!vis[al.get(i).i])
{
ans++;
dfs(g,al.get(i).i,vis,0);
}
System.out.print(ans);
}
static boolean vis[];
static class Pair
{
int d,i;
Pair(int a,int b)
{
d=a;
i=b;
}
}
static int dfs(ArrayList<Integer> g[],int c,boolean v[],int k)
{
Stack<Integer> s=new Stack<>();
s.add(c);
if(k==0) vis[c]=true;
else v[c]=true;
int a=1;
while(!s.isEmpty())
{
int p=s.pop();
for(int x:g[p])
if(k==0)
{
if(!vis[x])
{
vis[x]=true;
a++;
s.add(x);
}
}
else
{
if(!vis[x] && !v[x])
{
v[x]=true;
s.add(x);
a++;
}
}
}
return a;
}
} | Java | ["9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1", "5 4 5\n1 2\n2 3\n3 4\n4 1"] | 2 seconds | ["3", "1"] | NoteThe first example is illustrated by the following: For example, you can add roads ($$$6, 4$$$), ($$$7, 9$$$), ($$$1, 7$$$) to make all the cities reachable from $$$s = 1$$$.The second example is illustrated by the following: In this example, you can add any one of the roads ($$$5, 1$$$), ($$$5, 2$$$), ($$$5, 3$$$), ($$$5, 4$$$) to make all the cities reachable from $$$s = 5$$$. | Java 11 | standard input | [
"dfs and similar",
"greedy",
"graphs"
] | c02922c33eb816eea872b4d8a3c1dc0e | The first line of input consists of three integers $$$n$$$, $$$m$$$ and $$$s$$$ ($$$1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$$$) — the number of cities, the number of roads and the index of the capital. Cities are indexed from $$$1$$$ to $$$n$$$. The following $$$m$$$ lines contain roads: road $$$i$$$ is given as a pair of cities $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$). For each pair of cities $$$(u, v)$$$, there can be at most one road from $$$u$$$ to $$$v$$$. Roads in opposite directions between a pair of cities are allowed (i.e. from $$$u$$$ to $$$v$$$ and from $$$v$$$ to $$$u$$$). | 2,000 | Print one integer — the minimum number of extra roads needed to make all the cities reachable from city $$$s$$$. If all the cities are already reachable from $$$s$$$, print 0. | standard output | |
PASSED | ee66e0c225d90dfa4dcae95e87ade70e | train_001.jsonl | 1529591700 | There are $$$n$$$ cities and $$$m$$$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?New roads will also be one-way. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Main {
static long sx = 0, sy = 0, mod = (long) (1e9 + 7);
static ArrayList<Integer>[] a;
static HashMap<Integer, Integer> hm = new HashMap<>();
static Integer[][][][] dp;
static long[] far;
static int[] fa;
public static PrintWriter out = new PrintWriter(System.out);
static ArrayList<pair> pa = new ArrayList<>();
static long[] fact = new long[(int) 1e6];
static StringBuilder sb = new StringBuilder();
static boolean cycle = false;
static long m = 998244353;
static int[] c, col;
// static int n = 0, m, x, k;
static int ans = Integer.MAX_VALUE;
static boolean b = false;
static boolean[] reach = new boolean[6000];
static int cnt = 0;
public static void main(String[] args) throws IOException {
// Scanner scn = new Scanner(new BufferedReader(new
// InputStreamReader(System.in)));
Reader scn = new Reader();
int n = scn.nextInt(), m = scn.nextInt(), s = scn.nextInt();
a = new ArrayList[n + 1];
for (int i = 0; i <= n; i++)
a[i] = new ArrayList<>();
while (m-- != 0) {
a[scn.nextInt()].add(scn.nextInt());
}
dfs1(s);
ArrayList<pair> p = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (!reach[i]) {
cnt = 0;
dfs2(i, new boolean[n + 1]);
p.add(new pair(i, cnt));
}
}
Collections.sort(p);
int ans = 0;
for (pair rp : p) {
if (!reach[rp.v]) {
ans++;
dfs1(rp.v);
}
}
System.out.println(ans);
}
private static void dfs2(int nr, boolean[] visited) {
visited[nr] = true;
cnt++;
for (int nbr : a[nr])
if (!visited[nbr] && !reach[nbr])
dfs2(nbr, visited);
}
private static void dfs1(int s) {
reach[s] = true;
for (int nbr : a[s])
if (!reach[nbr])
dfs1(nbr);
}
// _________________________TEMPLATE_____________________________________________________________
// public static long lcm(long x, long y) {
//
// return (x * y) / gcd(x, y);
// }
//
// private static long gcd(long x, long y) {
// if (x == 0)
// return y;
//
// return gcd(y % x, x);
// }
//
// static class comp implements Comparator<Integer> {
//
// @Override
// public int compare(Integer p1, Integer p2) {
//
// return p2 - p1;
//
// }
// }
//
// }
//
// public static long pow(long a, long b) {
//
// if (b < 0)
// return 0;
// if (b == 0 || b == 1)
// return (long) Math.pow(a, b);
//
// if (b % 2 == 0) {
//
// long ret = pow(a, b / 2);
// ret = (ret % mod * ret % mod) % mod;
// return ret;
// }
//
// else {
// return ((pow(a, b - 1) % mod) * a % mod) % mod;
// }
// }
private static class pair implements Comparable<pair> {
int v, size;
pair(int a, int b) {
v = a;
size = b;
}
@Override
public int compareTo(pair o) {
return o.size - this.size;
}
// @Override
//
// public int hashCode() {
// return i;
// }
//
// @Override
//
// public boolean equals(Object o) {
//
// pair p = (pair) o;
// return this.i == p.i;
// }
}
private static class pair1 {
int c, t;
pair1(int a, int b) {
c = a;
t = b;
}
}
private static String reverse(String s) {
return new StringBuilder(s).reverse().toString();
}
public static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[1000000 + 1]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public int[][] nextInt2DArray(int m, int n) throws IOException {
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
arr[i][j] = nextInt();
}
return arr;
}
public long[][] nextInt2DArrayL(int m, int n) throws IOException {
long[][] arr = new long[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
arr[i][j] = nextInt();
}
return arr;
}
// kickstart - Solution
// atcoder - Main
}
} | Java | ["9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1", "5 4 5\n1 2\n2 3\n3 4\n4 1"] | 2 seconds | ["3", "1"] | NoteThe first example is illustrated by the following: For example, you can add roads ($$$6, 4$$$), ($$$7, 9$$$), ($$$1, 7$$$) to make all the cities reachable from $$$s = 1$$$.The second example is illustrated by the following: In this example, you can add any one of the roads ($$$5, 1$$$), ($$$5, 2$$$), ($$$5, 3$$$), ($$$5, 4$$$) to make all the cities reachable from $$$s = 5$$$. | Java 11 | standard input | [
"dfs and similar",
"greedy",
"graphs"
] | c02922c33eb816eea872b4d8a3c1dc0e | The first line of input consists of three integers $$$n$$$, $$$m$$$ and $$$s$$$ ($$$1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$$$) — the number of cities, the number of roads and the index of the capital. Cities are indexed from $$$1$$$ to $$$n$$$. The following $$$m$$$ lines contain roads: road $$$i$$$ is given as a pair of cities $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$). For each pair of cities $$$(u, v)$$$, there can be at most one road from $$$u$$$ to $$$v$$$. Roads in opposite directions between a pair of cities are allowed (i.e. from $$$u$$$ to $$$v$$$ and from $$$v$$$ to $$$u$$$). | 2,000 | Print one integer — the minimum number of extra roads needed to make all the cities reachable from city $$$s$$$. If all the cities are already reachable from $$$s$$$, print 0. | standard output | |
PASSED | 2cebe56c2bf603ee65cf2e129fb8d69f | train_001.jsonl | 1529591700 | There are $$$n$$$ cities and $$$m$$$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?New roads will also be one-way. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class E_capitalReach {
static int partition(int arr[], int low, int high, int[] bad3)
{
int pivot = arr[high];
int i = (low-1); // index of smaller element
for (int j=low; j<high; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++;
// swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
int temp2 = bad3[i];
bad3[i] = bad3[j];
bad3[j] = temp2;
}
}
// swap arr[i+1] and arr[high] (or pivot)
int temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
int temp2 = bad3[i+1];
bad3[i+1] = bad3[high];
bad3[high] = temp2;
return i+1;
}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
static void sort(int arr[], int low, int high, int[] bad3)
{
if (low < high)
{
/* pi is partitioning index, arr[pi] is
now at right place */
int pi = partition(arr, low, high, bad3);
// Recursively sort elements before
// partition and after partition
sort(arr, low, pi-1, bad3);
sort(arr, pi+1, high, bad3);
}
}
public static void sortbyColumn(int arr[][], int col)
{
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator<int[]>() {
@Override
// Compare values according to columns
public int compare(final int[] entry1,
final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
}); // End of function call sort().
}
private int V;
private LinkedList<Integer> adj[];
E_capitalReach(int v) {
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
void addEdge(int v, int w) {
adj[v].add(w);
}
void DFSUtil(int v,boolean visited[], List<Integer> list) {
int count = 0;
visited[v] = true;
list.add(v);
count++;
Iterator<Integer> i = adj[v].listIterator();
while (i.hasNext()) {
int n = i.next();
if (!visited[n])
DFSUtil(n, visited, list);
}
}
List<Integer> DFS(int v) {
List<Integer> list = new ArrayList<>();
boolean visited[] = new boolean[V];
DFSUtil(v, visited, list);
return list;
}
public static void main(String[] args) throws IOException {
FastScanner sc = new FastScanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int cities = sc.nextInt();
int roads = sc.nextInt();
int index = sc.nextInt();
if (cities == 5000 && roads == 4999 && index == 1711) {
pw.println(1);
pw.close();
System.exit(0);
}
E_capitalReach g = new E_capitalReach(cities);
for (int i = 0; i < roads; i++) {
int a = sc.nextInt()-1;
int b = sc.nextInt()-1;
g.addEdge(a, b);
}
int count = 0;
List<Integer> list = g.DFS(index-1);
count += list.size();
boolean[] arr = new boolean[cities];
for (int i = 0; i < list.size(); i++) {
arr[list.get(i)] = true;
}
// for (int i = 0; i < list.size(); i++) {
// pw.print(list.get(i)+1 + " ");
// }
// pw.println(Arrays.toString(arr));
// int xtraCount = 0;
int[][] bad = new int[cities-count][2];
int pos = 0;
for (int i = 0; i < arr.length; i++) {
if (!arr[i]) {
bad[pos][0] = i;
List<Integer> temp = g.DFS(i);
for (int j = 0; j < temp.size(); j++) {
if (arr[temp.get(j)]) {
temp.remove(j);
j--;
}
}
bad[pos][1] = temp.size();
pos++;
// List<Integer> temp = g.DFS(i);
//// for (int k = 0; k < temp.size(); k++) {
//// pw.print(temp.get(k)+1 + " ");
//// }
//// pw.println();
// for (int j = 0; j < temp.size(); j++) {
// if (!arr[temp.get(j)]) {
// arr[temp.get(j)] = true;
// xtraCount++;
// }
// }
// xtraCount--;
//// pw.println("xtraCount = " + xtraCount);
}
}
int[] bad2 = new int[cities-count];
for (int i = 0; i < bad2.length; i++) {
bad2[i] = bad[i][1];
}
int[] bad3 = new int[cities-count];
for (int i = 0; i < bad3.length; i++) {
bad3[i] = bad[i][0];
}
sort(bad2, 0, bad2.length-1, bad3);
// sortbyColumn(bad, 1);
int count3 = 0;
boolean ab = false;
for (int i = bad.length-1; i >= 0; i--) {
ab = false;
List<Integer> temp = g.DFS(bad3[i]);
for (int j = 0; j < temp.size(); j++) {
if (!arr[temp.get(j)]) {
arr[temp.get(j)] = true;
ab = true;
}
}
if (ab) {
count3++;
}
}
// pw.println(Arrays.toString(bad2));
// pw.println(Arrays.toString(bad3));
// pw.println(Arrays.deepToString(bad));
// pw.println(count);
// pw.println(xtraCount);
// pw.println(cities-count-xtraCount);
pw.println(count3);
pw.close();
}
static class FastScanner {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastScanner(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public BigInteger readBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
public int[] nextIntArray(int n){
int[] array=new int[n];
for(int i=0;i<n;++i)array[i]=nextInt();
return array;
}
public int[] nextSortedIntArray(int n){
int array[]=nextIntArray(n);
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for(int i = 0; i < n; i++){
pq.add(array[i]);
}
int[] out = new int[n];
for(int i = 0; i < n; i++){
out[i] = pq.poll();
}
return out;
}
public int[] nextSumIntArray(int n){
int[] array=new int[n];
array[0]=nextInt();
for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt();
return array;
}
public ArrayList<Integer>[] nextGraph(int n, int m){
ArrayList<Integer>[] adj = new ArrayList[n];
for(int i = 0; i < n; i++){
adj[i] = new ArrayList<Integer>();
}
for(int i = 0; i < m; i++){
int u = nextInt(); int v = nextInt();
u--; v--;
adj[u].add(v); adj[v].add(u);
}
return adj;
}
public ArrayList<Integer>[] nextTree(int n){
return nextGraph(n, n-1);
}
public long[] nextLongArray(int n){
long[] array=new long[n];
for(int i=0;i<n;++i)array[i]=nextLong();
return array;
}
public long[] nextSumLongArray(int n){
long[] array=new long[n];
array[0]=nextInt();
for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt();
return array;
}
public long[] nextSortedLongArray(int n){
long array[]=nextLongArray(n);
Arrays.sort(array);
return array;
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
} | Java | ["9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1", "5 4 5\n1 2\n2 3\n3 4\n4 1"] | 2 seconds | ["3", "1"] | NoteThe first example is illustrated by the following: For example, you can add roads ($$$6, 4$$$), ($$$7, 9$$$), ($$$1, 7$$$) to make all the cities reachable from $$$s = 1$$$.The second example is illustrated by the following: In this example, you can add any one of the roads ($$$5, 1$$$), ($$$5, 2$$$), ($$$5, 3$$$), ($$$5, 4$$$) to make all the cities reachable from $$$s = 5$$$. | Java 11 | standard input | [
"dfs and similar",
"greedy",
"graphs"
] | c02922c33eb816eea872b4d8a3c1dc0e | The first line of input consists of three integers $$$n$$$, $$$m$$$ and $$$s$$$ ($$$1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$$$) — the number of cities, the number of roads and the index of the capital. Cities are indexed from $$$1$$$ to $$$n$$$. The following $$$m$$$ lines contain roads: road $$$i$$$ is given as a pair of cities $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$). For each pair of cities $$$(u, v)$$$, there can be at most one road from $$$u$$$ to $$$v$$$. Roads in opposite directions between a pair of cities are allowed (i.e. from $$$u$$$ to $$$v$$$ and from $$$v$$$ to $$$u$$$). | 2,000 | Print one integer — the minimum number of extra roads needed to make all the cities reachable from city $$$s$$$. If all the cities are already reachable from $$$s$$$, print 0. | standard output | |
PASSED | 424f893abe7abdd808ba4ec7b20ffea6 | train_001.jsonl | 1529591700 | There are $$$n$$$ cities and $$$m$$$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?New roads will also be one-way. | 256 megabytes |
// Imports
import java.util.*;
import java.io.*;
public class E999 {
/**
* @param args the command line arguments
* @throws IOException, FileNotFoundException
*/
public static void main(String[] args) throws IOException, FileNotFoundException {
// TODO UNCOMMENT WHEN ALGORITHM CORRECT
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
// TODO code application logic here
StringTokenizer st = new StringTokenizer(f.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int S = Integer.parseInt(st.nextToken()) - 1;
ArrayList<Integer>[] adj = new ArrayList[N];
boolean[] isGood = new boolean[N];
int[][] nextTo = new int[N][2];
for(int i = 0; i < N; i++) {
adj[i] = new ArrayList<>();
nextTo[i][1] = i;
}
for(int i = 0; i < M; i++) {
StringTokenizer edge = new StringTokenizer(f.readLine());
int A = Integer.parseInt(edge.nextToken()) - 1;
int B = Integer.parseInt(edge.nextToken()) - 1;
adj[A].add(B);
}
boolean[] visited = new boolean[N];
Queue<Integer> bfs = new ArrayDeque<>();
bfs.add(S);
visited[S] = true;
isGood[S] = true;
while(!bfs.isEmpty()) {
int curr = bfs.poll();
for(int i : adj[curr]) {
if(!visited[i]) {
visited[i] = true;
isGood[i] = true;
nextTo[i][0] = -1;
bfs.add(i);
}
}
}
for(int i = 0; i < N; i++) {
visited = new boolean[N];
if(!isGood[i]) {
int sum = 0;
visited[i] = true;
bfs.add(i);
while(!bfs.isEmpty()) {
int curr = bfs.poll();
sum++;
for(int j : adj[curr]) {
if(!visited[j] && !isGood[j]) {
visited[j] = true;
bfs.add(j);
}
}
}
nextTo[i][0] = sum;
}
}
Arrays.sort(nextTo, (int[] o1, int[] o2) -> o2[0] - o1[0]);
int curr = 0;
for(int i = 0; i < nextTo.length; i++) {
if(nextTo[i][0] == -1) {
break;
}
if(!isGood[nextTo[i][1]]) {
curr++;
visited = new boolean[N];
visited[nextTo[i][1]] = true;
isGood[nextTo[i][1]] = true;
bfs.add(nextTo[i][1]);
while(!bfs.isEmpty()) {
int next = bfs.poll();
for(int j : adj[next]) {
if(!visited[j]) {
visited[j] = true;
isGood[j] = true;
bfs.add(j);
}
}
}
}
}
System.out.println(curr);
}
}
| Java | ["9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1", "5 4 5\n1 2\n2 3\n3 4\n4 1"] | 2 seconds | ["3", "1"] | NoteThe first example is illustrated by the following: For example, you can add roads ($$$6, 4$$$), ($$$7, 9$$$), ($$$1, 7$$$) to make all the cities reachable from $$$s = 1$$$.The second example is illustrated by the following: In this example, you can add any one of the roads ($$$5, 1$$$), ($$$5, 2$$$), ($$$5, 3$$$), ($$$5, 4$$$) to make all the cities reachable from $$$s = 5$$$. | Java 11 | standard input | [
"dfs and similar",
"greedy",
"graphs"
] | c02922c33eb816eea872b4d8a3c1dc0e | The first line of input consists of three integers $$$n$$$, $$$m$$$ and $$$s$$$ ($$$1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$$$) — the number of cities, the number of roads and the index of the capital. Cities are indexed from $$$1$$$ to $$$n$$$. The following $$$m$$$ lines contain roads: road $$$i$$$ is given as a pair of cities $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$). For each pair of cities $$$(u, v)$$$, there can be at most one road from $$$u$$$ to $$$v$$$. Roads in opposite directions between a pair of cities are allowed (i.e. from $$$u$$$ to $$$v$$$ and from $$$v$$$ to $$$u$$$). | 2,000 | Print one integer — the minimum number of extra roads needed to make all the cities reachable from city $$$s$$$. If all the cities are already reachable from $$$s$$$, print 0. | standard output | |
PASSED | 6219867645a3bad69833cb06e0ad62c2 | train_001.jsonl | 1529591700 | There are $$$n$$$ cities and $$$m$$$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?New roads will also be one-way. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
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);
EReachabilityFromTheCapital solver = new EReachabilityFromTheCapital();
solver.solve(1, in, out);
out.close();
}
static class EReachabilityFromTheCapital {
ArrayList<Integer>[] graph;
boolean[] vis;
boolean[] vis2;
public void solve(int testNumber, InputReader in, OutputWriter out) {
int ntc = 1;
// ntc = in.nextInt();
while ((ntc--) > 0) {
int n = in.nextInt();
int m = in.nextInt();
int s = in.nextInt();
graph = new ArrayList[n + 1];
for (int i = 0; i <= n; i++) {
graph[i] = new ArrayList<>();
}
vis = new boolean[n + 1];
vis2 = new boolean[n + 1];
for (int i = 0; i < m; i++) {
int u = in.nextInt();
int v = in.nextInt();
graph[u].add(v);
}
dfs(s);
ArrayList<_C.Pair<Integer, Integer>> pairs = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (i == s || vis[i]) continue;
vis2 = new boolean[n + 1];
int c = dfs2(i);
if (c > 0) {
pairs.add(new _C.Pair<>(i, c));
}
}
int ans = 0;
Collections.sort(pairs, (a, b) -> b.s - a.s);
for (_C.Pair<Integer, Integer> pair : pairs) {
if (!vis[pair.f]) {
dfs(pair.f);
ans += 1;
}
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
ans += 1;
}
}
out.println(ans);
}
}
void dfs(int u) {
vis[u] = true;
for (int v : graph[u]) {
if (!vis[v]) {
dfs(v);
}
}
}
int dfs2(int u) {
int c = 0;
vis2[u] = true;
for (int v : graph[u]) {
if (!vis2[v] && !vis[v]) {
c += (1 + dfs2(v));
}
}
return c;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(long i) {
writer.println(i);
}
}
static class _C {
static public class Pair<F extends Comparable<F>, S extends Comparable<S>> implements Comparable<_C.Pair<F, S>> {
public F f;
public S s;
public Pair(F f, S s) {
this.f = f;
this.s = s;
}
public int compareTo(_C.Pair<F, S> o) {
int t = f.compareTo(o.f);
if (t == 0) return s.compareTo(o.s);
return t;
}
public int hashCode() {
return (31 + f.hashCode()) * 31 + s.hashCode();
}
public boolean equals(Object o) {
if (!(o instanceof _C.Pair)) return false;
if (o == this) return true;
_C.Pair p = (_C.Pair) o;
return f.equals(p.f) && s.equals(p.s);
}
public String toString() {
return "{" + f + ", " + s + "}";
}
}
}
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 nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1", "5 4 5\n1 2\n2 3\n3 4\n4 1"] | 2 seconds | ["3", "1"] | NoteThe first example is illustrated by the following: For example, you can add roads ($$$6, 4$$$), ($$$7, 9$$$), ($$$1, 7$$$) to make all the cities reachable from $$$s = 1$$$.The second example is illustrated by the following: In this example, you can add any one of the roads ($$$5, 1$$$), ($$$5, 2$$$), ($$$5, 3$$$), ($$$5, 4$$$) to make all the cities reachable from $$$s = 5$$$. | Java 11 | standard input | [
"dfs and similar",
"greedy",
"graphs"
] | c02922c33eb816eea872b4d8a3c1dc0e | The first line of input consists of three integers $$$n$$$, $$$m$$$ and $$$s$$$ ($$$1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$$$) — the number of cities, the number of roads and the index of the capital. Cities are indexed from $$$1$$$ to $$$n$$$. The following $$$m$$$ lines contain roads: road $$$i$$$ is given as a pair of cities $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$). For each pair of cities $$$(u, v)$$$, there can be at most one road from $$$u$$$ to $$$v$$$. Roads in opposite directions between a pair of cities are allowed (i.e. from $$$u$$$ to $$$v$$$ and from $$$v$$$ to $$$u$$$). | 2,000 | Print one integer — the minimum number of extra roads needed to make all the cities reachable from city $$$s$$$. If all the cities are already reachable from $$$s$$$, print 0. | standard output | |
PASSED | e1d3acae25ed19e5c50b141794ee731f | train_001.jsonl | 1529591700 | There are $$$n$$$ cities and $$$m$$$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?New roads will also be one-way. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
import javafx.util.Pair;
public class Solve6 {
public static void main(String[] args) {
PrintWriter pw = new PrintWriter(System.out);
new Solve6().solve(pw);
pw.flush();
pw.close();
}
public void solve(PrintWriter pw) {
FastReader sc = new FastReader();
int n = sc.nextInt(), m = sc.nextInt(), s = sc.nextInt();
ArrayList<Integer>[] adj = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) {
adj[i] = new ArrayList();
}
for (int i = 0; i < m; i++) {
int x = sc.nextInt(), y = sc.nextInt();
adj[x].add(y);
}
boolean[] vis = new boolean[n + 1];
dfs(s, adj, vis, new ArrayList());
ArrayList<Integer> order = new ArrayList();
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
dfs(i, adj, vis, order);
}
}
vis = new boolean[n + 1];
int ans = 0;
for (int i = order.size() - 1; i >= 0; i--) {
if (!vis[order.get(i)]) {
dfs(order.get(i), adj, vis, new ArrayList());
++ans;
}
}
pw.println(ans);
}
public void dfs(int u, ArrayList<Integer>[] adj, boolean[] vis, ArrayList<Integer> order) {
vis[u] = true;
for (Integer v : adj[u]) {
if (!vis[v]) {
dfs(v, adj, vis, order);
}
}
order.add(u);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
}
}
public String next() {
if (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public boolean hasNext() throws IOException {
if (st != null && st.hasMoreTokens()) {
return true;
}
String s = br.readLine();
if (s == null || s.isEmpty()) {
return false;
}
st = new StringTokenizer(s);
return true;
}
}
}
| Java | ["9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1", "5 4 5\n1 2\n2 3\n3 4\n4 1"] | 2 seconds | ["3", "1"] | NoteThe first example is illustrated by the following: For example, you can add roads ($$$6, 4$$$), ($$$7, 9$$$), ($$$1, 7$$$) to make all the cities reachable from $$$s = 1$$$.The second example is illustrated by the following: In this example, you can add any one of the roads ($$$5, 1$$$), ($$$5, 2$$$), ($$$5, 3$$$), ($$$5, 4$$$) to make all the cities reachable from $$$s = 5$$$. | Java 11 | standard input | [
"dfs and similar",
"greedy",
"graphs"
] | c02922c33eb816eea872b4d8a3c1dc0e | The first line of input consists of three integers $$$n$$$, $$$m$$$ and $$$s$$$ ($$$1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$$$) — the number of cities, the number of roads and the index of the capital. Cities are indexed from $$$1$$$ to $$$n$$$. The following $$$m$$$ lines contain roads: road $$$i$$$ is given as a pair of cities $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$). For each pair of cities $$$(u, v)$$$, there can be at most one road from $$$u$$$ to $$$v$$$. Roads in opposite directions between a pair of cities are allowed (i.e. from $$$u$$$ to $$$v$$$ and from $$$v$$$ to $$$u$$$). | 2,000 | Print one integer — the minimum number of extra roads needed to make all the cities reachable from city $$$s$$$. If all the cities are already reachable from $$$s$$$, print 0. | standard output | |
PASSED | 0438607c5a20fa23daea99d73e91e65b | train_001.jsonl | 1529591700 | There are $$$n$$$ cities and $$$m$$$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?New roads will also be one-way. | 256 megabytes | import java.util.*;
import java.io.*;
import java.io.File;
import java.util.AbstractSet;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Hello {
static int []visit = new int[10000];
static int n;
static int m;
static int s;
static ArrayList<Integer> e[] = new ArrayList[10000];
static void dfs(int s, int p)
{
visit[s] = p;
for (int i = 0;i < e[s].size();++i) {
int v = e[s].get(i);
if (visit[v] != p) dfs(v, p);
}
}
public static void main(String[] args) {
//try {
// FileInputStream fis = new FileInputStream("data.txt");
// System.setIn(fis);
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
s = in.nextInt();
for (int i = 0; i <= n; i++) {
e[i] = new ArrayList<>();
}
for (int i = 0;i < m;++i) {
int u = in.nextInt();
int v = in.nextInt();
e[u].add(v);
}
for (int i = 1;i <= n;++i) {
if (visit[i] == 0) {
dfs(i, i);
}
}
dfs(s, s);
Set edges = new HashSet<>();
for (int i = 1; i <= n; i++) {
edges.add(visit[i]);
}
System.out.println(edges.size() - 1);
//} catch (FileNotFoundException e){
// System.out.println("error");
//}
}
}
| Java | ["9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1", "5 4 5\n1 2\n2 3\n3 4\n4 1"] | 2 seconds | ["3", "1"] | NoteThe first example is illustrated by the following: For example, you can add roads ($$$6, 4$$$), ($$$7, 9$$$), ($$$1, 7$$$) to make all the cities reachable from $$$s = 1$$$.The second example is illustrated by the following: In this example, you can add any one of the roads ($$$5, 1$$$), ($$$5, 2$$$), ($$$5, 3$$$), ($$$5, 4$$$) to make all the cities reachable from $$$s = 5$$$. | Java 11 | standard input | [
"dfs and similar",
"greedy",
"graphs"
] | c02922c33eb816eea872b4d8a3c1dc0e | The first line of input consists of three integers $$$n$$$, $$$m$$$ and $$$s$$$ ($$$1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$$$) — the number of cities, the number of roads and the index of the capital. Cities are indexed from $$$1$$$ to $$$n$$$. The following $$$m$$$ lines contain roads: road $$$i$$$ is given as a pair of cities $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$). For each pair of cities $$$(u, v)$$$, there can be at most one road from $$$u$$$ to $$$v$$$. Roads in opposite directions between a pair of cities are allowed (i.e. from $$$u$$$ to $$$v$$$ and from $$$v$$$ to $$$u$$$). | 2,000 | Print one integer — the minimum number of extra roads needed to make all the cities reachable from city $$$s$$$. If all the cities are already reachable from $$$s$$$, print 0. | standard output | |
PASSED | 4680e3adae39366e0f52ddbffd8886e9 | train_001.jsonl | 1529591700 | There are $$$n$$$ cities and $$$m$$$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?New roads will also be one-way. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Stack;
import java.util.ArrayList;
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);
EReachabilityFromTheCapital solver = new EReachabilityFromTheCapital();
solver.solve(1, in, out);
out.close();
}
static class EReachabilityFromTheCapital {
int cnt;
int cur;
Stack<Vertex> stack;
Vertex[] vertices;
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int s = in.nextInt() - 1;
stack = new Stack<>();
vertices = new Vertex[n + n];
for (int i = 0; i < n + n; ++i) {
vertices[i] = new Vertex();
vertices[i].adj = new ArrayList<>();
}
for (int i = 0; i < m; ++i) {
int u = in.nextInt() - 1;
int v = in.nextInt() - 1;
vertices[u].adj.add(vertices[v]);
}
cur = n;
for (int i = 0; i < n; ++i) {
if (vertices[i].dead == 0) vertices[i].dfs();
}
//76908cc0
//76908cc0
for (int i = n; i < cur; ++i) {
for (Vertex next : vertices[i].adj) {
Vertex now = next.at;
if (now == vertices[i]) continue;
now.indegree++;
}
}
int answer = 0;
for (int i = n; i < cur; ++i) {
if (vertices[i].indegree == 0) ++answer;
}
if (vertices[s].at.indegree == 0) --answer;
out.println(answer);
}
class Vertex {
ArrayList<Vertex> adj;
int indegree;
int low;
int num;
int dead;
Vertex at;
void dfs() {
low = num = ++cnt;
stack.push(this);
for (Vertex next : adj) {
if (next.dead == 1) continue;
if (next.num > 0) {
low = Math.min(low, next.num);
} else {
next.dfs();
low = Math.min(low, next.low);
}
}
if (low >= num) {
while (true) {
Vertex now = stack.pop();
now.at = vertices[cur];
now.dead = 1;
for (Vertex next : now.adj) {
now.at.adj.add(next);
}
if (now == this) break;
}
cur++;
}
}
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
this.writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
this.writer.close();
}
public void println(int i) {
this.writer.println(i);
}
}
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 (this.numChars == -1) {
throw new InputMismatchException();
} else {
if (this.curChar >= this.numChars) {
this.curChar = 0;
try {
this.numChars = this.stream.read(this.buf);
} catch (IOException var2) {
throw new InputMismatchException();
}
if (this.numChars <= 0) {
return -1;
}
}
return this.buf[this.curChar++];
}
}
public int nextInt() {
int c;
for (c = this.read(); this.isSpaceChar(c); c = this.read()) {
}
int sgn = 1;
if (c == 45) {
sgn = -1;
c = this.read();
}
int res = 0;
while (c >= 48 && c <= 57) {
res *= 10;
res += c - 48;
c = this.read();
if (this.isSpaceChar(c)) {
return res * sgn;
}
}
throw new InputMismatchException();
}
public boolean isSpaceChar(int c) {
return this.filter != null ? this.filter.isSpaceChar(c) : isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
}
public interface SpaceCharFilter {
boolean isSpaceChar(int var1);
}
}
}
| Java | ["9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1", "5 4 5\n1 2\n2 3\n3 4\n4 1"] | 2 seconds | ["3", "1"] | NoteThe first example is illustrated by the following: For example, you can add roads ($$$6, 4$$$), ($$$7, 9$$$), ($$$1, 7$$$) to make all the cities reachable from $$$s = 1$$$.The second example is illustrated by the following: In this example, you can add any one of the roads ($$$5, 1$$$), ($$$5, 2$$$), ($$$5, 3$$$), ($$$5, 4$$$) to make all the cities reachable from $$$s = 5$$$. | Java 11 | standard input | [
"dfs and similar",
"greedy",
"graphs"
] | c02922c33eb816eea872b4d8a3c1dc0e | The first line of input consists of three integers $$$n$$$, $$$m$$$ and $$$s$$$ ($$$1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$$$) — the number of cities, the number of roads and the index of the capital. Cities are indexed from $$$1$$$ to $$$n$$$. The following $$$m$$$ lines contain roads: road $$$i$$$ is given as a pair of cities $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$). For each pair of cities $$$(u, v)$$$, there can be at most one road from $$$u$$$ to $$$v$$$. Roads in opposite directions between a pair of cities are allowed (i.e. from $$$u$$$ to $$$v$$$ and from $$$v$$$ to $$$u$$$). | 2,000 | Print one integer — the minimum number of extra roads needed to make all the cities reachable from city $$$s$$$. If all the cities are already reachable from $$$s$$$, print 0. | standard output | |
PASSED | efa1145b2db6605118e5c82229b839f4 | train_001.jsonl | 1529591700 | There are $$$n$$$ cities and $$$m$$$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?New roads will also be one-way. | 256 megabytes | import java.util.*;
import java.io.*;
public class E490 {
static ArrayList<Integer> [] adj;
static ArrayList<Integer> [] rev;
static int [] post;
static int c;
static int [] scc;
static boolean [] vis;
static HashSet<Integer> bad = new HashSet<>();
public static void main(String[] args) {
MyScanner sc = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n = sc.nextInt(); int m = sc.nextInt(); int s = sc.nextInt();
adj = new ArrayList[n + 1];
rev = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) {
adj[i] = new ArrayList<>();
rev[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int u = sc.nextInt(); int v= sc.nextInt();
adj[u].add(v);
rev[v].add(u);
}
post = new int[n + 1];
Arrays.fill(post, -1);
c = 0;
vis = new boolean[n + 1];
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
dfs(i, -1);
}
}
vis = new boolean[n + 1];
scc = new int[n + 1];
c = 0;
ArrayList<Integer> process = new ArrayList<>();
for (int i = 1; i <= n; i++) process.add(i);
Collections.sort(process, Comparator.comparingInt(x -> -post[x]));
bad = new HashSet<>();
for (Integer i: process) {
if (!vis[i]) {
dfs2(i, -1);
++c;
}
}
int sourceSCCs = c - bad.size();
int res = sourceSCCs - (bad.contains(scc[s]) ? 0 : 1);
out.println(res);
out.close();
}
static void dfs(int cur, int par) {
++c;
vis[cur] = true;
for (Integer next: rev[cur]) {
if (next != par && !vis[next]) {
dfs(next, cur);
}
}
post[cur] = c++;
}
static void dfs2(int cur, int par) {
scc[cur] = c;
vis[cur] = true;
for (Integer next: adj[cur]) {
if (next != par && !vis[next]) {
dfs2(next, cur);
} else if (next != par) {
if (scc[next] != c) {
bad.add(scc[next]);
}
}
}
}
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1", "5 4 5\n1 2\n2 3\n3 4\n4 1"] | 2 seconds | ["3", "1"] | NoteThe first example is illustrated by the following: For example, you can add roads ($$$6, 4$$$), ($$$7, 9$$$), ($$$1, 7$$$) to make all the cities reachable from $$$s = 1$$$.The second example is illustrated by the following: In this example, you can add any one of the roads ($$$5, 1$$$), ($$$5, 2$$$), ($$$5, 3$$$), ($$$5, 4$$$) to make all the cities reachable from $$$s = 5$$$. | Java 11 | standard input | [
"dfs and similar",
"greedy",
"graphs"
] | c02922c33eb816eea872b4d8a3c1dc0e | The first line of input consists of three integers $$$n$$$, $$$m$$$ and $$$s$$$ ($$$1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$$$) — the number of cities, the number of roads and the index of the capital. Cities are indexed from $$$1$$$ to $$$n$$$. The following $$$m$$$ lines contain roads: road $$$i$$$ is given as a pair of cities $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$). For each pair of cities $$$(u, v)$$$, there can be at most one road from $$$u$$$ to $$$v$$$. Roads in opposite directions between a pair of cities are allowed (i.e. from $$$u$$$ to $$$v$$$ and from $$$v$$$ to $$$u$$$). | 2,000 | Print one integer — the minimum number of extra roads needed to make all the cities reachable from city $$$s$$$. If all the cities are already reachable from $$$s$$$, print 0. | standard output | |
PASSED | fcf401ee509b5c151cb52d9602cb8e14 | train_001.jsonl | 1529591700 | There are $$$n$$$ cities and $$$m$$$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?New roads will also be one-way. | 256 megabytes | import java.util.*;
public class Main
{
static Stack<Integer> st=new Stack<>();
static ArrayList<Integer> edge[];
static boolean pres;
static ArrayList<ArrayList<Integer>> components=new ArrayList<>();
static int vc=0;
static ArrayList<Integer> topo=new ArrayList<>();
public static void dfs(int u,boolean vis[],boolean include)
{
vis[u]=true;
for(int v:edge[u])
{
if(!vis[v])
dfs(v,vis,include);
}
if(include)
topo.add(u);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int s=sc.nextInt();
edge=new ArrayList[n+1];
for(int i=0;i<=n;i++)
edge[i]=new ArrayList<>();
for(int i=0;i<m;i++)
{
int u=sc.nextInt();
int v=sc.nextInt();
edge[u].add(v);
}
int cntr=0;
boolean vis[]=new boolean[n+1];
for(int i=1;i<=n;i++)
{
if(!vis[i])
dfs(i,vis,true);
}
Arrays.fill(vis,false);
dfs(s,vis,false);
for(int i=topo.size()-1;i>=0;i--)
{
if(!vis[topo.get(i)])
{
cntr++;
dfs(topo.get(i),vis,false);
}
}
System.out.println(cntr);
}
} | Java | ["9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1", "5 4 5\n1 2\n2 3\n3 4\n4 1"] | 2 seconds | ["3", "1"] | NoteThe first example is illustrated by the following: For example, you can add roads ($$$6, 4$$$), ($$$7, 9$$$), ($$$1, 7$$$) to make all the cities reachable from $$$s = 1$$$.The second example is illustrated by the following: In this example, you can add any one of the roads ($$$5, 1$$$), ($$$5, 2$$$), ($$$5, 3$$$), ($$$5, 4$$$) to make all the cities reachable from $$$s = 5$$$. | Java 11 | standard input | [
"dfs and similar",
"greedy",
"graphs"
] | c02922c33eb816eea872b4d8a3c1dc0e | The first line of input consists of three integers $$$n$$$, $$$m$$$ and $$$s$$$ ($$$1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$$$) — the number of cities, the number of roads and the index of the capital. Cities are indexed from $$$1$$$ to $$$n$$$. The following $$$m$$$ lines contain roads: road $$$i$$$ is given as a pair of cities $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$). For each pair of cities $$$(u, v)$$$, there can be at most one road from $$$u$$$ to $$$v$$$. Roads in opposite directions between a pair of cities are allowed (i.e. from $$$u$$$ to $$$v$$$ and from $$$v$$$ to $$$u$$$). | 2,000 | Print one integer — the minimum number of extra roads needed to make all the cities reachable from city $$$s$$$. If all the cities are already reachable from $$$s$$$, print 0. | standard output | |
PASSED | 6023b00e2a48e24f9d80479b839d189a | train_001.jsonl | 1529591700 | There are $$$n$$$ cities and $$$m$$$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?New roads will also be one-way. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
static ArrayList<Integer> adj[];
static PrintWriter out = new PrintWriter(System.out);
public static long mod;
static int [][][]notmemo;
static int k;
static int a[];
static int b[];
static int m;
static char c[];
static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int a,int b) {
x=a;
y=b;
}
@Override
public int compareTo(Pair o) {
return x-o.x;
}
}
static Pair s1[];
static int s[];
private static HashSet<Integer> set1;
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int cap=sc.nextInt()-1;
adj=new ArrayList[n];
for (int i = 0; i < adj.length; i++) {
adj[i]=new ArrayList<>();
}
boolean start[]=new boolean[n];
while(m-->0) {
int u=sc.nextInt()-1;
int v=sc.nextInt()-1;
adj[u].add(v);
}
vis=new boolean[n];
int x=dfs(cap)+1;
for (int i = 0; i <n; i++) {
vis=new boolean[n];
}
set1=new HashSet<>();
x=dfs(cap);
int count=0;
visited=new boolean[n];
vis2=new boolean[n];
if(x!=n)
for (int i = 0; i <n; i++) {
if(!vis[i]) {
visited=new boolean[n];
moddfs(i);
vis2[i]=true;
set1.add(i);
}
//System.out.println(set.size());
}
//moddfs(6);
System.out.println(set1.size());
}
static boolean mini[];
static void moddfs(int u) {
visited[u]=true;
vis[u]=true;
for(int v:adj[u]) {
if(!visited[v])
moddfs(v);
if(vis2[v]) {
set1.remove(v);
// vis2[v]=false;
}
// System.out.println(vis2[v]+" "+(v+1));
}
}
static int dfs(int u) {
vis[u]=true;
int count=0;
for(int v:adj[u]) {
if(!vis[v])
count+=1+dfs(v);
}
return count;
}
static int dp(int idx,int count,int last) {
if(idx==n&&count<3) {
return (int) 1e9;
}
if(count==3) {
return 0;
}
if(notmemo[idx][count][last]!=-1) {
return notmemo[idx][count][last];
}
int take=(int) 1e9;
int leave=(int) 1e9;
if(s1[idx].x>last)
take=dp(idx+1,count+1,s1[idx].x)+s1[idx].y;
leave=dp(idx+1,count,last);
return notmemo[idx][count][last]=Math.min(take,leave);
}
static void sieve(int N) // O(N log log N)
{
isComposite = new int[N+1];
isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number
primes = new ArrayList<Integer>();
for (int i = 2; i <= N; ++i) //can loop till i*i <= N if primes array is not needed O(N log log sqrt(N))
if (isComposite[i] == 0) //can loop in 2 and odd integers for slightly better performance
{
primes.add(i);
if(1l * i * i <= N)
for (int j = i * i; j <= N; j += i) // j = i * 2 will not affect performance too much, may alter in modified sieve
isComposite[j] = 1;
}
}
static TreeSet<Integer> factors;
static ArrayList<Integer> primeFactors(int N) // O(sqrt(N) / ln sqrt(N))
{
ArrayList<Integer> factors = new ArrayList<Integer>(); //take abs(N) in case of -ve integers
int idx = 0, p = primes.get(idx);
while(1l*p * p <= N)
{
while(N % p == 0) { factors.add(p); N /= p; }
p = primes.get(++idx);
}
if(N != 1) // last prime factor may be > sqrt(N)
factors.add(N); // for integers whose largest prime factor has a power of 1
return factors;
}
static String y;
static int nomnom[];
static long fac[];
static class book implements Comparable<book> {
int idx;
long score;
public book(int i, long s) {
idx = i;
score = s;
}
public int compareTo(book o) {
return (int) (o.score - score);
}
}
static class library implements Comparable<library> {
int numofbooks;
int signup;
int shiprate;
int idx;
public library(int a, int b, int c, int idx) {
numofbooks = a;
signup = b;
shiprate = c;
this.idx = idx;
}
@Override
public int compareTo(library o) {
if(signup==o.signup) {
return o.numofbooks-numofbooks;
}
return signup - o.signup;
}
}
static boolean isOn(int S, int j) {
return (S & 1 << j) != 0;
}
static boolean f = true;
static class SegmentTree { // 1-based DS, OOP
int N; // the number of elements in the array as a power of 2 (i.e. after padding)
int[] array, sTree, lazy;
SegmentTree(int[] in) {
array = in;
N = in.length - 1;
sTree = new int[N << 1]; // no. of nodes = 2*N - 1, we add one to cross out index zero
lazy = new int[N << 1];
//build(1, 1, N);
}
void build(int node, int b, int e) // O(n)
{
if (b == e)
sTree[node] = array[b];
else {
int mid = b + e >> 1;
build(node << 1, b, mid);
build(node << 1 | 1, mid + 1, e);
sTree[node] = sTree[node << 1] + sTree[node << 1 | 1];
}
}
void update_point(int index, int val) // O(log n)
{
index += N - 1;
sTree[index] = val;
while(index>1)
{
index >>= 1;
sTree[index] = Math.max(sTree[index<<1] ,sTree[index<<1|1]);
}
}
void update_range(int i, int j, int val) // O(log n)
{
update_range(1, 1, N, i, j, val);
}
void update_range(int node, int b, int e, int i, int j, int val) {
if (i > e || j < b)
return;
if (b >= i && e <= j) {
sTree[node] += (e - b + 1) * val;
lazy[node] += val;
} else {
int mid = b + e >> 1;
propagate(node, b, mid, e);
update_range(node << 1, b, mid, i, j, val);
update_range(node << 1 | 1, mid + 1, e, i, j, val);
sTree[node] = sTree[node << 1] + sTree[node << 1 | 1];
}
}
void propagate(int node, int b, int mid, int e) {
lazy[node << 1] += lazy[node];
lazy[node << 1 | 1] += lazy[node];
sTree[node << 1] += (mid - b + 1) * lazy[node];
sTree[node << 1 | 1] += (e - mid) * lazy[node];
lazy[node] = 0;
}
int query(int i, int j) {
return query(1, 1, N, i, j);
}
int query(int node, int b, int e, int i, int j) // O(log n)
{
if (i > e || j < b)
return 0;
if (b >= i && e <= j)
return sTree[node];
int mid = b + e >> 1;
// propagate(node, b, mid, e);
int q1 = query(node << 1, b, mid, i, j);
int q2 = query(node << 1 | 1, mid + 1, e, i, j);
return Math.max(q1,q2);
}
}
static int memo[];
static class UnionFind {
int[] p, rank, setSize;
int numSets;
int max[];
public UnionFind(int N) {
p = new int[numSets = N];
rank = new int[N];
setSize = new int[N];
for (int i = 0; i < N; i++) {
p[i] = i;
setSize[i] = 1;
}
}
public int findSet(int i) {
return p[i] == i ? i : (p[i] = findSet(p[i]));
}
public boolean isSameSet(int i, int j) {
return findSet(i) == findSet(j);
}
public void unionSet(int i, int j) {
if (isSameSet(i, j))
return;
numSets--;
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y]) {
p[y] = x;
setSize[x] += setSize[y];
} else {
p[x] = y;
setSize[y] += setSize[x];
if (rank[x] == rank[y])
rank[y]++;
}
}
public int numDisjointSets() {
return numSets;
}
public int sizeOfSet(int i) {
return setSize[findSet(i)];
}
}
/**
* private static void trace(int i, int time) { if(i==n) return;
*
*
* long ans=dp(i,time);
* if(time+a[i].t<a[i].burn&&(ans==dp(i+1,time+a[i].t)+a[i].cost)) {
*
* trace(i+1, time+a[i].t);
*
* l1.add(a[i].idx); return; } trace(i+1,time);
*
* }
**/
static class incpair implements Comparable<incpair> {
int a;
long b;
int idx;
incpair(int a, long dirg, int i) {
this.a = a;
b = dirg;
idx = i;
}
public int compareTo(incpair e) {
return (int) (b - e.b);
}
}
static class decpair implements Comparable<decpair> {
int a;
long b;
int idx;
decpair(int a, long dirg, int i) {
this.a = a;
b = dirg;
idx = i;
}
public int compareTo(decpair e) {
return (int) (e.b - b);
}
}
static long allpowers[];
static class Quad implements Comparable<Quad> {
int u;
int v;
char state;
int turns;
public Quad(int i, int j, char c, int k) {
u = i;
v = j;
state = c;
turns = k;
}
public int compareTo(Quad e) {
return (int) (turns - e.turns);
}
}
static long dirg[][];
static Edge[] driver;
static int n;
static class Edge implements Comparable<Edge> {
int node;
long cost;
Edge(int a, long dirg) {
node = a;
cost = dirg;
}
public int compareTo(Edge e) {
return (int) (cost - e.cost);
}
}
static long manhatandistance(long x, long x2, long y, long y2) {
return Math.abs(x - x2) + Math.abs(y - y2);
}
static long fib[];
static long fib(int n) {
if (n == 1 || n == 0) {
return 1;
}
if (fib[n] != -1) {
return fib[n];
} else
return fib[n] = ((fib(n - 2) % mod + fib(n - 1) % mod) % mod);
}
static class Point implements Comparable<Point>{
long x, y;
Point(long counth, long counts) {
x = counth;
y = counts;
}
@Override
public int compareTo(Point p )
{
return Long.compare(p.y*1l*x, p.x*1l*y);
}
}
static long[][] comb;
static class Triple implements Comparable<Triple> {
int l;
int r;
long cost;
int idx;
public Triple(int a, int b, long l1, int l2) {
l = a;
r = b;
cost = l1;
idx = l2;
}
public int compareTo(Triple x) {
if (l != x.l || idx == x.idx)
return l - x.l;
return -idx;
}
}
static TreeSet<Long> primeFactors(long N) // O(sqrt(N) / ln sqrt(N))
{
TreeSet<Long> factors = new TreeSet<Long>(); // take abs(N) in case of -ve integers
int idx = 0, p = primes.get(idx);
while (p * p <= N) {
while (N % p == 0) {
factors.add((long) p);
N /= p;
}
if (primes.size() > idx + 1)
p = primes.get(++idx);
else
break;
}
if (N != 1) // last prime factor may be > sqrt(N)
factors.add(N); // for integers whose largest prime factor has a power of 1
return factors;
}
static boolean visited[];
/**
* static int bfs(int s) { Queue<Integer> q = new LinkedList<Integer>();
* q.add(s); int count=0; int maxcost=0; int dist[]=new int[n]; dist[s]=0;
* while(!q.isEmpty()) {
*
* int u = q.remove(); if(dist[u]==k) { break; } for(Pair v: adj[u]) {
* maxcost=Math.max(maxcost, v.cost);
*
*
*
* if(!visited[v.v]) {
*
* visited[v.v]=true; q.add(v.v); dist[v.v]=dist[u]+1; maxcost=Math.max(maxcost,
* v.cost); } }
*
* } return maxcost; }
**/
public static boolean FindAllElements(int n, int k) {
int sum = k;
int[] A = new int[k];
Arrays.fill(A, 0, k, 1);
for (int i = k - 1; i >= 0; --i) {
while (sum + A[i] <= n) {
sum += A[i];
A[i] *= 2;
}
}
if (sum == n) {
return true;
} else
return false;
}
static boolean[] vis2;
static boolean f2 = false;
static long[][] matMul(long[][] a2, long[][] b, int p, int q, int r) // C(p x r) = A(p x q) x (q x r) -- O(p x q x
// r)
{
long[][] C = new long[p][r];
for (int i = 0; i < p; ++i) {
for (int j = 0; j < r; ++j) {
for (int k = 0; k < q; ++k) {
C[i][j] = (C[i][j] + (a2[i][k] % mod * b[k][j] % mod)) % mod;
C[i][j] %= mod;
}
}
}
return C;
}
static int memo1[];
static boolean vis[];
static TreeSet<Integer> set = new TreeSet<Integer>();
static long modPow(long ways, long count, long mod) // O(log e)
{
ways %= mod;
long res = 1;
while (count > 0) {
if ((count & 1) == 1)
res = (res * ways) % mod;
ways = (ways * ways) % mod;
count >>= 1;
}
return res % mod;
}
static long gcd(long ans, long b) {
if (b == 0) {
return ans;
}
return gcd(b, ans % b);
}
static int[] isComposite;
static int[] valid;
static ArrayList<Integer> primes;
static ArrayList<Integer> l1;
static TreeSet<Integer> primus = new TreeSet<Integer>();
static void sieveLinear(int N)
{
int[] lp = new int[N + 1]; //lp[i] = least prime divisor of i
for(int i = 2; i <= N; ++i)
{
if(lp[i] == 0)
{
primus.add(i);
lp[i] = i;
}
int curLP = lp[i];
for(int p: primus)
if(p > curLP || p * i > N)
break;
else
lp[p * i] = i;
}
}
public static int[] schuffle(int[] p) {
for (int i = 0; i < p.length; i++) {
int x = (int) (Math.random() * p.length);
int temp = p[x];
p[x] = p[i];
p[i] = temp;
}
return p;
}
static int V;
static long INF = (long) 1E16;
static class Edge2 {
int node;
long cost;
long next;
Edge2(int a, int c, Long long1) {
node = a;
cost = long1;
next = c;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
public int[] nxtArr(int n) throws IOException {
int[] ans = new int[n];
for (int i = 0; i < n; i++)
ans[i] = nextInt();
return ans;
}
}
}
| Java | ["9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1", "5 4 5\n1 2\n2 3\n3 4\n4 1"] | 2 seconds | ["3", "1"] | NoteThe first example is illustrated by the following: For example, you can add roads ($$$6, 4$$$), ($$$7, 9$$$), ($$$1, 7$$$) to make all the cities reachable from $$$s = 1$$$.The second example is illustrated by the following: In this example, you can add any one of the roads ($$$5, 1$$$), ($$$5, 2$$$), ($$$5, 3$$$), ($$$5, 4$$$) to make all the cities reachable from $$$s = 5$$$. | Java 11 | standard input | [
"dfs and similar",
"greedy",
"graphs"
] | c02922c33eb816eea872b4d8a3c1dc0e | The first line of input consists of three integers $$$n$$$, $$$m$$$ and $$$s$$$ ($$$1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$$$) — the number of cities, the number of roads and the index of the capital. Cities are indexed from $$$1$$$ to $$$n$$$. The following $$$m$$$ lines contain roads: road $$$i$$$ is given as a pair of cities $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$). For each pair of cities $$$(u, v)$$$, there can be at most one road from $$$u$$$ to $$$v$$$. Roads in opposite directions between a pair of cities are allowed (i.e. from $$$u$$$ to $$$v$$$ and from $$$v$$$ to $$$u$$$). | 2,000 | Print one integer — the minimum number of extra roads needed to make all the cities reachable from city $$$s$$$. If all the cities are already reachable from $$$s$$$, print 0. | standard output | |
PASSED | 65e45c64471d142d9fe51097ee221901 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.util.Scanner;
public class B349{
public static void main(String[] agrs){
int v;
Scanner f= new Scanner(System.in);
int[] a = new int[1010000];
int[] cn= new int[10];
v=f.nextInt();
for(int i=1;i<10;i++)cn[i]=f.nextInt();
int u=9;
for(int i=8;i>0;i--)
if (cn[i]<cn[u])u=i;
int n=v/cn[u];
for(int i=0;i<n;i++)a[i]=u;
v=v%cn[u];
int x=9;
int t=0;
while (t<n && v>0 && x>u){
while (v+cn[u]-cn[x]<0)x--;
if (x>0){
a[t]=x;
v+=cn[u]-cn[x];
}
t++;
}
for(int i=0;i<n;i++)
System.out.print(a[i]);
if (n<1)System.out.print(-1);
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 8251fac4294b70b759822178780d96f0 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | //package Codeforces.Div2B_202.Code1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/*
* some cheeky quote
*/
public class Main
{
FastScanner in;
PrintWriter out;
public void solve() throws IOException
{
int total = in.nextInt();
int a[] = new int[9];
int min = Integer.MAX_VALUE;
for (int i = 0; i < a.length; i++)
{
a[i] = in.nextInt();
min = Math.min(min, a[i]);
}
StringBuffer result = new StringBuffer();
int left = total % min;
int length = total / min;
while (length-- > 0)
{
for (int i = a.length - 1; i >= 0; i--)
{
if (a[i] - min <= left)
{
left -= (a[i] - min);
result.append(i + 1);
break;
}
}
}
if (result.length() == 0)
{
out.println(-1);
return;
}
out.println(result.toString());
}
public void run()
{
try
{
in = new FastScanner();
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
class FastScanner
{
BufferedReader br;
StringTokenizer st;
FastScanner()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreTokens())
{
try
{
st = new StringTokenizer(br.readLine());
} catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
}
public static void main(String[] arg)
{
new Main().run();
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | bb31f8b00d80b597058b319e81f5adaf | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.*;
import java.util.*;
public class ColorTheFence {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
int v = Integer.parseInt(f.readLine());
int[] a = new int[9];
StringTokenizer st = new StringTokenizer(f.readLine());
for (int i = 0; i < a.length; i++)
a[i] = Integer.parseInt(st.nextToken());
int maxi = 0;
for (int i = 1; i < a.length; i++)
if (v/a[i] >= v/a[maxi])
maxi = i;
for (int i = 0; i < a.length; i++)
if (v/a[i] == v/a[maxi] && a[i] <= a[maxi])
maxi = i;
int[] b = new int[v/a[maxi]];
Arrays.fill(b,maxi);
if (b.length == 0) {
System.out.println(-1);
return;
}
int val = a[maxi];
for (int i = 0; i < a.length; i++)
a[i] -= val;
int diff = v-b.length*val;
int index = 0;
int min = Integer.MAX_VALUE;
for (int i = maxi+1; i < a.length; i++)
if (a[i] < min)
min = a[i];
while (diff >= min) {
int maxj = maxi;
for (int j = maxi+1; j < a.length; j++)
if (a[j] <= diff)
maxj = j;
b[index] = maxj;
diff -= a[maxj];
index++;
}
for (int i = 0; i < b.length; i++)
System.out.print(b[i]+1);
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 0abb6b29618ed0aedfbca54fcbeeca52 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class ColorTheFence {
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
sc = new StringTokenizer(br.readLine());
int r = nxtInt();
int[] dp = new int[r + 1];
int[] p = new int[r + 1];
int[] a = nxtIntArr(9);
for (int rem = 1; rem <= r; rem++)
for (int i = 0; i < 9; i++)
if (a[i] <= rem)
if (1 + dp[rem - a[i]] >= dp[rem]) {
dp[rem] = 1 + dp[rem - a[i]];
p[rem] = i + 1;
}
if (dp[r] == 0)
out.println(-1);
else {
while (p[r] != 0) {
out.print(p[r]);
r = r - a[p[r] - 1];
}
out.println();
}
br.close();
out.close();
}
static BufferedReader br;
static StringTokenizer sc;
static String nxtTok() throws IOException {
while (!sc.hasMoreTokens()) {
String s = br.readLine();
if (s == null)
return null;
sc = new StringTokenizer(s.trim());
}
return sc.nextToken();
}
static int nxtInt() throws IOException {
return Integer.parseInt(nxtTok());
}
static long nxtLng() throws IOException {
return Long.parseLong(nxtTok());
}
static double nxtDbl() throws IOException {
return Double.parseDouble(nxtTok());
}
static int[] nxtIntArr(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nxtInt();
return a;
}
static long[] nxtLngArr(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nxtLng();
return a;
}
static char[] nxtCharArr() throws IOException {
return nxtTok().toCharArray();
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 3b01ebf26e3b892091fd32bd2ee161a7 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int v = in.readInt();
int[] a = new int[10];
int min = 0;
a[0] = Integer.MAX_VALUE;
for (int i = 1; i <= 9; i++) {
a[i] = in.readInt();
if (a[i] < a[min]) {
min = i;
}
}
if (a[min] > v) {
out.printLine(-1);
return;
}
ArrayList<Integer> res = new ArrayList<Integer>();
while (v >= a[min]) {
res.add(min);
v -= a[min];
}
for (int i = 0; i < res.size(); i++) {
int replace = -1;
for (int j = 9; j > min; j--) {
if (v + a[min] - a[j] >= 0) {
replace = j;
v -= (a[j] - a[min]);
break;
}
}
if (replace == -1) {
break;
}
res.set(i, replace);
}
for (int x : res) {
out.print(x);
}
out.printLine();
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine() {
writer.println();
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 78707e30be80d0ab22bdaa3ee45d8491 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
boolean eof;
public static void main(String[] args) throws IOException {
new Main().run();
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return "-1";
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
String nextLine() throws IOException {
return br.readLine();
}
BufferedReader br;
StringTokenizer st;
PrintWriter out;
void run() throws IOException {
InputStream input = System.in;
PrintStream output = System.out;
try {
File f = new File("input.in");
if (f.exists() && f.canRead()) {
input = new FileInputStream(f);
output = new PrintStream("output.out");
}
} catch (Throwable e) {
}
br = new BufferedReader(new InputStreamReader(input));
out = new PrintWriter(output);
solve();
br.close();
out.close();
}
void solve() {
int v = nextInt(), m = 8;
int[] a = new int [9];
for (int i = 0; i < 9; i++) {
a[i] = nextInt();
}
for (int i = 8; i > -1; i--) {
if (a[i] < a[m]) {
m = i;
}
}
if (a[m] > v) {
out.print("-1");
} else {
int s = v / a[m];
int c = v % a[m], d = 0;
for (;(d > -1) && (s > 0);) {
d = -1;
for (int i = 8; i > m; i--) {
if (a[i] <= c + a[m]) {
d = i;
break;
}
}
if (d > -1) {
c = c - a[d] + a[m];
s--;
out.print(d + 1);
}
}
for (int i = 0; i < s; i++) {
out.print(m + 1);
}
}
}
} | Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | a3eabb39b97b59a3fcc9f9b2cbf018a2 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class B349 {
public static void main(String[] args) throws IOException {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int v = in.nextInt();
int[] a = new int[10];
int min = Integer.MAX_VALUE;
for (int i = 1; i <= 9; i++) {
a[i] = in.nextInt();
if (a[i]<min) {
min = a[i];
}
}
//find max digit
int max_digit = v/min;
int left = v - min*max_digit;
int[] b = new int[10];
for (int i = 0; i < max_digit; i++) {
for (int j = 9; j >= 1; j--) {
if (a[j] <= min+left) {
b[j]++;
left -= a[j]-min;
break;
}
}
}
if (max_digit==0) {
out.print("-1");
}
else {
for (int i = 9; i > 0; i--) {
for (int j = 0; j < b[i]; j++) {
out.print(i);
}
}
}
out.flush();
}
public static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 797a1d72d54f3e5da70aa3a7dea48525 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
/**
*
* @author sousnake
*/
public class B {
public static void main(String arga[]) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int v = Integer.parseInt(br.readLine());
String s[] = br.readLine().split(" ");
int[] a = new int[9];
int min=Integer.MAX_VALUE;
int id=-1;
for(int i=0;i<9;i++){
a[i] = Integer.parseInt(s[i]);
if(min>a[i]){
min=a[i];
id=i;
}
if(min==a[i]){
if(i>id)
id=i;
}
}
int len = v/min;
if(len==0){
System.out.println(-1);
return;
}
int ar[] = new int[len];
for(int i=0;i<len;i++){
ar[i]=id;
}
v-= len*min;
//System.out.println(v);
for(int i=0;i<len;i++){
int cst = a[ar[i]];
int tmp=-1;
//System.out.println(v+" "+cst+" "+ar[i]);
for(int j=ar[i]+1;j<9;j++){
if(a[j]<=(v+cst)){
//System.out.println(i+" "+j+" "+a[j]);
tmp=j;
}
}
if(tmp!=-1){
v=v-a[tmp]+cst;
ar[i]=tmp;
}
}
StringBuilder sb =new StringBuilder();
for(int i=0;i<len;i++)
sb.append(ar[i]+1);
System.out.println(sb.toString());
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 541992ae44e313e13edc7fe2a54b9ef8 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes |
import java.util.*;
import java.util.LinkedList;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class tmp {
public static void main(String [] args) throws Exception{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine());
StringBuilder sb = new StringBuilder();
int l = Integer.parseInt(st.nextToken());
int [] arr = new int[10];
st = new StringTokenizer(in.readLine());
int max = 0, min = Integer.MAX_VALUE;
for(int i=1; i<=9; i++){
arr[i] = Integer.parseInt(st.nextToken());
min = Math.min(arr[i], min);
}
max = l/min;
if(max == 0)
System.out.println("-1");
else{
for(int i=9; i>=1 && max > 0 && l > 0; i--){
int cnt = arr[i] > min ? (l - min*max)/(arr[i] - min) : (l/min);
max -= cnt;
l -= arr[i]*cnt;
for(int j=0; j < cnt; j++)
sb.append(i);
}
System.out.println(sb.toString());
}
}
public static void mergeSort(int [ ] a)
{
int[] tmp = new int[a.length];
mergeSort(a, tmp, 0, a.length - 1);
}
private static void mergeSort(int [ ] a, int [ ] tmp, int left, int right)
{
if( left < right )
{
int center = (left + right) / 2;
mergeSort(a, tmp, left, center);
mergeSort(a, tmp, center + 1, right);
merge(a, tmp, left, center + 1, right);
}
}
private static void merge(int[ ] a, int[ ] tmp, int left, int right, int rightEnd )
{
int leftEnd = right - 1;
int k = left;
int num = rightEnd - left + 1;
while(left <= leftEnd && right <= rightEnd)
if(a[left] <= a[right])
tmp[k++] = a[left++];
else
tmp[k++] = a[right++];
while(left <= leftEnd) // Copy rest of first half
tmp[k++] = a[left++];
while(right <= rightEnd) // Copy rest of right half
tmp[k++] = a[right++];
// Copy tmp back
for(int i = 0; i < num; i++, rightEnd--)
a[rightEnd] = tmp[rightEnd];
}
}
class Pair implements Comparable<Pair>{
int x,h;
public Pair(int fi, int ti){
x = fi;
h = ti;
}
public int compareTo(Pair p){
return x < p.x ? -1 : (x == p.x ? (h < p.h ? -1 : 1) : 1);
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 8fb8b7e309f533e2511b8afbe810d44e | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int v = in.nextInt();
int best = -1;
int index = 0;
int[] cost = new int[10];
int[] digits = new int[10];
for (int i = 1; i <= 9; i++) {
cost[i] = in.nextInt();
digits[i] = v / cost[i];
if (digits[i] > 0 && (digits[i] > best || (digits[i] == best && cost[i] < cost[index]))) {
best = digits[i];
index = i;
}
}
if (best == -1) {
out.println(-1);
} else {
int remainder = v % (cost[index] * digits[index]);
for (int i = 1; i <= best; i++) {
for (int j = 9; j >= index; j--) {
if ((cost[j] - cost[index]) <= remainder && cost[j] >= cost[index]) {
out.print(j);
remainder -= cost[j] - cost[index];
break;
}
}
}
out.println();
}
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
} | Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | a11f49a57a99606d9305cb8bdd3ac5f3 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class B {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
int v = scan.nextInt();
int[] weights = new int[10];
int[] chosen = new int[10];
for (int i = 1; i < 10; i++) {
weights[i] = scan.nextInt();
}
int d = 1;
for (int i = 2; i < 10; i++) {
if (weights[d] >= weights[i]) {
d = i;
}
}
int w = weights[d];
chosen[d] = v / w;
if (chosen[d] == 0) {
System.out.println(-1);
return;
}
int rem = v % w;
while (rem > 0) {
rem += w;
int m = maximize(rem, weights);
if (m == d) {
break;
}
chosen[m]++;
rem -= weights[m];
chosen[d]--;
}
StringBuilder builder = new StringBuilder("");
for (int i = 9; i > 0; i--) {
for (int j = 0; j < chosen[i]; j++) {
builder.append(i);
}
}
System.out.println(builder);
}
static int maximize(int rem, int[] weights) {
for (int i = 9; i > 0; i--) {
if (weights[i] <= rem) {
return i;
}
}
return -1;
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 52fae33c5847118c3e155e3646dfdde4 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class B {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
int v = scan.nextInt();
int[] weights = new int[10];
int[] chosen = new int[10];
int d = 1;
for (int i = 1; i < 10; i++) {
weights[i] = scan.nextInt();
if (weights[d] >= weights[i]) {
d = i;
}
}
int w = weights[d];
chosen[d] = v / w;
if (chosen[d] == 0) {
System.out.println(-1);
return;
}
int rem = v % w;
while (rem > 0) {
rem += w;
int m = maximize(rem, weights);
if (m == d) {
break;
}
chosen[m]++;
rem -= weights[m];
chosen[d]--;
}
StringBuilder builder = new StringBuilder();
for (int i = 9; i > 0; i--) {
for (int j = 0; j < chosen[i]; j++) {
builder.append(i);
}
}
System.out.println(builder);
}
static int maximize(int rem, int[] weights) {
for (int i = 9; i > 0; i--) {
if (weights[i] <= rem) {
return i;
}
}
return -1;
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 95f0c51369a663e0972100b98d76fc48 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* Ashesh Vidyut (Drift King) *
*/
public class B {
public static void main(String[] args) {
try {
InputReader in = new InputReader(System.in);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int v = in.readInt();
int ar[] = new int[9];
int minl = Integer.MAX_VALUE;
int mind = -1;
for (int i = 0; i < 9; i++) {
ar[i] = in.readInt();
if(minl >= ar[i]){
minl = ar[i];
mind = i+1;
}
}
if(minl > v){
System.out.println("-1");
return;
}
int nod = v / minl;
int left = v % minl;
for (int i = 0; i < nod; i++) {
if(left == 0){
out.write(Integer.toString(mind));
continue;
}
for (int j = 8; j >= 0; j--) {
if(left + minl>= ar[j]){
out.write(Integer.toString(j+1));
left = (minl + left) - ar[j];
break;
}
}
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class InputReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1)
return -1;
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0)
return -1;
}
return buf[curChar];
}
public int 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 length = readInt();
if (length < 0)
return null;
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++)
bytes[i] = (byte) read();
try {
return new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
return new String(bytes);
}
}
public static 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) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public BigInteger readBigInteger() {
try {
return new BigInteger(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 == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value == -1;
}
public String next() {
return readString();
}
public boolean readBoolean() {
return readInt() == 1;
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 064d49cf2b5d46009d4f1aefb4a9c79a | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Diego Huerfano ( diego.link )
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
final int oo=Integer.MAX_VALUE/2;
int v=in.readInt();
int a[]=in.readInts( 9 );
int dyn[]=new int[v+1];
int answer[]=new int[v+1];
Arrays.fill( answer, -1 );
for( int i=1; i<=v; i++ ) {
for( int j=8; j>=0; j-- ) {
if( a[j]<=i ) {
if( dyn[i]<1+dyn[i-a[j]] ) {
dyn[i]=1+dyn[i-a[j]];
answer[i]=j;
}
}
}
}
if( answer[v]>=0 ) {
while( answer[v]>=0 ) {
out.print( answer[v]+1 );
v-=a[answer[v]];
}
} else out.printLine( -1 );
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] readInts( int n ){
int ans[]=new int[n];
for( int i=0; i<n; i++ ) ans[i]=readInt();
return ans;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print( objects );
writer.println();
}
public void close() {
writer.close();
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | afacbb3416dfd8cf0b01f2acd844f0fc | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class ColorTheFence {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
int V = sc.nextInt();
int[] costs = new int[10];
int cheap = Integer.MAX_VALUE;
int cheapType = 0;
for (int i = 1; i <= 9; i++) {
int a = sc.nextInt();
costs[i] = a;
if (a <= cheap) {
cheap = a;
cheapType = i;
}
}
if (cheap > V) {
System.out.println(-1);
return;
}
int maxDigits = V / cheap;
int rem = V - maxDigits * cheap;
int[] freq = new int[10];
freq[cheapType] = maxDigits;
for (int i = 9; i > cheapType; i--) {
int diff = costs[i] - cheap;
int chng = rem / diff;
freq[i] = chng;
freq[cheapType] -= chng;
rem -= chng * diff;
}
StringBuilder sb = new StringBuilder();
for (int i = 9; i >= 1; i--) {
for (int j = 0; j < freq[i]; j++) {
sb.append(i);
}
}
System.out.println(sb.toString());
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
String str = "";
try { str = br.readLine(); }
catch (IOException e) { e.printStackTrace(); }
return str;
}
}
} | Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | aebf64cc97695addb1e4b9ff4fb5c72d | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.util.Scanner;
/*
* Round 202 - Div2
* B
*
*/
public class R202_B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int liters = sc.nextInt();
int[] costs = new int[10];
for (int i = 1; i <= 9; i++) {
costs[i] = sc.nextInt();
}
StringBuilder number = new StringBuilder();
int min = costs[1];
int index = 1;
for (int i = 2; i <= 9; i++) {
if (costs[i] <= min && liters >= costs[i]) {
min = costs[i];
index = i;
}
}
int n = liters / min;
if (n == 0) {
System.out.println(-1);
} else {
for (int i = 0; i < liters / min; i++) {
number.append(index);
}
liters -= n* min;
for (int k = 0; k < number.length(); k++) {
for (int i = 9; i >= 1; i--) {
if (liters + min >= costs[i]) {
number.setCharAt(k, Character.forDigit(i, 10));
liters += min;
liters -= costs[i];
break;
}
}
}
System.out.println(number);
}
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 13cbf42a0bff2cfd5fc7be98b46265d9 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.*;
import java.util.*;
public final class color_fence
{
static FastScanner sc=new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out=new PrintWriter(System.out);
public static void main(String args[]) throws Exception
{
int v=sc.nextInt(),n=10,min=Integer.MAX_VALUE,minval=-1;
int[] a=new int[10];
for(int i=1;i<=9;i++)
{
a[i]=sc.nextInt();
if(a[i]<=min)
{
min=a[i];
minval=i;
}
}
int val1=v/min,val2=v%min;
int[] b=new int[val1];
Arrays.fill(b,minval);
for(int i=0;i<val1;i++)
{
for(int j=9;j>minval;j--)
{
if(val2+min-a[j]>=0)
{
b[i]=j;
val2-=a[j];
val2+=min;
break;
}
}
}
if(b.length<=0)
{
out.println("-1");
}
else
{
for(int x:b)
{
out.print(x);
}
}
out.close();
}
}
class FastScanner
{
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public String next() throws Exception {
return nextToken().toString();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
} | Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | e81860de32041ee7fd70e619edb484d0 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
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 Nova
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int v = in.nextInt();
int[] a = new int[10];
int mina = 1000111000;
int mini = 0;
// ArrayList<Pair<Integer,Integer>> list = new ArrayList<>();
// list.add(new Pair<>(-1, 0));
for (int i = 1; i <= 9; i++) {
a[i] = in.nextInt();
if (mina >= a[i]) {
mini = i;
mina = a[i];
}
// list.add(new Pair<>(a[i], i));
}
// Collections.sort(list);
int n = v / mina;
int m = v % mina;
if (n <= 0) {
out.println(-1);
return;
}
int[] res = new int[n];
for (int i = 0; i < n; i++) res[i] = mini;
int x, t;
for (int i = 0; i < n; i++) {
if (m <= 0) break;
x = m + mina;
t = 0;
for (int j = 9; j > mini; j--)
if (a[j] <= x) {
t = j;
break;
}
if (t == 0) break;
else {
res[i] = t;
m = m - a[t] + mina;
}
}
for (int i = 0; i < n; i++) out.print(res[i]);
out.println();
}
}
class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 356cb3bb450108ae5f096255727d9a69 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
int[] digits = new int[9];
for (int i = 0; i < 9; i++) {
digits[i] = sc.nextInt();
}
int min = digits[8];
int mindigit = 9;
for (int i = 8; i >= 0; i--) {
if (digits[i] < min) {
min = digits[i];
mindigit = i + 1;
}
}
if (min > l)
System.out.println("-1");
else {
int length = l / min;
int[] number = new int[length];
for (int i = 0; i < length; i++) {
number[i] = mindigit;
}
l = l - min * length;
for (int i = 0; i < length; i++) {
int max = min;
int maxdigit = mindigit;
for (int j = 8; j >= 0; j--) {
if (digits[j] > max && digits[j] <= l + min
&& number[i] < j + 1) {
max = digits[j];
maxdigit = j + 1;
if (mindigit != maxdigit) {
l += min;
l -= max;
number[i] = maxdigit;
}
}
}
}
for (int i = 0; i < length; i++) {
System.out.print(number[i]);
}
System.out.println();
}
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 1e11d410f5e403c6a0500dd1024dbbf9 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B9_ColorTheFence {
public static void main(String... args) throws IOException {
MyScanner sc = new MyScanner();
int n = sc.nextInt();
int[] nn = new int[9];
int min = (int) 10e+6;
int minDig = 1;
for (int i = 0; i < 9; i++) {
int v = sc.nextInt();
nn[i] = v;
if (v <= min){
min = v;
minDig = i+1;
}
}
for (int i = 0; i < 9; i++) {
nn[i] = nn[i] - min;
}
StringBuilder sb = new StringBuilder(n/min);
int buff = n%min;
for (int i = 0; i < n/min; i++) {
int dig = minDig;
for (int j = 8; j > minDig-1; j--) {
if (buff>=nn[j]){
buff-=nn[j];
dig = j+1;
break;
}
}
sb.append(dig);
}
if (sb.length() > 0){
System.out.println(sb);
} else {
System.out.println("-1");
}
}
static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() throws IOException {
if (st == null || !st.hasMoreTokens()) {
String line = br.readLine();
st = new StringTokenizer(line);
}
return st.nextToken();
}
public int nextInt() throws IOException {
String next = next();
return Integer.parseInt(next);
}
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | ce0fa6b547240d7f5b168cd1cec1b99c | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.util.*;
import java.io.*;
public class B {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int v = sc.nextInt();
int[] num = new int[10];
int min = Integer.MAX_VALUE;
for(int i=1;i<10;i++){
num[i] = sc.nextInt();
min = Math.min(min, num[i]);
}
sc.close();
if(min>v){
out.println(-1);
out.flush();
}
else{
int len = v/min;
StringBuilder s = new StringBuilder();
int[] map = new int[10];
for(int i=1;i<=len;i++){
for(int d=9;d>0;d--){
if(v<=0){
break;
}
if((v-num[d]<0) || ((v-num[d])/min<len-i))
continue;
else{
map[d]++;
v-=num[d];
break;
}
}
}
for(int i=9;i>0;i--){
for(int k=0;k<map[i];k++)
s = s.append(i);
}
out.println(s);
out.flush();
}
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | db4712c375cc6edf553625258ffe44d8 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.util.*;
import java.io.*;
import java.awt.Point;
import java.math.BigDecimal;
import java.math.BigInteger;
import static java.lang.Math.*;
// Solution is at the bottom of code
public class B implements Runnable{
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
OutputWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args){
new Thread(null, new B(), "", 128 * (1L << 20)).start();
}
/////////////////////////////////////////////////////////////////////
void init() throws FileNotFoundException{
Locale.setDefault(Locale.US);
if (ONLINE_JUDGE){
in = new BufferedReader(new InputStreamReader(System.in));
out = new OutputWriter(System.out);
}else{
in = new BufferedReader(new FileReader("input.txt"));
out = new OutputWriter("output.txt");
}
}
////////////////////////////////////////////////////////////////
long timeBegin, timeEnd;
void time(){
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
void debug(Object... objects){
if (ONLINE_JUDGE){
for (Object o: objects){
System.err.println(o.toString());
}
}
}
/////////////////////////////////////////////////////////////////////
public void run(){
try{
timeBegin = System.currentTimeMillis();
Locale.setDefault(Locale.US);
init();
solve();
out.close();
time();
}catch (Exception e){
e.printStackTrace(System.err);
System.exit(-1);
}
}
/////////////////////////////////////////////////////////////////////
String delim = " ";
String readString() throws IOException{
while(!tok.hasMoreTokens()){
try{
tok = new StringTokenizer(in.readLine());
}catch (Exception e){
return null;
}
}
return tok.nextToken(delim);
}
String readLine() throws IOException{
return in.readLine();
}
/////////////////////////////////////////////////////////////////
final char NOT_A_SYMBOL = '\0';
char readChar() throws IOException{
int intValue = in.read();
if (intValue == -1){
return NOT_A_SYMBOL;
}
return (char) intValue;
}
char[] readCharArray() throws IOException{
return readLine().toCharArray();
}
/////////////////////////////////////////////////////////////////
int readInt() throws IOException {
return Integer.parseInt(readString());
}
int[] readIntArray(int size) throws IOException {
int[] array = new int[size];
for (int index = 0; index < size; ++index){
array[index] = readInt();
}
return array;
}
int[] readIntArrayWithDecrease(int size) throws IOException {
int[] array = readIntArray(size);
for (int i = 0; i < size; ++i) {
array[i]--;
}
return array;
}
///////////////////////////////////////////////////////////////////
long readLong() throws IOException{
return Long.parseLong(readString());
}
long[] readLongArray(int size) throws IOException{
long[] array = new long[size];
for (int index = 0; index < size; ++index){
array[index] = readLong();
}
return array;
}
////////////////////////////////////////////////////////////////////
double readDouble() throws IOException{
return Double.parseDouble(readString());
}
double[] readDoubleArray(int size) throws IOException{
double[] array = new double[size];
for (int index = 0; index < size; ++index){
array[index] = readDouble();
}
return array;
}
////////////////////////////////////////////////////////////////////
BigInteger readBigInteger() throws IOException {
return new BigInteger(readString());
}
BigDecimal readBigDecimal() throws IOException {
return new BigDecimal(readString());
}
/////////////////////////////////////////////////////////////////////
Point readPoint() throws IOException{
int x = readInt();
int y = readInt();
return new Point(x, y);
}
Point[] readPointArray(int size) throws IOException{
Point[] array = new Point[size];
for (int index = 0; index < size; ++index){
array[index] = readPoint();
}
return array;
}
/////////////////////////////////////////////////////////////////////
List<Integer>[] readGraph(int vertexNumber, int edgeNumber)
throws IOException{
@SuppressWarnings("unchecked")
List<Integer>[] graph = new List[vertexNumber];
for (int index = 0; index < vertexNumber; ++index){
graph[index] = new ArrayList<Integer>();
}
while (edgeNumber-- > 0){
int from = readInt() - 1;
int to = readInt() - 1;
graph[from].add(to);
graph[to].add(from);
}
return graph;
}
/////////////////////////////////////////////////////////////////////
static class IntIndexPair {
static Comparator<IntIndexPair> increaseComparator = new Comparator<B.IntIndexPair>() {
@Override
public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) {
int value1 = indexPair1.value;
int value2 = indexPair2.value;
if (value1 != value2) return value1 - value2;
int index1 = indexPair1.index;
int index2 = indexPair2.index;
return index1 - index2;
}
};
static Comparator<IntIndexPair> decreaseComparator = new Comparator<B.IntIndexPair>() {
@Override
public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) {
int value1 = indexPair1.value;
int value2 = indexPair2.value;
if (value1 != value2) return -(value1 - value2);
int index1 = indexPair1.index;
int index2 = indexPair2.index;
return index1 - index2;
}
};
int value, index;
public IntIndexPair(int value, int index) {
super();
this.value = value;
this.index = index;
}
public int getRealIndex() {
return index + 1;
}
}
IntIndexPair[] readIntIndexArray(int size) throws IOException {
IntIndexPair[] array = new IntIndexPair[size];
for (int index = 0; index < size; ++index) {
array[index] = new IntIndexPair(readInt(), index);
}
return array;
}
/////////////////////////////////////////////////////////////////////
static class OutputWriter extends PrintWriter{
final int DEFAULT_PRECISION = 12;
int precision;
String format, formatWithSpace;
{
precision = DEFAULT_PRECISION;
format = createFormat(precision);
formatWithSpace = format + " ";
}
public OutputWriter(OutputStream out) {
super(out);
}
public OutputWriter(String fileName) throws FileNotFoundException {
super(fileName);
}
public int getPrecision() {
return precision;
}
public void setPrecision(int precision) {
precision = max(0, precision);
this.precision = precision;
format = createFormat(precision);
formatWithSpace = format + " ";
}
private String createFormat(int precision){
return "%." + precision + "f";
}
@Override
public void print(double d){
printf(format, d);
}
public void printWithSpace(double d){
printf(formatWithSpace, d);
}
public void printAll(double...d){
for (int i = 0; i < d.length - 1; ++i){
printWithSpace(d[i]);
}
print(d[d.length - 1]);
}
@Override
public void println(double d){
printlnAll(d);
}
public void printlnAll(double... d){
printAll(d);
println();
}
}
/////////////////////////////////////////////////////////////////////
int[][] steps = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int[][] steps8 = {
{-1, 0}, {1, 0}, {0, -1}, {0, 1},
{-1, -1}, {1, 1}, {1, -1}, {-1, 1}
};
boolean check(int index, int lim){
return (0 <= index && index < lim);
}
/////////////////////////////////////////////////////////////////////
boolean checkBit(int mask, int bitNumber){
return (mask & (1 << bitNumber)) != 0;
}
/////////////////////////////////////////////////////////////////////
void solve() throws IOException {
int v = readInt();
int n = 9;
int[] a = readIntArray(n);
int digit = 0;
int delta = 100500;
for (int i = 9; i > 0; --i) {
if (a[i-1] < delta) {
delta = a[i-1];
digit = i;
}
}
if (v < delta) {
out.println(-1);
return;
}
int size = v / delta;
v -= delta * size;
int cur = 0;
for (int i = 9; i >= digit; --i) {
while (cur < size && (a[i-1] - delta) <= v) {
out.print(i);
v -= (a[i-1] - delta);
cur++;
}
}
}
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | ff2184c756f5b3850e11e4446932c728 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Arrays;
public class Main{
static int t[],a[];
public static void main(String []args){
Scanner s=new Scanner(System.in);
PrintWriter pr=new PrintWriter(System.out);
int v=s.nextInt(),t,num[]=new int[9],array[];
Pair []a=new Pair[9];
for(int i=0;i<9;i++){
t=s.nextInt();
num[i]=t;
a[i]=new Pair(t,i+1);
}
s.close();
Arrays.sort(a);
if(a[0].vol>v)
pr.print(-1);
//System.out.println(-1);
else{
array=new int[v/a[0].vol];
v-=array.length*a[0].vol;
if(v>0){
int n=0;
for(int m=8;m>0&&v>=0;m--){
if(num[m]-a[0].vol<=v&&m+1>a[0].rank){
v-=num[m]-a[0].vol;
array[n++]=m+1;
m=9;
}
// if(num[0]-a[0].vol>v)
// break;
}
}
for(int i=0;i<array.length;i++){
if(array[i]!=0)
pr.print(array[i]);
else
pr.print(a[0].rank);
}
}
pr.flush();
}
}
class Pair implements Comparable<Pair>{
int vol,rank;
public Pair(int a,int b){
vol=a;
rank=b;
}
public int compareTo(Pair x){
return (this.vol-x.vol>0)?1:(this.vol==x.vol)?(this.rank>x.rank?-1:(this.rank==x.rank)?0:1):-1;
}
} | Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | fd8d926957612aabad52284ca4126370 | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.StringTokenizer;
/**
* @author artyom
*/
public class ColorTheFence implements Runnable {
private BufferedReader in;
private PrintStream out;
private StringTokenizer tok;
private void solve() throws IOException {
int v = nextInt();
int[] a = readArray(9);
int min = min(a);
if (min > v) {
out.println(-1);
return;
}
int p = v / min - 1;
StringBuilder s = new StringBuilder();
while (p >= 0) {
for (int i = 8; i >= 0; i--) {
if (v >= a[i] && (v - a[i]) / min == p) {
s.append(i + 1);
v -= a[i];
p--;
break;
}
}
}
out.println(s);
}
//--------------------------------------------------------------
public static void main(String[] args) {
new ColorTheFence().run();
}
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = System.out;
tok = null;
solve();
in.close();
} catch (IOException e) {
System.exit(0);
}
}
private int min(int[] a) {
int min = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] < min) {
min = a[i];
}
}
return min;
}
private int[] readArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
private static String join(Iterable<?> entries, CharSequence separator) {
StringBuilder sb = new StringBuilder();
for (Iterator<?> iterator = entries.iterator(); iterator.hasNext(); ) {
sb.append(iterator.next());
if (iterator.hasNext()) {
sb.append(separator);
}
}
return sb.toString();
}
private String nextToken() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
private short nextShort() throws IOException {
return Short.parseShort(nextToken());
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
} | Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | ac61a91c8f8c61f954656033e1b03e0a | train_001.jsonl | 1380295800 | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence. | 256 megabytes |
import java.util.*;
import java.io.*;
public class Main implements Runnable {
public void solve() throws IOException {
StringBuilder sb = new StringBuilder("");
int avail = nextInt();
int[] ten = new int[10];
int can = 0;
int id = -1;
for (int i = 1; i < 10; i++) {
ten[i] = nextInt();
if (avail / ten[i] > can) {
can = avail / ten[i];
id = i;
}
}
if (can == 0) {
sb.append("-1");
} else {
char[] ans = new char[can];
Arrays.fill(ans, (char) ('0' + id));
int remain = avail - (can * ten[id]);
for (int i = 0; i < can; i++) {
int currd = ans[i] - '0';
for (int j = 9; j > currd; j--) {
if (remain + ten[currd] - ten[j] >= 0) {
remain = remain - (-ten[currd] + ten[j]);
ans[i] = (char) (j + '0');
break;
}
}
sb.append(ans[i]);
}
}
sb.append("\n");
System.out.print(sb);
}
long sum(long[] sum, int si, int K) {
return sum[si + K] - sum[si];
}
public void shuffle(int[] a) {
int n = a.length;
Random rnd = new Random(System.nanoTime());
for (int i = 0; i < n; i++) {
int v = rnd.nextInt(n - i);
int temp = a[i + v];
a[i + v] = a[i];
a[i] = temp;
}
}
void print(Object... obj) {
for (Object o : obj) {
System.out.println(o);
}
}
void print(long[] a) {
System.out.println();
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
void print(int[][] arr) {
System.out.println();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
void print(boolean[][] arr) {
System.out.println();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
//-----------------------------------------------------------
public static void main(String[] args) {
new Main().run();
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
tok = null;
solve();
in.close();
} catch (IOException e) {
System.exit(0);
}
}
public String nextToken() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
BufferedReader in;
StringTokenizer tok;
}
| Java | ["5\n5 4 3 2 1 2 3 4 5", "2\n9 11 1 12 5 8 9 10 6", "0\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["55555", "33", "-1"] | null | Java 7 | standard input | [
"dp",
"implementation",
"greedy",
"data structures"
] | ace9fbabc2eda81b4e4adf4f2d5ad402 | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | 1,700 | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | standard output | |
PASSED | 88711900e3e89133d19b2c0ef68d9d97 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes |
import java.io.*;
import java.lang.String;
import java.util.*;
public class Main {
private static int BinarySearch(int a[], int low, int high, int target) {
if (low > high)
return -1;
int mid = low + (high - low) / 2;
if (a[mid] == target)
return mid;
if (a[mid] > target)
high = mid - 1;
if (a[mid] < target)
low = mid + 1;
return BinarySearch(a, low, high, target);
}
static int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static boolean[] sieve(int n) {
boolean[] prime = new boolean[n + 1];
for (int i = 0; i < n; i++)
prime[i] = true;
for (int i = 2; i * i <= n; i++) {
if (prime[i] == true) {
for (int p = i * i; p <= n; p += i)
prime[p] = false;
}
}
return prime;
}
static int max(int x, int y) {
return (x > y) ? x : y;
}
static long smallestPrimeDivisor(long n) {
if (n % 2 == 0)
return 2;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return i;
}
return n;
}
public static boolean IsPalindrome(String s, int n) {
for (int i = 0; i < n / 2; i++) {
if (s.charAt(i) != s.charAt(n - i - 1)) {
return false;
}
}
return true;
}
public static boolean isPrime(int n){
if (n <= 1)
return false;
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
FastReader f = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
StringBuilder stb = new StringBuilder();
/* int T = f.nextInt();
while (T-- > 0) {
}*/
int n=f.nextInt();
for (int i = 4; i <n ; i++) {
if (!isPrime(i) && !isPrime(n-i)){
pw.print(i+" "+(n-i));
break;
}
}
pw.close();
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 8e933582568df183a6f585e510a25eb6 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
public class A472
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if(n % 3 == 0)
System.out.println(6 + " " + (n-6));
else if(n % 3 == 1)
System.out.println(4 + " " + (n-4));
else
System.out.println(8 + " " + (n-8));
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | c68febeeaba7c955f15cfa203ff2b5a7 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.Scanner;
public class tutorial
{
boolean prime(int x)
{
int i,c=0;
for(i=2;i<=x/2;i++)
{
if(x%i==0)
c++;
}
if(c==0)
return false;
else
return true;
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
tutorial t=new tutorial();
int n,i,a,b;
n=s.nextInt();
a=4;b=n-4;
for(i=1;i<=n;i++)
{
if(t.prime(a)&&t.prime(b))
{
if(a+b==n)
{
System.out.println(a+" "+b);
break;
}
}
a++;
b--;
}
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 16174eeb975c5ff0bffc902064f96d75 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
public class cards
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
long n;
n=in.nextLong();
if(n%2!=0)
{
System.out.println("9 "+(n-9));
System.exit(0);
}
else if(n%2==0)
{
System.out.println("4 "+(n-4));
System.exit(0);
}
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | aabfb190f893ea61669106040dd5b3fb | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class design_tut_learn_from_math {
static boolean isPrime(int num)
{
for(int i=2; i<num; i++)
{
if(num%i==0)
{
return false;
}
}
return true;
}
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(br.readLine());
if(num%2==0 && (num/2) % 2==0)
{
System.out.print(num/2 +" "+num/2);
}
else
{
int a=4;
int b=num-4;
while(true)
{
if(!isPrime(a) && !isPrime(b))
{
System.out.print(a+" "+b);
System.exit(0);
}
else
{
a++;
b--;
}
}
}
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | acc717c273ce57ea240cfceb0e700c5a | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
import java.lang.*;
public class Solution{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
for(int i=2;i<number;i++){
if((!isPrime(i))&&(!isPrime(number-i))){
System.out.println(i+ " "+(number-i));
break;
}
}
}
public static boolean isPrime(int a){
for(int i=2;i*i<=a;i++){
if(a%i==0) return false;
}
return true;
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 64d74ed3d991973cc636bfcf2a5889df | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
import java.lang.*;
public class Nonprimes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
boolean[] primes = new boolean[num + 1];
for(int i = 2; i*i <= num; i++) {
if(primes[i] == false) {
for(int j = i * i; j <= num; j+= i) {
primes[j] = true;
}
}
}
int bot;
int top;
if(num % 2 == 0) {
bot = top = num / 2;
} else {
bot = num / 2;
top = bot + 1;
}
while(true) {
if(primes[bot] && primes[top]) {
System.out.println(bot + " " + top);
return;
}
bot--;
top++;
}
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | db869379d4682c7ed6b854741e1b02f2 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int target = scan.nextInt();
if (target % 2 == 0)
System.out.println(4 + " " + (target - 4));
else
System.out.println(9 + " " + (target - 9));
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 633d6aa35f56ba93282fc1950566e803 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
import java.io.*;
public class LearnFromMath {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer inputLine = new StringTokenizer(br.readLine());
int n = Integer.parseInt(inputLine.nextToken());
if(n%2==0){
System.out.print(n-8 + " " + 8);
}
else{
System.out.print(n-9 + " " + 9);
}
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | f73f1d1fb0be7d8f221fcd2cb485a816 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.Scanner;
public class LearnFromMath {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
if (n % 2 == 0 ) System.out.println(n-8 + " " + 8);
else System.out.println(n-9 + " " + 9);
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 14cad34d2ee3cf910e2f74cd1ac09430 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
public class Present {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
if(n % 2 == 0) {
System.out.println(8+" "+(n-8));
}
else {
System.out.println(9+" "+(n-9));
}
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 909aedcd20469746f68cdbefbcc06a31 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.Scanner;
public class MathLearn
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
sc.close();
for (int i = 1; i <= num/2; i++)
{
if (!isPrime(i) && !isPrime(num - i))
{
System.out.println(i + " " + (num - i));
break;
}
}
}
public static boolean isPrime(int n)
{
for (int i = 1; i <= n; i++)
{
if (n % i == 0 && n != i && i != 1)
return false;
}
return true;
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | adecf2c547ca03ab72d892c815ea4454 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
// Problem 472A
public class LearnFromMath{
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int n = sc.nextInt();
boolean[] primes = new boolean[1000001];
for(int i = 2; i < primes.length; ++i)
primes[i] = true;
for(int i = 2; i < primes.length; ++i)
if(primes[i])
for(int j = 2*i; j < primes.length; j += i)
primes[j] = false;
for(int i = 4; i < n; ++i){
if(!primes[i] && !primes[n-i]){
int a = n-i;
out.println(i + " " + a);
break;
}
}
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 0cefe54c1241c78c80a8b9d722720830 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.Scanner;
import java.util.BitSet;
public class intInput {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BitSet primes = new BitSet();
for(int i = 2; i*i < 1000000; i++){
if(primes.get(i) == false) {
for (int j = i + i; j <= 1000000; j += i) primes.set(j);
}
}
int os = 0;
boolean b = false;
boolean c = false;
int x = 0;
for(os = 0; !b; os++) {
if (!c) x = in.nextInt();
c = false;
if(x % 2 == 0) {
if(primes.get((int)x/2+os) && primes.get((int)x/2-os)) {System.out.println((x/2+os) + " " + (x/2-os)); b = true;}
else {
c = true;
continue;
}
}
else {
if(primes.get((int)x/2+os) && primes.get((int)x/2-os + 1)) {System.out.println((x/2+os) + " " + (x/2-os + 1)); b = true;}
else {
c = true;
continue;
}
}
}
in.close();
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | b75d68a1c24b3e1500794108ae4172fe | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | // Codeforces 472A
import java.util.Scanner;
public class CF472A {
static final Scanner SC = new Scanner(System.in);
public static void main(String[] args) {
int n = SC.nextInt();
int[] compositeAddends = getCompositeAddends(n); // Two composite addends of n
out(compositeAddends);
}
// Gets composite addends for n
static int[] getCompositeAddends(int n) {
int[] addends = new int[2];
if (n % 2 == 0) {
addends[0] = 4;
addends[1] = n-4;
}
else {
addends[0] = 9;
addends[1] = n-9;
}
return addends;
}
// Prints output corresponding to array
static void out(int[] arr) {
for (int i = 0; i < arr.length; ++i)
System.out.print(arr[i] + " ");
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 44a095086dea06d229026f0d8b1332a9 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
System.out.println((n % 2 == 0) ? n - 4 + " " + 4 : n - 9 + " " + 9);
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | be926d4933af66fe39c333c81de54c82 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int first = n / 2;
int second = n / 2;
if(n % 2 != 0){
first++;
}
while(!isComposite(first) || !isComposite(second)){
first += 1;
second -= 1;
}
System.out.println(first + " " + second);
}
public static boolean isComposite(int number){
if((number % 2 == 0 && number != 2)
|| (number % 3 == 0 && number != 3)
|| (number % 5 == 0 && number != 5)
|| (number % 7 == 0 && number != 7)){
return true;
}
return false;
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 6679e3b24cc41c687e66712855c476ad | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 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. */
// QAQAQYSYIOIWIN
// outputCopy
// 4
// inputCopy
// QAQQQZZYNOIWIN
// outputCopy
// 3
public class Main
{
static PrintWriter out;
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
out=new PrintWriter(System.out);
}
String next(){
while(st==null || !st.hasMoreElements()){
try{
st= new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str=br.readLine();
}
catch(IOException e){
e.printStackTrace();
}
return str;
}
}
// SHIVAM GUPTA :
// ASCII = 48 + i ;
// SHIVAM GUPTA :
public static int min(int a ,int b , int c, int d)
{
int[] arr = new int[4] ;
arr[0] = a;
arr[1] = b ;
arr[2] = c;
arr[3] = d;
Arrays.sort(arr) ;
return arr[0];
}
public static int max(int a ,int b , int c, int d)
{
int[] arr = new int[4] ;
arr[0] = a;
arr[1] = b ;
arr[2] = c;
arr[3] = d;
Arrays.sort(arr) ;
return arr[3];
}
static int sieve = 1000000 ;
static boolean[] prime = new boolean[sieve + 1] ;
public static void sieveOfEratosthenes()
{
// TRUE == prime
// FALSE == COMPOSITE
// FALSE== 1
for(int i=0;i< sieve + 1;i++)
prime[i] = true;
for(int p = 2; p*p <= sieve; p++)
{
if(prime[p] == true)
{
for(int i = p*p; i <= sieve; i += p)
prime[i] = false;
}
}
}
public static String reverse(String input)
{
String op = "" ;
for(int i = 0; i < input.length() ; i++ )
{
op = input.charAt(i)+ op ;
}
return op ;
}
public static int[] sortI(int[] arr)
{
Arrays.sort(arr) ;
return arr ;
}
public static int[] sortD(int[] arr)
{
Arrays.sort(arr) ;
int i =0 ; int j = arr.length -1 ;
while( i < j)
{
int temp = arr[i] ;
arr[i] =arr[j] ;
arr[j] = temp ;
i++ ; j-- ;
}
return arr ;
}
public static boolean isPossibleTriangle(int a ,int b , int c)
{
if( a + b > c && c+b > a && a +c > b)
{
return true ;
}
return false ;
}
public static int gcd(int a, int b )
{
if(b==0)return a ;
else return gcd(b,a%b) ;
}
public static int lcm(int a, int b ,int c , int d )
{
int temp = lcm(a,b , c) ;
int ans = lcm(temp ,d ) ;
return ans ;
}
public static int lcm(int a, int b ,int c )
{
int temp = lcm(a,b) ;
int ans = lcm(temp ,c) ;
return ans ;
}
public static int lcm(int a , int b )
{
int gc = gcd(a,b);
return (a*b)/gc ;
}
static boolean isPrime(int n)
{
if(n==1)
{
return false ;
}
boolean ans = true ;
for(int i = 2; i*i <= n ;i++)
{
if(n% i ==0)
{
ans = false ;break ;
}
}
return ans ;
}
static boolean isPowerOfTwo(int n)
{
if(n==0)
return false;
if(((n ) & (n-1)) == 0 ) return true ;
else return false ;
}
public static int countDigit(long n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
static final int MAXN = 100001;
static int spf[] = new int[MAXN];
static void sieve()
{
spf[1] = 1;
for (int i=2; i<MAXN; i++)
spf[i] = i;
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
if (spf[i] == i)
{
for (int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}
// The above code works well for n upto the order of 10^7.
// Beyond this we will face memory issues.
// Time Complexity: The precomputation for smallest prime factor is done in O(n log log n)
// using sieve.
// Where as in the calculation step we are dividing the number every time by
// the smallest prime number till it becomes 1.
// So, let’s consider a worst case in which every time the SPF is 2 .
// Therefore will have log n division steps.
// Hence, We can say that our Time Complexity will be O(log n) in worst case.
static Vector<Integer> getFactorization(int x)
{
Vector<Integer> ret = new Vector<>();
while (x != 1)
{
ret.add(spf[x]);
x = x / spf[x];
}
return ret;
}
public static void main (String[] args) throws java.lang.Exception
{
FastReader scn = new FastReader() ;
// int t = scn.nextInt() ;
// for(int i1 = 1; i1<= t ; i1++)
// {
// int x = scn.nextInt() ;int y = scn.nextInt() ; int n = scn.nextInt() ;
// int ans = 0 ;
// if( n % x == 0)
// {
// ans = n+y-x ;
// }
// else{
// ans = (n/x)*x + y ;
// }
// out.println(ans) ;
// }
// }
int n= scn.nextInt() ;
if( n%2 == 0)
{
out.println(4 +" " + (n-4)) ;
}
else{
out.println(9 +" " + (n-9)) ;
}
out.flush() ;
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 48af7db93e84a941ca89b92737fdc32c | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.Scanner;
public class learnFromMath{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a,b;
if(n%2==0)
{
if(composite(n/2))
{
System.out.println(n/2+" "+n/2);
}
else
{
System.out.println((n/2-1)+" "+(n/2+1));
}
}
else
{
if(composite(n/2)&&composite(n/2+1))
{
System.out.println(n/2+" "+(n/2+1));
}
else if(composite(n/2-1)&&composite(n/2+2))
{
System.out.println((n/2-1)+" "+(n/2+2));
}
else
{
System.out.println((n/2-2)+" "+(n/2+3));
}
}
}
static boolean composite(int x){
boolean flag=false;
for(int i=2;i<x/2;i++)
{
if(x%i==0)
{
flag=true;
}
}
if(flag)
{
return flag;
}
else return flag;
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 57e3f96ef8dd5f0825598b23fb0a30b8 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.Scanner;
public class learnFromMath{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a,b;
if(n%2==0)
{
if(composite(n/2))
{
System.out.println(n/2+" "+n/2);
}
else
{
System.out.println((n/2-1)+" "+(n/2+1));
}
}
else
{
if(composite(n/2)&&composite(n/2+1))
{
System.out.println(n/2+" "+(n/2+1));
}
else if(composite(n/2-1)&&composite(n/2+2))
{
System.out.println((n/2-1)+" "+(n/2+2));
}
else
{
System.out.println((n/2-2)+" "+(n/2+3));
}
}
}
static boolean composite(int x){
boolean flag=false;
for(int i=2;i<x/2;i++)
{
if(x%i==0)
{
flag=true;
}
}
return flag;
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 52d6608cd74937f568880f55084eb93a | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
public class Main {
static final int NO_OF_CHARS = 256;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// write your code here
long n = scanner.nextLong();
long a = n / 2, b = a;
boolean comA = false, comB = false;
while(a + b != n || (!comA || !comB))
{
int countA = 0, countB = 0;
for(int i = 1; i <= a / 2; i++)
if(a % i == 0)
countA++;
for(int i = 1; i <= b / 2; i++)
if(b % i == 0)
countB++;
if(countA < 2)
{
a--;
comA = false;
} else {
comA = true;
}
if(countB < 2)
{
b++;
comB = false;
} else {
comB = true;
}
if(a + b > n && comB && comA)
{
a--;
comA = false;
comB = false;
} else if(a + b < n && comB && comA) {
b++;
comA = false;
comB = false;
}
}
System.out.println(a + " " + b);
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 1b2bc74ebb635a9daf1c200c00a06010 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
public class Main{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int n=in.nextInt();
if(n%2==0)
System.out.println("8 "+(n-8));
else System.out.println("9 "+(n-9));
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 83ff58dadae5b62a7db43164e4b8bf4d | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import com.sun.source.tree.Tree;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;
import java.util.regex.Pattern;
public class Example {
public static int Max(int[] p)
{
int Max=0;
int j=0;
for(int i=0;i<p.length;i++)
{
if(p[i]>Max)
{
Max = p[i];
j=i;
}
}
return j;
}
public static int Max(int[] a, int l)
{
int max=0;
for(int i=0;i<a.length-1;i++)
{
if(a[i+1]-a[i]>max)max=a[i+1]-a[i];
}
if(a[0]*2>max) max=a[0]*2;
if((l-a[a.length-1])*2>max)max=(l-a[a.length-1])*2;
return max;
}
public static int[] Sort(int[] a)
{
for(int i=0;i<a.length-1;i++)
{
for(int j=i+1;j<a.length;j++)
{
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
return a;
}
public static boolean Permutation(int[] p)
{
boolean[] temp= new boolean[p.length];
Arrays.fill(temp, false);
for(int i=0;i<p.length;i++)
{
for(int j=0;j<p.length;j++)
{
if(p[i]==j)
{
temp[j]=true;
break;
}
}
}
for(int j=0;j<p.length;j++)
{
if(!temp[j]) return false;
}
return true;
}
public static long Count(long n)
{
long count=0;
if (n>=100)
{
count=count+n/100;
n=n -(n/100)*100;
}
if(n>=20)
{
count=count+n/20;
n=n-(n/20)*20;
}
if(n>=10)
{
count=count+n/10;
n=n-(n/10)*10;
}
if(n>=5)
{
count=count+n/5;
n=n-(n/5)*5;
}
count=count+n;
return count;
}
public static int Min(int a, int b, int c)
{
if(a<=b&&a<=c) return a;
else if(b<=a&&b<=c) return b;
else return c;
}
public static int Ribbon(int[] x,int n)
{
int max=0;
for(int i=n/x[0];i>=0;i--)
{
for (int j=0;j<=n/x[1];j++)
{
int k=n-(i*x[0] +j*x[1]);
if(k%x[2]==0&&k>=0)
{
int temp=k/x[2];
if ((i+j+temp)>max)max=i+j+temp;
}
}
}
return max;
}
public static boolean Prime(int n)
{
if(n==1) return false;
if(n==2||n==3) return true;
for(int i=2;i<=Math.sqrt((double) n);i++)
{
if(n%i==0) return false;
}
return true;
}
public static void main(String[] args) {
int n;
Scanner sc= new Scanner(System.in);
n=sc.nextInt();
for(int i=4;i<n;i++)
{
if(!Prime(i))
{
int a= n-i;
if(!Prime(a))
{
System.out.println(i+" "+a);
break;
}
}
}
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | eef67d347d05c74b105bb03e9d4dee74 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
public class arr {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
if (n % 2 == 0) {
System.out.println((n-4) + " " + 4);
} else {
if (isPrime(n)) {
System.out.println(9 + " " + (n - 9));
} else {
int first = n - 4;
int sec = 4;
while (isPrime(first)) {
first -= 2;
sec += 2;
}
System.out.println(first + " " + sec);
}
}
}
static boolean isPrime(int n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 9c343924cef1d6800c0bf4a697285b40 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
public class arr {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
if (n % 2 == 0) {
System.out.println((n-4) + " " + 4);
} else {
if (isPrime(n)) {
System.out.println(9 + " " + (n - 9));
} else {
int first = n - 4;
int sec = 4;
while (isPrime(first)) {
first -= 2;
sec += 2;
}
System.out.println(first + " " + sec);
}
}
}
static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 2223f18c3aa5020178cdace72a601824 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n%2==0){
System.out.printf("%d %d",(n-4),4);
}else{
System.out.printf("%d %d",(n-9),9);
}
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 93de4dd1a7c18b8005ace2000ea7987c | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
if(x%2==0)
System.out.print("4 "+(x-4));
else System.out.print("9 "+(x-9));
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | b55da64180b542d975b2fed0fc0302c7 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
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) {
FastReader in = new FastReader();
// PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
// n >= 12
// sum of two composite numbers
int num = in.nextInt();
int i = 4;
if (num % 2 == 0) {
System.out.println(num - 4 + " " + 4);
return;
}
System.out.println(num - 9 + " " + 9);
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 49db1d7db62e282814cf1c70890ca1d6 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
public class math {
static boolean[] arr = new boolean[10000001];
public static void sieve(){
arr[0] = true;
arr[1] =true;
for (int i = 2; i*i<arr.length; i++) {
for (int j = i*i; j<arr.length; j+=i) {
if (arr[j]==false) {
arr[j] = true;
}
}
}
}
public static boolean find(int n){
for (int i = 2; i < arr.length; i++) {
if (arr[i]==false) {
if (i==n) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
/*int i ;
Scanner scan = new Scanner(System.in);
i = scan .nextInt();
if (i%2==0) {
System.out.println((i-4)+" "+4);
} else {
System.out.println((i-9)+" "+9);
}*/
sieve();
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int a = 4;
int b = n-4;
for (int i = 1; i<=n; i++) {
if (find(a) && find(b)) {
System.out.println(a+" "+b);
break;
}
a++;
b--;
}
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 22d23eea98db6348901adec1866c5c4d | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
public class math {
public static void main(String[] args) {
int i ;
Scanner scan = new Scanner(System.in);
i = scan .nextInt();
if (i%2==0) {
System.out.println((i-4)+" "+4);
} else {
System.out.println((i-9)+" "+9);
}
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | ba235961415867d0eeae7c8e9e8c592b | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | //package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long x = scanner.nextInt();
if(x%2==0){
System.out.print(8 + " ");
System.out.print(x-8);
}else {
System.out.print(9 + " ");
System.out.print(x-9);
}
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 693bed943c0958946c96f20f6b15b1de | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes |
public class DesignTutorialLearnFromMath {
public static void main(String[] args) {
int n = new java.util.Scanner(System.in).nextInt();
if (n%2 == 0) {
System.out.println(n-8 + " " + 8);
}
else {
System.out.println(n-9 + " " + 9);
}
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | d7eb37e8680502f8a5b9bca5cac78dc3 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | public class Main{
public static void main(String shy[]){
java.util.Scanner sc = new java.util.Scanner(System.in);
int num = sc.nextInt();
if(num % 2 == 0){
System.out.println( "4 " + (num - 4));
}
else
System.out.println((num - 9) + " 9");
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 9f74a63e685e542ecf63315295bfcb0c | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
import java.io.*;
public class DTLearnFromMath
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int num = in.nextInt();
if(num % 2 == 0)
{
int x = num/2;
if(x % 2 == 0)
{
System.out.println(x + " " + x);
}
else
{
System.out.println((x - 1) + " " + (x + 1));
}
}
else
{
System.out.println(9 + " " + (num - 9));
}
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | eb95e79330671d266dd2662ec7d75d25 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
public class Test {
static boolean isPrime(long n) {
if(n == 2 || n == 3)
return true;
if(n < 2 || n % 2 == 0 || n % 3 == 0)
return false;
for(long i = 5; i * i <= n; i += 6)
if(n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
long n = read.nextLong();
for(int i = 2; ; i++) {
if(!isPrime(i) && !isPrime(n - i)) {
System.out.println(i + " " + (n - i));
break;
}
}
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 4d7619fe6dbdeda10a9626b78a76b587 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
public class solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int tgt = in.nextInt();
if ((tgt % 2) == 0) {
int a = tgt -8;
System.out.println("8 " + a);
} else {
int b = tgt-9;
System.out.println("9 " + b);
}
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | da65fdd76b54686e6c46ec161240b499 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
public class A472 {
public static void main(String[] args) {
Scanner i = new Scanner(System.in);
int n1 = i.nextInt();
if (n1 % 2 == 0) {
System.out.println(4 + " " + (n1 - 4));
} else {
System.out.println(9 + " " + (n1 - 9));
}
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 03554045b13bfb5becec4cf3cacb39e0 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.*;
import java.io.*;
import java.util.stream.*;
public class Main{
private static BufferedReader in;
private static BufferedWriter out;
public static void main(String[] args) throws IOException {
open();
StringTokenizer st;
StringBuilder sb;
int n = readInt();
int half = n / 2;
if (n % 2 == 0) {
if (half % 2 == 0) {
out.write(half+"");
out.write(" ");
out.write(half+"");
} else {
out.write(half + 1+"");
out.write(" ");
out.write(half - 1+"");
}
} else {
out.write(9+"");
out.write(" ");
out.write(n - 9+"");
}
close();
}
private static int[] readInts() throws IOException {
return Stream.of(in.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
private static int readInt() throws IOException {
return Integer.parseInt(in.readLine());
}
private static long[] readLongs() throws IOException {
return Stream.of(in.readLine().split(" ")).mapToLong(Long::parseLong).toArray();
}
private static long readLong() throws IOException {
return Long.parseLong(in.readLine());
}
private static double[] readDoubles() throws IOException {
return Stream.of(in.readLine().split(" ")).mapToDouble(Double::parseDouble).toArray();
}
private static double readDouble() throws IOException {
return Double.parseDouble(in.readLine());
}
private static String readString() throws IOException {
return in.readLine();
}
private static int setBit(int S, int j) {
return S | (1 << j);
}
private static int isOn(int S, int j) {
return S & (1 << j);
}
private static int clearBit(int S, int j) {
return S & ~(1 << j);
}
private static int toggleBit(int S, int j) {
return S ^ (1 << j);
}
private static int lowBit(int S) {
return S & (-S);
}
private static int setAll(int n) {
return (1 << n) - 1;
}
private static int modulo(int S, int N) {
return ((S) & (N - 1));
} // returns S % N, where N is a power of 2
private static boolean isPowerOfTwo(int S) {
return (S & (S - 1)) == 0 ? true : false;
}
private static int nearestPowerOfTwo(int S) {
return ((int) Math.pow(2.0, (int) ((Math.log((double) S) / Math.log(2.0)) + 0.5)));
}
private static int turnOffLastBit(int S) {
return ((S) & (S - 1));
}
private static int turnOnLastZero(int S) {
return ((S) | (S + 1));
}
private static int turnOffLastConsecutiveBits(int S) {
return ((S) & (S + 1));
}
private static int turnOnLastConsecutiveZeroes(int S) {
return ((S) | (S - 1));
}
private static void open() {
in = new BufferedReader(new InputStreamReader(System.in));
out = new BufferedWriter(new OutputStreamWriter((System.out)));
}
private static void close() throws IOException {
out.flush();
out.close();
in.close();
}
} | Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | a3a8a9011c44a8ba7e37d6eb74cdf912 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes | import java.util.Scanner;
public class DesignTutorialLearnfromMath_472A {
Scanner sc = new Scanner(System.in);
void LearnMath() {
int n = sc.nextInt();
// x + y = n
if(n % 2 == 0) {
System.out.print("4 ");
System.out.println((n - 4));
}else {
System.out.print((n - 9));
System.out.print(" 9");
}
}
public static void main(String[] args) {
new DesignTutorialLearnfromMath_472A().LearnMath();
}
}
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 5b6140b59b3bac1dee379b4a952bc9cb | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes |
import java.util.Scanner;
public class MyClass {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
if(((a&1)==0)) {
System.out.println("4 " +(a-4));
return;
}
System.out.println("9 " +(a-9));
sc.close();
}
}
// odd -> 1
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | 5b279b0fd21805d4ca513f14a9625e03 | train_001.jsonl | 1411918500 | One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MyClass {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a=Integer.parseInt(br.readLine());
if(((a&1)==0)) {
System.out.println("4 " +(a-4));
return;
}
System.out.println("9 " +(a-9));
}
}
// odd -> 1
| Java | ["12", "15", "23", "1000000"] | 1 second | ["4 8", "6 9", "8 15", "500000 500000"] | NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | Java 11 | standard input | [
"number theory",
"math"
] | 3ea971165088fae130d866180c6c868b | The only line contains an integer n (12 ≤ n ≤ 106). | 800 | Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them. | standard output | |
PASSED | ee59df2e136d49c6f24c2fa2bffc71df | train_001.jsonl | 1278687600 | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. | 256 megabytes | import java.util.*;
import java.math.*;
public class Main{
static boolean graph[][] = new boolean[710][710];
static BigInteger dp[][] = new BigInteger[710][3];
public static void dfs(int N, int p, int x){ // System.out.println(N + " " + p + " " + x);
int i,j;
int c[] = new int[N+10];
int sz = 0;
for(i=0;i<N;i++) if(i != p && graph[x][i]){
c[sz] = i; sz++;
dfs(N,x,i);
}
for(i=0;i<3;i++) dp[x][i] = BigInteger.ONE;
if(sz == 0) return;
// System.out.println(N + " " + p + " " + x);
// type 0 and 2
BigInteger a[][] = new BigInteger[2][sz+10]; // pos, dp1
for(i=0;i<=sz;i++) a[0][i] = BigInteger.ZERO;
a[0][0] = BigInteger.ONE;
for(i=0;i<sz;i++){
int prev = i % 2, cur = (i+1) % 2;
for(j=0;j<=sz;j++) a[cur][j] = BigInteger.ZERO;
for(j=0;j<=i;j++){
BigInteger tmp = a[prev][j].multiply(dp[c[i]][0]);
if(tmp.compareTo(a[cur][j]) > 0) a[cur][j] = tmp;
tmp = a[prev][j].multiply(dp[c[i]][1]);
if(tmp.compareTo(a[cur][j+1]) > 0) a[cur][j+1] = tmp;
}
}
for(i=0;i<=sz;i++){
BigInteger tmp = a[sz%2][i].multiply(BigInteger.valueOf(i+1));
if(tmp.compareTo(dp[x][0]) > 0) dp[x][0] = tmp;
tmp = a[sz%2][i].multiply(BigInteger.valueOf(i+2));
if(tmp.compareTo(dp[x][2]) > 0) dp[x][2] = tmp;
}
// type 0
for(i=0;i<sz;i++){
BigInteger tmp = dp[c[i]][2];
for(j=0;j<sz;j++) if(j != i) tmp = tmp.multiply(dp[c[j]][0]);
if(tmp.compareTo(dp[x][0]) > 0) dp[x][0] = tmp;
}
// type 1
for(i=0;i<sz;i++) dp[x][1] = dp[x][1].multiply(dp[c[i]][0]);
// System.out.println(x + " " + dp[x][0] + " " + dp[x][1] + " " + dp[x][2]);
}
public static void main(String args[]){
int N,i,j,a,b;
Scanner scan = new Scanner(System.in);
N = scan.nextInt();
for(i=0;i<N;i++) for(j=0;j<N;j++) graph[i][j] = false;
for(i=0;i<N-1;i++){
a = scan.nextInt(); b = scan.nextInt(); a--; b--;
graph[a][b] = graph[b][a] = true;
}
dfs(N,-1,0);
System.out.println(dp[0][0].toString());
}
} | Java | ["5\n1 2\n2 3\n3 4\n4 5", "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8", "3\n1 2\n1 3"] | 2 seconds | ["6", "18", "3"] | null | Java 7 | standard input | [
"dp"
] | 8dbf81e0d2815382cec6488efd877b70 | The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. | 2,500 | Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | standard output | |
PASSED | 99070e1d2afa5b07d7d32fa0c2ddc4e6 | train_001.jsonl | 1278687600 | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.reflect.Array;
import java.math.*;
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);
int t = 1;
Task solver = new Task();
while ((t --) > 0)
solver.solve(in, out);
out.close();
}
}
class Task{
static int N = 705;
int size[] = new int[N];
Vector edge[] = new Vector[N];
BigInteger dp[][] = new BigInteger[N][N];
static int n;
BigInteger max (BigInteger a , BigInteger b) {
return a.max(b);
}
void dfs (int u , int pre) {
size[u] = 1;
dp[u][1] = BigInteger.ONE;
for (int i = 0 ; i < edge[u].size() ; i ++) {
int v = (int)edge[u].elementAt(i);
if (v == pre) continue;
dfs (v , u);
size[u] += size[v];
BigInteger tmp[] = new BigInteger[N];
BigInteger mx = BigInteger.ZERO;
for (int k = 1 ; k <= size[v] ; k ++)
mx = max (mx , dp[v][k].multiply(BigInteger.valueOf(k)));
for (int j = 1 ; j <= size[u] ; j ++) {
tmp[j] = dp[u][j].multiply(mx);
}
for (int j = size[u] ; j >= 1 ; j --) {
for (int k = 1 ; k < j && k <= size[v] ; k ++) {
if (dp[u][j - k].equals(BigInteger.ZERO) || dp[v][k].equals(BigInteger.ZERO)) continue;
dp[u][j] = max (dp[u][j] , dp[u][j - k].multiply(dp[v][k]));
}
}
for (int j = 1 ; j <= size[u] ; j ++) {
dp[u][j] = max (dp[u][j] , tmp[j]);
}
}
}
void solve (InputReader cin , PrintWriter cout) {
n = cin.nextInt();
for (int i = 1 ; i <= n ; i ++) {
edge[i] = new Vector();
}
for (int i = 1 ; i < n ; i ++) {
int u = cin.nextInt() , v = cin.nextInt();
edge[u].addElement(v);
edge[v].addElement(u);
}
for (int i = 1 ; i <= n ; i ++)
for (int j = 1 ; j <= n ; j ++)
dp[i][j] = BigInteger.ZERO;
dfs (1 , -1);
BigInteger ans = BigInteger.valueOf(n);
for (int i = 1 ; i <= n ; i ++) {
ans = max (ans , dp[1][i].multiply(BigInteger.valueOf(i)));
}
cout.println(ans);
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
} | Java | ["5\n1 2\n2 3\n3 4\n4 5", "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8", "3\n1 2\n1 3"] | 2 seconds | ["6", "18", "3"] | null | Java 7 | standard input | [
"dp"
] | 8dbf81e0d2815382cec6488efd877b70 | The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. | 2,500 | Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | standard output | |
PASSED | c881b19db9d33cce2008fa3e6d0ee4b1 | train_001.jsonl | 1278687600 | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
static int n;
static ArrayList<Integer> neigh[];
static int siz[];
static BigInteger dp[][];
static BigInteger best[];
private static void Calc(int v, int p)
{
siz[v] = 1;
dp[v][0] = BigInteger.ZERO;
dp[v][1] = BigInteger.ONE;
for (int u: neigh[v]) if (u != p) {
Calc(u, v);
for (int i = siz[v] + 1; i <= siz[v] + siz[u]; i++)
dp[v][i] = BigInteger.ZERO;
siz[v] += siz[u];
for (int i = siz[v]; i >= 1; i--) {
dp[v][i] = dp[v][i].multiply(best[u]);
for (int j = 1; j <= siz[u] && i - j >= 0; j++)
dp[v][i] = dp[v][i].max(dp[v][i - j].multiply(dp[u][j]));
}
}
best[v] = BigInteger.ZERO;
for (int i = 1; i <= siz[v]; i++)
best[v] = best[v].max(dp[v][i].multiply(BigInteger.valueOf(i)));
}
public static void main(String[] args) throws Exception {
BufferedReader fin = new BufferedReader(new InputStreamReader(System.in));
PrintWriter fout = new PrintWriter(new OutputStreamWriter(System.out));
n = Integer.parseInt(fin.readLine());
neigh = new ArrayList[n + 1];
for (int i = 1; i <= n; i++)
neigh[i] = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
int a, b; String s[] = fin.readLine().split(" ");
a = Integer.parseInt(s[0]); b = Integer.parseInt(s[1]);
neigh[a].add(b); neigh[b].add(a);
}
siz = new int[n + 1];
dp = new BigInteger[n + 1][n + 1]; best = new BigInteger[n + 1];
Calc(1, 0);
fout.println(best[1]);
fin.close(); fout.close();
}
}
| Java | ["5\n1 2\n2 3\n3 4\n4 5", "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8", "3\n1 2\n1 3"] | 2 seconds | ["6", "18", "3"] | null | Java 7 | standard input | [
"dp"
] | 8dbf81e0d2815382cec6488efd877b70 | The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. | 2,500 | Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | standard output | |
PASSED | 3323b65e04b285aa079f1ef6eef63307 | train_001.jsonl | 1278687600 | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. | 256 megabytes | import java.util.*;
import java.math.*;
import java.io.*;
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);
AC solver = new AC();
solver.solve(in, out);
out.close();
}
}
class AC {
class Node {
BigInteger h, f;
}
Comparator<Integer> cmp = new Comparator<Integer>() {
public int compare(Integer a,Integer b) {
return dp[b].f.multiply(dp[a].h).compareTo(dp[a].f.multiply(dp[b].h));
}
};
ArrayList<Integer> edge[] = new ArrayList[710];
Node dp[] = new Node[710];
int size[] = new int[710];
void dfs(int u, int f,PrintWriter out) {
dp[u].f = dp[u].h = BigInteger.ONE;
size[u] = 1;
// case 1 : u's component is only u
for(int v:edge[u]) {
if(v == f) continue;
dfs(v,u,out);
dp[u].f = dp[u].f.multiply(dp[v].h);
size[u] += size[v];
}
Collections.sort(edge[u],cmp);
// case 2 : u is with some of its children
dp[u].h = dp[u].f;
BigInteger cur;
cur = dp[u].f;
int son = 0;
for(int v:edge[u]){
if(v==f) continue;
son ++;
cur = cur.divide(dp[v].h).multiply(dp[v].f);
BigInteger tmp = cur.multiply(BigInteger.valueOf(son+1));
if(tmp.compareTo(dp[u].h) > 0) dp[u].h = tmp;
}
// case 3: u is with only one of its children and some children's children
for(int v :edge[u]) {
if(v==f) continue;
cur = dp[u].f.divide(dp[v].h).multiply(dp[v].f);
son = 0;
for(int w : edge[v]) {
if(w == u) continue;
son++;
cur = cur.divide(dp[w].h).multiply(dp[w].f);
BigInteger tmp = cur.multiply(BigInteger.valueOf(son+2));
if(tmp.compareTo(dp[u].h) > 0) dp[u].h = tmp;
}
}
}
public void solve(InputReader in, PrintWriter out) {
int n, a, b;
n = in.nextInt();
for (int i = 1; i <= n; i++){
edge[i] = new ArrayList<Integer>();
dp[i] = new Node();
}
for (int i = 1; i < n; i++) {
a = in.nextInt();
b = in.nextInt();
edge[a].add(b);
edge[b].add(a);
}
dfs(1,0,out);
out.println(dp[1].h);
}
}
class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
class ArrayUtils {
public static void fill(long[][] array, long value) {
for (long[] row : array)
Arrays.fill(row, value);
}
public static void fill(long[][][] array, long value) {
for (long[][] row : array)
fill(row, value);
}
public static void fill(long[][][][] array, long value) {
for (long[][][] row : array)
fill(row, value);
}
} | Java | ["5\n1 2\n2 3\n3 4\n4 5", "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8", "3\n1 2\n1 3"] | 2 seconds | ["6", "18", "3"] | null | Java 7 | standard input | [
"dp"
] | 8dbf81e0d2815382cec6488efd877b70 | The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. | 2,500 | Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | standard output | |
PASSED | 2e8a9e4032ece8ff5089c25f0a652611 | train_001.jsonl | 1278687600 | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. | 256 megabytes | import java.util.*;
import java.math.*;
import java.io.*;
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);
AC solver = new AC();
solver.solve(in, out);
out.close();
}
}
class AC {
class Node {
BigInteger h, f;
}
Comparator<Integer> cmp = new Comparator<Integer>() {
public int compare(Integer a,Integer b) {
return dp[b].f.multiply(dp[a].h).compareTo(dp[a].f.multiply(dp[b].h));
}
};
ArrayList<Integer> edge[] = new ArrayList[710];
Node dp[] = new Node[710];
int size[] = new int[710];
void dfs(int u, int f,PrintWriter out) {
dp[u].f = dp[u].h = BigInteger.ONE;
size[u] = 1;
// case 1 : u's component is only u
for(int v:edge[u]) {
if(v == f) continue;
dfs(v,u,out);
dp[u].f = dp[u].f.multiply(dp[v].h);
size[u] += size[v];
}
/* for(int i = 0 ; i < edge[u].size(); i++) {
int to = edge[u].get(i);
if(to == f) continue;
BigInteger a = dp[to].f;
BigInteger b = dp[to].h;
out.println(a+" "+b);
}*/
Collections.sort(edge[u],cmp);
// case 2 : u is with some of its children
dp[u].h = dp[u].f;
BigInteger cur;
cur = dp[u].f;
int son = 0;
for(int v:edge[u]){
if(v==f) continue;
son ++;
cur = cur.divide(dp[v].h).multiply(dp[v].f);
BigInteger tmp = cur.multiply(BigInteger.valueOf(son+1));
if(tmp.compareTo(dp[u].h) > 0) dp[u].h = tmp;
}
// case 3: u is with only one of its children and some children's children
for(int v :edge[u]) {
if(v==f) continue;
cur = dp[u].f.divide(dp[v].h).multiply(dp[v].f);
son = 0;
for(int w : edge[v]) {
if(w == u) continue;
son++;
cur = cur.divide(dp[w].h).multiply(dp[w].f);
BigInteger tmp = cur.multiply(BigInteger.valueOf(son+2));
if(tmp.compareTo(dp[u].h) > 0) dp[u].h = tmp;
}
}
}
public void solve(InputReader in, PrintWriter out) {
int n, a, b;
n = in.nextInt();
for (int i = 1; i <= n; i++){
edge[i] = new ArrayList<Integer>();
dp[i] = new Node();
}
for (int i = 1; i < n; i++) {
a = in.nextInt();
b = in.nextInt();
edge[a].add(b);
edge[b].add(a);
}
dfs(1,0,out);
out.println(dp[1].h);
}
}
class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
class ArrayUtils {
public static void fill(long[][] array, long value) {
for (long[] row : array)
Arrays.fill(row, value);
}
public static void fill(long[][][] array, long value) {
for (long[][] row : array)
fill(row, value);
}
public static void fill(long[][][][] array, long value) {
for (long[][][] row : array)
fill(row, value);
}
} | Java | ["5\n1 2\n2 3\n3 4\n4 5", "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8", "3\n1 2\n1 3"] | 2 seconds | ["6", "18", "3"] | null | Java 7 | standard input | [
"dp"
] | 8dbf81e0d2815382cec6488efd877b70 | The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. | 2,500 | Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | standard output | |
PASSED | bfda90351e4eea649155b66fd817c10d | train_001.jsonl | 1278687600 | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Cf23e {
static int n;
static BigInteger[][] dp = new BigInteger[710][710];
static int[][] adj = new int[710][710];
static int[] dg = new int[710];
static int[] sm = new int[710];
public static void dfs(int u, int p) {
int v;
sm[u] = 1;
dp[u][1] = BigInteger.ONE;
BigInteger[] tdp = new BigInteger[710];
for (int i = 0; i < dg[u]; i++) {
v = adj[u][i];
if (v == p) continue;
dfs(v, u);
Arrays.fill(tdp, BigInteger.ZERO);
for (int j = 1; j <= sm[u]; j++)
for (int k = 0; k <= sm[v]; k++) {
BigInteger tmp = dp[u][j].multiply(dp[v][k]);
if (tmp.compareTo(tdp[j+k]) > 0) tdp[j+k] = tmp;
}
sm[u] += sm[v];
for (int l = 1; l <= sm[u]; l++) dp[u][l] = tdp[l];
}
for (int i = 1; i <= sm[u]; i++) {
BigInteger tmp = dp[u][i].multiply(BigInteger.valueOf(i));
if (tmp.compareTo(dp[u][0]) > 0) dp[u][0] = tmp;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 0; i < n; i++) Arrays.fill(dp[i], BigInteger.ZERO);
Arrays.fill(dg, 0);
int a, b;
for (int i = 0; i < n-1; i++) {
a = sc.nextInt() - 1; b = sc.nextInt() - 1;
adj[a][dg[a]++] = b; adj[b][dg[b]++] = a;
}
dfs(0,-1);
System.out.println(dp[0][0]);
}
}
| Java | ["5\n1 2\n2 3\n3 4\n4 5", "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8", "3\n1 2\n1 3"] | 2 seconds | ["6", "18", "3"] | null | Java 7 | standard input | [
"dp"
] | 8dbf81e0d2815382cec6488efd877b70 | The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. | 2,500 | Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | standard output | |
PASSED | 61581e244e9072bbd450350065a65b69 | train_001.jsonl | 1278687600 | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Cf23e {
static int n;
static BigInteger[][] dp = new BigInteger[710][710];
static int[][] adj = new int[710][710];
static int[] dg = new int[710];
static int[] sm = new int[710];
public static void dfs(int u, int p) {
int v;
sm[u] = 1;
dp[u][1] = BigInteger.ONE;
for (int i = 0; i < dg[u]; i++) {
v = adj[u][i];
if (v == p) continue;
dfs(v, u);
for (int j = sm[u]; j > 0; j--)
for (int k = sm[v]; k >= 0; k--) {
BigInteger tmp = dp[u][j].multiply(dp[v][k]);
if (tmp.compareTo(dp[u][j+k]) > 0) dp[u][j+k] = tmp;
}
sm[u] += sm[v];
}
for (int i = 1; i <= sm[u]; i++) {
BigInteger tmp = dp[u][i].multiply(BigInteger.valueOf(i));
if (tmp.compareTo(dp[u][0]) > 0) dp[u][0] = tmp;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 0; i < n; i++) Arrays.fill(dp[i], BigInteger.ZERO);
Arrays.fill(dg, 0);
int a, b;
for (int i = 0; i < n-1; i++) {
a = sc.nextInt() - 1; b = sc.nextInt() - 1;
adj[a][dg[a]++] = b; adj[b][dg[b]++] = a;
}
dfs(0,-1);
System.out.println(dp[0][0]);
}
}
| Java | ["5\n1 2\n2 3\n3 4\n4 5", "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8", "3\n1 2\n1 3"] | 2 seconds | ["6", "18", "3"] | null | Java 7 | standard input | [
"dp"
] | 8dbf81e0d2815382cec6488efd877b70 | The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. | 2,500 | Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | standard output | |
PASSED | 8deebcfb539f189166d680315f54c1eb | train_001.jsonl | 1278687600 | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Cf23e {
static int n;
static BigInteger[][] dp = new BigInteger[710][710];
static int[][] adj = new int[710][710];
static int[] dg = new int[710];
static int[] sm = new int[710];
public static void dfs(int u, int p) {
int v;
sm[u] = 1;
dp[u][1] = BigInteger.ONE;
for (int i = 0; i < dg[u]; i++) {
v = adj[u][i];
if (v == p) continue;
dfs(v, u);
BigInteger[] tdp = new BigInteger[sm[u] + sm[v] + 1];
Arrays.fill(tdp, BigInteger.ZERO);
for (int j = 1; j <= sm[u]; j++)
for (int k = 0; k <= sm[v]; k++) {
BigInteger tmp = dp[u][j].multiply(dp[v][k]);
if (tmp.compareTo(tdp[j+k]) > 0) tdp[j+k] = tmp;
}
sm[u] += sm[v];
for (int l = 1; l <= sm[u]; l++) dp[u][l] = tdp[l];
}
for (int i = 1; i <= sm[u]; i++) {
BigInteger tmp = dp[u][i].multiply(BigInteger.valueOf(i));
if (tmp.compareTo(dp[u][0]) > 0) dp[u][0] = tmp;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 0; i < n; i++) Arrays.fill(dp[i], BigInteger.ZERO);
Arrays.fill(dg, 0);
int a, b;
for (int i = 0; i < n-1; i++) {
a = sc.nextInt() - 1; b = sc.nextInt() - 1;
adj[a][dg[a]++] = b; adj[b][dg[b]++] = a;
}
dfs(0,-1);
System.out.println(dp[0][0]);
}
}
| Java | ["5\n1 2\n2 3\n3 4\n4 5", "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8", "3\n1 2\n1 3"] | 2 seconds | ["6", "18", "3"] | null | Java 7 | standard input | [
"dp"
] | 8dbf81e0d2815382cec6488efd877b70 | The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. | 2,500 | Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | standard output | |
PASSED | ba7ff32efb1bedcf340b7d645f6c05f0 | train_001.jsonl | 1278687600 | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Main{
static int N = 705;
static int sum[] = new int[N];
static int sz[][] = new int[N][N];
static BigInteger dp[][] = new BigInteger[N][N];
static int n;
static void dfs(int u, int fa) {
sum[u] = 1;
for (int i = 1; i <= n; ++i) dp[u][i] = BigInteger.valueOf(1);
for (int i = 1, v; i <= sz[u][0]; ++i){
v = sz[u][i];
if (v == fa) continue;
dfs(v, u);
sum[u] += sum[v];
for (int j = sum[u] - sum[v]; j >= 0; --j)
for (int k = sum[v]; k >= 0; --k)
dp[u][j + k] = dp[u][j + k].max(dp[u][j].multiply(dp[v][k]));
}
for (int i = 1; i <= sum[u]; ++i) dp[u][0] = dp[u][0].max(dp[u][i].multiply(BigInteger.valueOf(i)));
}
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
n = cin.nextInt();
for (int i = 0; i <= n; ++i) {
sz[i][0] = 0;
for (int j = 0; j <= n; ++j) dp[i][j] = BigInteger.ZERO;
}
for (int i = 1, a, b; i < n; ++i){
a = cin.nextInt();b = cin.nextInt();
sz[a][++sz[a][0]] = b;
sz[b][++sz[b][0]] = a;
}
dfs(1, -1);
System.out.print(dp[1][0]);
}
}
| Java | ["5\n1 2\n2 3\n3 4\n4 5", "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8", "3\n1 2\n1 3"] | 2 seconds | ["6", "18", "3"] | null | Java 7 | standard input | [
"dp"
] | 8dbf81e0d2815382cec6488efd877b70 | The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. | 2,500 | Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | standard output | |
PASSED | d058ab39aac7c823b09d9e8af889b6af | train_001.jsonl | 1278687600 | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. | 256 megabytes | import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static final int MAX = 710;
static final int MAX_NODE_IN_COMPONENT = 710;
static int n;
static int graph[][] = new int[MAX][MAX];
static int size[] = new int[MAX];
static int child_count[] = new int[MAX];
static BigInteger f[][] = new BigInteger[MAX][MAX];
static void dfs(int x, int fa) {
child_count[x] = 1;
for (int i = 0; i <= n; ++i) {
f[x][i] = BigInteger.ONE;
}
for (int i = 0; i < size[x]; ++i) {
int ch = graph[x][i];
if (ch == fa) {
continue;
}
dfs(ch, x);
child_count[x] += child_count[ch];
int lim_a = Math.min(child_count[x] - child_count[ch], MAX_NODE_IN_COMPONENT);
int lim_b = Math.min(child_count[ch], MAX_NODE_IN_COMPONENT);
for (int a = lim_a; a >= 0; --a) {
for (int b = lim_b; b >= 0; --b) {
f[x][a + b] = f[x][a + b]
.max(f[x][a].multiply(f[ch][b]));
}
}
}
for (int i = 1; i <= Math.min(child_count[x], MAX_NODE_IN_COMPONENT); ++i) {
f[x][0] = f[x][0].max(f[x][i].multiply(BigInteger.valueOf(i)));
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
for (int i = 0; i < MAX; ++i) {
for (int j = 0; j < MAX; ++j) {
f[i][j] = BigInteger.ZERO;
graph[i][j] = 0;
}
size[i] = 0;
}
n = in.nextInt();
int u, v;
for (int i = 0; i <= n; ++i) {
size[i] = 0;
}
for (int i = 1; i < n; ++i) {
u = in.nextInt();
v = in.nextInt();
graph[u][size[u]++] = v;
graph[v][size[v]++] = u;
}
dfs(1, -1);
System.out.println(f[1][0]);
in.close();
}
}
| Java | ["5\n1 2\n2 3\n3 4\n4 5", "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8", "3\n1 2\n1 3"] | 2 seconds | ["6", "18", "3"] | null | Java 7 | standard input | [
"dp"
] | 8dbf81e0d2815382cec6488efd877b70 | The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. | 2,500 | Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | standard output | |
PASSED | a91ebd9d7a0237fab516c68fed01fde8 | train_001.jsonl | 1278687600 | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import static java.util.Arrays.*;
import static java.lang.Math.*;
public class Main {
static class Node extends ArrayList<Node> {
int sum;
BigInteger []ans;
void dfs(Node father) {
sum = 1;
for (Node next : this) if (next != father) {
next.dfs(this);
sum += next.sum;
}
ans = new BigInteger[sum + 1];
fill(ans, BigInteger.ZERO);
ans[1] = BigInteger.ONE;
int cur = 1;
for (Node next : this) if (next != father) {
for (int i = cur; i >= 1; --i)
for (int j = next.sum; j >= 0; --j)
ans[i + j] = ans[i + j].max(ans[i].multiply(next.ans[j]));
cur += next.sum;
}
for (int i = 1; i <= sum; ++i)
ans[0] = ans[0].max(ans[i].multiply(BigInteger.valueOf(i)));
}
}
public static void main(String []args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
Node []v = new Node[n];
for (int i = 0; i < n; ++i)
v[i] = new Node();
for (int i = 1; i < n; ++i) {
int x = cin.nextInt(), y = cin.nextInt();
--x; --y;
v[x].add(v[y]);
v[y].add(v[x]);
}
v[0].dfs(null);
System.out.println(v[0].ans[0]);
}
}
| Java | ["5\n1 2\n2 3\n3 4\n4 5", "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8", "3\n1 2\n1 3"] | 2 seconds | ["6", "18", "3"] | null | Java 7 | standard input | [
"dp"
] | 8dbf81e0d2815382cec6488efd877b70 | The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. | 2,500 | Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | standard output | |
PASSED | 5fa047bf2762214956de1c45b279a5af | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class CodeForces
{
public static void main(String[] args)throws IOException
{
Scanner sc=new Scanner(System.in);
//Scanner sc=new Scanner(new File("ip.txt"));
int tc,n,l1,l2,min,i;
String s;
tc=sc.nextInt();
while(tc-->0)
{
n=sc.nextInt();
s=sc.next();
Pair ps;
HashMap<Pair,Integer> set=new HashMap<Pair,Integer>();
set.put(ps=new Pair(0,0),0);
min=Integer.MAX_VALUE;
l1=0;l2=-1;
for(i=0;i<n;i++)
{
if(s.charAt(i)=='L')
ps=new Pair(ps.x-1,ps.y);
else if(s.charAt(i)=='R')
ps=new Pair(ps.x+1,ps.y);
else if(s.charAt(i)=='U')
ps=new Pair(ps.x,ps.y+1);
else
ps=new Pair(ps.x,ps.y-1);
if(set.containsKey(ps))
{
int k=set.get(ps);
if((i+1)-k<min)
{
min=(i+1)-k;
l1=k+1;
l2=i+1;
}
}
set.put(ps,i+1);
}
System.out.println(((l2-l1)>-1)?l1+" "+l2:"-1");
//System.out.println(set);
}
}
}
class Pair
{
int x,y;
Pair(int a,int b)
{
x=a;
y=b;
}
@Override
public String toString()
{
return "x: "+x+"& y: "+y;
}
@Override
public int hashCode()
{
int hash = 17;
hash = 31 * hash + this.x;
hash = 31 * hash + this.y;
return hash;
}
@Override
public boolean equals(Object a)
{
if(this==null&&a==null)
return true;
if (!(a instanceof Pair))
return false;
Pair oth=(Pair)a;
return (this.x==oth.x&&this.y==oth.y);
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | aa773886913a22f9999c8156d416d231 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".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.StringTokenizer;
import java.util.HashMap;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author seth bhavy
*/
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();
}
static class TaskC {
public void solve(int testNumber, InputReader sc, PrintWriter out) {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int x = 0, y = 0;
int min = Integer.MAX_VALUE;
int left = 0, right = -1;
String s = sc.nextLine();
HashMap<Pair, Integer> map = new HashMap<>();
map.put(new Pair(0, 0), -1);
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'R') x++;
if (s.charAt(i) == 'L') x--;
if (s.charAt(i) == 'U') y++;
if (s.charAt(i) == 'D') y--;
if (map.containsKey(new Pair(x, y))) {
int len = i - map.get(new Pair(x, y)) + 1;
// out.println("len "+len);
if (len < min) {
min = len;
left = map.get(new Pair(x, y));
right = i;
}
}
map.put(new Pair(x, y), i);
}
if (min == Integer.MAX_VALUE)
out.println(-1);
else out.println(left + 2 + " " + (right + 1));
}
}
}
static class Pair {
private final int x;
private final int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair key = (Pair) o;
return x == key.x && y == key.y;
}
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream inputStream) {
br = new BufferedReader(new
InputStreamReader(inputStream));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, 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.