#line 2 "/home/nok0/documents/programming/library/template/header.hpp" #include #line 3 "/home/nok0/documents/programming/library/template/def_const.hpp" const int inf = 1000000000; const long long INF = 1000000000000000000ll; #line 4 "/home/nok0/documents/programming/library/template/debug.hpp" namespace viewer { void view(const long long &e) { if(e == INF) std::cerr << "INF"; else if(e == -INF) std::cerr << "-INF"; else std::cerr << e; } void view(const int &e) { if(e == inf) std::cerr << "inf"; else if(e == -inf) std::cerr << "-inf"; else std::cerr << e; } template void view(const T &e) { std::cerr << e; } template void view(const std::pair &p) { std::cerr << "("; view(p.first); std::cerr << ", "; view(p.second); std::cerr << ")"; } template void view(const std::tuple &p) { std::cerr << "("; view(std::get<0>(p)); std::cerr << ", "; view(std::get<1>(p)); std::cerr << ", "; view(std::get<2>(p)); std::cerr << ")"; } template void view(const std::tuple &p) { std::cerr << "("; view(std::get<0>(p)); std::cerr << ", "; view(std::get<1>(p)); std::cerr << ", "; view(std::get<2>(p)); std::cerr << ", "; view(std::get<3>(p)); std::cerr << ")"; } template void view(const std::set &s) { if(s.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(auto &t : s) { view(t); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::unordered_set &s) { if(s.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(auto &t : s) { view(t); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::multiset &s) { if(s.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(auto &t : s) { view(t); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::unordered_multiset &s) { if(s.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(auto &t : s) { view(t); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::vector &v) { if(v.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(const auto &e : v) { view(e); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::array &v) { if(v.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(const auto &e : v) { view(e); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::vector> &vv) { std::cerr << "{\n"; for(const auto &v : vv) { std::cerr << "\t"; view(v); std::cerr << '\n'; } std::cerr << "}"; } template void view(const std::vector> &v) { std::cerr << "{\n"; for(const auto &c : v) { std::cerr << "\t("; view(c.first); std::cerr << ", "; view(c.second); std::cerr << ")\n"; } std::cerr << "}"; } template void view(const std::vector> &v) { if(v.empty()) { std::cerr << "{ }"; return; } std::cerr << '{'; for(const auto &t : v) { std::cerr << "\n\t"; view(t); std::cerr << ","; } std::cerr << "\n}"; } template void view(const std::vector> &v) { if(v.empty()) { std::cerr << "{ }"; return; } std::cerr << '{'; for(const auto &t : v) { std::cerr << "\n\t"; view(t); std::cerr << ","; } std::cerr << "\n}"; } template void view(const std::map &m) { std::cerr << "{\n"; for(const auto &t : m) { std::cerr << "\t["; view(t.first); std::cerr << "] : "; view(t.second); std::cerr << '\n'; } std::cerr << "}"; } template void view(const std::unordered_map &m) { std::cerr << "{\n"; for(const auto &t : m) { std::cerr << "\t["; view(t.first); std::cerr << "] : "; view(t.second); std::cerr << '\n'; } std::cerr << "}"; } } // namespace viewer // when compiling : g++ foo.cpp -DLOCAL #ifdef LOCAL void debug_out() {} template void debug_out(Head H, Tail... T) { viewer::view(H); std::cerr << ", "; debug_out(T...); } #define debug(...) \ do { \ std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \ debug_out(__VA_ARGS__); \ std::cerr << "\b\b]\n"; \ } while(0) #define dump(x) \ do { \ std::cerr << __LINE__ << " " << #x << " : "; \ viewer::view(x); \ std::cerr << '\n'; \ } while(0) #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif #line 3 "/home/nok0/documents/programming/library/template/def_name.hpp" #define pb push_back #define eb emplace_back #define fi first #define se second #define SZ(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define popcnt(x) __builtin_popcountll(x) template using V = std::vector; template using VV = std::vector>; template using pqup = std::priority_queue, std::greater>; using ll = long long; using ld = long double; using int128 = __int128_t; using pii = std::pair; using pll = std::pair; #line 3 "/home/nok0/documents/programming/library/template/fast_io.hpp" struct fast_io { fast_io() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout << std::fixed << std::setprecision(15); } } fast_io_; #line 3 "/home/nok0/documents/programming/library/template/input.hpp" template std::istream &operator>>(std::istream &is, std::pair &p) { is >> p.first >> p.second; return is; } template std::istream &operator>>(std::istream &is, std::vector &v) { for(T &i : v) is >> i; return is; } std::istream &operator>>(std::istream &is, __int128_t &a) { std::string s; is >> s; __int128_t ret = 0; for(int i = 0; i < (int)s.length(); i++) if('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0'; a = ret * (s[0] == '-' ? -1 : 1); return is; } namespace scanner { void scan(int &a) { std::cin >> a; } void scan(long long &a) { std::cin >> a; } void scan(std::string &a) { std::cin >> a; } void scan(char &a) { std::cin >> a; } void scan(char a[]) { std::ignore = std::scanf("%s", a); } void scan(double &a) { std::cin >> a; } void scan(long double &a) { std::cin >> a; } template void scan(std::pair &p) { std::cin >> p; } template void scan(std::vector &a) { std::cin >> a; } void INPUT() {} template void INPUT(Head &head, Tail &...tail) { scan(head); INPUT(tail...); } } // namespace scanner #define VEC(type, name, size) \ std::vector name(size); \ scanner::INPUT(name) #define VVEC(type, name, h, w) \ std::vector> name(h, std::vector(w)); \ scanner::INPUT(name) #define INT(...) \ int __VA_ARGS__; \ scanner::INPUT(__VA_ARGS__) #define LL(...) \ long long __VA_ARGS__; \ scanner::INPUT(__VA_ARGS__) #define STR(...) \ std::string __VA_ARGS__; \ scanner::INPUT(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ scanner::INPUT(__VA_ARGS__) #define DOUBLE(...) \ double __VA_ARGS__; \ scanner::INPUT(__VA_ARGS__) #define LD(...) \ long double __VA_ARGS__; \ scanner::INPUT(__VA_ARGS__) #line 3 "/home/nok0/documents/programming/library/template/math.hpp" template inline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; } template inline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; } template T divup(T x, T y) { return (x + y - 1) / y; } template T POW(T a, long long n) { T ret = 1; while(n) { if(n & 1) ret *= a; a *= a; n >>= 1; } return ret; } long long POW(long long a, long long n, const int mod) { long long ret = 1; a = (a % mod + mod) % mod; while(n) { if(n & 1) (ret *= a) %= mod; (a *= a) %= mod; n >>= 1; } return ret; } 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(T ok, T ng, const F &f, int loop) { for(int i = 0; i < loop; i++) { T mid = (ok + ng) / 2; (f(mid) ? ok : ng) = mid; } return ok; } #line 3 "/home/nok0/documents/programming/library/template/output.hpp" template std::ostream &operator<<(std::ostream &os, const std::pair &p) { os << p.first << " " << p.second; return os; } template std::ostream &operator<<(std::ostream &os, const std::vector &a) { for(int i = 0; i < int(a.size()); ++i) { if(i) os << " "; os << a[i]; } return os; } std::ostream &operator<<(std::ostream &dest, __int128_t &value) { std::ostream::sentry s(dest); if(s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while(tmp != 0); if(value < 0) { --d; *d = '-'; } int len = std::end(buffer) - d; if(dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } template void print(const T a) { std::cout << a << '\n'; } template void print(Head H, Tail... T) { std::cout << H << ' '; print(T...); } template void println(const T a) { std::cout << a << '\n'; } template void println(const std::vector &a) { for(const auto &v : a) std::cout << v << '\n'; } template void println(Head H, Tail... T) { std::cout << H << '\n'; println(T...); } void Yes(const bool b = true) { std::cout << (b ? "Yes\n" : "No\n"); } void No() { std::cout << "No\n"; } void YES(const bool b = true) { std::cout << (b ? "YES\n" : "NO\n"); } void NO() { std::cout << "NO\n"; } #line 2 "/home/nok0/documents/programming/library/template/rep.hpp" #define foa(v, a) for (auto &v : a) #define repname(a, b, c, d, e, ...) e #define rep(...) repname(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__) #define rep0(x) for (int rep_counter = 0; rep_counter < (x); ++rep_counter) #define rep1(i, x) for (int i = 0; i < (x); ++i) #define rep2(i, l, r) for (int i = (l); i < (r); ++i) #define rep3(i, l, r, c) for (int i = (l); i < (r); i += (c)) #define repsname(a, b, c, ...) c #define reps(...) repsname(__VA_ARGS__, reps1, reps0)(__VA_ARGS__) #define reps0(x) for (int reps_counter = 1; reps_counter <= (x); ++reps_counter) #define reps1(i, x) for (int i = 1; i <= (x); ++i) #define rrepname(a, b, c, ...) c #define rrep(...) rrepname(__VA_ARGS__, rrep1, rrep0)(__VA_ARGS__) #define rrep0(x) for (int rrep_counter = (x)-1; rrep_counter >= 0; --rrep_counter) #define rrep1(i, x) for (int i = (x)-1; i >= 0; --i) #line 3 "/home/nok0/documents/programming/library/template/string_converter.hpp" struct string_converter { char start = 0; char type(const char &c) const { return (islower(c) ? 'a' : isupper(c) ? 'A' : isdigit(c) ? '0' : 0); } int convert(const char &c) { if(!start) start = type(c); return c - start; } int convert(const char &c, const std::string &chars) { return chars.find(c); } template auto convert(const T &v) { std::vector ret; ret.reserve(size(v)); for(auto &&e : v) ret.emplace_back(convert(e)); return ret; } template auto convert(const T &v, const std::string &chars) { std::vector ret; ret.reserve(size(v)); for(auto &&e : v) ret.emplace_back(convert(e, chars)); return ret; } int operator()(const char &v, char s = 0) { start = s; return convert(v); } int operator()(const char &v, const std::string &chars) { return convert(v, chars); } template auto operator()(const T &v, char s = 0) { start = s; return convert(v); } template auto operator()(const T &v, const std::string &chars) { return convert(v, chars); } } toint; #line 3 "/home/nok0/documents/programming/library/template/vector.hpp" template int lb(const std::vector &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); } template int ub(const std::vector &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); } template void UNIQUE(std::vector &a) { std::sort(a.begin(), a.end()); a.erase(std::unique(a.begin(), a.end()), a.end()); } template std::vector press(std::vector &a) { auto res = a; UNIQUE(res); for(auto &v : a) v = lb(res, v); return res; } #define SORTname(a, b, c, ...) c #define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__) #define SORT0(a) std::sort((a).begin(), (a).end()) #define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; }) template void ADD(std::vector &a, const T x = 1) { for(auto &v : a) v += x; } template void SUB(std::vector &a, const T x = 1) { for(auto &v : a) v -= x; } template struct cum_vector { public: cum_vector() = default; template cum_vector(const std::vector &vec) : cum((int)vec.size() + 1) { for(int i = 0; i < (int)vec.size(); i++) cum[i + 1] = cum[i] + vec[i]; } T prod(int l, int r) { return cum[r] - cum[l]; } private: std::vector cum; }; std::vector> rle(const std::string &s) { const int n = s.size(); std::vector> ret; ret.reserve(n); for(int l = 0; l < n;) { int r = l + 1; for(; r < n and s[l] == s[r]; r++) {} ret.emplace_back(s[l], r - l); l = r; } return ret; } template std::vector> rle(const std::vector &v) { const int n = v.size(); std::vector> ret; ret.reserve(n); for(int l = 0; l < n;) { int r = l + 1; for(; r < n and v[l] == v[r]; r++) {} ret.emplace_back(v[l], r - l); l = r; } return ret; } std::vector iota(int n) { std::vector p(n); std::iota(p.begin(), p.end(), 0); return p; } #line 12 "/home/nok0/documents/programming/library/template/all" using namespace std; #line 2 "a.cpp" void main_(); int main() { int t = 1; while(t--) main_(); } void main_() { LL(n, b, c, x); ll day = b + c; VEC(ll, a, n - 1); /* ボタンは起きた瞬間 or 有効になってかつ起きてるときに即座に押すとしてよい 有効になってるのに寝てる時が基本的にロスとなる ロスが生じるときに、ロスを減らす分だけアイテムを使えば良さそう アイテムが余ったら?よくわからん お気持ち greedy <- だめでした dp をします dp 何を持つ?時短アイテムは key にもてないので、これを value にするのが筋が良い 多分終端の値(mod day が大事)なので、それを key dp[i:ボタンを押した][j:今の時刻] = 時短アイテムを何個使えばそれになるか 初期状態 dp[0][0] = 0 遷移 now := dp[i][j] j + a_i 起きてるとき chmin(dp[i+1][j+a_i],now) j + a_i 寝てるとき chmin(dp[i+1][j+a_i + day - (j+a_i)%day],now) ll t = (j+a_i)%day-b if now + t <= x chmin(dp[i+1][j+a_i-t],now + t) */ vector> dp(n + 1); dp[0][0] = 0; auto is_safe = [&](ll ti) { return ti % day <= b; }; auto nxt_good = [&](ll ti) { if(is_safe(ti)) return ti; else return ti + day - ti % day; }; // 残りのアイテムを全部使う場合も考えたほうが良い cum_vector cum(a); ll ans = INF; rep(i, n - 1) { for(auto &[j, now] : dp[i]) { ll nxt = j + a[i]; if(is_safe(nxt)) chmin(dp[i + 1][nxt], now); else { chmin(dp[i + 1][nxt + day - (nxt) % day], now); ll t = (nxt) % day - b; if(now + t <= x) chmin(dp[i + 1][nxt - t], now + t); } ll u = max(j + cum.prod(i, n - 1) - (x - now), j); debug(i, j, now, u); chmin(ans, nxt_good(u)); } } print(ans); }