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
fef51502f4c1ff58d128291bd18ae2cc
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[] d=new int[n]; int[] a=new int[n]; for(int i=0;i<n;i++){ d[i]=sc.nextInt(); } int c=0; a[0]=d[0]; for(int i=1;i<n;i++){ int v1= d[i] + a[i-1]; int v2= a[i-1] -d[i]; if(v1>-1 && v2 >-1 && v1!=v2){ System.out.print(-1); c=1; break; } if(v1>-1){ a[i]=v1; }else if(v2>-1){ a[i]=v2; } } if(c==0){ for(int i=0;i<n;i++){ System.out.print( a[i] + " "); }} System.out.println(); 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 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
1d4e4957b23d516d6d15a63da1ae504d
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 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); boolean oneVariant = true; int[] result = new int[n]; int prev = in.nextInt(); if (n < 1) { return; } result[0] = prev; for (int i = 1; i < n; ++i) { int value = in.nextInt(); if (prev - value >= 0 && value != 0) { oneVariant = false; } prev = prev + value; result[i] = prev; } if (!oneVariant) { System.out.println(-1); } else { for (int 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 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
157a0aeab75ac7c294c807a3efc9ed5f
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; /** * For an array of non-negative integers a of size n, we construct another array d as follows: d1=a1, di=|ai−ai−1| for 2≤i≤n. * * Your task is to restore the array a from a given array d, or to report that there are multiple possible arrays. */ public class ArrayRecovery { private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int testCases = Integer.parseInt(reader.readLine()); for (int i = 0; i < testCases; i++) { check(); } } private static void check() throws IOException { int elements = Integer.parseInt(reader.readLine()); Integer[] transformed = getIntLine(); int previous = transformed[0]; StringBuilder original = new StringBuilder(String.valueOf(previous)); for (int i = 1; i < elements; i++) { int current = previous + transformed[i]; int alternativeCurrent = previous - transformed[i]; if (alternativeCurrent >= 0 && current != alternativeCurrent) { System.out.println(-1); return; } original.append(" ").append(current); previous = current; } System.out.println(original); } private static Integer[] getIntLine() throws IOException { return Arrays.stream(reader.readLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new); } }
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
b014efcd1671dedb6ce2ca0b1b47201a
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 Pset42 { 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[] a = new int[n]; a[0] = d[0]; boolean flag = true; for (int i = 1; i < n; i++) { int first = Math.abs(a[i-1] - d[i]); int second = Math.abs(d[i] + a[i-1]); if (d[i] != 0 && a[i-1] != 0) { if (Math.abs(first - a[i-1]) == d[i] && Math.abs(second - a[i-1]) == d[i]) { flag = false; break; } } if (Math.abs(first - a[i-1]) == d[i]) { a[i] = first; } if (Math.abs(second - a[i-1]) == d[i]) { a[i] = second; } } if (flag) { for (int num : a) { System.out.print(num + " "); } System.out.println(); } else { System.out.println(-1); } } 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 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
c54bac2123029f2674511a4f237ebb9a
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 long mod = (long) 1e9 + 7; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); long t=Long.parseLong(br.readLine()); while(t-->0){ int n=Integer.parseInt(br.readLine()); String[] in=br.readLine().split(" "); // int n=Integer.parseInt(in[0]); // long k=Long.parseLong(in[1]); // long d=Long.parseLong(in[2]); // int n=Integer.parseInt(in[0]); // int m=Integer.parseInt(in[1]); // // int m=Integer.parseInt(br.readLine()); // String[] in1=br.readLine().split(" "); // String[] in2=br.readLine().split(" "); long[] a=new long[n]; // long[] b=new long[m]; // ArrayList<Long>l1=new ArrayList<>(); for(int i=0;i<n;i++){ a[i]=Long.parseLong(in[i]); } ArrayList<Long>l1=new ArrayList<>(); l1.add(a[0]); boolean f=false; for(int i=1;i<n;i++){ if(a[i]!=0 && l1.get(i-1)-a[i]>=0) { f=true; break; } else{ l1.add(l1.get(i-1)+a[i]); } } if(f) out.println("-1"); else{ for(long i:l1){ out.print(i+" "); } out.println(); } } out.close(); } static void swap(int[] a,int x,int y){ a[x]=a[x]^a[y]; a[y]=a[x]^a[y]; a[x]=a[x]^a[y]; } static int memo(int c,int i,int j,long sum,long[] a,int[][] dp){ if(sum<0) return 0; if(i<0 || j>=a.length){ return 1; } if(dp[i][j]!=-1) return dp[i][j]; int left=memo(i-1,i-1,j,sum+a[c],a,dp); int right=memo(j+1,i,j+1,sum+a[c],a,dp); return dp[i][j]=Math.max(left,right); } static long n_of_factors(long[][] a) { long ans = 1; for (int i = 0; i < a.length; i++) { ans = ans * (a[i][1] + 1) % mod; } return ans % mod; } static long sum_of_factors(long[][] a) { long ans = 1; for (int i = 0; i < a.length; i++) { long res = (((expo(a[i][0], a[i][1] + 1) - 1) % mod) * (modular_inverse(a[i][0] - 1)) % mod) % mod; ans = ((res % mod) * (ans % mod)) % mod; } return (ans % mod); } static long prod_of_divisors(long[][] a) { long ans = 1; long d = 1; for (int i = 0; i < a.length; i++) { long res = expo(a[i][0], (a[i][1] * (a[i][1] + 1) / 2)); ans = ((expo(ans, a[i][1] + 1) % mod) * (expo(res, d) % mod)) % mod; d = (d * (a[i][1] + 1)) % (mod - 1); } return ans % mod; } static long expo(long a, long b) { long ans = 1; a = a % mod; while (b > 0) { if ((b & 1) == 1) { ans = ans * a % mod; } a = a * a % mod; b >>= 1; } return ans % mod; } static long modular_inverse(long a) { return expo(a, mod - 2) % mod; } static long phin(long a) { if (isPrime(a)) return a - 1; long res = a; for (int i = 2; i * i <= (int) a; i++) { if (a % i == 0) { while (a % i == 0) { a = a / i; } res -= res / i; } } if (a > 1) { res -= res / a; } return res; } static boolean isPrime(long a) { if (a < 2) return false; for (int i = 2; i * i <= (int) a; i++) { if (a % i == 0) return false; } return true; } static long catlan(long a) { long ans = 1; for (int i = 1; i <= a; i++) { ans = ans * (4 * i - 2) % mod; ans = ((ans % mod) * modular_inverse(i + 1)) % mod; } return ans % mod; } static HashMap<Long, Long> primeFactors(long n) { HashMap<Long, Long> m1 = new HashMap<>(); for (int i = 2; (long) i * (long) i <= n; i++) { if (n % i == 0) { while (n % i == 0) { m1.put((long) i, m1.getOrDefault(i, 0l) + 1); n = n / i; } } } return m1; } static long gcd(long a,long b){ a=Math.abs(a); b=Math.abs(b); if(b==0) return a; return gcd(b,a%b); } static boolean isPalin(String s){ int i=0; int j=s.length()-1; while(i<j){ if(s.charAt(i)!=s.charAt(j)) return false; i++; j--; } return true; } static boolean valid(int[] a,long mid){ long c=0; for(int i:a){ c+=mid-i; } return c>=mid; } // } } class pair{ long a; long b; pair(long a,long b){ this.a=a; this.b=b; } } class fabric{ String c; int d; int id; int i; fabric(String c,int d,int id,int i){ this.c=c; this.d=d; this.id=id; this.i=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
df73ef159ca8dee1dbfff2f28bafcf0d
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 jdk.jshell.execution.JdiInitiator; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Codeforces_Template { static FastReader scan =new FastReader(); static final Random random=new Random(); static long mod=1000000007L; //static HashMap<String,Integer> map=new HashMap<>(); //=======================Main Program========================== public static void main(String args[]) throws IOException { int t = scan.nextInt(); while(t-->0){ int n = scan.nextInt(); int[] d = new int[n]; int temp = 0; temp = scan.nextInt(); d[0] = temp; boolean ch = true; for(int i = 0; i < n-1; i++){ int num = scan.nextInt(); if(ch){ if((d[i]-num)>=0 && (d[i]-num) != (d[i]+num)){ ch = false; }else { d[i+1] = d[i]+num; } } } if(ch){ for(int i : d){ System.out.print(i+" "); } System.out.println(); } else { System.out.println(-1); } } } //=======================Useful Functions====================== static boolean equal(int[] a, int[] b){ for(int i = 0; i < a.length; i++){ if(a[i] != b[i]){ return false; } } return true; } static int non_equal(int[] a, int[] b){ int count = 0; for(int i = 0; i < a.length; i++){ if(a[i] != b[i]){ count++; } } return count; } static int min(int a, int b, int c){ int min = 0; min = a; if(min>b){ min = b; } if(min>c){ min = c; } return min; } static int max(int a, int b, int c){ int max = 0; max = a; if(max<b){ max = b; } if(max<c){ max = c; } return max; } static int min(int a, int b){ if(a>b){ return b; } else { return a; } } static int max(int a, int b) { if(a<b) return b; return a; } static int difference(int a, int b){ if(a > b){ return a-b; } else { return b-a; } } static void ruffleSort(int[] a) { int n=a.length; 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 < E > void print(E res) { System.out.println(res); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int abs(int a) { if(a<0) return -1*a; return 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; } int [] readintarray(int n) { int res [] = new int [n]; for(int i = 0; i<n; i++)res[i] = nextInt(); return res; } long [] readlongarray(int n) { long res [] = new long [n]; for(int i = 0; i<n; i++)res[i] = nextLong(); return res; } } }
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
f21726b927a1e2ac9bb762d74599d156
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 class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } private static final int modulo = 1000000007; private static int evenCount = 0; private static int oddCount = 0; private static long arraySum = 0L; private static long onesCountInBinary = 0L; public static Scanner scan = new Scanner(System.in); public static void main(String[] args){ int testCase = scan.nextInt(); for (int vedant = 0; vedant < testCase; vedant++) { solve(); } // out.println(5); } private static void solve() { // ******************** S O L V E M E T H O D ***************** // ************* M A I N L O G I C G O E S H E R E ********** int size = scan.nextInt(); int[] arr = readIntArray(size); int[] answer = new int[size]; answer[0] = arr[0]; for (int i = 1; i < size; i++) { int element = answer[i - 1] + arr[i]; if (answer[i - 1] - arr[i] >= 0 && arr[i] != 0) { System.out.println(-1); return; } answer[i] = element; } printArray(answer); // **************************** S O L U T I O N E N D S H E R E **************************** } private static String readBinaryString() { String binary = scan.next(); onesCountInBinary = getOnesCountInBinary(binary); return binary; } private static long getOnesCountInBinary(String binary) { onesCountInBinary = 0L; for (int i = 0; i < binary.length(); i++) { if (binary.charAt(i) == '1') { onesCountInBinary++; } } return onesCountInBinary; } private static int lowerBound(int[] arr, int target) { int starting = 0; int ending = arr.length - 1; int answer = -1; while (starting <= ending) { int mid = starting + ((ending - starting) / 2); if (arr[mid] == target) { ending = mid - 1; answer = mid; } else if (arr[mid] < target) { starting = mid + 1; } else { ending = mid - 1; } } return answer; } public int upperBound(int[] arr, int target) { int starting = 0; int ending = arr.length - 1; int answer = -1; while (starting <= ending) { int mid = starting + ((ending - starting) / 2); if (arr[mid] == target) { answer = mid; starting = mid + 1; } else if (arr[mid] < target) { starting = mid + 1; } else { ending = mid - 1; } } return answer; } private static void printArray(long[] arr) { StringBuilder output = new StringBuilder(); for (long j : arr) { output.append(j).append(" "); } System.out.println(output); } private static ArrayList<Integer> readArrayList(int size) { ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < size; i++) { arr.add(scan.nextInt()); if ((arr.get(i) & 1) == 0) { evenCount++; } else { oddCount++; } arraySum += arr.get(i); } return arr; } private static void printArray(int[] arr) { StringBuilder output = new StringBuilder(); for (int j : arr) { output.append(j).append(" "); } System.out.println(output); } private static void printYES() { System.out.println("YES"); } private static void printNO() { System.out.println("NO"); } private static long getMaximumFromList(List<Long> arr) { long max = Long.MIN_VALUE; for (Long aLong : arr) { max = Math.max(max, aLong); } return max; } public static int binarySearch(int[] arr, int target) { int starting = 0; int ending = arr.length - 1; while (starting < ending) { int mid = starting + (ending - starting) / 2; if (arr[mid] == target) { return mid; } if (arr[mid] > target) { ending = mid - 1; } else { starting = mid + 1; } } return -1; } public static int binarySearch(long[] arr, long target) { int starting = 0; int ending = arr.length - 1; while (starting < ending) { int mid = starting + (ending - starting) / 2; if (arr[mid] == target) { return mid; } if (arr[mid] > target) { ending = mid - 1; } else { starting = mid + 1; } } return -1; } public static int gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } public static long gcd(long a, long b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } private static int[] readIntArray(int size) { int[] arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = scan.nextInt(); if ((arr[i] & 1) == 1) { oddCount++; } else { evenCount++; } arraySum += arr[i]; } return arr; } private static long[] readLongArray(int size) { long[] arr = new long[size]; for (int i = 0; i < size; i++) { arr[i] = scan.nextLong(); if ((arr[i] & 1) == 1) { oddCount++; } else { evenCount++; } arraySum += arr[i]; } return arr; } private static boolean isVowel(char charAt) { charAt = Character.toLowerCase(charAt); return charAt == 'a' || charAt == 'e' || charAt == 'i' || charAt == 'o' || charAt == 'u'; } private static long binaryToDecimal(String binary) { int expo = 0; int index = binary.length() - 1; long answer = 0; while (index >= 0) { answer += Math.pow(2, expo) * Integer.parseInt(String.valueOf(binary.charAt(index))); index--; expo++; } return answer; } public static boolean isPrime(int 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 (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } private static long getFrequency(List<Long> array, long target) { long count = 0; for (long element : array) { if (element == target) { count++; } } return count; } private static int binarySearch(ArrayList<Integer> arr, int target) { int starting = 0; int ending = arr.size() - 1; int mid; while (starting <= ending) { mid = starting + (ending - starting) / 2; if (arr.get(mid) == target) { return mid; } if (arr.get(mid) > target) { ending = mid - 1; } else { starting = mid + 1; } } return -1; } private static int binarySearch(ArrayList<Long> arr, long target) { int starting = 0; int ending = arr.size() - 1; int mid; while (starting <= ending) { mid = starting + (ending - starting) / 2; if (arr.get(mid) == target) { return mid; } if (arr.get(mid) > target) { ending = mid - 1; } else { starting = mid + 1; } } return -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
92bb86f4e49dfaddfb78a32630612604
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
/** * Surly * 21.10.2022 **/ import java.util.*; import java.math.*; import java.io.*; public class Main { static Scanner sc = new Scanner(System.in); static PrintWriter pr = new PrintWriter(System.out); public static void main (String[] args) throws IOException{ double starttime = System.currentTimeMillis(); int t = sc.nextInt(); while(t-->0) solve(); pr.close(); double endtime=System.currentTimeMillis(); System.err.println("Elapsed time : "+(endtime-starttime)+" ms."); } public static void solve() { int n = sc.nextInt(); Vector<Integer> v = new Vector<Integer>(); for(int i=0;i<n;i++) { int x = sc.nextInt(); v.add(x); } Vector<Integer> res = new Vector<Integer>(Collections.nCopies(n,0)); int indx =0; res.set(0,v.get(0)); boolean good = true; for(int i=1;i<n;i++) { if(res.get(i-1)>=v.get(i) && v.get(i)>0) good = false; res.set(i,res.get(i-1)+v.get(i)); } if(good == false) { pr.print(-1); } else{ for(var i : res) pr.print(i+" "); } pr.println(); } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
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
b5d04e2c6abcfce2dae061ac87de0e31
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 [] d = new int[n]; int [] a = new int[n]; for(int i=0;i<n;i++) d[i] = sc.nextInt(); a[0] = d[0]; int flag = 0; for(int i=1;i<n;i++){ int diff1 = a[i-1] + d[i]; int diff2 = a[i-1] - d[i]; if(diff1 >= 0 && diff2 >= 0){ if(diff1 != diff2){ System.out.println("-1"); flag = 1; break; } } a[i] = diff1; } if(flag == 0){ 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 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
0eaeb1c804231be36614d38bdf4a399d
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
// Source: https://usaco.guide/general/io import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ int[] d = new int[sc.nextInt()]; for(int i=0;i<d.length;i++){ d[i] = sc.nextInt(); } int[] a = new int[d.length]; a[0] = d[0]; boolean multiple = false; for(int i=1;i<a.length;i++){ if(a[i-1]>=d[i] && d[i]!=0){ multiple = true; break; } else a[i]=d[i]+a[i-1]; } if(!multiple){ for(int num:a){ pw.print(num+ " "); } pw.println(); } else pw.println(-1); } /* * Make sure to include the line below, as it * flushes and closes the output stream. */ 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 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
f5cfbcc11632d5d011ec49ac2b083671
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 P1739B { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int testcases = scanner.nextInt(); String[] out = new String[testcases]; for (int i = 0; i < testcases; i++) { int[] d = new int[scanner.nextInt()]; int[] a = new int[d.length]; String output = ""; for (int j = 0; j < d.length; j++) { d[j] = scanner.nextInt(); } for (int j = 0; j < d.length; j++) { if (j == 0) { a[j] = d[j]; output += "" + d[j]; } else { if (d[j] <= a[j - 1] && d[j] > 0) { output = "-1"; break; } a[j] = d[j] + a[j - 1]; output += " " + (d[j] + a[j - 1]); } } out[i] = output; } scanner.close(); for (String line : out) { System.out.println(line); } } }
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
51b351847a4e1bdfbfa7484ede3402bb
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.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class Main{ static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null || !st.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(); } } /// static Scanner sl = new Scanner(System.in); public static void main(String[] args) { FastReader s=new FastReader(); // FastWriter out = new FastWriter(); int t = s.nextInt(); while(t-->0){ int n = s.nextInt(); int arr[] = new int[n]; int ar[]= new int [n]; for(int i=0;i<n;i++){ int ll = s.nextInt(); arr[i] = ll; if(i ==0) ar[i] = arr[i]; else ar[i] = arr[i] + ar[i-1]; } boolean dis = true; for(int i=0,j=1;i<n && j<n;i++,j++){ if(ar[i] >= arr[j] && arr[j] != 0){ dis = false; break; } } if(dis){ for(int i=0;i<n;i++){ System.out.print(ar[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 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
c69cacf35f39bd5b71716a3b0e58b876
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) { long n = sc.nextInt(); long arr[] = new long[(int) n]; for (long i = 0; i < n; i++) { arr[(int) i] = sc.nextInt(); } long arr2[] = new long[(int) n]; arr2[0] = arr[0]; boolean flag = false; for (long i = 1; i < n; i++) { long a = arr[(int) i] + arr2[(int) (i - 1)]; long b = arr2[(int) (i - 1)] - arr[(int) i]; if (a >= 0 && b >= 0 && a != b) { flag = true; break; } arr2[(int) i] = a >= b ? a: b; } if (flag) { System.out.println(-1); } else{ for (long i = 0; i < n; i++) { System.out.print(arr2[(int) 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
b80b7b4866ce7f56975534a7d73067d0
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.InputStreamReader; public class Main{ public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-- > 0){ int n = Integer.parseInt(br.readLine()); int[] ans = new int[n]; String[] arr = br.readLine().split(" "); ans[0] = Integer.parseInt(arr[0]); boolean flag = false; for(int i=1;i<n;i++){ int val = Integer.parseInt(arr[i]); if(val != 0 && ans[i-1]-val >= 0) { System.out.println(-1); flag = true; break; }else{ ans[i] = val + ans[i-1]; } } if(!flag){ for(int val:ans)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 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
3a331ad720cd3edc46dc14962ef18a43
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 { public static void main(String[]args){ Scanner sc=new Scanner(System.in); int test=sc.nextInt(); while(test-->0) { //System.out.println(solve(sc)); solve(sc); } sc.close(); } public static void solve(Scanner sc){ 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 f=false; for(int i=1;i<n;i++) { if(arr[i]==0) ans[i]=ans[i-1]+arr[i]; else if( (ans[i-1]+arr[i]>=0) && (ans[i-1]-arr[i]>=0) ) { // System.out.println(ans[i]); f=true; break; } else ans[i]=ans[i-1]+arr[i]; } if(f==true)System.out.println(-1); else define.printarray(ans); } } class define{ public static long power( long a,long b) { long res = 1; while (b > 0) { if ((b & 1)==1) res = (res * a); a = a * a; b >>= 1; } return res; } public static int power( int a,int b) { int res = 1; while (b > 0) { if ((b & 1)==1) res = (res * a); a = a * a; b >>= 1; } return res; } public static void printarray( long a[]) { for(long i:a) System.out.print(i+" "); System.out.println(); } public static void printarray( int a[]) { for(int i:a) System.out.print(i+" "); System.out.println(); } public static void printarraylist( ArrayList<Integer>a) { for(int i:a) System.out.print(i+" "); System.out.println(); } public static void swap( long a,long b) { a=a^b; b=a^b; a=a^b; System.out.println(a+" "+b); } public static void swap( int a,int b) { a=a^b; b=a^b; a=a^b; System.out.println(a+" "+b); } public static int maxxorbetweentwonumber( int a,int b) { int store=a^b; int count=0; while(store>0) { count++; store=store>>1; } int res=1; int ans=0; while(count-->0) { ans+=res; res=res<<1; } return ans; } public static void sort(int arr[]) { Arrays.sort(arr); } public static int[] insertarray(Scanner sc,int n) { int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); return arr; } }
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
da8b1acbad3ec0dca2b4bd5329fac08e
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 java.security.*; public class solution { public static void main(String[] args) throws Exception { Scanner input; try { File file = new File("/home/harsh/Documents/Codes/input.txt"); FileInputStream fis = new FileInputStream(file); input = new Scanner(fis); } catch (Exception e) { input = new Scanner(System.in); } int testcases = input.nextInt(); while (--testcases >= 0) { solve(input); } input.close(); } private static void solve(Scanner input) throws Exception { int n = input.nextInt(); long[] a = new long[n], d = new long[n]; for (int i = 0; i < n; i++) d[i] = input.nextLong(); a[0] = d[0]; for (int i = 1; i < n; i++) { a[i] = d[i] + a[i - 1]; long y = a[i-1]-d[i]; if(y>=0 && y!=a[i]) { System.out.println(-1); return; } } for(long 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 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
0957c245536b3b077f8eecb1f8c2ac5b
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(); while(t-->0) { int n = in.nextInt(); int[] d = new int[n]; int[] a = new int[n]; boolean pos = true; for (int i=0; i<n; i++) { d[i] = in.nextInt(); if (pos) { if(i==0) { a[i] = d[i]; } else if(d[i]==0) { a[i] = a[i-1]; } else if(d[i]<=a[i-1]) { pos = false; } else { a[i] = d[i]+a[i-1]; } } } if(!pos) { System.out.println("-1"); } else { StringBuilder sb = new StringBuilder(); for(int i=0; i<n; i++) { sb.append(a[i]).append(" "); } System.out.println(sb.toString().trim()); } } } }
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
7246a673ddfa802a991428421afe8d3c
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 int[] array(BufferedReader br,int n) throws IOException{ String [] values = br.readLine().split(" "); int [] arr = new int[n]; for(int i =0; i<n; i++){ arr[i] = Integer.parseInt(values[i]); } return arr; } public static int gcd(int a, int b){ if(b==0) return a; return gcd(b,a%b); } public static int lcm(int a, int b){ return (a*b)/gcd(a,b); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-->0){ int n = Integer.parseInt(br.readLine()); int[] arr = array(br,n); int[] rec = new int[n]; rec[0] = arr[0]; boolean mul = false; for(int i = 1; i<n; i++){ if(arr[i]==0){ rec[i] = rec[i-1]+arr[i]; } else if(rec[i-1]-arr[i]>=0){ mul=true; break; } else{ rec[i] = rec[i-1]+arr[i]; } } if(mul==true){ System.out.println(-1); } else{ StringBuilder sb = new StringBuilder(); for(int i =0; i<n; i++){ sb.append(rec[i] + " "); } 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
e1be1ee02bb298f9ca1e2a457d81253f
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 static java.lang.Math.*; import static java.lang.System.*; public class Main implements Runnable { @SuppressWarnings("unchecked") private final void process() throws IOException { // InputReader input = new InputReader(new FileReader(System.getenv("INPUT"))); // PrintWriter output = new PrintWriter(new BufferedOutputStream(new FileOutputStream(System.getenv("OUTPUT")))); InputReader input = new InputReader(in); PrintWriter output = new PrintWriter(new BufferedOutputStream(out)); byte test = input.nextByte(); while (test-- > 0) { int n = input.nextInt(); int[] dArr = input.readIntArray(); int[] aArr1 = new int[n], aArr2 = new int[n]; boolean isPoss = true; aArr1[0] = aArr2[0] = dArr[0]; for (int i = 1; i < n; i++) { aArr1[i] = dArr[i] + aArr1[i - 1]; if (aArr2[i - 1] - dArr[i] < 0) aArr2[i] = aArr1[i - 1] + dArr[i]; else aArr2[i] = aArr2[i - 1] - dArr[i]; } for (int i = 0; i < n; i++) { if (aArr1[i] != aArr2[i]) { isPoss = false; break; } } if (isPoss) printArr(output, aArr1); else output.println(-1); } output.close(); input.close(); } @Override public void run() { try { process(); } catch (IOException ignored) {} } public static void main(String[] args) throws IOException { new Thread(null, new Main(), "Main", 1 << 26).start(); } private final void printArr(PrintWriter output, short...arr) { output.println(Arrays.toString(arr).replaceAll("\\[|]|, ", " ").trim()); } private final void printArr(PrintWriter output, int...arr) { output.println(Arrays.toString(arr).replaceAll("\\[|]|, ", " ").trim()); } private final void printArr(PrintWriter output, double...arr) { output.println(Arrays.toString(arr).replaceAll("\\[|]|, ", " ").trim()); } private final void printArr(PrintWriter output, long...arr) { output.println(Arrays.toString(arr).replaceAll("\\[|]|, ", " ").trim()); } private final void printArr(PrintWriter output, char...arr) { output.println(Arrays.toString(arr).replaceAll("\\[|]|, ", " ").trim()); } private final void printArr(PrintWriter output, String...arr) { output.println(Arrays.toString(arr).replaceAll("\\[|]|, ", " ").trim()); } } class Pair <X, Y> { X x; Y y; Pair(X x, Y y) { this.x = x; this.y = y; } public String toString() { return "[" + this.x + ":" + this.y + "]"; } } class InputReader { private StringTokenizer token; private BufferedReader buffer; public InputReader(InputStream stream) { buffer = new BufferedReader(new InputStreamReader(stream)); } public InputReader(FileReader reader) throws FileNotFoundException { buffer = new BufferedReader(reader); } public final String next() throws IOException { while (token == null || !token.hasMoreTokens()) token = new StringTokenizer(buffer.readLine()); return token.nextToken(); } public final String nextLine() throws IOException { return buffer.readLine(); } public final byte nextByte() throws IOException { return Byte.parseByte(next()); } public final short nextShort() throws IOException { return Short.parseShort(next()); } public final int nextInt() throws IOException { return Integer.parseInt(next()); } public final long nextLong() throws IOException { return Long.parseLong(next()); } public final double nextDouble() throws IOException { return Double.parseDouble(next()); } public final char nextChar() throws IOException { return next().charAt(0); } public final boolean nextBoolean() throws IOException { return Boolean.parseBoolean(next()); // return Boolean.getBoolean(next()); // return Boolean.valueOf(next()); } public short[] readShortArray(int n) throws IOException { short[] arr = new short[n]; for (int i = 0; i < n; i++) arr[i] = nextShort(); return arr; } public int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } public int[] readIntArray() throws IOException { return java.util.Arrays.stream(nextLine().split("\\s+")). mapToInt(Integer::parseInt).toArray(); } public long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } public long[] readLongArray() throws IOException { return java.util.Arrays.stream(nextLine().split("\\s+")). mapToLong(Long::parseLong).toArray(); } public double[] readDoubleArray(int n) throws IOException { double[] arr = new double[n]; for (int i = 0; i < n; i++) arr[i] = nextDouble(); return arr; } public double[] readDoubleArray() throws IOException { return java.util.Arrays.stream(nextLine().split("\\s+")). mapToDouble(Double::parseDouble).toArray(); } public char[] readCharArray() throws IOException { return nextLine().toCharArray(); } public String[] readStringArray(int n) throws IOException { String[] arr = new String[n]; for (int i = 0; i < n; i++) arr[i] = next(); return arr; } public String[] readStringArray() throws IOException { return nextLine().split("\\s+"); } public boolean ready() throws IOException { return buffer.ready(); } public void close() throws IOException { buffer.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 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
a2846611abf9b60beb6cf9356bd3475d
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 { 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]; arr[0] = sc.nextInt(); boolean multiple = false; for(int i = 1; i < n; i++){ int num = sc.nextInt(); int prev = arr[i-1]; int less = prev-num; int more = prev+num; arr[i] = more; if(less != more && less >= 0){ multiple = true; } } if(multiple){ System.out.println(-1); }else{ for(int i: arr){ System.out.print(i+" "); } System.out.println(); } } 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 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
7ff557a6f0f8930a00a433a509c707dc
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 pratiksc = new Scanner(System.in); int t = pratiksc.nextInt(); while (t-- > 0) { int n = pratiksc.nextInt(); int d[] = new int[n]; for (int i = 0; i < n; i++) d[i] = pratiksc.nextInt(); int arr[] = new int[n]; arr[0] = d[0]; boolean flag = false; for (int i = 1; i < n; i++) { if (d[i] == 0) { arr[i] = arr[i - 1]; continue; } if (arr[i - 1] - d[i] >= 0) { flag = true; break; } arr[i] = arr[i - 1] + d[i]; } if (flag) System.out.print(-1); else for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); System.out.println(); } pratiksc.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 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
1a7117123e2d4cec78124f2890deb4db
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 Solution extends Helper { private static SuperFastReader sc = new SuperFastReader(); // private static final FastReader sc = new FastReader(); // private static final Scanner sc = new Scanner(System.in); private static PrintWriter out = new PrintWriter(System.out); // static { // try { // sc = new SuperFastReader("input.txt"); // out = new PrintWriter("output.txt"); // } catch (Exception e) { // debug(e); // } // } public static void main(String[] args) throws IOException { int t = sc.Int(); for (int i = 1; i <= t; ++i) { // System.out.print("Case #" + i + ": "); solve(); } // solve(); sc.close(); out.close(); } public static void solve() throws IOException { int n = sc.Int(); int arr[] = new int[n]; read(arr); int ans[] = new int[n]; ans[0] = arr[0]; for (int i = 1; i < n; i++) { ans[i] = arr[i] + ans[i - 1]; } for (int i = 1; i < n; i++) { if (arr[i] == 0) { continue; } if (ans[i - 1] + arr[i] >= 0 && ans[i - 1] - arr[i] >= 0) { println(-1); return; } } for (int i : ans) { print(i + " "); } println(); } public static <T> void debug(T data) { System.out.println(data); } public static <T> void print(T data) { out.print(data); } public static <T> void println(T data) { out.println(data); } public static void println() { out.println(); } public static void read(int arr[]) throws IOException { for (int i = 0; i < arr.length; i++) { arr[i] = sc.Int(); } } public static void read(long arr[]) throws IOException { for (int i = 0; i < arr.length; i++) { arr[i] = sc.Long(); } } public static void read(String arr[]) throws IOException { for (int i = 0; i < arr.length; i++) { arr[i] = sc.next(); } } public static void read(int mat[][]) throws IOException { for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat[i].length; j++) { mat[i][j] = sc.Int(); } } } public static void read(long mat[][]) throws IOException { for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat[i].length; j++) { mat[i][j] = sc.Long(); } } } } class DSU { public int par[]; private long size[]; public DSU(int n) { par = new int[n]; size = new long[n]; for (int i = 0; i < n; i++) { par[i] = i; size[i] = 1; } } public int get(int c) { if (c == par[c]) return c; return par[c] = get(par[c]); } public void union(int x, int y) { x = get(x); y = get(y); if (x != y) { if (size[x] < size[y]) { int t = x; x = y; y = t; } par[y] = x; size[x] += size[y]; } } } class Pair<K, V> { public K key; public V value; Pair(K k, V v) { key = k; value = v; } } class SuperFastReader { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar, numChars; // standard input public SuperFastReader() { stream = System.in; } // file input public SuperFastReader(String file) throws IOException { stream = new FileInputStream(file); } private int nextByte() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars == -1) return -1; // end of file } return buf[curChar++]; } public String next() { int c; do { c = nextByte(); } while (c <= ' '); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > ' '); return res.toString(); } public String nextLine() { int c; do { c = nextByte(); } while (c < '\n'); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n'); return res.toString().trim(); // .trim() used to remove '\n' from either ends } public int Int() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public long Long() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public double Double() { return Double.parseDouble(next()); } public char Char() { return next().charAt(0); } public void close() throws IOException { stream.close(); } } class Helper { public static final int MOD = (int) (1e9) + 7;// ((a + b) % MOD + MOD) % MOD public static long powMod(long base, long exp) { long ans = 1; for (; exp != 0;) { if ((exp & 1) == 1) { ans *= base; ans %= MOD; } base *= base; base %= MOD; exp = exp >> 1; } 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 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
e8d48eb130529ee26dc8230dbaa6e62e
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
//https://codeforces.com/contest/1739/problem/B import java.util.*; public class Array_Recovery_1739B { 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 arr[] = new int[n]; arr[0] = d[0]; boolean isPossible = true; for(int i=1; i<n; i++) { // System.out.println(Arrays.toString(arr)); int elem1 = arr[i-1] + d[i]; int elem2 = arr[i-1] - d[i]; if(elem1>=0 && elem2<0) arr[i] = elem1; else if(elem1<0 && elem2>=0) arr[i] = elem2; else if(elem1>=0 && elem1 == elem2){ arr[i] = elem2; } else { isPossible = false; break; } } if(isPossible) { for(int i : arr) System.out.print(i + " "); } 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
dc6c14b77f914520819aac6b8c6bdd86
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 { static class Pair implements Comparable<Pair> { int v, i; Pair(int v, int i) { this.v = v; this.i = i; } public int compareTo(Pair p) { if (p.v == v) return i - p.i; return v - p.v; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); long manan = Long.parseLong(sc.nextLine().trim()); while (manan-- > 0) { int n = sc.nextInt(); int[] a = new int[n]; boolean flag = true; for (int i=0;i<n;i++) { if (!flag) { sc.nextInt(); continue; } if(i == 0) { a[i] = sc.nextInt(); continue; } int prev = a[i-1]; int curr = sc.nextInt(); if ( curr != 0 && curr <= prev) { flag = false; continue; } a[i] = curr + prev; } if (flag) { StringBuilder sb = new StringBuilder(); for (int i : a) sb.append(i + " "); System.out.println(sb.toString().trim()); } 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
fae197ea3b412f794132e9eb2e73a059
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 { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); for(int t = 0; t < test; ++t){ int n = sc.nextInt(); long[] arr = new long[n]; for(int i = 0; i<n; ++i){ arr[i] = sc.nextLong(); } boolean flag = true; for(int i = 1; i<n; ++i){ long x = arr[i] + arr[i-1]; long y = arr[i-1] - arr[i]; if(x >= 0 && y >= 0 && x != y){ flag = false; break; } else{ arr[i] = Math.max(x,y); } } if(flag){ for(int i = 0; i<n; ++i){ System.out.print(arr[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 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
8f4f9faffbb9b803100f0389c95767e9
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 ArrRecOBSERVATION { static int[] Solve(int[] d,int n) { int a[] = new int[n]; a[0] = d[0]; for(int i=1;i<n;i++) { int forwardDiff = a[i-1] + d[i]; int backwardDiff = a[i-1] - d[i]; if(backwardDiff >= 0 && d[i] != 0 ) return new int[] {-1}; else a[i] = forwardDiff; } return a; } public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-- > 0) { int n =s.nextInt(); int arr[] =new int[n]; for(int i=0;i<n;i++) arr[i] = s.nextInt(); int[] ans = Solve(arr,n); 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 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
5ad380468899a21bbcd89c618dea4a97
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 com.adhok; 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]; for(int i=0;i<n;i++)arr[i]=sc.nextInt(); int ans[]=new int[n]; boolean f=false; for (int i = 0; i < n; i++) { if(i==0)ans[i]=arr[i]; else ans [i] = ans[i-1]+arr[i]; } // 1 0 2 3 // ans 1 1 3 5 // 1 1 3 for (int i = 0; i < n-1; i++) { if(ans[i]+arr[i+1] >= 0 && ans[i]-arr[i+1] >= 0 && arr[i+1] != 0){ // System.out.println( ans[i] + " " + arr[i+1] ); f=true; break; } } if(f){ System.out.println("-1"); } else{ for(int it:ans) System.out.print(it + " "); System.out.println(); } } } public static String findOrder(String [] dict, int N, int K) { int []indegree = new int[K]; ArrayList<ArrayList<Integer>> adj = new ArrayList<>(); for (int i = 0; i < K; i++) { adj.add(new ArrayList<>()); } for (int i = 0; i < N-1; i++) { String s1=dict[i]; String s2=dict[i+1]; for (int j = 0; j < Math.min(s1.length(),s2.length()); j++) { if(s1.charAt(j) != s2.charAt(j)){ // converting char to int adj.get(s1.charAt(j)-'0').add(s2.charAt(j)-'0'); indegree[s2.charAt(j)-'0']++; } } } Queue<Integer> q= new LinkedList<>(); for (int i = 0; i < indegree.length; i++) { if(indegree[i]==0){ q.add(i); } } StringBuilder sb = new StringBuilder(); while(q.size()>0){ int node=q.poll(); sb.append((char)(node+'a')); for(Integer it:adj.get(node)){ sb.append((char)(it+'a')); indegree[it]--; if(indegree[it] == 0){ q.add(it); } } } return sb.toString(); } public int shipWithinDays(int[] weights, int days) { int tot=0; for(Integer it:weights) tot+=it; Arrays.sort(weights); int l=1; int h=tot; int ans=0; while(l<=h){ int mid=(l+h)/2; if(isPossible(mid,weights,days) == true){ ans=mid; h=mid-1; }else{ l=mid+1; } } System.out.println("mid: "+ans + " low: "+l + "high:" + h); return ans; } private static boolean isPossible(int mid, int[] weights, int days) { int count=1; int sum=0; for (int i = 0; i < weights.length; i++) { if(sum+weights[i]<=mid){ sum+=weights[i]; }else{ count++; sum=weights[i]; } } return count<=days; } public static int LongestRepeatingSubsequence(String str) { int [][]dp=new int[str.length()+1][str.length()+1]; for(int []it:dp) Arrays.fill(it,-1); return f(str,str,0,0,dp); } public static int f(String s1,String s2,int i,int j, int [][]dp){ if(i>=s1.length() || j>=s2.length()){ return 0; } if(dp[i][j]!=-1){ return dp[i][j]; } int ti=0,nti1=0,nti2=0; if(s1.charAt(i) == s2.charAt(j) && i!=j){ ti = f(s1,s2,i+1,j+1,dp) + 1; } nti1 = f(s1,s2,i+1,j,dp) + 0; nti2 = f(s1,s2,i,j+1,dp) + 0; return dp[i][j]=Math.max(ti,Math.max(nti1,nti2)); } static char[][] fill(int n, int m, char a[][]) { boolean [][]vis=new boolean[n+1][m+1]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if(i == n-1 || j == m-1 || i == 0 || j == 0){ if( vis[i][j] == false){ dfs(a,i,j,vis); } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if(vis[i][j]==false && a[i][j]=='0'){ a[i][j]='X'; } } } return a; } public static void dfs(char [][]mat,int r,int c,boolean[][] vis){ if(r<0 || c<0 || r>=mat.length || c>= mat[0].length || mat[r][c]=='X' || vis[r][c]==true){ return; } vis[r][c]=true; int dir[][]={{1,0},{0,1},{-1,0},{0,-1}}; for (int i = 0; i < 4; i++) { dfs(mat,r+dir[i][0],c+dir[i][1],vis); } } public static String minWindow(String s, String t) { HashMap<Character,Integer> mapS = new HashMap<>(); HashMap<Character,Integer> mapT = new HashMap<>(); int tot=0; for(Character it:t.toCharArray()){ mapS.put(it,mapS.getOrDefault(it,0)+1); } for(Integer it:mapS.values()) tot+=it; int current_count=0; String ans = ""; for (int low=0,high = 0; high < s.length(); high++) { if(mapS.containsKey(s.charAt(high))){ if( mapT.containsKey(s.charAt(high)) && mapT.get(s.charAt(high)) <= mapS.get(s.charAt(high))) current_count++; mapT.put(s.charAt(high),mapT.getOrDefault(s.charAt(high),0)+1); } int back=0; for ( back = low; back <= high && current_count >= tot; back++) { String oa = s.substring(back,high+1); if( ans.isEmpty() || oa.length() < ans.length()){ ans = oa; } if(mapS.containsKey(s.charAt(back))){ if(mapT.get(s.charAt(back)) <= mapS.get(s.charAt(back))){ current_count--; } mapT.put(s.charAt(back),mapT.getOrDefault(s.charAt(back),0)-1); } } low = back; } return ans; } public static List<Integer> findSubstring(String s, String[] words) { int len=words[0].length(); int totlen = len* words.length; if(totlen>s.length()){ return new ArrayList<>(); } ArrayList<Integer> ans = new ArrayList<>(); HashMap<String,Integer> map = new HashMap<>(); for(String it:words) map.put(it,map.getOrDefault(it,0)+1); for (int i = 0; i < s.length() - totlen; i++) { HashMap<String ,Integer> copyMap = new HashMap<>(map); for (int j = 0; j < words.length; j++) { String word = s.substring(i + len * j, i+ j * len + len); if(copyMap.containsKey(word)){ if(copyMap.get(word) == 1){ copyMap.remove(word); }else{ copyMap.put(word,copyMap.get(word)-1); } if(copyMap.isEmpty()){ ans.add(i); break; } } else{ break; } } } return ans; } // public int wordLadderLength(String startWord, String targetWord, String[] wordList) // { // // HashSet<String> set = new HashSet<>(Arrays.asList(wordList)); // Queue<Pair> q = new LinkedList<>(); // q.add(new Pair(startWord,1)); // set.remove(startWord); // // while(q.size()>0){ // // String word = q.peek().s; // int steps = q.peek().steps; // // q.poll(); // // char []chAr=word.toCharArray(); // for (int i = 0; i < word.length(); i++) { // // char temp = chAr[i]; // for (char ch = 'a'; ch <= 'z' ; ch++) { // // chAr[i]=ch; // String check = new String(chAr); // // if(set.contains(check)){ // // q.add(new Pair(check,steps+1)); // set.remove(check); // // // if(check.equals(targetWord)){ // return steps+1; // } // } // // } // chAr[i]=temp; // // } // // } // // return -1; // // } // static class Pair{ // int val; // int dist; // public Pair(int v,int i){ // this.val=v; // this.dist=i; // } // } // public static List<Integer> shortestPath(int n, int m, int edges[][]) { // // ArrayList<ArrayList<ArrayList<Integer>>> adj = new ArrayList<>(); // for( int i=0;i<n;i++)adj.add(new ArrayList<>()); // for (int i = 0; i < edges.length; i++) { // // int u=edges[i][0]; // int v=edges[i][1]; // int d=edges[i][2]; // ArrayList<Integer> temp1 = new ArrayList<>(); // ArrayList<Integer> temp2 = new ArrayList<>(); // temp1.add(v); // temp1.add(d); // adj.get(u).add(temp1); // temp2.add(u); // temp2.add(d); // adj.get(v).add(temp2); // // } // // Queue<Pair> q = new LinkedList<>(); // q.add(new Pair(1,0)); // int dist[]=new int[n+1]; // Arrays.fill(dist,Integer.MAX_VALUE); // dist[1]=0;//1 based indexing // // int []parent=new int[n+1]; // Arrays.fill(parent,-1); // while(q.size()>0){ // Pair p = q.poll(); // int node = p.val; // int distance = p.dist; // // for(ArrayList<Integer> it:adj.get(node)){ // // int val = it.get(0); // int nodeDist = it.get(1); // // if(dist[val] > dist[node] + nodeDist){ // dist[val] = dist[node] + nodeDist; // parent[val]=node; // q.add(new Pair(val,nodeDist)); // } // } // } // // System.out.println(Arrays.toString(parent)); // List<Integer> ans = new ArrayList<>(); // for(Integer it:parent)ans.add(it); // return ans; // } // int shortestPath(int[][] grid, int[] source, int[] destination) { // // int n=grid.length; // int m=grid[0].length; // int dist[][]=new int[n][m]; // // for(int []it:dist)Arrays.fill(it,Integer.MAX_VALUE); // Queue<Pair> q = new LinkedList<>(); // q.add(new Pair(source[0],source[1],0)); // dist[source[0]][source[1]]=0; // // boolean [][]vis = new boolean[n+1][m+1]; // int [][]dir={{1,0},{0,1},{-1,0},{0,-1}}; // while(q.size()>0){ // // Pair p = q.poll(); // int i=p.i; // int j=p.j; // int steps=p.steps; // // for (int k = 0; k < 4; k++) { // // int r = i + dir[k][0]; // int c = j + dir[k][1]; // // if(isValid(grid,r,c,vis)){ // // if(r == destination[0] && c == destination[1]){ // return steps+1; // } // // q.add(new Pair(r,c,steps+1)); // vis[r][c]=true; // // } // } // // } // // // return -1; // } // public static boolean isValid(int [][]grid,int i,int j,boolean[][] vis){ // // if(i<0 || j<0|| i>=grid.length || j>= grid[0].length || grid[i][j] == 0 || vis[i][j]==true){ // return false; // } // return true; // } class Pair{ int i; int j; int maxDif; public Pair(int i,int j,int s){ this.i=i; this.j=j; this.maxDif=s; } } int MinimumEffort(int h[][]) { int n=h.length; int m=h[0].length; int dist[][]=new int[n][m]; for( int []it:dist)Arrays.fill(it,Integer.MAX_VALUE); Queue<Pair> q = new LinkedList<>(); q.add(new Pair(0,0,0)); dist[0][0]=0; int [][]dir={{1,0},{0,1},{-1,0},{0,-1}}; while(q.size()>0){ Pair p = q.poll(); int i=p.i; int j=p.j; int maxDiff=p.maxDif; for (int k = 0; k < 4; k++) { int r = i + dir[k][0]; int c = j + dir[k][1]; if(isValid(h,r,c,dist,maxDiff,h[i][j])){ int curVal = Math.abs(h[i][j] -h[r][c]); q.add(new Pair(r,c,curVal)); } } } return dist[n-1][m-1]; } public static boolean isValid(int [][]h,int i,int j,int [][]dist,int maxDif, int curVal){ if(i<0 || j<0 || i>=h.length || j>=h[0].length || Math.abs(curVal-h[i][j]) > dist[i][j] ){ return false; } return true; } class Pair1 implements Comparable<Pair1>{ int v; int stops; public Pair1(int v, int s){ this.v=v; this.stops=s; } public int compareTo(Pair1 o){ return this.stops - o.stops; } } public int CheapestFLight(int n,int f[][],int src,int dst,int k) { k++; ArrayList<ArrayList<ArrayList<Integer>>> adj = new ArrayList<>(); for (int i = 0; i < n; i++) { adj.add(new ArrayList<>()); } for (int i = 0; i < f.length; i++) { int u=f[i][0]; int v=f[i][1]; int d=f[i][2]; ArrayList<Integer> temp1 = new ArrayList<>(); ArrayList<Integer> temp2 = new ArrayList<>(); temp1.add(v);temp1.add(d); temp2.add(u);temp2.add(d); adj.get(u).add(temp1); adj.get(v).add(temp2); } PriorityQueue<Pair1> pq = new PriorityQueue<>(); pq.add(new Pair1(src,0)); int dist[]=new int[n]; Arrays.fill(dist,Integer.MAX_VALUE); dist[src]=0; while(pq.size()>0){ Pair1 p = pq.poll(); int node = p.v; int stops = p.stops; for(ArrayList<Integer> it:adj.get(node)){ int ver = it.get(0); int d = it.get(1); if(stops <= k && dist[ver] > dist[node] + d){ pq.add(new Pair1(ver,stops+1)); dist[ver] = dist[node] + d; } } } return dist[dst]; } class Pair2 implements Comparable<Pair2>{ int v; int d; public Pair2(int v,int d){ this.v=v; this.d=d; } public int compareTo(Pair2 o){ return this.d-o.d; } } public int paths(int [][]edges,int n ,int m){ int []dist=new int[n]; Arrays.fill(dist,Integer.MAX_VALUE); dist[0]=0; ArrayList<ArrayList<ArrayList<Integer>>> adj = new ArrayList<>(); for( int i=0;i<n;i++) adj.add(new ArrayList<>()); for(int i=0;i<edges.length;i++){ int u=edges[i][0]; int v=edges[i][1]; int d=edges[i][2]; ArrayList<Integer> t1=new ArrayList<>(); ArrayList<Integer> t2=new ArrayList<>(); t1.add(v); t1.add(d); adj.get(u).add(t1); t2.add(u); t2.add(d); adj.get(v).add(t2); } PriorityQueue<Pair2> pq= new PriorityQueue<>(); pq.add(new Pair2(0,0)); int count=0; while(pq.size()>0){ Pair2 p=pq.poll(); int node = p.v; int distance = p.d; for(ArrayList<Integer> it:adj.get(node)){ int vertex = it.get(0); int nodeDist = it.get(1); if(dist[vertex] > distance + dist[node]){ dist[vertex] = distance + dist[node]; pq.add(new Pair2(vertex,distance + dist[node])); if(vertex == n-1){ count=1; } } else if(dist[vertex] == distance + dist[node] && vertex == n-1){ count++; } } } return count; } }
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
4247e40f2c0ccc700218b79a15f43ba3
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 { 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; } } static int gcd(int a, int b) { if (a == 0) return b; if (b == 0) return a; if (a == b) return a; if (a > b) return gcd(a-b, b); return gcd(a, b-a); } static void print(long []arr) { for(long a : arr) System.out.print(a + " "); System.out.println(); } static void print(int []arr) { for(int a : arr) System.out.print(a + " "); System.out.println(); } static void print(long a) { System.out.print(a); } static void printl(long a) { System.out.println(a); } static void printl(char ch) { System.out.println(ch); } static void print(char ch) { System.out.print(ch); } static void printl(String s) { System.out.println(s); } static void print(String s) { System.out.print(s); } static int[] intArray(int n) { int arr[] = new int[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextInt(); return arr; } static long[] longArray(int n) { long arr[] = new long[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextLong(); return arr; } static double[] doubleArray(int n) { double arr[] = new double[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextDouble(); return arr; } static int[][] intMatrix(int n, int m) { int mat[][] = new int[n][m]; for(int i = 0; i < mat.length; i++) for(int j = 0; j < mat[0].length; j++) mat[i][j] = sc.nextInt(); return mat; } static long[][] longMatrix(int n, int m) { long mat[][] = new long[n][m]; for(int i = 0; i < mat.length; i++) for(int j = 0; j < mat[0].length; j++) mat[i][j] = sc.nextLong(); return mat; } static class Pair { int v1, v2; Pair(int v1, int v2) { this.v1 = v1; this.v2 = v2; } } static FastReader sc = new FastReader(); public static void main(String[] args) { int tc = sc.nextInt(); while(tc-- > 0) { int n = sc.nextInt(); int []d = intArray(n); int a[] = new int[n]; a[0] = d[0]; boolean broken = false; for(int i = 1; i < n; i++) { int a1 = d[i] + a[i - 1]; int a2 = a[i - 1] - d[i]; // System.out.println(d[i] + "->" + a1 + "->" + a2); if(a1 >= 0 && a2 >= 0 && a1 != a2) { printl(-1); broken = true; break; } a[i] = a1; } if(!broken) print(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 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
69feac1ef4d79ce77bd472aba08ac962
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.Integer.*; public class codeforces { 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(); } boolean f = true; int ans[] = new int[n]; ans[0] = d[0]; for(int i=1;i<n;i++){ int x1 = ans[i-1] + d[i]; int x2 = ans[i-1]-d[i]; if(x1!=x2 && x1>=0 && x2>=0){ f = false; break; }else { ans[i]=x1; } } if (f){ for(int i=0;i<n;i++){ System.out.print(ans[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 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
234f397096736d30469fd7cee9bdf4a8
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(); int n; boolean moreThanOne; int[] d = new int[107]; while (t > 0) { moreThanOne = false; n = sc.nextInt(); for (int j=0; j<n; j++) d[j] = sc.nextInt(); for (int j=1; j<n; j++) { if (d[j]>0 && d[j-1]-d[j]>=0) {moreThanOne = true; break;} d[j] = d[j-1] + d[j]; } if (moreThanOne) System.out.print(-1); else for (int i=0; i<n; i++) System.out.print(d[i] + " "); System.out.println(); 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 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
0114d37e68872a2a81f4e44dfae13739
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 in=new Scanner(System.in); int t=in.nextInt(); while(t--!=0) { int n=in.nextInt(); int[]arr=new int[n]; int f=0; for(int i=0;i<n;i++) arr[i]=in.nextInt(); for(int i=1;i<n;i++) { if(arr[i]<=arr[i-1]&&arr[i]!=0) { System.out.println(-1);f=1; break; } arr[i]+=arr[i-1]; } if(f==0) { for(int i:arr) System.out.print(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
df88d45678a3819aff435c07d3d27fab
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.Collections; import java.util.HashMap; import java.util.Scanner; // Import the Scanner class import java.util.Arrays; public class solve { private static Scanner sc = new Scanner(System.in); public static void solve1(){ int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); int[] d = new int[n]; d[0] = a[0]; for (int i = 1; i < n; i++) { d[i] = d[i - 1] + a[i]; int r = d[i] - d[i - 1]; if (d[i] - 2 * r >= 0 && r != 0) { System.out.println(-1); return; } } for (int i = 0; i < n; i++) System.out.print(d[i] + " "); System.out.println(); } public static void main(String[] args) { long tt = sc.nextInt(); while (tt-- > 0) { solve1(); } } }
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
28e6a1eb8cb1b36254d983ccc49bf634
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 CF_1736B_ArrayRecovery { public static void f(int n, int[] d) { int[] a = new int[n]; a[0] = d[0]; for (int i = 1; i < n; i++) { if (d[i] == 0 || a[i - 1] - d[i] < 0) { a[i] = a[i - 1] + d[i]; } else { System.out.println(-1); return; } } for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } System.out.println(); } public static void main(String[] args) { // TODO 自动生成的方法存根 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(); } f(n, arr); t--; } 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 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
68573d81871acd38edcc80acec75b3a1
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.StringTokenizer; public class Main { public static void main(String[] args) throws IOException{ new Main().run(); } void run()throws IOException{ new Solve().setIO(System.in,System.out).run(); } public class Solve extends IOTask{ int t,n,i; boolean ans; int a[]=new int[105]; public void run()throws IOException{ t=in.in(); while(t-->0){ n=in.in(); ans=true; for(i=1;i<=n;i++)a[i]=in.in(); for(i=2;i<=n;i++){ if(a[i]==0)a[i]=a[i-1]; else{ if(a[i-1]<a[i])a[i]+=a[i-1]; else { ans=false; break; } } } if(ans)for (i=1;i<=n;i++)out.print(a[i]+" "); else out.print(-1); out.println(); } out.close(); } } class In{ private StringTokenizer st; private BufferedReader br; private InputStream is; private void init(){ br=new BufferedReader(new InputStreamReader(is)); st=new StringTokenizer(""); } public In(InputStream is){ this.is=is; init(); } public In(File file)throws IOException { is=new FileInputStream(file); init(); } public String ins()throws IOException{ while(!st.hasMoreTokens())st=new StringTokenizer(br.readLine()); return st.nextToken(); } public int in()throws IOException{ return Integer.parseInt(ins()); } public long inl()throws IOException{ return Long.parseLong(ins()); } public double ind()throws IOException{ return Double.parseDouble(ins()); } } class Out{ public PrintWriter out; OutputStream os; private void init(){ out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(os))); } public Out(OutputStream os){ this.os=os; init(); } public Out(File file)throws IOException{ this.os=new FileOutputStream(file); init(); } } abstract class IOTask{ In in; PrintWriter out; public IOTask setIO(InputStream is,OutputStream os)throws IOException{ in=new In(is); out=new Out(os).out; return this; } public IOTask setIO(File is,OutputStream os)throws IOException{ in=new In(is); out=new Out(os).out; return this; } public IOTask setIO(InputStream is,File os)throws IOException{ in=new In(is); out=new Out(os).out; return this; } public IOTask setIO(File is,File os)throws IOException{ in=new In(is); out=new Out(os).out; return this; } void run()throws IOException{ } } }
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
785a303dbef1b6e05e5e1e469bf6d97a
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.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class CodeCodechef { static BufferedWriter put = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws java.lang.Exception { FastReader get = new FastReader(); int T = get.nextInt(); while (T-- > 0) { int n = get.nextInt(); // int k = get.nextInt(); long[] arr = readArr2(n, get); ans(arr, n); put.flush(); } put.close(); } public static void ans(long[] arr, int n) throws java.lang.Exception { long ans[] = new long[n]; ans[0] = arr[0]; for (int i = 1; i < n; i++) { ans[i] = ans[i - 1] + arr[i]; if (ans[i - 1] - arr[i] >= 0 && ans[i - 1] - arr[i] != ans[i]) { System.out.println(-1); return; } } for (long i : ans) { System.out.print(i + " "); } System.out.println(); /* * put.write(_idx + "\n"); * * for(int i =0 ; i < _idx; i++) put.write(arr[i] + " "); * put.write("\n" + (N-_idx) + "\n"); * * for(int i=_idx; i < N; i++) put.write(arr[i] + " "); * put.write("\n"); */ } public static int[] readArr(int N, FastReader f) throws Exception { int[] arr = new int[N]; StringTokenizer st = new StringTokenizer(f.nextLine(), " "); for (int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } public static long[] readArr2(int N, FastReader f) throws Exception { long[] arr = new long[N]; StringTokenizer st = new StringTokenizer(f.nextLine(), " "); for (int i = 0; i < N; i++) arr[i] = Long.parseLong(st.nextToken()); return arr; } public static void print(int[] arr) { for (int x : arr) System.out.print(x + " "); System.out.println(); } 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 long totient(long n) { long result = n; for (int p = 2; p * p <= n; ++p) if (n % p == 0) { while (n % p == 0) n /= p; result -= result / p; } if (n > 1) result -= result / n; return result; } public static ArrayList<Integer> findDiv(int N) { // gets 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; } // custom multiset (replace with HashMap if needed) public static void push(TreeMap<Integer, Integer> map, int k, int v) { // map[k] += v; if (!map.containsKey(k)) map.put(k, v); else map.put(k, map.get(k) + v); } public static void pull(TreeMap<Integer, Integer> map, int k, int v) { // assumes map[k] >= v // map[k] -= v int lol = map.get(k); if (lol == v) map.remove(k); else map.put(k, lol - v); } public static HashMap<Integer, Integer> freqArr(int[] arr) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int x : arr) if (!map.containsKey(x)) map.put(x, map.get(x) + 1); else map.put(x, 1); return map; } public static long[][] multiply(long[][] left, long[][] right) { long MOD = 1000000007L; int N = left.length; int M = right[0].length; long[][] res = new long[N][M]; for (int a = 0; a < N; a++) for (int b = 0; b < M; b++) for (int c = 0; c < left[0].length; c++) { res[a][b] += (left[a][c] * right[c][b]) % MOD; if (res[a][b] >= MOD) res[a][b] -= MOD; } return res; } public static long[][] power(long[][] grid, long pow) { long[][] res = new long[grid.length][grid[0].length]; for (int i = 0; i < res.length; i++) res[i][i] = 1L; long[][] curr = grid.clone(); while (pow > 0) { if ((pow & 1L) == 1L) res = multiply(curr, res); pow >>= 1; curr = multiply(curr, curr); } return res; } } class DSU { public int[] dsu; public int[] size; public DSU(int N) { dsu = new int[N + 1]; size = new int[N + 1]; for (int i = 0; i <= N; i++) { dsu[i] = i; size[i] = 1; } } // with path compression, no find by rank public int find(int x) { return dsu[x] == x ? x : (dsu[x] = find(dsu[x])); } public void merge(int x, int y) { int fx = find(x); int fy = find(y); dsu[fx] = fy; } public void merge(int x, int y, boolean sized) { int fx = find(x); int fy = find(y); size[fy] += size[fx]; dsu[fx] = fy; } } 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); } } class SegmentTree { // Tlatoani's segment tree // iterative implementation = low constant runtime factor // range query, non lazy final int[] val; final int treeFrom; final int length; public SegmentTree(int treeFrom, int treeTo) { this.treeFrom = treeFrom; int length = treeTo - treeFrom + 1; int l; for (l = 0; (1 << l) < length; l++) ; val = new int[1 << (l + 1)]; this.length = 1 << l; } public void update(int index, int delta) { // replaces value int node = index - treeFrom + length; val[node] = delta; for (node >>= 1; node > 0; node >>= 1) val[node] = comb(val[node << 1], val[(node << 1) + 1]); } public int query(int from, int to) { // inclusive bounds if (to < from) return 0; // 0 or 1? from += length - treeFrom; to += length - treeFrom + 1; // 0 or 1? int res = 0; for (; from + (from & -from) <= to; from += from & -from) res = comb(res, val[from / (from & -from)]); for (; to - (to & -to) >= from; to -= to & -to) res = comb(res, val[(to - (to & -to)) / (to & -to)]); return res; } public int comb(int a, int b) { // change this return Math.max(a, b); } } class LazySegTree { // definitions private int NULL = -1; private int[] tree; private int[] lazy; private int length; public LazySegTree(int N) { length = N; int b; for (b = 0; (1 << b) < length; b++) ; tree = new int[1 << (b + 1)]; lazy = new int[1 << (b + 1)]; } public int query(int left, int right) { // left and right are 0-indexed return get(1, 0, length - 1, left, right); } private int get(int v, int currL, int currR, int L, int R) { if (L > R) return NULL; if (L <= currL && currR <= R) return tree[v]; propagate(v); int mid = (currL + currR) / 2; return comb(get(v * 2, currL, mid, L, Math.min(R, mid)), get(v * 2 + 1, mid + 1, currR, Math.max(L, mid + 1), R)); } public void update(int left, int right, int delta) { add(1, 0, length - 1, left, right, delta); } private void add(int v, int currL, int currR, int L, int R, int delta) { if (L > R) return; if (currL == L && currR == R) { // exact covering tree[v] += delta; lazy[v] += delta; return; } propagate(v); int mid = (currL + currR) / 2; add(v * 2, currL, mid, L, Math.min(R, mid), delta); add(v * 2 + 1, mid + 1, currR, Math.max(L, mid + 1), R, delta); tree[v] = comb(tree[v * 2], tree[v * 2 + 1]); } private void propagate(int v) { // tree[v] already has lazy[v] if (lazy[v] == 0) return; tree[v * 2] += lazy[v]; lazy[v * 2] += lazy[v]; tree[v * 2 + 1] += lazy[v]; lazy[v * 2 + 1] += lazy[v]; lazy[v] = 0; } private int comb(int a, int b) { return Math.max(a, b); } } class RangeBit { // FenwickTree and RangeBit are faster than LazySegTree by constant factor final int[] value; final int[] weightedVal; public RangeBit(int treeTo) { value = new int[treeTo + 2]; weightedVal = new int[treeTo + 2]; } private void updateHelper(int index, int delta) { int weightedDelta = index * delta; for (int j = index; j < value.length; j += j & -j) { value[j] += delta; weightedVal[j] += weightedDelta; } } public void update(int from, int to, int delta) { updateHelper(from, delta); updateHelper(to + 1, -delta); } private int query(int to) { int res = 0; int weightedRes = 0; for (int j = to; j > 0; j -= j & -j) { res += value[j]; weightedRes += weightedVal[j]; } return ((to + 1) * res) - weightedRes; } public int query(int from, int to) { if (to < from) return 0; return query(to) - query(from - 1); } } class SparseTable { public int[] log; public int[][] table; public int N; public int K; public SparseTable(int N) { this.N = N; log = new int[N + 2]; K = Integer.numberOfTrailingZeros(Integer.highestOneBit(N)); table = new int[N][K + 1]; sparsywarsy(); } private void sparsywarsy() { log[1] = 0; for (int i = 2; i <= N + 1; i++) log[i] = log[i / 2] + 1; } public void lift(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) table[i][0] = arr[i]; for (int j = 1; j <= K; j++) for (int i = 0; i + (1 << j) <= n; i++) table[i][j] = Math.min(table[i][j - 1], table[i + (1 << (j - 1))][j - 1]); } public int query(int L, int R) { // inclusive, 1 indexed L--; R--; int mexico = log[R - L + 1]; return Math.min(table[L][mexico], table[R - (1 << mexico) + 1][mexico]); } } class LCA { public int N, root; public ArrayDeque<Integer>[] edges; private int[] enter; private int[] exit; private int LOG = 17; // change this private int[][] dp; public LCA(int n, ArrayDeque<Integer>[] edges, int r) { N = n; root = r; enter = new int[N + 1]; exit = new int[N + 1]; dp = new int[N + 1][LOG]; this.edges = edges; int[] time = new int[1]; // change to iterative dfs if N is large dfs(root, 0, time); dp[root][0] = 1; for (int b = 1; b < LOG; b++) for (int v = 1; v <= N; v++) dp[v][b] = dp[dp[v][b - 1]][b - 1]; } private void dfs(int curr, int par, int[] time) { dp[curr][0] = par; enter[curr] = ++time[0]; for (int next : edges[curr]) if (next != par) dfs(next, curr, time); exit[curr] = ++time[0]; } public int lca(int x, int y) { if (isAnc(x, y)) return x; if (isAnc(y, x)) return y; int curr = x; for (int b = LOG - 1; b >= 0; b--) { int temp = dp[curr][b]; if (!isAnc(temp, y)) curr = temp; } return dp[curr][0]; } private boolean isAnc(int anc, int curr) { return enter[anc] <= enter[curr] && exit[anc] >= exit[curr]; } } class BitSet { private int CONS = 62; // safe public long[] sets; public int size; public BitSet(int N) { size = N; if (N % CONS == 0) sets = new long[N / CONS]; else sets = new long[N / CONS + 1]; } public void add(int i) { int dex = i / CONS; int thing = i % CONS; sets[dex] |= (1L << thing); } public int and(BitSet oth) { int boof = Math.min(sets.length, oth.sets.length); int res = 0; for (int i = 0; i < boof; i++) res += Long.bitCount(sets[i] & oth.sets[i]); return res; } public int xor(BitSet oth) { int boof = Math.min(sets.length, oth.sets.length); int res = 0; for (int i = 0; i < boof; i++) res += Long.bitCount(sets[i] ^ oth.sets[i]); return res; } } class MaxFlow { // Dinic with optimizations (see magic array in dfs function) public int N, source, sink; public ArrayList<Edge>[] edges; private int[] depth; public MaxFlow(int n, int x, int y) { N = n; source = x; sink = y; edges = new ArrayList[N + 1]; for (int i = 0; i <= N; i++) edges[i] = new ArrayList<Edge>(); depth = new int[N + 1]; } public void addEdge(int from, int to, long cap) { Edge forward = new Edge(from, to, cap); Edge backward = new Edge(to, from, 0L); forward.residual = backward; backward.residual = forward; edges[from].add(forward); edges[to].add(backward); } public long mfmc() { long res = 0L; int[] magic = new int[N + 1]; while (assignDepths()) { long flow = dfs(source, Long.MAX_VALUE / 2, magic); while (flow > 0) { res += flow; flow = dfs(source, Long.MAX_VALUE / 2, magic); } magic = new int[N + 1]; } return res; } private boolean assignDepths() { Arrays.fill(depth, -69); ArrayDeque<Integer> q = new ArrayDeque<Integer>(); q.add(source); depth[source] = 0; while (q.size() > 0) { int curr = q.poll(); for (Edge e : edges[curr]) if (e.capacityLeft() > 0 && depth[e.to] == -69) { depth[e.to] = depth[curr] + 1; q.add(e.to); } } return depth[sink] != -69; } private long dfs(int curr, long bottleneck, int[] magic) { if (curr == sink) return bottleneck; for (; magic[curr] < edges[curr].size(); magic[curr]++) { Edge e = edges[curr].get(magic[curr]); if (e.capacityLeft() > 0 && depth[e.to] - depth[curr] == 1) { long val = dfs(e.to, Math.min(bottleneck, e.capacityLeft()), magic); if (val > 0) { e.augment(val); return val; } } } return 0L; // no flow } private class Edge { public int from, to; public long flow, capacity; public Edge residual; public Edge(int f, int t, long cap) { from = f; to = t; capacity = cap; } public long capacityLeft() { return capacity - flow; } public void augment(long val) { flow += val; residual.flow -= val; } } } 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
ef195be9f6d89feee4b085f1ae874db4
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; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int tc = 0; tc < t; ++tc) { int n = sc.nextInt(); int[] d = new int[n]; for (int i = 0; i < d.length; ++i) { d[i] = sc.nextInt(); } System.out.println(solve(d)); } sc.close(); } static String solve(int[] d) { int[] result = new int[d.length]; result[0] = d[0]; for (int i = 1; i < result.length; ++i) { if (d[i] != 0 && result[i - 1] >= d[i]) { return "-1"; } result[i] = result[i - 1] + d[i]; } return Arrays.stream(result).mapToObj(String::valueOf).collect(Collectors.joining(" ")); } }
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
d3eed30aef7fd8aab5918a936b2da459
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 problemB { public static void main(String[] args) { Scanner sh = new Scanner(System.in); int t = sh.nextInt(); while(t>0){ int n = sh.nextInt(); int d[] = new int[n]; boolean x = true; for(int i = 0 ; i<n; i++){ d[i] = sh.nextInt(); } int a[] = new int[n]; a[0] = d[0]; for(int i = 1; i<n; i++){ a[i] = d[i] + a[i-1]; if(a[i-1]>=d[i] && d[i] != 0){ x = false; } } if(x){ for(int i = 0 ; i<n; i++){ System.out.print(a[i] + " "); } }else{ System.out.print("-1"); } System.out.println(); 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 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
753dfaf94c8957f219e4c411496639a5
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.ArrayList; import java.util.List; import java.util.StringTokenizer; public class arrayRecovery { public static void main(String[] args) throws IOException { Reader r = new Reader(); int t = r.nextInt(); List<Integer[]> answers = new ArrayList<>(); int n; Integer[] a, b; for (int i = 0; i < t; i++) { n = r.nextInt(); a = new Integer[n - 1]; b = new Integer[n]; b[0] = r.nextInt(); for (int j = 0; j < n - 1; j++) { a[j] = r.nextInt(); } for (int j = 1; j < n; j++) { if (b[j - 1] + a[j - 1] != b[j - 1] - a[j - 1] && b[j - 1] - a[j - 1] >= 0) { b[0] = -1; break; } else { b[j] = b[j - 1] + a[j - 1]; } } answers.add(b); } for (int i = 0; i < t; i++) { a = answers.get(i); if (a[0] == -1) { System.out.println(-1); } else { for (Integer integer : a) { System.out.print(integer + " "); } System.out.println(); } } } static class Reader { String filename; BufferedReader br; StringTokenizer st; PrintWriter out; boolean fileOut = false; public Reader() { this.br = new BufferedReader(new InputStreamReader(System.in)); } public Reader(String name) throws IOException { this.filename = name; this.br = new BufferedReader(new FileReader(this.filename + ".in")); this.out = new PrintWriter(new BufferedWriter(new FileWriter(this.filename + ".out"))); this.fileOut = true; } public String next() throws IOException { while(this.st == null || !this.st.hasMoreTokens()) { try { this.st = new StringTokenizer(this.br.readLine()); } catch (Exception var2) { } } return this.st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(this.next()); } public long nextLong() throws IOException { return Long.parseLong(this.next()); } public void println(Object text) { if (this.fileOut) { this.out.println(text); } else { System.out.println(text); } } public void close() { if (this.fileOut) { this.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 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
b4a32db5fc91716a91d638d7da50433e
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.reflect.Array; import java.util.*; public class A { static int mod = (int) (1e9 + 7); public static void main(String[] args) throws IOException { // Scanner sc = new Scanner(new File("second_hands_input.txt")); // PrintWriter pw = new PrintWriter("second_hands_output.txt"); Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int l = sc.nextInt(); boolean f = true; ArrayList<Integer> arr = new ArrayList<>(); arr.add(l); for (int i = 1; i < n; i++) { int x = sc.nextInt(); f &= l - x < 0 || x==0; l += x; arr.add(l); } if (f) { for (int x : arr) pw.print(x + " "); pw.println(); } else pw.println(-1); } pw.flush(); } static class pair implements Comparable<pair> { int x, f; char y; public pair(int x, char y) { this.x = x; this.y = y; } @Override public String toString() { return x + " " + y; } @Override public int compareTo(pair o) { return y - o.y; } } static long modPow(long a, long e, int mod) // O(log e) { a %= mod; long res = 1; while (e > 0) { if ((e & 1) == 1) res = (res * 1l * a) % mod; a = (a * 1l * a) % mod; e >>= 1; } return res; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(File s) throws FileNotFoundException { br = new BufferedReader(new FileReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["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
0d64a8764e264be054a8d212eddb9f01
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
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ import java.util.*; public class Main { public static boolean issorted(int []arr){ for(int i = 1;i<arr.length;i++){ if(arr[i]<arr[i-1]){ return false; } } return true; } public static long sum(int []arr){ long sum = 0; for(int i = 0;i<arr.length;i++){ sum+=arr[i]; } return sum; } public static class pair implements Comparable<pair>{ int x; int y; pair(int x,int y){ this.x = x; this.y = y; } public int compareTo(pair o){ return this.x - o.x; // sort increasingly on the basis of x // return o.x - this.x // sort decreasingly on the basis of x } } public static void swap(int []arr,int i,int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static int gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); } static int []parent = new int[1000]; static int []size = new int[1000]; public static void make(int v){ parent[v] = v; size[v] = 1; } public static int find(int v){ if(parent[v]==v){ return v; } else{ return parent[v] = find(parent[v]); } } public static void union(int a,int b){ a = find(a); b = find(b); if(a!=b){ if(size[a]>size[b]){ parent[b] = parent[a]; size[b]+=size[a]; } else{ parent[a] = parent[b]; size[a]+=size[b]; } } } static boolean []visited = new boolean[1000]; public static void dfs(int vertex,ArrayList<ArrayList<Integer>>graph){ if(visited[vertex] == true){ return; } System.out.println(vertex); for(int child : graph.get(vertex)){ // work to be done before entering the child dfs(child,graph); // work to be done after exitting the child } } public static void displayint(int []arr){ for(int i = 0;i<arr.length;i++){ System.out.print(arr[i]+" "); } System.out.println(); } public static void displaystr(String str){ StringBuilder sb = new StringBuilder(str); for(int i = 0;i<sb.length();i++){ System.out.print(sb.charAt(i)); } System.out.println(); } public static boolean checkbalanceparenthesis(StringBuilder ans){ Stack<Character>st = new Stack<>(); int i = 0; while(i<ans.length()){ if(ans.charAt(i) == '('){ st.push('('); } else{ if(st.size() == 0 || st.peek()!='('){ return false; } else{ st.pop(); } } } return st.size() == 0; } public static long binaryExp(long a,long b,long m){ /// This is Iterative Version long res = 1; while(b>0){ if((b&1)!=0){ res = (res*a)%m; } b>>=1; a = (a*a)%m; } return res; } public static void query(int x,int y){ System.out.println("?"+" "+x+" "+y); System.out.flush(); } public static class SegmentTree{ int []tree; int []arr; SegmentTree(int []ar){ arr = ar; tree = new int[4*arr.length]; build(1,0,arr.length-1); } private void build(int node,int start,int end){ if(start == end){ tree[node] = arr[start]; } else{ int mid = start + (end-start)/2; int left = 2*node; int right = 2*node + 1; build(left,start,mid); build(right,mid+1,end); tree[node] = Math.max(tree[left],tree[right]); } } private int query(int node,int start,int end,int l,int r){ if(end<l || r<start){ return Integer.MIN_VALUE; } else if(l<=start && end<=r){ return tree[node]; } else{ int mid = start + (end-start)/2; int left = query(2*node,start,mid,l,r); int right = query(2*node+1,mid+1,end,l,r); return Math.max(left,right); } } int query(int l,int r){ return query(1,0,arr.length-1,l,r); } void update(int node,int start,int end,int pos,int val){ if(start == end){ arr[start] = val; tree[node] = val; } else{ int mid = start + (end-start)/2; int left = 2*node; int right = 2*node+1; if(start<=pos && pos<=mid){ update(left,start,mid,pos,val); } else{ update(right,mid+1,end,pos,val); } tree[node] = Math.max(tree[left],tree[right]); } } private void update(int pos,int val){ update(1,0,arr.length-1,pos,val); } } public static void main(String[] args) { 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(); } boolean f = true; int []arr = new int[n]; arr[0] = d[0]; for(int i = 1;i<n;i++){ int p1 = d[i]+arr[i-1]; int p2 = Math.abs(d[i]-arr[i-1]); // System.out.println(p1+" "+p2); int v1 = Math.abs(p1-arr[i-1]); int v2 = Math.abs(p2-arr[i-1]); if(v1 == d[i] && v2 == d[i] && p1!=p2){ f = false; break; } else{ arr[i] = p1; } } if(!f){ System.out.println("-1"); } else{ displayint(arr); } } } }
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
710422c899e8ca67ef3d1b34feb9e26a
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; import java.util.ArrayList; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int e=0; e<t; e++) { int n=sc.nextInt(); boolean nice=true; // |arr1[x]-arr1[x-1]|=arr[x] ==> arr1[x]=arr1[x-1]-arr[x] ArrayList<Integer> cars = new ArrayList<Integer>(); for(int q=0; q<n; q++) { int c=sc.nextInt(); cars.add(c);} ArrayList<Integer> nums=new ArrayList<Integer>(); for(int b=0; b<n; b++) { if(b==0) { nums.add(cars.get(b)); } else if(cars.get(b)==0) { nums.add(nums.get(b-1)); } else if((nums.get(b-1)-cars.get(b))>=0) { nice=false; break;} else{ nums.add((nums.get(b-1)+cars.get(b)));} } // 2=|xi-xi-1| if(!nice){ System.out.println(-1); } else{ for(int z=0; z<nums.size();z++) { System.out.print(nums.get(z)+" "); } 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
0e7b42ffa504acba455ddc82439fbea8
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; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int tc = 0; tc < t; ++tc) { int n = sc.nextInt(); int[] d = new int[n]; for (int i = 0; i < d.length; ++i) { d[i] = sc.nextInt(); } System.out.println(solve(d)); } sc.close(); } static String solve(int[] d) { int[] result = new int[d.length]; result[0] = d[0]; for (int i = 1; i < result.length; ++i) { if (d[i] != 0 && result[i - 1] >= d[i]) { return "-1"; } result[i] = result[i - 1] + d[i]; } return Arrays.stream(result).mapToObj(String::valueOf).collect(Collectors.joining(" ")); } }
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
d50bf8e0f5812a663b9ab2ab4ed593b2
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[] str) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while (t-- != 0) { int n = scan.nextInt(); int[] d = new int[n]; boolean flg = false; for (int i = 0; i < n; i++) d[i] = scan.nextInt(); int[] arr = new int[n]; arr[0] = d[0]; for (int i = 1; i < n; i++) { arr[i] = arr[i-1] + d[i]; int val = arr[i-1] - d[i]; if (val >= 0 && val != arr[i]) { flg = true; break; } } if (flg == true) 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 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
e08468b50bbdb5051209e673bb0518e2
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 { final int mod = 1000000007; 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 swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void main(String[] args) { // TODO Auto-generated method stub FastReader t = new FastReader(); PrintWriter o = new PrintWriter(System.out); int test = t.nextInt(); while (test-- > 0) { long min = Integer.MIN_VALUE, max = Integer.MAX_VALUE; long ans =0 ; int n = t.nextInt(); //long k = t.nextLong(); int[] a = new int[n]; for (int i = 0; i < n; ++i) { a[i] = t.nextInt(); } int b = 0; for (int i=1; i<n; i++) { int d = a[i]; int nd = -1*d; d += a[i-1]; nd += a[i-1]; if (nd>=0 && d>=0 && nd != d) { b = 1; break; } a[i] = Math.max(d, nd); } if (b == 0) { for (int i = 0; i < n; ++i) { o.print(a[i]+" "); } o.println(); } else o.println("-1"); // long m = t.nextLong(); // int[] a = new int[n]; // ArrayList<Integer> al = new ArrayList<>(); // HashSet<Integer> set = new HashSet<>(); // HashMap<Integer, Integer> map = new HashMap<>(); // TreeMap<Integer, Integer> map = new TreeMap<>(); // for (int i = 0; i < n; ++i) { // a[i] = t.nextInt(); // } // for (int i = 0; i < n; ++i) { // for (int j = 0; j < n; ++j) { // } // } } o.flush(); o.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 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
1a50beb028caa22c20c829b5bfd73b7c
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 ArrayRecovery { public static void main(String[] args) throws IOException{ FastReader s = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = s.nextInt(); while(t-->0){ int n = s.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = s.nextInt(); } int cur = arr[0]; StringBuilder sb = new StringBuilder(cur + " "); boolean flag = true; for(int i = 1; i < n; i++){ if(cur >= arr[i] && arr[i] != 0){ flag = false; break; } cur += arr[i]; sb.append(cur); sb.append(' '); } if(flag){ out.println(sb); } else{ out.println(-1); } } out.flush(); } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { String str = ""; if (st.hasMoreTokens()) { str = st.nextToken("\n"); } else { str = br.readLine(); } 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
e2dc0ceb430537e3d9dcce9c65e4341f
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.util.function.*; import java.io.*; // you can compare with output.txt and expected out public class RoundEdu136B { MyPrintWriter out; MyScanner in; // final static long FIXED_RANDOM; // static { // FIXED_RANDOM = System.currentTimeMillis(); // } final static String IMPOSSIBLE = "IMPOSSIBLE"; final static String POSSIBLE = "POSSIBLE"; final static String YES = "YES"; final static String NO = "NO"; private void initIO(boolean isFileIO) { if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) { try{ in = new MyScanner(new FileInputStream("input.txt")); out = new MyPrintWriter(new FileOutputStream("output.txt")); } catch(FileNotFoundException e){ e.printStackTrace(); } } else{ in = new MyScanner(System.in); out = new MyPrintWriter(new BufferedOutputStream(System.out)); } } public static void main(String[] args){ // Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); // the code for the deep recursion (more than 100k or 3~400k or so) // Thread t = new Thread(null, new RoundEdu130F(), "threadName", 1<<28); // t.start(); // t.join(); RoundEdu136B sol = new RoundEdu136B(); sol.run(); } private void run() { boolean isDebug = false; boolean isFileIO = true; boolean hasMultipleTests = true; initIO(isFileIO); int t = hasMultipleTests? in.nextInt() : 1; for (int i = 1; i <= t; ++i) { if(isDebug){ out.printf("Test %d\n", i); } getInput(); solve(); printOutput(); } in.close(); out.close(); } // use suitable one between matrix(2, n), matrix(n, 2), transposedMatrix(2, n) for graph edges, pairs, ... int n; int[] d; void getInput() { n = in.nextInt(); d = in.nextIntArray(n); } void printOutput() { if(ans == null) out.println(-1); else out.printlnAns(ans); } int[] ans; void solve(){ ans = new int[n]; ans[0] = d[0]; for(int i=1; i<n; i++) { // d[i] = a[i] - a[i-1] or a[i-1] - a[i] // a[i] = d[i] + a[i-1] or a[i-1] - d[i] if(d[i] > 0 && ans[i-1] - d[i] >= 0) { ans = null; return; } ans[i] = d[i] + ans[i-1]; } } // Optional<T> solve() // return Optional.empty(); static class Pair implements Comparable<Pair>{ final static long FIXED_RANDOM = System.currentTimeMillis(); int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } @Override public int hashCode() { // http://xorshift.di.unimi.it/splitmix64.c long x = first; x <<= 32; x += second; x += FIXED_RANDOM; x += 0x9e3779b97f4a7c15l; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9l; x = (x ^ (x >> 27)) * 0x94d049bb133111ebl; return (int)(x ^ (x >> 31)); } @Override public boolean equals(Object obj) { // if (this == obj) // return true; // if (obj == null) // return false; // if (getClass() != obj.getClass()) // return false; Pair other = (Pair) obj; return first == other.first && second == other.second; } @Override public String toString() { return "[" + first + "," + second + "]"; } @Override public int compareTo(Pair o) { int cmp = Integer.compare(first, o.first); return cmp != 0? cmp: Integer.compare(second, o.second); } } public static class MyScanner { BufferedReader br; StringTokenizer st; // 32768? public MyScanner(InputStream is, int bufferSize) { br = new BufferedReader(new InputStreamReader(is), bufferSize); } public MyScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); // br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt"))); } public void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } 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[][] nextMatrix(int n, int m) { return nextMatrix(n, m, 0); } int[][] nextMatrix(int n, int m, int offset) { int[][] mat = new int[n][m]; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { mat[i][j] = nextInt()+offset; } } return mat; } int[][] nextTransposedMatrix(int n, int m){ return nextTransposedMatrix(n, m, 0); } int[][] nextTransposedMatrix(int n, int m, int offset){ int[][] mat = new int[m][n]; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { mat[j][i] = nextInt()+offset; } } return mat; } int[] nextIntArray(int len) { return nextIntArray(len, 0); } int[] nextIntArray(int len, int offset){ int[] a = new int[len]; for(int j=0; j<len; j++) a[j] = nextInt()+offset; return a; } long[] nextLongArray(int len) { return nextLongArray(len, 0); } long[] nextLongArray(int len, int offset){ long[] a = new long[len]; for(int j=0; j<len; j++) a[j] = nextLong()+offset; return a; } String[] nextStringArray(int len) { String[] s = new String[len]; for(int i=0; i<len; i++) s[i] = next(); return s; } } public static class MyPrintWriter extends PrintWriter{ public MyPrintWriter(OutputStream os) { super(os); } // public <T> void printlnAns(Optional<T> ans) { // if(ans.isEmpty()) // println(-1); // else // printlnAns(ans.get()); // } public void printlnAns(OptionalInt ans) { println(ans.orElse(-1)); } public void printlnAns(long ans) { println(ans); } public void printlnAns(int ans) { println(ans); } public void printlnAns(boolean[] ans) { for(boolean b: ans) printlnAns(b); } public void printlnAns(boolean ans) { if(ans) println(YES); else println(NO); } public void printAns(long[] arr){ if(arr != null && arr.length > 0){ print(arr[0]); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]); } } } public void printlnAns(long[] arr){ printAns(arr); println(); } public void printAns(int[] arr){ if(arr != null && arr.length > 0){ print(arr[0]); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]); } } } public void printlnAns(int[] arr){ printAns(arr); println(); } public <T> void printAns(ArrayList<T> arr){ if(arr != null && arr.size() > 0){ print(arr.get(0)); for(int i=1; i<arr.size(); i++){ print(" "); print(arr.get(i)); } } } public <T> void printlnAns(ArrayList<T> arr){ printAns(arr); println(); } public void printAns(int[] arr, int add){ if(arr != null && arr.length > 0){ print(arr[0]+add); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]+add); } } } public void printlnAns(int[] arr, int add){ printAns(arr, add); println(); } public void printAns(ArrayList<Integer> arr, int add) { if(arr != null && arr.size() > 0){ print(arr.get(0)+add); for(int i=1; i<arr.size(); i++){ print(" "); print(arr.get(i)+add); } } } public void printlnAns(ArrayList<Integer> arr, int add){ printAns(arr, add); println(); } public void printlnAnsSplit(long[] arr, int split){ if(arr != null){ for(int i=0; i<arr.length; i+=split){ print(arr[i]); for(int j=i+1; j<i+split; j++){ print(" "); print(arr[j]); } println(); } } } public void printlnAnsSplit(int[] arr, int split){ if(arr != null){ for(int i=0; i<arr.length; i+=split){ print(arr[i]); for(int j=i+1; j<i+split; j++){ print(" "); print(arr[j]); } println(); } } } public <T> void printlnAnsSplit(ArrayList<T> arr, int split){ if(arr != null && !arr.isEmpty()){ for(int i=0; i<arr.size(); i+=split){ print(arr.get(i)); for(int j=i+1; j<i+split; j++){ print(" "); print(arr.get(j)); } println(); } } } } static private void permutateAndSort(long[] a) { int n = a.length; Random R = new Random(System.currentTimeMillis()); for(int i=0; i<n; i++) { int t = R.nextInt(n-i); long temp = a[n-1-i]; a[n-1-i] = a[t]; a[t] = temp; } Arrays.sort(a); } static private void permutateAndSort(int[] a) { int n = a.length; Random R = new Random(System.currentTimeMillis()); for(int i=0; i<n; i++) { int t = R.nextInt(n-i); int temp = a[n-1-i]; a[n-1-i] = a[t]; a[t] = temp; } Arrays.sort(a); } static private int[][] constructChildren(int n, int[] parent, int parentRoot){ int[][] childrens = new int[n][]; int[] numChildren = new int[n]; for(int i=0; i<parent.length; i++) { if(parent[i] != parentRoot) numChildren[parent[i]]++; } for(int i=0; i<n; i++) { childrens[i] = new int[numChildren[i]]; } int[] idx = new int[n]; for(int i=0; i<parent.length; i++) { if(parent[i] != parentRoot) childrens[parent[i]][idx[parent[i]]++] = i; } return childrens; } static private int[][][] constructDirectedNeighborhood(int n, int[][] e){ int[] inDegree = new int[n]; int[] outDegree = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; outDegree[u]++; inDegree[v]++; } int[][] inNeighbors = new int[n][]; int[][] outNeighbors = new int[n][]; for(int i=0; i<n; i++) { inNeighbors[i] = new int[inDegree[i]]; outNeighbors[i] = new int[outDegree[i]]; } for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; outNeighbors[u][--outDegree[u]] = v; inNeighbors[v][--inDegree[v]] = u; } return new int[][][] {inNeighbors, outNeighbors}; } static private int[][] constructNeighborhood(int n, int[][] e) { int[] degree = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; degree[u]++; degree[v]++; } int[][] neighbors = new int[n][]; for(int i=0; i<n; i++) neighbors[i] = new int[degree[i]]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; neighbors[u][--degree[u]] = v; neighbors[v][--degree[v]] = u; } return neighbors; } static private void drawGraph(int[][] e) { makeDotUndirected(e); try { final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot") .redirectOutput(new File("graph.png")) .start(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } static private void makeDotUndirected(int[][] e) { MyPrintWriter out2 = null; try { out2 = new MyPrintWriter(new FileOutputStream("graph.dot")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } out2.println("strict graph {"); for(int i=0; i<e.length; i++){ out2.println(e[i][0] + "--" + e[i][1] + ";"); } out2.println("}"); out2.close(); } static private void makeDotDirected(int[][] e) { MyPrintWriter out2 = null; try { out2 = new MyPrintWriter(new FileOutputStream("graph.dot")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } out2.println("strict digraph {"); for(int i=0; i<e.length; i++){ out2.println(e[i][0] + "->" + e[i][1] + ";"); } out2.println("}"); out2.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 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
7b3545876e74f5dd77b7717512fc754f
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 codeForces { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-->0) { int n = sc.nextInt(); int[] d = new int[n]; int[] arr = new int[n]; for (int i=0; i<n; i++) { d[i] = sc.nextInt(); } arr[0] = d[0]; boolean ansFound = false; for (int i=1; i<n; i++) { int plus = arr[i-1] + d[i]; int minus = arr[i-1] - d[i]; if ((plus >= 0 && minus >=0) && (plus != minus) ) { System.out.println(-1); ansFound = true; break; } else if (plus >= 0) { arr[i] = plus; } else { arr[i] = minus; } } if (ansFound) { ansFound = false; continue; } else { for (int k=0; k<n; k++) { System.out.print(arr[k] + " "); } } 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
2e1a6c4a0bbfadfd906005781e66871c
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 forces { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t; t=sc.nextInt(); while(t-->0) { int N; N=sc.nextInt(); int i; int A[]=new int[N]; int a[]=new int[N]; A[0]=sc.nextInt(); a[0]=A[0]; boolean multi=false; for(i=1;i<N;i++) { A[i]=sc.nextInt(); if(A[i]<=a[i-1]&&A[i]!=0) { multi=true; } a[i]=a[i-1]+A[i]; } if(multi) { System.out.print(-1); } else { for(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
dd409169ffb68282720f1cd752e3dc1c
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 ede { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int i = 0; i < n; i++) { int size = scanner.nextInt(); int[] arrr = new int[size]; int[] check = new int[size]; for (int j = 0; j < size; j++) { arrr[j] = scanner.nextInt(); check[j] = arrr[j]; } boolean t = true; // 2 8 3 for (int j = 1; j < size; j++) { int w = arrr[j] + arrr[j-1]; // 11 int s = arrr[j] - arrr[j-1]; // -5 3 if (Math.abs(w) != Math.abs(s) && Math.abs(w - arrr[j - 1]) == Math.abs(Math.abs(s) - arrr[j - 1])) { t = false; }else { arrr[j] = w; } } for (int j = 1; j < size; j++) { if (check[j] != Math.abs(arrr[j] - arrr[j - 1])) // t = false; } if(t){ for (int j = 0; j < size; j++) { System.out.print(arrr[j]+" "); } 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
fd431fee492347fa66513588d14ec27d
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.text.*; import java.math.*; import java.util.regex.*; public class Solution { static int arr[]=new int[1000]; public static void main(String[] args) { Scanner s=new Scanner(System.in); long ts=s.nextLong(); while(ts-->0){ long n=s.nextLong(); long res=0; for(int k=1;k<=n;k++){ arr[k]=s.nextInt(); } for(int k=1;k<n;k++){ if(arr[k+1]<=arr[k] && arr[k+1]!=0){ res=-1; break; } else{ arr[k+1]=Math.abs(arr[k]+arr[k+1]); } } if(res==-1){ System.out.print(res); } else{ for(int k=1;k<=n;k++){ System.out.print(arr[k]+" "); } } 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
96da296246bf5379e29fb72f13e0438f
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 input =new Scanner(System.in); int x = Integer.parseInt(input.next()); for (int i = 0; i < x; i++) { int n = input.nextInt(); int[] b = new int[n]; for (int j = 0; j < n; j++) { b[j]=input.nextInt(); } jud(n,b); } } public static void jud(int n,int[] b){ int[] a=new int[n]; a[0]=b[0]; int i=1; while (i<n){ if(a[i-1]-b[i]>=0){ if (a[i-1]+b[i]!=a[i-1]-b[i]){ System.out.println("-1"); break; }else { a[i]=a[i-1]+b[i]; i++; } }else { a[i]=a[i-1]+b[i]; i++; } } if (i==n){ for (int j = 0; j < n-1; j++) { System.out.print(a[j]+" "); } System.out.println(a[n-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
151833dbc7f1b6ae14ec6cfde6486749
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 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(); } int c=0; a[0]=d[0]; for(int j=1;j<n;j++) { if(d[j]==0) a[j]=a[j-1]; else { int z=d[j]+a[j-1]; int y=a[j-1]-d[j]; if(z>0&&y<0) a[j]=z; else if(z<0&&y>0) a[j]=y; else c=1; } } if(c==0) { for(int f=0;f<n;f++) { System.out.print(a[f]+" "); } 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
4827133618bb770ea6c9aea3a13bc2c2
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) { // Use the Scanner class Scanner sc = new Scanner(System.in); int t = sc.nextInt(); //System.out.println("t:"+t); int i=0; while(i<t) { int n = sc.nextInt(); // System.out.println("n:"+n); // System.out.println("m:"+m); int[] arr; arr = new int[n]; // arr[0] = 10; int j=0; while(j<n) { arr[j] = sc.nextInt(); // System.out.println("j:"+ arr[j]); j++; } int[] d; d = new int[n]; d[0]=arr[0]; j=1; int x=1,y=1; while(j<n) { // System.out.println("j:"+j); x= arr[j] +d[j-1]; // System.out.println("x:"+x); if(x>=0)d[j]=x; y= -arr[j] +d[j-1]; // System.out.println("y:"+y); if(y>=0&&x>=0&&x!=y) {System.out.println("-1");break;} if(y>=0)d[j]=y; j++; } if(y>=0&&x>=0&&x!=y) {i++;continue;} // System.out.print("d:"); j=0; while(j<n) { System.out.print(d[j]+" "); j++; } System.out.println(""); i++; } /* int n = sc.nextInt(); // read input as integer long k = sc.nextLong(); // read input as long double d = sc.nextDouble(); // read input as double String str = sc.next(); // read input as String String s = sc.nextLine(); // read whole line as String */ } }
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
f21dd732f23558be1d663f3dfd9fe09b
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 test { 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]; boolean flag = false; for(int i =0;i<n;i++){ int k = sc.nextInt(); if(i == 0){ a[0] = k; }else if(k==0){ a[i] = a[i-1]; }else{ if(a[i-1] + k >=0 && a[i-1] - k >=0){ flag = true; }else{ a[i] = Math.max(a[i-1] + k , a[i-1] - k); } } } if(flag){ System.out.print(-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 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
6afd025992d78f84b637db798d553781
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.math.*; import java.io.*; public class A1{ static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); static StringTokenizer st; static final long mod=1000000007; public static void Solve() throws IOException{ st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()); int[] ar=getArrIn(n); int[] res=new int[n]; res[0]=ar[0]; for(int i=1;i<n;i++){ int t1=ar[i]+res[i-1],t2=res[i-1]-ar[i]; //System.out.println(t1+" "+t2); if(((t2>=0 && t1>0) || (t2>0 && t1>=0)) && t1!=t2){ bw.write("-1\n"); return ; } res[i]=t1; } for(int i:res){ bw.write(i+" "); } bw.newLine(); } /** Main Method**/ public static void main(String[] YDSV) throws IOException{ //int t=1; int t=Integer.parseInt(br.readLine()); while(t-->0) Solve(); bw.flush(); } /** Helpers**/ private static char[] getStr()throws IOException{ return br.readLine().toCharArray(); } private static int Gcd(int a,int b){ if(b==0) return a; return Gcd(b,a%b); } private static long Gcd(long a,long b){ if(b==0) return a; return Gcd(b,a%b); } private static int[] getArrIn(int n) throws IOException{ st=new StringTokenizer(br.readLine()); int[] ar=new int[n]; for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken()); return ar; } private static Integer[] getArrInP(int n) throws IOException{ st=new StringTokenizer(br.readLine()); Integer[] ar=new Integer[n]; for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken()); return ar; } private static long[] getArrLo(int n) throws IOException{ st=new StringTokenizer(br.readLine()); long[] ar=new long[n]; for(int i=0;i<n;i++) ar[i]=Long.parseLong(st.nextToken()); return ar; } private static List<Integer> getListIn(int n) throws IOException{ st=new StringTokenizer(br.readLine()); List<Integer> al=new ArrayList<>(); for(int i=0;i<n;i++) al.add(Integer.parseInt(st.nextToken())); return al; } private static List<Long> getListLo(int n) throws IOException{ st=new StringTokenizer(br.readLine()); List<Long> al=new ArrayList<>(); for(int i=0;i<n;i++) al.add(Long.parseLong(st.nextToken())); return al; } private static long pow_mod(long a,long b) { long result=1; while(b!=0){ if((b&1)!=0) result=(result*a)%mod; a=(a*a)%mod; b>>=1; } return result; } private static int pow_mod(int a,int b) { int result=1; int mod1=(int)mod; while(b!=0){ if((b&1)!=0) result=(result*a)%mod1; a=(a*a)%mod1; b>>=1; } return result; } private static int lower_bound(int a[],int x){ 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+1; } private static long lower_bound(long a[],long x){ 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+1; } private static int upper_bound(int a[],int x){ int l=-1,r=a.length; while(l+1<r){ int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } private static long upper_bound(long a[],long x){ int l=-1,r=a.length; while(l+1<r){ int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } private static boolean Sqrt(int x){ int a=(int)Math.sqrt(x); return a*a==x; } private static boolean Sqrt(long x){ long a=(long)Math.sqrt(x); return a*a==x; } }
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
b5013eecadd15849cc14b0aa63358512
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 cf_arrayrecov{ public static void main(String[] args) { Scanner sc= new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++){ int n=sc.nextInt(); int[] d=new int[n]; int[] a=new int[n]; for(int j=0;j<n;j++) d[j]=sc.nextInt(); a[0]=d[0]; char ch='y'; for(int j=1;j<n;j++){ if(d[j]==0){ a[j]=a[j-1]; } else if(d[j]>a[j-1]){ a[j]=a[j-1]+d[j]; } else{ ch='n'; break; } } if(ch=='y'){ for(int j=0;j<n;j++) System.out.print(a[j]+" "); 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
b6368fe6b84f59f8afb20651c2d8f939
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 A { static InputReader in; static OutputWriter out; public static void main(String[] args) { in = new InputReader(System.in); out = new OutputWriter(System.out); if (System.getProperty("ONLINE_JUDGE") == null) { try { in = new InputReader(new FileInputStream("input.txt")); out = new OutputWriter(new FileOutputStream("output.txt")); } catch (Exception e) { } } int t = in.readInt(); for (int o = 1; o <= t; o++) { int n; n = in.readInt(); int arr[] = IOUtils.readIntArray(in, n); solve(n, arr); } out.flush(); out.close(); } public static void solve(int n, int arr[]) { boolean c = true; for (int i = 1; i < n; i++) { if (arr[i] == 0) { arr[i] = arr[i - 1]; } else if (arr[i] > arr[i - 1]) { arr[i] = arr[i - 1] + arr[i]; } else { c = false; break; } } if (c) { print(arr); } else { out.printLine(-1); } } public static void print(int[] arr) { for (int e : arr) { out.print(e + " "); } out.printLine(); } static class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.readInt(); 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 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
30b89d10bc612af230688ef010548132
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 Cont1{ public static void main(String[] args) { Scanner sc = new Scanner (System.in); int t= sc.nextInt(); for(int z=0;z<t;z++){ 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 x=0; for(int i=1;i<n;i++){ if(d[i]==0){ a[i]=a[i-1]; x++; } else if(d[i]>a[i-1]){ a[i]=d[i]+a[i-1]; x++; } else if(d[i]<=a[i-1]){ System.out.println(-1); break; } } if(x==n-1){ for(int i=0;i<n;i++){ System.out.print(a[i]+" "); } System.out.println(); } } 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 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
5424c7dbe0faa62826de70b16b68d851
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
//https://codeforces.com/contest/1739/problem/B import java.util.*; public class Array_Recovery_1739B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int drr[] = new int[n]; for(int i=0; i<n; i++) drr[i] = sc.nextInt(); int a[] = new int[n]; a[0] = drr[0]; boolean fl = true; for(int i=1; i<n; i++) { int elem1 = a[i-1] + drr[i]; int elem2 = a[i-1] - drr[i]; if(elem1>=0 && elem2<0) a[i] = elem1; else if(elem1<0 && elem2>=0) a[i] = elem2; else if(elem1>=0 && elem1 == elem2){ a[i] = elem2; } else { fl = false; break; } } if(fl) { for(int i : a) System.out.print(i + " "); } 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
f9098385a9879f4ab783e453904d03ac
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
//My code is my Identity import java.util.Arrays; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; public class prog4 { /** * @param args */ public static void main(String[] args) { { FastScanner sc = new FastScanner(System.in); PrintWriter pr = new PrintWriter(System.out); int t=sc.nextInt(); first: while(t-->0) { int n= sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); int arr2[]=new int[n]; arr2[0]=arr[0]; for(int i=1;i<n;i++) { if(arr2[i-1]<arr[i]||arr[i]==0) arr2[i]=arr[i]+arr2[i-1]; else { pr.println(-1); continue first; } } for(int i=0;i<n;i++) pr.print(arr2[i]+" "); pr.println(); } pr.close(); } } static int smallestDivisor(int n) { // if divisible by 2 if (n % 2 == 0) return 2; // iterate from 3 to sqrt(n) for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken( ); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); 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 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
798b79312b43cb00a213f79701e43526
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 problem { 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 [] d = new int[n]; for (int i = 0; i < n; i++) { d[i] = sc.nextInt(); } a[0] = d[0]; int f = 0; for (int i = 1; i < n; i++) { if (a[i - 1] < d[i] || d[i] == 0) { a[i] = a[i - 1] + d[i]; } else if (d[i] != 0) f = 1; } if (f == 0) { 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 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
be1e1f9ccd220e005b9a424d5e3de252
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.math.BigInteger; import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; import static java.lang.System.*; import static java.lang.System.out; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Arrays.sort; import static java.util.Collections.shuffle; import static java.util.Collections.sort; public class Solution { private final static FastScanner scanner = new FastScanner(); private final static int mod = (int) 1e9+7; private final static int max_value = Integer.MAX_VALUE; private final static int min_value = Integer.MIN_VALUE; private static int[] a; private static int n; private static int m; private static int t; private final static String endl = "\n"; private static void solve() { n = ii(); var d = rai(n); a = new int[n]; a[0] = d[0]; IntStream.range(1, n).forEach(i -> a[i] = d[i] + a[i - 1]); for (int i = 1; i<n; i++) { int x = a[i]-a[i-1]; if (a[i-1]-x>=0 && a[i-1]-x!=a[i]) { pl(-1); return; } } printArray(a); } public static void main(String[] args) { t = ii(); while (t-->0) solve(); } private static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } private static void swap(int[] a, int[] b, int i) { int temp = a[i]; a[i] = b[i]; b[i] = temp; } private static int f(int x) { String a = x+""; return a.length(); } private static void iota(int[] a, int x) { for (int i = 0; i<a.length; i++) a[i] = x++; } private static void reverse(int[] a) { int[] b = new int[a.length]; int k = 0; for (int i = a.length-1; i>=0; i--) { b[k++] = a[i]; } System.arraycopy(b, 0, a, 0, b.length); } private static long[] ral(int n) { long[] a = new long[n]; for (int i = 0; i<n; i++) { a[i] = ii(); } return a; } private static int[] rai(int n) { return scanner.readArray(n); } private static String[] ras(int n) { return IntStream.range(0, n).mapToObj(i -> s()).toArray(String[]::new); } private static int lcm (int a, int b) { return a / gcd (a, b) * b; } private static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } private static int ii() { return scanner.nextInt(); } private static long l() { return scanner.nextLong(); } private static double d() { return scanner.nextDouble(); } private static String s() { return scanner.next(); } private static int toInt(String s) { return Integer.parseInt(s); } private static ArrayList<Integer> list() { return new ArrayList<>(); } private static HashSet<Integer> set() { return new HashSet<>(); } private static HashMap<Integer, Integer> map() { return new HashMap<>(); } private static int toInt(char c) { return Integer.parseInt(c+""); } private static<K> void pl(K a) { p(a+endl); } private static<K> void p(K a) { out.print(a); } private static void yes() { pl("YES"); } private static void no() { pl("NO"); } private static int max_a(int[] a) { int max = a[0]; for (int j : a) { if (j > max) { max = j; } } return max; } private static int min_a(int[] a) { int min = a[0]; for (int j : a) { if (j < min) { min = j; } } return min; } private static long sum(int[] a) { return stream(a).asLongStream().sum(); } private static void pl() { out.println(); } private static void printArray(long[] a) { StringBuilder builder = new StringBuilder(); for (long i : a) builder.append(i).append(' '); pl(builder); } private static void printArray(int[] a) { StringBuilder builder = new StringBuilder(); for (int i : a) builder.append(i).append(' '); pl(builder); } private static<K> void printArray(K[] a) { StringBuilder builder = new StringBuilder(); for (K i : a) builder.append(i).append(' '); pl(builder); } private static int reverseInteger(int k) { String a = k+"", res = ""; for (int i = a.length()-1; i>=0; i--) { res+=a.charAt(i); } return toInt(res); } private static int phi(int n) { int result = n; for (int 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 pow(int a, int n) { if (n == 0) return 1; if (n % 2 == 1) return pow(a, n-1) * a; else { int b = pow (a, n/2); return b * b; } } private static boolean isPrimeLong(long n) { BigInteger a = BigInteger.valueOf(n); return a.isProbablePrime(10); } private static boolean isPrime(long n) { return IntStream.iterate(2, i -> i * i <= n, i -> i + 1).noneMatch(i -> n % i == 0); } private static List<Integer> primes(int N) { int[] lp = new int[N+1]; List<Integer> pr = new ArrayList<>(); for (int i=2; i<=N; ++i) { if (lp[i] == 0) { lp[i] = i; pr.add(i); } for (int j = 0; j<pr.size() && pr.get(j)<=lp[i] && i*pr.get(j)<=N; ++j) lp[i * pr.get(j)] = pr.get(j); } return pr; } private static Set<Integer> toSet(int[] a) { return stream(a).boxed().collect(Collectors.toSet()); } private static int linearSearch(int[] a, int key) { return IntStream.range(0, a.length).filter(i -> a[i] == key).findFirst().orElse(-1); } private static<K> int linearSearch(K[] a, K key) { return IntStream.range(0, a.length).filter(i -> a[i].equals(key)).findFirst().orElse(-1); } static int upper_bound(int[] arr, int key) { int index = binarySearch(arr, key); int n = arr.length; if (index < 0) { int upperBound = abs(index) - 1; if (upperBound < n) return upperBound; else return -1; } else { while (index < n) { if (arr[index] == key) index++; else { return index; } } return -1; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(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()); } double nextDouble() { return Double.parseDouble(next()); } } private static void stableSort(int[] a) { List<Integer> list = stream(a).boxed().sorted().toList(); setAll(a, list::get); } private static void psort(int[] arr, int n) { int min = min_a(arr); int max = max_a(arr); int range = max-min+1, i, j, index = 0; int[] count = new int[range]; for(i = 0; i<n; i++) count[arr[i] - min]++; for(j = 0; j<range; j++) while(count[j]-->0) arr[index++]=j+min; } private static void csort(int[] a, int n) { int max = max_a(a); int min = min_a(a); int range = max - min + 1; int[] count = new int[range]; int[] output = new int[n]; for (int i = 0; i < n; i++) { count[a[i] - min]++; } for (int i = 1; i < range; i++) { count[i] += count[i - 1]; } for (int i = n - 1; i >= 0; i--) { output[count[a[i] - min] - 1] = a[i]; count[a[i] - min]--; } arraycopy(output, 0, a, 0, n); } private static void csort(char[] arr) { int n = arr.length; char[] output = new char[n]; int[] count = new int[256]; for (int i = 0; i < 256; ++i) count[i] = 0; for (char c : arr) ++count[c]; for (int i = 1; i <= 255; ++i) count[i] += count[i - 1]; for (int i = n - 1; i >= 0; i--) { output[count[arr[i]] - 1] = arr[i]; --count[arr[i]]; } arraycopy(output, 0, arr, 0, n); } // Java Program to show segment tree operations like construction, // query and update static class SegmentTree { int[] st; // The array that stores segment tree nodes /* Constructor to construct segment tree from given array. This constructor allocates memory for segment tree and calls constructSTUtil() to fill the allocated memory */ SegmentTree(int[] arr, int n) { // Allocate memory for segment tree //Height of segment tree int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); //Maximum size of segment tree int max_size = 2 * (int) Math.pow(2, x) - 1; st = new int[max_size]; // Memory allocation constructSTUtil(arr, 0, n - 1, 0); } // A utility function to get the middle index from corner indexes. int getMid(int s, int e) { return s + (e - s) / 2; } /* A recursive function to get the sum of values in given range of the array. The following are parameters for this function. st --> Pointer to segment tree si --> Index of current node in the segment tree. Initially 0 is passed as root is always at index 0 ss & se --> Starting and ending indexes of the segment represented by current node, i.e., st[si] qs & qe --> Starting and ending indexes of query range */ int getSumUtil(int ss, int se, int qs, int qe, int si) { // If segment of this node is a part of given range, then return // the sum of the segment if (qs <= ss && qe >= se) return st[si]; // If segment of this node is outside the given range if (se < qs || ss > qe) return 0; // If a part of this segment overlaps with the given range int mid = getMid(ss, se); return getSumUtil(ss, mid, qs, qe, 2 * si + 1) + getSumUtil(mid + 1, se, qs, qe, 2 * si + 2); } /* A recursive function to update the nodes which have the given index in their range. The following are parameters st, si, ss and se are same as getSumUtil() i --> index of the element to be updated. This index is in input array. diff --> Value to be added to all nodes which have i in range */ void updateValueUtil(int ss, int se, int i, int diff, int si) { // Base Case: If the input index lies outside the range of // this segment if (i < ss || i > se) return; // If the input index is in range of this node, then update the // value of the node and its children st[si] = st[si] + diff; if (se != ss) { int mid = getMid(ss, se); updateValueUtil(ss, mid, i, diff, 2 * si + 1); updateValueUtil(mid + 1, se, i, diff, 2 * si + 2); } } // The function to update a value in input array and segment tree. // It uses updateValueUtil() to update the value in segment tree void updateValue(int arr[], int n, int i, int new_val) { // Check for erroneous input index if (i < 0 || i > n - 1) { System.out.println("Invalid Input"); return; } // Get the difference between new value and old value int diff = new_val - arr[i]; // Update the value in array arr[i] = new_val; // Update the values of nodes in segment tree updateValueUtil(0, n - 1, i, diff, 0); } // Return sum of elements in range from index qs (query start) to // qe (query end). It mainly uses getSumUtil() int getSum(int n, int qs, int qe) { // Check for erroneous input values if (qs < 0 || qe > n - 1 || qs > qe) { System.out.println("Invalid Input"); return -1; } return getSumUtil(0, n - 1, qs, qe, 0); } // A recursive function that constructs Segment Tree for array[ss..se]. // si is index of current node in segment tree st int constructSTUtil(int arr[], int ss, int se, int si) { // If there is one element in array, store it in current node of // segment tree and return if (ss == se) { st[si] = arr[ss]; return arr[ss]; } // If there are more than one elements, then recur for left and // right subtrees and store the sum of values in this node int mid = getMid(ss, se); st[si] = constructSTUtil(arr, ss, mid, si * 2 + 1) + constructSTUtil(arr, mid + 1, se, si * 2 + 2); return st[si]; } } }
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
ee1be6b8062969e60d3f94eeffa95ac7
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 Test1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int[] drr = new int[n]; for(int i=0;i<n;i++) drr[i] = sc.nextInt(); int[] arr = new int[n]; arr[0] = drr[0]; boolean fault = false; for(int i=1;i<n;i++) { int p = arr[i-1], d = drr[i]; int c1 = p+d, c2 = p-d; if(c1 != c2 && c2 >= 0) { fault = true; break; } arr[i] = c1; } if(fault) { System.out.println(-1); continue; } StringBuilder sb = new StringBuilder(); for(int x : arr) sb.append(x).append(" "); System.out.println(sb.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 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
1ccaf1d2adbc45fef53ffe4fc5e15955
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 CF_1736B_ArrayRecovery { public static void f(int n,int[] d) { int[] a=new int[n]; a[0]=d[0]; for(int i=1;i<n;i++) { if(d[i]==0||a[i-1]-d[i]<0) { a[i]=a[i-1]+d[i]; }else { System.out.println(-1); return; } } for(int i=0;i<n;i++) { System.out.print(a[i]+" "); } System.out.println(); } public static void main(String[] args) { // TODO 自动生成的方法存根 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(); } f(n, arr); t--; } 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 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
6c974f6b22fb012317e3d8627976a395
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 { static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int t = scanner.nextInt(); while(t-- > 0) { solve(); } } static void solve() { int n = scanner.nextInt(); int[] d = new int[n]; for(int i = 0;i < n;i++) { d[i] = scanner.nextInt(); } int[] res = new int[n]; res[0] = d[0]; for(int i = 1;i < n;i++) { if(res[i - 1] - d[i] < 0 || res[i - 1] - d[i] == res[i - 1] + d[i]) { res[i] = d[i] + res[i - 1]; } else { // System.out.println("sdf" + (res[i - 1] - d[i])); System.out.println(-1); return ; } } for(int i = 0;i < n;i++) { System.out.print(res[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
af0897e1bde9fb6c3abbc3f2887db16b
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 void print(int[] arr) { int[] lst = new int[arr.length]; int sum = arr[0]; lst[0] = sum; for (int i = 1; i < arr.length; i++) { if (arr[i] > 0 && sum >= arr[i]) { System.out.println(-1); return; } sum = sum + arr[i]; lst[i] = sum; } for (int x : lst) { System.out.print(x + " "); } System.out.println(); } public static void main(String[] args) { //System.out.println(); Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int q = 0; q < t; q++) { //int m = sc.nextInt(); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } print(arr); } } }
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
3e61eaa22976658d6048bfde783b532a
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.*; import static java.lang.Math.*; public class Solution { static FastReader fastReader = new FastReader(); public static void main(String[] args) { int t = fastReader.nextInt(); int x = 1; while (x++ <= t) { String s = solve(); System.out.println(s); } } private static String solve() { int n = fastReader.nextInt(); int[] di = new int[n]; for(int i = 0 ; i < n ; i++) di[i] = fastReader.nextInt(); StringBuilder ans = new StringBuilder(); boolean onlyOne = true; ans.append(di[0]).append(" "); for(int i = 1; i < n ; i++){ int a = -1 * di[i] + di[i - 1]; int b = di[i] + di[i - 1]; if(a >= 0 && b >= 0 && a != b) return "-1"; else if(a >= 0){ ans.append(a).append(" "); di[i] = a; }else{ ans.append(b).append(" "); di[i] = b; } } return String.valueOf(ans); } } 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
cc29ae5bb0d95eed166cbf9efec1e973
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 { final static boolean multipleTests = true; Input in; PrintWriter out; public B() { in = new Input(System.in); out = new PrintWriter(System.out); } public static void main(String[] args) { B solution = new B(); int t = 1; if (multipleTests) t = solution.in.nextInt(); for (; t > 0; t--) { solution.solve(); } solution.out.close(); } void solve() { int n = in.nextInt(); int[] d = in.nextIntArray(n); int[] a = new int[n]; a[0] = d[0]; for (int i=1; i<n; i++) { if (d[i] == 0 || a[i-1] - d[i] < 0) { a[i] = a[i-1] + d[i]; } else { out.println(-1); return; } } for (int x: a) { out.print(x); out.print(' '); } out.println(); } static class Input { BufferedReader br; StringTokenizer st; public Input(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(""); } String nextString() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextString()); } long nextLong() { return Long.parseLong(nextString()); } double nextDouble() { return Double.parseDouble(nextString()); } int[] nextIntArray(int size) { int[] ans = new int[size]; for (int i = 0; i < size; i++) { ans[i] = nextInt(); } 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 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
c045bba9b0f7eee0725fe1f40ea84130
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.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Solution { public static class CFScanner { BufferedReader br; StringTokenizer st; public CFScanner() { 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()); } } public static void main(String[] args) { // int n = in.nextInt(); // long n = in.nextLong(); // double n = in.nextDouble(); // String s = in.next(); CFScanner in = new CFScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int T = in.nextInt(); for (int t = 1; t <= T; t++) { int n = in.nextInt(); int[] ds = new int[n]; for (int i = 0; i < n; i++) { ds[i] = in.nextInt(); } out.println(getArray(ds)); // out.println(solve()); // out.println("Case #" + t + ": " + solve()); } out.close(); } private static String getArray(int[] ds) { StringBuilder strBd = new StringBuilder(); int curr = 0; for (int d : ds) { int next1 = curr + d; int next2 = curr - d; if (d > 0 && next2 >= 0) { return "-1"; } strBd.append(next1).append(" "); curr += d; } strBd.deleteCharAt(strBd.length() - 1); return strBd.toString(); } private static String getCell(int rows, int cols) { for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { if (cannotMove(r, c, rows, cols)) { return ++r + " " + ++c; } } } return "1 1"; } private static boolean cannotMove(int r, int c, int rows, int cols) { if (r - 2 >= 0 && (c - 1 >= 0 || c + 1 < cols)) { return false; } if (r + 2 < rows && (c - 1 >= 0 || c + 1 < cols)) { return false; } if (c - 2 >= 0 && (r - 1 >= 0 || r + 1 < rows)) { return false; } if (c + 2 < cols && (r - 1 >= 0 || r + 1 < rows)) { return false; } 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 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
d1c2cb1c7c019013450e1b25d9339a3a
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
/** * @author vivek * <> */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class B { private static void solveTC(int __) { /* For Google */ // ans.append("Case #").append(__).append(": "); //code start int n= scn.nextInt(); int[] arr = scn.nextIntArray(n); int[] ans= new int[n]; ans[0] = arr[0]; for (int i = 1; i < n; i++) { int opt1 = ans[i-1] + arr[i]; int opt2 = ans[i-1] - arr[i]; if (opt1>=0 && opt2>=0 && opt1!=opt2){ print("-1\n"); return; } ans[i] = Math.max(opt1, opt2); } for (int i = 0; i < n; i++) { print(ans[i]+" "); } //code end print("\n"); } public static void main(String[] args) { scn = new Scanner(); ans = new StringBuilder(); int t = scn.nextInt(); // int t = 1; // int limit= ; // sieve(limit); /* try { System.setOut(new PrintStream(new File("file_i_o\\output.txt"))); } catch (FileNotFoundException e) { e.printStackTrace(); } */ for (int i = 1; i <= t; i++) { solveTC(i); } System.out.print(ans); } //Stuff for prime start /** * sorting algos */ private static void sort(int[] arr) { ArrayList<Integer> li = new ArrayList<>(arr.length); for (int ele : arr) li.add(ele); Collections.sort(li); for (int i = 0; i < li.size(); i++) { arr[i] = li.get(i); } } private static void sort(long[] arr) { ArrayList<Long> li = new ArrayList<>(arr.length); for (long ele : arr) li.add(ele); Collections.sort(li); for (int i = 0; i < li.size(); i++) { arr[i] = li.get(i); } } private static void sort(float[] arr) { ArrayList<Float> li = new ArrayList<>(arr.length); for (float ele : arr) li.add(ele); Collections.sort(li); for (int i = 0; i < li.size(); i++) { arr[i] = li.get(i); } } private static void sort(double[] arr) { ArrayList<Double> li = new ArrayList<>(arr.length); for (double ele : arr) li.add(ele); Collections.sort(li); for (int i = 0; i < li.size(); i++) { arr[i] = li.get(i); } } /** * List containing prime numbers <br> * <b>i<sup>th</sup></b> position contains <b>i<sup>th</sup></b> prime number <br> * 0th index is <b>null</b> */ private static ArrayList<Integer> listOfPrimes; /** * query <b>i<sup>th</sup></b> element to get if its prime of not */ private static boolean[] isPrime; /** * Performs Sieve of Erathosnesis and initialise isPrime array and listOfPrimes list * * @param limit the number till which sieve is to be performed */ private static void sieve(int limit) { listOfPrimes = new ArrayList<>(); listOfPrimes.add(null); boolean[] array = new boolean[limit + 1]; Arrays.fill(array, true); array[0] = false; array[1] = false; for (int i = 2; i <= limit; i++) { if (array[i]) { for (long j = (long) i * i; j <= limit; j += i) { array[(int) j] = false; } } } isPrime = array; for (int i = 0; i <= limit; i++) { if (array[i]) { listOfPrimes.add(i); } } } //stuff for prime end /** * Calculates the Least Common Multiple of two numbers * * @param a First number * @param b Second Number * @return Least Common Multiple of <b>a</b> and <b>b</b> */ private static long lcm(long a, long b) { return a * b / gcd(a, b); } /** * Calculates the Greatest Common Divisor of two numbers * * @param a First number * @param b Second Number * @return Greatest Common Divisor of <b>a</b> and <b>b</b> */ private static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } static void print(Object obj) { ans.append(obj.toString()); } static Scanner scn; static StringBuilder ans; //Fast Scanner static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); /* try { br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("file_i_o\\input.txt")))); } catch (FileNotFoundException e) { e.printStackTrace(); } */ } 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; } Integer[] nextIntegerArray(int n) { Integer[] array = new Integer[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; } String[] nextStringArray() { return nextLine().split(" "); } String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) { array[i] = next(); } 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 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
74e98c32f66f4e854e6a238e77b28c92
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 cf { public static void main(String[] args) { Scanner css = new Scanner(System.in); int t = css.nextInt(); for (int i = 0; i < t; i++){ int n = css.nextInt(); int[] arr = new int[n]; int[] new_arr1 = new int[n]; int[] new_arr2 = new int[n]; for (int j = 0; j < n; j++) { arr[j] = css.nextInt(); } new_arr1[0] = arr[0]; new_arr2[0] = arr[0]; for (int j = 1; j < n; j++) { new_arr1[j] = arr[j] + new_arr1[j - 1]; } int count = 1; for (int j = 1; j < n; j++) { if(new_arr1[j] >= 0 && (new_arr1[j - 1] -arr[j]) >= 0 && new_arr1[j] != (new_arr1[j - 1] - arr[j])){ System.out.println(-1); count = 0; break; } } if(count == 1){ for (int k = 0; k < n; k++) { System.out.print(new_arr1[k] + " "); } System.out.print("\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 8
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
c1ae035d27859b81fd675f9d25d07328
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.*; @SuppressWarnings("unused") public class B_Array_Recovery { public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); out: while (t-- > 0) { int n = sc.nextInt(); int[] d = sc.readArray(n), a = new int[n]; a[0] = d[0]; for (int i = 1; i < n; i++) { if (d[i] > 0 && a[i - 1] - d[i] >= 0) { out.println(-1); continue out; } a[i] = a[i - 1] + d[i]; } for (int i : a) out.print(i + " "); out.println(); } out.close(); } public static PrintWriter out; public static long mod = (long) 1e9 + 7; public static int[] parent = new int[101]; public static int[] rank = new int[101]; 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()); } 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; } int[][] readArrayMatrix(int N, int M, int Index) { if (Index == 0) { int[][] res = new int[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) res[i][j] = (int) nextLong(); } return res; } int[][] res = new int[N][M]; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) res[i][j] = (int) nextLong(); } return res; } long[][] readArrayMatrixLong(int N, int M, int Index) { if (Index == 0) { long[][] res = new long[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) res[i][j] = nextLong(); } return res; } long[][] res = new long[N][M]; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) res[i][j] = nextLong(); } return res; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static boolean nextPerm(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; } private static void google(int tt) { out.print("Case #" + (tt) + ": "); } public static int lower_bound(int[] arr, int x) { int low = 0, high = arr.length - 1, mid = -1; int ans = -1; while (low <= high) { mid = (low + high) / 2; if (arr[mid] > x) { high = mid - 1; } else { ans = mid; low = mid + 1; } } return ans; } public static int upper_bound(int[] arr, int x) { int low = 0, high = arr.length - 1, mid = -1; int ans = arr.length; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) { ans = mid; high = mid - 1; } else { low = mid + 1; } } return ans; } static void reverseArray(int[] a) { int n = a.length; int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void reverseArray(long[] a) { int n = a.length; long arr[] = new long[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } public static void push(TreeMap<Integer, Integer> map, int k, int v) { if (!map.containsKey(k)) map.put(k, v); else map.put(k, map.get(k) + v); } public static void pull(TreeMap<Integer, Integer> map, int k, int v) { int lol = map.get(k); if (lol == v) map.remove(k); else map.put(k, lol - v); } static int[][] matrixMul(int[][] a, int[][] m) { if (a[0].length == m.length) { int[][] b = new int[a.length][m.length]; for (int i = 0; i < m.length; i++) { for (int j = 0; j < m.length; j++) { int sum = 0; for (int k = 0; k < m.length; k++) { sum += m[i][k] * m[k][j]; } b[i][j] = sum; } } return b; } return null; } static void swap(int[] a, int l, int r) { int temp = a[l]; a[l] = a[r]; a[r] = temp; } static void SieveOfEratosthenes(int n, boolean prime[]) { prime[0] = false; prime[1] = false; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) for (int i = p * p; i <= n; i += p) prime[i] = false; } } static void dfs(int root, boolean[] vis, int[] value, ArrayList[] gr, int prev) { vis[root] = true; value[root] = 3 - prev; prev = 3 - prev; for (int i = 0; i < gr[root].size(); i++) { int next = (int) gr[root].get(i); if (!vis[next]) dfs(next, vis, value, gr, prev); } } static boolean isPrime(int n) { for (int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } static boolean isPrime(long n) { for (long i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } static int abs(int a) { return a > 0 ? a : -a; } static int max(int a, int b) { return a > b ? a : b; } static int min(int a, int b) { return a < b ? a : b; } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static boolean isSquare(double a) { boolean isSq = false; double b = Math.sqrt(a); double c = Math.sqrt(a) - Math.floor(b); if (c == 0) isSq = true; return isSq; } static long sqrt(long z) { long sqz = (long) Math.sqrt(z); while (sqz * 1L * sqz < z) { sqz++; } while (sqz * 1L * sqz > z) { sqz--; } return sqz; } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long pow(long n, long m) { if (m == 0) return 1; long temp = pow(n, m / 2); long res = ((temp * temp) % mod); if (m % 2 == 0) return res; return (res * n) % mod; } static long modular_add(long a, long b) { return ((a % mod) + (b % mod)) % mod; } static long modular_sub(long a, long b) { return ((a % mod) - (b % mod) + mod) % mod; } static long modular_mult(long a, long b) { return ((a % mod) * (b % mod)) % mod; } 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 long lcm(long a, long b) { return (a * b) / gcd(a, b); } static int gcd(int n1, int n2) { if (n2 != 0) return gcd(n2, n1 % n2); else return n1; } static int find(int u) { if (u == parent[u]) return u; return parent[u] = find(parent[u]); } static void union(int u, int v) { int a = find(u), b = find(v); if (a == b) return; if (rank[a] > rank[b]) { parent[b] = a; rank[a] += rank[b]; } else { parent[a] = b; rank[b] += rank[a]; } } static void dsu() { for (int i = 0; i < 101; i++) { parent[i] = i; rank[i] = 1; } } static class Pair { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } static void sortbyx(Pair[] coll) { List<Pair> al = new ArrayList<>(Arrays.asList(coll)); Collections.sort(al, new Comparator<Pair>() { public int compare(Pair p1, Pair p2) { return p1.x - p2.x; } }); for (int i = 0; i < al.size(); i++) { coll[i] = al.get(i); } } static void sortbyy(Pair[] coll) { List<Pair> al = new ArrayList<>(Arrays.asList(coll)); Collections.sort(al, new Comparator<Pair>() { public int compare(Pair p1, Pair p2) { return p1.y - p2.y; } }); for (int i = 0; i < al.size(); i++) { coll[i] = al.get(i); } } static void sortbyx(ArrayList<Pair> al) { Collections.sort(al, new Comparator<Pair>() { public int compare(Pair p1, Pair p2) { return Integer.compare(p1.x, p2.x); } }); } static void sortbyy(ArrayList<Pair> al) { Collections.sort(al, new Comparator<Pair>() { public int compare(Pair p1, Pair p2) { return Integer.compare(p1.y, p2.y); } }); } public String toString() { return String.format("(%s, %s)", String.valueOf(x), String.valueOf(y)); } } static void sort(int[] a) { ArrayList<Integer> list = new ArrayList<>(); for (int i : a) list.add(i); Collections.sort(list); for (int i = 0; i < a.length; i++) a[i] = list.get(i); } static void sort(long a[]) { ArrayList<Long> list = new ArrayList<>(); for (long i : a) list.add(i); Collections.sort(list); for (int i = 0; i < a.length; i++) a[i] = list.get(i); } static int[] array(int n, int value) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = value; return a; } static long sum(long a[]) { long sum = 0; for (long i : a) sum += i; return (sum); } static long count(long a[], long x) { long c = 0; for (long i : a) if (i == x) c++; return (c); } static int sum(int a[]) { int sum = 0; for (int i : a) sum += i; return (sum); } static int count(int a[], int x) { int c = 0; for (int i : a) if (i == x) c++; return (c); } static int count(String s, char ch) { int c = 0; char x[] = s.toCharArray(); for (char i : x) if (ch == i) c++; return (c); } static int[] freq(int a[], int n) { int f[] = new int[n + 1]; for (int i : a) f[i]++; return f; } static int[] pos(int a[], int n) { int f[] = new int[n + 1]; for (int i = 0; i < n; i++) f[a[i]] = i; return f; } static boolean isPalindrome(String s) { StringBuilder sb = new StringBuilder(); sb.append(s); String str = String.valueOf(sb.reverse()); if (s.equals(str)) return true; else return false; } }
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 8
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
e6bf93a862b3d0bb80325bb8712e7c11
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 sc = new Scanner(System.in); int t = sc.nextInt(); while(t>0){ t--; int n = sc.nextInt(); int[] d = new int[n]; // int sum=0; // boolean allSame=true; for(int i=0; i<n; i++){ d[i] = sc.nextInt(); // sum += d[i]; // if(allSame && i>0 && d[i]!=d[i-1]){ // allSame=false; // } } int[] a = new int[n]; a[0] = d[0]; // a[1] = d[1]+d[0]; // a[n-1] = sum; boolean flag=false; for(int i=1; i<n; i++){ int x = a[i-1] - d[i]; int y = d[i] + a[i-1]; a[i] = y; if(x>=0 && x!=y){ flag = true; break; } // sum = a[i]; // System.out.println("i="+i+" sum="+sum+" a[i+1]="+a[i+1]+" d[i+1]="+d[i+1]); // if(a[i]-d[i+1]>0 && d[i+1]!=0){ // if(a[i]-d[i+1]>=0 && d[i+1]!=0 && !allSame){ // if(x>=0 && y>=0 && !allSame){ // flag=true; // } } if(flag){ System.out.println(-1); } else{ for(int i=0; i<n; i++){ System.out.print(a[i]+" "); } System.out.println(); } } } } // 1 1 1 // 1 0 2 5 // d3 = a3 - a2 // a3 = d3 + a2 // d3 = a2 - a3 // a3 = a2 - d3 // 2 // 2 6 3 // 0 0 0 0 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 8
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
e3f7be7060db7a3ca0941f259af517d5
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 static java.lang.Math.*; public class Main { public static void main(String[] args) throws IOException{ FastScanner fs=new FastScanner(); Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int t = fs.nextInt(); for (int i = 0; i < t; i++) { int n = fs.nextInt(); int[] arr = new int[n]; int[] nw = new int[n]; for(int j = 0; j < n; j++){ arr[j] = fs.nextInt(); } nw[0] = arr[0]; boolean isUnique = true; for(int j = 1; j < n; j++){ if(arr[j] != 0 && nw[j-1] >= arr[j]) { isUnique = false; } nw[j] = nw[j-1] + arr[j]; } if(!isUnique){ out.println(-1); }else{ for (int j = 0; j < n; j++) { out.print(nw[j] + " "); } out.println(); } } out.close(); // //5 7 15 7 5 //5 8 9 5 } public static boolean isPalindrome(String s){ int lo = 0, hi = s.length()-1; while(lo < hi){ if(s.charAt(lo) != s.charAt(hi)) return false; lo++; hi--; } return true; } public static int lcd(int a,int b){ return a*b/gcd(a,b); } public static int gcd(int a, int b){ if(b == 0){ return a; } return gcd(b,a % b); } public static boolean isPerfectSquare(int n){ int sqr = (int)sqrt(n); return sqr * sqr == n; } public static boolean isPrime(int x){ int a = (int)sqrt(x); for(int i = 2; i<=a ; i++){ if(x % i == 0) return false; } return true; } 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()); } } static class Pair implements Comparable { int a,b; public String toString() { return a+" " + b; } public Pair(int x , int y) { a=x;b=y; } @Override public int compareTo(Object o) { Pair p = (Pair)o; if(p.a < a) return 1; else if(p.a==a) if(p.b<b) return 1; else if(p.b==b) return 0; return -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 8
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
675798a04ec9d1c46a1c3276a706f8fc
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.text.DecimalFormat; import java.util.*; public class Solution { static BufferedReader bf; static PrintWriter out; static Scanner sc; static StringTokenizer st; static long mod = (long)(1e9+7); static long mod2 = 998244353; static long fact[] ; static long inverse[]; public static void main (String[] args)throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new Scanner(System.in); // fact[0] = 1; // inverse[0] = 1; // for(int i = 1;i<fact.length;i++){ // fact[i] = (fact[i-1] * i)%mod; // // inverse[i] = binaryExpo(fact[i], mod-2);a // } int t = nextInt(); while(t-->0){ solve(); } out.flush(); } public static void solve() throws IOException{ int n = nextInt(); int[]arr = new int[n]; for(int i = 0;i<n;i++){ arr[i] = nextInt(); } if(n == 1){ out.println(arr[0]); return; } long sum = arr[0]; long []ans = new long[n]; ans[0] = arr[0]; for(int i = 1;i<n;i++){ if(sum - arr[i] < sum && sum - arr[i] >= 0){ out.println(-1); return; } else{ sum += arr[i]; ans[i] = sum; } } for(int i = 0;i<n;i++){ out.print(ans[i] + " "); } out.println(); } public static void req(int i,int j){ out.println("? " + i + " " + j); out.flush(); } public static int maxy(int node, int l,int r,int tl,int tr,int[][]tree){ if(l>=tl && r <= tr)return tree[node][0]; if(r < tl || l > tr)return Integer.MIN_VALUE; int mid = (l + r)/2; return Math.max(maxy(node*2,l,mid,tl,tr,tree),maxy(node*2+1,mid+1,r,tl,tr,tree)); } public static int mini(int node,int l,int r,int tl,int tr,int[][]tree){ if(l >= tl && r <= tr)return tree[node][1]; if(r < tl || l > tr)return Integer.MAX_VALUE; int mid = (l + r)/2; return Math.min(mini(node*2 , l , mid ,tl ,tr,tree),mini(node*2 + 1, mid + 1, r, tl, tr, tree)); } public static void fillParent(int[][]parent,List<List<Integer>>list,int cur ,int p,int[]depths){ parent[cur][0] = p; List<Integer>temp = list.get(cur); for(int i = 0;i<temp.size();i++){ int next = temp.get(i); if(next != p){ depths[next] = depths[cur] + 1; fillParent(parent,list,next,cur,depths); } } } public static int lca(int[][]parent , int u, int v){ if(u == v)return u; for(int i = 18;i>=0;i--){ if(parent[u][i] != parent[v][i]){ u = parent[u][i]; v = parent[v][i]; } } return parent[u][0]; } public static int bringSame(int[][]parent,int[]depths,int u, int v){ if(depths[u] < depths[v]){ int temp = u; u = v; v = temp; } int k = depths[u] - depths[v]; for(int i = 0;i<=18;i++){ if((k & (1<<i)) != 0){ u = parent[u][i]; } } return u; } public static long nck(int n,int k){ return fact[n] * inverse[n-k] %mod * inverse[k]%mod; } public static void plus(int node,int low,int high,int tlow,int thigh,int[]tree){ if(low >= tlow && high <= thigh){ tree[node]++; return; } if(high < tlow || low > thigh)return; int mid = (low + high)/2; plus(node*2,low,mid,tlow,thigh,tree); plus(node*2 + 1 , mid + 1, high,tlow, thigh,tree); } public static boolean allEqual(int[]arr,int x){ for(int i = 0;i<arr.length;i++){ if(arr[i] != x)return false; } return true; } public static long helper(StringBuilder sb){ return Long.parseLong(sb.toString()); } public static int min(int node,int low,int high,int tlow,int thigh,int[][]tree){ if(low >= tlow&& high <= thigh)return tree[node][0]; if(high < tlow || low > thigh)return Integer.MAX_VALUE; int mid = (low + high)/2; // println(low+" "+high+" "+tlow+" "+thigh); return Math.min(min(node*2,low,mid,tlow,thigh,tree) ,min(node*2+1,mid+1,high,tlow,thigh,tree)); } public static int max(int node,int low,int high,int tlow,int thigh,int[][]tree){ if(low >= tlow && high <= thigh)return tree[node][1]; if(high < tlow || low > thigh)return Integer.MIN_VALUE; int mid = (low + high)/2; return Math.max(max(node*2,low,mid,tlow,thigh,tree) ,max(node*2+1,mid+1,high,tlow,thigh,tree)); } public static long[] help(List<List<Integer>>list,int[][]range,int cur){ List<Integer>temp = list.get(cur); if(temp.size() == 0)return new long[]{range[cur][1],1}; long sum = 0; long count = 0; for(int i = 0;i<temp.size();i++){ long []arr = help(list,range,temp.get(i)); sum += arr[0]; count += arr[1]; } if(sum < range[cur][0]){ count++; sum = range[cur][1]; } return new long[]{sum,count}; } public static long findSum(int node,int low, int high,int tlow,int thigh,long[]tree,long mod){ if(low >= tlow && high <= thigh)return tree[node]%mod; if(low > thigh || high < tlow)return 0; int mid = (low + high)/2; return((findSum(node*2,low,mid,tlow,thigh,tree,mod) % mod) + (findSum(node*2+1,mid+1,high,tlow,thigh,tree,mod)))%mod; } public static boolean allzero(long[]arr){ for(int i =0 ;i<arr.length;i++){ if(arr[i]!=0)return false; } return true; } public static long count(long[][]dp,int i,int[]arr,int drank,long sum){ if(i == arr.length)return 0; if(dp[i][drank]!=-1 && arr[i]+sum >=0)return dp[i][drank]; if(sum + arr[i] >= 0){ long count1 = 1 + count(dp,i+1,arr,drank+1,sum+arr[i]); long count2 = count(dp,i+1,arr,drank,sum); return dp[i][drank] = Math.max(count1,count2); } return dp[i][drank] = count(dp,i+1,arr,drank,sum); } public static void help(int[]arr,char[]signs,int l,int r){ if( l < r){ int mid = (l+r)/2; help(arr,signs,l,mid); help(arr,signs,mid+1,r); merge(arr,signs,l,mid,r); } } public static void merge(int[]arr,char[]signs,int l,int mid,int r){ int n1 = mid - l + 1; int n2 = r - mid; int[]a = new int[n1]; int []b = new int[n2]; char[]asigns = new char[n1]; char[]bsigns = new char[n2]; for(int i = 0;i<n1;i++){ a[i] = arr[i+l]; asigns[i] = signs[i+l]; } for(int i = 0;i<n2;i++){ b[i] = arr[i+mid+1]; bsigns[i] = signs[i+mid+1]; } int i = 0; int j = 0; int k = l; boolean opp = false; while(i<n1 && j<n2){ if(a[i] <= b[j]){ arr[k] = a[i]; if(opp){ if(asigns[i] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = asigns[i]; } i++; k++; } else{ arr[k] = b[j]; int times = n1 - i; if(times%2 == 1){ if(bsigns[j] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = bsigns[j]; } j++; opp = !opp; k++; } } while(i<n1){ arr[k] = a[i]; if(opp){ if(asigns[i] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = asigns[i]; } i++; k++; } while(j<n2){ arr[k] = b[j]; signs[k] = bsigns[j]; j++; k++; } } public static long binaryExpo(long base,long expo){ if(expo == 0)return 1; long val; if(expo%2 == 1){ val = (binaryExpo(base, expo-1)*base)%mod; } else{ val = binaryExpo(base, expo/2); val = (val*val)%mod; } return (val%mod); } public static boolean isSorted(int[]arr){ for(int i =1;i<arr.length;i++){ if(arr[i] < arr[i-1]){ return false; } } return true; } //function to find the topological sort of the a DAG public static boolean hasCycle(int[]indegree,List<List<Integer>>list,int n,List<Integer>topo){ Queue<Integer>q = new LinkedList<>(); for(int i =1;i<indegree.length;i++){ if(indegree[i] == 0){ q.add(i); topo.add(i); } } while(!q.isEmpty()){ int cur = q.poll(); List<Integer>l = list.get(cur); for(int i = 0;i<l.size();i++){ indegree[l.get(i)]--; if(indegree[l.get(i)] == 0){ q.add(l.get(i)); topo.add(l.get(i)); } } } if(topo.size() == n)return false; return true; } // function to find the parent of any given node with path compression in DSU public static int find(int val,int[]parent){ if(val == parent[val])return val; return parent[val] = find(parent[val],parent); } // function to connect two components public static void union(int[]rank,int[]parent,int u,int v){ int a = find(u,parent); int b= find(v,parent); if(a == b)return; if(rank[a] == rank[b]){ parent[b] = a; rank[a]++; } else{ if(rank[a] > rank[b]){ parent[b] = a; } else{ parent[a] = b; } } } // public static int findN(int n){ int num = 1; while(num < n){ num *=2; } return num; } // code for input public static void print(String s ){ System.out.print(s); } public static void print(int num ){ System.out.print(num); } public static void print(long num ){ System.out.print(num); } public static void println(String s){ System.out.println(s); } public static void println(int num){ System.out.println(num); } public static void println(long num){ System.out.println(num); } public static void println(){ System.out.println(); } public static int Int(String s){ return Integer.parseInt(s); } public static long Long(String s){ return Long.parseLong(s); } public static String[] nextStringArray()throws IOException{ return bf.readLine().split(" "); } public static String nextString()throws IOException{ return bf.readLine(); } public static long[] nextLongArray(int n)throws IOException{ String[]str = bf.readLine().split(" "); long[]arr = new long[n]; for(int i =0;i<n;i++){ arr[i] = Long.parseLong(str[i]); } return arr; } public static int[][] newIntMatrix(int r,int c)throws IOException{ int[][]arr = new int[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Integer.parseInt(str[j]); } } return arr; } public static long[][] newLongMatrix(int r,int c)throws IOException{ long[][]arr = new long[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Long.parseLong(str[j]); } } return arr; } static class pair{ int one; int two; pair(int one,int two){ this.one = one ; this.two =two; } } public static long gcd(long a,long b){ if(b == 0)return a; return gcd(b,a%b); } public static long lcm(long a,long b){ return (a*b)/(gcd(a,b)); } public static boolean isPalindrome(String s){ int i = 0; int j = s.length()-1; while(i<=j){ if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; } // these functions are to calculate the number of smaller elements after self public static void sort(int[]arr,int l,int r){ if(l < r){ int mid = (l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); smallerNumberAfterSelf(arr, l, mid, r); } } public static void smallerNumberAfterSelf(int[]arr,int l,int mid,int r){ int n1 = mid - l +1; int n2 = r - mid; int []a = new int[n1]; int[]b = new int[n2]; for(int i = 0;i<n1;i++){ a[i] = arr[l+i]; } for(int i =0;i<n2;i++){ b[i] = arr[mid+i+1]; } int i = 0; int j =0; int k = l; while(i<n1 && j < n2){ if(a[i] < b[j]){ arr[k++] = a[i++]; } else{ arr[k++] = b[j++]; } } while(i<n1){ arr[k++] = a[i++]; } while(j<n2){ arr[k++] = b[j++]; } } public static String next(){ while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bf.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static long nextLong() { return Long.parseLong(next()); } static double nextDouble() { return Double.parseDouble(next()); } static String nextLine(){ String str = ""; try { str = bf.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } static String interpolate(int n,List<Integer>items,List<Float>prices){ List<Integer>item = new ArrayList<>(); List<Float>price = new ArrayList<>(); for(int i =0 ;i<items.size();i++){ if(items.get(i)> 0){ item.add(items.get(i)); price.add(prices.get(i)); } } DecimalFormat df = new DecimalFormat("0.00"); if(item.size() == 1){ return df.format(price.get(0))+""; } for(int i = 0;i<item.size();i++){ if(n == item.get(i)){ return df.format(price.get(i))+""; } } for(int i = 0;i<item.size()-1;i++){ if(item.get(i) < n && n < item.get(i+1)){ float x1 = item.get(i); float x2 = item.get(i+1); float y1 = price.get(i); float y2 = price.get(i+1); float m = (y2 - y1)/(x2 - x1); float c = y1 - m * x1; float ans = m * n + c; return df.format(ans)+""; } } if(item.get(0) > n){ float x1 = item.get(0); float x2 = item.get(1); float y1 = price.get(0); float y2 = price.get(1); float m = (y2 - y1)/(x2 - x1); float c = y1 - m * x1; float ans = m * n + c; return df.format(ans)+""; } int size = item.size(); float x1 = item.get(size-2); float x2 = item.get(size-1); float y1 = price.get(size-2); float y2 = price.get(size-1); float m = (y2 - y1)/(x2 - x1); float c = y1 - m * x1; float ans = m * n + c; return df.format(ans)+""; } } // use some math tricks it might help // sometimes just try to think in straightforward plan in A and B problems don't always complecate the questions with thinking too much differently // always use long number to do 10^9+7 modulo // if a problem is related to binary string it could also be related to parenthesis // *****try to use binary search(it is a very beautiful thing it can work in some of the very unexpected problems ) in the question it might work****** // try sorting // try to think in opposite direction of question it might work in your way // if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+d or a,b,c,d in general // if the question is to much related to left and/or right side of any element in an array then try monotonic stack it could work. // in range query sums try to do binary search it could work // analyse the time complexity of program thoroughly // anylyse the test cases properly // if we divide any number by 2 till it gets 1 then there will be (number - 1) operation required // try to do the opposite operation of what is given in the problem //think about the base cases properly //If a question is related to numbers try prime factorisation or something related to number theory // keep in mind unique strings //you can calculate the number of inversion in O(n log n) // in a matrix you could sometimes think about row and cols indenpendentaly. // Try to think in more constructive(means a way to look through various cases of a problem) way. // observe the problem carefully the answer could be hidden in the given test cases itself. (A, B , C); // when we have equations like (a+b = N) and we have to find the max of (a*b) then the values near to the N/2 must be chosen as (a and b); // give emphasis to the number of occurences of elements it might help. // if a number is even then we can find the pair for each and every number in that range whose bitwise AND is zero. // if a you find a problem related to the graph make a graph
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 8
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
d607977127fb1467b879ef48a2745adb
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 solve{ public static void main (String[] args) { int t=sc.nextInt(); while(t--!=0) { int n=sc.nextInt(); int d[]=sc.Array(n); if(n>=2) { int flag=0; List<Integer> ls=new ArrayList<>(); ls.add(d[0]); for(int i=1;i<n;i++) { if((d[i]<=d[i-1]&&d[i]!=0)) { flag=1;break; }else { d[i]=d[i-1]+d[i]; ls.add(d[i]); } } if(flag==1) { out.println(-1); }else{ for(int i=0;i<n;i++) { out.print(ls.get(i)+" "); } out.println(); } }else { out.println(d[0]); } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// out.flush();out.close(); } //END OF MAIN METHOD. 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 (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } /* //IMPORTANT AND NEW METHODS. 1.Sort on basis of first key :Arrays.sort(a,(a,b)->a[0]-b[0]); 2.Sort on basis of 1st key and if equal compare and sort according to second key. :Arrays.sort(a,(x,y)->x[0]==y[0]?Integer.compare(x[1], y[1]):Integer.compare(x[0], y[0])); 3.SLIDING WINDOW APPROACH :Variable size - condition given we have to maximize the size of window. :Fixed size - maximize the sum with window size of k. 4.Calculate no of digits in N :No. of digits in N = Math.floor(Math.log10(N)) + 1; 5.Multiply any number by two or divide by two : n = n << 1; // Multiply n with 2 : n = n >> 1; // Divide n by 2 6.To check weather it divisible by two or not :n&1==1// divisible by two :n$1==0// not divisible by two */ //Filling map with frequency of array elements static Map<Integer, Integer> map(int[] a) { Map<Integer,Integer> map=new HashMap<>(); for(int i:a){ map.put(i,map.getOrDefault(i,0)+1); } return map; } //factorial of n static double fact(double n) { int i=1; double fact=1; while(i<=n) { fact=fact*i; i++; } return fact; } //Calculate the nCr static int nCr(int n,int r) { double com=fact(n)/(fact(n-r)*fact(r)); return (int)com; } //Calculate the nPr static int nPr(int n, int r) { return (int) (fact(n) / fact(n - r)); } //checks power of two static boolean powerTwo(long x) { return x!=0 && ((x&(x-1)) == 0); } //swap two numbers static void swap(int a,int b) { a ^= b; b ^= a; a ^= b; } static final Random random = new Random(); static class FastScanner { public long[][] readArrayL; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) {e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long[] Arrayl(int n) { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } int[] Array(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 PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static FastScanner sc = new FastScanner(); }//END OF MAIN CLASS.
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 8
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
afec06e9b1186018629d43806a3bf678
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 Balabizo { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tt = sc.nextInt(); while(tt-->0) { int n = sc.nextInt(); int[] a = sc.nextIntArray(n) , b = new int[n]; b[0] = a[0]; boolean f = false; for(int i=1; i<n ;i++){ if(a[i] == 0){ b[i] += b[i-1]; continue; } if(b[i-1] - a[i] >= 0 && b[i-1] + a[i] >= 0) f = true; b[i] += b[i-1] + a[i]; } if(!f) { for (int x : b) pw.print(x + " "); pw.println(); } else{ pw.println(-1); } } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws IOException { br = new BufferedReader(new FileReader(file)); } 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 String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } 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 int[] nextCntArray(int n , int max) throws IOException { int[] cnt = new int[max]; for(int i=0; i<n ;i++) cnt[nextInt()]++; return cnt; } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
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 8
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
3c53659e9b20792bc4714cf5e04a327e
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 Balabizo { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tt = sc.nextInt(); while(tt-->0) { int n = sc.nextInt(); int[] a = sc.nextIntArray(n) , b = new int[n]; b[0] = a[0]; boolean f = false; for(int i=1; i<n ;i++){ if(a[i] == 0){ b[i] += b[i-1]; continue; } if(b[i-1] - a[i] >= 0) f = true; b[i] += b[i-1] + a[i]; } if(!f) { for (int x : b) pw.print(x + " "); pw.println(); } else{ pw.println(-1); } } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws IOException { br = new BufferedReader(new FileReader(file)); } 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 String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } 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 int[] nextCntArray(int n , int max) throws IOException { int[] cnt = new int[max]; for(int i=0; i<n ;i++) cnt[nextInt()]++; return cnt; } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
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 8
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
d6ed653bb018c1b946166ed9639af185
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 { 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]; for(int i = 1; i < n; i++) { a[i] = a[i - 1] + d[i]; } int flag = 0; for(int i = 1; i < n; i++) { if(d[i] != 0 && a[i - 1] - d[i] >= 0) flag = 1; } if(flag == 0) { 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 8
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
20923051b9dc95c5752731b763bf53c0
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 { 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]; for(int i = 1; i < n; i++) { a[i] = a[i - 1] + d[i]; } int flag = 0; for(int i = 1; i < n; i++) { if(d[i] != 0 && a[i - 1] - d[i] >= 0) flag = 1; } if(flag == 0) { 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 8
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
9cbeb96ac86ca3e38179d9aa53aae865
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 EDR136{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 0; i < t; i++){ int n = sc.nextInt(); int[] a = new int[n]; int[] d = new int[n]; int result = 0, count = 0; for(int j = 0; j < n; j++){ d[j] = sc.nextInt(); } a[0] = d[0]; for(int k = 1; k < n; k++){ a[k] = a[k-1] + d[k]; } for(int r = 1; r < n; r++){ if(d[r] >= d[r-1] && d[r] > a[r-1] || d[r] == 0){ count++; if(count+1 == n){ result = 1; break; } }else{ result = -1; break; } } if(result == -1){ System.out.println(-1); } else{ for(int q = 0; q < n; q++){ System.out.print(a[q] + " "); } 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 8
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
8d5d63941334c49de9ab8299b3915430
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 final class B_Array_Recovery { //int 2e9 - long 9e18 int test=12344; static PrintWriter out = new PrintWriter(System.out); static FastReader fs = new FastReader(); static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)}; static Pair[] movesDiagonal = new Pair[]{new Pair(-1, -1), new Pair(-1, 1), new Pair(1, -1), new Pair(1, 1)}; static int mod = (int) (1e9 + 7); static int mod2 = 998244353; public static void main(String[] args) { int tt = fs.nextInt(); while (tt-- > 0) { solve(); } out.flush(); } public static void solve() { int n=fs.nextInt(); int[]a=new int[n]; boolean mult=false; for(int i=0;i<n;i++) { a[i]=fs.nextInt(); } int[]pre=new int[n]; pre[0]=a[0]; for(int i=1;i<n;i++) { pre[i]=a[i]+pre[i-1]; if(a[i]<=pre[i-1] && a[i]!=0)mult=true; } if(mult==true) { out.println(-1); } else{ for(int i=0;i<pre.length;i++) { out.print(pre[i]+" "); } out.println(); } } // (10,5) = 2 ,(11,5) = 3 public static long upperDiv(long a, long b) { return (a / b) + ((a % b == 0) ? 0 : 1); } static long sum(int[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static int[] preint(int[] a) { int[] pre = new int[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] pre(int[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] post(int[] a) { long[] post = new long[a.length + 1]; post[0] = 0; for (int i = 0; i < a.length; i++) { post[i + 1] = post[i] + a[a.length - 1 - i]; } return post; } static long[] pre(long[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static void print(char A[]) { for (char c : A) { out.print(c); } out.println(); } static void print(boolean A[]) { for (boolean c : A) { out.print(c + " "); } out.println(); } static void print(int A[]) { for (int c : A) { out.print(c + " "); } out.println(); } static void print(long A[]) { for (long i : A) { out.print(i + " "); } out.println(); } static void print(List<Integer> A) { for (int a : A) { out.print(a + " "); } } 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 int[][] inputWithIdx(int N) { int A[][] = new int[N][2]; for (int i = 0; i < N; i++) { A[i] = new int[]{i, fs.nextInt()}; } return A; } static int[] input(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = fs.nextInt(); } return A; } static long[] inputLong(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) { A[i] = fs.nextLong(); } return A; } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long GCD(long a, long b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long LCM(int a, int b) { return (long) a / GCD(a, b) * b; } static long LCM(long a, long b) { return a / GCD(a, b) * b; } // find highest i which satisfy a[i]<=x static int lowerbound(int[] a, int x) { int l = 0; int r = a.length - 1; while (l < r) { int m = (l + r + 1) / 2; if (a[m] <= x) { l = m; } else { r = m - 1; } } return l; } static void shuffle(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } } static void shuffleAndSort(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int[] temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr, comparator); } static void shuffleAndSort(long[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); long temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static boolean isPerfectSquare(double number) { double sqrt = Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static void swap(int A[], int a, int b) { int t = A[a]; A[a] = A[b]; A[b] = t; } static void swap(char A[], int a, int b) { char t = A[a]; A[a] = A[b]; A[b] = t; } static long pow(long a, long b, int mod) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow = (pow * x) % mod; } x = (x * x) % mod; b /= 2; } return pow; } static long pow(long a, long b) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow *= x; } x = x * x; b /= 2; } return pow; } static long modInverse(long x, int mod) { return pow(x, mod - 2, mod); } 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; } public static String reverse(String str) { if (str == null) { return null; } return new StringBuilder(str).reverse().toString(); } public static void reverse(int[] arr) { int l = 0; int r = arr.length - 1; while (l < r) { swap(arr, l, r); l++; r--; } } public static String repeat(char ch, int repeat) { if (repeat <= 0) { return ""; } final char[] buf = new char[repeat]; for (int i = repeat - 1; i >= 0; i--) { buf[i] = ch; } return new String(buf); } public static int[] manacher(String s) { char[] chars = s.toCharArray(); int n = s.length(); int[] d1 = new int[n]; for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1); while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) { k++; } d1[i] = k--; if (i + k > r) { l = i - k; r = i + k; } } return d1; } public static int[] kmp(String s) { int n = s.length(); int[] res = new int[n]; for (int i = 1; i < n; ++i) { int j = res[i - 1]; while (j > 0 && s.charAt(i) != s.charAt(j)) { j = res[j - 1]; } if (s.charAt(i) == s.charAt(j)) { ++j; } res[i] = j; } return res; } } class Pair { int i; int j; Pair(int i, int j) { this.i = i; this.j = j; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Pair pair = (Pair) o; return i == pair.i && j == pair.j; } @Override public int hashCode() { return Objects.hash(i, j); } } class ThreePair { int i; int j; int k; ThreePair(int i, int j, int k) { this.i = i; this.j = j; this.k = k; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } ThreePair pair = (ThreePair) o; return i == pair.i && j == pair.j && k == pair.k; } @Override public int hashCode() { return Objects.hash(i, j); } } 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; } } class Node { int val; public Node(int val) { this.val = val; } } class ST { int n; Node[] st; ST(int n) { this.n = n; st = new Node[4 * Integer.highestOneBit(n)]; } void build(Node[] nodes) { build(0, 0, n - 1, nodes); } private void build(int id, int l, int r, Node[] nodes) { if (l == r) { st[id] = nodes[l]; return; } int mid = (l + r) >> 1; build((id << 1) + 1, l, mid, nodes); build((id << 1) + 2, mid + 1, r, nodes); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } void update(int i, Node node) { update(0, 0, n - 1, i, node); } private void update(int id, int l, int r, int i, Node node) { if (i < l || r < i) { return; } if (l == r) { st[id] = node; return; } int mid = (l + r) >> 1; update((id << 1) + 1, l, mid, i, node); update((id << 1) + 2, mid + 1, r, i, node); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } Node get(int x, int y) { return get(0, 0, n - 1, x, y); } private Node get(int id, int l, int r, int x, int y) { if (x > r || y < l) { return new Node(0); } if (x <= l && r <= y) { return st[id]; } int mid = (l + r) >> 1; return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y)); } Node comb(Node a, Node b) { if (a == null) { return b; } if (b == null) { return a; } return new Node(GCD(a.val, b.val)); } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % 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 8
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
305ae4f5527b337147efb3a48d415a2a
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.StringTokenizer; import java.util.*; public class B { public static void main(String[] args){ FastReader fs=new FastReader(); PrintWriter out=new PrintWriter(System.out); int N=fs.nextInt(); for(int p=0;p<N;p++){ int n=fs.nextInt(); int[] d=new int[n]; for(int i=0;i<n;i++) d[i]=fs.nextInt(); int[] ans=new int[n]; int index=1; boolean isRepeat=false; ans[0]=d[0]; for(int i=1;i<n;i++) ans[i]=ans[i-1]+d[i]; for(int i=1;i<n;i++){ if(ans[i]-ans[i-1]!=0 && ans[i]-ans[i-1]<=ans[i-1]){ out.println(-1); isRepeat=true; break; } } if(!isRepeat){ for(int i:ans) out.print(i+" "); out.println(); } } out.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 8
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
fd2c5620b9cb06d458f68738620f51fd
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 _136B { 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]; boolean flag = false; for (int j = 0; j < n; ++j){ d[j] = in.nextInt(); if (j == 0) continue; else{ if (d[j - 1] - d[j] >= 0 && d[j] != 0){ flag = true; } else d[j] += d[j - 1]; } } if (!flag){ for(int k = 0; k < n; ++k){ System.out.print(d[k] + " "); } 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 8
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
b18221f09f7ee34d46e2d6d738db3045
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 test { static PrintWriter pw; public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); pw = new PrintWriter(System.out); int t =sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int [] arr =sc.nextIntArray(n); boolean flag= false; int [] prefix = new int [n]; prefix[0] = arr[0]; for (int i = 1; i < prefix.length; i++) { prefix[i]= arr[i]+prefix[i-1]; } for (int i = 0; i < prefix.length-1; i++) { if(prefix[i]>=arr[i+1] && arr[i+1]!=0) flag=true; } if(flag) pw.println(-1); else { int x=arr[0]; pw.print(arr[0]+" "); for (int i = 1; i < arr.length; i++) { x+=arr[i]; pw.print(x+" "); } pw.println(); } } pw.close(); } public static int log2(Long N) // log base 2 { int result = (int) (Math.log(N) / Math.log(2)); return result; } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int lcm(int a, int b) { return a * b / gcd(a, b); } public static long fac(long i) { long res = 1; while (i > 0) { res = res * i--; } return res; } public static long combination(long x, long y) { return 1l * (fac(x) / (fac(x - y) * fac(y))); } public static long permutation(long x, long y) { return combination(x, y) * fac(y); } static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public int compareTo(Pair p) { if (this.x == p.x) return this.y - p.y; return this.x - p.x; } } static class SegmentTree { char[] array; int[] sTree; int N; public SegmentTree(char[] in) { array = in; N = in.length - 1; sTree = new int[2 * N]; build(1, 1, N); } public void build(int node, int l, int r) { // o(n) if (l == r) { sTree[node] = 1 << array[l] - 'a'; } else { int mid = l + r >> 1; int leftChild = node << 1; int rightChild = (node << 1) + 1; build(leftChild, l, mid); build(rightChild, mid + 1, r); sTree[node] = sTree[leftChild] | sTree[rightChild]; } } public int query(int i, int j) { return query(1, 1, N, i, j); } public int query(int node, int l, int r, int i, int j) { // [i,j] range I query on if (l > j || r < i) // no intersection return 0; if (l >= i && r <= j) // kolo gwa el range return sTree[node]; // else (some intersection) int mid = l + r >> 1; int leftChild = node << 1; int rightChild = (node << 1) + 1; int left = query(leftChild, l, mid, i, j); int right = query(rightChild, mid + 1, r, i, j); return left | right; } public void updatePoint(int index, char val) { int node = index + N - 1; array[index] = val; sTree[node] = 1 << val - 'a'; // updating the point while (node > 1) { node >>= 1; // dividing by 2 to get the parent then update it int leftChild = node << 1; int rightChild = (node << 1) + 1; sTree[node] = sTree[leftChild] | sTree[rightChild]; } } } 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 int[] nextIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public long[] nextlongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public Long[] nextLongArray(int n) throws IOException { Long[] array = new Long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public char[] nextCharArray(int n) throws IOException { char[] array = new char[n]; String string = next(); for (int i = 0; i < n; i++) { array[i] = string.charAt(i); } return array; } public boolean ready() throws IOException { return br.ready(); } } }
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 8
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
aa2ca575cd8037e72027073b4b2b27ca
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.InputStreamReader; import java.util.Arrays; public class Main{ static int T,N; static int[] nums=new int[110],res=new int[110]; public static void main(String[] args) throws Exception{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); T=Integer.parseInt(br.readLine()); while(T-->0){ int pre=0; N=Integer.parseInt(br.readLine()); String[] in=br.readLine().split(" "); Arrays.fill(nums,0); Arrays.fill(res,0); for(int i=0;i<N;i++){ nums[i]=Integer.parseInt(in[i]); } pre=nums[0]; boolean flag=false; res[0]=nums[0]; for(int i=1;i<N;i++){ if(pre<nums[i]){ int val=nums[i]+pre; res[i]=val; }else{ int val1=nums[i]+pre; int val2=pre-nums[i]; if(val2>=0&&val2!=val1){ System.out.println(-1); flag=true; break; } res[i]=val1; } pre=res[i]; } if(!flag){ for(int i=0;i<N;i++){ System.out.print(res[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 8
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
7e409f15f014e790576fe7ec021c2d8b
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.aspose.words.Document; //import com.aspose.words.FontSettings; import java.util.Arrays; import java.util.Scanner; /** * Hello world! * */ public class App { public static void main( String[] args ) throws Exception { // Document doc = new Document("C:\\Users\\obenchennouf\\Desktop\\demo_Generator\\hello.docx"); // doc.save("C:\\Users\\obenchennouf\\Desktop\\demo_Generator\\hello.pdf"); // System.out.println( "Hello World!" ); int[] array; int size; Scanner s = new Scanner(System.in); int iterations = s.nextInt(); for(int i=0;i<iterations;i++){ size = s.nextInt(); array = new int[size]; for(int j=0;j<size;j++){ array[j] = s.nextInt(); } restoreOriginalArray(array); } } static void restoreOriginalArray(int[] array){ int[] result = new int[array.length]; result[0] = array[0]; for(int i=1;i<array.length;i++){ if (array[i]!=0 && result[i-1]-array[i]>=0){ System.out.println(-1); return; } result[i] = result[i-1]+array[i]; } printArray(result); } private static void printArray(int[] result) { for(int a : result){ System.out.println(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 8
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
69887c602b86071129ce30ce2a361b40
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_Array_Recovery { public static void solve(int[] A){ int t = 0; StringBuffer sb = new StringBuffer(); for(int i = 0; i < A.length; i++){ if(i>0 && A[i] != 0 && (t+A[i] >= 0) && (t-A[i] >=0)){ System.out.println(-1); return; } t += A[i]; sb.append(t+" "); } // b-a = -3 -> b = a-3 // b-a = 3 -> b = a+3 System.out.println(sb.toString()); } public static void main(String[] args) throws Exception{ int T = in.readInt(); while(T-->0){ int N = in.readInt(); int[] A = in.readA(); solve(A); } } // snippets // readint, readlong -> ri, rl // readA, readLA -> ria, rla // readline -> r // System.out.println() -> sop static Inputer in; static { in = new Inputer(); } static class Inputer{ BufferedReader br; Inputer(){ try{ br = new BufferedReader(new InputStreamReader(System.in)); } catch(Exception e){} } public int readInt() throws Exception{ return Integer.parseInt(readLine()); } public long readLong() throws Exception{ return Long.parseLong(readLine()); } public int[] readA(String delim) throws Exception{ String[] s = readLine().split(delim); int[] A = new int[s.length]; for(int i = 0; i < s.length; i++) A[i] = Integer.parseInt(s[i]); return A; } public int[] readA() throws Exception{ String[] s = readLine().split("\\s+"); int[] A = new int[s.length]; for(int i = 0; i < s.length; i++) A[i] = Integer.parseInt(s[i]); return A; } public long[] readLA() throws Exception{ String[] s = readLine().split("\\s+"); long[] A = new long[s.length]; for(int i = 0; i < s.length; i++) A[i] = Long.parseLong(s[i]); return A; } public String readLine() throws Exception{ return br.readLine(); } public int[] copyA(int[] A){ int[] B = new int[A.length]; for(int i= 0 ; i < A.length; i++) B[i] = A[i]; return 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 8
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
505d4f167fd33f1943169918190fe7d0
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 testCases= sc.nextInt(); for (int i = 0; i < testCases; i++) { int n= sc.nextInt(); int array[] = new int[n]; int answer[] = new int[n]; for (int j = 0; j < n; j++) { array[j] = sc.nextInt(); } answer = solve(array, answer, n); if (answer[0] == -1) { System.out.println("-1"); } else { for (int j = 0; j < n; j++) { System.out.print(answer[j] + " "); } System.out.println(); } } } public static int[] solve(int[] array, int[] answer, int n) { answer[0] = array[0]; int noneZero = answer[0]; for (int i = 1; i < n; i++) { if ((array[i] > noneZero) || (array[i] == 0)) { answer[i] = array[i] + answer[i - 1]; if (answer[i] != 0) { noneZero = answer[i]; } } else { answer[0] = -1; return answer; } } return answer; } }
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 8
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
83b2001138bab5612bea0a2b0fcc0973
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
// OM NAMAH SHIVAY // 32 days remaining(2609) import java.math.BigInteger; import java.util.*; import java.io.*; public class V { static long bit[]; static boolean prime[]; static class Pair { int a; int b; Pair(int a, int b ) { this.a = a; this.b = b; } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair) o; return p.a == a && p.b == b; } return false; } public int hashCode() { int hash = 5; hash = 17 * hash + this.a; return hash; } } static long power(long x, long y, long p) { if (y == 0) return 1; if (x == 0) return 0; long res = 1l; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } 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 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 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()); } double nextDouble() { return Double.parseDouble(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()); } } static void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } static int binomialCoeff(int n, int r) { if (r > n) return 0; long m = 1000000007; long inv[] = new long[r + 1]; inv[0] = 1; if(r+1>=2) inv[1] = 1; for (int i = 2; i <= r; i++) { inv[i] = m - (m / i) * inv[(int) (m % i)] % m; } int ans = 1; for (int i = 2; i <= r; i++) { ans = (int) (((ans % m) * (inv[i] % m)) % m); } for (int i = n; i >= (n - r + 1); i--) { ans = (int) (((ans % m) * (i % m)) % m); } return ans; } static BigInteger bi(String str) { return new BigInteger(str); } // Author - vaibhav_1710 static FastScanner fs = null; static int mod= 1_000_000_00; static int a[]; static int p[]; static int[][] dirs = { { 0, -1 }, { -1, 0 }, { 0, 1 }, { 1, 0 }, }; static int dp[][][]; static ArrayList<Integer> al[]; static int k1; static int k2; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t=fs.nextInt(); outer:while(t-->0) { int n = fs.nextInt(); int a[] = fs.readArray(n); int ans[] = new int[n]; ans[0]=a[0]; int curr=ans[0]; for(int i=1;i<n;i++){ if(curr>=a[i] && a[i]!=0){ out.println("-1"); continue outer; }else{ curr += a[i]; ans[i]=curr; } } for(int v:ans){ out.print(v+" "); } out.println(); } 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 8
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
33d766a61de614fadf5b694d28244cac
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
// "static void main" must be defined in a public class. import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int T = fs.nextInt(); for(int i = 0; i < T; i++) { int N = fs.nextInt(); int[] A = new int[N]; for(int j = 0; j < N; j++) { A[j] = fs.nextInt(); } int[] ans = new int[N]; Arrays.fill(ans, 0); ans[0] = A[0]; boolean a = true; for(int j = 1; j < N; j++) { if(A[j] <= ans[j-1] && A[j] != 0) { a = false; break; }else{ ans[j] = ans[j-1] + A[j]; } } if(a == false) { out.println(-1); }else{ for(int j = 0; j < N; j++) { out.print(ans[j] + " "); } out.println(); } } out.close(); } static long gcd(long a, long b) { if(b == 0) { return a; }else{ return gcd(b, a%b); } } 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()); } } }
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 8
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
9a9341e0895fa8ac5dc2b160adc2511f
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 Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); long n = in.nextInt(); while (n-- > 0) { int m = in.nextInt(); int[] temp = new int[m]; for (int i = 0; i < m; i++) { temp[i] = in.nextInt(); } int[] res = new int[m]; res[0] = temp[0]; boolean flag = false; for (int i = 1; i < m; i++) { int i1 = res[i - 1] + temp[i]; int i2 = res[i - 1] - temp[i]; if (i1 >= 0 && i2 >= 0 && i1 != i2){ flag = true; break; }else { res[i] = i1 >= 0 ? i1 : i2; } } StringBuilder str = new StringBuilder(); for (int re : res) { str.append(re); str.append(" "); } System.out.println(flag ? -1 : str.toString()); } } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % 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 8
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
23530d90c84a993f86083e3f8927cce6
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.*; public class Main { public static class Pair { int key, value; public Pair(int key, int value) { this.key = key; this.value = 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 { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } public static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } //BINARY EXPONENTIATION public static long binpow(long a, long b) { long res = 1; while (b > 0) { if ((b & 1) == 1) res = res * a; a = a * a; b >>= 1; } return res; } // Returns factorial of n static int fact(int n) { if (n == 0) return 1; int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); //int t = 1; while (t-- > 0) { int n = s.nextInt(); int arr[] = new int[n]; for(int i=0; i<n; i++){ arr[i] = s.nextInt(); } long sum=arr[0]; int c=1; for(int i=1; i<n; i++){ if(arr[i]!=0){ if(sum>=arr[i] && c==1){ System.out.println(-1); c=0; }else{ sum+=arr[i]; } } } if(c==1){ for(int i=1; i<n; i++){ arr[i]+=arr[i-1]; } 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 8
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
8095ccfd4cbf3cb8690e0507d7ed116a
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.awt.*; import java.util.*; import java.io.*; // You have that in you, you can do it........ public class Codeforces { static FScanner sc = new FScanner(); static PrintWriter out = new PrintWriter(System.out); static final Random random = new Random(); static long mod = 1000000007L; static HashMap<String, Integer> map = new HashMap<>(); static boolean[] sieve = new boolean[1000000]; static double[] fib = new double[1000000]; public static void main(String args[]) throws IOException { int T = sc.nextInt(); while (T-- > 0) { int n = sc.nextInt(); int[] d = sc.readintarray(n); int[] a = new int[n]; a[0]=d[0]; boolean b=true; for(int i=1;i<n;i++) { int cnt=0; if(a[i-1]+d[i]>=0) { a[i]=a[i-1]+d[i]; cnt++; } if(a[i-1]-d[i]>=0&&a[i]!=(a[i-1]-d[i])) { a[i]=a[i-1]-d[i]; cnt++; } if(cnt==2) { b=false; break; } } if(b) { for(int i=0;i<n;i++) out.print(a[i]+" "); out.println(); } else out.println(-1); } out.close(); } // TemplateCode static int maxi(int[] a) { int maxi = 0; int max = Integer.MIN_VALUE; for (int i = 0; i < a.length; i++) { if (a[i] > max) { max = a[i]; maxi = i; } } return maxi; } static void fib() { fib[0] = 0; fib[1] = 1; for (int i = 2; i < fib.length; i++) fib[i] = fib[i - 1] + fib[i - 2]; } static void primeSieve() { for (int i = 0; i < sieve.length; i++) sieve[i] = true; for (int i = 2; i * i <= sieve.length; i++) { if (sieve[i]) { for (int j = i * i; j < sieve.length; j += i) { sieve[j] = false; } } } } static int max(int a, int b) { if (a < b) return b; return a; } static int min(int a, int b) { if (a < b) return a; return b; } static void ruffleSort(int[] a) { int n = a.length; 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 <E> void print(E res) { System.out.println(res); } static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int abs(int a) { if (a < 0) return -1 * a; return a; } static class Pair { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } } static class FScanner { BufferedReader br; StringTokenizer st; public FScanner() { 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[] readintarray(int n) { int res[] = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] readlongarray(int n) { long res[] = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } } }
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 8
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
7ce9caf2e2e78ba90c9cbdc19264f777
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 arrayGeneration { public static void main(String[]args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0; i<t ; i++) { boolean flag= true; int n= sc.nextInt(); int[]a= new int [n]; int[]b= new int [n]; for(int j=0; j<n;j++) { a[j]=sc.nextInt(); if(j==0) { b[j]=a[j]; }else{ if(a[j]<=b[j-1] && a[j]!=0) { flag =false; }else { b[j]= a[j]+b[j-1]; } } } if(flag) { for(int j =0 ; j<n; j++) { System.out.print(b[j]+" "); } 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 8
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
a0300096981957e985cb2800ea77b4a2
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) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int test = in.nextInt(); for(int t =1;t<=test;t++){ int n = in.nextInt(); int a[] = new int[n]; for(int i = 0;i<n;i++){ a[i] = in.nextInt(); } int ans[] = new int[n]; int ara[] = new int[n]; int sum = 0; for(int i = 0;i<n;i++){ sum +=a[i]; ans[i] = sum; ara[i] = sum; } boolean flag =true; for(int i = 1;i<n;i++){ if(a[i]!=0 && ans[i-1]-a[i]>=0){ flag = false; break; } } if(flag){ for(int i :ans){ pw.print(i+" "); } pw.println(); } else { pw.println(-1); } } pw.close(); } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
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 8
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
af4be5fa9a9606ca60ede5f136077769
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(); for(int i=0;i<t;i++){ int n=sc.nextInt(); int [] arr= new int[n]; for(int ii=0;ii<n;ii++){ arr[ii]= sc.nextInt(); } solve1739B(arr); } } public static void solve1739B(int[]arr){ int[]unknown= new int[arr.length]; unknown[0]=arr[0]; StringBuilder sb= new StringBuilder(); for(int i=1;i<unknown.length;i++){ int prev = unknown[i-1]; int a = prev + arr[i]; int b = prev - arr[i]; if( a>=0 && b>=0 && a!=b){ System.out.println(-1); return; } else{ unknown[i]= Math.max(a,b); } } for(int i=0;i<unknown.length;i++){ sb.append(unknown[i]+" "); } System.out.println(sb.toString()); } public static void solve1739A(int r,int c){ r/=2; c/=2; r++; c++; System.out.println(r+" "+c); } public static void solve1738(){ Scanner sc= new Scanner(System.in); int t=sc.nextInt(); for(int ii=0;ii<t;ii++) { int n = sc.nextInt(); int[] skillTypes = new int[n]; int[] damages = new int[n]; ArrayList<Integer> fires = new ArrayList<>(); ArrayList<Integer> frosts = new ArrayList<>(); for (int i = 0; i < n; i++) { skillTypes[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { damages[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { if (skillTypes[i] == 0) fires.add(damages[i]); else frosts.add(damages[i]); } Collections.sort(fires); //System.out.println(fires); Collections.sort(frosts); //System.out.println(frosts); int count = Math.min(fires.size(), frosts.size()); long sum=0; for(int i=0;i<count;i++){ sum+=2*fires.get(fires.size()-1-i); sum+=2* frosts.get(frosts.size()-1-i); } if(fires.size()>frosts.size()){ for(int i=0;i<fires.size()-count;i++) sum+=fires.get(i); } else if(frosts.size()>fires.size()){ for(int i=0;i<frosts.size()-count;i++) sum+=frosts.get(i); } else{ sum-=Math.min(fires.get(0),frosts.get(0) ); } System.out.println(sum); continue; } } }
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 8
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
cbe410af7a877153aaddef00d40a3f61
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
/* This code is written by KESHAV DAGA @keshavkeshu */ import java.util.*; import java.io.*; public class Main { static FastReader sc=new FastReader(); static final Random random=new Random(); static int mod=1000000007; static int inf=Integer.MAX_VALUE; static int ninf=Integer.MIN_VALUE; static HashMap<String,Integer>map=new HashMap<>(); static String yes="YES",no="NO"; public static void main(String args[]) throws IOException { /* This is main section or the coding part @keshavkeshu */ int t=sc.nextInt(); StringBuilder res=new StringBuilder(); while(t-->0) { // logic int n=sc.nextInt(); int arr[]=sc.ria(n); int d[]=new int[n]; d[0]=arr[0]; res=new StringBuilder(); res.append(arr[0]+" "); for(int i=1;i<n;i++) { if(arr[i]!=0 && arr[i]<=d[i-1]){ res=new StringBuilder(); res.append("-1"); break; }else{ d[i]=arr[i]+d[i-1]; res.append(d[i]+" "); } } //res.append(ans+"\n"); print(res); } //print(res); } static int max(int a, int b) { if(a<b) return b; return a; } static int min(int a, int b) { if(a>b) return b; return a; } static long max(long a, long b) { if(a<b) return b; return a; } static long min(long a, long b) { if(a>b) return b; return a; } static int max(int a, int b, int c) { if(a>=b && a>=c) return a; else if(b>=a && b>=c) return b; else return c; } static int min(int a, int b, int c) { if(a<=b && a<=c) return a; else if(b<=a && b<=c) return b; else return c; } static void ruffleSort(int[] a) { int n=a.length; 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 < E > void print(E res) { System.out.println(res); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int abs(int a) { if(a<0) return -1*a; return 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; } int [] ria(int n) { int res [] = new int [n]; for(int i = 0; i<n; i++)res[i] = nextInt(); return res; } long [] rla(int n) { long res [] = new long [n]; for(int i = 0; i<n; i++)res[i] = nextLong(); return res; } } }
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 8
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
88149b136adf496000b6fec57db1d304
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 //class contest { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int[] d = new int[n]; int[] a = new int[n]; for(int i =0;i<n;i++){ d[i]=sc.nextInt(); } int flag =0; a[0]=d[0]; for(int i =1;i<n;i++){ if(d[i]==0){ a[i]=a[i-1]; } else{ if(a[i-1]-d[i]>=0){ flag=1; break; } else a[i]=d[i]+a[i-1]; } } if(flag==1) System.out.println(-1); else{ for(int i =0;i<n;i++){ System.out.print(a[i]+" "); } System.out.println(); } } } /* ################################functions##################################################### */ //power of ten public static boolean isPowerOfTen(long input) { if (input % 10 != 0 || input == 0) { return false; } if (input == 10) { return true; } return isPowerOfTen(input/10); } //method to check prime static boolean isPrime(long n) { // Corner cases 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; } // method to return gcd of two numbers static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } // method to return LCM of two numbers static long lcm(long a, long b) { return (a / gcd(a, 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 8
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
fbd4fb500ddb1da29d43f8eda96f2ec9
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 arrayRes{ 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 < d.length; i++) { d[i]=sc.nextInt(); } int[] a = new int[n]; a[0]=d[0]; int x = 0; for (int i = 1; i < a.length; i++) { if(d[i]!=0){ a[i]=a[i-1]-d[i]; if(a[i]>=0){ x++; } a[i]=d[i]+a[i-1]; } else{ a[i]=d[i]+a[i-1]; } } if(x==0){ for (int i = 0; i < a.length; 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 8
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