inline ll rd(){ ll x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 407
int testcase;
ll n, d, f[N][N][2], a[N];
inline ll dis(ll a, ll b){ ll w = abs(a - b); return min(w, d - w); }
inlinevoidgetmin(ll &a, ll b){a = (a < b ? a : b);}
inlinevoidwork(){ n = rd(); d = rd(); memset(f, 0x3f, sizeof(f)); for (int i = 1; i <= n; ++i) { a[i] = rd(); f[i][i][0] = f[i][i][1] = 0; } for (int len = 1; len < n; ++len) { for (int l = 1; l <= n - len + 1; ++l) { int r = l + len - 1; getmin(f[l - 1][r][0], f[l][r][0] + dis(a[l], a[l - 1])); getmin(f[l - 1][r][0], f[l][r][1] + dis(a[r], a[l - 1])); getmin(f[l][r + 1][1], f[l][r][0] + dis(a[l], a[r + 1])); getmin(f[l][r + 1][1], f[l][r][1] + dis(a[r], a[r + 1])); } } ll ans = min(f[1][n][0] + dis(a[1], 0), f[1][n][1] + dis(a[n], 0)); printf("Case #%d: %lld\n", ++testcase, ans); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 1007 #define fr first #define sc second
pair<int, double> p[N];
db ans, sum[N], f[N][N][2];
inlinevoidgetmin(db &a, db b){a = (a < b ? a : b);}
intmain(){ int n = rd(), c = rd(); for (int i = 1; i <= n; ++i) p[i].fr = rd(); for (int i = 1; i <= n; ++i) ans += rd() / 1000.0; for (int i = 1; i <= n; ++i) p[i].sc = rd() / 1000.0; p[++n] = make_pair(c, 0); sort(p + 1, p + 1 + n); for (int l = 1; l <= n; ++l) for (int r = 1; r <= n; ++r) for (int k = 0; k < 2; ++k) f[l][r][k] = 1e18; for (int i = 1; i <= n; ++i) { sum[i] = sum[i - 1] + p[i].sc; if (p[i].fr == c) f[i][i][0] = f[i][i][1] = 0; }
for (int len = 1; len < n; ++len) for (int l = 1; l <= n - len + 1; ++l) { int r = l + len - 1; db rsum = sum[n] - sum[r] + sum[l - 1]; getmin(f[l - 1][r][0], f[l][r][0] + (p[l].fr - p[l - 1].fr) * rsum); getmin(f[l - 1][r][0], f[l][r][1] + (p[r].fr - p[l - 1].fr) * rsum); getmin(f[l][r + 1][1], f[l][r][0] + (p[r + 1].fr - p[l].fr) * rsum); getmin(f[l][r + 1][1], f[l][r][1] + (p[r + 1].fr - p[r].fr) * rsum); } printf("%.3lf\n", ans - min(f[1][n][0], f[1][n][1])); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
inlinevoidgetmin(int &a, int b){a = (a < b ? a : b);}
#define N 5007
int a[N], f[N][N], lst[N], nxt[N];
inlinevoidwork(){ int n = rd(), tot = 0; for (int i = 1, x; i <= n; ++i) { x = rd(); if (!tot || x != a[tot]) a[++tot] = x; lst[i] = n + 1; } n = tot; for (int i = n; i; --i) { nxt[i] = lst[a[i]]; lst[a[i]] = i; } for (int len = 2; len <= n; ++len) for (int l = 1; l <= n - len + 1; ++l) { int r = l + len - 1; f[l][r] = f[l + 1][r] + 1; for (int j = nxt[l]; j <= r; j = nxt[j]) getmin(f[l][r], f[l + 1][j - 1] + f[j][r] + 1); } printf("%d\n", f[1][n]); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 207
int a[N], b[N], testcase;
ll f[N][N];
inlinevoidwork(){ int n = rd(); for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = 1; i <= n; ++i) b[i] = rd(); b[n + 1] = 0; for (int len = 1; len <= n; ++len) for (int l = 1; l <= n - len + 1; ++l) { int r = l + len - 1; f[l][r] = 1e18; for (int p = l; p <= r; ++p) f[l][r] = min(f[l][r], f[l][p - 1] + f[p + 1][r] + a[p] + b[l - 1] + b[r + 1]); } printf("Case #%d: %lld\n", ++testcase, f[1][n]); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 107
int testcase;
ll d[N], sum[N], f[N][N];
inlinevoidgetmin(ll &a, ll b){a = (a < b ? a : b);}
inlinevoidwork(){ int n = rd(); for (int i = 1; i <= n; ++i) { d[i] = rd(); sum[i] = sum[i - 1] + d[i]; } for (int len = 2; len <= n; ++len) for (int l = 1; l <= n - len + 1; ++l) { int r = l + len - 1; f[l][r] = 1e18; for (int k = 1; k <= len; ++k) getmin(f[l][r], f[l + 1][l + k - 1] + d[l] * (k - 1) + f[l + k][r] + (sum[r] - sum[l + k - 1]) * k); } printf("Case #%d: %lld\n", ++testcase, f[1][n]); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
vector<int> s;
unordered_map<int, int> tr;
#define N 73
structnode {int k, v, t;} c[N];
ll f[N][N][N], sumt[N];
inlinevoidgetmin(ll &a, ll b){a = (a < b ? a : b);}
intmain(){ int n = rd(), w = rd(); for (int i = 1; i <= n; ++i) c[i].k = rd(); for (int i = 1; i <= n; ++i) s.push_back(c[i].v = rd()); for (int i = 1; i <= n; ++i) c[i].t = rd(); sort(s.begin(), s.end()); int cnt = 0; for (auto i : s) tr[i] = ++cnt; for (int i = 1; i <= n; ++i) c[i].v = tr[c[i].v]; sort(c + 1, c + 1 + n, [](node x, node y){return x.k < y.k;}); for (int i = 1; i <= n; ++i) sumt[i] = sumt[i - 1] + c[i].t;
for (int len = 1; len <= n; ++len) for (int l = 1; l <= n - len + 1; ++l) { int r = l + len - 1; for (int k = 0; k <= n; ++k) { f[l][r][k] = 1e18; for (int rt = l; rt <= r; ++rt) { if (c[rt].v >= k) getmin(f[l][r][k], f[l][rt - 1][c[rt].v] + f[rt + 1][r][c[rt].v]); getmin(f[l][r][k], f[l][rt - 1][k] + f[rt + 1][r][k] + w); } f[l][r][k] += sumt[r] - sumt[l - 1]; } } ll ans = 1e18; for (int k = 0; k <= n; ++k) ans = min(ans, f[1][n][k]); printf("%lld\n", ans); return0; }
inline ll rd(){ ll x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 507 #define mod 998244353
ll a[N], f[N][N];
intmain(){ int n = rd(); for (int i = 1; i <= n; ++i) { a[i] = rd(); f[i][i] = 1; } for (int len = 2; len < n; ++len) { for (int l = 2; l <= n - len + 1; ++l) { int r = l + len - 1; f[l][r] = f[l + 1][r]; for (int k = l + 1; k <= r; ++k) if (a[k] > a[l]) f[l][r] = (f[l][r] + 1ll * max(1ll, f[l + 1][k - 1]) * f[k][r]) % mod; } } printf("%lld\n", f[2][n]); return0; }