Problem p00439 (C++) — buggy submission, verdict: Time Limit Exceeded

// include----------------------------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>

// using
// namespace--------------------------------------------------------------------------------------------------------------
using namespace std;

// define-----------------------------------------------------------------------------------------------------------------------
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, s, n) for (int i = s; i < (int)n; i++)
#define per(i, n) for (int i = n; i >= 0; i--)
#define ROF(i, s, n) for (int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\\n"
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define EACH(i, n)                                                             \\
  for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define INF 1 << 25
#define CLR(a) memset((a), 0, sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << "\\n";
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << "\\n";
#define sq(n) (n) * (n)

// typedef----------------------------------------------------------------------------------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
// const------------------------------------------------------------------------------------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

// global-----------------------------------------------------------------------------------------------------------------------
int n, m, k;
VI a, b, c;
#define mod 1000000007
// function--------------------------------------------------------------------------------------------------------------------
int main() {
  while (true) {
    scanf("%d %d", &n, &m);
    if (!n && !m) {
      return 0;
    }
    rep(i, n) {
      scanf("%d", &k);
      a.push_back(k);
    }
    int mx = 0;
    int d = 0;
    rep(i, m) {
      d += a[i];
      mx = d;
    }
    FOR(i, 1, n - m) {
      d -= a[i - 1];
      d += a[i + m - 1];
      mx = max(mx, d);
    }
    printf("%d\\n", mx);
    a = VI();
  }
  return 0;
}

Error: TLE

Fixed submission:

// include----------------------------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>

// using
// namespace--------------------------------------------------------------------------------------------------------------
using namespace std;

// define-----------------------------------------------------------------------------------------------------------------------
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, s, n) for (int i = s; i < (int)n; i++)
#define per(i, n) for (int i = n; i >= 0; i--)
#define ROF(i, s, n) for (int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\\n"
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define EACH(i, n)                                                             \\
  for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define INF 1 << 25
#define CLR(a) memset((a), 0, sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << "\\n";
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << "\\n";
#define sq(n) (n) * (n)

// typedef----------------------------------------------------------------------------------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
// const------------------------------------------------------------------------------------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

// global-----------------------------------------------------------------------------------------------------------------------
int n, m, k;
VI a, b, c;
#define mod 1000000007
// function--------------------------------------------------------------------------------------------------------------------
int main() {
  while (true) {
    scanf("%d %d", &n, &m);
    if (!n && !m) {
      return 0;
    }
    int e = 0;
    rep(i, n) {
      scanf("%d", &k);
      a.push_back(k);
      e = max(e, k);
    }
    int mx = 0;
    rep(i, n - m + 1) {
      int d = 0;
      rep(j, m) {
        d += a[i + j];
        if (mx >= d + e * (m - j - 1)) {
          break;
        }
      }
      mx = max(mx, d);
    }
    printf("%d\\n", mx);
    a = VI();
  }
  return 0;
}