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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (in i = 0; i < (in)(n); i++)
const double PI = 3.14159265358979323846;
const int64_t waru = 1000000007;
const in INF = 1 << 30;
#define vecvec(a, y, x) vector<vector<int>> a(y, vector<int>(x))
#define vec(a, n)                                                              \
  vector<int> a(n);                                                            \
  rep(i, n) cin >> a[i]

int main() {
  int n;
  vec(b, n - 1);
  vector<int> a(n - 1);
  a[0] = b[0];
  for (int i = 1; i < n - 1; i++) {
    a[i] = min(b[i - 1], b[i]);
  }
  int ans = b[n - 2];
  rep(i, n - 1) ans += a[i];
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (in i = 0; i < (in)(n); i++)
const double PI = 3.14159265358979323846;
const int64_t waru = 1000000007;
const in INF = 1 << 30;
#define vecvec(a, y, x) vector<vector<int>> a(y, vector<int>(x))
#define vec(a, n)                                                              \
  vector<int> a(n);                                                            \
  rep(i, n) cin >> a[i]

int main() {
  int n;
  cin >> n;
  vec(b, n - 1);
  vector<int> a(n - 1);
  a[0] = b[0];
  for (int i = 1; i < n - 1; i++) {
    a[i] = min(b[i - 1], b[i]);
  }
  int ans = b[n - 2];
  rep(i, n - 1) ans += a[i];
  cout << ans;
}
Problem p02917 (C++)

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

int main() {
  int n;
  vector<int> b(n - 1);
  rep(i, n - 1) cin >> b[i];
  vector<int> a(n);
  a[0] = b[0];
  for (int i = 1; i <= n - 2; i++) {
    a[i] = min(b[i], b[i - 1]);
  }
  a[n - 1] = b[n - 2];
  int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> b(n - 1);
  rep(i, n - 1) cin >> b[i];
  vector<int> a(n);
  a[0] = b[0];
  for (int i = 1; i <= n - 2; i++) {
    a[i] = min(b[i], b[i - 1]);
  }
  a[n - 1] = b[n - 2];
  int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
  return 0;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define pp pair<int, int>
const int INF = 1001001001;
// int 2×10の9乗

// vector<vector<int>> a(n, vector<int>(m));
// rep(i,n){
// rep(j,m) cin >> a[i][j];
//}

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n - 1);
  for (int i = 1; i <= n - 1; i++)
    cin >> b[i];
  int ans = b[1];
  for (int i = 1; i <= n - 1; i++) {
    a[i + 1] = b[i];
    ans += a[i + 1];
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define pp pair<int, int>
const int INF = 1001001001;
// int 2×10の9乗

// vector<vector<int>> a(n, vector<int>(m));
// rep(i,n){
// rep(j,m) cin >> a[i][j];
//}

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n - 1);
  rep(i, n) cin >> b[i];
  a[0] = b[0];
  rep(i, n - 2) a[i + 1] = min(b[i], b[i + 1]);
  a[n - 1] = b[n - 2];
  int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
}
Problem p02917 (C++)

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

typedef long long ll;
const ll INF = 1e18;
const int BIG = 1e9 + 555;
const int maxN = 20 + 9;

int n;
int a[maxN], b[maxN];

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

  cin >> n;

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

  b[0] = b[n] = BIG;
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = min(b[i - 1], b[i]);
    sum += a[i];
  }

  cout << sum << '\n';

  return 0;
}

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

typedef long long ll;
const ll INF = 1e18;
const int BIG = 1e9 + 555;
const int maxN = 1e5 + 9;

int n;
int a[maxN], b[maxN];

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

  cin >> n;

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

  b[0] = b[n] = BIG;
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = min(b[i - 1], b[i]);
    sum += a[i];
  }

  cout << sum << '\n';

  return 0;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
typedef long long ll;
int n;
vector<int> b;
ll ans;
const int inf = 100000000;
int main(int argc, char **argv) {
  cin >> n;
  b.resize(n - 1);
  rep(i, n - 1) { cin >> b[i]; }
  b[n - 1] = inf;
  ans = b[0];
  rep(i, n - 1) { ans += min(b[i], b[i + 1]); }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
typedef long long ll;
int n;
vector<int> b;
ll ans;
const int inf = 100000000;
int main(int argc, char **argv) {
  cin >> n;
  b.resize(n);
  rep(i, n - 1) { cin >> b[i]; }
  b[n - 1] = inf;
  ans = b[0];
  rep(i, n - 1) { ans += min(b[i], b[i + 1]); }
  cout << ans << endl;
  return 0;
}
Problem p02917 (C++)

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

int main() {
  int n;
  vector<int> b(n - 1);
  rep(i, n - 1) cin >> b[i];
  vector<int> a(n);
  a[0] = b[0];
  for (int i = 1; i <= n - 2; i++) {
    a[i] = min(b[i], b[i - 1]);
  }
  a[n - 1] = b[n - 2];
  int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> b(n - 1);
  rep(i, n - 1) cin >> b[i];
  vector<int> a(n);
  a[0] = b[0];
  for (int i = 1; i <= n - 2; i++) {
    a[i] = min(b[i], b[i - 1]);
  }
  a[n - 1] = b[n - 2];
  int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
  return 0;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;
using namespace std;
int main() {
  int N;
  vector<ll> A(N + 1);
  vector<ll> B(N);
  for (int i = 1; i <= N - 1; i++)
    cin >> B[i];
  ll ans = 0;

  A[1] = B[1];
  A[N] = B[N - 1];

  for (int i = 2; i <= N - 1; i++) {
    A[i] = min(B[i - 1], B[i]);
  }

  for (int i = 1; i <= N; i++)
    ans += A[i];

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<ll> A(N + 1);
  vector<ll> B(N);
  for (int i = 1; i <= N - 1; i++)
    cin >> B[i];
  ll ans = 0;

  A[1] = B[1];
  A[N] = B[N - 1];

  for (int i = 2; i <= N - 1; i++) {
    A[i] = min(B[i - 1], B[i]);
  }

  for (int i = 1; i <= N; i++)
    ans += A[i];

  cout << ans << endl;
}
Problem p02917 (C++)

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N), B(N - 1);

  for (int i = 1; i <= N - 1; i++) {
    cin >> B[i];
  }

  int ans = B[1] + B[N - 1];

  for (int i = 0; i <= N - 2; i++) {
    ans += min(B[i], B[i + 1]);
  }

  cout << ans << endl;
}

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> B(N);

  for (int i = 1; i <= N - 1; i++) {
    cin >> B[i];
  }

  int ans = B[1] + B[N - 1];

  for (int i = 0; i <= N - 2; i++) {
    ans += min(B[i], B[i + 1]);
  }

  cout << ans << endl;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const long long mod = 1000000007;
typedef long long ll;

int main() {

  int N;
  cin >> N;
  vector<int> B(N - 1);
  rep(i, N - 1) cin >> B.at(i);
  int ans = B.at(0) + B.at(N - 1);
  for (int i = 1; i < N - 1; i++) {

    if (B.at(i - 1) >= B.at(i)) {
      ans += B.at(i);
    } else {
      ans += B.at(i - 1);
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const long long mod = 1000000007;
typedef long long ll;

int main() {

  int N;
  cin >> N;
  vector<int> B(N - 1);
  rep(i, N - 1) cin >> B.at(i);
  int ans = B.at(0) + B.at(N - 2);
  for (int i = 1; i < N - 1; i++) {

    if (B.at(i - 1) >= B.at(i)) {
      ans += B.at(i);
    } else {
      ans += B.at(i - 1);
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02917 (C++)

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

int main() {
  int N;

  cin >> N;

  vector<int> B(N - 1);

  for (int i = 0; i < N - 1; i++) {
    cin >> B.at(i);
  }
  int A = B.at(0) + B.at(N - 1);
  for (int i = 0; i < N - 2; i++) {
    if (B.at(i) <= B.at(i + 1)) {
      A = A + B.at(i);
    } else if (B.at(i) > B.at(i + 1)) {
      A = A + B.at(i + 1);
    }
  }

  cout << A << endl;
}


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

int main() {
  int N;

  cin >> N;

  vector<int> B(N - 1);

  for (int i = 0; i < N - 1; i++) {
    cin >> B.at(i);
  }
  int A = B.at(0) + B.at(N - 2);
  for (int i = 0; i < N - 2; i++) {
    if (B.at(i) <= B.at(i + 1)) {
      A = A + B.at(i);
    } else if (B.at(i) > B.at(i + 1)) {
      A = A + B.at(i + 1);
    }
  }

  cout << A << endl;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
#define ff(ii, nn, mm) for (int ii = nn; ii < mm; ii++)
#define i64 int64_t
#define tos(x) to_string(x)

int main() {
  int n;
  vector<int> data(n + 1);
  ff(i, 1, n) { cin >> data.at(i); }
  data.at(0) = data.at(1);
  data.at(n) = data.at(n - 1);
  int ans = 0;
  ff(i, 0, n) { ans += min(data.at(i), data.at(i + 1)); }
  cout << ans << endl;
  return 0;
}

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

using namespace std;
#define ff(ii, nn, mm) for (int ii = nn; ii < mm; ii++)
#define i64 int64_t
#define tos(x) to_string(x)

int main() {
  int n;
  cin >> n;
  vector<int> data(n + 1);
  ff(i, 1, n) { cin >> data.at(i); }
  data.at(0) = data.at(1);
  data.at(n) = data.at(n - 1);
  int ans = 0;
  ff(i, 0, n) { ans += min(data.at(i), data.at(i + 1)); }
  cout << ans << endl;
  return 0;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> B(N - 1, 0);
  for (int i = 0; i < N - 1; i++) {
    int Bi;
    cin >> Bi;
    B[i] = Bi;
  }
  B[N - 1] = B[N - 2];

  vector<int> A(N, 0);
  A[0] = B[0];
  int prev = B[1];
  for (int i = 1; i < N; i++) {
    A[i] = min(B[i - 1], B[i]);
  }

  int total = 0;
  for (int i = 0; i < N; i++) {
    total += A[i];
  }

  cout << total;

  return 0;
}


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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> B(N, 0);
  for (int i = 0; i < N - 1; i++) {
    int Bi;
    cin >> Bi;
    B[i] = Bi;
  }
  B[N - 1] = B[N - 2];

  vector<int> A(N, 0);
  A[0] = B[0];
  int prev = B[1];
  for (int i = 1; i < N; i++) {
    A[i] = min(B[i - 1], B[i]);
  }

  int total = 0;
  for (int i = 0; i < N; i++) {
    total += A[i];
  }

  cout << total;

  return 0;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define optimize_cin()                                                         \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
#define debug(x) std::cerr << #x << ": " << (x) << endl;
using namespace std;
using lint = long long;
template <class T> using grid = vector<vector<T>>;
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;
}
static const lint MOD = (lint)1e9 + 7;

lint Sum(vector<lint> nums) {
  lint sum = 0;
  rep(i, SZ(nums)) { sum += nums[i]; }
  return sum;
}

int main() {
  int N;
  cin >> N;
  vector<lint> B(N - 1);
  rep(i, N - 1) { cin >> B[i]; }

  vector<lint> A(N);
  A[0] = B[0];
  for (int i = N - 1; i >= 0; --i) {
    A[i - 1] = min(B[i - 1], B[i]);
  }
  A[N - 1] = B[N - 2];

  rep(i, N) { cerr << A[i] << " "; }
  cerr << endl;

  lint sum = Sum(A);
  cout << sum << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define optimize_cin()                                                         \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
#define debug(x) std::cerr << #x << ": " << (x) << endl;
using namespace std;
using lint = long long;
template <class T> using grid = vector<vector<T>>;
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;
}
static const lint MOD = (lint)1e9 + 7;

lint Sum(vector<lint> nums) {
  lint sum = 0;
  rep(i, SZ(nums)) { sum += nums[i]; }
  return sum;
}

int main() {
  int N;
  cin >> N;
  vector<lint> B(N - 1);
  rep(i, N - 1) { cin >> B[i]; }

  vector<lint> A(N);
  A[0] = B[0];
  for (int i = N - 1; i > 0; --i) {
    A[i] = min(B[i - 1], B[i]);
  }
  A[N - 1] = B[N - 2];

  rep(i, N) { cerr << A[i] << " "; }
  cerr << endl;

  lint sum = Sum(A);
  cout << sum << endl;

  return 0;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
const ll MOD = 1000000007;

int main() {
  ll n;
  cin >> n;
  vll a(n), b(n + 1);
  rep(i, n - 1) cin >> b[i];
  a[0] = b[0];
  b[n - 1] = b[n - 2];
  rep(i, n) a[i + 1] = min(b[i], b[i + 1]);
  ll ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
const ll MOD = 1000000007;

int main() {
  ll n;
  cin >> n;
  vll a(n + 10, 0), b(n + 10, 0);
  rep(i, n - 1) cin >> b[i];
  a[0] = b[0];
  b[n - 1] = b[n - 2];
  rep(i, n) a[i + 1] = min(b[i], b[i + 1]);
  ll ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
}
Problem p02917 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
B = list(map(int, input().split()))
ans = 0
ans += B[0]
for i in range(n - 2):
    if B[i] > B[i + 1]:
        ans += B[i + i]
    else:
        ans += B[i]
ans += B[-1]
print(ans)


Fixed submission:
n = int(input())
B = list(map(int, input().split()))
ans = 0
ans += B[0]
for i in range(n - 2):
    if B[i] > B[i + 1]:
        ans += B[i + 1]
    else:
        ans += B[i]
ans += B[-1]
print(ans)
Problem p02917 (C++)

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

using namespace std;
int b[1000010];

long long ans = 0;

int main() {
  int n;
  scanf("%d", n);
  for (int i = 1; i < n; i++)
    scanf("%d", &b[i]);
  b[0] = b[n] = 0x7f7f7f7f;
  for (int i = 1; i <= n; i++)
    ans += min(b[i - 1], b[i]);
  cout << ans << endl;
  return 0;
}


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

using namespace std;
int b[1000010];

long long ans = 0;

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++)
    scanf("%d", &b[i]);
  b[0] = b[n] = 0x7f7f7f7f;
  for (int i = 1; i <= n; i++)
    ans += min(b[i - 1], b[i]);
  cout << ans << endl;
  return 0;
}
Problem p02917 (C++)

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

using namespace std;

int main(void) {
  int numbers_count;
  vector<int> limit_values;

  cin >> numbers_count;

  for (int i = 0; i < numbers_count - 1; i++) {
    int lv;
    cin >> lv;
    limit_values.push_back(lv);
  }

  int sum = limit_values[0];
  for (int i = 1; i < numbers_count - 1; i--) {
    sum += min(limit_values[i - 1], limit_values[i]);
  }
  sum += limit_values.back();

  cout << sum << endl;

  return 0;
}

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

using namespace std;

int main(void) {
  int numbers_count;
  vector<int> limit_values;

  cin >> numbers_count;

  for (int i = 0; i < numbers_count - 1; i++) {
    int lv;
    cin >> lv;
    limit_values.push_back(lv);
  }

  int sum = limit_values[0];
  for (int i = 1; i < numbers_count - 1; i++) {
    sum += min(limit_values[i - 1], limit_values[i]);
  }
  sum += limit_values.back();

  cout << sum << endl;

  return 0;
}
Problem p02917 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
B = [int(i) for i in input().split()][::-1]
#  N = 6
#  B = [0, 153, 10, 10, 23]
#  N = 3
#  B = [2, 5]
ans = 0
ans += B[1]  # A[1]
for i in range(0, N - 1):
    ans += min(B[i - 1], B[i])
print(ans)


Fixed submission:
N = int(input())
B = [int(i) for i in input().split()][::-1]
ret = B[0] + B[N - 2]
for i in range(1, N - 1):
    ret += min(B[i - 1], B[i])
print(ret)
Problem p02917 (C++)

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

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

  vector<int> a(n), b(n - 1);
  for (int i = 0; i < n - 1; i++) {
    cin >> b[i];
  }

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

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

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

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

  vector<int> a(n), b(n - 1);
  for (int i = 0; i < n - 1; i++) {
    cin >> b[i];
  }

  a[0] = b[0];
  a[n - 1] = b[n - 2];
  for (int i = 0; i < n - 2; i++) {
    a[i + 1] = min(b[i], b[i + 1]);
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
  }
  cout << sum << endl;
  return 0;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

int main() {
  int i, N, B[20];
  scanf("%d", &N);
  for (i = 0; i <= N - 2; i++) {
    scanf("%d", &B[i]);
  }
  if (N == 2) {
    printf("%d", B[0] * 2);
    return 0;
  }
  int sum = B[0];
  for (i = 0; i <= N - 3; i++) {
    if (B[i] >= B[i + 1]) {
      sum = sum + B[i + 1];
    } else {
      sum = sum + B[i];
    }
  }
  sum = sum + B[N - 2];
  printf("%d", sum);
}

Fixed submission:
#include <iostream>

int main() {
  int i, N, B[100];
  scanf("%d", &N);
  for (i = 0; i <= N - 2; i++) {
    scanf("%d", &B[i]);
  }
  if (N == 2) {
    printf("%d", B[0] * 2);
    return 0;
  }
  int sum = B[0];
  for (i = 0; i <= N - 3; i++) {
    if (B[i] >= B[i + 1]) {
      sum = sum + B[i + 1];
    } else {
      sum = sum + B[i];
    }
  }
  sum = sum + B[N - 2];
  printf("%d", sum);
}
Problem p02917 (C++)

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

using namespace std;

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

  vector<long> B(N - 1);
  long sum = 0;
  bool flag = true;

  for (int i = 0; i < N - 1; i++) {
    cin >> B.at(i);
  }

  for (int i = 0; i < N - 1; i++) {
    if (i == 0) {
      sum += B.at(i);
      if (B.at(i) > B.at(i + 1)) {
        sum += B.at(i + 1);
      } else {
        sum += B.at(i);
      }
    } else if (i == N - 2)
      sum += B.at(i);
    else {
      if (B.at(i) > B.at(i + 1)) {
        sum += B.at(i + 1);
        flag = true;
      } else if (B.at(i - 1) == B.at(i)) {
        sum += B.at(i);
        flag = false;
      } else {
        // else if(B.at(i) < B.at(i+1)){
        sum += B.at(i);
        flag = true;
      }
    }
  }

  cout << sum;

  return 0;
}

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

using namespace std;

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

  vector<long> B(N - 1);
  long sum = 0;
  bool flag = true;

  for (int i = 0; i < N - 1; i++) {
    cin >> B.at(i);
  }

  for (int i = 0; i < N - 1; i++) {
    if (i == 0) {
      sum += B.at(i);
      if (N > 2) {
        if (B.at(i) > B.at(i + 1)) {
          sum += B.at(i + 1);
        } else {
          sum += B.at(i);
        }
      } else {
        sum += B.at(i);
      }
    } else if (i == N - 2)
      sum += B.at(i);
    else {
      if (B.at(i) > B.at(i + 1)) {
        sum += B.at(i + 1);
        flag = true;
      } else if (B.at(i - 1) == B.at(i)) {
        sum += B.at(i);
        flag = false;
      } else {
        // else if(B.at(i) < B.at(i+1)){
        sum += B.at(i);
        flag = true;
      }
    }
  }

  cout << sum;

  return 0;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// ----------------------R00TDADA------------------------------
#define rep(i, a, b) for (__typeof((b)) i = (a); i < (b); i++)
#define nrep(i, a, b) for (__typeof((b)) i = (b); i > (a); i--)
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define ppi pair<int, int>
#define pppi pair<ppi, int>
#define vi vector<int>
#define vii vector<ppi>
#define viii vector<pppi>
#define vs vector<string>
#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define M_PI 3.14159265358979323846
#define rootdada ios_base::sync_with_stdio(false)
#define int long long

signed main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  rootdada;
  int n;
  cin >> n;
  int a[n - 1];
  rep(i, 0, n - 1) cin >> a[i];
  int b[n];
  b[0] = a[0];
  rep(i, 0, n - 1) {
    if (a[i] == max(a[i], b[i]))
      b[i + 1] = a[i];
    else {
      b[i + 1] = a[i];
      b[i] = a[i];
    }
  }
  int sum = 0;
  rep(i, 0, n) sum += b[i];
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// ----------------------R00TDADA------------------------------
#define rep(i, a, b) for (__typeof((b)) i = (a); i < (b); i++)
#define nrep(i, a, b) for (__typeof((b)) i = (b); i > (a); i--)
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define ppi pair<int, int>
#define pppi pair<ppi, int>
#define vi vector<int>
#define vii vector<ppi>
#define viii vector<pppi>
#define vs vector<string>
#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define M_PI 3.14159265358979323846
#define rootdada ios_base::sync_with_stdio(false)
#define int long long

signed main() {

  rootdada;
  int n;
  cin >> n;
  int a[n - 1];
  rep(i, 0, n - 1) cin >> a[i];
  int b[n];
  b[0] = a[0];
  rep(i, 0, n - 1) {
    if (a[i] == max(a[i], b[i]))
      b[i + 1] = a[i];
    else {
      b[i + 1] = a[i];
      b[i] = a[i];
    }
  }
  int sum = 0;
  rep(i, 0, n) sum += b[i];
  cout << sum << endl;
}
Problem p02917 (C++)

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

int main() {
  int n;
  vector<int> b(n - 1);
  rep(i, n - 1) cin >> b[i];
  vector<int> a(n);
  rep(i, n) {
    if (i == 0)
      a[i] = b[i];
    else if (i == n - 1)
      a[i] = b[i - 1];
    else {
      if (b[i] - b[i - 1] >= 0)
        a[i] = b[i - 1];
      else
        a[i] = b[i];
    }
  }
  int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> b(n - 1);
  rep(i, n - 1) cin >> b[i];
  vector<int> a(n);
  rep(i, n) {
    if (i == 0)
      a[i] = b[i];
    else if (i == n - 1)
      a[i] = b[i - 1];
    else {
      if (b[i] - b[i - 1] >= 0)
        a[i] = b[i - 1];
      else
        a[i] = b[i];
    }
  }
  int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
  return 0;
}
Problem p02917 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> b(n);
  for (int i = 0; i < n - 1; i++)
    cin >> b[i];
  b[n] = 0;
  vector<int> a(n);
  a[0] = b[0];
  for (int i = 1; i < n - 1; i++) {
    if (b[i] >= b[i - 1])
      a[i] = b[i - 1];
    else
      a[i] = b[i];
  }
  a[n - 1] = b[n - 2];
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
  }
  cout << sum << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> b(n);
  for (int i = 0; i < n - 1; i++)
    cin >> b[i];
  b[n - 1] = 0;
  vector<int> a(n);
  a[0] = b[0];
  for (int i = 1; i < n - 1; i++) {
    if (b[i] >= b[i - 1])
      a[i] = b[i - 1];
    else
      a[i] = b[i];
  }
  a[n - 1] = b[n - 2];
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
  }
  cout << sum << endl;
}
Problem p02917 (C++)

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

int main() {
  int n, sum;
  sum = 0;
  vector<int> b(n - 1);
  vector<int> a(n);
  for (int i = 0; i < n - 1; i++) {
    cin >> b.at(i);
  }
  a.at(0) = b.at(0);
  a.at(n - 1) = b.at(n - 2);
  for (int i = 1; i < n - 1; i++) {
    a.at(i) = min(b.at(i - 1), b.at(i));
  }
  for (int i = 0; i < n; i++) {
    sum += a.at(i);
  }
  cout << sum << endl;
}

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

int main() {
  int n, sum;
  sum = 0;
  cin >> n;
  vector<int> b(n - 1);
  vector<int> a(n);
  for (int i = 0; i < n - 1; i++) {
    cin >> b.at(i);
  }
  a.at(0) = b.at(0);
  a.at(n - 1) = b.at(n - 2);
  for (int i = 1; i < n - 1; i++) {
    a.at(i) = min(b.at(i - 1), b.at(i));
  }
  for (int i = 0; i < n; i++) {
    sum += a.at(i);
  }
  cout << sum << endl;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
#define inf INT_MAX
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(a, n)                                                               \
  rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
#define vi vector<int>

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vi b(n - 1);
  rep(i, n - 1) { cin >> b[i]; }

  vi a(n - 1, -inf);
  a[0] = b[0];
  a[1] = b[0];

  FOR(i, 1, n - 1) {
    if (a[i] > b[i])
      a[i] = b[i];
    a[i + 1] = b[i];
  }

  int ans = 0;
  rep(i, n) { ans += a[i]; }

  cout << ans << endl;
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define inf INT_MAX
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(a, n)                                                               \
  rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
#define vi vector<int>

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vi b(n - 1);
  rep(i, n - 1) { cin >> b[i]; }

  vi a(n, -inf);
  a[0] = b[0];
  a[1] = b[0];

  FOR(i, 1, n - 1) {
    if (a[i] > b[i])
      a[i] = b[i];
    a[i + 1] = b[i];
  }

  int ans = 0;
  rep(i, n) { ans += a[i]; }

  cout << ans << endl;
}
Problem p02917 (C++)

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

using namespace std;

int main() {
  std::ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  n--;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int sum = 0;
  for (int i = n - 1; i >= 0; i--) {
    // cout<<a[i]<<endl;
    if (i + 1 < n) {
      if (a[i] > a[i + 1]) {
        sum += a[i + 1];
        continue;
      }
    }
    sum += a[i];
  }

  cout << sum + a[0];
  return 0;
}


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

using namespace std;

int main() {
  std::ios::sync_with_stdio(false);

  int n;
  cin >> n;
  n--;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int sum = 0;
  for (int i = n - 1; i >= 0; i--) {
    // cout<<a[i]<<endl;
    if (i + 1 < n) {
      if (a[i] > a[i + 1]) {
        sum += a[i + 1];
        continue;
      }
    }
    sum += a[i];
  }

  cout << sum + a[0];
  return 0;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  vector<int> b(n - 1);
  cin >> n;
  for (int i = 0; i < n - 1; ++i)
    cin >> b[i];
  vector<int> a(n);
  a[0] = b[0];
  a[n - 1] = b[n - 2];
  for (int i = 1; i < n - 1; ++i) {
    a[i] = min(b[i - 1], b[i]);
  }
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    sum += a[i];
  }
  cout << sum << endl;
  for (int i = 0; i < n; ++i)
    cout << a[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  vector<int> b(n - 1);
  cin >> n;
  for (int i = 0; i < n - 1; ++i)
    cin >> b[i];
  vector<int> a(n);
  a[0] = b[0];
  a[n - 1] = b[n - 2];
  for (int i = 1; i < n - 1; ++i) {
    a[i] = min(b[i - 1], b[i]);
  }
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    sum += a[i];
  }
  cout << sum << endl;
}
Problem p02917 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> b(n - 1);
  vector<int> a(n);
  for (int i = 0; i < n - 1; i++)
    cin >> b[i];

  for (int i = n - 1; i >= 0; i++) {
    if (i == 0) {
      a[i] = b[i];
    } else if (i == n - 1) {
      a[i] = b[i - 1];
    } else {
      a[i] = min(b[i], b[i - 1]);
    }
  }

  int sum = 0;
  for (int it : a)
    sum += it;
  cout << sum << endl;

  return 0;
}


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

int main() {
  int n;
  cin >> n;
  vector<int> b(n - 1);
  vector<int> a(n);
  for (int i = 0; i < n - 1; i++)
    cin >> b[i];

  for (int i = n - 1; i >= 0; i--) {
    if (i == 0) {
      a[i] = b[i];
    } else if (i == n - 1) {
      a[i] = b[i - 1];
    } else {
      a[i] = min(b[i], b[i - 1]);
    }
  }

  int sum = 0;
  for (int it : a)
    sum += it;
  cout << sum << endl;

  return 0;
}
Problem p02917 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n - 1);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  a[0] = b[0];
  a[n] = b[n - 1];
  int count = a[0] + a[n];
  for (int j = 1; j < n; j++) {
    a[j] = min(b[j - 1], b[j]);
    count += a[j];
  }
  cout << count << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n - 1);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  a[0] = b[0];
  a[n - 1] = b[n - 2];
  int count = a[n - 1] + a[0];
  for (int j = 1; j < n - 1; j++) {
    a[j] = min(b[j - 1], b[j]);
    count += a[j];
  }
  cout << count << endl;
  return 0;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const double PI = 3.14159265358979;
const ll INF = pow(10, 18);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {
  ll n;
  vl b(n - 1);
  rep(i, n - 1) { cin >> b[i]; }
  ll ans = b[0] + b[n - 2];
  rep(i, n - 2) { ans += min(b[i], b[i + 1]); }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const double PI = 3.14159265358979;
const ll INF = pow(10, 18);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {
  ll n;
  cin >> n;
  vl b(n - 1);
  rep(i, n - 1) { cin >> b[i]; }
  ll ans = b[0] + b[n - 2];
  rep(i, n - 2) { ans += min(b[i], b[i + 1]); }
  cout << ans << endl;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, b[n - 1], a[n];
  int c = 0;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> b[i];
  }
  a[0] = b[0];
  a[n - 1] = b[n - 2];
  for (int i = 0; i < n - 2; i++) {
    a[i + 1] = min(b[i], b[i + 1]);
  }
  for (int i = 0; i < n; i++) {
    c += a[i];
  }
  cout << c;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long int n, b[n - 1], a[n];
  long int c = 0;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> b[i];
  }
  a[0] = b[0];
  a[n - 1] = b[n - 2];
  for (int i = 0; i < n - 2; i++) {
    a[i + 1] = min(b[i], b[i + 1]);
  }
  for (int i = 0; i < n; i++) {
    c += a[i];
  }
  cout << c;
  return 0;
}
Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repm(i, m, n) for (int i = (m); i < (int)(n); i++)
#define repr(i, n) for (int i = (n); i > 0; i--)
#define all(vec) vec.begin(), vec.end()
using namespace std;

int main() {
  int a;
  // string a;
  cin >> a;
  vector<int> seq(a - 1);
  rep(i, a - 1) cin >> seq[i];

  int ans = 0;
  // string ans = "Yes";
  // string ans = "No";
  // string ans = "YES";
  // string ans = "NO";
  int cnt = 0;
  ans = seq[0];
  rep(i, a - 2) a += min(seq[i + 1], seq[i]);
  ans += seq[a - 2];

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repm(i, m, n) for (int i = (m); i < (int)(n); i++)
#define repr(i, n) for (int i = (n); i > 0; i--)
#define all(vec) vec.begin(), vec.end()
using namespace std;

int main() {
  int a;
  // string a;
  cin >> a;
  vector<int> seq(a - 1);
  rep(i, a - 1) cin >> seq[i];

  int ans = 0;
  // string ans = "Yes";
  // string ans = "No";
  // string ans = "YES";
  // string ans = "NO";
  int cnt = 0;
  vector<int> w(a);
  w[0] = seq[0];
  repm(i, 1, a - 1) w[i] = min(seq[i], seq[i - 1]);
  w[a - 1] = seq[a - 2];
  rep(i, a) ans += w[i];

  cout << ans << endl;
}
Problem p02917 (C++)

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

using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> B(N);
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> B[i];

  for (int i = 0; i < N; i++) {
    A[i] = B[i];
    if (B[i] < B[i + 1])
      A[i + 1] = B[i];
    else
      A[i + 1] = B[i + 1];
  }
  int ans = 0;
  for (int i = 0; i < N; i++)
    ans += A[i];
  cout << ans;
  return 0;
}

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

using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> B(N);
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> B[i];
  A[0] = B[0];
  A[N - 1] = B[N - 2];
  for (int i = 1; i < N - 1; i++) {
    A[i] = min(B[i], B[i - 1]);
  }
  int ans = 0;
  for (int i = 0; i < N; i++)
    ans += A[i];
  cout << ans;
  return 0;
}
Problem p02918 (C++)

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

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

  string S;
  cin >> S;
  vector<pair<char, int>> T;
  S += "X";
  char prev = S[0];
  int num = 1;
  for (int i = 1; i < S.size(); ++i) {
    if (prev == S[i]) {
      num++;
    } else {
      T.emplace_back(prev, num);
      num = 1;
      prev = S[i];
    }
  }

  if (T.size() % 2 == 0)
    T.emplace_back('X', 0);
  vector<pair<int, int>> p;
  for (int i = 1; i < T.size() - 1; i += 2) {
    int num = T[i - 1].second + T[i].second + T[i + 1].second;
    p.emplace_back(num, i);
  }

  // for (auto tmp : p) {
  //     cout << tmp.first << tmp.second << " ";
  // }
  // cout << endl;

  // sort(p.rbegin(), p.rend());
  char tmp = T[0].first;
  rep(i, min(p[i].second, K)) { T[p[i].second].first = tmp; }

  string St = "";
  rep(i, T.size()) { St += string(T[i].second, T[i].first); }

  int ans = 0;
  rep(i, St.size() - 1) {
    if (St[i] == St[i + 1])
      ans++;
  }

  // cout << St << endl;

  cout << ans << endl;
}


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

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

  string S;
  cin >> S;
  vector<pair<char, int>> T;
  S += "X";
  char prev = S[0];
  int num = 1;
  for (int i = 1; i < S.size(); ++i) {
    if (prev == S[i]) {
      num++;
    } else {
      T.emplace_back(prev, num);
      num = 1;
      prev = S[i];
    }
  }

  if (T.size() % 2 == 0)
    T.emplace_back('X', 0);
  vector<pair<int, int>> p;
  for (int i = 1; i < T.size() - 1; i += 2) {
    int num = T[i - 1].second + T[i].second + T[i + 1].second;
    p.emplace_back(num, i);
  }

  // for (auto tmp : p) {
  //     cout << tmp.first << tmp.second << " ";
  // }
  // cout << endl;

  // sort(p.rbegin(), p.rend());
  char tmp = T[0].first;
  int sz = min((int)p.size(), K);
  rep(i, sz) { T[p[i].second].first = tmp; }

  string St = "";
  rep(i, T.size()) { St += string(T[i].second, T[i].first); }

  int ans = 0;
  rep(i, St.size() - 1) {
    if (St[i] == St[i + 1])
      ans++;
  }

  // cout << St << endl;

  cout << ans << endl;
}
Problem p02918 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <unordered_map>
using namespace std;

unordered_map<string, int> mp;

int getHappyValue(string &s) {
  int size = s.size();
  int happy = 0;
  for (int i = 0; i < size - 1; i++) {
    if (s[i] == s[i + 1]) {
      happy++;
    }
  }
  return happy;
}

void getRotateString(string &s, int l, int r) {
  int size = s.size();
  for (int i = 0; i < size; i++) {
    if (i < l) {
    } else if (i > r) {
    } else {
      if (i <= r - i + l) {
        char a = s[i];
        s[i] = (s[r - i + l] == 'L') ? 'R' : 'L';
        s[r - i + l] = (a == 'L') ? 'R' : 'L';
      }
    }
  }
}

int main(void) {
  int N;
  int K;
  string S;

  cin >> N >> K;
  cin >> S;

  int size = S.size();
  int l = -1;
  int r = -1;

  string tmp = S;
  int k = 0;

  for (int i = 0; i < size; i++) {
    if (tmp[i] != tmp[i + 1] && l == -1) {
      l = i + 1;
    } else if (tmp[i] != tmp[i + 1] && l != -1) {
      r = i;
    }
    if (l != -1 && r != -1) {
      getRotateString(tmp, l, r);
      // cout << l << "," << r << ":" << tmp << endl;

      k++;
      i = l;
      l = -1;
      r = -1;
      if (k >= K)
        break;
    }
  }

  cout << getHappyValue(tmp);
  return 0;
}

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

unordered_map<string, int> mp;

int getHappyValue(string &s) {
  int size = s.size();
  int happy = 0;
  for (int i = 0; i < size - 1; i++) {
    if (s[i] == s[i + 1]) {
      happy++;
    }
  }
  return happy;
}

void getRotateString(string &s, int l, int r) {
  int size = s.size();
  for (int i = l; i <= r; i++) {
    if (i <= r - i + l) {
      char a = s[i];
      s[i] = (s[r - i + l] == 'L') ? 'R' : 'L';
      s[r - i + l] = (a == 'L') ? 'R' : 'L';
    }
  }
}

int main(void) {
  int N;
  int K;
  string S;

  cin >> N >> K;
  cin >> S;

  int size = S.size();
  int l = -1;
  int r = -1;

  string tmp = S;
  int k = 0;

  for (int i = 0; i < size; i++) {
    if (tmp[i] != tmp[i + 1] && l == -1) {
      l = i + 1;
    } else if (tmp[i] != tmp[i + 1] && l != -1) {
      r = i;
    }
    if (l != -1 && r != -1) {
      getRotateString(tmp, l, r);
      // cout << l << "," << r << ":" << tmp << endl;

      k++;
      i = l;
      l = -1;
      r = -1;
      if (k >= K)
        break;
    }
  }

  cout << getHappyValue(tmp);
  return 0;
}
Problem p02918 (C++)

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

const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> vec;
  int sum = 1;
  if (n == 1)
    cout << 0 << endl;
  if (n == 2)
    cout << 1 << endl;
  else {
    rep(i, n - 1) {
      if (s[i] == s[i + 1])
        sum++;
      else {
        vec.push_back(sum);
        sum = 1;
      }
      if (i == n - 2 && sum > 1)
        vec.push_back(sum);
    }
    int N = vec.size();
    if (N == 1)
      cout << N - 1 << endl;
    else {
      ll ans = vec[0] + vec[N - 1];
      rep(i, k) {
        if (i + 1 < N - 2 - i)
          ans += vec[i + 1] + vec[N - 2 - i];
        else if (i + 1 == N - 2 - i)
          ans += vec[i + 1];
        else
          break;
      }
      if (N - 2 * k > 0)
        cout << ans - 2 << endl;
      else
        cout << ans - 1 << endl;
    }
  }
}


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

const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  int sum = 0;
  rep(i, n - 1) if (s[i] == s[i + 1]) sum++;
  cout << min(sum + 2 * k, n - 1) << endl;
}
Problem p02918 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define int long long
using namespace std;
const int INF = 1e10;
const int ZERO = 0;

signed main() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  int L = 0;
  int R = 0;
  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'L') {
      L++;
    } else {
      R++;
    }
  }
  int A = 0;
  int C = 0;
  char s = S.at(0);
  bool B = false;
  for (int i = 1; i < N; i++) {
    if (S.at(i) != s) {
      C = i + 1;
      if (i == N - 1) {
        A = N;
      }
      if (B == true) {
        continue;
      }
      A++;
      B = true;
      if (A == K + 1) {
        A = i;
        break;
      }
    } else {
      B = false;
    }
  }
  if (A <= K) {
    A = C;
  }
  for (int i = 0; i < A; i++) {
    S.at(i) = s;
  }
  int ret = 0;
  for (int i = 0; i < N - 1; i++) {
    if (S.at(i) == S.at(i + 1)) {
      ret++;
    }
  }
  cout << ret << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define int long long
using namespace std;
const int INF = 1e10;
const int ZERO = 0;

signed main() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  int L = 0;
  int R = 0;
  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'L') {
      L++;
    } else {
      R++;
    }
  }
  int A = 0;
  int C = 0;
  char s = S.at(0);
  bool B = false;
  for (int i = 1; i < N; i++) {
    if (S.at(i) != s) {
      C = i + 1;
      if (i == N - 1) {
        if (A == K) {
          C = N;
        } else {
          A == N;
        }
      }
      if (B == true) {
        continue;
      }
      A++;
      B = true;
      if (A == K + 1) {
        A = i;
        break;
      }
    } else {
      B = false;
    }
  }
  if (A <= K) {
    A = C;
  }
  for (int i = 0; i < A; i++) {
    S.at(i) = s;
  }
  int ret = 0;
  for (int i = 0; i < N - 1; i++) {
    if (S.at(i) == S.at(i + 1)) {
      ret++;
    }
  }
  cout << ret << endl;
}
Problem p02918 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
string str;
int N, K;
char nt(char a) {
  if (a == 'R') {
    return 'L';
  }
  if (a == 'L') {
    return 'R';
  }
}
int findInd(int a) {
  int i = a;
  char c = str[a];
  while (str[i] == c) {
    i++;
  }
  return i;
}

int rep(int l, int r) {
  string a(str);

  for (int i = 0; i <= r - l; i++) {
    a[i + l] = nt(str[r - i]);
  }

  str = a;
  return 0;
}
int countS() {
  int count = 0;
  for (int i = 0; i < N; i++) {
    if (str[i + 1] == str[i]) {
      count++;
    }
  }
  return count;
}

int main() {
  cin >> N >> K;
  cin >> str;
  int i = 0;
  while (findInd(0) != N && i < K) {
    rep(findInd(0), findInd(findInd(0)) - 1);
    i++;
  }
  cout << countS();
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
string str;
int N, K;
char nt(char a) {
  if (a == 'R') {
    return 'L';
  }
  if (a == 'L') {
    return 'R';
  }
}
int findInd(int a) {
  int i = a;
  char c = str[a];
  while (str[i] == c) {
    i++;
  }
  return i;
}

int rep(int l, int r) {
  string a(str);

  for (int i = 0; i <= r - l; i++) {
    a[i + l] = nt(str[r - i]);
  }

  str = a;
  return 0;
}
int countS() {
  int count = 0;
  for (int i = 0; i < N; i++) {
    if (str[i + 1] == str[i]) {
      count++;
    }
  }
  return count;
}

int main() {
  cin >> N >> K;
  cin >> str;
  int i = 0;
  int p = 0;
  while (findInd(p) != N && i < K) {
    p = findInd(p);
    rep(p, findInd(p) - 1);
    i++;
  }
  cout << countS();
  return 0;
}