Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):

/*        ,---,
         / _=_ \.~
        (,(OvO),) ##}
       ,-\(-"-) /{ #)\
       {  '---'  (   }\_
       {{ ).___|__( '}}  \_
       {  )       (  }}    \
       {  /        \ }}    /
       {  )       (  }  /  /
       ((_'_     _'_))_,\__\
 */
// ____      _                _          _      _                  _ _
//|  _ \ ___| |_ __ _ _ __ __| | ___  __| |    / \   _ __   ___    | | ____ _
//___ ___   __| | ___
//| |_) / _ \ __/ _` | '__/ _` |/ _ \/ _` |   / _ \ | '_ \ / _ \   | |/ / _` |
/// __/ _ \ / _` |/ _ \ |  _ <  __/ || (_| | | | (_| |  __/ (_| |  / ___ \| |_)
//|  __/   |   < (_| | | (_| (_) | (_| |  __/
//|_| \_\___|\__\__,_|_|  \__,_|\___|\__,_| /_/   \_\ .__/ \___|   |_|\_\__,_|
//\___\___/ \__,_|\___|
//                                                  | |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<unsigned long long int> vul;
#define INF 1000000000
typedef std::numeric_limits<double> dbl;

double d = 3.14159265358979;
ull f = 1000000007;

int c[100000];

ll haha(int a[], int n, int k) {
  if (n <= k) {
    int b[n];
    for (int i = 0; i < n; i++) {
      if (c[i] == -1) {
        c[i] = haha(a, i, k);
        b[i] = abs(a[n] - a[i]) + c[i];
      } else {
        b[i] = abs(a[n] - a[i]) + c[i];
      }
    }
    sort(b, b + n);
    return b[0];
  }
  int b[k];
  for (int i = 1; i <= k; i++) {
    b[i - 1] = abs(a[n] - a[n - i]) + haha(a, n - i, k);
  }
  sort(b, b + k);
  return b[0];
}

int main() {
  for (int i = 0; i < 100000; i++) {
    c[i] = -1;
  }
  c[0] = 0;

  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << haha(a, n - 1, k);
}

Fixed submission:

/*        ,---,
         / _=_ \.~
        (,(OvO),) ##}
       ,-\(-"-) /{ #)\
       {  '---'  (   }\_
       {{ ).___|__( '}}  \_
       {  )       (  }}    \
       {  /        \ }}    /
       {  )       (  }  /  /
       ((_'_     _'_))_,\__\
 */
// ____      _                _          _      _                  _ _
//|  _ \ ___| |_ __ _ _ __ __| | ___  __| |    / \   _ __   ___    | | ____ _
//___ ___   __| | ___
//| |_) / _ \ __/ _` | '__/ _` |/ _ \/ _` |   / _ \ | '_ \ / _ \   | |/ / _` |
/// __/ _ \ / _` |/ _ \ |  _ <  __/ || (_| | | | (_| |  __/ (_| |  / ___ \| |_)
//|  __/   |   < (_| | | (_| (_) | (_| |  __/
//|_| \_\___|\__\__,_|_|  \__,_|\___|\__,_| /_/   \_\ .__/ \___|   |_|\_\__,_|
//\___\___/ \__,_|\___|
//                                                  | |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<unsigned long long int> vul;
#define INF 1000000000
typedef std::numeric_limits<double> dbl;

double d = 3.14159265358979;
ull f = 1000000007;

int c[100000];

ll haha(int a[], int n, int k) {
  if (n <= k) {
    int b[n];
    for (int i = 0; i < n; i++) {
      if (c[i] == -1) {
        c[i] = haha(a, i, k);
        b[i] = abs(a[n] - a[i]) + c[i];
      } else {
        b[i] = abs(a[n] - a[i]) + c[i];
      }
    }
    sort(b, b + n);
    return b[0];
  }
  int b[k];
  for (int i = 1; i <= k; i++) {
    if (c[n - i] == -1) {
      c[n - i] = haha(a, n - i, k);
      b[i - 1] = abs(a[n] - a[n - i]) + c[n - i];
    } else {
      b[i - 1] = abs(a[n] - a[n - i]) + c[n - i];
    }
  }
  sort(b, b + k);
  return b[0];
}

int main() {
  for (int i = 0; i < 100000; i++) {
    c[i] = -1;
  }
  c[0] = 0;

  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << haha(a, n - 1, k);
}