Advertisement
AquaBlitz11

TASK_047 - AquaBlitz11's Solution

Dec 31st, 2017
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6.  
  7. const ll INF = 1e18;
  8. const int N = 1010;
  9.  
  10. int n, m, c;
  11. ll a[N], memo[N];
  12.  
  13. ll f(int i)
  14. {
  15.     if (i == 0) // base case
  16.         return 0;
  17.     if (memo[i] != 0) // if calculated, value != 0: answer immediately
  18.         return memo[i];
  19.    
  20.     ll ans = INF;
  21.     for (int j = 1; j <= i; ++j)
  22.         ans = min(ans, f(j-1) + (a[i]-a[j])*(a[i]-a[j]) + c );
  23.  
  24.     memo[i] = ans;
  25.     return ans;
  26. }
  27.  
  28. int main()
  29. {
  30.     scanf("%d%d%d", &n, &m, &c);
  31.     for (int i = 1; i <= n; ++i)
  32.         scanf("%lld", &a[i]);
  33.     while (m--) {
  34.         int q;
  35.         scanf("%d", &q);
  36.         printf("%lld\n", f(q));
  37.     }
  38.  
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement