Problem p03835 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define euc_dis(x, y) sqrt(x *x + y * y)

#define pb push_back
#define mp make_pair
#define INF 999999999
#define sp ' '
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()

int mod(int a, int b) { return (a % b + b) % b; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void Judge(bool b) { b ? Yes() : No(); }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void JUDGE(bool b) { b ? YES() : NO(); }

template <typename T> void ppp(T n) { cout << n << endl; }
template <typename T> void dpp(T n) { cerr << n << endl; }

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int ddy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

// typedef
typedef long long ll;
typedef pair<int, int> pint;
typedef map<int, int> mint;
typedef vector<int> vint;
typedef vector<char> vch;
typedef vector<long long> vll;
typedef vector<string> vstr;

template <typename T> std::string to_string(const T &n) {
  std::ostringstream stm;
  stm << n;
  return stm.str();
}

vector<string> split(const string &str, char sep) {
  vector<string> v;
  stringstream ss(str);
  string buffer;
  while (getline(ss, buffer, sep)) {
    v.pb(buffer);
  }
  return v;
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int k, s;
  cin >> k >> s;

  int cnt = 0;
  rep(z, k + 1) {
    rep(y, k + 1) {
      rep(x, k + 1) {
        if (x + y + z == s)
          cnt++;
      }
    }
  }

  ppp(cnt);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define euc_dis(x, y) sqrt(x *x + y * y)

#define pb push_back
#define mp make_pair
#define INF 999999999
#define sp ' '
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()

int mod(int a, int b) { return (a % b + b) % b; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void Judge(bool b) { b ? Yes() : No(); }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void JUDGE(bool b) { b ? YES() : NO(); }

template <typename T> void ppp(T n) { cout << n << endl; }
template <typename T> void dpp(T n) { cerr << n << endl; }

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int ddy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

// typedef
typedef long long ll;
typedef pair<int, int> pint;
typedef map<int, int> mint;
typedef vector<int> vint;
typedef vector<char> vch;
typedef vector<long long> vll;
typedef vector<string> vstr;

template <typename T> std::string to_string(const T &n) {
  std::ostringstream stm;
  stm << n;
  return stm.str();
}

vector<string> split(const string &str, char sep) {
  vector<string> v;
  stringstream ss(str);
  string buffer;
  while (getline(ss, buffer, sep)) {
    v.pb(buffer);
  }
  return v;
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int k, s;
  cin >> k >> s;

  int cnt = 0;
  rep(z, k + 1) {
    rep(y, k + 1) {
      if (s - z - y <= k && s - z - y >= 0)
        cnt++;
    }
  }

  ppp(cnt);

  return 0;
}