#include <bits/stdc++.h>
#define int long long
#define ll long long
#define all(vin) vin.begin(), vin.end()
using namespace std;
typedef tuple<ll,ll,ll> tp;
typedef pair<int,int> pii;
const int N = 1e6 + 5;
const int oo = 2e9;
int n, K, a[N], f[2][N], s[N];
vector<pii> hull;
int ptr = 0;
bool check(pii x, pii y, pii z){
return 1.0 * (z.second - x.second) / 1.0 * (x.first - z.first) < 1.0 * (y.second - x.second) / 1.0 * (x.first - y.first);
// return 1.0 * (z.second - y.second) / 1.0 * (y.first - z.first) > 1.0 * (y.second - x.second) / 1.0 * (x.first - y.first);
}
void add(pii x){
while((int)hull.size() >= 2 && check(hull[(int)hull.size() - 2], hull.back(), x)) hull.pop_back();
hull.push_back(x);
}
int get(pii x,int y){
return x.first * y + x.second;
}
int query(int x){
int l = 0, r = (int)hull.size() - 2, ans = get(hull.back(), x), pos = 0;
while(l <= r){
int mid = (l + r) / 2;
if(get(hull[mid], x) >= get(hull[mid + 1], x)) pos = l = mid + 1;
else r = mid - 1;
ans = max({ans, get(hull[mid], x), get(hull[mid + 1], x)});
}
ans = min(ans, get(hull[pos], x));
return ans;
}
namespace sub1{
int dp[3005][3005];
void pk(int &x,int y){
if(x == -1) x = y;
else x = min(x, y);
}
void solve(){
memset(dp, -1, sizeof(dp));
dp[0][0] = 0;
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= min(i, K); j ++){
for(int p = j - 1; p <= i - 1; p ++) if(dp[p][j - 1] != -1){
pk(dp[i][j], dp[p][j - 1] + (s[i] - s[p]) * (s[i] - s[p]));
}
}
}
cout << dp[n][K];
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> n >> K;
for(int i = 1; i <= n; i ++){
cin >> a[i];
s[i] = s[i - 1] + a[i];
}
int pre = 0, nx = 1;
for(int i = 1; i <= n;i ++){
f[0][i] = s[i] * s[i];
}
for(int k = 2; k <= K; k ++){
hull.clear();
ptr = 0;
add({-2 * s[k - 1], s[k - 1] * s[k - 1] + f[pre][k - 1]});
for(int i = k; i <= n; i ++){
f[nx][i] = s[i] * s[i] + query(s[i]);
add({-2 * s[i], s[i] * s[i] + f[pre][i]});
}
swap(pre, nx);
}
cout << f[pre][n];
}
Compilation message
coach.cpp: In function 'int main()':
coach.cpp:67:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
coach.cpp:68:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen(task ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
27 ms |
25424 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
27 ms |
25424 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
27 ms |
25424 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
27 ms |
25424 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |