#pragma region Macros #ifdef noimi #include "my_template.hpp" #else #pragma GCC optimize("O3") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef noimi #define oj_local(a, b) b #else #define oj_local(a, b) a #endif #define LOCAL if(oj_local(0, 1)) #define OJ if(oj_local(1, 0)) using namespace std; using ll = long long; using ull = unsigned long long int; using i128 = __int128_t; using pii = pair; using pll = pair; using ld = long double; template using vc = vector; template using vvc = vector>; template using vvvc = vector>; using vi = vc; using vl = vc; using vpi = vc; using vpl = vc; template using pq = priority_queue; template using pqg = priority_queue, greater>; template int si(const T &x) { return x.size(); } template inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } vi iota(int n) { vi a(n); return iota(a.begin(), a.end(), 0), a; } template vi iota(const vector &a, bool greater = false) { vi res(a.size()); iota(res.begin(), res.end(), 0); sort(res.begin(), res.end(), [&](int i, int j) { if(greater) return a[i] > a[j]; return a[i] < a[j]; }); return res; } // macros #define overload5(a, b, c, d, e, name, ...) name #define overload4(a, b, c, d, name, ...) name #define endl '\n' #define REP0(n) for(ll jidlsjf = 0; jidlsjf < n; ++jidlsjf) #define REP1(i, n) for(ll i = 0; i < (n); ++i) #define REP2(i, a, b) for(ll i = (a); i < (b); ++i) #define REP3(i, a, b, c) for(ll i = (a); i < (b); i += (c)) #define rep(...) overload4(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__) #define per0(n) for(int jidlsjf = 0; jidlsjf < (n); ++jidlsjf) #define per1(i, n) for(ll i = (n)-1; i >= 0; --i) #define per2(i, a, b) for(ll i = (a)-1; i >= b; --i) #define per3(i, a, b, c) for(ll i = (a)-1; i >= (b); i -= (c)) #define per(...) overload4(__VA_ARGS__, per3, per2, per1, per0)(__VA_ARGS__) #define fore0(a) rep(a.size()) #define fore1(i, a) for(auto &&i : a) #define fore2(a, b, v) for(auto &&[a, b] : v) #define fore3(a, b, c, v) for(auto &&[a, b, c] : v) #define fore4(a, b, c, d, v) for(auto &&[a, b, c, d] : v) #define fore(...) overload5(__VA_ARGS__, fore4, fore3, fore2, fore1, fore0)(__VA_ARGS__) #define perm(v) for(bool flag = true; (flag ? exchange(flag, false) : next_permutation(all(v)));) #define fi first #define se second #define pb push_back #define ppb pop_back #define ppf pop_front #define eb emplace_back #define drop(s) cout << #s << endl, exit(0) #define si(c) (int)(c).size() #define lb(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define rng(v, l, r) v.begin() + (l), v.begin() + (r) #define all(c) begin(c), end(c) #define rall(c) rbegin(c), rend(c) #define SORT(v) sort(all(v)) #define REV(v) reverse(all(v)) #define UNIQUE(x) SORT(x), x.erase(unique(all(x)), x.end()) template T SUM(const S &v) { return accumulate(all(v), T(0)); } #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define overload2(_1, _2, name, ...) name #define vec(type, name, ...) vector name(__VA_ARGS__) #define vv(type, name, h, ...) vector> name(h, vector(__VA_ARGS__)) #define vvv(type, name, h, w, ...) vector>> name(h, vector>(w, vector(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector>>> name(a, vector>>(b, vector>(c, vector(__VA_ARGS__)))) constexpr pii dx4[4] = {pii{1, 0}, pii{0, 1}, pii{-1, 0}, pii{0, -1}}; constexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; namespace yesno_impl { const string YESNO[2] = {"NO", "YES"}; const string YesNo[2] = {"No", "Yes"}; const string yesno[2] = {"no", "yes"}; const string firstsecond[2] = {"second", "first"}; const string FirstSecond[2] = {"Second", "First"}; const string possiblestr[2] = {"impossible", "possible"}; const string Possiblestr[2] = {"Impossible", "Possible"}; void YES(bool t = 1) { cout << YESNO[t] << endl; } void NO(bool t = 1) { YES(!t); } void Yes(bool t = 1) { cout << YesNo[t] << endl; } void No(bool t = 1) { Yes(!t); } void yes(bool t = 1) { cout << yesno[t] << endl; } void no(bool t = 1) { yes(!t); } void first(bool t = 1) { cout << firstsecond[t] << endl; } void First(bool t = 1) { cout << FirstSecond[t] << endl; } void possible(bool t = 1) { cout << possiblestr[t] << endl; } void Possible(bool t = 1) { cout << Possiblestr[t] << endl; } }; // namespace yesno_impl using namespace yesno_impl; #define INT(...) \ int __VA_ARGS__; \ IN(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ IN(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ IN(__VA_ARGS__) #define CHR(...) \ char __VA_ARGS__; \ IN(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ IN(__VA_ARGS__) #define VEC(type, name, size) \ vector name(size); \ IN(name) #define VEC2(type, name1, name2, size) \ vector name1(size), name2(size); \ for(int i = 0; i < size; i++) IN(name1[i], name2[i]) #define VEC3(type, name1, name2, name3, size) \ vector name1(size), name2(size), name3(size); \ for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i]) #define VEC4(type, name1, name2, name3, name4, size) \ vector name1(size), name2(size), name3(size), name4(size); \ for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i], name4[i]); #define VV(type, name, h, w) \ vector> name(h, vector(w)); \ IN(name) int scan() { return getchar(); } void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(string &a) { cin >> a; } template void scan(pair &p) { scan(p.first), scan(p.second); } template void scan(vector &); template void scan(vector &a) { for(auto &i : a) scan(i); } template void scan(T &a) { cin >> a; } void IN() {} template void IN(Head &head, Tail &...tail) { scan(head); IN(tail...); } template T ceil(T x, S y) { assert(y); return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y)); } template T floor(T x, S y) { assert(y); return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1))); } template T POW(T x, int n) { T res = 1; for(; n; n >>= 1, x *= x) if(n & 1) res *= x; return res; } template T POW(T x, S n, const ll &mod) { T res = 1; x %= mod; for(; n; n >>= 1, x = x * x % mod) if(n & 1) res = res * x % mod; return res; } vector factor(ll x) { vector ans; for(ll i = 2; i * i <= x; i++) if(x % i == 0) { ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; } template vector divisor(T x) { vector ans; for(T i = 1; i * i <= x; i++) if(x % i == 0) { ans.pb(i); if(i * i != x) ans.pb(x / i); } return ans; } template void zip(vector &x) { vector y = x; UNIQUE(y); for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); } } template void fold_in(vector &v) {} template void fold_in(vector &v, Head &&a, Tail &&...tail) { for(auto e : a) v.emplace_back(e); fold_in(v, tail...); } template void renumber(vector &v) {} template void renumber(vector &v, Head &&a, Tail &&...tail) { for(auto &&e : a) e = lb(v, e); renumber(v, tail...); } template vector zip(vector &head, Args &&...args) { vector v; fold_in(v, head, args...); sort(all(v)), v.erase(unique(all(v)), v.end()); renumber(v, head, args...); return v; } template void rearrange(vector &v, const vector &id) { vector w(v.size()); rep(i, si(id)) w[i] = v[id[i]]; v.swap(w); } template vector RUI(const vector &v) { vector res(v.size() + 1); for(int i = 0; i < v.size(); i++) res[i + 1] = res[i] + v[i]; return res; } template void zeta_subset(vector &f) { int n = f.size(); for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b] += f[b | i]; } template void zeta_superset(vector &f) { int n = f.size(); for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b | i] += f[b]; } template void mobius_subset(vector &f) { int n = f.size(); for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b] -= f[b | i]; } template void mobius_superset(vector &f) { int n = f.size(); for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b | i] -= f[b]; } // 反時計周りに 90 度回転 template void rot(vector> &v) { if(empty(v)) return; int n = v.size(), m = v[0].size(); vector res(m, vector(n)); rep(i, n) rep(j, m) res[m - 1 - j][i] = v[i][j]; v.swap(res); } // x in [l, r) template bool inc(const T &x, const S &l, const S &r) { return l <= x and x < r; } // 便利関数 constexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; } constexpr ll tri(ll n) { return n * (n + 1) / 2; } // l + ... + r constexpr ll tri(ll l, ll r) { return (l + r) * (r - l + 1) / 2; } ll max(int x, ll y) { return max((ll)x, y); } ll max(ll x, int y) { return max(x, (ll)y); } int min(int x, ll y) { return min((ll)x, y); } int min(ll x, int y) { return min(x, (ll)y); } // bit 演算系 ll pow2(int i) { return 1LL << i; } int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); } int lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); } // int allbit(int n) { return (1 << n) - 1; } constexpr ll mask(int n) { return (1LL << n) - 1; } // int popcount(signed t) { return __builtin_popcount(t); } // int popcount(ll t) { return __builtin_popcountll(t); } int popcount(uint64_t t) { return __builtin_popcountll(t); } static inline uint64_t popcount64(uint64_t x) { uint64_t m1 = 0x5555555555555555ll; uint64_t m2 = 0x3333333333333333ll; uint64_t m4 = 0x0F0F0F0F0F0F0F0Fll; uint64_t h01 = 0x0101010101010101ll; x -= (x >> 1) & m1; x = (x & m2) + ((x >> 2) & m2); x = (x + (x >> 4)) & m4; return (x * h01) >> 56; } bool ispow2(int i) { return i && (i & -i) == i; } ll rnd(ll l, ll r) { //[l, r) #ifdef noimi static mt19937_64 gen; #else static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); #endif return uniform_int_distribution(l, r - 1)(gen); } ll rnd(ll n) { return rnd(0, n); } template void random_shuffle(vc &a) { rep(i, si(a)) swap(a[i], a[rnd(0, i + 1)]); } int in() { int x; cin >> x; return x; } ll lin() { unsigned long long x; cin >> x; return x; } template pair operator-(const pair &x) { return pair(-x.first, -x.second); } template pair operator-(const pair &x, const pair &y) { return pair(x.fi - y.fi, x.se - y.se); } template pair operator+(const pair &x, const pair &y) { return pair(x.fi + y.fi, x.se + y.se); } template pair operator&(const pair &l, const pair &r) { return pair(max(l.fi, r.fi), min(l.se, r.se)); } template pair operator+=(pair &l, const pair &r) { return l = l + r; } template pair operator-=(pair &l, const pair &r) { return l = l - r; } template bool intersect(const pair &l, const pair &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); } template vector &operator++(vector &v) { fore(e, v) e++; return v; } template vector operator++(vector &v, int) { auto res = v; fore(e, v) e++; return res; } template vector &operator--(vector &v) { fore(e, v) e--; return v; } template vector operator--(vector &v, int) { auto res = v; fore(e, v) e--; return res; } template vector &operator+=(vector &l, const vector &r) { fore(e, r) l.eb(e); return l; } template struct edge { int from, to; T cost; int id; edge(int to, T cost) : from(-1), to(to), cost(cost) {} edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {} constexpr bool operator<(const edge &rhs) const noexcept { return cost < rhs.cost; } edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } friend ostream operator<<(ostream &os, const edge &e) { return os << e.to; } }; template using Edges = vector>; template Edges read_edges(int m, bool weighted = false) { Edges res; res.reserve(m); for(int i = 0; i < m; i++) { int u, v, c = 0; scan(u), scan(v), u--, v--; if(weighted) scan(c); res.eb(u, v, c, i); } return res; } using Tree = vector>; using Graph = vector>; template using Wgraph = vector>>; Graph getG(int n, int m = -1, bool directed = false, int margin = 1) { Tree res(n); if(m == -1) m = n - 1; while(m--) { int a, b; cin >> a >> b; a -= margin, b -= margin; res[a].emplace_back(b); if(!directed) res[b].emplace_back(a); } return res; } Graph getTreeFromPar(int n, int margin = 1) { Graph res(n); for(int i = 1; i < n; i++) { int a; cin >> a; res[a - margin].emplace_back(i); } return res; } template Wgraph getWg(int n, int m = -1, bool directed = false, int margin = 1) { Wgraph res(n); if(m == -1) m = n - 1; while(m--) { int a, b; T c; scan(a), scan(b), scan(c); a -= margin, b -= margin; res[a].emplace_back(b, c); if(!directed) res[b].emplace_back(a, c); } return res; } void add(Graph &G, int x, int y) { G[x].eb(y), G[y].eb(x); } template void add(Wgraph &G, int x, int y, T c) { G[x].eb(y, c), G[y].eb(x, c); } #define TEST \ INT(testcases); \ while(testcases--) i128 abs(const i128 &x) { return x > 0 ? x : -x; } istream &operator>>(istream &is, i128 &v) { string s; is >> s; v = 0; for(int i = 0; i < (int)s.size(); i++) { if(isdigit(s[i])) { v = v * 10 + s[i] - '0'; } } if(s[0] == '-') { v *= -1; } return is; } ostream &operator<<(ostream &os, const i128 &v) { if(v == 0) { return (os << "0"); } i128 num = v; if(v < 0) { os << '-'; num = -num; } string s; for(; num > 0; num /= 10) { s.push_back((char)(num % 10) + '0'); } reverse(s.begin(), s.end()); return (os << s); } namespace aux { template struct tp { static void output(std::ostream &os, const T &v) { os << std::get(v) << (&os == &cerr ? ", " : " "); tp::output(os, v); } }; template struct tp { static void output(std::ostream &os, const T &v) { os << std::get(v); } }; } // namespace aux template std::ostream &operator<<(std::ostream &os, const std::tuple &t) { if(&os == &cerr) { os << '('; } aux::tp, 0, sizeof...(Ts) - 1>::output(os, t); if(&os == &cerr) { os << ')'; } return os; } template std::ostream &operator<<(std::ostream &os, const priority_queue &_pq) { auto pq = _pq; vector res; while(!empty(pq)) res.emplace_back(pq.top()), pq.pop(); return os << res; } template ostream &operator<<(ostream &os, const pair &p) { if(&os == &cerr) { return os << "(" << p.first << ", " << p.second << ")"; } return os << p.first << " " << p.second; } template std::basic_ostream &operator<<(std::basic_ostream &os, const Container &x) { bool f = true; if(&os == &cerr) os << "["; for(auto &y : x) { if(&os == &cerr) os << (f ? "" : ", ") << y; else os << (f ? "" : " ") << y; f = false; } if(&os == &cerr) os << "]"; return os; } #ifdef noimi #undef endl void debug() { cerr << endl; } void debug(bool) { cerr << endl; } template void debug(bool is_front, Head head, Tail... tail) { if(!is_front) cerr << ", "; cerr << head; debug(false, tail...); } #define dump(args...) \ { \ vector _debug = _split(#args, ','); \ err(true, begin(_debug), args); \ } vector _split(const string &s, char c) { vector v; stringstream ss(s); string x; while(getline(ss, x, c)) { if(empty(v)) v.eb(x); else { bool flag = false; for(auto [c, d] : {pair('(', ')'), pair('[', ']'), pair('{', '}')}) { if(count(all(v.back()), c) != count(all(v.back()), d)) flag = true; } if(flag) v.back() += "," + x; else v.eb(x); } } return move(v); } void err(bool, vector::iterator) { cerr << endl; } template void err(bool is_front, vector::iterator it, T a, Args... args) { if(!is_front) cerr << ", "; cerr << it->substr((*it)[0] == ' ', (*it).size()) << " = " << a, err(false, ++it, args...); } // #define dump(...) cerr << #__VA_ARGS__ << " : ", debug(true, __VA_ARGS__) #else #define dump(...) static_cast(0) #define dbg(...) static_cast(0) #endif void OUT() { cout << endl; } template void OUT(const Head &head, const Tail &...tail) { cout << head; if(sizeof...(tail)) cout << ' '; OUT(tail...); } template static constexpr T inf = numeric_limits::max() / 2; template constexpr pair inf> = {inf, inf}; template struct REC { F f; REC(F &&f_) : f(std::forward(f_)) {} template auto operator()(Args &&...args) const { return f(*this, std::forward(args)...); } }; template vector> runLength(const vector &v) { vector> res; for(auto &e : v) { if(res.empty() or res.back().fi != e) res.eb(e, 1); else res.back().se++; } return res; } vector> runLength(const string &v) { vector> res; for(auto &e : v) { if(res.empty() or res.back().fi != e) res.eb(e, 1); else res.back().se++; } return res; } int toint(const char &c, const char start = 'a') { return c - start; } int toint(const char &c, const string &chars) { return find(all(chars), c) - begin(chars); } int alphabets_to_int(const char &c) { return (islower(c) ? c - 'a' : c - 'A' + 26); } template auto toint(const T &v, const char &start = 'a') { vector ret; ret.reserve(v.size()); for(auto &&e : v) ret.emplace_back(toint(e, start)); return ret; } template auto toint(const T &v, const string &start) { vector ret; ret.reserve(v.size()); for(auto &&e : v) ret.emplace_back(toint(e, start)); return ret; } // a -> 0, A -> 26 template auto alphabets_to_int(const T &s) { vector res; res.reserve(s.size()); for(auto &&e : s) { res.emplace_back(alphabets_to_int(e)); } return res; } template T bin_search(T ok, T ng, const F &f) { while(abs(ok - ng) > 1) { T mid = ok + ng >> 1; (f(mid) ? ok : ng) = mid; } return ok; } template T bin_search_double(T ok, T ng, const F &f, int iter = 80) { while(iter--) { T mid = (ok + ng) / 2; (f(mid) ? ok : ng) = mid; } return ok; } struct Setup_io { Setup_io() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(11); } } setup_io; #endif #pragma endregion namespace std { bool operator<(const complex &a, const complex &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); } } // namespace std namespace Geometry { using Real = double; using Point = complex; const Real EPS = 1e-8, PI = acos(-1); inline bool eq(Real a, Real b) { return fabs(b - a) < EPS; } Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); } istream &operator>>(istream &is, Point &p) { Real a, b; is >> a >> b; p = Point(a, b); return is; } ostream &operator<<(ostream &os, Point &p) { return os << fixed << setprecision(10) << p.real() << " " << p.imag(); } // 点 p を反時計回りに theta 回転 Point rotate(Real theta, const Point &p) { return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag()); } Real radian_to_degree(Real r) { return (r * 180.0 / PI); } Real degree_to_radian(Real d) { return (d * PI / 180.0); } // a-b-c の角度のうち小さい方を返す Real get_angle(const Point &a, const Point &b, const Point &c) { const Point v(b - a), w(c - b); Real alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real()); if(alpha > beta) swap(alpha, beta); Real theta = (beta - alpha); return min(theta, 2 * acos(-1) - theta); } struct Line { Point a, b; Line() = default; Line(Point a, Point b) : a(a), b(b) {} Line(Real A, Real B, Real C) // Ax + By = C { if(eq(A, 0)) a = Point(0, C / B), b = Point(1, C / B); else if(eq(B, 0)) a = Point(C / A, 0), b = Point(C / A, 1); else if(eq(C, 0)) a = Point(), b = Point(B, -A); else a = Point(0, C / B), b = Point(C / A, 0); } friend ostream &operator<<(ostream &os, Line &p) { return os << p.a << " to " << p.b; } friend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; } }; struct Segment : Line { Segment() = default; Segment(Point a, Point b) : Line(a, b) {} }; struct Circle { Point p; Real r; Circle() = default; Circle(Point p, Real r) : p(p), r(r) {} }; using Points = vector; using Polygon = vector; using Segments = vector; using Lines = vector; using Circles = vector; Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); } Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C // 点の回転方向 int ccw(const Point &a, Point b, Point c) { b = b - a, c = c - a; if(cross(b, c) > EPS) return +1; // "COUNTER_CLOCKWISE" if(cross(b, c) < -EPS) return -1; // "CLOCKWISE" if(dot(b, c) < 0) return +2; // "ONLINE_BACK" if(norm(b) < norm(c)) return -2; // "ONLINE_FRONT" return 0; // "ON_SEGMENT" } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A // 平行判定 bool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A // 垂直判定 bool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A // 射影 // 直線 l に p から垂線を引いた交点を求める Point projection(const Line &l, const Point &p) { double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + (l.a - l.b) * t; } Point projection(const Segment &l, const Point &p) { double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + (l.a - l.b) * t; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B // 反射 // 直線 l を対称軸として点 p と線対称にある点を求める Point reflection(const Line &l, const Point &p) { return p + (projection(l, p) - p) * 2.0; } bool intersect(const Line &l, const Point &p) { return abs(ccw(l.a, l.b, p)) != 1; } bool intersect(const Line &l, const Line &m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS; } bool intersect(const Segment &s, const Point &p) { return ccw(s.a, s.b, p) == 0; } bool intersect(const Line &l, const Segment &s) { return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS; } Real distance(const Line &l, const Point &p); bool intersect(const Circle &c, const Line &l) { return distance(l, c.p) <= c.r + EPS; } bool intersect(const Circle &c, const Point &p) { return abs(abs(p - c.p) - c.r) < EPS; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B bool intersect(const Segment &s, const Segment &t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } int intersect(const Circle &c, const Segment &l) { if(norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0; auto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b); if(d1 < c.r + EPS && d2 < c.r + EPS) return 0; if(d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS) return 1; const Point h = projection(l, c.p); if(dot(l.a - h, l.b - h) < 0) return 2; return 0; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp int intersect(Circle c1, Circle c2) { if(c1.r < c2.r) swap(c1, c2); Real d = abs(c1.p - c2.p); if(c1.r + c2.r < d) return 4; if(eq(c1.r + c2.r, d)) return 3; if(c1.r - c2.r < d) return 2; if(eq(c1.r - c2.r, d)) return 1; return 0; } Real distance(const Point &a, const Point &b) { return abs(a - b); } Real distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); } Real distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); } Real distance(const Segment &s, const Point &p) { Point r = projection(s, p); if(intersect(s, r)) return abs(r - p); return min(abs(s.a - p), abs(s.b - p)); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D Real distance(const Segment &a, const Segment &b) { if(intersect(a, b)) return 0; return min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)}); } Real distance(const Line &l, const Segment &s) { if(intersect(l, s)) return 0; return min(distance(l, s.a), distance(l, s.b)); } Point crosspoint(const Line &l, const Line &m) { Real A = cross(l.b - l.a, m.b - m.a); Real B = cross(l.b - l.a, l.b - m.a); if(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a; return m.a + (m.b - m.a) * B / A; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C Point crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D pair crosspoint(const Circle &c, const Line l) { Point pr = projection(l, c.p); Point e = (l.b - l.a) / abs(l.b - l.a); if(eq(distance(l, c.p), c.r)) return {pr, pr}; double base = sqrt(c.r * c.r - norm(pr - c.p)); return {pr - e * base, pr + e * base}; } pair crosspoint(const Circle &c, const Segment &l) { Line aa = Line(l.a, l.b); if(intersect(c, l) == 2) return crosspoint(c, aa); auto ret = crosspoint(c, aa); if(dot(l.a - ret.first, l.b - ret.first) < 0) ret.second = ret.first; else ret.first = ret.second; return ret; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E pair crosspoint(const Circle &c1, const Circle &c2) { Real d = abs(c1.p - c2.p); Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); Real t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real()); Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r); Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r); return {p1, p2}; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F // 点 p を通る円 c の接線 pair tangent(const Circle &c1, const Point &p2) { return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r))); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G // 円 c1, c2 の共通接線 Lines tangent(Circle c1, Circle c2) { Lines ret; if(c1.r < c2.r) swap(c1, c2); Real g = norm(c1.p - c2.p); if(eq(g, 0)) return ret; Point u = (c2.p - c1.p) / sqrt(g); Point v = rotate(PI * 0.5, u); for(int s : {-1, 1}) { Real h = (c1.r + s * c2.r) / sqrt(g); if(eq(1 - h * h, 0)) { ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r); } else if(1 - h * h > 0) { Point uu = u * h, vv = v * sqrt(1 - h * h); ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s); ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s); } } return ret; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B // 凸性判定 bool is_convex(const Polygon &p) { int n = (int)p.size(); for(int i = 0; i < n; i++) { if(ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1) return false; } return true; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A // 凸包 Polygon convex_hull(Polygon &p) { int n = (int)p.size(), k = 0; if(n <= 2) return p; sort(p.begin(), p.end()); vector ch(2 * n); for(int i = 0; i < n; ch[k++] = p[i++]) { while(k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k; } for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) { while(k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k; } ch.resize(k - 1); return ch; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C // 多角形と点の包含判定 enum { OUTTT, ONNN, INNN }; int contains(const Polygon &Q, const Point &p) { bool in = false; for(int i = 0; i < Q.size(); i++) { Point a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p; if(a.imag() > b.imag()) swap(a, b); if(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in; if(cross(a, b) == 0 && dot(a, b) <= 0) return ONNN; } return in ? INNN : OUTTT; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033 // 線分の重複除去 void merge_segments(vector &segs) { auto merge_if_able = [](Segment &s1, const Segment &s2) { if(abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false; if(ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false; if(ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2) return false; s1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b)); return true; }; for(int i = 0; i < segs.size(); i++) { if(segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b); } for(int i = 0; i < segs.size(); i++) { for(int j = i + 1; j < segs.size(); j++) { if(merge_if_able(segs[i], segs[j])) { segs[j--] = segs.back(), segs.pop_back(); } } } } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033 // 線分アレンジメント // 任意の2線分の交点を頂点としたグラフを構築する vector> segment_arrangement(vector &segs, vector &ps) { vector> g; int N = (int)segs.size(); for(int i = 0; i < N; i++) { ps.emplace_back(segs[i].a); ps.emplace_back(segs[i].b); for(int j = i + 1; j < N; j++) { const Point p1 = segs[i].b - segs[i].a; const Point p2 = segs[j].b - segs[j].a; if(cross(p1, p2) == 0) continue; if(intersect(segs[i], segs[j])) { ps.emplace_back(crosspoint(segs[i], segs[j])); } } } sort(begin(ps), end(ps)); ps.erase(unique(begin(ps), end(ps)), end(ps)); int M = (int)ps.size(); g.resize(M); for(int i = 0; i < N; i++) { vector vec; for(int j = 0; j < M; j++) { if(intersect(segs[i], ps[j])) { vec.emplace_back(j); } } for(int j = 1; j < vec.size(); j++) { g[vec[j - 1]].push_back(vec[j]); g[vec[j]].push_back(vec[j - 1]); } } return (g); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C // 凸多角形の切断 // 直線 l.a-l.b で切断しその左側にできる凸多角形を返す Polygon convex_cut(const Polygon &U, Line l) { Polygon ret; for(int i = 0; i < U.size(); i++) { Point now = U[i], nxt = U[(i + 1) % U.size()]; if(ccw(l.a, l.b, now) != -1) ret.push_back(now); if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) { ret.push_back(crosspoint(Line(now, nxt), l)); } } return (ret); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A // 多角形の面積 Real area(const Polygon &p) { Real A = 0; for(int i = 0; i < p.size(); ++i) { A += cross(p[i], p[(i + 1) % p.size()]); } return A * 0.5; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H // 円と多角形の共通部分の面積 Real area(const Polygon &p, const Circle &c) { if(p.size() < 3) return 0.0; function cross_area = [&](const Circle &c, const Point &a, const Point &b) { Point va = c.p - a, vb = c.p - b; Real f = cross(va, vb), ret = 0.0; if(eq(f, 0.0)) return ret; if(max(abs(va), abs(vb)) < c.r + EPS) return f; if(distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va)); auto u = crosspoint(c, Segment(a, b)); vector tot{a, u.first, u.second, b}; for(int i = 0; i + 1 < tot.size(); i++) { ret += cross_area(c, tot[i], tot[i + 1]); } return ret; }; Real A = 0; for(int i = 0; i < p.size(); i++) { A += cross_area(c, p[i], p[(i + 1) % p.size()]); } return A; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B // 凸多角形の直径(最遠頂点対間距離) Real convex_diameter(const Polygon &p) { int N = (int)p.size(); int is = 0, js = 0; for(int i = 1; i < N; i++) { if(p[i].imag() > p[is].imag()) is = i; if(p[i].imag() < p[js].imag()) js = i; } Real maxdis = norm(p[is] - p[js]); int maxi, maxj, i, j; i = maxi = is; j = maxj = js; do { if(cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) { j = (j + 1) % N; } else { i = (i + 1) % N; } if(norm(p[i] - p[j]) > maxdis) { maxdis = norm(p[i] - p[j]); maxi = i; maxj = j; } } while(i != is || j != js); return sqrt(maxdis); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A // 最近点対 Real closest_pair(Points ps) { if(ps.size() <= 1) throw(0); sort(begin(ps), end(ps)); auto compare_y = [&](const Point &a, const Point &b) { return imag(a) < imag(b); }; vector beet(ps.size()); const Real INF = 1e18; function rec = [&](int left, int right) { if(right - left <= 1) return INF; int mid = (left + right) >> 1; auto x = real(ps[mid]); auto ret = min(rec(left, mid), rec(mid, right)); inplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right, compare_y); int ptr = 0; for(int i = left; i < right; i++) { if(abs(real(ps[i]) - x) >= ret) continue; for(int j = 0; j < ptr; j++) { auto luz = ps[i] - beet[ptr - j - 1]; if(imag(luz) >= ret) break; ret = min(ret, abs(luz)); } beet[ptr++] = ps[i]; } return ret; }; return rec(0, (int)ps.size()); } } // namespace Geometry using namespace Geometry; int main() { VEC(ll, a, 3); LL(b); OUT(max(SUM(a) + b, a[2] * 3)); }