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

Buggy submission (status: Time Limit Exceeded):
// Nurbakyt Madibek
// Look at my code! IT'S AWESOME

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair
#define sz(a) (int)((a).size())
#ifdef _WIN32
#define I64 "%I64d"
#else
#define I64 "%lld"
#endif
#define fname "."

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<int, ull> pu;
typedef pair<ll, ll> pl;

const int inf = (int)1e9 + 123;
const ll infl = (ll)1e18 + 123;
const double eps = 1e-9;

const int MAX_N = (int)3e5 + 5;
const int mod = (int)1e9 + 7;

ll diff(ll a, ll b) { return abs(a - b); }

int main() {
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
#endif
  int a, b, c;
  cin >> a >> b >> c;
  ll mini = infl;
  for (int i = 1; i < a; i++)
    mini = min(mini, diff(1ll * i * b * c, (a - i) * b * c));
  for (int i = 1; i < b; i++)
    mini = min(mini, diff(1ll * i * a * c, (b - i) * a * c));
  for (int i = 1; i < c; i++)
    mini = min(mini, diff(1ll * i * a * b, (c - i) * a * b));
  cout << mini << endl;
  return 0;
}


Fixed submission:
// Nurbakyt Madibek
// Look at my code! IT'S AWESOME

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair
#define sz(a) (int)((a).size())
#ifdef _WIN32
#define I64 "%I64d"
#else
#define I64 "%lld"
#endif
#define fname "."

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<int, ull> pu;
typedef pair<ll, ll> pl;

const int inf = (int)1e9 + 123;
const ll infl = (ll)1e18 + 123;
const double eps = 1e-9;

const int MAX_N = (int)3e5 + 5;
const int mod = (int)1e9 + 7;

ll diff(ll a, ll b) { return abs(a - b); }

int main() {
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
#endif
  int a, b, c;
  cin >> a >> b >> c;
  ll mini = infl;
  for (int i = a / 2; i <= a / 2 + a % 2; i++)
    mini = min(mini, diff(1ll * i * b * c, 1ll * (a - i) * b * c));
  for (int i = b / 2; i <= b / 2 + b % 2; i++)
    mini = min(mini, diff(1ll * i * a * c, 1ll * (b - i) * a * c));
  for (int i = c / 2; i <= c / 2 + c % 2; i++)
    mini = min(mini, diff(1ll * i * a * b, 1ll * (c - i) * a * b));
  cout << mini << endl;
  return 0;
}
Problem p04005 (Python)

Buggy submission (status: Runtime Error):
A, B, C = map(int, input().split())

if any(i % 2 == 0 for i in [A, B, C]):
    print(0)
else:
    AB = A * B
    lis = [AB] * C
    mid = len(lis) // 2
    print(sum(lis[mid:]) - sum(lis[0:mid]))


Fixed submission:
A, B, C = map(int, input().split())

if any(i % 2 == 0 for i in [A, B, C]):
    print(0)
else:
    print(min(A * B, B * C, C * A))
Problem p04005 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main() {
  unsigned long long a, b, c;

  cin >> a >> b >> c;

  unsigned long long aa = (a % 2) * b * c;
  unsigned long long bb = a * (b % 2) * c;
  unsigned long long cc = a * b * (c % 2);

  unsigned long long ans = min(aa, min(bb, cc));

  cout << ans << endl;

  return ans;
}

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main() {
  unsigned long long a, b, c;

  cin >> a >> b >> c;

  unsigned long long aa = (a % 2) * b * c;
  unsigned long long bb = a * (b % 2) * c;
  unsigned long long cc = a * b * (c % 2);

  unsigned long long ans = min(aa, min(bb, cc));

  cout << ans << endl;

  return 0;
}
Problem p04006 (C++)

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

#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define si(c) ((int)(c).size())
#define forsn(i, s, n) for (int i = (int)(s); i < ((int)n); i++)
#define dforsn(i, s, n) for (int i = (int)(n)-1; i >= ((int)s); i--)
#define all(c) (c).begin(), (c).end()
#define D(a) cerr << #a << "=" << a << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

#ifdef LOCAL
#define i64 "%lld"
#else
#define i64 "%I64d"
#endif

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int N = 2020;

int n, x, a[N], c[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> x;
  forn(i, n) cin >> a[i], c[i] = a[i];

  ll res = 8e18;
  forn(spells, x) {
    ll cand = (ll)x * spells;
    forn(i, n) cand += c[i];
    res = min(res, cand);

    int j = (2 * n - spells - 1) % n;
    forn(i, n) {
      c[i] = min(c[i], a[j]);
      j = (j + 1) % n;
    }
  }
  cout << res << endl;

  return 0;
}


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

#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define si(c) ((int)(c).size())
#define forsn(i, s, n) for (int i = (int)(s); i < ((int)n); i++)
#define dforsn(i, s, n) for (int i = (int)(n)-1; i >= ((int)s); i--)
#define all(c) (c).begin(), (c).end()
#define D(a) cerr << #a << "=" << a << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

#ifdef LOCAL
#define i64 "%lld"
#else
#define i64 "%I64d"
#endif

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int N = 2020;

int n, x, a[N], c[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> x;
  forn(i, n) cin >> a[i], c[i] = a[i];

  ll res = 8e18;
  forn(spells, n) {
    ll cand = (ll)x * spells;
    forn(i, n) cand += c[i];
    res = min(res, cand);

    int j = (2 * n - spells - 1) % n;
    forn(i, n) {
      c[i] = min(c[i], a[j]);
      j = (j + 1) % n;
    }
  }
  cout << res << endl;

  return 0;
}
Problem p04006 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 2005;
int a[N], mn[N][N];
int now[N];
int n, k;
inline int Min(int a, int b) { return a < b ? a : b; }
inline int qmin(int x, int l) {
  if (x - l > 0)
    return mn[x - l][x];
  return Min(mn[1][x], mn[n - (x - l)][n]);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    for (int j = i; j <= n; j++) {
      if (i == j)
        mn[i][j] = a[i];
      else
        mn[i][j] = Min(mn[i][j - 1], a[j]);
    }
  long long ans = 1ll << 60ll;
  for (int i = 0; i < n; i++) {
    long long now = i * k;
    for (int j = 1; j <= n; j++)
      now += qmin(j, i);
    ans = min(ans, now);
  }
  printf("%lld", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 2005;
int a[N], mn[N][N];
int now[N];
int n, k;
inline int Min(int a, int b) { return a < b ? a : b; }
inline int qmin(int x, int l) {
  if (x - l > 0)
    return mn[x - l][x];
  return Min(mn[1][x], mn[n - (l - x)][n]);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    for (int j = i; j <= n; j++) {
      if (i == j)
        mn[i][j] = a[i];
      else
        mn[i][j] = Min(mn[i][j - 1], a[j]);
    }
  long long ans = 1ll << 60ll;
  for (int i = 0; i < n; i++) {
    long long now = i * k;
    for (int j = 1; j <= n; j++)
      now += qmin(j, i);
    ans = min(ans, now);
  }
  printf("%lld", ans);
  return 0;
}
Problem p04006 (C++)

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

using namespace std;

int N, X;
int A[2000];
int B[2000];

int main() {
  scanf("%d%d", &N, &X);
  for (int i = 0; i < N; i++)
    scanf("%d", A + i), B[i] = A[i];
  long long ans = 0x3f3f3f3f3f3f3f3fLL;
  for (int i = 0; i < X; i++) {
    for (int j = 0; j < N; j++)
      B[j] = min(B[j], A[(i + j) % N]);
    ans = min(ans, 1LL * i * X + accumulate(B, B + N, 0LL));
  }
  printf("%lld\n", ans);
  return 0;
}


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

using namespace std;

int N, X;
int A[2000];
int B[2000];

int main() {
  scanf("%d%d", &N, &X);
  for (int i = 0; i < N; i++)
    scanf("%d", A + i), B[i] = A[i];
  long long ans = 0x3f3f3f3f3f3f3f3fLL;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      B[j] = min(B[j], A[(i + j) % N]);
    ans = min(ans, 1LL * i * X + accumulate(B, B + N, 0LL));
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p04006 (C++)

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

typedef long long ll;

const int N = 2000;

int a[2 * N + 5], n, x;

int le[2 * N + 5], ri[N + 5];

ll s[2 * N + 5];

int main() {
  scanf("%d%d", &n, &x);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[n + i] = a[i];
  a[0] = a[2 * n + 1] = 0;
  for (int i = 1; i <= 2 * n; i++) {
    le[i] = i - 1;
    while (a[le[i]] >= a[i])
      le[i] = le[le[i]];
  }
  for (int i = 2 * n; i >= 1; i--) {
    ri[i] = i + 1;
    while (a[ri[i]] > a[i])
      ri[i] = ri[ri[i]];
  }
  for (int i = 1; i <= n; i++) {
    int p = (n + i) - le[n + i], q = ri[i] - i;
    s[0] += a[i], s[min(p, q)] -= a[i];
    s[max(p, q)] -= a[i], s[min(p, q) + max(p, q)] += a[i];
  }

  for (int i = 1; i <= n; i++)
    s[i] += s[i - 1];
  for (int i = 1; i <= n; i++)
    s[i] += s[i - 1];

  ll ans = (1ll << 60);
  for (int i = 0; i <= n; i++)
    ans = min(ans, s[i] + 1ll * i * x);
  printf("%lld\n", ans);
}

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

typedef long long ll;

const int N = 4000;

int a[2 * N + 5], n, x;

int le[2 * N + 5], ri[N + 5];

ll s[2 * N + 5];

int main() {
  scanf("%d%d", &n, &x);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[n + i] = a[i];
  a[0] = a[2 * n + 1] = 0;
  for (int i = 1; i <= 2 * n; i++) {
    le[i] = i - 1;
    while (a[le[i]] >= a[i])
      le[i] = le[le[i]];
  }
  for (int i = 2 * n; i >= 1; i--) {
    ri[i] = i + 1;
    while (a[ri[i]] > a[i])
      ri[i] = ri[ri[i]];
  }
  for (int i = 1; i <= n; i++) {
    int p = (n + i) - le[n + i], q = ri[i] - i;
    s[0] += a[i], s[min(p, q)] -= a[i];
    s[max(p, q)] -= a[i], s[min(p, q) + max(p, q)] += a[i];
  }

  for (int i = 1; i <= n; i++)
    s[i] += s[i - 1];
  for (int i = 1; i <= n; i++)
    s[i] += s[i - 1];

  ll ans = (1ll << 60);
  for (int i = 0; i <= n; i++)
    ans = min(ans, s[i] + 1ll * i * x);
  printf("%lld\n", ans);
}
Problem p04006 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "math.h"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "utility"

using namespace std;
const long long int MOD = 1000000007;

long long int power(long long int x, long long int n, long long int M) {
  long long int tmp = 1;

  if (n > 0) {
    tmp = power(x, n / 2, M);
    if (n % 2 == 0)
      tmp = (tmp * tmp) % M;
    else
      tmp = (((tmp * tmp) % M) * x) % M;
  }
  return tmp;
}

long long int N, M, K, Q, W, H, L, R;
long long int ans;

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  long long int num[1001][1001] = {};
  cin >> N >> M;
  long long int slime[2000] = {};
  for (int i = 0; i < N; i++) {
    cin >> slime[i];
    num[i][0] = slime[i];
  }
  for (int i = 0; i < N; i++) {
    for (int j = 1; j < N; j++) {
      num[i][j] = min(num[i][j - 1], slime[(i + N - j) % N]);
      // cout << i << " " << j << " " << num[i][j] << endl;
    }
  }
  ans = LLONG_MAX;
  for (int i = 0; i < N; i++) {
    long long int box = 0;
    for (int j = 0; j < N; j++) {
      box += num[j][i];
    }
    box += M * i;
    //	cout << box << endl;
    ans = min(ans, box);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "math.h"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "utility"

using namespace std;
const long long int MOD = 1000000007;

long long int power(long long int x, long long int n, long long int M) {
  long long int tmp = 1;

  if (n > 0) {
    tmp = power(x, n / 2, M);
    if (n % 2 == 0)
      tmp = (tmp * tmp) % M;
    else
      tmp = (((tmp * tmp) % M) * x) % M;
  }
  return tmp;
}

long long int N, M, K, Q, W, H, L, R;
long long int ans;

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  long long int num[2001][2001] = {};
  cin >> N >> M;
  long long int slime[2000] = {};
  for (int i = 0; i < N; i++) {
    cin >> slime[i];
    num[i][0] = slime[i];
  }
  for (int i = 0; i < N; i++) {
    for (int j = 1; j < N; j++) {
      num[i][j] = min(num[i][j - 1], slime[(i + N - j) % N]);
      // cout << i << " " << j << " " << num[i][j] << endl;
    }
  }
  ans = LLONG_MAX;
  for (int i = 0; i < N; i++) {
    long long int box = 0;
    for (int j = 0; j < N; j++) {
      box += num[j][i];
    }
    box += M * i;
    //	cout << box << endl;
    ans = min(ans, box);
  }
  cout << ans << endl;
  return 0;
}
Problem p04006 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize(2)
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define LL long long
LL read() {
  LL f = 1, x = 0;
  char c = getchar();
  while (c < '0' || '9' < c) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return f * x;
}
#define MAXN 2000
#define INF 10000000000000000ll
LL a[MAXN + 5], mn[MAXN + 5];
int main() {
  LL n = read(), x = read(), ans = INF;
  for (int i = 1; i <= n; i++)
    a[i] = a[i + n] = read(), mn[i] = INF;
  for (int t = 0; t <= n; t++) {
    LL tmp = t * x;
    for (int i = 1; i <= n; i++)
      mn[i] = min(mn[i], a[i + t]), tmp += mn[i];
    ans = min(ans, tmp);
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
// #pragma GCC optimize(2)
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define LL long long
LL read() {
  LL f = 1, x = 0;
  char c = getchar();
  while (c < '0' || '9' < c) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return f * x;
}
#define MAXN 5000
#define INF 10000000000000000ll
LL a[MAXN + 5], mn[MAXN + 5];
int main() {
  LL n = read(), x = read(), ans = INF;
  for (int i = 1; i <= n; i++)
    a[i] = a[i + n] = read(), mn[i] = INF;
  for (int t = 0; t <= n; t++) {
    LL tmp = t * x;
    for (int i = 1; i <= n; i++)
      mn[i] = min(mn[i], a[i + t]), tmp += mn[i];
    ans = min(ans, tmp);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p04006 (C++)

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

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(), x.end()
#define F first
#define S second
#define For(i, x, y) for (ll i = x; i <= y; i++)
#define FOr(i, x, y) for (ll i = x; i >= y; i--)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)
// ROAD to... Red

using namespace std;

inline bool isvowel(char c) {
  c = tolower(c);
  if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u')
    return 1;
  return 0;
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int n;
int a[N];
int mn[2002][2002];

int main() {
  int x;
  cin >> n >> x;
  ll ans = INF;
  For(i, 1, n) {
    cin >> a[i];
    a[i + n] = a[i];
  }

  for (int i = 1; i <= n + n; i++) {
    mn[i][i] = a[i];
    for (int j = i + 1; j <= n + n; j++)
      mn[i][j] = min(mn[i][j - 1], a[j]);
  }

  For(t, 0, n) {
    ll cur = 1ll * t * x;
    For(i, n + 1, n + n) {
      //    cout << mn[i - t][i] << '\n';
      cur += mn[i - t][i];
    }
    // cout << '\n';

    ans = min(ans, cur);
  }
  cout << ans << '\n';
  return Accepted;
}

// Coded By OB

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

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(), x.end()
#define F first
#define S second
#define For(i, x, y) for (ll i = x; i <= y; i++)
#define FOr(i, x, y) for (ll i = x; i >= y; i--)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)
// ROAD to... Red

using namespace std;

inline bool isvowel(char c) {
  c = tolower(c);
  if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u')
    return 1;
  return 0;
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int n;
int a[N];
int mn[4002][4002];

int main() {
  int x;
  cin >> n >> x;
  ll ans = INF;
  For(i, 1, n) {
    cin >> a[i];
    a[i + n] = a[i];
  }

  for (int i = 1; i <= n + n; i++) {
    mn[i][i] = a[i];
    for (int j = i + 1; j <= n + n; j++)
      mn[i][j] = min(mn[i][j - 1], a[j]);
  }

  For(t, 0, n) {
    ll cur = 1ll * t * x;
    For(i, n + 1, n + n) {
      //    cout << mn[i - t][i] << '\n';
      cur += mn[i - t][i];
    }
    // cout << '\n';

    ans = min(ans, cur);
  }
  cout << ans << '\n';
  return Accepted;
}

// Coded By OB
Problem p04006 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MAX_N = 2000;
int a[MAX_N], n, x;
int f[MAX_N][MAX_N];
int main() {
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long ans = 1e18;
  // スライムiを入手するのに必要なコストを考える　j回変色させたときの最小がf[i][j]
  for (int i = 0; i < n; i++) {
    f[i][0] = a[i];
    for (int j = 1; j < n; j++) {
      f[i][j] = min(f[i][j - 1], a[(i - j + n)]);
    }
  }
  // 変色させる回数をiとする
  for (int i = 0; i < n; i++) {
    long long cur = (long long)x * i;
    for (int j = 0; j < n; j++) {
      cur += f[j][i];
    }
    ans = min(ans, cur);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MAX_N = 2000;
int a[MAX_N], n, x;
int f[MAX_N][MAX_N];
int main() {
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long ans = 1e18;
  // スライムiを入手するのに必要なコストを考える　j回変色させたときの最小がf[i][j]
  for (int i = 0; i < n; i++) {
    f[i][0] = a[i];
    for (int j = 1; j < n; j++) {
      f[i][j] = min(f[i][j - 1], a[(i - j + n) % n]);
    }
  }
  // 変色させる回数をiとする
  for (int i = 0; i < n; i++) {
    long long cur = (long long)x * i;
    for (int j = 0; j < n; j++) {
      cur += f[j][i];
    }
    ans = min(ans, cur);
  }

  cout << ans << endl;

  return 0;
}
Problem p04006 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#define maxn 2005
using namespace std;
const long long INF = 999999999999999;
int n;
long long x;
long long a[maxn], d[maxn][maxn];
long long ans, res;
int main() {
  scanf("%d%lld", &n, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    d[i][i - 1] = INF;
    for (int j = i; j <= n; j++) {
      d[i][j] = min(d[i][j - 1], a[j]);
    }
  }
  ans = INF;
  for (int i = 0; i <= n; i++) {
    res = i * x;
    for (int j = 1; j <= i; j++) {
      res += min(d[n - i + j][n], d[1][j]);
      if (res >= INF)
        break;
    }
    for (int j = 1; j <= n; j++) {
      res += d[j - i][j];
      if (res >= INF)
        break;
    }
    ans = min(ans, res);
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define maxn 2005
using namespace std;
const long long INF = 999999999999999;
int n;
long long x;
long long a[maxn], d[maxn][maxn];
long long ans, res;
int main() {
  scanf("%d%lld", &n, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    d[i][i - 1] = INF;
    for (int j = i; j <= n; j++) {
      d[i][j] = min(d[i][j - 1], a[j]);
    }
  }
  ans = INF;
  for (int i = 0; i <= n; i++) {
    res = i * x;
    for (int j = 1; j <= i; j++) {
      res += min(d[n - i + j][n], d[1][j]);
      if (res >= INF)
        break;
    }
    for (int j = i + 1; j <= n; j++) {
      res += d[j - i][j];
      if (res >= INF)
        break;
    }
    ans = min(ans, res);
  }
  printf("%lld", ans);
  return 0;
}
Problem p04006 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

ll N, x;
ll a[2010];
int main() {
  cin >> N >> x;
  for (ll i = 0; i < N; i++) {
    cin >> a[i];
  }
  multiset<ll> st;
  ll ans = INF;
  for (ll m = 0; m < N; m++) {
    for (ll i = N - m; i < N; i++) {
      st.insert(a[i]);
    }
    ll cnt = 0;
    ll deleteIndex = N - m;
    if (m == 0)
      deleteIndex = 0;
    for (ll i = 0; i < N; i++) {
      st.insert(a[i]);
      auto minIt = st.begin();
      cnt += *minIt;
      st.erase(a[deleteIndex]);
      deleteIndex++;
    }
    ans = min(ans, cnt + x * m);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

ll N, x;
ll a[2010];
int main() {
  cin >> N >> x;
  for (ll i = 0; i < N; i++) {
    cin >> a[i];
  }
  multiset<ll> st;
  ll ans = INF;
  for (ll m = 0; m < N; m++) {
    for (ll i = N - m; i < N; i++) {
      st.insert(a[i]);
    }
    ll cnt = 0;
    ll deleteIndex = N - m;
    if (m == 0)
      deleteIndex = 0;
    for (ll i = 0; i < N; i++) {
      st.insert(a[i]);
      auto minIt = st.begin();
      cnt += *minIt;
      st.erase(a[deleteIndex]);
      deleteIndex++;
      if (deleteIndex >= N)
        deleteIndex = 0;
    }
    ans = min(ans, cnt + x * m);
  }
  cout << ans << endl;
  return 0;
}
Problem p04006 (C++)

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

ll n, x;
ll a[2010];
ll m[2010];
ll ans = (1ll << 62), t;

int main() {
  cin >> n >> x;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = n; i < 2 * n; i++)
    a[i] = a[i - n];
  for (int i = 0; i < n; i++)
    m[i] = a[i];
  for (int i = 0; i < n; i++) {
    ll ct = 0;
    for (int j = 0; j < n; j++)
      ct += m[j];
    ans = min(ans, ct + t);
    t += x;
    for (int j = 0; j < n; j++)
      m[j] = min(m[j], a[j + i + 1]);
  }
  cout << ans << endl;
  return 0;
}

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

ll n, x;
ll a[4010];
ll m[4010];
ll ans = (1ll << 62), t;

int main() {
  cin >> n >> x;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = n; i < 2 * n; i++)
    a[i] = a[i - n];
  for (int i = 0; i < n; i++)
    m[i] = a[i];
  for (int i = 0; i < n; i++) {
    ll ct = 0;
    for (int j = 0; j < n; j++)
      ct += m[j];
    ans = min(ans, ct + t);
    t += x;
    for (int j = 0; j < n; j++)
      m[j] = min(m[j], a[j + i + 1]);
  }
  cout << ans << endl;
  return 0;
}
Problem p04006 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define cmin(a, b) (a > b ? a = b : a)
#define cmax(a, b) (a < b ? a = b : a)
#define mem(a, k) memset(a, k, sizeof(a))
#define lop(i, s, t) for (int i = s; i < (t); ++i)
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define fore(i, v)                                                             \
  for (int i = g[v], d = es[i].d; i; i = es[i].nxt, d = es[i].d)
using namespace std;

#define Pr(f, ...) fprintf(stderr, f, ##__VA_ARGS__), fflush(stderr)

typedef long long ll;

template <typename T> void read(T &x) {
  x = 0;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
}

const int N = 2e5 + 50;

/*
int mul(int a,int b){ return 1ll*a*b%P; }
int add(int a,int b){ a+=b; return a>=P?a-P:a; }
int sub(int a,int b){ a-=b; return a<0?a+P:a; }
*/

ll n, x, a[N], ans = 1e18, q[N], h, t;

ll solve(int k) {
  ll res = 0;
  lop(i, 0, n + n) {
    while (h != t && i - q[h] > k)
      ++h;
    while (h != t && a[i] < a[q[t - 1]])
      --t;
    q[t++] = i;
    if (i >= n)
      res += a[q[h]];
  }
  return res;
}

int main(int argc, char *argv[]) {
#ifdef CURIOUSCAT
  // freopen("dat.in","r",stdin);
  // freopen("my.out","w",stdout);
#endif
  read(n), read(x);
  lop(i, 0, n) read(a[i]);
  lop(i, n, n + n) a[i] = a[i - n];
  lop(k, 0, n) ans = min(ans, solve(k) + x * k);
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define cmin(a, b) (a > b ? a = b : a)
#define cmax(a, b) (a < b ? a = b : a)
#define mem(a, k) memset(a, k, sizeof(a))
#define lop(i, s, t) for (int i = s; i < (t); ++i)
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define fore(i, v)                                                             \
  for (int i = g[v], d = es[i].d; i; i = es[i].nxt, d = es[i].d)
using namespace std;

#define Pr(f, ...) fprintf(stderr, f, ##__VA_ARGS__), fflush(stderr)

typedef long long ll;

template <typename T> void read(T &x) {
  x = 0;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
}

const int N = 2e5 + 50;

/*
int mul(int a,int b){ return 1ll*a*b%P; }
int add(int a,int b){ a+=b; return a>=P?a-P:a; }
int sub(int a,int b){ a-=b; return a<0?a+P:a; }
*/

ll n, x, a[N], ans = 1e18, q[N], h, t;

ll solve(int k) {
  h = t = 0;
  ll res = 0;
  lop(i, 0, n + n) {
    while (h != t && i - q[h] > k)
      ++h;
    while (h != t && a[i] < a[q[t - 1]])
      --t;
    q[t++] = i;
    if (i >= n)
      res += a[q[h]];
  }
  return res;
}

int main(int argc, char *argv[]) {
#ifdef CURIOUSCAT
  // freopen("dat.in","r",stdin);
  // freopen("my.out","w",stdout);
#endif
  read(n), read(x);
  lop(i, 0, n) read(a[i]);
  lop(i, n, n + n) a[i] = a[i - n];
  lop(k, 0, n) ans = min(ans, solve(k) + x * k);
  printf("%lld\n", ans);
  return 0;
}
Problem p04006 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
ll N, X;
ll A[2002];
ll R[2002];
int main() {
  cin >> N >> X;
  int mid = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[mid] > A[i])
      mid = i;
  }
  for (int i = N; i < 2 * N; i++)
    A[i] = A[i - N];
  ll res = LLONG_MAX;
  for (int i = 0; i < N; i++) {
    ll at = 0;
    priority_queue<P, vector<P>, greater<P>> q;
    for (int j = 0; j < 2 * N; j++) {
      q.push(P(A[j], j));
      while (!q.empty()) {
        P p = q.top();
        if (j - i <= p.second)
          break;
        q.pop();
      }
      //      cout << j << " "<< q.top().first << " " << q.top().second<< endl;
      if (j >= N)
        at += q.top().first;
    }
    res = min(at + X * (ll)i, res);
  }
  cout << res << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
ll N, X;
ll A[4002];
ll R[4002];
int main() {
  cin >> N >> X;
  int mid = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[mid] > A[i])
      mid = i;
  }
  for (int i = N; i < 2 * N; i++)
    A[i] = A[i - N];
  ll res = LLONG_MAX;
  for (int i = 0; i < N; i++) {
    ll at = 0;
    priority_queue<P, vector<P>, greater<P>> q;
    for (int j = 0; j < 2 * N; j++) {
      q.push(P(A[j], j));
      while (!q.empty()) {
        P p = q.top();
        if (j - i <= p.second)
          break;
        q.pop();
      }
      //      cout << j << " "<< q.top().first << " " << q.top().second<< endl;
      if (j >= N)
        at += q.top().first;
    }
    res = min(at + X * (ll)i, res);
  }
  cout << res << endl;
}
Problem p04006 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int n;
  long long x, a[2000], b[2000], ans = 1e18;
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  for (int j = 0; j < n; j++) {
    long long s = x * j;
    for (int i = 0; i < x; i++) {
      b[i] = min(b[i], a[(i - j + n) % n]);
      s += b[i];
    }
    ans = min(ans, s);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int n;
  long long x, a[2000], b[2000], ans = 1e18;
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  for (int j = 0; j < n; j++) {
    long long s = x * j;
    for (int i = 0; i < n; i++) {
      b[i] = min(b[i], a[(i - j + n) % n]);
      s += b[i];
    }
    ans = min(ans, s);
  }
  cout << ans << endl;
  return 0;
}
Problem p04006 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 2005
#define inf 1e18
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

inline ll read() {
  ll x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

ll n, a[maxn], x, mn[maxn][maxn], ans;

int main() {
  n = read();
  x = read();
  rep(i, 1, n) a[i] = read(), a[n + i] = a[i], mn[i][i] = a[i];
  rep(i, (n + 1), 2 * n) mn[i][i] = a[i];
  rep(i, 1, 2 * n) rep(j, (i + 1), 2 * n) mn[i][j] = min(mn[i][j - 1], a[j]);
  ans = inf;
  for (int k = 0; k <= n - 1; k++) {
    ll tmp = k * x;
    for (int i = 1; i <= n; i++)
      tmp += mn[i - k + n][i + n];
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 4005
#define inf 1e18
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

inline ll read() {
  ll x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

ll n, a[maxn], x, mn[maxn][maxn], ans;

int main() {
  n = read();
  x = read();
  rep(i, 1, n) a[i] = read(), a[n + i] = a[i], mn[i][i] = a[i];
  rep(i, (n + 1), 2 * n) mn[i][i] = a[i];
  rep(i, 1, 2 * n) rep(j, (i + 1), 2 * n) mn[i][j] = min(mn[i][j - 1], a[j]);
  ans = inf;
  for (int k = 0; k <= n - 1; k++) {
    ll tmp = k * x;
    for (int i = 1; i <= n; i++)
      tmp += mn[i - k + n][i + n];
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}
Problem p04006 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF (1 << 29)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))

Int n, x;
Int ans[2160], a[2160], res = 1LL << 60;
int main() {
  cin >> n >> x;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i <= n; i++) {
    Int tmp = x * i;
    for (int j = 0; j < n; j++) {
      tmp += a[j];
    }
    res = min(tmp, res);
    Int z = a[n - 1];
    for (int j = n - 1; j > 0; j++) {
      a[j] = min(a[j], a[j - 1]);
    }
    a[0] = min(a[0], z);
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF (1 << 29)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))

Int n, x;
Int ans[2160], a[2160], res = 1LL << 60;
int main() {
  cin >> n >> x;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i <= n; i++) {
    Int tmp = x * i;
    for (int j = 0; j < n; j++) {
      tmp += a[j];
    }
    res = min(tmp, res);
    Int z = a[n - 1];
    for (int j = n - 1; j > 0; j--) {
      a[j] = min(a[j], a[j - 1]);
    }
    a[0] = min(a[0], z);
  }
  cout << res << endl;
  return 0;
}
Problem p04006 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

#define mp make_pair
#define pb push_back
#define x first
#define y second

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

int a[2010], q[2010];

int main() {
#ifdef waltz
  freopen("input.txt", "r", stdin);
  // freopen("output.txt","w",stdout);
#endif
  int n, x;
  scanf("%d%d", &n, &x);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    a[i + n] = a[i];
  ll ans = 100000000000000000ll;
  for (int i = 0; i < n; i++) {
    int f = 1, r = 0;
    ll tmp = ll(x) * i;
    for (int j = 1; j <= 2 * n; j++) {
      while (f <= r && a[q[r]] > a[j])
        r--;
      q[++r] = j;
      while (f <= r && q[f] + i < j)
        f++;
      if (j > n)
        tmp += a[q[f]];
    }
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

#define mp make_pair
#define pb push_back
#define x first
#define y second

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

int a[4010], q[4010];

int main() {
#ifdef waltz
  freopen("input.txt", "r", stdin);
  // freopen("output.txt","w",stdout);
#endif
  int n, x;
  scanf("%d%d", &n, &x);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    a[i + n] = a[i];
  ll ans = 100000000000000000ll;
  for (int i = 0; i < n; i++) {
    int f = 1, r = 0;
    ll tmp = ll(x) * i;
    for (int j = 1; j <= 2 * n; j++) {
      while (f <= r && a[q[r]] > a[j])
        r--;
      q[++r] = j;
      while (f <= r && q[f] + i < j)
        f++;
      if (j > n)
        tmp += a[q[f]];
    }
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}
Problem p04006 (C++)

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

using namespace std;
typedef long long LL;

#define re register
#define gc getchar
#define digit(x) (x >= '0' && x <= '9')

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!digit(c)) {
    if (c == '-')
      f = -1;
    c = gc();
  }
  while (digit(c))
    x = (x << 3) + (x << 1) + c - '0', c = gc();
  return x * f;
}

const int N = 2005;
const LL inf = 0x3f3f3f3f3f3f3f3f;

int n, x;
int a[N];
int sk[N << 1], pos[N << 1], tp, tl;

inline int P(int x, int y) {
  if (x < y)
    return y - x;
  return y - x + n;
}

int main() {
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);

  n = read();
  x = read();

  for (re int i = 1; i <= n; i++)
    a[i] = read();

  LL ans = inf;
  for (re int i = 0; i < n; i++) {
    tl = tp = 0;
    LL tmp = 0;
    for (re int j = i; j >= 1; j--) {
      while (tl < tp && sk[tp - 1] >= a[n - j + 1])
        tp--;
      sk[tp] = a[n - j + 1], pos[tp++] = n - j + 1;
    }
    for (re int j = 1; j <= n; j++) {
      while (tl < tp && P(pos[tl], j) > i)
        tl++;
      while (tl < tp && sk[tp - 1] >= a[j])
        tp--;
      sk[tp] = a[j], pos[tp++] = j;
      tmp += sk[tl];
    }
    ans = min(ans, tmp + 1ll * i * x);
  }
  printf("%lld\n", ans);
  return 0;
}

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

using namespace std;
typedef long long LL;

#define re register
#define gc getchar
#define digit(x) (x >= '0' && x <= '9')

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!digit(c)) {
    if (c == '-')
      f = -1;
    c = gc();
  }
  while (digit(c))
    x = (x << 3) + (x << 1) + c - '0', c = gc();
  return x * f;
}

const int N = 2005;
const LL inf = 0x3f3f3f3f3f3f3f3f;

int n, x;
int a[N];
int sk[N << 1], pos[N << 1], tp, tl;

inline int P(int x, int y) {
  if (x < y)
    return y - x;
  return y - x + n;
}

int main() {
  n = read();
  x = read();

  for (re int i = 1; i <= n; i++)
    a[i] = read();

  LL ans = inf;
  for (re int i = 0; i < n; i++) {
    tl = tp = 0;
    LL tmp = 0;
    for (re int j = i; j >= 1; j--) {
      while (tl < tp && sk[tp - 1] >= a[n - j + 1])
        tp--;
      sk[tp] = a[n - j + 1], pos[tp++] = n - j + 1;
    }
    for (re int j = 1; j <= n; j++) {
      while (tl < tp && P(pos[tl], j) > i)
        tl++;
      while (tl < tp && sk[tp - 1] >= a[j])
        tp--;
      sk[tp] = a[j], pos[tp++] = j;
      tmp += sk[tl];
    }
    ans = min(ans, tmp + 1ll * i * x);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p04006 (C++)

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

using s64 = long long;

int main() {
  freopen("1_02.txt", "r", stdin);

  int n, extra;
  cin >> n >> extra;
  vector<int> cost(n);
  for (int i = 0; i < n; ++i) {
    cin >> cost[i];
  }

  vector<vector<int>> low(n);
  for (int i = 0; i < n; ++i) {
    low[i].resize(n);
    low[i][i] = cost[i];
    for (int k = 1; k < n; ++k) {
      int j = (i + k) % n;
      low[i][j] = min(low[i][(j + n - 1) % n], cost[j]);
    }
  }

  s64 answer = accumulate(cost.begin(), cost.end(), 0ll);
  for (int i = 1; i < n; ++i) {
    s64 current = (s64)i * extra;
    for (int r = 0; r < n; ++r) {
      current += low[(r + n - i) % n][r];
    }
    answer = min(answer, current);
  }
  cout << answer << endl;

  return 0;
}

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

using s64 = long long;

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

  vector<vector<int>> low(n);
  for (int i = 0; i < n; ++i) {
    low[i].resize(n);
    low[i][i] = cost[i];
    for (int k = 1; k < n; ++k) {
      int j = (i + k) % n;
      low[i][j] = min(low[i][(j + n - 1) % n], cost[j]);
    }
  }

  s64 answer = accumulate(cost.begin(), cost.end(), 0ll);
  for (int i = 1; i < n; ++i) {
    s64 current = (s64)i * extra;
    for (int r = 0; r < n; ++r) {
      current += low[(r + n - i) % n][r];
    }
    answer = min(answer, current);
  }
  cout << answer << endl;

  return 0;
}
Problem p04006 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const ll INF = 100000000000000LL;
const int MAX = 100001;
const double EPS = 1e-10;
int main() {
  int n;
  ll x;
  cin >> n >> x;
  ll a[2020];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll b[202][202] = {};
  ll ans = INF;
  for (int i = 0; i < n; i++) {
    b[i][0] = a[i];
    for (int j = 1; j < n; j++) {
      int idx = i - j;
      if (idx < 0) {
        idx = n + idx;
      }
      b[i][j] = min(b[i][j - 1], a[idx]);
    }
  }
  for (int i = 0; i < n; i++) {
    ll m = x * i;
    for (int j = 0; j < n; j++) {
      m += b[j][i];
    }
    ans = min(ans, m);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const ll INF = 100000000000000LL;
const int MAX = 100001;
const double EPS = 1e-10;
int main() {
  int n;
  ll x;
  cin >> n >> x;
  ll a[2020];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll b[2020][2020] = {};
  ll ans = INF;
  for (int i = 0; i < n; i++) {
    b[i][0] = a[i];
    for (int j = 1; j < n; j++) {
      int idx = i - j;
      if (idx < 0) {
        idx = n + idx;
      }
      b[i][j] = min(b[i][j - 1], a[idx]);
    }
  }
  for (int i = 0; i < n; i++) {
    ll m = x * i;
    for (int j = 0; j < n; j++) {
      m += b[j][i];
    }
    ans = min(ans, m);
  }
  cout << ans << endl;
}
Problem p04006 (C++)

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

#define pb push_back
#define mp make_pair

#define fi first
#define se second

typedef long long ll;

using namespace std;
int n, cost;
int a[2005], b[2005][2005];

int main() {

  cin >> n >> cost;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i + n] = a[i];
  }
  for (int i = 0; i < n + n; i++) {
    b[i][i] = a[i];
    for (int j = i + 1; j < n + n; j++) {
      b[i][j] = min(b[i][j - 1], a[j]);
    }
  }
  ll ans = 1e15;
  for (int m = 0; m < n; m++) {
    ll res = m * 1ll * cost;
    for (int i = 0; i < n; i++) {
      res += b[i][i + m];
    }
    ans = min(ans, res);
  }
  cout << ans;
  return 0;
}


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

#define pb push_back
#define mp make_pair

#define fi first
#define se second

typedef long long ll;

using namespace std;
int n, cost;
int a[5005], b[5005][5005];

int main() {

  cin >> n >> cost;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i + n] = a[i];
  }
  for (int i = 0; i < n + n; i++) {
    b[i][i] = a[i];
    for (int j = i + 1; j < n + n; j++) {
      b[i][j] = min(b[i][j - 1], a[j]);
    }
  }
  ll ans = 1e15;
  for (int m = 0; m < n; m++) {
    ll res = m * 1ll * cost;
    for (int i = 0; i < n; i++) {
      res += b[i][i + m];
    }
    ans = min(ans, res);
  }
  cout << ans;
  return 0;
}
Problem p04007 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

#define Rep(i, n) for (int i = 1; i <= n; i++)
#define Rep0(i, n) for (int i = 0; i <= n; i++)
#define RepG(i, x) for (int i = head[x]; i; i = edge[i].next)
#define v edge[i].to
#define mp(a, b) make_pair(a, b)

using namespace std;

const int N = 310;

char p[N];
int r[N][N], b[N][N];

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  Rep(i, h) {
    scanf("%s", p + 1);
    Rep(j, w) if (p[j] == '#') r[i][j] = b[i][j] = 1;
  }
  Rep(i, w) r[1][i] = b[h][i] = 1;
  Rep(i, h) Rep(j, w) {
    if (j & 1)
      r[i][j] = (i != h);
    else
      b[i][j] = (i != 1);
  }

  Rep(i, h) {
    Rep(j, w) printf("%c", r[i][j] ? '#' : '.');
    printf("\n");
  }
  printf("\n");
  Rep(i, h) {
    Rep(j, w) printf("%c", b[i][j] ? '#' : '.');
    printf("\n");
  }

  return 0;
}

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

#define Rep(i, n) for (int i = 1; i <= n; i++)
#define Rep0(i, n) for (int i = 0; i <= n; i++)
#define RepG(i, x) for (int i = head[x]; i; i = edge[i].next)
#define v edge[i].to
#define mp(a, b) make_pair(a, b)

using namespace std;

const int N = 510;

char p[N];
int r[N][N], b[N][N];

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  Rep(i, h) {
    scanf("%s", p + 1);
    Rep(j, w) if (p[j] == '#') r[i][j] = b[i][j] = 1;
  }
  Rep(i, w) r[1][i] = b[h][i] = 1;
  Rep(i, h) Rep(j, w) {
    if (j & 1)
      r[i][j] = (i != h);
    else
      b[i][j] = (i != 1);
  }

  Rep(i, h) {
    Rep(j, w) printf("%c", r[i][j] ? '#' : '.');
    printf("\n");
  }
  printf("\n");
  Rep(i, h) {
    Rep(j, w) printf("%c", b[i][j] ? '#' : '.');
    printf("\n");
  }

  return 0;
}