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
f8076f682ba107ac0112f8b8cc6bf283
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class Array_Recovery { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int [ ] arr = new int[n]; for(int i = 0 ; i < n ;i++){ arr[i] = sc.nextInt(); } // int[] arr = {1,0,2,5}; System.out.println(recover(arr,arr[0])); } } public static String recover(int[]arr, int sum ){ String ans = sum+" "; for(int i = 1 ; i<arr.length;i++){ int a = sum + arr[i]; int b = sum - arr[i]; if(a!=b && a>=0 && b>=0){ return "-1"; }else if(a>=0){ sum=a; }else{ sum=b; } ans+=sum+" "; } return ans; } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
cdbd91039c17549fe767f0153b29f022
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class ProblemB { public static void main(String[] args) { var sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] d = new int[n]; int[] a = new int[n]; for (int i = 0; i < n; i++) { d[i] = sc.nextInt(); } a[0] = d[0]; boolean flag = true; for (int i = 1; i < n; i++) { if (a[i - 1] - d[i] >= 0 && d[i] > 0) { flag = false; break; } else { a[i] = a[i - 1] + d[i]; } } if (flag) { for (int i = 0; i < n; i++) { System.out.print(a[i]+" "); }System.out.println(); } else { System.out.println("-1"); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
45dac95364ea00eaae15e8f0fe44826b
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class ProblemB { public static void main(String[] args) { var sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] d = new int[n]; int[] a = new int[n]; for (int i = 0; i < n; i++) { d[i] = sc.nextInt(); } a[0] = d[0]; boolean flag = true; for (int i = 1; i < n; i++) { if (a[i - 1] - d[i] >= 0 && d[i] > 0) { flag = false; break; } else { a[i] = a[i - 1] + d[i]; } } if (flag) { for (int i = 0; i < n; i++) { System.out.print(a[i]+" "); }System.out.println(); } else { System.out.println("-1"); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
62ae7932516a2e3a7f49d39197a1e073
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class elevator { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int d[]=new int[n]; for(int i=0;i<n;i++) { d[i]=sc.nextInt(); } int ans=d[0]; int f=0; for(int i=1;i<n;i++) { if(ans-d[i]<0 || d[i]==0) { ans=ans+d[i]; } else { f=1; break; } } if(f==1) System.out.println(-1); else { int temp=d[0]; System.out.print((temp)+" "); for(int i=1;i<n;i++) { System.out.print((d[i]+temp)+" "); temp=temp+d[i]; } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
a0c637d6fee5118fcebc9b1da9757db5
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import com.sun.security.jgss.GSSUtil; import com.sun.source.tree.ModuleTree; import org.w3c.dom.ls.LSOutput; import java.io.*; import java.security.spec.RSAOtherPrimeInfo; import java.sql.Array; import java.util.*; public class edu130 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } public static boolean func(long x,long k){ return (k-((x*x)+(x)+1))%(2*x)==0; } public static void gg(int [] arr, int l, int r, int count , int[] ans){ if(r<l){ return; } if(r==l){ ans[l]=count; return; } int m=l; for(int i=l+1;i<=r;i++){ if(arr[i]>arr[m]){ m=i; } } ans[m]=count; gg(arr,l,m-1,count+1,ans); gg(arr,m+1,r,count+1,ans); } public static void main(String[] args) { //RSRRSRSSSR try { FastReader sc = new FastReader(); FastWriter out = new FastWriter(); int t=sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int [] arr=new int[n]; int [] mnb=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); mnb[i]=arr[i]; } boolean temp=false; int gfg=0; for(int i=1;i<n;i++){ if(arr[i]==0) { arr[i]=arr[i-1]; gfg=arr[i]; } else if(arr[i]>arr[i-1]){ arr[i]=arr[i]+arr[i-1]; }else{ temp=true; break; } } if(temp){ System.out.println(-1); }else{ System.out.print(mnb[0]+" "); int gg=mnb[0]; for(int i=1;i<n;i++){ System.out.print((gg+mnb[i])+" "); gg=gg+mnb[i]; } System.out.println(); } } }catch(Exception e){ return; } } public static void gffg(int n,int [] bit){ int count=31; while(n>0){ if((n&1)==1){ bit[count]+=1; } count--; n=n>>1; } } public static void bit(int n,int[][] one){ int count=31; int m=n; while(n>0){ if((n&1)==1){ one[m][count]=one[m-1][count]+1; } else{ one[m][count]=one[m-1][count]; } count--; n=n>>1; } } private static char get(StringBuilder sb, int i,char ff,char ss) { if(i==sb.length()-1){ for(int f = 0; f<26; f++){ if((char)('a'+f)!=ff ){ return (char) ('a'+ f); } } } else if(i==sb.length()-2){ for(int f = 0; f<26; f++){ if((char)('a'+f)!=ff && (char)('a'+f)!=ss && (char)('a'+f)!=sb.charAt(i+1) ){ return (char) ('a'+ f); } } }else{ for(int f = 0; f<26; f++){ if((char)('a'+f)!=ff && (char)('a'+f)!=ss && (char)('a'+f)!=sb.charAt(i+1) && (char)('a'+f)!=sb.charAt(i+2)){ return (char) ('a'+ f); } } } return 'l'; } public static int gg(int num){ int count=0; while(num>0){ count++; num=(num>>1); } return count; } private static void swap(int[] arr, int i, int ii) { int temp=arr[i]; arr[i]=arr[ii]; arr[ii]=temp; } public static int lcm(int a,int b){ return (a/gcd(a,b))*b; } private static int gcd(int a, int b) { if(b==0)return a; return gcd(b,a%b); } static class Pair { int a; int b; Pair(int a, int b) { this.a = a; this.b = b; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
7e832bf7c84044e289d6810be4db1774
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; // B. Array Recovery public class Main { static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int test_case = scanner.nextInt(); scanner.nextLine(); while (test_case > 0) { solve(); test_case--; } } private static void solve() { int n = scanner.nextInt(); int[] d = new int[n]; for (int i = 0; i < n; i++) { d[i] = scanner.nextInt(); } if (n == 1) { System.out.println(d[0]); return; } for (int i = 1; i < n; i++) { if ((d[i - 1] - d[i] >= 0) && d[i] != 0) { System.out.println(-1); return; } else { d[i] = d[i - 1] + d[i]; } } for (int i = 0; i < n; i++) { System.out.print(d[i] + " "); } System.out.println(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
b95079884113ea67de02aa1101a6acdb
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; /* Name of the class has to be "Main" only if the class is public. */ public class Array_Recovery { public static Scanner scn = new Scanner(System.in); public static void helper() { int n = scn.nextInt(); int[] arr = new int[n]; for(int i=0; i<n ;i++){ arr[i] = scn.nextInt(); } int prev = arr[0]; for(int i=1; i<n; i++){ if(arr[i]<prev&& arr[i]!=0){ System.out.println(-1); return; } else{ if(arr[i]!=0){ prev = arr[i]; } } } int flag = 0; int[] ans = new int[n]; ans[0] = arr[0]; for(int i=1; i<n; i++){ if(((arr[i]-ans[i-1])<=0) && (arr[i]!=0)){ flag = 1; } ans[i] = arr[i]+ans[i-1]; } if(flag==1){ System.out.println(-1); flag=0; return; } for(int i=0; i<n; i++){ System.out.print(ans[i]+" "); } System.out.println(); } public static void main(String[] args) throws java.lang.Exception { int tc = scn.nextInt(); while (tc-- > 0) { helper(); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
6240691a8bbe441ad49723a5cb14b2b6
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class ArrayRecovery { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int arr[]= new int[n]; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } int ans[] = new int[n]; ans[0]=arr[0]; boolean flag = false; for(int i=1;i<n;i++){ int a = ans[i-1]+arr[i]; int b = ans[i-1]-arr[i]; if(a>=0&&b>=0&&a!=b){ flag = true; break; }else{ if(a>0) { ans[i]=a; }else{ ans[i]=b; } } } if(flag){ System.out.println(-1); }else{ for(int x:ans){ System.out.print(x+" "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
c8d8b6777c146b3931eed50cc0c6bc4c
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
/* _oo0oo_ o8888888o 88" . "88 (| -_- |) 0\ = /0 ___/`---'\___ .' \\| |// '. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' |_/ | \ .-\__ '-' ___/-. / ___'. .' /--.--\ `. .'___ ."" '< `.___\_<|>_/___.' >' "". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `_. \_ __\ /__ _/ .-` / / =====`-.____`.___ \_____/___.-`___.-'===== `=---=' */ import java.io.*; import java.util.ArrayList; import java.util.Collections; /* Name of the class has to be "Main" only if the class is public. */ public class B { static FastScanner sc= new FastScanner(); public static void main (String[] args) throws Exception { // your code goes here StringBuilder sb = new StringBuilder(); int t = sc.nextInt(); outer:while(t-->0){ int N = sc.nextInt(); int A[] = sc.nextInts(N); int sum=A[0]; for(int i=1; i<N; i++) { if(A[i]==0)continue; if(sum-A[i]>=0) { System.out.println(-1); continue outer; } sum+=A[i]; } sum=0; for(int a:A) { sum+=a; System.out.print(sum+" "); } System.out.println(); } System.out.println(sb); } public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static long gcd(long a, long b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } public static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
a81f4a802a1447372bcae536ec5fe614
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Array_Recovery { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t= sc.nextInt(); while(t>0){ int n = sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } ArrayList<Integer> list = ans(arr); for(int i:list){ System.out.print(i+" "); } System.out.println(); t--; } //int d[]= {0,0,0,0}; } private static ArrayList<Integer> ans(int[] d) { ArrayList<Integer> list = new ArrayList<Integer>(); int a1=d[0]; list.add(a1); int i=1; int pre = a1; while(i<d.length){ int num1= pre-d[i]; int num2= pre+d[i]; if(num1>=0 && num2>=0 && num1!=num2){ ArrayList<Integer> list1 = new ArrayList<Integer>(); list1.add(-1); return list1; }else if(num1>=0){ list.add(num1); pre= num1; }else{ list.add(num2); pre = num2; } i++; } return list; } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
df87de6cd95a6faafa3b526e617f4848
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static void main (String[] args){ // your code goes here Scanner scn = new Scanner(System.in); int t=scn.nextInt(); while(t-->0){ int n=scn.nextInt(); int[] d=new int[n]; for(int i=0;i<n;i++){ d[i]=scn.nextInt(); } int[] a=new int[n]; a[0]=d[0]; boolean flag=true; for(int i=1;i<n;i++){ int f=a[i-1]+d[i]; int s=a[i-1]-d[i]; if((f>=0 && s>=0) && (f!=s)){ System.out.println(-1); flag=false; break; }else{ if(f>=0){ a[i]=f; }else{ a[i]=s; } } } if(flag){ for(int val:a){ System.out.print(val+" "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
7bf72a9409dc4f382f80d2d993bbb5ec
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Random; import java.util.StringTokenizer; /* */ public class Main{ public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int n=fs.nextInt(); int[] a=fs.readArray(n); int[] b=new int[n]; b[0]=a[0]; int ans=0; for(int i=1;i<n;i++){ if(b[i-1]-a[i]<0 || a[i]==0){ b[i]=b[i-1]+a[i]; } else{ ans=1; break; } } if(ans>0){ out.println("-1"); } else{ for(int i:b){ out.print(i+" "); } out.println(); } } out.close(); } static int[] match(int[] a, int[] b) { int[] res=new int[a.length]; for (int i=0; i<a.length; i++) res[i]=o(a[i], b[i]); return res; } static long toL(int[] a) { long ans=0; for (int i:a) ans=ans*3+i; return ans; } static int o(int a, int b) { if (a==b) return a; return a^b^3; } static final Random random=new Random(); static final int mod=1_000_000_007; 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); } static long add(long a, long b) { return (a+b)%mod; } static long sub(long a, long b) { return ((a-b)%mod+mod)%mod; } static long mul(long a, long b) { return (a*b)%mod; } static long exp(long base, long exp) { if (exp==0) return 1; long half=exp(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials=new long[2_000_001]; static long[] invFactorials=new long[2_000_001]; static void precompFacts() { factorials[0]=invFactorials[0]=1; for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i); invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2); for (int i=invFactorials.length-2; i>=0; i--) invFactorials[i]=mul(invFactorials[i+1], i+1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k])); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
0e3a04611625e33c680020d4f8cf10b3
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.invoke.MethodHandles; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class c{ static int []f; //static int []f2; static int []size; //static int []size2; //static int []a=new int [500001]; static int max=Integer.MAX_VALUE; static long ans=0; static Set<Integer>set; static int k; static long mod= 998244353; public static void main(String []args) { MyScanner s=new MyScanner(); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int []a=new int [n]; for(int i=0;i<n;i++) { a[i]=s.nextInt(); } boolean f=true; int []d=new int [n]; int pre=0; for(int i=0;i<n;i++) { d[i]=pre+a[i]; if(i<n-1&&d[i]>=a[i+1]&&a[i+1]!=0) f=false; pre+=a[i]; } if(!f) out.println(-1); else { for(int x:d) out.print(x+" "); out.println(); } } out.close(); } public static void dfs(char [][]a,char [][]b,int x,int y,int [][]v) { if(k==0)return ; b[x][y]='.'; k--; int []dx= {0,1,0,-1}; int []dy= {1,0,-1,0}; for(int i=0;i<4;i++) { int x1=x+dx[i]; int y1=y+dy[i]; if(x1<0||x1>=a.length||y1<0||y1>=a[0].length||v[x1][y1]==1||a[x1][y1]=='#') continue; v[x1][y1]=1; dfs(a,b,x1,y1,v); } } public static void swap(int []a) { int l=0,r=a.length-1; while(l<r) { int t=a[l]; a[l]=a[r]; a[r]=t; l++;r--; } } public static boolean is(int j) { for(int i=2;i<=(int )Math.sqrt(j);i++) { if(j%i==0)return false; } return true; } public static int find (int []father,int x) { if(x!=father[x]) x=find(father,father[x]); return father[x]; } public static void union(int []father,int x,int y,int []size) { x=find(father,x); y=find(father,y); if(x==y) return ; if(size[x]<size[y]) { int tem=x; x=y; y=tem; } father[y]=x; size[x]+=size[y]; return ; } public static void shufu(int []f) { for(int i=0;i<f.length;i++) { int k=(int)(Math.random()*(f.length)); int t=f[k]; f[k]=f[0]; f[0]=t; } } public static void shufu1(long []f) { for(int i=0;i<f.length;i++) { int k=(int)(Math.random()*(f.length)); long t=f[k]; f[k]=f[0]; f[0]=t; } } public static int gcd(int x,int y) { return y==0?x:gcd(y,x%y); } public static int lcm(int x,int y) { return x*y/gcd(x,y); } /* public static void buildertree(int k,int l,int r) { if(l==r) { f[k]=a[l]; return ; } int m=l+r>>1; buildertree(k+k,l,m); buildertree(k+k+1,m+1,r); f[k]= } public static void update(int u,int l,int r,int x,int c) { if(l==x && r==x) { f[u]=c; return; } int mid=l+r>>1; if(x<=mid)update(u<<1,l,mid,x,c); else if(x>=mid+1)update(u<<1|1,mid+1,r,x,c); f[u]=Math.max(f[u+u], f[u+u+1]); } public static int query(int k,int l,int r,int x,int y) { if(x==l&&y==r) { return f[k]; } int m=l+r>>1; if(y<=m) { return query(k+k,l,m,x,y); } else if(x>m)return query(k+k+1,m+1,r,x,y); else { int i=query(k+k,l,m,x,m),j=query(k+k+1,m+1,r,m+1,y); return Math.max(j, Math.max(i+j, i)); } } public static void calc(int k,int l,int r,int x,int z) { f[k]+=z; if(l==r) { return ; } int m=l+r>>1; if(x<=m) calc(k+k,l,m,x,z); else calc(k+k+1,m+1,r,x,z); } */ public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { try { final String USERDIR = System.getProperty("user.dir"); String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", ""); cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname; final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in"); br = new BufferedReader(new InputStreamReader(fin.exists() ? new FileInputStream(fin) : System.in)); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
90c95afe39f91fc9f5112cbf4df73a96
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class ArrayRecovery { public static boolean contains(int[] d, int a) { for (int i = 0; i < d.length; i++) { if (d[i] == a) { return true; } } return false; } public static void ArrayRecovery(int n, int[] d) { int[] a = new int[n]; a[0] = d[0]; ok: for (int i = 1; i < n; i++) { int c1 = d[i] + a[i - 1]; int c2 = a[i - 1] - d[i]; if ((c1 * c2) >= 0 && (c1 != c2)) { a[i] = -1; } else { a[i] = Math.max(c1, c2); } } if (contains(a, -1)) { System.out.println(-1); } else { for (int j = 0; j < a.length; j++) { System.out.print(a[j] + " "); } System.out.println(); } } public static void main(String[] args) { Scanner w = new Scanner(System.in); int t = w.nextInt(); //number of test cases int[] p = new int[1]; p[0] = 1; for (int i = 0; i < t; i++) { int n = w.nextInt(); int[] d = new int[n]; for (int j = 0; j < n; j++) { d[j] = w.nextInt(); } ArrayRecovery(n, d); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
5dfbe6adc2ae360cf01e18b0833a19b4
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class CF1739B { public static void main(String[] args) throws FileNotFoundException { //File file = new File("D:\\projects\\dsANDalgo\\src\\main\\java\\codeforce\\problemset\\testdata\\CF1739B.txt"); Scanner in = new Scanner(System.in); //Scanner in = new Scanner(file); int t = in.nextInt(); while (t > 0){ int n = in.nextInt(); int[] d = new int[n]; int[] a = new int[n]; boolean oneArray = true; for(int i=0; i<n; i++){ d[i] = in.nextInt(); } for(int i=0; i<n; i++){ if(i == 0){ a[i] = d[i]; }else{ int x = Math.abs(d[i] + a[i-1]); int y = Math.abs(d[i] - a[i-1]); if(x != y && Math.abs(x - a[i-1]) == d[i] && Math.abs(y - a[i-1]) == d[i]) { oneArray = false; break; } a[i] = x; } } if(oneArray){ for(int i=0; i<n; i++) System.out.print(a[i] + " "); System.out.println(); }else{ System.out.println(-1); } t--; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
ba71e73f874b675d28d1c665f96163ee
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class B { static class Pair1{ Integer key=0; Integer val=0; public Pair1(int key,int val){ this.key=key; this.val=val; } } static class SortPair implements Comparator<Pair1>{ @Override public int compare(Pair1 o1, Pair1 o2) { int d1= o1.val-o1.key; int d2= o2.val-o2.key; if(d1==d2)return 0; else if(d1>d2)return 1; else return -1; } } static class SortPair1 implements Comparator<Pair1>{ @Override public int compare(Pair1 o1, Pair1 o2) { return o1.val-o2.val; } } public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); while (t-- > 0) { int n=fs.nextInt(); int arr[]=fs.readArray(n); check(n,arr); } out.close(); } public static void check(int n,int arr[]){ int res[]=new int[n]; res[0]=arr[0]; for(int i=1;i<n;i++){ if(res[i-1]>=arr[i] && arr[i]!=0){ System.out.println(-1); return; } else res[i]=Math.abs(res[i-1]+arr[i]); } for(int i:res){ System.out.print(i+" "); } System.out.println(); } /* HELPER FUNCTION's */ static final Random random = new Random(); static final int mod = 1_000_000_007; 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); } static long add(long a, long b) { return (a + b) % mod; } static long sub(long a, long b) { return ((a - b) % mod + mod) % mod; } static long mul(long a, long b) { return (a * b) % mod; } /* fast exponentiation */ static long exp(long base, long exp) { if (exp == 0) return 1; long half = exp(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } /* end of fast exponentiation */ static long[] factorials = new long[2_000_001]; static long[] invFactorials = new long[2_000_001]; static void precompFacts() { factorials[0] = invFactorials[0] = 1; for (int i = 1; i < factorials.length; i++) factorials[i] = mul(factorials[i - 1], i); invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2); for (int i = invFactorials.length - 2; i >= 0; i--) invFactorials[i] = mul(invFactorials[i + 1], i + 1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k])); } /* sort ascending and descending both */ static void sort(int[] a,int x) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); if(x==0) { Collections.sort(l); } if(x==1){ Collections.sort(l,Collections.reverseOrder()); } for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sortL(long[] a,int x) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); if(x==0) { Collections.sort(l); } if(x==1){ Collections.sort(l,Collections.reverseOrder()); } for (int i = 0; i < a.length; i++) a[i] = l.get(i); } /* sort String acc. to character */ static String sortString(String s){ char ch[]=s.toCharArray(); Arrays.sort(ch); String s1=String.valueOf(ch); return s1; } // lcm(a,b) * gcd(a,b) = a * b public static long _lcm(long a, long b) { return (a / _gcd(a, b)) * b; } // euclidean algorithm time O(max (loga ,logb)) public static long _gcd(long a, long b) { while (a > 0) { long x = a; a = b % a; b = x; } return b; } /* Pair Class implementation */ static class Pair<K, V> { K ff; V ss; public Pair(K ff, V ss) { this.ff = ff; this.ss = ss; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || this.getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return ff.equals(pair.ff) && ss.equals(pair.ss); } @Override public int hashCode() { return Objects.hash(ff, ss); } @Override public String toString() { return ff.toString() + " " + ss.toString(); } } /* pair class ends here */ /* fast input output class */ 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[] readArrayL(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
43abbfd691498a96b765ccd5163d5d26
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class Main { static final int INF = 0x3f3f3f3f; static final long LNF = 0x3f3f3f3f3f3f3f3fL; public static void main(String[] args) throws IOException { initReader(); int t=nextInt(); while (t--!=0){ int n=nextInt(); int[]b=new int[n+1]; for(int i=1;i<=n;i++){ b[i]=nextInt(); } int[]a=new int[n+1]; a[1]=b[1]; boolean ans=true; for(int i=2;i<=n;i++){ if(b[i]>a[i-1]||b[i]==0){ a[i]=b[i]+a[i-1]; }else { ans=false; break; } } if(!ans)pw.println("-1"); else { for(int i=1;i<=n;i++){ pw.print(a[i]+" "); } pw.println(); } } pw.close(); } /***************************************************************************************/ static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter pw; public static void initReader() throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = new StringTokenizer(""); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); // 从文件读写 // reader = new BufferedReader(new FileReader("test.in")); // tokenizer = new StringTokenizer(""); // pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out"))); } public static boolean hasNext() { try { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (Exception e) { return false; } return true; } public static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static String nextLine() { try { return reader.readLine(); } catch (Exception e) { return null; } } public static int nextInt() throws IOException { return Integer.parseInt(next()); } public static long nextLong() throws IOException { return Long.parseLong(next()); } public static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static char nextChar() throws IOException { return next().charAt(0); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
1d639e5aed26cfb542b785d962a9e991
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import com.sun.source.tree.Tree; import java.util.*; import java.io.*; import java.math.*; public class Codechef { static class pair{ int first; int second; pair(int first,int second){ this.first=first; this.second=second; } } static long mod=(long)1e9+7; static class Reader { BufferedReader in; StringTokenizer st; public Reader() { in = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } public String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } public String next() throws IOException { while (!st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } static Reader in = new Reader(); static PrintWriter out = new PrintWriter(System.out); // static Reader in = new Reader();static PrintWriter out = new PrintWriter(System.out);static long mod = (long)(1e9+7); static class TreeMultiset<T> {NavigableMap<T,Integer> h; public TreeMultiset() {h = new TreeMap<>();}private boolean has(T key) {return h.containsKey(key);} private void add(T key) {h.put(key,h.getOrDefault(key,0)+1);}private int get(T key) {return h.get(key);} public void del(T key) {if(h.containsKey(key)) {if(h.get(key)==1) h.remove(key);else h.put(key,h.getOrDefault(key,0)-1);}} private T down(T key) { Map.Entry<T, Integer> val;val=h.lowerEntry(key );if(val!=null) {return val.getKey();}return null;} public T up(T key) { Map.Entry<T, Integer> val;val=h.ceilingEntry(key);if(val!=null) {return val.getKey();}return null;} public int size(){int s=0; for(int k:h.values()) s+=k; return s; }} //-----------------------------INPUT-------------------------------------------------// public static int ni() throws IOException {return in.nextInt();} public static long nl() throws IOException {return in.nextLong();} public static String rl() throws IOException {return in.next();} public static char nc() throws IOException {return in.next().charAt(0);} //----------------------------ARRAYS INPUT--------------------------------------------// public static int[] ai(long n) throws IOException {int[] arr = new int[(int)n]; for (int i = 0; i < n; i++) {arr[i] = in.nextInt();}return arr;} public static long[] al(long n) throws IOException {long[] arr = new long[(int)n]; for (int i = 0; i < n; i++) {arr[i] = in.nextLong();}return arr;} public static char[] ac() throws IOException {String s = in.next();return s.toCharArray();} //----------------------------Print---------------------------------------------------// public static void pt(int[] arr) {for (int j : arr) out.print(j + " ");out.println();} public static void pt(long[] arr) {for (long l : arr) out.print(l + " ");out.println();} public static void pt(char[] arr) {for (char l : arr) out.print(l );out.println();} //--------------------------------------------Other Functions----------------------------------------------------// public static long gcd(long a, long b) {BigInteger x = BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)); return Long.parseLong(String.valueOf(x));} public static long expo(long a, long b) {long res = 1; while (b > 0) {if ((b & 1) != 0) res = res * a;a = a * a;b >>= 1;}return res;} public static long modexp(long a, long b) {long res = 1; while (b > 0) {if ((b & 1) != 0) res = (res * a % mod) % mod;a = (a % mod * a % mod) % mod;b >>= 1;}return res % mod;} public static int[] permute(int n) {int[] arr = new int[n];for (int i = 0; i < n; i++) {arr[i] = i;}return arr;} public static long min(long[] arr) {long min = Long.MAX_VALUE;for (long l : arr) {if (l < min) min = l;}return min;} public static long max(long[] arr) {long max = Long.MIN_VALUE;for (long l : arr) {if (l > max) max = l;}return max;} public static void sort(long[] arr) { List<Long> list = new ArrayList<>();for (long i : arr) {list.add(i);}Collections.sort(list); for (int i = 0; i < arr.length; i++) {arr[i] = list.get(i);}} public static void sort(int[] arr) { List<Integer> list = new ArrayList<>();for (int i : arr) {list.add(i);}Collections.sort(list); for (int i = 0; i < arr.length; i++) {arr[i] = list.get(i);}} public static long countfactors(long n) {long ans = 0;for (long i = 1; i * i <= n; i++) {if (n % i == 0) {ans += 2;if (n / i == i) ans--;}} return ans;} public static boolean isprime(long n) {for (long i = 2; i * i <= n; i++) {if (n % i == 0) return false;}return true;} public static long [] copy(long[] arr) {long []brr=new long[arr.length];System.arraycopy(arr, 0, brr, 0, arr.length);return brr;} public static long countDigit(long n) {long count = 0;while (n != 0) {n = n / 10;++count;}return count;} public static List<pair> sortpair(int[] n, int[] h) { List<pair> A=new ArrayList<pair>(); for(int i=0;i<n.length;i++){ A.add(new pair(h[i],n[i])); } A.sort((a, b) -> b.first - a.first); return A; } // static Scanner sc=new Scanner(System.in); //---------------------------------------------------------------------------------------------------------------------------------------// public static void Khud_Bhi_Krle_Kuch() throws IOException { int n=ni(); int arr[]=ai(n); int i=1; int drr[]=new int[n]; drr[0]=arr[0]; for(;i<=n-1;i++) { long s1=drr[i-1]+arr[i]; long s2=drr[i-1]-arr[i]; if(s1>=0&&s2>=0) { if(s1!=s2) break; } drr[i]=(int) s1; } if(i!=n) out.println(-1); else { out.print(arr[0]+" "); int s=arr[0]; for(i=1;i<n;i++) { s+=arr[i]; out.print(s+" "); } out.println(); } } //--------------------------------------MAIN--------------------------------------------// public static void main(String[] args) throws Exception { int t=ni();while(t--!=0) Khud_Bhi_Krle_Kuch(); out.close(); }}
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
8af51b0a36a9d3723b6a171f51933dc7
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class B { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while (t-->0){ int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int[] ans = new int[n]; ans[0] = arr[0]; boolean flag= true; for (int i = 1; i < n; i++) { if( ans[i-1] - arr[i]>=0 && arr[i]+ans[i-1]>=0 && ans[i-1] - arr[i]!=arr[i]+ans[i-1] ){ System.out.println(-1); flag = false; break ; }else{ ans[i] = ans[i-1]-arr[i]>=0?ans[i-1]-arr[i]: arr[i]+ans[i-1]; } } if(flag){ StringBuilder sb = new StringBuilder(); for (int i = 0; i < n ; i++) { sb.append(ans[i]).append(" "); } System.out.println(sb); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
30b9b55fc7d5e61a44851cab45218fcc
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353; public static void main(String args[]){ try { PrintWriter o = new PrintWriter(System.out); boolean multiTest = true; // init if(multiTest) { int t = fReader.nextInt(), loop = 0; while (loop < t) {loop++;solve(o);} } else solve(o); o.close(); } catch (Exception e) {e.printStackTrace();} } static void solve(PrintWriter o) { try { int n = fReader.nextInt(); int[] d = new int[n]; int[] a = new int[n]; for(int i=0;i<n;i++) d[i] = fReader.nextInt(); a[0] = d[0]; for(int i=1;i<n;i++) { if(d[i] > 0 && d[i] <= a[i-1]) { o.println("-1"); return; } a[i] = a[i-1] + d[i]; } for(int i=0;i<n;i++) o.print(a[i] + " "); o.println(); } catch (Exception e) { e.printStackTrace(); } } public static int upper_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) <= val) l = mid + 1; else r = mid; } return l; } public static int lower_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) < val) l = mid + 1; else r = mid; } return l; } public static long gcd(long a, long b){ return b == 0 ? a : gcd(b, a%b); } public static long lcm(long a, long b){ return a / gcd(a,b)*b; } public static boolean isPrime(long x){ boolean ok = true; for(long i=2;i<=Math.sqrt(x);i++){ if(x % i == 0){ ok = false; break; } } return ok; } public static void reverse(int[] array){ reverse(array, 0 , array.length-1); } public static void reverse(int[] array, int left, int right) { if (array != null) { int i = left; for(int j = right; j > i; ++i) { int tmp = array[j]; array[j] = array[i]; array[i] = tmp; --j; } } } public static long qpow(long a, long n){ long ret = 1l; while(n > 0){ if((n & 1) == 1){ ret = ret * a % mod; } n >>= 1; a = a * a % mod; } return ret; } public static class DSU { int[] parent; int[] size; int n; public DSU(int n){ this.n = n; parent = new int[n]; size = new int[n]; for(int i=0;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int p){ while(parent[p] != p){ parent[p] = parent[parent[p]]; p = parent[p]; } return p; } public void union(int p, int q){ int root_p = find(p); int root_q = find(q); if(root_p == root_q) return; if(size[root_p] >= size[root_q]){ parent[root_q] = root_p; size[root_p] += size[root_q]; size[root_q] = 0; } else{ parent[root_p] = root_q; size[root_q] += size[root_p]; size[root_p] = 0; } n--; } public int getTotalComNum(){ return n; } public int getSize(int i){ return size[find(i)]; } } public static class FenWick { int n; long[] tree; public FenWick(int n){ this.n = n; tree = new long[n+1]; } private void add(int x, long val){ while(x <= n){ tree[x] += val; x += x&-x; } } private long query(int x){ long ret = 0l; while(x > 0){ ret += tree[x]; x -= x&-x; } return ret; } } public static class Node implements Comparable<Node> { Long fst; Integer snd; public Node(Long fst, Integer snd) { this.fst = fst; this.snd = snd; } @Override public int hashCode() { int prime = 31, res = 1; res = prime*res + fst.hashCode(); res = prime*res + snd.hashCode(); return res; } @Override public boolean equals(Object obj) { if(obj instanceof Node) { return fst.equals(((Node) obj).fst) && snd.equals(((Node) obj).snd); } return false; } @Override public int compareTo(Node node) { int result = Long.compare(fst, node.fst); return result == 0 ? Integer.compare(snd, node.snd) : result; } } public static class fReader { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tokenizer = new StringTokenizer(""); private static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());} return tokenizer.nextToken(); } public static int nextInt() throws IOException {return Integer.parseInt(next());} public static Long nextLong() throws IOException {return Long.parseLong(next());} public static double nextDouble() throws IOException {return Double.parseDouble(next());} public static char nextChar() throws IOException {return next().toCharArray()[0];} public static String nextString() throws IOException {return next();} public static String nextLine() throws IOException {return reader.readLine();} } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
a9e94c10919f71c16ed73a9edabdf575
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class BTest { public static void method1(int[] d) { boolean ok = false; int[] a = new int[d.length]; a[0] = d[0]; if(a.length >= 2) { if(a[0] >= d[1] && d[1] != 0) { ok = true; } } for (int i = 1; i < a.length; i++) { a[i] = d[i] + a[i-1]; if(i < a.length -1) { if (a[i] >= d[i + 1] && d[i+1] != 0) { ok = true; } } } if(ok) { System.out.println(-1); } else { for (int i = 0; i < a.length; i++) { if(i == a.length-1) { System.out.println(a[i]); } else { System.out.print(a[i] + " "); } } } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int[] d = new int[n]; for (int j = 0; j < n; j++) { d[j] = in.nextInt(); } method1(d); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
54235dcc6b175c56630b1d9cefbaa98a
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
// Working program using Reader Class import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.Scanner; import java.util.StringTokenizer; //====== ++++++++ +++++++ |\ /| |+++ | =============================== //====== || + | \ / | | + | =============================== //====== || +++++++ | + | |+++ | =============================== //====== || + | | | | =============================== //====== || +++++++ | | | |++++++ =============================== public class A { 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') { if (cnt != 0) { break; } else { continue; } } 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(); } } //============================================================================================== //============================================================================================== //===================Templates================================================================== //=========== |++++++++ | | |\ | ========================================== //=========== | | | | \ | ========================================== //=========== |++++++++ | | | \ | ========================================== //=========== | | | | \ | ========================================== //=========== | \ / | \ | ========================================== //=========== | \___/ | \| ========================================== static void swap(long[] arr, int i, int j) { long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static int partition(long[] arr, int low, int high) { long pivot = arr[high]; int i = (low - 1); for(int j = low; j <= high - 1; j++) { if (arr[j] < pivot) { i++; swap(arr, i, j); } } swap(arr, i + 1, high); return (i + 1); } static void quickSort(long[] arr, int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int[n1]; int R[] = new int[n2]; for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } void sort(int arr[], int l, int r) { if (l < r) { int m =l+ (r-l)/2; sort(arr, l, m); sort(arr, m + 1, r); merge(arr, l, m, r); } } //======================================Functions=============================================== //============================================================================================== //============================================================================================== //=========== +++++ ++++++ ++++++ ++++++++ ================================ //=========== / + + | + + ================================ //=========== | + + | + + ================================ //=========== | + + | + ++++++++ ================================ //=========== \ + + | + + ================================ //=========== +++++ ++++++ ++++++ ++++++++ ================================ static Reader in = new Reader(); public static void solve() throws IOException { int n = in.nextInt(); int[] d = new int[n]; int[] a = new int[n]; for(int i=0 ; i<n ; i++) d[i] = in.nextInt(); a[0] = d[0]; boolean flag = true; for(int i=1 ; i<n ; i++) { int temp1 = d[i] + a[i-1]; int temp2 = a[i-1] - d[i]; if(temp1 >= 0 && temp2 >= 0 && temp1 != temp2) { System.out.println(-1); flag = false; break; } else { if(temp1 >= 0) a[i] = temp1; else a[i] = temp2; } } if(flag) { for(int i=0 ; i<n ; i++) System.out.print(a[i]+" "); System.out.println(); } } public static void main(String[] args) throws IOException { int t = in.nextInt(); while(t --> 0) { solve(); } } } //====================== code ================================================================== //============================================================================================== //============================================================================================== //==============================================================================================
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
c1d76b9fc1195096f9794ee981e7079f
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int tests = in.nextInt(); for (int i = 0;i < tests;i++) { solve (in, out); } } 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) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } // main code ? static void solve (FastScanner in, PrintWriter out) { int n = in.nextInt(), a[] = new int[n]; boolean many = false; a[0] = in.nextInt(); for(int i = 1;i < a.length;i++) { int diff = in.nextInt(); int x = a[i-1] - diff, y = a[i-1]+diff; if(x != y && x >= 0) { many = true; } else { if(x >= 0) { a[i] = x; } else { a[i] = y; } } } if(many) { out.println(-1); } else { for(int x : a) out.print(x + " "); out.println(); } out.flush(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
7c26a9be1b79946c43ad9c58f24939b8
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
// import java.lang.reflect.Array; // import java.math.BigInteger; // import java.nio.channels.AcceptPendingException; // import java.nio.charset.IllegalCharsetNameException; // import java.util.Collections; // import java.util.logging.SimpleFormatter; // import java.util.regex.Matcher; // import java.util.regex.Pattern; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.*; /* docstring*/ public class Main { static Templates.FastScanner sc = new Templates.FastScanner(); static PrintWriter fop = new PrintWriter(System.out); public static void main(String[] args) { try { // A(); B(); // C(); // D(); // E(); } catch (Exception e) { return; } } /* docstring*/ static void A() throws IOException{ int T = Integer.parseInt(sc.next()); while (T-- > 0) { int n = Integer.parseInt(sc.next()); int m = Integer.parseInt(sc.next()); if(n == 1 || m == 1){ System.out.println(n+" "+m); } else{ System.out.println(((n/2)+1)+" "+((m/2)+1)); } } fop.flush(); fop.close(); } /* docstring*/ static void B() throws IOException{ int T = Integer.parseInt(sc.next()); while (T-- > 0) { int n = Integer.parseInt(sc.next()); int d_arr[] = sc.readArray(n); int arr[] = new int[n]; boolean flag = false; arr[0] = d_arr[0]; for(int i=1;i<n;i++){ int val1 = Math.abs(arr[i-1] + d_arr[i]); int val2 = Math.abs(arr[i-1] - d_arr[i]); int ans1 = Math.abs(val1-arr[i-1]); int ans2 = Math.abs(val2-arr[i-1]); if((val1!=val2)){ if((ans1==d_arr[i] && ans2==d_arr[i])){ flag = true; break; } } arr[i] = val1; } if(flag) System.out.print(-1); else{ for(int i=0;i<n;i++){ System.out.print(arr[i]+" "); } } System.out.println(); } fop.flush(); fop.close(); } /* docstring*/ static void C() throws IOException{ int T = Integer.parseInt(sc.next()); while (T-- > 0) { // Write Your Code... } fop.flush(); fop.close(); } /* docstring*/ static void D() throws IOException{ int T = Integer.parseInt(sc.next()); while (T-- > 0) { // Write Your Code... } fop.flush(); fop.close(); } /* docstring*/ static void E() throws IOException{ int T = Integer.parseInt(sc.next()); while (T-- > 0) { // Write Your Code... } fop.flush(); fop.close(); } } class Templates { // int tree[] = new int[4*n+1] ; // BuiltTree(A , 0 , n-1 , tree , 1); // int lazy[] = new int[4*n + 1] ; // // updateRangeLazy(tree , lazy , 0 , n-1 , 0 , 2 , 10 , 1); // updateRangeLazy(tree , lazy , 0 , n-1 , 0 , 4 , 10 , 1); // // fop.println(querylazy(tree , lazy , 0 , n-1 , 1 ,1 ,1)); // // updateRangeLazy(tree, lazy , 0 , n-1 , 10 , 4 , 3 ,1); // fop.println(querylazy(tree , lazy , 0 , n-1 , 3 , 5 , 1 )); 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[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } // segment tree // BuiltTree(A , 0 , n-1 , tree , 1); static void BuiltTree(int A[], int s, int e, int tree[], int index) { if (s == e) { tree[index] = A[s]; return; } int mid = (s + e) / 2; BuiltTree(A, s, mid, tree, 2 * index); BuiltTree(A, mid + 1, e, tree, 2 * index + 1); tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]); return; } static int query(int tree[], int ss, int se, int qs, int qe, int index) { // complete overlap if (ss >= qs && se <= qe) return tree[index]; // no overlap if (qe < ss || qs > se) return Integer.MAX_VALUE; // partial overlap int mid = (ss + se) / 2; int left = query(tree, ss, mid, qs, qe, 2 * index); int right = query(tree, mid + 1, se, qs, qe, 2 * index + 1); return Math.min(left, right); } static void update(int tree[], int ss, int se, int i, int increment, int index) { // i is the index of which we want to update the value if (i > se || i < ss) return; if (ss == se) { tree[index] += increment; return; } int mid = (ss + se) / 2; update(tree, ss, mid, i, increment, 2 * index); update(tree, mid + 1, se, i, increment, 2 * index + 1); tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]); } static void updateRange(int tree[], int ss, int se, int l, int r, int inc, int index) { if (l > se || r < ss) return; if (ss == se) { tree[index] += inc; return; } int mid = (ss + se) / 2; updateRange(tree, ss, mid, l, r, inc, 2 * index); updateRange(tree, mid + 1, se, l, r, inc, 2 * index + 1); tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]); return; } // Lazy rnage Update static void updateRangeLazy(int tree[], int lazy[], int ss, int se, int l, int r, int increment, int index) { if (lazy[index] != 0) { tree[index] += lazy[index]; if (ss != se) { lazy[2 * index] += lazy[index]; lazy[2 * index + 1] += lazy[index]; } lazy[index] = 0; } if (ss > r && se < l) return; if (ss >= l && se <= r) { tree[index] += increment; if (ss != se) { lazy[2 * index] += increment; lazy[2 * index + 1] += increment; } return; } int mid = (ss + se) / 2; updateRange(tree, ss, mid, l, r, increment, 2 * index); updateRange(tree, mid + 1, se, l, r, increment, 2 * index + 1); tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]); } // min lazy query static int querylazy(int tree[], int lazy[], int ss, int se, int qs, int qe, int index) { if (lazy[index] != 0) { tree[index] += lazy[index]; if (ss != se) { lazy[2 * index] += lazy[index]; lazy[2 * index + 1] += lazy[index]; } lazy[index] = 0; } if (ss > qe || se < qs) return Integer.MAX_VALUE; if (ss >= qs && se <= qe) return tree[index]; int mid = (ss + se) / 2; int left = querylazy(tree, lazy, ss, mid, qs, qe, 2 * index); int right = querylazy(tree, lazy, mid + 1, se, qs, qe, 2 * index + 1); return Math.min(left, right); } static void sieve(int n) { boolean[] flag = new boolean[n]; for (int i = 2; i * i < n; i++) { if (flag[i]) continue; else for (int j = i * i; j <= n; j += i) { flag[j] = true; } } } static int gcd(int a, int b) { if (b > a) { int tenp = b; b = a; a = tenp; } int temp = 0; while (b != 0) { a %= b; temp = b; b = a; a = temp; } return a; } static long gcdl(long a, long b) { if (b > a) { long tenp = b; b = a; a = tenp; } long temp = 0; while (b != 0) { a %= b; temp = b; b = a; a = temp; } return a; } 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); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
a10ec370691e4fe84a546154dd596e19
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class ArrayRecovery { 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 ignored) { } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); pls: while (t-- > 0) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < n - 1; i++) { if (a[i + 1] <= a[i] && a[i + 1] != 0) { System.out.println(-1); continue pls; } else { a[i + 1] = a[i] + a[i + 1]; } } for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } System.out.println(); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
3cd745fe905705f32782e51d63a76f84
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class cp { public static void main(String[] args) { int t = inp(); while(t-->0){ int n = inp(); var a = inp(n); var ans = true; var d = new int[n]; d[0]=a[0]; for(int i=1;i<n;++i){ d[i]=d[i-1]+a[i]; } for(int i=1;i<n;++i){ if(d[i-1]>=a[i]&&a[i]!=0)ans=false; } if(ans) { out(d);}else out("-1",false); } } public static long fd(long n){ return n%10; } public static long[] inpla(int n){ var o = new long[n]; for(int i=0;i<n;++i) { o[i]=sc.hasNextLong()?sc.nextLong():0L; } return o; } public static void swap(int[] a, int p, int i){ var tmp=a[p]; a[p]=a[i]; a[i]=tmp; } public static ArrayList<Integer> conv(int[] a){ var tmp = new ArrayList<Integer>(); for(int i=0;i<a.length;++i){ tmp.add(a[i]); } return tmp; } public static int intma(){ return Integer.MAX_VALUE; } public static int intmi(){ return Integer.MIN_VALUE; } public static int range(int[] ps, int n, int l, int r) { return ps[r]-(l==0?0:ps[l-1]); } public static int lower_bound(int[]a,int n,int tar) { int k = -1; for(int b=n/2;b>=1;b/=2) while(k+b<n&&a[k+b]<tar) k+=b; return k+1; } public static int upper_bound(int[] a,int n,int tar) { int k = 0; for(int b = n/2; b>=1; b/=2) while(k+b<n&&a[k+b]<=tar) k+=b; return k; } public static int mod=(int)1e9+7; public static Scanner sc = new Scanner(System.in); public static String str() { return sc.hasNextLine()?sc.nextLine():""; } public static int inp() { int n = sc.hasNextInt()?sc.nextInt():0; sc.nextLine(); return n; } public static void sort(int[] a){ Arrays.sort(a); } public static int bs(int[] a, int k) { return Arrays.binarySearch(a,k); } public static int[] inp(int n) { int[] a = new int[n]; for(int i=0;i<n;++i) a[i]=sc.hasNextInt()?sc.nextInt():0; sc.nextLine(); return a; } public static String inp(boolean s) { return sc.hasNextLine()?sc.nextLine():""; } public static void out(String s, boolean chk) { if(!chk) System.out.println(s); else System.out.print(s); } public static void nl() { System.out.print("\n"); } public static void out(int[] a) { for(int i=0;i<a.length;++i) System.out.print(a[i]+" "); System.out.print("\n"); } public static void out(long[] a) { for(int i=0;i<a.length;++i) System.out.print(a[i]+" "); System.out.print("\n"); } public static void out(int n) { System.out.println(n); } public static void out(long n) { System.out.println(n); } public static int max(int a, int b) { return Math.max(a,b); } public static long max(long a, long b) { return Math.max(a,b); } public static int min(int a, int b) { return Math.min(a,b); } public static long min(long a, long b) { return Math.min(a,b); } public static int[] cprange(int[] a, int f, int t) { return Arrays.copyOfRange(a,f,t+1); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
77b8c97dc2ebfab8d05346f6316c1575
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
/***** ---> :) Vijender Srivastava (: <--- *****/ import java.util.*; import java.lang.*; import java.io.*; public class Main { static FastReader sc =new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=(long)32768; static StringBuilder sb = new StringBuilder(); /* start */ public static void main(String [] args) { int testcases = 1; testcases = i(); // calc(); while(testcases-->0) { solve(); } out.flush(); out.close(); } static void solve() { int n = i(),a[] = input(n); if(all(a,n)) { for(int i=0;i<n;i++)p(a[i]+" "); pl(); return ; } sb = new StringBuilder(); sb.append(a[0]+" "); for(int i=1;i<n;i++) { if(a[i]<=a[i-1] && a[i]!=0) { pl(-1); return ; } a[i]+=a[i-1]; sb.append(a[i]+" "); } pl(sb); } static boolean all(int a[] ,int n) { for(int i=0;i<n;i++) { if(a[i]!=0) return false; } return true; } /* end */ 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; } } // print code start static void p(Object o) { out.print(o); } static void pl(Object o) { out.println(o); } static void pl() { out.println(""); } // print code end // static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static long[] putL(long a[]) { long A[]=new long[a.length]; for(int i=0;i<a.length;i++) { A[i]=a[i]; } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static long mod(long x) { return ((x%mod + mod)%mod); } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x) ; y = y >> 1; x = (x * x); } return res; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long[] sort(long a[]) { ArrayList<Long> arr = new ArrayList<>(); for(long i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } static int[] sort(int a[]) { ArrayList<Integer> arr = new ArrayList<>(); for(Integer i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } //pair class private static class Pair implements Comparable<Pair> { long first, second; public Pair(long f, long s) { first = f; second = s; } @Override public int compareTo(Pair p) { if (first < p.first) return 1; else if (first > p.first) return -1; else { if (second > p.second) return 1; else if (second < p.second) return -1; else return 0; } } } // segment t start static long seg[] ; static void build(long a[], int v, int tl, int tr) { if (tl == tr) { seg[v] = a[tl]; } else { int tm = (tl + tr) / 2; build(a, v*2, tl, tm); build(a, v*2+1, tm+1, tr); seg[v] = Math.min(seg[v*2] , seg[v*2+1]); } } static long query(int v, int tl, int tr, int l, int r) { if (l > r || tr < tl) return Integer.MAX_VALUE; if (l == tl && r == tr) { return seg[v]; } int tm = (tl + tr) / 2; return (query(v*2, tl, tm, l, min(r, tm))+ query(v*2+1, tm+1, tr, max(l, tm+1), r)); } static void update(int v, int tl, int tr, int pos, long new_val) { if (tl == tr) { seg[v] = new_val; } else { int tm = (tl + tr) / 2; if (pos <= tm) update(v*2, tl, tm, pos, new_val); else update(v*2+1, tm+1, tr, pos, new_val); seg[v] = Math.min(seg[v*2] , seg[v*2+1]); } } // segment t end // }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
15ec289478986e54d9d82dc57bcea568
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.util.*; public class Solution{ public static void main(String args[])throws IOException{ InputStreamReader read = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(read); int t = Integer.parseInt(br.readLine()); while(t-->0){ int n = Integer.parseInt(br.readLine()); String inp[] = br.readLine().split(" "); int nums[] = new int[n]; for(int i=0;i<nums.length;i++){ nums[i] = Integer.parseInt(inp[i]); } solver(nums); } } public static void solver(int[] nums){ int[] ans = new int[nums.length]; ans[0] = nums[0]; for(int i=1;i<nums.length;i++){ if(nums[i] > ans[i-1] || nums[i] == 0){ ans[i] = nums[i] + ans[i-1]; } else{ System.out.println(-1); return; } } for(int i=0;i<ans.length;i++){ System.out.print(ans[i]+" "); } System.out.println(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
28a12a4cfac6ab725e10a807a5a236ef
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
/* ROHAN SHARMA */ 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 B { public static final FastScanner sc = new FastScanner(); public static void main (String[] args) { solveB(); } static void solveA(){ } static void solveB(){ int T = sc.nextInt(); while (T-- > 0) { //write your code int n = sc.nextInt(); int[] arr = sc.readArray(n); int[] ans = new int[n]; ans[0] = arr[0]; boolean flag = false; for(int i = 1 ; i< n ; i++){ int x = ans[i-1] - arr[i]; int y = arr[i] + ans[i-1]; if(x >= 0 && y >= 0 && x != y){ flag = true; } ans[i] = Math.max(y , x); } if(flag){ System.out.println(-1); }else{ for(int e : ans){ System.out.print(e + " "); } System.out.println(); } } } static void solveC(){ } 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(); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } 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()); } double nextDouble(){ return Double.parseDouble(next()); } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
79abc9114d247f027b9eeec9ceb9a174
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.io.*; public class B_Array_Recovery { // For fast input output static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try {br = new BufferedReader( new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out);} catch(Exception e) { 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; } } // end of fast i/o code public static void main(String[] args) { FastReader in = new FastReader(); int t = in.nextInt(); while(t-->0){ int n = in.nextInt(); int []arr = new int[n]; int []drr = new int[n]; boolean flg = false; for(int i=0;i<n;i++){ arr[i] = in.nextInt(); } drr[0] = arr[0]; for(int i=1;i<n;i++){ if(arr[i]==0){ drr[i]=drr[i-1]; } else{ if(drr[i-1]-arr[i]>=0){ flg = true; break; }else{ drr[i]=drr[i-1]+arr[i]; } } } if(flg) System.out.println(-1); else{ // System.out.println(Arrays.toString(arr)); for(int x:drr){ System.out.print(x+" "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
70138873dbfc62bf67395fc7f387424c
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner ( System.in); int t=sc.nextInt(); for( int i=0;i<t;i++){ int n=sc.nextInt(); int arr[]=new int[n]; for( int j=0;j<n;j++){ arr[j]=sc.nextInt(); } int a[]=new int[n]; boolean flag=true; for( int j=0;j<n;j++){ if (j==0){ a[j]=arr[j]; } else { int i1=a[j-1]-arr[j]; int i2=arr[j]+a[j-1]; if( i1>=0 && i1!=i2){ flag=false; break; } else{ a[j]=arr[j]+a[j-1]; } } } if (flag==false ){ System.out.println(-1); } else { for(int j=0;j<n;j++){ System.out.print(a[j]+" "); } System.out.println(""); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
b47834999de1f9e317b1156bb1786cda
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class Main { public static boolean onearronly(int n, int[] d) { int current = d[0]; for(int i = 1; i < n; i++) { if(current - d[i] >= 0 && d[i] != 0) { return false; } current += d[i]; } return true; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int tests = scan.nextInt(); for(int test = 0; test < tests; test++) { int n = scan.nextInt(); int[] a = new int[n]; int[] d = new int[n]; for(int i = 0; i < n; i++) { d[i] = scan.nextInt(); } if(onearronly(n,d)){ a[0] = d[0]; for(int i = 1; i < n; i++) { a[i] = a[i-1] + d[i]; } for(int i = 0; i < n-1; i++) { System.out.print(a[i] + " "); } System.out.println(a[n-1]); } else { System.out.println(-1); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
de24a7680cd29831361b23ffba2189be
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.lang.*; import java.io.*; import java.util.*; public class codeforces { public static void main(String[] args) throws IOException { // if (System.getProperty("ONLINE_JUDGE") == null) { // PrintStream ps = new PrintStream(new File("output.txt")); // InputStream is = new FileInputStream("input.txt"); // System.setIn(is); // System.setOut(ps); FastScanner sc = new FastScanner(); int ttt = sc.nextInt(); for(int tt=1;tt<=ttt;tt++){ int n = sc.nextInt(); int ar[] = new int[n]; for(int i=0;i<n;i++) ar[i] = sc.nextInt(); boolean b1 = true; for(int i=1;i<n;i++){ if(ar[i]==0){ ar[i] =ar[i-1]; continue;} if(ar[i-1]>=ar[i]) { b1=false; break; } else ar[i] = ar[i]+ar[i-1]; } if(b1){ for(int i=0;i<n;i++) System.out.print(ar[i]+" "); System.out.println(); } else System.out.println("-1"); } // } } public static class Pair { public int x; public int y; public Pair(int x, int y) { this.x = x; this.y = y; } } public static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
3cc089a56101bd2579048a60e3f8003b
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
/** * Created by Himanshu **/ import java.util.*; import java.io.*; public class B1739 { static final int ALPHABET_SIZE = 26; public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); Reader s = new Reader(); int t = s.i(); while (t-- > 0) { int n = s.i(); int [] arr = s.arr(n); int [] ans = new int [n]; ans[0] = arr[0]; boolean ok = true; for (int i=1;i<n;i++) { if (arr[i] == 0) { ans[i] = ans[i-1]; } else if (arr[i] <= ans[i-1]) { ok = false; break; } else ans[i] = ans[i-1] + arr[i]; } if (!ok) out.println(-1); else { for (int a : ans) out.print(a + " "); out.println(); } } out.flush(); } public static void shuffle(long[] arr) { int n = arr.length; Random rand = new Random(); for (int i = 0; i < n; i++) { long temp = arr[i]; int randomPos = i + rand.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = temp; } } private static long phi(long n) { long result = n; for (long i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n /= i; result -= result / i; } } if (n > 1) result -= result / n; return result; } private static int gcd(int a, int b) { if(b == 0) return a; return gcd(b,a%b); } public static long nCr(long[] fact, long[] inv, int n, int r, long mod) { if (n < r) return 0; return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod; } private static void factorials(long[] fact, long[] inv, long mod, int n) { fact[0] = 1; inv[0] = 1; for (int i = 1; i <= n; ++i) { fact[i] = (fact[i - 1] * i) % mod; inv[i] = power(fact[i], mod - 2, mod); } } private static long power(long a, long n, long p) { long result = 1; while (n > 0) { if (n % 2 == 0) { a = (a * a) % p; n /= 2; } else { result = (result * a) % p; n--; } } return result; } private static long power(long a, long n) { long result = 1; while (n > 0) { if (n % 2 == 0) { a = (a * a); n /= 2; } else { result = (result * a); n--; } } return result; } private static long query(long[] tree, int in, int start, int end, int l, int r) { if (start >= l && r >= end) return tree[in]; if (end < l || start > r) return 0; int mid = (start + end) / 2; long x = query(tree, 2 * in, start, mid, l, r); long y = query(tree, 2 * in + 1, mid + 1, end, l, r); return x + y; } private static void update(int[] arr, long[] tree, int in, int start, int end, int idx, int val) { if (start == end) { tree[in] = val; arr[idx] = val; return; } int mid = (start + end) / 2; if (idx > mid) update(arr, tree, 2 * in + 1, mid + 1, end, idx, val); else update(arr, tree, 2 * in, start, mid, idx, val); tree[in] = tree[2 * in] + tree[2 * in + 1]; } private static void build(int[] arr, long[] tree, int in, int start, int end) { if (start == end) { tree[in] = arr[start]; return; } int mid = (start + end) / 2; build(arr, tree, 2 * in, start, mid); build(arr, tree, 2 * in + 1, mid + 1, end); tree[in] = (tree[2 * in + 1] + tree[2 * in]); } static class Reader { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar, numChars; public Reader() { this(System.in); } public Reader(InputStream is) { mIs = is; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = mIs.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } 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 String s() { 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 long l() { 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 int i() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double d() throws IOException { return Double.parseDouble(s()); } 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; } public int[] arr(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = i(); } return ret; } public long[] arrLong(int n) { long[] ret = new long[n]; for (int i = 0; i < n; i++) { ret[i] = l(); } return ret; } } static class TrieNode { TrieNode[] children = new TrieNode[ALPHABET_SIZE]; boolean isLeaf; boolean isPalindrome; public TrieNode() { isLeaf = false; isPalindrome = false; for (int i = 0; i < ALPHABET_SIZE; i++) children[i] = null; } } // static class pairLong implements Comparator<pairLong> { // long first, second; // // pairLong() { // } // // pairLong(long first, long second) { // this.first = first; // this.second = second; // } // // @Override // public int compare(pairLong p1, pairLong p2) { // if (p1.first == p2.first) { // if(p1.second > p2.second) return 1; // else return -1; // } // if(p1.first > p2.first) return 1; // else return -1; // } // } // static class pair implements Comparator<pair> { // int first, second; // // pair() { // } // // pair(int first, int second) { // this.first = first; // this.second = second; // } // // @Override // public int compare(pair p1, pair p2) { // if (p1.first == p2.first) return p1.second - p2.second; // return p1.first - p2.first; // } // } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
3b44d2a54df4e71fa58e7abe1816290a
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); while(n>0){ int k=sc.nextInt(); int[] arr=new int[k]; for(int i=0;i<k;i++){ arr[i]=sc.nextInt(); } ArrayList<Integer> list=new ArrayList<>(); int sum=0; for(int i=0;i<k;i++){ if(sum>=arr[i] && arr[i]!=0){ list.clear(); list.add(-1); break; } sum=sum+arr[i]; list.add(sum); } for(int i=0;i<list.size();i++) System.out.print(list.get(i)+" "); System.out.println(); n--; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
4a976b83362889d1785dcf8b4afa8a06
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import com.sun.security.jgss.GSSUtil; import java.awt.*; import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.util.List; public class MainClass { static FastScanner fs; static FastWriter fw; static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null; private static final int[][] kdir = new int[][]{{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}}; private static final int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; private static final int iMax = (int) (1e9 + 100), iMin = (int) (-1e9 - 100); private static final long lMax = (long) (1e18 + 100), lMin = (long) (-1e18 - 100); private static final int mod1 = (int) (1e9 + 7); private static final int mod2 = 998244353; public static void main(String[] args) throws IOException { fs = new FastScanner(); fw = new FastWriter(); int t = 1; t = fs.nextInt(); while (t-- > 0) { solve(); } fw.out.close(); } private static void solve() { int n = fs.nextInt(); int[] arr = readIntArray(n); int[] res = new int[n]; int d = arr[0]; for(int i = 1; i<n; i++){ if(d - arr[i] < 0 || arr[i] == 0){ d = d + arr[i]; }else{ fw.out.println(-1); return; } } int temp = arr[0]; for(int i = 1; i<n; i++){ fw.out.print(temp + " "); temp += arr[i]; } fw.out.print(temp); fw.out.println(); } // public static int[] sortArray(int[] arr){ // List<Integer> al = new ArrayList<>(); // for(int val : arr) al.add(val); // Collections.sort(al); // int i = 0; // int[] res = new int[al.size()]; // for(int val : al)res[i++] = val; // return res; // } private static class UnionFind { private final int[] parent; private final int[] rank; UnionFind(int n) { parent = new int[n + 5]; rank = new int[n + 5]; for (int i = 0; i <= n; i++) { parent[i] = i; rank[i] = 0; } } private int find(int i) { if (parent[i] == i) return i; return parent[i] = find(parent[i]); } private void union(int a, int b) { a = find(a); b = find(b); if (a != b) { if (rank[a] < rank[b]) { int temp = a; a = b; b = temp; } parent[b] = a; if (rank[a] == rank[b]) rank[a]++; } } } private static class Calc_nCr { private final long[] fact; private final long[] invfact; private final int p; Calc_nCr(int n, int prime) { fact = new long[n + 5]; invfact = new long[n + 5]; p = prime; fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = (i * fact[i - 1]) % p; } invfact[n] = pow(fact[n], p - 2, p); for (int i = n - 1; i >= 0; i--) { invfact[i] = (invfact[i + 1] * (i + 1)) % p; } } private long nCr(int n, int r) { if (r > n || n < 0 || r < 0) return 0; return (((fact[n] * invfact[r]) % p) * invfact[n - r]) % p; } } private static long gcd(long a, long b) { return (b == 0 ? a : gcd(b, a % b)); } private static long lcm(long a, long b) { return ((a * b) / gcd(a, b)); } private static long pow(long a, long b, int mod) { long result = 1; while (b > 0) { if ((b & 1L) == 1) { result = (result * a) % mod; } a = (a * a) % mod; b >>= 1; } return result; } private static long ceilDiv(long a, long b) { return ((a + b - 1) / b); } private static long getMin(long... args) { long min = lMax; for (long arg : args) min = Math.min(min, arg); return min; } private static long getMax(long... args) { long max = lMin; for (long arg : args) max = Math.max(max, arg); return max; } private static boolean isPalindrome(String s, int l, int r) { int i = l, j = r; while (j - i >= 1) { if (s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } private static List<Integer> primes(int n) { boolean[] primeArr = new boolean[n + 5]; Arrays.fill(primeArr, true); for (int i = 2; (i * i) <= n; i++) { if (primeArr[i]) { for (int j = i * i; j <= n; j += i) { primeArr[j] = false; } } } List<Integer> primeList = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (primeArr[i]) primeList.add(i); } return primeList; } private static int noOfSetBits(long x) { int cnt = 0; while (x != 0) { x = x & (x - 1); cnt++; } return cnt; } private static boolean isPerfectSquare(long num) { long sqrt = (long) Math.sqrt(num); return ((sqrt * sqrt) == num); } private static class Pair<U, V> { private final U first; private final V second; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return first.equals(pair.first) && second.equals(pair.second); } @Override public int hashCode() { return Objects.hash(first, second); } @Override public String toString() { return "(" + first + ", " + second + ")"; } private Pair(U ff, V ss) { this.first = ff; this.second = ss; } } private static void randomizeIntArr(int[] arr, int n) { Random r = new Random(); for (int i = (n - 1); i > 0; i--) { int j = r.nextInt(i + 1); swapInIntArr(arr, i, j); } } private static void randomizeLongArr(long[] arr, int n) { Random r = new Random(); for (int i = (n - 1); i > 0; i--) { int j = r.nextInt(i + 1); swapInLongArr(arr, i, j); } } private static void swapInIntArr(int[] arr, int a, int b) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } private static void swapInLongArr(long[] arr, int a, int b) { long temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } private static int[] readIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = fs.nextInt(); return arr; } private static long[] readLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = fs.nextLong(); return arr; } private static List<Integer> readIntList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(fs.nextInt()); return list; } private static List<Long> readLongList(int n) { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(fs.nextLong()); return list; } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws IOException { if (checkOnlineJudge) this.br = new BufferedReader(new FileReader("src/input.txt")); else this.br = new BufferedReader(new InputStreamReader(System.in)); this.st = new StringTokenizer(""); } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException err) { err.printStackTrace(); } } return st.nextToken(); } public String nextLine() { if (st.hasMoreTokens()) { return st.nextToken("").trim(); } try { return br.readLine().trim(); } catch (IOException err) { err.printStackTrace(); } return ""; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } private static class FastWriter { PrintWriter out; FastWriter() throws IOException { if (checkOnlineJudge) out = new PrintWriter(new FileWriter("src/output.txt")); else out = new PrintWriter(System.out); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
827f70366cd85a4a13b76628d65c06c6
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.StringTokenizer; import java.util.*; public class Solver { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t=in.nextInt(); while(t-->0){ new Task().solveB(in, out); } // new Task().solveA(in, out); // new Task().solveB(in, out); // new Task().solveC(in, out); out.close(); } static class Task { public void solveA(FastReader in, PrintWriter out) { int n=in.nextInt();int m=in.nextInt(); int[] arr = in.nextIntArray(n); int r=0; int l=0; int ans=0; int cursum=0; while (r<n && l<n){ while (cursum<m){ cursum+=arr[r]; if(cursum>=m) break; r++; } ans+=(n-r); System.out.println(l+" "+r+" "+ans); cursum-=arr[l++]; r++; System.out.println(cursum); } System.out.println(ans); } public void solveB(FastReader in, PrintWriter out) { int w = in.nextInt(); int[] d = in.nextIntArray(w); int[] a = new int[w]; //start a[0] = d[0]; for(int i=1;i<w;i++){ int a1=d[i]+a[i-1]; int a2 = a[i-1]-d[i]; //System.out.println(a1+""+a2); // if(a1<0 && a2<0) { // System.out.println(-1); // return; // } if(a1<0) a[i]=a2; else if (a2<0) a[i]=a1; else if(a1==a2) a[i]=a1; else if(a1!=a2){ System.out.println(-1);return; } } //ends for(int x:a){ System.out.print(x+" "); } System.out.println(); } public void solveC(FastReader in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[20001]; for(int i=1;i<=n;++i){ a[i]=in.nextInt(); } } } //TEMPLATE private static int sum(int[] arr){ int sum=0; for(int i:arr){ sum+=i; } return sum; } private static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } private static long lcm(long a, long b) { return (a * b) / gcd(a, b); } private static int min(int a, int b) { return a < b ? a : b; } private static int max(int a, int b) { return a > b ? a : b; } private static int min(ArrayList<Integer> list) { int min = Integer.MAX_VALUE; for (int el : list) { if (el < min) { min = el; } } return min; } private static int max(ArrayList<Integer> list) { int max = Integer.MIN_VALUE; for (int el : list) { if (el > max) { max = el; } } return max; } private static int min(int[] list) { int min = Integer.MAX_VALUE; for (int el : list) { if (el < min) { min = el; } } return min; } private static int max(int[] list) { int max = Integer.MIN_VALUE; for (int el : list) { if (el > max) { max = el; } } return max; } private static void fill(int[] array, int value) { for (int i = 0; i < array.length; i++) { array[i] = value; } } 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; } int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
80fa09048f8658998ec129cba5b30e7d
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
//some updates in import stuff import java.util.*; import java.io.*; import java.math.*; import static java.lang.Math.*; //key points learned //max space ever that could be alloted in a program to pass in cf //int[][] prefixSum = new int[201][200_005]; -> not a single array more!!! //never allocate memory again again to such bigg array, it will give memory exceeded for sure //believe in your fucking soalution and keep improving it!!! (sometimes) ///commonrst mistakes public class Main{ static int mod = (int) (Math.pow(10, 9)+7); static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 }; static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 }; static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 }; static final double eps = 1e-10; static List<Integer> primeNumbers = new ArrayList<>(); public static void main(String[] args) { MyScanner sc = new MyScanner(); //pretty important for sure - out = new PrintWriter(new BufferedOutputStream(System.out)); //dope shit output for sure //code here int test = sc.nextInt(); while(test --> 0){ int n = sc.nextInt(); int[] arr = new int[n]; for(int i= 0; i < n; i++){ arr[i] = sc.nextInt(); } boolean flag = true; int[] expected = new int[n]; expected[0] = arr[0]; for(int i = 1; i < n; i++){ int current = arr[i]; int prev = expected[i-1]; int posOne = prev - current; int posTwo = prev + current; if(posOne != posTwo && posTwo >= 0 && posOne >= 0){ flag = false; break; } expected[i] = Math.max(posOne, posTwo); } if(flag){ for(int i : expected){ out.print(i + " "); } out.println(); }else{ out.println(-1); } } out.close(); } //new stuff to learn (whenever this is need for them, then only) //Lazy Segment Trees //Persistent Segment Trees //Square Root Decomposition //Geometry & Convex Hull //High Level DP -- yk yk //String Matching Algorithms //Heavy light Decomposition //Updation Required //Fenwick Tree - both are done (sum) //Segment Tree - both are done (min, max, sum) //-----CURRENTLY PRESENT-------// //Graph //DSU //powerMODe //power //Segment Tree (work on this one) //Prime Sieve //Count Divisors //Next Permutation //Get NCR //isVowel //Sort (int) //Sort (long) //Binomial Coefficient //Pair //Triplet //lcm (int & long) //gcd (int & long) //gcd (for binomial coefficient) //swap (int & char) //reverse //primeExponentCounts //Fast input and output //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //GRAPH (basic structure) public static class Graph{ public int V; public ArrayList<ArrayList<Integer>> edges; //2 -> [0,1,2] (current) Graph(int V){ this.V = V; edges = new ArrayList<>(V+1); for(int i= 0; i <= V; i++){ edges.add(new ArrayList<>()); } } public void addEdge(int from , int to){ edges.get(from).add(to); edges.get(to).add(from); } } //DSU (path and rank optimised) public static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; Arrays.fill(rank, 1); Arrays.fill(parent,-1); this.n = n; } public int find(int curr){ if(parent[curr] == -1) return curr; //path compression optimisation return parent[curr] = find(parent[curr]); } public void union(int a, int b){ int s1 = find(a); int s2 = find(b); if(s1 != s2){ //union by size if(rank[s1] < rank[s2]){ parent[s1] = s2; rank[s2] += rank[s1]; }else{ parent[s2] = s1; rank[s1] += rank[s2]; } } } } //with mod public static long powerMOD(long x, long y) { long res = 1L; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0){ x %= mod; res %= mod; res = (res * x)%mod; } // y must be even now y = y >> 1; // y = y/2 x%= mod; x = (x * x)%mod; // Change x to x^2 } return res%mod; } //without mod public static long power(long x, long y) { long res = 1L; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0){ res = (res * x); } // y must be even now y = y >> 1; // y = y/ x = (x * x); } return res; } public static class segmentTree{ //so let's make a constructor function for this bad boi for sure!!! public long[] arr; public long[] tree; //COMPLEXITY (normal segment tree, stuff) //build -> O(n) //query -> O(logn) //update -> O(logn) //update-range -> O(n) (worst case) //simple iteration and stuff for sure public segmentTree(long[] arr){ int n = arr.length; this.arr = new long[n]; for(int i= 0; i < n; i++){ this.arr[i] = arr[i]; } tree = new long[4*n + 1]; } //pretty basic idea if you read the code once //first make child node once //then form the parent node using them public void buildTree(int s, int e, int index){ if(s == e){ tree[index] = arr[s]; return; } //recursive case int mid = (s + e)/2; buildTree(s, mid, 2 * index); buildTree(mid + 1, e, 2*index + 1); //the condition we want from children be like this tree[index] = min(tree[2 * index], tree[2 * index + 1]); return; } //definitely index based 0 query!!! //only int index = 1!! //baaki everything is simple as fuck public long query(int s, int e, int qs , int qe, int index){ //complete overlap if(s >= qs && e <= qe){ return tree[index]; } //no overlap if(qe < s || qs > e){ return Long.MAX_VALUE; } //partial overlap int mid = (s + e)/2; long left = query( s, mid , qs, qe, 2*index); long right = query( mid + 1, e, qs, qe, 2*index + 1); return min(left, right); } //gonna do range updates for sure now!! //let's do this bois!!! (solve this problem for sure) public void updateRange(int s, int e, int l, int r, long increment, int index){ //out of bounds if(l > e || r < s){ return; } //leaf node if(s == e){ tree[index] += increment; return; //behnchoda return tera baap krvayege? } //recursive case int mid = (s + e)/2; updateRange(s, mid, l, r, increment, 2 * index); updateRange(mid + 1, e, l, r, increment, 2 * index + 1); tree[index] = min(tree[2 * index], tree[2 * index + 1]); } } public static class segmentTreeLazy{ //so let's make a constructor function for this bad boi for sure!!! public long[] arr; public long[] tree; public long[] lazy; //COMPLEXITY (normal segment tree, stuff) //build -> O(n) //query-range -> O(logn) //lazy update-range -> O(logn) (imp) //simple iteration and stuff for sure public segmentTreeLazy(long[] arr){ int n = arr.length; this.arr = new long[n]; for(int i= 0; i < n; i++){ this.arr[i] = arr[i]; } tree = new long[4*n + 1]; lazy = new long[1000000]; //pretty big for no inconvenience (no?) NONONONOONON! NO fucker NO! } //pretty basic idea if you read the code once //first make child node once //then form the parent node using them public void buildTree(int s, int e, int index){ if(s == e){ tree[index] = arr[s]; return; } //recursive case int mid = (s + e)/2; buildTree(s, mid, 2 * index); buildTree(mid + 1, e, 2*index + 1); //the condition we want from children be like this tree[index] = min(tree[2 * index], tree[2 * index + 1]); return; } //definitely index based 0 query!!! //only int index = 1!! //baaki everything is simple as fuck public long queryLazy(int s, int e, int qs, int qe, int index){ //before going down resolve if it exist if(lazy[index] != 0){ tree[index] += lazy[index]; //non leaf node if(s != e){ lazy[2*index] += lazy[index]; lazy[2*index + 1] += lazy[index]; } lazy[index] = 0; //clear the lazy value at current node for sure } //no overlap if(s > qe || e < qs){ return Long.MAX_VALUE; } //complete overlap if(s >= qs && e <= qe){ return tree[index]; } //partial overlap int mid = (s + e)/2; long left = queryLazy(s, mid, qs, qe, 2 * index); long right = queryLazy(mid + 1, e, qs, qe, 2 * index + 1); return Math.min(left, right); } //update range in O(logn) -- using lazy array public void updateRangeLazy(int s, int e, int l, int r, int inc, int index){ //before going down resolve if it exist if(lazy[index] != 0){ tree[index] += lazy[index]; //non leaf node if(s != e){ lazy[2*index] += lazy[index]; lazy[2*index + 1] += lazy[index]; } lazy[index] = 0; //clear the lazy value at current node for sure } //no overlap if(s > r || l > e){ return; } //another case if(l <= s && e <= r){ tree[index] += inc; //create a new lazy value for children node if(s != e){ lazy[2*index] += inc; lazy[2*index + 1] += inc; } return; } //recursive case int mid = (s + e)/2; updateRangeLazy(s, mid, l, r, inc, 2*index); updateRangeLazy(mid + 1, e, l, r, inc, 2*index + 1); //update the tree index tree[index] = Math.min(tree[2*index], tree[2*index + 1]); return; } } //prime sieve public static void primeSieve(int n){ BitSet bitset = new BitSet(n+1); for(long i = 0; i < n ; i++){ if (i == 0 || i == 1) { bitset.set((int) i); continue; } if(bitset.get((int) i)) continue; primeNumbers.add((int)i); for(long j = i; j <= n ; j+= i) bitset.set((int)j); } } //number of divisors public static int countDivisors(long number){ if(number == 1) return 1; List<Integer> primeFactors = new ArrayList<>(); int index = 0; long curr = primeNumbers.get(index); while(curr * curr <= number){ while(number % curr == 0){ number = number/curr; primeFactors.add((int) curr); } index++; curr = primeNumbers.get(index); } if(number != 1) primeFactors.add((int) number); int current = primeFactors.get(0); int totalDivisors = 1; int currentCount = 2; for (int i = 1; i < primeFactors.size(); i++) { if (primeFactors.get(i) == current) { currentCount++; } else { totalDivisors *= currentCount; currentCount = 2; current = primeFactors.get(i); } } totalDivisors *= currentCount; return totalDivisors; } //primeExponentCounts public static int primeExponentsCount(int n) { if (n <= 1) return 0; int sqrt = (int) Math.sqrt(n); int remainingNumber = n; int result = 0; for (int i = 2; i <= sqrt; i++) { while (remainingNumber % i == 0) { result++; remainingNumber /= i; } } //in case of prime numbers this would happen if (remainingNumber > 1) { result++; } return result; } //now adding next permutation function to java hehe public static boolean next_permutation(int[] p) { for (int a = p.length - 2; a >= 0; --a) if (p[a] < p[a + 1]) for (int b = p.length - 1;; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } return false; } //finding the value of NCR in O(RlogN) time and O(1) space public static long getNcR(int n, int r) { long p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r > 0) { p *= n; k *= r; long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } return p; } //is vowel function public static boolean isVowel(char c) { return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U'); } //to sort the array with better method public 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); } //sort long public 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); } //for calculating binomialCoeff public static int binomialCoeff(int n, int k) { int C[] = new int[k + 1]; // nC0 is 1 C[0] = 1; for (int i = 1; i <= n; i++) { // Compute next row of pascal // triangle using the previous row for (int j = Math.min(i, k); j > 0; j--) C[j] = C[j] + C[j - 1]; } return C[k]; } //Pair with int int public static class Pair{ public int a; public int b; public int hashCode; Pair(int a , int b){ this.a = a; this.b = b; this.hashCode = Objects.hash(a, b); } @Override public String toString(){ return a + " -> " + b; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair that = (Pair) o; return a == that.a && b == that.b; } @Override public int hashCode() { return this.hashCode; } } //Triplet with int int int public static class Triplet{ public int a; public int b; public int c; Triplet(int a , int b, int c){ this.a = a; this.b = b; this.c = c; } @Override public String toString(){ return a + " -> " + b; } } //Shortcut function public static long lcm(long a , long b){ return a * (b/gcd(a,b)); } //let's make one for calculating lcm basically public static int lcm(int a , int b){ return (a * b)/gcd(a,b); } //int version for gcd public static int gcd(int a, int b){ if(b == 0) return a; return gcd(b , a%b); } //long version for gcd public static long gcd(long a, long b){ if(b == 0) return a; return gcd(b , a%b); } //for ncr calculator(ignore this code) public static long __gcd(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } //swapping two elements in an array public static void swap(int[] arr, int left , int right){ int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //for char array public static void swap(char[] arr, int left , int right){ char temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //reversing an array public static void reverse(int[] arr){ int left = 0; int right = arr.length-1; while(left <= right){ swap(arr, left,right); left++; right--; } } public static long expo(long a, long b, long mod) { long res = 1; while (b > 0) { if ((b & 1) == 1L) res = (res * a) % mod; //think about this one for a second a = (a * a) % mod; b = b >> 1; } return res; } //SOME EXTRA DOPE FUNCTIONS public static long mminvprime(long a, long b) { return expo(a, b - 2, b); } public static long mod_add(long a, long b, long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } public static long mod_sub(long a, long b, long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } public static long mod_mul(long a, long b, long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } public static long mod_div(long a, long b, long m) { a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m; } //O(n) every single time remember that public static long nCr(long N, long K , long mod){ long upper = 1L; long lower = 1L; long lowerr = 1L; for(long i = 1; i <= N; i++){ upper = mod_mul(upper, i, mod); } for(long i = 1; i <= K; i++){ lower = mod_mul(lower, i, mod); } for(long i = 1; i <= (N - K); i++){ lowerr = mod_mul(lowerr, i, mod); } // out.println(upper + " " + lower + " " + lowerr); long answer = mod_mul(lower, lowerr, mod); answer = mod_div(upper, answer, mod); return answer; } // long[] fact = new long[2 * n + 1]; // long[] ifact = new long[2 * n + 1]; // fact[0] = 1; // ifact[0] = 1; // for (long i = 1; i <= 2 * n; i++) // { // fact[(int)i] = mod_mul(fact[(int)i - 1], i, mod); // ifact[(int)i] = mminvprime(fact[(int)i], mod); // } //ifact is basically inverse factorial in here!!!!!(imp) public static long combination(long n, long r, long m, long[] fact, long[] ifact) { long val1 = fact[(int)n]; long val2 = ifact[(int)(n - r)]; long val3 = ifact[(int)r]; return (((val1 * val2) % m) * val3) % m; } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //-------------------------------------------------------- }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
998d19fbc9e69b52fa045279d3c1ee46
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class two { public static void main(String[] args) { // TODO Auto-generated method stub Scanner key=new Scanner(System.in); int t=key.nextInt(); for(int i=0;i<t;i++) { int f=0; int n=key.nextInt(); int []d=new int[n+1]; for(int j=1;j<=n;j++) { d[j]=key.nextInt(); } int a=d[1]; for(int g=2;g<=n;g++) { if(a-d[g]<0||d[g]==0) { a=a+d[g]; }else { System.out.println(-1); f=1; break; } } if(f!=1) { int temp=d[0]; for(int j=1;j<=n;j++) { System.out.print((d[j]+temp)+" "); temp=temp+d[j]; } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
b0ac1cf9d867af8f4a767a718baea3d5
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.util.*; public class Main { static in sc; public static void main(String[] args) throws Exception{ try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } sc = new in(); int pvp = sc.nextInt(); while(pvp-- > 0){ solve(); } } private static void solve() throws Exception{ int n = sc.nextInt(); int d[] = new int[n]; for(int i = 0; i < n; i++){ d[i] = sc.nextInt(); } int a[] = new int[n]; a[0] = d[0]; int com = a[0]; for(int i = 1; i < n; i++){ if(a[i-1] - d[i] >= 0 && d[i] != 0){ System.out.println(-1); return; } a[i] = a[i-1] + d[i]; } for(int i = 0; i < n; i++){ System.out.print(a[i] + " "); } System.out.println(); } } class in { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));; StringTokenizer in = new StringTokenizer(""); boolean hasMoreTokens() throws IOException { while (in == null || !in.hasMoreTokens()) { String line = br.readLine(); if (line == null) return false; in = new StringTokenizer(line); } return true; } String nextString() throws IOException { return hasMoreTokens() ? in.nextToken() : null; } int nextInt() throws IOException { return Integer.parseInt(nextString()); } long nextLong() throws IOException { return Long.parseLong(nextString()); } double nextDouble() throws IOException { return Double.parseDouble(nextString()); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
ff1c9683863c01d59c39e40cf1c516f9
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.io.*; public class practice { static char vowels[] = new char[]{'a', 'e', 'i', 'o', 'u'}; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new PrintWriter(System.out))); StringBuilder sb = new StringBuilder(); int t = Integer.parseInt(br.readLine()); while (t --> 0) { int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = Integer.parseInt(st.nextToken()); int ans[] = new int[n]; ans[0] = arr[0]; boolean flag = true; for (int i = 1; i < n && flag; i++) { int d1 = arr[i] + ans[i - 1]; int d2 = arr[i] * -1 + ans[i - 1]; if (d1 == d2) ans[i] = d1; else { if (d1 >= 0 && d2 >= 0) flag = false; else ans[i] = d1 > 0 ? d1 : d2; } } if (!flag) sb.append(-1); else { for (int x : ans) sb.append(x).append(" "); } sb.append("\n"); } pw.println(sb.toString().trim()); pw.close(); br.close(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
a7d6e9b7cd04e240ef5be21f0dd226dc
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner kb = new Scanner(System.in); int t = kb.nextInt(); while(t-- > 0) { int n = kb.nextInt(); int [] arr = new int[n]; for(int i=0;i<n;i++) { arr[i] = kb.nextInt(); } boolean flag = false; for(int i=1;i<n;i++) { if(arr[i] <= arr[i-1] && arr[i]!=0) { flag = true; break; } else { arr[i] = arr[i-1]+arr[i]; } } if(flag) { System.out.println(-1); } else { for(int x : arr) { System.out.print(x+" "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
f591f54636f0dc79382e284e9213ac70
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class Codeforces { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int []d = new int[n]; for (int i = 0;i<n;i++) d[i]=in.nextInt(); int[]arr = new int[n]; arr[0] = d[0]; boolean flag = false; for (int i = 1;i<n;i++){ int n1 = d[i]+arr[i-1]; int n2 = -d[i]+arr[i-1]; if((n1>=0 && n2>=0 && n1!=n2) || (n1<0 && n2<0)) { flag = true; break; } arr[i]=Math.max(n1,n2); } if (flag){ System.out.println(-1); continue; } for (int e : arr){ System.out.print(e+" "); } System.out.println(); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
7cf55796670a73acb65e6d02ad712347
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); int testcases = in.nextInt(); while (testcases-- > 0) { int n = in.nextInt(); int a[] = new int[n]; int d[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } boolean flag = false; d[0] = a[0]; for (int i = 1; i < n; i++) { if (a[i] != 0 && d[i - 1] - a[i] >= 0) { flag = true; break; } else d[i] = d[i - 1] + a[i]; } if (flag) System.out.println(-1); else { for (int i = 0; i < d.length; i++) { System.out.print(d[i] + " "); } System.out.println(); } } in.close(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
6abbbec66cdeebbf8e4bb3c592944e72
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class ECR136B { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); G: for (int i = 0; i < t; i++) { int n = in.nextInt(); int[] d = new int[n]; for (int j = 0; j < n; j++) { d[j] = in.nextInt(); } int[] a = new int[n]; a[0] = d[0]; for (int j = 1; j < n; j++) { int sc = Math.min(a[j-1] + d[j], a[j-1] - d[j]); int bc = Math.max(a[j-1] + d[j], a[j-1] - d[j]); if (sc >= 0 && sc != bc) { System.out.println(-1); continue G; } a[j] = bc; } for (int j = 0; j < n-1; j++) { System.out.print(a[j] + " "); } System.out.println(a[n-1]); } in.close(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
a32c5306684a264ee4651ac7f88101ba
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class Solution { static Scanner sc = new Scanner(System.in); public static void solve() { int size = sc.nextInt() ; int arr [] = new int [size] ; for (int i = 0; i < size; i++) { arr[i] = sc.nextInt(); } int fin[] = new int [size] ; fin [0] = arr[0] ; for (int i = 1; i < size; i++) { int a = arr[i] + fin[i-1] ; int b = -arr[i] + fin[i-1] ; if (a>=0 && b>=0 && a!=b) { System.out.println(-1); return ; } if (a>0) fin[i] = a ; else fin [i] = b ; } for (int i = 0; i < size; i++) { System.out.print(fin[i]+" "); } } public static void main(String[] args) { int tt = sc.nextInt() ; while (tt-->0) { solve() ; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
ab5259f80435d53c7fdd8f6fe2e8f87b
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class B { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { int n = scanner.nextInt(); int x[] = new int[n]; int out[] = new int[n]; for (int j = 0; j < n; j++) { x[j] = scanner.nextInt(); } boolean found = true; out[0] = x[0]; for (int j = 1; j < n; j++) { if (out[j - 1] >= x[j] && x[j] != 0) { System.out.println(-1); found = false; break; } out[j] = x[j] + out[j - 1]; } if (found) { for(int j = 0; j < n; j++) { System.out.print(out[j] + " "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
c21d180b67d5a20a3157baf4483d5f44
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.util.*; public class ProblemD { static ArrayList<Integer>res ; public static void main(String[] args) throws Exception { Scanner in = new Scanner(); StringBuilder sb = new StringBuilder(); int test = in.readInt(); while(test-->0){ int n = in.readInt(); int a[] = new int[n]; for(int i = 0;i<n;i++)a[i] = in.readInt(); int res[] = new int[n];res[0] = a[0]; boolean ok = true; for(int i =1;i<n;i++){ int first = -a[i]+res[i-1]; int second = a[i]+res[i-1]; if(first>=0 && second>=0 && first != second)ok = false; res[i] = first>=0?first:second; } if(ok){ for(int it:res)sb.append(it+" "); }else sb.append("-1"); sb.append("\n"); } System.out.println(sb); } public static int min(int a,int b,int c){ int first = a-c-1; if(first<0)first+=c; System.out.println(Math.abs(b-a-1)+" "+" "+a+" "+b+" "+(c-1)); return Math.min(Math.min(Math.abs(b-a-1), Math.abs(c-b-1)),a-1); } public static long go(int flag, LinkedList<Integer>ll[]){ int t = flag^1; LinkedList<Integer>l[] = new LinkedList[2]; for(int i = 0;i<2;i++){ l[i] = new LinkedList<>(); for(int it:ll[i])l[i].add(it); } long ans = l[flag].size()>0?l[flag].remove():-1; flag^=1; for(int i = 0;;i++){ if(l[flag].size() == 0 || ans == -1)break; if(l[flag].size()>0)ans+=2*l[flag].removeLast(); flag^=1; } while(l[0].size()>0)ans+=l[0].remove(); while(l[1].size()>0)ans+=l[1].remove(); return ans; } public static boolean vis(boolean vis[][]){ boolean ok = true; int n = vis.length,m = vis[0].length; for(int i = 0;i<n;i++){ for(int j = 0;j<m;j++){ ok&=vis[i][j]; } } return ok; } public static int begin(ArrayList<Pair>list,int l,int r,long target){ int index = -1; while(l<=r){ int mid = (l+r)/2; if(list.get(mid).y - list.get(mid).x >= target){ index = mid; l = mid+1; }else { r = mid-1; } } return index; } public static void build(int seg[],int ind,int l,int r,int a[]){ if(l == r){ seg[ind] = a[l]; return; } int mid = (l+r)/2; build(seg,(2*ind)+1,l,mid,a); build(seg,(2*ind)+2,mid+1,r,a); seg[ind] = Math.min(seg[(2*ind)+1],seg[(2*ind)+2]); } public static long gcd(long a, long b){ return b ==0 ?a:gcd(b,a%b); } public static long nearest(TreeSet<Long> list,long target){ long nearest = -1,sofar = Integer.MAX_VALUE; for(long it:list){ if(Math.abs(it - target)<sofar){ sofar = Math.abs(it - target); nearest = it; } } return nearest; } public static ArrayList<Long> factors(long n){ ArrayList<Long>l = new ArrayList<>(); for(long i = 1;i*i<=n;i++){ if(n%i == 0){ l.add(i); if(n/i != i)l.add(n/i); } } Collections.sort(l); return l; } public static void swap(int a[],int i, int j){ int t = a[i]; a[i] = a[j]; a[j] = t; } public static boolean isSorted(long a[]){ for(int i =0;i<a.length;i++){ if(i+1<a.length && a[i]>a[i+1])return false; } return true; } public static int first(ArrayList<Long>list,long target){ int index = -1; int l = 0,r = list.size()-1; while(l<=r){ int mid = (l+r)/2; if(list.get(mid) == target){ index = mid; r = mid-1; }else if(list.get(mid)>target){ r = mid-1; }else l = mid+1; } return index; } public static int last(ArrayList<Long>list,long target){ int index = -1; int l = 0,r = list.size()-1; while(l<=r){ int mid = (l+r)/2; if(list.get(mid) == target){ index= mid; l = mid+1; }else if(list.get(mid)<target){ l = mid+1; }else r = mid-1; } return index; } public static void sort(int arr[]){ ArrayList<Integer>list = new ArrayList<>(); for(int it:arr)list.add(it); Collections.sort(list); for(int i = 0;i<arr.length;i++)arr[i] = list.get(i); } static class Pair{ int x,y; public Pair(int x,int y){ this.x = x; this.y = y; } } static class Scanner{ BufferedReader br; StringTokenizer st; public Scanner(){ br = new BufferedReader(new InputStreamReader(System.in)); } public String read() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int readInt() { return Integer.parseInt(read()); } public long readLong() { return Long.parseLong(read()); } public double readDouble(){return Double.parseDouble(read());} public String readLine() throws Exception { return br.readLine(); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
be3fc95d1ad64081ea8912a8afbe0378
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.util.*; public class ProblemD { static ArrayList<Integer>res ; public static void main(String[] args) throws Exception { Scanner in = new Scanner(); StringBuilder sb = new StringBuilder(); int test = in.readInt(); while(test-->0){ int n = in.readInt(); int a[] = new int[n]; for(int i = 0;i<n;i++)a[i] = in.readInt(); int res[] = new int[n];res[0] = a[0]; boolean ok = true; for(int i =1;i<n;i++){ int first = -a[i]+res[i-1]; int second = a[i]+res[i-1]; if(first>=0 && second>=0 && a[i]>0)ok = false; res[i] = first>=0?first:second; } if(ok){ for(int it:res)sb.append(it+" "); }else sb.append("-1"); sb.append("\n"); } System.out.println(sb); } public static int min(int a,int b,int c){ int first = a-c-1; if(first<0)first+=c; System.out.println(Math.abs(b-a-1)+" "+" "+a+" "+b+" "+(c-1)); return Math.min(Math.min(Math.abs(b-a-1), Math.abs(c-b-1)),a-1); } public static long go(int flag, LinkedList<Integer>ll[]){ int t = flag^1; LinkedList<Integer>l[] = new LinkedList[2]; for(int i = 0;i<2;i++){ l[i] = new LinkedList<>(); for(int it:ll[i])l[i].add(it); } long ans = l[flag].size()>0?l[flag].remove():-1; flag^=1; for(int i = 0;;i++){ if(l[flag].size() == 0 || ans == -1)break; if(l[flag].size()>0)ans+=2*l[flag].removeLast(); flag^=1; } while(l[0].size()>0)ans+=l[0].remove(); while(l[1].size()>0)ans+=l[1].remove(); return ans; } public static boolean vis(boolean vis[][]){ boolean ok = true; int n = vis.length,m = vis[0].length; for(int i = 0;i<n;i++){ for(int j = 0;j<m;j++){ ok&=vis[i][j]; } } return ok; } public static int begin(ArrayList<Pair>list,int l,int r,long target){ int index = -1; while(l<=r){ int mid = (l+r)/2; if(list.get(mid).y - list.get(mid).x >= target){ index = mid; l = mid+1; }else { r = mid-1; } } return index; } public static void build(int seg[],int ind,int l,int r,int a[]){ if(l == r){ seg[ind] = a[l]; return; } int mid = (l+r)/2; build(seg,(2*ind)+1,l,mid,a); build(seg,(2*ind)+2,mid+1,r,a); seg[ind] = Math.min(seg[(2*ind)+1],seg[(2*ind)+2]); } public static long gcd(long a, long b){ return b ==0 ?a:gcd(b,a%b); } public static long nearest(TreeSet<Long> list,long target){ long nearest = -1,sofar = Integer.MAX_VALUE; for(long it:list){ if(Math.abs(it - target)<sofar){ sofar = Math.abs(it - target); nearest = it; } } return nearest; } public static ArrayList<Long> factors(long n){ ArrayList<Long>l = new ArrayList<>(); for(long i = 1;i*i<=n;i++){ if(n%i == 0){ l.add(i); if(n/i != i)l.add(n/i); } } Collections.sort(l); return l; } public static void swap(int a[],int i, int j){ int t = a[i]; a[i] = a[j]; a[j] = t; } public static boolean isSorted(long a[]){ for(int i =0;i<a.length;i++){ if(i+1<a.length && a[i]>a[i+1])return false; } return true; } public static int first(ArrayList<Long>list,long target){ int index = -1; int l = 0,r = list.size()-1; while(l<=r){ int mid = (l+r)/2; if(list.get(mid) == target){ index = mid; r = mid-1; }else if(list.get(mid)>target){ r = mid-1; }else l = mid+1; } return index; } public static int last(ArrayList<Long>list,long target){ int index = -1; int l = 0,r = list.size()-1; while(l<=r){ int mid = (l+r)/2; if(list.get(mid) == target){ index= mid; l = mid+1; }else if(list.get(mid)<target){ l = mid+1; }else r = mid-1; } return index; } public static void sort(int arr[]){ ArrayList<Integer>list = new ArrayList<>(); for(int it:arr)list.add(it); Collections.sort(list); for(int i = 0;i<arr.length;i++)arr[i] = list.get(i); } static class Pair{ int x,y; public Pair(int x,int y){ this.x = x; this.y = y; } } static class Scanner{ BufferedReader br; StringTokenizer st; public Scanner(){ br = new BufferedReader(new InputStreamReader(System.in)); } public String read() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int readInt() { return Integer.parseInt(read()); } public long readLong() { return Long.parseLong(read()); } public double readDouble(){return Double.parseDouble(read());} public String readLine() throws Exception { return br.readLine(); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
b2916854b25817fba9228db8e314933c
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 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[] arr = new int[n]; boolean ans = true; for(int i = 0; i < n; i++){ if(i == 0) arr[i] = sc.nextInt(); else{ int temp = sc.nextInt(); int a = arr[i - 1] + temp; int b = arr[i - 1] - temp; if(a >= 0 && b >= 0 && a != b){ ans = false; }else if(a < 0 && b < 0){ ans = false; }else{ arr[i] = Math.max(a, b); } } } if(ans){ for(int num : arr){ System.out.print(num + " "); } System.out.println(); }else System.out.println(-1); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
fccc6cdbbbbe139c06bfc194e82cb810
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class B_Array_Recovery { public static void main(String[]args) { Scanner x=new Scanner (System.in); int t=x.nextInt(); while(t-->0) { int n=x.nextInt(); int a[]=new int[n]; int f=0; for(int i=0;i<n;i++) { a[i]=x.nextInt(); } for(int i=1;i<n;i++) { int e=a[i]+a[i-1]; int g=a[i-1]-a[i]; if(e>=0 && g>=0 && e!=g) { System.out.println("-1"); f=1; break; } else { a[i]=a[i]+a[i-1]; } } if(f==0) { for(int i=0;i<n;i++) { System.out.print(a[i]+" "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
058becea1b43df84bd57dda046208dba
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java .util.*; public class RecoverArray { public static void main(String[] args) { Scanner sc=new Scanner (System.in); int f=sc.nextInt(); while(f-->0) {int n=sc.nextInt(); int[] d=new int[n]; int[] a=new int[n]; for(int i=0;i<n;i++) d[i]=sc.nextInt(); boolean hehe=true; a[0]=d[0]; int p,q; for(int i=1;i<n;i++) { p=a[i-1]+d[i]; q=a[i-1]-d[i]; if((p>0&&q<0)||(q>0&&p<0)||p==q) a[i]=Math.max(p,q); else {System.out.println(-1); hehe=false; break;} } if(hehe){ for(int i=0;i<n;i++) System.out.print(a[i]+" "); System.out.println();} } }}
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
2065b868fdf6cbb0db3b21326b97d09f
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java .util.*; public class RecoverArray { public static void main(String[] args) { Scanner sc=new Scanner (System.in); int t=sc.nextInt(); while(t-->0) {int n=sc.nextInt(); int[] d=new int[n]; int[] a=new int[n]; for(int i=0;i<n;i++) d[i]=sc.nextInt(); boolean hehe=true; a[0]=d[0]; int p,q; for(int i=1;i<n;i++) { p=a[i-1]+d[i]; q=a[i-1]-d[i]; if((p>0&&q<0)||(q>0&&p<0)||p==q) a[i]=Math.max(p,q); else {System.out.println(-1); hehe=false; break;} } if(hehe){ for(int i=0;i<n;i++) System.out.print(a[i]+" "); System.out.println();} } }}
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
f4c6831920e75fdcbe61c1eac3a42ecf
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class HelloWorld { public static void main(String args[]) { Scanner scanner=new Scanner(System.in); int t=scanner.nextInt(); while(t-->0) { int n=scanner.nextInt(); int[] nums=new int[n]; for(int i=0;i<n;i++) { nums[i]=scanner.nextInt(); } int[] a=new int[n]; a[0]=nums[0]; boolean isVallied=true; for(int i=1;i<n;i++) { if(a[i-1]-nums[i]>=0 && nums[i]!=0) { isVallied=false; } else { a[i]=a[i-1]+nums[i]; } } if(isVallied) { for(int x:a) { System.out.print(x+" "); } System.out.println(); } else { System.out.println("-1"); } } scanner.close(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
b3ef3619a09463f907bfda9f417990bd
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
/* IF I HAD CHOICE, I WOULD HAVE BEEN A PIRATE, LOL XD ; _____________############# _____________##___________## ______________#____________# _______________#____________#_## _______________#__############### _____##############______________# _____##____________#_____################ ______#______##############______________# ______##_____##___________#______________## _______#______#____PARTH___#______________# ______##_______#___________##____________# ______###########__________##___________## ___________#_#_##________###_____########_____### ___________#_#_###########_#######_______#####__######### ######_____#_#______##_#_______#_#########___######## ##___#########______##_#____#######________##### __##________##################____##______### ____#____________________________##_#____## _____#_____###_____##_____###_____###___## ______#___##_##___##_#____#_##__________# ______##____##_____###_____##__________## ________##___________________________## ___________######################### */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main{ static PrintWriter out = new PrintWriter(System.out); static FastReader sc = new FastReader(); 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) { int tes = sc.nextInt(); while(tes-->0){ solve(); } out.close(); } private static void solve() { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i]=sc.nextInt(); } for (int i = 1; i < n; i++) { if(a[i-1]-a[i]>=0 && a[i]!=0) { out.println(-1); return; } a[i]=a[i-1]+a[i]; } for (int i = 0; i < n ; i++) out.print(a[i]+" "); out.println(); } } /* Test case 0 :- */
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
883476aa1636a2ef9e249e6b0cf81124
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); //число наборов входных данных for(int i = 0; i < t; i++){ int n = in.nextInt(); // размер массива int[] d = new int[n]; int[] a = new int[n]; for(int j = 0; j < n; j++){ int tmp = in.nextInt(); d[j] = tmp; //ввод массива d } a[0] = d[0]; boolean f = true; for(int j = 1; j < n; j++){ int tmp1 = d[j] + a[j - 1]; int tmp2 = -1 * (d[j] - a[j - 1]); if(d[j] == 0){a[j] = a[j - 1];} else if(tmp1 * tmp2 > 0){ System.out.println(-1); f = false; j = n; break; } else if(tmp1 * tmp2 == 0){ System.out.println(-1); f = false; j = n; break; } else if(tmp1 * tmp2 < 0){ if(tmp1 > 0){ a[j] = tmp1;} else if(tmp2>= 0){ a[j] = tmp2;} } } if(f) { //если удалось заполнить массив а for(int j = 0; j < n; j++){ System.out.print(a[j] + " "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
8fd558f7d25c8bac46d7fdff5e4acff6
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.*; public class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } 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(); } } static class SortingString { int oldIndex; int ch; public SortingString(int oldIndex, int ch) { this.oldIndex = oldIndex; this.ch = ch; } } static class SortingComparator implements Comparator<SortingString> { public int compare(SortingString a, SortingString b) { return a.ch - b.ch; } } public static void main(String[] args) throws IOException { Reader s = new Reader(); Scanner sc = new Scanner(System.in); try { int t = s.nextInt(); // BufferedReader bu = new BufferedReader(new InputStreamReader(System.in)); // StringBuilder sb = new StringBuilder(); // String[] str = bu.readLine().split(" "); // int t = Integer.parseInt(str[0]); while (t-- > 0) { // str = bu.readLine().split(" "); // solve(str[0]); // } // int n = s.nextInt(); int[] d= new int[n]; for(int i=0;i<n;i++) d[i]=s.nextInt(); solve(n, d); } } catch (Exception e) { return; } } public static void solve(int n, int[] d) { int[] a = new int[n]; StringBuilder ans= new StringBuilder(); a[0]=d[0]; ans.append(a[0]); for(int i=1;i<n;i++){ int aip,aim; aip=d[i]+a[i-1]; aim=(-1*d[i])+a[i-1]; if(aip>=0 && aim>=0 && aip!=aim){ System.out.println(-1); return; } a[i]=Math.max(aip,aim); ans.append(" ").append(a[i]); } System.out.println(ans); // StringBuilder ans2= new StringBuilder(); // ans2.append(d[0]); // for(int i=1;i<n;i++){ // int opt1 = d[i]+a[i-1]; // int opt2 = a[i-1]-d[i]; // if(opt1>=0 && opt2>=0 && opt1 != opt2){ // System.out.println("ans2 "+-1); // return; // } // a[i] = opt1 >= opt2 ? opt1 : opt2; // ans2.append(" ").append(a[i]); // } // System.out.println("ans2 ="+ans2); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
a02858c054578ac80da4c7e930ae949b
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Double.parseDouble; import static java.lang.Math.PI; import static java.lang.Math.min; import static java.lang.System.arraycopy; import static java.lang.System.exit; import static java.util.Arrays.copyOf; import java.util.LinkedList; import java.util.List; import java.util.Iterator; import java.io.FileReader; import java.io.FileWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.Queue; import java.util.Deque; import java.util.Stack; import java.util.StringTokenizer; import java.util.Comparator; import java.lang.StringBuilder; import java.util.Collections; import java.util.*; import java.text.DecimalFormat; public class Solution { // static class Edge{ // int u, v, w; // Edge(int u, int v, int w){ // this.u = u; // this.v = v; // this.w = w; // } // } static class Pair{ int first; int second; Pair(int first, int second){ this.first = first; this.second = second; } @Override public String toString(){ return (this.first+" "+this.second); } } static class Tuple{ int first, second, third; Tuple(int first, int second, int third){ this.first = first; this.second =second; this.third = third; } @Override public String toString(){ return first+" "+second+" "+third; } } static class Point{ int x, y; Point(int x, int y){ this.x = x; this.y = y; } @Override public String toString(){ return x+" "+y; } void minus(Point p){ //pointA.minus(pointB) this.x -= p.x; this.y -= p.y; } void plus(Point p){ //pointA.plus(pointB) this.x += p.x; this.y += p.y; } long cross(Point p){//p1.crossProduct(p2), p1 is left if returned value<0 return ((long)this.x*p.y)-((long)p.x*this.y); } long triangle(Point p2, Point p3){ //p1.triangle(p2, p3); // out.println("point1: "+this+" point2: "+p2+" point3: "+p3); p2.minus(this); p3.minus(this); long cross = p2.cross(p3); p2.plus(this); p3.plus(this); return cross; } } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static int dx[] = {0, -1, 0, 1}; //left up right down static int dy[] = {-1, 0, 1, 0}; // static int[] knX = {2, 2, 1, -1, -2, -2, -1, 1}; //knight moves // static int[] knY = {-1, 1, 2, 2, 1, -1, -2, -2}; static long MOD = (long)1e9+7, INF = (int)1e9; static long LINF = (long)1e18+5; static boolean vis[]; static ArrayList<Integer> adj[]; private static void solve() throws IOException{ int N = scanInt(); int d[] = inputArray(N); int arr[] = new int[N]; arr[0] = d[0]; for(int i = 1; i<N; ++i){ if(arr[i-1]-d[i]>=0 && d[i]>0){ out.println(-1); return; } arr[i] = arr[i-1]+d[i]; } displayArray(arr); } private static int[] inputArray(int n) throws IOException { int arr[] = new int[n]; for(int i=0; i<n; ++i) arr[i] = scanInt(); return arr; } private static void displayArray(int arr[]){ for(int i = 0; i<arr.length; ++i) out.print(arr[i]+" "); out.println(); } public static void main(String[] args) { try { long startTime = System.currentTimeMillis(); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); //in = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter(new FileWriter("output.txt")); int test = scanInt(); for(int t=1; t<=test; t++){ // out.print("Case #"+t+": "); solve(); } long endTime = System.currentTimeMillis(); long totalTime = endTime - startTime; //out.println(totalTime+"---------- "+System.currentTimeMillis() ); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } static int scanInt() throws IOException { return parseInt(scanString()); } static long scanLong() throws IOException { return parseLong(scanString()); } static double scanDouble() throws IOException { return parseDouble(scanString()); } static String scanString() throws IOException { if (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static String scanLine() throws IOException { return in.readLine(); } private static void sort(int arr[]){ mergeSort(arr, 0, arr.length-1); } private static void sort(int arr[], int start, int end){ mergeSort(arr, start, end-1); } private static void mergeSort(int arr[], int start, int end){ if(start >= end) return; int mid = (start+end)/2; mergeSort(arr, start, mid); mergeSort(arr, mid+1, end); merge(arr, start, mid, end); } private static void merge(int arr[], int start, int mid, int end){ int n1 = mid - start+1; int n2 = end - mid; int left[] = new int[n1]; int right[] = new int[n2]; for(int i = 0; i<n1; ++i){ left[i] = arr[i+start]; } for(int i = 0; i<n2; ++i){ right[i] = arr[mid+1+i]; } int i = 0, j = 0, curr = start; while(i <n1 && j <n2){ if(left[i] <= right[j]){ arr[curr] = left[i]; ++i; } else{ arr[curr] = right[j]; ++j; } ++curr; } while(i<n1){ arr[curr] = left[i]; ++i; ++curr; } while(j<n2){ arr[curr] = right[j]; ++j; ++curr; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
ef691e3db4bf1e563dbcb2b0a5e4b9ee
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; 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[] arr = new int[n]; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } solve(n,arr); } } public static void solve(int n,int[] arr){ int[] ans = new int[n]; ans[0] = arr[0]; for(int i=1;i<n;i++){ int a2 = ans[i-1] - arr[i]; int A2 = arr[i] + ans[i-1]; if(a2 != A2 && a2 >= 0){ System.out.println("-1"); return; } else{ ans[i] = A2; } } for(int i=0;i<ans.length;i++){ System.out.print(ans[i] +" "); } System.out.println(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
292c075f332abf43a2f0b8c198bb103f
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class cf22 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); aa : for(int i=1;i<=T;i++) { int n = sc.nextInt(); int d[] = new int[n]; int a[] = new int[n]; d[0] = a[0] = sc.nextInt(); for(int j=1;j<n;j++) { d[j] = sc.nextInt(); a[j] = d[j]+ a[j-1]; } boolean flag = false; for(int j=1;j<n;j++) { if(a[j-1]-d[j]>=0 && d[j]!=0) { flag = true; break; } } if(flag==true) { System.out.println(-1); } else { for(int j=0;j<n;j++) { System.out.print(a[j]+" "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
f30e43d6d74ea3081de5934b23ba4f98
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class b implements Runnable{ static ContestScanner in = new ContestScanner(); static ContestPrinter out = new ContestPrinter(); public static void main(String[] args) { new Thread(null, new b(), "main", 1<<28).start(); } public void run() { int tests = in.nextInt(); for (int t = 0; t < tests; t++) { int n = in.nextInt(); int []nums = in.nextIntArray(n); int curr = nums[0]; int flag = 0; int []ans = new int [n]; ans[0] = nums[0]; for (int i = 1 ; i < n ; i++) { int poss1 = curr + nums[i] , poss2 = curr - nums[i]; if (poss1 >= 0 && poss2 >= 0 && poss1 != poss2) { flag = 1; break; } else { ans[i] = Math.max(poss1 , poss2); curr = ans[i]; } } if (flag == 1) { out.println("-1"); } else out.printArray(ans); } out.close(); } static class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width){ long[][] mat = new long[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width){ int[][] mat = new int[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width){ double[][] mat = new double[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width){ char[][] mat = new char[height][width]; for(int h=0; h<height; h++){ String s = this.next(); for(int w=0; w<width; w++){ mat[h][w] = s.charAt(w); } } return mat; } } static class ContestPrinter extends java.io.PrintWriter{ public ContestPrinter(java.io.PrintStream stream){ super(stream); } public ContestPrinter(java.io.File file) throws java.io.FileNotFoundException{ super(new java.io.PrintStream(file)); } public ContestPrinter(){ super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if(x < 0){ sb.append('-'); x = -x; } x += Math.pow(10, -n)/2; sb.append((long)x); sb.append("."); x -= (long)x; for(int i = 0;i < n;i++){ x *= 10; sb.append((int)x); x -= (int)x; } return sb.toString(); } @Override public void print(float f){ super.print(dtos(f, 20)); } @Override public void println(float f){ super.println(dtos(f, 20)); } @Override public void print(double d){ super.print(dtos(d, 20)); } @Override public void println(double d){ super.println(dtos(d, 20)); } public void printArray(int[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public void printArray(int[] array){ this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n-1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map){ this.printArray(array, " ", map); } public void printArray(long[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public void printArray(long[] array){ this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n-1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map){ this.printArray(array, " ", map); } public <T> void printArray(T[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public <T> void printArray(T[] array){ this.printArray(array, " "); } public <T> void printArray(T[] array, String separator, java.util.function.UnaryOperator<T> map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.apply(array[i])); super.print(separator); } super.println(map.apply(array[n-1])); } public <T> void printArray(T[] array, java.util.function.UnaryOperator<T> map){ this.printArray(array, " ", map); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
8642793cf0709690282806be6f2fac6b
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public final class Main { FastReader s; // PrintWriter out ; public static void main(String[] args) throws java.lang.Exception { new Main().run(); } void run() { // out = new PrintWriter(new OutputStreamWriter(System.out)); s = new FastReader(); solve(); } StringBuffer sb; void solve() { sb = new StringBuffer(); for (int T = s.nextInt(); T > 0; T--) { start(); } System.out.print(sb); } HashMap<String,Integer> x; int [] dp ; void start() { int n = s.nextInt(); long [] arr = longArr(n); long ans[] = new long[n]; ans[0] = arr[0]; dp = new int[n+1]; Arrays.fill(dp,-1); if(call(arr,ans,1,n) == 1) { for(long i : ans) { sb.append(i+" "); } sb.append("\n"); } else { sb.append("-1\n"); } } int call(long [] arr, long ans[], int i, int n) { if(i == n) { return 1; } if(dp[i] != -1) return dp[i]; long diff = arr[i]; ans[i] = ans[i-1] + diff; int ans1 = 0; ans1+= call(arr,ans,i+1,n); if(ans[i-1] -diff >=0 && diff != 0) { ans[i] = ans[i-1] - diff; ans1+= call(arr,ans,i+1,n); } dp[i] = ans1; return ans1; } void swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } class Pair { int x; int y; // String hash; public Pair(int x, int y) { this.x = x; this.y = y; // this.hash = x+" "+y; } @Override public String toString() { return ("{ x = " + this.x + " , " + "y = " + this.y + " }"); } @Override public boolean equals(Object ob) { if (this == ob) return true; if (ob == null || this.getClass() != ob.getClass()) return false; Pair p = (Pair) ob; return (p.x == this.x) && (p.y == this.y); } // @Override // public int hashCode() // { // return hash.hashCode(); // } } class LongPair { long x; long y; // String hash; public LongPair(long x, long y) { this.x = x; this.y = y; // this.hash = x+" "+y; } @Override public String toString() { return ("{ x = " + this.x + " , " + "y = " + this.y + " }"); } @Override public boolean equals(Object ob) { if (this == ob) return true; if (ob == null || this.getClass() != ob.getClass()) return false; LongPair p = (LongPair) ob; return (p.x == this.x) && (p.y == this.y); } // @Override // public int hashCode() // { // return hash.hashCode(); // } } class SegmentTree { int[] tree; int n; public SegmentTree(int[] nums) { if (nums.length > 0) { n = nums.length; tree = new int[n * 2]; buildTree(nums); } } private void buildTree(int[] nums) { for (int i = n, j = 0; i < 2 * n; i++, j++) tree[i] = nums[j]; for (int i = n - 1; i > 0; --i) tree[i] = tree[i * 2] + tree[i * 2 + 1]; } void update(int pos, int val) { pos += n; tree[pos] = val; while (pos > 0) { int left = pos; int right = pos; if (pos % 2 == 0) { right = pos + 1; } else { left = pos - 1; } // parent is updated after child is updated tree[pos / 2] = tree[left] + tree[right]; pos /= 2; } } public int sumRange(int l, int r) { // get leaf with value 'l' l += n; // get leaf with value 'r' r += n; int sum = 0; while (l <= r) { if ((l % 2) == 1) { sum += tree[l]; l++; } if ((r % 2) == 0) { sum += tree[r]; r--; } l /= 2; r /= 2; } return sum; } } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1) > 0) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int find(int dsu[], int i) { if (dsu[i] == i) return i; dsu[i] = find(dsu, dsu[i]); return dsu[i]; } void union(int dsu[], int i, int j) { int a = find(dsu, i); int b = find(dsu, j); dsu[a] = b; } 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 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 String sort(String s) { Character ch[] = new Character[s.length()]; for (int i = 0; i < s.length(); i++) { ch[i] = s.charAt(i); } Arrays.sort(ch); StringBuffer st = new StringBuffer(""); for (int i = 0; i < s.length(); i++) { st.append(ch[i]); } return st.toString(); } // long array input public long[] longArr(int len) { // long arr input long[] arr = new long[len]; String[] strs = s.nextLine().trim().split("\\s+"); for (int i = 0; i < len; i++) { arr[i] = Long.parseLong(strs[i]); } return arr; } // int arr input public int[] intArr(int len) { // long arr input int[] arr = new int[len]; String[] strs = s.nextLine().trim().split("\\s+"); for (int i = 0; i < len; i++) { arr[i] = Integer.parseInt(strs[i]); } return arr; } public void printArray(int[] A) { System.out.println(Arrays.toString(A)); } public void printArray(long[] A) { System.out.println(Arrays.toString(A)); } 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
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
f68016c15d0ec062bac4b49bd4a98379
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
//Code By KB. import java.io.*; import java.lang.annotation.Target; import java.lang.reflect.Array; import java.security.KeyStore.Entry; import java.util.*; import java.util.logging.*; import java.io.*; import java.util.logging.*; import java.util.regex.*; import javax.swing.plaf.basic.BasicBorders.SplitPaneBorder; import javax.swing.text.AbstractDocument.LeafElement; public class Codeforces { public static void main(String[] args) { FlashFastReader in = new FlashFastReader(System.in); try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));) { new Codeforces().solution(in, out); } catch (Exception e) { System.out.println(e); } } public void solution(FlashFastReader in, PrintWriter out) { try { int t = in.nextInt(); while(t-->0) { int n =in.nextInt(); int a[] = in.nextIntsInputAsArray(n); int [] b = new int[n]; b[0]=a[0]; boolean f = false; for (int i = 1; i < a.length; i++) { if (a[i]==0) { b[i]=b[i-1]; } else if (a[i]-b[i-1]<=0) { f=true ; } else { b[i] = a[i]+b[i-1]; } } if (f) { out.print(-1); } else { for (int i = 0; i < b.length; i++) { out.print(b[i]+" "); } } out.println(); } } catch (Exception exception) { out.println(exception); } } public int[][] matrixBlockSum(int[][] mat, int k) { int m = mat.length; int n =mat[0].length; int dp[][]= new int[m+1][n+1]; for (int i = 0; i < m; i++) { for (int j = 0; j < mat[0].length; j++) { if(i==0&&j==0) { dp[i+1][j+1]=mat[i][j]; } else if (i==0&&j!=0) { dp[i+1][j+1]=mat[i][j]+dp[i+1][j]; } else if (i!=0&&j==0) { dp[i+1][j+1]=mat[i][j]+dp[i][j+1]; } else { dp[i+1][j+1]=mat[i][j]+dp[i][j+1]+dp[i+1][j]-dp[i][j]; } } } for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[0].length; j++) { System.out.print(dp[i][j]); } System.out.println(); } for (int i = 0; i < m; i++) { for (int j = 0; j < mat[0].length; j++) { int r1 = Math.max(0 , i-k); int c1 = Math.max(0 , j-k); int r2 = Math.min(m-1 , i+k); int c2 = Math.min(n-1 , j+k); mat[i][j] = dp[r2][c2] - dp[r2][c1-1] - dp[r1-1][c2] + dp[r1-1][c1-1]; } } return mat; } int x=0; public int uniquePathsIII(int[][] grid) { int zeros = 0; int sx=0; int sy =0; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { if (grid[i][j]==0) { zeros++; } else if (grid[i][j]==1) { sx=i; sy=j; } } } dfsUniquePaths(zeros , sx , sy , grid); return x; } public void dfsUniquePaths(int zeros ,int i ,int j , int g[][]) { if (i<0||i>=g.length||j>=g[0].length||j<0||g[i][j]<0) { System.out.println(i+" "+j); return; } if (g[i][j]==2) { if(zeros==0) x++; return; } g[i][j]=-2; zeros--; dfsUniquePaths(zeros, i+1, j, g); dfsUniquePaths(zeros, i-1, j, g); dfsUniquePaths(zeros, i, j+1, g); dfsUniquePaths(zeros, i, j-1, g); g[i][j]=0; zeros++; } public boolean wordBreak(String s, List<String> wordDict) { HashSet<String> wd = new HashSet<>(wordDict); wordBreakCheck(s , wd ,1); return false; } int j =0; public void wordBreakCheck(String s , HashSet<String>wordDict , int i) { if(i>s.length()) { return ; } if(wordDict.contains(s.substring(0 , i))){ x=i+j; } else if (x!=0) { s=s.substring(i); i=0; j=x; } wordBreakCheck(s, wordDict, i+1); } public int uniquePaths(int m, int n) { int dp[][]= new int [m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if(i==0&&j==0) { dp[i][j]=1; } else if (i==0) { dp[i][j]=dp[i][j]+dp[i][j-1]; } else if (j==0) { dp[i][j]=dp[i][j]+dp[i-1][j]; } else { dp[i][j]+=dp[i][j-1]+dp[i-1][j]; } } } return dp[m-1][n-1]; } public int maxProfit(int[] prices) { int p = -1; return p ; } public int trap(int[] height) { int n = height.length; int left[] = new int [n]; int right[] = new int [n]; left[0] = height[0]; for (int i = 1; i <n; i++) { left[i] = Math.max(height[i], left[i-1]); } right[n-1]=height[n-1]; for (int i = n-2; i>=0 ; i--) { right[i] = Math.max(height[i], right[i+1]); } for (int i = 1; i < right.length-1; i++) { int s =Math.min(left[i] , right[i])-height[i]; if (s>0) { x+=s; } } return x; } public int maxProduct(int[] nums) { int maxSoFar = Integer.MIN_VALUE; int maxTillEnd = 0; for (int i = 0; i < nums.length; i++) { maxTillEnd*=nums[i]; if (maxSoFar<maxTillEnd) { maxSoFar=maxTillEnd; } if (maxTillEnd<=0) { maxTillEnd=1; } } return maxSoFar; } public int maximumScore(int[] nums, int[] multipliers) { int n = multipliers.length; int dp[] = new int[n+1]; for (int i = 1; i < multipliers.length; i++) { for (int j = 1; j < nums.length; j++) { dp[i] = Math.max(dp[j-1], multipliers[i-1]*nums[i-1]); } } return dp[n]; } public int islandPerimeter(int[][] grid) { for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { if(grid[i][j]==1){ dfsIslandPerimeter(i , j , grid); } } } return x; } public void dfsIslandPerimeter(int i , int j , int[][] grid) { if(i<0||i>=grid.length||j<0||j>grid[0].length) { x++; return; } if(grid[i][j]==0) { x++; return; } if(grid[i][j]==1) { return; } dfsIslandPerimeter(i+1, j, grid); dfsIslandPerimeter(i-1, j, grid); dfsIslandPerimeter(i, j+1, grid); dfsIslandPerimeter(i, j-1, grid); } public int[] findOriginalArray(int[] changed) { int n =changed.length; int m = n/2; int a[] = new int[m]; if (n<2) { return new int[]{}; } if (n%2!=0) { return new int[]{}; } TreeMap<Integer , Integer> map = new TreeMap<>(); for (int i = 0; i < changed.length; i++) { map.put(changed[i], map.getOrDefault(changed[i] ,0)+1); } int j =0; for (Map.Entry<Integer , Integer> e : map.entrySet()) { if (map.containsKey(e.getKey()*2)&&map.get(e.getKey()*2)>=1&&map.get(e.getKey())>=1) { map.put(e.getKey(), map.get(e.getKey()) -1); if(j==n-1) break; a[j++] = e.getKey(); } } return a; } public int jump(int[] nums) { int n =nums.length; int jumps =1; int i =0; int j =0; while (i<n) { int max =0; for (j=i+1 ;j<=i+nums[i];j++) { max = Math.max(max , nums[j]+j); } jumps++; i=max; } return jumps; } static void sort(int[] arr) { if (arr.length < 2) return; int mid = arr.length / 2; int[] left_half = new int[mid]; int[] right_half = new int[arr.length - mid]; // copying the elements of array into left_half for (int i = 0; i < mid; i++) { left_half[i] = arr[i]; } // copying the elements of array into right_half for (int i = mid; i < arr.length; i++) { right_half[i - mid] = arr[i]; } sort(left_half); sort(right_half); merge(arr, left_half, right_half); } static void merge(int[] arr, int[] left_half, int[] right_half) { int i = 0, j = 0, k = 0; while (i < left_half.length && j < right_half.length) { if (left_half[i] < right_half[j]) { arr[k++] = left_half[i++]; } else { arr[k++] = right_half[j++]; } } while (i < left_half.length) { arr[k++] = left_half[i++]; } while (j < right_half.length) { arr[k++] = right_half[j++]; } } public int minCostClimbingStairs(int[] cost) { int n = cost.length; int dp[] = new int[n+1]; dp[1] = cost[0]; dp[2] = cost[1]; for (int i = 3; i <=n; i++) { dp[i]=cost[i-1]+Math.min(dp[i-1], dp[i-2]); } return dp[n]; } TreeNode newAns =null; public TreeNode removeLeafNodes(TreeNode root, int target) { newAns = root; dfsRemoveNode(newAns , target); return newAns; } public void dfsRemoveNode(TreeNode root , int t) { if (root==null) { return; } if (root.val==t) { if (root.left!=null) { root=root.left; }else if (root.right!=null){ root=root.right; } else { root=null; } } dfsRemoveNode(root.left, t); dfsRemoveNode(root.right, t); } public int maxProfit(int k, int[] prices) { int n = prices.length; if (n <= 1) return 0; if (k >= n/2) { return stocks(prices, n); } int[][] dp = new int[k+1][n]; for (int i = 1; i <=k; i++) { int c= dp[i-1][0] - prices[0]; for (int j = 1; j < n; j++) { dp[i][j] = Math.max(dp[i][j-1], prices[j] + c); c = Math.max(c, dp[i-1][j] - prices[j]); } } return dp[k][n-1]; } public int stocks(int [] prices , int n ) { int maxPro = 0; for (int i = 1; i < n; i++) { if (prices[i] > prices[i-1]) maxPro += prices[i] - prices[i-1]; } return maxPro; } public List<List<Integer>> combine(int n, int k) { List<Integer> x = new ArrayList<>(); combinations( n , k , 1 ,x); return list; } public void combinations(int n , int k ,int startPos , List<Integer> x) { if (k==x.size()) { list.add(new ArrayList<>(x)); //x=new ArrayList<>(); return; } for (int i = startPos; i <=n; i++) { x.add(i); combinations(n, k, i+1, x); x.remove(x.size()-1); } } List<List<Integer>> list = new ArrayList<>(); public List<List<Integer>> permute(int[] a) { List<Integer> x = new ArrayList<>(); permutations(a , 0 ,a.length ,x ); return list ; } public void permutations(int a[] , int startPos , int l , List<Integer>x) { if (l==x.size()) { list.add(new ArrayList<>(x)); //x=new ArrayList<>(); } for (int i = 0; i <=l; i++) { if (!x.contains(a[i])) { x.add(a[i]); permutations(a, i, l, x); x.remove(x.size()-1); } } } List<String> str = new ArrayList<>(); public List<String> letterCasePermutation(String s) { casePermutations(s , 0 , s.length() , "" ); return str; } public void casePermutations(String s ,int i , int l , String curr) { if (curr.length()==l) { str.add(curr); return; } char b = s.charAt(i); curr+=b; casePermutations(s, i+1, l, curr); curr = new StringBuilder(curr).deleteCharAt(curr.length()-1).toString(); if (Character.isAlphabetic(b)&&Character.isLowerCase(b)) { curr+=Character.toUpperCase(b); casePermutations(s, i+1, l, curr); curr = new StringBuilder(curr).deleteCharAt(curr.length()-1).toString(); } else if (Character.isAlphabetic(b)) { curr+=Character.toLowerCase(b); casePermutations(s, i+1, l, curr); curr = new StringBuilder(curr).deleteCharAt(curr.length()-1).toString(); } } TreeNode ans= null; public TreeNode lcaDeepestLeaves(TreeNode root) { dfslcaDeepestLeaves(root , 0 ); return ans; } public void dfslcaDeepestLeaves(TreeNode root , int level) { if (root==null) { return; } if (depth(root.left)==depth(root.right)) { ans= root; return; } else if (depth(root.left)>depth(root.right)){ dfslcaDeepestLeaves(root.left, level+1); } else { dfslcaDeepestLeaves(root.right, level+1); } } public int depth(TreeNode root) { if (root==null) { return 0; } return 1+Math.max(depth(root.left), depth(root.right)); } TreeMap<Integer , Integer> m = new TreeMap<>(); public int maxLevelSum(TreeNode root) { int maxlevel =0; int mx = Integer.MIN_VALUE; dfsMaxLevelSum(root , 0); for (Map.Entry<Integer,Integer> e : m.entrySet()) { if (e.getValue()>mx) { mx=e.getValue(); maxlevel=e.getKey()+1; } } return maxlevel; } public void dfsMaxLevelSum(TreeNode root , int currLevel) { if (root==null) { return; } if (!m.containsKey(currLevel)) { m.put(currLevel, root.val); } else { m.put(currLevel, m.get(currLevel)+root.val); } dfsMaxLevelSum(root.left, currLevel+1); dfsMaxLevelSum(root.right, currLevel+1); } int teampPerf = 0; int teampSpeed = 0; public int maxPerformance(int n, int[] speed, int[] efficiency, int k) { int [][] map = new int[efficiency.length][2]; for (int i = 0; i < efficiency.length; i++) { map[i][0] = efficiency[i]; map[i][1] = speed[i]; } Arrays.sort(map , (e1 , e2 )-> (e2[0] - e1[0])); PriorityQueue<Integer> pq = new PriorityQueue<>(); calmax(map , speed , efficiency , pq , k); return teampPerf ; } public void calmax(int [][]map , int s[] , int e[] , PriorityQueue<Integer>pq , int k) { for (int i = 0 ; i<n ; i++) { if (pq.size()==k) { int lowestSpeed =pq.remove(); teampSpeed-=lowestSpeed; } pq.add(map[i][1]); teampSpeed+=map[i][1]; teampPerf=Math.max(teampPerf, teampSpeed*map[i][1]); } } int maxRob = 0; public int rob(int[] nums) { int one = 0; int two = 0; for (int i = 0; i < nums.length; i++) { if (i%2==0) { one+=nums[i]; } else { two+=nums[i]; } } return Math.max(one, two); } public int minSubArrayLen(int target, int[] nums) { int res =0; HashMap<Integer,Integer> map =new HashMap<>(); int sum = 0 ; for (int i = 0; i < nums.length; i++) { sum+=nums[i]; if (sum==target) { res+=1; } if (!map.containsKey(sum)) { map.put(sum, i); } if (map.containsKey(sum-target)) { int lastIndex = map.get(sum-target); res=Math.min(i-lastIndex ,res); } } return res; } class Employee { public int id; public int importance; public List<Integer> subordinates; }; public int numberOfWeakCharacters(int[][] properties) { int c =0; Arrays.sort(properties, (a, b) -> (b[0] == a[0]) ? (a[1] - b[1]) : b[0] - a[0]); PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); pq.offer(0); for (int i = 0; i < properties.length; i++) { if (properties[i][1]<pq.peek()) { c++; } pq.offer(properties[i][1]); } return c; } public boolean canFinish(int numCourses, int[][] prerequisites) { boolean f = true ; TreeMap <Integer,Integer> map = new TreeMap<>(); for (int i = 0; i < prerequisites.length; i++) { if (map.get(prerequisites[i][1])!=null) { return false; } map.put(prerequisites[i][0], prerequisites[i][1]); } return f; } int n =0; char res[][] = new char [1000][1000]; public int countBattleships(char[][] board) { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { res[i][j]= board[i][j]; } } for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { if (res[i][j]=='X') { int x = dfsB( i , j ,board , res); n++; } } } return n ; } public int dfsB(int i , int j , char [][]b , char [][]res) { if (i<0||i>=b.length|j<0||j>=b[0].length||res[i][j]=='.') { return 0; } if (res[i][j]=='X') { return 1+dfsB(i+1, j, b, res)+dfsB(i, j+1, b, res); } return 0; } List<Integer> l = new ArrayList<>(); public List<Integer> rightSideView(TreeNode root) { return dfsRightSideView(root); } public List<Integer> dfsRightSideView(TreeNode root) { if (root!=null) { l.add(root.val); } if (root==null) { return l; } if (root.right==null) { l.add(root.right.val); } dfsRightSideView(root.right); return l; } public void dfsSumNumber(TreeNode root ,int sum , int l ) { l=l*10 +root.val; if (root.left!=null) { dfsSumNumber(root.left,sum , l); } if (root.right!=null) { dfsSumNumber(root.right , sum, l); } if (root.left==null && root.right==null) { sum+=l; } // r=l*10+root.val; l-=root.val; l/=10; } boolean visited[][] = new boolean[1000][1000]; public int countSubIslands(int[][] grid1, int[][] grid2) { int k =0; for (int i = 0; i < grid1.length; i++) { for (int j = 0; j < grid1[0].length; j++) { if (grid1[i][j]==1&&grid2[i][j]==1&&!visited[i][j]) { islands(i, j, grid1 ,grid2 , visited); k++; } } } return k; } public void islands(int i ,int j ,int [][]grid1,int [][]grid2 , boolean [][]visited ) { if (i<0||i>=grid1.length||j<0||j>=grid1[0].length) { return; } if (visited[i][j]) { return; } if (grid1[i][j]==1&&grid2[i][j]==1) { visited[i][j]=true; } else { return; } islands(i+1, j, grid1 ,grid2, visited); islands(i-1, j, grid1 , grid2, visited); islands(i, j+1, grid1 , grid2, visited); islands(i, j-1, grid1 , grid2, visited); } //BFS SOLN public int[][] updateMatrix(int[][] mat) { int m =mat.length; int n = mat[0].length; Queue<int[]> q = new LinkedList<>(); for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat[0].length; j++) { if (mat[i][j]==0) { q.offer(new int[] { i , j}); } else { mat[i][j] = Integer.MAX_VALUE; } } } int dir[][] = {{1 , 0} , {-1 , 0} , {0 , 1} , { 0 , -1}}; while (!q.isEmpty()) { int zeroPos[] = q.poll(); for (int[] d : dir) { int r = zeroPos[0]+d[0]; int c = zeroPos[1]+d[1]; if (r<0||r>=mat.length || c<0||c>=mat[0].length||mat[r][c]<=mat[zeroPos[0]][zeroPos[1]]+1) { continue; } q.add(new int[]{r,c}); mat[r][c] = mat[zeroPos[0]][zeroPos[1]]+1; } } return mat; } // DFS SOLN // public int[][] updateMatrix(int[][] mat) { // int res [][] = new int[mat.length][mat[0].length]; // for (int i = 0; i < mat.length; i++) { // for (int j = 0; j < mat.length; j++) { // if (mat[i][j]==0) { // Dis0(i, j, mat , res, 0); // } // } // } // return res; // } // public void Dis0(int i , int j , int [][]mat, int res[][] , int dist) { // if (i<0||i>=mat.length||j>=mat[0].length||j<0) { // return ; // } // if (dist==0 ||mat[i][j]==1&&(res[i][j]==0||res[i][j]>dist)) { // res[i][j]= dist; // Dis0(i+1, j, mat, res, dist+1); // Dis0(i-1, j, mat, res, dist+1); // Dis0(i, j+1, mat, res, dist+1); // Dis0(i, j-1, mat, res, dist+1); // } // // return dist; // } public int maxDepth(TreeNode root) { if (root==null) { return 0; } return maxD(root , 0); } public int maxD(TreeNode root , int d ) { if (root==null) { return d; } return Math.max(maxD(root.left, d+1), maxD(root.right, d+1)); } public int reverse(int x) { int sign=x>0?1:-1; //x=Math.abs(x); long s= 0; int r= 0; int n=x; while (n!=0) { r=n%10; s=s*10+r; n/=10; if (s>Integer.MAX_VALUE||s<Integer.MIN_VALUE) { return 0; } } return (int)s*sign; } public boolean checkInclusion(String s1, String s2) { boolean f = false ; if (s1.length()>s2.length()) { return false; } for (int i = 0; i < s2.length(); i++) { HashMap<Character ,Integer> c1 = new HashMap<>(); HashMap<Character ,Integer> c2 = new HashMap<>(); for (int j = 0; j < s1.length(); j++) { char ch2 = s2.charAt(i+j); char ch1 = s1.charAt(j); // if (c1.get(key)) { // } } } return f; } public class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } public ListNode middleNode(ListNode head) { ListNode mid = head; ListNode last = head; int k =0; while (last.next!=null&&last.next.next!=null) { k++; mid= mid.next; last = last.next.next; } if (getLen(head)%2==0) { return mid.next; } return mid; } public int getLen(ListNode mid) { int l = 0; ListNode p = mid; while (p!=null) { l++; p=p.next; } return l; } public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; } } public List<List<Integer>> verticalTraversal(TreeNode root) { TreeMap<Integer, TreeMap<Integer, PriorityQueue<Integer>>> map = new TreeMap<>(); dfs(root, 0, 0, map); List<List<Integer>> list = new ArrayList<>(); for (TreeMap<Integer, PriorityQueue<Integer>> ys : map.values()) { list.add(new ArrayList<>()); for (PriorityQueue<Integer> nodes : ys.values()) { while (!nodes.isEmpty()) { // list.get(list.size() - 1).add(nodes.poll()); } } } return list; } private void dfs(TreeNode root, int x, int y, TreeMap<Integer, TreeMap<Integer, PriorityQueue<Integer>>> map) { if (root == null) { return; } if (!map.containsKey(x)) { map.put(x, new TreeMap<>()); } if (!map.get(x).containsKey(y)) { map.get(x).put(y, new PriorityQueue<>()); } map.get(x).get(y).offer(root.val); dfs(root.left, x - 1, y + 1, map); dfs(root.right, x + 1, y + 1, map); } public void rotate(int[] a, int k) { int n = a.length; k = k%n; int temp[] = new int[k]; for (int i = n-k; i < n; i++) { temp[i-(n-k)]=a[i]; } for (int i =n-1 ; i>=k;i--) { a[i]=a[i-k]; } ArrayList<Integer> b = new ArrayList<>(); for (int i = 0; i < k; i++) { a[i]=temp[i]; } for (int i = 0; i < a.length; i++) { b.add(a[i]); } System.out.println(b); } public int search(int[] nums, int target) { int n =nums.length; int l=0; int r = n-1; int mid =(l+r)/2; while (l<r) { mid =(l+r)/2; if (target==nums[mid]) { return mid; } if (target>nums[mid]) { l=mid; } else { r=mid; } } return -1; } public boolean isMatch(String s, String p) { boolean f = true; if (p.length()!=s.length()&&!p.contains("*")) { return false; } if (p.length()==0&&p.contains("*")) { return f; } for (int i = 0; i < s.length(); i++) { char b =s.charAt(i); if (!(p.charAt(i)==b||p.charAt(i)=='?')) { return false; } } return f; } public String revferseWords(String s) { String str = s.trim()+' '; String s1 = ""; String finals = ""; for (int i = 0; i < str.length(); i++) { char b = s.charAt(i); if (b==' ') { char ch[] = str.toCharArray(); str=""; String temp = new String(ch); s1= temp.concat(String.valueOf(b)); finals = s1.concat(finals); } str+=b; } return finals.trim(); } public void twoLab(FlashFastReader in, PrintWriter out) { int h = in.nextInt(); int m = in.nextInt(); int s = in.nextInt(); if (!(h>=0&&h<24&&m>=0&&m<60&&s>=0&&s<60)) { out.println("Not applicable"); return; } if (m==59&&s>=45) { if (h!=23) { out.println(h+1+":"+"00"+":"+(s-45)); return; } out.println("00"+":"+"00"+":"+(s-45)); return ; } if (s>=45) { out.println(h+":"+m+1+":"+(s-45)); return ; } out.println(h+":"+m+":"+(s+15)); } public void oneLab(FlashFastReader in , PrintWriter out) { try { ArrayList<Integer> b = new ArrayList<Integer>(); while (true) { int n = in.nextInt(); if (b.size()==0&&n%2==1) { out.println("Average : " + 0); return; } if (n%2==1) { break; } b.add(n); } long sum=0; long l = b.size(); for (Integer a : b) { sum+=a; } out.println("average : " + sum/l); } catch (Exception e) { out.println(e); } } public long pow (int x ,int n ) { long y = 1; if (n==0) { return y; } while (n>0) { if (n%2!=0) { y=y*x; } x*=x; n/=2; } return y; } public long powrec (int x ,int n ) { long y = 1; if (n==0) { return y; } y = powrec(x, n/2); if (n%2==0) { return y*y; } return y*y*x; } public int fib(int n) { if (n==0||n==11) { return n; } return fib(n-1)+fib(n-2); } public int gcd(int a , int b) { if (b%a==0) { return a; } return gcd(b%a,a); } public int maximumElement(int a[]) { int x = Integer.MIN_VALUE; for (int i = 0; i < a.length; i++) { if (a[i]>x) { x=a[i]; } } return x; } public int minimumElement(int a[]) { int x = Integer.MAX_VALUE; for (int i = 0; i < a.length; i++) { if (a[i]<x) { x=a[i]; } } return x; } boolean [] sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p]) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } // GFG arraylist function for referral public ArrayList create2DArrayList(int n ,int k) { // Creating a 2D ArrayList of Integer type ArrayList<ArrayList<Integer> > x = new ArrayList<ArrayList<Integer> >(); // One space allocated for R0 // Adding 3 to R0 created above x(R0, C0) //x.get(0).add(0, 3); int xr = 0; for (int i = 1; i <= n; i+=2) { if ((i+k)*(i+1)%4==0) { x.add(new ArrayList<Integer>()); x.get(xr).addAll(Arrays.asList(i , i+1)); } else { x.add(new ArrayList<Integer>()); x.get(xr).addAll(Arrays.asList(i+1 , i)); } xr++; } // Creating R1 and adding values // Note: Another way for adding values in 2D // collections // x.add( // new ArrayList<Integer>(Arrays.asList(3, 4, 6))); // // Adding 366 to x(R1, C0) // x.get(1).add(0, 366); // // Adding 576 to x(R1, C4) // x.get(1).add(4, 576); // // Now, adding values to R2 // x.add(2, new ArrayList<>(Arrays.asList(3, 84))); // // Adding values to R3 // x.add(new ArrayList<Integer>( // Arrays.asList(83, 6684, 776))); // // Adding values to R4 // x.add(new ArrayList<>(Arrays.asList(8))); // // Appending values to R4 // x.get(4).addAll(Arrays.asList(9, 10, 11)); // // Appending values to R1, but start appending from // // C3 // x.get(1).addAll(3, Arrays.asList(22, 1000)); // This method will return 2D array return x; } } class FlashFastReader { BufferedReader in; StringTokenizer token; public FlashFastReader(InputStream ins) { in=new BufferedReader(new InputStreamReader(ins)); token=new StringTokenizer(""); } public boolean hasNext() { while (!token.hasMoreTokens()) { try { String s = in.readLine(); if (s == null) return false; token = new StringTokenizer(s); } catch (IOException e) { throw new InputMismatchException(); } } return true; } public String next() { hasNext(); return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextIntsInputAsArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } public long nextLong() { return Long.parseLong(next()); } public long[] nextLongsInputAsArray(int n) { long [] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public String nextString() { return String.valueOf(next()); } public String[] nextStringsInputAsArray(int n) { String [] a = new String[n]; for (int i = 0; i < n; i++) a[i] = nextString(); return a; } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
ddad8a9dbfb8203e302e50d3a85a587a
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] arr = sc.nextIntArray(n); int[] ans = new int[n]; ans[0] = arr[0]; boolean isPossible = true; for (int i = 1; i < n && isPossible; i++) { if (ans[i - 1] - arr[i] >= 0 && arr[i] != 0) isPossible = false; ans[i] = ans[i - 1] + arr[i]; } pw.println(isPossible ? toString(ans) : -1); } pw.close(); } static int getMaxDis(int[] arr, int[] time, double target) { double maxValue = -1; int maxIdx = -1; for (int i = 0; i < arr.length; i++) { double dist = dis(arr[i], target) + time[i]; if (dist > maxValue) { maxValue = dist; maxIdx = i; } } return maxIdx; } static double dis(double x, double y) { return Math.abs(x - y); } static long[] HashStr(char[] arr) { long[] hash = new long[arr.length]; int p = 31; int m = 1000000007; long hashValue = 0; long power = 1; for (int i = 0; i < arr.length; i++) { hashValue = (hashValue + (arr[i] - 'a' + 1) * power) % m; power = (power * p) % m; hash[i] = hashValue; } return hash; } static int log2(int n) { return (int) (Math.log(n) / Math.log(2)); } static int[] sieve() { int n = (int) 1e7; int[] arr = new int[n]; for (int i = 2; i < arr.length; i++) { for (int j = i; j < arr.length; j += i) { if (arr[j] == 0) arr[j] = i; } } return arr; } static void shuffle(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = arr[i]; arr[i] = arr[r]; arr[r] = tmp; } } static void sort(int[] arr) { shuffle(arr); Arrays.sort(arr); } static long getSum(int[] arr) { long sum = 0; for (int i = 0; i < arr.length; i++) { sum += arr[i]; } return sum; } static int getMin(int[] arr) { int min = Integer.MAX_VALUE; for (int i = 0; i < arr.length; i++) { min = Math.min(min, arr[i]); } return min; } static int getMax(int[] arr) { int max = Integer.MIN_VALUE; for (int i = 0; i < arr.length; i++) { max = Math.max(max, arr[i]); } return max; } static boolean isEqual(int[] a, int[] b) { if (a.length != b.length) return false; for (int i = 0; i < b.length; i++) { if (a[i] != b[i]) return false; } return true; } static void reverse(int[] arr, int start, int end) { while (start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } static boolean isSorted(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { if (arr[i] > arr[i + 1]) return false; } return true; } static int gcd(int x, int y) { if (x == 0) return y; return gcd(y % x, x); } static HashMap<Integer, Integer> Hash(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static HashMap<Character, Integer> Hash(char[] arr) { HashMap<Character, Integer> map = new HashMap<>(); for (char i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static boolean isPrime(int n) { if (n <= 1) return false; for (int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } public static long combination(long n, long r) { return factorial(n) / (factorial(n - r) * factorial(r)); } static long factorial(Long n) { if (n == 0) return 1; return (n % mod) * (factorial(n - 1) % mod) % mod; } static boolean isPalindrome(char[] str, int i, int j) { while (i < j) { if (str[i] != str[j]) return false; i++; j--; } return true; } public static int setBit(int mask, int idx) { return mask | (1 << idx); } public static boolean checkBit(int mask, int idx) { return (mask & (1 << idx)) != 0; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public int[] NextIntArray(int n) throws IOException { int[] arr = new int[n + 1]; for (int i = 1; i < arr.length; i++) { arr[i] = nextInt(); } return arr; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } public static String toString(int[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(long[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(ArrayList arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.size(); i++) { sb.append(arr.get(i) + " "); } return sb.toString().trim(); } public static String toString(int[][] arr) { StringBuilder sb = new StringBuilder(); for (int[] i : arr) { sb.append(toString(i) + "\n"); } return sb.toString(); } public static String toString(boolean[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(char[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i]); } return sb.toString().trim(); } public static <T> String toString(T[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
a49eddf281444955759398de2ebbd85d
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*;import java.lang.*;import java.util.*; //* --> number of prime numbers less then or equal to x are --> x/ln(x) //* --> String concatenation using the + operator within a loop should be avoided. Since the String object is immutable, each call for concatenation will // result in a new String object being created. // THE SIEVE USED HERE WILL RETURN A LIST CONTAINING ALL THE PRIME NUMBERS TILL N public class codechef {static FastScanner sc;static PrintWriter pw;static class FastScanner {InputStreamReader is;BufferedReader br;StringTokenizer st; public FastScanner() {is = new InputStreamReader(System.in);br = new BufferedReader(is);} String next() throws Exception {while (st == null || !st.hasMoreElements())st = new StringTokenizer(br.readLine()); return st.nextToken();}int nextInt() throws Exception {return Integer.parseInt(next());}long nextLong() throws Exception { return Long.parseLong(next());}int[] readArray(int num) throws Exception {int arr[]=new int[num]; for(int i=0;i<num;i++)arr[i]=nextInt();return arr;}String nextLine() throws Exception {return br.readLine(); }} public static boolean power_of_two(int a){if((a&(a-1))==0){ return true;}return false;} static boolean PS(double x){if (x >= 0) {double i= Math.sqrt(x);if(i%1!=0){ return false;}return ((i * i) == x);}return false;}public static int[] ia(int n){int ar[]=new int[n]; return ar;}public static long[] la(int n){long ar[]=new long[n];return ar;} public static void print(int ans,int t){System.out.println("Case"+" "+"#"+t+":"+" "+ans);} static long mod=1000000007;static int max=Integer.MIN_VALUE;static int min=Integer.MAX_VALUE; public static void sort(long[] arr){//because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Long> ls = new ArrayList<Long>();for(long x: arr)ls.add(x);Collections.sort(ls); for(int i=0; i < arr.length; i++)arr[i] = ls.get(i);}public static long fciel(long a, long b) {if (a == 0) return 0;return (a - 1) / b + 1;} static boolean[] is_prime = new boolean[1000001];static ArrayList<Integer> list = new ArrayList<>(); static long n = 1000000;public static void sieve() {Arrays.fill(is_prime, true); is_prime[0] = is_prime[1] = false;for (int i = 2; i * i <= n; i++) { if (is_prime[i]) {for (int j = i * i; j <= n; j += i)is_prime[j] = false;}}for (int i = 2; i <= n; i++) { if (is_prime[i]) {list.add(i);}}} // ---------- NCR ---------- \ static int NC=100005; static long inv[]=new long[NC]; static long fac_inv[]=new long[NC]; static long fac[]=new long[NC];public static void initialize() { long MOD=mod; int i; inv[1]=1; for(i=2;i<=NC-2;i++) inv[i]=(MOD-MOD/i)*inv[(int)MOD%i]%MOD; fac[0]=fac[1]=1; for(i=2;i<=NC-2;i++) fac[i]=i*fac[i-1]%MOD; fac_inv[0]=fac_inv[1]=1; for(i=2;i<=NC-2;i++) fac_inv[i]=inv[i]*fac_inv[i-1]%MOD; } public static long ncr(int n,int r) { long MOD=mod; if(n<r) return 0; return (fac[n]*fac_inv[r]%MOD)*fac_inv[n-r]%MOD; } // ---------- NCR ---------- \ // ---------- FACTORS -------- \ static int div[][] = new int[1000001][]; public static void factors() { int divCnt[] = new int[1000001]; for(int i = 1000000; i >= 1; --i) { for(int j = i; j <= 1000000; j += i) divCnt[j]++; } for(int i = 1; i <= 1000000; ++i) div[i] = new int[divCnt[i]]; int ptr[] = new int[1000001]; for(int i = 1000000; i >= 1; --i) { for(int j = i; j <= 1000000; j += i) div[j][ptr[j]++] = i; } } // ---------- FACTORS -------- \ // ------------- DSU ---------------\ static int par[]=new int[1000001];static int size[]=new int[1000001]; public static void make(int v){par[v]=v;size[v]++;} public static void union(int a,int b){a=find(a);b=find(b); if(a!=b){if(size[a]<size[b]){int temp=a;a=b;b=temp;}par[b]=a; size[a]++;}}public static int find(int v) {if(v==par[v]){return v;}return par[v]=find(par[v]);} // ------------- DSU ---------------\ public static void main(String args[]) throws java.lang.Exception { sc = new FastScanner();pw = new PrintWriter(System.out);StringBuilder s = new StringBuilder(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int ar[]=new int[n]; for(int i=0;i<n;i++) { ar[i]=sc.nextInt(); } int prev=ar[0]; int f=0; int ans[]=new int[n]; ans[0]=prev; for(int i=1;i<n;i++) { if(ar[i]!=0&&prev-ar[i]>=0) { //System.out.println(i); f=1; break; } ans[i]=prev+ar[i]; prev=ans[i]; } if(f==0) { for(int i:ans) { s.append(i+" "); } } else{ s.append(-1); } if(t>0) { s.append("\n"); }} pw.print(s);pw.close();}}
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
00a89d9f5cf3fde906b8401bf0fcb890
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static long mod = (int)1e9+7; // static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; outer : while(t-->0) { int n = sc.nextInt(); int arr[] = sc.readArray(n); int sum = arr[0]; for(int i=1;i<n;i++) { if(sum - arr[i] >= 0 && arr[i] > 0) { System.out.println(-1); continue outer; } sum += arr[i]; arr[i] = sum; } for(int x:arr) { System.out.print(x+" "); } System.out.println(); } } 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()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } static class FenwickTree { //Binary Indexed Tree //1 indexed public int[] tree; public int size; public FenwickTree(int size) { this.size = size; tree = new int[size+5]; } public void add(int i, int v) { while(i <= size) { tree[i] += v; i += i&-i; } } public int find(int i) { int res = 0; while(i >= 1) { res += tree[i]; i -= i&-i; } return res; } public int find(int l, int r) { return find(r)-find(l-1); } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } private static long mergeAndCount(int[] arr, int l, int m, int r) { // Left subarray int[] left = Arrays.copyOfRange(arr, l, m + 1); // Right subarray int[] right = Arrays.copyOfRange(arr, m + 1, r + 1); int i = 0, j = 0, k = l;long swaps = 0; while (i < left.length && j < right.length) { if (left[i] < right[j]) arr[k++] = left[i++]; else { arr[k++] = right[j++]; swaps += (m + 1) - (l + i); } } while (i < left.length) arr[k++] = left[i++]; while (j < right.length) arr[k++] = right[j++]; return swaps; } // Merge sort function private static long mergeSortAndCount(int[] arr, int l, int r) { // Keeps track of the inversion count at a // particular node of the recursion tree long count = 0; if (l < r) { int m = (l + r) / 2; // Total inversion count = left subarray count // + right subarray count + merge count // Left subarray count count += mergeSortAndCount(arr, l, m); // Right subarray count count += mergeSortAndCount(arr, m + 1, r); // Merge count count += mergeAndCount(arr, l, m, r); } return count; } static void my_sort(long[] arr) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<arr.length;i++) { list.add(arr[i]); } Collections.sort(list); for(int i=0;i<arr.length;i++) { arr[i] = list.get(i); } } static void reverse_sorted(int[] arr) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<arr.length;i++) { list.add(arr[i]); } Collections.sort(list , Collections.reverseOrder()); for(int i=0;i<arr.length;i++) { arr[i] = list.get(i); } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value int l=-1,r=list.size(); while(l+1<r) { int m=(l+r)>>>1; if(list.get(m)<=x) l=m; else r=m; } return l+1; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static class Queue_Pair implements Comparable<Queue_Pair> { int first , second; public Queue_Pair(int first, int second) { this.first=first; this.second=second; } public int compareTo(Queue_Pair o) { return Integer.compare(o.first, first); } } static void leftRotate(int arr[], int d, int n) { for (int i = 0; i < d; i++) leftRotatebyOne(arr, n); } static void leftRotatebyOne(int arr[], int n) { int i, temp; temp = arr[0]; for (i = 0; i < n - 1; i++) arr[i] = arr[i + 1]; arr[n-1] = temp; } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } static boolean palindrome_array(char arr[], int n) { // Initialise flag to zero. int flag = 0; // Loop till array size n/2. for (int i = 0; i <= n / 2 && n != 0; i++) { // Check if first and last element are different // Then set flag to 1. if (arr[i] != arr[n - i - 1]) { flag = 1; break; } } // If flag is set then print Not Palindrome // else print Palindrome. if (flag == 1) return false; else return true; } static boolean allElementsEqual(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]==arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } static boolean allElementsDistinct(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]!=arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } public static void reverse(int[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } public static void reverse_Long(long[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily long temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static boolean isReverseSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { return false; } } return true; } static int[] rearrangeEvenAndOdd(int arr[], int n) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); int[] array = list.stream().mapToInt(i->i).toArray(); return array; } static long[] rearrangeEvenAndOddLong(long arr[], int n) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); long[] array = list.stream().mapToLong(i->i).toArray(); return array; } static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (long i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static long getSum(long n) { long sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcdLong(long a, long b) { if (b == 0) return a; return gcdLong(b, a % b); } static void swap(int i, int j) { int temp = i; i = j; j = temp; } static int countDigit(int n) { return (int)Math.floor(Math.log10(n) + 1); } } class Pair { int first; int second; Pair(int first , int second) { this.first = first; this.second = second; } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
289c4397af355ef9216e51f9898766c1
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
// Jai Shree Ram ⛳⛳⛳ // Jai Bajrang Bali // Jai Saraswati maa // Har Har Mahadev // Thanks Kalash Shah :) import java.math.BigInteger; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import static java.lang.Character.isUpperCase; public class B { static FastScanner sn = new FastScanner(); public static void main(String[] args) { int T = sn.nextInt(); while (T-- > 0) solve(); } public static void solve() { int n = sn.nextInt(); int[] d = sn.scan(n); int[] a = new int[n]; a[0] = d[0]; for (int i = 1; i < n; i++) { int temp = a[i - 1] + d[i]; int temp2 = a[i - 1] - d[i]; if (temp >= 0 && temp2 >= 0 && temp != temp2) { System.out.println(-1); return; } a[i] = d[i] + a[i - 1]; } sn.print(a); System.out.println(); } //----------------------------------------------------------------------------------// public static void mergesort(long[] arr, int l, int r) { if (l >= r) { return; } int mid = l + (r - l) / 2; mergesort(arr, l, mid); mergesort(arr, mid + 1, r); Merge(arr, l, mid, r); } public static int lowerBound(long[] a, long temp) { int n = a.length; int ans = 0; int l = 0, r = n - 1; while (l <= r) { int mid = l + (r - l) / 2; if (a[mid] <= temp) { ans = Math.max(mid + 1, ans); l = mid + 1; } else { r = mid - 1; } } return ans; } public static int higherBound(long[] a, long temp) { int n = a.length; int ans = n - 1; int l = 0, r = n - 1; while (l <= r) { int mid = l + (r - l) / 2; if (a[mid] >= temp) { ans = Math.min(mid + 1, ans); r = mid - 1; } else { l = mid + 1; } } return ans; } static long getFirstSetBitPos(long n) { return (long) ((Math.log10(n & -n)) / Math.log10(2)) + 1; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static class Pair implements Comparable<Pair> { int ind; int ch; Pair(int a, int ch) { this.ind = a; this.ch = ch; } public int compareTo(Pair o) { if (this.ch == o.ch) { return this.ind - o.ind; } return this.ch - o.ch; } public String toString() { return ""; } } static int countSetBits(long n) { int count = 0; while (n > 0) { count += n & 1; n >>= 1; } return count; } public static void Merge(long[] arr, int l, int mid, int r) { long[] B = new long[r - l + 1]; int i = l; int j = mid + 1; int k = 0; while (i <= mid && j <= r) { if (arr[i] <= arr[j]) { B[k++] = arr[i++]; } else { B[k++] = arr[j++]; } } while (i <= mid) { B[k++] = arr[i++]; } while (j <= r) { B[k++] = arr[j++]; } for (int i1 = 0, j1 = l; i1 < B.length; i1++, j1++) { arr[j1] = B[i1]; } } 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()); } char nextChar() { return next().charAt(0); } long[] readArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[] scan(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } void print(int[] a) { for (int j : a) { System.out.print(j + " "); } } void printl(long[] a) { for (long j : a) { System.out.print(j + " "); } } long nextLong() { return Long.parseLong(next()); } } static class Debug { public static void debug(long a) { System.out.println("--> " + a); } public static void debug(long a, long b) { System.out.println("--> " + a + " + " + b); } public static void debug(char a, char b) { System.out.println("--> " + a + " + " + b); } public static void debug(int[] array) { System.out.print("Array--> "); System.out.println(Arrays.toString(array)); } public static void debug(char[] array) { System.out.print("Array--> "); System.out.println(Arrays.toString(array)); } public static void debug(HashMap<Integer, Integer> map) { System.out.print("Map--> " + map.toString()); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
294e71455b8384b43bb66fd19e975ea5
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
//۰۪۫A۪۫۰۰۪۫B۪۫۰۰۪۫D۪۫۰۰۪۫-۪۫۰۰۪۫A۪۫۰۰۪۫L۪۫۰۰۪۫L۪۫۰۰۪۫A۪۫۰۰۪۫H۪۫۰ import java.util.*; public class Main { static Scanner z = new Scanner(System.in); public static void main(String[] args) { int M=z.nextInt(); while(M-->0){ int N =z.nextInt(); int a[]=new int [N]; int d[]=new int [N]; boolean flag=true; for (int i = 0; i < N; i++) { d[i]=z.nextInt(); } for (int i = 0; i < N; i++) { if(i==0){ a[i]=d[i]; }else if ( a[i - 1] + d[i] == a[i - 1] - d[i] ||a[i - 1] - d[i] < 0 ) { a[i] = a[i - 1] + d[i]; } else { flag = false; break; } } if(flag){ for (int i = 0; i < N; i++) { System.out.print(a[i]+" "); } System.out.println("");} else System.out.println(-1); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
7c6a445b9e4b4213fa0f39db3deb67e5
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = Integer.parseInt(sc.next().trim()); String sol = ""; for (int i = 0; i<t; ++i) { sol = ""; int n = Integer.parseInt(sc.next().trim()); int d[] = new int[n]; int a[] = new int[n]; for (int j = 0 ; j<n; ++j) { d[j] = Integer.parseInt(sc.next().trim()); } a[0] = d[0]; sol = sol + a[0]; for (int j = 1; j<n && (!(sol.equals("-1"))); ++j) { a[j] = a[j-1] + d[j]; sol = sol + " " + a[j]; if (a[j-1] - d[j] >= 0 && (d[j]!=0)) { sol = "-1"; } } sol = sol.trim(); System.out.println(sol); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
423add4320ba91d5ce66e471f3292d29
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class cc { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int testcase = scan.nextInt(); for(int v = 0 ; v < testcase; v++){ int n = scan.nextInt(); int []arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = scan.nextInt(); } if(n == 1){ System.out.println(arr[0]); continue; } int count = 0; for(int i = 1; i < n; i++){ if((arr[i-1] - arr[i]) >= 0 && arr[i] != 0){ count++; } arr[i] = arr[i-1] + arr[i]; } if(count > 0){ System.out.println(-1); } else{ for (int i = 0; i < n; i++){ System.out.print(arr[i] + " "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
3cbd2c7e8495c64e8c81bfc16d26a295
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class B_Array_Recovery { public static void main(String[] args) { try { FastReader s = new FastReader(); int t = s.nextInt(); while (t-- > 0) { int a=0; int n = s.nextInt(); int[] d= new int[n+1]; for(int k=1;k<=n;k++){ d[k]=s.nextInt(); } for(int k=1;k<n;k++){ if(d[k+1]<=d[k]&&d[k+1]!=0){ a=-1; break; } else{ d[k+1]=Math.abs(d[k]+d[k+1]); } } if(a==-1){System.out.println(a);} else{ for(int k=1;k<=n;k++){ System.out.println(d[k]); } } } } catch (Exception e) { return; } } 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; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
0d56a0092a75a67ed0b1572b4781a4da
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class Recovery { public static void main(String args[]) { Scanner sc=new Scanner(System.in); byte t=sc.nextByte(); for(byte i=0; i<t; i++) { int n=sc.nextInt(), d[]=new int[n], a[]=new int[n]; for(int j=0; j<n; j++) d[j]=sc.nextInt(); a[0]=d[0]; boolean counter=false; for(int j=1; j<n; j++) { int option1=d[j]+a[j-1] , option2=(a[j-1]-d[j]); if(option1!=option2 && option2>-1) { counter=true; break; } else a[j]=option1; } if(counter) System.out.println(-1); else { for(int j=0; j<n; j++) System.out.print(a[j]+" "); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
ff3ee754044b1588bb01e8ea2c0cbb6a
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class Solution { static Scanner scn = new Scanner(System.in); public static void main(String[] args) { int t = scn.nextInt(); int temp = t; while (t-->0) { int n = scn.nextInt(); int[] arr = new int[n]; int[] ans = new int[n]; for(int i = 0 ; i<n ; i++){ arr[i] = scn.nextInt(); } ans[0] = arr[0]; boolean flag = true; for(int i = 1 ; i<n ; i++){ int d = arr[i]; //we have to check which one of the 2 possibilities lie in bound, if both do then we return a -1 int elem1 = d+ans[i-1]; int elem2 = ans[i-1] - d; if(check(elem1) && check(elem2) && (elem1!=elem2)) { System.out.println(-1); flag = false; break; } if(check(elem1)){ ans[i] = elem1; } if(check(elem2)) { ans[i] = elem2; } } if(flag){ for(int element : ans) { System.out.print(element + " "); } System.out.println(); } //System.out.println(); } } public static boolean check(int i){ if(i>=0){ return true; } return false; } } class Node{ int val; int days; Node(int _val , int _days){ val = _val; days = _days; } public int getDays() { return days; } public int getVal() { return val; } } /* 4 3 1 3 5 2 the end of the array will be in decreasing order we have to perform a binary search on the last elements of all the arraylists and find */
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
585daa828b9624ded0a4489b18555ce9
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
// Working program with FastReader 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 UtopianLord { 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 { if(st !=null && st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } static void solve(int[] nums,int n) { int[] res = new int[n]; StringBuilder sb= new StringBuilder(); boolean nonNeg=false; for(int i=0;i<n;i++) { if(i==0) res[i] = nums[i]; else { int count=0; if( nums[i] != 0 && res[i-1] - nums[i] >= 0) { res[i] = res[i-1]-nums[i]; // System.out.println("1. "+res[i]); count++; } if( nums[i] == 0 || res[i-1] + nums[i] >= 0) { res[i] = nums[i]+res[i-1]; count++; // System.out.println("2. "+res[i]); } if(count == 2) { nonNeg = true; break; } } sb.append(res[i]).append(" "); } if(nonNeg) System.out.println("-1"); else System.out.println( sb.toString() ); } public static void main(String[] args) { FastReader in = new FastReader(); int tc = in.nextInt(); while(tc-- > 0) { int n = in.nextInt(); int[] nums = new int[n]; for(int i=0;i<n;++i) { nums[i] = in.nextInt(); } solve(nums,n); } } } /* */
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
1d49a46a5e414b9c41a081ad3444651c
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int test=sc.nextInt(); for(int tc=0; tc<test; tc++) { int n=sc.nextInt(); int[] d=new int[n]; for(int i=0; i<n; i++) { d[i]=sc.nextInt(); } List<Integer> ans=new ArrayList<>(); ans.add(d[0]); int flag=0; for(int i=1; i<n; i++) { int temp1=ans.get(ans.size()-1)+d[i]; int temp2=ans.get(ans.size()-1)-d[i]; if((temp1!=temp2)&& temp1>=0 && temp2>=0) { flag=1; break; } ans.add(Math.max(temp1,temp2)); } if(flag==1) System.out.println(-1); else { for(Integer i: ans) System.out.print(i+" "); System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
84e15b8b555b2cc8797105968ef6e902
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class Solution{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int d[] = new int[n]; int a[] = new int[n]; for(int i=0;i<n;i++){ d[i] = sc.nextInt(); } a[0] = d[0]; boolean hasFound = false; for(int i=1;i<n;i++){ int x = a[i-1]-d[i]; if(x>=0 && d[i]!=0){ System.out.println(-1); hasFound = true; break; } a[i] = d[i]+a[i-1]; } if(!hasFound){ for(int i: a){ System.out.print(i+" "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
c88c0d29b95ce7027e3217a43ad71ed5
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static final PrintWriter out =new PrintWriter(System.out); static final FastReader sc = new FastReader(); //I invented a new word!Plagiarism! //Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them //What do Alexander the Great and Winnie the Pooh have in common? Same middle name. //I finally decided to sell my vacuum cleaner. All it was doing was gathering dust! //ArrayList<Integer> a=new ArrayList <Integer>(); //PriorityQueue<Integer> pq=new PriorityQueue<>(); //char[] a = s.toCharArray(); // char s[]=sc.next().toCharArray(); public static boolean sorted(int a[]) { int n=a.length,i; int b[]=new int[n]; for(i=0;i<n;i++) b[i]=a[i]; Arrays.sort(b); for(i=0;i<n;i++) { if(a[i]!=b[i]) return false; } return true; } public static void main (String[] args) throws java.lang.Exception { int tes=sc.nextInt(); label:while(tes-->0) { int n=sc.nextInt(); int a[]=new int[n]; int ans[]=new int[n]; int i; for(i=0;i<n;i++) a[i]=sc.nextInt(); ans[0]=a[0]; for(i=1;i<n;i++) { int A=a[i]+ans[i-1]; int B=ans[i-1]-a[i]; if(A>=0 && B>=0 && A!=B) { System.out.println(-1); continue label; } else ans[i]=Math.max(A,B); } for(int it:ans) System.out.print(it+" "); System.out.println(); } } public static int first(ArrayList<Integer> arr, int low, int high, int x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x) return mid; else if (x > arr.get(mid)) return first(arr, (mid + 1), high, x, n); else return first(arr, low, (mid - 1), x, n); } return -1; } public static int last(ArrayList<Integer> arr, int low, int high, int x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x) return mid; else if (x < arr.get(mid)) return last(arr, low, (mid - 1), x, n); else return last(arr, (mid + 1), high, x, n); } return -1; } public static int lis(int[] arr) { int n = arr.length; ArrayList<Integer> al = new ArrayList<Integer>(); al.add(arr[0]); for(int i = 1 ; i<n;i++) { int x = al.get(al.size()-1); if(arr[i]>=x) { al.add(arr[i]); }else { int v = upper_bound(al, 0, al.size(), arr[i]); al.set(v, arr[i]); } } return al.size(); } public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k) { Collections.sort(ar); int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (ar.get((int)mid) <k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k) { Collections.sort(ar); int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (ar.get(mid) <=k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long mod=1000000007; long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } 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
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
2fff5a709ea7a3fb7d3af3b0a2938dcd
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while(t-->0){ int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int[] arr = new int[n]; arr[0]=Integer.parseInt(st.nextToken()); boolean ans = true; for(int i=1; i<n; i++){ arr[i]=Integer.parseInt(st.nextToken()); if(arr[i]!=0&&arr[i-1]-arr[i]>=0){ ans=false; break; } int temp = arr[i]; arr[i]+=arr[i-1]; } if(ans){ for(int i: arr){ pw.print(i+" "); } pw.println(); } else{ pw.println(-1); } } pw.close(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
bf8682ce8cc95247d1a1b44801b8146c
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.io.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); outer: while(t-->0) { int n = sc.nextInt(); int[] d = new int[n]; int[] ans = new int[d.length]; for(int i = 0; i < n; i++) { d[i] = sc.nextInt(); } ans[0] = d[0]; for(int i = 1; i < n; i++) { ans[i] = ans[i-1] + d[i]; } for(int i = 0; i < n-1; i++) { if(ans[i] >= d[i+1] && d[i+1] != 0) { pw.println(-1); continue outer; } } for(int i = 0; i < n; i++) { pw.print(ans[i] + " "); } pw.println(); } pw.close(); sc.close(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
2fac5143a10f8eb019f1b5daddb07b47
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.io.*; public class Main { private static FS sc = new FS(); private static class FS { 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) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } private static class extra { static int[] intArr(int size) { int[] a = new int[size]; for(int i = 0; i < size; i++) a[i] = sc.nextInt(); return a; } static long[] longArr(int size) { Scanner scc = new Scanner(System.in); long[] a = new long[size]; for(int i = 0; i < size; i++) a[i] = sc.nextLong(); return a; } static long intSum(int[] a) { long sum = 0; for(int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } static long longSum(long[] a) { long sum = 0; for(int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } static LinkedList[] graphD(int vertices, int edges) { LinkedList<Integer>[] temp = new LinkedList[vertices+1]; for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>(); for(int i = 0; i < edges; i++) { int x = sc.nextInt(); int y = sc.nextInt(); temp[x].add(y); } return temp; } static LinkedList[] graphUD(int vertices, int edges) { LinkedList<Integer>[] temp = new LinkedList[vertices+1]; for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>(); for(int i = 0; i < edges; i++) { int x = sc.nextInt(); int y = sc.nextInt(); temp[x].add(y); temp[y].add(x); } return temp; } static void printG(List<Integer>[] temp) { for(List<Integer> aa:temp) System.out.println(aa); } static long cal(long val, long pow, long mod) { if(pow == 0) return 1; long ans = cal(val, pow / 2, mod); long ret = (ans * ans) % mod; if(pow % 2 == 0) return ret; return (ret * val) % mod; } static long gcd(long a, long b) { return b == 0 ? a:gcd(b, a%b); } } static long ans; public static void main(String[] args) { int tc = sc.nextInt(); // int tc = 1; // int cases = 1; StringBuilder ret = new StringBuilder(); while(tc-- > 0) { int n = sc.nextInt(); int[] a = extra.intArr(n); int flag = 0, cur = a[0]; int[] ans = new int[n]; ans[0] = a[0]; for(int i = 1; i < n; i++) { if(a[i] == 0) ans[i] = ans[i - 1]; else if(cur < a[i]) { ans[i] = a[i] + cur; cur = ans[i]; } else { flag = 1; break; } } if(flag == 1) ret.append(-1 + "\n"); else { for(int aa : ans) ret.append(aa + " "); ret.append("\n"); } } System.out.println(ret); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
fc464d85a9a2e8b53559649bd95c94ce
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.io.*; public class Main { private static final void solve() throws IOException { final int n = ni(); var a = ni(n); var d = a.clone(); Arrays.parallelPrefix(d, Integer::sum); for (int i = 1; i < n; i++) { if (a[i] != 0 && d[i - 1] >= a[i]) { ou.println(-1); return; } } ou.print(d); return; } public static void main(String[] args) throws IOException { for (int i = 0, t = ni(); i < t; i++) solve(); ou.flush(); } private static final int ni() throws IOException { return sc.nextInt(); } private static final int[] ni(int n) throws IOException { return sc.nextIntArray(n); } private static final long nl() throws IOException { return sc.nextLong(); } private static final long[] nl(int n) throws IOException { return sc.nextLongArray(n); } private static final String ns() throws IOException { return sc.next(); } private static final ContestInputStream sc = new ContestInputStream(); private static final ContestOutputStream ou = new ContestOutputStream(); } final class ContestInputStream extends FilterInputStream { protected final byte[] buf; protected int pos = 0; protected int lim = 0; private final char[] cbuf; public ContestInputStream() { super(System.in); this.buf = new byte[1 << 13]; this.cbuf = new char[1 << 20]; } boolean hasRemaining() throws IOException { if (pos < lim) return true; lim = in.read(buf); pos = 0; return lim > 0; } final int remaining() throws IOException { if (pos >= lim) { lim = in.read(buf); pos = 0; } return lim - pos; } @Override public final int available() throws IOException { if (pos < lim) return lim - pos + in.available(); return in.available(); } @Override public final long skip(long n) throws IOException { if (pos < lim) { int rem = lim - pos; if (n < rem) { pos += n; return n; } pos = lim; return rem; } return in.skip(n); } @Override public final int read() throws IOException { if (hasRemaining()) return buf[pos++]; return -1; } @Override public final int read(byte[] b, int off, int len) throws IOException { if (pos < lim) { int rem = Math.min(lim - pos, len); for (int i = 0; i < rem; i++) b[off + i] = buf[pos + i]; pos += rem; return rem; } return in.read(b, off, len); } public final char[] readToken() throws IOException { int cpos = 0; int rem; byte b; l: while ((rem = remaining()) > 0) { for (int i = 0; i < rem; i++) { b = buf[pos + i]; if (b <= 0x20) { pos += i + 1; cpos += i; if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; break l; } cbuf[cpos + i] = (char) b; } pos += rem; cpos += rem; } char[] arr = new char[cpos]; for (int i = 0; i < cpos; i++) arr[i] = cbuf[i]; return arr; } public final int readToken(char[] cbuf, int off) throws IOException { int cpos = off; int rem; byte b; l: while ((rem = remaining()) > 0) { for (int i = 0; i < rem; i++) { b = buf[pos + i]; if (b <= 0x20) { pos += i + 1; cpos += i; if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; break l; } cbuf[cpos + i] = (char) b; } pos += rem; cpos += rem; } return cpos - off; } public final int readToken(char[] cbuf) throws IOException { return readToken(cbuf, 0); } public final String next() throws IOException { int cpos = 0; int rem; byte b; l: while ((rem = remaining()) > 0) { for (int i = 0; i < rem; i++) { b = buf[pos + i]; if (b <= 0x20) { pos += i + 1; cpos += i; if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; break l; } cbuf[cpos + i] = (char) b; } pos += rem; cpos += rem; } return String.valueOf(cbuf, 0, cpos); } public final char[] nextCharArray() throws IOException { return readToken(); } public final int nextInt() throws IOException { if (!hasRemaining()) return 0; int value = 0; byte b = buf[pos++]; if (b == 0x2d) { while (hasRemaining() && (b = buf[pos++]) > 0x20) value = value * 10 - b + 0x30; } else { do { value = value * 10 + b - 0x30; } while (hasRemaining() && (b = buf[pos++]) > 0x20); } if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; return value; } public final long nextLong() throws IOException { if (!hasRemaining()) return 0L; long value = 0L; byte b = buf[pos++]; if (b == 0x2d) { while (hasRemaining() && (b = buf[pos++]) > 0x20) value = value * 10 - b + 0x30; } else { do { value = value * 10 + b - 0x30; } while (hasRemaining() && (b = buf[pos++]) > 0x20); } if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; return value; } public final char nextChar() throws IOException { if (!hasRemaining()) throw new EOFException(); final char c = (char) buf[pos++]; if (hasRemaining() && buf[pos++] == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; return c; } public final float nextFloat() throws IOException { return Float.parseFloat(next()); } public final double nextDouble() throws IOException { return Double.parseDouble(next()); } public final boolean[] nextBooleanArray(char ok) throws IOException { char[] s = readToken(); int n = s.length; boolean[] t = new boolean[n]; for (int i = 0; i < n; i++) t[i] = s[i] == ok; return t; } public final boolean[][] nextBooleanMatrix(int h, int w, char ok) throws IOException { boolean[][] s = new boolean[h][]; for (int i = 0; i < h; i++) { char[] t = readToken(); int n = t.length; s[i] = new boolean[n]; for (int j = 0; j < n; j++) s[i][j] = t[j] == ok; } return s; } public final String[] nextStringArray(int len) throws IOException { String[] arr = new String[len]; for (int i = 0; i < len; i++) arr[i] = next(); return arr; } public final int[] nextIntArray(int len) throws IOException { int[] arr = new int[len]; for (int i = 0; i < len; i++) arr[i] = nextInt(); return arr; } public final int[] nextIntArray(int len, java.util.function.IntUnaryOperator map) throws IOException { int[] arr = new int[len]; for (int i = 0; i < len; i++) arr[i] = map.applyAsInt(nextInt()); return arr; } public final long[] nextLongArray(int len, java.util.function.LongUnaryOperator map) throws IOException { long[] arr = new long[len]; for (int i = 0; i < len; i++) arr[i] = map.applyAsLong(nextLong()); return arr; } public final int[][] nextIntMatrix(int h, int w) throws IOException { int[][] arr = new int[h][w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) arr[i][j] = nextInt(); return arr; } public final int[][] nextIntMatrix(int h, int w, java.util.function.IntUnaryOperator map) throws IOException { int[][] arr = new int[h][w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) arr[i][j] = map.applyAsInt(nextInt()); return arr; } public final long[] nextLongArray(int len) throws IOException { long[] arr = new long[len]; for (int i = 0; i < len; i++) arr[i] = nextLong(); return arr; } public final long[][] nextLongMatrix(int h, int w) throws IOException { long[][] arr = new long[h][w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) arr[i][j] = nextLong(); return arr; } public final float[] nextFloatArray(int len) throws IOException { float[] arr = new float[len]; for (int i = 0; i < len; i++) arr[i] = nextFloat(); return arr; } public final double[] nextDoubleArray(int len) throws IOException { double[] arr = new double[len]; for (int i = 0; i < len; i++) arr[i] = nextDouble(); return arr; } public final char[][] nextCharMatrix(int h, int w) throws IOException { char[][] arr = new char[h][]; for (int i = 0; i < h; i++) arr[i] = readToken(); return arr; } public final void nextThrow() throws IOException { next(); return; } public final void nextThrow(int n) throws IOException { for (int i = 0; i < n; i++) nextThrow(); return; } } final class ContestOutputStream extends FilterOutputStream implements Appendable { protected final byte[] buf; protected int pos = 0; public ContestOutputStream() { super(System.out); this.buf = new byte[1 << 13]; } @Override public void flush() throws IOException { out.write(buf, 0, pos); pos = 0; out.flush(); } void put(byte b) throws IOException { if (pos >= buf.length) flush(); buf[pos++] = b; } int remaining() throws IOException { if (pos >= buf.length) flush(); return buf.length - pos; } @Override public void write(int b) throws IOException { put((byte) b); } @Override public void write(byte[] b, int off, int len) throws IOException { int o = off; int l = len; while (l > 0) { int rem = Math.min(remaining(), l); for (int i = 0; i < rem; i++) buf[pos + i] = b[o + i]; pos += rem; o += rem; l -= rem; } } @Override public ContestOutputStream append(char c) throws IOException { put((byte) c); return this; } @Override public ContestOutputStream append(CharSequence csq, int start, int end) throws IOException { int off = start; int len = end - start; while (len > 0) { int rem = Math.min(remaining(), len); for (int i = 0; i < rem; i++) buf[pos + i] = (byte) csq.charAt(off + i); pos += rem; off += rem; len -= rem; } return this; } @Override public ContestOutputStream append(CharSequence csq) throws IOException { return append(csq, 0, csq.length()); } public ContestOutputStream append(char[] arr, int off, int len) throws IOException { int o = off; int l = len; while (l > 0) { int rem = Math.min(remaining(), l); for (int i = 0; i < rem; i++) buf[pos + i] = (byte) arr[o + i]; pos += rem; o += rem; l -= rem; } return this; } public ContestOutputStream print(char[] arr) throws IOException { return append(arr, 0, arr.length).newLine(); } public ContestOutputStream print(boolean value) throws IOException { if (value) return append("o"); return append("x"); } public ContestOutputStream println(boolean value) throws IOException { if (value) return append("o\n"); return append("x\n"); } public ContestOutputStream print(boolean[][] value) throws IOException { final int n = value.length, m = value[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) print(value[i][j]); newLine(); } return this; } public ContestOutputStream print(int value) throws IOException { return append(String.valueOf(value)); } public ContestOutputStream println(int value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream print(long value) throws IOException { return append(String.valueOf(value)); } public ContestOutputStream println(long value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream print(float value) throws IOException { return append(String.valueOf(value)); } public ContestOutputStream println(float value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream print(double value) throws IOException { return append(String.valueOf(value)); } public ContestOutputStream println(double value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream print(char value) throws IOException { return append(value); } public ContestOutputStream println(char value) throws IOException { return append(value).newLine(); } public ContestOutputStream print(String value) throws IOException { return append(value); } public ContestOutputStream println(String value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream print(Object value) throws IOException { return append(String.valueOf(value)); } public ContestOutputStream println(Object value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream printYN(boolean yes) throws IOException { if (yes) return println("Yes"); return println("No"); } public ContestOutputStream printAB(boolean yes) throws IOException { if (yes) return println("Alice"); return println("Bob"); } public ContestOutputStream print(CharSequence[] arr) throws IOException { if (arr.length > 0) { append(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').append(arr[i]); } return this; } public ContestOutputStream print(int[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } newLine(); return this; } public ContestOutputStream print(int[] arr, int length) throws IOException { if (length > 0) print(arr[0]); for (int i = 1; i < length; i++) append('\u0020').print(arr[i]); newLine(); return this; } public ContestOutputStream println(int[] arr) throws IOException { for (int i : arr) print(i).newLine(); return this; } public ContestOutputStream println(int[] arr, int length) throws IOException { for (int i = 0; i < length; i++) println(arr[i]); return this; } public ContestOutputStream print(boolean[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } newLine(); return this; } public ContestOutputStream print(long[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } newLine(); return this; } public ContestOutputStream print(long[] arr, int length) throws IOException { if (length > 0) print(arr[0]); for (int i = 1; i < length; i++) append('\u0020').print(arr[i]); newLine(); return this; } public ContestOutputStream println(long[] arr, int length) throws IOException { for (int i = 0; i < length; i++) println(arr[i]); return this; } public ContestOutputStream println(long[] arr) throws IOException { for (long i : arr) print(i).newLine(); return this; } public ContestOutputStream print(float[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } return this; } public ContestOutputStream println(float[] arr) throws IOException { for (float i : arr) print(i).newLine(); return this; } public ContestOutputStream print(double[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } return newLine(); } public ContestOutputStream println(double[] arr) throws IOException { for (double i : arr) print(i).newLine(); return this; } public ContestOutputStream print(Object[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } return newLine(); } public ContestOutputStream print(java.util.ArrayList<?> arr) throws IOException { if (!arr.isEmpty()) { final int n = arr.size(); print(arr.get(0)); for (int i = 1; i < n; i++) print(" ").print(arr.get(i)); } return newLine(); } public ContestOutputStream println(java.util.ArrayList<?> arr) throws IOException { final int n = arr.size(); for (int i = 0; i < n; i++) println(arr.get(i)); return this; } public ContestOutputStream println(Object[] arr) throws IOException { for (Object i : arr) print(i).newLine(); return this; } public ContestOutputStream newLine() throws IOException { return append(System.lineSeparator()); } public ContestOutputStream endl() throws IOException { newLine().flush(); return this; } public ContestOutputStream print(int[][] arr) throws IOException { for (int[] i : arr) print(i); return this; } public ContestOutputStream print(long[][] arr) throws IOException { for (long[] i : arr) print(i); return this; } public ContestOutputStream print(char[][] arr) throws IOException { for (char[] i : arr) print(i); return this; } public ContestOutputStream print(Object[][] arr) throws IOException { for (Object[] i : arr) print(i); return this; } public ContestOutputStream println() throws IOException { return newLine(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
6ae6ccb139e4c9a30a6970733e03285a
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class Array_Recovery { public static void main(String args[]){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); for (int i = 0; i<t; i++){ int[] testList = new int[sc.nextInt()]; sc.nextLine(); for (int k = 0; k<testList.length; k++){ testList[k] = sc.nextInt(); } for (int j: solve(testList)){ System.out.print(j); System.out.print(" "); } System.out.println(); sc.nextLine(); } } public static int[] solve(int[] dList){ int sum = 0; int[] noAns = {-1}; int[] ansList = new int[dList.length]; for (int i = 0; i<dList.length; i++){ if (sum >= dList[i] && dList[i] != 0){ return noAns; } else { sum += dList[i]; ansList[i] = sum; } } return ansList; } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
d17c3338ed745a021d330ab724c64a14
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static int gcd(int a, int b) { if(b == 0) return a; return gcd(b, a % b); } public static void main(String[] args) { int t; Scanner scn = new Scanner(System.in); t = scn.nextInt(); while(t > 0) { t--; int n = scn.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++) { arr[i] = scn.nextInt(); } int result[] = new int[n]; result[0] = arr[0]; int i; for( i=1;i<n;i++) { int num1 = result[i-1] - arr[i]; int num2 = arr[i] + result[i-1]; if(num1 == num2) { result[i] = num1; continue; } if(num1 >= 0 && num2 >=0 ) { break; } if(num1 >= 0) { result[i] = num1; } else { result[i] = num2; } } if(i != n) { System.out.println(-1); continue; } for(i=0;i<n;i++) { System.out.print(result[i]+" "); } System.out.println(); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
183bc0ae0aba1475c3c1851a0b43c5e4
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
//package kg.my_algorithms.Codeforces; import java.util.*; import java.io.*; public class Solution { private static final int[][] knightMoves = {{1,2},{-1,2},{1,-2},{-1,-2},{2,1},{-2,1},{2,-1},{-2,-1}}; private static final FastReader fr = new FastReader(); public static void main(String[] args) throws IOException { BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); StringBuilder sb = new StringBuilder(); int testCases = fr.nextInt(); for(int test=1;test<=testCases;test++){ int n = fr.nextInt(); int[] d = new int[n]; for(int i=0;i<n;i++) d[i] = fr.nextInt(); int[] res = solve(d); for(int r: res) sb.append(r).append(" "); sb.append("\n"); } output.write(sb.toString()); output.flush(); } private static int[] solve(int[] d){ int n = d.length; int[] arr = new int[n]; arr[0] = d[0]; for(int i=1;i<n;i++){ int prev = arr[i-1]; int f = prev-d[i]; int s = prev+d[i]; if(f>=0&&s>=0 && d[i]!=0) return new int[]{-1}; arr[i] = Math.max(f,s); } return arr; } } //Fast Input 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 { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
405fbc1d18263a08744e5d5cdbf8ee85
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class Main2 { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t= Integer.parseInt(scn.nextLine()); for (int i = 0; i < t; i++) { int size = Integer.parseInt(scn.nextLine()); int[] d = new int[size]; int[] a = new int[size]; boolean cancel = false; // String input = scn.nextLine().trim(); String strArr [] = scn.nextLine().trim().split(" "); String str =""; // String [] arr2 = new String[size]; for (int j = 0; j < size; j++) { d[j] = Integer.parseInt(strArr[j]); if (j==0){ a[j] = d[j]; str+= a[j] + " "; } else{ // if (a[j-1] + d[j]>0 && a[j-1] - d[j]>0 && a[j-1] + d[j]!=a[j-1] - d[j]){ // cancel = true; // break; // } // else { // a[j] = a[j-1] + Math.abs(d[j]); // } if (a[j-1]< Math.abs(d[j]) || d[j]==0){ a[j] = a[j-1] + Math.abs(d[j]); } else { cancel = true; break; } } } if (cancel){ System.out.println(-1); } else { for (int h : a) { System.out.print(h + " "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
9d38406c6c2a12b6126d022d5fc3d9af
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import static java.lang.Math.round; public class sol{ public static void main(String args[]) { // https://codeforces.com/problemset/problem/1566/C // https://codeforces.com/problemset/problem/1739/B Scanner sc=new Scanner(System.in); int t=sc.nextInt(); // StringBuilder finalSbr= new StringBuilder(); while(t-->0){ int n = sc.nextInt(); boolean flag=true; StringBuilder sbr= new StringBuilder(); int previous=-1; int prevSum=0; for(int i=0;i<n;i++){ int tempV=sc.nextInt(); if(i>=1&&prevSum>=tempV&&tempV!=0){ // System.out.println("dklfjlkdsfj"); flag=false; } if(i==0){ prevSum=tempV; sbr.append(tempV+" "); } else{ prevSum=prevSum+tempV; sbr.append(prevSum+" "); } // System.out.println("i="+i+" prevS="+prevSum+" tempv="+tempV); // if() previous=tempV==0?previous:tempV; } System.out.println(flag?sbr:-1); } // https://codeforces.com/problemset/status/1739/problem/B } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
ec6430cc848efe4b015f5a8111be21e1
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class Main { static int check(int[] arr , int a,int x,int max){ boolean x1=false; if(x<=0) return -1; for(int i=x;i>=0;i--){ int gcd=0; //int lcm = Math.max(arr[i],arr[x]); for(int j=1;j<=arr[i] && j<=arr[x];j++){ if((arr[i]%j==0 && arr[x]%j==0)){ gcd=j;}} if(gcd==1){ x1 =true; a=i; break;} } if( x1 == true) { // System.out.println(a+" "+x); return a+x+2;} else x--; return a+check(arr,a,x,arr[x]); } static int gcd(int m, int n){ if(m<n) return gcd(n,m); if(m%n==0) return n ; return gcd(n,m%n); } public static void main(String[] args) { // System.out.println("Hello World"); Scanner scan = new Scanner(System.in); int l = scan.nextInt(); while(l-->0){ int n = scan.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++){ arr[i]= scan.nextInt();} int[] rarr = new int[n]; rarr[0]=arr[0]; boolean bool =false; for(int i=0;i<n;i++){ if(i<n-1){ if( arr[i+1]>rarr[i] || arr[i+1]==0){ rarr[i+1]= arr[i+1]+rarr[i]; continue; } // if(i==n-1) break; else{ bool= true; break; }} } if(bool){ System.out.println("-1"); } else { for(int i=0;i<n;i++){ System.out.print(rarr[i]+" "); } System.out.println(""); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
1a556019db10d26d4c9e44e528faee08
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.io.*; public class User { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String [] line = br.readLine().split(" "); int t = Integer.parseInt(line[0]); for(int i = 0; i < t; i++) { line = br.readLine().split(" "); int n = Integer.parseInt(line[0]); int [] arr = new int [n]; line = br.readLine().split(" "); for(int j = 0; j < line.length; j++) arr[j] = Integer.parseInt(line[j]); showResult(arr); } br.close(); } static void showResult(int [] arr) { if(arr.length == 1){ System.out.println(arr[0]); return; } boolean flag = true; int [] original = new int [arr.length]; original[0] = arr[0]; for(int i = 1; i < original.length; i++){ original[i] = original[i-1] + arr[i]; if(original[i-1] >= arr[i] && arr[i] != 0){flag = false; break;} } if(flag){Arrays.stream(original).forEach(a -> System.out.print(a+" ")); System.out.println(); } else System.out.println(-1); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
87c8367de0d0c1d2977b30eaaf5a9cd0
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class TaskB { public static void main(String[] args) { FastReader reader = new FastReader(); int tt = reader.nextInt(); // int tt = 1; for (; tt > 0; tt--) { int n = reader.nextInt(); int[] arr = new int[n]; boolean isOk = true; for (int i = 0; i < n; i++) { int a = reader.nextInt(); if (i == 0) { arr[i] = a; } else if (a > 0 && arr[i-1] >= a) { isOk = false; } else { arr[i] = arr[i-1] + a; } } if (isOk) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(arr[i]).append(" "); } System.out.println(sb); } else { System.out.println(-1); } } } 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 { if (st.hasMoreTokens()) { str = st.nextToken("\n"); } else { str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
4181f8ff3ce16ab8ae3f83a791038515
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class TaskB { static int[] result = null; public static void main(String[] args) { FastReader reader = new FastReader(); int tt = reader.nextInt(); // int tt = 1; mainLoop: for (; tt > 0; tt--) { int n = reader.nextInt(); int[] d = new int[n]; for (int i = 0; i < n; i++) { d[i] = reader.nextInt(); } result = null; if (tryFind(0, d, new int[n])) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(result[i]).append(" "); } System.out.println(sb); } else { System.out.println(-1); } } } static boolean tryFind(int i, int[] d, int[] arr) { if (i >= arr.length) { if (result != null) { return false; } else { result = Arrays.copyOf(arr, arr.length); return true; } } if (i == 0) { arr[0] = d[0]; return tryFind(i+1, d, arr); } arr[i] = arr[i-1] + d[i]; if (tryFind(i+1, d, arr)) { if (d[i] > 0) { arr[i] = arr[i - 1] - d[i]; if (arr[i] >= 0) { return tryFind(i + 1, d, arr); } } return true; } else { return false; } } 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 { if (st.hasMoreTokens()) { str = st.nextToken("\n"); } else { str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
ae701e5cab070e2e9dfb45b836a01109
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class Codechef_Contest_2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); ArrayList<Integer> a=new ArrayList<>(); int n=sc.nextInt(); while(n>0){ int k=sc.nextInt(); int p=0; int h=0; while(k>0){ int l=sc.nextInt(); if(l<=p && l!=0){ h=1; } a.add(p+l); p=p+l; k--; } if(h==0){ for(Integer ele:a){ System.out.print(ele+" "); } } else{ System.out.println(-1); } a.clear(); n--; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
69f84094e526f9fb8aba8fa6534ff6f7
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class BroCoders { static Scanner sc=null; public static void main(String[] args) { sc = new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { solve(); } } public static void solve(){ int n=sc.nextInt(); int d[]=new int[n]; for(int i=0;i<n;i++) { d[i]=sc.nextInt(); } int a[]=new int[n]; a[0]=d[0]; for(int i=1;i<n;i++) { int temp1=d[i]+a[i-1]; int temp2=a[i-1]-d[i]; if(temp1 != temp2 && (temp1>=0 && temp2>=0)) { System.out.println(-1); return; } else { a[i]=Math.max(temp1, temp2); } } for(int i=0;i<n;i++) { System.out.print(a[i]+" "); } System.out.println(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
91a6cfd9e7a8fe9006ae1c686ace06a6
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { try (Kattio reader = new Kattio()) { int t = reader.nextInt(); Outer: while (t-- > 0) { int n = reader.nextInt(); int[] A = new int[n]; for (int i = 0; i < n; ++i) A[i] = reader.nextInt(); int[] res = new int[n]; res[0] = A[0]; for (int i = 1; i < n; ++i) { res[i] = A[i] + res[i - 1]; if (A[i] != 0 && res[i - 1] >= A[i]) { reader.println(-1); continue Outer; } } for (int i = 0; i < n; ++i) reader.print(res[i] + " "); reader.println(); } } } } class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(problemName + ".out"); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
df931138d5ba823f21acb07f132d0be6
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class B { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int nbCases = scanner.nextInt(); for (int testCase = 0; testCase < nbCases; ++testCase) { int arraySize = scanner.nextInt(); boolean multiplePossibilities = false; int[] a = new int[arraySize]; a[0] = scanner.nextInt(); for (int i = 1; i < arraySize; i++) { int next = scanner.nextInt(); if (a[i-1]-next >= 0 && next != 0 && !multiplePossibilities) { System.out.println(-1); multiplePossibilities = true; } a[i] = a[i-1]+next; } if (!multiplePossibilities) { for (int i = 0; i < arraySize; ++i) { System.out.print(a[i] + " "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
3d53d6820a908b4d8a5ba39c1139d838
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class ArrayRecovery { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int test = scan.nextInt(); while(test-- >0) { int n = scan.nextInt(); long [] d = new long[n]; for (int i = 0 ; i<n;i++) { d[i] = scan.nextLong(); } int count1= 0 ; for(int i = 0;i <n-1;i++) { if(((d[i]-d[i+1])>=0) && d[i+1]!=0) { count1++; break; } else { d[i+1] = d[i]+d[i+1]; } } if(count1>0) { System.out.println(-1); } else { for (int i=0;i<n;i++) { if(i!= n-1) { System.out.print(d[i]+" "); } else{ System.out.println(d[i]); } } } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
e9ffe9e9da9116f0ae107acf18bd70b5
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.*; import static java.lang.System.*; public class CP0026 { public static void main(String args[]) throws Exception { PrintWriter pw = new PrintWriter(out); FastReader sc = new FastReader(); Reader s = new Reader(); int test; //code yaha se hai try { test = sc.nextInt(); } catch (Exception E) { return; } while (test-- > 0) { boolean pos = true ; int n = sc.nextInt(); ArrayList<Integer> ar = new ArrayList<>() ; int arr[] = new int[n] ; ar.add(sc.nextInt()) ; for (int i = 1; i < n; i++) { ar.add(sc.nextInt()) ; } arr[0] = ar.get(0) ; for (int i = 1; i < n; i++) { arr[i] = arr[i-1] + ar.get(i) ; if( arr[i-1] - ar.get(i) >= 0 && ar.get(i) != 0 ) { pos = false; break; } } if(pos){ for (int i = 0; i < n; i++) { out.print(arr[i] + " "); } out.println(); } else { out.println(-1); } } pw.close(); } static long binSearch(long n){ long st = 1 , end = n ; long mid , k , ans = 1 ; while (st <= end){ mid = (end-st)/2 + st ; k = (mid * (mid+1)) / 2 ; if(n <= k){ if(n == k){ return mid ; } ans = mid ; end = mid-1 ; } else { st = mid+1 ; } } return ans ; } public static int funcSort(int[] inputList) { int arr[] = new int[inputList.length +1 ] ; arr[0] = 0 ; for (int i = 0; i < inputList.length; i++) { arr[i+1] = arr[i] + inputList[i] ; } for (int i = 0; i < arr.length; i++) { out.println("ch " + arr[i]); } int minAns = Integer.MAX_VALUE ; for (int i = 1; i < arr.length-1 ; i++) { out.println( (arr[i] / i) + " " + ((arr[arr.length-1] -arr[i]) / (arr.length-i-1)) + " " + (arr[arr.length-1] -arr[i]) +" " + (arr.length-1-i) ); if(Math.abs( (arr[i] / i) - ((arr[arr.length-1] -arr[i]) / (arr.length-i-1)) ) < minAns){ minAns = Math.abs( (arr[i] / i) - ((arr[arr.length-1] -arr[i]) / (arr.length-i-1)) ); } LinkedList<Integer> ls = new LinkedList<>(); } return minAns ; } /* Note : Try using sorting , when approach is of O(N^2) As sorting takes : O( N Log N ) */ /* len : Length of Array / inputs num : key to map //Putting values in map <Integer , ArrayList> for (int i = 0; i < len; i++) { num =sc.nextInt(); if(!mp.containsKey(num)){ mp.put(num ,new ArrayList<>()); } } //getting size of ArrayList for(Integer key : mp.keySet()){ maxValue = max(maxValue , mp.get(key).size()); } */ /* Sieve boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } */ static long exponentiation(long base, long exp) { long N = 1000000007 ; if (exp == 0) return 1; if (exp == 1) return base % N; long t = exponentiation(base, exp / 2); t = (t * t) % N; // if exponent is even value if (exp % 2 == 0) return t; // if exponent is odd value else return ((base % N) * t) % N; } public static int BinarySearch (long ar[] , int start , int end , long key , boolean lower){ int mid ; boolean found = false; int ans = 0; if(lower){ while(start <= end){ mid = start + (end-start) / 2; if(ar[mid] < key ){ if(!found){ ans = mid; } start = mid+1; } else{ if(ar[mid] == key){ found = true; ans = mid ; } end = mid-1; } } } else{ while(start <= end){ mid = start + (end-start) / 2; if(ar[mid] <= key ){ if(ar[mid] == key){ found = true; ans = mid; } start = mid+1; } else{ if(!found){ ans = mid; } end = mid-1; } } } return ans; } public static int BinarySearchArrayList (ArrayList<Long> ar , int start , int end , long key , boolean lower){ int mid ; boolean found = false; int ans = -1; if(lower){ while(start <= end){ mid = start + (end-start) / 2; if(ar.get(mid) < key ){ if(!found){ ans = mid; } start = mid+1; } else{ if(ar.get(mid) == key){ found = true; ans = mid ; } end = mid-1; } } } else{ while(start <= end){ mid = start + (end-start) / 2; if(ar.get(mid) <= key ){ if(ar.get(mid) == key){ found = true; ans = mid; } start = mid+1; } else{ if(!found){ ans = mid; } end = mid-1; } } } return ans; } public static String reverseString(String str){ StringBuilder input1 = new StringBuilder(); // append a string into StringBuilder input1 input1.append(str); // reverse StringBuilder input1 input1.reverse(); return input1+""; } public static void sort(int[] arr){ //because Arrays.sort() uses quick sort , Worst Complexity : o(N^2) ArrayList <Integer> ls = new ArrayList<>(); for(int x:arr){ ls.add(x); } Collections.sort(ls); //Collections.sort(arrayList) : Uses Merge Sort : Complexity O(NlogN) for(int i = 0 ; i < arr.length ; i++){ arr[i] = ls.get(i); } } public static long gcd(long a , long b){ if(a>b){ a = (a+b) - (a = b); } if(a == 0L){ return b; } return gcd(b%a ,a); } public static boolean isPrime(long n){ if(n < 2){ return false; } if(n == 2 || n == 3){ return true; } if(n % 2 == 0 || n % 3 == 0){ return false; } long sq = (long)Math.sqrt(n) + 1; for(long i = 5L; i <=sq ; i=i+6){ if(n % i == 0 || n % (i+2) == 0){ return false; } } return true; } public static class Pair implements Comparable<Pair> { long first ; long second; Pair(long fst , long scnd){ first = fst; second = scnd; } public int compareTo(Pair obj){ if(this.first > obj.first){ return 1; } else if(this.first < obj.first){ return -1; } return 0; } } 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') { if (cnt != 0) { break; } else { continue; } } 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(); } } 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
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
a3c580f5c286aee70c777ba7c3dbcd5f
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 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 a[]=new int[n]; int temp[]=new int[n]; int temp1[]=new int[n]; int c=0; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } for(int i=0;i<n;i++){ c+=a[i]; temp[i]=c; } int c1=a[0]; temp1[0]=a[0]; for(int i=1;i<n;i++){ c1-=a[i]; if(c1<0){ temp1[i]=temp[i]; c1=temp[i]; } else temp1[i]=c1; } if(Arrays.equals(temp,temp1)){ for(int i=0;i<n;i++){ System.out.print(temp[i]+" "); } } else{ System.out.println(); System.out.println(-1); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
fafe75e7aa5cdb348b1a8ac50dac4957
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; public class ArrayRecovery { public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); for (int i = 0; i < t; i++) { int n=s.nextInt(); int[] d=new int[n]; int[] a=new int[n]; for (int j = 0; j < n; j++) { d[j]=s.nextInt(); } if(n==1){ System.out.println(d[0]); } else { a[0] = d[0]; int count=0; for (int j = 1; j < n; j++) { if(d[j]==0){ a[j]=a[j-1]; } else if(a[j-1]>=d[j]){ System.out.println("-1"); count++; break; } else{ a[j]=d[j]+a[j-1]; } } if(count==0){ for (int j = 0; j < n; j++) { System.out.println(a[j]); } } } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
e2462f8a3ef904eb698ace17ffb1a98f
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
/* package codechef; // don't place package name! */ import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; 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 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') { if (cnt != 0) { break; } else { continue; } } 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 long gcd(long a,long b) { long temp = 0; while (b != 0) { temp = b; b = a % b; a = temp; } return a; } public static void main (String[] args) throws java.lang.Exception { // your code goes here //Reader s=new Reader(); Reader s=new Reader(); StringBuilder sb=new StringBuilder(); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int d[]=new int[n]; for(int i=0;i<n;i++) { d[i]=s.nextInt(); } int f=0; int a[]=new int[n]; a[0]=d[0]; for(int i=1;i<n;i++) { int k=d[i]+a[i-1]; int k2=a[i-1]-d[i]; if(k>=0&&k2>=0&&k2!=k) { f=1; break; } else { a[i]=Math.max(k,k2); } } if(f==0) for(int i=0;i<n;i++) sb.append(a[i]+" "); else sb.append("-1"); sb.append("\n"); } System.out.println(sb); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output
PASSED
59da77ced49c8632665c8731b23d8aeb
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
/* package codechef; // don't place package name! */ import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; 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 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') { if (cnt != 0) { break; } else { continue; } } 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 long gcd(long a,long b) { long temp = 0; while (b != 0) { temp = b; b = a % b; a = temp; } return a; } public static void main (String[] args) throws java.lang.Exception { // your code goes here //Reader s=new Reader(); Reader s=new Reader(); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int d[]=new int[n]; for(int i=0;i<n;i++) { d[i]=s.nextInt(); } int f=0; int a[]=new int[n]; a[0]=d[0]; for(int i=1;i<n;i++) { int k=d[i]+a[i-1]; int k2=a[i-1]-d[i]; if(k>=0&&k2>=0&&k2!=k) { f=1; break; } else { a[i]=Math.max(k,k2); } } if(f==0) for(int i=0;i<n;i++) System.out.print(a[i]+" "); else System.out.print("-1"); System.out.println(); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 17
standard input
[ "constructive algorithms", "greedy", "math" ]
f4b790bef9a6cbcd5d1f9235bcff7c8f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$0 \le d_i \le 100$$$) — the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints.
1,100
For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$.
standard output