Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define task ""
- #define pb push_back
- #define ld long double
- #define ll long long
- #define ull unsigned long long
- using namespace std;
- int typetest;
- inline void fastIOfileinput(){
- ios_base:: sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if(fopen(task".inp", "r")){
- freopen(task".inp", "r", stdin);
- freopen(task".out", "w", stdout);
- }
- if(fopen(task".in", "r")){
- freopen(task".in", "r", stdin);
- freopen(task".out", "w", stdout);
- }
- typetest = 0;
- }
- ll l[10009][1009], h[10009][1009];
- int n;
- ll a[10009], m;
- ll f[10009][1009];
- ll c;
- inline void Enter(){
- cin >> n >> c;
- for(int i = 1; i <= n; ++i){
- cin >> a[i];
- m = max(m, a[i]);
- }
- memset(f, 0, sizeof(f));
- for(int i = 1; i <= n; ++i){
- h[i][m + 1] = INT_MAX;
- l[i][0] = INT_MAX;
- }
- }
- inline void solve(){
- for(int i = 1; i <= n; ++i){
- for(ll j = 1; j <= m; ++j){
- if(j >= a[i]){
- f[i][j] = (ll) (i != 1) * min(l[i - 1][j] + c * j, h[i - 1][j] - c * j) + (j - a[i]) * (j - a[i]);
- l[i][j] = min(l[i][j - 1], f[i][j] - c * j);
- }
- else{
- f[i][j] = l[i][j] = INT_MAX;
- }
- if(m - j + 1 >= a[i]){
- f[i][m - j + 1] = (ll)(i != 1) * min(l[i - 1][m - j + 1] + c * (m - j + 1),
- h[i - 1][m - j + 1] - c * (m - j + 1)) + (m - j + 1LL - a[i]) * (m - j + 1LL - a[i]);
- h[i][m - j + 1] = min(h[i][m - j + 2], f[i][m - j + 1] + c * (m - j + 1));
- }
- else{
- h[i][m - j + 1] = h[i][m - j + 2];
- }
- }
- }
- ll ans = 1e18;
- for(int i = 1; i <= m; ++i){
- ans = min(ans, f[n][i]);
- }
- cout << ans;
- }
- int main(){
- fastIOfileinput();
- if(typetest){
- int t;
- cin >> t;
- while(t--){
- Enter();
- solve();
- }
- }
- else{
- Enter();
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement