/**
* 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
class CHT {
private:
typedef long long i64;
struct Node {
Node *l, *r;
pair<i64, i64> line;
Node() {
l = r = nullptr;
line = {1e18, 1e18};
}
};
Node *root;
const int LO, HI;
long long f(const pair<i64, i64> line, int x) {
return 1LL * x * line.first + line.second;
}
void add_line(pair<i64, i64> line, int l, int r, Node *seg) {
const int mid = l + (r - l) / 2;
bool esq = f(seg -> line, l) > f(line, l);
bool middle = f(seg -> line, mid) > f(line, mid);
if (middle) {
swap(line, seg -> line);
}
if (l == r) return;
if (esq == middle) {
if (!seg -> r) seg -> r = new Node;
add_line(line, mid + 1, r, seg -> r);
} else {
if (!seg -> l) seg -> l = new Node;
add_line(line, l, mid, seg -> l);
}
}
long long query(int l, int r, int where, Node *seg) {
if (!seg) return 1e18;
if (l == r) return f(seg -> line, where);
const int mid = l + (r - l) / 2;
if (where > mid) {
return min(query(mid + 1, r, where, seg -> r), f(seg -> line, where));
}
return min(query(l, mid, where, seg -> l), f(seg -> line, where));
}
public:
CHT() : LO(0), HI(1e9 + 7) {
root = new Node;
}
void add_line(pair<i64, i64> line) {
add_line(line, LO, HI, root);
}
long long get_best(int where) {
return query(LO, HI, where, root);
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> arr(n + 1);
for (int i = 1; i <= n; i++) cin >> arr[i];
CHT seg;
vector<long long> dp(n + 1);
seg.add_line({0, 0});
for (int i = 1; i <= n; i++) {
dp[i] = arr[i] * n + seg.get_best(arr[i]);
seg.add_line({1LL * -i, dp[i]});
}
cout << dp[n] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
427 ms |
82824 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |