https://www.acmicpc.net/problem/6549
import java.io.*;
import java.util.*;
public class Main {
public static BufferedReader br;
public static BufferedWriter bw;
public static int N;
public static int[] arr;
public static SegmentTree segTree;
public static long result = Long.MIN_VALUE;
public static class SegmentTree {
int[] tree;
public SegmentTree(int n) {
double height = Math.ceil(Math.log(n) / Math.log(2)) + 1;
long count = Math.round(Math.pow(2, height));
tree = new int[Math.toIntExact(count)];
}
public int init(int node, int start, int end) {
int mid = (start + end) / 2;
if (start == end) return tree[node] = start;
else {
int leftMin = init(node*2, start, mid);
int rightMin = init(node*2+1, mid+1, end);
if (arr[leftMin] <= arr[rightMin]) return tree[node] = leftMin;
else return tree[node] = rightMin;
}
}
public int cal(int node, int start, int end, int left, int right) {
int mid = (start + end) / 2;
if (end < left || start > right) return N;
else if (left <= start && right >= end) return tree[node];
else {
int leftChild = cal(node*2, start, mid, left, right);
int rightChild = cal(node*2+1, mid+1, end, left, right);
if (arr[leftChild] <= arr[rightChild]) return leftChild;
else return rightChild;
}
}
}
public static void input() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public static void recursive(int s, int e) {
if (s == e) result = Math.max(result, arr[s]);
else if (s < e) {
int minIndex = segTree.cal(1, 0, N-1, s, e);
result = Math.max(result, (long) (e - s + 1) * arr[minIndex]);
recursive(s, minIndex - 1);
recursive(minIndex+1, e);
}
}
public static void solve() throws IOException {
StringTokenizer st;
while(true) {
//초기화
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
if (N == 0) break; //종료 조건
arr = new int[N + 1];
for (int i = 0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
arr[N] = Integer.MAX_VALUE;
segTree = new SegmentTree(N);
segTree.init(1, 0, N-1);
result = Long.MIN_VALUE;
recursive(0, N-1);
bw.write(result + "\n");
}
bw.flush();
}
public static void main(String[] args) throws IOException {
input();
solve();
}
}