Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
using ll = long long;
using namespace std;

typedef std::pair<int, int> ipair;
bool lessPair(const ipair &l, const ipair &r) { return l.second < r.second; }
bool greaterPair(const ipair &l, const ipair &r) { return l.second > r.second; }

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define irep(i, n) for (int i = 0; i <= (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rirep(i, n) for (int i = n; i >= 0; --i)
int factorical(int n) {
  int sum = 1;
  for (int i = 1; i <= n; ++i) {
    sum *= i;
  }
  return sum;
}

vector<int> divisors(int n) {
  vector<int> res;
  for (int i = 1; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    res.push_back(i);
    if (n / i == i)
      continue; // 上の行で追加済み。
    res.push_back(n / i);
  }
  return res;
}
vector<ll> divisors_ll(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    res.push_back(i);
    if (n / i == i)
      continue; // 上の行で追加済み。
    res.push_back(n / i);
  }
  return res;
}

void printVector(const vector<int> &vec) {
  for (int value : vec) {
    cout << value << " ";
  }
  cout << endl;
}

bool pal(string s) {
  int n = s.length() / 2;
  rep(i, n) {
    if (s[i] != s[s.length() - 1 - i])
      return false;
  }
  return true;
}

int euclidean_gcd(int a, int b) {
  if (a < b)
    return euclidean_gcd(b, a);
  int r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

vector<string> vec_sort_and_del(vector<string> s) {
  std::sort(s.begin(), s.end());
  s.erase(std::unique(s.begin(), s.end()), s.end());
  return s;
}

int main() {
  int n, k, nk, ans;
  cin >> n >> k;
  nk = n % k;
  ans = k - nk;
  if (ans < 0)
    ans = 0;
  cout << min(ans, nk) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
using ll = long long;
using namespace std;

typedef std::pair<int, int> ipair;
bool lessPair(const ipair &l, const ipair &r) { return l.second < r.second; }
bool greaterPair(const ipair &l, const ipair &r) { return l.second > r.second; }

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define irep(i, n) for (int i = 0; i <= (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rirep(i, n) for (int i = n; i >= 0; --i)
int factorical(int n) {
  int sum = 1;
  for (int i = 1; i <= n; ++i) {
    sum *= i;
  }
  return sum;
}

vector<int> divisors(int n) {
  vector<int> res;
  for (int i = 1; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    res.push_back(i);
    if (n / i == i)
      continue; // 上の行で追加済み。
    res.push_back(n / i);
  }
  return res;
}
vector<ll> divisors_ll(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    res.push_back(i);
    if (n / i == i)
      continue; // 上の行で追加済み。
    res.push_back(n / i);
  }
  return res;
}

void printVector(const vector<int> &vec) {
  for (int value : vec) {
    cout << value << " ";
  }
  cout << endl;
}

bool pal(string s) {
  int n = s.length() / 2;
  rep(i, n) {
    if (s[i] != s[s.length() - 1 - i])
      return false;
  }
  return true;
}

int euclidean_gcd(int a, int b) {
  if (a < b)
    return euclidean_gcd(b, a);
  int r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

vector<string> vec_sort_and_del(vector<string> s) {
  std::sort(s.begin(), s.end());
  s.erase(std::unique(s.begin(), s.end()), s.end());
  return s;
}

int main() {
  ll n, k, nk, ans;
  cin >> n >> k;
  nk = n % k;
  ans = k - nk;
  if (ans < 0)
    ans = 0;
  cout << min(ans, nk) << endl;
  return 0;
}