alexjercan/bugnet huggingface dataset:
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int main() {

  int n, x, a[1008], sum = 0;
  cin >> n >> x;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < x; ++i)
    sum += a[i];
  cout << sum;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, k, f = 0, c, i, j, p[1000], sum = 0;
  cin >> n >> k;
  for (j = 0; j < n; j++) {
    cin >> p[j];
  }
  int mini = 1000;
  for (i = 0; i < n; i++) {
    for (j = f; j < n; j++) {
      if (mini > p[j]) {
        mini = p[j];
        c = j;
      }
    }
    p[c] = p[i];
    p[i] = mini;
    mini = p[i + 1];
    f = f + 1;
  }
  for (i = 0; i < k; i++) {
    sum = sum + p[i];
  }
  cout << sum;
  return 0;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
int main() {

  int n, k, temp;
  long long d = 0;
  cin >> n >> k;

  int *p = new int[n];

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

    cin >> p[i];
  }

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

    for (int t = i + 1; t < n; t++) {

      if (p[i] > p[t]) {
        temp = p[i];
        p[i] = p[t];
        p[t] = temp;
      }
    }
  }

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

    d = d + p[i];
  }

  cout << d;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, k, x[n], sum(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> x[i];
  sort(x, x + n);
  for (int i = 0; i < k; i++)
    sum += x[i];
  cout << sum << endl;
  return 0;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> arr;
  for (int i = 0; i < n; ++i) {
    int tmp;
    cin >> tmp;
    arr.push_back(tmp);
  }
  int tong = 0;
  for (int i = 0; i < k; ++i) {
    int dem = 0;
    int MIN = arr[0];
    for (int i = 1; i < arr.size(); ++i) {
      if (arr[i] < MIN) {
        MIN = arr[i];
        dem = i;
      }
    }
    tong += MIN;
    arr.erase(arr.begin() + dem);
  }
  cout << tong;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n1, n2, a[100];
  cin >> n1;
  for (int i = 0; i < n1; ++i) {
    cin >> a[i];
  }
  cin >> n2;
  for (int i = 0; i < n2; ++i) {
    int x, y, tong = 0;
    cin >> x >> y;
    for (int j = 0; j < n1; ++j) {
      if (a[i] == x)
        a[i] = y;
      tong += a[i];
    }
    cout << tong << endl;
  }
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  int sum = 0;
  for (int i = 0; i < k; i++) {
    sum += a[i];
  }

  cout << sum;
}


Error: WA

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  priority_queue<int> p;
  long int n, k;
  int nums[100];
  long int sum = 0;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> nums[i];
  }
  for (int i = 0; i < n; i++) {
    if (p.size() < (unsigned)k) {
      p.push(nums[i]);
    } else {
      if (p.top() > nums[i]) {
        p.pop();
        p.push(nums[i]);
      }
    }
  }
  while (!p.empty()) {
    sum += p.top();
    p.pop();
  }

  cout << sum;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

/*https://atcoder.jp/contests/abc171/tasks/abc171_b*/
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a, sum = 0;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    cin >> a;
    v.push_back(a);
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < k; i++)
    sum += v[i];
  cout << sum;
}

Error: -11

Fixed submission:

/*https://atcoder.jp/contests/abc171/tasks/abc171_b*/
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a, sum = 0;
  vector<int> v;
  for (int i = 0; i < n; i++)
    cin >> a;
  sort(v.begin(), v.end());
  for (int i = 0; i < k; i++)
    sum += v[i];
  cout << sum;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
#define endl '\\n'

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int K, N;
  cin >> N >> K;
  int a[N];
  for (int i = 0; i < N; i++) {
    int n;
    cin >> n;
    a[i] = n;
  }
  sort(a, a + N);
  int num = 0;
  for (int j = 0; j < K; j++) {
    num = num + a[j];
  }
  cout << num << endl;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define endl '\\n'

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int K, N;
  cin >> N >> K;
  int a[K];
  for (int i = 0; i < N; i++) {
    int n;
    cin >> n;
    a[i] = n;
  }
  sort(a, a + N);
  int num = 0;
  for (int j = 0; j < K; j++) {
    num = num + a[j];
  }
  cout << num << endl;
}
Problem p02628 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

typedef long long int llint;

unsigned int min(unsigned int A, unsigned int B) {
  if (A < B) {
    return A;
  } else {
    return B;
  }
}

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

  vector<int> P;
  P.push_back(0);
  for (int i = 0; i < N; i++) {
    int p;
    cin >> p;
    P.push_back(p);
  }

  int dp[N + 1][K + 1]; // dp[i][j]=i番目までj個選んだ時の最小の金額
  dp[0][0] = 0;
  for (int i = 1; i <= K; i++) {
    dp[0][i] = 0x0FFFFFFF;
  }
  for (int i = 1; i <= N; i++) {
    dp[i][0] = 0;
    for (int k = 1; k <= K; k++) {
      dp[i][k] = min(dp[i - 1][k - 1] + P[i], dp[i - 1][k]);
    }
  }

  /*
          for(int i=0;i<=N;i++){
                  for(int k=0;k<=K;k++){
                          clog << dp[i][k] << " ";
                  }
                  clog << endl;
          }
          */

  cout << dp[N][K] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

typedef long long int llint;

unsigned int min(unsigned int A, unsigned int B) {
  if (A < B) {
    return A;
  } else {
    return B;
  }
}

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

  vector<int> P;
  P.push_back(0);
  for (int i = 0; i < N; i++) {
    int p;
    cin >> p;
    P.push_back(p);
  }

  int dp[N + 1][K + 1]; // dp[i][j]=i番目までj個選んだ時の最小の金額
  dp[0][0] = 0;
  for (int i = 1; i <= K; i++) {
    dp[0][i] = 0x0FFFFFFF;
  }
  for (int i = 1; i <= N; i++) {
    dp[i][0] = 0;
    for (int k = 1; k <= K; k++) {
      dp[i][k] = min(dp[i - 1][k - 1] + P[i], dp[i - 1][k]);
    }
  }

  for (int i = 0; i <= N; i++) {
    for (int k = 0; k <= K; k++) {
      clog << dp[i][k] << " ";
    }
    clog << endl;
  }

  cout << dp[N][K] << endl;
  return 0;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {

  int n, k, min = 0;
  cin >> n >> k;

  vector<int> p;

  for (size_t i = 0; i < n; i++) {
    int x;
    cin >> x;
    p.push_back(x);
  }

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

  for (size_t i = 0; i < k; i++) {
    min += p[i];
  }

  cout << min;

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;
int main() {
  int n, m, e = 100, s = 0, r = 0;
  int a[10];
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  cin >> m;
  for (int i = 1; i <= n; i++) {
    if (a[i] < e) {
      e = a[i];
      r++;
      if (r <= m) {
        s = s + e;
      }
    }
  }
  cout << s;

  return 0;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int n, k;
  int index = 0;
  int sum = 0;
  int *p;
  cin >> n >> k;
  p = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < n; i++) {
    int index = i;
    int min = p[i];
    for (int j = i + 1; j < n; j++) {
      if (p[j] < min) {
        min = p[j];
        index = j;
      }
    }
    p[index] = p[i];
    p[i] = min;
  }

  for (int i = 0; i < k; i++) {
    sum += p[i];
  }
  cout << sum << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;

void sortIN(int p[], int a, int &index, int plus);

int main() {
  int n, k;
  int index = 0;
  int sum = 0;
  int *p;
  cin >> n >> k;
  p = new int[n];
  for (int i = 1; i < n; i++) {
    p[n] = 0;
  }
  cin >> p[0];
  for (int i = 0; i < (n - 1); i++) {
    int a;
    cin >> a;
    sortIN(p, a, index, 0);
  }
  for (int i = 0; i < k; i++) {
    sum += p[i];
  }
  cout << sum << endl;
  for (int i = 0; i < n; i++) {
    cout << p[i] << endl;
  }
  return 0;
}

void sortIN(int p[], int a, int &index, int plus) {
  if (p[index] <= a) {
    index++;
    if (index >= sizeof(p)) {
      return;
    }
    p[index] = a;
    index += plus;
  } else {
    p[index + 1] = p[index];
    index--;
    if (index < 0) {
      return;
    }
    plus++;
    sortIN(p, a, index, plus);
  }
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {

  int total, k;

  cin >> total;
  cin >> k;

  int arr[total];

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

  sort(arr, arr + total);

  int sum = 0;
  for (int i = 0; i < k; i++) {
    sum += arr[i];
  }

  cout << sum;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  int total, k;
  int arr[total];

  cin >> total;
  cin >> k;

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

  sort(arr, arr + total);

  int sum = 0;
  for (int i = 0; i < k; i++) {
    sum += arr[i];
  }

  cout << sum;

  return 0;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
// #define INF 1000000000000 //10^12:極めて大きい値,∞
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)

int main() {
  int N, K, sum = 0, temp;
  vector<int> p;
  cin >> N >> K;
  REP(i, N) {
    cin >> temp;
    p.emplace_back(temp);
  }
  sort(p.begin(), p.end());
  REP(i, K) sum += p[i];
  cout << sum << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
// #define INF 1000000000000 //10^12:極めて大きい値,∞
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)

int main() {
  int N, K, sum = 0;
  vector<int> p;
  cin >> N >> K;
  REP(i, N) cin >> p[i];
  sort(p.begin(), p.end());
  REP(i, K) sum += p[i];
  cout << sum;
  return 0;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, i, j;
  int p[10000];
  int sum = 0;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> p[i];
  }
  sort(p, p + n);
  for (i = 0; i < k; i++) {
    sum = sum + p[i];
  }
  cout << sum;
  return 0;
}


Error: WA

Fixed submission:

using namespace std;
#include <iostream>
int main() {
  int n, k, a[100];
  int sum = 0, j, i;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n - 1; i++) {
    for (j = 0; j < n - i - 1; j++) {

      if (a[i] > a[j]) {
        int temp;
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
      }
    }
  }
  for (i = 0; i < k; i++) {
    sum = sum + a[i];
  }
  cout << sum;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, K, sum = 0;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());
  for (int i = 0; i < K; i++) {
    sum += A.at(i);
  }
  cout << sum << endl;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 5) >= this->size() (which is 5)


Fixed submission:

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

int main() {
  int N, K, sum = 0;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());
  for (int i = 0; i, K; i++) {
    sum += A.at(i);
  }
  cout << sum << endl;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[1001], k, t, i, s = 0, j;
  cin >> t >> k;
  for (i = 0; i < t; i++) {
    cin >> a[i];
  }
  sort(a, a + t);
  for (j = 0; j < k; j++) {
    s = s + a[j];
  }
  cout << s << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[50], k, t, i, s = 0, j;
  cin >> t >> k;
  for (i = 0; i < t; i++) {
    cin >> a[i];
  }
  sort(a, a + t);
  for (j = 0; j < k; j++) {
    s = s + a[j];
  }
  cout << s << endl;
  return 0;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

int arr[1030];
int N, K;
int answer;

int main() {
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; i++) {
    scanf("%d", &arr[i]);
  }
  std::sort(arr, arr + N);
  for (int i = 0; i < K; i++) {
    answer += arr[i];
  }
  printf("%d", answer);
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>

int arr[1030];
int N, K;
int answer;

int main() {
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; i++) {
    scanf("%d", arr[i]);
  }
  std::sort(arr, arr + N);
  for (int i = 0; i < K; i++) {
    answer += arr[i];
  }
  printf("%d", answer);
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <string.h>
using namespace std;
using namespace std;

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

    sum = sum + arr[i];
  }
  cout << sum;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;
int main() {
  int n, m, s = 0, r = 0;
  int a[10];
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  cin >> m;
  for (int i = 1; i <= n; i++) {

    for (int j = 1; j <= n - 1; j++) {
      if (a[j] > a[j + 1]) {
        r = a[j + 1];
        a[j + 1] = a[j];
        a[j] = r;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    if (i <= m) {
      s = s + a[i];
    }
  }
  cout << s;

  return 0;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[i] > a[j]) {
        int x = a[i];
        a[i] = a[j];
        a[j] = x;
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < k; i++) {
    sum += a[i];
  }
  cout << sum;
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int b[n];
  for (int i = 0; i < n; i++) {
    int sum = 0;
    for (int j = i; j < k + i; j++) {
      sum += a[j];
    }
    b[i] = sum;
  }
  int min = b[0];
  for (int i = 0; i < n; i++) {
    if (b[i] <= min)
      min = b[i];
  }
  cout << min;
  return 0;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long int
#define pb push_back
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pii pair<int, int>
#define vvi vector<vector<int>>
#define vi vector<int>
#define f first
#define s second
using namespace std;
const int mod = 1e9 + 7;
int logi(int n) {
  int p = 0;
  while (n / 2) {
    n = n / 2;
    p++;
  }
  return p;
}
int poww(int x, int y) {
  int pro = 1;
  while (y) {
    if (y & 1)
      pro = ((pro % mod) * (x % mod) % mod);
    y = y >> 1;
    x = ((x % mod) * (x % mod) % mod);
  }
  return pro;
}
int poww2(int x, int y) {
  int pro = 1;
  while (y) {
    if (y & 1)
      pro = pro * x;
    y = y >> 1;
    x = x * x;
  }
  return pro;
}
bool isprime(int n) {
  if (n == 1)
    return false;
  for (int i = 2; i <= sqrt(n); i++)
    if (n % i == 0)
      return false;
  return true;
}

/*

*/

void solve() {
  int n, k;
  cin >> n >> k;
  vi v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  sort(v.begin(), v.end());
  int sum = 0;
  for (int i = 0; i < k; i++) {
    sum += v[i];
  }
  cout << sum;
}

int32_t main() {
  fast;
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long int
#define pb push_back
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pii pair<int, int>
#define vvi vector<vector<int>>
#define vi vector<int>
#define f first
#define s second
using namespace std;
const int mod = 1e9 + 7;
int logi(int n) {
  int p = 0;
  while (n / 2) {
    n = n / 2;
    p++;
  }
  return p;
}
int poww(int x, int y) {
  int pro = 1;
  while (y) {
    if (y & 1)
      pro = ((pro % mod) * (x % mod) % mod);
    y = y >> 1;
    x = ((x % mod) * (x % mod) % mod);
  }
  return pro;
}
int poww2(int x, int y) {
  int pro = 1;
  while (y) {
    if (y & 1)
      pro = pro * x;
    y = y >> 1;
    x = x * x;
  }
  return pro;
}
bool isprime(int n) {
  if (n == 1)
    return false;
  for (int i = 2; i <= sqrt(n); i++)
    if (n % i == 0)
      return false;
  return true;
}

/*

*/

int solve() {
  int n, k;
  cin >> n >> k;
  vi v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  sort(v.begin(), v.end());
  int sum = 0;
  for (int i = 0; i < k; i++) {
    sum += v[i];
  }
  cout << sum;
}

int32_t main() {
  fast;
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int k, n, a;
  vector<int> prices;
  cin >> n;
  if (1 > n || n > 1000)
    return 0;
  cin >> k;
  if (1 > k || k > 1000)
    return 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (1 > a || a > 1000)
      return 0;
    prices.push_back(a);
  }
  int minindex = 0, total = 0;
  for (int i = 0; i < k; i++) {
    total = total + *min_element(prices.begin(), prices.end());
    minindex = min_element(prices.begin(), prices.end()) - prices.begin();
    prices[minindex] = 10001;
  }
  cout << total;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
  int n, k, p = 0;
  cin >> n >> k;
  int arr[5];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  for (int i = 0; i < k; i++) {
    p += arr[i];
  }
  cout << p;
  return 0;
}
Problem p02628 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順
vector<int> P;
int N, K;

int main() {
  cin >> N >> K;
  P.resize(N);
  rep(i, N) { cin >> P.at(i); }
  sort(P.begin(), P.end());
  ll sum = 0;
  rep(i, K) sum += P[i];

  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順
vector<int> P;
int N, K;
vector<bool> used;

ll dfs(int count, ll cur) {
  if (count >= K)
    return cur;
  ll mi = INT64_MAX;
  rep(i, N) {
    if (used[i])
      continue;
    used[i] = true;
    mi = min(mi, dfs(count + 1, cur + P[i]));
    used[i] = false;
  }
  return mi;
}
int main() {
  cin >> N >> K;
  P.resize(N);
  used.resize(N);
  rep(i, N) { cin >> P.at(i); }

  cout << dfs(0, 0) << endl;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

template <typename T> T gcd(T x, T y) { return (y ? gcd(y, x % y) : x); }
template <typename T> T lcm(T x, T y) { return (x / gcd(x, y) * y); }
template <typename T> void printVec(vector<T> &vec) {
  cout << "";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    cout << *it << " ";
  }
  cout << endl;
}

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  int ans = 0;
  sort(p.begin(), p.end());
  for (int i = 0; i < k; i++) {
    ans = ans + p[i];
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

template <typename T> T gcd(T x, T y) { return (y ? gcd(y, x % y) : x); }
template <typename T> T lcm(T x, T y) { return (x / gcd(x, y) * y); }
template <typename T> void printVec(vector<T> &vec) {
  cout << "";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    cout << *it << " ";
  }
  cout << endl;
}

int ans = INT_MAX;

int cout1(int level, int max_level, vector<int> &p, vector<int> &q, int k) {
  int size = q.size();
  if (level == max_level) {
    q[level] = 0;
    // printVec(q);
    int count = 0;
    int ans2 = 0;
    for (int i = 0; i < size; i++) {
      count = count + q[i];
      if (q[i] == 1) {
        ans2 = ans2 + p[i];
      }
    }
    // cout << "count = " << count << endl;
    // cout << "ans2 = " << ans2 << endl;
    if (count == k) {
      if (ans > ans2) {
        ans = ans2;
      }
    }
    // cout <<"------" << endl;
    q[level] = 1;
    for (int i = 0; i < size; i++) {
      count = count + q[i];
      if (q[i] == 1) {
        ans2 = ans2 + p[i];
      }
    }
    if (count == k) {
      if (ans > ans2) {
        ans = ans2;
      }
    }
    return 0;
  }
  q[level] = 0;
  cout1(level + 1, max_level, p, q, k);

  q[level] = 1;
  cout1(level + 1, max_level, p, q, k);
}
int main() {

  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  vector<int> q(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  cout1(0, n, p, q, k);
  cout << ans << endl;
  return 0;
}
Problem p02628 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> vec;
  int n, val, k, sum = 0;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> val;
    vec.push_back(val);
  }
  sort(vec.begin(), vec.end());
  for (int i = 0; i < k; i++) {
    sum += vec[i];
  }
  cout << sum;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, k, arr[100], sum = 0;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  for (int i = 0; i < k; i++) {
    sum += arr[i];
  }
  cout << sum;
  return 0;
}