#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;
typedef long long ll;

int n, m, k;
ll a[300002];

int main () {

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m >> k;
	for (int i = 1; i <= n; i ++) {
		cin >> a[i];
		a[i] += a[i-1];
	}

	ll sol = 0;
	int w = 0;
	for (int i = 1; i <= n; i ++) {
		ll tren = a[i];
		tren -= a[w];
		ll minus = (i-w+m-1)/m;
		minus *= k;
		tren -= minus;

		//cout << w << " do " << i << ": " << tren << endl;

		sol = max(sol, tren);
		if (tren < 0) {
			w = i;
		}
	}

	cout << sol;

	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int calculk(int l, int r, int k, int m)
{
  int num = r-l+1;
  int den = m;
  if (num%den == 0)
    return k*(num/den);
  else
    return k*(1 + (num/den));
}

int main()
{
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> tab(n);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    tab[i] = tmp;
  }
  vector<int> precalcul(n);
  for (int i = 0; i < n; i++)
    precalcul[i] = calculk(0, i+1, k, m);
  int maxi = 0;
  int somme = 0;
  int j = 0;
  for (int i = 0; i < n; i++) {
    somme += tab[i];
    int score_tmp = somme - calculk(j, i, k, m);//precalcul[j-i];
    if (score_tmp > maxi) {
      maxi = score_tmp;
      //cout << j << " " << i << endl;
    }
    if (somme <= 0) {
      j = i+1;
      somme = 0;
    }
  }
  cout << maxi << endl;
}


#include <iostream>
#include <vector>

using namespace std;

int calculk(int l, int r, int k, int m)
{
  int num = r-l+1;
  int den = m;
  if (num%den == 0)
    return k*(num/den);
  else
    return k*(1 + (num/den));
}

int main()
{
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> tab(n);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    tab[i] = tmp;
  }
  vector<int> precalcul(n);
  for (int i = 0; i < n; i++)
    precalcul[i] = calculk(0, i+1, k, m);
  int maxi = 0;
  for (int i = 0; i < n; i++) {
    int somme = 0;
    for (int j = i; j < n; j++) {
      somme += tab[j];
      int score_tmp = somme - precalcul[j-i];
      if (score_tmp > maxi)
        maxi = score_tmp;
    }
  }
  cout << maxi << endl;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> tab(n+1, 0);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    tab[i] = tmp;
  }
  vector<int> dp(n, 0);
  int somme = 0;
  int rep = 0;
  for (int i = n-1; i >= 0; i--) {
    somme += tab[i];
    if (i+m <= n) {
      somme -= tab[i+m];
      dp[i] = max(0, somme-k+dp[i+m]);
      rep = max(rep, dp[i]);
    }
  }
  for (int i = 0; i < n; i++) {
    int somme = 0;
    for (int r = 0; r < m; r++) {
      if (i+r < n) {
        somme += tab[i+r];
        int score_tmp = somme - k + dp[i+r+1];
        if (score_tmp > rep)
          rep = score_tmp;
      }
    }
  }
  cout << rep;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> tab(n+1, 0);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    tab[i] = tmp;
  }
  vector<int> dp(n+1, 0);
  int somme = 0;
  int rep = 0;
  for (int i = n-1; i >= 0; i--) {
    somme += tab[i];
    if (i+m <= n) {
      somme -= tab[i+m];
      dp[i] = max(0, somme-k+dp[i+m]);
      rep = max(rep, dp[i]);
    }
  }
  for (int i = 0; i < n; i++) {
    int somme = 0;
    for (int r = 0; r < m; r++) {
      if (i+r < n) {
        somme += tab[i+r];
        int score_tmp = somme - k + dp[i+r+1];
        if (score_tmp > rep)
          rep = score_tmp;
      }
    }
  }
  cout << rep;
}


#include <iostream>
#include <vector>

using namespace std;

int calculk(int l, int r, int k, int m)
{
  int num = r-l+1;
  int den = m;
  if (num%den == 0)
    return k*(num/den);
  else
    return k*(1 + (num/den));
}

int main()
{
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> tab(n);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    tab[i] = tmp;
  }
  int maxi = 0;
  for (int i = 0; i < n; i++) {
    int somme = 0;
    for (int j = i; j < n; j++) {
      somme += tab[j];
      int score_tmp = somme - calculk(i, j, k, m);
      if (score_tmp > maxi)
        maxi = score_tmp;
    }
  }
  cout << maxi << endl;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> tab(n+1, 0);
  for (int i = 0; i < n; i++) {
    long long tmp;
    cin >> tmp;
    tab[i] = tmp;
  }
  vector<long long> dp(n+1, 0);
  long long somme = 0;
  long long rep = 0;
  for (int i = n-1; i >= 0; i--) {
    somme += tab[i];
    if (i+m <= n) {
      somme -= tab[i+m];
      dp[i] = max(0, somme-k+dp[i+m]);
      rep = max(rep, dp[i]);
    }
  }
  for (int i = 0; i < n; i++) {
    long somme = 0;
    for (int r = 0; r < m; r++) {
      if (i+r < n) {
        somme += tab[i+r];
        long score_tmp = somme - k + dp[i+r+1];
        if (score_tmp > rep)
          rep = score_tmp;
      }
    }
  }
  cout << rep;
}


#include <iostream>
#include <vector>

using namespace std;

int calculk(int l, int r, int k, int m)
{
  int num = r-l+1;
  int den = m;
  if (num%den == 0)
    return k*(num/den);
  else
    return k*(1 + (num/den));
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> tab(n);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    tab[i] = tmp;
  }
  vector<int> precalcul(n);
  for (int i = 0; i < n; i++)
    precalcul[i] = calculk(0, i+1, k, m);
  int maxi = 0;
  for (int i = 0; i < n; i++) {
    int somme = 0;
    for (int j = i; j < n; j++) {
      somme += tab[j];
      if (somme > maxi) {
        int score_tmp = somme - precalcul[j-i];
        if (score_tmp > maxi)
          maxi = score_tmp;
      }
    }
  }
  cout << maxi << endl;
}


#include <iostream>
#include <vector>

using namespace std;

int calculk(int l, int r, int k, int m)
{
  int num = r-l+1;
  int den = m;
  if (num%den == 0)
    return k*(num/den);
  else
    return k*(1 + (num/den));
}

int main()
{
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> tab(n);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    tab[i] = tmp;
  }
  vector<int> precalcul(n);
  for (int i = 0; i < n; i++)
    precalcul[i] = calculk(0, i+1, k, m);
  int maxi = 0;
  int somme = 0;
  int j = 0;
  for (int i = 0; i < n; i++) {
    somme += tab[i];
    int score_tmp = somme - calculk(j, i, k, m);//precalcul[j-i];
    if (score_tmp > maxi) {
      maxi = score_tmp;
      //cout << j << " " << i << endl;
    }
    if (score_tmp <= 0) {
      j = i+1;
      somme = 0;
    }
  }
  cout << maxi << endl;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> tab(n+1, 0);
  for (int i = 0; i < n; i++) {
    long long tmp;
    cin >> tmp;
    tab[i] = tmp;
  }
  vector<long long> dp(n+1, 0);
  long long somme = 0;
  long long rep = 0;
  for (int i = n-1; i >= 0; i--) {
    somme += tab[i];
    if (i+m <= n) {
      somme -= tab[i+m];
      long long zero = 0;
      dp[i] = max(zero, somme-k+dp[i+m]);
      rep = max(rep, dp[i]);
    }
  }
  for (int i = 0; i < n; i++) {
    long long somme = 0;
    for (int r = 0; r < m-1; r++) {
      if (i+r < n) {
        somme += tab[i+r];
        long long score_tmp = somme - k + dp[i+r+1];
        if (score_tmp > rep)
          rep = score_tmp;
      }
    }
  }
  cout << rep;
}


#include <iostream>
#include <vector>
#include <math.h>

using namespace std;

int calculk(int l, int r, int k, int m)
{
  int num = r-l+1;
  int den = m;
  if (num%den == 0)
    return k*(num/den);
  else
    return k*(1 + (num/den));
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, _m, _k;
  cin >> n >> _m >> _k;
  float k = _k;
  float m = _m;
  vector<float> tab(n);
  for (int i = 0; i < n; i++) {
    float tmp;
    cin >> tmp;
    tmp -= k/m;
    tab[i] = tmp;
  }
  int somme = 0;
  int maxi = 0;
  for (int i = 0; i < n; i++) {
    somme += tab[i];
    if (somme > maxi)
      maxi = somme;
    if (somme < 0)
      somme = 0;
  }
  if (ceilf(somme) == somme) {
    int res = somme;
    cout << res;
  }
  else {
    int res = somme+1;
    cout << res;
  }
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> tab(n+1, 0);
  for (int i = 0; i < n; i++) {
    long long tmp;
    cin >> tmp;
    tab[i] = tmp;
  }
  vector<long long> dp(n+1, 0);
  long long somme = 0;
  long long rep = 0;
  for (int i = n-1; i >= 0; i--) {
    somme += tab[i];
    if (i+m <= n) {
      somme -= tab[i+m];
      long long zero = 0;
      dp[i] = max(zero, somme-k+dp[i+m]);
      rep = max(rep, dp[i]);
    }
  }
  for (int i = 0; i < n; i++) {
    long somme = 0;
    for (int r = 0; r < m; r++) {
      if (i+r < n) {
        somme += tab[i+r];
        long score_tmp = somme - k + dp[i+r+1];
        if (score_tmp > rep)
          rep = score_tmp;
      }
    }
  }
  cout << rep;
}


#include <iostream>
#include <vector>

using namespace std;

int calculk(int l, int r, int k, int m)
{
  int num = r-l+1;
  int den = m;
  if (num%den == 0)
    return k*(num/den);
  else
    return k*(1 + (num/den));
}

int main()
{
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> tab(n);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    tab[i] = tmp;
  }
  vector<int> precalcul(n);
  for (int i = 0; i < n; i++)
    precalcul[i] = calculk(0, i+1, k, m);
  int maxi = 0;
  int somme = 0;
  int j = 0;
  for (int i = 0; i < n; i++) {
    somme += tab[i];
    int score_tmp = somme - calculk(j, i, k, m);//precalcul[j-i];
    if (score_tmp > maxi) {
      maxi = score_tmp;
      //cout << j << " " << i << endl;
    }
    if (i < n-1 && score_tmp <= tab[i+1]) {
      j = i+1;
      somme = 0;
    }
  }
  cout << maxi << endl;
}


/* ---------------- BEGIN TEMPLATE ---------------- */
#include <bits/stdc++.h>
#define DEBUGA(a, b) cout << "[ "; for (auto _x = (a); _x != (b); _x++) cout << *_x << " "; \
  cout << "]" << endl
#define DEBUG(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }; \
  cout << endl
#define _1 first
#define _2 second

using namespace std;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "; ";
  err(++it, args...);
}
/* ---------------- END TEMPLATE ---------------- */

const int NMAX = 3e5 + 5;

int n, m;
long long k;
long long a[NMAX];
long long dp[NMAX][15];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);

  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  for (int i = 1; i <= n; i++) {
    dp[i][1] = max(a[i] - k, dp[i - 1][0] + a[i] - k);
    for (int j = 2; j <= m - 1; j++) {
      dp[i][j] = dp[i - 1][j - 1] + a[i];
    }

    dp[i][0] = max(dp[i - 1][m - 1] + a[i], (long long) 0);
  }

  long long res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= m - 1; j++) {
      res = max(res, dp[i][j]);
    }
  }

  cout << res;
}


/* ---------------- BEGIN TEMPLATE ---------------- */
#include <bits/stdc++.h>
#define DEBUGA(a, b) cout << "[ "; for (auto _x = (a); _x != (b); _x++) cout << *_x << " "; \
  cout << "]" << endl
#define DEBUG(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }; \
  cout << endl
#define _1 first
#define _2 second

using namespace std;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "; ";
  err(++it, args...);
}
/* ---------------- END TEMPLATE ---------------- */

const int NMAX = 3e5 + 5;

int n, m;
long long k;
long long a[NMAX];
long long dp[NMAX][15];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);

  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  for (int j = 0; j <= m - 1; j++) {
    dp[0][j] = LLONG_MIN / 2;
  }
  for (int i = 1; i <= n; i++) {
    dp[i][1] = max(a[i] - k, dp[i - 1][0] + a[i] - k);
    for (int j = 2; j <= m - 1; j++) {
      if (i < j) {
        dp[i][j] = LLONG_MIN / 2;
      } else {
        dp[i][j] = dp[i - 1][j - 1] + a[i];
      }
    }

    if (i < m) {
      dp[i][0] = LLONG_MIN / 2;
    } else {
      dp[i][0] = dp[i - 1][m - 1] + a[i];
    }
  }

  long long res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= m - 1; j++) {
      res = max(res, dp[i][j]);
    }
  }

  cout << res;
}


/* ---------------- BEGIN TEMPLATE ---------------- */
#include <bits/stdc++.h>
#define DEBUGA(a, b) cout << "[ "; for (auto _x = (a); _x != (b); _x++) cout << *_x << " "; \
  cout << "]" << endl
#define DEBUG(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }; \
  cout << endl
#define _1 first
#define _2 second

using namespace std;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "; ";
  err(++it, args...);
}
/* ---------------- END TEMPLATE ---------------- */

const int NMAX = 3e5 + 5;

int n, m;
long long k;
long long a[NMAX];
long long dp[NMAX][15];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);

  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  for (int j = 0; j <= m - 1; j++) {
    dp[0][j] = LLONG_MIN / 2;
  }
  for (int i = 1; i <= n; i++) {
    dp[i][1] = max(a[i] - k, dp[i - 1][0] + a[i] - k);
    for (int j = 2; j <= m - 1; j++) {
      if (i < j) {
        dp[i][j] = LLONG_MIN / 2;
      } else {
        dp[i][j] = dp[i - 1][j - 1] + a[i];
      }
    }

    if (i < m) {
      dp[i][0] = LLONG_MIN / 2;
    } else {
      dp[i][0] = max(dp[i - 1][m - 1] + a[i], (long long) 0);
    }
  }

  long long res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= m - 1; j++) {
      res = max(res, dp[i][j]);
    }
  }

  cout << res;
}


/* ---------------- BEGIN TEMPLATE ---------------- */
#include <bits/stdc++.h>
#define DEBUGA(a, b) cout << "[ "; for (auto _x = (a); _x != (b); _x++) cout << *_x << " "; \
  cout << "]" << endl
#define DEBUG(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }; \
  cout << endl
#define _1 first
#define _2 second

using namespace std;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "; ";
  err(++it, args...);
}
/* ---------------- END TEMPLATE ---------------- */

const int NMAX = 3e5 + 5;

int n, m;
long long k;
long long a[NMAX];
long long dp[NMAX][15];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);

  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  if (m > 1) {
    for (int j = 0; j <= m - 1; j++) {
      dp[0][j] = LLONG_MIN / 2;
    }
    for (int i = 1; i <= n; i++) {
      dp[i][1] = max(a[i] - k, dp[i - 1][0] + a[i] - k);
      for (int j = 2; j <= m - 1; j++) {
        if (i < j) {
          dp[i][j] = LLONG_MIN / 2;
        } else {
          dp[i][j] = dp[i - 1][j - 1] + a[i];
        }
      }

      if (i < m) {
        dp[i][0] = LLONG_MIN / 2;
      } else {
        dp[i][0] = dp[i - 1][m - 1] + a[i];
      }
    }

  } else {
    for (int i = 1; i <= n; i++) {
      dp[i][0] = max(dp[i - 1][0] + a[i] - k, a[i] - k);
    }
  }

  long long res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= m - 1; j++) {
      res = max(res, dp[i][j]);
    }
  }

  cout << res;
}


/* ---------------- BEGIN TEMPLATE ---------------- */
#include <bits/stdc++.h>
#define DEBUGA(a, b) cout << "[ "; for (auto _x = (a); _x != (b); _x++) cout << *_x << " "; \
  cout << "]" << endl
#define DEBUG(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }; \
  cout << endl
#define _1 first
#define _2 second

using namespace std;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "; ";
  err(++it, args...);
}
/* ---------------- END TEMPLATE ---------------- */

const int NMAX = 3e5 + 5;

int n, m;
long long k;
long long a[NMAX];
long long dp[NMAX][15];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);

  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  for (int i = 1; i <= n; i++) {
    dp[i][1] = max(a[i] - k, dp[i - 1][0] + a[i] - k);
    for (int j = 2; j <= m - 1; j++) {
      dp[i][j] = dp[i - 1][j - 1] + a[i];
    }

    dp[i][0] = dp[i - 1][m - 1] + a[i];
  }

  long long res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= m - 1; j++) {
      res = max(res, dp[i][j]);
    }
  }

  cout << res;
}


/* ---------------- BEGIN TEMPLATE ---------------- */
#include <bits/stdc++.h>
#define DEBUGA(a, b) cout << "[ "; for (auto _x = (a); _x != (b); _x++) cout << *_x << " "; \
  cout << "]" << endl
#define DEBUG(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }; \
  cout << endl
#define _1 first
#define _2 second

using namespace std;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "; ";
  err(++it, args...);
}
/* ---------------- END TEMPLATE ---------------- */

const int NMAX = 3e5 + 5;

int n, m;
long long k;
long long a[NMAX];
long long dp[NMAX][15];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);

  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  for (int i = 1; i <= n; i++) {
    dp[i][1] = max(a[i] - k, dp[i - 1][0] + a[i]);
    for (int j = 2; j <= m - 1; j++) {
      dp[i][j] = dp[i - 1][j - 1] + a[i];
    }

    dp[i][0] = dp[i - 1][m - 1] + a[i];
  }

  long long res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= m - 1; j++) {
      res = max(res, dp[i][j]);
    }
  }

  cout << res;
}


#include<iostream>
#include<vector>
using namespace std;
const int MAXN=300002;
#define ll long long
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll int m,k;
    int n;
    cin >> n >> m >> k;
    ll int a[MAXN];
    int i;
    for(i=0;i<n;i++){
        cin >> a[i];
    }
    ll int res=0;
    ll int dp[MAXN];
    for(i=0;i<n;i++){
        ll int prov=0;
        ll int best=-k;
        for(int j=0;j<m && i+j<n;j++){
            best+=a[i+j];
            if(best>prov)
                prov=best;
        }
        if(i-m>=0){
            dp[i]=max(dp[i-m]+prov, prov);
        }
        else
            dp[i]=max(prov,(ll int)0);
    }
    ll int ans=0;
    for(i=0;i<n;i++)
        if(ans<dp[i])
            ans=dp[i];
    cout << ans;

    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
const int MAXN=300002;
#define ll long long
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll int k;
    int n,m;
    cin >> n >> m;
    cin >> k;
    ll int a[MAXN];
    int i;
    for(i=0;i<n;i++){
        cin >> a[i];
    }
    ll int res=0;
    ll int dp[MAXN];
    for(i=0;i<n;i++){
        ll int prov=-k;
        ll int best=-k;
        for(int j=0;j<m && i+j<n;j++){
            best+=a[i+j];
            if(best>prov)
                prov=best;
        }
        if(i-m>=0){
            dp[i]=max(dp[i-m]+prov, prov);
        }
        else
            dp[i]=prov;
    }
    ll int ans=0;
    for(i=0;i<n;i++)
        if(ans<dp[i])
            ans=dp[i];
    cout << ans;

    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
#define ll long long
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll int m,k;
    int n;
    cin >> n >> m >> k;
    ll int a[n];
    int i;
    for(i=0;i<n;i++){
        cin >> a[i];
    }
    ll int res=0;
    ll int dp[n];
    for(i=0;i<n;i++){
        ll int prov=0;
        ll int best=-k;
        for(int j=0;j<m && i+j<n;j++){
            best+=a[i+j];
            if(best>prov)
                prov=best;
        }
        if(i>m-1){
            dp[i]=max(dp[i-m]+prov, dp[i-m]);
        }
        else
            dp[i]=max(prov,(ll int)0);
    }
    ll int ans=0;
    for(i=0;i<n;i++)
        if(ans<dp[i])
            ans=dp[i];
    cout << ans;

    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
const int MAXN=300002;
#define ll long long
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll int k;
    int n,m;
    cin >> n >> m;
    cin >> k;
    ll int a[MAXN];
    int i;
    for(i=0;i<n;i++){
        cin >> a[i];
    }
    ll int res=0;
    ll int dp[MAXN];
    ll int mn[MAXN];
    for(i=0;i<n;i++){
        ll int prov=-k;
        ll int best=-k;
        for(int j=0;j<m && i+j<n;j++){
            best+=a[i+j];
            if(best>prov)
                prov=best;
        }
        if(i-m>=0)
            mn[i]=max(best,best+mn[i-m]);
        else
            mn[i]=best;
        if(i-m>=0){
            dp[i]=max(mn[i-m]+prov, prov);
        }
        else
            dp[i]=prov;
    }
    ll int ans=0;
    for(i=0;i<n;i++)
        if(ans<dp[i])
            ans=dp[i];
    cout << ans;

    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
#define ll long long
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll int m,k;
    int n;
    cin >> n >> m >> k;
    ll int a[n];
    int i;
    for(i=0;i<n;i++){
        cin >> a[i];
    }
    ll int res=0;
    ll int dp[n];
    for(i=0;i<n;i++){
        ll int prov=0;
        ll int best=-k;
        for(int j=0;j<m && i+j<n;j++){
            best+=a[i+j];
            if(best>prov)
                prov=best;
        }
        if(i-m>=0){
            dp[i]=max(dp[i-m]+prov, prov);
        }
        else
            dp[i]=max(prov,(ll int)0);
    }
    ll int ans=0;
    for(i=0;i<n;i++)
        if(ans<dp[i])
            ans=dp[i];
    cout << ans;

    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
const int MAXN=300002;
#define ll long long
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll int k;
    int n,m;
    cin >> n >> m;
    cin >> k;
    ll int a[MAXN];
    int i;
    for(i=0;i<n;i++){
        cin >> a[i];
    }
    ll int res=0;
    ll int dp[MAXN];
    for(i=0;i<n;i++){
        ll int prov=0;
        ll int best=-k;
        for(int j=0;j<m && i+j<n;j++){
            best+=a[i+j];
            if(best>prov)
                prov=best;
        }
        if(i-m>=0){
            dp[i]=max(dp[i-m]+prov, prov);
        }
        else
            dp[i]=max(prov,(ll int)0);
    }
    ll int ans=0;
    for(i=0;i<n;i++)
        if(ans<dp[i])
            ans=dp[i];
    cout << ans;

    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
#define ll long long
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll int m,k;
    int n;
    cin >> n >> m >> k;
    ll int a[n];
    int i;
    for(i=0;i<n;i++){
        cin >> a[i];
    }
    ll int res=0;
    ll int dp[n];
    for(i=0;i<n;i++){
        ll int prov=0;
        ll int best=-k;
        for(int j=0;j<m && i+j<n;j++){
            best+=a[i+j];
            if(best>prov)
                prov=best;
        }
        if(i-m>=0){
            dp[i]=max(dp[i-m]+prov, dp[i-m]);
        }
        else
            dp[i]=max(prov,(ll int)0);
    }
    ll int ans=0;
    for(i=0;i<n;i++)
        if(ans<dp[i])
            ans=dp[i];
    cout << ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int nax=(int)3e5+7;
const ld inf=1e18;
int n;
ld m,k;
ld s[nax];

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

        cin>>n>>m>>k;
        for(int i=1;i<=n;i++)
        {
                int x;
                cin>>x;
                s[i]=s[i-1]+x;
        }

        ld ans=0;
        for(int a=0;a<m;a++)
                for(int b=0;b<m;b++)
                {
                        int df=a-b;
                        while(df%(int)m)
                                df++;
                        df-=(a-b);
                        ld cur=-inf;
                        for(int i=0;i<=n;i++)
                        {
                                if(i%(int)m==a)
                                        ans=max(ans,cur+s[i]-(ld)(i+df)/m*k);
                                if(i%(int)m==b)
                                        cur=max(cur,-s[i]+(ld)i/m*k);
                        }
                }
        cout<<fixed<<setprecision(0)<<ans<<"\n";
        return 0;
}
/**

**/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int nax=(int)3e5+7;
const ld inf=1e18;
int n;
ld m,k;
ld s[nax];
ld cur[10];
int df[10][10];

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

        cin>>n>>m>>k;
        for(int i=1;i<=n;i++)
        {
                int x;
                cin>>x;
                s[i]=s[i-1]+x;
        }

        for(int a=0;a<m;a++)
                for(int b=0;b<m;b++)
                {
                        df[a][b]=a-b;
                        while(df[a][b]%(int)m)
                                df[a][b]++;
                        df[a][b]-=(a-b);
                }

        ld ans=0;
        for(int i=0;i<=n;i++)
        {
                int a=i%(int)m;
                for(int b=0;b<m;b++)
                        ans=max(ans,cur[b]+s[i]-(ld)(i+df[a][b])/m*k);
                cur[a]=max(cur[a],-s[i]+(ld)i/m*k);
        }
        cout<<fixed<<setprecision(0)<<ans<<"\n";
        return 0;
}
/**

**/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int nax = (int) 3e5 + 7;
const ld inf = 1e18;
int n;
ld m, k;
ld s[nax];
ld cur[10];
int df[10][10];

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

        cin >> n >> m >> k;
        for(int i = 0; i < m; i++)
                cur[i] = -inf;
        for(int i = 1; i <= n; i++)
        {
                int x;
                cin >> x;
                s[i] = s[i - 1] + x;
        }

        for(int a = 0; a < m; a++)
                for(int b = 0; b < m; b++)
                {
                        df[a][b] = a - b;
                        while(df[a][b] % (int) m)
                                df[a][b]++;
                        df[a][b] -= (a - b);
                }

        ld ans = 0;
        for(int i = 0; i <= n; i++)
        {
                int a = i % (int) m;
                for(int b = 0; b < m; b++)
                        ans = max(ans, cur[b] + s[i] - (ld)(i + df[a][b]) / m * k);
                cur[a] = max(cur[a], -s[i] + (ld)i / m * k);
        }
        cout << fixed << setprecision(0) << ans << "\n";
        return 0;
}
/**

**/


#include <bits/stdc++.h>

using namespace std;

const long double inf = 1e18;
int n;
long double m, k;
long double s;
long double cur[10];
int df[10][10];

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

        cin >> n >> m >> k;
        for (int i = 0; i < m; i++)
                cur[i] = -inf;
        for (int a = 0; a < m; a++)
                for (int b = 0; b < m; b++)
                {
                        df[a][b] = a - b;
                        while (df[a][b] % (int) m)
                                df[a][b]++;
                        df[a][b] -= (a - b);
                }
        long double s = 0, ans = 0;
        cur[0] = 0;
        for (int i = 1; i <= n; i++)
        {
                int x;
                cin >> x;
                s += x;
                int a = i % (int) m;
                for (int b = 0; b < m; b++)
                        ans = max(ans, cur[b] + s - (long double) (i + df[a][b]) / m * k);
                cur[a] = max(cur[a], -s + (long double) i / m * k);
        }
        cout << fixed << setprecision(0) << ans << "\n";
        return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int nax=(int)3e5+7;
const ld inf=1e18;
int n;
ld m,k;
ld s[nax];

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

        cin>>n>>m>>k;
        for(int i=1;i<=n;i++)
        {
                int x;
                cin>>x;
                s[i]=s[i-1]+x;
        }

        ld ans=0;
        for(int a=0;a<m;a++)
                for(int b=0;b<m;b++)
                {
                        int df=a-b;
                        while(df%(int)m)
                                df++;
                        df-=(a-b);
                        ld cur=-inf;
                        for(int i=0;i<=n;i++)
                        {
                                if(i%(int)m==a)
                                        ans=max(ans,cur+s[i]-(ld)(i+df)/m*k);
                                if(i%(int)m==b)
                                        cur=max(cur,-s[i]+(ld)i/m*k);
                        }
                }
        ll sl=ans;
        cout<<sl<<"\n";
        return 0;
}
/**

**/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int nax=(int)3e5+7;
const ld inf=1e18;
int n;
ld m,k;
ld s[nax];
ld cur[10];
int df[10][10];

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

        cin>>n>>m>>k;
        for(int i=0;i<m;i++)
                cur[i]=-inf;
        for(int i=1;i<=n;i++)
        {
                int x;
                cin>>x;
                s[i]=s[i-1]+x;
        }

        for(int a=0;a<m;a++)
                for(int b=0;b<m;b++)
                {
                        df[a][b]=a-b;
                        while(df[a][b]%(int)m)
                                df[a][b]++;
                        df[a][b]-=(a-b);
                }

        ld ans=0;
        for(int i=0;i<=n;i++)
        {
                int a=i%(int)m;
                for(int b=0;b<m;b++)
                        ans=max(ans,cur[b]+s[i]-(ld)(i+df[a][b])/m*k);
                cur[a]=max(cur[a],-s[i]+(ld)i/m*k);
        }
        cout<<fixed<<setprecision(0)<<ans<<"\n";
        return 0;
}
/**

**/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const ld inf = 1e18;
int n;
ld m, k;
ld s;
ld cur[10];
int df[10][10];

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

        cin >> n >> m >> k;
        for (int i = 0; i < m; i++)
                cur[i] = -inf;
        for (int a = 0; a < m; a++)
                for (int b = 0; b < m; b++)
                {
                        df[a][b] = a - b;
                        while (df[a][b] % (int) m)
                                df[a][b]++;
                        df[a][b] -= (a - b);
                }
        ld s = 0, ans = 0;
        cur[0] = 0;
        for (int i = 1; i <= n; i++)
        {
                int x;
                cin >> x;
                s += x;
                int a = i % (int) m;
                for (int b = 0; b < m; b++)
                        ans = max(ans, cur[b] + s - (ld) (i + df[a][b]) / m * k);
                cur[a] = max(cur[a], -s + (ld) i / m * k);
        }
        cout << fixed << setprecision(0) << ans << "\n";
        return 0;
}
/**

**/


#include <bits/stdc++.h>

using namespace std;
const int MAXN = 3e5 + 10;
typedef long long LL;
LL a[MAXN];

LL getnum(LL len, LL m)
{
    if (len % m == 0)
        return len / m;
    else
    {
        return len / m + 1;
    }
}
LL dp[MAXN][12];
int main()
{
    int n;
    LL m, k;
    scanf("%d %lld %lld", &n, &m, &k);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", a + i);
    }
    for (int i = 1; i <= n; i++)
        for (int j = 0; j < m; j++)
            dp[i][j] = -1e18;
    dp[0][0] = 0;
    LL ans = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (j == 1)
                dp[i][j] = max(dp[i - 1][0] + a[i] - k, a[i] - k);
            else if (j == 0)
            {
                dp[i][j] = dp[i - 1][m - 1] + a[i];
            }else
            {
                dp[i][j] = dp[i - 1][j - 1] + a[i];
            }

            ans = max(ans, dp[i][j]);
        }
    }
    printf("%lld\n", ans);
    // cin >> n;

}


#include <bits/stdc++.h>

using namespace std;
const int MAXN = 3e5 + 10;
typedef long long LL;
LL a[MAXN];

LL getnum(LL len, LL m)
{
    if (len % m == 0)
        return len / m;
    else
    {
        return len / m + 1;
    }
}
LL dp[MAXN][12];
int main()
{
    int n;
    LL m, k;
    scanf("%d %lld %lld", &n, &m, &k);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", a + i);
    }
    for (int i = 1; i <= n; i++)
        for (int j = 0; j < m; j++)
            dp[i][j] = -1e18;
    dp[0][m] = 0;
    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]);
        }
    }
    printf("%lld\n", ans);
    // cin >> n;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll f[20];
ll bal,temp;
int main(){
	ll n,m,k,res=0;
	cin>>n>>m>>k;
	bal=0;
	for(int i=0;i<20;i++)f[i]=3e10;
	for(int i=1;i<=n;i++){
		scanf("%I64d",&temp);
		bal+=temp;
		for(int j=0;j<m;j++){
			if(j>=i%m){
				res=max(res,bal-f[j]-k*(i /m + 1));
			}
			else {
				res=max(res,bal-f[j]-k*(i / m + 2));
			}
		}
		f[i % m] = min(f[i % m],bal-k*(i / m + 1));
	}
	printf("%I64d\n",res);

}


#include <bits/stdc++.h>

using namespace std;
const int MAXN = 3e5 + 10;
typedef long long LL;
LL a[MAXN];

LL getnum(LL len, LL m)
{
    if (len % m == 0)
        return len / m;
    else
    {
        return len / m + 1;
    }
}
int main()
{
    int n;
    LL m, k;
    scanf("%d %lld %lld", &n, &m, &k);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", a + i);
    }
    LL L = 1, R = 1;
    LL allans = 0;
    LL sum = 0;
    while (R <= n)
    {
        sum += a[R];
        LL tmpans = sum - k * getnum(R - L + 1, m);
        allans = max(allans, tmpans);
        if (tmpans <= 0)
        {
            L = R + 1;
            sum = 0;
        }
        // cout << "L = " << L << " R = " << R << " allans = " << allans << '\n';
        R++;
    }
    printf("%lld\n", allans);
    // cin >> n;
}


#include <bits/stdc++.h>

using namespace std;
const int MAXN = 3e5 + 10;
typedef long long LL;
LL a[MAXN];

LL getnum(LL len, LL m)
{
    if (len % m == 0)
        return len / m;
    else
    {
        return len / m + 1;
    }
}
LL dp[MAXN][12];
int main()
{
    int n;
    LL m, k;
    scanf("%d %lld %lld", &n, &m, &k);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", a + i);
    }
    dp[0][0] = 0;
    LL ans = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (j == 1)
                dp[i][j] = max(dp[i - 1][0] + a[i] - k, a[i] - k);
            else if (j == 0)
            {
                dp[i][j] = dp[i - 1][m - 1] + a[i];
            }else
            {
                dp[i][j] = dp[i - 1][j - 1] + a[i];
            }

            ans = max(ans, dp[i][j]);
        }
    }
    printf("%lld\n", ans);
    // cin >> n;

}


#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<algorithm>
#include<set>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
ll n,m,k;
ll grter(ll l,ll r,ll m){
	ll ans;
	if((l-r+1)%m==0)ans=(l-r+1)/m;
	else ans=(l-r+1)/m+1;
	return ans*k;
}
int main(){

	while(cin>>n>>m>>k){
		ll ans=0,sum=0;
	     ll l=1,r=1;
		ll temp;
		for(int i=1;i<=n;i++){
			l=i;
			scanf("%lld",&temp);
			sum+=temp;
			if(sum<=0){
				r=i+1;
				sum=0;
			}
			else{
				ans=max(ans,sum-grter(l,r,m));
			}
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<algorithm>
#include<set>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
const int M=3e5+500;
ll a[M];
ll n,m,k;
ll grter(ll l,ll r,ll m){
	ll ans;
	if((l-r+1)%m==0)ans=(l-r+1)/m;
	else ans=(l-r+1)/m+1;
	return ans*k;
}
int main(){

	while(cin>>n>>m>>k){
		ll ans=0,sum=0,sum1=0;
	     ll l=1,r=1;
		ll temp;
		for(int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		ll i;

		for( i=m;i<=n;i+=m){
			 sum=0;
			for(int j=i;j>=i-m+1;j--){
				sum+=a[j];
			}
			if(sum>k){
				sum1+=sum-k;
				ans=max(sum1,ans);

			}

			else sum1=0;
		}
		i-=m;
		if(n%m){

			for(i=i+1 ;i<=n;i++){
				sum1+=a[i];
				ans=max(ans,sum1);
			}
		}

		cout<<ans<<endl;
	}
}


#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<algorithm>
#include<set>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
ll n,m,k;
ll grter(ll l,ll r,ll m){
	ll ans;
	if((l-r+1)%m==0)ans=(l-r+1)/m;
	else ans=(l-r+1)/m+1;
	return ans*k;
}
int main(){

	while(cin>>n>>m>>k){
		ll ans=0,sum=0,sum1=0;
	     ll l=1,r=1;
		ll temp;
		for(int i=1;i<=n;i++){
			l=i;
			scanf("%lld",&temp);
			sum+=temp;
			sum1=sum-grter(l,r,m);

			if(sum1<=0||sum<=0){
				r=i+1;
				sum=0;
			}
			else{
				ans=max(ans,sum1);
			}
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll f[20];
ll bal,temp;
int main(){
	ll n,m,k,res=0;
	cin>>n>>m>>k;
	bal=0;
	for(int i=0;i<20;i++)f[i]=3e18;
	f[0]=0;
	for(int i=1;i<=n;i++){
		 ll t;
		scanf("%I64d",&temp);
		bal+=temp;
		f[0]=-k;
//		if(i % m ==0)t=i /m;
//		else t=i / m + 1;
		for(int j=0;j<m;j++){
			if(j>=i%m){
				//cout<<"11111"<<endl;
				res=max(res,bal-f[j]-k*(i / m +1 ));
			}
			else {
				//cout<<"222222"<<endl;
				res=max(res,bal-f[j]-k*(i / m +2));
			}
		}
		f[i % m] = min(f[i % m],bal-k*(i / m +1));
	}
	printf("%I64d\n",res);

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll f[20];
ll bal,temp;
int main(){
	ll n,m,k,res=0;
	cin>>n>>m>>k;
	bal=0;
	for(int i=0;i<20;i++)f[i]=3e18;
	for(int i=1;i<=n;i++){
		scanf("%I64d",&temp);
		bal+=temp;
		for(int j=0;j<m;j++){
			if(j>=i%m){
				res=max(res,bal-f[j]-k*(i /m + 1));
			}
			else {
				res=max(res,bal-f[j]-k*(i / m + 2));
			}
		}
		f[i % m] = min(f[i % m],bal-k*(i / m + 1));
	}
	printf("%I64d\n",res);

}


#include <bits/stdc++.h>

using namespace std;
const int MAXN = 3e5 + 10;
typedef long long LL;
LL a[MAXN];
LL dp[MAXN][12];
int main()
{
    int n;
    LL m, k;
    scanf("%d %lld %lld", &n, &m, &k);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", a + i);
    }
    for (int i = 0; i <= n; i++)
        for (int j = 1; j <= m; j++)
            dp[i][j] = -1e18;
    dp[0][m] = 0;
    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]);
        }
    }
    printf("%lld\n", ans);
    // cin >> n;

}


#include <bits/stdc++.h>

using namespace std;
const int MAXN = 3e5 + 10;
typedef long long LL;
LL a[MAXN];

LL getnum(LL len, LL m)
{
    if (len % m == 0)
        return len / m;
    else
    {
        return len / m + 1;
    }
}
LL dp[MAXN][12];
int main()
{
    int n;
    LL m, k;
    scanf("%d %lld %lld", &n, &m, &k);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", a + i);
    }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            dp[i][j] = -1e18;
    dp[0][m] = 0;
    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]);
        }
    }
    printf("%lld\n", ans);
    // cin >> n;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll f[20];
ll bal,temp;
int main(){
	ll n,m,k,res=0;
	cin>>n>>m>>k;
	bal=0;
	for(int i=0;i<20;i++)f[i]=3e18;
	f[0]=-k;
	for(int i=1;i<=n;i++){
		 ll t;
		scanf("%I64d",&temp);
		bal+=temp;
		//f[0]=-k;
//		if(i % m ==0)t=i /m;
//		else t=i / m + 1;
		for(int j=0;j<m;j++){
			if(j>=i%m){
				//cout<<"11111"<<endl;
				res=max(res,bal-f[j]-k*(i / m +1 ));
			}
			else {
				//cout<<"222222"<<endl;
				res=max(res,bal-f[j]-k*(i / m +2));
			}
		}
		f[i % m] = min(f[i % m],bal-k*(i / m +1));
	}
	printf("%I64d\n",res);

}


#include <cstdio>

typedef long long ll;
typedef unsigned long long ull;
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))

inline ll read()
{
    char c = getchar();
    ll ret = 0, t = 1;
    while ((c < '0' || c > '9') && c != '-') c = getchar();
    if (c == '-') t = -1, c = getchar();
    while (c >= '0' && c <= '9') ret = ret * 10 + c - '0', c = getchar();
    return ret * t;
}

const int MAXN = 300005;
double a[MAXN], dp[MAXN][10], p, ans = -1e99;
int N, M, K;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
    #endif
    N = read(), M = read(), K = read(); int i, j; p = K / (double)M;
    for (i = 1; i <= N; ++i) a[i] = read() - p;
    for (i = 0; i < M; ++i) dp[0][i] = -1e99;
    dp[0][0] = K;
    for (i = 1; i <= N; ++i)
    {
        dp[i][0] = dp[i - 1][M - 1] - p * (M - 1) + a[i] + K;
        dp[i][1] = max(dp[i - 1][0] - K + p + a[i], p + a[i]);
        for (j = 2; j < M; ++j) dp[i][j] = dp[i - 1][j - 1] + p + a[i];
        for (j = 0; j < M; ++j) ans = max(ans, dp[i][j]);
        //for (j = 0; j < M; ++j) printf("dp[%d][%d]=%.2f\n", i, j, dp[i][j]);
    }
    printf("%I64d", max((ll)ans - K, 0));
    return 0;
}


#include <cstdio>

typedef long long ll;
typedef unsigned long long ull;
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))

inline ll read()
{
    char c = getchar();
    ll ret = 0, t = 1;
    while ((c < '0' || c > '9') && c != '-') c = getchar();
    if (c == '-') t = -1, c = getchar();
    while (c >= '0' && c <= '9') ret = ret * 10 + c - '0', c = getchar();
    return ret * t;
}

const int MAXN = 300005;
double a[MAXN], dp[MAXN][10], p, ans = -1e99;
int N, M, K;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
    #endif
    N = read(), M = read(), K = read(); int i, j; p = K / (double)M;
    for (i = 1; i <= N; ++i) a[i] = read() - p;
    for (i = 0; i < M; ++i) dp[0][i] = p * i;
    for (i = 1; i <= N; ++i)
    {
        dp[i][0] = dp[i - 1][M - 1] - p * (M - 1) + a[i];
        dp[i][1] = max(dp[i - 1][0] + p + a[i], p + a[i]);
        for (j = 2; j < M; ++j) dp[i][j] = dp[i - 1][j - 1] + p + a[i];
        for (j = 0; j < M; ++j) ans = max(ans, dp[i][j]);
        //for (j = 0; j < M; ++j) printf("dp[%d][%d]=%.2f\n", i, j, dp[i][j]);
    }
    printf("%I64d", max((ll)ans - K, 0));
    return 0;
}


#include <cstdio>

typedef long long ll;
typedef unsigned long long ull;
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))

inline ll read()
{
    char c = getchar();
    ll ret = 0, t = 1;
    while ((c < '0' || c > '9') && c != '-') c = getchar();
    if (c == '-') t = -1, c = getchar();
    while (c >= '0' && c <= '9') ret = ret * 10 + c - '0', c = getchar();
    return ret * t;
}

const int MAXN = 300005;
double a[MAXN], dp[MAXN][11], p, ans = -1e99;
int N, M, K;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
    #endif
    N = read(), M = read(), K = read(); int i, j; p = K / (double)M;
    for (i = 1; i <= N; ++i) a[i] = read() - p;
    for (i = 0; i < M; ++i) dp[0][i] = -1e99;
    if (M > 1)
    {
        dp[0][0] = K;
        for (i = 1; i <= N; ++i)
        {
            dp[i][0] = dp[i - 1][M - 1] - p * (M - 1) + a[i] + K;
            dp[i][1] = max(dp[i - 1][0] - K + p + a[i], p + a[i]);
            for (j = 2; j < M; ++j) dp[i][j] = dp[i - 1][j - 1] + p + a[i];
            for (j = 0; j < M; ++j) ans = max(ans, dp[i][j]);
            for (j = 0; j < M; ++j) printf("dp[%d][%d]=%.2f\n", i, j, dp[i][j]);
        }
        printf("%I64d", max((ll)ans - K, 0));
    }
    else
    {
        dp[0][0] = 0;
        for (i = 1; i <= N; ++i)
            dp[i][0] = max(dp[i - 1][0] + a[i], a[i]), ans = max(ans, dp[i][0]);
        printf("%I64d", max((ll)ans, 0));
    }
    return 0;
}


#include <cstdio>

typedef long long ll;
typedef unsigned long long ull;
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))

inline ll read()
{
    char c = getchar();
    ll ret = 0, t = 1;
    while ((c < '0' || c > '9') && c != '-') c = getchar();
    if (c == '-') t = -1, c = getchar();
    while (c >= '0' && c <= '9') ret = ret * 10 + c - '0', c = getchar();
    return ret * t;
}

const int MAXN = 300005;
double a[MAXN], dp[MAXN][11], p, ans = -1e99;
int N, M, K;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
    #endif
    N = read(), M = read(), K = read(); int i, j; p = K / (double)M;
    for (i = 1; i <= N; ++i) a[i] = read() - p;
    for (i = 0; i < M; ++i) dp[0][i] = -1e99;
    if (M > 1)
    {
        dp[0][0] = K;
        for (i = 1; i <= N; ++i)
        {
            dp[i][0] = dp[i - 1][M - 1] - p * (M - 1) + a[i] + K;
            dp[i][1] = max(dp[i - 1][0] - K + p + a[i], p + a[i]);
            for (j = 2; j < M; ++j) dp[i][j] = dp[i - 1][j - 1] + p + a[i];
            for (j = 0; j < M; ++j) ans = max(ans, dp[i][j]);
            //for (j = 0; j < M; ++j) printf("dp[%d][%d]=%.2f\n", i, j, dp[i][j]);
        }
        printf("%I64d", max((ll)ans - K, 0));
    }
    else
    {
        dp[0][0] = 0;
        for (i = 1; i <= N; ++i)
            dp[i][0] = max(dp[i - 1][0] + a[i], a[i]), ans = max(ans, dp[i][0]);
        printf("%I64d", max((ll)ans, 0));
    }
    return 0;
}


#include <cstdio>

typedef long long ll;
typedef unsigned long long ull;
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))

inline ll read()
{
    char c = getchar();
    ll ret = 0, t = 1;
    while ((c < '0' || c > '9') && c != '-') c = getchar();
    if (c == '-') t = -1, c = getchar();
    while (c >= '0' && c <= '9') ret = ret * 10 + c - '0', c = getchar();
    return ret * t;
}

const int MAXN = 300005;
long double a[MAXN], dp[MAXN][11], p, ans = -1e99;
int N, M, K;
int main()
{
    N = read(), M = read(), K = read(); int i, j; p = K / (double)M;
    for (i = 1; i <= N; ++i) a[i] = read() - p;
    for (i = 0; i < M; ++i) dp[0][i] = -1e99;
    if (M > 1)
    {
        dp[0][0] = K;
        for (i = 1; i <= N; ++i)
        {
            dp[i][0] = dp[i - 1][M - 1] - p * (M - 1) + a[i] + K;
            dp[i][1] = max(dp[i - 1][0] - K + p + a[i], p + a[i]);
            for (j = 2; j < M; ++j) dp[i][j] = dp[i - 1][j - 1] + p + a[i];
            for (j = 0; j < M; ++j) ans = max(ans, dp[i][j]);
            //for (j = 0; j < M; ++j) printf("dp[%d][%d]=%.2f\n", i, j, dp[i][j]);
        }
        printf("%I64d", max((ll)ans - K, 0));
    }
    else
    {
        dp[0][0] = 0;
        for (i = 1; i <= N; ++i)
            dp[i][0] = max(dp[i - 1][0] + a[i], a[i]), ans = max(ans, dp[i][0]);
        printf("%I64d", max((ll)ans, 0));
    }
    return 0;
}


#include <cstdio>

typedef long long ll;
typedef unsigned long long ull;
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))

inline ll read()
{
    char c = getchar();
    ll ret = 0, t = 1;
    while ((c < '0' || c > '9') && c != '-') c = getchar();
    if (c == '-') t = -1, c = getchar();
    while (c >= '0' && c <= '9') ret = ret * 10 + c - '0', c = getchar();
    return ret * t;
}

const int MAXN = 300005;
long double a[MAXN], dp[MAXN][11], p, ans = -1e99; //
int N, M, K;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
    #endif
    N = read(), M = read(), K = read(); int i, j; p = K / (double)M;
    for (i = 1; i <= N; ++i) a[i] = read() - p;
    for (i = 0; i < M; ++i) dp[0][i] = -1e99;
    if (M > 1)
    {
        dp[0][0] = K;
        for (i = 1; i <= N; ++i)
        {
            dp[i][0] = dp[i - 1][M - 1] - p * (M - 1) + a[i] + K;
            dp[i][1] = max(dp[i - 1][0] - K + p + a[i], p + a[i]);
            for (j = 2; j < M; ++j) dp[i][j] = dp[i - 1][j - 1] + p + a[i];
            for (j = 0; j < M; ++j) ans = max(ans, dp[i][j]);
            //for (j = 0; j < M; ++j) printf("dp[%d][%d]=%.2f\n", i, j, dp[i][j]);
        }
        printf("%I64d", max((ll)ans - K, 0));
    }
    else
    {
        dp[0][0] = 0;
        for (i = 1; i <= N; ++i)
            dp[i][0] = max(dp[i - 1][0] + a[i], a[i]), ans = max(ans, dp[i][0]);
        printf("%I64d", max((ll)ans, 0));
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
  int n, m; long long int k; cin >> n >> m >> k;
  vector<long long int> nums1(n);
  for(int i = 0; i < n; ++i){
    cin >> nums1[i];
  }
  bool flag = true;
  vector<vector<long long int>> dp(m, vector<long long int>(n));
  for(int I = 0; I < m; ++I){
    vector<long long int> nums = nums1;
    for(int j = 0; j < n; ++j){
      if(j%m==I){
        nums[j]-=k;
      }
    }
    dp[I][0] = nums[0];
    // cout << dp[I][0] << " ";
    for(int i = 1; i < n; ++i){
      dp[I][i]=max(dp[I][i-1]+nums[i], nums[i]);
      // cout << dp[I][i] << " ";
    }
    // cout << endl;
    long long int max = nums[0];
  }

  vector<long long int> mins(n);
  for(int i = 0; i < n; ++i){
    long long int min = -1;
    bool flag = true;
    for(int j = 0; j < m; ++j){
      if(flag || min > dp[j][i]){
        min = dp[j][i];
        flag=false;
      }
    }
    mins[i] = min;
  }
  // for(int i = 0; i < n; ++i){
  //   cout << mins[i] << " ";
  // }
  // cout << endl;
  int Max = 0;
  bool F = true;
  for(int i = 0; i < n; ++i){
    if(F||Max<mins[i]){
      Max = mins[i];
    }
  }
  cout << max(0,Max) << endl;

}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
  int n, m; long long int k; cin >> n >> m >> k;
  vector<long long int> nums1(n);
  for(int i = 0; i < n; ++i){
    cin >> nums1[i];
  }
  bool flag = true;
  vector<vector<long long int>> dp(m, vector<long long int>(n));
  for(int I = 0; I < m; ++I){
    vector<long long int> nums = nums1;
    for(int j = 0; j < n; ++j){
      if(j%m==I){
        nums[j]-=k;
      }
    }
    dp[I][0] = nums[0];
    // cout << dp[I][0] << " ";
    for(int i = 1; i < n; ++i){
      dp[I][i]=max(dp[I][i-1]+nums[i], nums[i]);
      // cout << dp[I][i] << " ";
    }
    // cout << endl;
    long long int max = nums[0];
  }

  vector<long long int> mins(n);
  for(int i = 0; i < n; ++i){
    long long int min = -1;
    bool flag = true;
    for(int j = 0; j < m; ++j){
      if(flag || min > dp[j][i]){
        min = dp[j][i];
        flag=false;
      }
    }
    mins[i] = min;
  }
  // for(int i = 0; i < n; ++i){
  //   cout << mins[i] << " ";
  // }
  // cout << endl;
  long long int Max = 0;
  bool F = true;
  for(int i = 0; i < n; ++i){
    if(F||Max<mins[i]){
      Max = mins[i];
      F = false;
    }
  }
  cout << max(0,Max) << endl;

}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
  int n, m; long long int k; cin >> n >> m >> k;
  vector<long long int> nums1(n);
  for(int i = 0; i < n; ++i){
    cin >> nums1[i];
  }
  bool flag = true;
  vector<vector<long long int>> dp(m, vector<long long int>(n));
  for(int I = 0; I < m; ++I){
    vector<long long int> nums = nums1;
    for(int j = 0; j < n; ++j){
      if(j%m==I){
        nums[j]-=k;
      }
    }
    dp[I][0] = nums[0];
    // cout << dp[I][0] << " ";
    for(int i = 1; i < n; ++i){
      dp[I][i]=max(dp[I][i-1]+nums[i], nums[i]);
      // cout << dp[I][i] << " ";
    }
    // cout << endl;
    long long int max = nums[0];
  }

  vector<long long int> mins(n);
  for(int i = 0; i < n; ++i){
    long long int min = -1;
    bool flag = true;
    for(int j = 0; j < m; ++j){
      if(flag || min > dp[j][i]){
        min = dp[j][i];
        flag=false;
      }
    }
    mins[i] = min;
  }
  // for(int i = 0; i < n; ++i){
  //   cout << mins[i] << " ";
  // }
  // cout << endl;
  long long int Max = 0;
  bool F = true;
  for(int i = 0; i < n; ++i){
    if(F||Max<mins[i]){
      Max = mins[i];
      F = false;
    }
  }
  if(Max > 0){
    cout << Max << endl;
  }else{
    cout << 0 << endl;
  }
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
  int n, m; long long int k; cin >> n >> m >> k;
  vector<long long int> nums1(n);
  for(int i = 0; i < n; ++i){
    cin >> nums1[i];
  }
  bool flag = true;
  vector<vector<long long int>> dp(m, vector<long long int>(n));
  for(int I = 0; I < m; ++I){
    vector<long long int> nums = nums1;
    for(int j = 0; j < n; ++j){
      if(j%m==I){
        nums[j]-=k;
      }
    }
    dp[I][0] = nums[0];
    // cout << dp[I][0] << " ";
    for(int i = 1; i < n; ++i){
      dp[I][i]=max(dp[I][i-1]+nums[i], nums[i]);
      // cout << dp[I][i] << " ";
    }
    // cout << endl;
    long long int max = nums[0];
  }

  vector<long long int> mins(n);
  for(int i = 0; i < n; ++i){
    long long int min = -1;
    bool flag = true;
    for(int j = 0; j < m; ++j){
      if(flag || min > dp[j][i]){
        min = dp[j][i];
        flag=false;
      }
    }
    mins[i] = min;
  }
  // for(int i = 0; i < n; ++i){
  //   cout << mins[i] << " ";
  // }
  // cout << endl;
  int Max = 0;
  bool F = true;
  for(int i = 0; i < n; ++i){
    if(F||Max<mins[i]){
      Max = mins[i];
      F = false
    }
  }
  cout << max(0,Max) << endl;

}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
  int n, m; long long int k; cin >> n >> m >> k;
  vector<long long int> nums1(n);
  for(int i = 0; i < n; ++i){
    cin >> nums1[i];
  }
  bool flag = true;
  vector<vector<long long int>> dp(n, vector<long long int>(m));
  for(int I = 0; I < m; ++I){
    vector<long long int> nums = nums1;
    for(int j = 0; j < n; ++j){
      if(j%m==I){
        nums[j]-=k;
      }
    }
    dp[I][0] = nums[0];
    for(int i = 1; i < n; ++i){
      dp[I][i]=max(dp[I][i-1]+nums[i], nums[i]);
    }
    long long int max = nums[0];
  }
  vector<long long int> mins(n);
  for(int i = 0; i < n; ++i){
    int min = -1;
    bool flag = true;
    for(int j = 0; j < m; ++j){
      if(flag || min > dp[j][i]){
        min = dp[j][i];
      }
    }
    mins[i] = min;
  }
  int Max = 0;
  bool F = true;
  for(int i = 0; i < n; ++i){
    if(F||Max<mins[i]){
      Max = mins[i];
    }
  }
  cout << max(0,Max) << endl;

}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
  int n, m; long long int k; cin >> n >> m >> k;
  vector<long long int> nums1(n);
  for(int i = 0; i < n; ++i){
    cin >> nums1[i];
  }
  bool flag = true;
  vector<vector<long long int>> dp(n, vector<long long int>(m));
  for(int I = 0; I < m; ++I){
    vector<long long int> nums = nums1;
    for(int j = 0; j < n; ++j){
      if(j%m==I){
        nums[j]-=k;
      }
    }
    dp[I][0] = nums[0];
    for(int i = 1; i < n; ++i){
      dp[I][i]=max(dp[I][i-1]+nums[i], nums[i]);
    }
    long long int max = nums[0];
    // for(int i = 0; i < n; ++i){
    //   cout << dp[I][i] << " ";
    // }
    // cout << endl;
  }
  vector<long long int> mins(n);
  for(int i = 0; i < n; ++i){
    int min = -1;
    bool flag = true;
    for(int j = 0; j < m; ++j){
      if(flag || min > dp[j][i]){
        min = dp[j][i];
      }
    }
    mins[i] = min;
  }
  int Max = 0;
  bool F = true;
  for(int i = 0; i < n; ++i){
    if(F||Max<mins[i]){
      Max = mins[i];
    }
  }
  cout << max(0,Max) << endl;

}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
  int n, m; long long int k; cin >> n >> m >> k;
  vector<long long int> nums1(n);
  for(int i = 0; i < n; ++i){
    cin >> nums1[i];
  }
  bool flag = true;
  vector<vector<long long int>> dp(m, vector<long long int>(n));
  for(int I = 0; I < m; ++I){
    vector<long long int> nums = nums1;
    for(int j = 0; j < n; ++j){
      if(j%m==I){
        nums[j]-=k;
      }
    }
    dp[I][0] = nums[0];
    // cout << dp[I][0] << " ";
    for(int i = 1; i < n; ++i){
      dp[I][i]=max(dp[I][i-1]+nums[i], nums[i]);
      // cout << dp[I][i] << " ";
    }
    // cout << endl;
    long long int max = nums[0];
  }

  vector<long long int> mins(n);
  for(int i = 0; i < n; ++i){
    long long int min = -1;
    bool flag = true;
    for(int j = 0; j < m; ++j){
      if(flag || min > dp[j][i]){
        min = dp[j][i];
        flag=false;
      }
    }
    mins[i] = min;
  }
  // for(int i = 0; i < n; ++i){
  //   cout << mins[i] << " ";
  // }
  // cout << endl;
  int Max = 0;
  bool F = true;
  for(int i = 0; i < n; ++i){
    if(F||Max<mins[i]){
      Max = mins[i];
      F = false;
    }
  }
  cout << max(0,Max) << endl;

}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

long long max(long long a, long long b){return a > b ? a : b;}
long long min(long long a, long long b){return a < b ? a : b;}
void swap(long long &a, long long &b){long long tmp = a;a = b;b = tmp;}
long long lowbit(long long &x){return x & (-x);}
void read(long long &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}

const long long INF = 0x3f3f3f3f;
const long long MAXN = 300000 + 10;
const long long MAXM = 10;

long long dp[MAXN][MAXM + 10], n, m, k, a[MAXN];
//dp[i][j]表示以i为结尾或者空串，长度余数为j的最大值
//dp[i][j] = dp[i - 1][j - 1] + a[i]
//dp[i][j] = dp[i - 1][j - 1] + a[i] - k  if j == 0

int main()
{
	read(n), read(m), read(k);
	long long ans = 0;
	for(long long i = 1;i <= n;++ i) read(a[i]);

	for(int i = 1;i <= n;++ i)
		for(int j = 0;j < m;++ j)
			dp[i][j] = -INF;
	dp[0][m - 1] = 0;

	for(long long i = 1;i <= n;++ i)
		for(long long j = 0;j < m;++ j)
		{
			if(j == 0)
				dp[i][j] = max(dp[i - 1][m - 1] + a[i] , 0) - k;
			else
				dp[i][j] = dp[i - 1][j - 1] + a[i];
			ans = max(ans, dp[i][j]);
		}
	printf("%I64d", ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

long long max(long long a, long long b){return a > b ? a : b;}
long long min(long long a, long long b){return a < b ? a : b;}
void swap(long long &a, long long &b){long long tmp = a;a = b;b = tmp;}
long long lowbit(long long &x){return x & (-x);}
void read(long long &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}

const long long INF = 0x3f3f3f3f;
const long long MAXN = 300000 + 10;
const long long MAXM = 10;

long long dp[MAXN][MAXM + 10], n, m, k, a[MAXN];
//dp[i][j]表示以i为结尾或者空串，长度余数为j的最大值
//dp[i][j] = dp[i - 1][j - 1] + a[i]
//dp[i][j] = dp[i - 1][j - 1] + a[i] - k  if j == 0

int main()
{
	read(n), read(m), read(k);
	long long ans = 0;
	for(long long i = 1;i <= n;++ i) read(a[i]);

	for(int i = 1;i <= n;++ i)
		for(int j = 0;j < m;++ j)
			dp[i][j] = -INF;
	dp[0][m - 1] = 0;

	for(long long i = 1;i <= n;++ i)
		for(long long j = 0;j < m;++ j)
		{
			if(j == 0)
				dp[i][j] = max(dp[i - 1][m - 1] + a[i] , a[i]) - k;
			else
				dp[i][j] = dp[i - 1][j - 1] + a[i];
			ans = max(ans, dp[i][j]);
		}
	printf("%I64d", ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

long long max(long long a, long long b){return a > b ? a : b;}
long long min(long long a, long long b){return a < b ? a : b;}
void swap(long long &a, long long &b){long long tmp = a;a = b;b = tmp;}
long long lowbit(long long &x){return x & (-x);}
void read(long long &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}

const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 300000 + 10;
const long long MAXM = 10;

long long dp[MAXN][MAXM + 10], n, m, k, a[MAXN];
//dp[i][j]表示以i为结尾或者空串，长度余数为j的最大值
//dp[i][j] = dp[i - 1][j - 1] + a[i]
//dp[i][j] = dp[i - 1][j - 1] + a[i] - k

int main()
{
	read(n), read(m), read(k);
	long long ans = 0;
	for(long long i = 1;i <= n;++ i) read(a[i]);

	for(int i = 0;i <= n;++ i)
		for(int j = 0;j < m;++ j)
			dp[i][j] = -INF;

	for(long long i = 1;i <= n;++ i)
		for(long long j = 0;j < m;++ j)
		{
			if(j == 1 || m == 1)
				dp[i][j] = max(dp[i - 1][0], 0) + a[i] - k;
			else if(j == 0)
				dp[i][j] = dp[i - 1][m - 1] + a[i];
			else
				dp[i][j] = dp[i - 1][j - 1] + a[i];
			ans = max(ans, dp[i][j]);
		}
	printf("%I64d", ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

long long max(long long a, long long b){return a > b ? a : b;}
long long min(long long a, long long b){return a < b ? a : b;}
void swap(long long &a, long long &b){long long tmp = a;a = b;b = tmp;}
long long lowbit(long long &x){return x & (-x);}
void read(long long &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}

const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 300000 + 10;
const long long MAXM = 10;

long long dp[MAXN][MAXM + 10], n, m, k, a[MAXN];
//dp[i][j]表示以i为结尾或者空串，长度余数为j的最大值
//dp[i][j] = dp[i - 1][j - 1] + a[i]
//dp[i][j] = dp[i - 1][j - 1] + a[i] - k

int main()
{
	read(n), read(m), read(k);
	long long ans = 0;
	for(long long i = 1;i <= n;++ i) read(a[i]);

	for(int i = 0;i <= n;++ i)
		for(int j = 0;j < m;++ j)
			dp[i][j] = -INF;
	dp[0][m - 1] = 0;

	for(long long i = 1;i <= n;++ i)
		for(long long j = 0;j < m;++ j)
		{
			if(j == 1 || m == 1)
				dp[i][j] = max(dp[i - 1][0], 0) + a[i] - k;
			else if(j == 0)
				dp[i][j] = dp[i - 1][m - 1] + a[i];
			else
				dp[i][j] = dp[i - 1][j - 1] + a[i];
			ans = max(ans, dp[i][j]);
		}
	printf("%I64d", ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

long long max(long long a, long long b){return a > b ? a : b;}
long long min(long long a, long long b){return a < b ? a : b;}
void swap(long long &a, long long &b){long long tmp = a;a = b;b = tmp;}
long long lowbit(long long &x){return x & (-x);}
void read(long long &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}

const long long INF = 0x3f3f3f3f;
const long long MAXN = 300000 + 10;
const long long MAXM = 10;

long long dp[MAXN][MAXM + 10], n, m, k, a[MAXN];
//dp[i][j]表示以i为结尾或者空串，长度余数为j的最大值
//dp[i][j] = dp[i - 1][j - 1] + a[i]
//dp[i][j] = dp[i - 1][j - 1] + a[i] - k  if j == 0

int main()
{
	read(n), read(m), read(k);
	long long ans = 0;
	for(long long i = 1;i <= n;++ i) read(a[i]);

	for(int i = 1;i <= n;++ i)
		for(int j = 0;j < m;++ j)
			dp[i][j] = -INF;
	dp[0][m - 1] = 0;

	for(long long i = 1;i <= n;++ i)
		for(long long j = 0;j < m && j <= i;++ j)
		{
			if(j == 0)
				dp[i][j] = max(dp[i - 1][m - 1] + a[i] , 0) - k;
			else
				dp[i][j] = dp[i - 1][j - 1] + a[i];
			ans = max(ans, dp[i][j]);
		}
	printf("%I64d", ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

long long max(long long a, long long b){return a > b ? a : b;}
long long min(long long a, long long b){return a < b ? a : b;}
void swap(long long &a, long long &b){long long tmp = a;a = b;b = tmp;}
long long lowbit(long long &x){return x & (-x);}
void read(long long &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}

const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 300000 + 10;
const long long MAXM = 10;

long long dp[MAXN][MAXM + 10], n, m, k, a[MAXN];
//dp[i][j]表示以i为结尾或者空串，长度余数为j的最大值
//dp[i][j] = dp[i - 1][j - 1] + a[i]
//dp[i][j] = dp[i - 1][j - 1] + a[i] - k  if j == 0

int main()
{
	read(n), read(m), read(k);
	long long ans = 0;
	for(long long i = 1;i <= n;++ i) read(a[i]);

	for(int i = 0;i <= n;++ i)
		for(int j = 0;j < m;++ j)
			dp[i][j] = -INF;
	dp[0][m - 1] = 0;

	for(long long i = 1;i <= n;++ i)
		for(long long j = 0;j < m;++ j)
		{
			if(j == 0)
				dp[i][j] = max(dp[i - 1][m - 1] + a[i] , a[i]) - k;
			else
				dp[i][j] = dp[i - 1][j - 1] + a[i];
			ans = max(ans, dp[i][j]);
		}
	printf("%I64d", ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

long long max(long long a, long long b){return a > b ? a : b;}
long long min(long long a, long long b){return a < b ? a : b;}
void swap(long long &a, long long &b){long long tmp = a;a = b;b = tmp;}
long long lowbit(long long &x){return x & (-x);}
void read(long long &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}

const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 300000 + 10;
const long long MAXM = 10;

long long dp[MAXN][MAXM + 10], n, m, k, a[MAXN];
//dp[i][j]表示以i为结尾或者空串，长度余数为j的最大值
//dp[i][j] = dp[i - 1][j - 1] + a[i]
//dp[i][j] = dp[i - 1][j - 1] + a[i] - k  if j == 0

int main()
{
	read(n), read(m), read(k);
	long long ans = 0;
	for(long long i = 1;i <= n;++ i) read(a[i]);

	for(int i = 1;i <= n;++ i)
		for(int j = 0;j < m;++ j)
			dp[i][j] = -INF;
	dp[0][m - 1] = 0;

	for(long long i = 1;i <= n;++ i)
		for(long long j = 0;j < m;++ j)
		{
			if(j == 0)
				dp[i][j] = max(dp[i - 1][m - 1] + a[i] , a[i]) - k;
			else
				dp[i][j] = dp[i - 1][j - 1] + a[i];
			ans = max(ans, dp[i][j]);
		}
	printf("%I64d", ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

long long max(long long a, long long b){return a > b ? a : b;}
long long min(long long a, long long b){return a < b ? a : b;}
void swap(long long &a, long long &b){long long tmp = a;a = b;b = tmp;}
long long lowbit(long long &x){return x & (-x);}
void read(long long &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}

const long long INF = 0x3f3f3f3f;
const long long MAXN = 300000 + 10;
const long long MAXM = 10;

long long dp[MAXN][MAXM + 10], n, m, k, a[MAXN];
//dp[i][j]表示以i为结尾或者空串，长度余数为j的最大值
//dp[i][j] = dp[i - 1][j - 1] + a[i]
//dp[i][j] = dp[i - 1][j - 1] + a[i] - k  if j == 0

int main()
{
	read(n), read(m), read(k);
	long long ans = 0;
	for(long long i = 1;i <= n;++ i) read(a[i]);

	for(long long i = 1;i <= n;++ i)
		for(long long j = 0;j < m && j <= i;++ j)
		{
			if(j == 0)
				dp[i][j] = max(dp[i - 1][m - 1] + a[i] , 0) - k;
			else
				dp[i][j] = dp[i - 1][j - 1] + a[i];
			ans = max(ans, dp[i][j]);
		}
	printf("%I64d", ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

long long max(long long a, long long b){return a > b ? a : b;}
long long min(long long a, long long b){return a < b ? a : b;}
void swap(long long &a, long long &b){long long tmp = a;a = b;b = tmp;}
long long lowbit(long long &x){return x & (-x);}
void read(long long &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}

const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 300000 + 10;
const long long MAXM = 10;

long long dp[MAXN][MAXM + 10], n, m, k, a[MAXN];
//dp[i][j]表示以i为结尾或者空串，长度余数为j的最大值
//dp[i][j] = dp[i - 1][j - 1] + a[i]
//dp[i][j] = dp[i - 1][j - 1] + a[i] - k

int main()
{
	read(n), read(m), read(k);
	long long ans = 0;
	for(long long i = 1;i <= n;++ i) read(a[i]);

	for(int i = 0;i <= n;++ i)
		for(int j = 0;j < m;++ j)
			dp[i][j] = -INF;
	dp[0][m - 1] = 0;

	for(long long i = 1;i <= n;++ i)
		for(long long j = 0;j < m;++ j)
		{
			if(j == 0)
				dp[i][j] = dp[i - 1][m - 1] + a[i];
			else if(j == 1 || m == 1)
				dp[i][j] = max(dp[i - 1][j - 1], 0) + a[i] - k;
			else
				dp[i][j] = dp[i - 1][j - 1] + a[i];
			ans = max(ans, dp[i][j]);
		}
	printf("%I64d", ans);
	return 0;
}


#pragma comment(linker, "/STACK:1000000000")
#include <cctype>
#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <time.h>
#include <cmath>
#include <vector>
#include <queue>
#include <time.h>
#include <deque>
#include <map>
#include <list>
#include <set>
#include <string>
#include <iomanip>
#include <stack>
#include <iterator>
#include <assert.h>
#include <unordered_set>
#include <unordered_map>

#define PI 3.14159265359

#define forn(i, n) for (i64 i = 0; i < n; ++i)
#define revn(i, n) for (i64 i = n-1; i>=0; --i)
#define cyc(i, s, n) for (i64 i = s; i <= n; ++i)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(x) x.begin(), x.end()
#define sqr(x) ((x)*(x))
#define F first
#define S second
#define eps 1e-8
#define inf 10000007
#define debug(x) cout << x << endl;

using namespace std;
typedef unsigned long long u64;
typedef long long i64;
typedef long double ld;
typedef vector < i64 > vi64;
typedef vector < u64 > vu64;
typedef pair < i64, i64 > pi64;
typedef vector < vi64 > graph;
typedef int huint;

inline i64 nextInt() {
    i64 s = 1, x = 0, c = getc(stdin);
    while (c <= 32)
        c = getc(stdin);
    if (c == '-')
        s = -1, c = getc(stdin);
    while ('0' <= c && c <= '9')
        x = x * 10 + c - '0', c = getc(stdin);
    return x * s;
}

inline void printInt(i64 x) {
    if (x < 0)
        putc('-', stdout), x = -x;
    char s[20];
    i64 n = 0;
    while (x || !n)
        s[n++] = '0' + x % 10, x /= 10;
    while (n--)
        putc(s[n], stdout);
}

#define FILE "sum"
#define N 10000

///////////////////////////////////////////////////
//actual code
///////////////////////////////////////////////////

int main() {
    i64 n, s;
    cin >> n >> s;
    map<i64, i64> a, b;
    forn(i, n) {
        char c; i64 p, q;
        cin >> c >> p >> q;
        if (c=='S') a[p]+=q;
        else b[-p]+=q;
    }
    i64 j = 0;
    map<i64, i64> e;
    for (auto i = a.begin(); i!=a.end(); ++i) e[-i->F]=i->S;
    if (e.begin()!=e.end()) for (auto i = e.begin(); j<s && i!=e.end(); ++i, ++j) cout << "S " << -i->F << ' ' << i->S << '\n';
    j = 0;
    if (b.begin()!=b.end()) for (auto i = b.begin(); j<s && i!=b.end(); ++i, ++j) cout << "B " << -i->F << ' ' << i->S << '\n';
}


#pragma comment(linker, "/STACK:1000000000")
#include <cctype>
#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <time.h>
#include <cmath>
#include <vector>
#include <queue>
#include <time.h>
#include <deque>
#include <map>
#include <list>
#include <set>
#include <string>
#include <iomanip>
#include <stack>
#include <iterator>
#include <assert.h>
#include <unordered_set>
#include <unordered_map>

#define PI 3.14159265359

#define forn(i, n) for (i64 i = 0; i < n; ++i)
#define revn(i, n) for (i64 i = n-1; i>=0; --i)
#define cyc(i, s, n) for (i64 i = s; i <= n; ++i)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(x) x.begin(), x.end()
#define sqr(x) ((x)*(x))
#define F first
#define S second
#define eps 1e-8
#define inf 10000007
#define debug(x) cout << x << endl;

using namespace std;
typedef unsigned long long u64;
typedef long long i64;
typedef long double ld;
typedef vector < i64 > vi64;
typedef vector < u64 > vu64;
typedef pair < i64, i64 > pi64;
typedef vector < vi64 > graph;
typedef int huint;

inline i64 nextInt() {
    i64 s = 1, x = 0, c = getc(stdin);
    while (c <= 32)
        c = getc(stdin);
    if (c == '-')
        s = -1, c = getc(stdin);
    while ('0' <= c && c <= '9')
        x = x * 10 + c - '0', c = getc(stdin);
    return x * s;
}

inline void printInt(i64 x) {
    if (x < 0)
        putc('-', stdout), x = -x;
    char s[20];
    i64 n = 0;
    while (x || !n)
        s[n++] = '0' + x % 10, x /= 10;
    while (n--)
        putc(s[n], stdout);
}

#define FILE "sum"
#define N 10000

///////////////////////////////////////////////////
//actual code
///////////////////////////////////////////////////

int main() {
    i64 n, s;
    cin >> n >> s;
    map<i64, i64> a, b;
    forn(i, n) {
        char c; i64 p, q;
        cin >> c >> p >> q;
        if (c=='S') a[p]+=q;
        else b[p] += q;
    }
    vector<pi64> BY, SO;
    for (auto i = a.begin(); i!=a.end(); ++i) SO.pb(mp(i->F, i->S));
    for (auto i = b.begin(); i!=b.end(); ++i) BY.pb(mp(i->F, i->S));
    sort(all(SO)); sort(all(BY));
    i64 j = 0;
    i64 ii = (SO.size()>s)?s-1:SO.size()-1;
    for (i64 i = ii; i>=0 && j<s; --i, ++j) cout << "S " << SO[i].F << ' ' << SO[i].S << '\n';
    j = 0;
    for (i64 i = BY.size()-1; i>=0 && j<s; --i, ++j) cout << "B " << BY[i].F << ' ' << BY[i].S << '\n';
}


#pragma comment(linker, "/STACK:1000000000")
#include <cctype>
#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <time.h>
#include <cmath>
#include <vector>
#include <queue>
#include <time.h>
#include <deque>
#include <map>
#include <list>
#include <set>
#include <string>
#include <iomanip>
#include <stack>
#include <iterator>
#include <assert.h>
#include <unordered_set>
#include <unordered_map>

#define PI 3.14159265359

#define forn(i, n) for (i64 i = 0; i < n; ++i)
#define revn(i, n) for (i64 i = n-1; i>=0; --i)
#define cyc(i, s, n) for (i64 i = s; i <= n; ++i)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(x) x.begin(), x.end()
#define sqr(x) ((x)*(x))
#define F first
#define S second
#define eps 1e-8
#define inf 10000007
#define debug(x) cout << x << endl;

using namespace std;
typedef unsigned long long u64;
typedef long long i64;
typedef long double ld;
typedef vector < i64 > vi64;
typedef vector < u64 > vu64;
typedef pair < i64, i64 > pi64;
typedef vector < vi64 > graph;
typedef int huint;

inline i64 nextInt() {
    i64 s = 1, x = 0, c = getc(stdin);
    while (c <= 32)
        c = getc(stdin);
    if (c == '-')
        s = -1, c = getc(stdin);
    while ('0' <= c && c <= '9')
        x = x * 10 + c - '0', c = getc(stdin);
    return x * s;
}

inline void printInt(i64 x) {
    if (x < 0)
        putc('-', stdout), x = -x;
    char s[20];
    i64 n = 0;
    while (x || !n)
        s[n++] = '0' + x % 10, x /= 10;
    while (n--)
        putc(s[n], stdout);
}

#define FILE "sum"
#define N 10000

///////////////////////////////////////////////////
//actual code
///////////////////////////////////////////////////

int main() {
    i64 n, s;
    cin >> n >> s;
    map<i64, i64> a, b;
    forn(i, n) {
        char c; i64 p, q;
        cin >> c >> p >> q;
        if (c=='S') a[p]+=q;
        else b[p] += q;
    }
    i64 j = 0;
    for (auto i = --a.end(); j<s; --i, ++j) {
        cout << 'S' << ' ' << i->F << ' ' << i->S << '\n';
        if (i==a.begin()) break;
    }
    j = 0;
    for (auto i = --b.end(); j<s; --i, ++j) {
         cout << 'B' << ' ' << i->F << ' ' << i->S << '\n';
        if (i==b.begin()) break;
    }
}


#pragma comment(linker, "/STACK:1000000000")
#include <cctype>
#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <time.h>
#include <cmath>
#include <vector>
#include <queue>
#include <time.h>
#include <deque>
#include <map>
#include <list>
#include <set>
#include <string>
#include <iomanip>
#include <stack>
#include <iterator>
#include <assert.h>
#include <unordered_set>
#include <unordered_map>

#define PI 3.14159265359

#define forn(i, n) for (i64 i = 0; i < n; ++i)
#define revn(i, n) for (i64 i = n-1; i>=0; --i)
#define cyc(i, s, n) for (i64 i = s; i <= n; ++i)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(x) x.begin(), x.end()
#define sqr(x) ((x)*(x))
#define F first
#define S second
#define eps 1e-8
#define inf 10000007
#define debug(x) cout << x << endl;

using namespace std;
typedef unsigned long long u64;
typedef long long i64;
typedef long double ld;
typedef vector < i64 > vi64;
typedef vector < u64 > vu64;
typedef pair < i64, i64 > pi64;
typedef vector < vi64 > graph;
typedef int huint;

inline i64 nextInt() {
    i64 s = 1, x = 0, c = getc(stdin);
    while (c <= 32)
        c = getc(stdin);
    if (c == '-')
        s = -1, c = getc(stdin);
    while ('0' <= c && c <= '9')
        x = x * 10 + c - '0', c = getc(stdin);
    return x * s;
}

inline void printInt(i64 x) {
    if (x < 0)
        putc('-', stdout), x = -x;
    char s[20];
    i64 n = 0;
    while (x || !n)
        s[n++] = '0' + x % 10, x /= 10;
    while (n--)
        putc(s[n], stdout);
}

#define FILE "sum"
#define N 10000

///////////////////////////////////////////////////
//actual code
///////////////////////////////////////////////////

int main() {
    i64 n, s;
    cin >> n >> s;
    map<i64, i64> a, b;
    map<i64, i64> _a, _b;
    forn(i, n) {
        char c; i64 p, q;
        cin >> c >> p >> q;
        if (c=='S') a[p]+=q, _a[p]++;
        else b[p] += q, _b[p]++;
    }
    vector<pi64> BY, SO, plus_so, plus_by;
    for (auto i = a.begin(); i!=a.end(); ++i) if (_a[i->F]>1) SO.pb(mp(i->F, i->S));
    if (SO.size()<s) for (auto i = a.begin(); i!=a.end(); ++i) if (_a[i->F]==1) SO.pb(mp(i->F, i->S));
    for (auto i = b.begin(); i!=b.end(); ++i) if (_b[i->F]>1) BY.pb(mp(i->F, i->S));
    if (BY.size()<s) for (auto i = b.begin(); i!=b.end(); ++i) if (_b[i->F]==1) BY.pb(mp(i->F, i->S));
    sort(all(SO)); sort(all(BY));
    i64 j = 0;
    i64 ii = (SO.size()>s)?s-1:SO.size()-1;
    for (i64 i = ii; i>=0 && j<s; --i, ++j) cout << "S " << SO[i].F << ' ' << SO[i].S << '\n';
    j = 0;
    for (i64 i = BY.size()-1; i>=0 && j<s; --i, ++j) cout << "B " << BY[i].F << ' ' << BY[i].S << '\n';/*
    i64 a,b,c,l;
    cin >> a >> b >> c >> l;
    i64 ans = 0;
    cyc(i, 0, l) {
        i64 AC = a+i;
        i64 l =
    }*/
}


#pragma comment(linker, "/STACK:1000000000")
#include <cctype>
#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <time.h>
#include <cmath>
#include <vector>
#include <queue>
#include <time.h>
#include <deque>
#include <map>
#include <list>
#include <set>
#include <string>
#include <iomanip>
#include <stack>
#include <iterator>
#include <assert.h>
#include <unordered_set>
#include <unordered_map>

#define PI 3.14159265359

#define forn(i, n) for (i64 i = 0; i < n; ++i)
#define revn(i, n) for (i64 i = n-1; i>=0; --i)
#define cyc(i, s, n) for (i64 i = s; i <= n; ++i)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(x) x.begin(), x.end()
#define sqr(x) ((x)*(x))
#define F first
#define S second
#define eps 1e-8
#define inf 10000007
#define debug(x) cout << x << endl;

using namespace std;
typedef unsigned long long u64;
typedef long long i64;
typedef long double ld;
typedef vector < i64 > vi64;
typedef vector < u64 > vu64;
typedef pair < i64, i64 > pi64;
typedef vector < vi64 > graph;
typedef int huint;

inline i64 nextInt() {
    i64 s = 1, x = 0, c = getc(stdin);
    while (c <= 32)
        c = getc(stdin);
    if (c == '-')
        s = -1, c = getc(stdin);
    while ('0' <= c && c <= '9')
        x = x * 10 + c - '0', c = getc(stdin);
    return x * s;
}

inline void printInt(i64 x) {
    if (x < 0)
        putc('-', stdout), x = -x;
    char s[20];
    i64 n = 0;
    while (x || !n)
        s[n++] = '0' + x % 10, x /= 10;
    while (n--)
        putc(s[n], stdout);
}

#define FILE "sum"
#define N 10000

///////////////////////////////////////////////////
//actual code
///////////////////////////////////////////////////

int main() {
    i64 n, s;
    cin >> n >> s;
    map<i64, i64> a, b;
    forn(i, n) {
        char c; i64 p, q;
        cin >> c >> p >> q;
        if (c=='S') a[p]+=q;
        else b[p] += q;
    }
    vector<pi64> BY, SO;
    for (auto i = a.begin(); i!=a.end(); ++i) SO.pb(mp(i->F, i->S));
    for (auto i = b.begin(); i!=b.end(); ++i) BY.pb(mp(i->F, i->S));
    sort(all(SO)); sort(all(BY));
    i64 j = 0;
    i64 ii = (SO.size()>s)?s-1:SO.size()-1;
    for (i64 i = SO.size()-1; i>=0 && j<s; --i, ++j) cout << "S " << SO[i].F << ' ' << SO[i].S << '\n';
    j = 0;
    for (i64 i = BY.size()-1; i>=0 && j<s; --i, ++j) cout << "B " << BY[i].F << ' ' << BY[i].S << '\n';
}


#pragma comment(linker, "/STACK:1000000000")
#include <cctype>
#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <time.h>
#include <cmath>
#include <vector>
#include <queue>
#include <time.h>
#include <deque>
#include <map>
#include <list>
#include <set>
#include <string>
#include <iomanip>
#include <stack>
#include <iterator>
#include <assert.h>
#include <unordered_set>
#include <unordered_map>

#define PI 3.14159265359

#define forn(i, n) for (i64 i = 0; i < n; ++i)
#define revn(i, n) for (i64 i = n-1; i>=0; --i)
#define cyc(i, s, n) for (i64 i = s; i <= n; ++i)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(x) x.begin(), x.end()
#define sqr(x) ((x)*(x))
#define F first
#define S second
#define eps 1e-8
#define inf 10000007
#define debug(x) cout << x << endl;

using namespace std;
typedef unsigned long long u64;
typedef long long i64;
typedef long double ld;
typedef vector < i64 > vi64;
typedef vector < u64 > vu64;
typedef pair < i64, i64 > pi64;
typedef vector < vi64 > graph;
typedef int huint;

inline i64 nextInt() {
    i64 s = 1, x = 0, c = getc(stdin);
    while (c <= 32)
        c = getc(stdin);
    if (c == '-')
        s = -1, c = getc(stdin);
    while ('0' <= c && c <= '9')
        x = x * 10 + c - '0', c = getc(stdin);
    return x * s;
}

inline void printInt(i64 x) {
    if (x < 0)
        putc('-', stdout), x = -x;
    char s[20];
    i64 n = 0;
    while (x || !n)
        s[n++] = '0' + x % 10, x /= 10;
    while (n--)
        putc(s[n], stdout);
}

#define FILE "sum"
#define N 10000

///////////////////////////////////////////////////
//actual code
///////////////////////////////////////////////////

int main() {
    i64 n, s;
    cin >> n >> s;
    map<i64, i64> a, b;
    forn(i, n) {
        char c; i64 p, q;
        cin >> c >> p >> q;
        if (c=='S') a[p]+=q;
        else b[-p]+=q;
    }
    i64 j = 0;
    map<i64, i64> e;

    for (auto i = a.begin(); i!=a.end() && j<s; ++i, j++) e[-i->F]=i->S;
    j=0;
    if (e.begin()!=e.end()) for (auto i = e.begin(); j<s && i!=e.end(); ++i, ++j) cout << "S " << -i->F << ' ' << i->S << '\n';
    j = 0;
    if (b.begin()!=b.end()) for (auto i = b.begin(); j<s && i!=b.end(); ++i, ++j) cout << "B " << -i->F << ' ' << i->S << '\n';
}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
        int n, s, bs = 0;

        cin >> n >> s;

        char* d = new char[n];

        std::vector<std::pair<int,int>> pq;

    //std::sort(baryProc.begin(),baryProc.end());
        int p;
        int q;

        for (int i = 0; i < n; ++i)
        {
                cin >> d[i] >> p >> q;
                pq.push_back(std::make_pair(p, q));

                if (d[i] == 'B')
                {
                        ++bs;
                }
        }

        std::sort(pq.begin(), pq.end());

        int s1 = s;
        for (int i = n - 1; i >= bs; --i)
        {
                int curr_p = pq[i].first;
                int curr_q = pq[i].second;
                for (int j = i - 1; j >= bs; --j)
                {
                        if (pq[j].first == curr_p)
                        {
                                curr_q += pq[j].second;
                        }
                        else
                        {
                                ++j;
                                i = j;
                                break;
                        }
                        if (j == bs)
                        {
                                i = bs -1;
                        }
                }

                cout << "S " << curr_p << " " << curr_q;

                --s1;
                if (s1 == 0)
                {
                        if (bs == 0)
                                break;
                        else
                        {
                                cout << endl;
                                break;
                        }
                }
                else {
                        if (i == bs)
                        {
                                break;
                        }
                        cout << endl;
                }

        }

        int s2 = s;
        for (int i = bs - 1; i >= 0; --i)
        {
                int curr_p = pq[i].first;
                int curr_q = pq[i].second;
                for (int j = i - 1; j >= 0; --j)
                {
                        if (pq[j].first == curr_p)
                        {
                                curr_q += pq[j].second;
                        }
                        else
                        {
                                ++j;
                                i = j;
                                break;
                        }
                        if (j == 0)
                        {
                                i = 0;
                        }
                }

                cout << "B " << curr_p << " " << curr_q;

                --s2;
                if (s2 == 0 || i == 0)
                {
                        break;
                }
                cout << endl;
        }

    return 0;
}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
        int n, s, bs = 0;

        cin >> n >> s;

        char* d = new char[n];

        std::vector<std::pair<int,int>> pq;

    //std::sort(baryProc.begin(),baryProc.end());
        int p;
        int q;

        for (int i = 0; i < n; ++i)
        {
                cin >> d[i] >> p >> q;
                pq.push_back(std::make_pair(p, q));

                if (d[i] == 'B')
                {
                        ++bs;
                }
        }

        std::sort(pq.begin(), pq.end());

        int s1 = s;
        for (int i = n - 1; i >= bs; --i)
        {
                int curr_p = pq[i].first;
                int curr_q = pq[i].second;
                for (int j = i - 1; j >= bs; --j)
                {
                        if (pq[j].first == curr_p)
                        {
                                curr_q += pq[j].second;
                        }
                        else
                        {
                                ++j;
                                i = j;
                                break;
                        }
                        if (j == bs)
                        {
                                i = bs;
                        }
                }

                cout << "S " << curr_p << " " << curr_q;

                --s1;
                if (s1 == 0)
                {
                        if (bs == 0)
                                break;
                        else
                        {
                                cout << endl;
                                break;
                        }
                }
                else {
                        if (i == bs)
                        {
                                break;
                        }
                        cout << endl;
                }

        }

        int s2 = s;
        for (int i = bs - 1; i >= 0; --i)
        {
                int curr_p = pq[i].first;
                int curr_q = pq[i].second;
                for (int j = i - 1; j >= 0; --j)
                {
                        if (pq[j].first == curr_p)
                        {
                                curr_q += pq[j].second;
                        }
                        else
                        {
                                ++j;
                                i = j;
                                break;
                        }
                        if (j == 0)
                        {
                                i = 0;
                        }
                }

                cout << "B " << curr_p << " " << curr_q;

                --s2;
                if (s2 == 0 || i == 0)
                {
                        break;
                }
                cout << endl;
        }

    return 0;
}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
	int n, s, bs = 0;

	cin >> n >> s;

	char* d = new char[n];

	std::vector<std::pair<int,int>> pq;

    //std::sort(baryProc.begin(),baryProc.end());
	int p;
	int q;

	for (int i = 0; i < n; ++i)
	{
		cin >> d[i] >> p >> q;
		pq.push_back(std::make_pair(p, q));

		if (d[i] == 'B')
		{
			++bs;
		}
	}

	std::sort(pq.begin(), pq.end());

	int s1 = s;
	for (int i = n - 1; i >= bs; --i)
	{
		int curr_p = pq[i].first;
		int curr_q = pq[i].second;
		for (int j = i - 1; j >= bs; --j)
		{
			if (pq[j].first == curr_p)
			{
				curr_q += pq[j].second;
			}
			else
			{
				i = ++j;
				break;
			}
		}

		cout << "S " << curr_p << " " << curr_q << endl;

		--s1;
		if (s1 == 0)
		{
			break;
		}
	}

	int s2 = s;
	for (int i = bs - 1; i >= 0; --i)
	{
		int curr_p = pq[i].first;
		int curr_q = pq[i].second;
		for (int j = i - 1; j >= 0; --j)
		{
			if (pq[j].first == curr_p)
			{
				curr_q += pq[j].second;
			}
			else
			{
				++j;
				i = j;
				break;
			}
		}

		cout << "B " << curr_p << " " << curr_q;

		--s2;
		if (s2 == 0 || i == 0)
		{
			break;
		}
		cout << endl;
	}

    return 0;
}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
	int n, s, bs = 0;

	cin >> n >> s;

	char* d = new char[n];

	std::vector<std::pair<int,int>> pq;

    //std::sort(baryProc.begin(),baryProc.end());
	int p;
	int q;

	for (int i = 0; i < n; ++i)
	{
		cin >> d[i] >> p >> q;
		pq.push_back(std::make_pair(p, q));

		if (d[i] == 'B')
		{
			++bs;
		}
	}

	std::sort(pq.begin(), pq.end());

	int s1 = s;
	for (int i = n - 1; i >= bs; --i)
	{
		int curr_p = pq[i].first;
		int curr_q = pq[i].second;
		for (int j = i - 1; j >= bs; --j)
		{
			if (pq[j].first == curr_p)
			{
				curr_q += pq[j].second;
			}
			else
			{
				i = ++j;
				break;
			}
		}

		cout << "S " << curr_p << " " << curr_q << endl;

		--s1;
		if (s1 == 0)
		{
			break;
		}
	}

	int s2 = s;
	for (int i = bs - 1; i >= 0; --i)
	{
		int curr_p = pq[i].first;
		int curr_q = pq[i].second;
		for (int j = i - 1; j >= 0; --j)
		{
			if (pq[j].first == curr_p)
			{
				curr_q += pq[j].second;
			}
			else
			{
				i = ++j;
				break;
			}
		}

		cout << "B " << curr_p << " " << curr_q;

		--s2;
		if (s2 == 0)
		{
			break;
		}
		cout << endl;
	}

    return 0;
}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
        int n, s, bs = 0;

        cin >> n >> s;

		char d;

        std::vector<std::pair<int,int>> pq;

        int p, q;

        for (int i = 0; i < n; ++i)
        {
                cin >> d >> p >> q;
                pq.push_back(std::make_pair(p, q));

                if (d == 'B')
                {
                        ++bs;
                }
        }

        std::sort(pq.begin(), pq.end());

        int s1 = s;
        for (int i = n - 1; i >= bs; --i)
        {
                int curr_p = pq[i].first;
                int curr_q = pq[i].second;
                for (int j = i - 1; j >= bs; --j)
                {
                        if (pq[j].first == curr_p)
                        {
                                curr_q += pq[j].second;
                        }
                        else
                        {
                                i = j + 1;
                                break;
                        }
                        if (j == bs)
                        {
                                i = bs;
                        }
                }

                cout << "S " << curr_p << " " << curr_q;

                --s1;
                if (s1 == 0)
                {
                        if (bs == 0)
                                break;
                        else
                        {
                                cout << endl;
                                break;
                        }
                }
                else
				{
                        if (i == bs)
                        {
                                break;
                        }
                        cout << endl;
                }

        }

        int s2 = s;
        for (int i = bs - 1; i >= 0; --i)
        {
                int curr_p = pq[i].first;
                int curr_q = pq[i].second;
                for (int j = i - 1; j >= 0; --j)
                {
                        if (pq[j].first == curr_p)
                        {
                                curr_q += pq[j].second;
                        }
                        else
                        {
                                i = j + 1;
                                break;
                        }
                        if (j == 0)
                        {
                                i = 0;
                        }
                }

                cout << "B " << curr_p << " " << curr_q;

                --s2;
                if (s2 == 0 || i == 0)
                {
                        break;
                }
                cout << endl;
        }

    return 0;
}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
        int n, s, bs = 0;

        cin >> n >> s;

		char d;

        std::vector<std::pair<int,int>> pq;

        int p, q;

        for (int i = 0; i < n; ++i)
        {
                cin >> d >> p >> q;
                pq.push_back(std::make_pair(p, q));

                if (d == 'B')
                {
                        ++bs;
                }
        }

        std::sort(pq.begin(), pq.end());

        int s1 = s;
        for (int i = n - 1; i >= bs; --i)
        {
                int curr_p = pq[i].first;
                int curr_q = pq[i].second;
                for (int j = i - 1; j >= bs; --j)
                {
                        if (pq[j].first == curr_p)
                        {
                                curr_q += pq[j].second;
                        }
                        else
                        {
                                i = j + 1;
                                break;
                        }
                        if (j == bs)
                        {
                                i = bs;
                        }
                }

                cout << "S " << curr_p << " " << curr_q;

                --s1;
                if (s1 == 0)
                {
                        if (bs == 0)
                                break;
                        else
                        {
                                cout << endl;
                                break;
                        }
                }
                else
				{
                        if (i == bs && bs == 0)
                        {
                                break;
                        }
                        cout << endl;
                }

        }

        int s2 = s;
        for (int i = bs - 1; i >= 0; --i)
        {
                int curr_p = pq[i].first;
                int curr_q = pq[i].second;
                for (int j = i - 1; j >= 0; --j)
                {
                        if (pq[j].first == curr_p)
                        {
                                curr_q += pq[j].second;
                        }
                        else
                        {
                                i = j + 1;
                                break;
                        }
                        if (j == 0)
                        {
                                i = 0;
                        }
                }

                cout << "B " << curr_p << " " << curr_q;

                --s2;
                if (s2 == 0 || i == 0)
                {
                        break;
                }
                cout << endl;
        }

    return 0;
}