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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int a;
  cin >> a;
  vector<string> word;
  word.push_back("Christmas");

  if (a == 25) {
    cout << word[0];
  }
  if (a < 25) {
    for (int i = 25; i > a; i--) {
      word.push_back(" Eve");
    }
    for (int j = 0; j < 25; j++) {
      cout << word[j];
    }
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int a;
  cin >> a;
  vector<string> word;
  word.push_back("Christmas");

  if (a == 25) {
    cout << word[0];
  }
  if (a < 25) {
    for (int i = 25; i > a; i--) {
      word.push_back(" Eve");
    }
    for (int j = 0; j <= 25 - a; j++) {
      cout << word[j];
    }
  }
}
Problem p03206 (C++)

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

int main() {
  int N;
  cin >> N;
  cout << "Christmas" << endl;
  for (int i = 25; i != N; i++) {
    cout << " "
         << "Eve" << endl;
  }
}

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

int main() {
  int N;
  cin >> N;
  cout << "Christmas" << endl;
  for (int i = 25; i != N; i--) {
    cout << " "
         << "Eve" << endl;
  }
}
Problem p03206 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
int main() {
  int d;
  cin >> d;
  string a[4] = {"Christmas Eve Eve Eve", "Christmas Eve Eve", "Christmas Eve",
                 "Christmas"};

  cout << a[d - 23] << endl;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int main() {
  int d;
  cin >> d;
  string a[4] = {"Christmas Eve Eve Eve", "Christmas Eve Eve", "Christmas Eve",
                 "Christmas"};

  cout << a[d - 22] << endl;
}
Problem p03206 (Python)

Buggy submission (status: Runtime Error):
def solve():
    d = int(input())
    eves = 25 - d
    return "Christmas" + " Eve" + eves


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


Fixed submission:
def solve():
    d = int(input())
    eves = 25 - d
    return "Christmas" + " Eve" * eves


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

Buggy submission (status: Runtime Error):
print(f"Christmas {' '.join(['Eve'] * (25 - int(input())))}".strip())


Fixed submission:
print("Christmas {}".format(" ".join(["Eve"] * (25 - int(input())))).strip())
Problem p03206 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python
# -*- coding: utf-8 -*-

al = [0, 5] + [0] * 49
bl = [0, 3] + [0] * 49


def solve2(ans, x, idx, cnt):
    if idx == 1:
        return ans + (0, 0, 1, 2, 3, 3, 3)[x] + [0, 1][cnt >= 1]
    if x == (al[idx] // 2) + 1:
        return ans + bl[idx - 1] + 1 + [0, 1][cnt >= 1]
    elif x > (al[idx] // 2) + 1:
        ans += bl[idx - 1]
        x -= al[idx] // 2
        cnt += 1
    return solve2(ans, x, idx - 1, cnt)


def solve():
    n, x = map(int, input().split())
    for i in range(2, n + 1):
        al[i] = al[i - 1] * 2 + 3
        bl[i] = bl[i - 1] * 2 + 1
    ans = solve2(0, x, n, 0)
    print(ans)


if __name__ == "__main__":
    solve()


Fixed submission:
print("Christmas" + (" Eve" * (25 - int(input()))))
Problem p03206 (Python)

Buggy submission (status: Runtime Error):
D = int(input)
print("Christmas" + " Eve" * (25 - D))


Fixed submission:
D = int(input())
print("Christmas" + " Eve" * (25 - D))
Problem p03207 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define int long long
using namespace std;
signed main() {
  int N;
  int A = 0;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    A += p[i];
  }
  sort(p.begin(), p.end()); // {1, 2, 2, 5}
  reverse(p.begin(), p.end());
  cout << A - p[0] / 2 << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define int long long
using namespace std;
signed main() {
  int N;
  cin >> N;
  int A = 0;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    A += p[i];
  }
  sort(p.begin(), p.end()); // {1, 2, 2, 5}
  reverse(p.begin(), p.end());
  cout << A - p[0] / 2 << endl;
}
Problem p03207 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using i64 = int64_t;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep2(i, x, n) for (int i = x; i < n; ++i)
#define INF 1 << 29;

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

  sort(p.begin(), p.end(), greater<int>());
  p[0] /= 2;
  int price = 0;
  rep(i, n) price += p[i];

  cout << price << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using i64 = int64_t;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep2(i, x, n) for (int i = x; i < n; ++i)
#define INF 1 << 29;

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

  sort(p.begin(), p.end(), greater<int>());
  p[0] /= 2;
  int price = 0;
  rep(i, n) price += p[i];

  cout << price << endl;

  return 0;
}
Problem p03207 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define rep(a, t) for (int a = 0; a < t; ++a)
#define forever while (true)
#define Sort(a) sort(a.begin(), a.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define print_double(val, a) cout << fixed << setprecision(a) << val << endl;
ll mod = 1e9 + 7;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  Sort(a);
  int sum = 0;
  rep(i, n) { sum += a[i]; }
  sum -= a[n - 1] / 2;
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define rep(a, t) for (int a = 0; a < t; ++a)
#define forever while (true)
#define Sort(a) sort(a.begin(), a.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define print_double(val, a) cout << fixed << setprecision(a) << val << endl;
ll mod = 1e9 + 7;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  Sort(a);
  int sum = 0;
  rep(i, n) { sum += a[i]; }
  sum -= a[n - 1] / 2;
  cout << sum << endl;
  return 0;
}
Problem p03207 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main(int argc, const char *argv[]) {
  // cant wait for holiday
  //    string days[7] ={"SUN","MON","TUE","WED","THU","FRI","SAT"};
  //    string s;
  //    int n;
  //    cin>> s;
  //    for (int i = 0; i< 7; i++){
  //        if (s== days[i]){
  //            n = i;
  //        }
  //    }
  //    n = 7-n;
  //    cout<< n;
  // day before christmas eve
  int a, b;
  int n;
  cin >> n;
  int prices[n];
  for (int i = 0; i < n; i++) {
    cin >> prices[i];
  }
  for (int i = 0; i < n; i++) {
    // find the greatest number and the index
    if (prices[i] >= a) {
      a = prices[i];
      b = i;
    }
  }
  // b is index, a is value
  a = 0; // a will now be the sum
  prices[b] /= 2;
  for (int i = 0; i < n; i++) {
    a += prices[i];
  }
  cout << a;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main(int argc, const char *argv[]) {
  // cant wait for holiday
  //    string days[7] ={"SUN","MON","TUE","WED","THU","FRI","SAT"};
  //    string s;
  //    int n;
  //    cin>> s;
  //    for (int i = 0; i< 7; i++){
  //        if (s== days[i]){
  //            n = i;
  //        }
  //    }
  //    n = 7-n;
  //    cout<< n;
  // day before christmas eve
  int a = 0, b;
  int n;
  cin >> n;
  int prices[n];
  for (int i = 0; i < n; i++) {
    cin >> prices[i];
  }
  for (int i = 0; i < n; i++) {
    // find the greatest number and the index
    if (prices[i] >= a) {
      a = prices[i];
      b = i;
    }
  }
  // b is index, a is value
  a = 0; // a will now be the sum
  prices[b] /= 2;
  for (int i = 0; i < n; i++) {
    a += prices[i];
  }
  cout << a;
  return 0;
}
Problem p03207 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#include <string>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  int n;
  cin >> n;
  vector<int> p(n);
  rep(i, n) { cin >> p[i]; }
  sort(p.begin(), p.end(), greater<int>());
  int ans = 0;
  rep(i, n) {
    if (i = 0)
      ans += p[i] / 2;
    else
      ans += p[i];
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> p(n);
  rep(i, n) { cin >> p[i]; }
  sort(p.begin(), p.end(), greater<int>());
  int ans = -p[0] / 2;
  rep(i, n) { ans += p[i]; }
  cout << ans << endl;
  return 0;
}
Problem p03207 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  for (int i = 0; i < n; i++) {
    cin >> vec.at(n);
  }
  sort(vec.begin(), vec.end());
  int sum = 0;
  for (int i = 0; i < n - 1; i++) {
    sum = sum + vec.at(i);
  }
  sum = sum + vec.at(n - 1) / 2;
  cout << sum << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  for (int i = 0; i < n; i++) {
    cin >> vec.at(i);
  }
  sort(vec.begin(), vec.end());
  int sum = 0;
  for (int i = 0; i < n - 1; i++) {
    sum = sum + vec.at(i);
  }
  sum = sum + vec.at(n - 1) / 2;
  cout << sum << endl;
}
Problem p03207 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<ll, ll, ll> tl3;

const int BIG_NUM = 1e9;
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;

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

  sort(p.begin(), p.end());

  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += p[i];
  }

  sum -= p[n - 1] / 2;

  cout << sum << endl;
}


Fixed submission:
#define _USE_MATH_DEFINES

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<ll, ll, ll> tl3;

const int BIG_NUM = 1e9;
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;

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

  sort(p.begin(), p.end());

  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += p[i];
  }

  sum -= p[n - 1] / 2;

  cout << sum << endl;
}
Problem p03207 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
p = [int(input()) for _ in N]
print(sum(p) - max(p) // 2)


Fixed submission:
N = int(input())
p = [int(input()) for _ in range(N)]
print(sum(p) - max(p) // 2)
Problem p03207 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int n;
  cin >> n;

  vector<int> value;

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

  int max = 0;
  for (int i = 0; i < n; i++) {
    if (max < value[i])
      max = value[i];
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum = sum + value[i];
  }

  cout << sum - max / 2 << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int n;
  cin >> n;

  vector<int> value(10, 0);

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

  int max = 0;
  for (int i = 0; i < n; i++) {
    if (max < value[i])
      max = value[i];
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum = sum + value[i];
  }

  cout << sum - max / 2 << endl;

  return 0;
}
Problem p03207 (C++)

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

#define rep(i, n) for ((i) = 0; (i) < (n); ++(i))
#define repr(i, n) for ((i) = (n); (i) > 0; == (i))
#define FOR(i, j, n) for ((i) = (j); (i) < (n); ++(i))
#define forr(i, j, n) for ((i) = (j); (i) > (n); --(i))
#define debg(a) cout << (a) << endl;
#define INF 9999999
#define Int long long
int i, j, k;
int dx, dy;
int a, b, c;
string s, t;

int main() {
  int n;
  cin >> n;
  int p[i];
  rep(i, n) cin >> p[i];
  int max = 100;
  rep(i, n) if (p[i] > max) max = p[i];
  int ttl = 0;
  rep(i, n) ttl += p[i];
  ttl -= max;
  ttl += max / 2;
  cout << ttl;

  return 0;
}


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

#define rep(i, n) for ((i) = 0; (i) < (n); ++(i))
#define repr(i, n) for ((i) = (n); (i) > 0; == (i))
#define FOR(i, j, n) for ((i) = (j); (i) < (n); ++(i))
#define forr(i, j, n) for ((i) = (j); (i) > (n); --(i))
#define debg(a) cout << (a) << endl;
#define INF 9999999
#define Int long long
int i, j, k;
int dx, dy;
int a, b, c;
string s, t;

int main() {
  int n;
  cin >> n;
  int p[n];
  rep(i, n) cin >> p[i];
  int max = 100;
  rep(i, n) if (p[i] > max) max = p[i];
  int ttl = 0;
  rep(i, n) ttl += p[i];
  ttl -= max;
  ttl += max / 2;
  cout << ttl;

  return 0;
}
Problem p03207 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long Int;
int main() {
  priority_queue<int> pque;
  int n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    pque.push(a);
  }
  int ans = pque.top();
  pque.pop();
  ans /= 2;
  while (!pque.empty()) {
    ans += pque.top();
    pque.pop();
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long Int;
int main() {
  priority_queue<int> pque;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    pque.push(a);
  }
  int ans = pque.top();
  pque.pop();
  ans /= 2;
  while (!pque.empty()) {
    ans += pque.top();
    pque.pop();
  }
  cout << ans << endl;
}
Problem p03207 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define SZ(x) ((int)(x).size())
#define INF 999999999
typedef long long int Int;
using ll = long long;
using VI = vector<int>;

int main() {
  int n;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  sort(a.begin(), a.end());
  Int s = a.back();
  s /= 2;
  a.pop_back();
  a.push_back(s);
  Int ans = 0;
  for (auto &i : a)
    ans += i;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define SZ(x) ((int)(x).size())
#define INF 999999999
typedef long long int Int;
using ll = long long;
using VI = vector<int>;

int main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  int ans = 0;
  rep(i, n - 1) ans += a[i];
  cout << ans + (a[n - 1] / 2) << endl;
}
Problem p03207 (Python)

Buggy submission (status: Runtime Error):
N = input()
p = [int(input()) for i in range(N)]
print(sum(p) - max(p) // 2)


Fixed submission:
N = int(input())
p = [int(input()) for i in range(N)]
print(sum(p) - max(p) // 2)
Problem p03207 (C++)

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

int main() {
  int N;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p.at(i);
  }
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  p.at(0) /= 2;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    sum += p.at(i);
  }
  cout << sum;
}


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

int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p.at(i);
  }
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  p.at(0) /= 2;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    sum += p.at(i);
  }
  cout << sum;
}
Problem p03207 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> K;
  for (int i = 0; i < N; i++) {
    cin >> K.at(i);
  }
  sort(K.begin(), K.end());
  K.at(N - 1) /= 2;
  int H = 0;
  for (int i = 0; i < N; i++) {
    H += K.at(i);
  }
  cout << H << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> K(N);
  for (int i = 0; i < N; i++) {
    cin >> K.at(i);
  }
  sort(K.begin(), K.end());
  K.at(N - 1) /= 2;
  int H = 0;
  for (int i = 0; i < N; i++) {
    H += K.at(i);
  }
  cout << H << endl;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define ll long long
#define ull unsigned long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define loop(i, N, M) for (int i = N; i < M; i++)
#define MAX(v) *max_element(v.begin(), v.end())
#define MIN(v) *min_element(v.begin(), v.end())
#define SORTL2S(type, v) sort(v.begin(), v.end(), greater<type>())
#define SORTS2L(type, v) sort(v.begin(), v.end())
#define SORTMF(v, func) sort(v.begin(), v.end(), func)

// 変数
ll N, K;
vector<ll> vec;

// 解法
void solve() {

  SORTS2L(ll, vec);

  ll ans = vec[N - 1] - vec[0];
  for (ll i = 0; i <= N - K; ++i) {
    ll maxx = *max_element(vec.begin() + i, vec.begin() + i + K);
    ll minn = *min_element(vec.begin() + i, vec.begin() + i + K);
    ans = min(ans, maxx - minn);
  }

  cout << ans;

  return;
}

int main() {
  // 入力

  cin >> N >> K;
  vec.resize(N);
  rep(i, N) cin >> vec[i];

  solve();
  cout << endl;
  return 0;
}

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define ll long long
#define ull unsigned long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define loop(i, N, M) for (int i = N; i < M; i++)
#define MAX(v) *max_element(v.begin(), v.end())
#define MIN(v) *min_element(v.begin(), v.end())
#define SORTL2S(type, v) sort(v.begin(), v.end(), greater<type>())
#define SORTS2L(type, v) sort(v.begin(), v.end())
#define SORTMF(v, func) sort(v.begin(), v.end(), func)

// 変数
ll N, K;
vector<ll> vec;

// 解法
void solve() {

  SORTS2L(ll, vec);

  ll ans = vec[N - 1] - vec[0];
  for (ll i = 0; i <= N - K; ++i) {
    ans = min(ans, vec[i + K - 1] - vec[i]);
  }

  cout << ans;

  return;
}

int main() {
  // 入力

  cin >> N >> K;
  vec.resize(N);
  rep(i, N) cin >> vec[i];

  solve();
  cout << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<long long> h;
  long long l;
  for (int i = 0; i < n; i++) {
    cin >> l;
    h.push_back(l);
  }
  sort(h.begin(), h.end());
  long long ans = 1000000001;
  for (int i = 0; i < k; i++) {
    ans = min(ans, h[k - 1 + i] - h[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<long long> h;
  long long l;
  for (int i = 0; i < n; i++) {
    cin >> l;
    h.push_back(l);
  }
  sort(h.begin(), h.end());
  long long ans = 1000000001;
  for (int i = 0; i <= n - k; i++) {
    ans = min(ans, h[k - 1 + i] - h[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mkp make_pair
#define pb push_back
#define inf INT_MAX

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  IOS int n, k;
  cin >> n >> k;
  // max-min of k-th size window in the sorted array!

  std::vector<int> v(n, 0);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  sort(v.begin(), v.end());
  int ans = v[k - 1] - v[0];

  for (int i = k; i < n; i++) {
    int diff = v[i] - v[i - k + 1];
    ans = min(ans, diff);
  }

  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mkp make_pair
#define pb push_back
#define inf INT_MAX

int32_t main() {

  IOS int n, k;
  cin >> n >> k;
  // max-min of k-th size window in the sorted array!

  std::vector<int> v(n, 0);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  sort(v.begin(), v.end());
  int ans = v[k - 1] - v[0];

  for (int i = k; i < n; i++) {
    int diff = v[i] - v[i - k + 1];
    ans = min(ans, diff);
  }

  cout << ans;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 15;
int n, k, a[N], ans;
int main() {
  ios::sync_with_stdio(false);
  ans = 0x3f3f3f3f;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n - k + 1; ++i)
    ans = min(ans, a[i + k - 1] - a[i]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, k, a[N], ans;
int main() {
  ios::sync_with_stdio(false);
  ans = 0x3f3f3f3f;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n - k + 1; ++i)
    ans = min(ans, a[i + k - 1] - a[i]);
  cout << ans << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> h(n);
  for (auto &&e : h)
    cin >> e;

  sort(h.begin(), h.end());

  vector<int> c(n - (k - 1));

  for (int i = 0; i + k <= n; ++i) {
    for (int j = 1; j < k; ++j) {
      c[i] += h[i + j] - h[i + j - 1];
    }
  }

  sort(c.begin(), c.end());

  cout << c[0] << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> h(n);
  for (auto &&e : h)
    cin >> e;

  sort(h.begin(), h.end());

  vector<int> c(n - (k - 1));

  for (int i = 0; i + k <= n; ++i)
    c[i] = h[i + (k - 1)] - h[i];

  sort(c.begin(), c.end());

  cout << c[0] << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define lp(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define ll long long
#define ii pair<string, int> ii;

string IntToStr(int tmp) {
  ostringstream out;
  out << tmp;
  return out.str();
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> v;
  while (n--) {
    ll x;
    cin >> x;
    v.pb(x);
  }
  sort(v.begin(), v.end());
  int len = v.size();
  ll min = 100000000000000;
  for (int i = 0; i < len; i++) {
    ll kk = abs(v[i] - v[i + k - 1]);
    if (min > kk)
      min = kk;
  }
  cout << min << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lp(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define ll long long
#define ii pair<string, int> ii;

string IntToStr(int tmp) {
  ostringstream out;
  out << tmp;
  return out.str();
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> v;
  while (n--) {
    ll x;
    cin >> x;
    v.pb(x);
  }
  sort(v.begin(), v.end());
  int len = v.size();
  ll min = 100000000000000;
  for (int i = 0; i < len - k + 1; i++) {
    ll kk = abs(v[i] - v[i + k - 1]);
    if (min > kk)
      min = kk;
  }
  cout << min << endl;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, k;
const int max_n = 1e6;
ll h[max_n];
void solve() {
  ll mi = 1e10;
  for (int i = 0; i < n; i++) {
    ll mmi = 1e10;
    ll mma = -1;
    for (int j = i; j < i + k; j++) {
      mmi = min(mmi, h[j]);
      mma = max(mma, h[j]);
    }
    mi = min(mi, mma - mmi);
  }
  cout << mi << endl;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  sort(h, h + n);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, k;
const int max_n = 1e6;
ll h[max_n];
void solve() {
  ll mi = 1e10;
  for (int i = 0; i <= n - k; i++) {
    ll mma = h[i + k - 1];
    ll mmi = h[i];
    mi = min(mi, mma - mmi);
  }
  cout << mi << endl;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  sort(h, h + n);
  solve();
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
typedef long long int lli;
#define urept(soeji, start, n) for (int soeji = start; soeji < n; soeji++)
#define drept(soeji, start, n) for (int soeji = start; soeji > n; soeji--)
int main(void) {
  int N, K;
  cin >> N >> K;
  vector<lli> h;
  lli x;
  for (int i = 0; i < N; i++) {
    cin >> x;
    h.push_back(x);
  }
  vector<lli> hcopy;
  hcopy = h;
  sort(h.begin(), h.end());
  sort(hcopy.begin(), hcopy.end());
  hcopy.erase(unique(hcopy.begin(), hcopy.end()), hcopy.end());
  if (hcopy.size() == N - K) {
    cout << "0" << endl;
    return 0;
  } else {
    lli minx = h[K - 1] - h[0];
    vector<lli> hd;
    for (int i = 0; i < N - K + 1; i++) {
      for (int j = i; j <= i + K - 1; j++) {
        hd.push_back(h[j]);
      }
      minx = min(hd[hd.size() - 1] - hd[0], minx);
      hd.erase(hd.begin(), hd.end());
    }
    cout << minx << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
typedef long long int lli;
#define urept(soeji, start, n) for (int soeji = start; soeji < n; soeji++)
#define drept(soeji, start, n) for (int soeji = start; soeji > n; soeji--)
int main(void) {
  int N, K;
  cin >> N >> K;
  vector<lli> h;
  lli x;
  for (int i = 0; i < N; i++) {
    cin >> x;
    h.push_back(x);
  }
  vector<lli> hcopy;
  hcopy = h;
  sort(h.begin(), h.end());
  sort(hcopy.begin(), hcopy.end());
  hcopy.erase(unique(hcopy.begin(), hcopy.end()), hcopy.end());
  if (hcopy.size() == N - K) {
    cout << "0" << endl;
    return 0;
  } else {
    lli minx = h[K - 1] - h[0];
    vector<lli> hd;
    for (int i = 0; i < N - K + 1; i++) {
      /*for (int j = i; j <= i + K - 1; j++)
      {
          hd.push_back(h[j]);
      }*/
      lli max = h[i + K - 1];
      lli tmp = h[i];
      minx = min(max - tmp, minx);
      // hd.erase(hd.begin(), hd.end());
    }
    cout << minx << endl;
  }
  return 0;
}
Problem p03208 (C++)

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

int main() {
  int n, k, a[10000], ans = 2000000000;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < n - k + 1; i++) {
    ans = min(ans, a[i + k - 1] - a[i]);
  }
  cout << ans << endl;
}

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

int main() {
  int n, k, a[100000], ans = 2000000000;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < n - k + 1; i++) {
    ans = min(ans, a[i + k - 1] - a[i]);
  }
  cout << ans << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <chrono>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define eps 1e-9
#define f first
#define s second

typedef long long ll;

using namespace std;

const int N = 1e5;
int a[N + 5];

int main()

{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, k, ans = INT_MAX;

  cin >> n >> k;

  k--;

  int arr[n];

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

  sort(arr, arr + n);

  for (int i = 0; i < n; i++) {

    ans = min(ans, arr[i + k] - arr[i]);
  }

  cout << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <chrono>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define eps 1e-9
#define f first
#define s second

typedef long long ll;

using namespace std;

const int N = 1e5;
int a[N + 5];

int main()

{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, k, ans = INT_MAX;

  cin >> n >> k;

  k--;

  int arr[n];

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

  sort(arr, arr + n);

  for (int i = 0; i < n - k; i++) {

    ans = min(ans, arr[i + k] - arr[i]);
  }

  cout << ans;

  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long inf = numeric_limits<long long>::max();
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> seq(n);
  rep(i, n) { cin >> seq[i]; }
  sort(seq.begin(), seq.end());
  int ans = 100000;
  rep(i, n - k + 1) { ans = min(ans, abs(seq[i] - seq[i + 2])); }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long inf = numeric_limits<long long>::max();
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> seq(n);
  rep(i, n) { cin >> seq[i]; }
  sort(seq.begin(), seq.end());
  long long ans = inf;
  rep(i, n - k + 1) { ans = min(ans, (long long)abs(seq[i] - seq[i + k - 1])); }
  cout << ans << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int N, K;
ll h[100000];

void _main() {
  cin >> N >> K;
  rep(i, 0, N) cin >> h[i];
  sort(h, h + N);

  ll ans = infl;
  for (int a = 0, b = K - 1; a < K; a++, b++)
    chmin(ans, h[b] - h[a]);
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int N, K;
ll h[100000];

void _main() {
  cin >> N >> K;
  rep(i, 0, N) cin >> h[i];
  sort(h, h + N);

  ll ans = infl;
  rep(i, 0, N - K + 1) chmin(ans, h[i + K - 1] - h[i]);
  cout << ans << "\n";
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rep(i, N) for (int(i) = 0; (i) < (N); (i)++)
typedef long long ll;
typedef pair<int, int> pii;

int ans = 0;
vector<int> ps = {3, 5, 7};
ll N;

void dfs(ll n, ll b) {
  if (n <= N && b == 7) {
    ans++;
  }
  if (n > N) {
    return;
  }
  rep(i, ps.size()) {
    ll nb = b | (1 << i);
    ll nn = 10 * n + 1L * ps[i];
    dfs(nn, nb);
  }
  return;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> hs(N);
  rep(i, N) { cin >> hs[i]; }
  sort(hs.begin(), hs.end());
  int ans = 1e9;
  for (int i = 0; i < N; i++) {
    for (int j = i + K - 1; j < N; j++) {
      int tmp = hs[j] - hs[i];
      ans = min(tmp, ans);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rep(i, N) for (int(i) = 0; (i) < (N); (i)++)
typedef long long ll;
typedef pair<int, int> pii;

int ans = 0;
vector<int> ps = {3, 5, 7};
ll N;

void dfs(ll n, ll b) {
  if (n <= N && b == 7) {
    ans++;
  }
  if (n > N) {
    return;
  }
  rep(i, ps.size()) {
    ll nb = b | (1 << i);
    ll nn = 10 * n + 1L * ps[i];
    dfs(nn, nb);
  }
  return;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> hs(N);
  rep(i, N) { cin >> hs[i]; }
  sort(hs.begin(), hs.end());
  int ans = 1e9;
  for (int i = 0; i + K - 1 < N; i++) {
    int tmp = hs[i + K - 1] - hs[i];
    ans = min(tmp, ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p03208 (C++)

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

using namespace std;

typedef long long ll;

const double pi = 3.14159265358979323846;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define mod 1000000007LL
#define PS(p) cout << setprecision(15) << fixed << p << endl;
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
#define ALL(c) c.begin(), c.end()
#define CIN(v) cin >> v;
#define COUT(v) cout << v << endl;

int main() {
  int N, K;

  CIN(N >> K);

  int h[N];

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

  sort(h, h + N);
  int result = INT_MAX;

  for (int i = 0; i < N; i++) {
    int maxV = 0;
    int minV = INT_MAX;

    if (N < i + K) {
      // COUT("break " << i << " " << K)
      break;
    }

    for (int j = i; j < i + K; j++) {
      maxV = max(maxV, h[j]);
      minV = min(minV, h[j]);
      // COUT("h[j]" << h[j] << "max " << maxV << " minv " << minV);
    }
    result = min(result, maxV - minV);
  }

  COUT(result);

  return 0;
}


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

using namespace std;

typedef long long ll;

const double pi = 3.14159265358979323846;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define mod 1000000007LL
#define PS(p) cout << setprecision(15) << fixed << p << endl;
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
#define ALL(c) c.begin(), c.end()
#define CIN(v) cin >> v;
#define COUT(v) cout << v << endl;

int main() {
  int N, K;

  CIN(N >> K);

  int h[N];

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

  sort(h, h + N);
  int result = INT_MAX;

  for (int i = 0; i < N; i++) {
    int maxV = 0;
    int minV = INT_MAX;

    if (N < i + K) {
      // COUT("break " << i << " " << K)
      break;
    }

    result = min(result, h[i + K - 1] - h[i]);
  }

  COUT(result);

  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <algorithm>
#include <bitset>  // bitset
#include <cctype>  // isupper, islower, isdigit, toupper, tolower
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iostream>      // cout, endl, cin
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;

int main() {

  int i, j;
  int N, K;
  long long diff = 9223372036854775807;
  long long temp;

  cin >> N >> K;

  vector<long long> H(N);

  for (i = 0; i < N; i++) {
    cin >> H.at(i);
  }

  /* 数値を降順にソート */
  sort(H.begin(), H.end(), greater<long long>());

  for (i = 0; i < N + K - 1; i++) {
    temp = H.at(i) - H.at(i + K - 1);

    if (temp < diff) {
      diff = temp;
    }
  }

  cout << diff << endl;

  return 0;
}

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <algorithm>
#include <bitset>  // bitset
#include <cctype>  // isupper, islower, isdigit, toupper, tolower
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iostream>      // cout, endl, cin
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;

int main() {

  int i, j;
  int N, K;
  long long diff = 9223372036854775807;
  long long temp;

  cin >> N >> K;

  vector<long long> H(N);

  for (i = 0; i < N; i++) {
    cin >> H.at(i);
  }

  /* 数値を降順にソート */
  sort(H.begin(), H.end(), greater<long long>());

  for (i = 0; i < N - K + 1; i++) {
    temp = H.at(i) - H.at(i + K - 1);

    if (temp < diff) {
      diff = temp;
    }
  }

  cout << diff << endl;

  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;

using namespace std;

int K, N;
vector<ll> min_list;
int main() {
  scanf("%d%d", &N, &K);
  ll h[N];
  rep(i, N) { scanf("%lld", &h[i]); }
  sort(h, h + N);
  for (int i = 0; i < N - K + 1; i++) {
    vector<ll> temp;
    rep(j, K) temp.push_back(h[i + j]);

    ll min = temp.front();
    ll max = temp.back();
    min_list.push_back(max - min);
  }
  ll min = *min_element(min_list.begin(), min_list.end());
  printf("%lld\n", min);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;

using namespace std;

int K, N;
vector<ll> min_list;
int main() {
  scanf("%d%d", &N, &K);
  ll h[N];
  rep(i, N) { scanf("%lld", &h[i]); }
  sort(h, h + N);
  for (int i = 0; i < N - K + 1; i++) {
    ll min = h[i];
    ll max = h[i + K - 1];
    min_list.push_back(max - min);
  }
  ll min = *min_element(min_list.begin(), min_list.end());
  printf("%lld\n", min);
  return 0;
}
Problem p03208 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  sort(h.begin(), h.end());
  vector<int> j(N - K + 1);
  for (int i = 0; i < N - K + 1; i++) {
    j[i] = j[i + K - 1] - j[i];
  }
  sort(j.begin(), j.end());
  cout << j[0] << endl;
}

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  sort(h.begin(), h.end());
  vector<int> j(N - K + 1);
  for (int i = 0; i < N - K + 2; i++) {
    j[i] = h[i + K - 1] - h[i];
  }
  sort(j.begin(), j.end());
  cout << j[0] << endl;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  int h[N];
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  // sort
  for (int i = 0; i < N; i++) {
    for (int j = i; j < N; j++) {
      if (h[j] > h[i]) {
        int temp = h[i];
        h[i] = h[j];
        h[j] = temp;
      }
    }
  }
  int result = h[0] - h[K - 1];
  for (int i = K; i < N; i++) {
    int local = h[i - K + 1] - h[i];
    if (local < result)
      result = local;
  }
  cout << result << endl;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  int h[N];
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  // sort
  sort(h, h + N);

  int result = 1000000000;
  for (int i = K - 1; i < N; i++) {
    int local = h[i] - h[i - K + 1];
    if (local < result)
      result = local;
  }
  cout << result << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
  int K, N;
  cin >> N >> K;
  long long h[11000];
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  sort(h, h + N);
  long long min = 1000000000;
  for (int i = 0; i + K - 1 < N; i++) {
    if (h[i + K - 1] - h[i] < min)
      min = h[i + K - 1] - h[i];
  }
  cout << min << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
  int K, N;
  cin >> N >> K;
  long long h[110000];
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  sort(h, h + N);
  long long min = 1000000000;
  for (int i = 0; i + K - 1 < N; i++) {
    if (h[i + K - 1] - h[i] < min)
      min = h[i + K - 1] - h[i];
  }
  cout << min << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
ll a[1005];
int Min = 0x3f3f3f3f;

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  for (int i = k - 1; i < n; i++) {
    int temp = a[i] - a[i - k + 1];
    Min = min(Min, temp);
  }
  printf("%d\n", Min);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int a[100005];
int Min = 0x3f3f3f3f;

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  for (int i = k - 1; i < n; i++) {
    int temp = a[i] - a[i - k + 1];
    Min = min(Min, temp);
  }
  printf("%d\n", Min);

  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

int main() {
  int n, k;
  cin >> n >> k;
  vl h(n);
  rep(i, n) cin >> h[i];
  sort(all(h));
  set<int> g;
  rep(i, n - 2) { g.insert(h[i + k - 1] - h[i]); }
  cout << *begin(g) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

int main() {
  int n, k;
  cin >> n >> k;
  vl h(n);
  rep(i, n) cin >> h[i];
  sort(all(h));
  set<int> g;
  rep(i, n - k + 1) { g.insert(h[i + k - 1] - h[i]); }
  cout << *begin(g) << endl;
}