alexjercan/bugnet huggingface dataset:
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 2147483647;
const ll LINF = 9223372036854775807;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int main() {
  ll n;
  cin >> n;
  vector<P> a(n);
  REP(i, n) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(ALL(a), greater<P>());
  ll dp[2020][2020];
  dp[0][0] = 0;
  dp[0][1] = 0;
  REP(i, n - 1) {
    ll p = a[i].first, x = a[i].second;
    ll aki = n - i;
    REP(j, i + 2) {
      if (j == 0)
        dp[i + 1][j] = dp[i][j] + abs(x - (j + aki - 1)) * p;
      else if (j == i + 1)
        dp[i + 1][j] = dp[i][j - 1] + abs(x - (j - 1)) * p;
      else
        dp[i + 1][j] = max(dp[i][j] + abs(x - (j + aki - 1)) * p,
                           dp[i][j - 1] + abs(x - (j - 1)) * p);
    }
  }
  ll ans = 0;
  REP(i, n)
  ans = max(ans, dp[n - 1][i] + a[n - 1].first * abs(i - a[n - 1].second));
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 2147483647;
const ll LINF = 9223372036854775807;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int main() {
  int n;
  cin >> n;
  vector<P> a(n);
  REP(i, n) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(ALL(a), greater<P>());
  ll dp[202][202];
  dp[0][0] = 0;
  REP(i, n - 1) {
    int p = a[i].first, x = a[i].second;
    int aki = n - i;
    REP(j, i + 2) {
      if (j == 0)
        dp[i + 1][j] = dp[i][j] + abs(x - (j + aki - 1)) * p;
      else if (j == i + 1)
        dp[i + 1][j] = dp[i][j - 1] + abs(x - (j - 1)) * p;
      else
        dp[i + 1][j] = max(dp[i][j] + abs(x - (j + aki - 1)) * p,
                           dp[i][j - 1] + abs(x - (j - 1)) * p);
    }
  }
  ll ans = 0;
  REP(i, n)
  ans = max(ans, dp[n - 1][i] + a[n - 1].first * (i - a[n - 1].second));
  cout << ans << endl;
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int, int>
#define pb push_back

const int N = 2005;

P a[N];
int n, memo[N][N];

int dp(int i, int fs, int ls) {
  if (i == n) {
    return 0;
  }
  if (memo[i][fs] != -1)
    return memo[i][fs];
  int ans = abs(fs - a[i].S) * a[i].F + dp(i + 1, fs + 1, ls);
  ans = max(ans, abs(ls - a[i].S) * a[i].F + dp(i + 1, fs, ls - 1));
  return memo[i][fs] = ans;
}

void solve() {
  int i, j, k, m, ans = 0, cnt = 0, sum = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i].F;
    a[i].S = i;
  }
  sort(a, a + n);
  reverse(a, a + n);
  memset(memo, -1, sizeof(memo));
  cout << dp(0, 0, n - 1);
  return;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // int t; cin >> t; while (t--)
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int, int>
#define pb push_back

const int N = 2005;

P a[N];
int n;

int dp(int i, int fs, int ls) {
  if (i == n) {
    return 0;
  }
  int ans = abs(fs - a[i].S) * a[i].F + dp(i + 1, fs + 1, ls);
  ans = max(ans, abs(ls - a[i].S) * a[i].F + dp(i + 1, fs, ls - 1));
  return ans;
}

void solve() {
  int i, j, k, m, ans = 0, cnt = 0, sum = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i].F;
    a[i].S = i;
  }
  sort(a, a + n);
  reverse(a, a + n);
  cout << dp(0, 0, n - 1);
  return;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // int t; cin >> t; while (t--)
  solve();
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

// dp[i][j]: 左にi個、右にj個詰めた時の最大値
ll dp[2020][2020];

int main() {
  int N;
  cin >> N;
  vector<pair<ll, ll>> A(N);
  REP(i, N) {
    cin >> A[i].first;
    A[i].second = i;
  }
  sort(A.begin(), A.end(), greater<pair<ll, ll>>());
  REP(i, N) {
    for (ll j = 0; i + j < N; j++) {
      // left
      dp[i + 1][j] =
          max(dp[i + 1][j], dp[i][j] + A[i + j].first * (A[i + j].second - i));
      // right
      dp[i][j + 1] =
          max(dp[i][j + 1],
              dp[i][j] + A[i + j].first * (N - 1 - j - A[i + j].second));
    }
  }
  ll ans = 0;
  REP(i, N + 1) { ans = max(ans, dp[i][N - i]); }
  cout << ans << endl;
}


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


Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

// dp[i][j]: 左にi個、右にj個詰めた時の最大値
int dp[2020][2020];

int main() {
  int N;
  vector<pair<int, int>> A(N);
  cin >> N;
  REP(i, N) {
    cin >> A[i].first;
    A[i].second = i;
  }
  sort(A.begin(), A.end(), greater<pair<int, int>>());
  REP(i, N) {
    for (int j = 0; i + j < N; j++) {
      // left
      dp[i + 1][j] =
          max(dp[i + 1][j], dp[i][j] + A[i + j].first * (A[i + j].second - i));
      // right
      dp[i][j + 1] =
          max(dp[i][j + 1],
              dp[i][j] + A[i + j].first * (N - 1 - j - A[i + j].second));
    }
  }
  int ans = 0;
  REP(i, N + 1) { ans = max(ans, dp[i][N - i]); }
  cout << ans << endl;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

long long dp[2005][2005] = {};

int main() {

  long long n;
  cin >> n;
  vector<pair<long long, long long>> a(n + 1);
  for (long long i = 1; i < n + 1; ++i)
    cin >> a[i].first, a[i].second = i;
  sort(a.begin() + 1, a.end(), greater<pair<long long, long long>>());
  for (long long i = 1; i < n + 1; ++i) {
    for (long long j = 0; j < i; ++j) {
      long long L = j;
      long long R = i - 1 - j;
      dp[L + 1][R] =
          max(dp[L + 1][R], dp[L][R] + abs(L + 1 - a[i].second) * a[i].first);
      R = j;
      L = i - 1 - j;
      dp[L][R + 1] =
          max(dp[L][R + 1], dp[L][R] + abs(n - R - a[i].second) * a[i].first);
    }
  }
  long long ans = 0;
  for (long long i = 1; i < n + 1; ++i) {
    ans = max(ans, dp[i][n - i]);
  }
  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

long long dp[2005][2005] = {};

int main() {

  long long n;
  cin >> n;
  vector<pair<long long, long long>> a(n + 1);
  dp[2005][2005] = {};
  for (long long i = 1; i < n + 1; ++i)
    cin >> a[i].first, a[i].second = i;
  sort(a.begin() + 1, a.end(), greater<pair<long long, long long>>());
  for (long long i = 1; i < n + 1; ++i) {
    for (long long j = 0; j < i; ++j) {
      long long L = j;
      long long R = i - 1 - j;
      dp[L + 1][R] =
          max(dp[L + 1][R], dp[L][R] + abs(L + 1 - a[i].second) * a[i].first);
      R = j;
      L = i - 1 - j;
      dp[L][R + 1] =
          max(dp[L][R + 1], dp[L][R] + abs(n - R - a[i].second) * a[i].first);
    }
  }
  long long ans = 0;
  for (long long i = 1; i < n + 1; ++i) {
    ans = max(ans, dp[i][n - i]);
  }
  cout << ans << endl;

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

#include <algorithm>
#include <iostream>
#include <vector>

#define ff first
#define ss second

using namespace std;
struct _ {
  ios_base::Init i;
  _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;

int main() {

  int n;
  cin >> n;
  vector<pair<long long, long long>> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i].ff, a[i].ss = i;
  sort(a.begin(), a.end(), greater<pair<long long, long long>>());

  vector<vector<long long>> dp(n, vector<long long>(n + 1, 0));

  dp[0] = {a[0].ff * abs(a[0].ss - n + 1), a[0].ff * abs(a[0].ss)};
  for (int i = 1; i <= n - 1; i++) {
    for (int j = 0; j <= i + 1; j++) {
      if (j == 0)
        dp[i][j] = dp[i - 1][j] + a[i].ff * abs(a[i].ss - n + 1 + i - j);
      else if (j <= i)
        dp[i][j] = max(dp[i - 1][j - 1] + a[i].ff * abs(a[i].ss - j + 1),
                       dp[i - 1][j] + a[i].ff * abs(a[i].ss - n + 1 + i - j));
      else
        dp[i][j] = dp[i - 1][j - 1] + a[i].ff * abs(a[i].ss - j + 1);
    }
  }

  cout << *max_element(dp[n - 1].begin(), dp[n - 1].end());

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

#define ff first
#define ss second

using namespace std;
struct _ {
  ios_base::Init i;
  _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;

int main() {

  int n;
  cin >> n;
  vector<pair<int, int>> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i].ff, a[i].ss = i;
  sort(a.begin(), a.end(), greater<pair<int, int>>());

  vector<vector<int>> dp(n, vector<int>(n, 0));

  dp[0] = {a[0].ff * abs(a[0].ss - n + 1), a[0].ff * abs(a[0].ss)};
  for (int i = 1; i <= n - 1; i++) {
    for (int j = 0; j <= i + 1; j++) {
      if (j == 0)
        dp[i][j] = dp[i - 1][j] + a[i].ff * abs(a[i].ss - n + 1 + i - j);
      else if (j <= i)
        dp[i][j] = max(dp[i - 1][j - 1] + a[i].ff * abs(a[i].ss - j + 1),
                       dp[i - 1][j] + a[i].ff * abs(a[i].ss - n + 1 + i - j));
      else
        dp[i][j] = dp[i - 1][j - 1] + a[i].ff * abs(a[i].ss - j + 1);
    }
  }

  cout << *max_element(dp[n - 1].begin(), dp[n - 1].end());

  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n;
  std::cin >> n;

  vector<ll> a(n);
  vector<pair<ll, ll>> b(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
    b[i] = {a[i], i};
  }
  sort(b.rbegin(), b.rend());

  // map<pair<ll,ll>,ll> memo;
  vector<vector<ll>> memo(n + 1, vector<ll>(n + 1, 0));
  // memo[{0,0}] = 0;
  // memo[0][0] = 0;
  function<ll(ll, ll)> rec = [&](ll l, ll r) {
    if (l + r > n)
      return 0ll;
    if (l == 0 && r == 0)
      return 0ll;
    if (memo[l][r] != 0)
      return memo[l][r];

    if (l == 0) {
      return memo[l][r] =
                 rec(l, r - 1) +
                 b[l + r - 1].first * abs(b[l + r - 1].second - (n - r));
    }
    if (r == 0) {
      return memo[l][r] = rec(l - 1, r) +
                          b[l + r - 1].first * abs(b[l + r - 1].second - l + 1);
    }

    return memo[l][r] =
               max(rec(l - 1, r) +
                       b[l + r - 1].first * abs(b[l + r - 1].second - l + 1),
                   rec(l, r - 1) +
                       b[l + r - 1].first * abs(b[l + r - 1].second - (n - r)));
  };
  ll tmp = 0;

  for (int i = 0; i <= n; i++) {
    tmp = max(tmp, rec(i, n - i));
  }
  std::cout << tmp << std::endl;
}


Error: TLE

Fixed submission:

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

int main() {
  ll n;
  std::cin >> n;

  vector<ll> a(n);
  vector<pair<ll, ll>> b(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
    b[i] = {a[i], i};
  }
  sort(b.rbegin(), b.rend());

  map<pair<ll, ll>, ll> memo;
  memo[{0, 0}] = 0;
  function<ll(ll, ll)> rec = [&](ll l, ll r) {
    if (l + r > n)
      return 0ll;
    if (memo.find({l, r}) != memo.end())
      return memo[{l, r}];

    if (l == 0) {
      return memo[{l, r}] =
                 rec(l, r - 1) +
                 b[l + r - 1].first * abs(b[l + r - 1].second - (n - r));
    }
    if (r == 0) {
      return memo[{l, r}] =
                 rec(l - 1, r) +
                 b[l + r - 1].first * abs(b[l + r - 1].second - l + 1);
    }

    return memo[{l, r}] =
               max(rec(l - 1, r) +
                       b[l + r - 1].first * abs(b[l + r - 1].second - l + 1),
                   rec(l, r - 1) +
                       b[l + r - 1].first * abs(b[l + r - 1].second - (n - r)));
  };
  ll tmp = 0;

  for (int i = 0; i <= n; i++) {
    tmp = max(tmp, rec(i, n - i));
  }
  std::cout << tmp << std::endl;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<ll, int> P;
int n;
vector<P> A;
ll dp[2005][2005];
ll nex[2005][2005];
ll ans;
int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;

    A.push_back(P(a, i));
  }

  sort(A.begin(), A.end(), greater<P>());

  // for(int i = 0; i < n; i++) cout << A[i].first <<" "<<A[i].second<< endl;

  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j <= i + 1; j++) {
      ll k = i - j + 1;
      if (j == 0) {
        nex[j][k] = dp[j][k - 1] + abs(n - k - A[i].second) * A[i].first;
        continue;
      }
      if (k == 0) {
        nex[j][k] = dp[j - 1][k] + abs(A[i].second - j + 1) * A[i].first;
        continue;
      }
      nex[j][k] = max(dp[j - 1][k] + abs(A[i].second - j + 1) * A[i].first,
                      dp[j][k - 1] + abs(n - k - A[i].second) * A[i].first);
    }
    for (ll j = 0; j <= i + 1; j++) {
      ll k = i - j + 1;
      dp[j][k] = nex[j][k];
    }
    /* cout << i << endl;
     for(int ii = 0; ii <= i+1; ii++) {
       int j = i - ii + 1;
    cout <<"dp["<<ii<<"]["<<j<<"] = "<< dp[ii][j] << endl;
    }*/
  }

  for (int i = 0; i <= n; i++) {
    int j = n - i;
    ans = max(ans, dp[i][j]);
    // cout <<"dp["<<i<<"]["<<j<<"] = "<< dp[i][j] << endl;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<ll, int> P;
int n;
vector<P> A;
ll dp[2005][2005];
ll nex[2005][2005];
ll ans;
int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;

    A.push_back(P(a, i));
  }

  sort(A.begin(), A.end(), greater<P>());

  // for(int i = 0; i < n; i++) cout << A[i].first <<" "<<A[i].second<< endl;

  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j <= i + 1; j++) {
      ll k = i - j + 1;
      if (j == 0) {
        nex[j][k] = dp[j][k - 1] + abs(n - k - A[i].second) * A[i].first;
        continue;
      }
      if (k == 0) {
        nex[j][k] = dp[j - 1][k] + abs(A[i].second - j + 1) * A[i].first;
        continue;
      }
      nex[j][k] = max(dp[j - 1][k] + abs(A[i].second - j + 1) * A[i].first,
                      dp[j][k - 1] + abs(n - k - A[i].second) * A[i].first);
    }
    swap(nex, dp);
    /* cout << i << endl;
     for(int ii = 0; ii <= i+1; ii++) {
       int j = i - ii + 1;
    cout <<"dp["<<ii<<"]["<<j<<"] = "<< dp[ii][j] << endl;
    }*/
  }

  for (int i = 0; i <= n; i++) {
    int j = n - i;
    ans = max(ans, dp[i][j]);
    // cout <<"dp["<<i<<"]["<<j<<"] = "<< dp[i][j] << endl;
  }
  cout << ans << endl;
}
Problem p02709 (C++) — buggy submission, verdict: 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();
  }
}

Error: TLE

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

#pragma GCC optimize("Ofast", "unroll-loops")

// #define TEST

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> A_idx(N);
  for (int i = 0; i < N; ++i) {
    int ai;
    cin >> ai;
    A_idx[i] = pair<int, int>(ai, i);
  }
  // (Ai,i)を降順ソート
  sort(A_idx.begin(), A_idx.end(), [&](auto &a, auto &b) { return a >= b; });
#ifdef TEST
  cout << "-----sorted A_idx-----" << endl;
  for (auto &p : A_idx)
    cout << p.first << " " << p.second << endl;
#endif
  // DP[i][j] := 大きいほうからi個をj番目までに割り当てるときのmax
  vector<vector<ll>> DP(N + 1, vector<ll>(N + 1, 0));
  for (int i = 1; i <= N; ++i) {
    int idx = A_idx[i - 1].second;
    int ai = A_idx[i - 1].first;
    for (int j = 0; j <= i; ++j) {
      if (j == 0)
        DP[i][j] = DP[i - 1][j] + (ll)ai * abs(idx - (N - i + j));
      else if (j == i)
        DP[i][j] = DP[i - 1][j - 1] + (ll)ai * abs(idx - (j - 1));
      else {
        ll tmp1 = DP[i - 1][j] + (ll)ai * abs(idx - (N - i + j));
        ll tmp2 = DP[i - 1][j - 1] + (ll)ai * abs(idx - (j - 1));
        DP[i][j] = max(tmp1, tmp2);
      }
    }
  }
#ifdef TEST
  cout << "-----DP-----" << endl;
  for (int j = 0; j <= N; ++j) {
    for (int i = 0; i <= N; ++i)
      cout << DP[i][j] << " ";
    cout << endl;
  }
  cout << "-----answer-----" << endl;
#endif
  ll res = 0;
  for (int j = 0; j <= N; ++j)
    res = max(res, DP[N][j]);
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast", "unroll-loops")

// #define TEST

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

using namespace std;

#define ll long long
#define ld long double

using weight = ll;
using matrix = vector<vector<ll>>;
const ll inf = (1LL << 55);

weight hungarian(const matrix &a) {
  int n = a.size(), p, q;
  vector<ll> fx(n, inf), fy(n, 0);
  vector<int> x(n, -1), y(n, -1);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      fx[i] = max(fx[i], a[i][j]);
  for (int i = 0; i < n;) {
    vector<int> t(n, -1), s(n + 1, i);
    for (p = q = 0; p <= q && x[i] < 0; ++p)
      for (int k = s[p], j = 0; j < n && x[i] < 0; ++j)
        if (fx[k] + fy[j] == a[k][j] && t[j] < 0) {
          s[++q] = y[j], t[j] = k;
          if (s[q] < 0)
            for (p = j; p >= 0; j = p)
              y[j] = k = t[j], p = x[k], x[k] = j;
        }
    if (x[i] < 0) {
      weight d = inf;
      for (int k = 0; k <= q; ++k)
        for (int j = 0; j < n; ++j)
          if (t[j] < 0)
            d = min(d, fx[s[k]] + fy[j] - a[s[k]][j]);
      for (int j = 0; j < n; ++j)
        fy[j] += (t[j] < 0 ? 0 : d);
      for (int k = 0; k <= q; ++k)
        fx[s[k]] -= d;
    } else
      ++i;
  }
  weight ret = 0;
  for (int i = 0; i < n; ++i)
    ret += a[i][x[i]];
  return ret;
}

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  vector<vector<ll>> m(N, vector<ll>(N, 0));
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      m[i][j] = A[i] * abs(j - i);
    }
  }
  cout << hungarian(m) << endl;
  return 0;
}
Problem p02710 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int MAXN = 200005;

template <typename T> inline void read(T &AKNOI) {
  T x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  AKNOI = flag * x;
}

int n, m, col[MAXN], has[MAXN];
vector<int> e[MAXN];
int siz[MAXN], sum[MAXN];
LL ans[MAXN];

inline LL C2(int x) { return 1LL * x * (x + 1) / 2; }

void DFS(int u, int fa) {
  siz[u] = 1;
  for (auto v : e[u]) {
    if (v == fa)
      continue;
    int cur = sum[col[u]];
    DFS(v, u);
    int blk = siz[v] - (sum[col[u]] - cur);
    ans[col[u]] += C2(blk);
    sum[col[u]] += blk;
    siz[u] += siz[v];
  }
  sum[col[u]] += 1;
}

void init() {
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(col[i]);
    has[col[i]] = 1;
  }
  for (int i = 1, u, v; i < n; ++i) {
    read(u);
    read(v);
    e[u].push_back(v);
    e[v].push_back(u);
  }
}

void solve() {
  DFS(1, 0);
  for (int i = 1; i <= n; ++i) {
    if (i == col[1] || !has[i])
      continue;
    ans[i] += C2(n - sum[i]);
  }
  for (int i = 1; i <= n; ++i) {
    printf("%lld\\n", has[i] ? C2(n) - ans[i] : 0);
  }
}

int main() {
  init();
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int MAXN = 100005;

template <typename T> inline void read(T &AKNOI) {
  T x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  AKNOI = flag * x;
}

int n, m, col[MAXN], has[MAXN];
vector<int> e[MAXN];
int siz[MAXN], sum[MAXN];
LL ans[MAXN];

inline LL C2(int x) { return 1LL * x * (x + 1) / 2; }

void DFS(int u, int fa) {
  siz[u] = 1;
  for (auto v : e[u]) {
    if (v == fa)
      continue;
    int cur = sum[col[u]];
    DFS(v, u);
    int blk = siz[v] - (sum[col[u]] - cur);
    ans[col[u]] += C2(blk);
    sum[col[u]] += blk;
    siz[u] += siz[v];
  }
  sum[col[u]] += 1;
}

void init() {
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(col[i]);
    has[col[i]] = 1;
  }
  for (int i = 1, u, v; i < n; ++i) {
    read(u);
    read(v);
    e[u].push_back(v);
    e[v].push_back(u);
  }
}

void solve() {
  DFS(1, 0);
  for (int i = 1; i <= n; ++i) {
    if (i == col[1] || !has[i])
      continue;
    ans[i] += C2(n - sum[i]);
  }
  for (int i = 1; i <= n; ++i) {
    printf("%lld\\n", has[i] ? C2(n) - ans[i] : 0);
  }
}

int main() {
  init();
  solve();
  return 0;
}