Advertisement
YEZAELP

SMMR-159: Election Campaign

Jul 11th, 2020
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const long long INF=1e18;
  4. long long dp[1001];
  5. long long pow(long long i){
  6.     return i*i;
  7. }
  8. int main(){
  9.  
  10.     int n,k;
  11.     scanf("%d%d",&n,&k);
  12.     long long ar[n+1];
  13.  
  14.     for(int i=1;i<=n;i++){
  15.         scanf("%lld",&ar[i]);
  16.         dp[i]=INF;
  17.     }
  18.  
  19.     long long mn=INF;
  20.     for(int i=n;i>=1;i--){
  21.         for(int j=i;j<=n;j++){
  22.              if(j+1>n) dp[i]=min(dp[i],k+pow(ar[i]-ar[j]));
  23.              else dp[i]=min(dp[i],k+pow(ar[i]-ar[j])+dp[j+1] );
  24.         }
  25.     }
  26.     printf("%lld",dp[1]);
  27.     return 0;
  28. }
Advertisement
RAW Paste Data Copied
Advertisement