prithivMLmods/Coder-Stat huggingface dataset:
Problem p02780 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[20001];
double calc(int x) {
  int c = (double)x;
  return (c + 1) / 2;
}
int main() {
  memset(a, 0, sizeof(a));
  int n, k;
  cin >> n >> k;

  for (int i = 0; i < n; i++)
    cin >> a[i];
  // sort(a, a+n);
  double temp = 0, mx = 0, index = 0;
  for (int i = 0; i < n; i++) {
    if (index < k) {
      temp += a[i];
      index++;
    } else {
      temp -= a[i - k];
      temp += a[i];
      // index--;
    }
    if (mx < temp) {
      mx = temp;
    }
  }
  // cout << endl;
  mx = (mx + k) / 2;
  printf("%.12f\n", mx);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[200001];

int main() {
  memset(a, 0, sizeof(a));
  int n, k;
  cin >> n >> k;

  for (int i = 0; i < n; i++)
    cin >> a[i];
  // sort(a, a+n);
  double temp = 0, mx = 0, index = 0;
  for (int i = 0; i < n; i++) {
    if (index < k) {
      temp += a[i];
      index++;
    } else {
      temp -= a[i - k];
      temp += a[i];
      // index--;
    }
    if (mx < temp) {
      mx = temp;
    }
  }
  // cout << endl;
  mx = (mx + k) / 2;
  printf("%.12f\n", mx);
  return 0;
}
Problem p02780 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int N = 101101;
int a[N];
int n, k;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int sum = 0;
  for (int i = 1; i <= k; i++)
    sum = sum + a[i];
  int mx = sum;
  for (int i = k + 1; i <= n; i++)
    sum = sum + a[i] - a[i - k], mx = max(mx, sum);
  printf("%lf\n", 1.0 * (mx + k) / 2);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int N = 202202;
int a[N];
int n, k;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int sum = 0;
  for (int i = 1; i <= k; i++)
    sum = sum + a[i];
  int mx = sum;
  for (int i = k + 1; i <= n; i++)
    sum = sum + a[i] - a[i - k], mx = max(mx, sum);
  printf("%lf\n", 1.0 * (mx + k) / 2);
  return 0;
}
Problem p02780 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int inf = 1001001001;

double ev(double p) {
  double sum = 0;
  for (int i = 1; i <= p; i++) {
    sum += i / p;
  }
  return sum;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<double> e(n);
  rep(i, n) {
    double temp;
    cin >> temp;
    e.at(i) = ev(temp);
  }

  double ev_sum = 0;
  rep(i, k) ev_sum += e.at(i);

  vector<double> ans(n - k + 1);
  ans.at(0) = ev_sum;
  for (int i = 0; i < n - k; i++) {
    ev_sum += (-e.at(i) + e.at(i + k));
    ans.at(i + 1) = ev_sum;
  }
  sort(ans.begin(), ans.end());
  printf("%5.7f\n", ans.at(n - k - 1));
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int inf = 1001001001;

double ev(double p) {
  double sum = 0;
  for (int i = 1; i <= p; i++) {
    sum += i / p;
  }
  return sum;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<double> e(n);
  rep(i, n) {
    double temp;
    cin >> temp;
    e.at(i) = ev(temp);
  }

  double ev_sum = 0;
  rep(i, k) ev_sum += e.at(i);

  vector<double> ans(n - k + 1);
  ans.at(0) = ev_sum;
  for (int i = 0; i < n - k; i++) {
    ev_sum += (-e.at(i) + e.at(i + k));
    ans.at(i + 1) = ev_sum;
  }
  sort(ans.begin(), ans.end());
  printf("%5.7f\n", ans.at(n - k));
}
Problem p02780 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N, K;
  cin >> N >> K;
  vector<double> p;
  for (int i = 0; i < N; i++) {
    double x;
    cin >> x;
    p.push_back((x + 1.) / 2.);
  }
  double m = 0;
  for (int i = 0; i < K; i++) {
    m += p[i];
  }
  for (int i = 1; i <= N - K; i++) {
    double m2 = 0;
    for (int j = i; j < K + i; j++) {
      m2 += p[j];
    }
    m = max(m, m2);
  }
  cout << fixed << setprecision(12) << m << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N, K;
  cin >> N >> K;
  vector<double> p;
  for (int i = 0; i < N; i++) {
    double x;
    cin >> x;
    p.push_back((x + 1.) / 2.);
  }
  double m = 0;
  for (int i = 0; i < K; i++) {
    m += p[i];
  }
  double m2 = m;
  for (int i = K; i < N; i++) {
    m2 += p[i];
    m2 -= p[i - K];
    m = max(m, m2);
  }
  cout << fixed << setprecision(12) << m << endl;
}
Problem p02780 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  int N, K;
  cin >> N >> K;
  vector<double> A(N);
  rep(i, N) {
    double d;
    cin >> d;
    d = (d + 1) / 2;
    A[i] = d;
  }
  double ans = 0;
  for (int i = 0; i <= N - K; i++) {
    double sum = 0;
    for (int j = i; j < i + K; j++)
      sum += A[j];
    ans = max(ans, sum);
  }
  cout << setprecision(10) << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  int N, K;
  cin >> N >> K;
  vector<double> A(N);
  rep(i, N) {
    double d;
    cin >> d;
    d = (d + 1) / 2;
    A[i] = d;
  }
  double sum = 0;
  rep(i, K) sum += A[i];
  double ans = sum;
  for (int i = 1; i <= N - K; i++) {
    sum -= A[i - 1];
    sum += A[i + K - 1];
    ans = max(ans, sum);
  }
  cout << setprecision(10) << ans << endl;
  return 0;
}
Problem p02780 (Python)

Buggy submission (status: Time Limit Exceeded):
N, K = map(int, input().split())
lst = [int(x) for x in input().split()]
a = len(lst) - K + 1
n = 0
ret_lst = []
for i in range(a):
    m = sum(lst[i : i + K])
    if n < m:
        n = m
        ret_lst = lst[i : i + K]

ll = [x / 2 * (x + 1) / x for x in ret_lst]
print(sum(ll))


Fixed submission:
N, K = map(int, input().split())
lst = [int(x) for x in input().split()]
tmp = sum(lst[:K])
ret = tmp
for i in range(N - K):
    tmp -= lst[i]
    tmp += lst[i + K]
    ret = max(ret, tmp)
print((ret + K) / 2)
Problem p02780 (Python)

Buggy submission (status: Time Limit Exceeded):
# https://atcoder.jp/contests/abc154/tasks/abc154_d
import numpy as np

N, K = map(int, input().split())
p = np.array(list(map(int, input().split())))

v = 0
idx = -1
for i in range(N):
    tmp = p[i : i + K].sum()
    if tmp > v:
        v = tmp
        idx = i

# print('v idx', v, idx)
subset = p[idx : idx + K]
ans = ((subset + 1) / 2).sum()
print(ans)


Fixed submission:
# https://atcoder.jp/contests/abc154/tasks/abc154_d
import numpy as np

N, K = map(int, input().split())
p = np.array(list(map(int, input().split())))
p = (p + 1) / 2
cumsum = np.cumsum(np.array(p))
tmp = np.pad(cumsum[:-K], [K, 0], "constant")
print((cumsum - tmp).max())
Problem p02780 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    N, K = list(map(int, input().split()))
    P = list(map(int, input().split()))

    expect = [((p + 1) / 2.0) for p in P]
    sum_exp = [sum(expect[i : i + K]) for i in range(N - K + 1)]
    print(max(sum_exp))


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N, K = list(map(int, input().split()))
    P = list(map(int, input().split()))

    E = [((p + 1) / 2.0) for p in P]

    total = 0
    m = 0
    for i in range(N):
        if i < K:
            total += E[i]
        else:
            total += E[i] - E[i - K]

        if total > m:
            m = total

    print(m)


if __name__ == "__main__":
    main()
Problem p02780 (Python)

Buggy submission (status: Time Limit Exceeded):
n, k = map(int, input().split())
p = [int(s) for s in input().split()]

for i in range(n):
    p[i] = (1 + p[i]) / 2

ans = 0.0
for i in range(k, n + 1):
    ans = max(ans, sum(p[i - k : i]))

print(ans)


Fixed submission:
n, k = map(int, input().split())
p = [int(s) for s in input().split()]

for i in range(n):
    p[i] = (1 + p[i]) / 2

value = sum(p[0:k])
ans = value
for i in range(k, n):
    value = value + p[i] - p[i - k]
    ans = max(ans, value)

print(ans)
Problem p02780 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    n, k = map(int, input().split())
    p = list(map(lambda pi: (int(pi) + 1) / 2, input().split()))
    ex = 0
    for i in range(n - k + 1):
        ex = max(ex, sum(p[i : i + k]))
    print(ex)


if __name__ == "__main__":
    main()


Fixed submission:
n, k = map(int, input().split())
p = list(map(lambda pi: (int(pi) + 1) / 2, input().split()))
ex = sum(p[:k])
mx = ex
for i in range(n - k):
    ex += p[i + k] - p[i]
    if mx < ex:
        mx = ex
print(mx)
Problem p02780 (Python)

Buggy submission (status: Runtime Error):
import sys
from itertools import accumulate


def solve():
    N, K = map(int, sys.stdin.readline().split())
    cusum_p = list(accumulate(map(int, sys.stdin.read().split()), initial=0))
    return (max(i - j for i, j in zip(cusum_p[K:], cusum_p[:-K])) + K) / 2


if __name__ == "__main__":
    answer = solve()
    print(answer)


Fixed submission:
import sys
from itertools import accumulate


def solve():
    N, K = map(int, sys.stdin.readline().split())
    cusum_p = (0,) + tuple(accumulate(map(int, sys.stdin.read().split())))
    return (max(i - j for i, j in zip(cusum_p[K:], cusum_p[:-K])) + K) / 2


if __name__ == "__main__":
    answer = solve()
    print(answer)
Problem p02780 (Python)

Buggy submission (status: Time Limit Exceeded):
n, k = map(int, input().split(" "))
pp = list(map(int, input().split(" ")))

pp_ex = list(map(lambda p: (p + 1) / 2, pp))

max_ex = 0
# for i in range(k):
#     max_ex += pp_ex[i]

for i in range(0, n - k + 1):
    ex = 0
    for j in range(k):
        ex += pp_ex[i + j]
    if max_ex < ex:
        max_ex = ex

print(max_ex)


Fixed submission:
n, k = map(int, input().split(" "))
pp = list(map(int, input().split(" ")))

pp_ex = list(map(lambda p: (p + 1) / 2, pp))

max_ex = 0
for i in range(k):
    max_ex += pp_ex[i]
bef_ex = max_ex
for i in range(1, n - k + 1):
    ex = bef_ex - pp_ex[i - 1] + pp_ex[i + k - 1]
    bef_ex = ex
    if max_ex < ex:
        max_ex = ex

print(max_ex)
Problem p02780 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    N, K = map(int, input().split())
    P = [(v + 1) / 2 for v in map(int, input().split())]

    ans = 0
    for i in range(N - K + 1):
        ans = max(ans, sum(P[i : i + K]))

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N, K = map(int, input().split())
    P = [(v + 1) / 2 for v in map(int, input().split())]

    ans = tmp = sum(P[0:K])
    for i in range(N - K):
        tmp = tmp - P[i] + P[i + K]
        ans = max(ans, tmp)

    print(ans)


if __name__ == "__main__":
    main()
Problem p02780 (Python)

Buggy submission (status: Runtime Error):
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10**9)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7
# MOD = 998244353

N, K = list(map(int, sys.stdin.buffer.readline().split()))
P = list(map(int, sys.stdin.buffer.readline().split()))

P = np.array(P, dtype=float)
E = (P + 1) / 2
cs = E.cumsum()


ans = (cs[K:] - cs[:-K]).max()
ans = max(ans, cs[K - 1])
print(ans)


Fixed submission:
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10**9)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7
# MOD = 998244353

N, K = list(map(int, sys.stdin.buffer.readline().split()))
P = list(map(int, sys.stdin.buffer.readline().split()))

P = np.array(P, dtype=float)
E = (P + 1) / 2
cs = E.cumsum()

ans = cs[K - 1]
if K < N:
    ans = max(ans, (cs[K:] - cs[:-K]).max())
print(ans)
Problem p02780 (Python)

Buggy submission (status: Time Limit Exceeded):
N, K = map(int, input().split())
P = list(map(int, input().split()))

E = []
for p in P:
    e = ((p * p + p) / 2) * (1 / p)
    E.append(e)

ans = 0
for i in range(0, N - K + 1):
    ans = max(ans, sum(E[i : i + K]))

print(ans)


Fixed submission:
N, K = map(int, input().split())
P = list(map(int, input().split()))

E = []
for p in P:
    e = ((p * p + p) / 2) * (1 / p)
    E.append(e)

memo = sum(E[:K])
ans = memo
for i in range(0, N - K):
    memo = memo - E[i] + E[i + K]
    ans = max(ans, memo)

print(ans)
Problem p02780 (Python)

Buggy submission (status: Time Limit Exceeded):
n, k = map(int, input().split())
E = list(map(lambda x: (x + 1) * 0.5, list(map(int, input().split()))))
ans = sum(E[:k])
for i in range(1, n - k + 1):
    ans = max(ans, sum(E[i : i + k]))
print(ans)


Fixed submission:
n, k = map(int, input().split())
E = list(map(lambda x: (x + 1) * 0.5, list(map(int, input().split()))))
esum = sum(E[:k])
ans = esum
for i in range(n - k):
    esum = esum - E[i] + E[i + k]
    ans = max(ans, esum)
print(ans)
Problem p02780 (Python)

Buggy submission (status: Time Limit Exceeded):
n, k = map(int, input().split())
p = [(1 + int(x)) / 2 for x in input().split()]
ans = 0
for i in range(n - k + 1):
    ans = max(ans, sum(p[i : i + k]))
print(ans)


Fixed submission:
n, k = map(int, input().split())
p = [(1 + int(x)) / 2 for x in input().split()]
a_idx, l, r = 0, 0, 0
for i in range(1, n - k + 1):
    l += p[i - 1]
    r += p[i + k - 1]
    if r > l:
        a_idx, l, r = i, 0, 0
print(sum(p[a_idx : a_idx + k]))
Problem p02780 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python3
n, k, *p = map(int, open(0).read().split())
ans = float()
for i in range(n - k + 1):
    ans = max(ans, (sum(p[i : k + i]) + k) / 2)
print(ans)


Fixed submission:
#!/usr/bin/env python3
n, k, *P = map(int, open(0).read().split())
p = [(i + 1) / 2 for i in P]
c = sum(p[:k])
ans = c
for i in range(n - k):
    c = c + p[i + k] - p[i]
    ans = max(c, ans)
print(ans)
Problem p02780 (Python)

Buggy submission (status: Time Limit Exceeded):
n, k = map(int, input().split())
p = list(map(int, input().split()))
M = 0
for i in range(n - k + 1):
    s = sum(p[i : i + k])
    M = max(M, s)
print((M + k) / 2)


Fixed submission:
n, k = map(int, input().split())
p = list(map(int, input().split()))
s = sum(p[:k])
M = s
for i in range(n - k):
    s += p[i + k] - p[i]
    M = max(M, s)
print((M + k) / 2)
Problem p02780 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int arr[200005];

  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }

  int xx = 0;
  for (int i = 0; i < n - k + 1; ++i) {
    int aa = 0;
    for (int j = 0; j < k; ++j) {
      aa += arr[i + j];
    }
    if (aa > xx)
      xx = aa;
  }
  // cout<<<<endl;
  printf("%.15lf\n", (double)(xx + k) / 2);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int arr[200005];

  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }

  int xx = 0;
  int aa = 0;
  for (int j = 0; j < k; ++j) {
    aa += arr[j];
  }
  xx = aa;
  for (int i = 1; i < n - k + 1; ++i) {
    aa -= (arr[i - 1] - arr[i + k - 1]);
    if (aa > xx)
      xx = aa;
  }
  // cout<<<<endl;
  printf("%.15lf\n", (double)(xx + k) / 2);
  return 0;
}
Problem p02780 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
using P = pair<int, int>;

ll MOD = 1000000007;
ll INF = 1 << 29;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  rep(i, n) cin >> p[i];
  int tot = 0;
  int omax = -1;
  if (k == 1) {
    rep(i, n) { omax = max(omax, p[i]); }
    double oans = double(omax + 1) / 2;
    cout << fixed << setprecision(8) << oans << endl;
    return 0;
  }
  vector<int> s(n);
  s[0] = p[0];
  rep(i, n - 1) { s[i + 1] = s[i] + p[i + 1]; }
  int ma = s[k - 1];
  int ans;
  for (int i = 1; i + k < n; ++i) {
    if (s[i + k] - s[i] > ma) {
      ma = s[i + k] - s[i];
      ans = i;
    }
  }
  double kita = 0;
  for (int i = ans; i < ans + k; ++i) {
    kita += (double)(p[i + 1] + 1) / 2;
  }
  cout << fixed << setprecision(8) << kita << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
using P = pair<int, int>;

ll MOD = 1000000007;
ll INF = 1 << 29;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  rep(i, n) cin >> p[i];
  int tot = 0;
  int omax = -1;
  if (k == 1) {
    rep(i, n) { omax = max(omax, p[i]); }
    double oans = double(omax + 1) / 2;
    cout << fixed << setprecision(8) << oans << endl;
    return 0;
  }
  double nans;
  if (k == n) {
    rep(i, n) { nans += double(p[i] + 1) / 2; }
    cout << fixed << setprecision(8) << nans << endl;
    return 0;
  }
  vector<int> s(n);
  s[0] = p[0];
  rep(i, n - 1) { s[i + 1] = s[i] + p[i + 1]; }
  int ma = s[k - 1];
  int ans;
  for (int i = 1; i + k < n; ++i) {
    if (s[i + k] - s[i] > ma) {
      ma = s[i + k] - s[i];
      ans = i;
    }
  }
  double kita = 0;
  for (int i = ans; i < ans + k; ++i) {
    kita += (double)(p[i + 1] + 1) / 2;
  }
  cout << fixed << setprecision(8) << kita << endl;
}
Problem p02780 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, p[1010], k, num, ans;
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> p[i];
    num += p[i];
  }
  ans = num;
  for (int i = k; i < n; i++) {
    cin >> p[i];
    num += p[i] - p[i - k];
    ans = max(ans, num);
  }
  float x = ans, y = k;
  printf("%.12f", (x + y) / 2);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, p[200010], k, num, ans;
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> p[i];
    num += p[i];
  }
  ans = num;
  for (int i = k; i < n; i++) {
    cin >> p[i];
    num += p[i] - p[i - k];
    ans = max(ans, num);
  }
  float x = ans, y = k;
  printf("%.12f", (x + y) / 2);
  return 0;
}
Problem p02780 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef pair<int, int> pi;

int main() {
  int n, k;
  cin >> n >> k;
  vi p(n);
  vector<double> pa(n);
  rep(i, n) { cin >> p[i]; }

  rep(i, n) { pa[i] = (double)(p[i] + 1) / 2; }

  double ans = 0;
  rep(i, n - k + 1) {
    double x = 0;
    rep(j, k) { x += pa[i + j]; }
    if (ans < x)
      ans = x;
  }
  printf("%.10lf\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef pair<int, int> pi;

int main() {
  int n, k;
  cin >> n >> k;
  vi p(n);
  vector<double> pa(n);
  rep(i, n) { cin >> p[i]; }

  rep(i, n) { pa[i] = (double)(p[i] + 1) / 2; }

  double x = 0;
  rep(i, k) x += pa[i];
  double ans = x;
  rep(i, n - k) {
    x = x - pa[i] + pa[i + k];
    ans = max(ans, x);
  }
  printf("%.10lf\n", ans);
  return 0;
}
Problem p02780 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define INF (ll)1e9
#define deb(x) cerr << #x << " " << x << "\n"
#define fixed setprecision(9)
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;
  vector<double> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int index = 0;
  ll ma = (ll)0;
  for (int i = 0; i < k; i++) {
    ma += v[i];
  }
  ll prev = ma;
  for (int i = 1; i <= n - k; i++) {
    ll sum = prev - v[i - k] + v[i];
    if (sum > ma) {
      index = i;
      ma = sum;
    }
    prev = sum;
  }
  long double ans = 0;
  for (int i = index; i < index + k; i++) {
    ans += ((v[i] * (v[i] + 1) / 2.00) / v[i]);
  }
  // cerr<<v[index]<<"\n";
  cout << fixed << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define INF (ll)1e9
#define deb(x) cerr << #x << " " << x << "\n"
#define fixed setprecision(9)
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;
  vector<double> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int index = 0;
  ll ma = (ll)0;
  for (int i = 0; i < k; i++) {
    ma += v[i];
  }
  ll prev = ma;
  for (int i = 1; i <= n - k; i++) {
    ll sum = prev - v[i - 1] + v[i + k - 1];
    if (sum > ma) {
      index = i;
      ma = sum;
    }
    prev = sum;
  }
  long double ans = 0;
  for (int i = index; i < index + k; i++) {
    ans += ((v[i] * (v[i] + 1) / 2.00) / v[i]);
  }
  // cerr<<v[index]<<"\n";
  cout << fixed << ans;

  return 0;
}
Problem p02780 (C++)

Buggy submission (status: Runtime Error):
#include <fstream>
#include <iomanip>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {
  long long n, k;
  ifstream ifs("10.txt");
  cin.rdbuf(ifs.rdbuf());
  if (ifs.fail()) {
    cout << "読み込み失敗" << endl;
  }
  cin >> n >> k;
  long long p[n];
  rep(i, n) { cin >> p[i]; }
  long long max = 0;
  rep(i, k) { max += p[i]; }
  long long sum = max;
  rep(i, n - k) {
    sum += p[i + k];
    sum -= p[i];
    if (sum > max) {
      max = sum;
    }
  }
  max += k;
  cout << setprecision(15) << max / 2.0 << endl;

  return 0;
}


Fixed submission:
#include <fstream>
#include <iomanip>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long long p[n];
  rep(i, n) { cin >> p[i]; }
  long long max = 0;
  rep(i, k) { max += p[i]; }
  long long sum = max;
  rep(i, n - k) {
    sum += p[i + k];
    sum -= p[i];
    if (sum > max) {
      max = sum;
    }
  }
  max += k;
  cout << setprecision(15) << max / 2.0 << endl;

  return 0;
}
Problem p02780 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define sz(arr) (int)(arr).size()
#define rng(arr) arr.begin(), arr.end()
#define show(x) cout << #x << " = " << x << endl;
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
typedef long long int ll;

void _cin() {}
template <class Head, class... Tail> void _cin(Head &&head, Tail &&...tail) {
  cin >> head;
  _cin(forward<Tail>(tail)...);
}

#define Cin(T, ...)                                                            \
  T __VA_ARGS__;                                                               \
  _cin(__VA_ARGS__)

#define Cins(T, n, xs)                                                         \
  vector<T> xs(n);                                                             \
  for (int i = 0; i < n; ++i)                                                  \
  cin >> xs[i]

void Main();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

void Main() {
  Cin(int, n, k);
  Cins(int, n, p);

  vector<double> e(n);
  rep(i, n) e[i] = (1 + p[i]) / 2.;

  vector<double> s(n + 1);
  s[0] = 0;
  rep(i, n) rep(j, i + 1) s[i + 1] += e[j];

  double mx = 0;
  for (int i = 0; i + k <= n; ++i)
    mx = max(mx, s[i + k] - s[i]);

  cout << mx << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define sz(arr) (int)(arr).size()
#define rng(arr) arr.begin(), arr.end()
#define show(x) cout << #x << " = " << x << endl;
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
typedef long long int ll;

void _cin() {}
template <class Head, class... Tail> void _cin(Head &&head, Tail &&...tail) {
  cin >> head;
  _cin(forward<Tail>(tail)...);
}

#define Cin(T, ...)                                                            \
  T __VA_ARGS__;                                                               \
  _cin(__VA_ARGS__)

#define Cins(T, n, xs)                                                         \
  vector<T> xs(n);                                                             \
  for (int i = 0; i < n; ++i)                                                  \
  cin >> xs[i]

void Main();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

void Main() {
  Cin(int, n, k);
  Cins(int, n, p);

  vector<double> e(n);
  rep(i, n) e[i] = (1 + p[i]) / 2.;

  vector<double> s(n + 1);
  s[0] = 0;
  for (int i = 1; i <= n; ++i)
    s[i] = s[i - 1] + e[i - 1];

  double mx = 0;
  for (int i = 0; i + k <= n; ++i)
    mx = max(mx, s[i + k] - s[i]);

  cout << mx << endl;
}
Problem p02780 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < (a + n); i++)
#define var auto
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  rep(i, 0, n) cin >> p[i];

  // 累積和
  vector<int> s(n + 1, 0);
  rep(i, 0, n + 1) s[i + 1] = s[i] + p[i];

  // 最大を探す
  int i_max = 0;
  int s_max = 0;
  rep(i, 0, n - k + 1) {
    if (s_max < (s[i + k] - s[i])) {
      i_max = i;
      s_max = s[i + k] - s[i];
    }
  }

  double m = 0;
  rep(i, i_max, k) { m += (double)(1 + p[i]) / 2; }
  cout << fixed << setprecision(12) << m << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < (a + n); i++)
#define var auto
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  rep(i, 0, n) cin >> p[i];

  // 累積和
  vector<int> s(n + 1, 0);
  rep(i, 0, n) { s[i + 1] = s[i] + p[i]; }

  // 最大を探す
  int i_max = 0;
  int s_max = 0;
  rep(i, 0, n - k + 1) {
    if (s_max < (s[i + k] - s[i])) {
      i_max = i;
      s_max = s[i + k] - s[i];
    }
  }

  double m = 0;
  rep(i, i_max, k) { m += (double)(1 + p[i]) / 2; }
  cout << fixed << setprecision(12) << m << endl;

  return 0;
}
Problem p02780 (Python)

Buggy submission (status: Runtime Error):
from itertools import accumulate


def solve(string):
    n, k, *p = map(int, string.split())
    (*p,) = accumulate((1 + _p) / 2 for _p in p)
    return str(max(p2 - p1 for p1, p2 in zip(p, p[k:])))


if __name__ == "__main__":
    import sys

    print(solve(sys.stdin.read().strip()))


Fixed submission:
from itertools import accumulate


def solve(string):
    n, k, *p = map(int, string.split())
    (*p,) = accumulate((1 + _p) / 2 for _p in p)
    return str(max(p2 - p1 for p1, p2 in zip(p, p[k:]))) if k < n else str(p[-1])


if __name__ == "__main__":
    import sys

    print(solve(sys.stdin.read().strip()))
Problem p02780 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int> vint;
typedef vector<ll> vll;
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int main() {
  int n, k;
  cin >> n >> k;
  vint p(n);
  cin >> p;
  int max = 0;
  int idx = 0;
  for (int i = k - 1; i < n; i++) {
    int sum = 0;
    for (int j = i - (k - 1); j <= i; j++) {
      sum += p[j];
    }
    if (max < sum) {
      max = sum;
      idx = i - (k - 1);
    }
  }
  double ans = 0;
  for (int i = idx; i < (idx + k); i++) {
    ans += (p[i] + 1) / ((double)2);
  }
  cout << fixed << setprecision(10) << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int> vint;
typedef vector<ll> vll;
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int main() {
  int n, k;
  cin >> n >> k;
  vint p(n);
  cin >> p;
  int max = 0;
  int idx = 0;
  for (int i = 0; i < k; i++) {
    max += p[i];
  }
  int tmp = max;
  for (int i = k; i < n; i++) {
    tmp += p[i];
    tmp -= p[i - k];
    if (max < tmp) {
      max = tmp;
      idx = i - k + 1;
    }
  }
  double ans = 0;
  for (int i = idx; i < (idx + k); i++) {
    ans += (p[i] + 1) / ((double)2);
  }
  cout << fixed << setprecision(10) << ans << endl;
}
Problem p02781 (C++)

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

typedef long long ll;

string s;
int k;

vector<vector<vector<ll>>> dp;

ll f(int indextoadd, bool isequal, int numleft) {
  if (numleft < 0) {
    return dp[indextoadd][isequal][numleft] = 0;
  }
  if (indextoadd == s.size()) {
    if (numleft == 0) {
      return dp[indextoadd][isequal][numleft] = 1;
    } else {
      return dp[indextoadd][isequal][numleft] = 0;
    }
  }
  if (dp[indextoadd][isequal][numleft] != -1) {
    return dp[indextoadd][isequal][numleft];
  }
  ll total = 0;
  for (int i = 0; i <= 9; i++) {
    if (isequal && i > (s[indextoadd] - '0')) {
      break;
    }
    total += f(indextoadd + 1, (isequal && (s[indextoadd] - '0' == i)),
               (i == 0 ? numleft : numleft - 1));
  }
  return dp[indextoadd][isequal][numleft] = total;
}

int main() {
  cin >> s >> k;
  dp.resize(s.size() + 1, vector<vector<ll>>(2, vector<ll>(k + 1, -1)));
  cout << f(0, true, k) << endl;
  return 0;
}

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

typedef long long ll;

string s;
int k;

vector<vector<vector<ll>>> dp;

ll f(int indextoadd, bool isequal, int numleft) {
  if (numleft < 0) {
    return 0;
  }
  if (indextoadd == s.size()) {
    if (numleft == 0) {
      return dp[indextoadd][isequal][numleft] = 1;
    } else {
      return dp[indextoadd][isequal][numleft] = 0;
    }
  }
  if (dp[indextoadd][isequal][numleft] != -1) {
    return dp[indextoadd][isequal][numleft];
  }
  ll total = 0;
  for (int i = 0; i <= 9; i++) {
    if (isequal && i > (s[indextoadd] - '0')) {
      break;
    }
    total += f(indextoadd + 1, (isequal && (s[indextoadd] - '0' == i)),
               (i == 0 ? numleft : numleft - 1));
  }
  return dp[indextoadd][isequal][numleft] = total;
}

int main() {
  cin >> s >> k;
  dp.resize(s.size() + 1, vector<vector<ll>>(2, vector<ll>(k + 1, -1)));
  cout << f(0, true, k) << endl;
  return 0;
}
Problem p02781 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define call(var) cout << #var << "=" << var << endl;
#define dup(x, y) (((x) + (y)-1) / (y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using Graph = vector<vi>;
using P = pair<int, int>;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const int INTMAX = 2147483647;
template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
}
template <class t, class u> void chmin(t &a, u b) {
  if (b < a)
    a = b;
}

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int K;
  cin >> K;
  vector<vector<vl>> dp(n + 10, vector<vl>(2, vl(K + 1, 0)));
  dp[0][0][0] = 1;
  for (int i = 0; i < n; ++i) {
    int D = s[i] - '0';
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k <= K; ++k) {
        for (int d = 0; d <= (j ? 9 : D); ++d) {
          if (d == 0) {
            dp[i + 1][j || (d < D)][k] += dp[i][j][k];
          } else
            dp[i + 1][j || (d < D)][k + 1] += dp[i][j][k];
        }
      }
    }
  }
  cout << dp[n][0][K] + dp[n][1][K] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define call(var) cout << #var << "=" << var << endl;
#define dup(x, y) (((x) + (y)-1) / (y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using Graph = vector<vi>;
using P = pair<int, int>;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const int INTMAX = 2147483647;
template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
}
template <class t, class u> void chmin(t &a, u b) {
  if (b < a)
    a = b;
}

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int K;
  cin >> K;
  vector<vector<vl>> dp(n + 10, vector<vl>(2, vl(K + 1, 0)));
  dp[0][0][0] = 1;
  for (int i = 0; i < n; ++i) {
    int D = s[i] - '0';
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k <= K; ++k) {
        for (int d = 0; d <= (j ? 9 : D); ++d) {
          if (d == 0) {
            dp[i + 1][j || (d < D)][k] += dp[i][j][k];
          } else if (k < K)
            dp[i + 1][j || (d < D)][k + 1] += dp[i][j][k];
        }
      }
    }
  }
  cout << dp[n][0][K] + dp[n][1][K] << endl;
}
Problem p02781 (C++)

Buggy submission (status: Runtime Error):
// 6/23 解き直し
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int, int>;

string N;
vector<int> n;
int dp[100][2][5];

int main() {
  cin >> N;
  int K;
  cin >> K;

  // int size = N.size();
  for (auto a : N) {
    n.push_back(a - '0');
  }
  int l = n.size();

  dp[0][0][0] = 1; // 初期条件

  for (int i = 0; i < l; i++) {
    for (int smaller = 0; smaller < 2; smaller++) {
      for (int k = 0; k <= K; k++) {
        for (int x = 0; x <= (smaller ? 9 : n[i]); x++) {
          if (x == 0)
            dp[i + 1][smaller || x < n[i]][k] += dp[i][smaller][k];
          else
            dp[i + 1][smaller || x < n[i]][k + 1] += dp[i][smaller][k];
        }
      }
    }
  }

  cout << dp[l][0][K] + dp[l][1][K] << endl;
}

Fixed submission:
// 6/23 解き直し
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int, int>;

string N;
vector<int> n;
int dp[110][2][5];

int main() {
  cin >> N;
  int K;
  cin >> K;

  // int size = N.size();
  for (auto a : N) {
    n.push_back(a - '0');
  }
  int l = n.size();

  dp[0][0][0] = 1; // 初期条件

  for (int i = 0; i < l; i++) {
    for (int smaller = 0; smaller < 2; smaller++) {
      for (int k = 0; k <= K; k++) {
        for (int x = 0; x <= (smaller ? 9 : n[i]); x++) {
          if (x == 0)
            dp[i + 1][smaller || x < n[i]][k] += dp[i][smaller][k];
          else
            dp[i + 1][smaller || x < n[i]][k + 1] += dp[i][smaller][k];
        }
      }
    }
  }

  cout << dp[l][0][K] + dp[l][1][K] << endl;
}
Problem p02781 (C++)

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

typedef long long ll;
using namespace std;
const ll mod = 1000000007;
const ll mod998 = 998244353;
const ll intmax = 2147483647;
const ll llmax = 9223372036854775807;
const char sp = ' ';

string S, T;
int K, M;
ll res;

bool comp() {
  for (int i = 0; i < M; i++) {
    if (S[i] > T[i])
      return true;
    if (S[i] < T[i])
      return false;
  }
  return true;
}

int main() {
  cin >> S >> K;
  M = S.size();
  T = string('0', M);
  if (K == 1) {
    for (int i = 0; i < M; i++) {
      for (char ci = '1'; ci <= '9'; ci++) {
        T[i] = ci;
        res += comp();
        T[i] = '0';
      }
    }
  } else if (K == 2) {
    for (int i = 0; i < M; i++) {
      for (char ci = '1'; ci <= '9'; ci++) {
        T[i] = ci;
        for (int j = i + 1; j < M; j++) {
          for (char cj = '1'; cj <= '9'; cj++) {
            T[j] = cj;
            res += comp();
            T[j] = '0';
          }
        }
        T[i] = '0';
      }
    }
  } else if (K == 3) {
    for (int i = 0; i < M; i++) {
      for (char ci = '1'; ci <= '9'; ci++) {
        T[i] = ci;
        for (int j = i + 1; j < M; j++) {
          for (char cj = '1'; cj <= '9'; cj++) {
            T[j] = cj;
            for (int k = j + 1; k < M; k++) {
              for (char ck = '1'; ck <= '9'; ck++) {
                T[k] = ck;
                res += comp();
                T[k] = '0';
              }
            }
            T[j] = '0';
          }
        }
        T[i] = '0';
      }
    }
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
const ll mod = 1000000007;
const ll mod998 = 998244353;
const ll intmax = 2147483647;
const ll llmax = 9223372036854775807;
const char sp = ' ';

string S, T;
int K, M;
ll res;

bool comp() {
  for (int i = 0; i < M; i++) {
    if (S[i] > T[i])
      return true;
    if (S[i] < T[i])
      return false;
  }
  return true;
}

int main() {
  cin >> S >> K;
  M = S.size();
  for (int i = 0; i < M; i++) {
    T.push_back('0');
  }
  if (K == 1) {
    for (int i = 0; i < M; i++) {
      for (char ci = '1'; ci <= '9'; ci++) {
        T[i] = ci;
        res += comp();
        T[i] = '0';
      }
    }
  } else if (K == 2) {
    for (int i = 0; i < M; i++) {
      for (char ci = '1'; ci <= '9'; ci++) {
        T[i] = ci;
        for (int j = i + 1; j < M; j++) {
          for (char cj = '1'; cj <= '9'; cj++) {
            T[j] = cj;
            res += comp();
            T[j] = '0';
          }
        }
        T[i] = '0';
      }
    }
  } else if (K == 3) {
    for (int i = 0; i < M; i++) {
      for (char ci = '1'; ci <= '9'; ci++) {
        T[i] = ci;
        for (int j = i + 1; j < M; j++) {
          for (char cj = '1'; cj <= '9'; cj++) {
            T[j] = cj;
            for (int k = j + 1; k < M; k++) {
              for (char ck = '1'; ck <= '9'; ck++) {
                T[k] = ck;
                res += comp();
                T[k] = '0';
              }
            }
            T[j] = '0';
          }
        }
        T[i] = '0';
      }
    }
  }
  cout << res << endl;
}
Problem p02781 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007
ll dp[100][2][4];
int main() {
  string s;
  cin >> s;
  ll k;
  cin >> k;
  dp[0][0][0] = 1;
  for (ll i = 0; i < s.size(); i++) {
    for (ll j = 0; j <= 9; j++) {
      if (s[i] - '0' == j) {
        if (j == 0) {
          // cout << i<< " "<<j;
          for (ll l = 0; l < 4; l++) {
            dp[i + 1][0][l] += dp[i][0][l];
            dp[i + 1][1][l] += dp[i][1][l];
          }
          //	cout << " "<<dp[1][0][1]<< " "<<dp[1][1][1]<<endl;
        } else {
          for (ll l = 0; l < 4; l++) {
            dp[i + 1][0][l + 1] += dp[i][0][l];
            dp[i + 1][1][l + 1] += dp[i][1][l];
          }
        }
      } else {
        if (j == 0) {
          for (ll l = 0; l < 4; l++) {
            dp[i + 1][1][l] += dp[i][0][l];
            dp[i + 1][1][l] += dp[i][1][l];
          }
        } else {
          for (ll l = 0; l < 4; l++) {
            if (s[i] - '0' > j) {
              dp[i + 1][1][l + 1] += dp[i][0][l];
            }
            dp[i + 1][1][l + 1] += dp[i][1][l];
          }
        }
      }
    }
  }
  cout << dp[s.size()][0][k] + dp[s.size()][1][k];
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007
ll dp[100][2][5];
int main() {
  string s;
  cin >> s;
  ll k;
  cin >> k;
  dp[0][0][0] = 1;
  for (ll i = 0; i < s.size(); i++) {
    for (ll j = 0; j <= 9; j++) {
      if (s[i] - '0' == j) {
        if (j == 0) {
          // cout << i<< " "<<j;
          for (ll l = 0; l < 4; l++) {
            dp[i + 1][0][l] += dp[i][0][l];
            dp[i + 1][1][l] += dp[i][1][l];
          }
          //	cout << " "<<dp[1][0][1]<< " "<<dp[1][1][1]<<endl;
        } else {
          for (ll l = 0; l < 4; l++) {
            dp[i + 1][0][l + 1] += dp[i][0][l];
            dp[i + 1][1][l + 1] += dp[i][1][l];
          }
        }
      } else {
        if (j == 0) {
          for (ll l = 0; l < 4; l++) {
            dp[i + 1][1][l] += dp[i][0][l];
            dp[i + 1][1][l] += dp[i][1][l];
          }
        } else {
          for (ll l = 0; l < 4; l++) {
            if (s[i] - '0' > j) {
              dp[i + 1][1][l + 1] += dp[i][0][l];
            }
            dp[i + 1][1][l + 1] += dp[i][1][l];
          }
        }
      }
    }
  }
  cout << dp[s.size()][0][k] + dp[s.size()][1][k];
  // your code goes here
  return 0;
}
Problem p02781 (C++)

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  string S;
  cin >> S;
  ll N, K;
  cin >> K;
  N = S.size();
  vector<vector<vector<ll>>> dp(N + 1,
                                vector<vector<ll>>(2, vector<ll>(K + 1, 0)));
  dp[0][0][0] = 1;
  rep(i, N) {
    ll d = S[i] - '0';
    rep(j, 2) {
      if (j == 1) {
        d = 9;
      }
      rep(k, K + 1) {
        rep(m, d + 1) {
          dp[i + 1][j == 1 || m < d][(m != 0 ? k + 1 : k)] += dp[i][j][k];
        }
      }
    }
  }
  ll ans = dp[N][0][K] + dp[N][1][K];
  cout << ans << '\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  string S;
  cin >> S;
  ll N, K;
  cin >> K;
  N = S.size();
  vector<vector<vector<ll>>> dp(N + 1,
                                vector<vector<ll>>(2, vector<ll>(K + 2, 0)));
  dp[0][0][0] = 1;
  rep(i, N) {
    ll d = S[i] - '0';
    rep(j, 2) {
      if (j == 1) {
        d = 9;
      }
      rep(k, K + 1) {
        rep(m, d + 1) {
          dp[i + 1][j == 1 || m < d][(m != 0 ? k + 1 : k)] += dp[i][j][k];
        }
      }
    }
  }
  ll ans = dp[N][0][K] + dp[N][1][K];
  cout << ans << '\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}
Problem p02781 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int t;
  cin >> t;
  int n = s.size();
  string u = "";
  for (int i = 0; i < n; i++) {
    u += '0';
  }
  if (n < t) {
    cout << 0 << endl;
    return 0;
  }
  long long ans = 0;
  string q;
  if (t == 1) {
    for (int i = 0; i < n; i++) {
      for (char x = '1'; x <= '9'; x++) {
        q = u;
        q[i] = x;
        if (q <= s) {
          ans++;
        }
      }
    }
  } else if (t == 2) {
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        for (char x = '1'; x <= '9'; x++) {
          for (char y = '1'; y <= '9'; y++) {
            q = u;
            q[i] = x;
            q[j] = y;
            if (q <= s) {
              ans++;
            }
          }
        }
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        for (int k = j + 1; k < n; k++) {
          for (char x = '1'; x <= '9'; x++) {
            for (char y = '1'; y <= '9'; y++) {
              for (int z = '1'; z <= '9'; z++) {
                q = u;
                q[i] = x;
                q[j] = y;
                q[k] = z;
                if (q <= s) {
                  ans++;
                }
              }
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int t;
  cin >> t;
  int n = s.size();
  string u = "";
  for (int i = 0; i < n; i++) {
    u += '0';
  }
  if (n < t) {
    cout << 0 << endl;
    return 0;
  }
  long long ans = 0;
  string q;
  if (t == 1) {
    for (int i = 0; i < n; i++) {
      for (char x = '1'; x <= '9'; x++) {
        q = u;
        q[i] = x;
        if (q <= s) {
          ans++;
        }
      }
    }
  } else if (t == 2) {
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        for (char x = '1'; x <= '9'; x++) {
          for (char y = '1'; y <= '9'; y++) {
            q = u;
            q[i] = x;
            q[j] = y;
            if (q <= s) {
              ans++;
            }
          }
        }
      }
    }
  } else {
    ans += (n - 1) * (n - 2) * (n - 3) / 6 * 9 * 9 * 9;
    for (int j = 1; j < n; j++) {
      for (int k = j + 1; k < n; k++) {
        for (char x = '1'; x <= '9'; x++) {
          for (char y = '1'; y <= '9'; y++) {
            for (int z = '1'; z <= '9'; z++) {
              q = u;
              q[0] = x;
              q[j] = y;
              q[k] = z;
              if (q <= s) {
                ans++;
              }
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02781 (C++)

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

using namespace std;
using i64 = std::int_fast64_t;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string S;
  int K;
  cin >> S >> K;
  int N = (int)S.size();
  vector<vector<vector<i64>>> dp(N + 1,
                                 vector<vector<i64>>(2, vector<i64>(K + 1, 0)));
  dp[0][0][0] = 1;
  for (int d = 0; d < N; d++) {
    for (int is_less = 0; is_less < 2; is_less++) {
      for (int k = 0; k < K + 1; k++) {
        int c = S[d] - '0';
        for (int nxt = 0; nxt < 10; nxt++) {
          if (c < nxt && is_less == 0)
            continue;

          int nis_less = is_less;
          if (nxt < c)
            nis_less = 1;

          int nk = k + (nxt == 0 ? 0 : 1);

          dp[d + 1][nis_less][nk] += dp[d][is_less][k];
        }
      }
    }
  }
  cout << dp[N][0][K] + dp[N][1][K] << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using i64 = std::int_fast64_t;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string S;
  int K;
  cin >> S >> K;
  int N = (int)S.size();
  vector<vector<vector<i64>>> dp(N + 1,
                                 vector<vector<i64>>(2, vector<i64>(K + 2, 0)));
  dp[0][0][0] = 1;
  for (int d = 0; d < N; d++) {
    for (int is_less = 0; is_less < 2; is_less++) {
      for (int k = 0; k < K + 1; k++) {
        int c = S[d] - '0';
        for (int nxt = 0; nxt < 10; nxt++) {
          if (c < nxt && is_less == 0)
            continue;

          int nis_less = is_less;
          if (nxt < c)
            nis_less = 1;

          int nk = k + (nxt == 0 ? 0 : 1);

          dp[d + 1][nis_less][nk] += dp[d][is_less][k];
        }
      }
    }
  }
  cout << dp[N][0][K] + dp[N][1][K] << '\n';
  return 0;
}