#include	<bits/stdc++.h>
using	namespace	std;
typedef	long long ll;

// int/long:		-2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 (-2^63 <= long < 2^63)

//#define	INF (1<<30)
#define	INF (2147483647)
// 2^31 -1
//= 1,073,741,824 *2 -1
//=   536,870,912 *4 -1

#define	MOD 1000000007

#define	Rep0(i, n)	for (auto i=0; i<n; i++)
#define	Rep1(i, n)	for (auto i=1; i<=n; i++)
#define Sort(P)	sort(P.begin(), P.end())
#define Rev(P)	reverse(P.begin(), P.end())

int	main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	ll N;
	cin >>N;
	vector<ll> A(N, 0);
	Rep0(i, N)
		cin >>A.at(i);
	ll ANS=0;
	ll D=0;
	ll DMax=0;
	ll X=0;
	Rep0(i, N) {
		D +=A.at(i);
		DMax=max(DMax, D);
		ANS=max(ANS, X+DMax);
		X +=D;
	}
	cout <<ANS <<endl;
	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
#define all(a) a.begin(), a.end()
using namespace std;
using P  = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vv = vector<vi>;

template<typename T>
struct BIT{
    int n;
    vector<T> d;
    BIT(int n=0): n(n), d(n+1) {}
    void add(int i, T x=1){//場所i(0-indexed)にxを足す
        for(i++; i<=n; i+= i&-i){
            d[i] += x;
        }
    }
    T sum(int i){//場所i以下(0-indexed)の和
        T x = 0;
        for(i++; i; i -= i&-i){
            x += d[i];
        }
        return x;
    }
};

int main(){
    int h, w, m;
    cin >> h >> w >> m;
    vi left(h, w);
    vi  top(w, h);
    vv wall(h);
    //vector<set<int>> yoko(h);
    //vector<set<int>> tate(w);
    rep(i,m){
        int x, y;
        cin >> x >> y;
        x--; y--;
        left[x] = min(left[x], y);
        top[y]  = min( top[y], x);
        wall[x].push_back(y);
        //yoko[x].insert(y);
        //tate[y].insert(x);
    }
    //rep(i,h)yoko[i].insert(w);
    //rep(j,w)tate[j].insert(h);
    
    //1手で動けるマス数
    ll ans = left[0] + top[0] -1;
    
    //横縦の順で動けるマス数
    for(int j=1; j<left[0]; j++){
        ans += top[j]-1;
    }
    
    //横縦の順で動けなかったが縦横でなら動ける(縦からは影になっている)マス数
    BIT<int> bit(w);
    for(int j0 = left[0]; j0<w; j0++)
        bit.add(j0, 1);
    
    for(int i=1; i<top[0]; i++){
        ans += bit.sum(left[i]-1);
        auto itr = wall[i].begin();
        while(itr != wall[i].end()){
            if(bit.sum(*itr) == bit.sum(*itr-1))
                bit.add(*itr, 1);
            itr++;
        }
    }
    
    cout << ans << endl;
    
    return 0;
}