#include #include #include #include #include #include #include #include #include namespace zawa { using i16 = std::int16_t; using i32 = std::int32_t; using i64 = std::int64_t; using i128 = __int128_t; using u8 = std::uint8_t; using u16 = std::uint16_t; using u32 = std::uint32_t; using u64 = std::uint64_t; using usize = std::size_t; } // namespace zawa #include #include #include namespace zawa { namespace internal { template T MidPoint(T a, T b) { if (a > b) std::swap(a, b); return a + ((b - a) >> 1); } template T Abs(T a, T b) { return (a >= b ? a - b : b - a); } } // namespace zawa::internal template T BinarySearch(T ok, T ng, const Function& f) { static_assert(std::is_integral_v, "T must be integral type"); static_assert(std::is_convertible_v>, "f must be function bool(T)"); while (internal::Abs(ok, ng) > 1) { T mid{ internal::MidPoint(ok, ng) }; (f(mid) ? ok : ng) = mid; } return ok; } template T BinarySearch(T ok, T ng, const Function& f, u32 upperLimit) { static_assert(std::is_signed_v, "T must be signed arithmetic type"); static_assert(std::is_convertible_v>, "f must be function bool(T)"); for (u32 _{} ; _ < upperLimit ; _++) { T mid{ (ok + ng) / (T)2 }; (f(mid) ? ok : ng) = mid; } return ok; } } // namespace zawa // #include "Src/Sequence/CompressedSequence.hpp" // #include "Src/Sequence/RunLengthEncoding.hpp" using namespace zawa; // #include "atcoder/modint" // using mint = atcoder::modint998244353; long long X, N; long long solve() { if (X > 0) { auto f = [&](long long k) -> bool { return X - k * (k + 1) / 2 <= 0LL; }; long long first = BinarySearch((long long)2e9, 0LL, f); if (N <= first) return X - N * (N + 1) / 2; X -= first * (first + 1) / 2; assert(X <= 0LL); long long n = std::max(0LL, N - first); if (n & 1) { return X + first + (n + 1) / 2; } else { return X - n / 2; } } else { auto f = [&](long long k) -> bool { return X + k * (k + 1) / 2 > 0LL; }; long long first = BinarySearch((long long)2e9, 0LL, f); // std::cout << first << std::endl; if (N <= first) return X + N * (N + 1) / 2; X += first * (first + 1) / 2; assert(X > 0LL); long long n = std::max(0LL, N - first); if (n & 1) { return X - first - (n + 1) / 2; } else { return X + n / 2; } } } int main() { std::cin.tie(nullptr); std::cout.tie(nullptr); std::ios::sync_with_stdio(false); std::cin >> X >> N; std::cout << solve() << '\n'; }