Problem p03835 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> T inf;
template <> constexpr int inf<int> = 1e9;
template <> constexpr ll inf<ll> = 1e18;
template <> constexpr ld inf<ld> = 1e30;
#define DEBUG(X) cerr << __LINE__ << " " << #X << ": " << X << endl;
#define DUMP(A, n)                                                             \
  for (auto x = begin(A); x != begin(A) + n; x++) {                            \
    cout << *x << ' ';                                                         \
  }                                                                            \
  cout << endl;
#define DUMPP(A, n, m)                                                         \
  for (auto x = begin(A); x != begin(A) + n; x++) {                            \
    for (auto y = begin(*x); y != begin(*x) + m;)                              \
      cout << *y++ << ' ';                                                     \
    cout << endl;                                                              \
  };
#define DUMPM(M)                                                               \
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {                        \
    cout << itr->first << " -> " << itr->second << endl;                       \
  }
#define all(x) begin(x), end(x)
#define sci(x)                                                                 \
  int x;                                                                       \
  scanf("%d", &x);
#define scii(x, y)                                                             \
  int x, y;                                                                    \
  scanf("%d%d", &x, &y);
#define scf(x) scanf("%d", &x), x != 0
#define scff(x, y) scanf("%d%d", &x, &y), x != 0 && y != 0
#define scdd(x, y)                                                             \
  ld x, y;                                                                     \
  scanf("%Lf%Lf", &x, &y);
#define FOR(i, x, y) for (int i = (x); i < (int)(y); i++)
#define FORR(i, x, y) for (int i = (x); i <= (int)(y); i++)
#define REP(i, y) for (int i = 0; i < (int)(y); i++)
#define REPP(i, y) for (int i = 1; i <= (int)(y); i++)
#define fin(x, y)                                                              \
  ld x, y;                                                                     \
  while (~scanf("%Lf,%Lf", &x, &y))

int main() {
  int ans = 0;
  scii(K, S);
  REP(i, K + 1) {
    REP(j, K + 1) {
      REP(k, K + 1) {
        if (i + j + k == S) {
          ans++;
          break;
        }
      }
    }
  }
  printf("%d\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> T inf;
template <> constexpr int inf<int> = 1e9;
template <> constexpr ll inf<ll> = 1e18;
template <> constexpr ld inf<ld> = 1e30;
#define DEBUG(X) cerr << __LINE__ << " " << #X << ": " << X << endl;
#define DUMP(A, n)                                                             \
  for (auto x = begin(A); x != begin(A) + n; x++) {                            \
    cout << *x << ' ';                                                         \
  }                                                                            \
  cout << endl;
#define DUMPP(A, n, m)                                                         \
  for (auto x = begin(A); x != begin(A) + n; x++) {                            \
    for (auto y = begin(*x); y != begin(*x) + m;)                              \
      cout << *y++ << ' ';                                                     \
    cout << endl;                                                              \
  };
#define DUMPM(M)                                                               \
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {                        \
    cout << itr->first << " -> " << itr->second << endl;                       \
  }
#define all(x) begin(x), end(x)
#define sci(x)                                                                 \
  int x;                                                                       \
  scanf("%d", &x);
#define scii(x, y)                                                             \
  int x, y;                                                                    \
  scanf("%d%d", &x, &y);
#define scf(x) scanf("%d", &x), x != 0
#define scff(x, y) scanf("%d%d", &x, &y), x != 0 && y != 0
#define scdd(x, y)                                                             \
  ld x, y;                                                                     \
  scanf("%Lf%Lf", &x, &y);
#define FOR(i, x, y) for (int i = (x); i < (int)(y); i++)
#define FORR(i, x, y) for (int i = (x); i <= (int)(y); i++)
#define REP(i, y) for (int i = 0; i < (int)(y); i++)
#define REPP(i, y) for (int i = 1; i <= (int)(y); i++)
#define fin(x, y)                                                              \
  ld x, y;                                                                     \
  while (~scanf("%Lf,%Lf", &x, &y))

int main() {
  int ans = 0;
  scii(K, S);
  REP(i, K + 1) {
    REP(j, K + 1) {
      int z = S - i - j;
      if (0 <= z && z <= K)
        ans++;
    }
  }
  printf("%d\n", ans);
}