Problem p03835 (C++)

Buggy submission (status: Time Limit Exceeded):
//
//  main.cpp
//  CompetitiveProgramming
//
//  Created by Yuma Kikuchi on 2018/03/02.
//  Copyright © 2018年 yumarimo. All rights reserved.
//

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define INF 999999999

using namespace std;

typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

llong pow(int x, int n) {
  llong ans = x;
  if (n == 0)
    return 1;

  for (int i = 0; i < n - 1; i++) {
    ans *= x;
  }

  return ans;
}

void swap(int *X, int *Y) {
  int t = *X;
  *X = *Y;
  *Y = t;
}

string toUpper(string s) {
  string t = s;
  REP(i, s.size()) { t[i] = toupper(s[i]); }
  return t;
}

string toLower(string s) {
  string t = s;
  REP(i, s.size()) { t[i] = tolower(s[i]); }
  return t;
}

int main() {
  int k, s;
  cin >> k >> s;

  int cnt = 0;
  for (int x = 0; x <= k; ++x) {
    for (int y = 0; y <= k; ++y) {
      for (int z = 0; z <= k; ++z) {
        if (x + y + z == s) {
          cnt += 1;
          break;
        }
      }
    }
  }

  cout << cnt << endl;

  return 0;
}

Fixed submission:
//
//  main.cpp
//  CompetitiveProgramming
//
//  Created by Yuma Kikuchi on 2018/03/02.
//  Copyright © 2018年 yumarimo. All rights reserved.
//

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define INF 999999999

using namespace std;

typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

llong pow(int x, int n) {
  llong ans = x;
  if (n == 0)
    return 1;

  for (int i = 0; i < n - 1; i++) {
    ans *= x;
  }

  return ans;
}

void swap(int *X, int *Y) {
  int t = *X;
  *X = *Y;
  *Y = t;
}

string toUpper(string s) {
  string t = s;
  REP(i, s.size()) { t[i] = toupper(s[i]); }
  return t;
}

string toLower(string s) {
  string t = s;
  REP(i, s.size()) { t[i] = tolower(s[i]); }
  return t;
}

int main() {
  int k, s;
  cin >> k >> s;

  int cnt = 0;
  for (int x = 0; x <= k; ++x) {
    for (int y = 0; y <= k; ++y) {
      if (0 <= s - x - y && s - x - y <= k)
        ++cnt;
    }
  }

  cout << cnt << endl;

  return 0;
}