package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	hh := make([]histogram, n)
	dec := newDecomposition(n)
	for i := 0; i < n; i++ {
		hh[i].i = i
		hh[i].h = getNextInt64(scanner)
	}
	sort.SliceStable(hh, func(i, j int) bool {
		return hh[i].h < hh[j].h
	})
	l := 0
	var ans cint
	for r := 0; r < n; r++ {
		for r > l && hh[r].h != hh[l].h {
			dec.add(hh[l].i, 1)
			l++
		}
		ans.maxAs(cint(hh[r].h) * cint(dec.kright(hh[r].i+1, n, 1)-dec.kleft(0, hh[r].i, 1)-1))
	}
	fmt.Fprintln(writer, ans)
}

type histogram struct {
	i int
	h int64
}
type decomposition struct {
	chunk  int
	bucket [][]int
}

func newDecomposition(n int) decomposition {
	bucket := make([][]int, 1)
	bucket[0] = make([]int, n)
	chunk := 8
	for i := 0; n > 1; i++ {
		n = (n-1)/chunk + 1
		bucket = append(bucket, make([]int, n))
	}
	return decomposition{
		chunk:  chunk,
		bucket: bucket,
	}
}
func (dec *decomposition) upleft(l int) int {
	return (l + dec.chunk - 1) / dec.chunk
}
func (dec *decomposition) upright(r int) int {
	return r / dec.chunk
}

func (dec *decomposition) add(index int, value int) {
	h := len(dec.bucket)
	for i := 0; i < h; i++ {
		dec.bucket[i][index] += value
		index /= dec.chunk
	}
}
func (dec *decomposition) kright(l, r, k int) int {
	return dec.krighth(l, r, 0, &k)
}
func (dec *decomposition) krighth(l, r, h int, k *int) int {
	ll := dec.upleft(l)
	rr := dec.upright(r)
	for i := l; i < r && i < ll*dec.chunk; i++ {
		if *k > dec.bucket[h][i] {
			*k -= dec.bucket[h][i]
			continue
		}
		return i
	}
	if ll < rr {
		rr = dec.krighth(ll, rr, h+1, k)
	}
	for i := rr * dec.chunk; i < r && ll <= rr; i++ {
		if *k > dec.bucket[h][i] {
			*k -= dec.bucket[h][i]
			continue
		}
		return i
	}
	return r
}
func (dec *decomposition) kleft(l, r, k int) int {
	return dec.klefth(l, r, 0, &k)
}
func (dec *decomposition) klefth(l, r, h int, k *int) int {
	ll := dec.upleft(l)
	rr := dec.upright(r)
	for i := r - 1; i >= 0 && i >= rr*dec.chunk; i-- {
		if *k > dec.bucket[h][i] {
			*k -= dec.bucket[h][i]
			continue
		}
		return i
	}
	if ll < rr {
		ll = dec.klefth(ll, rr, h+1, k) + 1
	}
	for i := ll*dec.chunk - 1; i >= 0 && i >= l && ll <= rr; i-- {
		if *k > dec.bucket[h][i] {
			*k -= dec.bucket[h][i]
			continue
		}
		return i
	}
	return -1
}

type cint int64

func (ct *cint) minAs(x cint) *cint {
	*ct = ct.min(x)
	return ct
}
func (ct *cint) maxAs(x cint) *cint {
	*ct = ct.max(x)
	return ct
}
func (ct cint) min(x cint) cint {
	if ct > x {
		return x
	}
	return ct
}
func (ct cint) max(x cint) cint {
	if ct < x {
		return x
	}
	return ct
}



Measured memory_bytes: 6400