Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define ini(a, i) memset(a, i, sizeof(a))
#define contains(c, i) ((c).find(i) != (c).end())
#define present(i, c) (find(all(c), i) != (c).end())

#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, b, e)                                                          \\
  for (int i = (b) - ((b) > (e)); i != (e) - ((b) > (e));                      \\
       i += 1 - 2 * ((b) > (e)))

#define dbg(x) cerr << #x << " = " << x << endl;
#define dbgall(x)                                                              \\
  cerr << #x << " = ";                                                         \\
  for (auto i_ : x)                                                            \\
    cerr << i_ << " ";                                                         \\
  cerr << endl;
#define debug(args...)                                                         \\
  {                                                                            \\
    string s_(#args);                                                          \\
    replace(all(s_), ',', ' ');                                                \\
    stringstream ss_(s_);                                                      \\
    istream_iterator<string> it_(ss_);                                         \\
    cerr_(it_, args);                                                          \\
  }
void cerr_(istream_iterator<string> it) { (void)it; }
template <class T, class... Args>
void cerr_(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  cerr_(++it, args...);
}

template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <class T> ostream &operator<<(ostream &os, const set<T> &s) {
  os << '{';
  string sep;
  for (const auto &x : s)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <class T, class S>
ostream &operator<<(ostream &os, const map<T, S> &m) {
  os << '{';
  string sep;
  for (const auto &x : m)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <class T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
  os << '{';
  string sep;
  for (const auto &x : arr)
    os << sep << x, sep = ", ";
  return os << '}';
}

//=========================

int n;
vector<pii> v;
ll memo[2001][2001];

ll solve(int i, int l, int r) {
  if (i == n || l > r)
    return 0;
  ll &ans = memo[l][r];
  if (ans != -1)
    return ans;
  ll x = v[i].fi;
  ll p = v[i].se;
  ans = 0;
  ans = max(ans, x * abs(p - l) + solve(i + 1, l + 1, r));
  ans = max(ans, x * abs(p - r) + solve(i + 1, l, r - 1));
  return ans;
}

void run_test() {
  cin >> n;
  v.resize(n);
  rep(i, n) cin >> v[i].fi, v[i].se = i;
  sort(rall(v));
  ini(memo, -1);
  cout << solve(0, 0, n - 1) << endl;
}

//=========================

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // int t_ = 1, t__; cin >> t__; while(t_ <= t__) { cout << "Case #" << t_++ <<
  // ": "; run_test(); }
  //  int t_; cin >> t_; while(t_--) run_test();
  run_test();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define ini(a, i) memset(a, i, sizeof(a))
#define contains(c, i) ((c).find(i) != (c).end())
#define present(i, c) (find(all(c), i) != (c).end())

#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, b, e)                                                          \\
  for (int i = (b) - ((b) > (e)); i != (e) - ((b) > (e));                      \\
       i += 1 - 2 * ((b) > (e)))

#define dbg(x) cerr << #x << " = " << x << endl;
#define dbgall(x)                                                              \\
  cerr << #x << " = ";                                                         \\
  for (auto i_ : x)                                                            \\
    cerr << i_ << " ";                                                         \\
  cerr << endl;
#define debug(args...)                                                         \\
  {                                                                            \\
    string s_(#args);                                                          \\
    replace(all(s_), ',', ' ');                                                \\
    stringstream ss_(s_);                                                      \\
    istream_iterator<string> it_(ss_);                                         \\
    cerr_(it_, args);                                                          \\
  }
void cerr_(istream_iterator<string> it) { (void)it; }
template <class T, class... Args>
void cerr_(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  cerr_(++it, args...);
}

template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <class T> ostream &operator<<(ostream &os, const set<T> &s) {
  os << '{';
  string sep;
  for (const auto &x : s)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <class T, class S>
ostream &operator<<(ostream &os, const map<T, S> &m) {
  os << '{';
  string sep;
  for (const auto &x : m)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <class T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
  os << '{';
  string sep;
  for (const auto &x : arr)
    os << sep << x, sep = ", ";
  return os << '}';
}

//=========================

int n;
vector<pii> v;
ll memo[2001][2001];

ll solve(int i, int l, int r) {
  if (i == n || l > r)
    return 0;
  // ll &ans = memo[l][r];
  // if(ans != -1) return ans;
  ll x = v[i].fi;
  ll p = v[i].se;
  // ans = 0;
  ll ans = 0;
  ans = max(ans, x * abs(p - l) + solve(i + 1, l + 1, r));
  ans = max(ans, x * abs(p - r) + solve(i + 1, l, r - 1));
  return ans;
}

void run_test() {
  cin >> n;
  v.resize(n);
  rep(i, n) cin >> v[i].fi, v[i].se = i;
  sort(rall(v));
  // ini(memo, -1);
  cout << solve(0, 0, n - 1) << endl;
}

//=========================

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // int t_ = 1, t__; cin >> t__; while(t_ <= t__) { cout << "Case #" << t_++ <<
  // ": "; run_test(); }
  //  int t_; cin >> t_; while(t_--) run_test();
  run_test();
  return 0;
}