Problem p02918 (C++)

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

#define ll long long
#define pb push_back
#define endl '\n'
#define mii map<ll, ll>
#define pii pair<ll, ll>
#define vi vector<ll>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define INF (1ll << 60)
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define rrep(i, a, b) for (ll i = a; i >= b; i--)
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define time                                                                   \
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
const int N = 100005;
void solve() {
  ll n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  // t=s;
  ll i = 0, c = 0;
  vi temp;
  while (i < n) {
    while (i < n && s[i] == 'R')
      c++, i++;
    if (c)
      temp.pb(c);
    c = 0;
    while (i < n && s[i] == 'L')
      c++, i++;
    if (c)
      temp.pb(c);
    c = 0;
  }
  // rep(i,0,sz(temp)-1) cout<<temp[i]<<" \n"[i==sz(temp)-1];
  vector<pii> odd;
  vector<pii> even;
  rep(i, 0, sz(temp) - 1) {
    if (i % 2 == 0)
      even.pb({temp[i], i});
    else
      odd.pb({temp[i], i});
  }
  sort(all(even), greater<pii>());
  sort(all(odd), greater<pii>());
  bool vis[sz(temp)];
  memset(vis, 0, sizeof(vis));
  rep(i, 0, k - 1) vis[even[i].S] = 1;
  rep(i, 0, k - 1) vis[odd[i].S] = 1;
  ll ans = 0, curr = 0;
  ll kk = 0;
  // rep(i,0,sz(temp)-1) cout<<vis[i]<<" \n"[i==sz(temp)-1];
  rep(i, 0, sz(temp) - 1) {
    if (i % 2 == 0 && vis[i]) {
      curr += temp[i];
    } else if (i % 2 == 1)
      curr += temp[i];
    else {
      if (curr)
        ans += curr - 1;
      curr = 0;
    }
  }
  if (curr)
    ans += curr - 1;
  kk = max(ans, kk);
  // cout<<ans<<endl;
  ans = 0;
  curr = 0;
  rep(i, 0, sz(temp) - 1) {
    if (i % 2 == 1 && vis[i]) {
      curr += temp[i];
    } else if (i % 2 == 0)
      curr += temp[i];
    else {
      ans += curr - 1;
      curr = 0;
    }
  }
  if (curr)
    ans += curr - 1;
  kk = max(ans, kk);
  cout << kk;
}
signed main() {
  ios int TESTS = 1;
  // cin>>TESTS;
  while (TESTS--) {
    solve();
  }
  time return 0;
}

Fixed submission:
// From Naruto to Hokage
// #include <RJ>
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define endl '\n'
#define mii map<ll, ll>
#define pii pair<ll, ll>
#define vi vector<ll>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define INF (1ll << 60)
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define rrep(i, a, b) for (ll i = a; i >= b; i--)
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define time                                                                   \
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
const int N = 100005;
void solve() {
  ll n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  // t=s;
  ll i = 0, c = 0;
  vi temp;
  while (i < n) {
    while (i < n && s[i] == 'R')
      c++, i++;
    if (c)
      temp.pb(c);
    c = 0;
    while (i < n && s[i] == 'L')
      c++, i++;
    if (c)
      temp.pb(c);
    c = 0;
  }
  // rep(i,0,sz(temp)-1) cout<<temp[i]<<" \n"[i==sz(temp)-1];
  ll ans = 0;
  for (auto i : temp)
    ans += i - 1;
  ans += min(sz(temp) - 1, k) * 2;
  cout << min(ans, n - 1);
}
signed main() {
  ios int TESTS = 1;
  // cin>>TESTS;
  while (TESTS--) {
    solve();
  }
  time return 0;
}