Problem p00961 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 500010

int main() {
  int n, k, sum;
  string s, t;

  int left[SIZE] = {}, right[SIZE] = {};

  cin >> n >> k >> s >> t;

  for (int i = 0; i < n; i++) {
    if (i == 0 || t[i - 1] != t[i])
      left[i]++;
    if (i == n - 1 || t[i] != t[i + 1])
      right[i]++;
  }

  for (int i = 0; i < n; i++)
    right[i + 1] += right[i];
  for (int i = n; i > 0; i--)
    left[i - 1] += left[i];
  sum = left[0];

  priority_queue<pair<int, int>> pq0, pq1;
  int sameDP = 0;
  pq0.push({-INF, INF});
  pq1.push({-INF, INF});
  pq0.push({0, -1});

  for (int i = 0; i < n; i++) {
    while (pq0.size() && pq0.top().second < i - k) {
      pq0.pop();
    }
    while (pq1.size() && pq1.top().second < i - k) {
      pq1.pop();
    }

    if (t[i] != s[i])
      sameDP = INF;

    int p0 = -1 * pq0.top().first;
    int p1 = -1 * pq1.top().first;
    int l0 = pq0.top().second;
    int l1 = pq1.top().second;
    int calc0, calc1;

    if (l0 == -1 || l0 == INF)
      calc0 = p0 + (sum - left[i + 1]) / 2 + 1;
    else
      calc0 = p0 + right[l0] / 2 + (sum - left[i + 1] - right[l0]) / 2 + 1;

    if (l1 == -1 || l1 == INF)
      calc1 = p1 + (sum - left[i + 1]) / 2 + 1;
    else
      calc1 = p1 + right[l1] / 2 + (sum - left[i + 1] - right[l1]) / 2 + 1;

    sameDP = min({calc0, calc1, sameDP});

    if (right[i] % 2 == 0)
      pq0.push({-(sameDP - right[i] / 2), i});
    else
      pq1.push({-(sameDP - right[i] / 2), i});
  }

  printf("%d\\n", sameDP);

  return 0;
}


Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int main() {
  int n, k, sum;
  string s, t;

  int left[SIZE] = {}, right[SIZE] = {};

  cin >> n >> k >> s >> t;

  for (int i = 0; i < n; i++) {
    if (i == 0 || t[i - 1] != t[i])
      left[i]++;
    if (i == n - 1 || t[i] != t[i + 1])
      right[i]++;
  }

  for (int i = 0; i < n; i++)
    right[i + 1] += right[i];
  for (int i = n; i > 0; i--)
    left[i - 1] += left[i];
  sum = left[0];

  priority_queue<pair<int, int>> pq;
  int sameDP = 0;

  pq.push({0, -1});

  for (int i = 0; i < n; i++) {
    while (pq.size() && pq.top().second < i - k) {
      pq.pop();
    }

    if (t[i] != s[i])
      sameDP = INF;

    int p = -pq.top().first / 2;
    int l = pq.top().second;
    // (sum - left[i+1] - right[l])/2
    int calc;

    if (l == -1)
      calc = p + (sum - left[i + 1]) / 2 + 1;
    else
      calc = p + right[l] / 2 + (sum - left[i + 1] - right[l]) / 2 + 1;

    pq.push({-((calc - right[i] / 2) * 2 + 1 - right[i] % 2), i});

    sameDP = min(sameDP, calc);
  }

  printf("%d\\n", sameDP);

  return 0;
}