Problem p02780 (C++)

Buggy submission (status: Time Limit Exceeded):
// include
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
//------------------------------------------
// typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<char> VC;
typedef vector<double> VD;
typedef vector<long long> VL;
typedef vector<VI> VVI;
typedef vector<VB> VVB;
typedef vector<string> VS;
typedef vector<VL> VVL;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;
typedef pair<string, string> PSS;
//------------------------------------------
// comparison
#define C_MAX(a, b) ((a) > (b) ? (a) : (b))
#define C_MIN(a, b) ((a) < (b) ? (a) : (b))
#define C_ABS(a, b) ((a) < (b) ? (b) - (a) : (a) - (b))
//------------------------------------------
// container util
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define SUMI(obj) accumulate((obj).begin(), (obj).end(), 0)
#define SUMD(obj) accumulate((obj).begin(), (obj).end(), 0.)
#define SUML(obj) accumulate((obj).begin(), (obj).end(), 0ll)
#define UB(obj, n) upper_bound((obj).begin(), (obj).end(), n)
#define LB(obj, n) lower_bound((obj).begin(), (obj).end(), n)
#define BS(v, n) binary_search(ALL(v), (n))
#define PB push_back
#define MP make_pair
//------------------------------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FORL(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define RFORL(i, a, b) for (ll i = ll(b) - 1; i >= ll(a); --i)
#define REPL(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREPL(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOREACH(x, v) for (auto &(x) : (v))
#define FORITER(x, v) for (auto(x) = (v).begin(); (x) != (v).end(); ++(x))
//------------------------------------------
// input output
#define GL(s) getline(cin, (s))
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define IN(...) GET_MACRO(__VA_ARGS__, IN3, IN2, IN1)(__VA_ARGS__)
#define IN1(n) std::cin >> (n);
#define IN2(n, m) std::cin >> (n) >> (m);
#define IN3(n, m, l) std::cin >> (n) >> (m) >> (l);
#define OUT(d) std::cout << (d);
#define OUT_L(d) std::cout << (d) << endl;
#define OUT_Y std::cout << "Yes" << endl;
#define OUT_N std::cout << "No" << endl;
#define FOUT(n, d) std::cout << std::fixed << std::setprecision(n) << (d);
#define EL() std::cout << "\n";
//------------------------------------------
// constant
#define MAX 200000
#define MOD 1000000007
//------------------------------------------

// 数値・文字列
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
inline ll toLongLong(string s) {
  ll v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
inline VC toVC(string s) {
  VC data(s.begin(), s.end());
  return data;
}
//------------------------------------------
// extended permutation combination
// possible implementation introduced at
// http://en.cppreference.com/w/cpp/algorithm/rotate with slight modification to
// handle parted ranges
template <typename FI>
void parted_rotate(FI first1, FI last1, FI first2, FI last2) {
  if (first1 == last1 || first2 == last2)
    return;
  FI next = first2;
  while (first1 != next) {
    std::iter_swap(first1++, next++);
    if (first1 == last1)
      first1 = first2;
    if (next == last2) {
      next = first2;
    } else if (first1 == first2) {
      first2 = next;
    }
  }
}
template <typename BI>
bool next_combination_imp(BI first1, BI last1, BI first2, BI last2) {
  if (first1 == last1 || first2 == last2)
    return false;
  auto target = last1;
  --target;
  auto last_elem = last2;
  --last_elem;
  // find right-most incrementable element: target
  while (target != first1 && !(*target < *last_elem))
    --target;
  if (target == first1 && !(*target < *last_elem)) {
    parted_rotate(first1, last1, first2, last2);
    return false;
  }
  // find the next value to be incremented: *next
  auto next = first2;
  while (!(*target < *next))
    ++next;
  std::iter_swap(target++, next++);
  parted_rotate(target, last1, next, last2);
  return true;
}
// INVARIANT: is_sorted(first, mid) && is_sorted(mid, last)
template <typename BI> inline bool next_combination(BI first, BI mid, BI last) {
  return next_combination_imp(first, mid, mid, last);
}
// INVARIANT: is_sorted(first, mid) && is_sorted(mid, last)
template <typename BI> inline bool prev_combination(BI first, BI mid, BI last) {
  return next_combination_imp(mid, last, first, mid);
}
//------------------------------------------
// other functions
//------------------------------------------
// a^n mod を二分累乗法で計算する
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
// a^{-1} mod を計算する
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
long long moddiv(long long a, long long b, long long mod) {
  a %= mod;
  return a * modinv(b, mod) % mod;
}
// 任意の二項係数nCkをO(n)->O(1)で求める
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void nCkinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long nCk(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// 最大公約数
template <class T> inline T GCD(const T x, const T y) {
  if (x < 0)
    return GCD(-x, y);
  if (y < 0)
    return GCD(x, -y);
  return (!y) ? x : GCD(y, x % y);
}

// 最小公倍数
template <class T> inline T LCM(const T x, const T y) {
  if (x < 0)
    return LCM(-x, y);
  if (y < 0)
    return LCM(x, -y);
  return x * (y / GCD(x, y));
}
// memo-------------------------------------
/*
//尺取り法

//二分法
vector<int> a = {1, 14, 32, 51, 51, 51, 243, 419, 750, 910};
// index が条件を満たすかどうか
bool isOK(int index, int key) {
    if (a[index] >= key) return true;
    else return false;
}
// 汎用的な二分探索のテンプレ
int binary_search(int key) {
    int ng = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1
    int ok = (int)a.size(); // 「index =
a.size()-1」が条件を満たさないこともあるので、初期値は a.size()

    // ok と ng のどちらが大きいかわからないことを考慮
    while (abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;

        if (isOK(mid, key)) ok = mid;
        else ng = mid;
    }
    return ok;
}
//-----------------------------------------

*/
// main code--------------------------------
#define INF 1LL << 61
void _main() {
  ll n, k;
  IN(n, k)
  VL si(n);
  REP(i, n) IN(si[i]) double out = 0;
  REP(i, n - k + 1) {
    double sum = 0;
    REP(j, k) { sum += (double)(si[i + j] + 1) / 2; }
    out = max(out, sum);
  }
  OUT_L(out);
  return;
}
int main() {
  cout << fixed << setprecision(10);
  nCkinit();
  _main();
  return 0;
}


Fixed submission:
// include
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
//------------------------------------------
// typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<char> VC;
typedef vector<double> VD;
typedef vector<long long> VL;
typedef vector<VI> VVI;
typedef vector<VB> VVB;
typedef vector<string> VS;
typedef vector<VL> VVL;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;
typedef pair<string, string> PSS;
//------------------------------------------
// comparison
#define C_MAX(a, b) ((a) > (b) ? (a) : (b))
#define C_MIN(a, b) ((a) < (b) ? (a) : (b))
#define C_ABS(a, b) ((a) < (b) ? (b) - (a) : (a) - (b))
//------------------------------------------
// container util
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define SUMI(obj) accumulate((obj).begin(), (obj).end(), 0)
#define SUMD(obj) accumulate((obj).begin(), (obj).end(), 0.)
#define SUML(obj) accumulate((obj).begin(), (obj).end(), 0ll)
#define UB(obj, n) upper_bound((obj).begin(), (obj).end(), n)
#define LB(obj, n) lower_bound((obj).begin(), (obj).end(), n)
#define BS(v, n) binary_search(ALL(v), (n))
#define PB push_back
#define MP make_pair
//------------------------------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FORL(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define RFORL(i, a, b) for (ll i = ll(b) - 1; i >= ll(a); --i)
#define REPL(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREPL(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOREACH(x, v) for (auto &(x) : (v))
#define FORITER(x, v) for (auto(x) = (v).begin(); (x) != (v).end(); ++(x))
//------------------------------------------
// input output
#define GL(s) getline(cin, (s))
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define IN(...) GET_MACRO(__VA_ARGS__, IN3, IN2, IN1)(__VA_ARGS__)
#define IN1(n) std::cin >> (n);
#define IN2(n, m) std::cin >> (n) >> (m);
#define IN3(n, m, l) std::cin >> (n) >> (m) >> (l);
#define OUT(d) std::cout << (d);
#define OUT_L(d) std::cout << (d) << endl;
#define OUT_Y std::cout << "Yes" << endl;
#define OUT_N std::cout << "No" << endl;
#define FOUT(n, d) std::cout << std::fixed << std::setprecision(n) << (d);
#define EL() std::cout << "\n";
//------------------------------------------
// constant
#define MAX 200000
#define MOD 1000000007
//------------------------------------------

// 数値・文字列
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
inline ll toLongLong(string s) {
  ll v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
inline VC toVC(string s) {
  VC data(s.begin(), s.end());
  return data;
}
//------------------------------------------
// extended permutation combination
// possible implementation introduced at
// http://en.cppreference.com/w/cpp/algorithm/rotate with slight modification to
// handle parted ranges
template <typename FI>
void parted_rotate(FI first1, FI last1, FI first2, FI last2) {
  if (first1 == last1 || first2 == last2)
    return;
  FI next = first2;
  while (first1 != next) {
    std::iter_swap(first1++, next++);
    if (first1 == last1)
      first1 = first2;
    if (next == last2) {
      next = first2;
    } else if (first1 == first2) {
      first2 = next;
    }
  }
}
template <typename BI>
bool next_combination_imp(BI first1, BI last1, BI first2, BI last2) {
  if (first1 == last1 || first2 == last2)
    return false;
  auto target = last1;
  --target;
  auto last_elem = last2;
  --last_elem;
  // find right-most incrementable element: target
  while (target != first1 && !(*target < *last_elem))
    --target;
  if (target == first1 && !(*target < *last_elem)) {
    parted_rotate(first1, last1, first2, last2);
    return false;
  }
  // find the next value to be incremented: *next
  auto next = first2;
  while (!(*target < *next))
    ++next;
  std::iter_swap(target++, next++);
  parted_rotate(target, last1, next, last2);
  return true;
}
// INVARIANT: is_sorted(first, mid) && is_sorted(mid, last)
template <typename BI> inline bool next_combination(BI first, BI mid, BI last) {
  return next_combination_imp(first, mid, mid, last);
}
// INVARIANT: is_sorted(first, mid) && is_sorted(mid, last)
template <typename BI> inline bool prev_combination(BI first, BI mid, BI last) {
  return next_combination_imp(mid, last, first, mid);
}
//------------------------------------------
// other functions
//------------------------------------------
// a^n mod を二分累乗法で計算する
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
// a^{-1} mod を計算する
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
long long moddiv(long long a, long long b, long long mod) {
  a %= mod;
  return a * modinv(b, mod) % mod;
}
// 任意の二項係数nCkをO(n)->O(1)で求める
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void nCkinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long nCk(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// 最大公約数
template <class T> inline T GCD(const T x, const T y) {
  if (x < 0)
    return GCD(-x, y);
  if (y < 0)
    return GCD(x, -y);
  return (!y) ? x : GCD(y, x % y);
}

// 最小公倍数
template <class T> inline T LCM(const T x, const T y) {
  if (x < 0)
    return LCM(-x, y);
  if (y < 0)
    return LCM(x, -y);
  return x * (y / GCD(x, y));
}
// memo-------------------------------------
/*
//尺取り法

//二分法
vector<int> a = {1, 14, 32, 51, 51, 51, 243, 419, 750, 910};
// index が条件を満たすかどうか
bool isOK(int index, int key) {
    if (a[index] >= key) return true;
    else return false;
}
// 汎用的な二分探索のテンプレ
int binary_search(int key) {
    int ng = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1
    int ok = (int)a.size(); // 「index =
a.size()-1」が条件を満たさないこともあるので、初期値は a.size()

    // ok と ng のどちらが大きいかわからないことを考慮
    while (abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;

        if (isOK(mid, key)) ok = mid;
        else ng = mid;
    }
    return ok;
}
//-----------------------------------------

*/
// main code--------------------------------
#define INF 1LL << 61
void _main() {
  ll n, k;
  IN(n, k)
  VL si(n);
  REP(i, n) IN(si[i]) double out = 0;
  REP(j, k) { out += (double)(si[j] + 1) / 2; }
  double prev = out;
  REP(i, n - k) {
    double next = prev;
    next += (double)(si[i + k] + 1) / 2;
    next -= (double)(si[i] + 1) / 2;
    out = max(next, out);
    prev = next;
  }
  OUT_L(out);
  return;
}
int main() {
  cout << fixed << setprecision(10);
  nCkinit();
  _main();
  return 0;
}