Problem p03835 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>

#define ld long double
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLr(obj) (obj).rbegin(), (obj).rend()
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, s, n) for (int i = s; i < n; i++)
#define endl "\n"

const ll INF = 1LL << 60;
const ld EPS = 1e-11;

// 切り上げ整数除算
int divRoundUp(const int &a, const int &b) { return (a + b - 1) / b; }
// 四捨五入
int roundOff(const double &a) { return (int)(a + 0.5); }
// r の(誤差付きの)符号に従って, -1, 0, 1 を返す.
int sgn(const ld &r) { return (r > EPS) - (r < -EPS); }
// a, b の(誤差付きの)大小比較の結果に従って, -1, 0, 1 を返す.
int sgn(const ld &a, const ld &b) { return sgn(a - b); }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;

inline void Main() {
  int K;
  cin >> K;
  int S;
  cin >> S;
  int cnt = 0;
  for (int X = K; X >= 0; --X) {
    if (X > S)
      continue;
    for (int Y = K; Y >= 0; --Y) {
      if (X + Y > S)
        continue;
      for (int Z = K; Z >= 0; --Z) {
        if (X + Y + Z == S)
          ++cnt;
      }
    }
  }
  cout << cnt << endl;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  Main();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>

#define ld long double
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLr(obj) (obj).rbegin(), (obj).rend()
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, s, n) for (int i = s; i < n; i++)
#define endl "\n"

const ll INF = 1LL << 60;
const ld EPS = 1e-11;

// 切り上げ整数除算
int divRoundUp(const int &a, const int &b) { return (a + b - 1) / b; }
// 四捨五入
int roundOff(const double &a) { return (int)(a + 0.5); }
// r の(誤差付きの)符号に従って, -1, 0, 1 を返す.
int sgn(const ld &r) { return (r > EPS) - (r < -EPS); }
// a, b の(誤差付きの)大小比較の結果に従って, -1, 0, 1 を返す.
int sgn(const ld &a, const ld &b) { return sgn(a - b); }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;

inline void Main() {
  int K;
  cin >> K;
  int S;
  cin >> S;
  int cnt = 0;
  for (int X = K; X >= 0; --X) {
    if (X > S)
      continue;
    for (int Y = K; Y >= 0; --Y) {
      if (X + Y > S)
        continue;
      int Z = S - X - Y;
      if (0 <= Z && Z <= K)
        ++cnt;
    }
  }
  cout << cnt << endl;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  Main();

  return 0;
}