import java.io.*;
import java.util.*;

public class delivery_pk implements Runnable {
	public static void main(String[] args) {
		new Thread(new delivery_pk()).start();
	}

	BufferedReader br;
	StringTokenizer in;
	PrintWriter out;

	public String nextToken() throws IOException {
		while (in == null || !in.hasMoreTokens()) {
			in = new StringTokenizer(br.readLine());
		}
		return in.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public void solve() throws IOException {
		int n = nextInt();
		long[] a = new long[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		long[] part = new long[n];
		part[0] = a[0];
		long bigsum = 0;
		for (int i = 1; i < n; i++) {
			part[i] = part[i - 1] + a[i];
			bigsum += part[i - 1];
		}
		long ans = bigsum;
		long bigsum2 = 0;
		for (int i = n - 2; i >= -1; i--) {
		  if (i >= 0) {
  			bigsum -= part[i];
			}
			bigsum2 += part[n - 1] - part[i + 1];
			ans = Math.min(ans, bigsum + bigsum2);
		}
		out.println(ans);
	}

	public void run() {
		try {
			br = new BufferedReader(new FileReader("delivery.in"));
			out = new PrintWriter("delivery.out");

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
}