Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- // tiom4eg's precompiler options
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- // IO settings
- #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
- // Quick types
- #define ll long long
- #define ld long double
- #define ull unsigned long long
- #define pii pair <int, int>
- #define pll pair <ll, ll>
- #define vi vector <int>
- #define mi vector <vector <int> >
- // Quick functions
- #define endl "\n"
- #define F first
- #define S second
- #define all(a) a.begin(), a.end()
- #define pb push_back
- #define mp make_pair
- #define fint(n) int n; cin >> n
- #define fstr(s) string s; cin >> s
- #define farr(a, n) int a[n]; for (int pog = 0; pog < n; ++pog) cin >> a[pog]
- #define min(a, b) ((a < b) ? (a) : (b))
- #define max(a, b) ((a > b) ? (a) : (b))
- // Quick fors
- #define FOR1(s, n) for (int i = s; i < n; ++i)
- #define FOR2(s, n) for (int j = s; j < n; ++j)
- #define FOR3(s, n) for (int k = s; k < n; ++k)
- #define RFOR(n, s) for (int l = n; l >= s; --l)
- // Pragmas
- #pragma GCC optimize("Ofast") // let the chaos begin!
- #pragma GCC optimize ("unroll-loops")
- #pragma target("avx,fma")
- #pragma comment(linker, "/stack:200000000")
- #pragma target("tune=native")
- // PBDS
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
- #define ook order_of_key
- #define fbo find_by_order
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- using namespace std;
- using namespace __gnu_pbds;
- mt19937 rng(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
- ll op(ll a, ll b) { return (a + b - 1) / b; }
- int main() {
- fastIO;
- ll a, b, c, d, k; cin >> a >> b >> c >> d >> k;
- if (a >= k) {
- cout << k;
- return 0;
- }
- if (b >= k) {
- cout << a + k;
- return 0;
- }
- if (c >= k + a) {
- cout << a + a + b + k;
- return 0;
- }
- if (d >= k + b) {
- cout << a + b + b + c + k;
- return 0;
- }
- ll step = 5e18;
- if (a > c) {
- ll trav = k - a; // Расстояние от края, на котором должны находиться, чтобы выйти из леса на текущей итерации
- ll stp = op(trav, a - c); // Ищем минимальное количество полных итераций, чтобы оказаться на расстоянии от края, не превосходящем trav
- ll pos = k - stp * (a - c); // Вместо a мы пройдём меньшее расстояние
- step = min(step, stp * (a + b + c + d) + pos); // Вычисляем количество шагов
- //cout << "UP: " << stp * (a + b + c + d) + k - stp * (a - c) << endl;
- }
- if (c > a) { // Аналогично...
- ll trav = k - (c - a);
- ll stp = op(trav, c - a);
- ll pos = k - stp * (c - a) + a; //
- step = min(step, stp * (a + b + c + d) + a + b + pos);
- //cout << "DOWN: " << stp * (a + b + c + d) + a + b + pos << endl;
- }
- if (b > d) {
- ll trav = k - b;
- ll stp = op(trav, b - d);
- ll pos = k - stp * (b - d);
- step = min(step, stp * (a + b + c + d) + a + pos);
- //cout << "RIGHT: " << stp * (a + b + c + d) + a + pos << endl;
- }
- if (d > b) {
- ll trav = k - (d - b);
- ll stp = op(trav, d - b);
- ll pos = k - stp * (d - b) + b;
- step = min(step, stp * (a + b + c + d) + a + b + c + pos);
- //cout << "LEFT: " << stp * (a + b + c + d) + a + b + c + pos << endl;
- }
- cout << step << endl; // Выводим наименьшее из возможных
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement