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
77c5b46b76b1f900cb6855ede7e5eb25
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.io.*; public class practice { public static void solve() { Reader sc = new Reader(); PrintWriter out = new PrintWriter(System.out); 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(); boolean ok = true; List<Integer> list = new ArrayList<>(); list.add(arr[0]); for(int i = 1;i < n;i++) { if(arr[i] != 0 && arr[i] <= list.get(i - 1)) { ok = false; break; } else list.add((arr[i] + list.get(i - 1))); } if(ok) { for(int i : list) out.print(i + " "); out.println(); } else out.println(-1); } out.flush(); } public static void main(String[] args) throws IOException { solve(); } static class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if(st.hasMoreTokens()) str = st.nextToken("\n"); else str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
90e9c332a1b27b628f197efd0643e74b
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 { static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); static int dx[]={0,0,-1,1,-1,-1,1,1},dy[]={-1,1,0,0,-1,1,-1,1}; static final double pi=3.1415926536; // static long mod=1000000007; static long mod=998244353; static int MAX=Integer.MAX_VALUE; static int MIN=Integer.MIN_VALUE; static long MAXL=Long.MAX_VALUE; static long MINL=Long.MIN_VALUE; static ArrayList<Integer> graph[]; static long fact[]; static long seg[]; static long dp[][]; // static long dp[][]; public static void main (String[] args) throws java.lang.Exception { // code goes here int t=I(); outer:while(t-->0) { int n=I(); int d[]=I(n); int a[]=new int[n]; a[0]=d[0]; int q=0; for(int i=1;i<n;i++){ int val1=d[i]; int val2=-1*d[i]; int temp1=val1+a[i-1]; int temp2=val2+a[i-1]; // out.println(temp1+" "+temp2); if(temp1==temp2 && temp1>=0){ a[i]=temp1; }else if(temp1<0 && temp2<0){ q=1; break; }else if(temp1<0 || temp2<0){ a[i]=(temp1<0?temp2:temp1); }else{ q=1; break; } } if(q==1){ out.println("-1"); }else{ printArray(a); } } out.close(); } public static class pair { int a; int b; public pair(int aa,int bb) { a=aa; b=bb; } } public static class myComp implements Comparator<pair> { //sort in ascending order. public int compare(pair p1,pair p2) { if(p1.a==p2.a) return p1.b-p2.b; else if(p1.a<p2.a) return -1; else return 1; } // sort in descending order. // public int compare(pair p1,pair p2) // { // if(p1.b==p2.b) // return 0; // else if(p1.b<p2.b) // return 1; // else // return -1; // } } public static void setGraph(int n,int m)throws IOException { graph=new ArrayList[n+1]; for(int i=0;i<=n;i++){ graph[i]=new ArrayList<>(); } for(int i=0;i<m;i++){ int u=I(),v=I(); graph[u].add(v); graph[v].add(u); } } //LOWER_BOUND and UPPER_BOUND functions //It returns answer according to zero based indexing. public static int lower_bound(pair[] arr,int X,int start, int end) //start=0,end=n-1 { if(start>end)return -1; // if(arr.get(end)<X)return end; if(arr[end].a<X)return end; // if(arr.get(start)>X)return -1; if(arr[start].a>X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; if(arr[mid].a==X){ // if(arr.get(mid)==X){ //Returns last index of lower bound value. if(mid<end && arr[mid+1].a==X){ // if(mid<end && arr.get(mid+1)==X){ left=mid+1; }else{ return mid; } } // if(arr.get(mid)==X){ //Returns first index of lower bound value. // if(arr[mid]==X){ // // if(mid>start && arr.get(mid-1)==X){ // if(mid>start && arr[mid-1]==X){ // right=mid-1; // }else{ // return mid; // } // } else if(arr[mid].a>X){ // else if(arr.get(mid)>X){ if(mid>start && arr[mid-1].a<X){ // if(mid>start && arr.get(mid-1)<X){ return mid-1; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1].a>X){ // if(mid<end && arr.get(mid+1)>X){ return mid; }else{ left=mid+1; } } } return left; } //It returns answer according to zero based indexing. public static int upper_bound(long arr[],long X,int start,int end) //start=0,end=n-1 { if(arr[0]>=X)return start; if(arr[arr.length-1]<X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; if(arr[mid]==X){ //returns first index of upper bound value. if(mid>start && arr[mid-1]==X){ right=mid-1; }else{ return mid; } } // if(arr[mid]==X){ //returns last index of upper bound value. // if(mid<end && arr[mid+1]==X){ // left=mid+1; // }else{ // return mid; // } // } else if(arr[mid]>X){ if(mid>start && arr[mid-1]<X){ return mid; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1]>X){ return mid+1; }else{ left=mid+1; } } } return left; } //END //Segment Tree Code public static void buildTree(long a[],int si,int ss,int se) { if(ss==se){ seg[si]=a[ss]; return; } int mid=(ss+se)/2; buildTree(a,2*si+1,ss,mid); buildTree(a,2*si+2,mid+1,se); seg[si]=max(seg[2*si+1],seg[2*si+2]); } // public static void update(int si,int ss,int se,int pos,int val) // { // if(ss==se){ // // seg[si]=val; // return; // } // int mid=(ss+se)/2; // if(pos<=mid){ // update(2*si+1,ss,mid,pos,val); // }else{ // update(2*si+2,mid+1,se,pos,val); // } // // seg[si]=min(seg[2*si+1],seg[2*si+2]); // if(seg[2*si+1].a < seg[2*si+2].a){ // seg[si].a=seg[2*si+1].a; // seg[si].b=seg[2*si+1].b; // }else{ // seg[si].a=seg[2*si+2].a; // seg[si].b=seg[2*si+2].b; // } // } public static long query(int si,int ss,int se,int qs,int qe) { if(qs>se || qe<ss)return 0; if(ss>=qs && se<=qe)return seg[si]; int mid=(ss+se)/2; long p1=query(2*si+1,ss,mid,qs,qe); long p2=query(2*si+2,mid+1,se,qs,qe); return max(p1,p2); } public static void merge(ArrayList<Integer> f,ArrayList<Integer> a,ArrayList<Integer> b) { int i=0,j=0; while(i<a.size() && j<b.size()){ if(a.get(i)<=b.get(j)){ f.add(a.get(i)); i++; }else{ f.add(b.get(j)); j++; } } while(i<a.size()){ f.add(a.get(i)); i++; } while(j<b.size()){ f.add(b.get(j)); j++; } } //Segment Tree Code end //Prefix Function of KMP Algorithm public static int[] KMP(char c[],int n) { int pi[]=new int[n]; for(int i=1;i<n;i++){ int j=pi[i-1]; while(j>0 && c[i]!=c[j]){ j=pi[j-1]; } if(c[i]==c[j])j++; pi[i]=j; } return pi; } public static long kadane(long a[],int n) //largest sum subarray { long max_sum=Long.MIN_VALUE,max_end=0; for(int i=0;i<n;i++){ max_end+=a[i]; if(max_sum<max_end){max_sum=max_end;} if(max_end<0){max_end=0;} } return max_sum; } public static ArrayList<Long> primeFact(long x) { ArrayList<Long> arr=new ArrayList<>(); if(x%2==0){ arr.add(2L); while(x%2==0){ x/=2; } } for(long i=3;i*i<=x;i+=2){ if(x%i==0){ arr.add(i); while(x%i==0){ x/=i; } } } if(x>0){ arr.add(x); } return arr; } public static long nPr(int n,int r) { long ans=divide(fact(n),fact(n-r),mod); return ans; } public static long nCr(int n,int r) { long ans=divide(fact[n],mul(fact[n-r],fact[r]),mod); return ans; } public static boolean isSorted(int a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static boolean isSorted(long a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } public static int computeXOR(int n) //compute XOR of all numbers between 1 to n. { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } public static int np2(int x) { x--; x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x++; return x; } public static int hp2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static long hp2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static ArrayList<Integer> primeSieve(int n) { ArrayList<Integer> arr=new ArrayList<>(); boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } // Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n); public static class FenwickTree { int farr[]; int n; public FenwickTree(int c) { n=c+1; farr=new int[n]; } // public void update_range(int l,int r,long p) // { // update(l,p); // update(r+1,(-1)*p); // } public void update(int x,int p) { for(;x<n;x+=x&(-x)) { farr[x]+=p; } } public int get(int x) { int ans=0; for(;x>0;x-=x&(-x)) { ans=ans+farr[x]; } return ans; } } //Disjoint Set Union //NOTE: call find function for all the index in the par array at last, //in order to set parent of every index properly. public static class DSU { int par[],rank[]; public DSU(int c) { par=new int[c+1]; rank=new int[c+1]; for(int i=0;i<=c;i++) { par[i]=i; rank[i]=0; } } public int find(int a) { if(a==par[a]) return a; return par[a]=find(par[a]); } public void union(int a,int b) { int a_rep=find(a),b_rep=find(b); if(a_rep==b_rep) return; if(rank[a_rep]<rank[b_rep]) par[a_rep]=b_rep; else if(rank[a_rep]>rank[b_rep]) par[b_rep]=a_rep; else { par[b_rep]=a_rep; rank[a_rep]++; } } } public static boolean isVowel(char c) { if(c=='a' || c=='e' || c=='i' || c=='u' || c=='o')return true; return false; } public static boolean isInteger(double N) { int X = (int)N; double temp2 = N - X; if (temp2 > 0) { return false; } return true; } public static boolean isPalindrome(String s) { int n=s.length(); for(int i=0;i<=n/2;i++){ if(s.charAt(i)!=s.charAt(n-i-1)){ return false; } } return true; } public static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static long fact(long n) { long fact=1; for(long i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static long fact(int n) { long fact=1; for(int i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static void printArray(long a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(int a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(char a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]); } out.println(); } public static void printArray(String a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(boolean a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(pair a[]) { for(pair p:a){ out.println(p.a+"->"+p.b); } } public static void printArray(int a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(String a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(boolean a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(long a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(char a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(ArrayList<?> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printMapI(HashMap<?,?> hm){ for(Map.Entry<?,?> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } public static void printMap(HashMap<Long,ArrayList<Integer>> hm){ for(Map.Entry<Long,ArrayList<Integer>> e:hm.entrySet()){ out.print(e.getKey()+"->"); ArrayList<Integer> arr=e.getValue(); for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); }out.println(); } } public static void printGraph(ArrayList<Integer> graph[]) { int n=graph.length; for(int i=0;i<n;i++){ out.print(i+"->"); for(int j:graph[i]){ out.print(j+" "); }out.println(); } } //Modular Arithmetic public static long add(long a,long b) { a+=b; if(a>=mod)a-=mod; return a; } public static long sub(long a,long b) { a-=b; if(a<0)a+=mod; return a; } public static long mul(long a,long b) { return ((a%mod)*(b%mod))%mod; } public static long divide(long a,long b,long m) { a=mul(a,modInverse(b,m)); return a; } public static long modInverse(long a,long m) { int x=0,y=0; own p=new own(x,y); long g=gcdExt(a,m,p); if(g!=1){ out.println("inverse does not exists"); return -1; }else{ long res=((p.a%m)+m)%m; return res; } } public static long gcdExt(long a,long b,own p) { if(b==0){ p.a=1; p.b=0; return a; } int x1=0,y1=0; own p1=new own(x1,y1); long gcd=gcdExt(b,a%b,p1); p.b=p1.a - (a/b) * p1.b; p.a=p1.b; return gcd; } public static long pwr(long m,long n) { long res=1; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m); } n=n>>1; m=(m*m); } return res; } public static long modpwr(long m,long n) { long res=1; m=m%mod; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m)%mod; } n=n>>1; m=(m*m)%mod; } return res; } public static class own { long a; long b; public own(long val,long index) { a=val; b=index; } } //Modular Airthmetic public static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(char[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { char tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } //max & min public static int max(int a,int b){return Math.max(a,b);} public static int min(int a,int b){return Math.min(a,b);} public static int max(int a,int b,int c){return Math.max(a,Math.max(b,c));} public static int min(int a,int b,int c){return Math.min(a,Math.min(b,c));} public static long max(long a,long b){return Math.max(a,b);} public static long min(long a,long b){return Math.min(a,b);} public static long max(long a,long b,long c){return Math.max(a,Math.max(b,c));} public static long min(long a,long b,long c){return Math.min(a,Math.min(b,c));} public static int maxinA(int a[]){int n=a.length;int mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static long maxinA(long a[]){int n=a.length;long mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static int mininA(int a[]){int n=a.length;int mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long mininA(long a[]){int n=a.length;long mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long suminA(int a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} public static long suminA(long a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} //end public static int[] I(int n)throws IOException{int a[]=new int[n];for(int i=0;i<n;i++){a[i]=I();}return a;} public static long[] IL(int n)throws IOException{long a[]=new long[n];for(int i=0;i<n;i++){a[i]=L();}return a;} public static long[] prefix(int a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;} public static long[] prefix(long a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;} public static int I()throws IOException{return sc.I();} public static long L()throws IOException{return sc.L();} public static String S()throws IOException{return sc.S();} public static double D()throws IOException{return sc.D();} } 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 I(){ return Integer.parseInt(next());} long L(){ return Long.parseLong(next());} double D(){return Double.parseDouble(next());} String S(){ 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
2396ec81c18f9d75acefb6c649791210
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
/*############################################################################################################ ########################################## >>>> Diaa12360 <<<< ############################################### ########################################### Just Nothing ################################################# #################################### If You Need it, Fight For IT; ######################################### ###############################################.-. 1 5 9 2 .-.################################################ ############################################################################################################*/ import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Solution { public static void main(String[] args) throws Exception { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } boolean ans = true; for (int i = 0; i < n - 1; i++) { if (arr[i + 1] <= arr[i] && arr[i + 1] != 0) { ans = false; break; } arr[i + 1] += arr[i]; } if (!ans) out.print(-1); else for (int x : arr) { out.print(x + " "); } out.println(); } out.flush(); } static int lcm(int a, int b) { return a / gcd(a, b) * b; } static int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } //all primes static ArrayList<Integer> primes; static boolean[] primesB; //sieve algorithm static void sieve(int n) { primes = new ArrayList<>(); primesB = new boolean[n + 1]; for (int i = 2; i <= n; i++) { primesB[i] = true; } for (int i = 2; i * i <= n; i++) { if (primesB[i]) { for (int j = i * i; j <= n; j += i) { primesB[j] = false; } } } for (int i = 0; i <= n; i++) { if (primesB[i]) { primes.add(i); } } } // Function to find gcd of array of // numbers static int findGCD(int[] arr, int n) { int result = arr[0]; for (int element : arr) { result = gcd(result, element); if (result == 1) { return 1; } } return result; } private static class FastScanner { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; private FastScanner() throws IOException { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private short nextShort() throws IOException { short ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = (short) (ret * 10 + c - '0'); while ((c = read()) >= '0' && c <= '9'); if (neg) return (short) -ret; return ret; } private int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } private char nextChar() throws IOException { byte c = read(); while (c <= ' ') c = read(); return (char) c; } private String nextString() throws IOException { StringBuilder ret = new StringBuilder(); byte c = read(); while (c <= ' ') c = read(); do { ret.append((char) c); } while ((c = read()) > ' '); return ret.toString(); } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } private static int anInt(String s) { return Integer.parseInt(s); } private static long ll(String s) { return Long.parseLong(s); } } class Pair<K, V> implements Comparable<Pair<K, V>> { K first; V second; Pair(K f, V s) { first = f; second = s; } @Override public int compareTo(Pair<K, V> o) { return 0; } } class PairInt implements Comparable<PairInt> { int first; int second; PairInt(int f, int s) { first = f; second = s; } @Override public int compareTo(PairInt o) { if (this.first > o.first) { return 1; } else if (this.first < o.first) return -1; else { if (this.second < o.second) return 1; else if (this.second == o.second) return -1; return 0; } } @Override public String toString() { return "<" + first + ", " + second + ">"; } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
609739f582de2efd14d91492bdf28fb2
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 A { static Scanner sc; static PrintWriter pw; public static void main(String[] args) throws IOException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int d[]=sc.nextIntArray(n); int ans[]=new int [n]; ans[0]=d[0]; boolean f=false; for (int i = 1; i < ans.length; i++) { ans[i]=d[i]+ans[i-1]; } for (int i = ans.length-1; i >0; i--) { int m=ans[i]-ans[i-1]; if(m<=ans[i-1]&&m!=0) f=true; } if(f) pw.println(-1); else { for (int i = 0; i < ans.length; i++) pw.print(ans[i]+" "); pw.println(); } } pw.flush(); } } 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 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 String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public 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
d354694a1c46df9b623dcb61a85e6c04
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 abc { public static void main(String[] args){ Scanner scn = new Scanner(System.in); int t = scn.nextInt(); for(int x=0;x<t;x++){ int n=scn.nextInt(); int d[] = new int[n]; for(int i=0;i<n;i++){ d[i]= scn.nextInt(); } int a[]=new int[n]; a[0]=d[0]; boolean flag = true; for(int i=1;i<n;i++){ int j = Math.abs(d[i]-a[i-1]); if(Math.abs(j-a[i-1]) == d[i] && j != d[i]+a[i-1]){ System.out.println(-1); flag = false; break; } a[i] = a[i-1]+d[i]; } for(int i=0;i<n && flag;i++){ System.out.print(a[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 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
44a4f0f8723cb1537ac06a5bec4e105c
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.math.*; public class B_1739 { 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 z=0; for(int i=0;i<n;i++) { d[i]=sc.nextInt(); if(d[i]==0) z++; } if(z==n) { for(int i=0;i<n;i++) System.out.print(0+" "); continue; } int[] a=new int[n]; a[0]=d[0]; boolean check=true; for(int i=1;i<n;i++) { a[i]=a[i-1]+d[i]; if(a[i-1]-d[i]!=a[i] && a[i-1]-d[i]>=0) check=false; } if(check) { for(int i=0;i<n;i++) System.out.print(a[i]+" "); } else System.out.print(-1); System.out.println(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } 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
bdf8ea0359752ea9e502e0e4429eb963
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 code1 { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int tc = sc.nextInt(); while (tc-- > 0) { solve(); } } static void solve() { int n=sc.nextInt(); int[] d=new int[n]; for (int i = 0; i < n; i++) { d[i]=sc.nextInt(); } int[] a=new int[n]; a[0]=d[0]; for (int i = 1; i <n; i++) { boolean fl=false; int val=-1; for (int j = 0; j <=100000; j++) { if(Math.abs(j-a[i-1])==d[i]){ if(!fl){ val=j; fl=true; } else{ System.out.println(-1); return; } } } if(!fl){ System.out.println(-1); return; } else a[i]=val; } for (int i = 0; i < n; i++) { System.out.print(a[i]+" "); } System.out.println(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
f5a8dc61fac7bf5d3f792bde1fb719f3
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 Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0){ int n = sc.nextInt(); int arr[] = new int [n]; boolean possible = true; arr[0] = sc.nextInt(); for(int i=1;i<n;i++){ int diff = sc.nextInt(); if(diff == 0){ arr[i] = arr[i-1]; continue; } int v1 = arr[i-1] - diff; int v2 = arr[i-1] + diff; if(v1 >= 0 && v2 >=0 && possible){ System.out.println(-1); possible = false; } arr[i] = v2; } if(possible){ 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
638f769df7696ded1c455dee7fa8b90e
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.math.*; public class A { public static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static String concat(String s1, String s2) { return new StringBuilder(s1).append(s2).toString(); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static boolean isPrime(int n) { if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } public class linearArray{ int nStudents; int maxIntegers; int l []; public linearArray(int maxIntegers) { this.maxIntegers = maxIntegers; nStudents = 0; l = new int[maxIntegers]; } // O(1) void insertLast(int x) { if (nStudents >= maxIntegers) return; l[nStudents++] = x; } void insertFirst(int x) { if (nStudents >= maxIntegers) return; for(int i = nStudents;i>0;i--) { l[i] = l[i-1]; } l[0] = x; nStudents++; } int linearSearch(int x) { for (int i = 0;i<nStudents;i++) { if (l[i] == x) { return i; } } return -1; } void delete(int x) { int s = linearSearch(x); for (int i = s;i<nStudents-1;i++) { l[i] = l[i+1]; } nStudents--; } } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t1 = sc.nextInt(); outer: while (t1-- > 0) { int n = sc.nextInt(); long l [] = sc.readArray(n); long d [] = new long[n]; d[0] = l[0]; boolean works = true; for (int i = 1;i<n;i++) { long x = (long)Math.abs(l[i]-d[i-1]); long x1 = (long)Math.abs(l[i]+d[i-1]); if (Math.abs(x-d[i-1]) == l[i] &&Math.abs(x1-d[i-1]) == l[i] && x!=x1) { out.println(-1); continue outer; }else { d[i] = x1; } } for (int i = 0;i<n;i++) { out.print(d[i]+ " "); } out.println(); } out.close(); } static class Pair implements Comparable<Pair> { long first; long second; public Pair(long first, long second) { this.first = first; this.second = second; } public int compareTo(Pair p) { if (first != p.first) return Long.compare(first, p.first); else if (second != p.second) return Long.compare(second, p.second); else return 0; } } static class Tuple implements Comparable<Tuple> { long first; long second; long third; public Tuple(long a, long b, long c) { first = a; second = b; third = c; } public int compareTo(Tuple t) { if (first != t.first) return Long.compare(first, t.first); else if (second != t.second) return Long.compare(second, t.second); else if (third != t.third) return Long.compare(third, t.third); else return 0; } } static final Random random = new Random(); static void shuffleSort(int[] a) { int n = a.length; for (int i = 0; i < n; i++) { int r = random.nextInt(n), temp = a[r]; a[r] = a[i]; a[i] = temp; } Arrays.sort(a); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } long[] readArray(int n) throws IOException { 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 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
b2dfe068a7a4ae4a8031c7177c04e89e
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.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /* * Reading from STDIN: * - Integer _readInt(BufferedReader reader) * - BigInteger _readBigInteger(BufferedReader reader) * - BigDecimal _readBigDecimal(BufferedReader reader) * - List<Integer> _readIntList(BufferedReader reader) * - _readIntArray(BufferedReader reader) * * - <T> Map<T, Long> _getCount(Collection<T> values) * - _getMaxIndex(int[] array) * - int[] _concat(int[] array1, int[] array2) * - double _logb(double x) * - long _lcd(long a, long b) * - _printArray(int[] array) * - _printBoolean(boolean b) */ public class TaskB { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int total = _readInt(reader); outer: for (int i = 0; i < total; i++) { int count = _readInt(reader); List<Integer> values = _readIntList(reader); List<Integer> result = buildList(values); if (result == null) { System.out.println(-1); } else { String out = result.stream().map(s -> s.toString()).collect(Collectors.joining(" ")); System.out.println(out); } } reader.close(); } static List<Integer> buildList(List<Integer> input) { if (input.size() <= 1) { return input; } List<Integer> result = new ArrayList<>(input.size()); result.add(input.get(0)); for (int i = 1; i < input.size(); i++) { int diff = input.get(i); int prev = result.get(i - 1); if (diff == 0 || diff > prev) { result.add(prev + diff); } else { return null; } } return result; } /* READING FROM STDIN */ private static Integer _readInt(BufferedReader reader) { try { String s = reader.readLine(); return Integer.parseInt(s); } catch (IOException e) { e.printStackTrace(); } return null; } private static BigInteger _readBigInteger(BufferedReader reader) { try { String s = reader.readLine(); return new BigInteger(s); } catch (IOException e) { e.printStackTrace(); } return null; } private static BigDecimal _readBigDecimal(BufferedReader reader) { try { String s = reader.readLine(); return new BigDecimal(s); } catch (IOException e) { e.printStackTrace(); } return null; } private static List<Integer> _readIntList(BufferedReader reader) { try { String s = reader.readLine(); return Arrays.stream(s.split(" ")) .map(Integer::parseInt) .collect(Collectors.toList()); } catch (IOException e) { e.printStackTrace(); } return null; } private static int[] _readIntArray(BufferedReader reader) { try { String s = reader.readLine(); String[] sts = s.split(" "); int[] result = new int[sts.length]; for (int i = 0; i < sts.length; i++) { result[i] = Integer.parseInt(sts[i]); } return result; } catch (IOException e) { e.printStackTrace(); } return null; } /* AGGREGATE FUNCTIONS */ private static <T> Map<T, Long> _getCount(Collection<T> values) { Map<T, Long> result = new HashMap<>(); for (T key : values) { if (result.containsKey(key)) { result.put(key, result.get(key) + 1L); } else { result.put(key, 1L); } } return result; } private static int _getMaxIndex(int[] array) { int max = 0; for (int i = 0; i < array.length; i++) { if (array[i] > array[max]) { max = i; } } return max; } /* PROCESSING */ static int[] _concat(int[] array1, int[] array2) { if (array1 == null || array1.length == 0) { return array2; } if (array2 == null || array2.length == 0) { return array1; } int[] result = Arrays.copyOf(array1, array1.length + array2.length); System.arraycopy(array2, 0, result, array1.length, array2.length); return result; } /* MATH */ private static double _logb(double x) { return Math.log(x) / Math.log(2.0); } private static long _lcd(long a, long b) { if (a < b) { long t = a; a = b; b = t; } long t = a % b; if (t == 0) { return b; } return _lcd(b, t); } /* WRITING TO STDOUT */ private static void _printArray(int[] array) { for (int i = 0; i < array.length - 1; i++) { System.out.print(array[i] + " "); } System.out.println(array[array.length - 1]); } private static void _printBoolean(boolean b) { if (b) { System.out.println("YES"); } else { System.out.println("NO"); } } }
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
bdc2b09db253287cd400b7ba84191368
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 Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int t; t = cin.nextInt(); while (t-- > 0) { int n = cin.nextInt(); int a[] = new int[n], ans[] = new int[n]; boolean ok = true; for (int i = 0; i < n; i++) a[i] = cin.nextInt(); ans[0] = a[0]; for (int i = 1; i < n && ok; i++) { int x = a[i] + ans[i - 1], y = ans[i - 1] - a[i]; if (x >= 0 && y >= 0 && x != y) ok = false; ans[i] = x >= 0 ? x : y; } if (ok) for (int i = 0; i < n; i++) System.out.print(ans[i] + " "); else System.out.print("-1"); System.out.println(); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
8d295d6dcd8b18622ff76d2c813e21f5
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Solve { static FastScanner fs = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int a = fs.nextInt(); while (a-- != 0) { solve(); } out.close(); } public static void solve() { int n = fs.nextInt(); int[] num = new int[n]; long[] newNum = new long[n]; long sum = 0; boolean bool = true; for (int i = 0; i < n; i++) { num[i] = fs.nextInt(); if (sum >= num[i] && num[i]!=0) bool = false; sum += num[i]; newNum[i] = sum; } if (!bool) { System.out.println(-1); return; } for (int i = 0; i < n; i++) { System.out.print(newNum[i] + " "); } System.out.println(); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
57e84cbcebbd2ab419bed3b5a21d2678
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 x=0;x<t;x++){ int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); int a[]=new int[n]; int b[]=new int[n]; a[0]=arr[0]; for(int i=1;i<n;i++) a[i]=a[i-1]+arr[i]; b[0]=arr[0];boolean k=true; for(int i=1;i<n;i++) { if(a[i-1]-arr[i]>=0) b[i]=a[i-1]-arr[i]; else b[i]=a[i]; if(a[i]!=b[i]){ k=false; break; } } if(k==false) System.out.println(-1); else { for(int i=0;i<n;i++) System.out.print(a[i]+" "); System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
084d6fb0e0c6937596ae8787a90338e0
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 B_Array_Recovery { static PrintWriter out = new PrintWriter((System.out)); static Reader sc; public static void main(String args[]) throws IOException { sc=new Reader(); int t=sc.nextInt(); while(t-->0) { solve(); } } private static void solve() { 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]; for(int i=1; i<n; i++){ int a = ans[i-1]+arr[i]; int b = ans[i-1] - arr[i]; if(b>=0 && a != b){ System.out.println(-1); return; } a = Math.max(a, b); ans[i] = a; } for(int i=0; i<n; i++){ System.out.print(ans[i]+" "); } System.out.println(); } static class Reader { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } public boolean hasNext() { String next = null; try { next = br.readLine(); } catch (Exception e) { } if (next == null) { return false; } st = new StringTokenizer(next); 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 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
b91a2bc7d9133273bc9d1189a1b8ad7f
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 codeforces { public static int gcd(int a,int b) { if(b>a) { if (b % a == 0) return a; int c = b % a; return gcd(c, a); } if (a % b == 0) return b; int c = a % b; return gcd(c, b); } public static void main(String[] args) { Scanner s = new Scanner(System.in); int t=s.nextInt(); for(int z=0;z<t;z++) { int n=s.nextInt(); int arr[] = new int[n]; int x=0; int sum =0; for(int i=0;i<n;i++) { arr[i]=s.nextInt(); if(arr[i] <= sum && arr[i] != 0) x=1; sum = sum + arr[i]; } if(x==1) System.out.println("-1"); else { int ans=0; for(int i=0;i<n;i++) { ans = ans + arr[i]; System.out.print(ans + " "); } 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
f16fedb78c5c9b58adc08afe51a2d94f
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_136B { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { int size = s.nextInt(); int[] arr = new int[size]; for (int i = 0; i < size; i++) arr[i] = s.nextInt(); solve(arr); System.out.println(); } } public static void solve(int[] arr) { int[] add = new int[arr.length]; int[] sub = new int[arr.length]; add[0] = arr[0]; sub[0] = arr[0]; for (int i = 1; i < arr.length; i++) { add[i] = add[i-1] + arr[i]; sub[i] = sub[i-1] - arr[i]; if (sub[i] < 0) sub[i] = add[i]; if (add[i] < 0) add[i] = sub[i]; if (add[i] != sub[i]) { System.out.print("-1"); return; } } for (int num : add) { System.out.print(num + " "); } } }
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
f4f21d001a1927a3acfdb93bb1b5d63d
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, InterruptedException { Scanner sc = new Scanner(new BufferedInputStream(System.in)); PrintWriter pw = new PrintWriter(System.out); 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 sum = a[0]; boolean f = true; for (int i = 1; i < n; i++) { if(d[i]!=0 && sum - d[i] >= 0) { f = false; break; } sum += d[i]; a[i] = sum; } if(!f) pw.print(-1); else { for (int x: a ) { pw.print(x + " "); } } pw.println(); } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } 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 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
83086a61a7df785a108561ba39b1c554
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 Restore { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int[] d = new int[n]; for (int j = 0; j < n; j++) { d[j] = in.nextInt(); } boolean two_or_more = false; for (int j = 1; j < n; j++) { if (d[j - 1] >= d[j] && d[j] != 0) { two_or_more = true; break; } else { d[j] += d[j - 1]; } } // Print output if (two_or_more) { System.out.println("-1"); } else { System.out.print(d[0]); for (int j = 1; j < n; j++) { System.out.print(" " + d[j]); } System.out.println(); } } return; } }
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
30f28a2fb88675cc0f2a27215282eccb
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 Testing { public static void main(String[] args) { Scanner s = new Scanner(System.in); int count = s.nextInt(); int[] d= null; int[] a = null; boolean mult = false; for(int run = 0; run < count; run++) { int size = s.nextInt(); d = new int[size]; a = new int[size]; mult = false; for(int i = 0; i < size; i++) { d[i] = s.nextInt(); } a[0] = d[0]; for(int i = 1; i < size; i++ ) { a[i] = d[i] + a[i - 1]; if(a[i-1] >= d[i] && d[i] != 0) { mult = true; break; } } if(mult) System.out.println(-1); else { for(int i : a) { System.out.print(i + " "); } 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
40ae305ea045ff730e861506fb1656e3
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.StringTokenizer; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.HashSet; import java.util.TreeSet; import java.util.HashMap; import java.util.Queue; import java.util.ArrayList; import java.util.List; import java.util.PriorityQueue; import java.util.Collections; import java.util.Queue; import java.util.LinkedList; import java.util.Arrays; import java.math.BigInteger; public class Main{ private static FastReader fs; public static void main(String[] args) throws Exception{ fs = new FastReader(); int test = 1; test =fs.nextInt(); while(test-- > 0) solve(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } int[] readArray(int n){ int ar[] = new int[n]; for(int i=0; i<n; i++) ar[i] = fs.nextInt(); return ar; } int gcd(int a, int b){ int result = Math.min(a, b); while (result > 0) { if (a % result == 0 && b % result == 0) { break; } result--; } return result; } } // use fs as scanner // snippets -> sieve, power, Node , lowerbound, upperbound, readarray public static void solve(){ int n = fs.nextInt(); int ar[] = fs.readArray(n); int ans[] = new int[n]; ans[0] = ar[0]; boolean flag = false; for(int i=1;i<n;i++){ int left = ans[i-1] - ar[i]; int right = ans[i-1] + ar[i]; if(left >= 0 && right >= 0 && left != right){ flag = true; break; } else{ ans[i] = Math.max(left,right); } } if(flag){ System.out.println("-1"); } else{ StringBuilder str = new StringBuilder(); for(int ele : ans) str.append(ele + " "); System.out.println(str); } } } /* do something when you are stuck and be calm */
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
338b20e8d8578e2ce82d64f8149420c6
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.math.*; import java.util.* ; import java.io.* ; @SuppressWarnings("unused") //Scanner s = new Scanner(new File("input.txt")); //s.close(); //PrintWriter writer = new PrintWriter("output.txt"); //writer.close(); public class cf { static long gcd(long a, long b){if (b == 0) {return a;}return gcd(b, a % b);} public static void main(String[] args)throws Exception { FastReader in = new FastReader() ; // FastIO in = new FastIO(System.in) ; StringBuilder op = new StringBuilder() ; int T = in.nextInt() ; // int T=1 ; for(int tt=0 ; tt<T ; tt++){ //CODE : int n = in.nextInt() ; int dif[] = in.readArray(n) ; int ans[] = new int[n] ; ans[0] = dif[0] ; boolean mul=false ; for(int i=1 ; i<n ; i++) { if(dif[i]!=0 && ans[i-1]-dif[i]>=0) { mul=true ; break ; } else { ans[i] = ans[i-1] + dif[i] ; } } if(mul) { op.append("-1\n") ; } else { for(int el : ans) { op.append(el+" "); }op.append("\n"); } } System.out.println(op.toString()) ; } static class pair implements Comparable<pair> { int first, second; pair(int first, int second) { this.first = first; this.second = second; } @Override public int compareTo(pair other) { return this.first - other.first; } @Override public boolean equals(Object obj) { pair other = (pair)obj ; return this.first == other.first && this.second == other.second ; } } 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 FastIO { InputStream dis; byte[] buffer = new byte[1 << 17]; int pointer = 0; public FastIO(String fileName) throws Exception { dis = new FileInputStream(fileName); } public FastIO(InputStream is) throws Exception { dis = is; } int nextInt() throws Exception { int ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } long nextLong() throws Exception { long ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } byte nextByte() throws Exception { if (pointer == buffer.length) { dis.read(buffer, 0, buffer.length); pointer = 0; } return buffer[pointer++]; } String next() throws Exception { StringBuffer ret = new StringBuffer(); byte b; do { b = nextByte(); } while (b <= ' '); while (b > ' ') { ret.appendCodePoint(b); b = nextByte(); } return ret.toString(); } int[] readArray(int n) throws Exception { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); 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[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
1c54b1d5bf93019df7bb129c2e0f96a1
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.math.*; import java.util.* ; import java.io.* ; @SuppressWarnings("unused") //Scanner s = new Scanner(new File("input.txt")); //s.close(); //PrintWriter writer = new PrintWriter("output.txt"); //writer.close(); public class cf { static long gcd(long a, long b){if (b == 0) {return a;}return gcd(b, a % b);} public static void main(String[] args)throws Exception { FastReader in = new FastReader() ; // FastIO in = new FastIO(System.in) ; StringBuilder op = new StringBuilder() ; int T = in.nextInt() ; // int T=1 ; for(int tt=0 ; tt<T ; tt++){ //CODE : int n = in.nextInt() ; int dif[] = in.readArray(n) ; int ans[] = new int[n] ; ans[0] = dif[0] ; boolean mul=false ; for(int i=1 ; i<n ; i++) { if(dif[i]!=0 && ans[i-1]-dif[i]>=0 && ans[i-1]+dif[i]!=dif[i]-ans[i-1]) { mul=true ; break ; } else { ans[i] = ans[i-1] + dif[i] ; } } if(mul) { op.append("-1\n") ; } else { for(int el : ans) { op.append(el+" "); }op.append("\n"); } } System.out.println(op.toString()) ; } static class pair implements Comparable<pair> { int first, second; pair(int first, int second) { this.first = first; this.second = second; } @Override public int compareTo(pair other) { return this.first - other.first; } @Override public boolean equals(Object obj) { pair other = (pair)obj ; return this.first == other.first && this.second == other.second ; } } 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 FastIO { InputStream dis; byte[] buffer = new byte[1 << 17]; int pointer = 0; public FastIO(String fileName) throws Exception { dis = new FileInputStream(fileName); } public FastIO(InputStream is) throws Exception { dis = is; } int nextInt() throws Exception { int ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } long nextLong() throws Exception { long ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } byte nextByte() throws Exception { if (pointer == buffer.length) { dis.read(buffer, 0, buffer.length); pointer = 0; } return buffer[pointer++]; } String next() throws Exception { StringBuffer ret = new StringBuffer(); byte b; do { b = nextByte(); } while (b <= ' '); while (b > ' ') { ret.appendCodePoint(b); b = nextByte(); } return ret.toString(); } int[] readArray(int n) throws Exception { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); 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[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
197635fa5e67eee308c6d951f9de885f
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class Main { static PrintWriter out; static int MOD = 1000000007; static FastReader scan; /*-------- I/O usaing short named function ---------*/ public static String ns() { return scan.next(); } public static int ni() { return scan.nextInt(); } public static long nl() { return scan.nextLong(); } public static double nd() { return scan.nextDouble(); } public static String nln() { return scan.nextLine(); } public static void p(Object o) { out.print(o); } public static void ps(Object o) { out.print(o + " "); } public static void pn(Object o) { out.println(o); } /*-------- for output of an array ---------------------*/ static void iPA(int arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void lPA(long arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void sPA(String arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void dPA(double arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } /*-------------- for input in an array ---------------------*/ static void iIA(int arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = ni(); } static void lIA(long arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = nl(); } static void sIA(String arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = ns(); } static void dIA(double arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = nd(); } /*------------ for taking input faster ----------------*/ 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; } } // Method to check if x is power of 2 static boolean isPowerOfTwo(int x) { return x != 0 && ((x & (x - 1)) == 0); } //Method to return lcm of two numbers static int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } //Method to count digit of a number static int countDigit(long n) { return (int) Math.floor(Math.log10(n) + 1); } //Method for sorting 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); } //Method for checking if a number is prime or not static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if ( n % i == 0 || n % (i + 2) == 0 ) return false; return true; } public static void reverse(int a[], int n) { int i, k, t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } // printing the reversed array System.out.println("Reversed array is: \n"); for (k = 0; k < n; k++) { System.out.println(a[k]); } } public static int binarysearch(int arr[], int left, int right, int num) { int idx = 0; while (left <= right) { int mid = (left + right) / 2; if (arr[mid] >= num) { idx = mid; // if(arr[mid]==num)break; right = mid - 1; } else { left = mid + 1; } } return idx; } public static void main(String[] args) throws java.lang.Exception { OutputStream outputStream = System.out; out = new PrintWriter(outputStream); scan = new FastReader(); int t = ni(); while (t-- > 0) { int n = ni(); int arr[]=new int[n]; int ans[]=new int[n]; iIA(arr); int prev=arr[0]; ans[0]=arr[0]; boolean chk=false; for(int i=1;i<n;i++){ if(arr[i]+prev>=0&&prev-arr[i]>=0&&(arr[i]+prev!=prev-arr[i])){ System.out.println(-1); chk=true; break; }else{ ans[i]=arr[i]+prev; } prev=ans[i]; } if(chk)continue; for(int i:ans){ System.out.print(i+" "); } System.out.println(); } out.flush(); 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
fc2d96a77e78420ec4ff09d156e3b9fb
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; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split(" "); StringBuilder sb = new StringBuilder(); int a = Integer.parseInt(s[0]); sb.append(a); for (int i = 1; i < n; i++) { int d = Integer.parseInt(s[i]); if (d > 0 && a >= d) { sb = new StringBuilder("-1"); break; } else { a += d; sb.append(" ").append(a); } } 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 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
070c4534e2b8f2eb8a6aa2d2bc038974
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.math.*; import static java.lang.Math.*; public class abc { 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()); String s[] = br.readLine().split("\\ "); List<Long> list= new ArrayList<>(); list.add(Long.parseLong(s[0])); boolean flag=true; for(int i=1;i<n;i++) { long z = Long.parseLong(s[i]); if(z!=0 && list.get(i-1)>=z) { flag = false; } list.add(z+list.get(i-1)); } // System.out.println(list); if(!flag) { System.out.println("-1"); } else { for(long z:list) { System.out.print(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 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
cbf949765dcc9f6e21d78563e6b8aa8f
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 den(int []arr,int i) { // for (int j = 0; j < arr.length-1; j++) { // if(j==arr.length-i) { // break; // } // int temp=0; // temp=arr[j]; // arr[j]=arr[j+1]; // arr[j+1]=temp; // } // } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); while(n>0) { int size=sc.nextInt(); int[] darr=new int[size]; for (int i = 0; i < darr.length; i++) { darr[i]=sc.nextInt(); } int[] aarr=new int[size]; aarr[0]=darr[0]; boolean broken=false; for (int i = 1; i < aarr.length; i++) { int s=darr[i]+aarr[i-1]; int ss=aarr[i-1]-darr[i]; if((s>=0&&ss>=0)&&s!=ss) { broken =true; break; } if(s>0)aarr[i]=s; else aarr[i]=ss; } if(broken)System.out.println("-1"); else { for (int i = 0; i < aarr.length; i++) { System.out.print(aarr[i]+" "); } System.out.println(); } n=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 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
e2eedc3fb3fcc8d37b57e1346431a809
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 Solution { public static void main(String args[]) { Scanner in=new Scanner(System.in); int t=in.nextInt(); testcase: while(t-- >0) { int n=in.nextInt(); int[] d=new int[n], a=new int[n]; for(int i=0; i<n; i++) { d[i]=in.nextInt(); } a[0]=d[0]; for(int i=1; i<n; i++) { if(d[i]>0 && a[i-1]-d[i]>=0) { System.out.println(-1); continue testcase; } a[i]=a[i-1]+d[i]; } for(int i: a) { System.out.print(i+" "); } System.out.println(); } in.close(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
8347e04d68b2613137d5735e9efb662a
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 { //d2 = a2-a1 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 result[] = new int[n]; result[0] = arr[0]; int flag = 0; for(int i=1; i<n; i++){ int x = result[i-1]-arr[i]; int y = arr[i] + result[i-1]; if(x>=0 && y>=0 && x!=y){ flag = 1; break; } result[i] = x>=y ? x : y; } if(flag == 1){ System.out.print(-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 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
37935575141379aeae28e153c9e3220a
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
//package kg.my_algorithms.Codeforces; import java.util.*; import java.io.*; public class Solution { private static final int[][] knightMoves = {{1,2},{-1,2},{1,-2},{-1,-2},{2,1},{-2,1},{2,-1},{-2,-1}}; private static final FastReader fr = new FastReader(); public static void main(String[] args) throws IOException { BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); StringBuilder sb = new StringBuilder(); int testCases = fr.nextInt(); for(int test=1;test<=testCases;test++){ int n = fr.nextInt(); int[] d = new int[n]; for(int i=0;i<n;i++) d[i] = fr.nextInt(); int[] res = solve(d); for(int r: res) sb.append(r).append(" "); sb.append("\n"); } output.write(sb.toString()); output.flush(); } private static int[] solve(int[] d){ int n = d.length; int[] arr = new int[n]; arr[0] = d[0]; for(int i=1;i<n;i++){ int prev = arr[i-1]; int f = prev-d[i]; int s = prev+d[i]; if(f>=0&&s>=0 && d[i]!=0) return new int[]{-1}; arr[i] = Math.max(f,s); } return arr; } } //Fast Input class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
34669a2e4bffaa1d45ad1c942bb3ccb3
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) { new Main().run(); } private void run() { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int count = 0; count < t; count++) { int n = sc.nextInt(); int[] a = new int[n]; a[0] = sc.nextInt(); boolean repeat = false; for (int i = 1; i < n; i++) { int d = sc.nextInt(); int a1 = d + a[i - 1]; int a2 = a[i - 1] - d; if (a1 != a2 && a1 >= 0 && a2 >= 0) { repeat = true; } a[i] = Math.max(a1, a2); } if (repeat) { System.out.println(-1); } else { for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
e5380deadc8f00ea699c1ffb668369d4
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.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; public class Main { public static void main(String[] args) throws Exception { Sol obj=new Sol(); obj.runner(); } } class Sol{ FastScanner fs=new FastScanner(); Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); void runner() throws Exception{ int T=1; //T=sc.nextInt(); //preCompute(); T=fs.nextInt(); while(T-->0) { solve(T); } out.close(); System.gc(); } private void solve(int T) throws Exception { int n=fs.nextInt(); int arr[]=new int[n]; for( int i=0;i<n;i++ ) arr[i]=fs.nextInt(); int ans[]=new int[n]; ans[0]=arr[0]; for( int i=1;i<n;i++ ) { int p1 = arr[i] + ans[i-1]; int p2= - arr[i] + ans[i-1]; if( p1 >=0 && p2>=0 && p1!= p2 ) { System.out.println(-1); return ; } ans[i]=Math.max(p1, p2); } for( int i=0;i<n;i++ ) { System.out.print(ans[i]+" "); } System.out.println(); } private void preCompute() { } static class FastScanner { BufferedReader br; StringTokenizer st ; FastScanner(){ br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } FastScanner(String file) { try { br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); st = new StringTokenizer(""); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("file not found"); e.printStackTrace(); } } 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()); } String readLine() throws IOException{ return br.readLine(); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
2122eda5a758ab11bc4c11cb091d9cf5
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.PrintStream; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintStream ps = new PrintStream(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int [] a = new int[n]; int [] b = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } boolean flag = true; b[0] = a[0]; for (int i = 1; i < n; i++) { if (a[i] != 0 && b[i - 1] - a[i] >= 0){ flag = false; break; } else b[i] = b[i - 1] + a[i]; } if (!flag) ps.println(-1); else{ for (int i = 0; i < n; i++) { ps.print(b[i] + " "); } ps.println(); } } ps.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
54af38bcb88dedbccd9f2fdd47404582
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 void main(String[] args) { FastScanner input = new FastScanner(); int tc = input.nextInt(); work: while (tc-- > 0) { int n = input.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = input.nextInt(); } ArrayList<Integer> ans = new ArrayList<>(); ans.add(a[0]); for (int i = 1; i <n; i++) { int one = ans.get(i-1)-a[i]; int two = ans.get(i-1)+a[i]; if(one!=two&&one>=0&&two>=0) { System.out.println("-1"); continue work; } ans.add(ans.get(i-1)+a[i]); } for (Integer an : ans) { System.out.print(an+" "); } System.out.println(""); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
777b2a7cb489f5e6ee732960b3ba94cd
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.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; public class B { static final long mod = (long) 1e9 + 7l; private static void solve(int t){ int n = fs.nextInt(); int arr[] = new int[n]; boolean zero = true; for(int i =0; i<n; i++){ arr[i]=fs.nextInt(); if(arr[i]>0)zero=false; } if(zero){ for (int i = 0; i < n; i++) { out.print(arr[i]+" "); } out.println(); return; } int res [] = new int[n]; res[0]=arr[0]; boolean flag = false; for (int i = 1; i < n; i++) { if(arr[i]==0){ res[i] = res[i-1]; continue; } int f = res[i-1]+arr[i]; int s = res[i-1]-arr[i]; if(f<=0 && s<=0){ flag = true; break; } if(f>=0 && s>=0){ flag = true; break; } res[i] = Math.max(f,s); } if(flag){ out.println(-1); return; } for (int i = 0; i < n; i++) { out.print(res[i]+" "); } out.println(); } private static int[] sortByCollections(int[] arr) { ArrayList<Integer> ls = new ArrayList<>(arr.length); for (int i = 0; i < arr.length; i++) { ls.add(arr[i]); } Collections.sort(ls); for (int i = 0; i < arr.length; i++) { arr[i] = ls.get(i); } return arr; } public static void main(String[] args) { fs = new FastScanner(); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); int t = fs.nextInt(); for (int i = 1; i <= t; i++) solve(t); out.close(); // System.err.println( System.currentTimeMillis() - s + "ms" ); } static boolean DEBUG = true; static PrintWriter out; static FastScanner fs; static void trace(Object... o) { if (!DEBUG) return; System.err.println(Arrays.deepToString(o)); } static void pl(Object o) { out.println(o); } static void p(Object o) { out.print(o); } static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } static void sieveOfEratosthenes(int n, int factors[]) { factors[1] = 1; for (int p = 2; p * p <= n; p++) { if (factors[p] == 0) { factors[p] = p; for (int i = p * p; i <= n; i += p) factors[i] = p; } } } static long mul(long a, long b) { return a * b % mod; } static long fact(int x) { long ans = 1; for (int i = 2; i <= x; i++) ans = mul(ans, i); return ans; } static long fastPow(long base, long exp) { if (exp == 0) return 1; long half = fastPow(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long modInv(long x) { return fastPow(x, mod - 2); } static long nCk(int n, int k) { return mul(fact(n), mul(modInv(fact(k)), modInv(fact(n - k)))); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) 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 _Scanner { InputStream is; _Scanner(InputStream is) { this.is = is; } byte[] bb = new byte[1 << 15]; int k, l; byte getc() throws IOException { if (k >= l) { k = 0; l = is.read(bb); if (l < 0) return -1; } return bb[k++]; } byte skip() throws IOException { byte b; while ((b = getc()) <= 32) ; return b; } int nextInt() throws IOException { int n = 0; for (byte b = skip(); b > 32; b = getc()) n = n * 10 + b - '0'; return 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
943b3ed9167e75ef413d40fc843e17ca
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(in.readLine()); while (t-- > 0) { int n = Integer.parseInt(in.readLine()); int[] arr = new int[n]; String[] s = in.readLine().split(" "); for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(s[i]); } int[] ogArray = new int[n]; ogArray[0] = arr[0]; boolean multiArray = false; for (int i = 1; i < n; i++) { ogArray[i] = ogArray[i - 1] + arr[i]; if (arr[i] != 0 && ogArray[i - 1] - arr[i] >= 0) { multiArray = true; } } if (multiArray) { out.write("-1\n"); out.flush(); continue; } for (int e : ogArray) { out.write(e+" "); } out.write("\n"); out.flush(); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
4ef5730688ad6d88375b29e8c6a6750f
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 contest2 { public static int sum(int[]arr,int idx){ int s=0; for(int h=0;h<=idx;h++){ s=s+arr[h]; } return s; } public static void main(String args[]){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); for(int i=0;i<n;i++){ int l=sc.nextInt(); int []arr=new int [l]; ArrayList<Integer>out=new ArrayList<>(); for(int j=0;j<l;j++){ arr[j]=sc.nextInt(); } out.add(arr[0]); if(l==1){ System.out.println(arr[0]); } else{ if(arr[0]<arr[1]||arr[1]==0){ out.add(arr[0]+arr[1]); } for(int g=2;g<l;g++){ if(arr[g]>sum(arr, g-1)||arr[g]==0){ out.add(sum(arr, g)); } else{ break; } } if(out.size()==l){ for(int b=0;b<out.size();b++){ System.out.print(out.get(b)+" "); } 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
17df6c3dc805513d876aa193d127f0ae
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 CF { 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) { String s=br.readLine(); int n=Integer.parseInt(s); String[] s1=br.readLine().split(" "); if(n==1){ System.out.println(s1[0]); continue; } int[] ds=new int[n]; for(int i=0;i<n;i++) { ds[i]=Integer.parseInt(s1[i]); } int[] arr=new int[n]; arr[0]=ds[0]; boolean manyPossible=false; for(int i=1;i<n;i++) { if(ds[i]!=0 && arr[i-1]-ds[i]>=0) { manyPossible=true; break; } arr[i]=arr[i-1]+ds[i]; } if(manyPossible) System.out.println(-1); else { for(int num:arr) { System.out.print(num+" "); } System.out.println(); } } } } // Aman and garden // Medium // There is a garden and Aman is the gardener and he wants to arrange trees in a row // of length n such that every Kth plant is non-decreasing in height. For example the plant // at position 1 should be smaller than or equal to the tree planted at position 1+K and plant // at position 1+K should be smaller than or equal to 1+2*K and so on, this // should be true for every position(for eg. 2 <= 2+K <= 2+2*K ...). // Now Aman can change a plant at any position with a plant of any height in order to create // the required arrangment. He wants to know minimum number of trees he will have to change to get the required arrangement. // We"ll be given a plants array which represents the height of plants at every position // and a number K and we need to output the minimum number of plants we will have to change to get the required arrangement. // Example: // plants = [5,3,4,1,6,5]; // K=2; // here plants at index (0,2,4) and (1,3,5) should be non decreasing. // plants[0]=5; // plants[2]=4; // plants[4]=6; // We can change the plant at index 2 with a plant of height 5(one change). // Similarly // plants[1]=3; // plants[3]=1; // plants[5]=5; // We can change the plant at index 3 with a plant of height 4(one change). // So minimum 2 changes are required. // Constraints // 1<=plants.length<=100000 // 1<=plants[i],K<=plants.length // Format // Input // First line contains an integer N representing length of the plant array. // Next line contains N space separated integers representing height of trees. // Last line contains an integer K // Output // Output the minimum number of changes required. // Example // Sample Input // 6 // 5 // 3 // 4 // 1 // 6 // 5 // 2 // Sample Output // 2 // Tokyo Drift // Easy // There is a racing competition which will be held in your city next weekend. There are N racers who are going to take part in the competition. Each racer is at a given distance from the finish line, which is given in an integer array. // Due to some safety reasons, total sum of speeds with which racers can drive is restricted with a given limit. Since, cost of organizing such an event depends on how long the event will last, you need to find out the minimum time in which all racers will cross the finishing line, but sum of speeds of all racers should be less than or equal to the given limit. // If it is impossible to complete the race for all racers within given limit, we have to return -1. // Note: Speed is defined as Ceil (distance/time), where ceil represents smaller than or equal to. For example if distance is 20 km and time taken to travel is 3 hrs then speed equals ceil(20/3) i.e. 7 km/hr. // Example: Let us take 4 Racers with Distances from finishing line as [15 km, 25 km, 5 km, 20 km], and the maximum sum of speeds allowed is 12 km/hr. The minimum time in which all racers will reach the finishing line will be 7 hours. // Racer 1 will have 15 km / 7 hr = 3 km/hr speed // Racer 2 will have 25 km / 7 hr = 4 km/hr speed // Racer 3 will have 05 km / 7 hr = 1 km/hr speed // Racer 4 will have 20 km / 7 hr = 3 km/hr speed // Hence, the total sum of speeds will be (3 + 4 + 1 + 3) = 11 km/hr which is less than or equal to 12 km/hr. // Constraints // 1 <= N <= 100000 // 1 <= racers[i] <= 10000000 // 1 <= Limit <= 10000000 // Format // Input // First line contains an integer N representing length of array. // Next line contains N space seprated integers representing distance from finishing line. // Last line contains an integer representing limit of sum of speeds with which racers can drive // Output // A single line integer representing minimum time in which all racers will cross the finishing line, If not possible print -1 // Example // Sample Input // 4 // 15 25 5 20 // 12 // Sample Output // 7
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
4ee318165b990ed8dd9a15795d42c4c2
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 static java.lang.Math.*; public class Solution { static MyScanner str = new MyScanner(); // static Scanner str = new Scanner(System.in); public static void main(String[] args) throws IOException { int T = i(); while (T-- > 0) { solve(); } } static void solve() throws IOException { int n = i(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = i(); } int[] d = new int[n]; int[] t = new int[n]; d[0] = a[0]; t[0] = a[0]; for (int i = 1; i < n; i++) { d[i] = (a[i] + d[i - 1]); if (a[i] > t[i - 1]) t[i] = abs(a[i] + t[i - 1]); else t[i] = abs(a[i] - t[i - 1]); } boolean ok = true; for (int i = 1; i < n; i++) { if (abs(t[i] - t[i - 1]) != a[i]) ok = false; } if (ok && !isSame(d, t)) { System.out.println(-1); return; } out_arr(d); } public static boolean isSame(int[] d, int[] t) { for (int i = 0; i < d.length; i++) { if(d[i] != t[i]) return false; } return true; } public static void out_arr(int[] a) { StringBuffer sf = new StringBuffer(); for (int i = 0; i < a.length; i++) { sf.append(a[i] + " "); } System.out.println(sf); } public static int i() throws IOException { return str.nextInt(); } public static long l() throws IOException { return str.nextLong(); } public static double d() throws IOException { return str.nextDouble(); } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static class ArraysInt { static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int[n1]; int R[] = new int[n2]; for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void sort(int arr[], int l, int r) { if (l < r) { int m = (l + r) / 2; sort(arr, l, m); sort(arr, m + 1, r); merge(arr, l, m, r); } } static void sort(int[] arr) { sort(arr, 0, arr.length - 1); } } private static class ArraysLong { static void merge(long arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; long L[] = new long[n1]; long R[] = new long[n2]; for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void sort(long arr[], int l, int r) { if (l < r) { int m = (l + r) / 2; sort(arr, l, m); sort(arr, m + 1, r); merge(arr, l, m, r); } } static void sort(long[] arr) { sort(arr, 0, arr.length - 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
9ce0157d6465da2fba40a70bfd289b16
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(in.readLine()); while (t-- > 0) { StringTokenizer st1 = new StringTokenizer(in.readLine()); StringTokenizer st2 = new StringTokenizer(in.readLine()); int n = Integer.parseInt(st1.nextToken()); int[] d = new int[n]; for (int i = 0; i < n; i++) { d[i] = Integer.parseInt(st2.nextToken()); } int[] ans = new int[n]; ans[0] = d[0]; boolean found = false; for (int i = 1; i < n; i++) { if (d[i] != 0 && ans[i-1] >= d[i]) { found = true; break; } else { ans[i] = ans[i-1] + d[i]; } } if (found) out.println(-1); else { out.print(ans[0]); for (int i = 1; i < n; i++) { out.print(" " + ans[i]); } out.println(); } // Queue<Integer> q = new LinkedList<>(); // q.add(d[0]); // for (int i=1; i<n; i++) { // int size = q.size(); // while (size-- > 0) { // int temp = q.poll(); // if (d[i] == 0) { // q.add(temp); // } else { // int temp2 = temp + d[i]; // int temp3 = temp - d[i]; // if (temp2 >= 0 && temp2 <= 100) // q.add(temp2); // if (temp3 >= 0 && temp3 <= 100) // q.add(temp3); // } // } // } // if (q.size() == 0) { // out.println("Something's wrong"); // } else if (q.size() > 1) { // out.println(-1); // } else { // ans[n-1] = q.poll(); // for (int i = n-2; i >= 0; i--) { // ans[i] = ans[i+1] - d[i+1]; // } // // // out.print(ans[0]); // for (int i = 1; i < n; i++) { // out.print(" " + ans[i]); // } // out.println(); // } } in.close(); 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
f6a8abe0fa49df505ac49db570c6c99a
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.math.*; import java.time.*; import static java.time.temporal.ChronoUnit.MINUTES; import javafx.util.Pair; public class Program { public static void print(Object str) { System.out.print(str); } public static void println(Object str) { System.out.println(str); } public static void printArr(int[] arr) { StringBuilder sb = new StringBuilder(""); for(int i=0;i<arr.length;i++) { sb.append(arr[i]+" "); } System.out.println(sb.toString()); } public static void printArr2(int[][] arr) { int n = arr.length; int m = arr[0].length; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(arr[i][j]+" "); } System.out.println(""); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } // code FastReader sc = new FastReader(); int t = sc.nextInt(); // int t = 1; for(int tt=0; tt<t; tt++) { int n = sc.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) { arr[i] = sc.nextInt(); } Object result = find(n, arr); // println(result); } return; } public static Object find(int n, int[] arr) { int[] a= new int[n]; a[0] = arr[0]; for(int i=1;i<n;i++) { if(arr[i]==0) { a[i] = a[i-1]; } else if(a[i-1]-arr[i]>=0 && a[i-1]+arr[i]>=0) { println(-1); return 0; } else { if(a[i-1]-arr[i]>=0) { a[i] = a[i-1]-arr[i]; } else { a[i] = a[i-1]+arr[i]; } } } printArr(a); return 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
93264bfeb44bde91253fed34441579e2
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.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { //nums Scanner scn = new Scanner(System.in); int test_cases = scn.nextInt(); for(int tc = 1; tc <= test_cases; tc++){ int n = scn.nextInt(); int[] arr = new int[n]; for(int i = 0; i < arr.length; i++){ arr[i] = scn.nextInt(); } int[]res = new int[n]; res[0] = arr[0]; boolean flag = true; for(int i = 1; i < arr.length; i++){ int op1 = res[i - 1] + arr[i]; int op2 = res[i - 1] - arr[i]; if(op1 != op2 && (op1 >= 0 && op2 >= 0)){ System.out.println(-1); flag = false; break; } res[i] = op1; } if(!flag){ continue; } for(int val: res){ System.out.print(val + " "); } } } }
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
1a8aa57ff70020100d4a1561defe5024
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 Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuffer output = new StringBuffer(); int T = sc.nextInt(); while(T-- > 0) { int n = sc.nextInt(); int[] d = new int[n + 1]; int[] a = new int[n + 1]; for(int i = 1; i <= n; i++) { d[i] = sc.nextInt(); } a[1] = d[1]; boolean multiple = false; for(int i = 2; i <= n; i++) { int cnt = 0; for(int x = 0; x <= 1000; x++) { if(Math.abs(x - a[i - 1]) == d[i]) { a[i] = x; cnt ++; } } if(cnt > 1) { multiple = true; break; } } if(multiple) { output.append(-1).append('\n'); } else { for(int i = 1; i <= n; i++) { output.append(a[i]).append(' '); } output.append('\n'); } } output = output.deleteCharAt(output.length() - 1); System.out.println(output); } }
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
47254f9610c766a38946fe5c83e9fff0
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 arrayRecovery1100 { public static void main(String[] args) throws Exception { 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(); //1 0 2 5 //a1 = d1 = 1 //di = |ai - ai-1| => di = ai - ai-1 and di = -(ai - ai-1) => di = ai-1 - ai //ai = ai-1 - di //ai = di+ai-1 //a2 = 0 + (1) = 1 //a3 = 2 + (1) = 3 //a4 = 5 + 3 = 8 //a[] = [1,1,3,8] //and with other formula too -> ai = ai-1 - di //a1 = d1 = 1 //a2 = a1 - d2 = 1 - 0 = 1 (same as with other formula) //a3 = a2 - d3 = 1 - 2 = -1 //a4 = a3 - d4 = -1 - 5 = -6 //a[] = [1,1,-1,-6] -> but this can't be an answer since the question says the ans should be having +ve integers only //so simply calculate each ai using both above equations and if at any point +ve ai comes at any index from both the equations //then there will be two answers for sure //ai = ai-1+di will not give -ve ever since di is having +ve and a1 will be +ve too then since a1=d1 //but if d[i] == 0 then we will not check as then ans will be same from both eq's ie +ve ....so we will not check then as it will not //give correct ans int prevEq1 = d[0];//a1 = d1 String ans = ""; StringBuilder sb = new StringBuilder(); sb.append(d[0]+" "); for(int i=1;i<n;i++) { int fromPositiveEq = prevEq1 + d[i]; int fromEq2 = prevEq1 - d[i]; //will check using the previous from equation 1 always ...as eq 2 on 2nd consecutive will always give //-ve no matter what...eg -> [2,6,3] so a1=2 , a2 = 2+6 = 8 (eq1) and a2 = 2-6=-4 (eq 2) ...now we will leave it ..kyunki aage agar //kuch bhi karenge -4 - (something) then that will give me -ve only..so for a3 too we will use a2 of eq 1 and then apply noth eq //on it // System.out.println(fromEq2); if(d[i]!=0 && fromEq2 >= 0) //then multiple answers will be there { ans = "-1"; break; } sb.append(fromPositiveEq+" "); prevEq1 = fromPositiveEq; } if(ans.length()!=0) { System.out.println(ans); } else { System.out.println(sb.substring(0,sb.length()-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 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
0ebff75f7c638507dadacce249b9e1b7
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 B1739 { 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]; boolean flag = true; for(int i=0; i<n; i++){ d[i] = sc.nextInt(); } for(int i=0; i<n; i++){ if(i==0) a[0] = d[0]; else{ if((a[i-1] >= d[i]) && d[i]!=0){ flag = false; break; } else a[i] = a[i-1] +d[i]; } } if(flag){ for(int i=0; i<a.length; i++){ System.out.print(a[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 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
7207c0591bfc90f5e0c1e7379cf9ca34
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static class Reader { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public Reader() { this(System.in); } public Reader(InputStream input) { br = new BufferedReader(new InputStreamReader(input)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextl() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } int[] arrin(long num) { int n = (int) num; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] arrnl(long num) { int n = (int) num; long[] l = new long[n]; for (int i = 0; i < n; i++) l[i] = nl(); return l; } } //<------------------------------------------------WRITER----------------------------> static class Writer { static PrintWriter out; public Writer() { this(System.out); } public Writer(OutputStream outs) { out = new PrintWriter(outs); } public void pl(int i) { out.println(i); } public void pl(long l) { out.println(l); } public void pl(double d) { out.println(d); } public void pl(String s) { out.println(s); } public void p(int i) { out.print(i); } public void p(long l) { out.print(l); } public void p(double d) { out.print(d); } public void p(String s) { out.print(s); } public void p() { out.println(); } public void close() { out.close(); } } //-----------------------------------------------------------------------------------> //--------------------------VARIABLES------------------------------------// static Reader in = new Reader(); static OutputStream outputStream = System.out; static Writer out = new Writer(outputStream); static long lmax = Long.MAX_VALUE, lmin = Long.MIN_VALUE; static int imax = Integer.MAX_VALUE, imin = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------Red_Hair-----------------------------------// private static void Red_Hair() throws IOException { String FILE = "RED"; try { FILE = System.getProperty("user.dir"); } catch (Exception e) { } if(new File(FILE).getName().equals("CP")) { out = new Writer(new FileOutputStream("output.txt")); in = new Reader(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// public static void main(String[] args) throws IOException { Red_Hair(); int t = in.ni(); while (t-- > 0) solve(); out.close(); } static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } static long lcm(long a, long b) { return a * b / gcd(a, b); } static void solve() throws IOException { int n = in.ni(); int[] arr= in.arrin(n); //ajjfkahf//ajjfkahf//ajjfkahf int[] ans1 = new int[n]; int fl = 1; ans1[0] = arr[0]; for (int i = 1; i < n; i++) { //oijfoipaujfoiahfo if (ans1[i - 1] + arr[i] >= 0 && ans1[i - 1] - arr[i] >= 0 && ans1[i - 1] + arr[i] != ans1[i - 1] - arr[i]) { //ajjfkahf//ajjfkahf//ajjfkahf //ajjfkahf//ajjfkahf //ajjfkahf//ajjfkahf//ajjfkahf out.pl(-1); //ajjfkahf fl = 0; //ajjfkahf break; } else if (ans1[i - 1] + arr[i] >= 0) { //ajjfkahf//ajjfkahf//ajjfkahf ans1[i] = ans1[i - 1] + arr[i]; } else { //ajjfkahf//ajjfkahf ans1[i] = ans1[i - 1] - arr[i]; } } if (fl==1) { //ajjfkahf//ajjfkahf//ajjfkahf for (int i = 0; i < n; i++) { out.p(ans1[i] + " "); } //ajjfkahf//ajjfkahf//ajjfkahf out.pl(""); } } static void swap(long X[],int i,int j) { long x=X[i]; X[i]=X[j]; X[j]=x; } static class pr <T extends Comparable<T>, V extends Comparable<V>> implements Comparable<pr<T, V>> { T a; V b; public pr(T a, V b) { this.a = a; this.b = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof pr)) return false; pr<?, ?> pr = (pr<?, ?>) o; return a.equals(pr.a) && b.equals(pr.b); } @Override public int hashCode() { return Objects.hash(a, b); } @Override public int compareTo(pr o) { return this.a.compareTo(((T) o.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
2786fe4785a6acd9c51fc2073677ebc9
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); ///input int t = sc.nextInt(); int[][] testcases = new int[t][]; int index = 0, length = t; while (length > 0) { int n = sc.nextInt(); int[] d = new int[n]; for (int i = 0; i < n; i++) { d[i] = sc.nextInt(); } testcases[index++] = d; length--; } ///Output arrayRecovery(testcases, t); } private static void arrayRecovery(int[][] testcases, int t) { for (int i = 0; i < t; i++) { int[] d = testcases[i]; int[] a = new int[d.length]; a[0] = d[0]; boolean flag = true; for (int j = 1; j < d.length; j++) { int temp1 = a[j - 1] + d[j]; int temp2 = a[j - 1] - d[j]; if (temp1 >= 0 && temp2 >= 0 && temp1 != temp2) { System.out.println("-1"); flag = false; break; } a[j] = Math.max(temp1, temp2); } if (flag) { for (int k = 0; k < a.length; k++) { System.out.print(a[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 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
23f29a2056601c1f04811bdb53d0e0f6
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.InputStreamReader; import java.util.*; public class Main { static Scanner cin = new Scanner(new InputStreamReader(System.in)); public static void main(String[] args) { int t = cin.nextInt(); for (int i=1;i<=t;i++) { solve(); } } private static void solve() { int n = cin.nextInt(); int[] d = new int[n]; for(int i=0;i<n;i++) { d[i] = cin.nextInt(); } for(int i=1;i<n;i++) { if(d[i] > 0 && d[i-1] - d[i]>=0) { System.out.println(-1); return; } d[i] = d[i-1] + d[i]; } for(int i=0;i<n;i++) { System.out.print(d[i]); if(i+1<n) { System.out.print(" "); } } 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
41c78fb36b2c72f8099cdf58623556cf
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.rajat.cp.codeforces.edu136; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BArrayRecovery { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().trim()); x: while (t-- > 0) { int n = Integer.parseInt(br.readLine().trim()); int[] ans = new int[n]; int[] d = new int[n]; String[] input = br.readLine().split(" "); ans[0] = Integer.parseInt(input[0]); for (int i = 1; i < n; i++) { d[i] = Integer.parseInt(input[i]); if(d[i] != 0 && ans[i-1] - d[i] >= 0) { System.out.println("-1"); continue x; } ans[i] = d[i] + ans[i-1]; } for(int i = 0; i < n; 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 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
5ff6bcf5779c410ba6c26a4d5a0cbc5b
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 // Use this editor to write, compile and run your Java code online import java.util.*; public class HelloWorld { public static int gcd(int Num1, int Num2){ int Temp, GCD=0; while(Num2 != 0) { Temp = Num2; Num2 = Num1 % Num2; Num1 = Temp; } GCD = Num1; return GCD; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int p = sc.nextInt(); while(p>0){ p--; int n = sc.nextInt(); int[] d = new int[n]; for(int i =0;i<n;i++){ d[i] = sc.nextInt(); } int condi=1; int[] a = new int[n+1]; a[0] = d[0]; int temp = a[0]; for(int i=1;i<n;i++){ if( d[i] != 0 && temp - d[i] >= 0){ condi=0; } temp = temp+d[i]; } if(condi==0){ System.out.println(-1); }else{ for(int i=1;i<n;i++){ a[i] = a[i-1] +d[i]; } for(int i =0;i<n;i++){ System.out.print(a[i] +" "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
8f8064c5e89bb5d6ae98a5fb9e7bbb08
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); for (int i = 0; i < t; i++) { int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int[] a = new int[n]; boolean possible = true; for (int j = 0; j < n; j++) { int num = Integer.parseInt(st.nextToken()); int neg = -1 * num; if(j==0) { a[0]=num; } else if(a[j-1]+num>=0 && a[j-1]+neg>=0 && num!=0) { possible=false; break; } else { a[j]=a[j-1]+num; } } if(possible) { for (int j = 0; j < a.length-1; j++) { pw.print(a[j]+" "); } pw.println(a[a.length-1]); } else { pw.println(-1); } } pw.close(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
75ad07f570d8e017ccf417eba7357f86
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 { private static void solve(int n, int[] arr){ int[] res = new int[n]; res[0] = arr[0]; for(int i = 1; i < arr.length; i++){ if(res[i-1] + arr[i] >= 0 && res[i-1] - arr[i]>= 0 && arr[i] != 0){ out.println(-1); return; } if(res[i-1] + arr[i] >= 0){ res[i] = res[i-1] + arr[i]; } else if(res[i-1] - arr[i]>= 0){ res[i] = res[i-1] - arr[i]; } } print1DArray(res); } public static void main(String[] args){ MyScanner scanner = new MyScanner(); int testCount = scanner.nextInt(); for(int testIdx = 1; testIdx <= testCount; testIdx++){ int n = scanner.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = scanner.nextInt(); } solve(n, arr); } out.close(); } static void print1DArray(int[] arr){ for(int i = 0; i < arr.length; i++){ out.print(arr[i]); if(i < arr.length - 1){ out.print(' '); } } out.print('\n'); } static void print1DArrayList(List<Integer> arrayList){ for(int i = 0; i < arrayList.size(); i++){ out.print(arrayList.get(i)); if(i < arrayList.size() - 1){ out.print(' '); } } out.print('\n'); } public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["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
b05d79ed7c03c5cd401c4a7b7d142b1a
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.*; public class Recovery{ public static void main(String[] args) { Scanner o = new Scanner(System.in); int t = o.nextInt(); while(t-- > 0){ int n = o.nextInt(); int[] a = new int[n]; a[0] = o.nextInt(); boolean more = false; for(int i = 1; i < n; i ++){ int elt = o.nextInt(); if(a[i - 1] < elt || elt == 0){ a[i] = elt + a[i - 1]; } else{ more = true; } } if(more) System.out.println(-1); else{ for(int i : a) System.out.print(i + " "); System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
9a5247446590db6b28de8d1c084db1cf
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.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeMap; public class B { public static void main(String[]args) throws IOException { Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int t=sc.nextInt(); a:while(t-->0) { int n=sc.nextInt(); int[]a=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); int[]ans=new int[n]; ans[0]=a[0]; for(int i=1;i<n;i++) { if(a[i]==0) { ans[i]=ans[i-1];continue; } if(a[i]<=ans[i-1]) { out.println(-1);continue a; } ans[i]=a[i]+ans[i-1]; } for(int x:ans)out.print(x+" "); out.println(); } out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public boolean hasNext() {return st.hasMoreTokens();} 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 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
c1bdb77b54db948f3c0638a9ce1e6e8c
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA import java.util.*; import java.util.Map.Entry; import java.util.stream.*; import java.lang.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.io.*; public class arrayrecovery { static private final String INPUT = "input.txt"; static private final String OUTPUT = "output.txt"; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.00000"); final static int mod = (int) (1e9 + 7); final static int MAX = Integer.MAX_VALUE; final static int MIN = Integer.MIN_VALUE; final static long INF = Long.MAX_VALUE; final static long NEG_INF = Long.MIN_VALUE; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; // ==== start ==== input(); preprocess(); int t = 1; t = readInt(); while (t-- > 0) { solve(); } out.flush(); // ==== end ==== if (!oj) System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } private static void solve() throws IOException { int n=readInt(); int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=readInt(); } int[] ans=new int[n]; ans[0]=arr[0]; StringBuilder sb=new StringBuilder(); sb.append(ans[0]); sb.append(" "); for(int i=1;i<n;i++){ ans[i]=arr[i]+ans[i-1]; if(arr[i]!=0 && ans[i-1]-arr[i]>=0){ System.out.println("-1"); return; } sb.append(ans[i]); sb.append(" "); } System.out.println(sb); } private static void preprocess() throws IOException { } // cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces // javac CodeForces.java // java CodeForces // javac CodeForces.java && java CodeForces // ==================== CUSTOM CLASSES ================================ static class Pair { int first, second; Pair(int f, int s) { first = f; second = s; } public int compareTo(Pair o) { if (this.first == o.first) return this.second - o.second; return this.first - o.first; } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (this.getClass() != obj.getClass()) return false; Pair other = (Pair) (obj); if (this.first != other.first) return false; if (this.second != other.second) return false; return true; } @Override public int hashCode() { return this.first ^ this.second; } @Override public String toString() { return this.first + " " + this.second; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } // ======================= FOR INPUT ================================== private static void input() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { System.err.println("Error Occurred."); } br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(readLine()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return br.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray(int n) throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(m); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } private static void yes() { out.println("YES"); } private static void no() { out.println("NO"); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== 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 lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static int mod_pow(long a, long b, int mod) { if (b == 0) return 1; int temp = mod_pow(a, b >> 1, mod); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static int multiply(int a, int b) { return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod); } private static int divide(int a, int b) { return multiply(a, mod_pow(b, mod - 2, mod)); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } private static List<Integer> factors(int n) { List<Integer> list = new ArrayList<>(); for (int i = 1; 1L * i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } private static List<Long> factors(long n) { List<Long> list = new ArrayList<>(); for (long i = 1; i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } // ==================== Primes using Seive ===================== private static List<Integer> getPrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; 1L * i * i <= n; i++) if (prime[i]) for (int j = i * i; j <= n; j += i) prime[j] = false; // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime(int n) { int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; 1L * i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) if (primes[j] == j) primes[j] = i; } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } // check if a is subsequence of b private static boolean isSubsequence(String a, String b) { int idx = 0; for (int i = 0; i < b.length() && idx < a.length(); i++) if (a.charAt(idx) == b.charAt(i)) idx++; return idx == a.length(); } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // =============== Lower Bound & Upper Bound =========== // less than or equal private static int lower_bound(List<Integer> list, int val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(List<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(List<Long> list, long val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return false; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return true; } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } void insert(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; } curr.isEnd = true; } boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ================== SEGMENT TREE (RANGE SUM & RANGE UPDATE) ================== public static class SegmentTree { int n; long[] arr, tree, lazy; SegmentTree(long arr[]) { this.arr = arr; this.n = arr.length; this.tree = new long[(n << 2)]; this.lazy = new long[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, long val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, long val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } long query(int l, int r) { return query(1, 0, n - 1, l, r); } long query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0L; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== FENWICK TREE ================================ static class FT { int n; int[] arr; int[] tree; FT(int[] arr, int n) { this.arr = arr; this.n = n; this.tree = new int[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } FT(int n) { this.n = n; this.tree = new int[n + 1]; } // 1 based indexing void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } // 1 based indexing long query(int l, int r) { return getSum(r) - getSum(l - 1); } long getSum(int idx) { long ans = 0L; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } 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 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
bd0d9a7048d872243365639383a4d672
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); int t = in.nextInt(); for(int ii = 0; ii < t; ii++) { int n = in.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = in.nextInt(); } int[] b = new int[n]; b[0] = a[0]; boolean bb = true; for(int i = 1 ; i < n; i++) { if(a[i] <= b[i - 1] && a[i] != 0) { System.out.println(-1); bb = false; break; } else { b[i] = a[i] + b[i - 1]; } } if(bb) { for(int i = 0; i < n; i++) { System.out.print(b[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
e858aaaa1773494d2354269413b9156a
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.*; public class Solution { public static void main(String args[]){ FastScanner sc = new FastScanner(); 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 found = true; for(int i=1; i<n; i++){ if(d[i] == 0){ arr[i] = arr[i-1]; } else if(arr[i-1] - d[i] >= 0){ found = false; break; } else{ arr[i] = arr[i-1] + d[i]; } } if(found){ for(int i=0; i<n; i++){ System.out.print(arr[i] + " "); } System.out.println(); } else{ System.out.println(-1); } } } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
a170e1976339794c097c285f3fce707e
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 EF136 { static Scanner sc = new Scanner(System.in); public static void solve1() { int m = sc.nextInt(); int n = sc.nextInt(); if (m < 4 && n < 4) { m = m / 2 + 1; n = n / 2 + 1; } else { m = n = 1; } System.out.println(m + " " + n); } public static void solve2() { int n = sc.nextInt(); int[] a = new int[n]; int[] d = new int[n]; int sum = 0; boolean flag = false; for (int i = 0; i < n; i++) { d[i] = sc.nextInt(); sum += d[i]; a[i] = sum; } for (int i = 1; i < n; i++) { if (a[i - 1] >= d[i] && (a[i - 1] - d[i] != a[i - 1] + d[i])) { flag = true; break; } } if (flag) System.out.println(-1); else { for (int i = 0; i < n; i++) System.out.print(a[i] + " "); System.out.println(); } } public static void solve3() { System.out.println(); } public static void solve4() { System.out.println(); } public static void solve5() { System.out.println(); } public static void solve6() { System.out.println(); } public static void main(String[] args) { int t = sc.nextInt(); int testcase = 1; while (testcase <= t) { solve2(); testcase++; } 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 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
e92a8cf7ee5486395eb784180c225d63
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 HelloWorld { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-->0){ int n = scan.nextInt(); int[] d = new int[n]; for(int i=0; i<n; i++){ d[i] = scan.nextInt(); } int[] a = new int[n]; a[0] = d[0]; boolean flag = true; 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 = false; break; }else{ a[i] = a[i-1] + d[i]; } } } if(!flag){ System.out.println(-1); }else{ for(int i=0; i<n; i++){ System.out.print(a[i] + " "); } System.out.println(); } } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
b4421b446e6fb7588789cd48085e9124
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
// Java program to find length of the longest valid // substring import java.util.*; public class A1739 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int tt=0;tt<t;tt++) { int n = sc.nextInt(); int d[] = new int[n]; long sum = 0; boolean flag = true; for(int i=0;i<n;i++) { d[i] = sc.nextInt(); if(sum>=d[i]&&d[i]!=0) { // System.out.println(sum); flag = false; } sum += d[i]; } if(flag) { sum = 0; for(int i=0;i<n;i++) { sum += d[i]; System.out.print(sum+" "); } 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
b99b942494ca417015f28cb927ac84b9
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.util.List; public class Main { static PrintWriter out = new PrintWriter(System.out); static Scanner in = new Scanner(System.in); static BufferedReader re = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out)); //String[] strs = re.readLine().split(" "); int a = Integer.parseInt(strs[0]); public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); //String[] strs = re.readLine().split(" "); //int T=Integer.parseInt(strs[0]); int T=in.nextInt(); //int T=1; while(T>0){ //String[] strs1 = re.readLine().split(" "); //int n=Integer.parseInt(strs1[0]); //String s=re.readLine(); //char arr[]=s.toCharArray(); //Set<Integer>set=new HashSet<>(); Map<Integer,Integer>map=new HashMap<>(); //Map<List<Integer>,Integer>map=new HashMap<>(); //TreeSet<Integer> set = new TreeSet<>(); int n=in.nextInt(); int arr[]=new int [n]; for(int i=0;i<n;i++){ arr[i]=in.nextInt(); } int res[]=new int [n]; res[0]=arr[0];int t=0; for(int i=1;i<n;i++){ res[i]=res[i-1]+arr[i]; int x=res[i-1]-arr[i]; if(x>=0&&x!=res[i])t++; } if(t>0)out.print("-1"); else{ for(int i=0;i<n;i++)out.print(res[i]+" "); } out.println(); T--; } out.flush(); } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
6cb9001172867ce600ba6b7b560bb4d0
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.*; // Map.Entry<Integer,String>dum; //res=(num1>num2) ? (num1+num2):(num1-num2) /* Name of the class has to be "Main" only if the class is public. */ public class Pupil { static FastReader sc = new FastReader(); public static void main (String[] args) throws java.lang.Exception { // your code goes here int t=sc.nextInt(); while(t>0){ Solve(); t--; } } public static void Solve() { int n=sc.nextInt(); long arr[]=new long[n]; al(arr,n); long ans[]=new long[n]; ans[0]=arr[0]; for(int i=1;i<n;i++) { long val1=arr[i]+ans[i-1]; long val2=(arr[i]*(-1L))+ans[i-1]; // System.out.println(val1+" "+val2); if((val1>0 && val2<0) ||(val1==val2)) { ans[i]=val1; } else { System.out.println("-1"); return ; } } for(int i=0;i<n;i++) { System.out.print(ans[i]+" "); } System.out.println(); return; } // FAST I/O 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 boolean two(int n)//power of two { if((n&(n-1))==0) { return true; } else{ return false; } } public static int factorial(int n) { int res = 1, i; for (i = 2; i <= n; i++) res *= i; return res; } public static boolean isPrime(long n){ for (int i = 2; i * i <= n; ++i) { if (n % i == 0) { return false; } } return true; } public static int digit(int n) { int n1=(int)Math.floor((int)Math.log10(n)) + 1; return n1; } public static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } public static long highestPowerof2(long x) { // check for the set bits x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; // Then we remove all but the top bit by xor'ing the // string of 1's with that string of 1's shifted one to // the left, and we end up with just the one top bit // followed by 0's. return x ^ (x >> 1); } public static void yes() { System.out.println("YES"); return; } public static void no() { System.out.println("NO"); return ; } public static void al(long arr[],int n) { for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); } return ; } public static void iswap(int[]arr,int i,int j){ arr[i]^=arr[j]; arr[j]^=arr[i]; arr[i]^=arr[j]; } public static void lswap(long[]arr,int i,int j){ long temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } public static void ai(int arr[],int n) { for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } return ; } public static void makeSet(int parent[],int rank[]){ for(int i=0;i<100;i++) { parent[i]=i; rank[i]=0; } } public static int findParent(int node,int parent[]) { if(node==parent[node]) { return parent[node]; } return parent[node]=findParent(parent[node], parent); } public static void union(int u,int v,int parent[],int rank[]) { u=findParent(u,parent); v=findParent(v,parent); if(rank[u]<rank[v]) { parent[u]=v; } else if(rank[u]>rank[v]) { parent[v]=u; } else{ parent[v]=u; rank[u]++; } } } //HashMap<Integer, Integer> map = new HashMap<>(); //for (int i = 0; i < n; i++) { // int x = in.nextInt(); // int count = map.get(x) == null ? 0 : map.get(x); // map.put(x, count + 1); //} //CAAL THE BELOW FUNCTION IF PARING PRIORITY IS NEEDED // // PriorityQueue<pair> pq = new PriorityQueue<>(); **********declare the syntax in the main function****** // pq.add(1,2)/////// // class pair implements Comparable<pair> { // int value, index; // pair(int v, int i) { index = i; value = v; } // @Override // public int compareTo(pair o) { return o.value - value; } // } //o.value-value DESCENDING ORDER //value-o.value ASCENDING ORDER // User defined Pair class // class Pair { // int x; // int y; // // Constructor // public Pair(int x, int y) // { // this.x = x; // this.y = y; // } // } // Arrays.sort(arr, new Comparator<Pair>() { // @Override public int compare(Pair p1, Pair p2) // { // return p1.x - p2.x; // } // }); // class Pair { // int height, id; // // public Pair(int i, int s) { // this.height = s; // this.id = i; // } // } //Arrays.sort(trips, (a, b) -> Integer.compare(a[1], b[1])); // ArrayList<ArrayList<Integer>> connections = new ArrayList<ArrayList<Integer>>(); // for(int i = 0; i<n;i++) // connections.add(new ArrayList<Integer>()); //WHENEVER CIRCULAR ARRAY COMES ALWAYS CREATE A NEW ARRAY OF LENGTH (2*LENGTH OF ORIGINAL ARRAY) //Integer.bitCount(i) counts number of times 1 appear in binary string of 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 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
579eaab0a109d1c87e05506d8938ccfe
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.math.*; public class CF1 { static FastReader sc=new FastReader(); // static long dp[][]; // static boolean v[][][]; // static int mod=998244353;; // static int mod=1000000007; static long oset[]; static int oset_p; static long mod=1000000007; // static int max; // static int bit[]; //static long fact[]; //static HashMap<Long,Long> mp; //static StringBuffer sb=new StringBuffer(""); //static HashMap<Integer,Integer> map; //static List<Integer>list; //static int m; //static StringBuffer sb=new StringBuffer(); // static PriorityQueue<Integer>heap; //static int dp[]; // static boolean cycle; static PrintWriter out=new PrintWriter(System.out); // static int msg[]; public static void main(String[] args) { // StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); // Queue<int[]>q=new LinkedList<>(); outer :while (ttt-- > 0) { long n=l(); long d[]=inputL((int)n); long ans[]=new long[(int)n]; ans[0]=d[0]; for(int i=1;i<n;i++) { if((ans[i-1]-d[i])>=0&&d[i]!=0) { pln("-1"); continue outer; } ans[i]+=(ans[i-1]+d[i]); } for(long it:ans) p(it+" "); pln(""); } out.close(); } static int findPar(int x,int parent[]) { if(parent[x]==x) return x; return parent[x]=findPar(parent[x],parent); } static void union(int u,int v,int parent[],int rank[]) { int x=findPar(u,parent); int y=findPar(v,parent); if(x==y) return; if(rank[x]>rank[y]) { parent[y]=x; } else if(rank[y]>rank[x]) parent[x]=y; else { parent[y]=x; rank[x]++; } } static class Pair implements Comparable<Pair> { int x; int y; // int z; Pair(int x,int y){ this.x=x; this.y=y; // this.z=z; } @Override public int compareTo(Pair o) { if(this.x>o.x) return 1; else if(this.x<o.x) return -1; else { if(this.y>o.y) return 1; else if(this.y<o.y) return -1; else return 0; } } public int hashCode() { final int temp = 14; int ans = 1; ans =x*31+y*13; return ans; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null) { return false; } if (this.getClass() != o.getClass()) { return false; } Pair other = (Pair)o; if (this.x != other.x || this.y!=other.y) { return false; } return true; } // /* FOR TREE MAP PAIR USE */ // public int compareTo(Pair o) { // if (x > o.x) { // return 1; // } // if (x < o.x) { // return -1; // } // if (y > o.y) { // return 1; // } // if (y < o.y) { // return -1; // } // return 0; // } } static ArrayList<Long> gLL() { return new ArrayList<>(); } static ArrayList<Integer> gL() { return new ArrayList<>(); } static StringBuffer gsb() { return new StringBuffer(); } static int find(int A[],int a) { if(A[a]==a) return a; return A[a]=find(A, A[a]); } //static int find(int A[],int a) { // if(A[a]==a) // return a; // return find(A, A[a]); //} //FENWICK TREE static class BIT{ int bit[]; BIT(int n){ bit=new int[n+1]; } int lowbit(int i){ return i&(-i); } int query(int i){ int res=0; while(i>0){ res+=bit[i]; i-=lowbit(i); } return res; } void update(int i,int val){ while(i<bit.length){ bit[i]+=val; i+=lowbit(i); } } } //END static long summation(long A[],int si,int ei) { long ans=0; for(int i=si;i<=ei;i++) ans+=A[i]; return ans; } static void add(long v,Map<Long,Long>mp) { if(!mp.containsKey(v)) { mp.put(v, (long)1); } else { mp.put(v, mp.get(v)+(long)1); } } static void remove(long v,Map<Long,Long>mp) { if(mp.containsKey(v)) { mp.put(v, mp.get(v)-(long)1); if(mp.get(v)==0) mp.remove(v); } } public static int upper(List<Long>A,long k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A.get(mid)<=k) { ans=mid; l=mid+1; } else { u=mid-1; } } return ans; } public static int upper(List<Integer>A,int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A.get(mid)<=k) { ans=mid; l=mid+1; } else { u=mid-1; } } return ans; } public static boolean ceq(long a[],int si,int ei) { for(int i=si+1;i<=ei;i++) if(a[i]!=a[i-1]) return false; return true; } public static int upper(long A[],long k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { ans=mid; l=mid+1; } else { u=mid-1; } } return ans; } public static int lower(List<Long>A,long k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A.get(mid)<=k) { l=mid+1; } else { ans=mid; u=mid-1; } } return ans; } public static int lower(List<Integer>A,int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A.get(mid)<=k) { l=mid+1; } else { ans=mid; u=mid-1; } } return ans; } public static int lower(long A[],long k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { l=mid+1; } else { ans=mid; u=mid-1; } } return ans; } static void pln(String s) { out.println(s); } static void p(String s) { out.print(s); } static int[] copy(int A[]) { int B[]=new int[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long[] copy(long A[]) { long B[]=new long[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void reverse(long A[]) { int n=A.length; long B[]=new long[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static char[][] inputG(int n,int m) { char str[][]=new char[n][m]; for(int i=0;i<n;i++) { String s=s(); for(int j=0;j<m;j++) str[i][j]=s.charAt(j); } return str; } static void reverse(int A[]) { int n=A.length; int B[]=new int[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static long[] inputL1(int n) { long arr[]=new long[n+1]; for(int i=1;i<=n;i++) arr[i]=l(); return arr; } static int[] input1(int n) { int arr[]=new int[n+1]; for(int i=1;i<=n;i++) arr[i]=i(); return arr; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static long[][] inputL(int n,int m){ long A[][]=new long[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=l(); } } return A; } static int[][] input(int n,int m){ int A[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=i(); } } return A; } static char[][] charinput(int n,int m){ char A[][]=new char[n][m]; for(int i=0;i<n;i++) { String s=s(); for(int j=0;j<m;j++) { A[i][j]=s.charAt(j); } } return A; } static int nextPowerOf2(int n) { if(n==0) return 1; n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } static int highestPowerof2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static long highestPowerof2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static char[] cs() { String s=s(); char ch[]=new char[s.length()]; for(int i=0;i<s.length();i++) ch[i]=s.charAt(i); return ch; } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long [] prefix(long A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] prefix(int A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] suffix(long A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static long [] suffix(int A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static void fill(int dp[]) { Arrays.fill(dp, -1); } static void fill(int dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(int dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(int dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static void fill(long dp[]) { Arrays.fill(dp, -1); } static void fill(long dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(long dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(long dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long power(long x, long y, long p) { if(y==0) return 1; if(x==0) return 0; long res = 1; 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 dln(String s) { out.println(s); } static void d(String s) { out.print(s); } static void print(int A[]) { for(int i : A) { out.print(i+" "); } out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Long> hash(int A[]){ HashMap<Integer,Long> map=new HashMap<Integer, Long>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+(long)1); } else { map.put(i, (long)1); } } return map; } static HashMap<Long,Long> hash(long A[]){ HashMap<Long,Long> map=new HashMap<Long, Long>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+(long)1); } else { map.put(i, (long)1); } } return map; } static TreeMap<Integer,Integer> tree(int A[]){ TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Long,Integer> tree(long A[]){ TreeMap<Long,Integer> map=new TreeMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static int round(int x,int y) { int ans=x/y; if(x%y!=0) ans++; return ans; } static long round(long x,long y) { long ans=x/y; if(x%y!=0) ans++; return ans; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static 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
ce1b93382c7d384190260aff63d550d5
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
/* package codechef; // don't place package name! */ import java.io.*; import java.lang.*; import java.util.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main(String[] args) throws java.lang.Exception { // your code goes here Scanner scn = new Scanner(System.in); int t; t = scn.nextInt(); while (t-- > 0) { int n; n = scn.nextInt(); int[] d = new int[n]; for (int i = 0; i < n; i++) { d[i] = scn.nextInt(); } int newarr[] = new int[n]; int break1 = 0; newarr[0] = d[0]; for (int i = 1; i < n; i++) { int firstnum = d[i] + newarr[i - 1]; int secondnum = -d[i] + newarr[i - 1]; boolean condition1 = firstnum >= 0 && secondnum < 0; boolean condition2 = firstnum < 0 && secondnum >= 0; boolean condition3 = firstnum == secondnum; if (condition2 || condition1 || condition3) { int positive = firstnum >= 0 ? firstnum : secondnum; newarr[i] = positive; } else { System.out.println(-1); break1 = 1; break; } } if (break1 == 0) { for (int i = 0; i < n; i++) { System.out.print(newarr[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
f86ec02521102f426032c065fc5849e6
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
//created by Toufique on 29/09/2022 import java.io.*; import java.util.*; public class EDU_136B { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = in.nextInt(); outer: for (int tt = 0; tt < t; tt++) { int n = in.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = in.nextLong(); long[] ans = new long[n]; ans[0] = a[0]; for (int i = 1; i < n; i++) { ans[i] = a[i] + ans[i - 1]; } for (int i = 1; i < n; i++) { if (a[i] != 0 && ans[i - 1] - a[i] >= 0) { pw.println(-1); continue outer; } } for (long x: ans) pw.print(x + " "); pw.println(); } 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
f7c63dbc50db924a038cf81593b9d58c
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.HashSet; import java.util.LinkedList; public class Main { static InputReader in; static OutputWriter out; public static void main(String[] args) throws Exception { in=new InputReader(System.in); out=new OutputWriter(System.out); int T=in.nextInt(); while(T-->0) { int n=in.nextInt(); boolean flag=true; long[] d=new long[n+1]; for(int i=1;i<=n;i++) { d[i]=in.nextLong(); if(d[i-1]-d[i]>=0 && d[i]!=0) flag=false; d[i]+=d[i-1]; } if(!flag) out.println(-1); else { for(int i=1;i<=n;i++) out.print(d[i]+" "); out.println(); } } out.flush(); } static class InputReader { private final BufferedReader br; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public int nextInt() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } boolean negative = false; if (c == '-') { negative = true; c = br.read(); } int x = 0; while (c > 32) { x = x * 10 + c - '0'; c = br.read(); } return negative ? -x : x; } public long nextLong() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } boolean negative = false; if (c == '-') { negative = true; c = br.read(); } long x = 0; while (c > 32) { x = x * 10 + c - '0'; c = br.read(); } return negative ? -x : x; } public String next() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } StringBuilder sb = new StringBuilder(); while (c > 32) { sb.append((char) c); c = br.read(); } return sb.toString(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } static class OutputWriter { private final BufferedWriter writer; public OutputWriter(OutputStream out) { writer=new BufferedWriter(new OutputStreamWriter(out)); } public void print(String str) { try { writer.write(str); } catch (IOException e) { e.printStackTrace(); } } public void print(Object obj) { print(String.valueOf(obj)); } public void println(String str) { print(str+"\n"); } public void println() { print('\n'); } public void println(Object obj) { println(String.valueOf(obj)); } public void flush() { try { writer.flush(); } catch (IOException e) { e.printStackTrace(); } } } }
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
723747fc4fc402d1b7674c7b43023fb4
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 { static class Pair { int f;int s; // Pair(){} Pair(int f,int s){ this.f=f;this.s=s;} } static class sortbyfirst implements Comparator<Pair> { public int compare(Pair a,Pair b) { return a.f-b.f; } } static class Fast { BufferedReader br; StringTokenizer st; public Fast() { 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[] readArray1(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (IOException e) { e.printStackTrace(); } return str; } } /* static long noOfDivisor(long a) { long count=0; long t=a; for(long i=1;i<=(int)Math.sqrt(a);i++) { if(a%i==0) count+=2; } if(a==((long)Math.sqrt(a)*(long)Math.sqrt(a))) { count--; } return count; }*/ static boolean isPrime(long a) { for (long i = 2; i <= (long) Math.sqrt(a); i++) { if (a % i == 0) return false; } return true; } static void primeFact(int n) { int temp = n; HashMap<Integer, Integer> h = new HashMap<>(); for (int i = 2; i * i <= n; i++) { if (temp % i == 0) { int c = 0; while (temp % i == 0) { c++; temp /= i; } h.put(i, c); } } if (temp != 1) h.put(temp, 1); } static void reverseArray(int a[]) { int n = a.length; for (int i = 0; i < n / 2; i++) { a[i] = a[i] ^ a[n - i - 1]; a[n - i - 1] = a[i] ^ a[n - i - 1]; a[i] = a[i] ^ a[n - i - 1]; } } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(long [] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static int max(int a,int b) { return a>b?a:b; } static int min(int a,int b) { return a<b?a:b; } public static void main(String args[]) throws IOException { Fast sc = new Fast(); PrintWriter out = new PrintWriter(System.out); int t1 = sc.nextInt(); outer: while (t1-- > 0) { int n=sc.nextInt();int d[]=sc.readArray(n); int a[]=new int[n]; a[0]=d[0];int f=0; for(int i=1;i<n;i++) { int one=a[i-1]+d[i]; int two=a[i-1]-d[i]; if(one>=0&&two>=0&&one!=two) { f=1; break; } if(one>=0) a[i]=one; else a[i]=two; } if(f==1) { out.println(-1);continue outer; } for(int ne:a) out.print(ne+" "); 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
c288e624062b3fbeac3f29619cefb751
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.*; public class Solution { private FastReader reader; private StringBuilder sb; private static final int TEN_POW_FIVE = (int)1e5; private static final int MOD = (int)1e9+7; public Solution() { this.reader = new FastReader(); this.sb = new StringBuilder(); } private void solve() throws Exception { int t = reader.getInt(); //int[][] dirs = {{-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1}}; while(t-->0) { int n = reader.getInt(); int[] arr = reader.getIntArr(n); solve(arr, n); } reader.closeReader(); } private boolean isValid(int x, int y, int n, int m) { return x>0 && x<=n && y>0 && y<=m; } private void solve(int[] arr, int n) { for(int i=1; i<n; i++) { if(arr[i] == 0) { arr[i] = arr[i-1]; continue; } if(arr[i-1]>=arr[i]) { sb.append("-1").append("\n"); return; } arr[i] += arr[i-1]; } printArr(arr, n, 0); } private void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } private int min(Integer... minArr) { int min = minArr[0]; for(int i=1; i<minArr.length; i++) { min = Math.min(min, minArr[i]); } return min; } private int max(Integer... maxArr) { int max = maxArr[0]; for(int i=1; i<maxArr.length; i++) { max = Math.max(max, maxArr[i]); } return max; } private void printArr(int[] arr, int n, int incrementer) { for(int i=0; i<n; i++) { sb.append(arr[i]+incrementer).append(" "); } sb.append("\n"); } public static void main(String[] args) { try { Solution solution = new Solution(); solution.solve(); solution.printOp(); } catch(Exception e) { e.printStackTrace(); } } private void printOp() { System.out.println(sb.toString()); } static class FastReader { private BufferedReader inputReader; private StringTokenizer st; public FastReader() { inputReader = new BufferedReader(new InputStreamReader(System.in)); } private String getNext() throws Exception { if(st==null || !st.hasMoreTokens()) { st = new StringTokenizer(inputReader.readLine()); } return st.nextToken(); } public String[] getLine(String delimiter) throws Exception { return inputReader.readLine().split(delimiter); } public String getString() throws Exception { return getNext(); } public int getInt() throws Exception { return Integer.parseInt(getNext()); } public long getLong() throws Exception { return Long.parseLong(getNext()); } public double getDouble() throws Exception { return Double.parseDouble(getNext()); } public int[] getIntArr(int n) throws Exception { int[] arr = new int[n]; for(int i=0; i<n; i++) { arr[i] = getInt(); } return arr; } public long[] getLongArr(int n) throws Exception { long[] arr = new long[n]; for(int i=0; i<n; i++) { arr[i] = getLong(); } return arr; } public double[] getDoubleArr(int n) throws Exception { double[] arr = new double[n]; for(int i=0; i<n; i++) { arr[i] = getDouble(); } return arr; } public String[] getStrArr(int n) throws Exception { String[] arr = new String[n]; for(int i=0; i<n; i++) { arr[i] = getString(); } return arr; } public void closeReader() throws Exception { inputReader.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
e40d4f45213c91f04993c700df677e48
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; /** * @author freya * @date 2022/9/6 **/ public class B { public static Reader in; public static PrintWriter out; public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); in = new Reader(); solve(); out.close(); } static void solve(){ int t = in.nextInt(); while (t-->0){ int n = in.nextInt(); int[] d = in.nextIntArray(n); int[] res = new int[n]; res[0] = d[0]; boolean flag = true; for(int i = 1;i<n;i++){ if(d[i] != 0 && res[i-1] >= d[i]){ out.println(-1); flag = false; break; } res[i] = res[i-1] + d[i]; } if (flag) { for (int i = 0; i < n; i++) out.print(res[i] + " "); out.println(); } } } static class Reader { private BufferedReader br; private StringTokenizer st; Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { try { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } long nextLong() { return Long.parseLong(next()); } String nextLine() { String s = ""; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } } }
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
df1cd2f987b5c90e41cc5c4979f1b7fe
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 c1733 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t,j,i,n,a[],p; //long a[]; t=sc.nextInt(); for(j=1;j<=t;j++){ n=sc.nextInt(); a=new int[n]; for(i=0;i<n;i++) a[i]=sc.nextInt(); List<Integer> ll=new ArrayList<>(); p=a[0]; ll.add(p); for(i=1;i<n;i++){ if((p-a[i])>=0&&a[i]!=0) break; p+=a[i]; ll.add(p); } if(i==n){ for(i=0;i<n;i++) System.out.print(ll.get(i)+" "); } else System.out.print(-1); System.out.println(); } } }
Java
["3\n\n4\n\n1 0 2 5\n\n3\n\n2 6 3\n\n5\n\n0 0 0 0 0"]
2 seconds
["1 1 3 8\n-1\n0 0 0 0 0"]
NoteIn the second example, there are two suitable arrays: $$$[2, 8, 5]$$$ and $$$[2, 8, 11]$$$.
Java 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
cf24ebb5826f4da833bb1d3688d3ef52
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 codeforces.edu136; import java.io.*; import java.util.*; import static java.lang.Math.*; public class Main { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); solve(in.nextInt()); //solve(1); } /* General tips 1. It is ok to fail, but it is not ok to fail for the same mistakes over and over! 2. Train smarter, not harder! 3. If you find an answer and want to return immediately, don't forget to flush before return! */ /* Read before practice 1. Set a timer based on a problem's difficulty level: 45 minutes at your current target practice level; 2. During a problem solving session, focus! Do not switch problems or even worse switch to do something else; 3. If fail to solve within timer limit, read editorials to get as little help as possible to get yourself unblocked; 4. If after reading the entire editorial and other people's code but still can not solve, move this problem to to-do list and re-try in the future. 5. Keep a practice log about new thinking approaches, good tricks, bugs; Review regularly; 6. Also try this new approach suggested by um_nik: Solve with no intention to read editorial. If getting stuck, skip it and solve other similar level problems. Wait for 1 week then try to solve again. Only read editorial after you solved a problem. 7. Remember to also submit in the original problem link (if using gym) so that the 1 v 1 bot knows which problems I have solved already. 8. Form the habit of writing down an implementable solution idea before coding! You've taken enough hits during contests because you rushed to coding! */ /* Read before contests and lockout 1 v 1 Mistakes you've made in the past contests: 1. Tried to solve without going through given test examples -> wasting time on solving a different problem than asked; 2. Rushed to coding without getting a comprehensive sketch of your solution -> implementation bugs and WA; Write down your idea step by step, no need to rush. It is always better to have all the steps considered before hand! Think about all the past contests that you have failed because slow implementation and implementation bugs! This will be greatly reduced if you take your time to get a thorough idea steps! 3. Forgot about possible integer overflow; When stuck: 1. Understand problem statements? Walked through test examples? 2. Take a step back and think about other approaches? 3. Check rank board to see if you can skip to work on a possibly easier problem? 4. If none of the above works, take a guess? */ static void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { int n = in.nextInt(); int[] d = in.nextIntArrayPrimitive(n); int[] a = new int[n]; a[0] = d[0]; for(int i = 1; i < n; i++) { a[i] = a[i - 1] + d[i]; } boolean unique = true; for(int i = 1; i < n; i++) { if(d[i] != 0) { int prev = a[i] - d[i] * 2; int j = i + 1; for(; j < n && prev >= 0; j++) { prev += d[j]; } if(j == n && prev >= 0) { unique = false; break; } } } if(unique) { for(int v : a) { out.print(v + " "); } out.println(); } else { out.println(-1); } } out.close(); } static long addWithMod(long x, long y, long mod) { return (x + y) % mod; } static long subtractWithMod(long x, long y, long mod) { return ((x - y) % mod + mod) % mod; } static long multiplyWithMod(long x, long y, long mod) { return x * y % mod; } static long modInv(long x, long mod) { return fastPowMod(x, mod - 2, mod); } static long fastPowMod(long x, long n, long mod) { if (n == 0) return 1; long half = fastPowMod(x, n / 2, mod); if (n % 2 == 0) return half * half % mod; return half * half % mod * x % mod; } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream("src/input.in")); out = new PrintWriter(new FileOutputStream("src/output.out")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } List<Integer>[] readUnWeightedGraphOneIndexed(int n, int m) { List<Integer>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); adj[u].add(v); adj[v].add(u); } return adj; } List<int[]>[] readWeightedGraphOneIndexed(int n, int m) { List<int[]>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); int w = in.nextInt(); adj[u].add(new int[]{v, w}); adj[v].add(new int[]{u, w}); } return adj; } List<Integer>[] readUnWeightedGraphZeroIndexed(int n, int m) { List<Integer>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u].add(v); adj[v].add(u); } return adj; } List<int[]>[] readWeightedGraphZeroIndexed(int n, int m) { List<int[]>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; int w = in.nextInt(); adj[u].add(new int[]{v, w}); adj[v].add(new int[]{u, w}); } return adj; } /* A more efficient way of building an undirected graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildUndirectedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]] = end2[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]] = end1[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building an undirected weighted graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][][] buildUndirectedWeightedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt], weight = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(), w = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; weight[i] = w; } int[][][] adj = new int[nodeCnt + 1][][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]][2]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]][0] = end2[i]; adj[end1[i]][idxForEachNode[end1[i]]][1] = weight[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]][0] = end1[i]; adj[end2[i]][idxForEachNode[end2[i]]][1] = weight[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building a directed graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildDirectedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]] = to[i]; idxForEachNode[from[i]]++; } return adj; } /* A more efficient way of building a directed weighted graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][][] buildDirectedWeightedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt], weight = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(), w = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; weight[i] = w; } int[][][] adj = new int[nodeCnt + 1][][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]][2]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]][0] = to[i]; adj[from[i]][idxForEachNode[from[i]]][1] = weight[i]; idxForEachNode[from[i]]++; } return adj; } } }
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
40ef49570ccfc70a479fcaf90a0963f5
train_109.jsonl
1664462100
For an array of non-negative integers $$$a$$$ of size $$$n$$$, we construct another array $$$d$$$ as follows: $$$d_1 = a_1$$$, $$$d_i = |a_i - a_{i - 1}|$$$ for $$$2 \le i \le n$$$.Your task is to restore the array $$$a$$$ from a given array $$$d$$$, or to report that there are multiple possible arrays.
256 megabytes
import java.util.Scanner; public class solution { static Scanner sc = new Scanner(System.in); public static void main(String [] args) { int a = sc.nextInt(); for(int i = 0; i < a; i++) { int flag = 0; int num = sc.nextInt(); int [] array = new int[num]; int [] arr = new int[num]; for(int j = 0; j < num; j++) { int n = sc.nextInt(); array[j] = n; } arr[0] = array[0]; for(int j = 1; j < num; j++) { int c = array[j] + arr[j-1]; int d = -array[j] + arr[j-1]; if(c == d) { arr[j] = c; } else if(c < 0) { if(d > 0) { arr[j] = d; } } else if(d < 0) { if(c > 0) { arr[j] = c; } } else { arr[j] = -10000; } } for(int j = 0; j < num; j++) { if(arr[j] == -10000) { flag = 1; break; } else { flag = 0; } } if(flag == 0) { for(int j = 0; j < num; j++) { System.out.print(arr[j]); System.out.print(" "); } } if(flag == 1) { 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
ea987771f9f815664a063f43b770dbc4
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.awt.Point; import java.math.BigInteger; import static java.lang.Math.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); long t=sc.nextInt(); for(int i=0;i<t;i++){ long n=sc.nextInt(); long ans=0; int d[]=new int[1000]; for(int j=1;j<=n;j++){ d[j]=sc.nextInt(); } for(int k=1;k<n;k++){ if(d[k+1]<=d[k]&&d[k+1]!=0){ ans=-1; break; } else{ d[k+1]=Math.abs(d[k]+d[k+1]); } } if(ans==-1){ System.out.println(ans); } else{ for(int l=1;l<=n;l++){ System.out.println(d[l]); } } } } }
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
e7a20808ba327e87a84c77949158a1ae
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 in=new Scanner (System.in); int t=in.nextInt(); // boolean cnt=true; while(t>0) { boolean cnt=true; int n=in.nextInt(); int d[]=new int[n]; for(int i=0;i<n;i++) d[i]=in.nextInt(); int a[]=new int[n]; a[0]=d[0]; for(int i=1;i<n;i++) { if(d[i]==0) { a[i]=a[i-1]; // System.out.println("hello"+i); } else if(d[i]>a[i-1]) { a[i]=d[i]+a[i-1]; } else{ //System.out.println(-1); cnt=false; break; } } if(cnt==false) System.out.println(-1); else for(int j=0;j<n;j++) System.out.print(a[j]+" "); t--; } // System.out.println("Hello World"); } }
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
cc3db398293c8ea525c59d0e1e4b3610
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
// package codeforces; import java.util.Scanner; public class deddazai{ public static void main(String[] args) { Scanner dazai = new Scanner(System.in); int t = dazai.nextInt(); int n, m, x, y, i; for ( i = 0; i < t; i++) { n = dazai.nextInt(); m = dazai.nextInt(); x=1; y=1; checkCell(n,m,x,y); } } private static void checkCell(int n, int m, int i, int j){ if(n<2 || m<2){ System.out.println(n + " " + m); return; } for(i=1; i<=n; i++){ for (j=1; j<=m; j++){ if( ( (i-2)<1 || (j-1)<1 || (i-2)>n || (j-1)>m ) && ( (i-1)<1 || (j-2)<1 || (i-1)>n || (j-2)>m ) && ( (i+2)<1 || (j+1)<1 || (i+2)>n || (j+1)>m ) && ( (i+1)<1 || (j+2)<1 || (i+1)>n || (j+2)>m ) && ( (i+1)<1 || (j-2)<1 || (i+1)>n || (j-2)>m ) && ( (i+2)<1 || (j-1)<1 || (i+2)>n || (j-1)>m ) && ( (i-2)<1 || (j+1)<1 || (i-2)>n || (j+1)>m ) && ( (i-1)<1 || (j+2)<1 || (i-1)>n || (j+2)>m ) ){ System.out.println(i + " " + j); return; } } } System.out.println(n + " " + m); } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
fce5bf3a1e4f94127f7ed6bc7cfb1457
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int testCases = scn.nextInt(); for (int i = 0; i < testCases; i++) { int n,m; n = scn.nextInt(); m = scn.nextInt(); if (n==1 || m==1){ System.out.println((new java.util.Random().nextInt(n)+1) + " " + (new java.util.Random().nextInt(m)+1)); continue; } else if (n<=3 && m<=3) { System.out.println(2 + " " + 2); } else { System.out.println((new java.util.Random().nextInt(n)+1) + " " + (new java.util.Random().nextInt(m)+1)); } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
05e716da37c41b0aab3a3e42d07518ce
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int testCases = scn.nextInt(); for (int i = 0; i < testCases; i++) { int n,m; n = scn.nextInt(); m = scn.nextInt(); if (n==1 || m==1){ System.out.println((new java.util.Random().nextInt(n)+1) + " " + (new java.util.Random().nextInt(m)+1)); continue; } else if (n<=3 && m<=3) { if (n==m){ System.out.println(2 + " " + 2); continue; } else if (n>m){ System.out.println(2 + " " + (new java.util.Random().nextInt(m)+1)); } else { System.out.println((new java.util.Random().nextInt(n)+1) + " " + 2); } } else { System.out.println((new java.util.Random().nextInt(n)+1) + " " + (new java.util.Random().nextInt(m)+1)); } // if ((n<=3 || m<=3)){ // if (n==m || n<3 || m<3){ // if (n==3){ // System.out.println(2 + " " + 2); // continue; // } // System.out.println(n + " " + m); // } // else { // if (m == 3) { // System.out.println(n + " " + 2); // } else if (n == 3) { // System.out.println(2 + " " + m); // } // } // } else { // System.out.println((new java.util.Random().nextInt(n)+1) + " " + (new java.util.Random().nextInt(m)+1)); // } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
313e1400b99750b7dbccaa45c6d318db
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
//package com.company; import java.util.Arrays; import java.util.HashMap; import java.util.Map; 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 row = in.nextInt(); int col = in.nextInt(); if(row == 3 && col == 3){ System.out.println("2 2"); }else if(row == 2 && col == 3){ System.out.println("1 2"); }else if(row == 3 && col == 2){ System.out.println("2 1"); }else{ System.out.println(row + " " + col); } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
2ae170034e3233cc8330d2cc67daefbe
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; public class Knight { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); while(n!=0){ int a=sc.nextInt(); int b=sc.nextInt(); int x=(int)Math.floor(a/2)+1; int y=(int)Math.floor(b/2)+1; System.out.println(x+" "+y); n=n-1; } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
b8d3ab9ac2f6664ceddd1da848936035
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
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) { long n=sc.nextLong(); long m=sc.nextLong(); if(n==3&&m==3) System.out.println((n-1)+" "+(m-1)); else if(n==2&&m==3) System.out.println((n-1)+" "+(m-1)); else if(n==3&&m==2) System.out.println((n-1)+" "+(m-1)); else System.out.println(n+" "+m); } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
4c044653a2a7ee1132079e68fe23be53
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; public class ImmobileKnight { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while (t > 0) { int n = sc.nextInt(); int m = sc.nextInt(); if(n == 1 || m == 1) System.out.println(n + " " + m); else if((n == 2 && m <= 3) || (m <= 2 && n <= 3) || (n == 3 && m == 3)) System.out.println("2 2"); else{ // int bufferHo = m; // int bufferVe = n; // if((bufferVe + 2 > 8 || bufferVe - 2 < 0) && (bufferHo + 1 > 8 || bufferHo - 1 < 0)) // System.out.println(bufferHo + " " + bufferVe); // else if((bufferHo + 2 > 8 || bufferHo - 2 < 0) && (bufferVe + 1 > 8 || bufferVe - 1 < 0)) // System.out.println(bufferHo + " " + bufferVe); System.out.println(n + " " + m); } t--; } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
c2181e753ba2791a2afc790424a8836a
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; public class ImmobileKnight { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int n, m; for (int i=0; i<t; i++) { n = sc.nextInt(); m = sc.nextInt(); if ((n<=3 & m<=3) & (n+m==5 || n+m==6)) System.out.println("2 2"); else System.out.println("1 1"); } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
b93ff3f842f9b7dea3dfc494571028ed
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.*; import java.io.*; import java.lang.reflect.Array; 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 m=sc.nextInt(); int min=Math.min(n, m); int max=Math.max(n, m); if(min>=2&&max>=4) { System.out.println(2+" "+2); } else if(min==1) System.out.println(1+" "+1); else System.out.println(2+" "+2); } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
a51317d51f45825ec2eaf92a34fcbbb2
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; public class ImmobileKnight { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int m=sc.nextInt(); int ro[]={+2,+2,-2,-2,+1,+1,-1,-1}; int co[]={+1,-1,+1,-1,+2,-2,+2,-2}; int ans[]=new int[2]; ans=f(0,0,n,m,ro,co); System.out.println(ans[0]+" "+ans[1]); } } static int[] f(int i,int j,int n,int m, int[] ro, int[] co){ int ans[]=new int[2]; ans[0]=0; ans[0]=0; int count=0; for(i=0;i<n;i++){ for( j=0;j<m;j++){ count=0; for(int k=0;k<8;k++){ int nr=i+ro[k]; int nc=j+co[k]; if(nr<n&& nr>=0&&nc<m&&nc>=0){ break; } else count++; } if(count==8){ ans[0]=i+1; ans[1]=j+1; return ans; } } } ans[0]=1; ans[1]=1; return ans; } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
3e5188c0d791bd44c1fdfe03b7c18c9e
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.*; public class ImmobileKnight { public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); for (int x = 0; x < t; x++) { int n=s.nextInt(); int m=s.nextInt(); if(n==1 || m==1){ System.out.println(n+" "+m); } else if(n>3 || m>3){ System.out.println(n+" "+m); } else if((n==2 && m==2) ||(n==3 && m==3) ){ System.out.println("2 2"); } else{ System.out.println("2 2"); } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
ff23212f89d958f036d61b36a66db7fb
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.io.*; import java.util.*; public class syntax { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for(int i = 0; i<t; i++) { int m = scanner.nextInt(), n = scanner.nextInt(); if(m<=2&n<=2) { System.out.println(m+" "+n); } else if(m==1|n==1) { System.out.println(m+" "+n); } else if(m==2&n==3) { System.out.println("2 2"); } else if(m==3&n==2) { System.out.println("2 2"); } else if(m==3&n==3) { System.out.println("2 2"); } else if(m==3&n>3) { System.out.println(m+" "+n); } else if(m>3&n==3) { System.out.println(m+" "+n); } else if(m>3&n>3) { System.out.println(m+" "+n); } else { System.out.println(m+" "+n); } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
28b71111eb9b32b49ad05c65e49b7de8
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.*; import java.io.*; public class Main{ private static FastReader sc; private static PrintWriter out; public static void main(String[] args) { sc = new FastReader(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); for(int T = 1 ; T<=t ; T++){ int n = sc.nextInt(); int m = sc.nextInt(); if(n==1 || m==1) out.println(n+" "+m); else if(n<3 && m<3) out.println(n+" "+m); else if((n==3 && m==3) || (n<=2 && m==3) || (n==3 && m<=2)) out.println(2+" "+2); else out.println(n+" "+m); } out.close(); } } class FastReader{ BufferedReader br; StringTokenizer str; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (str == null || !str.hasMoreElements()){ try{ str = new StringTokenizer(br.readLine()); } catch (IOException Error){ Error.printStackTrace(); } } return str.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 Error){ Error.printStackTrace(); } return str; } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
9d0f589dd0aa3848a85908b64d1cdfd2
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int m = in.nextInt(); if((n==2 && m == 3) || (n == 3 && m == 2) || (m == 3 & n == 3)){ System.out.print(2 +" "+2); }else{ System.out.print(1 +" "+1); } System.out.println(); } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
a1f4035729266a62c75edc2ae20d9bd6
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testCases = sc.nextInt(); for(int i = 0; i < testCases; i++) { int nRows = sc.nextInt(); int mCols = sc.nextInt(); if(nRows == 3 && mCols == 3) { System.out.println("2 2"); } else if((nRows == 2 && mCols == 3) || (nRows == 3 && mCols == 2)) { System.out.println("2 2"); } else { System.out.println("1 1"); } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
2cfe5a035f553a770c039c9b1a487449
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testCases = sc.nextInt(); for(int i = 0; i < testCases; i++) { int nRows = sc.nextInt(); int mCols = sc.nextInt(); if(nRows == 1 || mCols == 1) { System.out.println("1 1"); } else if(nRows == 2 && mCols == 2) { System.out.println("1 1"); } else if(nRows == 3 && mCols == 3) { System.out.println("2 2"); } else if((nRows == 2 && mCols == 3) || (nRows == 3 && mCols == 2)) { System.out.println("2 2"); } else { System.out.println("1 1"); } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
5d32f1fc3866e3c17ff94559e19d3904
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testCases = sc.nextInt(); List<Integer> results = new ArrayList<Integer>(); for(int i = 0; i < testCases; i++) { int nRows = sc.nextInt(); int mCols = sc.nextInt(); if(nRows == 1 || mCols == 1) { results.add(1); results.add(1); } else if(nRows == 2 && mCols == 2) { results.add(1); results.add(1); } else if(nRows == 3 && mCols == 3) { results.add(2); results.add(2); } else if((nRows == 2 && mCols == 3) || (nRows == 3 && mCols == 2)) { results.add(2); results.add(2); } else { results.add(1); results.add(1); } } for (int i = 0; i < results.size(); i+=2) { System.out.println(results.get(i) + " " + results.get(i+1)); } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
0fbf84944b35c5bb89d30ee646ceda1c
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
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(); int m = in.nextInt(); int cell_n = (n + 1) / 2; int cell_m = (m + 1) / 2; System.out.format("%d %d\n", cell_n, cell_m); } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
24a9aad7163d977fc1e349e2c705d43b
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
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(); for(int i=0; i<t; i++) { int n=sc.nextInt(); int m=sc.nextInt(); if(n==1 || m==1) { System.out.print(n+ " "); System.out.print(m); System.out.println("\t"); } else if(n<=2 && m<=2) { System.out.print(n +" "); System.out.print(m); System.out.println("\t"); } else if(m==3 && n==3) { System.out.print(2 +" "); System.out.print(2); System.out.println("\t"); } else if(n>=3 && m>=3) { System.out.print(n + " "); System.out.print(m); System.out.println("\t"); } else if(m==3 && n==2) { System.out.print(1+ " "); System.out.print(2); System.out.println("\t"); } else if(m==2 && n==3) { System.out.print(2 +" "); System.out.print(1); System.out.println("\t"); } else { System.out.print(n + " "); System.out.print(m); System.out.println("\t"); } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
1a85417310e08111bd25468af203e3e9
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; public class Qwerty { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for (int q = 0; q < t; q++) { int n = scanner.nextInt(); int m = scanner.nextInt(); if(n>3 && m>3){ System.out.println(n + " " + m); }else if(n==3 && m ==3){ System.out.println("2 2"); } else if(n>=2 && m>=3){ System.out.println("1 2"); }else if(n>=3 && m>=2){ System.out.println("2 1"); }else{ System.out.println(n + " " + m); } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
c956498e665472c511a1d83703690f70
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
//package codeforces; import java.util.Scanner; public class Problem_23 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while (test-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); if(n == 1 || m == 1) { System.out.println(n + " " + m); continue; } if(n == m) { if(n == 2) { System.out.println(2 + " " + 2); }else if(n == 3) { System.out.println(2 + " " + 2); }else { System.out.println(n + " " + m); } }else { if(n == 2 && m == 3) { System.out.println(1+ " " + 2); }else if(n == 3 && m == 2) { System.out.println(2 + " " + 1); }else { System.out.println(n + " " + m); } } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
f9c095630c9f0e8f228ec3740611d418
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; public class IsolatedHorse { public static Scanner sc =new Scanner(System.in); public static void main(String[] args) { int cases = sc.nextInt(); for(int i = 0; i<cases;i++){ int n = sc.nextInt(); int m = sc.nextInt(); if(n%2!=0) n=n+1; if(m%2!=0) m=m+1; System.out.println(n/2 + " "+ m/2); } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
7410adeb011be7cf5b6cfbb12ce971b9
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; public class immobile_knight_cf { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int t=sc.nextInt(); while(t--> 0){ int n=sc.nextInt(); int m=sc.nextInt(); if(n == 1 || m ==1) { System.out.print(n + " "); System.out.println(m); }else if(n ==2 && m ==2) { System.out.println(2 +" "+ 2); }else if(n ==3 && m ==3) { System.out.println(2 +" "+ 2); }else if(n ==2 && m ==3) { System.out.println(2 +" "+ 2); }else if(n ==3 && m ==2) { System.out.println(2 +" "+ 2); } else { System.out.print(n + " "); System.out.println(m); } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
875817e5e10429052948fddbb660d011
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; public class Konnb { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int m=sc.nextInt(); if(m==3&&n==3||m==2&&n==3||n==2&&m==3){ System.out.println(2+" "+2); }else{ System.out.println(n+" "+m); } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
ec2367d878c6cd326b8fc5cea58abd7a
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; public class Konnb { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int m=sc.nextInt(); if(n>=4&&m>=4){ System.out.println(n+" "+m); }else if(m==3&&n==3){ m=m-1; n=n-1; System.out.println(n+" "+m); }else if(n==3){ n=n-1; System.out.println(n+" "+m); }else if(m==3){ m=m-1; System.out.println(n+" "+m); }else if(n==1||m==1||m==2||n==2){ System.out.println(n+" "+m); } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
b19283b46194fb3fb317764c09530699
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.*; public class decelaration { 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 m = sc.nextInt(); if (n <= 3 && m <= 3 && n > 1 && m > 1){ System.out.println(2 + " " + 2); } else { System.out.println(n + " " + m); } } } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output
PASSED
c35549d8f579359420f63af278ef0530
train_109.jsonl
1664462100
There is a chess board of size $$$n \times m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$.Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board.
256 megabytes
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class Solution { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t!=0) { int n=sc.nextInt(); int m=sc.nextInt(); int x,y,c=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { x=i+2; y=j+1; if((x>n||x<1)||(y>m||y<1)) { x=i+2; y=j-1; if((x>n||x<1)||(y>m||y<1)) { x=i-1; y=j+2; if((x>n||x<1)||(y>m||y<1)) { x=i+1; y=j+2; if((x>n||x<1)||(y>m||y<1)) { x=i-2; y=j+1; if((x>n||x<1)||(y>m||y<1)) { x=i-2; y=j-1; if((x>n||x<1)||(y>m||y<1)) { x=i+1; y=j-2; if((x>n||x<1)||(y>m||y<1)) { x=i-1; y=j-2; if((x>n||x<1)||(y>m||y<1)) { System.out.println(i+" "+j); c=1; break; } } else { continue; } } else { continue; } } else { continue; } } else { continue; } } else { continue; } } else { continue; } } else { continue; } } if(c==1) { break; } } if(c==0) { System.out.println("1 1"); } t--; } sc.close(); } }
Java
["3\n\n1 7\n\n8 8\n\n3 3"]
2 seconds
["1 7\n7 2\n2 2"]
NoteIn the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer.In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer.In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
Java 17
standard input
[ "implementation" ]
e6753e3f71ff13cebc1aaf04d3d2106b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 64$$$) — the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the number of rows and columns of the board.
800
For each testcase, print two integers — the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board.
standard output