I have a problem, I need to compare two inputstreams fast.
Today I have a function like this:
private boolean isEqual(InputStream i1, InputStream i2) throws IOException {
try {
// do the compare
while (true) {
int fr = i1.read();
int tr = i2.read();
if (fr != tr)
return false;
if (fr == -1)
return true;
}
} finally {
if (i1 != null)
i1.close();
if (i2 != null)
i2.close();
}
}
But it's really slow. I want to use buffered reads but have not come up with a good way of doing it.
Some extra stuff that makes it harder:
I don't want to read one of the input streams into memory (the whole one)
I don't want to use a third party library
I need a practial solution - code! :)
By far my favorite is to use the org.apache.commons.io.IOUtils helper class from the Apache Commons IO library:
IOUtils.contentEquals( is1, is2 );
Something like this may do:
private static boolean isEqual(InputStream i1, InputStream i2)
throws IOException {
ReadableByteChannel ch1 = Channels.newChannel(i1);
ReadableByteChannel ch2 = Channels.newChannel(i2);
ByteBuffer buf1 = ByteBuffer.allocateDirect(1024);
ByteBuffer buf2 = ByteBuffer.allocateDirect(1024);
try {
while (true) {
int n1 = ch1.read(buf1);
int n2 = ch2.read(buf2);
if (n1 == -1 || n2 == -1) return n1 == n2;
buf1.flip();
buf2.flip();
for (int i = 0; i < Math.min(n1, n2); i++)
if (buf1.get() != buf2.get())
return false;
buf1.compact();
buf2.compact();
}
} finally {
if (i1 != null) i1.close();
if (i2 != null) i2.close();
}
}
Using buffered reads is just a matter of wrapping the InputStreams with BufferedInputStreams. However you are likely to get the best performance reading large blocks at a time.
private boolean isEqual(InputStream i1, InputStream i2) throws IOException {
byte[] buf1 = new byte[64 *1024];
byte[] buf2 = new byte[64 *1024];
try {
DataInputStream d2 = new DataInputStream(i2);
int len;
while ((len = i1.read(buf1)) > 0) {
d2.readFully(buf2,0,len);
for(int i=0;i<len;i++)
if(buf1[i] != buf2[i]) return false;
}
return d2.read() < 0; // is the end of the second file also.
} catch(EOFException ioe) {
return false;
} finally {
i1.close();
i2.close();
}
}
why not simply wrap both streams at the very beginning of your method:
i1 = new BufferedInputStream(i1);
i2 = new BufferedInputStream(i2);
Alternatively, you could simply try reading both streams into a buffer:
public static boolean equals(InputStream i1, InputStream i2, int buf) throws IOException {
try {
// do the compare
while (true) {
byte[] b1 = new byte[buf];
byte[] b2 = new byte[buf];
int length = i1.read(b1);
if (length == -1) {
return i2.read(b2, 0, 1) == -1;
}
try {
StreamUtils.readFully(i2, b2, 0, length);
} catch (EOFException e) {
// i2 is shorter than i1
return false;
}
if (!ArrayUtils.equals(b1, b2, 0, length)) {
return false;
}
}
} finally {
// simply close streams and ignore (log) exceptions
StreamUtils.close(i1, i2);
}
}
// StreamUtils.readFully(..)
public static void readFully(InputStream in, byte[] b, int off, int len) throws EOFException, IOException {
while (len > 0) {
int read = in.read(b, off, len);
if (read == -1) {
throw new EOFException();
}
off += read;
len -= read;
}
}
// ArrayUtils.equals(..)
public static boolean equals(byte[] a, byte[] a2, int off, int len) {
if (off < 0 || len < 0 || len > a.length - off || len > a2.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return true;
}
if (a == a2) {
return true;
}
if (a == null || a2 == null) {
return false;
}
for (int i = off; i < off + len; i++) {
if (a[i] != a2[i]) {
return false;
}
}
return true;
}
EDIT: I've fixed my implementation now. That's how it looks like without DataInputStream or NIO. Code is available at GitHub or from Sonatype's OSS Snapshot Repository Maven:
<dependency>
<groupId>at.molindo</groupId>
<artifactId>molindo-utils</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Related
I am solving a question on hackerearth and it is successfully compiling and passing when I am using java version Java 8 (oracle 1.8.0_131) but when Java (openjdk 1.7.0_95) is used it gives an error 15: error: cannot infer type arguments for PriorityQueue<> .The error is on the line when mx priority queue is being declared. I want to know how to resolve it and why this error occurs. Here is the code : (note that this question is not part of any ongoing contest) and the relevant part of the code is in main function only.
import java.io.*;
import java.util.*;
class TestClass {
public static void main(String[] args) {
InputReader sc = new InputReader(System.in);
int Q=sc.nextInt();
PriorityQueue<Integer> mn=new PriorityQueue<>();
PriorityQueue<Integer> mx=new PriorityQueue<>(Collections.reverseOrder());
int[] cnt =new int[100000+1];
for (int q = 0; q < Q; q++) {
String str=sc.nextLine();
if(str.substring(0,4).equals("Push")) {
int X=Integer.parseInt(str.substring(5));
++cnt[X];
mx.add(X);
mn.add(X);
}
else if (str.equals("Diff")) {
if(mx.isEmpty()||mn.isEmpty())
out.println(-1);
else {
int min = mn.poll();
int max = mx.poll();
if(min==max) {
--cnt[max];
}
else {
--cnt[min];
--cnt[max];
}
mn.remove(max);
mx.remove(min);
out.println(max-min);
}
}
else if (str.equals("CountHigh")) {
if(mx.isEmpty()) {
out.println(-1);
}
else {
out.println(cnt[mx.peek()]);
}
}
else {
if(mn.isEmpty()) {
out.println(-1);
}
else {
out.println(cnt[mn.peek()]);
}
}
// System.out.println(q+" "+mx+" "+mn);
}
out.close();
}
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static int mod = 1000000000+7;
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public double nextDouble() {
return (Double.parseDouble(readString()));
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
In Java 7 there is no PriorityQueue constructor that takes only Comparator as an argument. Take a look Java 7 Priority queue docs. However in Java 8+ there is such constructor for this class.
Your best choice would be to use constructor that takes initial capacity and a Comparator :
PriorityQueue<Integer> mx = new PriorityQueue<Integer>(10, Collections.reverseOrder());
That constructor was added in java-8, so there is no way this would work against 1.7.
There is a feature called target type that was added in java-8, but that is un-related to your question; so it is as simple as adding one more constructor parameter for example, like initial capacity.
PriorityQueue<Integer> mx = new PriorityQueue<>(5, Collections.reverseOrder());
I'm trying to fix a piece of code I've written that currently has race conditions. In doing so I need to put the condition of a while loop in a synchronized block, however I don't want to synchronise the whole while block since that would starve other threads of the resource, which they need. I can't figure a reasonable way of doing it without repetition or breaks in places that slightly obscure the control flow. Below is the gist of the problem code:
while ((numRead = in.read(buffer)) != -1) {
out.write(buffer);
}
and I need to synchronise the use of in. The two potential solutions I could think of (but don't think they're very good) are:
synchronized (this) {
numRead = in.read(buffer);
}
while (numRead != -1) {
out.write(buffer);
synchronized (this) {
numRead = in.read(buffer);
}
}
which has undesirable repetition, and this:
while (true) {
synchronized (this) {
numRead = in.read(buffer);
}
if (numRead == -1)
break;
else
out.write(buffer);
}
which isn't great for readability. Any suggestions?
Try it like below.
public testMyMethod () {
byte[] buffer = new int[1024];
int numRead = -1;
while ((numRead = readInput(buffer)) != -1) {
out.write(buffer);
}
}
//first method
int readInput(byte[] buffer) {
int readLen = -1;
synchronized(in) {
in.read(buffer);
}
return readLen;
}
//second method, more performant about 3 times, just the synchronization parts
private static final ReentrantLock inputLock = new ReentrantLock();
int readInput(byte[] buffer) {
int readLen = -1;
inputLock.lock();
try {
readLen = in.read(buffer);
} finally {
inputLock.unlock();
}
return readLen;
}
I was designing a simple parser(it works on a simple version of Shunting Yard Algorithm). Here's my code(I haven't dealt with associativity).
public class Parser {
String stack[] = new String[50];
String res = "";
int top = 0;
Operator o1 = new Operator("", 0, 0);
public String parse(String x) {
push("(");
x = x + ")";
for (int i = 0; i < x.length(); i++) {
if (o1.isNumber(x.charAt(i) + "")) {
res = res + x.charAt(i);
} else if (x.charAt(i) == '(') {
push("(");
} else if (o1.isOperator("" + x.charAt(i))) {
if (top != -1) {
while ((top != -1) && (o1.isOperator(stack[top]))) {
int m = o1.getOperatorIndex(stack[top]);
int mp = o1.op[m].prec;
int xp = o1.op[o1.getOperatorIndex("" + x.charAt(i))].prec;
if (m >= xp) {
res = res + stack[top];
}
top--;
}
}
push("" + x.charAt(i));
} else {
if (top != -1) {
while ((top != -1) && (stack[top] != ")")) {
if (o1.isOperator(stack[top])) {
res = res + stack[top];
}
top--;
}
}
}
}
return res;
}
public void push(String m) {
if (top != 49) {
stack[top] = m;
top++;
} else {
System.out.println("Overflow");
}
}
}
I guess you won't need the Operator class's code. When I try executing parse("1+2"), it just returns 12 and not the + sign. What is wrong? and yes, o[0] is +,o[1] is -,o[2] is*, o[3] is /
I have trouble with networking in java. I have tried to read a message from a client over sockets. I use BufferedReader for reading the message.
public String read() throws IOException {
String message = reader.readLine();
return message;
}
When I am on reader.readline() method on the server, if the client kills the connection I expect an error actually. However, instead of throwing an exception, it returns NULL.
#Eray Tuncer
it depends on when the connection was closed if it is before start reading the line then yes you should expect an exception. but if it is in between reading I think you will get "null" indicating end of the stream. Please check the following implementation of readLine from BufferedReader :
String readLine(boolean ignoreLF) throws IOException {
StringBuffer s = null;
int startChar;
synchronized (lock) {
ensureOpen(); //This method ensures that the stream is open and this is called before start reading
..................
................
//----Now reading operation started if the connection is closed it will just return a null---------
bufferLoop:
for (;;) {
if (nextChar >= nChars)
fill();
if (nextChar >= nChars) { /* EOF */
if (s != null && s.length() > 0)
return s.toString();
else
return null;
}
boolean eol = false;
char c = 0;
int i;
/* Skip a leftover '\n', if necessary */
if (omitLF && (cb[nextChar] == '\n'))
nextChar++;
skipLF = false;
omitLF = false;
charLoop:
for (i = nextChar; i < nChars; i++) {
c = cb[i];
if ((c == '\n') || (c == '\r')) {
eol = true;
break charLoop;
}
}
startChar = nextChar;
nextChar = i;
if (eol) {
String str;
if (s == null) {
str = new String(cb, startChar, i - startChar);
} else {
s.append(cb, startChar, i - startChar);
str = s.toString();
}
nextChar++;
if (c == '\r') {
skipLF = true;
}
return str;
}
if (s == null)
s = new StringBuffer(defaultExpectedLineLength);
s.append(cb, startChar, i - startChar);
}
}
}
So bottom line is that you should check for null in this operation rather than relying on an IOException. I hope it will help you to fix your problem. Thank you !
You can trigger an exception manually like this:
public String read() throws IOException {
String message = reader.readLine();
if (message == null)
throw new IOException("reader.readLine() returned null");
return message;
}
Yes I want to start programming competitions so I dont know how to submit in Java?
Answers very apreciated
bye
i have been in a lot of contests:
/** #team=civilian */
import java.io.*;
import java.math.*;
import java.util.*;
public class _template {
static BufferedReader input;
static StringTokenizer _stk;
static String readln() throws IOException {
String l = input.readLine();
if (l != null)
_stk = new StringTokenizer(l, " ");
return l;
}
static String next() {
return _stk.nextToken();
}
static int nextInt() {
return Integer.parseInt(next());
}
static void dbg(Object... o) {
System.out.println(Arrays.deepToString(o));
}
static PrintWriter output = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(System.out)));
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
input = new BufferedReader(new InputStreamReader(System.in));
// input = new BufferedReader(new FileReader("_template"));
// output.print("ja");
// output.close();// Be sure to close the output at the end
//or maybe he does not print everything.
}
}
Some think is best to compite in c++:
//
#include <bits/stdc++.h>
#define D(x) cout << #x << " = " << (x) << endl;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define FOREACH(it,v) for(__typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long int64;
const int INF = (int)(1e9);
const int64 INFLL = (int64)(1e18);
const double EPS = 1e-13;
int main() {
#ifdef LOCAL
freopen(".in.txt", "r", stdin);
freopen(".out.txt", "w", stdout);
#else
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#endif
}
and this help when you are practicing:
#createfiles.sh
function createfiles {
cp path/_template.cpp ./"$1.cpp";
touch "$1.in.txt";
touch "$1.out.txt";
sed "s/.in.txt/$1.in.txt/" < "$1.cpp" > "tmp";
sed "s/.out.txt/$1.out.txt/" < "tmp" > "$1.cpp" ;
rm tmp;
}
createfiles $1
runing test:
#compile_cpp.sh
function compile_cpp {
g++ -o "${1::-4}_exe" -DLOCAL "$1";
./"${1::-4}_exe";
}
compile_cpp $1
and you run it like this:
$ bash createfiles.sh uva12520
# you program an awesome solution
# and you test it
$ bash compile_ccp.sh uva12520.cpp
i think is best to create an alias in your .bash like
alias compile_cpp_maraton='function fun() { bash /path/my_commands/compile_cpp_maraton.sh "$1"; }; fun'
Sorry for the long post.
Here is a template mentioned in FlatFoot Codeforces user blog http://codeforces.com/blog/entry/7018
public class Main{
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
// Start writing your solution here. -------------------------------------
/*
int n = sc.nextInt(); // read input as integer
long k = sc.nextLong(); // read input as long
double d = sc.nextDouble(); // read input as double
String str = sc.next(); // read input as String
String s = sc.nextLine(); // read whole line as String
int result = 3*n;
out.println(result); // print via PrintWriter
*/
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
}
I do not know about CF but you can try this at SPOJ. Have a look at examples here
Have a look at it,you might find your answer here :
http://rgpv.ac.in/Campus/CodeForce%20Tutorial.pdf
Java syntax :
import java.util.*;
public class Prime {
public static void main(String args[]) {
int i;
Scanner in = new Scanner(System.in);
i = in.nextInt();
/* Take input from standard input */
if (isprime(i)) {
System.out.println("YES");
/*
* Print outp ut on standard output
*/
} else {
System.out.println("NO");
/* Print output on standard output */
}
}
public static boolean isprime(int a) {
for (int i = 2; i < a; i++) {
if (a % i == 0)
return false;
}
return true;
}
}
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.Writer;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.OutputStream;
import java.io.PrintWriter;
/**
* Author: o_panda_o
* Email: emailofpanda#yahoo.com
*/
public class Main{
static class ProblemSolver{
public void solveTheProblem(InputReader in,OutputWriter out){
/**
* This template is used by many coders for fast IO
*
* This portion acts as the main method we use. Everything we need to code...
* ...can be written here without even touching the main method.
*
* Following are the examples for IO and for more information have a look...
* ...on the classes InputReader(for Fast Input) and OutputWriter(for Fast...
* ...Output). The methods there are self explanatory
*/
/**
* How to take inputs:
* - Taking input is just nearly similar to the way we take input using scanner in Java.
* - e.g. int n=in.nextInt(); long n=in.nextLong();
*/
/**
* How to print output:
* - It is just like we use PrintWriter. Instead of using System.out, we can...
* ...only use out in that place.
* - e.g. out.print(), out.println() etc.
*/
}
//You can add necessary methods here also. Uncomment the code and test it above
//public int add(int a,int b){
// return a+b;
//}
}
//Everything below you see is a template for every code. You don't need to change them most of the time.
//As you go on coding, in the time of need you will realise what you need to change when. Just trust me on this.
public static void main(String[] args){
InputStream inputStream=System.in;
OutputStream outputStream=System.out;
InputReader in=new InputReader(inputStream);
OutputWriter out=new OutputWriter(outputStream);
ProblemSolver problemSolver=new ProblemSolver();
problemSolver.solveTheProblem(in,out);
out.flush();
out.close();
}
}
class InputReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public BigInteger readBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
public int[] nextIntArray(int n){
int[] array=new int[n];
for(int i=0;i<n;++i)array[i]=nextInt();
return array;
}
public int[] nextSortedIntArray(int n){
int array[]=nextIntArray(n);
Arrays.sort(array);
return array;
}
public int[] nextSumIntArray(int n){
int[] array=new int[n];
array[0]=nextInt();
for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt();
return array;
}
public long[] nextLongArray(int n){
long[] array=new long[n];
for(int i=0;i<n;++i)array[i]=nextLong();
return array;
}
public long[] nextSumLongArray(int n){
long[] array=new long[n];
array[0]=nextInt();
for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt();
return array;
}
public long[] nextSortedLongArray(int n){
long array[]=nextLongArray(n);
Arrays.sort(array);
return array;
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(char[] array) {
writer.print(array);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void print(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void print(double[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void print(long[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void println(int[] array) {
print(array);
writer.println();
}
public void println(double[] array) {
print(array);
writer.println();
}
public void println(long[] array) {
print(array);
writer.println();
}
public void println() {
writer.println();
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void print(char i) {
writer.print(i);
}
public void println(char i) {
writer.println(i);
}
public void println(char[] array) {
writer.println(array);
}
public void printf(String format, Object... objects) {
writer.printf(format, objects);
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
public void print(long i) {
writer.print(i);
}
public void println(long i) {
writer.println(i);
}
public void print(int i) {
writer.print(i);
}
public void println(int i) {
writer.println(i);
}
public void separateLines(int[] array) {
for (int i : array) {
println(i);
}
}
}