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

// AtCode_Beginner_160.cpp : This file contains the 'main' function. Program
// execution begins and ends there.
//
#include <algorithm>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

void A();
void B();
void C();
void D();
void E();
void F();

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  C();
}

void A() {
  string s;
  cin >> s;

  cout << ((s[2] == s[3]) && (s[4] == s[5]) ? "Yes" : "No") << endl;
}

void B() {
  int n;
  cin >> n;

  int sum = n - n % 500;
  sum *= 2;
  sum += (n % 500) - (n % 5);

  cout << sum << endl;
}

void C() {

  int K, N;
  cin >> K >> N;

  vector<int> points(N);

  for (int &num : points)
    cin >> num;

  sort(points.begin(), points.end());

  int maxDistance = 0;

  for (int i = 0; i < N; i++) {

    int dif = points[(i + 1) % N] - points[i];
    if (i == N - 1)
      dif = K + dif;
    if (dif > maxDistance)
      maxDistance = dif;
  }

  cout << K - maxDistance;
}

void D() {
  /*int N, X, Y;
  cin >> N >> X >> Y;

  vector<int> pair{ X, Y };

  vector<int> results(N);

  int distance = Y - X;


  for (int i = 1; i < N; i++) {

          int def = N - i;
          if (i == ) results[i] = def;
          if (i == distance) results[i] = def + 1;
          if (i > distance) results[i] = def - 1;

          cout << results[i] << endl;

  }*/
}

void E() {

  int X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;

  vector<int> R(A);
  vector<int> G(B);
  vector<int> W(C);

  for (int &r : R)
    cin >> r;
  for (int &g : G)
    cin >> g;
  for (int &w : W)
    cin >> w;

  sort(R.begin(), R.end());
  sort(G.begin(), G.end());
  sort(W.begin(), W.end());

  reverse(R.begin(), R.end());
  reverse(G.begin(), G.end());
  reverse(W.begin(), W.end());

  long long total = 0;

  int a = 0, b = 0, c = 0;

  while (a + b < X + Y) {

    long long mRed = a != A ? R[a] : -1;
    long long mGreen = b != B ? G[b] : -1;
    long long mWhite = c != C ? W[c] : -1;

    // need more reds or greens
    if (X - a > 0 && Y - b > 0) {
      if (mRed > mGreen) {
        if (mRed > mWhite) {
          total += mRed;
          a++;
        } else {
          total += mWhite;
          c++;
          Y--;
        }
      } else {
        if (mGreen > mWhite) {
          total += mGreen;
          b++;
        } else {
          total += mWhite;
          c++;
          X--;
        }
      }

    } else if (X - a > 0) {
      if (mRed > mWhite) {
        total += mRed;
        a++;
      } else {
        total += mWhite;
        c++;
        X--;
      }
    } else {
      if (mGreen > mWhite) {
        total += mGreen;
        b++;
      } else {
        total += mWhite;
        c++;
        Y--;
      }
    }
  }

  cout << total;
}

int helper(int N, int K) {

  while (N >= K) {
    if (N % K == 0)
      N /= K;
    else
      N %= K;
  }

  return N == 1;
}

void F() {

  int N;
  cin >> N;

  vector<bool> factor(N, false);

  int count = 2;

  for (int n = 2; n * n < N; n++) {

    int divisible = false;
    if (N % n == 1)
      count++;
    else if (N % n == 0)
      if (helper(N, n))
        count++;
    /*
    //hasnt been done before
    if (!factor[n]) {

            divisible = (N % n == 0);

            for (int i = n; i < N; i+=n) {
                    factor[i] = true;

                    if (divisible) {
                            if(helper(N, i)) count++;
                    }

            }

    }*/
  }

  cout << count;
}

Error: WA

Fixed submission:

// AtCode_Beginner_160.cpp : This file contains the 'main' function. Program
// execution begins and ends there.
//
#include <algorithm>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

void A();
void B();
void C();
void D();
void E();
void F();

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  E();
}

void A() {
  string s;
  cin >> s;

  cout << ((s[2] == s[3]) && (s[4] == s[5]) ? "Yes" : "No") << endl;
}

void B() {
  int n;
  cin >> n;

  int sum = n - n % 500;
  sum *= 2;
  sum += (n % 500) - (n % 5);

  cout << sum << endl;
}

void C() {

  int K, N;
  cin >> K >> N;

  vector<int> points(N);

  for (int &num : points)
    cin >> num;

  sort(points.begin(), points.end());

  int maxDistance = 0;

  for (int i = 0; i < N; i++) {

    int dif = points[(i + 1) % N] - points[i];
    if (i == N - 1)
      dif = 20 + dif;
    if (dif > maxDistance)
      maxDistance = dif;
  }

  cout << K - maxDistance;
}

void D() {
  /*int N, X, Y;
  cin >> N >> X >> Y;

  vector<int> pair{ X, Y };

  vector<int> results(N);

  int distance = Y - X;


  for (int i = 1; i < N; i++) {

          int def = N - i;
          if (i == ) results[i] = def;
          if (i == distance) results[i] = def + 1;
          if (i > distance) results[i] = def - 1;

          cout << results[i] << endl;

  }*/
}

void E() {

  int X, Y, A, B, C;
  cin >> X >> Y >> A >> B >> C;

  vector<int> R(A);
  vector<int> G(B);
  vector<int> W(C);

  for (int &r : R)
    cin >> r;
  for (int &g : G)
    cin >> g;
  for (int &w : W)
    cin >> w;

  sort(R.begin(), R.end());
  sort(G.begin(), G.end());
  sort(W.begin(), W.end());

  reverse(R.begin(), R.end());
  reverse(G.begin(), G.end());
  reverse(W.begin(), W.end());

  long long total = 0;

  int a = 0, b = 0, c = 0;

  while (a + b < X + Y) {

    int mRed = a != A ? R[a] : -1;
    int mGreen = b != B ? G[b] : -1;
    int mWhite = c != C ? W[c] : -1;

    // need more reds or greens
    if (X - a > 0 && Y - b > 0) {
      if (mRed > mGreen) {
        if (mRed > mWhite) {
          total += mRed;
          a++;
        } else {
          total += mWhite;
          c++;
          Y--;
        }
      } else {
        if (mGreen > mWhite) {
          total += mGreen;
          b++;
        } else {
          total += mWhite;
          c++;
          X--;
        }
      }

    } else if (X - a > 0) {
      if (mRed > mWhite) {
        total += mRed;
        a++;
      } else {
        total += mWhite;
        c++;
        X--;
      }
    } else {
      if (mGreen > mWhite) {
        total += mGreen;
        b++;
      } else {
        total += mWhite;
        c++;
        Y--;
      }
    }
  }

  cout << total;
}

int helper(int N, int K) {

  while (N >= K) {
    if (N % K == 0)
      N /= K;
    else
      N %= K;
  }

  return N == 1;
}

void F() {

  int N;
  cin >> N;

  vector<bool> factor(N, false);

  int count = 2;

  for (int n = 2; n * n < N; n++) {

    int divisible = false;
    if (N % n == 1)
      count++;
    else if (N % n == 0)
      if (helper(N, n))
        count++;
    /*
    //hasnt been done before
    if (!factor[n]) {

            divisible = (N % n == 0);

            for (int i = n; i < N; i+=n) {
                    factor[i] = true;

                    if (divisible) {
                            if(helper(N, i)) count++;
                    }

            }

    }*/
  }

  cout << count;
}