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

// #ifdef DEBUG
// #define _GLIBCXX_DEBUG
// #endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>

// these require C++11
#include <chrono>
#include <random>
#include <thread>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define int long long

#define all(c) c.begin(), c.end()
#define repeat(i, n) for (int i = 0; i < static_cast<int>(n); i++)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

typedef complex<double> point;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int calc_power(const vector<int> &ss, const int p, const int n, const int L) {
  if (n < p)
    return calc_power(ss, n, p, L);
  return (ss[n] - ss[p - 1]) / L;
}

int solve(int ii, int ui, int di, vector<int> &ss, vector<int> &k, const int L,
          vector<vector<int>> &memo) {
  if (ui > di)
    return solve(ii, di, ui, ss, k, L, memo);
  if (memo[ui][di] >= 0)
    return memo[ui][di];
  int u = k[ui];
  int d = k[di];
  int i = k[ii];
  if (ii == k.size() - 1) {
    return memo[ui][di] = calc_power(ss, u, i, L) + calc_power(ss, d, i, L);
  } else {
    return memo[ui][di] = min(
               calc_power(ss, u, i, L) + solve(ii + 1, ii, di, ss, k, L, memo),
               calc_power(ss, d, i, L) + solve(ii + 1, ui, ii, ss, k, L, memo));
  }
}

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

  int N, M, L;
  cin >> N >> M >> L;

  vector<int> K(N);
  vector<int> S(M);
  vector<vector<int>> memo(N, vector<int>(N, -1));
  for (int &i : K)
    cin >> i;
  sort(all(K));
  for (int &i : S)
    cin >> i;

  vector<int> SS(M + 1);
  for (int i = 1; i < SS.size(); i++) {
    SS[i] = SS[i - 1] + S[i - 1];
  }
  cout << solve(1, 0, 0, SS, K, L, memo) << endl;
  return 0;
}

Error: TLE

Fixed submission:

// #ifdef DEBUG
// #define _GLIBCXX_DEBUG
// #endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>

// these require C++11
#include <chrono>
#include <random>
#include <thread>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define int long long

#define all(c) c.begin(), c.end()
#define repeat(i, n) for (int i = 0; i < static_cast<int>(n); i++)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

typedef complex<double> point;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int calc_power(const vector<int> &ss, const int p, const int n, const int L) {
  if (n < p)
    return calc_power(ss, n, p, L);
  return (ss[n] - ss[p - 1]) / L;
}

int solve(int i, int u, int d, vector<int> &ss, vector<int> &k, const int L) {
  if (i == k.size() - 1) {
    return calc_power(ss, u, k.back(), L) + calc_power(ss, d, k.back(), L);
  } else {
    return min(calc_power(ss, u, k[i], L) + solve(i + 1, k[i], d, ss, k, L),
               calc_power(ss, d, k[i], L) + solve(i + 1, u, k[i], ss, k, L));
  }
}

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

  int N, M, L;
  cin >> N >> M >> L;

  vector<int> K(N);
  vector<int> S(M);
  for (int &i : K)
    cin >> i;
  sort(all(K));
  for (int &i : S)
    cin >> i;

  vector<int> SS(M + 1);
  for (int i = 1; i < SS.size(); i++) {
    SS[i] = SS[i - 1] + S[i - 1];
  }
  cout << solve(1, K[0], K[0], SS, K, L) << endl;
  return 0;
}