이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/**
* author: Haunted_Cpp
**/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
template<typename T> ostream &operator << (ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename 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 << '}'; }
template<typename A, typename B> ostream &operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif
const int MAX_N = 1e6 + 5;
vector<long long> seg(4 * MAX_N);
void build(int l, int r, int node, const vector<long long> &arr) {
if (l == r) {
seg[node] = arr[l];
return;
}
const int mid = l + (r - l) / 2;
build(l, mid, 2 * node + 1, arr);
build(mid + 1, r, 2 * node + 2, arr);
seg[node] = max(seg[2 * node + 1], seg[2 * node + 2]);
}
int rmq(int ql, int qr, int l, int r, int node) {
if (l > qr || r < ql) return 0;
if (l >= ql && r <= qr) return seg[node];
const int mid = l + (r - l) / 2;
return max(rmq(ql, qr, l, mid, 2 * node + 1), rmq(ql, qr, mid + 1, r, 2 * node + 2));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<long long> arr(n + 1);
for (int i = 1; i <= n; i++) cin >> arr[i];
vector<long long> dp(n + 1, 1e18);
build(0, n, 0, arr);
dp[0] = 0;
for (int i = 1; i <= n; i++) {
//~ long long mx = 0;
//~ pair<long long, int> best_way = {1e18, 1e9};
for (int j = min(10, i - 1); j >= 0; j--) {
//~ mx = max(mx, arr[j + 1]);
//~ pair<long long, int> cur = {dp[j] + 1LL * mx * (n - j), j};
//~ best_way = min(best_way, cur);
dp[i] = min(dp[i], dp[j] + 1LL * rmq(j + 1, i, 0, n, 0) * (n - j));
}
for (int j = i; j >= max(0, i - 10); j--) {
//~ mx = max(mx, arr[j + 1]);
//~ pair<long long, int> cur = {dp[j] + 1LL * mx * (n - j), j};
//~ best_way = min(best_way, cur);
dp[i] = min(dp[i], dp[j] + 1LL * rmq(j + 1, i, 0, n, 0) * (n - j));
}
//~ cout << best_way.second << '\n';
}
cout << dp[n] << '\n';
return 0;
}
# | 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... |