text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
long long mx[] = {+1, 0, -1, -1, 0, +1};
long long my[] = {+1, +1, 0, -1, -1, 0};
long long c[6];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y;
for (int i = 0; i < 6; ++i) cin >> c[i];
long long ans = LLONG_MAX;
for (int i = 0; i < 6; ++i) {
for (int j = i + 1; j < 6; ++j) {
long long det = mx[i] * my[j] - my[i] * mx[j];
if (det == 0) continue;
long long detA = x * my[j] - y * mx[j], detB = mx[i] * y - my[i] * x;
long long A = detA / det, B = detB / det;
if (A >= 0 && B >= 0 && (mx[i] * A + mx[j] * B == x) &&
(my[i] * A + my[j] * B == y))
ans = min(ans, A * c[i] + B * c[j]);
}
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> q;
while (q--) {
long long x, y;
cin >> x >> y;
array<long long, 7> c;
for (int i = 1; i <= 6; i++) cin >> c[i];
swap(c[2], c[6]);
swap(c[3], c[5]);
long long res = (1ll << 62);
if (x >= 0 && y >= 0) res = min(res, x * c[2] + y * c[6]);
if (x <= 0 && y >= 0) res = min(res, (-x) * c[5] + y * c[6]);
if (x <= 0 && y <= 0) res = min(res, (-x) * c[5] + (-y) * c[3]);
if (x >= 0 && y <= 0) res = min(res, x * c[2] + (-y) * c[3]);
if (x == y && x >= 0) res = min(res, x * c[1]);
if (x == y && x <= 0) res = min(res, (-x) * c[4]);
if (y > 0) {
long long t = x - y;
if (t >= 0) res = min(res, t * c[2] + y * c[1]);
if (t <= 0) res = min(res, (-t) * c[5] + y * c[1]);
}
if (y < 0) {
long long t = x - y;
if (t >= 0) res = min(res, t * c[2] + (-y) * c[4]);
if (t <= 0) res = min(res, (-t) * c[5] + (-y) * c[4]);
}
if (x > 0) {
long long t = y - x;
if (t >= 0) res = min(res, t * c[6] + x * c[1]);
if (t <= 0) res = min(res, (-t) * c[3] + x * c[1]);
}
if (x < 0) {
long long t = y - x;
if (t >= 0) res = min(res, t * c[6] + (-x) * c[4]);
if (t <= 0) res = min(res, (-t) * c[3] + (-x) * c[4]);
}
cout << res << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
long long int x, y, c1, c2, c3, c4, c5, c6;
vector<long long int> ans(3);
cin >> x >> y;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
if (y > 0)
ans[0] += c2 * y, ans[1] += c1 * y;
else
ans[0] += c5 * abs(y), ans[1] += c4 * abs(y);
if (x > 0)
ans[0] += c6 * x, ans[2] += c1 * x;
else
ans[0] += c3 * abs(x), ans[2] += c4 * abs(x);
if (x > y)
ans[1] += c6 * (x - y), ans[2] += c5 * (x - y);
else
ans[1] += c3 * (y - x), ans[2] += c2 * (y - x);
sort(ans.begin(), ans.end());
cout << ans[0] << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
os << '{';
string sep;
for (const auto &x : arr) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
void run_case() {
int64_t X, Y;
array<int64_t, 6> C;
cin >> X >> Y;
for (int i = 0; i < 6; i++) cin >> C[i];
auto rotate60 = [&] {
int64_t new_X = Y;
int64_t new_Y = Y - X;
X = new_X;
Y = new_Y;
rotate(C.begin(), C.begin() + 1, C.end());
};
while (!(X >= 0 && Y >= X)) rotate60();
int64_t right = Y - X;
int64_t up_right = X;
int64_t answer = C[0] * up_right + C[1] * right;
answer = min(answer, C[0] * (up_right + right) + C[2] * right);
answer = min(answer, C[1] * (right + up_right) + C[5] * up_right);
cout << answer << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tests;
cin >> tests;
while (tests-- > 0) run_case();
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int main() {
int t;
scanf("%d", &t);
while (t--) {
long long x, y, c1, c2, c3, c4, c5, c6;
scanf("%lld%lld", &x, &y);
scanf("%lld%lld%lld%lld%lld%lld", &c1, &c2, &c3, &c4, &c5, &c6);
long long ans = 0;
if (x >= 0 && y >= 0) {
ans = x * c6 + y * c2;
ans = min(ans, x * c1 + abs(x - y) * (x > y ? c5 : c2));
ans = min(ans, y * c1 + abs(x - y) * (x > y ? c6 : c3));
} else if (x >= 0 && y <= 0) {
x = abs(x), y = abs(y);
ans = c6 * x + c5 * abs(y);
ans = min(ans, x * c1 + (x + y) * c5);
ans = min(ans, y * c4 + (x + y) * c6);
} else if (x <= 0 && y >= 0) {
x = abs(x), y = abs(y);
ans = c3 * x + c2 * y;
ans = min(ans, y * c1 + (y + x) * c3);
ans = min(ans, x * c4 + (x + y) * c2);
} else {
x = abs(x), y = abs(y);
ans = x * c3 + y * c5;
ans = min(ans, x * c4 + abs(x - y) * (x > y ? c2 : c5));
ans = min(ans, y * c4 + abs(x - y) * (x > y ? c3 : c6));
}
printf("%lld\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long T, x, y, ans, c[10];
inline long long read() {
long long red = 0, f_f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f_f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') red = red * 10 + ch - '0', ch = getchar();
return red * f_f;
}
signed main() {
T = read();
while (T--) {
x = read(), y = read();
for (long long i = 1; i <= 6; i++) c[i] = read();
for (long long ii = 1; ii <= 10; ii++)
for (long long i = 1; i <= 6; i++) {
long long l = i - 1, r = i + 1;
if (!l) l = 6;
if (r == 7) r = 1;
c[i] = min(c[i], c[l] + c[r]);
}
ans = 0ll;
if (x >= 0 && y >= 0) {
ans = c[1] * min(x, y);
if (x > y)
ans += c[6] * llabs(x - y);
else
ans += c[2] * llabs(x - y);
} else if (x <= 0 && y <= 0) {
if (x < y)
ans = -y * c[4] - (x - y) * c[3];
else
ans = -x * c[4] - (y - x) * c[5];
} else if (x <= 0 && y >= 0)
ans = c[3] * (-x) + c[2] * y;
else if (x >= 0 && y <= 0)
ans = c[6] * x + c[5] * (-y);
printf("%lld\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int mine(long long int a, long long int b) {
if (a < b) return a;
return b;
}
long long int helper(long long int x, long long int plus, long long int minus) {
if (x >= 0) return x * plus;
return minus * (-x);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
long long int x, y;
cin >> x >> y;
long long int cost[6];
for (int i = 0; i < 6; i++) cin >> cost[i];
long long int ans = 5e18 + 1;
ans = mine(ans, helper(x, cost[5], cost[2]) + helper(y, cost[1], cost[4]));
ans = mine(ans,
helper(y, cost[0], cost[3]) + helper(x - y, cost[5], cost[2]));
ans = mine(ans,
helper(x, cost[0], cost[3]) + helper(y - x, cost[1], cost[4]));
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
long long int dist(long long int d1, long long int d2, long long int d3) {
if (d1 >= 0) {
return d1 * d2;
} else {
return d1 * d3 * (-1);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long int var;
cin >> var;
while (var--) {
long long int i, j, k, l, x, y;
cin >> x >> y;
vector<long long int> v(6);
for (i = 0; i < 6; i++) cin >> v[i];
long long int ans = LLONG_MAX;
ans = dist(x, v[5], v[2]) + dist(y, v[1], v[4]);
ans = min(ans, dist(x, v[0], v[3]) + dist(y - x, v[1], v[4]));
ans = min(ans, dist(x - y, v[5], v[2]) + dist(y, v[0], v[3]));
cout << ans << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int up, down, mleft, mright;
int c[10];
long long myabs(long long a) {
if (a < 0) return -a;
return a;
}
void solve() {
long long x, y;
cin >> x >> y >> c[1];
swap(x, y);
cin >> mright >> down >> c[4] >> mleft >> up;
c[1] = min(c[1], mright + up);
c[4] = min(c[4], mleft + down);
for (int i = (1), iend = (20); i <= iend; i++)
mright = min(mright, c[1] + down), up = min(up, c[1] + mleft),
mleft = min(mleft, c[4] + up), down = min(down, c[4] + mright);
long long ans = 0;
if (x < 0 && y < 0) {
int used = min(myabs(x), myabs(y));
ans = 1ll * used * c[4] + 1ll * (myabs(x) - used) * mleft +
1ll * (myabs(y) - used) * down;
} else if (x > 0 && y > 0) {
int used = min(myabs(x), myabs(y));
ans = 1ll * used * c[1] + 1ll * (myabs(x) - used) * mright +
1ll * (myabs(y) - used) * up;
} else {
if (x > 0)
ans += 1ll * myabs(x) * mright;
else
ans += 1ll * myabs(x) * mleft;
if (y > 0)
ans += 1ll * myabs(y) * up;
else
ans += 1ll * myabs(y) * down;
}
cout << ans << endl;
}
int main() {
int t;
cin >> t;
while (t--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y;
long long c1, c2, c3, c4, c5, c6;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
vector<pair<pair<int, int>, long long> > v = {
{{0, 0}, 0}, {{1, 1}, c1}, {{0, 1}, c2}, {{-1, 0}, c3},
{{-1, -1}, c4}, {{0, -1}, c5}, {{1, 0}, c6}};
long long ans = -1;
for (int i = 1; i < 6; i++) {
for (int j = i + 1; j <= 6; j++) {
long long c = v[j].first.first * v[i].first.second -
v[i].first.first * v[j].first.second;
if (c == 0) {
continue;
}
long long a = (v[j].first.first * y - v[j].first.second * x) / c;
long long b = (v[i].first.second * x - v[i].first.first * y) / c;
if (a < 0 || b < 0) {
continue;
}
long long tmp = v[i].second * a + v[j].second * b;
if (ans == -1 || tmp < ans) {
ans = tmp;
}
}
}
cout << ans << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 100005;
long long c[10];
signed main() {
long long t = 1;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y >> c[1] >> c[2] >> c[3] >> c[4] >> c[5] >> c[6];
for (long long i = 2; i <= 5; i++) {
c[i] = min(c[i], c[i + 1] + c[i - 1]);
}
c[1] = min(c[1], c[2] + c[6]);
c[6] = min(c[6], c[1] + c[5]);
for (long long i = 2; i <= 5; i++) {
c[i] = min(c[i], c[i + 1] + c[i - 1]);
}
c[1] = min(c[1], c[2] + c[6]);
c[6] = min(c[6], c[1] + c[5]);
long long ans = 0;
if (x >= 0 && y >= 0) {
x = abs(x);
y = abs(y);
long long k = min(x, y);
ans = min(x, y) * c[1];
x -= k;
y -= k;
ans += x * c[6] + y * c[2];
} else if (x <= 0 && y >= 0) {
x = abs(x);
y = abs(y);
ans += x * c[3] + y * c[2];
} else if (x <= 0 && y <= 0) {
x = abs(x);
y = abs(y);
long long k = min(x, y);
ans = min(x, y) * c[4];
x -= k;
y -= k;
ans += x * c[3] + y * c[5];
} else {
x = abs(x);
y = abs(y);
ans += x * c[6] + y * c[5];
}
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1e9;
long long c[10], dp[10];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y;
for (int i = 1; i <= 6; i++) {
cin >> c[i];
}
c[0] = c[6];
c[7] = c[1];
for (int i = 1; i <= 6; i++) {
dp[i] = min(c[i], c[i - 1] + c[i + 1]);
}
if (x == 0) {
if (y == 0)
cout << 0;
else if (y > 0)
cout << y * dp[2];
else
cout << -y * dp[5];
cout << endl;
} else if (y == 0) {
if (x > 0)
cout << x * dp[6];
else
cout << -x * dp[3];
cout << endl;
} else {
long long ans = 0;
if (x > 0 && y > 0) {
ans = min(x, y) * dp[1];
if (x > y)
ans += (x - y) * dp[6];
else
ans += (y - x) * dp[2];
} else if (x < 0 && y < 0) {
ans = -max(x, y) * dp[4];
if (x > y)
ans += (x - y) * dp[5];
else
ans += (y - x) * dp[3];
} else if (x > 0 && y < 0) {
ans += x * dp[6] - y * dp[5];
} else if (x < 0 && y > 0) {
ans += y * dp[2] - x * dp[3];
}
cout << ans << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int X, Y;
long long f[2][10][2][10];
const long long ooo = 2e18;
struct TEdge {
int dx, dy;
long long cost;
};
vector<TEdge> E;
const int _DX[6] = {1, 0, -1, -1, 0, 1}, _DY[6] = {1, 1, 0, -1, -1, 0};
void enter() {
cin >> X >> Y;
E.resize(6);
for (int d = 0; d < 6; ++d) {
E[d].dx = _DX[d];
E[d].dy = _DY[d];
cin >> E[d].cost;
}
}
const int _M = 3;
bool locked[2][10][2][10];
struct TV {
int i, j;
long long cost;
TV(int i, int j, long long cost) : i(i), j(j), cost(cost) {}
bool operator<(const TV &h) const { return cost > h.cost; }
};
map<pair<int, int>, long long> m;
long long M(int x, int y) {
if (m.count({x, y})) return m[{x, y}];
m[{x, y}] = 0;
long long &r = m[{x, y}];
r = ooo;
if (abs(x) < _M && abs(y) < _M)
r = f[1][x][1][y];
else {
if (abs(x) >= _M && abs(y) >= _M) {
int x1 = x / 2;
int x2 = x - x1;
int y1 = y / 2;
int y2 = y - y1;
r = min(r, M(x1, y1) + M(x2, y2));
r = min(r, M(x1, y2) + M(x2, y1));
} else if (abs(x) >= _M) {
int x1 = x / 2;
int x2 = x - x1;
r = min(r, M(x1, 0) + M(x2, y));
r = min(r, M(x1, y) + M(x2, 0));
} else {
int y1 = y / 2;
int y2 = y - y1;
r = min(r, M(x, y1) + M(0, y2));
r = min(r, M(0, y1) + M(x, y2));
}
}
return r;
}
long long solve() {
int z, x, y, u, v;
for (x = -_M; x <= _M; ++x)
for (y = -_M; y <= _M; ++y) {
f[1][x][1][y] = ooo;
locked[1][x][1][y] = 0;
}
f[1][0][1][0] = 0;
for (z = 26; z >= 0; --z) {
for (x = -2; x <= 2; ++x) {
for (y = -2; y <= 2; ++y) {
for (auto e : E) {
u = x + e.dx;
v = y + e.dy;
f[1][u][1][v] = min(f[1][u][1][v], f[1][x][1][y] + e.cost);
}
}
}
}
long long ans = 0;
m.clear();
ans = M(X, Y);
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
int T;
cin >> T;
for (int t = 1; t <= T; ++t) {
enter();
cout << solve() << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10;
const long long M = 5e4 + 10;
const long long mod = 1e9 + 7;
const long long inf = 0x3f3f3f3f3f3f3f3f;
int main() {
long long t, x, y, c[10];
scanf("%lld", &t);
while (t--) {
scanf("%lld%lld", &x, &y);
for (long long i = 1; i <= 6; ++i) scanf("%lld", &c[i]);
c[1] = min(c[1], c[2] + c[6]);
c[2] = min(c[2], c[1] + c[3]);
c[3] = min(c[3], c[2] + c[4]);
c[4] = min(c[4], c[3] + c[5]);
c[5] = min(c[5], c[4] + c[6]);
c[6] = min(c[6], c[5] + c[1]);
long long minn = inf;
if (x > 0 && y > 0) {
long long tmp = min(x, y) * c[1];
if (x > y)
tmp += c[6] * (x - y);
else
tmp += c[2] * (y - x);
minn = min(minn, tmp);
} else if (x > 0 && y <= 0) {
long long tmp = c[6] * x - c[5] * y;
minn = min(minn, tmp);
} else if (x <= 0 && y > 0) {
long long tmp = c[2] * y - c[3] * x;
minn = min(minn, tmp);
} else {
x = -x, y = -y;
long long tmp = min(x, y) * c[4];
if (x > y)
tmp += (x - y) * c[3];
else
tmp += (y - x) * c[5];
minn = min(minn, tmp);
}
printf("%lld\n", minn);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void rd(T &k) {
k = 0;
int flag = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flag = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
k = k * 10 + c - '0';
c = getchar();
}
k *= flag;
}
const int N = 81;
int n, m, k, s, t;
int main() {
long long t;
scanf("%lld", &t);
while (t--) {
long long x, y;
scanf("%lld %lld", &x, &y);
long long c1, c2, c3, c4, c5, c6;
scanf("%lld %lld %lld %lld %lld %lld", &c1, &c2, &c3, &c4, &c5, &c6);
for (int i = 1; i <= 10; i++) {
c1 = min(c1, c6 + c2);
c2 = min(c2, c1 + c3);
c3 = min(c3, c2 + c4);
c4 = min(c4, c3 + c5);
c5 = min(c5, c4 + c6);
c6 = min(c6, c5 + c1);
}
if (x >= 0 && y >= 0) {
if (x > y)
printf("%lld\n", y * c1 + (x - y) * c6);
else
printf("%lld\n", x * c1 + (y - x) * c2);
} else if (x <= 0 && y <= 0) {
if (x < y)
printf("%lld\n", -y * c4 - (x - y) * c3);
else
printf("%lld\n", -x * c4 - (y - x) * c5);
} else if (x >= 0 && y <= 0)
printf("%lld\n", x * c6 - y * c5);
else if (x <= 0 && y >= 0)
printf("%lld\n", -x * c3 + y * c2);
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long cst[8], val[3][3];
long long dx[] = {1, 0, -1, -1, 0, 1, 1, -1};
long long dy[] = {1, 1, 0, -1, -1, 0, -1, 1};
void dfs(int x, int y) {
for (int i = 0; i < 8; i++) {
int a = dx[i] + x;
int b = dy[i] + y;
if (a >= -1 && a <= 1 && b >= -1 && b <= 1 &&
val[a + 1][b + 1] > val[x + 1][y + 1] + cst[i])
val[a + 1][b + 1] = val[x + 1][y + 1] + cst[i], dfs(a, b);
}
return;
}
int main() {
int ts;
scanf("%d", &ts);
while (ts--) {
long long x, y, res = 0;
scanf("%lld %lld", &x, &y);
for (int i = 0; i < 6; i++) scanf("%lld", &cst[i]);
cst[6] = cst[7] = 1e15;
for (int i = 0; i < 8; i++) val[dx[i] + 1][dy[i] + 1] = 1e15;
dfs(0, 0);
for (int i = 0; i < 8; i++)
cst[i] = val[dx[i] + 1][dy[i] + 1], cerr << i << " " << cst[i] << endl;
if (x >= 0 && y >= 0) {
int mn = min(x, y);
res += cst[0] * mn;
if (x > mn)
res += cst[5] * (x - mn);
else
res += cst[1] * (y - mn);
} else if (x >= 0 && y < 0) {
int mn = min(x, -y);
res += cst[6] * mn;
if (x > mn)
res += cst[5] * (x - mn);
else
res += cst[4] * (-y - mn);
} else if (x < 0 && y >= 0) {
int mn = min(-x, y);
res += cst[7] * mn;
if (-x > mn)
res += cst[2] * (-x - mn);
else
res += cst[1] * (y - mn);
} else {
int mn = min(-x, -y);
res += cst[3] * mn;
if (-x > mn)
res += cst[2] * (-x - mn);
else
res += cst[4] * (-y - mn);
}
printf("%lld\n", res);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll oo = 0x3f3f3f3f3f3f3f3fLL;
void solve() {
ll x, y;
cin >> x >> y;
vector<ll> c(6);
for (auto &k : c) cin >> k;
ll R = min(c[1], c[0] + c[2]);
ll BR = min(c[2], c[1] + c[3]);
ll BL = min(c[3], c[2] + c[4]);
ll L = min(c[4], c[3] + c[5]);
ll TL = min(c[5], c[4] + c[0]);
ll TR = min(c[0], c[5] + c[1]);
ll best;
if (x > 0) {
if (y > 0) {
ll diag = min(x, y);
best = diag * TR;
x -= diag;
y -= diag;
best += x * TL;
best += y * R;
} else if (y == 0) {
best = TL * x;
} else {
y *= -1;
best = x * TL;
best += y * L;
}
} else if (x == 0) {
if (y > 0) {
best = y * R;
} else if (y == 0) {
best = 0;
} else {
y *= -1;
best = y * L;
}
} else {
if (y > 0) {
x *= -1;
best = x * BR;
best += y * R;
} else if (y == 0) {
x *= -1;
best = x * BR;
} else {
x *= -1;
y *= -1;
ll diag = min(x, y);
best = diag * BL;
x -= diag;
y -= diag;
best += x * BR;
best += y * L;
}
}
cout << best << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll t;
cin >> t;
while (t--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
long long absc(long long a) {
if (a < 0) {
return -a;
} else {
return a;
}
}
void solve() {
long long x, y, c[7], ans = 0;
int i;
cin >> x >> y;
for (i = 1; i <= 6; i++) cin >> c[i];
for (int k = 1; k <= 100; k++) {
c[2] = min(c[1] + c[3], c[2]);
c[3] = min(c[2] + c[4], c[3]);
c[4] = min(c[3] + c[5], c[4]);
c[5] = min(c[4] + c[6], c[5]);
c[6] = min(c[5] + c[1], c[6]);
c[1] = min(c[2] + c[6], c[1]);
}
if (x == 0) {
cout << abs(y) * (y < 0 ? c[5] : c[2]) << "\n";
return;
}
if (y == 0) {
cout << abs(x) * (x < 0 ? c[3] : c[6]) << "\n";
return;
}
if (x < 0) {
int mn = min(abs(x), abs(y));
if (y < 0) {
ans = c[4] * mn;
x += mn;
y += mn;
}
ans += abs(x) * c[3] + abs(y) * ((y > 0) ? c[2] : c[5]);
}
if (x > 0) {
int mn = min(x, abs(y));
if (y > 0) {
ans = c[1] * mn;
x -= mn;
y -= mn;
}
ans += x * c[6] + abs(y) * ((y > 0) ? c[2] : c[5]);
}
cout << ans << "\n";
}
int main() {
long long T = 1;
cin >> T;
while (T--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <class T>
int size(T &&a) {
return (int)(a.size());
}
ostream &operator<<(ostream &os, string str) {
for (char c : str) os << c;
return os;
}
template <class A, class B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << "," << p.second << ')';
}
template <class T>
auto operator<<(ostream &os, T &&x) -> decltype(x.begin(), os) {
os << '{';
for (auto it = x.begin(); it != x.end(); ++it)
os << *it << (it == prev(x.end()) ? "" : " ");
return os << '}';
}
template <class T>
ostream &operator<<(ostream &os, vector<vector<T>> vec) {
for (auto x : vec) os << "\n " << x;
return os;
}
void dump() {}
template <class T, class... Args>
void dump(T &&x, Args... args) {
cerr << x << "; ";
dump(args...);
}
void solve() {
int x, y;
cin >> y >> x;
vector<int> c(6);
for (int i = 0; i < (6); ++i) cin >> c[i];
auto f = [&](int k) {
if (k == -1) return 5;
return k % 6;
};
for (int i = 0; i < (6); ++i) c[i] = min(c[i], c[f(i - 1)] + c[f(i + 1)]);
0;
ll ans = 0;
if (ll(x) * y > 0) {
if (x < 0) {
int d1 = min(abs(x), abs(y));
ans += ll(d1) * c[3];
x += d1, y += d1;
} else {
int d2 = min(abs(x), abs(y));
ans += ll(d2) * c[0];
x -= d2, y -= d2;
}
}
0;
if (y < 0) {
y *= -1;
ans += ll(c[2]) * y;
} else {
ans += ll(c[5]) * y;
}
if (x < 0) {
x *= -1;
ans += ll(c[4]) * x;
} else {
ans += ll(c[1]) * x;
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace std;
long long fpow(long long x, long long y, long long p = 1000000007) {
x = x % p;
long long sum = 1;
while (y) {
if (y & 1) sum = sum * x;
sum %= p;
y = y >> 1;
x = x * x;
x %= p;
}
return sum;
}
long long inv(long long a, long long m = 1000000007) {
long long c = m;
long long y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m;
long long t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += c;
return x;
}
long long add(long long a, long long b) {
if (a < 0) a += 1000000007;
if (b < 0) b += 1000000007;
return (a % 1000000007 + b % 1000000007 + 1000000007) % 1000000007;
}
long long sub(long long a, long long b) {
if (a < 0) a += 1000000007;
if (b < 0) b += 1000000007;
return (a % 1000000007 - b % 1000000007 + 1000000007) % 1000000007;
}
long long mul(long long a, long long b) {
if (a < 0) a += 1000000007;
if (b < 0) b += 1000000007;
return ((a % 1000000007) * (b % 1000000007) + 1000000007) % 1000000007;
}
void setIO(string name = "") {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if ((int)(name).size()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
int main() {
setIO();
long long t, x, y, c1, c2, c3, c4, c5, c6;
cin >> t;
while (t--) {
cin >> x >> y;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
long long res = LLONG_MAX, ans;
if (x >= 0) {
ans = c6 * x + ((y >= 0) ? c2 * y : c5 * -y);
res = min(res, ans);
ans = c1 * x + ((y >= x) ? c2 * (y - x) : c5 * (x - y));
res = min(res, ans);
} else {
ans = c3 * -x + ((y >= 0) ? c2 * y : c5 * -y);
res = min(res, ans);
ans = c4 * -x + ((y >= x) ? c2 * (y - x) : c5 * (x - y));
res = min(res, ans);
}
if (y >= 0) {
ans = c2 * y + ((x >= 0) ? c6 * x : c3 * -x);
res = min(res, ans);
ans = c1 * y + ((x >= y) ? c6 * (x - y) : c3 * (y - x));
res = min(res, ans);
} else {
ans = c5 * -y + ((x >= 0) ? c6 * x : c3 * -x);
res = min(res, ans);
ans = c4 * -y + ((x >= y) ? c6 * (x - y) : c3 * (y - x));
res = min(res, ans);
}
cout << res << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
constexpr int di[6] = {1, 0, -1, -1, 0, 1};
constexpr int dj[6] = {1, 1, 0, -1, -1, 0};
void solve() {
int64_t x, y;
cin >> x >> y;
int64_t cost[6], tmp[6];
for (int i = 0; i < 6; i++) cin >> cost[i];
for (int i = 0; i < 6; i++) {
int prv = i == 0 ? 5 : i - 1;
int nxt = i == 5 ? 0 : i + 1;
tmp[i] = cost[prv] + cost[nxt];
}
for (int i = 0; i < 6; i++) {
if (cost[i] > tmp[i]) {
cost[i] = tmp[i];
}
}
int64_t ans = 0;
if (x >= 0 && y >= 0) {
int64_t s = min(x, y);
x -= s, y -= s;
ans = s * cost[0] + x * cost[5] + y * cost[1];
} else if (x < 0 && y < 0) {
x = -x, y = -y;
int64_t s = min(x, y);
x -= s, y -= s;
ans = s * cost[3] + x * cost[2] + y * cost[4];
} else {
if (x >= 0) {
ans += x * cost[5];
} else {
ans += -x * cost[2];
}
if (y >= 0) {
ans += y * cost[1];
} else {
ans += -y * cost[4];
}
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int tt = 1;
cin >> tt;
while (tt--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 2e18 + 1;
long long c[6];
long long dx[6] = {1, 0, -1, -1, 0, 1};
long long dy[6] = {1, 1, 0, -1, -1, 0};
int main() {
long long T;
cin >> T;
for (long long t = 0; t < T; t++) {
long long x, y;
cin >> x >> y;
for (long long i = 0; i < 6; i++) cin >> c[i];
int TEMP = 10;
while (TEMP > 0) {
for (long long i = 0; i < 6; i++) {
long long nc = (i + 1) % 6, ncc = (i + 2) % 6;
c[nc] = min(c[nc], c[i] + c[ncc]);
}
for (long long i = 0; i < 6; i++) {
long long lc = (i - 1 + 6) % 6, lcc = (i - 2 + 6) % 6;
c[lc] = min(c[lc], c[i] + c[lcc]);
}
TEMP--;
}
long long minc = inf;
for (long long i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
long long p1 = inf, p2 = inf;
if (dx[i] != 0) p1 = min(p1, x / dx[i]);
if (dy[i] != 0) p1 = min(p1, y / dy[i]);
p1 = max(p1, 0ll);
long long x1 = x, y1 = y;
x1 -= p1 * dx[i], y1 -= p1 * dy[i];
if (dx[j] != 0) p2 = min(p2, x1 / dx[j]);
if (dy[j] != 0) p2 = min(p2, y1 / dy[j]);
if (p1 < 0 || p2 < 0) continue;
if ((dx[i] * p1 + dx[j] * p2) == x && (dy[i] * p1 + dy[j] * p2) == y)
minc = min(p1 * c[i] + p2 * c[j], minc);
}
}
cout << minc << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5 + 228;
const int INF = (int)1e9 + 228;
long long c[6], mn[6];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tst;
cin >> tst;
while (tst--) {
long long x, y;
cin >> x >> y;
for (int i = 0; i < 6; i++) {
cin >> c[i];
}
for (int i = 0; i < 6; i++) {
mn[i] = min(c[i], c[(i - 1 + 6) % 6] + c[(i + 1) % 6]);
}
long long ans = 0;
if (x >= 0 && y >= 0) {
if (y >= x) {
ans = mn[0] * x + mn[1] * (y - x);
} else {
ans = mn[0] * y + mn[5] * (x - y);
}
} else if (x >= 0 && y <= 0) {
ans = mn[5] * x + mn[4] * (-y);
} else if (x <= 0 && y <= 0) {
if (-y >= -x) {
ans = mn[3] * (-x) + mn[4] * (x - y);
} else {
ans = mn[3] * (-y) + mn[2] * (y - x);
}
} else {
ans = mn[2] * (-x) + mn[1] * y;
}
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7, N = 1e5 + 5;
long long solve() {
long long x, y;
cin >> y >> x;
long long ans, c[7];
for (long long i = (long long)1; i < 7; i++) cin >> c[i];
ans = 2e18;
if (x >= 0 && y >= 0 && y >= x) {
ans = min(ans, x * c[1] + (y - x) * c[6]);
ans = min(ans, x * c[2] + y * c[6]);
ans = min(ans, y * c[1] + (y - x) * c[5]);
}
if (y >= 0 && x >= 0 && y <= x) {
ans = min(ans, y * c[1] + (x - y) * c[2]);
ans = min(ans, x * c[2] + y * c[6]);
ans = min(ans, x * c[1] + (x - y) * c[3]);
}
if (x >= 0 && y <= 0) {
ans = min(ans, x * c[2] + abs(y) * c[3]);
ans = min(ans, x * c[1] + (x - y) * c[3]);
ans = min(ans, abs(y) * c[4] + (x - y) * c[2]);
}
if (x <= 0 && y <= 0 && x <= y) {
ans = min(ans, abs(y) * c[4] + abs(y - x) * c[5]);
ans = min(ans, abs(x) * c[5] + abs(y) * c[3]);
ans = min(ans, abs(x) * c[4] + abs(y - x) * c[6]);
}
if (y <= 0 && x <= 0 && y <= x) {
ans = min(ans, abs(x) * c[4] + abs(y - x) * c[3]);
ans = min(ans, abs(x) * c[5] + abs(y) * c[3]);
ans = min(ans, abs(y) * c[4] + abs(y - x) * c[2]);
}
if (x <= 0 && y >= 0) {
ans = min(ans, abs(x) * c[5] + y * c[6]);
ans = min(ans, y * c[1] + (y - x) * c[5]);
ans = min(ans, abs(x) * c[4] + (y - x) * c[6]);
}
return ans;
}
int32_t main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
cin >> t;
while (t--) cout << solve() << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
long long x, y;
long long c[7];
long long getCost(int a, int b) {
long long ans = 0;
if (a == 0 && b == 1) {
if (x > 0)
ans += x * c[6];
else
ans += abs(x) * c[3];
if (y > 0)
ans += y * c[2];
else
ans += abs(y) * c[5];
} else if (a == 0 && b == 2) {
if (x > 0)
ans += x * c[1];
else
ans += abs(x) * c[4];
if (x > y)
ans += (x - y) * c[5];
else
ans += (y - x) * c[2];
} else if (a == 1 && b == 2) {
if (y > 0)
ans += y * c[1];
else
ans += abs(y) * c[4];
if (x < y)
ans += (y - x) * c[3];
else
ans += (x - y) * c[6];
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
cin >> x >> y;
for (int i = 1; i <= 6; i++) cin >> c[i];
c[1] = min(c[1], c[2] + c[6]);
c[2] = min(c[2], c[1] + c[3]);
c[3] = min(c[3], c[2] + c[4]);
c[4] = min(c[4], c[3] + c[5]);
c[5] = min(c[5], c[4] + c[6]);
c[6] = min(c[6], c[1] + c[5]);
long long ans = LLONG_MAX;
for (int i = 0; i < 3; i++)
for (int j = i + 1; j < 3; j++) {
ans = min(ans, getCost(i, j));
}
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
ll cost[7];
ll cost1(ll x, ll y) {
ll res = 0LL;
if (x < 0) {
res += -x * cost[3];
} else {
res += x * cost[6];
}
if (y < 0) {
res += -y * cost[5];
} else {
res += y * cost[2];
}
return res;
}
ll cost2(ll x, ll y) {
ll res = 0LL;
if (x > 0) {
res += x * cost[1];
y -= x;
} else {
res += -x * cost[4];
y -= x;
}
if (y < 0) {
res += -y * cost[5];
} else {
res += y * cost[2];
}
return res;
}
ll cost3(ll x, ll y) {
ll res = 0LL;
if (y > 0) {
res += y * cost[1];
x -= y;
} else {
res += -y * cost[4];
x -= y;
}
if (x < 0) {
res += -x * cost[3];
} else {
res += x * cost[6];
}
return res;
}
void solve() {
ll x, y;
cin >> x >> y;
for (int i = 1; i <= 6; ++i) cin >> cost[i];
ll ans1 = cost1(x, y);
ll ans2 = cost2(x, y);
ll ans3 = cost3(x, y);
ll ans = min({ans1, ans2, ans3});
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const double PI = acos(-1.0);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long c[7];
long long rec(long long x, long long y) {
if (x == 0 && y == 0) return 0;
if (x == 0) {
if (y > 0) {
long long ans = min(c[2] * y, y * (c[3] + c[1]));
return ans;
} else {
long long aby = abs(y);
long long ans = min(aby * c[5], aby * (c[4] + c[6]));
return ans;
}
} else if (y == 0) {
if (x > 0) {
long long ans = min(c[6] * x, x * (c[5] + c[1]));
return ans;
} else {
long long ans = min(c[3] * (-x), (-x) * (c[2] + c[4]));
return ans;
}
}
long long ans = 4e18;
if (x > 0) {
long long tmp = min(c[6] * x, x * (c[5] + c[1])) + rec(0, y);
ans = min(ans, tmp);
} else {
long long tmp = min(c[3] * (-x), (-x) * (c[2] + c[4])) + rec(0, y);
ans = min(ans, tmp);
}
if (y > 0) {
long long tmp = min(c[2] * y, y * (c[3] + c[1])) + rec(x, 0);
ans = min(ans, tmp);
} else {
long long aby = abs(y);
long long tmp = min(aby * c[5], aby * (c[4] + c[6])) + rec(x, 0);
ans = min(ans, tmp);
}
if (x > 0) {
long long tmp = c[1] * x + rec(0, y - x);
ans = min(ans, tmp);
} else {
long long tmp = c[4] * (-x) + rec(0, y - x);
ans = min(ans, tmp);
}
if (y > 0) {
long long tmp = c[1] * y + rec(x - y, 0);
ans = min(ans, tmp);
} else {
long long tmp = c[4] * (-y) + rec(x - y, 0);
ans = min(ans, tmp);
}
return ans;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y;
for (long long i = 1; i <= 6; i++) {
cin >> c[i];
}
cout << rec(x, y) << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &res) {
char c;
T flag = 1;
while ((c = getchar()) < '0' || c > '9')
if (c == '-') flag = -1;
res = c - '0';
while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0';
res *= flag;
}
void wenjian() {
freopen("concatenation.in", "r", stdin);
freopen("concatenation.out", "w", stdout);
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long qpow(long long a, long long b, long long mod) {
a %= mod;
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
struct chongzai {
int c;
bool operator<(const chongzai &b) const { return c > b.c; }
} sss;
const int maxn = 1e6 + 177;
const int maxm = 1e6 + 177;
const long long mod = 65536;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int x, y;
scanf("%d%d", &x, &y);
long long c1, c2, c3, c4, c5, c6;
scanf("%lld%lld%lld%lld%lld%lld", &c1, &c2, &c3, &c4, &c5, &c6);
long long ys, yy, yx, zx, zz, zs;
ys = min(c1, c2 + c6);
yy = min(c2, c1 + c3);
yx = min(c3, c2 + c4);
zx = min(c4, c3 + c5);
zz = min(c5, c4 + c6);
zs = min(c6, c5 + c1);
long long ans;
if (x >= 0 && y >= 0) {
ans = min(min(x, y) * ys + (y - min(x, y)) * yy + (x - min(x, y)) * zs,
x * zs + (x + y) * yy);
}
if (x >= 0 && y < 0) {
ans = min(x * zs + abs(y) * zz, (abs(y) + x) * zz + x * ys);
}
if (x < 0 && y >= 0) {
ans = min(abs(x) * yx + y * yy, (abs(x) + y) * yy + zx * abs(x));
}
if (x < 0 && y < 0) {
x = abs(x);
y = abs(y);
ans = min(min(x, y) * zx + (y - min(x, y)) * zz + (x - min(x, y)) * yx,
(y + x) * zz + x * yx);
}
printf("%lld\n", ans);
}
}
|
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int32_t main() {
fast();
long long t;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y;
swap(x, y);
long long c1, c2, c3, c4, c5, c6;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
long long p, q;
if (x > 0)
p = min(c2, c1 + c3);
else if (x < 0)
p = min(c5, c6 + c4);
else
p = 0;
if (y > 0)
q = min(c6, c5 + c1);
else if (y < 0)
q = min(c3, c2 + c4);
else
q = 0;
if (x > 0 && y > 0) {
long long temp = min(c1, p + q);
if (x >= y)
cout << y * temp + (x - y) * p << "\n";
else
cout << x * temp + (y - x) * q << "\n";
} else if (x < 0 && y < 0) {
long long temp = min(c4, p + q);
x = -x;
y = -y;
if (x >= y)
cout << y * temp + (x - y) * p << "\n";
else
cout << x * temp + (y - x) * q << "\n";
} else {
if (x < 0) x = -x;
if (y < 0) y = -y;
cout << p * x + q * y << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
using pii = pair<int, int>;
const int N = 0;
const int mod = 0;
void solve() {
int x, y;
scanf("%d %d", &x, &y);
int c[15];
for (int i = 1; i <= 6; i++) {
scanf("%d", &c[i]);
}
c[2] = min(c[2], c[1] + c[3]);
c[3] = min(c[3], c[4] + c[2]);
c[5] = min(c[5], c[4] + c[6]);
c[6] = min(c[6], c[1] + c[5]);
c[1] = min(c[1], c[2] + c[6]);
c[4] = min(c[4], c[3] + c[5]);
c[7] = c[3] + c[2];
c[8] = c[6] + c[5];
if (x == 0) {
if (y < 0)
printf("%lld\n", 1LL * -y * c[5]);
else
printf("%lld\n", 1LL * y * c[2]);
return;
}
if (y == 0) {
if (x < 0)
printf("%lld\n", 1LL * -x * c[3]);
else
printf("%lld\n", 1LL * x * c[6]);
return;
}
ll ans = 0;
if (x < 0 && y < 0) {
ans += 1LL * min(-x, -y) * c[4];
if (x > y)
ans += 1LL * (x - y) * c[5];
else
ans += 1LL * (y - x) * c[3];
} else if (x < 0 && y > 0) {
ans += 1LL * min(-x, y) * c[7];
if (-x > y)
ans += 1LL * (-x - y) * c[3];
else
ans += 1LL * (y + x) * c[2];
} else if (x > 0 && y < 0) {
ans += 1LL * min(x, -y) * c[8];
if (x > -y)
ans += 1LL * (x + y) * c[6];
else
ans += 1LL * (-x - y) * c[5];
} else {
ans += 1LL * min(x, y) * c[1];
if (x > y)
ans += 1LL * (x - y) * c[6];
else
ans += 1LL * (y - x) * c[2];
}
printf("%lld\n", ans);
return;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 300;
int c[10];
int main() {
int T;
scanf("%d", &T);
while (T--) {
long long x, y;
scanf("%lld %lld", &x, &y);
for (int i = 1; i <= 6; i++) scanf("%d", c + i);
long long costX, costY;
if (x < 0)
costX = min(c[3], c[4] + c[2]);
else if (x > 0)
costX = min(c[6], c[1] + c[5]);
else
costX = 0;
if (y < 0)
costY = min(c[5], c[4] + c[6]);
else if (y > 0)
costY = min(c[2], c[1] + c[3]);
else
costY = 0;
long long ans = 0;
if (x >= 0 && y >= 0 && c[6] + c[2] > c[1])
ans = min(x, y) * c[1] + (x >= y ? (x - y) * costX : (y - x) * costY);
else if (x <= 0 && y <= 0 && c[3] + c[5] > c[4])
ans = -max(x, y) * c[4] + (x <= y ? (x - y) * -costX : (y - x) * -costY);
else
ans = abs(x) * costX + abs(y) * costY;
printf("%lld\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void parseArray(long long* A, long long n) {
for (long long K = 0; K < n; K++) {
cin >> A[K];
}
}
long long modInverse(long long a, long long b) {
return 1 < a ? b - modInverse(b % a, a) * b / a : 1;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long double dist(long double x, long double y, long double a, long double b) {
return sqrt((x - a) * (x - a) + (y - b) * (y - b));
}
void debug(long long* a, long long n) {
for (long long k = 0; k < n; k++) {
cerr << a[k] << " ";
}
cout << "\n";
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
long long _;
cin >> _;
while (_--) {
long long x, y;
cin >> x >> y;
long long a, b, c, d, e, f;
cin >> a >> b >> c >> d >> e >> f;
long long inc_x = min(b, a + c);
long long inc_y = min(f, e + a);
long long dec_x = min(e, d + f);
long long dec_y = min(c, d + b);
long long inc_xy = min(a, inc_x + inc_y);
long long dec_xy = min(d, dec_x + dec_y);
long long ans = 0;
swap(x, y);
if (x >= 0 && y >= 0) {
ans = min({
inc_x * x + inc_y * y,
inc_xy * min(x, y) + (x >= y ? inc_x * (x - y) : inc_y * (y - x)),
inc_xy * max(x, y) + (x >= y ? dec_y * (x - y) : dec_x * (y - x)),
});
} else if (x >= 0 && y <= 0) {
ans = inc_x * x + dec_y * -y;
} else if (x <= 0 && y >= 0) {
ans = dec_x * -x + inc_y * y;
} else if (x <= 0 && y <= 0) {
x = -x;
y = -y;
ans = min({
dec_x * x + dec_y * y,
dec_xy * min(x, y) + (x >= y ? dec_x * (x - y) : dec_y * (y - x)),
dec_xy * max(x, y) + (x >= y ? inc_y * (x - y) : inc_x * (y - x)),
});
}
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int C[6] = {1, 3, 5, 7, 9, 11}, x, y;
cin >> x >> y;
for (int i = 0; i < 6; i++) {
cin >> C[i];
}
for (int j = 0; j < 36; j++) {
for (int i = 1; i < 7; i++) {
C[i % 6] = min(C[i - 1] + C[(i + 1) % 6], C[i % 6]);
}
}
for (int i = 0; i < 6; i++) {
}
if (x >= 0) {
long long int a = 9e18, b = 9e18, c = 9e18;
if (y > 0) {
a = C[5] * x + C[1] * y;
} else {
a = C[5] * x - C[4] * y;
a = min(a, (-y) * C[3] + C[5] * (x - y));
}
if (y > x) {
b = C[0] * x + (y - x) * C[1];
b = min(b, (y - x) * C[2] + C[0] * y);
} else {
b = C[0] * x + (x - y) * C[4];
}
if (y < x && y > 0) {
c = min(C[0] * (y) + C[5] * (x - y), c);
}
cout << min(a, min(b, c)) << "\n";
} else {
long long int a = 9e18, b = 9e18, c = 9e18;
if (y > 0) {
a = -C[2] * x + C[1] * y;
a = min(a, (y - x) * C[2] + C[0] * y);
} else {
a = -C[2] * x - C[4] * y;
}
if (y > x) {
b = -C[3] * x + (y - x) * C[1];
} else {
b = -C[3] * x + (x - y) * C[4];
b = min(b, (-y + x) * C[5] - C[3] * y);
}
if (y > x && y < 0) {
c = min(C[2] * (y - x) + C[3] * (-y), c);
}
cout << min(a, min(b, c)) << "\n";
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long int MOD = 1e9 + 7;
const int N = 1e6 + 6;
long long int dst[10];
int dx[] = {-10, -1, -1, 0, 1, 1, 0};
int dy[] = {-10, 1, 0, -1, -1, 0, 1};
long long int ara[10][10];
void change() {
priority_queue<
pair<long long int, pair<long long int, long long int> >,
vector<pair<long long int, pair<long long int, long long int> > >,
greater<pair<long long int, pair<long long int, long long int> > > >
pq;
pq.push({0, {1, 1}});
for (int i = 0; i <= 5; i++) {
for (int j = 0; j <= 5; j++) ara[i][j] = INF;
}
ara[1][1] = 0;
while (!pq.empty()) {
pair<long long int, pair<long long int, long long int> > top = pq.top();
long long int cst = top.first;
long long int x = top.second.first;
long long int y = top.second.second;
pq.pop();
if (ara[x][y] != cst) continue;
for (int i = 1; i <= 6; i++) {
int xx = x + dx[i];
int yy = y + dy[i];
if (xx < 0 || yy < 0 || xx > 2 || yy > 2) continue;
if (cst + dst[i] < ara[xx][yy]) {
ara[xx][yy] = cst + dst[i];
pq.push({ara[xx][yy], {xx, yy}});
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int x, y;
cin >> x >> y;
for (int i = 1; i <= 6; i++) cin >> dst[i];
change();
dst[0] = ara[1][1];
dst[1] = ara[0][2];
dst[2] = ara[0][1];
dst[3] = ara[1][0];
dst[4] = ara[2][0];
dst[5] = ara[2][1];
dst[6] = ara[1][2];
long long int ans = 0;
if (x == 0) {
if (y > 0)
ans = y * dst[2];
else
ans = -y * dst[5];
} else if (y == 0) {
if (x > 0)
ans = x * dst[6];
else
ans = -x * dst[3];
} else if (x > 0 && y > 0) {
long long int mn = min(x, y);
ans = mn * dst[1];
x -= mn;
y -= mn;
ans += x * dst[6] + y * dst[2];
} else if (x > 0 && y < 0) {
ans = x * dst[6] - y * dst[5];
} else if (x < 0 && y > 0) {
ans = -x * dst[3] + y * dst[2];
} else if (x < 0 && y < 0) {
x = -x;
y = -y;
long long int mn = min(x, y);
ans = mn * dst[4];
x -= mn;
y -= mn;
ans += x * dst[3] + y * dst[5];
} else
assert(0);
cout << ans << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int T;
long long X, Y, c1, c2, c3, c4, c5, c6;
inline long long calc(int x) {
int a1 = x, a2 = Y - a1, a3 = a1 - X;
return (a1 > 0 ? c1 : -c4) * a1 + (a2 > 0 ? c2 : -c5) * a2 +
(a3 > 0 ? c3 : -c6) * a3;
}
int main() {
for (scanf("%d", &T); T--;)
scanf("%lld%lld%lld%lld%lld%lld%lld%lld", &X, &Y, &c1, &c2, &c3, &c4, &c5,
&c6),
printf("%lld\n", min(calc(0), min(calc(X), calc(Y))));
}
|
#include <bits/stdc++.h>
using namespace std;
long long c[8];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long x, y, t;
cin >> t;
while (t--) {
cin >> x >> y;
for (int i = 1; i <= 6; i++) cin >> c[i];
c[1] = min(c[1], c[2] + c[6]);
c[2] = min(c[2], c[1] + c[3]);
c[3] = min(c[3], c[2] + c[4]);
c[4] = min(c[4], c[3] + c[5]);
c[5] = min(c[5], c[4] + c[6]);
c[6] = min(c[6], c[5] + c[1]);
long long ans = 0;
if (x >= 0 && y >= 0) {
if (x > y)
ans = y * c[1] + (x - y) * c[6];
else
ans = x * c[1] + (y - x) * c[2];
} else if (x <= 0 && y <= 0) {
if (x < y)
ans = -y * c[4] + (y - x) * c[3];
else
ans = -x * c[4] + (x - y) * c[5];
} else if (x <= 0 && y >= 0)
ans = y * c[2] - x * c[3];
else
ans = x * c[6] - y * c[5];
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[6] = {1, 0, -1, -1, 0, 1};
int a[6], b[6];
int dy[6] = {1, 1, 0, -1, -1, 0};
long long dc[6];
long long x, y;
int main() {
int t;
int i, j, k;
long long ans, s1, s2, s3, s4, s5, s6, x1, x2, x3, y1, y2, y3;
cin >> t;
while (t--) {
cin >> x >> y;
for (i = 0; i < 6; i++) cin >> dc[i];
ans = 8E18;
for (i = 0; i < 6; i++)
for (j = 0; j < 6; j++) {
a[i] = dx[i];
a[j] = dy[i];
b[i] = dx[j];
b[j] = dy[j];
if (abs(i - j) == 3 || i == j) continue;
s1 = a[i] * b[j] - a[j] * b[i];
if (s1 == 0) continue;
x1 = x * b[j] - y * b[i];
y1 = y * a[i] - x * a[j];
if (s1 == 0 || x1 % s1 != 0 || y1 % s1 != 0) continue;
x2 = x1 / s1;
y2 = y1 / s1;
if (x2 < 0 || y2 < 0) continue;
s2 = x2 * dc[i] + y2 * dc[j];
ans = min(ans, s2);
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long x, y;
long long a[10];
vector<long long> dx = {1, 0, -1, -1, 0, 1};
vector<long long> dy = {1, 1, 0, -1, -1, 0};
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t;
cin >> t;
while (t--) {
cin >> x >> y;
for (long long i = 0; i < 6; i++) cin >> a[i];
for (long long i = 1; i <= 100; i++)
for (long long j = 0; j < 6; j++)
a[j] = min(a[j], a[(j - 1 + 6) % 6] + a[(j + 1) % 6]);
long long ans = 4e18;
for (long long i = 0; i < 6; i++) {
long long rx = x, ry = y;
long long take = 2e9;
if (dx[i] != 0) take = rx / dx[i];
if (dy[i] != 0) take = min(take, ry / dy[i]);
if (take < 0) take = take * (-1);
rx -= take * dx[i];
ry -= take * dy[i];
long long curCost = take * a[i];
long long cx = rx, cy = ry;
for (long long j = 0; j < 6; j++) {
long long rx = cx, ry = cy;
long long take = 2e9;
if (dx[j] != 0) take = rx / dx[j];
if (dy[j] != 0) take = min(take, ry / dy[j]);
if (take < 0) take = take * (-1);
rx -= take * dx[j];
ry -= take * dy[j];
if (rx == 0 && ry == 0) ans = min(ans, curCost + take * a[j]);
}
}
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
int c[6];
long long solvesystem(int x, int y, int a1, int a2, int b1, int b2, int c1plus,
int c1minus, int c2plus, int c2minus) {
int A, B;
if (a1 == 0) {
B = x / b1;
A = (y - b2 * B) / a2;
} else if (a2 == 0) {
B = y / b2;
A = (x - b1 * B) / a1;
} else if (b1 == 0) {
A = x / a1;
B = (y - A * a2) / b2;
} else {
A = y / a2;
B = (x - a1 * A) / b1;
}
long long res = 0LL;
if (A > 0)
res = res + (long long)c1plus * A;
else
res = res - (long long)c1minus * A;
if (B > 0)
res = res + (long long)c2plus * B;
else
res = res - (long long)c2minus * B;
return res;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int x, y;
scanf("%d%d", &x, &y);
for (int i = 0; i < 6; ++i) scanf("%d", &c[i]);
long long res = std::min(
std::min(solvesystem(x, y, 1, 1, 0, 1, c[0], c[3], c[1], c[4]),
solvesystem(x, y, 1, 1, -1, 0, c[0], c[3], c[2], c[5])),
solvesystem(x, y, 0, 1, -1, 0, c[1], c[4], c[2], c[5]));
printf("%lld\n", res);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long fact[1000001], power[1000001];
long long powerr(long long x, long long y);
long long mpower(long long x, long long y, long long p);
long long gcdExtended(long long a, long long b);
bool isPrime(long long n);
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t, j, k, l, h, p, q, r, x, i, y, z, a, b, c, d, f, n, m,
M = 1000000007;
char ch, ch2, ch3;
string s1, s2, s3;
set<long long> S;
cin >> t;
while (t--) {
cin >> x >> y;
long long c1, c2, c3, c4, c5, c6;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
long long ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
if (x >= 0 && y > 0) {
if (x > y) {
ans1 = c1 * y + c6 * (x - y);
ans2 = c2 * y + c6 * (x);
ans3 = c1 * x + c5 * (x - y);
} else {
ans1 = c1 * x + c2 * (abs(x - y));
ans2 = c2 * y + c6 * x;
ans3 = c1 * y + c3 * (abs(x - y));
}
long long ans;
ans = min(ans2, ans1);
ans = min(ans, ans3);
cout << ans;
cout << "\n";
} else if (x >= 0 && y <= 0) {
ans1 = c6 * x + c5 * (abs(y));
ans2 = c1 * x + c5 * (x - y);
ans3 = c4 * abs(y) + c6 * (x - y);
long long ans;
ans = min(ans2, ans1);
ans = min(ans, ans3);
cout << ans;
cout << "\n";
} else if (x < 0 && y >= 0) {
ans1 = c2 * y + c3 * abs(x);
ans2 = c4 * (abs(x)) + c2 * (y - x);
ans3 = c1 * y + c3 * (y - x);
long long ans;
ans = min(ans2, ans1);
ans = min(ans, ans3);
cout << ans;
cout << "\n";
} else if (x < 0 && y < 0) {
if (y > x) {
ans1 = c4 * abs(y) + c3 * (y - x);
ans2 = c4 * abs(x) + c2 * (y - x);
ans3 = c3 * abs(x) + c5 * abs(y);
} else {
ans1 = c4 * abs(x) + c5 * (x - y);
ans2 = c5 * abs(y) + c3 * (abs(x));
ans3 = c4 * abs(y) + c6 * (x - y);
}
long long ans;
ans = min(ans2, ans1);
ans = min(ans, ans3);
cout << ans;
cout << "\n";
}
}
}
long long gcdExtended(long long a, long long b) {
if (b == 0) {
return a;
} else
return gcdExtended(b, a % b);
}
long long mpower(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long powerr(long long x, long long y) {
long long res = 1;
while (y > 0) {
if (y & 1) res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
bool isPrime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
long long p = sqrt(n);
for (int i = 5; i <= p; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
long long int x, y;
long long int c1, c2, c3, c4, c5, c6;
cin >> t;
while (t--) {
cin >> x >> y;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
long long int ans = (abs(x) * (c6 * (x > 0) + c3 * (x < 0)) +
abs(y) * (c2 * (y > 0) + c5 * (y < 0)));
{
ans = min(ans, abs(y - x) * (c6 * (x - y > 0) + c3 * (x - y < 0)) +
abs(y) * (c1 * (y > 0) + c4 * (y < 0)));
ans = min(ans, abs(y - x) * (c2 * (x - y < 0) + c5 * (x - y > 0)) +
abs(x) * (c1 * (x > 0) + c4 * (x < 0)));
}
cout << ans << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e18;
const long long mod = (long long)1e9 + 7;
const double eps = (double)1e-9;
const double pi = acos(-1.0);
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};
const int N = 100500;
int main() {
int T;
cin >> T;
while (T--) {
long long x, y;
cin >> x >> y;
swap(x, y);
long long c[7];
for (int i = 1; i <= 6; i++) cin >> c[i];
for (int it = 1; it <= 6; it++) {
for (int i = 1; i <= 6; i++) {
int prv = (i == 1 ? 6 : i - 1);
int nxt = (i == 6 ? 1 : i + 1);
c[i] = min(c[i], c[prv] + c[nxt]);
}
}
if (x <= 0 && y >= 0)
cout << (-x) * c[5] + y * c[6] << "\n";
else if (x >= 0 && y <= 0)
cout << x * c[2] + (-y) * c[3] << "\n";
else if (x >= 0 && y >= 0)
cout << min(min(x, y) * c[1] + (x - min(x, y)) * c[2] +
(y - min(x, y)) * c[6],
x * c[2] + y * c[6])
<< "\n";
else {
x = -x, y = -y;
cout << min(min(x, y) * c[4] + (x - min(x, y)) * c[5] +
(y - min(x, y)) * c[3],
x * c[5] + y * c[3])
<< "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
void bolt() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int32_t main() {
bolt();
long long t;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y;
long long c[7];
for (long long i = 1; i < 7; i++) cin >> c[i];
if (x == 0) {
if (y >= 0) {
cout << min(y * c[2], y * (c[1] + c[3])) << "\n";
} else {
y = -y;
cout << min(y * c[5], y * (c[4] + c[6])) << "\n";
}
} else if (y == 0) {
if (x >= 0) {
cout << min(x * c[6], x * (c[5] + c[1])) << "\n";
} else {
x = -x;
cout << min(x * c[3], x * (c[2] + c[4])) << "\n";
}
} else if (x == y) {
if (x >= 0) {
cout << min(x * c[1], x * (c[2] + c[6])) << "\n";
} else {
x = -x;
cout << min(x * c[4], x * (c[3] + c[5])) << "\n";
}
} else if (x > 0 and y > 0 and y > x) {
long long ans1 = x * c[1] + (y - x) * c[2];
long long ans2 = y * c[1] + (y - x) * c[3];
long long ans3 = y * c[2] + x * c[6];
cout << min({ans1, ans2, ans3}) << "\n";
} else if (x < 0 and y > 0) {
x = -x;
long long ans1 = y * c[2] + x * c[3];
long long ans2 = (x + y) * c[3] + y * c[1];
long long ans3 = (x + y) * c[2] + x * c[4];
cout << min({ans1, ans2, ans3}) << "\n";
} else if (x < 0 and y < 0 and abs(x) > abs(y)) {
x = -x;
y = -y;
long long ans1 = y * c[4] + (x - y) * c[3];
long long ans2 = x * c[4] + (x - y) * c[2];
long long ans3 = x * c[3] + y * c[5];
cout << min({ans1, ans2, ans3}) << "\n";
} else if (x < 0 and y < 0 and abs(x) < abs(y)) {
x = -x;
y = -y;
long long ans1 = x * c[4] + (y - x) * c[5];
long long ans2 = y * c[5] + x * c[3];
long long ans3 = y * c[4] + (y - x) * c[6];
cout << min({ans1, ans2, ans3}) << "\n";
} else if (x > 0 and y < 0) {
y = -y;
long long ans1 = y * c[5] + x * c[6];
long long ans2 = (x + y) * c[6] + y * c[4];
long long ans3 = (x + y) * c[5] + x * c[1];
cout << min({ans1, ans2, ans3}) << "\n";
} else if (x > 0 and y > 0 and x > y) {
long long ans1 = (x - y) * c[6] + y * c[1];
long long ans2 = x * c[1] + (x - y) * c[5];
long long ans3 = x * c[6] + y * c[2];
cout << min({ans1, ans2, ans3}) << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 50;
const int INF = 0x3f3f3f3f;
int c[7];
int main() {
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y;
for (int i = 1; i <= 6; i++) cin >> c[i];
int cnt2 = y - x, cnt1 = y - cnt2;
long long ans = 4e18;
ans = min(ans, 1ll * (cnt1 < 0 ? c[4] : c[1]) * abs(cnt1) +
1ll * (cnt2 < 0 ? c[5] : c[2]) * abs(cnt2));
cnt2 = x - y;
cnt1 = x - cnt2;
ans = min(ans, 1ll * (cnt1 < 0 ? c[4] : c[1]) * abs(cnt1) +
1ll * (cnt2 < 0 ? c[3] : c[6]) * abs(cnt2));
cnt2 = x;
cnt1 = y;
ans = min(ans, 1ll * (cnt1 < 0 ? c[5] : c[2]) * abs(cnt1) +
1ll * (cnt2 < 0 ? c[3] : c[6]) * abs(cnt2));
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MX = 1005;
const int mod = 1000000007;
const long long inf = 1000000000000000069ll;
int main() {
cin.tie(0)->sync_with_stdio(0);
int tc;
cin >> tc;
vector<int> cc(6, 0);
for (int xx, yy; tc--;) {
cin >> xx >> yy;
for (int i = 0; i < 6; i++) cin >> cc[i];
for (;;) {
vector<int> tmp = cc;
for (int j = 0; j < 6; j++)
cc[j] = min(cc[j], cc[(j + 1) % 6] + cc[(j + 5) % 6]);
if (tmp == cc) break;
}
vector<__int128_t> c;
for (int i = 0; i < 6; i++) c.push_back(cc[i]);
__int128_t ans = (__int128_t)inf * (__int128_t)inf;
__int128_t x = xx, y = yy;
if (x == y && y == 0) {
cout << 0 << endl;
continue;
}
if (x == y)
ans = min(ans, (x < 0 ? c[3] * (-x) : c[0] * 1ll * x));
else if (y == 0)
ans = min(ans, (x < 0 ? c[2] * 1ll * (-x) : c[5] * 1ll * x));
else if (x == 0)
ans = min(ans, (y < 0 ? c[4] * 1ll * (-y) : c[1] * 1ll * y));
if (x <= y && x >= 0) ans = min(ans, x * 1ll * c[0] + (y - x) * 1ll * c[1]);
if (x <= y && y >= 0) ans = min(ans, y * 1ll * c[0] + (y - x) * 1ll * c[2]);
if (x >= 0 && x >= y) ans = min(ans, x * 1ll * c[0] + (x - y) * 1ll * c[4]);
if (y >= 0 && x >= y) ans = min(ans, y * 1ll * c[0] + (x - y) * 1ll * c[5]);
if (x <= 0 && y >= 0) ans = min(ans, (-x) * 1ll * c[2] + y * 1ll * c[1]);
if (x <= 0 && y >= x)
ans = min(ans, (-x) * 1ll * c[3] + (y - x) * 1ll * c[1]);
if (x >= 0 && y >= 0) ans = min(ans, x * 1ll * c[5] + y * 1ll * c[1]);
if (y <= 0 && x <= y)
ans = min(ans, (-y) * 1ll * c[3] + (y - x) * 1ll * c[2]);
if (x <= 0 && y <= 0) ans = min(ans, (-y) * 1ll * c[4] + (-x) * 1ll * c[2]);
if (x <= 0 && x >= y)
ans = min(ans, (-x) * 1ll * c[3] + (x - y) * 1ll * c[4]);
if (y <= 0 && x >= y)
ans = min(ans, (-y) * 1ll * c[3] + (x - y) * 1ll * c[5]);
if (x >= 0 && y <= 0) ans = min(ans, x * 1ll * c[5] + (-y) * 1ll * c[4]);
long long get = ans;
cout << get << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const long long INF = 2 * 1e18;
const long double PI = 3.14159265358979323846;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long fpow(long long a, long long b, long long m) {
if (!b) return 1;
long long ans = fpow(a * a % m, b / 2, m);
return (b % 2 ? ans * a % m : ans);
}
long long inv(long long a, long long m) { return fpow(a, m - 2, m); }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
long long x, y, c[10];
cin >> x >> y;
for (long long i = 1; i <= 6; i++) cin >> c[i];
c[0] = c[6], c[7] = c[1];
for (long long i = 1; i <= 6; i++) {
c[i] = min(c[i], c[i + 1] + c[i - 1]);
}
for (long long i = 1; i <= 6; i++) {
c[i] = min(c[i], c[i + 1] + c[i - 1]);
}
if (y >= x && x >= 0)
cout << x * c[1] + (y - x) * c[2] << "\n";
else if (y >= x && y <= 0)
cout << -y * c[4] + (y - x) * c[3] << "\n";
else if (y >= x && x <= 0 && y >= 0)
cout << -x * c[3] + y * c[2] << "\n";
else if (y <= x && y >= 0)
cout << c[6] * (x - y) + c[1] * y << "\n";
else if (y <= x && x <= 0)
cout << c[4] * (-x) + c[5] * (x - y) << "\n";
else
cout << x * c[6] + c[5] * (-y) << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long x, y, c, c2, c3, c4, c5, c6;
long long calc(long long mid) {
long long res;
if (mid > 0)
res = mid * c;
else
res = -mid * c4;
if (mid < x)
res += (long long)(x - mid) * c6;
else
res += (long long)(mid - x) * c3;
if (mid < y)
res += (long long)(y - mid) * c2;
else
res += (long long)(mid - y) * c5;
return res;
}
long long query(long long l, long long r) {
while (l + 5 < r) {
long long mid = l + (r - l) / 3, mid2 = r - (r - l) / 3;
if (calc(mid) < calc(mid2))
r = mid2;
else
l = mid;
}
long long res = calc(l);
for (long long i = l + 1; i <= r; i++) res = min(res, calc(i));
return res;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%lld%lld", &x, &y);
scanf("%lld%lld%lld%lld%lld%lld", &c, &c2, &c3, &c4, &c5, &c6);
printf("%lld\n", query(-1e9, 1e9));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll c[10];
int dx[] = {1, 0, -1, -1, 0, 1};
int dy[] = {1, 1, 0, -1, -1, 0};
int main() {
int t;
cin >> t;
while (t--) {
ll x, y;
cin >> x >> y;
for (int i = 0; i < 6; ++i) cin >> c[i];
long long ans = 5e18;
auto sign = [](long long x) {
if (x == 0) return 0;
if (x < 0) return -1;
return 1;
};
for (int i = 0; i < 6; ++i) {
for (int j = i; j < 6; ++j) {
int det = dx[i] * dy[j] - dx[j] * dy[i];
assert(abs(det) <= 1);
if (det != 0) {
long long t1 = x * dy[j] - y * dx[j];
long long t2 = dx[i] * y - dy[i] * x;
t1 /= det;
t2 /= det;
if (t1 >= 0 && t2 >= 0) {
ans = min(ans, abs(t1) * c[i] + abs(t2) * c[j]);
}
} else {
if (sign(x) == dx[i] && sign(y) == dy[i]) {
long long res = 0;
if (dx[i] != 0)
res += abs(x) * c[i];
else if (dy[i] != 0)
res += abs(y) * c[i];
if (x == 0 || y == 0 || x == y) {
ans = min(ans, res);
}
}
if (sign(x) == dx[j] && sign(y) == dy[j]) {
long long res = 0;
if (dx[j] != 0)
res += abs(x) * c[j];
else if (dy[j] != 0)
res += abs(y) * c[j];
if (x == 0 || y == 0 || x == y) {
ans = min(ans, res);
}
}
}
}
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const long long mod = (long long)1e9 + 7;
const long long MOD = (long long)998244353;
const long long INF = (long long)1e18;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
void solve() {
long long x, y;
cin >> x >> y;
long long c[7];
for (__typeof(7) i = (1) - ((1) > (7)); i != (7) - ((1) > (7));
i += 1 - 2 * ((1) > (7)))
cin >> c[i];
long long ans = 9e18;
if (x <= 0 and y >= 0) {
ans = min(
ans, abs(x) * min(c[3], c[2] + c[4]) + abs(y) * min(c[2], c[1] + c[3]));
}
if (x >= 0 and y <= 0) {
ans = min(
ans, abs(x) * min(c[6], c[5] + c[1]) + abs(y) * min(c[5], c[6] + c[4]));
}
if (x >= 0 and y <= x and y >= 0) {
ans = min(ans, abs(x - y) * min(c[6], c[1] + c[5]) +
abs(y) * min(c[1], c[6] + c[2]));
}
if (x >= 0 and y >= x and y >= 0) {
ans = min(ans, abs(x) * min(c[1], c[2] + c[6]) +
abs(y - x) * min(c[2], c[1] + c[3]));
}
if (x <= 0 and y >= x and y <= 0) {
ans = min(ans, abs(x - y) * min(c[3], c[2] + c[4]) +
abs(y) * min(c[4], c[3] + c[5]));
}
if (x <= 0 and y <= x and y <= 0) {
ans = min(ans, abs(x) * min(c[4], c[5] + c[3]) +
abs(y - x) * min(c[5], c[4] + c[6]));
}
cout << ans << "\n";
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
long long t;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using pll = pair<ll, ll>;
ll inf = 4e18, mod = 1e9 + 7;
ll c1, c2, c3, c4, c5, c6;
ll funcx(ll sx, ll ex) {
if (sx == ex) return 0;
if (sx < ex) return (ex - sx) * min(c6, c1 + c5);
if (sx > ex) return (sx - ex) * min(c3, c2 + c4);
}
ll funcy(ll sy, ll ey) {
if (sy == ey) return 0;
if (sy < ey) return (ey - sy) * min(c2, c1 + c3);
if (sy > ey) return (sy - ey) * min(c5, c4 + c6);
}
ll funcxy(ll sx, ll sy, ll ex, ll ey) {
if (sx - ex != sy - ey) return inf;
if (sx < ex && sy < ey)
return min((ex - sx) * c1, funcx(sx, ex) + funcy(sy, ey));
if (sx > ex && sy > ey)
return min((sx - ex) * c4, funcx(sx, ex) + funcy(sy, ey));
}
void(o_o)() {
ll x, y;
cin >> x >> y;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
ll res = funcx(0, x) + funcy(0, y);
res = min(res, funcxy(0, 0, x, x) + funcy(x, y));
res = min(res, funcxy(0, 0, y, y) + funcx(y, x));
cout << res << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout << fixed << setprecision(10);
int t = 1;
cin >> t;
while (t--) (o_o)();
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const double eps = 1e-6;
const long long inf = 0x3f3f3f3f;
const long long N = 2e5 + 10;
char s[N];
int main() {
long long t;
scanf("%lld", &t);
while (t--) {
long long x, y;
long long v1, v2, v3, v4, v5, v6;
scanf("%lld%lld", &x, &y);
scanf("%lld%lld%lld%lld%lld%lld", &v1, &v2, &v3, &v4, &v5, &v6);
v1 = min(v1, v6 + v2);
v2 = min(v2, v1 + v3);
v3 = min(v3, v2 + v4);
v4 = min(v4, v3 + v5);
v5 = min(v5, v4 + v6);
v6 = min(v6, v1 + v5);
v1 = min(v1, v6 + v2);
v2 = min(v2, v1 + v3);
v3 = min(v3, v2 + v4);
v4 = min(v4, v3 + v5);
v5 = min(v5, v4 + v6);
v6 = min(v6, v1 + v5);
v1 = min(v1, v6 + v2);
v2 = min(v2, v1 + v3);
v3 = min(v3, v2 + v4);
v4 = min(v4, v3 + v5);
v5 = min(v5, v4 + v6);
v6 = min(v6, v1 + v5);
v1 = min(v1, v6 + v2);
v2 = min(v2, v1 + v3);
v3 = min(v3, v2 + v4);
v4 = min(v4, v3 + v5);
v5 = min(v5, v4 + v6);
v6 = min(v6, v1 + v5);
v1 = min(v1, v6 + v2);
v2 = min(v2, v1 + v3);
v3 = min(v3, v2 + v4);
v4 = min(v4, v3 + v5);
v5 = min(v5, v4 + v6);
v6 = min(v6, v1 + v5);
v1 = min(v1, v6 + v2);
v2 = min(v2, v1 + v3);
v3 = min(v3, v2 + v4);
v4 = min(v4, v3 + v5);
v5 = min(v5, v4 + v6);
v6 = min(v6, v1 + v5);
long long ans1 = 0LL;
long long ans2 = 0LL;
if (x * y == 0LL) {
if (x == 0LL) {
if (y >= 0) {
ans1 += v2 * abs(y);
} else {
ans1 += v5 * abs(y);
}
} else {
if (x >= 0) {
ans1 += v6 * abs(x);
} else {
ans1 += v3 * abs(x);
}
}
printf("%lld\n", ans1);
} else if (x * y < 0LL) {
if (x < 0LL) {
ans1 += v3 * abs(x);
ans1 += v2 * abs(y);
} else {
ans1 += v6 * abs(x);
ans1 += v5 * abs(y);
}
printf("%lld\n", ans1);
} else {
if (x < 0LL) {
if (x < y) {
ans1 += v4 * abs(y);
ans1 += v3 * abs(y - x);
} else {
ans1 += v4 * abs(x);
ans1 += v5 * abs(x - y);
}
} else {
if (x < y) {
ans1 += abs(x) * v1;
ans1 += v2 * abs(y - x);
} else {
ans1 += abs(y) * v1;
ans1 += v6 * abs(x - y);
}
}
printf("%lld\n", ans1);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int t;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> t;
for (int test = 1; test <= t; test++) {
long long x, y;
long long c1, c2, c3, c4, c5, c6;
cin >> x >> y;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
long long rez = 0;
if (x <= 0 && y >= 0) {
x = (-x);
cout << 1LL * (min(1LL * x * c3, 1LL * x * c4 + 1LL * x * c2) +
min(1LL * y * c2, 1LL * y * c1 + 1LL * y * c3))
<< '\n';
} else if (x >= 0 && y <= 0) {
y = (-y);
cout << 1LL * (min(1LL * x * c6, 1LL * x * c1 + 1LL * x * c5) +
min(1LL * y * c5, 1LL * y * c4 + 1LL * y * c6))
<< '\n';
} else if (x <= 0 && y <= 0) {
x = (-x);
y = (-y);
if (x >= y) {
cout << 1LL * min(1LL * x * c3 + 1LL * y * c5,
min(1LL * x * c4 + 1LL * (x - y) * c2,
1LL * y * c4 + 1LL * (x - y) * c3))
<< '\n';
} else {
cout << 1LL * min(1LL * x * c3 + 1LL * y * c5,
min(1LL * x * c4 + 1LL * (y - x) * c5,
1LL * y * c4 + 1LL * (y - x) * c6))
<< '\n';
}
} else if (x >= 0 && y >= 0) {
if (x >= y) {
cout << 1LL * min(1LL * x * c6 + 1LL * y * c2,
min(1LL * x * c1 + 1LL * (x - y) * c5,
1LL * y * c1 + 1LL * (x - y) * c6))
<< '\n';
} else {
cout << 1LL * min(1LL * x * c6 + 1LL * y * c2,
min(1LL * x * c1 + 1LL * (y - x) * c2,
1LL * y * c1 + 1LL * (y - x) * c3))
<< '\n';
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long c[10];
long long tmpx, tmpy, x, y;
long long solve(long long x, long long y) {
long long ret = 2e18;
{
if (y < 0)
tmpx = c[5];
else
tmpx = c[2];
if (x < 0)
tmpy = c[3];
else
tmpy = c[6];
ret = min(ret, tmpx * abs(y) + tmpy * abs(x));
}
{
if (y - x > 0) {
tmpx = c[2];
} else
tmpx = c[5];
if (x < 0) {
tmpy = c[4];
} else
tmpy = c[1];
ret = min(ret, tmpx * abs(y - x) + tmpy * abs(x));
}
return ret;
}
void right() {
long long tmp = c[6];
for (int i = 5; i >= 1; i--) {
c[i + 1] = c[i];
}
c[1] = tmp;
tmp = x;
x = x - y;
y = tmp;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
cin >> x >> y;
for (int i = 1; i <= 6; i++) cin >> c[i];
long long ans = 2e18;
for (int i = 0; i < 3; i++) {
ans = min(ans, solve(x, y));
right();
}
cout << ans << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 5;
const int oo = INT_MAX;
const long long OO = LLONG_MAX;
long long c1, c2, c3, c4, c5, c6;
long long res1, res2, res3, res4;
int t;
long long x, y;
void solve() {
res1 = res2 = res3 = 0;
if (x < 0) {
res1 = c4 * (-x);
}
if (x > 0) {
res1 = c1 * x;
}
if (x < y) {
res1 += c2 * (y - x);
}
if (x > y) {
res1 += c5 * (x - y);
}
if (y > 0) {
res2 = c1 * y;
}
if (y < 0) {
res2 = c4 * -y;
}
if (x < y) {
res2 += c3 * (y - x);
}
if (x > y) {
res2 += c6 * (x - y);
}
if (x > 0) {
res3 = c6 * x;
}
if (x < 0) {
res3 = c3 * -x;
}
if (0 < y) {
res3 += c2 * y;
}
if (0 > y) {
res3 += c5 * -y;
}
cout << min(res1, min(res2, res3)) << '\n';
}
void input() {
cin >> x >> y;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
input();
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long int> c;
long long int xcost(long long int a) {
if (a > 0) {
return a * c[6];
} else {
return -a * c[3];
}
}
long long int ycost(long long int a) {
if (a > 0) {
return a * c[2];
} else {
return -a * c[5];
}
}
long long int xycost(long long int a) {
if (a > 0) {
return a * c[1];
} else {
return -a * c[4];
}
}
void solve() {
long long int x, y;
cin >> x >> y;
c.assign(7, 0);
for (long long int i = 1; i < (7); i++) {
cin >> c[i];
}
long long int ans = LLONG_MAX;
ans = min(ans, xcost(x) + ycost(y));
ans = min(ans, xycost(x) + ycost(y - x));
ans = min(ans, xcost(x - y) + xycost(y));
cout << ans << endl;
}
int main() {
long long int t;
cin >> t;
while (t--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long t;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y;
long long arr[6];
for (long long i = 0; i < 6; i++) {
cin >> arr[i];
}
if (x == 0 && y == 0) {
cout << 0 << endl;
continue;
}
long long a[6];
memset(a, 0, sizeof(a));
if (x > 0) {
a[5] = x;
}
if (x < 0) {
a[2] = (-1) * x;
}
if (y > 0) {
a[1] = y;
}
if (y < 0) {
a[4] = (-1) * y;
}
long long sum = 0;
for (long long i = 0; i < 6; i++) {
long long ex = a[(i + 1) % 6];
long long ey;
if (i == 0) {
ey = a[5];
} else {
ey = a[i - 1];
}
long long mn = min(ex, ey);
a[i] += mn;
a[(i + 1) % 6] -= mn;
if (i == 0) {
a[5] -= mn;
} else {
a[i - 1] -= mn;
}
}
for (long long i = 0; i < 6; i++) {
long long ex = arr[(i - 1) % 6] * a[i];
long long ey = arr[(i + 1) % 6] * a[i];
if (i == 0) {
ex = arr[5] * a[i];
}
sum += min(arr[i] * a[i], ex + ey);
}
cout << sum << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int T;
long long N;
long long c[7];
int main() {
cin >> T;
while (T--) {
long long x, y;
cin >> x >> y;
for (int i = 1; i < 7; i++) cin >> c[i];
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 6; i++) {
if (i == 0)
c[1] = min(c[6] + c[2], c[1]);
else if (i == 5)
c[6] = min(c[1] + c[5], c[6]);
else
c[i + 1] = min(c[i] + c[i + 2], c[i + 1]);
}
}
long long px = 0, py = 0, cst = 0;
long long ans = 4000000000000000000;
if (y < 0) {
cst -= y * c[4];
if (x - y > 0)
cst += c[6] * (x - y);
else
cst -= c[3] * (x - y);
ans = min(ans, cst);
} else {
cst += y * c[1];
if (y - x > 0)
cst += c[3] * (y - x);
else
cst -= c[6] * (y - x);
ans = min(ans, cst);
}
if (x < 0) {
cst = 0;
cst -= x * c[4];
if (x < y)
cst += (y - x) * c[2];
else
cst += (x - y) * c[5];
ans = min(ans, cst);
cst = 0;
cst -= x * c[3];
if (y > 0)
cst += y * c[2];
else
cst -= y * c[5];
ans = min(ans, cst);
} else {
cst = x * c[6];
if (y > 0)
cst += c[2] * y;
else
cst -= c[5] * y;
ans = min(ans, cst);
cst = 0;
cst += c[1] * x;
y -= x;
if (y > 0)
cst += c[2] * y;
else
cst -= c[5] * y;
ans = min(ans, cst);
}
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long x, y;
cin >> x >> y;
long long c1, c2, c3, c4, c5, c6;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
long long ans = LLONG_MAX;
long long cost;
if (x >= 0 && y >= 0) {
if (y >= x) {
cost = (y * c2) + x * c6;
ans = min(ans, cost);
cost = (y - x) * c2 + x * c1;
ans = min(ans, cost);
cost = (y - x) * c3 + y * c1;
ans = min(ans, cost);
} else {
cost = x * c6 + y * c2;
ans = min(ans, cost);
cost = (x - y) * c5 + x * c1;
ans = min(ans, cost);
cost = (x - y) * c6 + y * c1;
ans = min(ans, cost);
}
} else if (x >= 0 && y <= 0) {
cost = x * c6 - y * c5;
ans = min(ans, cost);
cost = x * c1 + (x - y) * c5;
ans = min(ans, cost);
cost = c6 * (x - y) + c4 * (-1 * y);
ans = min(ans, cost);
} else if (x <= 0 && y <= 0) {
if (x >= y) {
cost = -c5 * y + -1 * x * c3;
ans = min(ans, cost);
cost = (x - y) * c5 - x * c4;
ans = min(ans, cost);
cost = (x - y) * c6 + (-1 * y) * c4;
ans = min(ans, cost);
} else {
cost = (y - x) * c3 + -1 * y * c4;
ans = min(ans, cost);
cost = (y - x) * c2 + (-1 * x * c4);
ans = min(ans, cost);
cost = -1 * x * c3 + -1 * y * c5;
ans = min(ans, cost);
}
} else {
cost = c2 * y - c3 * x;
ans = min(ans, cost);
cost = (y - x) * c3 + c1 * y;
ans = min(ans, cost);
cost = c4 * -1 * x + (y - x) * c2;
ans = min(ans, cost);
}
cout << ans << "\n";
return;
}
int main() {
int t;
t = 1;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long T, x, y, c1, c2, c3, c4, c5, c6, ans, tp;
inline void cal() {
if (y > 0)
ans += min(c1 + c5, c6) * y;
else if (y < 0)
ans -= min(c3, c4 + c2) * y;
if (x > 0)
ans += min(c2, c3 + c1) * x;
else if (x < 0)
ans -= min(c5, c4 + c6) * x;
}
int main() {
cin >> T;
while (T--) {
cin >> y >> x;
ans = 0;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
if (x * y > 0) {
if (x > 0) {
tp = min(x, y);
ans += min(c2 + c6, c1) * tp;
x -= tp;
y -= tp;
}
if (x < 0) {
tp = min(-x, -y);
ans += min(c4, c3 + c5) * tp;
x += tp;
y += tp;
}
}
cal();
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void print() { cout << endl; }
template <typename First, typename... Strings>
void print(First arg, const Strings&... rest) {
cout << arg << " ";
print(rest...);
}
template <typename First>
void print_vec(vector<First>& v) {
for (First x : v) cout << x << " ";
cout << endl;
}
void test_case() {
long long int a, b;
cin >> a >> b;
vector<long long int> c(6);
for (long long int i = 0; i < 6; i++) cin >> c[i];
long long int u, d, l, r;
u = min(c[5], c[4] + c[0]);
d = min(c[2], c[3] + c[1]);
l = min(c[4], c[5] + c[3]);
r = min(c[1], c[0] + c[2]);
long long int res = 0;
if (a < 0)
res += (-a) * d;
else
res += (a)*u;
if (b < 0)
res += (-b) * l;
else
res += (b)*r;
if (a > 0 && b > 0)
res =
min(res, min(a, b) * c[0] + (a - min(a, b)) * u + (b - min(a, b)) * r);
if (a < 0 && b < 0)
res = min(res,
-(max(a, b) * c[3] + (a - max(a, b)) * d + (b - max(a, b)) * l));
print(res);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
cin >> t;
for (long long int i = 0; i < t; i++) {
test_case();
}
return 0;
}
|
#include <bits/stdc++.h>
using pii = std::pair<long long, long long>;
using namespace std;
long long t, x, y, origc[6], bestc[6];
long long getans(long long x, long long y, long long take_common,
long long take_x, long long take_y) {
long long common = min(x, y);
long long curans = 0;
if (x == 0 && y == 0) return 0;
if (common > 0) {
assert(take_common >= 0 && take_common < 6);
curans += common * bestc[take_common];
}
if (x > y) {
assert(take_x >= 0 && take_x < 6);
curans += (x - y) * bestc[take_x];
} else {
assert(take_y >= 0 && take_y < 6);
curans += (y - x) * bestc[take_y];
}
return curans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> t;
for (long long cases = 0; cases < t; cases++) {
cin >> x >> y;
for (long long i = 0; i < 6; i++) {
cin >> origc[i];
bestc[i] = origc[i];
}
for (long long j = 0; j < 1000; j++)
for (long long i = 0; i < 6; i++)
bestc[i] = min(bestc[i], bestc[(i + 1) % 6] + bestc[(i - 1 + 6) % 6]);
long long ans = -1;
if (x >= 0 && y >= 0)
cout << getans(abs(x), abs(y), 0, 5, 1) << "\n";
else if (x < 0 && y < 0)
cout << getans(abs(x), abs(y), 3, 2, 4) << "\n";
else if (x >= 0 && y < 0)
cout << getans(abs(x), 0, -1, 5, -1) + getans(0, abs(y), -1, -1, 4)
<< "\n";
else if (x < 0 && y >= 0)
cout << getans(abs(x), 0, -1, 2, -1) + getans(0, abs(y), -1, -1, 1)
<< "\n";
else
assert(false);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long infy = 1e30;
void solve() {
long long x, y, c[8], ax, ay;
cin >> x >> y;
ax = abs(x);
ay = abs(y);
for (long long i = 1; i <= 6; i++) cin >> c[i];
c[7] = c[1];
c[0] = c[6];
for (long long i = 1; i <= 6; i++) c[i] = min(c[i], c[i - 1] + c[i + 1]);
long long ans;
if (x >= 0 && y >= 0) {
if (ax > ay)
ans = ((ax - ay) * c[6]) + (ay * c[1]);
else
ans = (ax * c[1]) + ((ay - ax) * c[2]);
}
if (x >= 0 && y < 0) ans = (ax * c[6]) + (ay * c[5]);
if (x < 0 && y >= 0) ans = (ax * c[3]) + (ay * c[2]);
if (x < 0 && y < 0) {
if (ax > ay)
ans = ((ax - ay) * c[3]) + (ay * c[4]);
else
ans = (ax * c[4]) + ((ay - ax) * c[5]);
}
cout << ans << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
long long X, Y;
cin >> X >> Y;
long long A, B, C, D, E, F;
cin >> A >> B >> C >> D >> E >> F;
long long ans = 4e18;
if (Y >= X && X >= 0) {
ckmin(ans, A * X + B * (Y - X));
}
if (Y >= 0 && X <= Y) {
ckmin(ans, Y * A + (Y - X) * C);
}
if (X >= 0 && Y <= X) {
ckmin(ans, X * A + (X - Y) * E);
}
if (X >= Y && Y >= 0) {
ckmin(ans, A * Y + F * (X - Y));
}
if (Y <= X && X <= 0) {
ckmin(ans, D * (-X) + E * (X - Y));
}
if (X <= Y && Y <= 0) {
ckmin(ans, D * (-Y) + C * (Y - X));
}
if (Y <= 0 && Y <= X) {
ckmin(ans, D * (-Y) + F * (X - Y));
}
if (X <= 0 && X <= Y) {
ckmin(ans, D * (-X) + B * (Y - X));
}
long long xc = X * F;
if (X < 0) xc = (-X) * C;
long long yc = Y * B;
if (Y < 0) yc = (-Y) * E;
cout << min(ans, xc + yc) << nl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
template <typename Arg1>
void prn(bool space, Arg1&& arg1) {
cout << arg1;
if (space)
cout << " ";
else
cout << "\n";
}
template <typename Arg1, typename... Args>
void prn(bool space, Arg1&& arg1, Args&&... args) {
prn(space, arg1);
prn(space, args...);
}
long long mulmod(long long a, long long b) {
long long res = 0;
a = a % MOD;
while (b > 0) {
if (b % 2 == 1) res = (res + a) % MOD;
a = (a * 2) % MOD;
b /= 2;
}
return res;
}
long long powmod(long long x, long long y) {
long long res = 1;
x = x % MOD;
while (y > 0) {
if (y & 1) res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
long long log(long long n, long long b) {
long long c = 0;
while (n > (b - 1)) {
c++;
n /= b;
}
return c;
}
void solve() {
long long i, j;
long long x, y;
cin >> x >> y;
vector<long long> c(7);
for (i = 1; i <= 6; i++) cin >> c[i];
c[2] = min(c[2], c[1] + c[3]);
c[5] = min(c[5], c[6] + c[4]);
c[1] = min(c[1], c[2] + c[6]);
c[3] = min(c[3], c[2] + c[4]);
c[4] = min(c[4], c[5] + c[3]);
c[6] = min(c[6], c[5] + c[1]);
if (x >= 0 && y >= 0) {
long long ans1 = y * c[2] + x * c[6];
long long ans2 = min(x, y) * c[1];
long long temp = min(x, y);
x -= temp;
y -= temp;
if (x > 0) {
ans2 += x * c[6];
} else
ans2 += y * c[2];
prn(0, min(ans1, ans2));
} else if (x <= 0 && y <= 0) {
y = -y;
x = -x;
long long ans1 = y * c[5] + x * c[3];
long long ans2 = min(x, y) * c[4];
long long temp = min(x, y);
x -= temp;
y -= temp;
if (x > 0) {
ans2 += x * c[3];
} else
ans2 += y * c[5];
prn(0, min(ans1, ans2));
} else if (x >= 0 && y <= 0) {
prn(0, x * c[6] - y * c[5]);
} else
prn(0, y * c[2] - x * c[3]);
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
for (long long tt = 1; tt <= t; tt++) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
using vec = vector<T>;
auto _fast_io = []() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
return cout.tie(nullptr);
}();
template <typename T>
istream& operator>>(istream& in, vector<T>& v) {
for (size_t i = 0; i < v.size(); ++i) {
in >> v[i];
}
return in;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T>& v) {
if (!v.empty()) {
out << v.front();
for (size_t i = 1; i < v.size(); ++i) {
out << " " << v[i];
}
}
return out;
}
template <typename T, size_t Size>
istream& operator>>(istream& in, array<T, Size>& v) {
for (size_t i = 0; i < Size; ++i) {
in >> v[i];
}
return in;
}
template <typename T, size_t Size>
ostream& operator<<(ostream& out, array<T, Size>& v) {
if (!v.empty()) {
out << v.front();
for (size_t i = 1; i < v.size(); ++i) {
out << " " << v[i];
}
}
return out;
}
inline int64_t gcd(int64_t a, int64_t b) {
if (a < b) std::swap(a, b);
while (b != 0) {
int64_t tmp = a % b;
a = b;
b = tmp;
}
return a;
}
struct fraction {
fraction() = default;
fraction(int64_t nom, int64_t denom)
: nom(nom), denom(denom), full_denom(denom) {
normalize();
}
void normalize() {
auto g = gcd(nom, denom);
nom /= g;
denom /= g;
}
int64_t nom;
int denom;
int full_denom;
};
inline bool operator==(const fraction& left, const fraction& right) {
return left.nom == right.nom && left.denom == right.denom;
}
inline bool operator<(const fraction& left, const fraction& right) {
auto left_w = left.nom * right.denom;
auto right_w = right.nom * left.denom;
return left_w < right_w ||
left_w == right_w && left.full_denom < right.full_denom;
}
inline bool operator<(int64_t left, const fraction& right) {
return left * right.denom < right.nom;
}
int64_t calculate_cost(int64_t x, int64_t y, int64_t c, int64_t cx, int64_t cy,
int s, int sx, int sy) {
int64_t q_lower = 0;
int64_t q_upper = std::numeric_limits<int64_t>::max();
if (s * sx > 0)
q_upper = min(q_upper, s * x);
else
q_lower = max(q_lower, s * x);
if (s * sy > 0)
q_upper = min(q_upper, s * y);
else
q_lower = max(q_lower, s * y);
if (q_lower > q_upper) return -1;
auto coef = c - s * sx * cx - s * sy * cy;
if (coef >= 0) {
return cx * sx * x + coef * q_lower + cy * sy * y;
} else {
return cx * sx * x + coef * q_upper + cy * sy * y;
}
}
void run_test(size_t t) {
int64_t x, y;
int64_t c[7];
cin >> x >> y;
cin >> c[1] >> c[2] >> c[3] >> c[4] >> c[5] >> c[6];
int64_t max_cost = std::numeric_limits<int64_t>::max();
{
auto cost = calculate_cost(x, y, c[1], c[6], c[2], 1, 1, 1);
if (cost != -1) max_cost = min(max_cost, cost);
}
{
auto cost = calculate_cost(x, y, c[1], c[6], c[5], 1, 1, -1);
if (cost != -1) max_cost = min(max_cost, cost);
}
{
auto cost = calculate_cost(x, y, c[1], c[3], c[2], 1, -1, 1);
if (cost != -1) max_cost = min(max_cost, cost);
}
{
auto cost = calculate_cost(x, y, c[1], c[3], c[5], 1, -1, -1);
if (cost != -1) max_cost = min(max_cost, cost);
}
{
auto cost = calculate_cost(x, y, c[4], c[6], c[2], -1, 1, 1);
if (cost != -1) max_cost = min(max_cost, cost);
}
{
auto cost = calculate_cost(x, y, c[4], c[6], c[5], -1, 1, -1);
if (cost != -1) max_cost = min(max_cost, cost);
}
{
auto cost = calculate_cost(x, y, c[4], c[3], c[2], -1, -1, 1);
if (cost != -1) max_cost = min(max_cost, cost);
}
{
auto cost = calculate_cost(x, y, c[4], c[3], c[5], -1, -1, -1);
if (cost != -1) max_cost = min(max_cost, cost);
}
cout << max_cost << endl;
}
int main() {
size_t T;
cin >> T;
for (size_t t = 1; t <= T; ++t) {
run_test(t);
}
}
|
#include <bits/stdc++.h>
using namespace std;
void fastio() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void one();
int main() {
fastio();
int t = 1;
cin >> t;
for (int i = 0; i < t; ++i) {
one();
}
return 0;
}
long long c[6];
long long d[6];
void one() {
int x, y;
cin >> x >> y;
for (int i = 0; i < 6; ++i) cin >> c[i];
for (int i = 0; i < 6; ++i) {
d[i] = min(c[i], c[(i - 1 + 6) % 6] + c[(i + 1 + 6) % 6]);
}
map<pair<int, int>, long long> ma;
ma[{1, 1}] = d[0];
ma[{0, 1}] = d[1];
ma[{-1, 0}] = d[2];
ma[{-1, -1}] = d[3];
ma[{0, -1}] = d[4];
ma[{1, 0}] = d[5];
long long ans = 0;
if (y >= 0 && x >= 0) {
if (y >= x) {
ans = (y - x) * ma[{0, 1}] + x * ma[{1, 1}];
} else {
ans = min(y, x) * ma[{1, 1}] + (max(x, y) - min(x, y)) * ma[{1, 0}];
}
} else if (y >= 0 && x <= 0) {
ans = (y)*ma[{0, 1}] + (-x) * ma[{-1, 0}];
} else if (x >= 0 && y <= 0) {
ans = (-y) * ma[{0, -1}] + (x)*ma[{1, 0}];
} else {
if (y >= x) {
ans = min(-y, -x) * ma[{-1, -1}] +
(max(-x, -y) - min(-x, -y)) * ma[{-1, 0}];
} else {
ans = (-y - -x) * ma[{0, -1}] + (-x) * ma[{-1, -1}];
}
}
cout << ans << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
long long t, c[10], d[3][3],
moveit[10][2] = {{0, 0}, {1, 1}, {0, 1}, {-1, 0},
{-1, -1}, {0, -1}, {1, 0}};
long long x, y;
inline void dfs(long long now, long long mx, long long my, long long nowd) {
if (mx >= -1 && mx <= 1 && my >= -1 && my <= 1) {
if (d[mx + 1][my + 1] == 0 || d[mx + 1][my + 1] > nowd)
d[mx + 1][my + 1] = nowd;
}
if (now > 6) return;
dfs(now + 1, mx, my, nowd);
dfs(now + 1, mx + moveit[now][0], my + moveit[now][1], nowd + c[now]);
return;
}
inline long long check() {
c[1] = min(c[1], c[2] + c[6]);
c[2] = min(c[2], c[1] + c[3]);
c[3] = min(c[3], c[2] + c[4]);
c[4] = min(c[4], c[5] + c[3]);
c[5] = min(c[5], c[6] + c[4]);
c[6] = min(c[6], c[5] + c[1]);
if (x <= 0 && y <= 0) {
if ((abs(x) > abs(y))) return c[4] * abs(y) + c[3] * abs((abs(x) - abs(y)));
return c[4] * abs(x) + c[5] * abs((abs(x) - abs(y)));
}
if (x >= 0 && y >= 0) {
if (x > y) return c[1] * y + c[6] * (x - y);
return c[1] * x + c[2] * (y - x);
}
if (x >= 0 && y <= 0) return c[6] * x + c[5] * abs(y);
if (x <= 0 && y >= 0) return c[3] * (abs(x)) + c[2] * y;
}
signed main() {
cin >> t;
while (t--) {
cin >> x >> y;
for (long long i = 1; i <= 6; i++) cin >> c[i];
memset(d, 0, sizeof(d));
dfs(1, 0, 0, 0);
long long ans = 0;
if (x < 0 && y < 0) ans += d[0][0] * min(abs(x), abs(y));
if (x < 0 && y > 0) ans += d[0][2] * min(abs(x), abs(y));
if (x > 0 && y < 0) ans += d[2][0] * min(abs(x), abs(y));
if (x > 0 && y > 0) ans += d[2][2] * min(abs(x), abs(y));
if ((abs(x) > abs(y))) {
if (x < 0)
ans += d[0][1] * (abs(x) - abs(y));
else
ans += d[2][1] * (abs(x) - abs(y));
} else {
if (y < 0)
ans += d[1][0] * (abs(y) - abs(x));
else
ans += d[1][2] * (abs(y) - abs(x));
}
ans = min(ans, check());
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
vector<long long int> c(7);
void update() {
c[1] = min(c[1], c[2] + c[6]);
c[2] = min(c[2], c[3] + c[1]);
c[3] = min(c[3], c[2] + c[4]);
c[4] = min(c[4], c[3] + c[5]);
c[5] = min(c[5], c[4] + c[6]);
c[6] = min(c[6], c[5] + c[1]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int tt = 1;
cin >> tt;
while (tt--) {
int x, y;
cin >> y >> x;
for (int i = 1; i <= 6; i++) cin >> c[i];
update();
long long int ans = 0;
if (y > 0) {
long long int ans1, ans2;
ans1 = y * c[6] + abs(x) * (x > 0 ? c[2] : c[5]);
if (x > 0 && c[6] + c[2] > c[1]) {
ans1 = ans1 - min(y, x) * (c[6] + c[2]) + min(y, x) * c[1];
}
ans2 = y * c[1] + abs(y - x) * (x > y ? c[2] : c[5]);
if (x < y && c[1] + c[5] > c[6]) {
ans2 = ans2 - min(y, abs(y - x)) * (c[1] + c[5]) +
min(y, abs(y - x)) * c[6];
}
ans = min(ans1, ans2);
} else if (y == 0) {
if (x > 0) {
ans = x * (c[1] + c[3]);
ans = min(ans, x * c[2]);
} else if (x < 0) {
ans = abs(x) * (c[6] + c[4]);
ans = min(ans, abs(x) * c[5]);
}
} else if (y < 0) {
long long int ans1, ans2;
ans1 = abs(y) * c[4] + abs(y - x) * (x > y ? c[2] : c[5]);
if (x > y && c[4] + c[2] > c[3]) {
ans1 = ans1 - min(abs(y), abs(y - x)) * (c[2] + c[4]) +
min(abs(y), abs(y - x)) * c[3];
}
ans2 = abs(y) * c[3] + abs(x) * (x > 0 ? c[2] : c[5]);
if (x < 0 && c[3] + c[5] > c[4]) {
ans2 = ans2 - min(abs(y), abs(x)) * (c[5] + c[3]) +
min(abs(y), abs(x)) * c[4];
}
ans = min(ans1, ans2);
}
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long x, y, c[6], ans;
void solve() {
cin >> x >> y;
ans = 0;
for (int i = 0; i < 6; i++) cin >> c[i];
for (int i = 0; i < 100; i++) {
c[0] = min(c[0], c[1] + c[5]);
c[1] = min(c[1], c[0] + c[2]);
c[2] = min(c[2], c[3] + c[1]);
c[3] = min(c[3], c[4] + c[2]);
c[4] = min(c[4], c[3] + c[5]);
c[5] = min(c[5], c[0] + c[4]);
}
if (x * y <= 0) {
if (x > 0)
ans += c[5] * x;
else
ans += c[2] * llabs(x);
if (y > 0)
ans += c[1] * y;
else
ans += c[4] * llabs(y);
} else if (x < 0) {
ans += llabs(max(x, y)) * c[3];
if (x < y)
ans += (y - x) * c[2];
else
ans += (x - y) * c[4];
} else {
ans += min(x, y) * c[0];
if (x < y)
ans += (y - x) * c[1];
else
ans += (x - y) * c[5];
}
cout << ans << '\n';
}
int main() {
int t;
cin >> t;
while (t--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
long long choose(long long n, long long k);
bool isprime(long long n);
long long power(long long x, long long y, long long p);
long long power(long long x, long long y);
long long gcd(long long a, long long b);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y;
long long c[7], d[7], ans[7] = {};
for (long long i = 1; i <= 6; i++) cin >> c[i];
d[1] = min({c[1], c[2] + c[6]});
d[2] = min({c[2], c[1] + c[3]});
d[3] = min({c[3], c[2] + c[4]});
d[4] = min({c[4], c[3] + c[5]});
d[5] = min({c[5], c[4] + c[6]});
d[6] = min({c[6], c[5] + c[1]});
long long rem;
if (x >= 0) {
if (y >= 0) {
ans[1] = x;
if (y < x) {
ans[5] = x - y;
rem = min(ans[1], ans[5]);
ans[1] -= rem;
ans[5] -= rem;
ans[6] += rem;
} else {
ans[2] = y - x;
}
} else {
ans[6] = x;
ans[5] = abs(y);
}
} else {
if (y >= 0) {
ans[3] = abs(x);
ans[2] = y;
} else {
ans[4] = abs(x);
if (abs(y) >= abs(x)) {
ans[5] = abs(y) - abs(x);
} else {
ans[2] = abs(x) - abs(y);
rem = min(ans[2], ans[4]);
ans[2] -= rem;
ans[4] -= rem;
ans[3] += rem;
}
}
}
long long tot = 0;
for (long long i = 1; i <= 6; i++) tot += (ans[i] * d[i]);
cout << tot << endl;
}
return 0;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long choose(long long n, long long k) {
if (k == 0) return 1;
return (n * choose(n - 1, k - 1)) / k;
}
bool isprime(long long n) {
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long power(long long x, long long y) {
long long res = 1;
while (y > 0) {
if (y & 1) res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
struct node {
long long dis, x, y;
node() {}
node(long long ndis, long long nx, long long ny) : dis(ndis), x(nx), y(ny) {}
friend bool operator<(const node &A, const node &B) { return A.dis > B.dis; }
};
priority_queue<node> Q;
const int dx[6] = {1, 0, -1, -1, 0, 1}, dy[6] = {1, 1, 0, -1, -1, 0};
long long dis[5][5];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
memset(dis, 0x3f3f3f3f3f, sizeof(dis));
dis[0][0] = 0;
long long x, y;
cin >> x >> y;
cin >> dis[2][2] >> dis[1][2] >> dis[0][1] >> dis[0][0] >> dis[1][0] >>
dis[2][1];
Q.emplace(dis[2][2], 2, 2);
Q.emplace(dis[1][2], 1, 2);
Q.emplace(dis[0][1], 0, 1);
Q.emplace(dis[0][0], 0, 0);
Q.emplace(dis[1][0], 1, 0);
Q.emplace(dis[2][1], 2, 1);
while (Q.size()) {
node np = Q.top();
Q.pop();
long long ndis = np.dis, nx = np.x, ny = np.y;
if (ndis != dis[nx][ny]) continue;
for (int i = 0; i < 6; i++) {
long long nex = nx + dx[i], ney = ny + dy[i];
if (nex > 2 || ney > 2 || nex < 0 || ney < 0) continue;
if (dis[nex][ney] > ndis + dis[dx[i] + 1][dy[i] + 1]) {
dis[nex][ney] = ndis + dis[dx[i] + 1][dy[i] + 1];
Q.emplace(dis[nex][ney], nex, ney);
}
}
}
long long rex, rey, mkx = (x < 0), mky = (y < 0);
x = abs(x);
y = abs(y);
if (x > y) {
rex = x - y;
rey = 0;
x = y;
} else
rex = 0, rey = y - x, y = x;
long long res = 0;
res = (rex ? (dis[1 + (mkx ? -1 : 1)][1] * rex)
: (dis[1][1 + (mky ? -1 : 1)] * rey));
if (x) res += x * dis[1 + (mkx ? -1 : 1)][1 + (mky ? -1 : 1)];
cout << res << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int mx[] = {1, 0, -1, -1, 0, 1};
int my[] = {1, 1, 0, -1, -1, 0};
long long c[7];
long long x, y;
long long f(int a, int b) {
if (mx[a] + mx[b] == 0 && my[a] + my[b] == 0) return LLONG_MAX;
long long sa = 0, sb = 0;
long long X = 0, Y = 0;
long long cost = 0;
if (mx[a] == 0) {
sb = llabs(x);
X += sb * mx[b];
Y += sb * my[b];
sa = llabs(Y - y);
} else if (my[a] == 0) {
sb = llabs(y);
X += sb * mx[b];
Y += sb * my[b];
sa = llabs(X - x);
} else if (mx[b] == 0) {
sa = llabs(x);
X += sa * mx[a];
Y += sa * my[a];
sb = llabs(Y - y);
} else if (my[b] == 0) {
sa = llabs(y);
X += sa * mx[a];
Y += sa * my[a];
sb = llabs(X - x);
} else {
sa = llabs(x);
sb = 0;
X += sa * mx[a];
Y += sb * mx[b];
}
if (sa * mx[a] + sb * mx[b] != x || sa * my[a] + sb * my[b] != y)
return LLONG_MAX;
cost = sa * c[a] + sb * c[b];
return cost;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%lld %lld", &x, &y);
for (int i = 0; i < 6; i++) scanf("%lld", &c[i]);
long long best = LLONG_MAX;
if (x == 0 && y == 0) {
printf("0\n");
continue;
}
for (int i = 0; i < 6; i++) {
for (int j = i; j < 6; j++) {
best = min(best, f(i, j));
}
}
printf("%lld\n", best);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
int t;
int INF = 1000228;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y;
long long c1, c2, c3, c4, c5, c6;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
c1 = min(c1, c2 + c6);
c2 = min(c2, c3 + c1);
c3 = min(c3, c4 + c2);
c4 = min(c4, c5 + c3);
c5 = min(c5, c6 + c4);
c6 = min(c6, c1 + c5);
if (x < 0) {
swap(c1, c4);
swap(c2, c5);
swap(c3, c6);
x = -x;
y = -y;
}
if (x <= y) {
cout << c1 * x + c2 * (y - x) << "\n";
} else if (y <= 0) {
cout << c6 * x + abs(y) * c5 << "\n";
} else {
cout << y * c1 + (x - y) * c6 << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace std;
const long double EPS = 1e-7;
namespace Simplex {
vector<int> X, Y;
vector<vector<long double>> A;
vector<long double> b, c;
long long z;
int n, m;
void pivot(int x, int y) {
swap(X[y], Y[x]);
b[x] /= A[x][y];
for (int i = 0, ThxDem = m; i < ThxDem; ++i)
if (i != y) A[x][i] /= A[x][y];
A[x][y] = 1 / A[x][y];
for (int i = 0, ThxDem = n; i < ThxDem; ++i)
if (i != x && abs(A[i][y]) > EPS) {
b[i] -= A[i][y] * b[x];
for (int j = 0, ThxDem = m; j < ThxDem; ++j)
if (j != y) A[i][j] -= A[i][y] * A[x][j];
A[i][y] = -A[i][y] * A[x][y];
}
z += (long long)(c[y]) * (long long)(b[x]);
for (int i = 0, ThxDem = m; i < ThxDem; ++i)
if (i != y) c[i] -= c[y] * A[x][i];
c[y] = -c[y] * A[x][y];
}
pair<long long, vector<long double>> simplex(vector<vector<long double>> _A,
vector<long double> _b,
vector<long double> _c) {
A = _A;
b = _b;
c = _c;
n = b.size();
m = c.size();
z = 0.;
X = vector<int>(m);
Y = vector<int>(n);
for (int i = 0, ThxDem = m; i < ThxDem; ++i) X[i] = i;
for (int i = 0, ThxDem = n; i < ThxDem; ++i) Y[i] = i + m;
while (1) {
int x = -1, y = -1;
long double mn = -EPS;
for (int i = 0, ThxDem = n; i < ThxDem; ++i)
if (b[i] < mn) mn = b[i], x = i;
if (x < 0) break;
for (int i = 0, ThxDem = m; i < ThxDem; ++i)
if (A[x][i] < -EPS) {
y = i;
break;
}
if (y < 0) {
exit(0);
}
pivot(x, y);
}
while (1) {
long double mx = EPS;
int x = -1, y = -1;
for (int i = 0, ThxDem = m; i < ThxDem; ++i)
if (c[i] > mx) mx = c[i], y = i;
if (y < 0) break;
long double mn = 1e200;
for (int i = 0, ThxDem = n; i < ThxDem; ++i)
if (A[i][y] > EPS && b[i] / A[i][y] < mn) mn = b[i] / A[i][y], x = i;
if (x < 0) {
exit(0);
}
pivot(x, y);
}
vector<long double> r(m);
for (int i = 0, ThxDem = n; i < ThxDem; ++i)
if (Y[i] < m) r[Y[i]] = b[i];
return make_pair(z, r);
}
} // namespace Simplex
int main() {
vector<vector<long double>> A{{1, 0, -1, -1, 0, 1},
{1, 1, 0, -1, -1, 0},
{-1, 0, 1, 1, 0, -1},
{-1, -1, 0, 1, 1, 0}};
vector<long double> c(6, 0);
vector<long double> b(4, 0);
int t;
cin >> t;
while (t--) {
cin >> b[0] >> b[1];
b[2] = -b[0];
b[3] = -b[1];
cin >> c[0] >> c[1] >> c[2] >> c[3] >> c[4] >> c[5];
for (int i = 0; i < 6; i++) c[i] *= -1;
long long mx = Simplex::simplex(A, b, c).first;
mx *= -1;
cout << mx << endl;
}
}
|
#include <bits/stdc++.h>
int t;
long long x, y;
long long c[10];
int main() {
scanf("%d", &t);
while (t--) {
scanf("%lld%lld", &x, &y);
for (int i = 1; i <= 6; ++i) {
scanf("%lld", &c[i]);
}
if (c[2] > c[1] + c[3]) {
c[2] = c[1] + c[3];
}
if (c[3] > c[2] + c[4]) {
c[3] = c[2] + c[4];
}
if (c[5] > c[4] + c[6]) {
c[5] = c[4] + c[6];
}
if (c[6] > c[5] + c[1]) {
c[6] = c[5] + c[1];
}
if (c[4] > c[3] + c[5]) {
c[4] = c[3] + c[5];
}
if (c[1] > c[2] + c[6]) {
c[1] = c[2] + c[6];
}
if (x > 0 && y > 0) {
if (x < y) {
printf("%lld\n", c[1] * x + c[2] * (y - x));
} else {
printf("%lld\n", c[1] * y + c[6] * (x - y));
}
} else if (x < 0 && y < 0) {
if (-x < -y) {
printf("%lld\n", c[4] * (-x) + c[5] * ((-y) - (-x)));
} else {
printf("%lld\n", c[4] * (-y) + c[3] * ((-x) - (-y)));
}
} else {
long long ans = 0ll;
if (x > 0) {
ans += c[6] * x;
} else {
ans -= c[3] * x;
}
if (y > 0) {
ans += c[2] * y;
} else {
ans -= c[5] * y;
}
printf("%lld\n", ans);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = (ll)4e18 + 10;
void solve() {
ll x, y;
cin >> x >> y;
vector<ll> c(6);
for (ll i = (0); i < (6); i++) cin >> c[i];
for (ll i = (0); i < (3); i++) {
vector<ll> nc(6);
for (ll j = (0); j < (6); j++)
nc[j] = min(c[j], c[(j + 5) % 6] + c[(j + 1) % 6]);
c = nc;
}
ll ans = 0;
if (x <= y and x >= 0) {
ans = c[0] * x + c[1] * (y - x);
} else if (x < 0 and y >= 0) {
ans = c[1] * y - c[2] * x;
} else if (x <= y and y < 0) {
ans = c[3] * (-y) + c[2] * (y - x);
} else if (x >= y and x < 0) {
ans = c[3] * (-x) + c[4] * (x - y);
} else if (x >= 0 and y < 0) {
ans = c[5] * x - c[4] * y;
} else if (x >= y and y >= 0) {
ans = c[0] * y + c[5] * (x - y);
}
cout << ans << endl;
return;
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> fac(1e6);
long long p = 1e9 + 7;
long long gcd(long long x, long long y) {
if (x == 0) {
return y;
}
return gcd(y % x, x);
}
long long power(long long a, long long x) {
long long ans = 1;
while (x) {
if (x & 1) {
ans = (ans * a) % p;
}
x = x >> 1;
a = (a * a) % p;
}
return ans;
}
long long modInv(long long a) { return power(a, p - 2); }
long long ncr(long long n, long long r) {
long long ans = 1;
if (r != 0 && r != n) {
ans = fac[n] * modInv(fac[r]) % p * modInv(fac[n - r]) % p;
}
return ans;
}
void pre() {
fac[0] = 1;
for (int i = 1; i < (int)fac.size(); i++) {
fac[i] = (fac[i - 1] * i) % p;
}
}
void solve() {
long long x, y, d, ans = 0;
cin >> x >> y;
vector<long long> c(6);
for (auto &i : c) {
cin >> i;
}
for (int i = 0; i < 12; i++) {
c[0] = min(c[0], c[1] + c[5]);
c[1] = min(c[1], c[0] + c[2]);
c[2] = min(c[2], c[1] + c[3]);
c[3] = min(c[3], c[2] + c[4]);
c[4] = min(c[4], c[3] + c[5]);
c[5] = min(c[5], c[0] + c[4]);
}
d = min(abs(x), abs(y));
if (d) {
if (x >= 0) {
x -= d;
if (y >= 0) {
ans = d * c[0];
y -= d;
} else {
ans = d * (c[4] + c[5]);
y += d;
}
} else {
x += d;
if (y >= 0) {
ans = d * (c[1] + c[2]);
y -= d;
} else {
ans = d * c[3];
y += d;
}
}
}
if (x) {
ans += abs(x) * (x > 0 ? c[5] : c[2]);
} else {
ans += abs(y) * (y > 0 ? c[1] : c[4]);
}
cout << ans << endl;
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
const int MAXN = 2e5 + 15;
using namespace std;
void solve() {
long long x, y;
cin >> x >> y;
int C[7];
for (int i = 1; i <= 6; i++) cin >> C[i];
if ((x >= 0 && y >= 0) || (x <= 0 && y <= 0)) {
if (x < 0 || y < 0) {
swap(C[1], C[4]);
swap(C[2], C[5]);
swap(C[3], C[6]);
}
long long last = abs(x) * C[6] + abs(y) * C[2];
if (abs(x) > abs(y)) {
swap(C[2], C[6]);
swap(C[3], C[5]);
swap(x, y);
}
x = abs(x);
y = abs(y);
cout << min(min(y * C[1] + (y - x) * C[3], x * C[1] + (y - x) * C[2]),
last);
} else {
if (x < 0) {
swap(C[1], C[4]);
swap(C[2], C[5]);
swap(C[3], C[6]);
}
x = abs(x);
y = abs(y);
cout << min(min(x * C[1] + (x + y) * C[5], y * C[4] + (x + y) * C[6]),
x * C[6] + y * C[5]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
cout << fixed << setprecision(12);
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
solve();
cout << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long t;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y;
long long arr[6];
for (long long i = 0; i < 6; i++) {
cin >> arr[i];
}
if (x == 0 && y == 0) {
cout << 0 << endl;
continue;
}
long long a[6];
memset(a, 0, sizeof(a));
if (x > 0) {
a[5] = x;
}
if (x < 0) {
a[2] = (-1) * x;
}
if (y > 0) {
a[1] = y;
}
if (y < 0) {
a[4] = (-1) * y;
}
long long sum = 0;
for (long long i = 0; i < 6; i++) {
long long mn = min(a[(i + 1 + 6) % 6], a[(i - 1 + 6) % 6]);
a[i] += mn;
a[(i + 1 + 6) % 6] -= mn;
a[(i - 1 + 6) % 6] -= mn;
}
for (long long i = 0; i < 6; i++) {
sum += min(arr[i] * a[i],
arr[(i - 1 + 6) % 6] * a[i] + arr[(i + 1 + 6) % 6] * a[i]);
}
cout << sum << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long x, y;
long long a[10];
vector<long long> dx = {1, 0, -1, -1, 0, 1};
vector<long long> dy = {1, 1, 0, -1, -1, 0};
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t;
cin >> t;
while (t--) {
cin >> x >> y;
for (long long i = 0; i < 6; i++) cin >> a[i];
for (long long i = 1; i <= 100; i++)
for (long long j = 0; j < 6; j++)
a[j] = min(a[j], a[(j - 1 + 6) % 6] + a[(j + 1) % 6]);
long long ans = 4e18;
for (long long i = 0; i < 6; i++) {
long long rx = x, ry = y;
long long take = 2e9;
if (dx[i] != 0) take = min(take, rx / dx[i]);
if (dy[i] != 0) take = min(take, ry / dy[i]);
if (take < 0) take = take * (-1);
rx -= take * dx[i];
ry -= take * dy[i];
long long curCost = take * a[i];
long long cx = rx, cy = ry;
for (long long j = 0; j < 6; j++) {
long long rx = cx, ry = cy;
long long take = 2e9;
if (dx[j] != 0) take = min(take, rx / dx[j]);
if (dy[j] != 0) take = min(take, ry / dy[j]);
if (take < 0) take = take * (-1);
rx -= take * dx[j];
ry -= take * dy[j];
if (rx == 0 && ry == 0) ans = min(ans, curCost + take * a[j]);
}
}
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t-- > 0) {
long long int x;
long long int y;
cin >> x;
cin >> y;
long long int c[6];
for (int i = 0; i < 6; i++) cin >> c[i];
int flag = 1;
while (flag == 1) {
flag = 0;
if (c[0] + c[4] < c[5]) {
c[5] = c[0] + c[4];
flag = 1;
}
if (c[5] + c[1] < c[0]) {
c[0] = c[5] + c[1];
flag = 1;
}
if (c[0] + c[2] < c[1]) {
c[1] = c[0] + c[2];
flag = 1;
}
if (c[1] + c[3] < c[2]) {
c[2] = c[1] + c[3];
flag = 1;
}
if (c[2] + c[4] < c[3]) {
c[3] = c[2] + c[4];
flag = 1;
}
if (c[3] + c[5] < c[4]) {
c[4] = c[3] + c[5];
flag = 1;
}
}
long long int cost = 0;
if ((x == 0) && (y == 0))
cout << 0 << endl;
else {
if (x >= 0 && y >= 0) {
cost = cost + c[0] * min(x, y);
if (y > x) {
cost = cost + c[1] * (y - x);
} else
cost = cost + c[5] * (x - y);
} else if (x <= 0 && y <= 0) {
cost = cost - c[3] * max(x, y);
if (y > x) {
cost = cost + c[2] * (y - x);
} else
cost = cost + c[4] * (x - y);
} else {
if (x < 0) {
cost = cost - x * c[2];
} else if (x > 0) {
cost = cost + x * c[5];
}
if (y < 0) {
cost = cost - y * c[4];
} else if (y > 0) {
cost = cost + y * c[1];
}
}
cout << cost << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int tc, n, m, k;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cin >> tc;
while (tc--) {
long long int x, y;
cin >> x >> y;
n = 7;
vector<long long> arr(7, 0);
for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n));
i += 1 - 2 * ((1) > (n)))
cin >> arr[i];
arr[1] = min(arr[1], arr[6] + arr[2]);
arr[2] = min(arr[2], arr[1] + arr[3]);
arr[3] = min(arr[3], arr[2] + arr[4]);
arr[4] = min(arr[4], arr[3] + arr[5]);
arr[5] = min(arr[5], arr[4] + arr[6]);
arr[6] = min(arr[6], arr[5] + arr[1]);
long long int ans = 5e18;
if (x >= 0 && y >= 0) {
if (x >= y) {
ans = min(ans, (x - y) * arr[6] + y * arr[1]);
} else {
ans = min(ans, (y - x) * arr[2] + x * arr[1]);
}
} else if (x <= 0 && y >= 0) {
ans = min(ans, y * arr[2] + -x * arr[3]);
} else if (x <= 0 && y <= 0) {
if (x <= y) {
ans = min(ans, (y - x) * arr[3] + -y * arr[4]);
} else {
ans = min(ans, (x - y) * arr[5] - x * arr[4]);
}
} else {
ans = min(ans, x * arr[6] + -y * arr[5]);
}
cout << ans;
cout << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
const long long MOD2 = (long long)998244353 * (long long)998244353;
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "YES" : "NO");
exit(0);
}
void addmod(int& x, int y, int mod = 998244353) {
x += y;
if (x >= mod) x -= mod;
if (x < 0) x += mod;
assert(x >= 0 && x < mod);
}
void et(int x = -1) {
printf("%d\n", x);
exit(0);
}
long long fastPow(long long x, long long y, int mod = 998244353) {
long long ans = 1;
while (y > 0) {
if (y & 1) ans = (x * ans) % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
int Dx[] = {1, 0, -1, -1, 0, 1};
int Dy[] = {1, 1, 0, -1, -1, 0};
int c[10];
void fmain(int tid) {
int x, y;
scanf("%d%d", &x, &y);
for (int(i) = 0; (i) < (int)(6); (i)++) scanf("%d", c + i);
if (x == 0 && y == 0) {
puts("0");
return;
}
long long ans = ((1LL << 60)) * 6;
for (int(i) = 0; (i) < (int)(6); (i)++)
for (int j = i + 1; j < 6; j++) {
int z = Dy[j] * Dx[i] - Dx[j] * Dy[i];
int Z = x * Dy[j] - y * Dx[j];
if (z == 0) {
continue;
}
int pa = Z / z, pb;
if (Dx[j] == 0) {
pb = (y - Dy[i] * pa) / Dy[j];
} else {
pb = (x - Dx[i] * pa) / Dx[j];
}
if (pa < 0 || pb < 0) continue;
mintt(ans, (long long)pa * c[i] + (long long)pb * c[j]);
}
printf("%lld\n", ans);
}
int main() {
int t = 1;
scanf("%d", &t);
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain(i);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 3;
const long long nax = 2e6 + 2;
const long long max_val = 5e4 + 10;
const long long mod = 998244353;
const long long bits = 18;
long long caseNumber = 1;
void solve() {
long long x, y;
cin >> x >> y;
vector<long long> c(6);
for (int i = 0; i < 6; i++) cin >> c[i];
bool isPosX = ((x >= 0) ? 1 : 0), isPosY = ((y >= 0) ? 1 : 0);
if (isPosY == isPosX) {
if (isPosY == 1) {
long long z = min(x, y);
long long fir = c[1] * y + c[5] * x;
long long sec = z * c[0];
long long ok = max(x, y);
long long thi = ok * c[0];
if (ok > x) {
thi += c[2] * (ok - x);
}
if (ok > y) {
thi += c[4] * (ok - y);
}
if (x > z) {
sec += c[5] * (x - z);
}
if (y > z) {
sec += c[1] * (y - z);
}
cout << min(min(fir, sec), thi) << "\n";
return;
} else {
x = abs(x);
y = abs(y);
long long z = min(x, y);
long long ok = max(x, y);
long long fir = c[4] * y + c[2] * x;
long long sec = z * c[3];
long long thi = ok * (c[3]);
if (x > z) {
sec += c[2] * (x - z);
}
if (y > z) {
sec += c[4] * (y - z);
}
if (ok > x) {
thi += c[5] * (ok - x);
}
if (ok > y) {
thi += c[1] * (ok - y);
}
cout << (min(fir, min(sec, thi))) << "\n";
return;
}
} else {
if (isPosX) {
y = abs(y);
long long fir = c[5] * x + c[4] * y;
long long sec = c[3] * y + c[5] * (x + y);
long long thi = c[0] * x + c[4] * (x + y);
cout << min({fir, sec, thi}) << "\n";
return;
} else {
x = abs(x);
long long fir = c[2] * x + c[1] * y;
long long sec = c[3] * x + c[1] * (x + y);
long long thi = c[0] * y + c[2] * (x + y);
cout << min({fir, sec, thi}) << "\n";
return;
}
}
}
bool TestCase1 = 1;
bool isGoogles = 0;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long t;
t = 1;
if (TestCase1) {
cin >> t;
}
long long here = 1;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
inline int read() {
char c = getchar();
int x = 0, f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
using namespace std;
long long x, y, t, c[10];
int main() {
cin >> t;
while (t--) {
long long ans = 0;
cin >> x >> y;
for (int i = 1; i <= 6; i++) {
cin >> c[i];
}
if (x >= 0 && y < 0) {
if (c[6] < c[1] + c[5]) {
ans += x * c[6];
} else {
ans += x * (c[1] + c[5]);
}
if (c[5] < c[4] + c[6]) {
ans += (-y) * c[5];
} else {
ans += (-y) * (c[4] + c[6]);
}
}
if (x < 0 && y >= 0) {
if (c[2] < c[1] + c[3]) {
ans += y * c[2];
} else {
ans += y * (c[1] + c[3]);
}
if (c[3] < c[4] + c[2]) {
ans += (-x) * c[3];
} else {
ans += (-x) * (c[4] + c[2]);
}
}
if (x >= 0 && y >= 0) {
if (c[1] < c[2] + c[6]) {
ans += min(x, y) * c[1];
} else {
ans += min(x, y) * (c[2] + c[6]);
}
if (x >= y) {
if (c[6] < c[1] + c[5]) {
ans += (x - y) * c[6];
} else {
ans += (x - y) * (c[1] + c[5]);
}
} else {
if (c[2] < c[1] + c[3]) {
ans += (y - x) * c[2];
} else {
ans += (y - x) * (c[1] + c[3]);
}
}
}
if (x < 0 && y < 0) {
if (c[4] < c[3] + c[5]) {
ans += min(-x, -y) * c[4];
} else {
ans += min(-x, -y) * (c[3] + c[5]);
}
if (x < y) {
if (c[3] < c[4] + c[2]) {
ans += (y - x) * c[3];
} else {
ans += (y - x) * (c[2] + c[4]);
}
} else {
if (c[5] < c[4] + c[6]) {
ans += (x - y) * c[5];
} else {
ans += (x - y) * (c[4] + c[6]);
}
}
}
cout << ans << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
std::array<long long, 6> step;
void minimize(int i, int depth = 1) {
if (depth > 20) return;
int l = (i + 5) % 6;
int r = (i + 1) % 6;
minimize(l, depth + 1);
minimize(r, depth + 1);
step[i] = std::min(step[i], step[l] + step[r]);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
int t = 1;
std::cin >> t;
while (t--) {
long long x, y;
std::cin >> x >> y;
std::vector<long long> step(6);
for (int i = 0; i < 6; i++) std::cin >> step[i];
step[0] = std::min(step[0], step[1] + step[5]);
step[2] = std::min(step[2], step[3] + step[1]);
step[4] = std::min(step[4], step[5] + step[3]);
step[1] = std::min(step[1], step[2] + step[0]);
step[3] = std::min(step[3], step[4] + step[2]);
step[5] = std::min(step[5], step[0] + step[4]);
step[0] = std::min(step[0], step[1] + step[5]);
step[2] = std::min(step[2], step[3] + step[1]);
step[4] = std::min(step[4], step[5] + step[3]);
step[1] = std::min(step[1], step[2] + step[0]);
step[3] = std::min(step[3], step[4] + step[2]);
step[5] = std::min(step[5], step[0] + step[4]);
step[0] = std::min(step[0], step[1] + step[5]);
step[2] = std::min(step[2], step[3] + step[1]);
step[4] = std::min(step[4], step[5] + step[3]);
step[1] = std::min(step[1], step[2] + step[0]);
step[3] = std::min(step[3], step[4] + step[2]);
step[5] = std::min(step[5], step[0] + step[4]);
long long d = 0;
if (x * y > 0) {
if (x < 0) {
long long t = std::max(x, y);
d += -t * step[3];
x += -t;
y += -t;
} else {
long long t = std::min(x, y);
d += t * step[0];
x -= t;
y -= t;
}
}
if (x > 0) d += x * step[5];
if (x < 0) d += -x * step[2];
if (y > 0) d += y * step[1];
if (y < 0) d += -y * step[4];
std::cout << d << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b) {
long long int res = 1;
a = a % 1000000007;
while (b > 0) {
if (b & 1) {
res = (res * a) % 1000000007;
b--;
}
a = (a * a) % 1000000007;
b >>= 1;
}
return res;
}
long long int gcd(long long int a, long long int b) {
return (b == 0) ? a : gcd(b, a % b);
}
using namespace std;
long long int solve(long long int x, long long int pos, long long int neg) {
if (x > 0)
return x * pos;
else
return (-x) * neg;
}
int main() {
long long int T, mn, x, y;
long long int c[7];
cin >> T;
while (T--) {
cin >> x >> y;
for (int i = 1; i <= 6; i++) cin >> c[i];
mn = 2e18;
mn = min(mn, solve(x, c[6], c[3]) + solve(y, c[2], c[5]));
mn = min(mn, solve(x, c[1], c[4]) + solve(y - x, c[2], c[5]));
mn = min(mn, solve(y, c[1], c[4]) + solve(x - y, c[6], c[3]));
cout << mn << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll c[6];
int main() {
ios ::sync_with_stdio(0);
int T;
cin >> T;
while (T--) {
ll x, y;
cin >> x >> y;
for (int i = 0; i < 6; i++) cin >> c[i];
for (;;) {
bool f = 0;
for (int i = 0; i < 6; i++) {
if (c[i] + c[(i + 2) % 6] < c[(i + 1) % 6]) {
c[(i + 1) % 6] = c[i] + c[(i + 2) % 6];
f = 1;
}
}
if (!f) break;
}
while (!(x >= 0 && y >= x)) {
ll nx = y, ny = y - x;
x = nx;
y = ny;
int tc = c[0];
for (int i = 0; i < 5; i++) c[i] = c[i + 1];
c[5] = tc;
}
ll ans = c[0] * x + c[1] * (y - x);
cout << ans << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int x, y;
cin >> x >> y;
long long int c[10], i;
for (i = 1; i <= 6; i++) {
cin >> c[i];
}
c[0] = c[6];
c[7] = c[1];
for (i = 1; i <= 6; i++) {
c[i] = min(c[i - 1] + c[i + 1], c[i]);
}
long long int ans = 0;
if (x >= 0) {
if (y >= x) {
ans = (y - x) * c[2] + x * c[1];
} else if (y <= 0) {
ans = abs(y) * c[5] + x * c[6];
} else {
ans = y * c[1] + (x - y) * c[6];
}
} else {
if (x >= y) {
ans = ((x - y) * c[5]) + abs(x) * c[4];
} else if (y >= 0) {
ans = abs(x) * c[3] + y * c[2];
} else {
ans = abs(y) * c[4] + abs(x - y) * c[3];
}
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long calc(long long x, long long plus, long long minus) {
if (x >= 0)
return plus * x;
else
return minus * (-x);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tst;
cin >> tst;
while (tst--) {
long long x, y;
cin >> x >> y;
long long c1, c2, c3, c4, c5, c6;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
long long best = 5e18;
best = min(best, calc(x, c6, c3) + calc(y, c2, c5));
best = min(best, calc(y, c1, c4) + calc(x - y, c6, c3));
best = min(best, calc(x, c1, c4) + calc(y - x, c2, c5));
cout << best << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long MAXN = 2e5 + 5;
long long cost[6];
int main() {
int t;
cin >> t;
while (t--) {
long long n, m;
cin >> n >> m;
for (int i = 0; i < 6; i++) {
cin >> cost[i];
}
long long c = 1e18;
if (n >= 0 && m >= 0) {
if (n >= m) {
c = min(n * cost[5] + m * cost[1],
min(n * cost[0] + (n - m) * cost[4],
m * cost[0] + (n - m) * cost[5]));
}
if (n < m) {
c = min(n * cost[5] + m * cost[1],
min(n * cost[0] + (m - n) * cost[1],
m * cost[0] + (m - n) * cost[2]));
}
} else if (n <= 0 && m >= 0) {
c = min((-1) * n * cost[2] + m * cost[1],
min((-1) * n * cost[3] + (m - n) * cost[1],
m * cost[0] + cost[2] * (m - n)));
} else if (n >= 0 && m <= 0) {
c = min(n * cost[5] + (-1) * cost[4] * m,
min((-1) * m * cost[3] + (n - m) * cost[5],
n * cost[0] + (n - m) * cost[4]));
} else if (n <= 0 && m <= 0) {
n = (-1) * n;
m = (-1) * m;
if (n >= m) {
c = min(n * cost[2] + m * cost[4],
min(n * cost[3] + (n - m) * cost[1],
m * cost[3] + (n - m) * cost[2]));
} else {
c = min(n * cost[2] + m * cost[4],
min(n * cost[3] + (m - n) * cost[4],
m * cost[3] + (m - n) * cost[5]));
}
}
cout << c << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
int T;
int c[10];
int dx[10] = {1, 0, -1, -1, 0, 1};
int du[10] = {1, 1, 0, -1, -1, 0};
int main() {
cin >> T;
while (T--) {
int x, u;
scanf("%d%d", &x, &u);
for (int i = 0; i < 6; i++) {
scanf("%d", &c[i]);
}
while (true) {
bool flag = false;
for (int i = 0; i < 6; i++) {
int v = c[(i + 5) % 6];
int u = c[(i + 1) % 6];
if (u + v < c[i]) {
c[i] = u + v;
flag = true;
break;
}
}
if (!flag) break;
}
long long answ = 0;
if (x >= 0 && u >= 0) {
int t = min(x, u);
answ += t * 1ll * c[0];
x -= t;
u -= t;
answ += x * 1ll * c[5];
answ += u * 1ll * c[1];
cout << answ << endl;
continue;
}
if (x <= 0 && u <= 0) {
x *= -1;
u *= -1;
int t = min(x, u);
answ += t * 1ll * c[3];
x -= t;
u -= t;
answ += x * 1ll * c[2];
answ += u * 1ll * c[4];
cout << answ << endl;
continue;
}
if (x >= 0) {
answ += c[5] * 1ll * x;
} else {
answ -= c[2] * 1ll * x;
}
if (u >= 0) {
answ += c[1] * 1ll * u;
} else {
answ -= c[4] * 1ll * u;
}
cout << answ << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
int32_t main() {
std::ios::sync_with_stdio(false);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
int32_t T;
std::cin >> T;
while (T--) {
int32_t x, y;
std::cin >> x >> y;
int32_t cost[6];
for (int32_t i = 0; i < 6; i++) std::cin >> cost[i];
for (int32_t i = 0; i < 6; i++)
cost[i] = std::min(cost[i], cost[(i + 1) % 6] + cost[(i + 5) % 6]);
const std::pair<int32_t, int32_t> d[]{{1, 1}, {0, 1}, {-1, 0},
{-1, -1}, {0, -1}, {1, 0}};
auto solve = [&](const std::vector<int32_t>& order, int32_t x, int32_t y) {
int64_t sum = 0;
for (int32_t i : order) {
int32_t take = std::min(d[i].first ? x / d[i].first : INT_MAX,
d[i].second ? y / d[i].second : INT_MAX);
if (take < 0) continue;
sum += 1ll * take * cost[i];
x -= d[i].first * take;
y -= d[i].second * take;
}
return sum;
};
std::vector<int32_t> order(6);
std::iota(order.begin(), order.end(), 0);
int64_t best = LLONG_MAX;
do {
best = std::min(best, solve(order, x, y));
} while (std::next_permutation(order.begin(), order.end()));
std::cout << best << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = 2e4 + 5, inf = 0x3f3f3f3f, mod = 1e9 + 7;
long long c[7];
long long ans;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int x, y;
scanf("%d%d", &x, &y);
for (int i = 1; i <= 6; i++) scanf("%lld", &c[i]);
c[1] = min(c[1], c[2] + c[6]);
c[2] = min(c[2], c[1] + c[3]);
c[3] = min(c[3], c[2] + c[4]);
c[4] = min(c[4], c[3] + c[5]);
c[5] = min(c[5], c[4] + c[6]);
c[6] = min(c[6], c[1] + c[5]);
if (x >= 0 && y >= 0) {
if (x > y)
printf("%lld\n", y * c[1] + (x - y) * c[6]);
else
printf("%lld\n", x * c[1] + (y - x) * c[2]);
} else if (x <= 0 && y <= 0) {
if (x > y)
printf("%lld\n", (-x) * c[4] + (x - y) * c[5]);
else
printf("%lld\n", (-y) * c[4] + (y - x) * c[3]);
} else if (x >= 0 && y <= 0)
printf("%lld\n", x * c[6] + (-y) * c[5]);
else
printf("%lld\n", (-x) * c[3] + y * c[2]);
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-all-loops")
#pragma GCC target("sse4.2")
using namespace std;
ifstream in;
ofstream out;
const long long kk = 1000;
const long long ml = kk * kk;
const long long mod = ml * kk + 7;
const long long inf = ml * ml * ml + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long n, x, y, cnt;
vector<long long> c;
bool viv = false;
void make_less() {
bool cont = false;
for (int i = 0; i < 6; i++) {
int j = (i - 1 + 6) % 6;
int u = (i + 1 + 6) % 6;
if (c[j] + c[u] < c[i]) c[i] = min(c[i], c[j] + c[u]), cont = true;
}
if (!cont) return;
cnt++;
make_less();
}
void solve() {
cin >> x >> y;
c.resize(6);
cnt = 0;
for (auto &i : c) cin >> i;
for (int i = 0; i < 100; i++) make_less();
if (viv) cout << cnt << " made " << endl;
long long ans = 0;
if (x >= 0) {
if (y >= 0) {
if (x <= y) {
ans = x * c[0] + (y - x) * c[1];
} else {
ans = y * c[0] + (x - y) * c[5];
}
} else {
ans = (-y) * c[4] + x * c[5];
}
} else {
if (y >= 0) {
ans = y * c[1] + (-x) * c[2];
} else {
if (x <= y) {
ans = (-y) * c[3] + (y - x) * c[2];
} else {
ans = (-x) * c[3] + (x - y) * c[4];
}
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long a[7];
long long T, X, Y, ans;
long long ABS(long long x) { return (x > 0) ? x : -x; }
int main() {
scanf("%lld", &T);
while (T--) {
scanf("%lld%lld", &X, &Y);
for (int i = 1; i <= 6; i++) scanf("%lld", &a[i]);
a[1] = min(a[1], a[6] + a[2]);
a[6] = min(a[6], a[5] + a[1]);
for (int i = 2; i <= 5; i++) {
a[i] = min(a[i], a[i - 1] + a[i + 1]);
}
if (X <= Y && X >= 0) {
X = ABS(X);
Y = ABS(Y);
ans = a[1] * X + a[2] * (Y - X);
} else if (X < 0 && Y >= 0) {
X = ABS(X);
Y = ABS(Y);
ans = a[2] * Y + a[3] * X;
} else if (X < 0 && -X >= -Y && Y < 0) {
X = ABS(X);
Y = ABS(Y);
ans = a[4] * Y + a[3] * (X - Y);
} else if (X <= 0 && Y < 0 && -X < -Y) {
X = ABS(X);
Y = ABS(Y);
ans = a[4] * X + a[5] * (Y - X);
} else if (X > 0 && Y <= 0) {
X = ABS(X);
Y = ABS(Y);
ans = a[6] * X + a[5] * Y;
} else {
X = ABS(X);
Y = ABS(Y);
ans = a[1] * Y + a[6] * (X - Y);
}
printf("%lld\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
long long x, y;
scanf("%lld%lld", &x, &y);
long long ans = 0;
long long a, b, c, d, e, f;
scanf("%lld%lld%lld%lld%lld%lld", &a, &b, &c, &d, &e, &f);
a = min(a, f + b), b = min(b, a + c), c = min(c, b + d);
d = min(d, c + e), e = min(e, d + f), f = min(f, e + a);
if (x * y > 0) {
if (x < 0) {
x = abs(x), y = abs(y);
long long mi = min(x, y);
if (d < c + e) x -= mi, y -= mi, ans += mi * d;
ans += x * c + y * e;
} else {
long long mi = min(x, y);
if (a < b + f) x -= mi, y -= mi, ans += mi * a;
ans += x * f + y * b;
}
} else {
if (x > 0)
ans += x * f;
else
ans += abs(x) * c;
if (y > 0)
ans += y * b;
else
ans += abs(y) * e;
}
printf("%lld\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long get(long long a, long long b, pair<int, int> i, pair<int, int> j,
long long pi, long long pj) {
b -= i.second * abs(a);
if ((j.second < 0 ? -1 : j.second > 0) == (b < 0 ? -1 : b > 0) || b == 0)
return abs(a) * pi + abs(b) * pj;
return -1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
pair<int, int> delta[6] = {{1, 1}, {0, 1}, {-1, 0},
{-1, -1}, {0, -1}, {1, 0}};
int t, c[6], x, y;
cin >> t;
while (t--) {
cin >> x >> y;
for (int i = 0; i < 6; ++i) cin >> c[i];
long long ans = LLONG_MAX;
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 6; ++j) {
long long got1 = -1, got2 = -1, got3 = -1, got4 = -1;
if (delta[i].first == 0 &&
(delta[j].first < 0 ? -1 : delta[j].first > 0) ==
(x < 0 ? -1 : x > 0))
got1 = get(x, y, delta[j], delta[i], c[j], c[i]);
if (delta[j].first == 0 &&
(delta[i].first < 0 ? -1 : delta[i].first > 0) ==
(x < 0 ? -1 : x > 0))
got2 = get(x, y, delta[i], delta[j], c[i], c[j]);
if (delta[i].second == 0 &&
(delta[j].second < 0 ? -1 : delta[j].second > 0) ==
(y < 0 ? -1 : y > 0))
got3 = get(y, x, {delta[j].second, delta[j].first},
{delta[i].second, delta[i].first}, c[j], c[i]);
if (delta[j].second == 0 &&
(delta[i].second < 0 ? -1 : delta[i].second > 0) ==
(y < 0 ? -1 : y > 0))
got4 = get(y, x, {delta[i].second, delta[i].first},
{delta[j].second, delta[j].first}, c[i], c[j]);
if (got1 != -1) ans = min(ans, got1);
if (got2 != -1) ans = min(ans, got2);
if (got3 != -1) ans = min(ans, got3);
if (got4 != -1) ans = min(ans, got4);
}
}
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int T, a, b, c, d, n, m, ans, W[6], S[6];
int dx[6] = {1, 0, -1, -1, 0, 1}, dy[6] = {1, 1, 0, -1, -1, 0};
string s[300];
priority_queue<long long int> qx, qy;
int main() {
ios::sync_with_stdio(false);
cin >> T;
while (T--) {
ans = 0;
cin >> a >> b;
for (int i = 0; i < 6; i++) cin >> W[i];
S[0] = min(W[0], W[5] + W[1]);
S[1] = min(W[1], W[2] + W[0]);
S[2] = min(W[2], W[1] + W[3]);
S[3] = min(W[3], W[2] + W[4]);
S[4] = min(W[4], W[3] + W[5]);
S[5] = min(W[5], W[4] + W[0]);
W[0] = min(S[0], S[5] + S[1]);
W[1] = min(S[1], S[2] + S[0]);
W[2] = min(S[2], S[1] + S[3]);
W[3] = min(S[3], S[2] + S[4]);
W[4] = min(S[4], S[3] + S[5]);
W[5] = min(S[5], S[4] + S[0]);
if (a * b > 0) {
if (a < 0) {
ans = -max(a, b) * W[3];
d = max(a, b);
a -= d;
b -= d;
} else {
ans = min(a, b) * W[0];
d = min(a, b);
a -= d;
b -= d;
}
}
if (a < 0)
ans += -a * W[2];
else
ans += a * W[5];
if (b < 0)
ans += -b * W[4];
else
ans += b * W[1];
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int pow_mod(long long int a, long long int b) {
long long int res = 1;
while (b != 0) {
if (b & 1) {
res = (res * a) % 998244353;
}
a = (a * a) % 998244353;
b /= 2;
}
return res;
}
long long int inv_mod(long long int x) { return pow_mod(x, 998244353 - 2); }
const long long int N = (long long int)3e5;
long long int inv[N];
long long int fac[N];
void init() {
fac[0] = 1;
for (long long int i = 1; i < N; i++) fac[i] = (i * fac[i - 1]) % 998244353;
for (long long int i = 0; i < N; i++) inv[i] = inv_mod(fac[i]);
}
long long int ncr(long long int n, long long int r) {
if (n < r) return 0;
if (n == r || r == 0) return 1LL;
long long int x = fac[n];
long long int y = inv[n - r];
long long int z = inv[r];
return ((x * y) % 998244353 * z) % 998244353;
}
void solve() {
long long int x_cor;
long long int y_cor;
cin >> x_cor >> y_cor;
long long int c[6];
for (long long int i = 0; i < 6; i++) cin >> c[i];
long long int ans = 0;
ans = (long long int)6e18;
if (x_cor == y_cor) {
if (x_cor < 0)
ans = x_cor * c[3];
else
ans = x_cor * c[0];
ans = abs(ans);
} else if (x_cor == 0) {
if (y_cor > 0) {
ans = y_cor * c[1];
} else {
ans = y_cor * c[4];
}
ans = abs(ans);
} else if (y_cor == 0) {
if (x_cor > 0) {
ans = x_cor * c[5];
} else
ans = x_cor * c[2];
ans = abs(ans);
}
long long int dirx[6];
long long int diry[6];
dirx[0] = diry[0] = 1;
dirx[1] = 0;
diry[1] = 1;
dirx[2] = -1;
diry[2] = 0;
dirx[3] = diry[3] = -1;
dirx[4] = 0;
diry[4] = -1;
dirx[5] = 1;
diry[5] = 0;
for (long long int i = 0; i < 6; i++) {
for (long long int j = 0; j < 6; j++) {
if (dirx[i] + dirx[j] == 0 && diry[i] + diry[j] == 0) continue;
if (i == j) continue;
if (dirx[i] * diry[j] - diry[i] * dirx[j] == 0) continue;
long long int b = y_cor * dirx[i] - x_cor * diry[i];
b /= (dirx[i] * diry[j] - diry[i] * dirx[j]);
long long int a = x_cor - b * dirx[j];
if (dirx[i] == 0) continue;
a /= dirx[i];
if (a > 0 && b > 0) ans = min(ans, a * c[i] + b * c[j]);
}
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
t = 1;
cin >> t;
while (t--) solve();
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.