alexjercan/bugnet huggingface dataset:
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
LL add(LL s, LL e, LL n) // zhe'li
{
  return (s + e) * n / 2;
}
int main() {
  int n, k;
  cin >> n >> k;
  LL ans = 1;
  for (int i = k; i <= n; i++) {
    ans = (ans + add(n - i + 1, n, i) - add(0, i - 1, i) + 1) % mod;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
typedef long long LL;
int n, k;
const int mod = 1e9 + 7;
const int N = 2e5 + 7;
map<int, int> mp;
int vis[N];
int tmp[N];
LL sum = 0;
int ans = 0;
void dfs(int num) {
  if (num == k + 1) {
    if (mp[sum] == 0) {
      ans++;
      ans %= mod;
    }
    mp[sum]++;
    return;
  }
  for (int i = 1; i <= n + 1; i++) {
    if (!mp[i]) {
      if (i > tmp[num - 1] - 10) {
        tmp[num] = i + 10;
        vis[i] = 1;
        sum += (i + 10);
        sum %= mod;
        dfs(num + 1);
        vis[i] = 0;
        sum -= (i + 10);
        sum %= mod;
      }
    }
  }
}
int main() {
  cin >> n >> k;
  while (k <= n + 1) {
    dfs(1);
    k++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll myco(ll n, ll k) {
  ll a = 0, b = 0;
  /*for(ll i = 0; i < k; i++) {
    a += i;
    b += n - i;
  }*/
  b = (2 * n - k + 1) * k / 2;
  a = k * (k - 1) / 2;
  return (b - a + 1);
}

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


Error: TLE

Fixed submission:

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

ll myco(ll n, ll k) {
  ll a = 0, b = 0;
  for (ll i = 0; i < k; i++) {
    a += i;
    b += n - i;
  }
  return (b - a + 1);
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll z = 0;
  for (ll i = k; i <= n + 1; i++) {
    z += myco(n, i);
    z %= 1000000007;
  }
  cout << z << endl;
}
Problem p02708 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
#define vec(s) vector
;
#define vvec(s) vector<vector
>;
typedef vector<lli> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define YN(x) cout << (bool x ? "Yes" : "No") << endl;
#define out(s) cout << s << endl;
#define pb(s) push_back(s);
#define sp " ";
#define INF 10000000000
#define all(s) s.begin(), s.end()
void vout(vector<int> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v.at(i) << endl;
}
int main() {
  lli mod = 1000000007;
  lli ans = 0;
  lli n, k;
  cin >> n >> k;
  vi sum(n + 1);

  sum[0] = 0;
  rep(i, n) { sum[i + 1] = sum[i] + i + 1; }
  for (int j = k; j <= n; j++) {
    ans += (sum[n] - sum[n - j]) - sum[j - 1] + 1;
    ans %= mod;
  }
  ans += 1;
  ans %= mod;
  out(ans);
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
#define vec(s) vector
;
#define vvec(s) vector<vector
>;
typedef vector<lli> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define YN(x) cout << (bool x ? "Yes" : "No") << endl;
#define out(s) cout << s << endl;
#define pb(s) push_back(s);
#define sp " ";
#define INF 10000000000
#define all(s) s.begin(), s.end()
void vout(vector<int> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v.at(i) << endl;
}
int main() {
  lli mod = 1000000007;
  lli ans = 0;
  lli n, k;
  vi sum(n + 1);
  cin >> n >> k;
  sum[0] = 0;
  rep(i, n) { sum[i + 1] = sum[i] + i + 1; }
  for (int j = k; j <= n; j++) {
    ans += (sum[n] - sum[n - j]) - sum[j - 1] + 1;
    ans %= mod;
  }
  ans += 1;
  ans %= mod;
  out(ans);
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  long long int n, k;
  long long int ans = 0;
  cin >> n >> k;
  for (long long int u = k; u < n + 2; ++u) {
    long long int min = 0, max = 0;
    min = (u - 1) * u / 2;
    max = (2 * n - u + 1) * u / 2;
    ans = (ans + max - min + 1) % 1000000007;
  }

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, k;
  long int ans = 0;
  cin >> n >> k;
  for (int u = k; u < n + 2; u++) {
    long int min = 0, max = 0;
    for (int i = 0; i < u; i++) {
      min += i;
      max += n - i;
    }
    ans += max - min + 1;
  }

  cout << ans % 1000000007 << endl;
  return 0;
}
Problem p02708 (C++) — buggy submission, verdict: 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 = (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;
}


Error: TLE

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 = 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;
}
Problem p02708 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define lson node << 1, st, mid
#define rson node << 1 | 1, mid + 1, ed
#define mem(a, x) memset(a, x, sizeof(a))
#define me(a) memset(a, 0, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) x & (-x)
#define up(i, x, y) for (long long i = x; i < y; i++)
#define down(i, x, y) for (long long i = x; i >= y; i--)
#define in freopen("in.txt", "r", stdin)
#define out freopen("out.txt", "w", stdout)
typedef long long ll;
const ll mod = 1e9 + 7;
const ll INF = 0x3f3f3f3f;
const ll maxn = 1e6 + 5;
const ll N = 1e3 + 5;
const double pi = acos(-1.0);
const double eps = 1e-9;
using namespace std;

ll a[maxn];
int main() {
  // in;
  // out;
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i <= n; i++) {
    a[i] = i;
  }
  ll sum = 0;
  for (ll i = m; i <= n + 1; i++) {
    ll minn = (i - 1) * i / 2;
    ll maxx = (n) * (n + 1) / 2;
    ll hh = (n - i) * (n - i + 1) / 2;
    if (n - i + 1 < 0)
      hh = 0;
    maxx -= hh;
    // cout<<minn<<" "<<maxx<<endl;
    // cout<<maxx-minn+1<<endl;
    sum += maxx - minn + 1;
    sum %= mod;
  }
  cout << sum << endl;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define lson node << 1, st, mid
#define rson node << 1 | 1, mid + 1, ed
#define mem(a, x) memset(a, x, sizeof(a))
#define me(a) memset(a, 0, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) x & (-x)
#define up(i, x, y) for (long long i = x; i < y; i++)
#define down(i, x, y) for (long long i = x; i >= y; i--)
#define in freopen("in.txt", "r", stdin)
#define out freopen("out.txt", "w", stdout)
typedef long long ll;
const ll mod = 1e9 + 7;
const ll INF = 0x3f3f3f3f;
const ll maxn = 1e6 + 5;
const ll N = 1e3 + 5;
const double pi = acos(-1.0);
const double eps = 1e-9;
using namespace std;

ll a[maxn];
int main() {
  in;
  out;
  ll n, m;
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    a[i] = i;
  }
  ll sum = 0;
  for (int i = m; i <= n + 1; i++) {
    ll minn = (i - 1) * i / 2;
    ll maxx = (n) * (n + 1) / 2;
    ll hh = (n - m) * (n - m + 1) / 2;
    // cout<<hh<<endl;

    // cout<<minn<<" "<<maxx<<endl;
    sum += maxx - minn - hh;
  }
  cout << sum << endl;
}
Problem p02708 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

const int MOD = 1000000007;

int main() {
  std::size_t n, k;
  std::cin >> n >> k;

  std::size_t result = 0;
  for (std::size_t i = k; i <= n + 1; ++i) {
    result = (result + i * (n - i + 1) + 1) % MOD;
  }
  std::cout << result << std::endl;
}


Error: -8

Fixed submission:

#include <algorithm>
#include <iostream>

std::size_t binomial_coeff(const std::size_t n, const std::size_t r) {
  std::size_t result = 1;
  std::size_t denom = 1;
  for (std::size_t i = 0; i < r; ++i) {
    result *= n - i;
    denom *= (i + 1);
  }
  return result / denom;
}

int main() {
  std::size_t n, k;
  std::cin >> n >> k;

  std::size_t result = 0;
  for (std::size_t i = 0; i < k - n; ++i) {
    result += binomial_coeff(n, std::min(i, n - i));
  }
  std::cout << result << std::endl;
}
Problem p02708 (C++) — buggy submission, verdict: Runtime Error

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

#define db(x) cerr << #x << " == " << x << endl
#define dbs(x) cerr << x << endl
#define _ << ", " <<
#define pb push_back
#define ar array

typedef long long ll;
typedef long double ld;
// using ll = long long;

typedef vector<int> vi;

const ld EPS = 1e-9, PI = acos(-1.);
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;

ll summ[200007];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, k;
  cin >> n >> k;
  summ[0] = 0;
  for (int i = 1; i <= 200001; ++i) {
    summ[i] = summ[i - 1] + i;
  }

  ll ans = 0;
  for (int i = k; i <= (n + 1); ++i) {
    ll minsum = summ[i - 1];
    ll maxsum = summ[n] - summ[n - i];
    ans += (maxsum - minsum + 1);
    ans %= MOD;
  }
  cout << ans % MOD << endl;
}


Error: 0

Fixed submission:

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

#define db(x) cerr << #x << " == " << x << endl
#define dbs(x) cerr << x << endl
#define _ << ", " <<
#define pb push_back
#define ar array

typedef long long ll;
typedef long double ld;
// using ll = long long;

typedef vector<int> vi;

const ld EPS = 1e-9, PI = acos(-1.);
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;

ll combi(int n, int k) {
  ll ans = 1;
  k = k > n - k ? n - k : k;
  int j = 1;
  for (; j <= k; j++, n--) {
    if (n % j == 0) {
      ans *= n / j;
    } else if (ans % j == 0) {
      ans = ans / j * n;
    } else {
      ans = (ans * n) / j;
    }
  }
  return ans % MOD;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, k;
  cin >> n >> k;
  if (k >= n + 1) {
    return 1;
  }
  ll ans = 0;
  for (int i = k; i < n + 1; i++) {
    ans = (ans + combi(n + 1, i)) % MOD;
  }
  cout << ans << "\\n";
}
Problem p02708 (C++) — buggy submission, verdict: 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;
  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;
}

Error: TLE

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;
  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;
}
Problem p02708 (C++) — buggy submission, verdict: 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;

  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;
}


Error: TLE

Fixed submission:

#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;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const int mod = 1000000007;

int main() {
  int n, k;
  cin >> n >> k;
  ll ans = 0;
  for (ll i = k; i <= n + 1; i++) {
    ll small = i * (i - 1) / 2;
    ll large = (2 * n - i + 1) * i / 2;
    ll res = large - small + 1;
    ans = (ans + res) % mod;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const int mod = 1000000007;

int n;

ll calc(int x) { return (n - x + 1) * x + 1; }

int main() {
  int k;
  cin >> n >> k;
  ll ans = 0;
  for (int i = k; i <= n + 1; k++) {
    ans += calc(i);
    ans %= mod;
  }
  cout << ans << endl;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

//
#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define f first
#define s second

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int MOD = 1000000007;
const int INF = 1000000007;

ll dp[2002][2002];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  pll arr[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> arr[i].f;
    arr[i].s = i;
  }

  sort(arr + 1, arr + n + 1);
  ll ans = 0;
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= n + 1 - i; j++) {
      dp[j - 1][i + j - 1] =
          max(dp[j - 1][i + j - 1],
              dp[j - 1][i + j] + abs(arr[i].s - (i + j - 1)) * arr[i].f);
      dp[j][i + j] =
          max(dp[j][i + j], dp[j - 1][i + j] + abs(arr[i].s - j) * arr[i].f);
      // cout << arr[i].f << " " << dp[j][i+j] << "\\n";
      ans = max(ans, max(dp[j - 1][i + j - 1], dp[j][i + j]));
    }
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

//
#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define f first
#define s second

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int MOD = 1000000007;
const int INF = 1000000007;

ll dp[2002][2002];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  pll arr[n];
  for (int i = 1; i <= n; i++) {
    cin >> arr[i].f;
    arr[i].s = i;
  }

  sort(arr + 1, arr + n + 1);
  ll ans = 0;
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= n + 1 - i; j++) {
      dp[j - 1][i + j - 1] =
          max(dp[j - 1][i + j - 1],
              dp[j - 1][i + j] + abs(arr[i].s - (i + j - 1)) * arr[i].f);
      dp[j][i + j] =
          max(dp[j][i + j], dp[j - 1][i + j] + abs(arr[i].s - j) * arr[i].f);
      // cout << arr[i].f << " " << dp[j][i+j] << "\\n";
      ans = max(ans, dp[j][i + j]);
    }
  }
  cout << ans;
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int somod = 1e9 + 7;
const int maxn = 2e3 + 7;

int n, m, a[maxn], dp[maxn][maxn], ans;

void Maximize(int &a, int b) {
  if (a < b)
    a = b;
  ans = max(ans, a);
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen("test.inp", "r"))
    freopen("test.inp", "r", stdin);
  cin >> n;
  vector<pair<int, int>> vt;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    vt.emplace_back(a[i], i);
  }
  sort(vt.rbegin(), vt.rend());
  for (int i = 0; i < n; i++) {
    for (int le = 0; le <= i + 1; le++) {
      int ri = i + 1 - le;
      int curri = n - ri + 1;
      if (le) {
        Maximize(dp[le][ri],
                 dp[le - 1][ri] + vt[i].first * abs(vt[i].second - le));
      }
      if (ri) {
        Maximize(dp[le][ri],
                 dp[le][ri - 1] + vt[i].first * abs(vt[i].second - curri));
      }
    }
  }
  cout << ans << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long

using namespace std;
const long double PI = acos(-1);
const int somod = 1e9 + 7;
const int maxn = 1e4 + 7;

int n, m, a[maxn];
vector<int> vt[maxn];

struct Edge {
  int u, v, cap, cost;
};

struct MCMF {
  int trace[maxn], inq[maxn], s, t, dist[maxn], ans;
  vector<Edge> e;
  vector<int> vt[maxn];
  void AddEdge(int u, int v, int cap, int cost) {
    vt[u].push_back(e.size());
    e.push_back({u, v, cap, cost});
    vt[v].push_back(e.size());
    e.push_back({v, u, 0, -cost});
  }
  bool SPFA() {
    for (int i = s; i <= t; i++)
      trace[i] = -1, dist[i] = 1e18, inq[i] = 0;
    queue<int> q;
    q.push(s);
    dist[s] = 0;
    while (!q.empty()) {
      int gau = q.front();
      q.pop();
      inq[gau] = false;
      for (auto i : vt[gau]) {
        if (e[i].cap > 0 && dist[e[i].v] > dist[gau] + e[i].cost) {
          dist[e[i].v] = dist[gau] + e[i].cost;
          trace[e[i].v] = i;
          if (!inq[e[i].v]) {
            inq[e[i].v] = 1;
            q.push(e[i].v);
          }
        }
      }
    }
    return (trace[t] != -1);
  }
  void Update() {
    int Min = 1e18, cc = t;
    while (cc != s) {
      int x = trace[cc];
      Min = min(Min, e[x].cap);
      cc = e[x].u;
    }
    cc = t;
    while (cc != s) {
      int x = trace[cc];
      e[x].cap -= Min;
      e[x ^ 1].cap += Min;
      ans += Min * e[x].cost;
      cc = e[x].u;
    }
  }
  int FindMCMF() {
    while (SPFA()) {
      Update();
    }
    return ans;
  }
} flow;

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen("test.inp", "r"))
    freopen("test.inp", "r", stdin);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  flow.s = 0;
  flow.t = n + n + 1;
  for (int i = 1; i <= n; i++) {
    flow.AddEdge(0, i, 1, 0);
    flow.AddEdge(n + i, n + n + 1, 1, 0);
    for (int j = 1; j <= n; j++) {
      flow.AddEdge(i, n + j, 1, -a[i] * abs(i - j));
    }
  }
  cout << -flow.FindMCMF();
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stdio.h>
using namespace std;

const double pi = acos(-1);

typedef long long ll;
const int maxn = 2005;
struct pp {
  ll v;
  int id;
  bool operator<(const pp &o) const {
    if (v != o.v)
      return v > o.v;
    return id < o.id;
  }
};
pp A[maxn];
int mark[maxn];
ll dp[maxn][maxn];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
#ifdef acm
  freopen("a.in", "r", stdin);
#endif // acm

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++)
    cin >> A[i].v, A[i].id = i;

  sort(A + 1, A + n + 1);

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      dp[i][j] = -1;

  dp[0][0] = 0;

  ll sol = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i + j > n)
        continue;
      if (dp[i][j] == -1)
        continue;
      int t = i + j + 1;
      int ini = i + 1;
      int fin = n - j;
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + abs(A[t].id - ini) * A[t].v);
      dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + abs(A[t].id - fin) * A[t].v);
    }
  }

  for (int i = 0; i <= n; i++)
    sol = max(sol, dp[i][n - i]);

  cout << sol << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stdio.h>
using namespace std;

const double pi = acos(-1);

typedef long long ll;
const int maxn = 2005;
struct pp {
  ll v;
  int id;
  bool operator<(const pp &o) const {
    if (v != o.v)
      return v > o.v;
    return id < o.id;
  }
};
pp A[maxn];
int mark[maxn];
ll dp[maxn][maxn];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
#ifdef acm
  freopen("a.in", "r", stdin);
#endif // acm

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++)
    cin >> A[i].v, A[i].id = i;

  sort(A + 1, A + n + 1);

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      dp[i][j] = -1;

  dp[0][0] = 0;

  ll sol = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (dp[i][j] == -1)
        continue;
      int t = i + j + 1;
      int ini = i + 1;
      int fin = n - j;
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + abs(A[t].id - ini) * A[t].v);
      dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + abs(A[t].id - fin) * A[t].v);
    }
  }

  for (int i = 0; i <= n; i++)
    sol = max(sol, dp[i][n - i]);

  cout << sol << "\\n";

  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node {
  ll p;
  ll v;
} a[2010];

ll dp[2010][2010]; // dp[i][j] i表示移左边数量 j表示移右边数量

bool cmp(node x, node y) {
  if (x.v == y.v)
    return x.p < y.p;
  return x.v > y.v;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].v;
    a[i].p = i;
  }
  sort(a, a + n, cmp);

  for (int i = 0; i < n; i++) {
    for (int j = 0; i + j < n; j++) {
      int t = i + j;
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + abs(a[t].p - i) * a[t].v);
      dp[i][j + 1] =
          max(dp[i][j + 1], dp[i][j] + abs(a[t].p - (n - 1 - j)) * a[t].v);
    }
  }

  ll ans = 0;
  for (int i = 0; i <= n; i++)
    ans = max(ans, dp[i][n - i]);
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node {
  int p;
  int v;
} a[2010];

ll dp[2010][2010]; // dp[i][j] i表示移左边数量 j表示移右边数量

bool cmp(node x, node y) {
  if (x.v == y.v)
    return x.p < y.p;
  return x.v > y.v;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].v;
    a[i].p = i;
  }
  sort(a, a + n, cmp);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int t = i + j;
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + abs(a[t].p - i) * a[t].v);
      dp[i][j + 1] =
          max(dp[i][j + 1], dp[i][j] + abs(a[t].p - (n - 1 - j)) * a[t].v);
    }
  }

  ll ans = 0;
  for (int i = 0; i <= n; i++)
    ans = max(ans, dp[i][n - i]);
  cout << ans;
  return 0;
}