Problem p02918 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define pi pair<int, int>
#define pl pair<long long, long long>
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
#define en cout << endl // セミコロンつけろ
// #define MM 1000000000
// #define MOD MM+7
typedef long long ll;
const int MM = 1e9;
const int MOD = MM + 7;
const long double PI = acos(-1);
const long long INF = 1e15;
int dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1};
// 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122
template <typename T> T GCD(T u, T v) { return v ? GCD(v, u % v) : u; }
template <typename T> T LCM(T x, T y) {
  T gc = GCD(x, y);
  return x * y / gc;
}
struct edge {
  ll to, cost;
};
/*
const int COM_MAX = 500500;
long long fac[COM_MAX],finv[COM_MAX],inv[COM_MAX];
void init(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i = 2; i < COM_MAX; i++){
        fac[i] = fac[i-1]*i%MOD;
        inv[i] = MOD - inv[MOD%i]*(MOD/i)%MOD;
        finv[i] = finv[i-1]*inv[i]%MOD;
    }
}
long long COM(int n,int k){
    if(n < k || n < 0 || k < 0) return 0;
    return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
}
*/

int main() {
  int N, K;
  cin >> N >> K;
  string s;
  cin >> s;
  // Lmax
  vector<int> L(0), R(0), sz(0);
  int tmp = 1;
  for (int i = 1; i < s.size(); i++) {
    if (s[i - 1] == s[i]) {
      tmp++;
    } else {
      if (s[i] == 'R') {
        sz.push_back(tmp);
      } else {
        sz.push_back(tmp);
      }
      tmp = 1;
    }
  }
  sz.push_back(tmp);
  int length = 0, mx = 0;
  for (int i = 0; i < 2 * K + 1; i++) {
    length += sz[i];
  }
  length--;

  for (int i = 2 * K + 1; i < sz.size(); i++) {
    length += sz[i] - 1;
  }
  // cout << "check : " << length << endl;
  mx = max(length, mx);

  int in = 1;
  for (int i = 2 * K + 1; i < sz.size(); i++) {
    length -= sz[in];
    length += sz[in] - 1;
    length -= sz[in - 1];
    length += sz[in - 1] - 1;
    in += 2;

    length -= sz[i] - 1;
    length += sz[i];
    i++;
    mx = max(mx, length);
    // cout  << "check : " << length << endl;
  }
  cout << mx << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define pi pair<int, int>
#define pl pair<long long, long long>
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
#define en cout << endl // セミコロンつけろ
// #define MM 1000000000
// #define MOD MM+7
typedef long long ll;
const int MM = 1e9;
const int MOD = MM + 7;
const long double PI = acos(-1);
const long long INF = 1e15;
int dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1};
// 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122
template <typename T> T GCD(T u, T v) { return v ? GCD(v, u % v) : u; }
template <typename T> T LCM(T x, T y) {
  T gc = GCD(x, y);
  return x * y / gc;
}
struct edge {
  ll to, cost;
};
/*
const int COM_MAX = 500500;
long long fac[COM_MAX],finv[COM_MAX],inv[COM_MAX];
void init(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i = 2; i < COM_MAX; i++){
        fac[i] = fac[i-1]*i%MOD;
        inv[i] = MOD - inv[MOD%i]*(MOD/i)%MOD;
        finv[i] = finv[i-1]*inv[i]%MOD;
    }
}
long long COM(int n,int k){
    if(n < k || n < 0 || k < 0) return 0;
    return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
}
*/

int main() {
  int N, K;
  cin >> N >> K;
  string s;
  cin >> s;
  // Lmax
  vector<int> L(0), R(0), sz(0);
  int tmp = 1;
  for (int i = 1; i < s.size(); i++) {
    if (s[i - 1] == s[i]) {
      tmp++;
    } else {
      if (s[i] == 'R') {
        sz.push_back(tmp);
      } else {
        sz.push_back(tmp);
      }
      tmp = 1;
    }
  }
  if (tmp >= 1)
    sz.push_back(tmp);
  if (sz.size() % 2 == 0) {
    if (sz.size() / 2 <= K) {
      cout << N - 1 << endl;
      return 0;
    }
  } else {
    if (sz.size() - 1 <= 2 * K) {
      cout << N - 1 << endl;
      return 0;
    }
  }

  int length = 0, mx = 0;
  for (int i = 0; i < 2 * K + 1; i++) {
    length += sz[i];
  }
  length--;

  for (int i = 2 * K + 1; i < sz.size(); i++) {
    length += sz[i] - 1;
  }
  // cout << "check : " << length << endl;
  mx = max(length, mx);

  int in = 1;
  for (int i = 2 * K + 1; i < sz.size(); i++) {
    length -= sz[in];
    length += sz[in] - 1;
    length -= sz[in - 1];
    length += sz[in - 1] - 1;
    in += 2;

    length -= sz[i] - 1;
    length += sz[i];
    i++;
    mx = max(mx, length);
    // cout  << "check : " << length << endl;
  }
  cout << mx << endl;
}