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
41cb664b60faea239e8c5502e24035bc
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Scanner; public class A167 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n= sc.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int s = sc.nextInt(); int t= sc.nextInt(); int a=0,b=0; int x= (s<=t?s-1:t-1); int y=(s>t?s-1:t-1); for (int i =x; i < y; i++) { a += arr[i]; } for (int i = 0; i < n; i++) { if(s>t){ if(i <t-1 || i>s-2) b += arr[i]; } else{ if(i <s-1 || i>t-2) b += arr[i]; } } System.out.println(a>=b?b:a); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
26376d4a876fe3e28bdeb62ea8806524
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
//package javaapplication1; import java.io.*; import java.util.*; public class JavaApplication1 implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); final boolean OJ = System.getProperty("ONLINE_JUDGE") != null; void init() throws FileNotFoundException { if (OJ) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("Input.txt")); out = new PrintWriter("Output.txt"); } } @Override public void run() { try { init(); solve(); out.close(); } catch(Exception e) { e.printStackTrace(); System.exit(-1); } } public static void main(String[] args) { new Thread(null, new JavaApplication1(), "", (1L << 20) * 256).start(); } String readString() { while(!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine()); } catch(Exception e) { e.printStackTrace(); System.exit(-1); } } return tok.nextToken(); } int readInt() { return Integer.parseInt(readString()); } //////////////////////////////////////////////////////////////////////////////// class Pair implements Comparable { public int a, b; public Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Object x) { int cx = ((Pair)x).a; int cy = ((Pair)x).b; if (a > cx) { return 1; } else if (a < cx) { return -1; } else { if (b > cy) { return 1; } else if (b < cy) { return -1; } else { return 0; } } } } void solve() { int n = readInt(); int[] d = new int[n + 1]; int sum = 0; for(int i = 1; i <= n; i++) { d[i] = readInt(); sum += d[i]; } int s = readInt(); int t = readInt(); if (s > t) { int tmp = s; s = t; t = tmp; } int ans = 0; for(int i = s; i < t; i++) { ans += d[i]; } out.println(Math.min(ans, sum - ans)); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
725ab64219606d91ae118ae7351d3afd
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class r2 { public static void main(String[] args) { Scanner cin=new Scanner(System.in); while(cin.hasNext()){ int n=cin.nextInt(); int[] num=new int[n+5]; int sum=0; for(int i=1;i<=n;i++){ sum+=num[i]=cin.nextInt(); } int s=cin.nextInt(); int t=cin.nextInt(); int ans=0; if(s>t) { int tmp=s; s=t; t=tmp; } for(int i=s;i<t;i++) { ans+=num[i]; } if(ans>sum-ans) ans=sum-ans; System.out.println(ans); } } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
555a0b7dd25c4725a78eba796cc9b20f
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] circle = new int[n]; int idx = 0; while (n > 0) { n--; circle[idx] = in.nextInt(); idx++; } int start = in.nextInt(), end = in.nextInt(); int direct = 0, reverse = 0; idx = (start - 1) % circle.length; while (idx != end - 1) { direct += circle[idx]; idx = (idx+1) % circle.length; } idx = (end - 1) % circle.length; while (idx != start - 1) { reverse += circle[idx]; idx = (idx+1) % circle.length; } System.out.println(Math.min(direct, reverse)); /*System.out.println(direct); System.out.println(reverse);*/ } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
3a7ac0c43e3df8eada649b6ad4f56da7
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
/* * @Author: steve * @Date: 2015-10-08 15:04:28 * @Last Modified by: steve * @Last Modified time: 2015-10-08 15:12:35 */ import java.io.*; import java.util.*; public class CircleLine { public static void main(String[] args) throws Exception{ BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(entrada.readLine()); String[] cads = entrada.readLine().split(" "); String[] pos = entrada.readLine().split(" "); int a1 = Integer.parseInt(pos[0])-1,b1=Integer.parseInt(pos[1])-1; int a=Math.min(a1,b1), b=Math.max(a1,b1); int d=0,d1=0; boolean esta=false; for(int i=a;i<n+a;i++){ if(i==b) esta=true; if(esta) d1+=Integer.parseInt(cads[i%n]); else d+=Integer.parseInt(cads[i%n]); } System.out.println(Math.min(d1,d)); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
63d3f436931e3d085b18b2575574365c
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ //package prob2; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * * @author Rania */ public class Prob2 { /** * @param args the command line arguments */ public static BufferedReader in; public static void main(String[] args) throws IOException { // TODO code application logic here in = new BufferedReader(new InputStreamReader(System.in)); String temp = in.readLine(); temp = temp.trim(); int n = Integer.parseInt(temp); if (n>100 || n < 3){ System.out.println("n should be range from 3 to 100"); System.exit(-1); } String distances = in.readLine(); String[] values = new String[n]; int[] intVals = new int[n]; int j=0; for (String p : distances.split("\\s")) { values [j] = p ; //System.out.println("ur vals "+values[j]); intVals[j]= Integer.parseInt(values[j]); j++; } String stations = in.readLine(); String[] checkPoints = new String[2]; int[] startEnd = new int[2]; j=0; for (String p : stations.split("\\s")) { checkPoints [j] = p ; //System.out.println("ur vals "+values[j]); startEnd[j]= Integer.parseInt(checkPoints[j]); j++; } if(startEnd[0]>n || startEnd[1] >n){ System.out.println("Start and Distination should be less than n"); //System.exit(-2); } int s = startEnd[0]; int t = startEnd[1]; int cw =0; int acw =0; if(s>t){ int swap = s; s = t; t = swap; } if(s == t){ System.out.println("0"); //System.exit(-1); } else { for(int i =s; i<t; i++){ cw+=intVals[i-1]; } //System.out.println(cw); int i=(s>1)?s-1:n; for(;;i--) { acw+=intVals[i-1]; if(i==1) i=n+1; if(i==t) break; } //System.out.println(acw); if(cw < acw) System.out.println(cw); else System.out.println(acw); }} }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
fb135f10df65caa2fa0a396060f3da59
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.*; public class Main{ public static void main(String[]args){ Scanner cin=new Scanner(System.in); for(int n,m,S[]=new int[101];cin.hasNextInt();System.out.println(Math.min(m,S[n]-m))){ for(n=cin.nextInt(),m=0;n>m++;S[m]=cin.nextInt()+S[m-1]); m=cin.nextInt()-1;m=Math.abs(S[cin.nextInt()-1]-S[m]); } } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
ca03430a35c13635b883e525bf065311
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.*; public class Main{ public static void main(String[]args){ Scanner cin=new Scanner(System.in); int n=cin.nextInt(),m=0,S[]=new int[1+n]; for(;n>m++;S[m]=cin.nextInt()+S[m-1]); m=Math.abs(S[cin.nextInt()-1]-S[cin.nextInt()-1]); System.out.print(Math.min(m,S[n]-m)); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
3855678f5a25665a0f1a4cad19a0ee09
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class AA { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(rd.readLine()); int[] num = new int[n]; StringTokenizer t = new StringTokenizer(rd.readLine(), " "); for (int i = 0; i < n; i++) { num[i] = Integer.parseInt(t.nextToken()); } t = new StringTokenizer(rd.readLine(), " "); int f = Integer.parseInt(t.nextToken()) - 1; int to = Integer.parseInt(t.nextToken()) - 1; // int min = Math.min(f, to); // int max = Math.max(f, to); // f = min; // to = max; int fw = 0; int bw = 0; // if (f == to) { // System.out.println(0); // return; // } for (int i = (f), j = (i + 1) % n; i != to; i = j, j = (j + 1) % n) { fw += num[i]; } // fw += num[(f) % n]; for (int i = (f), j = (i - 1 + n) % n; i != to; i = j, j = (j - 1 + n) % n) { bw += num[j]; } // bw += num[(to) % n]; System.out.println(Math.min(fw, bw)); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
ad12c16386b2a791774667ec4ccd705b
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Scanner; public class CircleLine278A { public static void main(String[] args) { @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); int n = sc.nextInt(), totalDistance = 0; int[] arr = new int[n]; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); totalDistance += arr[i]; } int x = sc.nextInt(), y = sc.nextInt(); System.out.println(findDistance(x, y, arr, totalDistance)); } private static int findDistance(int x, int y, int[] arr, int totalDistance) { if(x == y) return 0; if(x > y){ x = x + y; y = x - y; x = x - y; } x--; y--; int path1 = pathForward(x, y, arr); return Math.min(path1, totalDistance - path1); } private static int pathForward(int x, int y, int[] arr) { int distance = 0; for(int i=x;i<y;i++) distance += arr[i]; //System.out.println(distance); return distance; } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
9760842c22b1ec498e92c17b2b5a613c
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.lang.*; import java.io.*; import java.util.*; public class Line { public static void main(String[] args) throws java.lang.Exception { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(in, out); out.close(); } } class TaskA { public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); int s, t; int tot = 0, tmp; int ans; int[] a = new int[n+1]; for (int i=1; i<=n; ++i) { a[i] = in.nextInt(); tot += a[i]; } s = in.nextInt(); t = in.nextInt(); // make s <= t if (s > t) { tmp = s; s = t; t = tmp; } // if (s==1 && t==n) { // ans = Math.min(a[n], tot-a[n]); // } else { tmp = 0; for (int i=s; i<t; ++i) tmp += a[i]; ans = Math.min(tmp, tot-tmp); // } out.println(ans); } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer==null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
3c5e818b7d5116940a2633bf8d36ba87
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
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.*; import java.io.InputStream; /** * Created by G.Gekko. */ public class Solution { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } } class Task { public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); int s = in.nextInt(); int t = in.nextInt(); s--; t--; if(s > t) { int x = t; t = s; s = x; } int cnt = 0; for (int i = s; i < t; i++) { cnt += a[i]; } int cntTwo = 0; for (int i = 0; i < s;i++) { cntTwo += a[i]; } for (int i = t; i < n; i++) { cntTwo += a[i]; } if (cnt > cntTwo) out.print(cntTwo); else out.print(cnt); } } /* * Reading */ class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader (InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
decce964c6bdcca0d0be096d6ebeb702
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.io.*; import java.util.*; public class CF170A { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer stringTokenizer; String next() throws IOException{ while(stringTokenizer == null || !stringTokenizer.hasMoreTokens()){ stringTokenizer = new StringTokenizer(br.readLine()); } return stringTokenizer.nextToken(); } CF170A() throws IOException{ } int nextInt() throws IOException{ return Integer.parseInt(next()); } long nextLong() throws IOException{ return Long.parseLong(next()); } void solve() throws IOException{ //EnterCoder here... int n = nextInt(); int[] d = new int[n]; for(int i=0;i<n;i++){ d[i] = nextInt(); } /*CircularArrayList<Integer> list = new CircularArrayList<Integer>(); for(int i=0;i<n;i++){ list.add(nextInt()); }*/ int s = nextInt(); int t = nextInt(); if(s>t){ s = s+t; t = s-t; s = s-t; } int sumA=0,sumB =0; for(int i=s-1;i<t-1;i++){ sumA += d[i]; if(i==t-2){ break; } } for(int i=s-2;;i--){ if(i<0){ i = d.length-1; } sumB += d[i]; if(i==(t-1)){ break; } } System.out.println(Math.min(sumA, sumB)); } public void swap(int a, int b){ int temp = a; a = b; b = temp; } public static void main(String[] args) throws IOException{ new CF170A().solve(); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
171f354eb6b4a7f38813e0f81984f155
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Scanner; public class ShortestDistance { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[]d=new int[n+1]; for(int i=1;i<n+1;i++) { d[i]=sc.nextInt(); } int s=sc.nextInt(); int t=sc.nextInt(); int min=Math.min(s, t); int max=Math.max(s,t); if(Math.min(s, t)==1) { int sum0=0; for(int i=1;i<max;i++) { sum0+=d[i]; } int sum4=0; for(int i=max;i<=n;i++) { sum4+=d[i]; } System.out.println(Math.min(sum0, sum4)); } else { int sum1=0; for(int i=min;i<max;i++) { sum1+=d[i]; } int sum2=0; for(int i=max;i<=n;i++) { sum2+=d[i]; } int sum3=0; for(int i=1;i<min;i++) { sum3+=d[i]; } System.out.println(Math.min(sum1, sum2+sum3)); } } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
d35beed1b287a6dee90a904664b9c250
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Scanner; public class ShortestDistance { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[]d=new int[n+1]; for(int i=1;i<n+1;i++) { d[i]=sc.nextInt(); } int s=sc.nextInt(); int t=sc.nextInt(); if(s==t) { System.out.println(0); System.exit(0); } int min=Math.min(s, t); int max=Math.max(s,t); if(Math.min(s, t)==1) { int sum0=0; for(int i=1;i<max;i++) { sum0+=d[i]; } int sum4=0; for(int i=max;i<=n;i++) { sum4+=d[i]; } System.out.println(Math.min(sum0, sum4)); } else { int sum1=0; for(int i=min;i<max;i++) { sum1+=d[i]; } int sum2=0; for(int i=max;i<=n;i++) { sum2+=d[i]; } int sum3=0; for(int i=1;i<min;i++) { sum3+=d[i]; } System.out.println(Math.min(sum1, sum2+sum3)); } } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
e462fb67280ca8ea5ca996013fde9dcc
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Scanner; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int s[] = new int[n]; int sum = 0; for (int i = 0; i < n; i++) { s[i] = sc.nextInt(); sum += s[i]; } int aa = sc.nextInt(); int bb = sc.nextInt(); int a = Math.min(aa, bb); int b = Math.max(aa, bb); if(a == b){ System.out.println("0"); return; } int d = 0; for (int i = a - 1; i < b - 1; i++) { d += s[i]; } System.out.println(Math.min(d, sum - d)); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
1758552294bdda2e92b1bfbc4eef7618
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.*; public class CircleLine { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] distances = new int[n]; int total = 0; for (int i = 0; i < n; i++) { distances[i] = in.nextInt(); total += distances[i]; } int s, t; s = in.nextInt(); t = in.nextInt(); int firsttrip, secondtrip; firsttrip = 0; secondtrip = 0; int ans = 0; if (s == t) { firsttrip = 0; secondtrip = 100; } else if (s < t) { for (int i = s - 1; i < t-1; i++) firsttrip += distances[i]; secondtrip = total - firsttrip; } else { for (int i = t - 1; i < s-1; i++) firsttrip += distances[i]; secondtrip = total - firsttrip; } if (firsttrip < secondtrip) System.out.println(firsttrip); else System.out.println(secondtrip); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
e056d9157c75b6873274ed6069797ec0
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class CF0278A { public static void main(String[] helloWorld) throws IOException { BufferedReader sIn = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(sIn.readLine()); int[] pos = new int[n + 1]; int total = 0; StringTokenizer sT = new StringTokenizer(sIn.readLine()); for (int i = 2; i <= n; i ++) { total += Integer.parseInt(sT.nextToken()); pos[i] = total; } total += Integer.parseInt(sT.nextToken()); sT = new StringTokenizer(sIn.readLine()); int s = Integer.parseInt(sT.nextToken()); int t = Integer.parseInt(sT.nextToken()); int x = Math.abs(pos[t] - pos[s]); System.out.println(Math.min(x, total - x)); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
4f8b790bbef8eff8e73c85bcfc6dd84c
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Scanner; /** * * @author Izhari Ishak Aksa */ public class CircleLine { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] d = new int[n]; for (int i = 0; i < n; i++) { d[i] = sc.nextInt(); } int s = sc.nextInt() - 1; int t = sc.nextInt() - 1; if (s > t) { s ^= t; t ^= s; s ^= t; } int a = 0; int b = 0; for (int i = s; i < t; i++) { a += d[i]; } for (int i = t; i < n; i++) { b += d[i]; } for (int i = 0; i < s; i++) { b += d[i]; } System.out.println(Math.min(a, b)); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
db68ed0d05c73911865e4d158368c4e1
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Scanner; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int d[] = new int[n]; for (int i = 0; i < n; i++) { d[i] = sc.nextInt(); } int s = sc.nextInt() - 1; int e = sc.nextInt() - 1; int d1 = 0; int p = s; while (p != e) { d1 += d[p]; p++; if (p == n) { p = 0; } } int d2 = 0; p = s; while (p != e) { p--; if (p == -1) { p = n - 1; } d2 += d[p]; } System.out.println(Math.min(d1, d2)); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
c25339062e5a9224b2786ee539a4322e
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner kb = new Scanner(System.in); int n=kb.nextInt(); int[] dis=new int[n]; for(int i=0;i<n;i++){ dis[i]=kb.nextInt(); } int s=kb.nextInt(); int t=kb.nextInt(); if(t<s){ int temp=s; s=t; t=temp; } int len1=0; int len2=0; for(int i=s-1;i<t-1;i++){ len1+=dis[i]; } for(int i=t-1;i<n;i++){ len2+=dis[i]; } for(int i=0;i<s-1;i++){ len2+=dis[i]; } if(len1<=len2){ System.out.println(len1); }else{ System.out.println(len2); } } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
049f75415aff4931c74e5ae1f3d781be
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.*; import java.io.IOException; public class metro { public static void main (String[] args) throws IOException { Scanner in = new Scanner(System.in); int summ1=0, summ2=0; int stan = in.nextInt(); int array[] =new int [stan]; for (int i = 0; i < stan; i++) { int a = in.nextInt(); array[i]=a; } int k = in.nextInt(); int n = in.nextInt(); if(n<k) { int tmp=k; k=n; n=tmp; } for (int i=k-1; i<n-1; i++) { summ1+=array[i]; } for (int i =0; i <k-1; i++) { summ2+=array[i]; } for (int i = n-1; i <stan; i++) { summ2+=array[i]; } if(summ2<=summ1) System.out.println(summ2); else System.out.println(summ1); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
48a7da1aac233d396c0d638385de6285
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import org.omg.DynamicAny._DynArrayStub; import java.lang.reflect.Array; import java.util.*; /** * Created by root on 7/14/14. */ public class test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] distances = new int[n]; for (int i = 0; i < distances.length; i++) { distances[i] = sc.nextInt(); } int point_one = sc.nextInt(); int point_two = sc.nextInt(); int temp = 0; if(point_one > point_two){ temp = point_one; point_one = point_two; point_two = temp; } int first_total = 0; int second_total = 0; for (int i = point_one - 1; i < point_two - 1; i++) { first_total += distances[i]; } for (int i = 0; i < point_one - 1; i++) { second_total += distances[i]; } for (int i = distances.length-1; i >= point_two-1; i--) { second_total += distances[i]; } if (first_total < second_total){ System.out.println(first_total); }else{ System.out.println(second_total); } } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
7b5c1c53d6b46cc707292592a7013e81
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Scanner; /** * The circle line of the Berland subway has n stations. We know the distances * between all pairs of neighboring stations: * * d1 is the distance between the 1-st and the 2-nd station; d2 is the distance * between the 2-nd and the 3-rd station; ... * * dn - 1 is the distance between the n - 1-th and the n-th station; dn is the * distance between the n-th and the 1-st station. The trains go along the * circle line in both directions. Find the shortest distance between stations * with numbers s and t. * * Input * The first line contains integer n (3 ≤ n ≤ 100) — the number of * stations on the circle line. The second line contains n integers * d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring * stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the * numbers of stations, between which you need to find the shortest distance. * These numbers can be the same. * * The numbers in the lines are separated by single spaces. * * Output * Print a single number — the length of the shortest path between * stations number s and t. * * Sample test(s) * input * 4 * 2 3 4 9 * 1 3 * output * 5 * input * 4 * 5 8 2 100 * 4 1 * output * 15 * input * 3 * 1 1 1 * 3 1 * output * 1 * input * 3 * 31 41 59 * 1 1 * output * 0 * Note * In the first * sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 * equals 13. * * In the second sample the length of path 4 → 1 is 100, the length of path * 4 → 3 → 2 → 1 is 15. * * In the third sample the length of path 3 → 1 is 1, the length of path * 3 → 2 → 1 is 2. * * In the fourth sample the numbers of stations are the same, so the shortest * distance equals 0. * * @Title: A278CircleLine.java * @author lxh * @date Nov 2, 2015 8:13:04 AM * */ public class Main{ public static void main(String []args){ Scanner cin=new Scanner(System.in); int n=cin.nextInt(); int []d=new int[n]; int sum=0; for(int i=0;i<n;i++){ d[i]=cin.nextInt(); sum+=d[i]; } int s=cin.nextInt(); int t=cin.nextInt(); if(s==t){ System.out.println(0); return ; } if(s>t){ int tmp=s; s=t; t=tmp; } int s1=0; for(int i=s;i<t;i++)s1+=d[i-1]; System.out.println(Math.min(s1, sum-s1)); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
85a632c32769a4ae79d3a88631d4bb30
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Scanner; public class Dist { public static void main(String ar[]){ Scanner sb=new Scanner(System.in); String s=sb.nextLine(); int n=Integer.parseInt(s); String b=sb.nextLine(); String a[]=b.split(" "); int d[]=new int[n]; for(int i=0;i<n;i++) { d[i]=Integer.parseInt(a[i]); } String e=sb.nextLine(); String f[]=e.split(" "); int s1=Integer.parseInt(f[0]); int t=Integer.parseInt(f[1]); int sum=0; if(s1<=t) { for(int i=s1-1;i<=(t-2);i++) { sum=sum+d[i]; } } else { int p=s1; s1=t; t=p; for(int i=s1-1;i<=(t-2);i++) { sum=sum+d[i]; } } int totsum=0; for(int i=0;i<n;i++) {totsum=totsum+d[i]; } int totsum1=totsum-sum; if(sum<=totsum1) { System.out.println(sum); } else { System.out.println(totsum1); } } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
5e561cb34eeebf360c01c94fa4b2bd4d
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Scanner; public class CircleLine{ public static void main(String args[]) { int n, s, t; int[] array = new int[100]; Scanner sc = new Scanner(System.in); n = sc.nextInt(); for(int i=0;i<n;i++) { array[i] = sc.nextInt(); } s = sc.nextInt(); t = sc.nextInt(); int sumA = 0, sumB = 0; boolean cek = false; for(int i=0;i<n;i++) { if((i+1 == s || i+1 == t) && (s!=t)) cek = !cek; if(cek) sumA += array[i]; else sumB += array[i]; } if(sumA < sumB) System.out.println(sumA); else System.out.println(sumB); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
31aaccb9e23b8f3f93395df232e883ef
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.*; public class CF278A_CircleLine { public static void main(String[] args){ Scanner in=new Scanner(System.in); int n=in.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++){ a[i]=in.nextInt(); } int s=in.nextInt()-1; int f=in.nextInt()-1; if(s>f){ int swap=f; f=s; s=swap; } int d1=0; for(int i=s;i<f;i++){ d1+=a[i]; } int d2=0; for(int i=f;i<n;i++){ d2+=a[i]; } for(int i=0;i<s;i++){ d2+=a[i]; } System.out.println(Math.min(d1, d2)); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
fd62a8e1f866a5b9f89a3dd3aec7900b
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; import static java.lang.Math.*; public class Maain implements Runnable{ final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException{ if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException{ while(!tok.hasMoreTokens()){ try{ tok = new StringTokenizer(in.readLine()); }catch (Exception e){ return null; } } return tok.nextToken(); } int readInt() throws IOException{ return Integer.parseInt(readString()); } long readLong() throws IOException{ return Long.parseLong(readString()); } double readDouble() throws IOException{ return Double.parseDouble(readString()); } public static void main(String[] args){ new Thread(null, new Maain(), "", 256 * (1L << 20)).start(); } 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()); } } } int sm(int x){ if (x==1) return 0;else return 1; } void solve() throws IOException{ int n=readInt(); int[] a=new int[n+1]; for(int i=1;i<n+1;i++){ a[i]=a[i-1]+readInt(); } int q=readInt()-1; int p=readInt()-1; int mi=min(q,p); int ma=max(q,p); out.println(min((a[ma]-a[mi]),a[n]-(a[ma]-a[mi]))); //Arrays.sort(a); } public void run(){ try{ timeBegin = System.currentTimeMillis(); init(); solve(); out.close(); time(); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
69a23c111f58e98d3b199633e5117685
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here Scanner scan = new Scanner(System.in); int stationCount = scan.nextInt(); int[] lenghtBetwStations = new int[102]; for (int i=1;i<=stationCount;i++){ lenghtBetwStations[i] = scan.nextInt(); } int station1 = scan.nextInt(); int station2 = scan.nextInt(); int temp = 0; if (station1>station2){ temp = station2; station2 = station1; station1 = temp; } int currentLength = 0; int minLength = 10000000; boolean stop = false; int index = station1; int globalIteration = 0; int otherStation = station2; while(!stop){ if (index == otherStation || (otherStation == 1 && index == 1 )) { globalIteration++; if (globalIteration == 2) { stop = true; } index = station2; otherStation = station1; if (currentLength<minLength) { minLength=currentLength; } currentLength = 0; continue; } currentLength+=lenghtBetwStations[index]; index++; if (index > stationCount) { index = 1; } } System.out.print(minLength); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
523ba94a675dcdd9c842956ca82bc2b5
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.Scanner; public class dis { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; sc.nextLine(); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } sc.nextLine(); int x=sc.nextInt(),y=sc.nextInt(); if(x>y) { int t=x; x=y; y=t; } int l=0,r=0; int k=x-1; for(int i=0;i<(y-x);i++) { l=l+a[k]; k++; } int u=y-1,q=0; for(int i=0;i<n-(y-x);i++) { if(u<n && q==0) { r=r+a[u]; } else {if(q==0) u=0; q=1; r=r+a[u]; } u++; } if(l>r) System.out.println(r); else System.out.println(l); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
3fc05a26470ed58f41f902c02e1ebd8e
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.*; public class Main { public static void main (String args[]){ Scanner input= new Scanner(System.in); int n= input.nextInt(); int a[]= new int[n+1]; int i; for(i=1;i<=n;i++) a[i]=a[i-1]+input.nextInt(); int temp1=input.nextInt(); int temp2=input.nextInt(); int max= Math.max(temp1, temp2); int min= Math.min(temp1, temp2); int way1=a[max-1]-a[min-1]; int way2=a[n]-way1; System.out.println(Math.min(way1,way2)); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
d650b9fbbdd2c15eb463096016040594
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
256 megabytes
import java.util.*; public class DisplayTime{ public static void main(String[] arg) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int dis[] = new int[n]; for (int i = 0; i < n; ++i) { dis[i] = input.nextInt(); } int s = input.nextInt(), t = input.nextInt(); input.close(); int onePath = 0, otherPath = 0, nMin = 0, nMax = 0; if (s > t) { nMin = t - 1; nMax = s - 1; } else { nMin = s - 1; nMax = t - 1; } for (int i = nMin; i < nMax; ++i) { onePath += dis[i]; } for (int i = 0; i < nMin; ++i) { otherPath += dis[i]; } for (int i = nMax; i < n; ++i) { otherPath += dis[i]; } if (onePath > otherPath) { System.out.print(otherPath); } else { System.out.print(onePath); } } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
11127c00de5496ca4b3af3288a15f95d
train_001.jsonl
1362065400
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
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); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int d[] = new int[n]; int sum = 0; for (int i=0; i<n; i++){ d[i] = in.nextInt(); sum += d[i]; } int a = in.nextInt()-1; int b = in.nextInt()-1; int from = Math.min(a, b); int to = Math.max(a, b); int tmp = 0; for (int i=from; i< to; i++){ tmp += d[i]; } out.println(Math.min(tmp, sum-tmp)); } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"]
2 seconds
["5", "15", "1", "0"]
NoteIn the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
Java 7
standard input
[ "implementation" ]
22ef37041ebbd8266f62ab932f188b31
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.
800
Print a single number — the length of the shortest path between stations number s and t.
standard output
PASSED
c686ab2adc38117657cb39f6ff4e3c4d
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.Scanner; public class qq4 { public static void main(String[] args) { Scanner a = new Scanner(System.in); int t = a.nextInt(); int[] digits = new int[19]; int[] presum = new int[19]; for(int i = 0; i < t; i++) { for(int k = 0; k < 19; k++) { digits[k] = presum[k] = 0; } long n = a.nextLong(); int s = a.nextInt(); long temp = n; int j = 0; while(temp>0) { digits[j] = (int)(temp%10); temp = temp/10; j++; } presum[18] = digits[18]; for(int k = 17; k >=0; k--) { presum[k] = presum[k+1] + digits[k]; } // System.out.println(presum[0]); if(presum[0] <= s) { System.out.println(0); } else { int k = 18; while(k>0 && presum[k] <= s) { k--; } k = k+1; while(k < 19 && (presum[k] + 1) > s) { k++; } digits[k] +=1; for(int l = k-1; l>=0; l--) { digits[l] = 0; } // for(int l = 18; l>=0; l--) { // System.out.print(presum[l] + " "); // } // System.out.println(); // for(int l = 18; l>=0; l--) { // System.out.print(digits[l]); // } // System.out.println(); long finalAns = 0; for(int l = 0; l<19; l++) { finalAns += digits[l] * (long)Math.pow(10, l); } System.out.println(finalAns - n); } } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
842c340d799c61139620a6ef88fbdc43
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.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.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DDecreaseTheSumOfDigits solver = new DDecreaseTheSumOfDigits(); solver.solve(1, in, out); out.close(); } static class DDecreaseTheSumOfDigits { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); for (int i = 0; i < t; i++) { solve(in, out); } } private void solve(InputReader in, PrintWriter out) { long n = in.nextLong(); int s = in.nextInt(); long answer = Long.MAX_VALUE; for (int zeros = 0; zeros <= 18; zeros++) { long tens = 1; for (int i = 0; i < zeros; i++) { tens *= 10; } long nextMultipleOfTens = ((n + tens - 1) / tens) * tens; if (sum(nextMultipleOfTens) > s) continue; answer = Math.min(answer, nextMultipleOfTens - n); } out.println(answer); } private int sum(long x) { int sum = 0; while (x > 0) { sum += x % 10; x /= 10; } return sum; } } static class InputReader { public final BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
2555840a3f4e4f58ed41943dc98254bd
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.InputStream; import java.io.BufferedReader; import java.io.PrintWriter; import java.io.InputStreamReader; import java.io.IOException; import java.util.*; public class SumDigit { public static void main(String[] args) throws IOException { InputReader in = new InputReader(System.in); PrintWriter pw = new PrintWriter(System.out); int t, s, cur, tmp; long moves, n; char[] ns, res; t = in.nextInt(); while (t-- > 0) { n = in.nextLong(); s = in.nextInt(); res = new char[19]; cur = 0; ns = new char[19]; Arrays.fill(ns, '0'); tmp = 18; while (n > 0) { ns[tmp--] = (char)('0' + n % 10); cur += n % 10; n /= 10; } Arrays.fill(res, '0'); moves = 0; if (cur > s) { tmp = 18; while (cur > s && tmp >= 0) { if (ns[tmp] == '0') { res[tmp] = (char)(moves + '0'); } else { cur -= ns[tmp] - '0'; res[tmp] = (char)(((10 - (ns[tmp] - '0')) % 10) + '0'); for (int i = tmp-1; i >= 0; i--) { if (ns[i] != '9') { ns[i] = (char)(ns[i] + 1); cur += 1; break; } else { ns[i] = '0'; cur -= 9; } } } tmp--; } for (int i = tmp+1; i < 19; i++) pw.print(res[i]); pw.println(); } else pw.println(0); } pw.close(); } static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
9ddd5bded2cc67cf267de75d5707d08d
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.io.*; public class Solution{ static int sumdig(long n) { int ans=0; while(n!=0) { ans=ans+(int)(n%10); n=n/10; } return ans; } public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); int t=Integer.parseInt(br.readLine()); while(t-->0) { String str[]=br.readLine().split(" "); long n=Long.parseLong(str[0]); int s=Integer.parseInt(str[1]); long ans=0,x=1; int sum=sumdig(n); int c=0; while(sum>s) { // sum=sum-c; int k=c+(int)(n%10); if(k==0) { c=0; } else if(k==10) { c=1; sum=sum-10; } else { sum=sum-k; c=1; int p=10-k; ans=ans+p*x; } sum=sum+c; x=x*10; n=n/10; } bw.write(ans+"\n"); } bw.close(); } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
027f4e4b125469735343040c64c389ef
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class D { public static void main(String[] args) throws IOException { FastReader f=new FastReader(); StringBuffer sb=new StringBuffer(); int test=f.nextInt(); while(test-->0) { long n=f.nextLong(); int s=f.nextInt(); long ans=0; if(sum(n)<=s) { sb.append(ans+"\n"); continue; } int no[]=new int[20]; long cpy=n; int idx=0; while(cpy>0) { no[idx]=(int)(cpy%10); cpy/=10; idx++; } for(int i=0;i<no.length;i++) { if(no[i]==0) continue; no[i]=0; no[i+1]++; while(no[i+1]>9) { i++; no[i]=0; no[i+1]++; } if(sum(no)<=s) { break; } } long l=getNo(no); ans=Math.abs(l-n); sb.append(ans+"\n"); } System.out.println(sb); } static long getNo(int a[]) { long s=0;int idx=0; for(int i=0;i<a.length;i++) if(a[i]!=0) idx=i; for(int i=idx;i>=0;i--) s=s*10+(long)a[i]; return s; } static int sum(long n) { int s=0; while(n>0) { s+=n%10; n/=10; } return s; } static int sum(int n[]) { int s=0; for(int i=0;i<n.length;i++) s+=n[i]; return s; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try{ st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
59d1d25ae03783ba9e107c554b4df40b
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.Scanner; public class NewClass4 { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); w: while (t-- > 0) { long n = scan.nextLong(); int s = scan.nextInt(); String k = String.valueOf(n); int r = 0; for (int i = 0; i < k.length(); i++) { r += (k.charAt(i) - '0'); } long res = 0; if (r <= s) { System.out.println("0"); } else { for (int i = k.length() - 1; i >= 0; i--) { r -= k.charAt(i) - '0'; if (r <= s && k.charAt(i) != '0') { String p = ""; for (int j = 0; j < i; j++) { p += k.charAt(j); } String y = "1"; for (int j = i; j < k.length(); j++) { p += "0"; y += "0"; } long w = Long.valueOf(y) + Long.valueOf(p); String q = String.valueOf(w); int u = 0; for (int j = 0; j < q.length(); j++) { u += q.charAt(j) - '0'; } /// System.out.println(q); if (u > s) { q = 1 + "0" + q.substring(1); w = Long.valueOf(q); } else { res += (w) - n; n = w; break; } } else if (k.charAt(i) != '0') { String p = ""; for (int j = 0; j < i; j++) { p += k.charAt(j); } String y = "1"; for (int j = i; j < k.length(); j++) { p += "0"; y += "0"; } res += (Long.valueOf(y) + Long.valueOf(p)) - n; n = Long.valueOf(y) + Long.valueOf(p); } } System.out.println(res); } } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
f1d7eb077270cf189c89d81642e0363f
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.Scanner; public class DecreasetheSumofDigits { static long sumOfDigit(long n) { String s = String.valueOf(n); long sum = 0; for (int i = 0; i < s.length(); i++) { sum += s.charAt(i) - '0'; } return sum; } static long rec(long n, long s) { if (sumOfDigit(n) <= s) { return 0; } if (n % 10 == 0) { return rec(n / 10, s) * 10; } return rec(n + 10 - n % 10, s) + (10 - n % 10); } public static void main(String args[]) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while (t-- > 0) { long n = scan.nextLong(); long s = scan.nextLong(); System.out.println(rec(n, s)); } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
937a30d1444a35032a4ab6c4912dbfa0
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.util.Scanner; import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import javax.lang.model.util.ElementScanner6; public class D1409 { public static void main(String args[]) { FastReader in=new FastReader(); PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int t=1; t=in.nextInt(); while(t-->0) { long n=in.nextLong(); long temp=n; long s=in.nextLong(); long sum=0; while(temp>0) { long d=temp%10; sum+=d; temp/=10; } if(sum<=s) { System.out.println(0); } else { long dif=s-sum; long temp2=n; long cost=00; long i=10; while(temp2>0) { long d=temp2%i; cost=(i-d); long temp3=temp2+cost; long newsum=0; while(temp3>0) { long d2=temp3%10; newsum+=d2; temp3/=10; } if(newsum<=s) break; i*=10; } System.out.println(cost); } } pr.flush(); } static int gcd(int a,int b) { if(a==0) return b; return gcd(a%b,b); } static int lcm(int a,int b) { return (a*b)/gcd(a,b); } static boolean prime[]; static void sieveofe() { int n=1000000; prime=new boolean[n+1]; Arrays.fill(prime,true); prime[1]=false; for(int i=2;i*i<=n;i++) { if(prime[i]==true) { for(int j=i*i;j<=n;j+=i) { prime[j]=false; } } } } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
a89d61ea2e6b8e2fe4d66244593b686e
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
/*****Author: Satyajeet Singh************************************/ import java.io.*; import java.util.*; import java.text.*; import java.lang.*; import java.math.*; public class Main{ /*********************************************Constants******************************************/ static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static long mod=(long)1e9+7; static long mod1=998244353; static ArrayList<Integer> graph[]; static int pptr=0,pptrmax=0; static String st[]; /*****************************************Solution Begins***************************************/ public static void main(String args[]) throws Exception{ int tt=pi(); while(tt-->0){ char input[]=ps().toCharArray(); int s=pi(); int n=input.length; int c=0; int A[]=new int[n]; for(int i=0;i<n;i++){ A[i]=input[i]-'0'; c+=A[i]; } long ans=0; long mul=1; int i=n-1; while(A[i]==0){ mul*=10; i--; } boolean first=true; for(;i>=0;i--){ //debug(c,A[i]); if(c<=s)break; if(A[i]==10){ c-=A[i]; c++; mul*=10; if(i>0)A[i-1]++; continue; } int k=10; long y=k-A[i]; c-=A[i]; //debug(c,A[i],A[i-1]); c++; if(i>0)A[i-1]++; ans+=y*mul; mul*=10; } out.println(ans); } /****************************************Solution Ends**************************************************/ clr(); } static void clr(){ out.flush(); out.close(); } static void nl() throws Exception{ pptr=0; st=br.readLine().split(" "); pptrmax=st.length; } static void nls() throws Exception{ pptr=0; st=br.readLine().split(""); pptrmax=st.length; } static int pi() throws Exception{ if(pptr==pptrmax) nl(); return Integer.parseInt(st[pptr++]); } static long pl() throws Exception{ if(pptr==pptrmax) nl(); return Long.parseLong(st[pptr++]); } static double pd() throws Exception{ if(pptr==pptrmax) nl(); return Double.parseDouble(st[pptr++]); } static String ps() throws Exception{ if(pptr==pptrmax) nl(); return st[pptr++]; } /***************************************Precision Printing**********************************************/ static void printPrecision(double d){ DecimalFormat ft = new DecimalFormat("0.00000000000"); out.println(ft.format(d)); } /**************************************Bit Manipulation**************************************************/ static int countBit(long mask){ int ans=0; while(mask!=0){ mask&=(mask-1); ans++; } return ans; } /******************************************Graph*********************************************************/ static void Makegraph(int n){ graph=new ArrayList[n]; for(int i=0;i<n;i++) graph[i]=new ArrayList<>(); } static void addEdge(int a,int b){ graph[a].add(b); } // static void addEdge(int a,int b,int c){ // graph[a].add(new Pair(b,c)); // } /*********************************************PAIR********************************************************/ static class Pair{ int u; int v; public Pair(int u, int v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return u == other.u && v == other.v; } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /******************************************Long Pair*******************************************************/ static class Pairl{ long u; long v; public Pairl(long u, long v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pairl other = (Pairl) o; return u == other.u && v == other.v; } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /*****************************************DEBUG***********************************************************/ public static void debug(Object... o){ System.out.println(Arrays.deepToString(o)); } /************************************MODULAR EXPONENTIATION***********************************************/ static long modulo(long a,long b,long c){ long x=1,y=a%c; while(b > 0){ if(b%2 == 1) x=(x*y)%c; y = (y*y)%c; b = b>>1; } return x%c; } /********************************************GCD**********************************************************/ static long gcd(long x, long y){ if(x==0) return y; if(y==0) return x; long r=0, a, b; a = (x > y) ? x : y; b = (x < y) ? x : y; r = b; while(a % b != 0){ r = a % b; a = b; b = r; } return r; } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
a73916822de95a573b87038914b260d2
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
//package ru.ilinchik.codeforces; import java.util.Scanner; /** * https://codeforces.com/contest/1409/problem/D */ public class D667 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int i = 0; i < t; i++) { long n = in.nextLong(); long s = in.nextLong(); System.out.println(solve(n, s)); } in.close(); } private static long inc(long n, long digit) { return (n / (digit * 10)) * (digit * 10) + (digit * 10); } private static long solve(long n, long s) { long i = n; int d = 0; while (sumOfDigits(i) > s) { i = inc(i, (long) Math.pow(10, d++)); } return i - n; } private static int sumOfDigits(long n) { int sum = 0; while (n > 0) { sum += (n % 10); n /= 10; } return sum; } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
f25fda13d31aa772972c35a890dab820
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; import java.math.BigInteger; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, Scanner in, PrintWriter out) { int T = in.nextInt(); while (T-- > 0) { solveOne(in, out); } } private void solveOne(Scanner in, PrintWriter out) { char[] chars = ("" + in.nextLong()).toCharArray(); int S = in.nextInt(); if (isGood(chars, S)) { out.println(0); return; } String temp = new String(chars); int s = 0; boolean add = false; for (int i = 0; i < chars.length; i++) { s += chars[i] - '0'; if ((s >= S && anyNonZero(i + 1, chars)) || (s > S)) { int curr = i - 1; while (curr >= 0 && chars[curr] == '9') { chars[curr] = '0'; curr--; } if (curr >= 0) chars[curr]++; else add = true; for (int j = i; j < chars.length; j++) { chars[j] = '0'; } break; } } String ans = (add ? "1" : "") + new String(chars); out.println(new BigInteger(ans).subtract(new BigInteger("" + temp))); } boolean anyNonZero(int pos, char[] chars) { if (pos >= chars.length) return false; for (char c : chars) { if (c != '0') return true; } return false; } boolean isGood(char[] chars, int s) { int sum = 0; for (char c : chars) { sum += c - '0'; } return sum <= s; } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
9098bcc0a59b523cd914e7629a5b4376
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class WhatIsThisBro { private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); private static StringTokenizer st; private static final long[] POWERS_OF_TEN = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000L, 100000000000L, 1000000000000L, 10000000000000L, 100000000000000L, 1000000000000000L, 10000000000000000L, 100000000000000000L, 1000000000000000000L }; private static final long[] POWERS_OF_TWO = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648L, 4294967296L, 8589934592L, 17179869184L, 34359738368L, 68719476736L, 137438953472L, 274877906944L, 549755813888L, 1099511627776L, 2199023255552L, 4398046511104L, 8796093022208L, 17592186044416L, 35184372088832L, 70368744177664L, 140737488355328L, 281474976710656L, 562949953421312L, 1125899906842624L, 2251799813685248L, 4503599627370496L, 9007199254740992L, 18014398509481984L, 36028797018963968L, 72057594037927936L, 144115188075855872L, 288230376151711744L, 576460752303423488L, 1152921504606846976L, 2305843009213693952L, 4611686018427387904L }; private static int readInt() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } private static Long readLong() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return Long.parseLong(st.nextToken()); } private static String readToken() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public static void main(String[] args) throws IOException { int T = readInt(); while (T-- > 0) solve(); pw.close(); } private static void solve() throws IOException { String n = readToken(); int s = readInt(); long operations = 0; int[] nchars = new int[n.length()]; int sum = 0; for (int i = 0; i < n.length(); i++) { int tempval = Character.getNumericValue(n.charAt(i)); nchars[i] = tempval; sum += tempval; } for (int i = n.length() - 1; i > -1; i--) { if (sum <= s) { break; } // if (sum - nchars[i] + 1 <= s) { // nchars[i] = sum - s; // somevalue++; // } sum -= (nchars[i] - 1); operations += (10L - nchars[i]) * powerOfTen(n.length() - 1 - i); if (i != 0) { nchars[i - 1]++; } } pw.println(operations); } private static long powerOfTen(int degree) { return POWERS_OF_TEN[degree]; } private static long powerOfTwo(int degree) { return POWERS_OF_TWO[degree]; } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
70fef2d22611382a81774987756ae2a2
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
// package CodeForces; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; public class Round667D { public static void solve() { int t = s.nextInt(); long[] pow10 = new long[20]; pow10[0] = 1; for(int i = 1; i < 20; i++) { pow10[i] = pow10[i - 1] * 10L; } while(t-- > 0) { long n = s.nextLong(); int sum = s.nextInt(); String str = Long.toString(n); int[] sum_till = new int[str.length()]; long ans = Long.MAX_VALUE; boolean ok = false; for(int i = 0; i < str.length(); i++) { int ls = i == 0 ? 0 : sum_till[i - 1]; int cc = str.charAt(i) - '0'; if(ls + cc > sum) { for(int j = i - 1; j >= 0; j--) { if(sum_till[j] + 1 > sum || str.charAt(j) == '9') { continue; } ok = true; ans = n/pow10[str.length() - 1 - j]; ans++; ans *= pow10[str.length() - 1 - j]; ans -= n; break; } break; } sum_till[i] = ls + cc; } if(sum_till[str.length() - 1] > 0 && sum_till[str.length() - 1] <= sum) { out.println(0L); continue; } if(ok) { out.println(ans); }else { long val = 1 * pow10[str.length()]; out.println(val - n); } } } public static void main(String[] args) { new Thread(null, null, "Thread", 1 << 27) { public void run() { try { out = new PrintWriter(new BufferedOutputStream(System.out)); s = new FastReader(System.in); solve(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } public static PrintWriter out; public static FastReader s; public static class FastReader { private InputStream stream; private byte[] buf = new byte[4096]; private int curChar, snumChars; public FastReader(InputStream stream) { this.stream = stream; } 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 nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int number = 0; do { number *= 10; number += c - '0'; c = read(); } while (!isSpaceChar(c)); return number * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } long sgn = 1; if (c == '-') { sgn = -1; c = read(); } long number = 0; do { number *= 10L; number += (long) (c - '0'); c = read(); } while (!isSpaceChar(c)); return number * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = this.nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = this.nextLong(); } return arr; } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } 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; } public boolean isEndofLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
2e838510b2401fc0e8b188f2c66481d5
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class Main { static final long MOD = 1000000007L; static final int INF = 50000000; static final int NINF = -50000000; public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int Q = sc.ni(); for (int q = 0; q < Q; q++) { long N = sc.nl(); String num = Long.toString(N); int D = num.length(); int S = sc.ni(); if (sum(N)<=S) { pw.println(0); continue; } int[] pref = new int[D+1]; for (int i = 1; i <= D; i++) pref[i] = pref[i-1]+(num.charAt(i-1)-'0'); long ans = 1L; for (int i = 0; i < D; i++) ans *= 10L; ans = ans-N; long p = 1; for (int i = D-2; i >= 0; i--) { p *= 10; if (num.charAt(i)=='9')continue; int sum = pref[i+1]+1; if (sum <= S) { long X = (N/p)*p + p; ans = Math.min(ans,X-N); } } pw.println(ans); } pw.close(); } public static int sum(long n) { if (n==0) return 0; else return (int)((n%10)+sum(n/10)); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { 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 ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
7dc5fa8c6f7de5cd88b1ba30985e5821
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.io.*; import java.text.*; /** * * @author alanl */ public class Main{ static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String[] args) throws IOException{ int t = readInt(); while(t-->0){ long n = readLong(); String[]arr = Long.toString(n).split(""); int s = readInt(), cur = 0; for(int i = 0; i<arr.length; i++){ cur+=Integer.parseInt(arr[i]); } if(cur<=s){ System.out.println(0); continue; } long pow = 1, ans = 0; for(int i = 0; i<=arr.length; i++){ long val = (n/pow)%10, cur1 = pow*((10-val)%10); n+=cur1; ans+=cur1; cur = 0; arr = Long.toString(n).split(""); for(int j = 0; j<arr.length; j++){ cur+=Integer.parseInt(arr[j]); } if(cur<=s){ break; } pow*=10; } System.out.println(ans); } } static String next () throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine().trim()); return st.nextToken(); } static long readLong () throws IOException { return Long.parseLong(next()); } static int readInt () throws IOException { return Integer.parseInt(next()); } static double readDouble () throws IOException { return Double.parseDouble(next()); } static char readChar () throws IOException { return next().charAt(0); } static String readLine () throws IOException { return input.readLine().trim(); } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH // Did you read the bounds? // Did you make typos? // Are there edge cases (N=1?) // Are array sizes proper (scaled by proper constant, for example 2* for koosaga tree) // Integer overflow? // DS reset properly between test cases? // Is using long longs causing TLE? // Are you using floating points? */ }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
21e9cef27ae74301d7b481b86322635c
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class D { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); long N = Long.parseLong(st.nextToken()); int S = Integer.parseInt(st.nextToken()); if(N == 1000000000000000000L) sb.append("0\n"); else { int[] digits = new int[19]; for(int i=0; i < 19; i++) { if(N == 0) break; digits[i] = (int)(N%10); N /= 10; } long res = 0L; int sum = 0; for(int d: digits) sum += d; if(sum > S) { for(int d=0; d < 18; d++) { long temp = 10-digits[d]; for(int i=0; i < d; i++) temp *= 10; res += temp; digits[d] = 0; if(digits[d+1] == 9) { for(int i=d+1; i < 19; i++) { if(digits[i] == 9) digits[i] = 0; else { digits[i]++; break; } } } else digits[d+1]++; sum = 0; for(int dd: digits) sum += dd; if(sum <= S) break; } } sb.append(res+"\n"); } } System.out.print(sb); } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
008ecd13365ada9746af214358d60f78
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.io.*; public class _1409D { static int[] MODS = {1000000007, 998244353}; static int MOD = MODS[0]; public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t-- > 0) { String n = sc.next(); int s = sc.nextInt(); int[] arr = new int[n.length()+1]; for (int i = 0; i < n.length(); i++) { arr[i+1] = n.charAt(i) - '0'; } int sum = 0; for (int i = 0; i <= n.length(); i++) { sum += arr[i]; } int index = n.length(); while (index > 0 && sum > s) { sum -= arr[index]; sum++; arr[index] = 0; arr[index-1]++; index--; } long less = 0; for (int i = 0; i < arr.length; i++) { less *= 10; less += arr[i]; } long original = 0; for (int i = 0; i < n.length(); i++) { original *= 10; original += n.charAt(i) - '0'; } out.println(less - original); } out.close(); } public static PrintWriter out; 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
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
d1c69935d67e458374c85437834cf7d2
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; public class Main { public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); 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[] nextSArray() { String sr[] = null; try { sr = br.readLine().trim().split(" "); } catch (IOException e) { e.printStackTrace(); } return sr; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // static long mod = 1000000007; // // static ArrayList<String> al=new ArrayList<>(); // public static int n; // // static long inversion=0; // // // static long arr[]; //static long tree[] = new long[100000]; //static void MakeTree(long arr[],int s,int e,int i) //{ // if(s==e) // { // tree[i] = arr[s]; // return; // } // if(s<e) // { // int mid = (s+e)/2; // MakeTree(arr,s,mid,2*i); // MakeTree(arr,mid+1,e,2*i+1); // tree[i] = Math.min(tree[2*i],tree[2*i+1]); // } // return; //} //static long MinQuery(int s,int e,int qs,int qe,int i) //{ // long t=0; // // if(qe<s || qs>e) // { t= Long.MAX_VALUE;} // else if(s==e) // { // t = tree[i]; // } // else if(qs==s&&qe==e) // { // t = tree[i]; // // } // else { // int mid = (s + e) / 2; // t= Math.min(MinQuery(s, mid, qs, qe, 2 * i), MinQuery(mid + 1, e, qs, qe, 2 * i + 1)); // // } // // return t; //} // //public static void UpdateQuery(int s,int e,int i,int qi,long val) //{ // if(qi<s||qi>e) // return; // if(s==e) // { // if(s==qi) // tree[i]=val; // return; // } // int mid = (s+e)/2; // UpdateQuery(s,mid,2*i,qi,val); // UpdateQuery(mid+1,e,2*i+1,qi,val); // // // //} public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); int t = sc.nextInt(); for (int t1 = 0; t1 < t; ++t1) { String s=sc.next(); long n=Long.valueOf(s); long sum=sc.nextLong(); long sum2=0; for(int i=0;i<s.length();++i) { sum2+=s.charAt(i)-48; } if(sum2<=sum) { out.println(0); continue; } sum-=1; long summ=0; boolean ans=false; for(int i=0;i<s.length();++i) { char ch=s.charAt(i); summ+=ch-48; //System.out.println(summ); if(summ>sum) { out.println((long) Math.pow(10,s.length()-i)-Long.valueOf(s.substring(i))); ans=true; break; } } } out.close(); } } // out.println(al.toString().replaceAll("[\\[|\\]|,]",""));
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
47ada2bc8e1344f0d92f152bd36323fe
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; public class Tester { public static void main(String[] args){ Scanner input = new Scanner(System.in); int t = input.nextInt(); for(int testCase = 1; testCase <= t; testCase++){ long n = Long.valueOf(input.next()); long s = Long.valueOf(input.next()); long moves; if(n <= s || (s >= sumOfAllDigits(n))){ moves = 0; }else if(s == 1){ long target = 1; for(long i = n; i > 0; i /= 10){ target *= 10; } moves = target - n; }else{ long nonZeroDigit = 1; long target = -1; for(long i = n; i > 0; i /= 10){ if(sumOfAllDigits(i + 1) <= s && (i + 1) % 10 != 0){ target = (i + 1) * nonZeroDigit; break; } nonZeroDigit *= 10; } if(target == -1){ target = nonZeroDigit; } moves = Math.abs(n - target); } System.out.println(moves); } } private static long sumOfAllDigits(long x){ long num = 0; while(x > 0){ num += x % 10; x /= 10; } return num; } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
9719af0787fc3a248643bc46075ce2f3
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; public class D { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int tt = 1; tt <= T; tt++) { String n = "0" + sc.next(), tmp = n; int s = sc.nextInt(); while(sum(n) > s) { n = reduce(n); } System.out.println(Long.parseLong(n) - Long.parseLong(tmp)); } } static String reduce(String s) { char A[] = s.toCharArray(); int n = s.length(); for(int i = n-1; i >= 0; i--) { if(A[i] != '0') { A[i] = '0'; StringBuilder sb = new StringBuilder(); for(int j = 0; j < i; j++) sb.append(A[j]); String next = add1(sb.toString()); for(int j = 0; j < next.length(); j++) { A[i - j - 1] = next.charAt(next.length() - j - 1); } break; } } StringBuilder sb = new StringBuilder(); for(int j = 0; j < A.length; j++) sb.append(A[j]); return sb.toString(); } static String add1(String s) { if(s.length() == 0) return "1"; LinkedList<Character> A = new LinkedList<>(); for(int i = 0; i < s.length(); i++) A.add(s.charAt(i)); int i = A.size() - 1; while(i >= 0 && A.get(i) == '9') { A.set(i, '0'); i--; } if(i < 0) A.addFirst('1'); else A.set(i, (char)((int)A.get(i) + 1)); StringBuilder sb = new StringBuilder(); for(char ch : A) sb.append(ch); return sb.toString(); } static int sum(String n) { int s = 0; for(char ch : n.toCharArray()) s += (ch - '0'); return s; } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
6a2cdad1275161f0e5723cb8cf406576
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; public class D { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int tt = 1; tt <= T; tt++) { String n = sc.next(), tmp = n; int s = sc.nextInt(); while(sum(n) > s) { n = reduce(n); } System.out.println(Long.parseLong(n) - Long.parseLong(tmp)); } } static String reduce(String s) { s = "0" + s; char A[] = s.toCharArray(); int n = s.length(); for(int i = n-1; i >= 0; i--) { if(A[i] != '0') { A[i] = '0'; StringBuilder sb = new StringBuilder(); for(int j = 0; j < i; j++) sb.append(A[j]); String next = add1(sb.toString()); for(int j = 0; j < next.length(); j++) { A[i - j - 1] = next.charAt(next.length() - j - 1); } break; } } StringBuilder sb = new StringBuilder(); for(int j = 0; j < A.length; j++) sb.append(A[j]); return sb.toString(); } static String add1(String s) { if(s.length() == 0) return "1"; LinkedList<Character> A = new LinkedList<>(); for(int i = 0; i < s.length(); i++) A.add(s.charAt(i)); int i = A.size() - 1; while(i >= 0 && A.get(i) == '9') { A.set(i, '0'); i--; } if(i < 0) A.addFirst('1'); else A.set(i, (char)((int)A.get(i) + 1)); StringBuilder sb = new StringBuilder(); for(char ch : A) sb.append(ch); return sb.toString(); } static int sum(String n) { int s = 0; for(char ch : n.toCharArray()) s += (ch - '0'); return s; } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
4c24cc9040ac0ba09c50cf291d22e2bb
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class DecreasetheSumofDigits { static int countDigits(long n) { int ans = 0; while(n!=0) { n/=10; ans++; } return ans; } static int sumod(int a[],int j) { int sum = 0; for(int i=0;i<=j;i++) sum+=a[i]; return sum; } static void putZero(int a[],int i,int n) { for(int aa=i;aa<n;aa++) a[aa] = 0; } public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while(t-->0) { long n = in.nextLong(); int s = in.nextInt(); int k = countDigits(n); long mod = (long)Math.pow(10, k-1); long ncopy = n; int no[] = new int[k]; for(int i=k-1;i>=0;i--) { no[i] = (int)(ncopy%10); ncopy /= 10; } int index = 0; while(index<k&&sumod(no,index)<=s) { index++; } if(index==k&&sumod(no,k-1)<=s) out.println("0"); else { if(index==k) index--; while(index>0&&sumod(no,index)>s) { putZero(no,index,k); no[index-1]++;index--; } if(index==0&&no[0]>s) { putZero(no,0,k); no[0] = 10; } long fin = 0; for(int i=0;i<k;i++) { fin = fin*10 + no[i]; } out.println(fin-n); } } out.flush(); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while(!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); }catch(IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } Integer[] readArray(long n) { Integer a[] = new Integer[(int)n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } long[] readArray(int n,long x) { long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = nextLong(); return a; } } static class Pair implements Comparable<Pair> { int x, y; Pair(int a, int b){ x = a; y = b; } int getKey() { return x; } int getVal() { return y; } @Override public int compareTo(Pair o) { if(o.getVal() - this.getVal()>0) return -1; else if(o.getVal() - this.getVal()<0) return 1; else return 0; } } static boolean arrayEquals(char a[], char b[]) { int n = a.length; boolean verdict = true; for(int i=0;i<n;i++) { if(a[i]!=b[i]) { verdict = false;break; } } return verdict; } static long lcm(long a, long b) { return (a*b)/gcd(a,b); } static long gcd(long a, long b) { if(b==0) return a; else return gcd(b,a%b); } static long hashInt(int x,int y) { return x*(1_000_000_000L)+y; } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
e7c853c0d0a395cad5ff64b5add754a6
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; public final class codeForces { public static void main(String args[]) { StringBuilder ans=new StringBuilder();; FastReader in=new FastReader(); int T=in.nextInt(); while(T-->0) { long N=in.nextLong(); int s=in.nextInt(); long num=N; long count=0; for(int i=1; i>0; i++) { long sum=0; while(num>0) { sum+=(num%10); num/=10; } if(sum<=s) { System.out.println(count); break; } long a=(long)Math.pow(10,i); long x=N%a; count=a-x; num=N+count; } } } //fucntions //fucntions //fucntions //fucntions static int[] input(int A[]) //input of Int Array { FastReader in=new FastReader(); int N=A.length; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] input(long A[]) //Input of long Array { FastReader in=new FastReader(); for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } static int GCD(int a,int b) //wrong output if a ||b are intially zero { if(b==0) { return a; } else return GCD(b,a%b ); } static boolean isPrime(int N) { for(int i=2; i*i<N; i++) if(N%i==0)return false; return true; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
013e1c8752c25cf6136b8be2e4c09e39
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class D { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader s = new FastReader(); static PrintWriter out = new PrintWriter(System.out); private static int[] rai(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); } return arr; } private static int[][] rai(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextInt(); } } return arr; } private static long[] ral(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } return arr; } private static long[][] ral(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextLong(); } } return arr; } private static int ri() { return s.nextInt(); } private static long rl() { return s.nextLong(); } private static String rs() { return s.next(); } static int MOD=1000000007; static int dfs(List<Integer>[] arr,int node,boolean[] vis,List<Long> count,int n) { vis[node]=true; // List<Integer> list=new ArrayList<>(); // list.add(node); int c=1; for(int i:arr[node]) { if(!vis[i]) { c+=dfs(arr,i,vis,count,n); } } long val=((long)c)*(n-c); // val%=MOD; if(node!=0) { count.add(val); } return c; } static boolean dfs(int[] arr,int prev,int turn) { int flag=0; System.out.println("turn= "+turn); for(int i:arr) { System.out.print(i+" "); if(i!=0) { flag=1; } } System.out.println(); if(flag==0) { return turn==1; } boolean check=false; for(int i=0;i<arr.length;i++) { if(prev!=i && arr[i]>0) { arr[i] -= 1; check|=dfs(arr, i, 1 - turn); // if(check) // { // return true; // } arr[i] += 1; } } if(turn ==1) { return !check; } else { return check; } } public static void main(String[] args) { StringBuilder ans = new StringBuilder(); int t = ri(); // int t=1; while (t-- > 0) { String first=rs(); char[] arr=first.toCharArray(); int total=ri(); int n=arr.length; int sum=0; int ind=n-1; int f=1; int currInd=-1; for(int i=0;i<n;i++) { sum+=arr[i]-'0'; if(sum==total) { currInd=i; break; } // if(sum<=total && i==n-1) // { // f=1; // break; // } if(sum>total) { f=0; sum-=arr[i]-'0'; if(sum==total) { ind=i-2; } else { ind = i - 1; } break; } } if(currInd!=-1) { for (int i=currInd+1;i<n;i++) { if(arr[i]!='0') { f=0; ind=currInd-1; break; } } } // System.out.println(ind); StringBuilder str=new StringBuilder(); if(ind==-1) { str.append("1"); } else { List<Integer> list=new ArrayList<>(); for (int i = 0; i <=ind; i++) { list.add(arr[i]-'0'); // str.append(arr[i]); } if(f==0) { int flag = 0; int i = list.size() - 1; while (i >= 0) { if (list.get(i) == 9) { list.set(i, 0); } else { flag = 1; list.set(i, list.get(i) + 1); break; } i--; } if (flag == 0) { list.add(0, 1); } } for(int j:list) { str.append(j); } } for(int i=ind+1;i<n;i++) { str.append("0"); } // System.out.println(str.toString()); ans.append(Long.parseLong(str.toString())-Long.parseLong(first)).append("\n"); } out.print(ans.toString()); out.flush(); } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
a12cd60151114467b64babedca28b7db
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
//package CodeForces; import java.io.*; import java.util.*; public class Main { public class Haha { /* _____ ___ /\ /\ | | | | /\ /\ /\ /\ /\ /\ | \ محمد أبوحسن* / \ / \ | | |___| /__\ / \ / \ / \ / \ /__\ | \ / \ / \ | | | | / \ / \ / \ / \ / \ / \ | / / \/ \ |_____| | | / \ / \/ \ / \/ \ / \ |___/ */ } 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; } } public static class MyPair { int x; int y; public MyPair(int x, int y) { this.x = x; this.y = y; } } public static int Fibonacci(int n) { int[] a = new int[n + 1]; a[0] = 0; a[1] = 1; for (int i = 2; i < a.length; i++) { a[i] = a[i - 1] + a[i - 2]; } return a[n]; } public static boolean isprime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return true; } } return false; } public static int[] addarr(int[] a, int n) { for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } return a; } public static int fact(int n){ int fact = 1; for(int i = 1; i <= n; i++) fact *= i; return fact; } public static long sumDigit(long n){ int a = 0; while(n != 0){ a += n%10; n /= 10; } return a; } public static long fastpow(long v, long p){ if (p == 0) return 1; if (p == 1) return v; long ans = fastpow(v, p / 2); if (p % 2 == 1) return ans * ans * v; else return ans * ans; } public static MyScanner sc = new MyScanner(); public static int MOD = 1000000007; // __builtin_popcountll(n) <-- c++ // binary count(1) ex. 8 --> 0100 --> 1 HashSet<Integer> set = new HashSet<>(); HashMap<Integer, Integer> map = new HashMap<>(); Random r = new Random(); ArrayList<Integer> l = new ArrayList<>(); public static void main(String[] args) { int t = sc.nextInt(); while(t-->0){ long n = sc.nextLong(); int c = 0, sum = 0, q = 0; int s = sc.nextInt(); if(sumDigit(n) <= s){ System.out.println(0); continue; } String ss = n + ""; while(sum < s && q != ss.length()){ sum += (int)(ss.charAt(q++) - 48); c++; } long g = Long.valueOf(ss.substring(c-1)); long rr = (long)fastpow(10, ss.length() - c + 1); System.out.println((rr - g)); } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
5218c9468e237da131664d566d53a4a6
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
//package CodeForces; import java.io.*; import java.util.*; public class Main { public class Haha { /* _____ ___ /\ /\ | | | | /\ /\ /\ /\ /\ /\ | \ محمد أبوحسن* / \ / \ | | |___| /__\ / \ / \ / \ / \ /__\ | \ / \ / \ | | | | / \ / \ / \ / \ / \ / \ | / / \/ \ |_____| | | / \ / \/ \ / \/ \ / \ |___/ */ } 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; } } public static class MyPair { int x; int y; public MyPair(int x, int y) { this.x = x; this.y = y; } } public static int Fibonacci(int n) { int[] a = new int[n + 1]; a[0] = 0; a[1] = 1; for (int i = 2; i < a.length; i++) { a[i] = a[i - 1] + a[i - 2]; } return a[n]; } public static boolean isprime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return true; } } return false; } public static int[] addarr(int[] a, int n) { for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } return a; } public static int fact(int n){ int fact = 1; for(int i = 1; i <= n; i++) fact *= i; return fact; } public static long sumDigit(long n){ int a = 0; while(n != 0){ a += n%10; n /= 10; } return a; } public static long fastpow(long v, long p){ if (p == 0) return 1; if (p == 1) return v; long ans = fastpow(v, p / 2); if (p % 2 == 1) return ans * ans * v; else return ans * ans; } public static MyScanner sc = new MyScanner(); public static int MOD = 1000000007; // __builtin_popcountll(n) <-- c++ // binary count(1) ex. 8 --> 0100 --> 1 HashSet<Integer> set = new HashSet<>(); HashMap<Integer, Integer> map = new HashMap<>(); Random r = new Random(); ArrayList<Integer> l = new ArrayList<>(); public static void main(String[] args) { int t = sc.nextInt(); while(t-->0){ long n = sc.nextLong(); long r = n, c = 0; long sum = 0; int s = sc.nextInt(); if(sumDigit(n) < s) System.out.println(0); else if(sumDigit(n) == s){ System.out.println(0); } else{ String ss = n + ""; int q = 0; while(r > 0){ if(sum < s){ if(q == ss.length()) break; int a = (int)(ss.charAt(q++) - 48); sum += a; c++; } else break; } long g = 0; for (int i = (int)c-1; i < ss.length(); i++) { g += (int)(ss.charAt(i) - 48); if(i != ss.length()-1)g *= 10; } long rr = (long)fastpow(10, ss.length()-c+1); System.out.println((rr - g)); } } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
bf7cbd5e363083eaef80add4c486edcb
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
//package CodeForces; import java.io.*; import java.util.*; public class Main { public class Haha { /* _____ ___ /\ /\ | | | | /\ /\ /\ /\ /\ /\ | \ محمد أبوحسن* / \ / \ | | |___| /__\ / \ / \ / \ / \ /__\ | \ / \ / \ | | | | / \ / \ / \ / \ / \ / \ | / / \/ \ |_____| | | / \ / \/ \ / \/ \ / \ |___/ */ } 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; } } public static class MyPair { int x; int y; public MyPair(int x, int y) { this.x = x; this.y = y; } } public static int Fibonacci(int n) { int[] a = new int[n + 1]; a[0] = 0; a[1] = 1; for (int i = 2; i < a.length; i++) { a[i] = a[i - 1] + a[i - 2]; } return a[n]; } public static boolean isprime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return true; } } return false; } public static int[] addarr(int[] a, int n) { for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } return a; } public static int fact(int n){ int fact = 1; for(int i = 1; i <= n; i++) fact *= i; return fact; } public static long sumDigit(long n){ int a = 0; while(n != 0){ a += n%10; n /= 10; } return a; } public static long fastpow(long v, long p){ if (p == 0) return 1; if (p == 1) return v; long ans = fastpow(v, p / 2); if (p % 2 == 1) return ans * ans * v; else return ans * ans; } public static MyScanner sc = new MyScanner(); public static int MOD = 1000000007; // __builtin_popcountll(n) <-- c++ // binary count(1) ex. 8 --> 0100 --> 1 HashSet<Integer> set = new HashSet<>(); HashMap<Integer, Integer> map = new HashMap<>(); Random r = new Random(); ArrayList<Integer> l = new ArrayList<>(); public static void main(String[] args) { int t = sc.nextInt(); while(t-->0){ long n = sc.nextLong(); long r = n, c = 0; long sum = 0; int s = sc.nextInt(); if(sumDigit(n) <= s){ System.out.println(0); } else{ String ss = n + ""; int q = 0; while(r > 0){ if(sum < s){ if(q == ss.length()) break; int a = (int)(ss.charAt(q++) - 48); sum += a; c++; } else break; } long g = 0; for (int i = (int)c-1; i < ss.length(); i++) { g += (int)(ss.charAt(i) - 48); if(i != ss.length()-1)g *= 10; } long rr = (long)fastpow(10, ss.length()-c+1); System.out.println((rr - g)); } } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
f45ed81821a4db35414275f4a1fc8e2d
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.Scanner; public class q5 { static long sum(String str){ long sum=0; for (int i = 0; i < str.length(); i++) { sum=sum+Long.parseLong(str.charAt(i)+""); } return sum; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while (t-->0) { String str=sc.next(); long s=sc.nextLong(); if (sum(str)<=s) { System.out.println(0); } else { long sum=1; int i=0; for (i = 0; i < str.length(); i++) { if (sum<=s) { sum=sum+Long.parseLong(str.charAt(i)+""); } else{ break; } } long bpart=1; for (int j = 0; j <= str.length()-i; j++) { bpart=bpart*10; } String spart=str.substring(i-1,str.length()); // System.out.println(bpart+" "+spart); long rem=Long.parseLong(spart); System.out.println(bpart-rem); } } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
b3c8da5227bdb258fed0d5c34de39595
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.math.BigInteger; import java.util.*; import java.io.*; public class CFD { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; private static final long MOD = 1000L * 1000L * 1000L + 7; private static final int[] dx = {0, -1, 0, 1}; private static final int[] dy = {1, 0, -1, 0}; private static final String yes = "Yes"; private static final String no = "No"; void solve() { int T = nextInt(); for (int i = 0; i < T; i++) { helper(); } } void helper() { long n = nextLong(); int s = nextInt(); String str = Long.toString(n); int sum = 0; for (int i = 0; i < str.length(); i++) { sum += str.charAt(i) - '0'; } if (sum <= s) { outln(0); return; } BigInteger res = new BigInteger(Long.toString(Long.MAX_VALUE)); BigInteger nBig = new BigInteger(Long.toString(n)); char[] arr = ("0" + str).toCharArray(); for (int i = 1; i < arr.length; i++) { if (arr[i - 1] == '9') { continue; } char[] tmp = Arrays.copyOf(arr, arr.length); tmp[i - 1] = (char) (arr[i - 1] + 1); for (int j = i; j < tmp.length; j++) { tmp[j] = '0'; } BigInteger nxt = findVal(tmp, s); BigInteger cand = nxt.subtract(nBig); if (res.compareTo(cand) > 0) { res = cand; } } outln(res.toString()); } BigInteger findVal(char[] tmp, int s) { int sum = 0; for (char c : tmp) { sum += c - '0'; } if (sum > s) { return new BigInteger(Long.toString(Long.MAX_VALUE)); } return new BigInteger(new String(tmp)); } void shuffle(long[] a) { int n = a.length; for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); long tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } long gcd(long a, long b) { while(a != 0 && b != 0) { long c = b; b = a % b; a = c; } return a + b; } private void outln(Object o) { out.println(o); } private void out(Object o) { out.print(o); } private void formatPrint(double val) { outln(String.format("%.9f%n", val)); } public CFD() { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) { new CFD(); } public long[] nextLongArr(int n) { long[] res = new long[n]; for(int i = 0; i < n; i++) res[i] = nextLong(); return res; } public int[] nextIntArr(int n) { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } public String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
c408ec08fa3b77273c3658f23108d685
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.System.exit; public class Main { public static void main(String[] args) { try { //inputStream = new FileInputStream(new File("./src/test.txt")); //outputStream = new FileOutputStream(new File("./src/out.txt")); inputStream = System.in; outputStream = System.out; out = new PrintWriter(outputStream); in = new InputReader(inputStream); int tests = in.nextInt(); for (test = 1; test <= tests; test++) { // printCase(); solve(); } out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } static InputStream inputStream; static OutputStream outputStream; static PrintWriter out; static InputReader in; static int test; static void solve() throws Exception { long n = Long.parseLong(in.next()); int s = in.nextInt(); long[] arr = new long[200]; long t = n; int i = 0; while (t > 0) { arr[i] = t % 10; t = (t - arr[i]) / 10; i = i + 1; } i = 0; while (getSum(arr, 100) > s) { arr[i] = 0; arr[i + 1] += 1; i = i + 1; while (arr[i] >= 10) { arr[i + 1] += arr[i] / 10; arr[i] = arr[i] % 10; i++; } } BigInteger overall = BigInteger.valueOf(-n); for (int j = 0; j < 100; j++) { if (arr[j] != 0) overall = overall.add(BigInteger.valueOf(arr[j]).multiply(BigInteger.valueOf(10).pow(j)) ); } out.println(overall.longValue()); } static long getSum(long[] arr, int n) { long res = 0; for (int i = 0; i < n; i++) { res += arr[i]; } return res; } static void printCase() { out.print("Case #" + test + ": "); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) throws FileNotFoundException { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { String str = reader.readLine(); if (str == null) return ""; else tokenizer = new StringTokenizer(str); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
717c682e6a5880a99b816d9aa04172bb
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder out = new StringBuilder(); while(t-->0){ long n = sc.nextLong() , m = sc.nextInt(); long p = 10; long num = 0; while(sum(n)>m){ long remain = n%p; n += p - remain; num += p - remain; p = p*10; } out.append(num+"\n"); } System.out.println(out); } static long sum(long n){ long c = 0; while(n>0){ c+=n%10; n=n/10; } return c; } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
92a0d0cdd977b6125341a79ac4aef389
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.PriorityQueue; import java.util.StringTokenizer; public class D{ public static void main(String[] args) throws IOException { FastScanner sc=new FastScanner(); int t = sc.nextInt(); first : while(t-- != 0) { String str = sc.next(); int len = str.length(); int s = sc.nextInt(); if(len == 1) { if(Integer.parseInt(str) <= s) { System.out.println(0); continue first; } } int sum = 0; int count = 0; while(sum <= s && count < len) { sum += Integer.parseInt(""+str.charAt(count)); count++; } if(sum <= s) { System.out.println(0); continue; } sum-=Integer.parseInt(""+str.charAt(count-1)); if(sum == s) { sum = 0; count = 0; while(sum < s && count < len) { sum += Integer.parseInt(""+str.charAt(count)); count++; } } else sum+=Integer.parseInt(""+str.charAt(count-1)); String ss = ""; ss = str.substring(count-1); //System.out.println(ss); int length = ss.length(); String xxx = "1"; while(length-- != 0) xxx+="0"; long value = Long.parseLong(xxx) - Long.parseLong(ss); System.out.println(value); } } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
ded5b8975e390aa4a875e3f9f6986df3
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Solution { public static void main(String[] args) throws IOException { Reader fs=new Reader(); // BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); // StringTokenizer st = new StringTokenizer(br.readLine()); PrintWriter out = new PrintWriter(System.out); // int T = Integer.parseInt(st.nextToken()); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { // for (int tt=1;; tt++) { long n = fs.nextLong(); long norig = n; int s = fs.nextInt(); int d = checkSum(n); int k= 1; while (d>s) { // System.out.println(" "+n); n+=10-(n%10); n/=10; d=checkSum(n); k++; } long ans = n*(long)Math.pow(10,k-1)-norig; out.println((long)ans); } out.close(); } static int checkSum(long n ) { int s=0; while (n>0) { s+=n%10; n/=10; } return s; } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } /** Faster input **/ 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[1024];int cnt=0,c; while((c= read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} public void close()throws IOException{if(din==null) return;din.close();} } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
9dc29e6afdb6c142e3a1f66d614f1f31
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.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.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /* * Copyright (c) --> Arpit * Date Created : 4/9/2020 * Have A Good Day ! */ /** * Built using CHelper plug-in * Actual solution is at the top * * @author Arpit */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); OutputWriter out = new OutputWriter(outputStream); DDecreaseTheSumOfDigits solver = new DDecreaseTheSumOfDigits(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class DDecreaseTheSumOfDigits { int digiSum(long num) { int ans = 0; while (num > 0) { ans += (num % 10); num /= 10; } return ans; } public void solve(int testNumber, FastReader r, OutputWriter out) { long n = r.nextLong(); long s = r.nextLong(); long ans = 0; char[] num = Long.toString(n, 10).toCharArray(); long[] digits = new long[num.length + 1]; for (int i = 1; i < digits.length; i++) digits[i] = (num[i - 1] - '0'); if (digiSum(n) <= s) { out.println(0); } else { int digi_sum = 0; int idx = 0; for (int i = 0; i < digits.length; i++) { if (digi_sum + digits[i] < s) { digi_sum += digits[i]; idx = i; } else break; } for (int i = idx + 1; i < digits.length; i++) digits[i] = 0; if (digits[idx] == 9) { for (int i = idx; i >= 0; i--) { if (digits[i] == 9) digits[i] = 0; else { digits[i]++; break; } } } else { digits[idx]++; } long new_num = 0; for (int i = 0; i < digits.length; i++) { int place = (digits.length - i - 1); new_num += (digits[i] * (long) Math.pow(10, place)); } ans = new_num - n; out.println(ans); } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public 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; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { writer.print(objects[i]); if (i != objects.length - 1) writer.print(" "); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
18e5903fdb2b7093dd83aa1a7d269311
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public final class CF { public static void main(String[]args)throws IOException { FastReader ob=new FastReader(); int t=ob.nextInt(); StringBuffer sb=new StringBuffer(); while(t-->0) { long n=ob.nextLong();long n1=n; long s=ob.nextInt(); ArrayList<Long> a=new ArrayList<>(); long sum=0; while(n!=0) { a.add(n%10); sum+=n%10; n/=10; } Collections.reverse(a); if(sum<=s) System.out.println("0"); else { int c=0,pos=-1; for(int i=0;i<a.size();i++) { if(a.get(i)+1+c<=s) { if(a.get(i)!=9) pos=i; } else break; c+=a.get(i); //System.out.println(c); } if(pos==-1) { int num[]=new int[a.size()+1]; num[0]=1; StringBuffer ans=new StringBuffer(); for(int i=0;i<num.length;i++) ans.append(num[i]); System.out.println(Long.valueOf(ans.toString())-n1); } else { long num[]=new long[a.size()]; for(int i=0;i<pos;i++) num[i]=a.get(i); num[pos]=a.get(pos)+1; StringBuffer ans=new StringBuffer(); for(int i=0;i<num.length;i++) ans.append(num[i]); if(ans.length()==a.size()+1) { num=new long[a.size()+1]; num[0]=1; ans=new StringBuffer(); for(int i=0;i<num.length;i++) ans.append(num[i]); } System.out.println(Long.valueOf(ans.toString())-n1); } } } System.out.println(sb); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String nextLine() { String s=""; try { s=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
678a2fda29bede13d16f26cbfa757269
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public final class CF { public static void main(String[]args)throws IOException { FastReader ob=new FastReader(); int t=ob.nextInt(); StringBuffer sb=new StringBuffer(); while(t-->0) { long n=ob.nextLong();long n1=n; long s=ob.nextInt(); ArrayList<Long> a=new ArrayList<>(); long sum=0; while(n!=0) { a.add(n%10); sum+=n%10; n/=10; } Collections.reverse(a); if(sum<=s) System.out.println("0"); else { int c=0,pos=-1; for(int i=0;i<a.size();i++) { if(a.get(i)+1+c<=s) { if(a.get(i)!=9) pos=i; } else break; c+=a.get(i); //System.out.println(c); } if(pos==-1) { int num[]=new int[a.size()+1]; num[0]=1; StringBuffer ans=new StringBuffer(); for(int i=0;i<num.length;i++) ans.append(num[i]); System.out.println(Long.valueOf(ans.toString())-n1); } else { long num[]=new long[a.size()]; for(int i=0;i<pos;i++) num[i]=a.get(i); num[pos]=a.get(pos)+1; StringBuffer ans=new StringBuffer(); for(int i=0;i<num.length;i++) ans.append(num[i]); System.out.println(Long.valueOf(ans.toString())-n1); } } } System.out.println(sb); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String nextLine() { String s=""; try { s=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
25c4ec82b3ac5f9277f166ab6d9b4218
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static long dsum(long a) { long sum =0; while(a>0) { sum +=a%10; a /=10; } return sum; } public static long pow(int d) { long a=1; for (int i = 0; i < d; i++) { a*=10; } return a; } public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); StringTokenizer st; int T = Integer.parseInt(br.readLine()); for (int test_case = 0; test_case < T; test_case++) { st = new StringTokenizer(br.readLine()); long n = Long.parseLong(st.nextToken()); long s = Long.parseLong(st.nextToken()); long sum = dsum(n); long ans = 0; long now = n; long next = n; if(sum<=s) { } else { int digit = 1; while(true) { now = next; next = (n/pow(digit))*pow(digit)==0?pow(digit):(n/pow(digit))*pow(digit)+pow(digit); // System.out.println("next:"+next); // System.out.println("dsum:"+dsum(next)); if(now>next) { digit++; continue; } if(dsum(next)<=s) { ans = next-n; break; } digit++; } } // System.out.println("ans:"+ans); sb.append(ans); sb.append("\n"); } System.out.println(sb.toString()); } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
08292ae844bb44592f33452052f0c61b
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class round667D { public static void main(String s[]) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-->0){ s=(br.readLine()).split(" "); long n=Long.parseLong(s[0]); int sum=Integer.parseInt(s[1]); int currSum=sum(n); BigInteger t1=BigInteger.ONE; BigInteger res=BigInteger.ZERO; while(currSum>sum && n>0){ int currDig=(int)(n%10); n=n/10; if(currDig>0){ n=n+1; currDig=10-currDig; res=res.add(t1.multiply(BigInteger.valueOf(currDig))); currSum=sum(n); } t1=t1.multiply(BigInteger.valueOf(10)); } System.out.println(res); } } static int sum(long a){ int sum=0; while(a>0){ sum+=a%10; a=a/10; } return sum; } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
078bbabc7cb82564ceb781297d6a161d
train_001.jsonl
1599230100
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
// package CodeForcesContests; import java.util.Scanner; public class zCF_667_3_D { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 1; i<= t; i++) { long num = sc.nextLong(); int sum = sc.nextInt(); long added = 0; long div = 10; while(sumOfDigits(num) > sum) { long remain = num%div; num += div - remain; added += div - remain; div = div*10; } System.out.println(added); } } public static long sumOfDigits(long num) { int sum = 0; while(num>0) { int dig = (int)(num%10); num = num/10; sum += dig; } return sum; } }
Java
["5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1"]
2 seconds
["8\n0\n500\n2128012501878\n899999999999999999"]
null
Java 8
standard input
[ "greedy", "math" ]
50738d19041f2e97e2e448eff3feed84
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^{18}$$$; $$$1 \le s \le 162$$$).
1,500
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.
standard output
PASSED
1ae08229893ea5fa00b3086d0da3b3eb
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { // BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // int t=Integer.parseInt(br.readLine().trim()); Scanner sc=new Scanner(System.in); int t= sc.nextInt(); while (t-->0){ // String[] nx=br.readLine().trim().split(" "); // int n=Integer.parseInt(nx[0]); // int x=Integer.parseInt(nx[1]); int n=sc.nextInt(); int x=sc.nextInt(); // String[] a1=br.readLine().trim().split(" "); // String[] a2=br.readLine().trim().split(" "); int[] ar1=new int[n]; Integer[] ar2=new Integer[n]; for (int i=0;i<n;i++) ar1[i]= sc.nextInt(); for (int i=0;i<n;i++) ar2[i]= sc.nextInt(); Arrays.sort(ar1); Arrays.sort(ar2,Collections.reverseOrder()); int i; for ( i=0;i<n;i++){ if (ar1[i]+ar2[i]>x) break; } if (i==n) System.out.println("Yes"); else System.out.println("No"); } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
9a840da4e485494274fafffc979b7486
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.*; public class Main { public static void main(String[] args) { String isTrue = "No"; Scanner sc = new Scanner(System.in); int T = sc.nextInt(); sc.nextLine(); while(T>0) { int n = sc.nextInt(); Integer b[] = new Integer[n]; Integer a[] = new Integer[n]; int x = sc.nextInt(); sc.nextLine(); for(int i=0;i<n;i++) { a[i] = sc.nextInt(); b[i] = sc.nextInt(); } Collections.reverse(Arrays.asList(b)); for(int i=0;i<n;i++) { if(a[i]+b[i]<=x) { isTrue = "Yes"; } else{ isTrue = "No"; break; } } System.out.println(isTrue); T--; } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
dfa480abe2ab0da1866633a448cd35c4
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.Scanner; public class CF1445A{ public static void main(String[]args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int x = sc.nextInt(); int a [] = new int[n]; int b [] = new int[n]; int c = 0; for(int i = 0; i<n; i++){ a[i] = sc.nextInt(); } for(int i = 0; i<n; i++){ b[i] = sc.nextInt(); } for(int i = 0; i<n; i++){ if(a[i]+b[n-i-1]>x){ c=1; break; } } if(c==0)System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
4f6b5f8bd1c27ce88cdcc2391f3ea093
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
//package codeforce; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class pereuporyadMassiva { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int j = 0; j < t; j++) { int n = sc.nextInt(); int x = sc.nextInt(); int[] a = new int[n]; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { b[i] = sc.nextInt(); } int cnt = 0; Arrays.sort(b, Collections.reverseOrder()); Arrays.sort(a); for (int i = 0; i < n; i++) { if(a[i] + b[i] <= x) cnt++; } if(cnt == n) System.out.println("Yes"); else System.out.println("No"); } // for (int i = 0; i < n; i++) { // if(a[i] + b[i] <= x) { // cnt++; // } // } // System.out.println(cnt); // if(cnt == n) // System.out.println("Yes"); // else // System.out.println("No"); } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
c75675ec9b69f68684455e4a0d51cdce
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); int ar[]=new int[n]; int br[]=new int[n]; for(int i=0;i<n;i++) ar[i]=sc.nextInt(); for(int i=0;i<n;i++) br[i]=sc.nextInt(); int j=n-1; boolean ch=true; for(int i=0;i<n;i++) { if(ar[i]+br[j]>k) { System.out.println("NO"); ch=false; break; } j--; } if(ch) System.out.println("YES"); } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
e8f66e5be6477681c7e9d33b068b8d4d
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.io.*; import java.util.Arrays; public class JavaApplication12 { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(in.readLine()); for (int i = 0; i < t; i++) { int p[] = get(in.readLine()); int x = p[1]; int[] a = get(in.readLine()); int[] b = get(in.readLine()); if (i != t - 1) { in.readLine(); } Arrays.sort(a); Arrays.sort(b); boolean o = true; for (int u = 0; u < a.length; u++) { if (a[u] + b[b.length - u - 1] > x) { o = false; break; } } if (o) { System.out.println("Yes"); } else { System.out.println("No"); } } } public static int[] get(String s) { String out[] = s.split(" "); int[] ot = new int[out.length]; for (int i = 0; i < out.length; i++) { ot[i] = Integer.parseInt(out[i]); } return ot; } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
d5118b4a46af6a629d8c8295ce3f219b
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.*; //import java.io.*; //import java.lang.reflect.Array; public class div2$680 { public static void main(String args[]) { Scanner in =new Scanner(System.in); int t = in.nextInt(); // int i; while(t>0) { t--; int n = in.nextInt(); int k = in.nextInt(); int[] ar= new int[n]; int[] br= new int[n]; for(int i=0;i<n;i++) { ar[i]=in.nextInt(); } for(int i=0;i<n;i++) { br[i]=in.nextInt(); } Arrays.sort(ar); Arrays.sort(br); int a = ar[0]; int b = ar[n-1]; int c = br[0]; int d = br[n-1]; int j; boolean yes = true; for(j=0;j<n;j++) { if(ar[j]+br[n-1-j]>k) { yes=false; break; } } if(yes) System.out.println("YES"); else System.out.println("NO"); // if((a+d<=k)&&b+c<=k) { // System.out.println("YES"); // }else { // System.out.println("NO"); // } } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
72daa6284b94855dbfdd90baca05548b
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
//package olp; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class CF1145_A { public static void main(String[] args) { Scanner sc = new Scanner (System.in); int t = sc.nextInt(); while (t > 0) { int n, x; n = sc.nextInt(); x = sc.nextInt(); boolean kt = true; Integer a[] = new Integer[n]; Integer b[] = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { b[i] = sc.nextInt(); } Arrays.sort(a); Arrays.sort(b, Collections.reverseOrder()); for (int i = 0; i < n; i++) { if (a[i] + b[i] > x) { kt = false; break; } } if (kt) { System.out.println("Yes"); } else { System.out.println("No"); } t--; } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
72bfa4b693a855d5875ed69481011186
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.io.IOException; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++){ int n = sc.nextInt(); int x = sc.nextInt(); boolean test = true; int[] mas_a = new int[n]; int[] mas_b = new int[n]; for (int j = 0; j < n; j++){ mas_a[j] = sc.nextInt(); } for (int j = 0; j < n; j++){ mas_b[j] = sc.nextInt(); } Arrays.sort(mas_a); Arrays.sort(mas_b); int[] new_mas = new int[n]; for (int j = 0; j < n; j++){ new_mas[j] = mas_b[n - j - 1]; } mas_b = new_mas; // for (int p:mas_a) { // System.out.print(p + " "); // } // System.out.println(); // for (int p:mas_b) { // System.out.print(p + " "); // } for (int j = 0; j < n; j++){ if(mas_a[j] + mas_b[j] > x){ test = false; } } if (test){ System.out.println("Yes"); } else { System.out.println("No"); } } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
bcb2b785534654266b1c87409e9fe9d9
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class Arrayrearrange { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int x = sc.nextInt(); Integer a[] = new Integer[n]; Integer b[] = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { b[i] = sc.nextInt(); } Arrays.sort(a); Arrays.sort(b,Collections.reverseOrder()); int f=1; for (int i = 0; i < n; i++) { if (a[i] + b[i] > x) { f=0; break; } } if(f==1) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
2b9408997fb3e31d0191ddb29063f3a7
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.*; public class a { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while (t-- > 0) { int n = scn.nextInt(); int x = scn.nextInt(); int[] arr1 = new int[n]; int[] arr2 = new int[n]; for (int i = 0; i < n; i++) { arr1[i] = scn.nextInt(); } for (int i = 0; i < n; i++) { arr2[i] = scn.nextInt(); } int i = 0; int j = n - 1; boolean flag = true; while(j>=0){ if((arr1[i]+arr2[j])>x){ System.out.println("No"); flag=false; break; }else{ i++; j--; } } // if (arr1[i] + arr2[j] > x) // System.out.println("No"); // else if (arr1[j] + arr2[i] > x) // System.out.println("No"); // else if(flag) System.out.println("Yes"); } scn.close(); } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
f2607d81f9517644141a5046ebccf12c
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; public class Array{ public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String linea; linea=br.readLine(); int limite=Integer.parseInt(linea); int nn,x; int a[]; int b[]; for(int n=1;n<=limite;n++) { linea=br.readLine(); String array[]=linea.split(" "); nn=Integer.parseInt(array[0]); x=Integer.parseInt(array[1]); linea=br.readLine(); String array2[]=linea.split(" "); a=new int[array2.length]; for(int i=0;i<array2.length;i++) { a[i]=Integer.parseInt(array2[i]); } linea=br.readLine(); String array3[]=linea.split(" "); b=new int[array3.length]; int indice=0; for(int i=array3.length-1;i>=0;i--) { b[indice]=Integer.parseInt(array3[i]); indice++; } if(suma(a,b,x,nn)==true) { System.out.println("No"); }else { System.out.println("Yes"); } linea=br.readLine(); } br.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } static boolean suma(int a[],int b[], int x, int nn) { for(int i=0;i<nn;i++) { if(a[i]+b[i]>x) { return true; } } return false; } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
ba44fcda41d337758999cc953f9bdc38
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.*; public class Solution { public static String arrange(int [] a, int [] b, int n, int k) { if (n == 1 && k < a[0]+b[0]) return "No"; if (n == 1 && k >= a[0]+b[0]) return "Yes"; Arrays.sort(a); Arrays.sort(b); for (int i=0; i<n; i++) { if (a[i] + b[n-i-1] > k) return "No"; } return "Yes"; } public static void main(String [] args) { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while (test-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); int [] a = new int [n]; int [] b = new int [n]; for (int i=0; i<n; i++) { a[i] = sc.nextInt(); } for (int i=0; i<n; i++) { b[i] = sc.nextInt(); } System.out.println(arrange(a, b, n, k)); } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
1169bf0c7e55df817184958386899969
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
// _________________________RATHOD_____________________________________________________________ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { // FastReader sc=new FastReader(); Scanner sc=new Scanner(System.in); int t; t=1; t=sc.nextInt(); StringBuilder sb=new StringBuilder(); while(t>0) { t--; int n=sc.nextInt(); int x=sc.nextInt(); int ar1[]=new int[n]; int ar2[]=new int[n]; for(int i=0;i<n;i++) { ar1[i]=sc.nextInt(); } for(int i=0;i<n;i++) { ar2[i]=sc.nextInt(); } int flag=1; int j=n-1; for(int i=0;i<n;i++) { if(ar1[i]+ar2[j]<=x) { j--; continue; } else { flag=0; break; } } if(flag==0) System.out.println("NO"); else System.out.println("YES"); String s; //Scanner sp=new Scanner(System.in); } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
1981d223df81c5517d2d88bf94127064
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.io.*; import java.util.Collections; import java.util.PriorityQueue; import java.util.StringTokenizer; public class FloorNumber { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int x = sc.nextInt(); PriorityQueue<Integer> a = new PriorityQueue<Integer>(); PriorityQueue<Integer> b = new PriorityQueue<Integer>(Collections.reverseOrder()); while(n-->0){ a.add(sc.nextInt()); }n=a.size(); while(n-->0){ b.add(sc.nextInt()); } while(!a.isEmpty()) { if (a.poll() + b.poll() > x) { n = 1; break; } } pw.println(n!=1?"YES":"NO"); } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
66ee395535bd019463e0e9e2cb35f86a
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.io.*; import java.util.*; /** * @username: sriman_chetan * @author: Chetan Raikwar * @date: 01-11-2020 */ public class Solution { public static void main(String[] args) throws IOException { try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out))) { int t = Integer.parseInt(reader.readLine()); while (t-- > 0) { StringTokenizer input = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(input.nextToken()); int x = Integer.parseInt(input.nextToken()); int[] a = new int[n]; int[] b = new int[n]; input = new StringTokenizer(reader.readLine()); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(input.nextToken()); } input = new StringTokenizer(reader.readLine()); for (int i = 0; i < n; i++) { b[i] = Integer.parseInt(input.nextToken()); } Arrays.sort(a); Arrays.sort(b); boolean isPossible = true; for (int i = 0; i < n; i++) { if (a[i] + b[n - i - 1] > x) { isPossible = false; break; } } reader.readLine(); writer.println(isPossible ? "Yes" : "No"); } } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
54334fb0b46914230757e554ecf4d387
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int flag = 0; while(t>0) { int n = sc.nextInt(); int x = sc.nextInt(); int a[] = new int[n]; int b[] = new int[n]; int tempArr[] = new int[n]; for(int i=0; i<n; i++) { a[i] = sc.nextInt(); } for(int i=0; i<n; i++) { b[i] = sc.nextInt(); } for(int i=n-1, temp=0; i>=0; temp++, i--) { tempArr[temp] = b[i]; } for(int i=0; i<n; i++) { if(a[i] + tempArr[i] <= x) { flag = 1; }else { flag = -1; break; } } if(flag==1) { System.out.println("Yes"); } if(flag==-1) { System.out.println("No"); } t--; } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
2587aaee763182d5c9bf08807cca3bdc
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.Arrays; import java.util.Scanner; public class ArrayRearrangment { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); a: while (t-- > 0) { int n = scan.nextInt(); int[] a = new int[n]; int x = scan.nextInt(); int[] b = new int[n]; for (int i = 0; i < n; i++) { a[i] = scan.nextInt(); } for (int i = 0; i < n; i++) { b[i] = scan.nextInt(); } Arrays.sort(a); Arrays.sort(b); for (int i = 0; i < n; i++) { if (a[i] + b[n - i - 1] > x) { System.out.println("No"); continue a; } } System.out.println("Yes"); } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
e1a9a3e4fef68abd40227604c6a41ac5
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class CodeTest { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); int n=0; int x =0; boolean bol=true; for(int i=0;i<t;i++) { n=s.nextInt(); x=s.nextInt(); Integer a[]=new Integer[n]; int b[]=new int[n]; for(int j=0;j<n;j++) { a[j]=s.nextInt(); } for(int j=0;j<n;j++) { b[j]=s.nextInt(); } Arrays.sort(a, Collections.reverseOrder()); for(int k=0;k<a.length;k++) { if((a[k]+b[k])>x) { bol=false; break; } else { bol=true; } } if(bol) System.out.println("Yes"); else { System.out.println("No"); } } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
f65d81266acceb58fdba226b21d9dac1
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.*; public class Solution{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t-->0){ int n = in.nextInt(); int x = in.nextInt(); Integer[] a = new Integer[n]; Integer[] b = new Integer[n]; for(int i = 0;i<n;i++){ a[i] = in.nextInt(); } for(int i = 0;i<n;i++){ b[i] = in.nextInt(); } Arrays.sort(a); Arrays.sort(a, Collections.reverseOrder()); boolean ans = true; for(int i = 0;i<n;i++){ if(a[i]+b[i]>x){ ans = false; break; } } System.out.println(ans?"Yes":"No"); } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
6cbd571199a9f5990d55de2c97339f5a
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.*; import java.io.*; public class ProblemA { public static String getResult(Long A[],Long B[],int n,Long x) { Arrays.sort(B,Collections.reverseOrder()); for(int i=0;i<n;i++) { if(A[i]+B[i]>x) return "No"; } return "Yes"; } public static void main(String[] args) throws IOException { //System.out.println("Hello World!"); try { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine().trim()); for(int z=0;z<t;z++) { String ip1[]=br.readLine().trim().split(" "); int n=Integer.parseInt(ip1[0]); Long x=Long.parseLong(ip1[1]); Long A[]=new Long[n]; Long B[]=new Long[n]; String ip2[]=br.readLine().trim().split(" "); String ip3[]=br.readLine().trim().split(" "); //System.out.println(); String temp; if(z!=t-1) temp=br.readLine().trim(); for(int i=0;i<n;i++) { A[i]=Long.parseLong(ip2[i]); B[i]=Long.parseLong(ip3[i]); } System.out.println(getResult(A,B,n,x)); } } catch (Exception e) { return; } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
78096e98601941281884f1769e05a235
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.*; import java.util.Collections; public class test { public static void main(String[] args) { int n,x,max = 0,min = 0; Integer[] a=new Integer[1000]; Integer[] b=new Integer[1000]; int i,t,flag; test obj=new test(); Scanner sc=new Scanner(System.in); t=sc.nextInt(); while(t-->0) { n=sc.nextInt(); x=sc.nextInt(); flag=0; for(i=0;i<n;i++) { a[i]=sc.nextInt(); } for(i=0;i<n;i++) { b[i]=sc.nextInt(); } /* Arrays.sort(a,0,n-1,Collections.reverseOrder()); */ obj.reverse(b,n); /* * for(i=0;i<n;i++) { System.out.print(b[i]); } */ for(i=0;i<n;i++) { if(a[i]+b[i]>x) { flag=1; } } if(flag==1) { System.out.println("No"); } else { System.out.println("Yes"); } } } private void reverse(Integer[] arr,int n) { int temp; for(int i=0;i<n/2;i++) { temp=arr[i]; arr[i]=arr[n-1-i]; arr[n-1-i]=temp; } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
1e9dc12bb4e5021921e0f21ca1126540
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t--!=0){ int n = sc.nextInt(); int x = sc.nextInt(); int a[] = new int[n]; int b[] = new int[n]; boolean f = false; for(int i=0;i<n;i++){ a[i] = sc.nextInt(); // if(a[i] == x) // f= false; }for(int i=0;i<n;i++){ b[i] = sc.nextInt(); // if(b[i]==x) // f = false; } // if(!f) // System.out.println("No"); // else{ Arrays.sort(a); Arrays.sort(b); for(int i=0;i<n;i++){ if(a[i]+b[n-i-1]>x) { f= true; break; } } if(f) System.out.println("No"); else System.out.println("Yes"); } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
5ee678ca45ff21374b91ad6d08f76f96
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; public class C680 { public static void main(String[] args)throws IOException { BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in)); int T=Integer.parseInt(bReader.readLine()); StringBuilder sBuilder=new StringBuilder(); while(T-->0) { String t1[]=bReader.readLine().split(" "); String t2[]=bReader.readLine().split(" "); String t3[]=bReader.readLine().split(" "); //Arrays.sort(t2); //Arrays.sort(t3); int n=Integer.parseInt(t1[0]); int x=Integer.parseInt(t1[1]); //System.out.println(x); //System.out.println(Integer.parseInt(t2[n-1])+Integer.parseInt(t3[n-1])); int flag=0; for(int i=0;i<n;i++) { if(x<(Integer.parseInt(t2[i])+Integer.parseInt(t3[n-1-i]))) { System.out.println("No"); flag=1; break; } } if(flag==0) System.out.println("Yes"); bReader.readLine(); } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
ebf7fc2dcb7d0a175579717751f7b2bb
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.*; public class test { public static void main (String[] args) { //code Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t>0){ t--; int n=s.nextInt(); int x = s.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=s.nextInt(); int b[]=new int[n]; for(int i=0;i<n;i++) b[i]=s.nextInt(); Arrays.sort(b); Arrays.sort(a); boolean ff = true; for(int i=0;i<n;i++){ if(a[n-i-1]+b[i]>x) { ff=false; } } if(!ff) System.out.println("No"); else System.out.println("Yes"); } } static class pq implements Comparator<int[]>{ @Override public int compare(int a[],int b[]){ return a[2]-b[2]; } } public static void queue(){ PriorityQueue<int[]> pq = new PriorityQueue<>(new pq()); int b[]={1}; int i=1,j=1; int a[]={i,j,b[0]}; pq.add(new int[]{i, j}); } public static void stack(){ Stack<Integer> s= new Stack<Integer>(); } public static int justMax(int a[],int n,int x){ int l=0,r=n-1; int ans= -1; while(l<=r){ int m=(l+r)/2; if(a[m]>x){ ans= m; r=m-1; } else{ l=m+1; } } return ans; } public static int justMin(int a[],int n,int x){ int l=0,r=n-1; int ans= -1; while(l<=r){ int m=(l+r)/2; if(a[m]>x){ r=m-1; }else{ l=m+1; ans=m; } } return ans; } public static void quickSort(int a[],int n,int l,int r){ if(l>=r) return; int p=l; for(int i=l+1;i<=r;i++){ if(a[i]<a[p]){ int t=a[i]; a[i]=a[p+1]; a[p+1]=t; t=a[p];a[p]=a[p+1];a[p+1]=t;p++; } } quickSort(a,n,l,p-1); quickSort(a,n,p+1,r); } public static void mergeSort(int a[],int n,int l,int r){ if(l>=r) return ; int m=(l+r)/2; mergeSort(a,n,l,m); mergeSort(a,n,m+1,r); merge(a,n,l,r,m); } public static void merge(int a[],int n,int l,int r,int m){ int i=l,j=m+1; int b[]=new int[m-l+1]; int c[]=new int[r-m]; for(int k=0;k<m-l+1;k++) b[k]=a[k+l]; for(int k=0;k<r-m;k++) c[k]=a[m+1+k]; int k=l;i=0;j=0; while(i<m-l+1&&j<r-m){ if(c[j]>b[i]){ a[k] = b[i]; i++; } else{ a[k]=c[j];j++; } k++; } while(i< m-l+1){ a[k]=b[i];i++;k++; } while(j<r-m){ a[k]=c[j];j++;k++; } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
2e570cbcd56be44addffdefe290bfd0c
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.io.*; import java.util.*; public class a { public static void main(String[] args) throws Exception { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); StringTokenizer st = new StringTokenizer(f.readLine()); int T = Integer.parseInt(st.nextToken()); for (int t = 0; t < T; t++) { st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); int[] arr1 = new int[n]; int[] arr2 = new int[n]; st = new StringTokenizer(f.readLine()); for (int i = 0; i < n; i++) { arr1[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(f.readLine()); for (int i = 0; i < n; i++) { arr2[i] = Integer.parseInt(st.nextToken()); } Arrays.sort(arr1); Arrays.sort(arr2); boolean works = true; for (int i = 0; i < n; i++) { works = works && (arr1[i] + arr2[n-1-i] <= x); } if (works) { out.println("Yes"); } else { out.println("No"); } f.readLine(); } out.close(); } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
aff977386452a1c4439b9fff8a287d32
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
/* ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿ ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿ ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿ ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿ ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿ ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺ ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘ ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆ ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇ ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇ ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇ ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇ ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇ ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀ ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀ ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸ ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼ ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿ ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿ ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿ ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿ */ import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Map; public class b { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws Exception{ Reader s=new Reader(); int t=s.nextInt(); for(int ie=0;ie<t;ie++) { int n=s.nextInt(); int[] arr=new int[n]; int[] brr=new int[n]; int x=s.nextInt(); for(int i=0;i<n;i++) { arr[i]=s.nextInt(); } for(int j=0;j<n;j++) { brr[j]=s.nextInt(); } Arrays.sort(arr); Arrays.sort(brr); int g=0; for(int i=0;i<n;i++) { if(arr[i]+brr[n-1-i]<=x) { }else { g=1; break; } } if(g==1) { System.out.println("No"); }else { System.out.println("Yes"); } } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output
PASSED
b39a1fd181523120bf999f57e074eb45
train_001.jsonl
1604228700
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$).
512 megabytes
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int x=sc.nextInt(); int a[]=new int[n]; int []b=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); for(int i=0;i<n;i++) b[i]=sc.nextInt(); Arrays.sort(a); Arrays.sort(b); int flag=1; for(int i=0;i<n;i++){ if(a[i]+b[n-i-1] >x){ System.out.println("No"); flag=0; break; } } if(flag==1) System.out.println("Yes"); } } }
Java
["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"]
1 second
["Yes\nYes\nNo\nNo"]
NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \leq 4$$$; $$$2 + 2 \leq 4$$$; $$$3 + 1 \leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \leq 6$$$; $$$4 + 2 \leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 &gt; 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 &gt; 5$$$.
Java 8
standard input
[ "sortings", "greedy" ]
7e765c1b0e3f3e9c44de825a79bc1da2
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 50$$$; $$$1 \leq x \leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.
800
For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \leq x$$$ holds for each $$$i$$$ ($$$1 \le i \le n$$$) or No otherwise. Each character can be printed in any case.
standard output