Problem:
オレンジの出荷(Oranges)
あなたはJuicy Orange Industry 社を知っているだろうか? この会社の業務は美味しいオレンジを栽培して出荷することである．ここでは略してJOI 社と呼ぶ．
JOI 社では，収穫された $N$ 個のオレンジを箱詰めして出荷することになった．オレンジは工場にあるベルトコンベアの上に並べられており，ベルトコンベアの前から順番に $1$ から $N$ までの番号が付けられている．オレンジは大小さまざまであり，オレンジ $i$ $(1 \leq i \leq N)$ の大きさは $A_i$ である．
これらのオレンジを前から順番にいくつかの箱に分けて詰める．ひとつの箱には連続した番号のオレンジしか詰めることができない．
ひとつの箱には最大で $M$ 個までのオレンジを詰めることができる．ある箱にいくつかのオレンジを詰めるためにかかるコストは，箱に詰める最大のオレンジの大きさを $a$，箱に詰める最小のオレンジの大きさを $b$，箱に詰めるオレンジの個数を $s$ としたときに，$K + s \times (a - b)$ で求めることができる．ここで，$K$ は箱にかかるコストであり，すべての箱で共通の値である．
適切な個数の箱を用意して，すべてのオレンジを適切に箱詰めすることで，箱詰めにかかるコストの総和をできるだけ小さくしたい．
課題
ベルトコンベア上に並んでいるオレンジの情報と，ひとつの箱に詰められるオレンジの個数の最大値および，箱にかかるコストが与えられたとき，箱詰めにかかるコストの総和の最小値を求めるプログラムを作成せよ．
入力
標準入力から以下の入力を読み込め．
1 行目には，3 個の整数 $N, M, K$ が空白を区切りとして書かれている．これは，オレンジが $N$ 個あり，ひとつの箱に詰められるオレンジの個数の最大値が $M$ であって，箱にかかるコストが $K$ であることを表す．
続く $N$ 行のうちの $i$ 行目 $(1 \leq i \leq N)$ には，整数 $A_i$ が書かれている．これは，オレンジ $i$ の大きさが $A_i$ であることを表す．
出力
標準出力に，箱詰めにかかるコストの総和の最小値を 1 行で出力せよ．
制限
すべての入力データは以下の条件を満たす．
$1 \leq N \leq 20 000$
$1 \leq M \leq 1 000$
$0 \leq K \leq 1 000 000 000$
$1 \leq A_i \leq 1 000 000 000$ $(1 \leq i \leq N)$
$M \leq N$
入出力例
入力例1
6 3 6
1
2
3
1
2
1
出力例1
21
$1$ 番目の箱にオレンジ $1$ からオレンジ $3$ までの $3$ 個のオレンジを詰め，$2$ 番目の箱にオレンジ $4$ からオレンジ $6$ までの $3$ 個のオレンジを詰めると，箱詰めにかかるコストの総和は $(6+3 \times (3 - 1))+(6+3 \times (2 - 1)) = 21$ となる．
どのように詰めても箱詰めにかかるコストの総和が $21$ を下回ることはないので， $21$ を出力する．
入力例2
16 4 12
3
10
13
10
19
9
12
16
11
2
19
9
13
2
13
19
出力例2
164
$11$ 個の箱を用意して，それぞれの箱に前から順に $1$ 個，$3$ 個，$1$ 個，$1$ 個，$3$ 個，$1$ 個，$1$ 個，$2$ 個，$1$ 個，$1$ 個，$1$ 個のオレンジを詰めることで，箱詰めにかかるコストの総和が最小となる．
入力例3
16 6 14
19
7
2
15
17
7
14
12
3
14
5
10
17
20
19
12
出力例3
177
入力例4
10 1 1000000000
1
1
1
1
1
1
1
1
1
1
出力例4
10000000000
答えが32 ビット符号付き整数の範囲に収まるとは限らないことに注意せよ．
第15回 日本情報オリンピック本選 課題   2016 年 2 月 14 日

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
int N, M, K, a[20009]; long long dp[20009];
int main() {
	cin >> N >> M >> K;
	for(int i = 0; i < N; i++) cin >> a[i];
	dp[0] = 0;
	for(int i = 1; i <= N; i++) {
		int l = 1999999999, r = -l;
		dp[i] = 1LL << 60;
		for(int j = 1; j <= M && i - j >= 0; j++) {
			l = min(l, a[i - j]);
			r = max(r, a[i - j]);
			dp[i] = min(dp[i], dp[i - j] + K + 1LL * j * (r - l));
		}
	}
	cout << dp[N] << endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3672
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main(){
    int n,m,k;
    cin >> n >> m >> k;
    vector<int> a(n);
    rep(i,n){
        cin >> a[i];
    }

    vector<long long> dp(n+1,LLONG_MAX);
    dp[0]=0;
    for(int i=1;i<=n;i++){
        int cur_max=-1;
        int cur_min=1000000001;
        for(int j=i-1;j>=max(0,i-m);j--){
            cur_max=max(cur_max,a[j]);
            cur_min=min(cur_min,a[j]);
            int s=i-j;
            long long cost=k+(long long)s*(cur_max-cur_min);
            dp[i]=min(dp[i],dp[j]+cost);
        }
    }
    cout << dp[n] << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3520
final_rank: 11

code_snippet: #include <bits/stdc++.h>


using namespace std;
//make -f ../makefile SRC=
/*
dp
*/


//------------------------------------------------------------------------------
bool DEBUG = false;
//const int INF = 2147483647;
const int64_t INF = 1e18;
const int BIG = 2147483647;

const int MAX_N = 20000 +5;
static int64_t vect[MAX_N];
static int64_t S[MAX_N];
//------------------------------------------------------------------------------
int64_t dp(int N, int M, int K, int i)
{
    if (S[i] != -1) return S[i];
    int64_t& res = S[i];
    res = INF;

    int64_t max_v = 0;
    int64_t min_v = INF;
    for (int num=1; num<=min(M,i+1); ++num)
    {
        max_v = max(max_v, vect[i-num+1]);
        min_v = min(min_v, vect[i-num+1]);
        res = min(res, dp(N, M, K, i-num) + K + num*(max_v-min_v));
    }
    
    return res;
}
//------------------------------------------------------------------------------
int64_t solve(int N, int M, int64_t K)
{
    //--------------------------------------------------------------------------
    // base cases:
    if (N == 1) return K;
    else if (N == 2)
    {
        if (M == 1) return 2*K;
        return min(2*K, K + abs(vect[0]-vect[1])*2);
    }
    else if (M == 1) return K*N;
    //--------------------------------------------------------------------------
    // init:
    for (int i=0; i<N; ++i) S[i] = -1;
    S[0] = K;
    S[1] = min(2*K, K + abs(vect[0]-vect[1])*2);
    //--------------------------------------------------------------------------
    // compute:
    int64_t res = dp(N, M, K, N-1);

    //--------------------------------------------------------------------------
    // report:
    return res;
}

//------------------------------------------------------------------------------
void test()
{

}

//------------------------------------------------------------------------------
int main()
{
    //test(); return 0;
    //DEBUG = true;
    //--------------------------------------------------------------------------
    int N, M, num;
    int64_t K;
    num = scanf("%d %d %ld ", &N, &M, &K);
    for (int i=0; i<N; ++i) num = scanf("%ld ", &vect[i]);
    int64_t res = solve(N, M, K);
    printf("%ld\n", res);
    //--------------------------------------------------------------------------
    return 0;
}
//------------------------------------------------------------------------------
accuracy: 1
time_ms: 50
memory_kb: 5744
final_rank: 16

code_snippet: #include <bits/stdc++.h>

using namespace std;

using LL = long long;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    LL n, m, k;
    cin >> n >> m >> k;

    vector<LL> a(n + 1, 0);
    for (int i = 1; i <= n; ++i) cin >> a[i];

    vector<vector<LL>> dp(n + 1, vector<LL>(m + 1, LONG_LONG_MAX));
    for (int i = 0; i <= m; ++i) dp[0][i] = 0;
    dp[1][1] = k;

    vector<LL> min_cost(n + 1, LONG_LONG_MAX);
    min_cost[0] = 0;
    min_cost[1] = k;
    for (LL i = 2; i <= n; ++i) {
        LL mn, mx;
        mn = mx = a[i];
        for (LL j = 1; j <= min(i, m); ++j) {
            LL cost = k + j * (mx - mn);
            dp[i][j] = cost + min_cost[i - j];
            min_cost[i] = min(min_cost[i], dp[i][j]);
            mn = min(mn, a[i - j]);
            mx = max(mx, a[i - j]);
        }
    }
    cout << min_cost[n] << endl;
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 160440
final_rank: 18

code_snippet: #include <vector>
#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define fr first
#define sc second
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep1(i, x) for(ll i = 1; i <= x; i++)
const ll INF = 0xfffffffffffffff; // 大きな値を設定

ll N, M, K;
ll A[20001]; // オレンジの大きさを格納する配列
ll dp[20001]; // dp[i]: i番目のオレンジまでの最小コスト

int main() {
    // 入力の読み込み
    scanf("%lld %lld %lld", &N, &M, &K);
    rep1(i, N) {
        scanf("%lld", &A[i]);
        dp[i] = INF; // 初期化
    }
    dp[0] = 0; // 初期状態

    // 動的計画法で最小コストを計算
    rep1(i, N) {
        P p = P(INF, -INF); // p.first は最小値、p.second は最大値
        rep(j, min(M, i)) { // 最大で M 個のオレンジを含む区間を考慮
            p = P(min(p.fr, A[i - j]), max(p.sc, A[i - j]));
            dp[i] = min(dp[i], dp[i - j - 1] + (p.sc - p.fr) * (j + 1) + K);
        }
    }

    // 最小コストを出力
    printf("%lld\n", dp[N]);
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3856
final_rank: 5

code_snippet: #line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

template <typename T>
T POP(queue<T> &que) {
  T a = que.front();
  que.pop();
  return a;
}
template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
f128 binary_search_real(F check, f128 ok, f128 ng, ll iter = 100) {
  rep(iter) {
    f128 x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  rep(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vc<T> cumsum(vc<U> &A, ll off = 1) {
  ll N = A.size();
  vc<T> B(N + 1);
  rep(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vi argsort(const vector<T> &A) {
  vi ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](ll i, ll j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vi &I) {
  vc<T> B(len(I));
  rep(i, len(I)) B[i] = A[I[i]];
  return B;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  if (x < 0) { cout << (i64)(x); }
  else { cout << (u64)(x); }
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first); wt(' '); wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 2 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {break;}
  return 0;
}

int solve() {
  ll N, M, K;
  cin >> N >> M >> K;
  vi A(N);
  rep(i, N) cin >> A[i];
  vv(ll, cost, N+1, M+1, 0);
  rep(i, N) {
    ll amin = inf;
    ll amax = -inf;
    rep(j, i, min(N, i+M)) {
      chmin(amin, A[j]);
      chmax(amax, A[j]);
      cost[i][j-i+1] = K + (amax-amin)*(j-i+1);
    }
  }
  vi dp(N+1, inf);
  dp[0] = 0;
  rep(i, N) {
    rep(j, i+1, min(N+1, i+M+1)) {
      chmin(dp[j], dp[i] + cost[i][j-i]);
    }
  }
  print(dp[N]);
  return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 160488
final_rank: 19

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cassert>
#include <chrono>
#include <random>
#include <set>
#include <utility>
#include <array>
#include <bitset>
#include <unordered_set>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,m; cin >> n >> m;
    ll k; cin >> k;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    vector<ll> dp(n+1, 1e18);
    dp[0] = 0;
    for (int i = 0; i < n; ++i) {
        ll mi = a[i], mx = a[i];
        for (int j = 0; j < m and i+j < n; ++j) {
            mi = min(mi, a[i+j]);
            mx = max(mx, a[i+j]);
            dp[i+j+1] = min(dp[i+j+1], dp[i]+k+(mx-mi)*(j+1));
        }
    }
    cout << dp[n] << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3492
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
#define all(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N,M,K;
    cin>>N>>M>>K;
    vll A(N);
    rep(i,N){
        cin>>A[i];
    }
    vll DP(N+1,1e18);
    DP[0]=0;
    rep(i,N){
        ll MA=-1e18;
        ll mi=1e18;
        for(ll m=1;m<=M;m++){
            if(i+m>N)break;
            chmax(MA,A[i+m-1]);
            chmin(mi,A[i+m-1]);
            chmin(DP[i+m],DP[i]+K+m*(MA-mi));
        }
    }
    cout<<DP[N]<<endl;
    
}
accuracy: 1
time_ms: 30
memory_kb: 3496
final_rank: 10

code_snippet: #include <bits/stdc++.h>

int main() {
  using i64 = long long;

  int n, m;
  i64 k;
  std::cin >> n >> m >> k;

  std::vector< i64 > as(n);
  for (auto &a: as) std::cin >> a;

  constexpr i64 inf = 1001001001001001001ll;
  std::vector< i64 > dp(n + 1, inf);
  dp[0] = 0;

  for (int i = 1; i <= n; i++) {
    i64 max = -1;
    i64 min = inf;

    for (int s = 1; s <= m; s++) {
      if (i < s) break;
      max = std::max(max, as[i - s]);
      min = std::min(min, as[i - s]);
      dp[i] = std::min(dp[i], dp[i - s] + k + s * (max - min));
    }
  }

  std::cout << dp.back() << std::endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3480
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll INF = (ll)1e18;

int main() {
    int N, M, K; cin >> N >> M >> K;
    vector<int> A(N);
    for (auto& a : A) cin >> a;
    vector<ll> dp(N + 1, INF);
    dp[0] = 0;
    for (int i = 1 ; i <= N ; i++) {
        ll mn = INF, mx = -INF;
        for (int j = 1 ; j <= M ; j++) {
            if (i - j < 0) break;
            mn = min<ll>(mn, A[i - j]);
            mx = max<ll>(mx, A[i - j]);
            ll cost = dp[i - j] + K + (ll)j * (mx - mn);
            dp[i] = min(dp[i], cost);
        }
    }

    cout << dp[N] << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3524
final_rank: 2

code_snippet: #include<bits/stdc++.h>

using namespace std;
using ll = long long;

ll func(){
    int n;
    cin >> n;
    int m;
    cin >> m;
    int k;
    cin >> k;
    vector<ll> dp(n+1,1e18);
    dp[0] = 0;
    vector<int> as(n);
    for(auto &i:as)cin >> i;
    for(int i=0;i<dp.size();++i){
        ll maxs = -1;
        ll mins = 1e9;
        for(int j=i+1;j<dp.size() and (j-i)<=m;++j){
            maxs = max<ll>(maxs, as[j-1]);
            mins = min<ll>(mins, as[j-1]);
            dp[j] = min<ll>(dp[j], dp[i] + (j-i) * (maxs-mins) + k);
        }
    }
    return dp.back();
}

int main(){
    cout << func() << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3512
final_rank: 1

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0625.cc: 
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 20000;
const int MAX_M = 1000;
const int INF = 1 << 30;
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;

/* global variables */

int as[MAX_N];
ll dp[MAX_N + 1];

/* subroutines */

inline void setmin(ll &a, ll b) { if (a > b) a = b; }

/* main */

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  fill(dp, dp + n + 1, LINF);
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    int mina = INF, maxa = 0;
    for (int j = 0; j < m && i + j < n; j++) {
      mina = min(mina, as[i + j]);
      maxa = max(maxa, as[i + j]);
      ll d = dp[i] + k + (ll)(maxa - mina) * (j + 1);
      setmin(dp[i + j + 1], d);
    }
  }

  printf("%lld\n", dp[n]);
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3192
final_rank: 6

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<numeric>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<random>
#include<bitset>
#include<cassert>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int dx[]={1,0,0,-1},dy[]={0,1,-1,0};
ll dp[20001];
int main(){
	int n,m,k,a[20000];
	cin>>n>>m>>k;
	for(int i=0;i<n;i++)
		cin>>a[i];
	fill(dp+1,dp+n+1,1e18);
	for(int i=0;i<n;i++){
		int mx=0,mn=1e9;
		for(int j=1;j<=m;j++){
			if(i+j<=n){
				mx=max(mx,a[i+j-1]);
				mn=min(mn,a[i+j-1]);
				dp[i+j]=min(dp[i+j],dp[i]+k+(ll)j*(mx-mn));
			}
		}
	}
	cout<<dp[n]<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3652
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
typedef long long ll;
//typedef int ll;
typedef pair<ll, ll> P;
const long long INF = 1e17;
template <typename T>
void input_arr(vector<T>& A, ll N) {
	for (ll i = 0; i < N; i++) {
		cin >> A[i];
	}
}
template <typename T, typename Q>
void input_arr(vector<pair<T, Q>>& A, ll N) {
	for (ll i = 0; i < N; i++) {
		cin >> A[i].first >> A[i].second;
	}
}
template <typename T>
void input_arr(vector<vector<T>>& A, ll H, ll W) {
	for (ll i = 0; i < H; i++) {
		for (ll j = 0; j < W; j++) {
			cin >> A[i][j];
		}
	}
}
void solve() {
    ll N, M, K; cin >> N >> M >> K;
    vector<int>A(N); input_arr(A, N);
    vector<vector<int>>Mn(N, vector<int>(M + 1)), Mx(N, vector<int>(M + 1));
    for (ll i = 0; i < N; i++) {
        Mn[i][0] = A[i];
        Mx[i][0] = A[i];
    }
    //m[j][i] = [i, i + j] の min とか max とか
    for (ll i = 0; i < N; i++) {
        for (ll j = 1; j <= M; j++) {
            if (i + j >= N) continue;
            Mn[i][j] = min(Mn[i][j - 1], A[i + j]);
            Mx[i][j] = max(Mx[i][j - 1], A[i + j]);
        }
    }
    //dp[i] = i 個目のオレンジまでを梱包したときのコストの総和の最小値
    ll dp[20100];
    dp[0] = 0;
    for (ll i = 1; i <= N; i++) {
        dp[i] = INF;
        for (ll j = 1; j <= M && i - j >= 0; j++) {
            dp[i] = min(dp[i], dp[i - j] + K + j * (ll)(Mx[i - j][j - 1] - Mn[i - j][j - 1]));
        }
    }
    cout << dp[N] << endl;
}
int main() {
    ll T=1; //cin >> T;
    while (T--) solve();
}
accuracy: 1
time_ms: 210
memory_kb: 161248
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcount(v)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=1LL<<60;
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){return (a+b-1)/b;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

template<class T,T (*op)(T, T)> struct SparseTable {
    vector<vector<T>> table;
    vector<int> lookup;
    int LOGV=0, _n;
    SparseTable(const vector<T> &v) {
        _n=v.size();
        while(_n>>LOGV)LOGV++;
        table.assign(LOGV,vector<T>(_n));
        lookup.resize(_n+1);
        for(int i = 0; i < _n; i++) table[0][i] = v[i];
        for(int i = 1; i < LOGV; i++) {
            for(int j = 0; j + (1 << i) <= _n; j++) {
                table[i][j] = op(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
            }
        }
        lookup[1]=0;
        for(int i = 2; i < lookup.size(); i++) {
            lookup[i] = lookup[i >> 1] + 1;
        }
    }
    inline T prod(int l, int r){
        assert(0 <= l && l < r && r <= _n);
        int b = lookup[r - l];
        return op(table[b][l],table[b][r - (1<<b)]);
    }
};

ll op1(ll a,ll b){return min(a,b);}
ll op2(ll a,ll b){return max(a,b);}

int main(){
    LL(n,m,k);
    vector<ll> a(n);
    in(a);
    SparseTable<ll,op1> st1(a);
    SparseTable<ll,op2> st2(a);
    vector<ll> dp(n+1,INF2);
    dp[0]=0;
    for(int i=0;i<n;i++){
        for(int j=1;j<=m&&i+j<=n;j++){
            chmin(dp[i+j],dp[i]+k+j*(st2.prod(i,i+j)-st1.prod(i,i+j)));
        }
    }
    out(dp[n]);
}
accuracy: 1
time_ms: 60
memory_kb: 8508
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using lint = long long;
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, l, n) for(int i = int(l); i < int(n); i++)
#define repr(i, n) for(int i = int(n) - 1; i >= 0; i--)
#define repr2(i, l, n) for(int i = int(n) - 1; i >= int(l); i--)

const lint inf = 1e18;

int main(){
    int n, m;
    lint k;
    cin >> n >> m >> k;
    lint a[n];
    rep(i, n) cin >> a[i];
    
    lint dp[20202];
    rep(i, 20202) dp[i] = inf;
    dp[0] = 0;
    
    rep(i, n){
        lint maxi = -inf, mini = inf;
        for(int j = 1; j <= m && i + j <= n; j++){
            maxi = max(maxi, a[i + j - 1]);
            mini = min(mini, a[i + j - 1]);
            
            dp[i + j] = min(dp[i + j], dp[i] + k + j * (maxi - mini));
        }
    }
    
    cout << dp[n] << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3752
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using lli = long long int;
const lli inf = 1e18;

int main(){
    lli n,m,k;
    cin >> n >> m >> k;
    vector<lli> a(n);
    for(int i=0; i<n; i++){
        cin >> a[i];
    }
    vector<lli> dp(n+1, inf);
    dp[0] = 0;
    for(int i=0; i<n; i++){
        lli vmin = inf;
        lli vmax = -inf;
        for(int j=0; j<m and i+j<n; j++){
            vmin = min(vmin, a[i+j]);
            vmax = max(vmax, a[i+j]);
            dp[i+j+1] = min(dp[i+j+1], dp[i] + k+(j+1)*(vmax-vmin));
        }
    }
    cout << dp[n] << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3476
final_rank: 7

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n, m, k; cin >> n >> m >> k;
  vector<int> orange(n); REP(i, n) cin >> orange[i];
  vector<ll> dp(n + 1, LINF);
  dp[0] = 0;
  REP(i, n) {
    int a = 0, b = INF;
    for (int j = 1; j <= m && i + j <= n; ++j) {
      chmax(a, orange[i + j - 1]);
      chmin(b, orange[i + j - 1]);
      chmin(dp[i + j], dp[i] + k + 1LL * j * (a - b));
    }
  }
  cout << dp[n] << '\n';
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3544
final_rank: 12

code_snippet: #include<cstdio>
#include<algorithm>
using namespace std;
long long dp[20001]={0};
int main(void)
{
	int n,a[20001],m,k,i,j,mn,mx;
	scanf("%d %d %d",&n,&m,&k);
	for(i=1;i<=n;i++){
		scanf("%d",&a[i]);
		dp[i]=0;
	}
	dp[0]=0;
	for(i=1;i<=n;i++){
		mn=1000000001;
		mx=0;
		for(j=0;j<m;j++){
			if(i+j>n){
				break;
			}
			mn=min(mn,a[i+j]);
			mx=max(mx,a[i+j]);
			dp[i+j]=min(dp[i+j],(long long)(mx-mn)*(j+1)+k+dp[i-1]);
			if(dp[i+j]==0){
				dp[i+j]=(long long)(mx-mn)*(j+1)+k+dp[i-1];
			}
		}
	}
	printf("%lld\n",dp[n]);
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 2988
final_rank: 14

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 20005

ll N,M,K;
ll dp[SIZE];
ll A[SIZE];

ll recursive(ll left){

	if(dp[left] != -1){

		return dp[left];
	}

	ll num = 0;
	ll maxi = -HUGE_NUM;
	ll mini = HUGE_NUM;

	ll ret = HUGE_NUM;

	for(ll right = left; right <= min(N,left+M-1); right++){
		num++;
		maxi = max(maxi,A[right]);
		mini = min(mini,A[right]);

		ret = min(ret,K+num*(maxi-mini)+recursive(right+1));
	}

	return dp[left] = ret;
}

int main(){

	scanf("%lld %lld %lld",&N,&M,&K);

	A[0] = 0;
	for(ll i = 1; i <= N; i++){

		scanf("%lld",&A[i]);
	}

	for(int i = 0; i <= N; i++){

		dp[i] = -1;
	}

	dp[N+1] = 0;

	recursive(1);

	printf("%lld\n",dp[1]);

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 5084
final_rank: 15