제출 #977724

#제출 시각아이디문제언어결과실행 시간메모리
977724mannshah1211Candies (JOI18_candies)C++17
100 / 100
2537 ms24624 KiB
/**
 *  author: hashman
 *  created:
**/

// Thanks Dominater069 for the idea of solution!

#include <bits/stdc++.h>

using namespace std;

string to_string(string s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

void debug_out() {
  cerr << endl;
}

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

using Int = long long;

const Int inf = 1e18;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  auto solve = [&](auto&& self, int l, int r) -> vector<vector<Int>> {
    int sz = r - l + 1, till = (sz + 1) / 2;
    vector<vector<Int>> dp(till + 1, vector<Int>(4, -inf));
    if (l == r) {
      dp[0][0] = 0;
      dp[1][3] = a[l];
      return dp;
    }
    int m = (l + r) / 2;
    vector<vector<Int>> dpl = self(self, l, m), dpr = self(self, m + 1, r);
    for (int i = 0; i <= till; i++) {
      for (int ll = 0; ll < 2; ll++) {
        for (int lr = 0; lr < 2; lr++) {
          for (int rl = 0; rl < 2; rl++) {
            for (int rr = 0; rr < 2; rr++) {
              if (rl && lr) {
                continue;
              }
              int curo = ll + 2 * rr, lfo = ll + 2 * lr, rgo = rl + 2 * rr;
              int low = 0, high = i;
              while (high - low > 2) {
                int mid_1 = (2 * low + high) / 3, mid_2 = (low + 2 * high) / 3;
                if (mid_2 >= int(dpl.size())) {
                  high = mid_2;
                  continue;
                }
                if (i - mid_1 >= int(dpr.size())) {
                  low = mid_1;
                  continue;
                }
                Int candl = dpl[mid_1][lfo] + dpr[i - mid_1][rgo], candh = dpl[mid_2][lfo] + dpr[i - mid_2][rgo];
                if (candl > candh) {
                  high = mid_2;
                } else {
                  low = mid_1;
                }
              }
              for (int op = low; op <= high; op++) {
                int a = op, b = i - op;
                if (a >= int(dpl.size()) || b >= int(dpr.size())) {
                  continue;
                }
                dp[i][curo] = max(dp[i][curo], dpl[a][lfo] + dpr[b][rgo]);
              }
            }
          }
        }
      }
    }
    return dp;
  };
  vector<vector<Int>> dp = solve(solve, 0, n - 1);
  for (int i = 1; i <= (n + 1) / 2; i++) {
    cout << max({dp[i][0], dp[i][1], dp[i][2], dp[i][3]}) << '\n';
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...