This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using i64 = long long;
constexpr i64 inf = 1ll << 60;
class SparseTable {
int n;
std::vector<int> log_table;
std::vector<std::vector<i64>> data;
public:
SparseTable(std::vector<i64> vec) : n(vec.size()) {
log_table.assign(n + 1, 0);
for (int i = 1; i <= n; ++i) {
log_table[i] = std::max(log_table[i], log_table[i - 1]);
if (2 * i <= n) {
log_table[2 * i] = log_table[i] + 1;
}
}
data.resize(log_table[n] + 1);
data[0] = vec;
for (int d = 1; d <= log_table[n]; ++d) {
const int w = 1 << d;
data[d].resize(n - w + 1);
for (int i = 0; i + w <= n; ++i) {
data[d][i] = std::max(data[d - 1][i], data[d - 1][i + w / 2]);
}
}
}
i64 fold(int l, int r) const {
const int w = r - l;
const int d = log_table[w];
return std::max(data[d][l], data[d][r - (1 << d)]);
}
};
int main() {
int N;
std::cin >> N;
std::vector<i64> T(N);
for (auto &e : T) {
std::cin >> e;
}
const auto st = SparseTable(T);
auto cost = [&](const int l, const int r) {
return (N - l) * st.fold(l, r);
};
std::vector<i64> dp(N + 1, inf);
dp[0] = 0;
auto dist = [&](const int l, const int r) {
return dp[l] + cost(l, r);
};
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j <= N; ++j) {
dp[j] = std::min(dp[j], dist(i, j));
}
}
std::cout << dp[N] << std::endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |