Problem p02891 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <bitset>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define REP(i, a, b) for (int i = int(a); i <= int(b); i++)
#define MD 1000000007
#define MOD2 1000000009
#define INF 2000000000
#define DESPACITO 1000000000000000
#define E 998244353
#define pb push_back
#define mp make_pair
#define double long double
#define pdd pair<double, double>
#define ll long long

using namespace std;
// using namespace __gnu_pbds;

// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> pbds;

int main() {
  IOS;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  string s;
  ll k;
  cin >> s;
  cin >> k;
  int n = s.length();
  int l = 0;
  REP(i, 0, s.length() - 1) {
    if (s[i] == s[0])
      l++;
    else
      break;
  }
  int r = 0;
  for (int i = s.length() - 1; i >= 0; i--) {
    if (s[i] == s[s.length() - 1])
      r++;
    else
      break;
  }
  if (l - 1 >= n - r) {
    cout << (1LL * k * (s.length())) / 2;
    return 0;
  }
  ll x = 1;
  ll ans = 0;
  ll ct = 0;
  REP(i, 1, s.length() - 1) {
    if (s[i] == s[i - 1])
      x++;
    else {
      ct += (x / 2);
      x = 1;
    }
  }
  ct += (x / 2);
  if (s[s.length() - 1] == s[0]) {
    ct -= (l / 2);
    ct -= (r / 2);
    ll val = (l + r);
    ll an = val / 2;
    an *= (k - 1);
    ans += an;
    ans += (ct * k);
    ans += (l / 2);
    ans += (r / 2);
  } else
    ans = (ct * k);
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#include <bitset>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define REP(i, a, b) for (int i = int(a); i <= int(b); i++)
#define MD 1000000007
#define MOD2 1000000009
#define INF 2000000000
#define DESPACITO 1000000000000000
#define E 998244353
#define pb push_back
#define mp make_pair
#define double long double
#define pdd pair<double, double>
#define ll long long

using namespace std;
// using namespace __gnu_pbds;

// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> pbds;

int main() {
  IOS;
  string s;
  ll k;
  cin >> s;
  cin >> k;
  int n = s.length();
  int l = 0;
  REP(i, 0, s.length() - 1) {
    if (s[i] == s[0])
      l++;
    else
      break;
  }
  int r = 0;
  for (int i = s.length() - 1; i >= 0; i--) {
    if (s[i] == s[s.length() - 1])
      r++;
    else
      break;
  }
  if (l - 1 >= n - r) {
    cout << (1LL * k * (s.length())) / 2;
    return 0;
  }
  ll x = 1;
  ll ans = 0;
  ll ct = 0;
  REP(i, 1, s.length() - 1) {
    if (s[i] == s[i - 1])
      x++;
    else {
      ct += (x / 2);
      x = 1;
    }
  }
  ct += (x / 2);
  if (s[s.length() - 1] == s[0]) {
    ct -= (l / 2);
    ct -= (r / 2);
    ll val = (l + r);
    ll an = val / 2;
    an *= (k - 1);
    ans += an;
    ans += (ct * k);
    ans += (l / 2);
    ans += (r / 2);
  } else
    ans = (ct * k);
  cout << ans;
}