Problem p02994 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define test()                                                                 \
  int t;                                                                       \
  cin >> t;                                                                    \
  loop(test, 1, t + 1)
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define nl cout << "\n"
#define sp cout << " "
#define F first
#define S second
#define vi vector<int>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<pii>
#define vll vector<pll>
#define MOD 1000000007
#define all(x) x.begin(), x.end()

template <class C> void min_self(C &a, C b) { a = min(a, b); }
template <class C> void max_self(C &a, C b) { a = max(a, b); }

void mod(ll &n, ll m = MOD) { n %= m, n += m, n %= m; }

const int MAXN = 1e5 + 5;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio();

  int n, l;
  cin >> n >> l;

  ll sum = (n * (n + 1)) / 2;
  ll total = (l - 1) * n;
  ll left = l;
  ll right = l + n - 1;
  ll ans = 0;

  if (left <= 0 && right >= 0)
    ans = sum + total;
  else if (left <= 0)
    ans = sum + total - right;
  else if (right >= 0)
    ans = sum + total - left;

  cout << ans, nl;

  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define test()                                                                 \
  int t;                                                                       \
  cin >> t;                                                                    \
  loop(test, 1, t + 1)
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define nl cout << "\n"
#define sp cout << " "
#define F first
#define S second
#define vi vector<int>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<pii>
#define vll vector<pll>
#define MOD 1000000007
#define all(x) x.begin(), x.end()

template <class C> void min_self(C &a, C b) { a = min(a, b); }
template <class C> void max_self(C &a, C b) { a = max(a, b); }

void mod(ll &n, ll m = MOD) { n %= m, n += m, n %= m; }

const int MAXN = 1e5 + 5;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  fastio();

  int n, l;
  cin >> n >> l;

  ll sum = (n * (n + 1)) / 2;
  ll total = (l - 1) * n;
  ll left = l;
  ll right = l + n - 1;
  ll ans = 0;

  if (left <= 0 && right >= 0)
    ans = sum + total;
  else if (left <= 0)
    ans = sum + total - right;
  else if (right >= 0)
    ans = sum + total - left;

  cout << ans, nl;

  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}