#include<iostream>
#include<vector>
using namespace std;
using ll = long long;
const int mod = 1e9+7;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }

  friend ostream& operator<<(ostream& os, const mint& m){
    os << m.x;
    return os;
  }
};

int main(){
    int N;
    cin >> N;
    vector<int> A(N);
    for(int i=0;i<N;i++)cin >> A[i];
    vector<vector<mint>> dp1(N+1,vector<mint>(2)),dp2(N+1,vector<mint>(2));
    dp1[1][0] = 1;
    dp2[1][0] = A[0];
    for(int i=2; i<=N; i++){
        dp1[i][0] += dp1[i-1][0] + dp1[i-1][1];
        dp1[i][1] += dp1[i-1][0];
        dp2[i][0] += dp2[i-1][0] + dp2[i-1][1]  + dp1[i][0]*A[i-1];
        dp2[i][1] += dp2[i-1][0] - dp1[i][1]*A[i-1];
    }
    cout << dp2[N][0] + dp2[N][1] << endl;
    return 0; 
}
#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ll long long

int main(){
	ll n;
	cin >> n;
	vector<ll> a(n);
	ll sigma = 0;
	ll p = 0;
	ll maxd = 0;
	ll ans = 0;
	for(ll i=0;i<n;i++){
		cin >> a.at(i);
		sigma += a.at(i);
		maxd = max(maxd,sigma);
		ans = max(ans,p+maxd);
		p += sigma;
		ans = max(ans,p);
	}
	cout << ans << endl;
	return 0;
}