Problem p02780 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;
const int INF = 1000000000;

// long long
using ll = long long;

// 出力系
#define print(x) cout << x << endl
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl

// begin() end()
#define all(x) (x).begin(), (x).end()

// for
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

// 最大公約数
unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

// 最小公倍数
unsigned lcm(unsigned a, unsigned b) { return a / gcd(a, b) * b; }

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

double ki(double a) {
  int su = 0;
  for (int i = 1; i <= a; i++)
    su += i;
  return su / a;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> p(N);
  REP(i, N) cin >> p[i];
  double ans = 0;
  if (N == 1) {
    ans = ki(p[0]);
    cout << fixed << setprecision(10) << ans << endl;
    return 0;
  }
  ll sum = 0;
  REP(i, K) { sum += p[i + K]; }
  ll now = sum;
  int pos = 0;

  for (int i = 1; i + (K - 1) < N; i++) {
    now -= p[i - 1];
    now += p[i + (K - 1)];
    if (sum < now)
      pos = i;
    sum = max(sum, now);
  }
  for (int i = pos; i <= pos + (K - 1); i++) {
    ans += ki(p[i]);
  }
  cout << fixed << setprecision(10) << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;
const int INF = 1000000000;

// long long
using ll = long long;

// 出力系
#define print(x) cout << x << endl
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl

// begin() end()
#define all(x) (x).begin(), (x).end()

// for
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

// 最大公約数
unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

// 最小公倍数
unsigned lcm(unsigned a, unsigned b) { return a / gcd(a, b) * b; }

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

double ki(double a) {
  int su = 0;
  for (int i = 1; i <= a; i++)
    su += i;
  return su / a;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> p(N);
  REP(i, N) cin >> p[i];
  double ans = 0;
  if (N == 1) {
    ans = ki(p[0]);
    cout << fixed << setprecision(10) << ans << endl;
    return 0;
  }
  ll sum = 0;
  REP(i, K) { sum += p[i]; }
  ll now = sum;
  int pos = 0;

  for (int i = 1; i + (K - 1) < N; i++) {
    now -= p[i - 1];
    now += p[i + (K - 1)];
    if (sum < now)
      pos = i;
    sum = max(sum, now);
  }
  for (int i = pos; i <= pos + (K - 1); i++) {
    ans += ki(p[i]);
  }
  cout << fixed << setprecision(10) << ans << endl;
}