Problem p02709 (C++) — buggy submission, verdict: Runtime Error



#include <bits/stdc++.h>

using namespace std;

#define UB upper_bound
#define LB lower_bound
#define BS binary_search
#define MP make_pair
#define EB emplace_back
#define endl "\\n"
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define F first
#define S second
#define ALL(a) (a).begin(), (a).end()
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define int long long

typedef long long int64;
typedef vector<int> VI64;
typedef vector<pair<int, int>> VII64;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;

int n;

int dp[2005][2005];

int suri(int idx, int iav, VII64 &v) {
  if (idx == n)
    return 0;
  int &ans = dp[idx][iav];
  if (ans != -1)
    return ans;
  ans = (abs(iav - v[idx].S) * v[idx].F) + suri(idx + 1, iav + 1, v);
  int jav = n - 1 - (idx - iav);
  ans = max(ans, (abs(jav - v[idx].S) * v[idx].F) + suri(idx + 1, iav, v));
  return ans;
}

int32_t main() {
  IOS;
  int T = 1;
  // cin>>T;
  while (T--) {
    int i, j;
    cin >> n;
    VII64 v(n);
    for (i = 0; i < n; i++) {
      cin >> v[i].F;
      v[i].S = i;
    }
    memset(dp, -1, sizeof dp);
    sort(ALL(v), greater<pair<int, int>>());
    cout << suri(0, 0, v) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define print(A, n)                                                            \\
  for (ll i = 0; i < n; ++i)                                                   \\
    cout << A[i] << ' ';                                                       \\
  cout << endl;
#define take(A, n)                                                             \\
  for (ll i = 0; i < n; ++i)                                                   \\
    cin >> A[i];
//    for(auto& it : m)
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
//  priority_queue<ll int> pq;
//  priority_queue <ll int, vector<ll int>, greater<ll int> > pq;
//  vector<ll int> v;
//  vector< pair<ll int,ll int> > v;
//  set<ll int>::iterator it=s.begin();
//  unordered_map<  pair<ll int,ll int>, ll int> mp;
int dp[1001][1001];
int solve(int idx, int us, vector<pair<int, int>> vect) {
  if (idx == (int)vect.size()) {
    return 0;
  }

  if (dp[idx][us] != -1)
    return dp[idx][us];
  int ans = abs(vect[idx].second - us) * vect[idx].first +
            solve(idx + 1, us + 1, vect);
  ans = max(ans, abs(vect[idx].second - ((int)vect.size() - (idx - us) - 1)) *
                         vect[idx].first +
                     solve(idx + 1, us, vect));
  dp[idx][us] = ans;

  return ans;
}
int main() {
  fastio();
  ll int T; // cin>>T;
  T = 1;
  while (T--) {
    memset(dp, -1, sizeof(dp));
    int n, i, j;
    cin >> n;

    vector<pair<int, int>> vect;
    int st, en;
    // ll int dp[n][n];
    int a[n];
    for (i = 0; i < n; i++) {
      cin >> a[i];
      vect.push_back(make_pair(a[i], i));
    }
    sort(vect.begin(), vect.end());
    reverse(vect.begin(), vect.end());

    ll int ans = solve(0, 0, vect);
    cout << ans << endl;
    // for(i=0;i<n;i++)
    // {
    //   for(j=0;j<n;j++)
    //   {
    //     dp[i][j]=abs(i-j)*a[i];
    //     pq.push(make_pair(dp[i][j],make_pair(i,j)));
    //   }
    // }
    // set<ll int> x,y;
    //
    // stack<pair<ll int,pair<ll int,ll int>>> st;
    // ll int count=0;
    // while(count!=n)
    // {
    //   auto tp =pq.top();pq.pop();
    //   if(x.find(tp.second.first)==x.end()&&y.find(tp.second.second)==y.end())
    //   {
    //     x.insert(tp.second.first);y.insert(tp.second.second);
    //     count++;
    //     ans+=tp.first;
    //     while(!st.empty())
    //     {
    //       pq.push(st.top());
    //       st.pop();
    //     }
    //   }
    //   else
    //   {
    //       st.push(tp);

    //   }
    // }

    // cout<<ans<<endl;
  }

  return 0;
}