prithivMLmods/Coder-Stat huggingface dataset:
Problem p02707 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define pi 3.14159265358979323846
#define ll long long
#define N 100001

int main() {
  ll n;
  cin >> n;
  ll ara[n - 1];
  for (int i = 0; i < n - 1; i++)
    cin >> ara[i];
  ll x;
  ll p[N] = {0};

  for (int i = 0; i < n - 1; i++) {

    x = ara[i];
    p[x]++;
  }
  for (int i = 1; i <= n; i++)
    cout << p[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define pi 3.14159265358979323846
#define ll long long
#define N 200001

int main() {
  ll n;
  cin >> n;
  ll ara[n - 1];
  for (int i = 0; i < n - 1; i++)
    cin >> ara[i];
  ll x;
  ll p[N] = {0};

  for (int i = 0; i < n - 1; i++) {

    x = ara[i];
    p[x]++;
  }
  for (int i = 1; i <= n; i++)
    cout << p[i] << endl;
}
Problem p02707 (C++)

Buggy submission (status: Runtime Error):
// #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
// #define int long long
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second
#define chmin(a, b) a = min((a), (b))
#define chmax(a, b) a = max((a), (b))
#define l1 list[index]
#define l2 list[index - 1]
#define l3 list[index + 1]
#define iif(i, j) ((i < 0 && j < 0) || (i > 0 && j > 0)) ? true : false
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
typedef pair<pint, pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = 3.14159265359;
int ca[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

vector<int> list(100005, 0);

signed main() {
  int N;

  cin >> N;

  for (int i = 1; i < N; i++) {
    int num;
    cin >> num;
    list[num]++;
  }

  for (int i = 1; i <= N; i++) {
    cout << list[i] << endl;
  }

  return 0;
}

Fixed submission:
// #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
// #define int long long
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second
#define chmin(a, b) a = min((a), (b))
#define chmax(a, b) a = max((a), (b))
#define l1 list[index]
#define l2 list[index - 1]
#define l3 list[index + 1]
#define iif(i, j) ((i < 0 && j < 0) || (i > 0 && j > 0)) ? true : false
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
typedef pair<pint, pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = 3.14159265359;
int ca[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

vector<int> list(200005, 0);

signed main() {
  int N;

  cin >> N;

  for (int i = 1; i < N; i++) {
    int num;
    cin >> num;
    list[num]++;
  }

  for (int i = 1; i <= N; i++) {
    cout << list[i] << endl;
  }

  return 0;
}
Problem p02707 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {
  int n, j, a[20000], count[20000];

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    count[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    j = a[i] - 1;
    count[j]++;
  }

  for (int i = 0; i < n; i++) {
    cout << count[i] << endl;
  }
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n, j, a[200000], count[200000];

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    count[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    j = a[i] - 1;
    count[j]++;
  }

  for (int i = 0; i < n; i++) {
    cout << count[i] << endl;
  }
}
Problem p02707 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using vi = vector<int>;
using vvi = vector<vector<int>>;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i > 0; i--)
#define all(v) v.begin(), v.end()
#define debug(x) cout << x << endl
#define vout(v) rep(i, v.size()) cout << v[i] << " ";
#define INF INT_MAX

int N;

int main() {
  cin >> N;
  vi a(N, 0);
  rep(i, N - 1) { cin >> a[i + 1]; }
  vi ans(N, 0);
  rep(i, N) { ans[a[i] - 1]++; }
  rep(i, N) { cout << ans[i] << endl; }
  return 0;
}


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

using namespace std;
using vi = vector<int>;
using vvi = vector<vector<int>>;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i > 0; i--)
#define all(v) v.begin(), v.end()
#define debug(x) cout << x << endl
#define vout(v) rep(i, v.size()) cout << v[i] << " ";
#define INF INT_MAX

int N;

int main() {
  cin >> N;
  vi a(N, 0);
  rep(i, N - 1) { cin >> a[i + 1]; }
  vi ans(N, 0);
  rep(i, N - 1) { ans[a[i + 1] - 1]++; }
  rep(i, N) { cout << ans[i] << endl; }
  return 0;
}
Problem p02707 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int N;
int a[200001];

int main() {
  cin >> N;
  for (int i = 1; i < N; i++)
    cin >> a[i];
  map<int, int> mp;
  for (int i = 1; i < N; i++) {
    mp[i] = 0;
  }
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    for (int i = 1; i < N; i++) {
      if (a[i] == itr->first) {
        itr->second++;
      }
    }
  }
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    cout << itr->second << endl;
  }
  cout << 0 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int N;
int a[200001];

int main() {
  cin >> N;
  for (int i = 1; i < N; i++)
    cin >> a[i];
  map<int, int> mp;
  for (int i = 1; i < N; i++) {
    mp[i] = 0;
  }
  for (int i = 1; i < N; i++) {
    mp[a[i]]++;
  }
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    cout << itr->second << endl;
  }
  cout << 0 << endl;
}
Problem p02707 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n - 1);
  for (int i = 0; i < n - 1; i++) {
    cin >> a[i];
  }
  vector<int> b(n, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      if (a[j] == i + 1) {
        b[i]++;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << b[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n - 1);
  for (int i = 0; i < n - 1; i++) {
    cin >> a[i];
  }
  vector<int> b(n, 0);
  for (int j = 0; j < n - 1; j++) {
    b[a[j] - 1]++;
  }
  for (int i = 0; i < n; i++) {
    cout << b[i] << endl;
  }
  return 0;
}
Problem p02707 (C++)

Buggy submission (status: Runtime Error):
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N = 0;
  vector<int> A;
  vector<int> answer;

  cin >> N;

  rep(i, N) {
    A.push_back(0);
    answer.push_back(0);
    if (i >= 1) {
      cin >> A[i];
    }
  }

  rep(i, N) { answer[A[i] - 1]++; }

  rep(i, N) { cout << answer[i] << endl; }
}

Fixed submission:
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N = 0;
  vector<int> A;
  vector<int> answer;

  cin >> N;

  rep(i, N) {
    A.push_back(0);
    answer.push_back(0);
    if (i >= 1) {
      cin >> A[i];
    }
  }

  rep(i, N) {
    if (i >= 1) {
      answer[A[i] - 1]++;
    }
  }

  rep(i, N) { cout << answer[i] << endl; }
}
Problem p02707 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n - 1; i++)
    cin >> a[i];
  vector<int> c(n);

  rep(i, n) { c[a[i] - 1]++; }

  rep(i, n) { printf("%d\n", c[i]); }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n - 1; i++)
    cin >> a[i];
  vector<int> c(n);

  for (int i = 0; i < n - 1; i++) {
    c[a[i] - 1]++;
  }

  rep(i, n) { printf("%d\n", c[i]); }

  return 0;
}
Problem p02707 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> count(100000, 0);
  for (int i = 0; i < n - 1; i++) {
    int a;
    cin >> a;
    count[a - 1]++;
  }
  for (int i = 0; i < n; i++) {
    cout << count[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> count(200000, 0);
  for (int i = 0; i < n - 1; i++) {
    int a;
    cin >> a;
    count[a - 1]++;
  }
  for (int i = 0; i < n; i++) {
    cout << count[i] << endl;
  }
  return 0;
}
Problem p02707 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n - 1], ans[n];
  for (int i = 0; i < n; i++)
    ans[i] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans[a[i] - 1]++;
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n - 1], ans[n];
  for (int i = 0; i < n; i++)
    ans[i] = 0;
  for (int i = 0; i < n - 1; i++) {
    cin >> a[i];
    ans[a[i] - 1]++;
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
int main() {
  ll n, k;
  cin >> n >> k;
  ll cnt = 0;
  for (int j = k; j <= n + 1; j++) {
    ll min = 0;
    ll max = 0;
    for (int i = 0; i < j; i++) {
      min += i;
    }
    for (int i = n - j + 1; i < n + 1; i++) {
      max += i;
    }
    cnt = (cnt + max - min + 1) % MOD;
  }
  cout << cnt << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
int main() {
  ll n, k;
  cin >> n >> k;
  ll cnt = 0;
  for (int j = k; j <= n + 1; j++) {
    // ll min=(j-1)*j/2;
    // ll max=(n+n-j+1)*j/2;
    // for(int i=0; i<j;i++){
    //   min+=i;
    // }
    // cout << min << endl;
    // for(int i=n-j+1; i<n+1;i++){
    //   max+=i;
    // }
    // cout << max << endl;
    // cnt = (cnt + max -min+1)%MOD;
    cnt = (cnt + (n - j + 1) * j + 1) % MOD;
    // cout << (n-j+1)*j << endl;
    // cout << max - min << endl;
  }
  cout << cnt << endl;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  lint n, k, s = 0;
  cin >> n >> k;
  ++n;
  v1li v(n + 1);
  v[0] = 0;
  rep(i, n) v[i + 1] = v[i] + i + 1;
  for (int i = k; k <= n; ++i) {
    s += v[n] - v[n - k] - v[k];
    s %= mod1;
  }
  cout << s << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  lint n, k, s = 0;
  cin >> n >> k;
  ++n;
  v1li v(n + 1);
  v[0] = 0;
  rep(i, n) v[i + 1] = v[i] + i + 1;
  for (int i = k; i <= n; ++i) {
    s += v[n] - v[n - i] - v[i] + 1;
    s %= mod1;
  }
  cout << s << endl;
  return 0;
}
Problem p02708 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()

const ll mod = 1000000007;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> m(n + 2);
  ll s = 0, e = 0, a = 0, b = n;
  rep(i, n + 2) {
    s += a;
    a++;
    e += b;
    b--;
    m[i + 1] = e - s + 1;
  }
  ll ans = 0;
  for (int i = k; i < n + 2; ++i) {
    ans += m[i];
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()

const ll mod = 1000000007;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> m(10000000);
  ll s = 0, e = 0, a = 0, b = n;
  rep(i, n + 2) {
    s += a;
    a++;
    e += b;
    b--;
    m[i + 1] = e - s + 1;
  }
  ll ans = 0;
  for (int i = k; i < n + 2; ++i) {
    ans += m[i];
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02708 (C++)

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

typedef long lint;
typedef long long llint;

static const llint MAX = 2e5 + 5;
// static const int NIL = -1;
// static const ll INF = 1<<21;
static const llint MOD = 1e9 + 7;

bool compPair(const pair<int, int> &arg1, const pair<int, int> &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int main(void) {
  llint n, k;
  cin >> n >> k;

  llint ans = 0, tmp, s, g;
  for (llint in = k; in <= n + 1; in++) {
    s = 0;
    g = 0;
    for (llint ip = 0; ip < in; ip++) {
      s += ip;
      g += n - ip;
    }
    g = min(n * (n + 1) / 2, g);
    tmp = g - s + 1;
    tmp %= MOD;
    ans += tmp;
    // cout << s << g <<tmp << endl;
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long lint;
typedef long long llint;

static const llint MAX = 2e5 + 5;
// static const int NIL = -1;
// static const ll INF = 1<<21;
static const llint MOD = 1e9 + 7;

bool compPair(const pair<int, int> &arg1, const pair<int, int> &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int main(void) {
  llint n, k;
  cin >> n >> k;

  llint ans = 0, tmp, s, g;
  for (llint in = k; in <= n + 1; in++) {
    s = 0;
    g = 0;
    s = (in) * (in - 1) / 2;
    g = n * in - (in) * (in - 1) / 2;
    g = min(n * (n + 1) / 2, g);
    tmp = g - s + 1;
    tmp %= MOD;
    ans += tmp;
    // cout << s << g <<tmp << endl;
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}
Problem p02708 (C++)

Buggy submission (status: Runtime Error):
// Winners don't quit and Quitters don't win
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define mod 1000000007
#define MAX 100005
ll sum[2 * MAX];
ll solve_() {
  ll i;
  for (i = 1; i < 2 * MAX - 2; ++i) {
    sum[i] = sum[i - 1] + i;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve_();
  ll n, k, i, ans = 1;
  cin >> n >> k;
  for (i = k; i <= n; ++i) {
    ans = (ans + (sum[n] - sum[n - i]) - sum[i - 1] + 1 + mod) % mod;
  }
  cout << ans % mod;
}

Fixed submission:
// Winners don't quit and Quitters don't win
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define mod 1000000007
#define MAX 100005
ll sum[2 * MAX];
void solve_() {
  ll i;
  for (i = 1; i < 2 * MAX - 2; ++i) {
    sum[i] = sum[i - 1] + i;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve_();
  ll n, k, i, ans = 1;
  cin >> n >> k;
  for (i = k; i <= n; ++i) {
    ans = (ans + (sum[n] - sum[n - i]) - sum[i - 1] + 1 + mod) % mod;
  }
  cout << ans % mod;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, K;
  cin >> N >> K;
  long long tempmin, tempmax;
  long long ans = 0;
  for (long long i = K; i <= N + 1; i++) {
    tempmin = 0;
    tempmax = 0;
    for (long long j = 0; j < i; j++) {
      tempmin = tempmin + j;
      tempmax = tempmax + (N - j);
    }
    // cout<<tempmin<<" "<<tempmax<<endl;
    ans = (ans + tempmax - tempmin + 1) % 1000000007;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, K;
  cin >> N >> K;
  long long tempmin, tempmax;
  long long ans = 0;
  for (long long i = K; i <= N + 1; i++) {
    tempmin = (i - 1) * i / 2;
    tempmax = (2 * N + 1 - i) * i / 2;
    // cout<<tempmin<<" "<<tempmax<<endl;
    ans = (ans + tempmax - tempmin + 1) % 1000000007;
  }
  cout << ans << endl;
  return 0;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main(void) {
  int N, K;
  long int sum = 0;
  long int mod = 1000000007;

  cin >> N >> K;

  for (int i = K; i <= N + 1; i++) {
    long int max = 0;
    long int min = 0;
    for (int j = 0; j < i; j++) {
      min += j;
      max += (N - j);
    }
    sum += (max - min + 1);
  }

  cout << sum % mod << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int main(void) {
  int N, K;
  long int sum = 0;
  long int mod = 1000000007;

  cin >> N >> K;

  long int max = 0;
  long int min = 0;
  for (int i = 0; i < K; i++) {
    min += i;
    max += (N - i);
  }
  sum += (max - min + 1);

  for (int i = K; i < N + 1; i++) {
    min += i;
    max += (N - i);
    sum += (max - min + 1);
  }

  cout << sum % mod << endl;

  return 0;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  long long c = 0;
  for (int kk = k; kk <= n + 1; kk++) {
    long long b = 1;
    for (int i = 0; i < kk; i++)
      b += (n - 2 * i) % (1000000007);
    c = (b + c) % (1000000007);
  }
  cout << c << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  long long c = 0;
  for (long long kk = k; kk <= n + 1; kk++) {
    c = (kk * (n + 1 - kk) + 1 + c) % (1000000007);
  }
  cout << c << endl;
  return 0;
}
Problem p02708 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef pair<ll, ll> pair_t;
using Graph = vector<vector<ll>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = LLONG_MAX;
/****** define constant end ********/
/****** define variable start ******/
ll n, k;
/******* define variable end *******/
int solve() {
  cin >> n >> k;
  vl ma(n);
  vl mi(n);
  ll ans = 0;
  for (ll i = k; i <= n + 1; i++) {
    mi[i] = i * (i - 1) / 2;
    ma[i] = n * (n + 1) / 2 - (n + 1 - i) * (n - i) / 2;
    ans += ma[i] - mi[i] + 1;
    ans %= MOD;
  }
  cout << ans % MOD << endl;
  return 0;
}

int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\n", time);
  */

  return end_solve;
}


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

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef pair<ll, ll> pair_t;
using Graph = vector<vector<ll>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = LLONG_MAX;
/****** define constant end ********/
/****** define variable start ******/
ll n, k;
/******* define variable end *******/
int solve() {
  cin >> n >> k;
  vl ma(n + 5);
  vl mi(n + 5);
  ll ans = 0;
  for (ll i = k; i <= n + 1; i++) {
    mi[i] = i * (i - 1) / 2;
    ma[i] = n * (n + 1) / 2 - (n + 1 - i) * (n - i) / 2;
    ans += ma[i] - mi[i] + 1;
    ans %= MOD;
  }
  cout << ans % MOD << endl;
  return 0;
}

int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\n", time);
  */

  return end_solve;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ll ans, n, k, i, a, b, j;
  cin >> n >> k;
  ans = 0;
  for (i = k; i <= n + 1; i++) {
    a = 0;
    b = 0;
    for (j = 0; j < i; j++) {
      a += j;
      b += n - j;
    }
    ans += b - a + 1;
    ans = ans % 1000000007;
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ll ans, n, k, i, a, b, j;
  cin >> n >> k;
  ans = 0;
  a = 0;
  b = 0;
  for (j = 0; j < k; j++) {
    a += j;
    b += n - j;
  }
  ans += b - a + 1;
  ans = ans % 1000000007;
  for (i = k; i <= n; i++) {
    a += i;
    b += n - i;
    ans += b - a + 1;
    ans = ans % 1000000007;
  }
  cout << ans;
}
Problem p02708 (C++)

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

using namespace std;

unsigned long long a(unsigned long long N, unsigned long long K) {
  return (N - K + 1) * K + 1;
}

unsigned long long solve(unsigned long long N, unsigned long long K) {
  unsigned long long ans = 0;
  for (int i = K; i <= N + 1; i++) {
    unsigned long long temp = a(N, i) % 1000000007;
    ans += temp;
    ans %= 1000000007;
    // if(temp<0){
    cerr << N << " " << K << " " << i << " " << ans << endl;
    // }
  }
  ans %= 1000000007;
  cout << ans << "\n";
  return ans;
}

// class input{
//     public:
//     input(int _A, int _B, int _C, int _X, int _ans){
//         A = _A;
//         B = _B;
//         C = _C;
//         X = _X;
//         ans = _ans;
//     }
//     int A;
//     int B;
//     int C;
//     int X;
//     int ans;
// };

// int solve(input key){
//     return solve(key.A, key.B, key.C, key.X);
// }

int main() {
  int N, K;
  bool use_stdin = true;
  // use_stdin = false;
  if (use_stdin) {
    cin >> N >> K;
    solve(N, K);
  } else {
    // input keys[] = {
    //     {30,40,50,6000,213},
    //     {5,1,0,150,0},
    //     {2,2,2,100,2}
    // };
    // for(auto key:keys){
    //     assert(solve(key)==key.ans);
    // }
  }
  return 0;
}

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

using namespace std;

unsigned long long a(unsigned long long N, unsigned long long K) {
  return (N - K + 1) * K + 1;
}

unsigned long long solve(unsigned long long N, unsigned long long K) {
  unsigned long long ans = 0;
  for (int i = K; i <= N + 1; i++) {
    unsigned long long temp = a(N, i) % 1000000007;
    ans += temp;
    ans %= 1000000007;
    // if(temp<0){
    // cerr << N << " " << K << " " << i << " " << ans << endl;
    // }
  }
  ans %= 1000000007;
  cout << ans << "\n";
  return ans;
}

// class input{
//     public:
//     input(int _A, int _B, int _C, int _X, int _ans){
//         A = _A;
//         B = _B;
//         C = _C;
//         X = _X;
//         ans = _ans;
//     }
//     int A;
//     int B;
//     int C;
//     int X;
//     int ans;
// };

// int solve(input key){
//     return solve(key.A, key.B, key.C, key.X);
// }

int main() {
  int N, K;
  bool use_stdin = true;
  // use_stdin = false;
  if (use_stdin) {
    cin >> N >> K;
    solve(N, K);
  } else {
    // input keys[] = {
    //     {30,40,50,6000,213},
    //     {5,1,0,150,0},
    //     {2,2,2,100,2}
    // };
    // for(auto key:keys){
    //     assert(solve(key)==key.ans);
    // }
  }
  return 0;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort(ALL((c)))
#define debug(x)                                                               \
  do {                                                                         \
    std::cout << #x << ": ";                                                   \
    view(x);                                                                   \
  } while (0)

template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << ", ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  ll ans = 0;
  for (ll i = K; i <= (N + 1); i++) {
    ll mi = i * (i - 1) / 2;
    ll ma = 0;
    rep(j, i) { ma += N - j; }
    ans += (ma - mi + 1) % (1000000000 + 7);
  }
  cout << ans % (1000000000 + 7) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort(ALL((c)))
#define debug(x)                                                               \
  do {                                                                         \
    std::cout << #x << ": ";                                                   \
    view(x);                                                                   \
  } while (0)

template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << ", ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  ll ans = 0;
  for (ll i = K; i <= (N + 1); i++) {
    ll mi = i * (i - 1) / 2;
    ll ma = 0;
    rep(j, i) { ma += N - j; }
    ma = i * (2 * (N - i + 1) + (i - 1)) / 2;
    ans += (ma - mi + 1) % (1000000000 + 7);
  }
  cout << ans % (1000000000 + 7) << endl;
  return 0;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {

  const ll mod = 1000000007;
  ll n, k;
  cin >> n >> k;
  ll ans = 0;
  for (ll i = k; i <= n + 1; i++) {
    ll tmpLeft = 0;
    for (ll j = 0; j < i; j++) {
      tmpLeft += j;
    }
    ll tmpRight = 0;
    for (ll j = n - i + 1; j <= n; j++) {
      tmpRight += j;
    }
    ans += tmpRight - tmpLeft + 1;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {

  const ll mod = 1000000007;
  ll n, k;
  cin >> n >> k;
  ll ans = 0;
  for (ll i = k; i <= n + 1; i++) {
    ll tmpLeft = (i - 1) * i / 2;
    ll tmpRight = n * (n + 1) / 2 - (n - i) * (n - i + 1) / 2;
    ans += tmpRight - tmpLeft + 1;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int

ll sum(ll a, ll b) {
  ll count = 0;
  for (ll i = a; i <= b; i++)
    count += i;
  return count;
}
int main(void) {
  ll N = 0, M = 0;
  cin >> N >> M;
  ll s = 0;

  for (ll i = M; i <= N + 1; i++) {
    s += (sum(N + 1 - i, N) - sum(0, i - 1) + 1);
  }

  cout << s % 1000000007 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int

ll sum(ll a, ll b) {
  ll count = 0;
  count = (b - a + 1) * (a + b) / 2;
  return count;
}
int main(void) {
  ll N = 0, M = 0;
  cin >> N >> M;
  ll s = 0;

  for (ll i = M; i <= N + 1; i++) {
    s += (sum(N + 1 - i, N) - sum(0, i - 1) + 1);
  }

  cout << s % 1000000007 << endl;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
const int INF = 0x7fffffff;
const int MOD = 1000000007;
using lll = long long;
using ull = unsigned long long;
using namespace std;

int main() {
  lll ii, jj, kk;
  vector<int> ret;
  int n, k;

  cin >> n >> k;

  lll minNum, maxNum;
  lll sum = 0;
  lll cnt;
  for (ii = k; ii <= n + 1; ii++) {

    minNum = 0;
    maxNum = 0;

    for (jj = 0, cnt = 0; jj < ii; jj++, cnt++) {
      minNum += cnt;
      maxNum += (n - cnt);
    }

    // cout << "maxNum:" << maxNum << "minNum:" << minNum << endl;

    sum = (sum + maxNum - minNum + 1) % MOD;
  }

  cout << sum << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
const int INF = 0x7fffffff;
const int MOD = 1000000007;
using lll = long long;
using ull = unsigned long long;
using namespace std;

int main() {
  lll ii, jj, kk;
  vector<int> ret;
  int n, k;

  cin >> n >> k;

  lll minNum, maxNum;
  lll sum = 0;
  lll cnt;
  for (ii = k; ii <= n + 1; ii++) {

    minNum = 0;
    maxNum = 0;

    // for(jj=0,cnt=0;jj<ii;jj++,cnt++){
    //   minNum += cnt;
    //   maxNum += (n-cnt);
    // }
    minNum = (ii * (ii - 1)) / 2;
    maxNum = n * ii - minNum;
    // cout << "maxNum:" << maxNum << "minNum:" << minNum << endl;

    sum = (sum + maxNum - minNum + 1) % MOD;
  }

  cout << sum << endl;

  return 0;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end());
#define vecmin(A) *min_element(A.begin(), A.end());
#define vecmax(A) *max_element(A.begin(), A.end());
typedef long long ll;

ll sum(ll n) {
  ll ans = 0;
  for (ll i = 1; i <= n; i++)
    ans += i;
  return ans;
}

ll f(ll n, ll k) {
  ll left = sum(n) - sum(n - k);
  ll right = sum(k - 1);
  return left - right + 1;
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll ans = 0;
  for (ll i = k; i <= n + 1; i++) {
    ans += f(n, i);
    ans %= 1000000007;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end());
#define vecmin(A) *min_element(A.begin(), A.end());
#define vecmax(A) *max_element(A.begin(), A.end());
typedef long long ll;

ll f(ll n, ll k) { return -k * k + k + n * k + 1; }

int main() {
  ll n, k;
  cin >> n >> k;
  ll ans = 0;
  for (ll i = k; i <= n + 1; i++) {
    ans += f(n, i);
    ans %= 1000000007;
  }
  cout << ans << endl;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int main() {
  ll n, k;
  cin >> n >> k;
  ll i, ans = 0, j, mod = 1e9 + 7;
  for (i = k; i <= n + 1; i++) {
    ll sum = 0, sum1 = 0;
    for (j = 0; j < i; j++) {
      sum += j;
      sum1 += n - j;
    }
    ans += sum1 - sum + 1;
    ans %= mod;
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int main() {
  ll n, k;
  cin >> n >> k;
  ll i, ans = 0, j, mod = 1e9 + 7;
  for (i = k; i <= n + 1; i++) {
    ll sum = 0, sum1 = 0;
    sum = i * (i - 1) / 2;
    sum1 = i * (n + n - i + 1) / 2;
    ans += sum1 - sum + 1;
    ans %= mod;
  }
  cout << ans;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
#define int ll
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MOD = 1e9 + 7;

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

  int n, k;
  cin >> n >> k;
  int ans = 0;
  for (int i = k; i <= n + 1; i++) {
    int minu = 0;
    for (int j = 0; j < i; j++)
      minu += j;
    int mxnu = 0;
    for (int j = 0; j < i; j++)
      mxnu += (n - j);
    ans += (mxnu - minu + 1) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
#define int ll
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MOD = 1e9 + 7;

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

  int n, k;
  cin >> n >> k;
  int ans = 0;
  for (int i = k; i <= n + 1; i++) {
    int minu = i * (i - 1) / 2;
    // int minu = 0; for(int j=0;j<i;j++) minu += j;
    int mxnu = i * (2 * n - i + 1) / 2;
    // int mxnu = 0; for(int j=0;j<i;j++) mxnu += (n-j);
    ans += (mxnu - minu + 1) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;

  return 0;
}
Problem p02708 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {

  long N;
  long K;
  cin >> N >> K;

  long ans = 0;

  vector<long> bb(N + 1);
  vector<long> aa(N + 1);
  aa.at(0) = 0;
  bb.at(0) = 0;
  for (int i = 1; i < N + 2; i++) {
    bb.at(i) = bb.at(i - 1) + (i - 1);
  }

  for (int i = 1; i < N + 2; i++) {
    aa.at(i) = aa.at(i - 1) + N - (i - 1);
  }
  for (int j = K; j < N + 2; j++) {

    ans = ans + 1 + aa.at(j) - bb.at(j);

    ans = ans % 1000000007;
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {

  long N;
  long K;
  cin >> N >> K;

  long ans = 0;

  vector<long> bb(N + 2);
  vector<long> aa(N + 2);
  aa.at(0) = 0;
  bb.at(0) = 0;
  for (int i = 1; i < N + 2; i++) {
    bb.at(i) = bb.at(i - 1) + (i - 1);
  }

  for (int i = 1; i < N + 2; i++) {
    aa.at(i) = aa.at(i - 1) + N - (i - 1);
  }
  for (int j = K; j < N + 2; j++) {

    ans = ans + 1 + aa.at(j) - bb.at(j);

    ans = ans % 1000000007;
  }

  cout << ans << endl;
  return 0;
}
Problem p02708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>

#define rep(i, m, n) for (int i = (m); i < (n); ++i)
#define rem(i, m, n) for (int i = (m); i > (n); --i)
#define MOD 1000000007

using namespace std;

using ll = long long;
using std::swap;

// vector<long long int> v;
vector<long long int> w;

int main() {
  ll N, K, MA, MI;
  ll ANS = 0;
  cin >> N >> K;
  rep(i, K, N + 2) {
    MA = 0;
    MI = 0;
    rep(j, 0, i) MI += j;
    rem(j, N, N - i) MA += j;
    // MI = (0 + (i - 1))*i / 2;
    // MA = (N + (N - i + 1))*i / 2;
    // cout << MI << " " << MA << '\n';
    ANS += (MA - MI + 1) % MOD;
    ANS %= MOD;
  }
  ANS %= MOD;
  cout << ANS << '\n';

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>

#define rep(i, m, n) for (int i = (m); i < (n); ++i)
#define rem(i, m, n) for (int i = (m); i > (n); --i)
#define MOD 1000000007

using namespace std;

using ll = long long;
using std::swap;

// vector<long long int> v;
vector<long long int> w;

int main() {
  ll N, K, MA, MI;
  ll ANS = 0;
  cin >> N >> K;
  rep(i, K, N + 2) {
    MA = 0;
    MI = 0;
    MI = (0 + (i - 1)) * ll(i) / 2;
    MA = (N + (N - i + 1)) * ll(i) / 2;
    ANS += (MA - MI + 1) % MOD;
    ANS %= MOD;
  }
  ANS %= MOD;
  cout << ANS << '\n';

  return 0;
}
Problem p02709 (C++)

Buggy submission (status: Time Limit Exceeded):
// Sakhiya07 - Yagnik Sakhiya

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(), x.end()

#define mp make_pair
#define bp __builtin_popcountll
#define MOD 1000000007
const int N = 2005;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \
  srand(time(NULL));
pair<ll, ll> p[N + 1];
ll dp[N][N];
ll solve1(ll id, ll first, ll last) {
  if (id == 0)
    return 0;
  ll crt = p[id].ss;
  ll dif1 = abs(crt - first);
  ll dif2 = abs(crt - last);
  ll ans = dp[id][first];
  //  if(ans!=-1)   return ans;
  ans = max((solve1(id - 1, first, last - 1) + p[id].ff * dif2),
            (solve1(id - 1, first + 1, last) + p[id].ff * dif1));
  return dp[id][first] = ans;
}
void solve() {
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> p[i].ff;
    p[i].ss = i;
  }
  sort(p + 1, p + n + 1);
  set<ll> s;
  memset(dp, -1, sizeof(dp));
  ll ans = solve1(n, 1, n);
  cout << ans;
}

int main() {
  FAST;
  ll t = 1;
  //  cin >> t;
  while (t--) {
    solve();
  }
}

Fixed submission:
// Sakhiya07 - Yagnik Sakhiya

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(), x.end()

#define mp make_pair
#define bp __builtin_popcountll
#define MOD 1000000007
const int N = 2005;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \
  srand(time(NULL));
pair<ll, ll> p[N + 1];
ll dp[N][N];
ll solve1(ll id, ll first, ll last) {
  if (id == 0)
    return 0;
  ll crt = p[id].ss;
  ll dif1 = abs(crt - first);
  ll dif2 = abs(crt - last);
  ll ans = dp[id][first];
  if (ans != -1)
    return ans;
  ans = max((solve1(id - 1, first, last - 1) + p[id].ff * dif2),
            (solve1(id - 1, first + 1, last) + p[id].ff * dif1));
  return dp[id][first] = ans;
}
void solve() {
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> p[i].ff;
    p[i].ss = i;
  }
  sort(p + 1, p + n + 1);
  set<ll> s;
  memset(dp, -1, sizeof(dp));
  ll ans = solve1(n, 1, n);
  cout << ans;
}

int main() {
  FAST;
  ll t = 1;
  //  cin >> t;
  while (t--) {
    solve();
  }
}
Problem p02709 (Python)

Buggy submission (status: Time Limit Exceeded):
# 解説AC
n = int(input())
A = list(map(int, input().split()))
B = []
for i, e in enumerate(A):
    B.append((e, i + 1))
B.sort(reverse=True)
# dp[i][j]: Aiまで入れた時、左にj個決めた時の最大値
dp = [[-1] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 0

for i in range(n):
    for j in range(i + 1):  # 左の個数
        k = i - j  # 右の個数
        ni = i + 1
        val, idx = B[i]
        dp[ni][j] = max(dp[ni][j], dp[i][j] + abs(n - k - idx) * val)
        dp[ni][j + 1] = max(dp[ni][j + 1], dp[i][j] + abs(idx - (j + 1)) * val)
ans = 0
for i in range(n + 1):
    ans = max(ans, dp[n][i])
print(ans)


Fixed submission:
# 解説AC
import sys

input = sys.stdin.buffer.readline

n = int(input())
A = list(map(int, input().split()))
B = []
for i, e in enumerate(A):
    B.append((e, i + 1))
B.sort(reverse=True)
# dp[i][j]: Aiまで入れた時、左にj個決めた時の最大値
dp = [[-1] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 0

for i in range(n):
    for j in range(i + 1):  # 左の個数
        k = i - j  # 右の個数
        ni = i + 1
        val, idx = B[i]
        dp[ni][j] = max(dp[ni][j], dp[i][j] + abs(n - k - idx) * val)
        dp[ni][j + 1] = max(dp[ni][j + 1], dp[i][j] + abs(idx - (j + 1)) * val)
ans = 0
for i in range(n + 1):
    ans = max(ans, dp[n][i])
print(ans)
Problem p02709 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x7fffffff
#define INFL 0x7fffffffffffffffLL

vector<pair<ll, ll>> a(2000);
ll n;
ll dp[2000][2000];

ll calc(ll x, ll y) {
  int idx = x + ((n - 1) - y);
  if (idx == n)
    return 0;
  // if (dp[x][y]!=0) return dp[x][y];
  ll ans = max(abs(a[idx].second - x) * a[idx].first + calc(x + 1, y),
               abs(a[idx].second - y) * a[idx].first + calc(x, y - 1));
  dp[x][y] = ans;
  return ans;
}

int main() {
  ll b, c, i, j, k, m, x, y, ans = 0;
  string str;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> j;
    a[i] = make_pair(j, i);
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());

  x = 0;
  y = n - 1;

  ans = calc(x, y);

  cout << ans << endl;
}


Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x7fffffff
#define INFL 0x7fffffffffffffffLL

vector<pair<ll, ll>> a(2000);
ll n;
ll dp[2000][2000];

ll calc(ll x, ll y) {
  int idx = x + ((n - 1) - y);
  if (idx == n)
    return 0;
  if (dp[x][y] != 0)
    return dp[x][y];
  ll ans = max(abs(a[idx].second - x) * a[idx].first + calc(x + 1, y),
               abs(a[idx].second - y) * a[idx].first + calc(x, y - 1));
  dp[x][y] = ans;
  return ans;
}

int main() {
  ll b, c, i, j, k, m, x, y, ans = 0;
  string str;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> j;
    a[i] = make_pair(j, i);
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());

  x = 0;
  y = n - 1;

  ans = calc(x, y);

  cout << ans << endl;
}
Problem p02709 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REP2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1, )(__VA_ARGS__)
#define MOD 1'000'000'007

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (size_t i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 == v.size() ? "" : ", ");
  os << "}";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}

long long dp[2222][2222];

int main() {
  int N;
  cin >> N;
  vector<pair<long long, long long>> A(N);
  REP(i, N) {
    long long a;
    cin >> a;
    A[i] = make_pair(a, i);
  }
  sort(A.begin(), A.end(), greater<pair<long long, long long>>());

  vector<vector<long long>> dp(2222, vector<long long>(2222, -(1LL << 60)));
  dp[0][N] = 0;

  for (int i = 0; i < N; i++) {
    long long x = A[i].first;
    long long pos = A[i].second;
    for (int l = 0; l < N; l++) {
      int r = l + N - i;
      dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + x * abs(pos - l));
      dp[l][r - 1] = max(dp[l][r - 1], dp[l][r] + x * abs(pos - (r - 1)));
    }
  }
  long long ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, dp[i][i]);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REP2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1, )(__VA_ARGS__)
#define MOD 1'000'000'007

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (size_t i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 == v.size() ? "" : ", ");
  os << "}";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}

long long dp[2222][2222];

int main() {
  int N;
  cin >> N;
  vector<pair<long long, long long>> A(N);
  REP(i, N) {
    long long a;
    cin >> a;
    A[i] = make_pair(a, i);
  }
  sort(A.begin(), A.end(), greater<pair<long long, long long>>());

  vector<vector<long long>> dp(2222, vector<long long>(2222, -(1LL << 60)));
  dp[0][N] = 0;

  for (int i = 0; i < N; i++) {
    long long x = A[i].first;
    long long pos = A[i].second;
    for (int l = 0; l <= i; l++) {
      int r = l + N - i;
      dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + x * abs(pos - l));
      dp[l][r - 1] = max(dp[l][r - 1], dp[l][r] + x * abs(pos - (r - 1)));
    }
  }
  long long ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, dp[i][i]);
  cout << ans << endl;
  return 0;
}