/* string coder = "Balajiganapathi"; */

//#define LOCAL
#ifdef LOCAL
#   define TRACE
#else
//#   define NDEBUG
#endif

#include<bits/stdc++.h>

using namespace std;

/* aliases */
using vi  = vector<int>;
using pi  = pair<int, int>;
using vs  = vector<string>;
using vpi = vector<pi>;
using ll  = long long int;

/* shortcut macros */
#define mp              make_pair
#define fi              first
#define se              second
#define mt              make_tuple
#define gt(t, i)        get<i>(t)
#define all(x)          (x).begin(), (x).end()
#define ini(a, v)       memset(a, v, sizeof(a))
#define re(i, s, n)     for(auto i = s, _##i = (n); i < _##i; ++i)
#define rep(i, s, n)    re(i, s, (n) + 1)
#define fo(i, n)        re(i, 0, n)
#define si(x)           (int)(x.size())
#define pu              push_back
#define is1(mask,i)     ((mask >> i) & 1)

/* trace macro */
#ifdef TRACE
#   define trace(v...)  {cerr << __func__ << ":" << __LINE__ << ": " ;_dt(#v, v);}
#else
#   define trace(...)
#endif

#ifdef TRACE
pi _gp(string s) {
    pi r(0, si(s) - 1);
    int p = 0, s1 = 0, s2 = 0, start = 1;
    fo(i, si(s)) {
        int x = (s1 | s2);
        if(s[i] == ' ' && start) {
            ++r.fi;
        } else {
            start = 0;
            if(s[i] == ',' && !p && !x) {
                r.se = i - 1;
                return r;
            }
            if(x && s[i] == '\\') ++i;
            else if(!x && s[i] == '(') ++p;
            else if(!x && s[i] == ')') --p;
            else if(!s2 && s[i] == '\'') s1 ^= 1;
            else if(!s1 && s[i] == '"') s2 ^= 1;
        }
    }
    return r;
}

template<typename H> void _dt(string u, H&& v) {
    pi p = _gp(u);
    cerr << u.substr(p.fi, p.se - p.fi + 1) << " = " << forward<H>(v) << " |" << endl;
}

template<typename H, typename ...T> void _dt(string u, H&& v, T&&... r) {
    pi p = _gp(u);
    cerr << u.substr(p.fi, p.se - p.fi + 1) << " = " << forward<H>(v) << " | ";
    _dt(u.substr(p.se + 2), forward<T>(r)...);
}

template<typename T>
ostream &operator <<(ostream &o, vector<T> v) { // print a vector
    o << "{";
    fo(i, si(v) - 1) o << v[i] << ", ";
    if(si(v)) o << v.back();
    o << "}";
    return o;
}

template<typename T1, typename T2>
ostream &operator <<(ostream &o, map<T1, T2> m) { // print a map
    o << "[";
    for(auto &p: m) {
        o << " (" << p.fi << " -> " << p.se << ")";
    }
    o << " ]";
    return o;
}

template <size_t n, typename... T>
typename enable_if<(n >= sizeof...(T))>::type
    print_tuple(ostream&, const tuple<T...>&) {}

template <size_t n, typename... T>
typename enable_if<(n < sizeof...(T))>::type
    print_tuple(ostream& os, const tuple<T...>& tup) {
    if (n != 0)
        os << ", ";
    os << get<n>(tup);
    print_tuple<n+1>(os, tup);
}

template <typename... T>
ostream& operator<<(ostream& os, const tuple<T...>& tup) { // print a tuple
    os << "(";
    print_tuple<0>(os, tup);
    return os << ")";
}

template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) { // print a pair
    return os << "(" << p.fi << ", " << p.se << ")";
}
#endif

/* util functions */

template<typename T>
string tostr(T x) {
    ostringstream oss;
    oss << x;
    return oss.str();
}

template<typename T1, typename T2, typename T3>
T1 modpow(T1 a, T2 p, T3 mod) {
    T1 ret = 1;

    a %= mod;
    for(; p > 0; p /= 2) {
        if(p & 1) ret = 1ll * ret * a % mod;
        a = 1ll * a * a % mod;
    }

    return ret;
}

#define x1 _asdfzx1
#define y1 _ysfdzy1

int dx[] {-1, 0, 1, 0, 1, 1, -1, -1};
int dy[] {0, -1, 0, 1, 1, -1, 1, -1};

/* constants */
constexpr auto PI  = 3.14159265358979323846L;
constexpr auto oo  = numeric_limits<int>::max() / 2 - 10;
constexpr auto eps = 1e-6;
constexpr auto mod = 1000000007;

/* code */
const int mx = 100005, log_mx = 19;
char str[mx];
int sa[mx], inv_sa[mx], sa_rank[mx][log_mx], lcp[mx];
int log_n, n;

class sa_entry {
    public:
    int rank[2], pos;

    bool operator <(const sa_entry &s) const {
        if(rank[0] == s.rank[0]) return rank[1] < s.rank[1];
        else return rank[0] < s.rank[0];
    }

    bool operator == (const sa_entry &s) const {
        return rank[0] == s.rank[0] && rank[1] == s.rank[1];
    }
} sa_nxt[mx];

int get_lcp(int x, int y) {
    int ret = 0;

    for(int i = log_n - 1; i >= 0 && x < n && y < n; --i) {
        if(sa_rank[x][i] == sa_rank[y][i]) {
            ret += (1 << i);
            x += (1 << i);
            y += (1 << i);
        }
    }

    return ret;
}

void build_sa() {
    for(int i = 0; i < n; ++i) sa_rank[i][0] = str[i];

    if(n == 1) sa_rank[0][0] = 0;

    int siz = 1;
    for(log_n = 1; siz < n; ++log_n, siz *= 2) {
        for(int i = 0; i < n; ++i) {
            sa_nxt[i].rank[0] = sa_rank[i][log_n - 1];
            sa_nxt[i].rank[1] = i + siz < n? sa_rank[i + siz][log_n - 1]: -1;
            sa_nxt[i].pos = i;
        }

        sort(sa_nxt, sa_nxt + n);
        for(int i = 0; i < n; ++i) {
            sa_rank[sa_nxt[i].pos][log_n] = (i > 0 && sa_nxt[i] == sa_nxt[i - 1])? sa_rank[sa_nxt[i - 1].pos][log_n]: i;
        }
    }

    for(int i = 0; i < n; ++i) {
        sa[sa_rank[i][log_n - 1]] = i;
    }

    for(int i = 0; i < n; ++i) inv_sa[sa[i]] = i;

    lcp[0] = 0;
    for(int i = 1; i < n; ++i) lcp[i] = get_lcp(sa[i - 1], sa[i]);
}

class SegNode {
    public:
    int cnt;
    ll sum;
    int cleared;
    SegNode() {
        cleared = cnt = sum = 0;
    }
    void clear() {
        cleared = 1;
        cnt = sum = 0;
    }
} seg[3 * mx];

ll ans = 0;

SegNode get(int i, int a, int b, int qa, int qb) {
    if(qa <= a && b <= qb) {
        //trace(i, a, b, qa, qb, seg[i].cnt, seg[i].sum);
        return seg[i];
    }
    int l = 2 * i + 1, r = 2 * i + 2;
    int m = (a + b) / 2;

    if(seg[i].cleared) {
        seg[i].cleared = 0;
        seg[l].clear();
        seg[r].clear();
    }
    if(qb <= m) return get(l, a, m, qa, qb);
    else if(qa > m) return get(r, m + 1, b, qa, qb);
    SegNode n1 = get(l, a, m, qa, qb);
    SegNode n2 = get(r, m + 1, b, qa, qb);
    SegNode ret;

    ret.sum = n1.sum + n2.sum;
    ret.cnt = n1.cnt + n2.cnt;

    return ret;
}

void clearIt(int i, int a, int b, int qa, int qb) {
    //trace(i, a, b, qa, qb);
    if(b < qa || a > qb) return;
    if(qa <= a && b <= qb) {
        seg[i].clear();
        return;
    }
    int l = 2 * i + 1, r = 2 * i + 2;
    int m = (a + b) / 2;

    if(seg[i].cleared) {
        seg[i].cleared = 0;
        seg[l].clear();
        seg[r].clear();
    }
    clearIt(l, a, m, qa, qb);
    clearIt(r, m + 1, b, qa, qb);

    seg[i].cnt = seg[l].cnt + seg[r].cnt;
    seg[i].sum = seg[l].sum + seg[r].sum;
    trace(i, seg[i].cnt, seg[i].sum);
}

void update(int i, int a, int b, int q, SegNode n) {
    if(q < a || q > b) return;
    if(a == b) {
        seg[i].cnt += n.cnt;
        seg[i].sum += n.sum;
        return;
    }
    int l = 2 * i + 1, r = 2 * i + 2;
    int m = (a + b) / 2;

    if(seg[i].cleared) {
        seg[i].cleared = 0;
        seg[l].clear();
        seg[r].clear();
    }

    if(q <= m) update(l, a, m, q, n);
    else update(r, m + 1, b, q, n);

    seg[i].cnt = seg[l].cnt + seg[r].cnt;
    seg[i].sum = seg[l].sum + seg[r].sum;
    //trace(i, a, b, q, seg[i].cnt, seg[i].sum);
}

void proc(int i) {
    int x = lcp[i];
    ll cur = x;
    SegNode larger = get(0, 0, n, x + 1, n);
    trace(x, larger.cnt, larger.sum);
    cur += 1ll * larger.cnt * x;
    SegNode notLarger = get(0, 0, n, 0, x);
    trace(x, notLarger.cnt, notLarger.sum);

    cur += notLarger.sum;
    ll tmp = x;
    int tc = x;
    for(int j = i - 1; j > 0; --j) {
        tc = min(tc, lcp[j]);
        tmp += tc;
    }

    if(cur != tmp) {
        trace(i, x, cur, tmp);
        assert(cur == tmp);
    }

    clearIt(0, 0, n, x + 1, n);
    SegNode updateNode;
    updateNode.cnt = larger.cnt + 1;
    updateNode.sum = 1ll * updateNode.cnt * x;
    update(0, 0, n, x, updateNode);

    ans += cur;
    trace(x, cur, ans, updateNode.cnt, updateNode.sum);

}

int main() {
    scanf("%s", str);
    n = strlen(str);
    build_sa();

    ans += 1ll * n * (n + 1) / 2;

    re(i, 1, n) {
        trace(i, lcp[i]);
        proc(i);
    }

    cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <deque>
#include <queue>
#include <ctime>
#include <iomanip>
#include <cstring>

#define SZ(x) ((int)x.size())
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define rank def1

using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef pair<pii, int> piii;

const int N = 100 * 1000 + 100;
const int LG = 20;

int n;
string s;
int rank[LG][N];
piii t[N];
pii l[N];
int par[N];
ll sz[N];

int lcp(int x, int y){
	if(x == y) return n - x;
	int ans = 0;
	for(int i = LG; i --;){
		if(rank[i][x] == rank[i][y]){
			x += 1 << i;
			y += 1 << i;
			ans |= 1 << i;
		}
	}
	return ans;
}

int get_par(int x){
	if(par[x] == x) return x;
	return par[x] = get_par(par[x]);
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> s;
	n = SZ(s);
	for(int j = 0; j < n; ++j) sz[j] = 1, par[j] = j, rank[0][j] = s[j];
	for(int i = 1; i < LG; ++i){
		for(int j = 0; j < n; ++j)
			if(j + (1 << (i - 1)) < n)
				t[j] = {{rank[i - 1][j], rank[i - 1][j + (1 << (i - 1))]}, j};
			else
				t[j] = {{rank[i - 1][j], -1}, j};
		sort(t, t + n);
		rank[i][t[0].Y] =  1;
		for(int j = 1; j < n; ++j){
			if(t[j - 1].X == t[j].X)
				rank[i][t[j].Y] = rank[i][t[j - 1].Y];
			else
				rank[i][t[j].Y] = j + 1;
		}
	}
	for(int i = 0; i < n - 1; ++i)
		 l[i] = {lcp(t[i].Y, t[i + 1].Y), i};
	sort(l, l + n - 1);
	reverse(l, l + n - 1);
	ll ans = n * (n + 1) / 2;
	for(int i = 0; i < n - 1; ++i){
		int x = l[i].Y, y = l[i].Y + 1;
		x = get_par(x);
		y = get_par(y);
		ans += l[i].X * sz[x] * sz[y];
		par[y] = x;
		sz[x] += sz[y];
	}
	cout << ans << "\n";
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <deque>
#include <queue>
#include <ctime>
#include <iomanip>
#include <cstring>

#define SZ(x) ((int)x.size())
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define rank def1

using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef pair<pii, int> piii;

const int N = 100 * 1000 + 100;
const int LG = 20;

ll n;
string s;
int rank[LG][N];
piii t[N];
pii l[N];
int par[N];
ll sz[N];

int lcp(int x, int y){
	if(x == y) return n - x;
	int ans = 0;
	for(int i = LG; i --;){
		if(rank[i][x] == rank[i][y]){
			x += 1 << i;
			y += 1 << i;
			ans |= 1 << i;
		}
	}
	return ans;
}

int get_par(int x){
	if(par[x] == x) return x;
	return par[x] = get_par(par[x]);
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> s;
	n = SZ(s);
	for(int j = 0; j < n; ++j) sz[j] = 1, par[j] = j, rank[0][j] = s[j];
	for(int i = 1; i < LG; ++i){
		for(int j = 0; j < n; ++j)
			if(j + (1 << (i - 1)) < n)
				t[j] = {{rank[i - 1][j], rank[i - 1][j + (1 << (i - 1))]}, j};
			else
				t[j] = {{rank[i - 1][j], -1}, j};
		sort(t, t + n);
		rank[i][t[0].Y] =  1;
		for(int j = 1; j < n; ++j){
			if(t[j - 1].X == t[j].X)
				rank[i][t[j].Y] = rank[i][t[j - 1].Y];
			else
				rank[i][t[j].Y] = j + 1;
		}
	}
	for(int i = 0; i < n - 1; ++i)
		 l[i] = {lcp(t[i].Y, t[i + 1].Y), i};
	sort(l, l + n - 1);
	reverse(l, l + n - 1);
	ll ans = n * (n + 1) / 2;
	for(int i = 0; i < n - 1; ++i){
		int x = l[i].Y, y = l[i].Y + 1;
		x = get_par(x);
		y = get_par(y);
		ans += l[i].X * sz[x] * sz[y];
		par[y] = x;
		sz[x] += sz[y];
	}
	cout << ans << "\n";
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <deque>
#include <queue>
#include <ctime>
#include <iomanip>
#include <cstring>

#define SZ(x) ((int)x.size())
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define rank def1

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;

const int N = 100 * 1000 + 100;
const int LG = 20;

int n;
string s;
int rank[LG][N];
piii t[N];
pii l[N];
int par[N];
int sz[N];

int lcp(int x, int y){
	if(x == y) return n - x;
	int ans = 0;
	for(int i = LG; i --;){
		if(rank[i][x] == rank[i][y]){
			x += 1 << i;
			y += 1 << i;
			ans |= 1 << i;
		}
	}
	return ans;
}

int get_par(int x){
	if(par[x] == x) return x;
	return par[x] = get_par(par[x]);
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> s;
	n = SZ(s);
	for(int j = 0; j < n; ++j) sz[j] = 1, par[j] = j, rank[0][j] = s[j];
	for(int i = 1; i < LG; ++i){
		for(int j = 0; j < n; ++j)
			if(j + (1 << (i - 1)) < n)
				t[j] = {{rank[i - 1][j], rank[i - 1][j + (1 << (i - 1))]}, j};
			else
				t[j] = {{rank[i - 1][j], -1}, j};
		sort(t, t + n);
		rank[i][t[0].Y] =  1;
		for(int j = 1; j < n; ++j){
			if(t[j - 1].X == t[j].X)
				rank[i][t[j].Y] = rank[i][t[j - 1].Y];
			else
				rank[i][t[j].Y] = j + 1;
		}
	}
	for(int i = 0; i < n - 1; ++i)
		 l[i] = {lcp(t[i].Y, t[i + 1].Y), i};
	sort(l, l + n - 1);
	reverse(l, l + n - 1);
	ll ans = n * (n + 1) / 2;
	for(int i = 0; i < n - 1; ++i){
		int x = l[i].Y, y = l[i].Y + 1;
		x = get_par(x);
		y = get_par(y);
		ans += l[i].X * sz[x] * sz[y];
		par[y] = x;
		sz[x] += sz[y];
	}
	cout << ans << "\n";
	return 0;
}


//2019.2.21 by ljz
#include<bits/stdc++.h>
using namespace std;
#define res register int
#define LL long long
#define inf 0x3f3f3f3f
#define eps 1e-10
inline int read() {
    res s=0;
    bool w=0;
    char ch=getchar();
    while(ch<'0'||ch>'9') {
        if(ch=='-')w=1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
    return w?-s:s;
}
inline void _swap(res &x,res &y) {
    x^=y^=x^=y;
}
inline int _abs(const res &x) {
    return x>0?x:-x;
}
inline int _max(const res &x,const res &y) {
    return x>y?x:y;
}
inline int _min(const res &x,const res &y) {
    return x<y?x:y;
}
const int N=1e5+10;
namespace MAIN {
    int n;
    int a[N];
    LL ans;
    char str[N];
    struct SA{
        int rnk[N],buc[N],sa[N],hei[N],fi[N],se[N],m,str[N];
        inline void get_SA(){
            m=2000;
            for(res i=1;i<=n;i++)buc[fi[i]=str[i]]++;
            for(res i=2;i<=m;i++)buc[i]+=buc[i-1];
            for(res i=n;i;i--)sa[buc[fi[i]]--]=i;
            for(res k=1;k<=n;k<<=1){
                res num=0;
                for(res i=n-k+1;i<=n;i++)se[++num]=i;
                for(res i=1;i<=n;i++)if(sa[i]>k)se[++num]=sa[i]-k;
                memset(buc,0,sizeof(buc));
                for(res i=1;i<=n;i++)buc[fi[i]]++;
                for(res i=2;i<=m;i++)buc[i]+=buc[i-1];
                for(res i=n;i;i--)sa[buc[fi[se[i]]]--]=se[i],se[i]=0;
                swap(fi,se);
                fi[sa[1]]=1;
                num=1;
                for(res i=2;i<=n;i++)fi[sa[i]]=(se[sa[i]]==se[sa[i-1]]&&se[sa[i]+k]==se[sa[i-1]+k])?num:++num;
                if(num==n)break;
                m=num;
            }
        }
        inline void get_hei(){
            res k=0;
            for(res i=1;i<=n;i++)rnk[sa[i]]=i;
            for(res i=1;i<=n;i++){
                if(rnk[i]==1)continue;
                if(k)k--;
                res j=sa[rnk[i]-1];
                while(j+k<=n&&i+k<=n&&str[i+k]==str[j+k])k++;
                hei[rnk[i]]=k;
            }
        }
        int st[N][21];
        inline void PRE(){
            memset(st,inf,sizeof(st));
            for(res i=1;i<=n;i++)st[i][0]=hei[i];
            for(res j=1;j<=20;j++)
                for(res i=1;i+(1<<j)-1<=n;i++)
                    st[i][j]=_min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
        }
        inline int query_min(const res &l,const res &r){
            res k=log2(r-l+1);
            return _min(st[l][k],st[r-(1<<k)+1][k]);
        }
        inline int LCP(const res &l,const res &r){
            return query_min(_min(rnk[l],rnk[r])+1,_max(rnk[l],rnk[r]));
        }
        inline void pre(){
            get_SA(),get_hei(),PRE();
        }
    }lcp;
    inline int getlcp(const res &l,const res &r){
        return lcp.LCP(l,r);
    }
    int st[N],top;
    inline void MAIN(){
        scanf("%s",str+1),n=strlen(str+1),ans=1LL*n*(n+1)/2;
        for(res i=1;i<=n;i++)lcp.str[i]=str[i];
        lcp.pre();
        for(res i=1,sum=0;i<=n+1;i++){
            ans+=sum;
            while(top&&lcp.hei[st[top]]>lcp.hei[i])sum-=1LL*(lcp.hei[st[top]]-lcp.hei[i])*(st[top]-st[top-1]),top--;
            sum+=lcp.hei[st[++top]=i];
        }
        printf("%lld\n",ans);
    }
}
int main() {
    MAIN::MAIN();
    return 0;
}


//2019.2.21 by ljz
#include<bits/stdc++.h>
using namespace std;
#define res register int
#define LL long long
#define inf 0x3f3f3f3f
#define eps 1e-10
inline int read() {
    res s=0;
    bool w=0;
    char ch=getchar();
    while(ch<'0'||ch>'9') {
        if(ch=='-')w=1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
    return w?-s:s;
}
inline void _swap(res &x,res &y) {
    x^=y^=x^=y;
}
inline int _abs(const res &x) {
    return x>0?x:-x;
}
inline int _max(const res &x,const res &y) {
    return x>y?x:y;
}
inline int _min(const res &x,const res &y) {
    return x<y?x:y;
}
const int N=1e5+10;
namespace MAIN {
    int n;
    int a[N];
    LL ans;
    char str[N];
    struct SA{
        int rnk[N],buc[N],sa[N],hei[N],fi[N],se[N],m,str[N];
        inline void get_SA(){
            m=2000;
            for(res i=1;i<=n;i++)buc[fi[i]=str[i]]++;
            for(res i=2;i<=m;i++)buc[i]+=buc[i-1];
            for(res i=n;i;i--)sa[buc[fi[i]]--]=i;
            for(res k=1;k<=n;k<<=1){
                res num=0;
                for(res i=n-k+1;i<=n;i++)se[++num]=i;
                for(res i=1;i<=n;i++)if(sa[i]>k)se[++num]=sa[i]-k;
                memset(buc,0,sizeof(buc));
                for(res i=1;i<=n;i++)buc[fi[i]]++;
                for(res i=2;i<=m;i++)buc[i]+=buc[i-1];
                for(res i=n;i;i--)sa[buc[fi[se[i]]]--]=se[i],se[i]=0;
                swap(fi,se);
                fi[sa[1]]=1;
                num=1;
                for(res i=2;i<=n;i++)fi[sa[i]]=(se[sa[i]]==se[sa[i-1]]&&se[sa[i]+k]==se[sa[i-1]+k])?num:++num;
                if(num==n)break;
                m=num;
            }
        }
        inline void get_hei(){
            res k=0;
            for(res i=1;i<=n;i++)rnk[sa[i]]=i;
            for(res i=1;i<=n;i++){
                if(rnk[i]==1)continue;
                if(k)k--;
                res j=sa[rnk[i]-1];
                while(j+k<=n&&i+k<=n&&str[i+k]==str[j+k])k++;
                hei[rnk[i]]=k;
            }
        }
        int st[N][21];
        inline void PRE(){
            memset(st,inf,sizeof(st));
            for(res i=1;i<=n;i++)st[i][0]=hei[i];
            for(res j=1;j<=20;j++)
                for(res i=1;i+(1<<j)-1<=n;i++)
                    st[i][j]=_min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
        }
        inline int query_min(const res &l,const res &r){
            res k=log2(r-l+1);
            return _min(st[l][k],st[r-(1<<k)+1][k]);
        }
        inline int LCP(const res &l,const res &r){
            return query_min(_min(rnk[l],rnk[r])+1,_max(rnk[l],rnk[r]));
        }
        inline void pre(){
            get_SA(),get_hei(),PRE();
        }
    }lcp;
    inline int getlcp(const res &l,const res &r){
        return lcp.LCP(l,r);
    }
    int st[N],top;
    inline void MAIN(){
        scanf("%s",str+1),n=strlen(str+1),ans=1LL*n*(n+1)/2;
        for(res i=1;i<=n;i++)lcp.str[i]=str[i];
        lcp.pre();
        LL sum=0;
        for(res i=1;i<=n+1;i++){
            ans+=sum;
            while(top&&lcp.hei[st[top]]>lcp.hei[i])sum-=1LL*(lcp.hei[st[top]]-lcp.hei[i])*(st[top]-st[top-1]),top--;
            sum+=lcp.hei[st[++top]=i];
        }
        printf("%lld\n",ans);
    }
}
int main() {
    MAIN::MAIN();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;

#define mp make_pair
#define F first
#define S second

const ll N = 2e5;
const ll LG = 20;
const ll SQ = 320;
const ll INF = 1e16;
const ll MOD = 1e9+7;

ll n;
string s;
ll r[LG][N];
ll suf[N];
pair<pii,ll> p[N];
ll l[N];
vector<pii> st;
ll a[N];

int lcp(int x,int y)
{
	if (x>y) swap(x,y);
	if (x==y) return n-y;
	int res = 0;
	for (int i = LG-1; i>=0; i--)
		if (r[i][x]==r[i][y])
		{
			x += (1<<i);
			y += (1<<i);
			res += (1<<i);
		}
	return res;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> s;
	int n = s.size();
	for (int i = 0; i<n; i++)
		r[0][i] = s[i];
	for (int i = 1; i<LG; i++)
	{
		for (int j = 0; j<n; j++)
			if (j+(1<<(i-1))>=n)
				p[j] = mp(mp(r[i-1][j],-1),j);
			else
				p[j] = mp(mp(r[i-1][j],r[i-1][j+(1<<(i-1))]),j);
		sort(p,p+n);
		r[i][p[0].S] = 1;
		for (int j = 1; j<n; j++)
			if (p[j].F==p[j-1].F)
				r[i][p[j].S] = r[i][p[j-1].S];
			else
				r[i][p[j].S] = j+1;
	}
	for (int i = 0; i<n; i++)
	{
		suf[i] = p[i].S;
	//	cout << suf[i] << ' ';
	}
	for (int i = 1; i<n; i++)
	{
		l[i] = lcp(suf[i-1],suf[i]);
	//	cout << l[i] << '
	}
	st.push_back(mp(-1,0));
	for (ll i = 1; i<n; i++)
	{
		while (st.back().F>=l[i])
			st.pop_back();
		a[i] = i-st.back().S;
		st.push_back(mp(l[i],i));
	}
	st.clear();
	st.push_back(mp(-1,n));
	for (ll i = n-1; i>0; i--)
	{
		while (st.back().F>l[i])
			st.pop_back();
		a[i] *= st.back().S-i;
		st.push_back(mp(l[i],i));
	}
	ll ans = (n+1)*n/2;
	for (int i = 1; i<n; i++)
		ans += l[i]*a[i];
	cout << ans << '\n';

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;

#define mp make_pair
#define F first
#define S second

const ll N = 2e5;
const ll LG = 20;
const ll SQ = 320;
const ll INF = 1e16;
const ll MOD = 1e9+7;

ll n;
string s;
ll r[LG][N];
ll suf[N];
pair<pii,ll> p[N];
ll l[N];
vector<pii> st;
ll a[N];

ll lcp(int x,int y)
{
	if (x>y) swap(x,y);
	if (x==y) return n-y;
	int res = 0;
	for (int i = LG-1; i>=0; i--)
		if (r[i][x]==r[i][y])
		{
			x += (1<<i);
			y += (1<<i);
			res += (1<<i);
		}
	return res;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> s;
	n = s.size();
	for (int i = 0; i<n; i++)
		r[0][i] = s[i];
	for (int i = 1; i<LG; i++)
	{
		for (int j = 0; j<n; j++)
			if (j+(1<<(i-1))>=n)
				p[j] = mp(mp(r[i-1][j],-1),j);
			else
				p[j] = mp(mp(r[i-1][j],r[i-1][j+(1<<(i-1))]),j);
		sort(p,p+n);
		r[i][p[0].S] = 1;
		for (int j = 1; j<n; j++)
			if (p[j].F==p[j-1].F)
				r[i][p[j].S] = r[i][p[j-1].S];
			else
				r[i][p[j].S] = j+1;
	}
	for (int i = 0; i<n; i++)
	{
		suf[i] = p[i].S;
	//	cout << suf[i] << ' ';
	}
	for (int i = 1; i<n; i++)
	{
		l[i] = lcp(suf[i-1],suf[i]);
	//	cout << l[i] << '
	}
	st.push_back(mp(-1,0));
	for (ll i = 1; i<n; i++)
	{
		while (st.back().F>=l[i])
			st.pop_back();
		a[i] = i-st.back().S;
		st.push_back(mp(l[i],i));
	}
	st.clear();
	st.push_back(mp(-1,n));
	for (ll i = n-1; i>0; i--)
	{
		while (st.back().F>l[i])
			st.pop_back();
		a[i] *= st.back().S-i;
		st.push_back(mp(l[i],i));
	}
	ll ans = (n+1)*n/2;
	for (int i = 1; i<n; i++)
		ans += l[i]*a[i];
	cout << ans << '\n';

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;

#define mp make_pair
#define F first
#define S second

const ll N = 2e5;
const ll LG = 20;
const ll SQ = 320;
const ll INF = 1e16;
const ll MOD = 1e9+7;

ll n;
string s;
ll rank[LG][N];
ll suf[N];
pair<pii,ll> p[N];
ll l[N];
vector<pii> st;
ll a[N];

int lcp(int x,int y)
{
	if (x>y) swap(x,y);
	if (x==y) return n-y;
	int res = 0;
	for (int i = LG-1; i>=0; i--)
		if (rank[i][x]==rank[i][y])
		{
			x += (1<<i);
			y += (1<<i);
			res += (1<<i);
		}
	return res;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> s;
	int n = s.size();
	for (int i = 0; i<n; i++)
		rank[0][i] = s[i];
	for (int i = 1; i<LG; i++)
	{
		for (int j = 0; j<n; j++)
			if (j+(1<<(i-1))>=n)
				p[j] = mp(mp(rank[i-1][j],-1),j);
			else
				p[j] = mp(mp(rank[i-1][j],rank[i-1][j+(1<<(i-1))]),j);
		sort(p,p+n);
		rank[i][p[0].S] = 1;
		for (int j = 1; j<n; j++)
			if (p[j].F==p[j-1].F)
				rank[i][p[j].S] = rank[i][p[j-1].S];
			else
				rank[i][p[j].S] = j+1;
	}
	for (int i = 0; i<n; i++)
	{
		suf[i] = p[i].S;
	//	cout << suf[i] << ' ';
	}
	for (int i = 1; i<n; i++)
	{
		l[i] = lcp(suf[i-1],suf[i]);
	//	cout << l[i] << ' ';
	}
	st.push_back(mp(-1,0));
	for (ll i = 1; i<n; i++)
	{
		while (st.back().F>=l[i])
			st.pop_back();
		a[i] = i-st.back().S;
		st.push_back(mp(l[i],i));
	}
	st.clear();
	st.push_back(mp(-1,n));
	for (ll i = n-1; i>0; i--)
	{
		while (st.back().F>l[i])
			st.pop_back();
		a[i] *= st.back().S-i;
		st.push_back(mp(l[i],i));
	}
	ll ans = (n+1)*n/2;
	for (int i = 1; i<n; i++)
		ans += l[i]*a[i];
	cout << ans << '\n';

	return 0;
}


/*
 * @Author: Simon
 * @Date: 2019-07-23 00:18:54
 * @Last Modified by: Simon
 * @Last Modified time: 2019-07-23 00:34:34
 */
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 300005
int a[maxn];
Int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,k;cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int tmp=0,l=0,ans=0;
    for(int i=1;i<=n;i++){
        if(tmp-k*(ceil(l+1*1.0/m))+a[i]>=0){
            l++;
            tmp+=a[i];
            ans=max(ans,tmp-k*((l+m-1)/m));
        }
        else{
            tmp=0;l=0;
        }
    }
    tmp=0;
    l=0;
    for(int i=n;i>=1;i--){
        if(tmp-k*(ceil(l+1*1.0/m))+a[i]>=0){
            l++;
            tmp+=a[i];
            ans=max(ans,tmp-k*((l+m-1)/m));
        }
        else{
            tmp=0;l=0;
        }
    }
    cout<<ans<<endl;
    cin.get(),cin.get();
    return 0;
}


/*
 * @Author: Simon
 * @Date: 2019-07-23 00:18:54
 * @Last Modified by: Simon
 * @Last Modified time: 2019-07-23 00:21:36
 */
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 300005
int a[maxn];
Int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,k;cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int tmp=0,l=0,ans=0;
    for(int i=1;i<=n;i++){
        if(tmp-k*((l+1+m-1)/m+a[i]>=0){
            l++;
            tmp+=a[i];
            ans=max(ans,tmp-k*((l+m-1)/m));
        }
        else{
            tmp=0;l=0;
        }
    }
    cout<<ans<<endl;
    cin.get(),cin.get();
    return 0;
}


/*
 * @Author: Simon
 * @Date: 2019-07-23 00:18:54
 * @Last Modified by: Simon
 * @Last Modified time: 2019-07-23 01:08:41
 */
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 300005
int a[maxn];
Int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,k;cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int tmp=0,l=0,ans=0;
    for(int i=1;i<=n;i++){
        tmp+=a[i];l++;
        // if(tmp-(l+m-1)/m*k>=0){
        //     ans=max(ans,tmp-(l+m-1)/m*k);
        // }
        // else tmp=0,l=0;
        if(l%m==0) tmp-=k;
        if(tmp<0) tmp=0,l=0;
        if(l%m==0) ans=max(ans,tmp);
        else ans=max(ans,tmp-k);
        // if(tmp-k*(ceil(l*1.0/m))+a[i]>=0){
        //     l++;
        //     tmp+=a[i];
        //     ans=max(ans,(int)(tmp-k*ceil(l*1.0/m)));
        // }
        // else{
        //     tmp=0;l=0;
        // }
    }
    cout<<ans<<endl;
    cin.get(),cin.get();
    return 0;
}


/*
 * @Author: Simon
 * @Date: 2019-07-23 10:30:58
 * @Last Modified by: Simon
 * @Last Modified time: 2019-07-23 10:46:54
 */
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 100005
Int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,k;cin>>n>>m>>k;
    vector<int>a(n+5,0);
    for(int i=1;i<=n;i++) cin>>a[i];
    vector<int>b(a);
    int ans=0;
    for(int i=0;i<m;i++){
        for(int j=1;j<=n;j++) b[j]=a[j]-(j%m==i?k:0);
        int tmp=0;
        for(int j=1;j<=n;j++){
            tmp=max(tmp+b[j],(int)0);
            if(j%m==i) ans=max(ans,tmp);
            else ans=max(ans,tmp-k);
        }
    }
    cout<<ans<<endl;
    cin.get(),cin.get();
    return 0;
}


/*
 * @Author: Simon
 * @Date: 2019-07-23 00:18:54
 * @Last Modified by: Simon
 * @Last Modified time: 2019-07-23 00:37:59
 */
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 300005
int a[maxn];
Int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,k;cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int tmp=0,l=0,ans=0;
    for(int i=1;i<=n;i++){
        if(tmp-k*(ceil(l*1.0/m))+a[i]>=0){
            l++;
            tmp+=a[i];
            ans=max(ans,tmp-k*((l+m-1)/m));
        }
        else{
            tmp=0;l=0;
        }
    }
    tmp=0;
    l=0;
    for(int i=n;i>=1;i--){
        if(tmp-k*(ceil(l*1.0/m))+a[i]>=0){
            l++;
            tmp+=a[i];
            ans=max(ans,tmp-k*((l+m-1)/m));
        }
        else{
            tmp=0;l=0;
        }
    }
    cout<<ans<<endl;
    cin.get(),cin.get();
    return 0;
}


/*
 * @Author: Simon
 * @Date: 2019-07-23 00:18:54
 * @Last Modified by: Simon
 * @Last Modified time: 2019-07-23 00:31:58
 */
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 300005
int a[maxn];
Int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,k;cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int tmp=0,l=0,ans=0;
    for(int i=1;i<=n;i++){
        if(tmp-k*(ceil(l*1.0/m))+a[i]>=0){
            l++;
            tmp+=a[i];
            ans=max(ans,tmp-k*((l+m-1)/m));
        }
        else{
            tmp=0;l=0;
        }
    }
    cout<<ans<<endl;
    cin.get(),cin.get();
    return 0;
}


/*
 * @Author: Simon
 * @Date: 2019-07-23 00:18:54
 * @Last Modified by: Simon
 * @Last Modified time: 2019-07-23 00:34:34
 */
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 300005
int a[maxn];
Int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,k;cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int tmp=0,l=0,ans=0;
    for(int i=1;i<=n;i++){
        if(tmp-k*(ceil(l+1*1.0/m))+a[i]>=0){
            l++;
            tmp+=a[i];
            ans=max(ans,tmp-k*((l+m-1)/m));
        }
        else{
            tmp=0;l=0;
        }
    }
    tmp=0;
    for(int i=n;i>=1;i--){
        if(tmp-k*(ceil(l+1*1.0/m))+a[i]>=0){
            l++;
            tmp+=a[i];
            ans=max(ans,tmp-k*((l+m-1)/m));
        }
        else{
            tmp=0;l=0;
        }
    }
    cout<<ans<<endl;
    cin.get(),cin.get();
    return 0;
}


/*
 * @Author: Simon
 * @Date: 2019-07-23 00:18:54
 * @Last Modified by: Simon
 * @Last Modified time: 2019-07-23 00:24:39
 */
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 300005
int a[maxn];
Int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,k;cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int tmp=0,l=0,ans=0;
    for(int i=1;i<=n;i++){
        if(tmp-k*((l+m-1)/m)+a[i]>=0){
            l++;
            tmp+=a[i];
            ans=max(ans,tmp-k*((l+m-1)/m));
        }
        else{
            tmp=0;l=0;
        }
    }
    cout<<ans<<endl;
    cin.get(),cin.get();
    return 0;
}


#include <bits/stdc++.h>

#define int long long
typedef long long ll;
using namespace std;
const int N = 5e5 + 10;
int n, m, k, a[N];
int pre[N];

struct mmin {
    int n = 1e18, cnt;

    void init(int ns) {
        n = ns, cnt = -1;
    }

    void next() {
        cnt++;
        cnt %= m;
        if (cnt == 0)n += k;
    }
} mn[1000];

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)cin >> a[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)pre[i] = pre[i - 1] + a[i];
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            mn[j].next();
            ans = max(ans, pre[i] - mn[j].n);
        }
        int t1 = mn[m].n;
        int t2 = mn[m - 1].n;
        for (int j = m; j >= 0; j--) {
            mn[j] = mn[j - 1];
        }
        mn[0].init(min(pre[i], min(t1, t2)));
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long
typedef long long ll;
using namespace std;
const int N = 5e5 + 10;
int n, m, k, a[N];
int pre[N];

struct mmin {
    int n = 1e18, cnt;

    void init(int ns) {
        n = ns, cnt = -1;
    }

    void next() {
        cnt++;
        cnt %= m;
        if (cnt == 0)n += k;
    }
} mn[1000];

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)cin >> a[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)pre[i] = pre[i - 1] + a[i];
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            mn[j].next();
            ans = max(ans, pre[i] - mn[j].n);
        }
        int t2 = mn[m - 1].n;
        for (int j = m; j >= 0; j--) {
            mn[j] = mn[j - 1];
        }
        mn[0].init(min(pre[i], t2));
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long
typedef long long ll;
using namespace std;
const int N = 5e5 + 10;
int n, m, k, a[N];
int pre[N];

struct mmin {
    int n = 1e18, cnt;

    void init(int ns) {
        n = ns, cnt = -1;
    }

    void next() {
        cnt++;
        cnt %= m;
        if (cnt == 0)n += k;
    }
} mn[1000];

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)cin >> a[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)pre[i] = pre[i - 1] + a[i];
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            mn[j].next();
            ans = max(ans, pre[i] - mn[j].n);
        }
        int t2 = mn[m - 1].n;
        for (int j = m; j >= 1; j--) {
            mn[j] = mn[j - 1];
        }
        mn[0].init(min(pre[i], t2));
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long
typedef long long ll;
using namespace std;
const int N = 5e5 + 10;
int n, m, k, a[N];
int pre[N];

struct mmin {
    int n = 1e18, cnt;

    void init(int ns) {
        n = ns, cnt = -1;
    }

    void next() {
        cnt++;
        cnt %= m;
        if (cnt == 0)n += k;
    }
} mn[1000];

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)cin >> a[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)pre[i] = pre[i - 1] + a[i];
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m - 1; j++) {
            mn[j].next();
            ans = max(ans, pre[i] - mn[j].n);
        }
        int t = mn[m].n;
        for (int j = m; j >= 0; j--) {
            mn[j] = mn[j - 1];
        }
        mn[0].init(min(pre[i], t));
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long
typedef long long ll;
using namespace std;
const int N = 5e5 + 10;
int n, m, k, a[N];
int pre[N];

struct mmin {
    int n, cnt;

    void init(int ns) {
        n = ns, cnt = -1;
    }

    void next() {
        cnt++;
        if (cnt % m == 0)n += k;
    }
};

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)cin >> a[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)pre[i] = pre[i - 1] + a[i];
    mmin mn1, mn2;
    mn1.init(pre[1]);
    mn2.init(pre[1]);
    for (int i = 2; i <= n; i++) {
        mn1.next();
        mn2.next();
        ans = max(ans, pre[i] - mn1.n);
        ans = max(ans, pre[i] - mn2.n);
        if ((mn1.cnt + 1) % m == 0) {
            if (pre[i] < mn1.n) {
                mn2 = mn1;
                mn1.init(pre[i]);
            }
        } else {
            if (pre[i] + k < mn1.n) {
                mn2 = mn1;
                mn1.init(pre[i]);
            }
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long
typedef long long ll;
using namespace std;
const int N = 5e5 + 10;
int n, m, k, a[N];
int pre[N];

struct mmin {
    int n = 1e18, cnt = 0;

    void init(int ns) { n = ns, cnt = 0; }

    void next() {
        cnt++;
        cnt %= m;
        if (cnt == 0)n += k;
    }
} mn[1000];

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m >> k;
    n++;
    a[0] = 0;
    for (int i = 2; i <= n; i++)cin >> a[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)pre[i] = pre[i - 1] + a[i];
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m - 1; j++) ans = max(ans, pre[i] - mn[j].n);
        for (int j = m; j >= 1; j--) {
            mn[j - 1].next();
            mn[j] = mn[j - 1];
        }
        mn[0].init(min(mn[m].n, pre[i] + k));
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long
typedef long long ll;
using namespace std;
const int N = 5e5 + 10;
int n, m, k, a[N];
int pre[N];

struct mmin {
    int n = 1e18, cnt = 0;

    void init(int ns) { n = ns, cnt = 0; }

    void next() {
        cnt++;
        cnt %= m;
        if (cnt == 0)n += k;
    }
} mn[1000];

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)cin >> a[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)pre[i] = pre[i - 1] + a[i];
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m - 1; j++) ans = max(ans, pre[i] - mn[j].n);
        for (int j = m; j >= 1; j--) {
            mn[j - 1].next();
            mn[j] = mn[j - 1];
        }
        mn[0].init(min(mn[m].n, pre[i] + k));
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long
typedef long long ll;
using namespace std;
const int N = 5e5 + 10;
int n, m, k, a[N];
int pre[N];

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)cin >> a[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)pre[i] = pre[i - 1] + a[i];
//    for (int i = 1; i <= n; i++)cout << pre[i] << " ";
//    cout << endl;
    int mn = pre[1] + k;
    int cnt = 0;
    bool f = 0;
    for (int i = 2; i <= n; i++) {
        if (cnt % m == 0 && f)mn += k;
        f = 1;
//        cout << i << " " << pre[i] << " " << mn << endl;
        ans = max(ans, pre[i] - mn);
        if (pre[i] + k < mn) {
            mn = pre[i] + k;
            cnt = 0;
            f = 0;
        } else cnt++;
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long
typedef long long ll;
using namespace std;
const int N = 5e5 + 10;
int n, m, k, a[N];
int pre[N];

struct mmin {
    int n = 1e18, cnt = -1;

    void init(int ns) { n = ns, cnt = -1; }

    void next() {
        cnt++;
        cnt %= m;
        if (cnt == 0)n += k;
    }
} mn[1000];

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)cin >> a[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)pre[i] = pre[i - 1] + a[i];
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m - 1; j++) ans = max(ans, pre[i] - mn[j].n);
        for (int j = m; j >= 1; j--) {
            mn[j - 1].next();
            mn[j] = mn[j - 1];
        }
        mn[0].init(min(pre[i], mn[m].n));
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long
typedef long long ll;
using namespace std;
const int N = 5e5 + 10;
int n, m, k, a[N];
int pre[N];

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)cin >> a[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)pre[i] = pre[i - 1] + a[i];
    int mn = pre[0];
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (cnt % m == 0)mn += k;
        if (pre[i] < mn) {
            mn = pre[i];
            cnt = 0;
        } else cnt++;
        ans = max(ans, pre[i] - mn);
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long
typedef long long ll;
using namespace std;
const int N = 5e5 + 10;
int n, m, k, a[N];
int pre[N];

struct mmin {
    int n = 1e18, cnt = -1;

    void init(int ns) {
        n = ns, cnt = -1;
    }

    void next() {
        cnt++;
        cnt %= m;
        if (cnt == 0)n += k;
    }
} mn[1000];

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)cin >> a[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)pre[i] = pre[i - 1] + a[i];
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            mn[j].next();
            ans = max(ans, pre[i] - mn[j].n);
        }
        int t2 = mn[m - 1].n;
        for (int j = m; j >= 1; j--) {
            mn[j] = mn[j - 1];
        }
        mn[0].init(min(pre[i], t2));
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#include <tuple>
#define print(cont) for (const auto& elem: cont) cout<<elem<<" "; cout<<endl;
#define read(cont) for(auto & a:cont) cin>>a;
#define foreach(elem, cont) for (const auto& elem: cont)
#define forin(i, n) for (int i = 0; i < n; ++i)
#define foriin(i, n, init) for (int i = init; i < n; ++i)
#define ll long long
#define pii pair<int, int>
#define INF INT_MAX
#define range(container) begin(container), end(container)
using namespace std;
auto gcd(ll a, ll b) {while (b != 0) {auto t = b;b = a % b;a = t;}return a;}
ll mod = 998244353;
ll modpow(ll a,ll b){ if(b==1) return a%mod; return (a % mod * modpow(a,b-1)% mod)% mod; }

void solve(){
    // ifstream in; in.open("input.txt");
    // ofstream out; out.open("output.txt");
    int n,m,k;
    cin>>n>>m>>k;
    vector<int> arr(n);
    read(arr);
    ll ans = 0;
    int count = 1;
    ll accum = 0;
    forin(i,n){
        accum += arr[i];
        ll multiples = (ll)(ceil(1.*count/m));
        if(accum <= 0 ){
            count = 0;
            accum = 0;
        }else if (count == m+1 && accum - k* multiples<= 0){
            accum -= arr[i- --count];
            --multiples;
        }
        ++count;
        ans = max(ans, accum - k*multiples);

    }
    cout<<ans;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    solve();
    return 0;
}


#include <bits/stdc++.h>
#include <tuple>
#define print(cont) for (const auto& elem: cont) cout<<elem<<" "; cout<<endl;
#define read(cont) for(auto & a:cont) cin>>a;
#define foreach(elem, cont) for (const auto& elem: cont)
#define forin(i, n) for (int i = 0; i < n; ++i)
#define foriin(i, n, init) for (int i = init; i < n; ++i)
#define ll long long
#define pii pair<int, int>
#define INF INT_MAX
#define range(container) begin(container), end(container)
using namespace std;
auto gcd(ll a, ll b) {while (b != 0) {auto t = b;b = a % b;a = t;}return a;}
ll mod = 998244353;
ll modpow(ll a,ll b){ if(b==1) return a%mod; return (a % mod * modpow(a,b-1)% mod)% mod; }

void solve(){
    // ifstream in; in.open("input.txt");
    // ofstream out; out.open("output.txt");
    int n,m,k;
    cin>>n>>m>>k;
    vector<int> arr(n);
    read(arr);
    ll ans = 0, accum = 0;
    int count = 1;
    forin(i,n){
        if(arr[i] + accum - (ll)ceil(1.*count/m)*k <= -k){
            count = 1;
            accum = 0;
        }else{
            accum += arr[i];
            ans = max(ans, accum - (ll)ceil(1.*count/m)*k);
            ++count;
        }
    }
    cout<<ans;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    solve();
    return 0;
}


#include <bits/stdc++.h>
#include <tuple>
#define forin(i, n) for (int i = 0; i < n; ++i)
#define foriin(i, n, init) for (int i = init; i < n; ++i)
#define ll long long
#define pii pair<int, int>
#define INF 5000
using namespace std;
auto gcd(ll a, ll b) {while (b != 0) {auto t = b;b = a % b;a = t;}return a;}
const int N = 500;

void solve() {
    vector<int> A = {2,2,3};
    cout<< accumulate(A.begin(), A.end(),0,[](long long x,long long y){return x+y*y;});
}

void read() {
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    read();
    solve();
    return 0;
}


#include <bits/stdc++.h>
#include <tuple>
#define print(cont) for (const auto& elem: cont) cout<<elem<<" "; cout<<endl;
#define read(cont) for(auto & a:cont) cin>>a;
#define foreach(elem, cont) for (const auto& elem: cont)
#define forin(i, n) for (int i = 0; i < n; ++i)
#define foriin(i, n, init) for (int i = init; i < n; ++i)
#define ll long long
#define pii pair<int, int>
#define INF INT_MAX
#define range(container) begin(container), end(container)
using namespace std;
auto gcd(ll a, ll b) {while (b != 0) {auto t = b;b = a % b;a = t;}return a;}
ll mod = 998244353;
ll modpow(ll a,ll b){ if(b==1) return a%mod; return (a % mod * modpow(a,b-1)% mod)% mod; }

void solve(){
    // ifstream in; in.open("input.txt");
    // ofstream out; out.open("output.txt");
    int n,m,k;
    cin>>n>>m>>k;
    vector<ll> arr(n);
    vector<ll> ans(n, 0), presum(n+1,0);
    read(arr);
    partial_sum(range(arr), next(begin(presum)));
    print(presum)
    forin(i,n){
        for (int j = i; j > i-m && j>= 0; --j)  {
            ans[i] = max(ans[i], presum[i+1] - presum[j] - k);
        }
        if(i >= m){
            ans[i] = max(ans[i],ans[i-m] + presum[i+1] - presum[i+1-m] - k);
        }
    }

    cout<<*max_element(range(ans));
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    solve();
    return 0;
}


#include <bits/stdc++.h>
#include <tuple>
#define print(cont) for (const auto& elem: cont) cout<<elem<<" "; cout<<endl;
#define read(cont) for(auto & a:cont) cin>>a;
#define foreach(elem, cont) for (const auto& elem: cont)
#define forin(i, n) for (int i = 0; i < n; ++i)
#define foriin(i, n, init) for (int i = init; i < n; ++i)
#define ll long long
#define pii pair<int, int>
#define INF INT_MAX
#define range(container) begin(container), end(container)
using namespace std;
auto gcd(ll a, ll b) {while (b != 0) {auto t = b;b = a % b;a = t;}return a;}
ll mod = 998244353;
ll modpow(ll a,ll b){ if(b==1) return a%mod; return (a % mod * modpow(a,b-1)% mod)% mod; }

void solve(){
    // ifstream in; in.open("input.txt");
    // ofstream out; out.open("output.txt");
    int n,m,k;
    cin>>n>>m>>k;
    vector<int> arr(n);
    read(arr);
    ll ans = 0;
    int count = 1;
    ll accum = 0;
    forin(i,n){
        if(arr[i] + accum - k*(count/m + (count%m > 0)) <= 0){
            count = 1;
            accum = 0;
        }else{
            accum += arr[i];
            ans = max(ans, accum - k*int(ceil(1.*count/m)));
            ++count;
        }
    }
    cout<<ans;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    solve();
    return 0;
}


#include <bits/stdc++.h>
#include <tuple>
#define print(cont) for (const auto& elem: cont) cout<<elem<<" "; cout<<endl;
#define read(cont) for(auto & a:cont) cin>>a;
#define foreach(elem, cont) for (const auto& elem: cont)
#define forin(i, n) for (int i = 0; i < n; ++i)
#define foriin(i, n, init) for (int i = init; i < n; ++i)
#define ll long long
#define pii pair<int, int>
#define INF INT_MAX
#define range(container) begin(container), end(container)
using namespace std;
auto gcd(ll a, ll b) {while (b != 0) {auto t = b;b = a % b;a = t;}return a;}
ll mod = 998244353;
ll modpow(ll a,ll b){ if(b==1) return a%mod; return (a % mod * modpow(a,b-1)% mod)% mod; }

void solve(){
    // ifstream in; in.open("input.txt");
    // ofstream out; out.open("output.txt");
    int n,m,k;
    cin>>n>>m>>k;
    vector<int> arr(n);
    read(arr);
    ll ans = 0;
    int count = 1;
    ll accum = 0;
    forin(i,n){
        accum += arr[i];
        int multiples = (ll)(ceil(1.*count/m));
        if(accum <= 0 ){
            count = 0;
            accum = 0;
        }else if (count == m+1 && accum - k* multiples<= 0){
            accum -= arr[i- --count];
            --multiples;
        }
        ++count;
        ans = max(ans, accum - k*multiples);

    }
    cout<<ans;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    solve();
    return 0;
}


#include <bits/stdc++.h>
#include <tuple>
#define print(cont) for (const auto& elem: cont) cout<<elem<<" "; cout<<endl;
#define read(cont) for(auto & a:cont) cin>>a;
#define foreach(elem, cont) for (const auto& elem: cont)
#define forin(i, n) for (int i = 0; i < n; ++i)
#define foriin(i, n, init) for (int i = init; i < n; ++i)
#define ll long long
#define pii pair<int, int>
#define INF INT_MAX
#define range(container) begin(container), end(container)
using namespace std;
auto gcd(ll a, ll b) {while (b != 0) {auto t = b;b = a % b;a = t;}return a;}
ll mod = 998244353;
ll modpow(ll a,ll b){ if(b==1) return a%mod; return (a % mod * modpow(a,b-1)% mod)% mod; }

void solve(){
    // ifstream in; in.open("input.txt");
    // ofstream out; out.open("output.txt");
    int n,m,k;
    cin>>n>>m>>k;
    vector<int> arr(n);
    read(arr);
    ll ans = 0, accum = 0;
    int count = 1;
    forin(i,n){
        if((count%m == m-1  && arr[i] + accum - (ll)ceil(1.*count/m)*k <=0) || arr[i] + accum <= 0){
            count = 1;
            accum = 0;
        }else{
            accum += arr[i];
            ans = max(ans, accum - (ll)ceil(1.*count/m)*k);
            ++count;
        }
    }
    cout<<ans;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    solve();
    return 0;
}


#include <bits/stdc++.h>
#include <tuple>
#define print(cont) for (const auto& elem: cont) cout<<elem<<" "; cout<<endl;
#define read(cont) for(auto & a:cont) cin>>a;
#define foreach(elem, cont) for (const auto& elem: cont)
#define forin(i, n) for (int i = 0; i < n; ++i)
#define foriin(i, n, init) for (int i = init; i < n; ++i)
#define ll long long
#define pii pair<int, int>
#define INF INT_MAX
#define range(container) begin(container), end(container)
using namespace std;
auto gcd(ll a, ll b) {while (b != 0) {auto t = b;b = a % b;a = t;}return a;}
ll mod = 998244353;
ll modpow(ll a,ll b){ if(b==1) return a%mod; return (a % mod * modpow(a,b-1)% mod)% mod; }

void solve(){
    // ifstream in; in.open("input.txt");
    // ofstream out; out.open("output.txt");
    int n,m,k;
    cin>>n>>m>>k;
    vector<int> arr(n);
    read(arr);
    ll ans = 0;
    int count = 1;
    ll accum = 0;
    forin(i,n){
        accum += arr[i];
        ll multiples = (ceil(1.*count/m));
        if(accum <= 0 ){
            count = 0;
            accum = 0;
        }else if ((count%m == 1 && count > m) && accum - k* multiples<= 0){
            accum -= arr[i- --count];
            --multiples;
        }
        ++count;
        ans = max(ans, accum - k*multiples);

    }
    cout<<ans;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    solve();
    return 0;
}


#include <bits/stdc++.h>
#include <tuple>
#define print(cont) for (const auto& elem: cont) cout<<elem<<" "; cout<<endl;
#define read(cont) for(auto & a:cont) cin>>a;
#define foreach(elem, cont) for (const auto& elem: cont)
#define forin(i, n) for (int i = 0; i < n; ++i)
#define foriin(i, n, init) for (int i = init; i < n; ++i)
#define ll long long
#define pii pair<int, int>
#define INF INT_MAX
#define range(container) begin(container), end(container)
using namespace std;
auto gcd(ll a, ll b) {while (b != 0) {auto t = b;b = a % b;a = t;}return a;}
ll mod = 998244353;
ll modpow(ll a,ll b){ if(b==1) return a%mod; return (a % mod * modpow(a,b-1)% mod)% mod; }

void solve(){
    // ifstream in; in.open("input.txt");
    // ofstream out; out.open("output.txt");
    int n,m,k;
    cin>>n>>m>>k;
    vector<ll> arr(n);
    vector<ll> ans(n, 0), presum(n+1,0);
    read(arr);
    partial_sum(range(arr), next(begin(presum)));
    forin(i,n){
        for (int j = i; j > i-m && j>= 0; --j)  {
            ans[i] = max(ans[i], presum[i+1] - presum[j] - k);
        }
        if(i >= m){
            ans[i] = max(ans[i],ans[i-m] + presum[i+1] - presum[i+1-m] - k);
        }
    }

    cout<<*max_element(range(ans));
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    solve();
    return 0;
}


#include <bits/stdc++.h>
#include <tuple>
#define print(cont) for (const auto& elem: cont) cout<<elem<<" "; cout<<endl;
#define read(cont) for(auto & a:cont) cin>>a;
#define foreach(elem, cont) for (const auto& elem: cont)
#define forin(i, n) for (int i = 0; i < n; ++i)
#define foriin(i, n, init) for (int i = init; i < n; ++i)
#define ll long long
#define pii pair<int, int>
#define INF INT_MAX
#define range(container) begin(container), end(container)
using namespace std;
auto gcd(ll a, ll b) {while (b != 0) {auto t = b;b = a % b;a = t;}return a;}
ll mod = 998244353;
ll modpow(ll a,ll b){ if(b==1) return a%mod; return (a % mod * modpow(a,b-1)% mod)% mod; }

void solve(){
    // ifstream in; in.open("input.txt");
    // ofstream out; out.open("output.txt");
    int n,m,k;
    cin>>n>>m>>k;
    vector<int> arr(n);
    read(arr);
    ll ans = 0;
    int count = 1;
    ll accum = 0;
    forin(i,n){
        if(arr[i] + accum - k*(ll)(ceil(1.*count/m)) <= 0){
            count = 1;
            accum = 0;
        }else{
            accum += arr[i];
            ans = max(ans, accum - k*(ll)(ceil(1.*count/m)));
            ++count;
        }
    }
    cout<<ans;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
#define ss second
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define um unordered_map
#define us unordered_set
#define pll pair<ll,ll>
#define large 100005
const int mod = 1e9+7;
#define PI 3.1415926535
#define ld long double
const int N = 55;
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}

int32_t main() {

    ios_base::sync_with_stdio(0);
	cin.tie(0);
    cout.tie(0);

    int n,m,k;
    cin>>n>>m>>k;

    vector<int> a(n);

    for(int i=0;i<n;i++)
      cin>>a[i];

	vector<ll> dp(n);
   	vector<ll> cad(n);
   	vector<ll> len(n);

	dp[0] = a[0]-k*ceil((ld)1/m);
    cad[0] = a[0];
    len[0] = 1;

    int ans = max(dp[0],0ll);

	for(int i=1;i<n;i++){

	  int temp1 = cad[i-1]+a[i]-k*ceil((ld)(len[i-1]+1)/m);
	  int temp2 = a[i]-k*ceil((ld)1/m);

	  if(temp1>temp2 && temp1>0){
	  	len[i] = len[i-1]+1;
	  	cad[i] = cad[i-1]+a[i];
	  	dp[i] = temp1;
	  	ans = max(ans,dp[i]);
	  }
	  else{
	  	len[i] = 1;
	  	cad[i] = a[i];
	  	dp[i] = temp2;
	  	ans = max(ans,dp[i]);
	  }

	}

	cout<<ans;

    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
#define ss second
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define um unordered_map
#define us unordered_set
#define pll pair<ll,ll>
#define large 100005
const int mod = 1e9+7;
#define PI 3.1415926535
#define ld long double
const int N = 55;
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}

int32_t main() {

    ios_base::sync_with_stdio(0);
	cin.tie(0);
    cout.tie(0);

    int n,m,k;
    cin>>n>>m>>k;

    vector<int> a(n);

    for(int i=0;i<n;i++)
      cin>>a[i];

	vector<ll> dp(n);
   	vector<ll> cad(n);
   	vector<ll> len(n);

	dp[0] = a[0]-k*ceil((ld)1/m);
    cad[0] = a[0];
    len[0] = 1;

    int ans = max(dp[0],0);

	for(int i=1;i<n;i++){

	  int temp1 = cad[i-1]+a[i]-k*ceil((ld)(len[i-1]+1)/m);
	  int temp2 = a[i]-k*ceil((ld)1/m);

	  if(temp1>temp2){
	  	len[i] = len[i-1]+1;
	  	cad[i] = cad[i-1]+a[i];
	  	dp[i] = temp1;
	  	ans = max(ans,dp[i]);
	  }
	  else{
	  	len[i] = 1;
	  	cad[i] = a[i];
	  	dp[i] = temp2;
	  	ans = max(ans,dp[i]);
	  }

	}

	cout<<ans;

    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
#define ss second
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define um unordered_map
#define us unordered_set
#define pll pair<ll,ll>
#define large 100005
const int mod = 1e9+7;
#define PI 3.1415926535
#define ld long double
const int N = 55;
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}

int32_t main() {

    ios_base::sync_with_stdio(0);
	cin.tie(0);
    cout.tie(0);

    int n,m,k;
    cin>>n>>m>>k;

    vector<int> a(n);

    for(int i=0;i<n;i++)
      cin>>a[i];

    int ans = 0;

    vector<int> pre(n);

    pre[0] = a[0];

    for(int i=1;i<n;i++)
      pre[i] = pre[i-1]+a[i];

    vector<int> dp(n);

    for(int i=0;i<m && i<n;i++){
      ans = max(ans,pre[i]-k);
      dp[i] = max(0ll,pre[i]-k);
	}

	for(int j=m;j<m+m-1 && j<n;j++)
	   ans = max(ans,dp[m-1]+pre[j]-pre[m-1]);

    for(int i=m;i<n;i++){

	  dp[i] = max(0ll,dp[i-m]+pre[i]-pre[i-m]-k);
      ans = max(ans,dp[i]);

	  for(int j=i+1;j<i+m && j<n;j++)
      	ans = max(ans,dp[i]+pre[j]-pre[i]-k);

	}

	cout<<ans;

    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
#define ss second
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define um unordered_map
#define us unordered_set
#define pll pair<ll,ll>
#define large 100005
const int mod = 1e9+7;
#define PI 3.1415926535
#define ld long double
const int N = 55;
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}

int32_t main() {

    ios_base::sync_with_stdio(0);
	cin.tie(0);
    cout.tie(0);

    int n,m,k;
    cin>>n>>m>>k;

    vector<int> a(n);

    for(int i=0;i<n;i++)
      cin>>a[i];

    int ans = 0;

    vector<int> pre(n);

    pre[0] = a[0];

    for(int i=1;i<n;i++)
      pre[i] = pre[i-1]+a[i];

    vector<int> dp(n);

    for(int i=0;i<m && i<n;i++){
      ans = max(ans,pre[i]-k);
      dp[i] = max(0ll,pre[i]-k);
	}

	for(int j=m;j<m+m-1 && j<n;j++)
	   ans = max(ans,dp[m-1]+pre[j]-pre[m-1]-k);

    for(int i=m;i<n;i++){

	  dp[i] = max(0ll,dp[i-m]+pre[i]-pre[i-m]-k);
      ans = max(ans,dp[i]);

	  for(int j=i+1;j<i+m && j<n;j++)
      	ans = max(ans,dp[i]+pre[j]-pre[i]-k);

	}

	cout<<ans;

    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
#define ss second
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define um unordered_map
#define us unordered_set
#define pll pair<ll,ll>
#define large 100005
const int mod = 1e9+7;
#define PI 3.1415926535
#define ld long double
const int N = 55;
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}

int32_t main() {

    ios_base::sync_with_stdio(0);
	cin.tie(0);
    cout.tie(0);

    int n,m,k;
    cin>>n>>m>>k;

    vector<int> a(n);

    for(int i=0;i<n;i++)
      cin>>a[i];

	vector<ll> dp(n);
   	vector<ll> cad(n);
   	vector<ll> len(n);

	dp[0] = a[0]-k*ceil((ld)1/m);
    cad[0] = a[0];
    len[0] = 1;

    int ans = (dp[0],0);

	for(int i=1;i<n;i++){

	  int temp1 = cad[i-1]+a[i]-k*ceil((ld)(len[i-1]+1)/m);
	  int temp2 = a[i]-k*ceil((ld)1/m);

	  if(temp1>temp2){
	  	len[i] = len[i-1]+1;
	  	cad[i] = cad[i-1]+a[i];
	  	dp[i] = temp1;
	  	ans = max(ans,dp[i]);
	  }
	  else{
	  	len[i] = 1;
	  	cad[i] = a[i];
	  	dp[i] = temp2;
	  	ans = max(ans,dp[i]);
	  }

	}

	cout<<ans;

    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
#define ss second
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define um unordered_map
#define us unordered_set
#define pll pair<ll,ll>
#define large 100005
const int mod = 1e9+7;
#define PI 3.1415926535
#define ld long double
const int N = 55;
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}

int32_t main() {

    ios_base::sync_with_stdio(0);
	cin.tie(0);
    cout.tie(0);

    int n,m,k;
    cin>>n>>m>>k;

    vector<int> a(n);

    for(int i=0;i<n;i++)
      cin>>a[i];

    int ans = 0;

    vector<int> pre(n);

    pre[0] = a[0];

    for(int i=1;i<n;i++)
      pre[i] = pre[i-1]+a[i];

    vector<int> dp(n);

    for(int i=0;i<m && i<n;i++){
      ans = max(ans,pre[i]-k);
      dp[i] = max(0ll,pre[i]-k);
	}

    for(int i=0;i<n;i++){

      if(i>=m){
	   dp[i] = max(0ll,dp[i-m]+pre[i]-pre[i-m]-k);
       ans = max(ans,dp[i]);
      }

	  for(int j=i+1;j<i+m && j<n;j++)
      	ans = max(ans,dp[i]+pre[j]-pre[i]-k);

	}

	cout<<ans;

    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
#define ss second
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define um unordered_map
#define us unordered_set
#define pll pair<ll,ll>
#define large 100005
const int mod = 1e9+7;
#define PI 3.1415926535
#define ld long double
const int N = 55;
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}

int32_t main() {

    ios_base::sync_with_stdio(0);
	cin.tie(0);
    cout.tie(0);

    int n,m,k;
    cin>>n>>m>>k;

    vector<int> a(n);

    for(int i=0;i<n;i++)
      cin>>a[i];

	vector<ll> dp(n);
   	vector<ll> cad(n);
   	vector<ll> len(n);

	dp[0] = a[0]-k*ceil((ld)1/m);
    cad[0] = a[0];
    len[0] = 1;

    int ans = max(dp[0],0ll);

	for(int i=1;i<n;i++){

	  int temp1 = cad[i-1]+a[i]-k*ceil((ld)(len[i-1]+1)/m);
	  int temp2 = a[i]-k*ceil((ld)1/m);

	  if(temp1>temp2){
	  	len[i] = len[i-1]+1;
	  	cad[i] = cad[i-1]+a[i];
	  	dp[i] = temp1;
	  	ans = max(ans,dp[i]);
	  }
	  else{
	  	len[i] = 1;
	  	cad[i] = a[i];
	  	dp[i] = temp2;
	  	ans = max(ans,dp[i]);
	  }

	}

	cout<<ans;

    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
#define ss second
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define um unordered_map
#define us unordered_set
#define pll pair<ll,ll>
#define large 100005
const int mod = 1e9+7;
#define PI 3.1415926535
#define ld long double
const int N = 55;
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}

int32_t main() {

    ios_base::sync_with_stdio(0);
	cin.tie(0);
    cout.tie(0);

    int n,m,k;
    cin>>n>>m>>k;

    vector<int> a(n);

    for(int i=0;i<n;i++)
      cin>>a[i];

    int ans = 0;

    vector<int> pre(n);

    pre[0] = a[0];

    for(int i=1;i<n;i++)
      pre[i] = pre[i-1]+a[i];

    vector<int> dp(n);

    for(int i=0;i<m;i++){
      ans = max(ans,pre[i]-k);
      dp[i] = max(0ll,pre[i]-k);
	}

	for(int j=m;j<m+m-1 && j<n;j++)
	   ans = max(ans,dp[m-1]+pre[j]-pre[m-1]);

    for(int i=m;i<n;i++){

	  dp[i] = max(0ll,dp[i-m]+pre[i]-pre[i-m]-k);
      ans = max(ans,dp[i]);

	  for(int j=i+1;j<i+m && j<n;j++)
      	ans = max(ans,dp[i]+pre[j]-pre[i]-k);

	}

	cout<<ans;

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))

#define pi (2*acos(0.0))
#define eps (1e-6)

typedef long long Long;

const int imax = 1e9+7;
const Long lmax = 1e18;

vector<Long> v;

int calc()
{
    Long n,m,k,ans = 0,val = 0,cnt = 0,p; cin >> n >> m >> k;
    int i; v.resize(n);
    foo(i,0,v)cin >> v[i];
    foo(i,0,v)
    {
        val += v[i]; cnt++;
        if(val <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
        p = (cnt + m - 1) / m;
        p = val - (k * p);
        ans = max(ans,p);
        if(p <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
    }
    cout << ans;
	return 0;
}

int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
	#ifdef localhost
	freopen("/home/tariqul/input.txt","r",stdin);
	//freopen("E://output.txt","w",stdout);
	#endif

	calc();

	cout << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))

#define pi (2*acos(0.0))
#define eps (1e-6)

typedef long long Long;

const int imax = 1e9+7;
const Long lmax = 1e18;

vector<Long> v;

int calc()
{
    Long n,m,k,ans = 0,val = 0,cnt = 0,p; cin >> n >> m >> k;
    int i; v.resize(n);
    foo(i,0,v)cin >> v[i];
    foo(i,0,v)
    {
        val += v[i]; cnt++;
        if(val <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
        p = (cnt + m - 1) / m;
        p = val - (k * p);
        ans = max(ans,p);
        if(p <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
    }
    reverse(li(v)); val = cnt = 0;
    foo(i,0,v)
    {
        val += v[i]; cnt++;
        if(val <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
        p = (cnt + m - 1) / m;
        p = val - (k * p);
        ans = max(ans,p);
        if(p <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
    }
    foo(i,0,v)
    {
        val += v[i]; cnt++;
        if(val <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
        p = (cnt + m - 1) / m;
        p = val - (k * p);
        ans = max(ans,p);
        if(p < 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
    }
    reverse(li(v)); val = cnt = 0;
    foo(i,0,v)
    {
        val += v[i]; cnt++;
        if(val <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
        p = (cnt + m - 1) / m;
        p = val - (k * p);
        ans = max(ans,p);
        if(p < 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
    }
    cout << ans;
	return 0;
}

int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
	#ifdef localhost
	freopen("/home/tariqul/input.txt","r",stdin);
	//freopen("E://output.txt","w",stdout);
	#endif

	calc();

	cout << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))

#define pi (2*acos(0.0))
#define eps (1e-6)

typedef long long ll;

const ll INF = (ll)1e18;
const int N = 300300;
const int M = 10;
ll ans = 0;
ll n, m;
ll k;

int calc()
{
    ll b[M];
	cin >> n >> m >> k;
	for (int i = 0; i < m; i++)
		b[i] = INF;
	// b[0] = 0;
	ll bal = 0;
	for (int i = 0; i < n; i++) {
		ll x;
		cin >> x;
		bal += x;
		ll z = bal - ((i + m) / m) * k,tz;
		// ll z = bal - (i  / m) * k,tz;
		b[i % m] = min(b[i % m], z);
		ans = max(ans,z);
		for (int r = 0; r < m; r++) {
            tz = z;
			if (r < (i % m) ) tz -= k;
			ans = max(ans, tz - b[r]);
		}
	}
	printf("%lld\n", ans);
	return 0;
}

int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
	#ifdef localhost
	freopen("/home/tariqul/input.txt","r",stdin);
	//freopen("E://output.txt","w",stdout);
	#endif

	calc();

	cout << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))

#define pi (2*acos(0.0))
#define eps (1e-6)

typedef long long ll;

const ll INF = (ll)1e18;
const int N = 300300;
const int M = 10;
ll ans = 0;
int n, m;
ll k;

int calc()
{
    ll b[M];
	scanf("%d%d%lld", &n, &m, &k);
	for (int i = 0; i < m; i++)
		b[i] = INF;
	// b[0] = 0;
	ll bal = 0;
	for (int i = 0; i < n; i++) {
		ll x;
		scanf("%lld", &x);
		bal += x;
		ll z = bal - ((i + m) / m) * k,tz;
		// ll z = bal - (i  / m) * k,tz;
		b[i % m] = min(b[i % m], z);
		ans = max(ans,z);
		for (int r = 0; r < m; r++) {
            tz = z;
			if (r <= (i % m) ) tz -= k;
			ans = max(ans, tz - b[r]);
		}
	}
	printf("%lld\n", ans);
	return 0;
}

int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
	#ifdef localhost
	freopen("/home/tariqul/input.txt","r",stdin);
	//freopen("E://output.txt","w",stdout);
	#endif

	calc();

	cout << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))

#define pi (2*acos(0.0))
#define eps (1e-6)

typedef long long Long;

const int imax = 1e9+7;
const Long lmax = 1e18;

vector<Long> v;

int calc()
{
    Long n,m,k,ans = 0,val = 0,cnt = 0,p; cin >> n >> m >> k;
    int i; v.resize(n);
    foo(i,0,v)cin >> v[i];
    foo(i,0,v)
    {
        val += v[i]; cnt++;
        if(val <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
        p = (cnt + m - 1) / m;
        p = val - (k * p);
        ans = max(ans,p);
        if(p <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
    }
    reverse(li(v)); val = cnt = 0;
    foo(i,0,v)
    {
        val += v[i]; cnt++;
        if(val <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
        p = (cnt + m - 1) / m;
        p = val - (k * p);
        ans = max(ans,p);
        if(p <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
    }
    val = cnt = 0;
    foo(i,0,v)
    {
        val += v[i]; cnt++;
        if(val <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
        p = (cnt + m - 1) / m;
        p = val - (k * p);
        ans = max(ans,p);
        if(p < 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
    }
    reverse(li(v)); val = cnt = 0;
    foo(i,0,v)
    {
        val += v[i]; cnt++;
        if(val <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
        p = (cnt + m - 1) / m;
        p = val - (k * p);
        ans = max(ans,p);
        if(p < 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
    }
    cout << ans;
	return 0;
}

int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
	#ifdef localhost
	freopen("/home/tariqul/input.txt","r",stdin);
	//freopen("E://output.txt","w",stdout);
	#endif

	calc();

	cout << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))

#define pi (2*acos(0.0))
#define eps (1e-6)

typedef long long ll;

const ll INF = (ll)1e18;
const int N = 300300;
const int M = 10;
ll ans = 0;
int n, m;
ll k;

int calc()
{
    ll b[M];
	scanf("%d%d%lld", &n, &m, &k);
	for (int i = 0; i < m; i++)
		b[i] = INF;
	b[0] = 0;
	ll bal = 0;
	for (int i = 0; i < n; i++) {
		ll x;
		scanf("%lld", &x);
		bal += x;
		ll z = bal - ((i + m) / m) * k,tz;
		// ll z = bal - (i  / m) * k,tz;
		b[i % m] = min(b[i % m], z);
		ans = max(ans,z);
		for (int r = 0; r < m; r++) {
            tz = z;
			if (r <= (i % m) ) tz -= k;
			ans = max(ans, tz - b[r]);
		}
	}
	printf("%lld\n", ans);
	return 0;
}

int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
	#ifdef localhost
	freopen("/home/tariqul/input.txt","r",stdin);
	//freopen("E://output.txt","w",stdout);
	#endif

	calc();

	cout << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))

#define pi (2*acos(0.0))
#define eps (1e-6)

typedef long long Long;

const int imax = 1e9+7;
const Long lmax = 1e18;

vector<Long> v;

int calc()
{
    Long n,m,k,ans = 0,val = 0,cnt = 0,p; cin >> n >> m >> k;
    int i; v.resize(n);
    foo(i,0,v)cin >> v[i];
    foo(i,0,v)
    {
        val += v[i]; cnt++;
        if(val <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
        p = (cnt + m - 1) / m;
        p = val - (k * p);
        ans = max(ans,p);
        if(p <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
    }
    reverse(li(v)); val = cnt = 0;
    foo(i,0,v)
    {
        val += v[i]; cnt++;
        if(val <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
        p = (cnt + m - 1) / m;
        p = val - (k * p);
        ans = max(ans,p);
        if(p <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
    }
    cout << ans;
	return 0;
}

int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
	#ifdef localhost
	freopen("/home/tariqul/input.txt","r",stdin);
	//freopen("E://output.txt","w",stdout);
	#endif

	calc();

	cout << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))

#define pi (2*acos(0.0))
#define eps (1e-6)

typedef long long Long;

const int imax = 1e9+7;
const Long lmax = 1e18;

vector<Long> v;

int calc()
{
    Long n,m,k,ans = 0,val = 0,cnt = 0,p; cin >> n >> m >> k;
    int i; v.resize(n);
    foo(i,0,v)cin >> v[i];
    foo(i,0,v)
    {
        val += v[i]; cnt++;
        if(val <= 0)
        {
            val = 0;
            cnt = 0;
            continue;
        }
        p = (cnt + m - 1) / m;
        p = val - (k * p);
        ans = max(ans,p);
    }
    cout << ans;
	return 0;
}

int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
	#ifdef localhost
	freopen("/home/tariqul/input.txt","r",stdin);
	//freopen("E://output.txt","w",stdout);
	#endif

	calc();

	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (int) (n); (i) ++)
using namespace std;

int main() {
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);
        vector<long long> a(n);
        rep(i, n) {
                scanf("%lld", &a[i]);
        }
        rep(i, n - 1) {
                a[i + 1] += a[i];
        }
        vector<vector<int>> pos(n, vector<int> (m, -1));
        rep(l, m) {
                int c = l;
                long long v = -(1e9 + 10);
                long long add = 0;
                int idx = -1;
                for (int i = n - 1; i >= 0; i --) {
                        if (a[i] + add > v) {
                                v = a[i] + add;
                                idx = i;
                        }
                        c ++;
                        if (c % m == 0) {
                                pos[i][l] = idx;
                                add += k;
                        }
                }
        }
        long long ans = 0;
        rep(l, n) {
                rep(i, m) {
                        int r = pos[l][i];
                        if (r == -1) continue;
                        ans = max(ans, a[r] - (l ? a[l - 1] : 0) - (long long) k * ((r - l + 1 + m - 1) / m));
                }
        }
        /*
        long long gutyoku = 0;
        rep(l, n) {
                for (int r = l; r < n; r ++) {
                        gutyoku = max(gutyoku, a[r] - (l ? a[l - 1] : 0) - (long long) k * ((r - l + 1 + m - 1) / m));
                }
        }
        cerr << gutyoku << ' ' << ans << endl;
        if (gutyoku != ans) {
                cerr << "wrong!!!" << endl;
        }
        */
        printf("%lld\n", ans);
        return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (int) (n); (i) ++)
using namespace std;

int main() {
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);
        vector<int> a(n);
        rep(i, n) {
                scanf("%d", &a[i]);
        }
        rep(i, n - 1) {
                a[i + 1] += a[i];
        }
        vector<vector<int>> pos(n, vector<int> (m, -1));
        rep(l, m) {
                int c = l;
                long long v = -(1e9 + 10);
                long long add = 0;
                int idx = -1;
                for (int i = n - 1; i >= 0; i --) {
                        if (a[i] + add > v) {
                                v = a[i] + add;
                                idx = i;
                        }
                        c ++;
                        if (c % m == 0) {
                                pos[i][l] = idx;
                                add += k;
                        }
                }
        }
        long long ans = -1e9 - 10;
        rep(l, n) {
                rep(i, m) {
                        int r = pos[l][i];
                        if (r == -1) continue;
                        ans = max(ans, a[r] - (l ? a[l - 1] : 0) - (long long) k * ((r - l + 1 + m - 1) / m));
                }
        }
        printf("%lld\n", max(ans, 0ll));
        return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (int) (n); (i) ++)
using namespace std;

int main() {
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);
        vector<long long> a(n);
        rep(i, n) {
                scanf("%lld", &a[i]);
        }
        rep(i, n - 1) {
                a[i + 1] += a[i];
        }
        vector<vector<int>> pos(n, vector<int> (m, -1));
        rep(l, m) {
                int c = l;
                long long v = -(1e18 + 10);
                long long add = 0;
                int idx = -1;
                for (int i = n - 1; i >= 0; i --) {
                        if (a[i] + add > v) {
                                v = a[i] + add;
                                idx = i;
                        }
                        c ++;
                        if (c % m == 0) {
                                pos[i][l] = idx;
                                add += k;
                        }
                }
        }
        long long ans = 0;
        rep(l, n) {
                rep(i, m) {
                        int r = pos[l][i];
                        if (r == -1) continue;
                        ans = max(ans, a[r] - (l ? a[l - 1] : 0) - (long long) k * ((r - l + 1 + m - 1) / m));
                }
        }
        /*
        long long gutyoku = 0;
        rep(l, n) {
                for (int r = l; r < n; r ++) {
                        gutyoku = max(gutyoku, a[r] - (l ? a[l - 1] : 0) - (long long) k * ((r - l + 1 + m - 1) / m));
                }
        }
        cerr << gutyoku << ' ' << ans << endl;
        if (gutyoku != ans) {
                cerr << "wrong!!!" << endl;
        }
        */
        printf("%lld\n", ans);
        return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (int) (n); (i) ++)
using namespace std;

int main() {
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);
        vector<int> a(n);
        rep(i, n) {
                scanf("%d", &a[i]);
        }
        rep(i, n - 1) {
                a[i + 1] += a[i];
        }
        // rep(i, n) cerr << a[i] << ' '; cerr << endl;
        vector<vector<int>> pos(n, vector<int> (m, -1));
        rep(l, m) {
                int c = m - l;
                long long v = -(1e9 + 10);
                long long add = 0;
                int idx = -1;
                for (int i = n - 1; i >= 0; i --) {
                        if (a[i] + add > v) {
                                v = a[i] + add;
                                idx = i;
                        }
                        c ++;
                        if (c % m == 0) {
                                pos[i][l] = idx;
                                add += k;
                        }
                }
        }
        long long ans = -1e9 - 10;
        rep(l, n) {
                rep(i, m) {
                        int r = pos[l][i];
                        if (r == -1) continue;
                        // cerr << "l, r = " << l << ' ' << r << endl;
                        // cerr << "acc = " << a[r] - (l ? a[l - 1] : 0) << endl;
                        // cerr << "d = " << k * (r - l + 1) / m << endl;
                        ans = max(ans, a[r] - (l ? a[l - 1] : 0) - (long long) k * ((r - l + 1 + m - 1) / m));
                }
        }
        printf("%lld\n", max(ans, 0ll));
        return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (int) (n); (i) ++)
using namespace std;

int main() {
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);
        vector<int> a(n);
        rep(i, n) {
                scanf("%d", &a[i]);
        }
        rep(i, n - 1) {
                a[i + 1] += a[i];
        }
        // rep(i, n) cerr << a[i] << ' '; cerr << endl;
        vector<vector<int>> pos(n, vector<int> (m, -1));
        rep(l, m) {
                int c = m - l;
                long long v = -(1e9 + 10);
                long long add = 0;
                int idx = -1;
                for (int i = n - 1; i >= 0; i --) {
                        if (a[i] + add >= v) {
                                v = a[i] + add;
                                idx = i;
                        }
                        c ++;
                        if (c % m == 0) {
                                pos[i][l] = idx;
                                add += k;
                        }
                }
        }
        long long ans = -1e9 - 10;
        rep(l, n) {
                rep(i, m) {
                        int r = pos[l][i];
                        if (r == -1) continue;
                        // cerr << "l, r = " << l << ' ' << r << endl;
                        // cerr << "acc = " << a[r] - (l ? a[l - 1] : 0) << endl;
                        // cerr << "d = " << k * (r - l + 1) / m << endl;
                        ans = max(ans, a[r] - (l ? a[l - 1] : 0) - (long long) k * ((r - l + 1 + m - 1) / m));
                }
        }
        printf("%lld\n", max(ans, 0ll));
        return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (int) (n); (i) ++)
using namespace std;

int main() {
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);
        vector<int> a(n);
        rep(i, n) {
                scanf("%d", &a[i]);
        }
        rep(i, n - 1) {
                a[i + 1] += a[i];
        }
        // rep(i, n) cerr << a[i] << ' '; cerr << endl;
        vector<vector<int>> pos(n, vector<int> (m));
        rep(l, m) {
                int c = m - l;
                long long v = -(1e9 + 10);
                long long add = 0;
                int idx = -1;
                for (int i = n - 1; i >= 0; i --) {
                        if (a[i] + add > v) {
                                v = a[i] + add;
                                idx = i;
                        }
                        pos[i][l] = idx;
                        c ++;
                        if (c % m == 0) {
                                add += k;
                        }
                }
        }
        long long ans = -1e9 - 10;
        rep(l, n) {
                rep(i, m) {
                        int r = pos[l][i];
                        // cerr << "l, r = " << l << ' ' << r << endl;
                        // cerr << "acc = " << a[r] - (l ? a[l - 1] : 0) << endl;
                        // cerr << "d = " << k * (r - l + 1) / m << endl;
                        ans = max(ans, a[r] - (l ? a[l - 1] : 0) - (long long) k * ((r - l + 1 + m - 1) / m));
                }
        }
        printf("%lld\n", max(ans, 0ll));
        return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (int) (n); (i) ++)
using namespace std;

int main() {
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);
        vector<int> a(n);
        rep(i, n) {
                scanf("%d", &a[i]);
        }
        rep(i, n - 1) {
                a[i + 1] += a[i];
        }
        rep(i, n) cerr << a[i] << ' '; cerr << endl;
        vector<vector<int>> pos(n, vector<int> (m));
        rep(l, m) {
                int c = m - l;
                long long v = -(1e9 + 10);
                long long add = 0;
                int idx = -1;
                for (int i = n - 1; i >= 0; i --) {
                        if (a[i] + add > v) {
                                v = a[i] + add;
                                idx = i;
                        }
                        pos[i][l] = idx;
                        c ++;
                        if (c % m == 0) {
                                add += k;
                        }
                }
        }
        long long ans = -1e9 - 10;
        rep(l, n) {
                rep(i, m) {
                        int r = pos[l][i];
                        cerr << "l, r = " << l << ' ' << r << endl;
                        cerr << "acc = " << a[r] - (l ? a[l - 1] : 0) << endl;
                        cerr << "d = " << k * (r - l + 1) / m << endl;
                        ans = max(ans, a[r] - (l ? a[l - 1] : 0) - (long long) k * ((r - l + 1 + m - 1) / m));
                }
        }
        printf("%lld\n", max(ans, 0ll));
        return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include<stack>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define per(i,b,a) for(int i = (b) - 1; i >= (a); --i)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll << (x))
#define mst(a,b) memset(a,b,sizeof(a));
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int inf=0x3f3f3f3f;
const int maxn=3e5+7;
ll dp[maxn];
ll a[maxn];
ll sum[maxn];
int main()
{
 	ios::sync_with_stdio(false);
	std::cin.tie(0);
	ll n,m,k;
	cin>>n>>m>>k;
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
	}
	dp[1]=a[1]-k;
	ll l=1;
	ll ans=0;
	ans=max(dp[1],ans);
	for(ll i=2;i<=n;i++)
	{
		if(a[i]-k>=(sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1))||((sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1))<ans))
		{
			l=i;
			dp[i]=a[i]-k;
		}
		else
		{
			dp[i]=sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1);
		}
		ans=max(ans,dp[i]);

	}
	cout<<ans<<endl;
	return 0;/*6 5 10
				1 0 0 0 5 6*/
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include<stack>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define per(i,b,a) for(int i = (b) - 1; i >= (a); --i)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll << (x))
#define mst(a,b) memset(a,b,sizeof(a));
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int inf=0x3f3f3f3f;
const int maxn=3e5+7;
ll dp[maxn][11];
ll a[maxn];
int main()
{
 	ios::sync_with_stdio(false);
	std::cin.tie(0);
	ll n,m,k;
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(int i=1;i<=m;i++)
	{
		dp[0][i]=-1e18;
	}
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(j==1)
			{
				dp[i][j]=max(dp[i-1][m]+a[i]-k,a[i]-k);
			}
			else
			{
				dp[i][j]=dp[i-1][j-1]+a[i];
			}
			ans=max(ans,dp[i][j]);
		}
	}
	cout<<ans<<endl;
	return 0;

}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include<stack>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define per(i,b,a) for(int i = (b) - 1; i >= (a); --i)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll << (x))
#define mst(a,b) memset(a,b,sizeof(a));
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int inf=0x3f3f3f3f;
const int maxn=3e5+7;
ll dp[maxn];
ll a[maxn];
ll sum[maxn];
ll sum2[maxn];
ll dp2[maxn];
ll b[maxn];
int main()
{
 	ios::sync_with_stdio(false);
	std::cin.tie(0);
	ll n,m,k;
	cin>>n>>m>>k;
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
	}
	dp[1]=a[1]-k;
	ll l=1;
	ll ans=0;
	ans=max(dp[1],ans);
	for(ll i=2;i<=n;i++)
	{
		if(a[i]-k>=(sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1)))
		{
			l=i;
			dp[i]=a[i]-k;
		}
		else
		{
			dp[i]=sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1);
		}
		ans=max(ans,dp[i]);
	}
	int cnt=1;
	for(int i=n;i>=1;i--)
	{
		b[cnt++]=a[i];
	}
	for(int i=1;i<=n;i++)
	{
		sum2[i]=sum2[i-1]+b[i];
	}
	l=1;
	dp2[1]=b[1]-k;
	for(ll i=2;i<=n;i++)
	{
		if(b[i]-k>=(sum2[i]-sum2[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1)))
		{
			l=i;
			dp2[i]=b[i]-k;
		}
		else
		{
			dp2[i]=sum2[i]-sum2[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1);
		}
		ans=max(ans,dp2[i]);
	}
	cout<<ans<<endl;
	return 0;/*6 5 10
				1 0 0 0 5 6*/
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include<stack>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define per(i,b,a) for(int i = (b) - 1; i >= (a); --i)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll << (x))
#define mst(a,b) memset(a,b,sizeof(a));
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int inf=0x3f3f3f3f;
const int maxn=3e5+7;
ll dp[maxn];
ll a[maxn];
ll sum[maxn];
int main()
{
 	ios::sync_with_stdio(false);
	std::cin.tie(0);
	ll n,m,k;
	cin>>n>>m>>k;
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
	}
	dp[1]=a[1]-k;
	ll l=1;
	ll ans=0;
	ans=max(dp[1],ans);
	for(ll i=2;i<=n;i++)
	{
		if(a[i]-k>(sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1)))
		{
			l=i;
			dp[i]=a[i]-k;
		}
		else
		{
			dp[i]=sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1);
			if(dp[i]<0)
			{
				l=i;
			}
		}
		ans=max(ans,dp[i]);

	}
	cout<<ans<<endl;
	return 0;/*6 5 10
				1 0 0 0 5 6*/
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include<stack>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define per(i,b,a) for(int i = (b) - 1; i >= (a); --i)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll << (x))
#define mst(a,b) memset(a,b,sizeof(a));
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int inf=0x3f3f3f3f;
const int maxn=3e5+7;
ll dp[maxn];
ll a[maxn];
ll sum[maxn];
int main()
{
 	ios::sync_with_stdio(false);
	std::cin.tie(0);
	ll n,m,k;
	cin>>n>>m>>k;
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
	}
	dp[1]=a[1]-k;
	ll l=1;
	ll ans=0;
	ans=max(dp[1],ans);
	for(ll i=2;i<=n;i++)
	{
		if(a[i]-k>=(sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1))||((sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1))<=0&&a[i]-k<=0))
		{
			l=i;
			dp[i]=a[i]-k;
		}
		else
		{
			dp[i]=sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1);
		}
		ans=max(ans,dp[i]);

	}
	cout<<ans<<endl;
	return 0;/*6 5 10
				1 0 0 0 5 6*/
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include<stack>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define per(i,b,a) for(int i = (b) - 1; i >= (a); --i)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll << (x))
#define mst(a,b) memset(a,b,sizeof(a));
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int inf=0x3f3f3f3f;
const int maxn=3e5+7;
ll dp[maxn];
ll a[maxn];
ll sum[maxn];
int main()
{
 	ios::sync_with_stdio(false);
	std::cin.tie(0);
	ll n,m,k;
	cin>>n>>m>>k;
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
	}
	dp[1]=a[1]-k;
	ll l=1;
	ll ans=0;
	ans=max(dp[1],ans);
	for(ll i=2;i<=n;i++)
	{
		if(a[i]-k>=(sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1))||((sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1))<=0))
		{
			l=i;
			dp[i]=a[i]-k;
		}
		else
		{
			dp[i]=sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1);
		}
		ans=max(ans,dp[i]);

	}
	cout<<ans<<endl;
	return 0;/*6 5 10
				1 0 0 0 5 6*/
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include<stack>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define per(i,b,a) for(int i = (b) - 1; i >= (a); --i)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll << (x))
#define mst(a,b) memset(a,b,sizeof(a));
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int inf=0x3f3f3f3f;
const int maxn=3e5+7;
ll dp[maxn];
ll a[maxn];
ll sum[maxn];
int main()
{
 	ios::sync_with_stdio(false);
	std::cin.tie(0);
	ll n,m,k;
	cin>>n>>m>>k;
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
	}
	dp[1]=a[1]-k;
	ll l=1;
	ll ans=0;
	ans=max(dp[1],ans);
	for(ll i=2;i<=n;i++)
	{
		if(a[i]-k>=(sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1)))
		{
			l=i;
			dp[i]=a[i]-k;
		}
		else
		{
			dp[i]=sum[i]-sum[l-1]-k*((i-l+1)%m==0?(i-l+1)/m:(i-l+1)/m+1);
			if(dp[i]<=0)
			{
				l=i;
			}
		}
		ans=max(ans,dp[i]);

	}
	cout<<ans<<endl;
	return 0;/*6 5 10
				1 0 0 0 5 6*/
}


#include<bits/stdc++.h>

using namespace std;

#define sf scanf
#define pf printf
#define ll long long
#define fr(I,M,N) for(I=M;I<=N;I++)
#define fr_(I,M,N) for(I=M;I>=N;I--)
#define re return
#define sfn cin>>n
#define bal pf("bal\n")
#define pb push_back
#define ins insert
#define sz(X) X.size()
#define xx first
#define yy second
#define skip continue
#define memo(X,N) memset(X,N,sizeof(X))
#define all(X) X.begin(),X.end()
#define mp make_pair
#define pi acos(-1)

/*
#define nMX 5000000
#define pMX 500000
int pr[pMX+1],prnum[nMX+1];

int sieve()
{
    int i,j;
    for(i=1;i<=nMX;i++)
        prnum[i]=i+1;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            for(j=2*prnum[i]-1;j<=nMX;j+=prnum[i])
                    prnum[j]=-1;
    j=0;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            pr[++j]=prnum[i];
    re j;
}*/

/*ll m_ncr[10001][10001];
ll ncr(ll i,ll j)
{
    if(j==1) re i;
    if(i==j) re 1;
    if(m_ncr[i][j]!=-1) re m_ncr[i][j];
    re m_ncr[i][j]=ncr(i-1,j) + ncr(i-1,j-1);
}*/

/*ll m_fact[21];
ll fact(ll i)
{
    if(i==1 || i==0) re 1;
    if(m_fact[i]!=-1) re m_fact[i];
    re m_fact[i]=i*fact(i-1);
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    ll int i,j,x,y,n,m,cnt=0,k,g,flg,t;
    cin>>n>>m>>k;
    ll in[n+1],sm[n+1];
    sm[0]=0;
    fr(i,1,n)
    {
        cin>>in[i];
        sm[i]=in[i]+sm[i-1];
    }
    ll a,b,mx=0;
    a=b=1;
    while(a<=n && b<=n)
    {
        y=b-a+1;
        x=y/m;
        if(y%m) x++;

        //cout<<a<<" "<<b<<" "<<sm[b]-sm[a-1]-k*x<<" "<<x<<endl;
        cnt=max(cnt, sm[b]-sm[a-1]-k*x);
        if(a>b)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else if(sm[b]-sm[a-1]<0)
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
        else if(b<n)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
    }
    cout<<cnt;
}