problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p03007
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; using namespace std; #define swap(a, b) \ do { \ typeof(a) __tmp = (a); \ (a) = (b); \ (b) = __tmp; \ } while (0) #define repd(i, a, b) for (typeof(b) i = a; i < (b); ++i) #define rep(i, n) repd(i, 0, n) #define dump(x) cerr << #x << " = " << (x) << endl #define debug(x) cerr << "L" << __LINE__ << ": " << #x << " = " << (x) << endl #define YesNo(x) cout << ((x) ? "Yes" : "No") << endl #define YESNO(x) cout << ((x) ? "YES" : "NO") << endl #define absi(x) (((x) >= 0) ? (x) : (-(x))) int main(int argc, const char *argv[]) { ll n; cin >> n; vector<ll> a, b; ll asum = 0; ll bsum = 0; rep(i, n) { ll tmp; cin >> tmp; if (tmp >= 0) { a.push_back(tmp); asum += tmp; } else { b.push_back(tmp); bsum += tmp; } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); vector<pair<ll, ll>> ret(n - 1); if (b.size() > 0) { // 負の数がある cout << asum - bsum << endl; for (ll i = 0; i < a.size() - 1; ++i) { cout << b[0] << " " << a[i] << endl; b[0] -= a[i]; } rep(i, b.size()) { cout << a.back() << " " << b[i] << endl; a.back() -= b[i]; } } else { cout << asum - a[0] * 2 << endl; for (ll i = 1; i < a.size() - 1; ++i) { cout << a[0] << " " << a[i] << endl; a[0] -= a[i]; } cout << a.back() << " " << a[0] << endl; } return 0; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; #define swap(a, b) \ do { \ typeof(a) __tmp = (a); \ (a) = (b); \ (b) = __tmp; \ } while (0) #define repd(i, a, b) for (typeof(b) i = a; i < (b); ++i) #define rep(i, n) repd(i, 0, n) #define dump(x) cerr << #x << " = " << (x) << endl #define debug(x) cerr << "L" << __LINE__ << ": " << #x << " = " << (x) << endl #define YesNo(x) cout << ((x) ? "Yes" : "No") << endl #define YESNO(x) cout << ((x) ? "YES" : "NO") << endl #define absi(x) (((x) >= 0) ? (x) : (-(x))) int main(int argc, const char *argv[]) { ll n; cin >> n; vector<ll> a, b; ll asum = 0; ll bsum = 0; rep(i, n) { ll tmp; cin >> tmp; if (tmp >= 0) { a.push_back(tmp); asum += tmp; } else { b.push_back(tmp); bsum += tmp; } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); vector<pair<ll, ll>> ret(n - 1); if (b.size() > 0) { // 負の数がある if (a.size() == 0) { cout << -bsum + b.back() * 2 << endl; for (ll i = 0; i < b.size() - 1; ++i) { cout << b.back() << " " << b[i] << endl; b.back() -= b[i]; } return 0; } cout << asum - bsum << endl; for (ll i = 0; i < a.size() - 1; ++i) { cout << b[0] << " " << a[i] << endl; b[0] -= a[i]; } rep(i, b.size()) { cout << a.back() << " " << b[i] << endl; a.back() -= b[i]; } } else { cout << asum - a[0] * 2 << endl; for (ll i = 1; i < a.size() - 1; ++i) { cout << a[0] << " " << a[i] << endl; a[0] -= a[i]; } cout << a.back() << " " << a[0] << endl; } return 0; }
insert
44
44
44
54
0
p03007
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) \ for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } const lint MOD = 1e9 + 7, INF = 1e18, MAX_N = 200000; lint N, arr[100000], sum = 0; priority_queue<lint> mns, pls, zero; int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> N; REP(i, N) { cin >> arr[i]; if (arr[i] > 0) pls.push(-arr[i]); else if (arr[i] < 0) mns.push(arr[i]); else zero.push(0); sum += abs(arr[i]); } if (SZ(pls) == N) { lint minv = -pls.top(); pls.pop(); cout << sum - 2 * minv << endl; lint tmp = minv; REP(i, N - 2) { cout << tmp << " "; pls.pop(); cout << -pls.top() << endl; tmp += pls.top(); } cout << minv << " " << tmp << endl; } else if (SZ(mns) == N) { cout << sum + 2 * mns.top() << endl; lint tmp = mns.top(); REP(i, N - 1) { cout << tmp << " "; mns.pop(); cout << mns.top() << endl; tmp -= mns.top(); } } else { if (SZ(pls) == 0) { REP(i, SZ(zero)) pls.push(0); } else { REP(i, SZ(zero)) mns.push(0); } cout << sum << endl; lint ld = -pls.top(); lint tmp = mns.top(); REP(i, SZ(pls) - 1) { cout << tmp << " "; pls.pop(); cout << -pls.top() << endl; tmp -= -pls.top(); } cout << ld << " " << tmp << endl; ld -= tmp; tmp = ld; REP(i, SZ(mns) - 1) { cout << tmp << " "; mns.pop(); cout << mns.top() << endl; tmp -= mns.top(); } } }
#include "bits/stdc++.h" using namespace std; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) \ for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } const lint MOD = 1e9 + 7, INF = 1e18, MAX_N = 200000; lint N, arr[100000], sum = 0; priority_queue<lint> mns, pls, zero; int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> N; REP(i, N) { cin >> arr[i]; if (arr[i] > 0) pls.push(-arr[i]); else if (arr[i] < 0) mns.push(arr[i]); else zero.push(0); sum += abs(arr[i]); } if (SZ(pls) == N) { lint minv = -pls.top(); pls.pop(); cout << sum - 2 * minv << endl; lint tmp = minv; REP(i, N - 2) { cout << tmp << " "; pls.pop(); cout << -pls.top() << endl; tmp += pls.top(); } cout << minv << " " << tmp << endl; } else if (SZ(mns) == N) { cout << sum + 2 * mns.top() << endl; lint tmp = mns.top(); REP(i, N - 1) { cout << tmp << " "; mns.pop(); cout << mns.top() << endl; tmp -= mns.top(); } } else { if (SZ(pls) == 0 && SZ(mns) == 0) { REP(i, SZ(zero) - 1) pls.push(0); mns.push(0); } else if (SZ(pls) == 0) { REP(i, SZ(zero)) pls.push(0); } else { REP(i, SZ(zero)) mns.push(0); } cout << sum << endl; lint ld = -pls.top(); lint tmp = mns.top(); REP(i, SZ(pls) - 1) { cout << tmp << " "; pls.pop(); cout << -pls.top() << endl; tmp -= -pls.top(); } cout << ld << " " << tmp << endl; ld -= tmp; tmp = ld; REP(i, SZ(mns) - 1) { cout << tmp << " "; mns.pop(); cout << mns.top() << endl; tmp -= mns.top(); } } }
replace
76
77
76
81
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // Here's a small hint that helped motivate my solution. typedef long long int ll; int main() { // Suppose we have three terms x, y, and z. After subtracting z from y, we get // y-z, and after subtracting // that from x, we get x-y+z. As you can see, the z is being added now, since // it was subtracted twice. In general, subtracting a number an even number of // times is equivalent to adding that number. Can we take advantage of this // somehow? ll no; cin >> no; ll a; vector<ll> p, n; for (int i = 0; i < no; i++) { cin >> a; if (a > 0) p.push_back(a); if (a < 0) n.push_back(a); } ll b; int flag = 0; if (p.size() == 0) { sort(n.rbegin(), n.rend()); flag = 1; a = n[0]; b = n[1]; ll val = n[0] - n[1]; p.push_back(val); n.erase(n.begin(), n.begin() + 2); } if (n.size() == 0) { if (p.size() == 2) { sort(p.begin(), p.end()); cout << p[1] - p[0] << "\n" << p[1] << " " << p[0] << endl; return 0; } if (p.size() > 2) { sort(p.begin(), p.end()); a = p[0]; b = p[1]; ll val = p[0] - p[1]; n.push_back(val); p.erase(p.begin(), p.begin() + 2); flag = 1; } } // for(int i=0;i<p.size();i++) cout<<p[i]; // cout<<endl; // for(int i=0;i<n.size();i++) cout<<n[i]; // cout<<endl; ll sum = 0; ll s = 0; for (int i = 0; i < p.size(); i++) sum += p[i]; for (int i = 0; i < n.size(); i++) s += n[i]; s *= -1; sum += s; cout << sum << endl; if (flag) cout << a << " " << b << endl; if (p.size() != 0 && n.size() != 0) { ll val = n[0]; for (int i = 0; i < p.size() - 1; i++) { cout << val << " " << p[i] << endl; val = val - p[i]; } cout << p[p.size() - 1] << " " << val << endl; val = p[p.size() - 1] - val; for (int i = 1; i < n.size(); i++) { cout << val << " " << n[i] << endl; val = val - n[i]; } return 0; } }
#include <bits/stdc++.h> using namespace std; // Here's a small hint that helped motivate my solution. typedef long long int ll; int main() { // Suppose we have three terms x, y, and z. After subtracting z from y, we get // y-z, and after subtracting // that from x, we get x-y+z. As you can see, the z is being added now, since // it was subtracted twice. In general, subtracting a number an even number of // times is equivalent to adding that number. Can we take advantage of this // somehow? ll no; cin >> no; ll a; vector<ll> p, n; for (int i = 0; i < no; i++) { cin >> a; if (a >= 0) p.push_back(a); if (a < 0) n.push_back(a); } ll b; int flag = 0; if (p.size() == 0) { sort(n.rbegin(), n.rend()); flag = 1; a = n[0]; b = n[1]; ll val = n[0] - n[1]; p.push_back(val); n.erase(n.begin(), n.begin() + 2); } if (n.size() == 0) { if (p.size() == 2) { sort(p.begin(), p.end()); cout << p[1] - p[0] << "\n" << p[1] << " " << p[0] << endl; return 0; } if (p.size() > 2) { sort(p.begin(), p.end()); a = p[0]; b = p[1]; ll val = p[0] - p[1]; n.push_back(val); p.erase(p.begin(), p.begin() + 2); flag = 1; } } // for(int i=0;i<p.size();i++) cout<<p[i]; // cout<<endl; // for(int i=0;i<n.size();i++) cout<<n[i]; // cout<<endl; ll sum = 0; ll s = 0; for (int i = 0; i < p.size(); i++) sum += p[i]; for (int i = 0; i < n.size(); i++) s += n[i]; s *= -1; sum += s; cout << sum << endl; if (flag) cout << a << " " << b << endl; if (p.size() != 0 && n.size() != 0) { ll val = n[0]; for (int i = 0; i < p.size() - 1; i++) { cout << val << " " << p[i] << endl; val = val - p[i]; } cout << p[p.size() - 1] << " " << val << endl; val = p[p.size() - 1] - val; for (int i = 1; i < n.size(); i++) { cout << val << " " << n[i] << endl; val = val - n[i]; } return 0; } }
replace
16
17
16
17
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int #define ff first #define ss second #define N 100005 int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int a[n]; int sum = 0; vector<int> pos, neg; pos.push_back(0); for (int i = 0; i < n; ++i) { cin >> a[i]; sum += abs(a[i]); if (a[i] >= 0) pos.push_back(a[i]); else neg.push_back(a[i]); } sort(pos.begin(), pos.end()); sort(neg.begin(), neg.end(), greater<int>()); int ans; if (neg.size() > 0) { pos[0] = neg[0]; neg.erase(neg.begin()); if (pos.size() > 1) ans = sum; else ans = sum + 2 * pos[0]; } else { pos.erase(pos.begin()); ans = sum - 2 * pos[0]; } int n1 = pos.size(); cout << ans << "\n"; int in = 0; int last; last = pos[0]; for (int i = 0; i < n1 - 2; ++i) { cout << last << " " << pos[i + 1] << "\n"; last = last - pos[i + 1]; } if (n1 != 1) { cout << pos[n1 - 1] << " " << last << '\n'; last = pos[n - 1] - last; } int n2 = neg.size(); for (int i = 0; i < n2; ++i) { cout << last << " " << neg[i] << "\n"; last = last - neg[i]; } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int #define ff first #define ss second #define N 100005 int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int a[n]; int sum = 0; vector<int> pos, neg; pos.push_back(0); for (int i = 0; i < n; ++i) { cin >> a[i]; sum += abs(a[i]); if (a[i] >= 0) pos.push_back(a[i]); else neg.push_back(a[i]); } sort(pos.begin(), pos.end()); sort(neg.begin(), neg.end(), greater<int>()); int ans; if (neg.size() > 0) { pos[0] = neg[0]; neg.erase(neg.begin()); if (pos.size() > 1) ans = sum; else ans = sum + 2 * pos[0]; } else { pos.erase(pos.begin()); ans = sum - 2 * pos[0]; } int n1 = pos.size(); cout << ans << "\n"; int in = 0; int last; last = pos[0]; for (int i = 0; i < n1 - 2; ++i) { cout << last << " " << pos[i + 1] << "\n"; last = last - pos[i + 1]; } if (n1 != 1) { cout << pos[n1 - 1] << " " << last << '\n'; last = pos[n1 - 1] - last; } int n2 = neg.size(); for (int i = 0; i < n2; ++i) { cout << last << " " << neg[i] << "\n"; last = last - neg[i]; } return 0; }
replace
52
53
52
53
0
p03007
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; typedef pair<long long, long long> P; typedef long long ll; typedef long long lint; #define REP(i, n) for (long long(i) = 0; (i) < (n); ++i) #define FOR(i, a, b) for (long long(i) = (a); (i) < (b); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() const long long MOD = static_cast<long long>(1e9) + 7LL; const long long INF = 1234567891234567890LL; signed main() { ll N; cin >> N; multiset<ll> POS, NEG, ZER; stringstream ss; REP(i, N) { ll x; cin >> x; if (x > 0) POS.insert(x); else if (x < 0) NEG.insert(x); else ZER.insert(x); } if (NEG.size() == 0) { if (ZER.size()) { ll h = *POS.begin(); NEG.insert(-h); POS.erase(POS.begin()); ZER.erase(ZER.begin()); ss << 0 << " " << h << "\n"; } else { ll l = *POS.begin(); ll h = *--POS.end(); NEG.insert(l - h); POS.erase(POS.begin()); POS.erase(--POS.end()); ss << l << " " << h << "\n"; } } else if (POS.size() == 0) { if (ZER.size()) { ll h = *NEG.begin(); POS.insert(-h); NEG.erase(NEG.begin()); ZER.erase(ZER.begin()); ss << 0 << " " << h << "\n"; } else { ll l = *NEG.begin(); ll h = *--NEG.end(); POS.insert(h - l); NEG.erase(NEG.begin()); NEG.erase(--NEG.end()); ss << h << " " << l << "\n"; } } ll pos = *POS.begin(); POS.erase(POS.begin()); ll neg = *NEG.begin(); NEG.erase(NEG.begin()); for (auto &x : NEG) { ss << pos << " " << x << "\n"; pos -= x; } for (auto &x : POS) { ss << neg << " " << x << "\n"; neg -= x; } for (auto &x : ZER) { ss << pos << " " << x << "\n"; } ss << pos << " " << neg << "\n"; cout << pos - neg << endl; cout << ss.str(); cout << flush; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; typedef pair<long long, long long> P; typedef long long ll; typedef long long lint; #define REP(i, n) for (long long(i) = 0; (i) < (n); ++i) #define FOR(i, a, b) for (long long(i) = (a); (i) < (b); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() const long long MOD = static_cast<long long>(1e9) + 7LL; const long long INF = 1234567891234567890LL; signed main() { ll N; cin >> N; multiset<ll> POS, NEG, ZER; stringstream ss; if (N == 2) { ll x, y; cin >> x >> y; if (x < y) swap(x, y); cout << x - y << endl; cout << x << " " << y << endl; return 0; } REP(i, N) { ll x; cin >> x; if (x > 0) POS.insert(x); else if (x < 0) NEG.insert(x); else ZER.insert(x); } if (NEG.size() == 0) { if (ZER.size()) { ll h = *POS.begin(); NEG.insert(-h); POS.erase(POS.begin()); ZER.erase(ZER.begin()); ss << 0 << " " << h << "\n"; } else { ll l = *POS.begin(); ll h = *--POS.end(); NEG.insert(l - h); POS.erase(POS.begin()); POS.erase(--POS.end()); ss << l << " " << h << "\n"; } } else if (POS.size() == 0) { if (ZER.size()) { ll h = *NEG.begin(); POS.insert(-h); NEG.erase(NEG.begin()); ZER.erase(ZER.begin()); ss << 0 << " " << h << "\n"; } else { ll l = *NEG.begin(); ll h = *--NEG.end(); POS.insert(h - l); NEG.erase(NEG.begin()); NEG.erase(--NEG.end()); ss << h << " " << l << "\n"; } } ll pos = *POS.begin(); POS.erase(POS.begin()); ll neg = *NEG.begin(); NEG.erase(NEG.begin()); for (auto &x : NEG) { ss << pos << " " << x << "\n"; pos -= x; } for (auto &x : POS) { ss << neg << " " << x << "\n"; neg -= x; } for (auto &x : ZER) { ss << pos << " " << x << "\n"; } ss << pos << " " << neg << "\n"; cout << pos - neg << endl; cout << ss.str(); cout << flush; return 0; }
insert
32
32
32
41
TLE
p03007
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using i64 = long long; #define rep(i, s, e) for (int(i) = (s); (i) <= (e); (i)++) #define all(x) x.begin(), x.end() struct revran { const i64 b, e; struct iter { i64 i; constexpr iter(const i64 i) noexcept : i(i) {} void operator++() noexcept { --i; } constexpr i64 operator*() const noexcept { return i; } constexpr bool operator!=(const iter x) const noexcept { return i != x.i; } }; constexpr revran(const i64 e) noexcept : b(0), e(e) {} constexpr revran(const i64 b, const i64 e) noexcept : b(b), e(e) {} constexpr iter begin() const noexcept { return iter(e - 1); } constexpr iter end() const noexcept { return iter(b - 1); } }; struct ran { const i64 b, e; struct iter { i64 i; constexpr iter(const i64 i) noexcept : i(i) {} void operator++() noexcept { ++i; } constexpr i64 operator*() const noexcept { return i; } constexpr bool operator!=(const iter x) const noexcept { return i != x.i; } }; constexpr ran(const i64 e) noexcept : b(0), e(e) {} constexpr ran(const i64 b, const i64 e) noexcept : b(b), e(e) {} constexpr iter begin() const noexcept { return iter(b); } constexpr iter end() const noexcept { return iter(e); } constexpr revran rev() const noexcept { return revran(b, e); } }; constexpr ran rin(const i64 e) noexcept { return ran(1, e + 1); } constexpr ran rin(const i64 b, const i64 e) noexcept { return ran(b, e + 1); } int main() { i64 N; cin >> N; vector<i64> A(N); for (int i = 0; i < N; i++) cin >> A[i]; sort(all(A)); i64 sum = A[N - 1] - A[0]; int L = 0; for (int i = 1; i < N - 1 && A[i] <= 0; i++) { sum -= A[i]; L = i; } for (int i = L + 1; i < N - 1; i++) { sum += A[i]; } i64 mi = L + 1; i64 pl = N - mi; cout << sum << endl; vector<pair<i64, i64>> vec; i64 pi = N - 1; i64 mii = 0; i64 sw = 0; while (pl > 1) { if (sw == 0) { i64 LEFT = A[pi]; i64 RIGHT = -(sum - LEFT); vec.push_back({LEFT, RIGHT}); sum = RIGHT; pi--; pl--; swap(pl, mi); sw ^= 1; } else { i64 LEFT = A[mii]; i64 RIGHT = -(sum - LEFT); vec.push_back({LEFT, RIGHT}); sum = RIGHT; mii++; pl--; swap(pl, mi); sw ^= 1; } } for (int j = 0; j < mi; j++) { if (sw == 0) { i64 RIGHT = A[mii]; mii++; i64 LEFT = sum + RIGHT; vec.push_back({LEFT, RIGHT}); sum = LEFT; } else { i64 RIGHT = A[pl]; pl--; i64 LEFT = sum + RIGHT; vec.push_back({LEFT, RIGHT}); sum = LEFT; } } reverse(all(vec)); for (auto p : vec) { cout << p.first << " " << p.second << endl; } }
#include <bits/stdc++.h> using namespace std; using i64 = long long; #define rep(i, s, e) for (int(i) = (s); (i) <= (e); (i)++) #define all(x) x.begin(), x.end() struct revran { const i64 b, e; struct iter { i64 i; constexpr iter(const i64 i) noexcept : i(i) {} void operator++() noexcept { --i; } constexpr i64 operator*() const noexcept { return i; } constexpr bool operator!=(const iter x) const noexcept { return i != x.i; } }; constexpr revran(const i64 e) noexcept : b(0), e(e) {} constexpr revran(const i64 b, const i64 e) noexcept : b(b), e(e) {} constexpr iter begin() const noexcept { return iter(e - 1); } constexpr iter end() const noexcept { return iter(b - 1); } }; struct ran { const i64 b, e; struct iter { i64 i; constexpr iter(const i64 i) noexcept : i(i) {} void operator++() noexcept { ++i; } constexpr i64 operator*() const noexcept { return i; } constexpr bool operator!=(const iter x) const noexcept { return i != x.i; } }; constexpr ran(const i64 e) noexcept : b(0), e(e) {} constexpr ran(const i64 b, const i64 e) noexcept : b(b), e(e) {} constexpr iter begin() const noexcept { return iter(b); } constexpr iter end() const noexcept { return iter(e); } constexpr revran rev() const noexcept { return revran(b, e); } }; constexpr ran rin(const i64 e) noexcept { return ran(1, e + 1); } constexpr ran rin(const i64 b, const i64 e) noexcept { return ran(b, e + 1); } int main() { i64 N; cin >> N; vector<i64> A(N); for (int i = 0; i < N; i++) cin >> A[i]; sort(all(A)); i64 sum = A[N - 1] - A[0]; int L = 0; for (int i = 1; i < N - 1 && A[i] <= 0; i++) { sum -= A[i]; L = i; } for (int i = L + 1; i < N - 1; i++) { sum += A[i]; } i64 mi = L + 1; i64 pl = N - mi; cout << sum << endl; vector<pair<i64, i64>> vec; i64 pi = N - 1; i64 mii = 0; i64 sw = 0; while (pl > 1) { if (sw == 0) { i64 LEFT = A[pi]; i64 RIGHT = -(sum - LEFT); vec.push_back({LEFT, RIGHT}); sum = RIGHT; pi--; pl--; swap(pl, mi); sw ^= 1; } else { i64 LEFT = A[mii]; i64 RIGHT = -(sum - LEFT); vec.push_back({LEFT, RIGHT}); sum = RIGHT; mii++; pl--; swap(pl, mi); sw ^= 1; } } for (int j = 0; j < mi; j++) { if (sw == 0) { i64 RIGHT = A[mii]; mii++; i64 LEFT = sum + RIGHT; vec.push_back({LEFT, RIGHT}); sum = LEFT; } else { i64 RIGHT = A[pi]; pi--; i64 LEFT = sum + RIGHT; vec.push_back({LEFT, RIGHT}); sum = LEFT; } } reverse(all(vec)); for (auto p : vec) { cout << p.first << " " << p.second << endl; } }
replace
94
96
94
96
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll n, count = 0; cin >> n; vector<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); if (n == 2) { cout << a[1] - a[0] << endl; cout << a[1] << " " << a[0] << endl; return 0; } vector<ll> x(n - 1), y(n - 1); queue<ll> plus; queue<ll> minus; for (int i = 0; i < n; i++) { if (a[i] >= 0) plus.push(a[i]); else minus.push(a[i]); } while (plus.size() > 1) { ll P = plus.front(); ll M = minus.front(); plus.pop(); minus.pop(); minus.push(M - P); x[count] = M; y[count] = P; count++; } while (minus.size() > 0) { ll P = plus.front(); ll M = minus.front(); plus.pop(); minus.pop(); plus.push(P - M); x[count] = P; y[count] = M; count++; } ll ans = plus.front(); cout << ans << endl; for (int i = 0; i < n - 1; i++) cout << x[i] << " " << y[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll n, count = 0; cin >> n; vector<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); if (n == 2) { cout << a[1] - a[0] << endl; cout << a[1] << " " << a[0] << endl; return 0; } vector<ll> x(n - 1), y(n - 1); queue<ll> plus; queue<ll> minus; for (int i = 0; i < n; i++) { if (a[i] >= 0) plus.push(a[i]); else minus.push(a[i]); } if (plus.size() == 0) { for (int i = 0; i < n - 2; i++) { ll p = minus.front(); minus.pop(); minus.push(p); } ll minus1 = minus.front(); minus.pop(); ll minus2 = minus.front(); minus.pop(); plus.push(minus2 - minus1); x[count] = minus2; y[count] = minus1; count++; } if (minus.size() == 0) { ll plus1 = plus.front(); plus.pop(); ll plus2 = plus.front(); plus.pop(); minus.push(plus1 - plus2); x[count] = plus1; y[count] = plus2; count++; } while (plus.size() > 1) { ll P = plus.front(); ll M = minus.front(); plus.pop(); minus.pop(); minus.push(M - P); x[count] = M; y[count] = P; count++; } while (minus.size() > 0) { ll P = plus.front(); ll M = minus.front(); plus.pop(); minus.pop(); plus.push(P - M); x[count] = P; y[count] = M; count++; } ll ans = plus.front(); cout << ans << endl; for (int i = 0; i < n - 1; i++) cout << x[i] << " " << y[i] << endl; return 0; }
insert
26
26
26
53
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; ; #define ll long long #define rep(i, n) for (int i = 0; i < n; i++) #define repr(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define INF 1 << 30 #define LINF 1LL << 62 #define all(x) (x).begin(), (x).end() #define mp make_pair #define pb push_back const int MOD = 1000000007; typedef pair<int, int> P; typedef pair<ll, ll> LP; typedef pair<int, P> PP; typedef pair<ll, LP> LPP; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n; cin >> n; vector<ll> a(n); bool pos = true; bool neg = true; ll sum = 0; ll dsum = 0; ll mi = LINF; ll ma = -LINF; vector<ll> ps, ns; rep(i, n) { cin >> a[i]; sum += a[i]; dsum += abs(a[i]); mi = min(mi, a[i]); ma = max(ma, a[i]); if (a[i] < 0) pos = false; if (a[i] > 0) neg = false; if (a[i] >= 0) ps.pb(a[i]); else ns.pb(a[i]); } sort(all(ps)); sort(all(ns)); reverse(all(ns)); if (n == 2) { cout << ma - mi << endl; cout << ma << " " << mi << endl; return 0; } if (pos) { cout << sum - 2 * mi << endl; ll tmp = mi; FOR(i, 1, n - 1) { cout << tmp << " " << ps[i] << endl; tmp -= ps[i]; } cout << ps[n - 1] << " " << tmp << endl; return 0; } if (neg) { cout << -(sum - 2 * ma) << endl; ll tmp = ma; FOR(i, 1, n) { cout << tmp << " " << ns[i] << endl; tmp -= ns[i]; } return 0; } cout << dsum << endl; rep(i, ps.size() - 1) { cout << ns[0] << " " << ps[i] << endl; ; ns[0] -= ps[i]; } rep(i, ns.size()) { cout << ps[ps.size() - 1] << " " << ns[i] << endl; ps[ps.size() - 1] -= ns[i]; } }
#include <bits/stdc++.h> using namespace std; ; #define ll long long #define rep(i, n) for (int i = 0; i < n; i++) #define repr(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define INF 1 << 30 #define LINF 1LL << 62 #define all(x) (x).begin(), (x).end() #define mp make_pair #define pb push_back const int MOD = 1000000007; typedef pair<int, int> P; typedef pair<ll, ll> LP; typedef pair<int, P> PP; typedef pair<ll, LP> LPP; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n; cin >> n; vector<ll> a(n); bool pos = true; bool neg = true; ll sum = 0; ll dsum = 0; ll mi = LINF; ll ma = -LINF; vector<ll> ps, ns; rep(i, n) { cin >> a[i]; sum += a[i]; dsum += abs(a[i]); mi = min(mi, a[i]); ma = max(ma, a[i]); if (a[i] < 0) pos = false; if (a[i] >= 0) neg = false; if (a[i] >= 0) ps.pb(a[i]); else ns.pb(a[i]); } sort(all(ps)); sort(all(ns)); reverse(all(ns)); if (n == 2) { cout << ma - mi << endl; cout << ma << " " << mi << endl; return 0; } if (pos) { cout << sum - 2 * mi << endl; ll tmp = mi; FOR(i, 1, n - 1) { cout << tmp << " " << ps[i] << endl; tmp -= ps[i]; } cout << ps[n - 1] << " " << tmp << endl; return 0; } if (neg) { cout << -(sum - 2 * ma) << endl; ll tmp = ma; FOR(i, 1, n) { cout << tmp << " " << ns[i] << endl; tmp -= ns[i]; } return 0; } cout << dsum << endl; rep(i, ps.size() - 1) { cout << ns[0] << " " << ps[i] << endl; ; ns[0] -= ps[i]; } rep(i, ns.size()) { cout << ps[ps.size() - 1] << " " << ns[i] << endl; ps[ps.size() - 1] -= ns[i]; } }
replace
42
43
42
43
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() #define ll long long #define rep(i, n) for (int i = 0; i < int(n); i++) #define vi vector<int> using namespace std; const int INF = 1001001001; const int MOD = 1e9 + 7; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int main() { cin.tie(0), ios::sync_with_stdio(false); int n; cin >> n; vi a(n); int minus = 0, plus = 0; rep(i, n) { cin >> a[i]; if (0 < a[i]) plus++; if (a[i] < 0) minus++; } sort(all(a)); int cnt = 0; vi x, y; while (1 < minus && a.size() != 1) { cnt++; if (a.back() < 0) minus--; x.push_back(a.back()); y.push_back(a[0]); a.back() -= a[0]; minus--; a.erase(a.begin()); } if (a.size() == 1) { cout << a[0] << endl; rep(i, cnt) cout << x[i] << " " << y[i] << endl; return 0; } int num = a[0]; a.erase(a.begin()); if (minus == 0) { cnt++; x.push_back(num); y.push_back(a[0]); num -= a[0]; a.erase(a.begin()); } while (a.size() != 1) { cnt++; x.push_back(num); y.push_back(a[0]); num -= a[0]; a.erase(a.begin()); } cnt++; x.push_back(a[0]); y.push_back(num); a[0] -= num; cout << a[0] << endl; rep(i, cnt) cout << x[i] << " " << y[i] << endl; // cout << "\n"; return 0; }
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() #define ll long long #define rep(i, n) for (int i = 0; i < int(n); i++) #define vi vector<int> using namespace std; const int INF = 1001001001; const int MOD = 1e9 + 7; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int main() { cin.tie(0), ios::sync_with_stdio(false); int n; cin >> n; vi a(n); int minus = 0, plus = 0; rep(i, n) { cin >> a[i]; if (0 < a[i]) plus++; if (a[i] < 0) minus++; } sort(all(a)); int cnt = 0; vi x, y; while (1 < minus && a.size() != 1) { cnt++; if (a.back() < 0) minus--; x.push_back(a.back()); y.push_back(a[0]); a.back() -= a[0]; minus--; a.erase(a.begin()); } if (a.size() == 1) { cout << a[0] << endl; rep(i, cnt) cout << x[i] << " " << y[i] << endl; return 0; } if (minus == 0 && a.size() == 2) { cout << a[1] - a[0] << endl; cout << a[1] << " " << a[0]; return 0; } int num = a[0]; a.erase(a.begin()); if (minus == 0) { cnt++; x.push_back(num); y.push_back(a[0]); num -= a[0]; a.erase(a.begin()); } while (a.size() != 1) { cnt++; x.push_back(num); y.push_back(a[0]); num -= a[0]; a.erase(a.begin()); } cnt++; x.push_back(a[0]); y.push_back(num); a[0] -= num; cout << a[0] << endl; rep(i, cnt) cout << x[i] << " " << y[i] << endl; // cout << "\n"; return 0; }
insert
57
57
57
62
0
p03007
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <utility> #include <vector> using namespace std; typedef pair<int, int> P; const int MAX_N = 1e5; int n; vector<int> a; vector<P> ans; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int b; scanf("%d", &b); a.emplace_back(b); } sort(a.begin(), a.end()); vector<int>::iterator c = upper_bound(a.begin(), a.end(), 0); if (c == a.begin()) c++; vector<int>::iterator b = prev(c); ans.emplace_back(*c, *b); int d = *c - *b; a.erase(c); a.erase(b); while (!a.empty()) { if (a.back() > 0) { int e = ans.back().first; ans.back().first = ans.back().second; ans.back().second = e; d = -d; } if (a.back() >= d) { ans.emplace_back(a.back(), d); d = a.back() - d; } else { ans.emplace_back(d, a.back()); d = d - a.back(); } a.pop_back(); } printf("%d\n", d); for (int i = 0; i < n - 1; i++) { printf("%d %d\n", ans[i].first, ans[i].second); } return 0; }
#include <algorithm> #include <cstdio> #include <utility> #include <vector> using namespace std; typedef pair<int, int> P; const int MAX_N = 1e5; int n; vector<int> a; vector<P> ans; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int b; scanf("%d", &b); a.emplace_back(b); } sort(a.begin(), a.end()); vector<int>::iterator c = upper_bound(a.begin(), a.end(), 0); if (c == a.begin()) c++; if (c == a.end()) c--; vector<int>::iterator b = prev(c); ans.emplace_back(*c, *b); int d = *c - *b; a.erase(c); a.erase(b); while (!a.empty()) { if (a.back() > 0) { int e = ans.back().first; ans.back().first = ans.back().second; ans.back().second = e; d = -d; } if (a.back() >= d) { ans.emplace_back(a.back(), d); d = a.back() - d; } else { ans.emplace_back(d, a.back()); d = d - a.back(); } a.pop_back(); } printf("%d\n", d); for (int i = 0; i < n - 1; i++) { printf("%d %d\n", ans[i].first, ans[i].second); } return 0; }
insert
24
24
24
26
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define pb push_back #define F first #define S second using namespace std; typedef pair<int, int> P; const int MOD = 1000000007; int INF = 100100100100100; int a[100010]; int neg; int ans = 0; vector<P> res; signed main() { int n; cin >> n; rep(i, n) cin >> a[i]; sort(a, a + n); if (a[0] > 0) { for (int i = 1; i <= n - 2; i++) { res.pb(P(a[0], a[i])); a[0] -= a[i]; } res.pb(P(a[n - 1], a[0])); ans = a[n - 1] - a[0]; } else if (a[n - 1] < 0) { for (int i = n - 2; i >= 0; i++) { res.pb(P(a[n - 1], a[i])); a[n - 1] -= a[i]; } ans = a[n - 1]; } else { int ima = n - 2; while (a[ima] > 0) { res.pb(P(a[0], a[ima])); a[0] -= a[ima]; ima--; } rep(i, ima + 1) { res.pb(P(a[n - 1], a[i])); a[n - 1] -= a[i]; } ans = a[n - 1]; } cout << ans << endl; rep(i, n - 1) { cout << res[i].F << " " << res[i].S << endl; } return 0; }
#include <bits/stdc++.h> #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define pb push_back #define F first #define S second using namespace std; typedef pair<int, int> P; const int MOD = 1000000007; int INF = 100100100100100; int a[100010]; int neg; int ans = 0; vector<P> res; signed main() { int n; cin >> n; rep(i, n) cin >> a[i]; sort(a, a + n); if (a[0] > 0) { for (int i = 1; i <= n - 2; i++) { res.pb(P(a[0], a[i])); a[0] -= a[i]; } res.pb(P(a[n - 1], a[0])); ans = a[n - 1] - a[0]; } else if (a[n - 1] < 0) { for (int i = n - 2; i >= 0; i--) { res.pb(P(a[n - 1], a[i])); a[n - 1] -= a[i]; } ans = a[n - 1]; } else { int ima = n - 2; while (a[ima] > 0) { res.pb(P(a[0], a[ima])); a[0] -= a[ima]; ima--; } rep(i, ima + 1) { res.pb(P(a[n - 1], a[i])); a[n - 1] -= a[i]; } ans = a[n - 1]; } cout << ans << endl; rep(i, n - 1) { cout << res[i].F << " " << res[i].S << endl; } return 0; }
replace
34
35
34
35
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> P; #define rep(i, n) for (int i = 0; i < (n); ++i) #define repi(i, a, b) for (int i = int(a); i < (b); i++) #define repr(i, b, a) for (int i = int(b); i >= (a); i--) #define all(x) x.begin(), x.end() const ll mod = 1e9 + 7; const ll INF = 1e9; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool valid(int lx, int ux, int ly, int uy, int x, int y) { return lx <= x && x < ux && ly <= y && y < uy; } ll power(ll x, ll p) { if (p == 0) return 1; ll res = power(x * x % mod, p / 2); if (p % 2 == 1) res = res * x % mod; return res; } int main() { int n; cin >> n; vector<int> a(n); rep(i, n) cin >> a[i]; sort(all(a)); vector<P> ans; deque<int> b; if (a[0] >= 0) { ans.push_back(P(a[0], a[n - 1])); a[0] -= a[n - 1]; rep(i, n - 1) b.push_back(a[i]); } else if (a[n - 1] <= 0) { ans.push_back(P(a[n - 1], a[0])); a[n - 1] -= a[0]; repi(i, 1, n) b.push_back(a[i]); } else { rep(i, n) b.push_back(a[i]); } int min_ = b.front(); b.pop_front(); int max_ = b.back(); b.pop_back(); for (auto x : b) { if (x > 0) { ans.push_back(P(min_, x)); min_ -= x; } else { ans.push_back(P(max_, x)); max_ -= x; } } ans.push_back(P(max_, min_)); cout << max_ - min_ << endl; for (auto x : ans) { cout << x.first << " " << x.second << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> P; #define rep(i, n) for (int i = 0; i < (n); ++i) #define repi(i, a, b) for (int i = int(a); i < (b); i++) #define repr(i, b, a) for (int i = int(b); i >= (a); i--) #define all(x) x.begin(), x.end() const ll mod = 1e9 + 7; const ll INF = 1e9; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool valid(int lx, int ux, int ly, int uy, int x, int y) { return lx <= x && x < ux && ly <= y && y < uy; } ll power(ll x, ll p) { if (p == 0) return 1; ll res = power(x * x % mod, p / 2); if (p % 2 == 1) res = res * x % mod; return res; } int main() { int n; cin >> n; vector<int> a(n); rep(i, n) cin >> a[i]; sort(all(a)); if (n == 2) { cout << a[1] - a[0] << endl; cout << a[1] << " " << a[0] << endl; return 0; } vector<P> ans; deque<int> b; if (a[0] >= 0) { ans.push_back(P(a[0], a[n - 1])); a[0] -= a[n - 1]; rep(i, n - 1) b.push_back(a[i]); } else if (a[n - 1] <= 0) { ans.push_back(P(a[n - 1], a[0])); a[n - 1] -= a[0]; repi(i, 1, n) b.push_back(a[i]); } else { rep(i, n) b.push_back(a[i]); } int min_ = b.front(); b.pop_front(); int max_ = b.back(); b.pop_back(); for (auto x : b) { if (x > 0) { ans.push_back(P(min_, x)); min_ -= x; } else { ans.push_back(P(max_, x)); max_ -= x; } } ans.push_back(P(max_, min_)); cout << max_ - min_ << endl; for (auto x : ans) { cout << x.first << " " << x.second << endl; } return 0; }
insert
38
38
38
44
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int mod = (int)1e9 + 7; int INF = (int)2e18; signed main() { int N; cin >> N; deque<int> negative, positive; for (int i = 0; i < N; i++) { int temp; cin >> temp; if (temp <= 0) negative.push_back(temp); else positive.push_back(temp); } sort(negative.begin(), negative.end()); sort(positive.begin(), positive.end()); vector<pair<int, int>> ans; if (negative.empty()) { int big = positive.back(); int small = positive.front(); positive.pop_back(); positive.pop_front(); ans.emplace_back(small, big); positive.push_back(small - big); } while (positive.size() != 1) { int big = positive.back(); int small = negative.front(); positive.pop_back(); negative.pop_front(); ans.emplace_back(small, big); negative.push_back(small - big); } while (!negative.empty()) { int big = positive.back(); int small = negative.front(); positive.pop_back(); negative.pop_front(); ans.emplace_back(big, small); positive.push_back(big - small); } cout << positive.front() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i].first << " " << ans[i].second << endl; } }
#include <bits/stdc++.h> using namespace std; #define int long long int mod = (int)1e9 + 7; int INF = (int)2e18; signed main() { int N; cin >> N; deque<int> negative, positive; for (int i = 0; i < N; i++) { int temp; cin >> temp; if (temp <= 0) negative.push_back(temp); else positive.push_back(temp); } sort(negative.begin(), negative.end()); sort(positive.begin(), positive.end()); vector<pair<int, int>> ans; if (negative.empty()) { int big = positive.back(); int small = positive.front(); positive.pop_back(); positive.pop_front(); ans.emplace_back(small, big); negative.push_back(small - big); } else if (positive.empty()) { int big = negative.front(); int small = negative.back(); negative.pop_back(); negative.pop_front(); ans.emplace_back(small, big); positive.push_back(small - big); } while (positive.size() != 1) { int big = positive.back(); int small = negative.front(); positive.pop_back(); negative.pop_front(); ans.emplace_back(small, big); negative.push_back(small - big); } while (!negative.empty()) { int big = positive.back(); int small = negative.front(); positive.pop_back(); negative.pop_front(); ans.emplace_back(big, small); positive.push_back(big - small); } cout << positive.front() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i].first << " " << ans[i].second << endl; } }
insert
27
27
27
34
0
p03007
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int N; cin >> N; vector<long long> A(N); for (int i = 0; i < N; ++i) cin >> A[i]; sort(A.begin(), A.end(), greater<long long>()); vector<long long> sum(A); for (int i = 1; i < N; ++i) sum[i] += sum[i - 1]; long long M = 0; int idx; // 正になる数を探索 for (int i = 0; i < N - 1; ++i) { if (M < 2 * sum[i] - sum[N - 1]) { idx = i; M = 2 * sum[i] - sum[N - 1]; } } cout << M << endl; int count = 0; while (count < N) { if (count < idx) { cout << A[idx + 1] << " " << A[count] << endl; A[idx + 1] -= A[count++]; } else if (count == idx) { cout << A[idx] << " " << A[idx + 1] << endl; A[idx] -= A[idx + 1]; count += 2; } else { cout << A[idx] << " " << A[count] << endl; A[idx] -= A[count++]; } } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int N; cin >> N; vector<long long> A(N); for (int i = 0; i < N; ++i) cin >> A[i]; sort(A.begin(), A.end(), greater<long long>()); vector<long long> sum(A); for (int i = 1; i < N; ++i) sum[i] += sum[i - 1]; long long M = 0; int idx = 0; // 正になる数を探索 for (int i = 0; i < N - 1; ++i) { if (M < 2 * sum[i] - sum[N - 1]) { idx = i; M = 2 * sum[i] - sum[N - 1]; } } cout << M << endl; int count = 0; while (count < N) { if (count < idx) { cout << A[idx + 1] << " " << A[count] << endl; A[idx + 1] -= A[count++]; } else if (count == idx) { cout << A[idx] << " " << A[idx + 1] << endl; A[idx] -= A[idx + 1]; count += 2; } else { cout << A[idx] << " " << A[count] << endl; A[idx] -= A[count++]; } } return 0; }
replace
18
19
18
19
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a; vector<long long> b; vector<pair<long long, long long>> anss; for (int i = 0; i < n; i++) { long long ab; cin >> ab; if (ab >= 0) a.push_back(ab); else b.push_back(ab); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); if (a.size() == 0) { a.push_back(b[b.size() - 1] - b[0]); anss.push_back(make_pair(b[b.size() - 1], b[0])); b.erase(b.begin()); b.erase(b.end() - 1); } if (b.size() == 0) { b.push_back(a[0] - a[a.size() - 1]); anss.push_back(make_pair(a[0], a[a.size() - 1])); a.erase(a.begin()); a.erase(a.end() - 1); } for (int i = 0; i < a.size() - 1; i++) { anss.push_back(make_pair(b[0], a[i])); b[0] -= a[i]; } int maxa = a[a.size() - 1]; int lenb = b.size(); for (int i = 0; i < lenb; i++) { anss.push_back(make_pair(maxa, b[i])); maxa -= b[i]; } cout << maxa << endl; for (auto p : anss) { cout << p.first << " " << p.second << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a; vector<long long> b; vector<pair<long long, long long>> anss; if (n == 2) { long long x, y; cin >> x >> y; cout << max(x, y) - min(x, y) << endl; cout << max(x, y) << " " << min(x, y) << endl; return 0; } for (int i = 0; i < n; i++) { long long ab; cin >> ab; if (ab >= 0) a.push_back(ab); else b.push_back(ab); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); if (a.size() == 0) { a.push_back(b[b.size() - 1] - b[0]); anss.push_back(make_pair(b[b.size() - 1], b[0])); b.erase(b.begin()); b.erase(b.end() - 1); } if (b.size() == 0) { b.push_back(a[0] - a[a.size() - 1]); anss.push_back(make_pair(a[0], a[a.size() - 1])); a.erase(a.begin()); a.erase(a.end() - 1); } for (int i = 0; i < a.size() - 1; i++) { anss.push_back(make_pair(b[0], a[i])); b[0] -= a[i]; } int maxa = a[a.size() - 1]; int lenb = b.size(); for (int i = 0; i < lenb; i++) { anss.push_back(make_pair(maxa, b[i])); maxa -= b[i]; } cout << maxa << endl; for (auto p : anss) { cout << p.first << " " << p.second << endl; } }
insert
10
10
10
18
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> PII; int main() { int N; cin >> N; vector<int> data; int pl = 0; int mn = 0; vector<int> pls, mns; for (int i = 0; i < N; i++) { int a; cin >> a; if (a > 0) pls.push_back(a); if (a < 0) mns.push_back(a); } sort(mns.begin(), mns.end()); sort(pls.begin(), pls.end()); reverse(pls.begin(), pls.end()); if (pls.size() == 0) { int k = mns.back(); mns.pop_back(); pls.push_back(k); } if (mns.size() == 0) { int k = pls.back(); pls.pop_back(); mns.push_back(k); } vector<PII> output; while (pls.size() >= 2) { int k = mns.back(); mns.pop_back(); int j = pls.back(); pls.pop_back(); output.push_back(PII(k, j)); k -= j; mns.push_back(k); } while (mns.size() >= 1) { int k = pls.back(); pls.pop_back(); int j = mns.back(); mns.pop_back(); output.push_back(PII(k, j)); k -= j; pls.push_back(k); } cout << pls[0] << endl; for (auto elem : output) { int a = elem.first; int b = elem.second; cout << a << " " << b << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> PII; int main() { int N; cin >> N; vector<int> data; int pl = 0; int mn = 0; vector<int> pls, mns; for (int i = 0; i < N; i++) { int a; cin >> a; if (a > 0) pls.push_back(a); else mns.push_back(a); } sort(mns.begin(), mns.end()); sort(pls.begin(), pls.end()); reverse(pls.begin(), pls.end()); if (pls.size() == 0) { int k = mns.back(); mns.pop_back(); pls.push_back(k); } if (mns.size() == 0) { int k = pls.back(); pls.pop_back(); mns.push_back(k); } vector<PII> output; while (pls.size() >= 2) { int k = mns.back(); mns.pop_back(); int j = pls.back(); pls.pop_back(); output.push_back(PII(k, j)); k -= j; mns.push_back(k); } while (mns.size() >= 1) { int k = pls.back(); pls.pop_back(); int j = mns.back(); mns.pop_back(); output.push_back(PII(k, j)); k -= j; pls.push_back(k); } cout << pls[0] << endl; for (auto elem : output) { int a = elem.first; int b = elem.second; cout << a << " " << b << endl; } return 0; }
replace
18
19
18
19
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using i64 = int64_t; #define rep(i, x, y) \ for (i64 i = i64(x), i##_max_for_repmacro = i64(y); \ i < i##_max_for_repmacro; ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define print(x) #endif const int inf = 1.01e9; const i64 inf64 = 4.01e18; const double eps = 1e-9; template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T, size_t size> void fill(T (&ary)[size], const T &val) { fill_n((T *)ary, size, val); } void solve() { // const i64 mod = 1'000'000'007; i64 N; cin >> N; vector<i64> A(N); rep(i, 0, N) { cin >> A[i]; } sort(rbegin(A), rend(A)); i64 plus = 1, minus = 1; vector<i64> l = {A[0]}, r = {A[N - 1]}; rep(i, 1, N - 1) { if (A[i] >= 0) { ++plus; l.emplace_back(A[i]); } else { --minus; r.emplace_back(A[i]); } } vector<i64> x, y; function<i64(i64)> dfs = [&](i64 sgn) { if (sgn == 0) { --plus; --minus; i64 a = dfs(1), b = dfs(-1); x.emplace_back(a); y.emplace_back(b); return a - b; } if (sgn == 1) { if (minus > 0) { --minus; i64 a = dfs(1), b = dfs(-1); x.emplace_back(a); y.emplace_back(b); return a - b; } else { i64 res = l.back(); l.pop_back(); return res; } } else { if (plus > 0) { --plus; i64 a = dfs(-1), b = dfs(1); x.emplace_back(a); y.emplace_back(b); return a - b; } else { i64 res = r.back(); r.pop_back(); return res; } } }; i64 M = dfs(0); cout << M << endl; rep(i, 0, N - 1) { cout << x[i] << " " << y[i] << endl; } } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using i64 = int64_t; #define rep(i, x, y) \ for (i64 i = i64(x), i##_max_for_repmacro = i64(y); \ i < i##_max_for_repmacro; ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define print(x) #endif const int inf = 1.01e9; const i64 inf64 = 4.01e18; const double eps = 1e-9; template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T, size_t size> void fill(T (&ary)[size], const T &val) { fill_n((T *)ary, size, val); } void solve() { // const i64 mod = 1'000'000'007; i64 N; cin >> N; vector<i64> A(N); rep(i, 0, N) { cin >> A[i]; } sort(rbegin(A), rend(A)); i64 plus = 1, minus = 1; vector<i64> l = {A[0]}, r = {A[N - 1]}; rep(i, 1, N - 1) { if (A[i] >= 0) { ++plus; l.emplace_back(A[i]); } else { ++minus; r.emplace_back(A[i]); } } vector<i64> x, y; function<i64(i64)> dfs = [&](i64 sgn) { if (sgn == 0) { --plus; --minus; i64 a = dfs(1), b = dfs(-1); x.emplace_back(a); y.emplace_back(b); return a - b; } if (sgn == 1) { if (minus > 0) { --minus; i64 a = dfs(1), b = dfs(-1); x.emplace_back(a); y.emplace_back(b); return a - b; } else { i64 res = l.back(); l.pop_back(); return res; } } else { if (plus > 0) { --plus; i64 a = dfs(-1), b = dfs(1); x.emplace_back(a); y.emplace_back(b); return a - b; } else { i64 res = r.back(); r.pop_back(); return res; } } }; i64 M = dfs(0); cout << M << endl; rep(i, 0, N - 1) { cout << x[i] << " " << y[i] << endl; } } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); return 0; }
replace
73
74
73
74
0
p03007
C++
Runtime Error
#include <algorithm> #include <cstring> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <math.h> #include <queue> #include <string> #include <tuple> #include <vector> using namespace std; #define MOD (long long int)(1e9 + 7) #define ll long long int #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, n) for (int i = 1; i <= (int)(n); i++) #define REP(i, n) for (int i = n - 1; i >= 0; i--) #define REPS(i, n) for (int i = n; i > 0; i--) #define INF (int)(1123456789) #define LINF (long long int)(112345678901234567) #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) #define all(v) v.begin(), v.end() int main(void) { int n; vector<ll> A; ll a, countP, countM; countP = 0; countM = 0; cin >> n; rep(i, n) { cin >> a; A.push_back(a); if (a >= 0) { countP++; } if (a <= 0) { countM++; } } sort(all(A)); reverse(all(A)); ll ans = 0; if (countP == 0) { rep(i, n) { ans += abs(A[i]); } ans -= abs(A[0]) * 2; cout << ans << endl; ll memo = A[0]; rep(i, n) { if (i == 0) continue; cout << memo << " " << A[i] << endl; memo -= A[i]; } return 0; } else if (countM == 0) { sort(all(A)); rep(i, n) { ans += A[i]; } ans -= A[0] * 2; cout << ans << endl; ll memo = A[0]; rep(i, n) { if (i == 0) continue; if (i == n - 1) { cout << A[i] << " " << memo << endl; return 0; } cout << memo << " " << A[i] << endl; memo -= A[i]; } } else { // 降順 rep(i, n) { ans += abs(A[i]); } cout << ans << endl; int idx = 1; while (A[idx] >= 0) { cout << A[n - 1] << " " << A[idx] << endl; A[n - 1] -= A[idx]; idx++; } while (idx <= n - 2) { cout << A[0] << " " << A[idx] << endl; A[0] -= A[idx]; idx++; } cout << A[0] << " " << A[n - 1] << endl; return 0; } return 0; }
#include <algorithm> #include <cstring> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <math.h> #include <queue> #include <string> #include <tuple> #include <vector> using namespace std; #define MOD (long long int)(1e9 + 7) #define ll long long int #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, n) for (int i = 1; i <= (int)(n); i++) #define REP(i, n) for (int i = n - 1; i >= 0; i--) #define REPS(i, n) for (int i = n; i > 0; i--) #define INF (int)(1123456789) #define LINF (long long int)(112345678901234567) #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) #define all(v) v.begin(), v.end() int main(void) { int n; vector<ll> A; ll a, countP, countM; countP = 0; countM = 0; cin >> n; rep(i, n) { cin >> a; A.push_back(a); if (a >= 0) { countP++; } if (a <= 0) { countM++; } } sort(all(A)); reverse(all(A)); ll ans = 0; if (countP == 0) { rep(i, n) { ans += abs(A[i]); } ans -= abs(A[0]) * 2; cout << ans << endl; ll memo = A[0]; rep(i, n) { if (i == 0) continue; cout << memo << " " << A[i] << endl; memo -= A[i]; } return 0; } else if (countM == 0) { sort(all(A)); rep(i, n) { ans += A[i]; } ans -= A[0] * 2; cout << ans << endl; ll memo = A[0]; rep(i, n) { if (i == 0) continue; if (i == n - 1) { cout << A[i] << " " << memo << endl; return 0; } cout << memo << " " << A[i] << endl; memo -= A[i]; } } else { // 降順 rep(i, n) { ans += abs(A[i]); } cout << ans << endl; int idx = 1; while (A[idx] >= 0 && idx <= n - 2) { cout << A[n - 1] << " " << A[idx] << endl; A[n - 1] -= A[idx]; idx++; } while (idx <= n - 2) { cout << A[0] << " " << A[idx] << endl; A[0] -= A[idx]; idx++; } cout << A[0] << " " << A[n - 1] << endl; return 0; } return 0; }
replace
79
80
79
80
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int, int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define ba 47 #define MAXN 100005 // #define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template <class T> void read(T &res) { res = 0; T f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template <class T> void out(T x) { if (x < 0) { x = -x; putchar('-'); } if (x >= 10) { out(x / 10); } putchar('0' + x % 10); } int N, a[100005], cnt[2]; vector<pii> ans; int64 res = 0; void Solve() { read(N); for (int i = 1; i <= N; ++i) { read(a[i]); if (a[i] < 0) cnt[0]++; else cnt[1]++; } if (cnt[0] && cnt[1]) { int s, t; for (int i = 1; i <= N; ++i) res += abs(a[i]); for (int i = 1; i <= N; ++i) { if (a[i] > 0) s = i; if (a[i] < 0) t = i; } for (int i = 1; i <= N; ++i) { if (a[i] >= 0 && i != s) { ans.pb(mp(a[t], a[i])); a[t] -= a[i]; } } for (int i = 1; i <= N; ++i) { if (a[i] < 0) { ans.pb(mp(a[s], a[i])); a[s] -= a[i]; } } } else if (cnt[0]) { int p = 1; for (int i = 2; i <= N; ++i) { if (a[i] > a[p]) p = i; } res += a[p]; for (int i = 1; i <= N; ++i) { if (i != p) res += abs(a[i]); } for (int i = 1; i <= N; ++i) { if (i != p) { ans.pb(mp(a[p], a[i])); a[p] -= a[i]; } } } else { int p = 1, q; for (int i = 2; i <= N; ++i) { if (a[i] < a[p]) p = i; } res -= a[p]; for (int i = 1; i <= N; ++i) { if (i != p) res += a[i]; } if (p == 1) q = 2; else q = 1; for (int i = 1; i <= N; ++i) { if (i != p && i != q) { ans.pb(mp(a[p], a[i])); a[p] -= a[i]; } } ans.pb(mp(a[q], a[p])); } out(res); enter; for (auto t : ans) { out(t.fi); space; out(t.se); enter; } } int main() { #ifdef ivorysi freopen("f1.in", "r", stdin); #endif Solve(); }
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int, int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define ba 47 #define MAXN 100005 // #define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template <class T> void read(T &res) { res = 0; T f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template <class T> void out(T x) { if (x < 0) { x = -x; putchar('-'); } if (x >= 10) { out(x / 10); } putchar('0' + x % 10); } int N, a[100005], cnt[2]; vector<pii> ans; int64 res = 0; void Solve() { read(N); for (int i = 1; i <= N; ++i) { read(a[i]); if (a[i] < 0) cnt[0]++; else cnt[1]++; } if (cnt[0] && cnt[1]) { int s, t; for (int i = 1; i <= N; ++i) res += abs(a[i]); for (int i = 1; i <= N; ++i) { if (a[i] >= 0) s = i; if (a[i] < 0) t = i; } for (int i = 1; i <= N; ++i) { if (a[i] >= 0 && i != s) { ans.pb(mp(a[t], a[i])); a[t] -= a[i]; } } for (int i = 1; i <= N; ++i) { if (a[i] < 0) { ans.pb(mp(a[s], a[i])); a[s] -= a[i]; } } } else if (cnt[0]) { int p = 1; for (int i = 2; i <= N; ++i) { if (a[i] > a[p]) p = i; } res += a[p]; for (int i = 1; i <= N; ++i) { if (i != p) res += abs(a[i]); } for (int i = 1; i <= N; ++i) { if (i != p) { ans.pb(mp(a[p], a[i])); a[p] -= a[i]; } } } else { int p = 1, q; for (int i = 2; i <= N; ++i) { if (a[i] < a[p]) p = i; } res -= a[p]; for (int i = 1; i <= N; ++i) { if (i != p) res += a[i]; } if (p == 1) q = 2; else q = 1; for (int i = 1; i <= N; ++i) { if (i != p && i != q) { ans.pb(mp(a[p], a[i])); a[p] -= a[i]; } } ans.pb(mp(a[q], a[p])); } out(res); enter; for (auto t : ans) { out(t.fi); space; out(t.se); enter; } } int main() { #ifdef ivorysi freopen("f1.in", "r", stdin); #endif Solve(); }
replace
59
60
59
60
0
p03007
C++
Runtime Error
// #includes {{{ #ifdef MY_DEBUG #include "header/header.hpp" #else #include <bits/stdc++.h> #endif using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define RREP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define LET(x, a) __typeof(a) x(a) // #define IFOR(i,it,c) // for(__typeof((c).begin())it=(c).begin();it!=(c).end();++it,++i) #define ALL(c) (c).begin(), (c).end() #define MP make_pair #define EXIST(e, s) ((s).find(e) != (s).end()) #define RESET(a) memset((a), 0, sizeof(a)) #define SET(a) memset((a), -1, sizeof(a)) #define PB push_back #define DEC(it, command) __typeof(command) it = command // debug #define whole(f, x, ...) \ ([&](decltype((x)) whole) { \ return (f)(begin(whole), end(whole), ##__VA_ARGS__); \ })(x) typedef long long Int; typedef unsigned long long uInt; typedef long double rn; template <class T> T inf() { return numeric_limits<T>::has_infinity ? numeric_limits<T>::infinity() : (numeric_limits<T>::max() / 2); } typedef pair<int, int> pii; #ifdef MY_DEBUG #include "debug.hpp" #include "print.hpp" #endif // }}} Int N; deque<Int> A; void solve() { sort(ALL(A)); reverse(ALL(A)); bool all_minus = true, all_plus = true; REP(i, N) { if (A[i] >= 0) all_minus = false; if (A[i] < 0) all_plus = false; } if (all_minus) { Int sum = accumulate(ALL(A), 0ll); cout << A[0] * 2 - sum << endl; Int result = A[0]; REP(i, N - 1) { cout << result << " " << A[i + 1] << endl; result = result - A[i + 1]; } return; } if (all_plus) { Int sum = accumulate(ALL(A), 0ll); cout << sum - A.back() * 2 << endl; if (A.size() == 2) { cout << A[0] << " " << A[1] << endl; return; } Int result = A.back(); A.pop_back(); // assert(A.size()>=2); for (int i = 0; i < (int)A.size() - 1; i++) { cout << result << " " << A[i] << endl; result = result - A[i]; } cout << A.back() << " " << result << endl; return; } deque<Int> vp, vn; REP(i, N) { if (A[i] > 0) vp.push_back(A[i]); else vn.push_back(A[i]); } cout << accumulate(ALL(vp), 0ll) - accumulate(ALL(vn), 0ll) << endl; Int result; if (vp.size() == 1) { result = vp.front(); vp.pop_front(); } else { result = vn.front(); vn.pop_front(); REP(i, vp.size() - 1) { cout << result << " " << vp[i] << endl; result = result - vp[i]; } cout << vp.back() << " " << result << endl; result = vp.back() - result; } REP(i, vn.size()) { cout << result << " " << vn[i] << endl; result = result - vn[i]; } } //{{{ main fucnction int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); /* N = rand()%1000; A.assign(N,Int()); REP(i,N)A[i] = rand()%2000 - 1000; */ cin >> N; A.assign(N, Int()); for (int i = 0; i < N; i++) { cin >> A[i]; } solve(); return 0; } //}}}
// #includes {{{ #ifdef MY_DEBUG #include "header/header.hpp" #else #include <bits/stdc++.h> #endif using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define RREP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define LET(x, a) __typeof(a) x(a) // #define IFOR(i,it,c) // for(__typeof((c).begin())it=(c).begin();it!=(c).end();++it,++i) #define ALL(c) (c).begin(), (c).end() #define MP make_pair #define EXIST(e, s) ((s).find(e) != (s).end()) #define RESET(a) memset((a), 0, sizeof(a)) #define SET(a) memset((a), -1, sizeof(a)) #define PB push_back #define DEC(it, command) __typeof(command) it = command // debug #define whole(f, x, ...) \ ([&](decltype((x)) whole) { \ return (f)(begin(whole), end(whole), ##__VA_ARGS__); \ })(x) typedef long long Int; typedef unsigned long long uInt; typedef long double rn; template <class T> T inf() { return numeric_limits<T>::has_infinity ? numeric_limits<T>::infinity() : (numeric_limits<T>::max() / 2); } typedef pair<int, int> pii; #ifdef MY_DEBUG #include "debug.hpp" #include "print.hpp" #endif // }}} Int N; deque<Int> A; void solve() { sort(ALL(A)); reverse(ALL(A)); bool all_minus = true, all_plus = true; REP(i, N) { if (A[i] > 0) all_minus = false; if (A[i] < 0) all_plus = false; } if (all_minus) { Int sum = accumulate(ALL(A), 0ll); cout << A[0] * 2 - sum << endl; Int result = A[0]; REP(i, N - 1) { cout << result << " " << A[i + 1] << endl; result = result - A[i + 1]; } return; } if (all_plus) { Int sum = accumulate(ALL(A), 0ll); cout << sum - A.back() * 2 << endl; if (A.size() == 2) { cout << A[0] << " " << A[1] << endl; return; } Int result = A.back(); A.pop_back(); // assert(A.size()>=2); for (int i = 0; i < (int)A.size() - 1; i++) { cout << result << " " << A[i] << endl; result = result - A[i]; } cout << A.back() << " " << result << endl; return; } deque<Int> vp, vn; REP(i, N) { if (A[i] > 0) vp.push_back(A[i]); else vn.push_back(A[i]); } cout << accumulate(ALL(vp), 0ll) - accumulate(ALL(vn), 0ll) << endl; Int result; if (vp.size() == 1) { result = vp.front(); vp.pop_front(); } else { result = vn.front(); vn.pop_front(); REP(i, vp.size() - 1) { cout << result << " " << vp[i] << endl; result = result - vp[i]; } cout << vp.back() << " " << result << endl; result = vp.back() - result; } REP(i, vn.size()) { cout << result << " " << vn[i] << endl; result = result - vn[i]; } } //{{{ main fucnction int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); /* N = rand()%1000; A.assign(N,Int()); REP(i,N)A[i] = rand()%2000 - 1000; */ cin >> N; A.assign(N, Int()); for (int i = 0; i < N; i++) { cin >> A[i]; } solve(); return 0; } //}}}
replace
58
59
58
59
0
p03007
C++
Runtime Error
// includes {{{ #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdlib> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <tuple> #include <vector> // #include<deque> // #include<multiset> // #include<cstring> // #include<bits/stdc++.h> // }}} using namespace std; using ll = long long; // #undef DEBUG // #define DEBUG // DEBUG {{{ #include <array> #include <deque> #include <iostream> #include <list> #include <queue> #include <stack> #include <tuple> #include <valarray> #include <vector> template <int n, class... T> typename std::enable_if<(n >= sizeof...(T))>::type __output_tuple(std::ostream &, std::tuple<T...> const &) {} template <int n, class... T> typename std::enable_if<(n < sizeof...(T))>::type __output_tuple(std::ostream &os, std::tuple<T...> const &t) { os << (n == 0 ? "" : ", ") << std::get<n>(t); __output_tuple<n + 1>(os, t); } template <class... T> std::ostream &operator<<(std::ostream &os, std::tuple<T...> const &t) { os << "("; __output_tuple<0>(os, t); os << ")"; return os; } template <class T, class U> std::ostream &operator<<(std::ostream &os, std::pair<T, U> const &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <class T> std::ostream &operator<<(std::ostream &os, const std::stack<T> &a) { os << "{"; for (auto tmp = a; tmp.size(); tmp.pop()) os << (a.size() == tmp.size() ? "" : ", ") << tmp.top(); os << "}"; return os; } template <class T, class Container, class Compare> std::ostream &operator<<(std::ostream &os, std::priority_queue<T, Container, Compare> a) { os << "{ (top) "; while (a.size()) os << a.top() << (a.size() == 1 ? "" : ", "), a.pop(); os << " }"; return os; } template <class T, class Container> std::ostream &operator<<(std::ostream &os, std::queue<T, Container> a) { os << "{ "; while (a.size()) os << a.front() << (a.size() == 1 ? "" : ", "), a.pop(); os << " }"; return os; } #ifdef DEBUG #if !defined(DEBUG_OUT) // #define DEBUG_OUT std::cerr #endif #define dump(...) \ [&]() { \ auto __debug_tap = std::make_tuple(__VA_ARGS__); \ DEBUG_OUT << "[" << __LINE__ << "] " << #__VA_ARGS__ << " = " \ << __debug_tap << std::endl; \ }() template <class T> inline void dump2D(T &d, size_t sizey, size_t sizex) { for (size_t i = 0; i < sizey; i++) { DEBUG_OUT << "\t"; for (size_t j = 0; j < sizex; j++) DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t"); DEBUG_OUT << std::endl; } } template <class T> inline void dump1D(T &d, size_t sizey) { for (size_t i = 0; i < sizey; i++) { DEBUG_OUT << d[i] << (i + 1 == sizey ? "" : " "); } DEBUG_OUT << std::endl; } template < class T, class = typename std::iterator_traits<decltype(begin(T()))>::value_type, class = typename std::enable_if<!std::is_same<T, std::string>::value>::type> std::ostream &operator<<(std::ostream &os, const T &a) { os << "{"; for (auto ite = begin(a); ite != end(a); ++ite) os << (ite == begin(a) ? "" : ", ") << *ite; os << "}"; return os; } #else #define dump(...) ((void)42) #define dump2D(...) ((void)42) #define dump1D(...) ((void)42) template < class T, class = typename std::iterator_traits<decltype(begin(T()))>::value_type, class = typename std::enable_if<!std::is_same<T, std::string>::value>::type> std::ostream &operator<<(std::ostream &os, const T &a) { for (auto ite = begin(a); ite != end(a); ++ite) os << (ite == begin(a) ? "" : " ") << *ite; return os; } #endif // }}} int main() { std::ios::sync_with_stdio(false), std::cin.tie(0); int n; cin >> n; vector<int> neg, pos; int zero = 0; for (int i = 0; i < n; i++) { int a; cin >> a; if (a < 0) neg.push_back(a); else if (a > 0) pos.push_back(a); else zero++; } if (neg.size() == 0 || pos.size() == 0) if (zero) { if (neg.size()) for (int i = 0; i < zero; i++) neg.push_back(0); else for (int i = 0; i < zero; i++) pos.push_back(0); } vector<pair<int, int>> ans; int M = 0; if (pos.size() == 0) { dump(1); sort(begin(neg), end(neg)); int m = neg.size(); int x = neg.back(); for (int i = 0; i < m - 1; i++) { ans.emplace_back(x, neg[i]); x = x - neg[i]; } M = x; } else if (neg.size() == 0) { dump(2); sort(begin(pos), end(pos)); int m = pos.size(); if (m == 2) { ans.emplace_back(pos[1], pos[0]); M = pos[1] - pos[0]; } else { int x = pos.front(); for (int i = 1; i < m - 1; i++) { ans.emplace_back(x, pos[i]); x -= pos[i]; } ans.emplace_back(pos.back(), x); M = pos.back() - x; } } else { dump(3); int x = neg[0]; for (int i = 1; i < pos.size(); i++) { ans.emplace_back(x, pos[i]); x -= pos[i]; } int y = pos[0]; for (int i = 1; i < neg.size(); i++) { ans.emplace_back(y, neg[i]); y -= neg[i]; } ans.emplace_back(y, x); M = y - x; } dump(pos, neg); dump(ans); assert(ans.size() == n - 1); cout << M << endl; for (int i = 0; i < n - 1; i++) cout << ans[i].first << " " << ans[i].second << "\n"; return 0; }
// includes {{{ #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdlib> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <tuple> #include <vector> // #include<deque> // #include<multiset> // #include<cstring> // #include<bits/stdc++.h> // }}} using namespace std; using ll = long long; // #undef DEBUG // #define DEBUG // DEBUG {{{ #include <array> #include <deque> #include <iostream> #include <list> #include <queue> #include <stack> #include <tuple> #include <valarray> #include <vector> template <int n, class... T> typename std::enable_if<(n >= sizeof...(T))>::type __output_tuple(std::ostream &, std::tuple<T...> const &) {} template <int n, class... T> typename std::enable_if<(n < sizeof...(T))>::type __output_tuple(std::ostream &os, std::tuple<T...> const &t) { os << (n == 0 ? "" : ", ") << std::get<n>(t); __output_tuple<n + 1>(os, t); } template <class... T> std::ostream &operator<<(std::ostream &os, std::tuple<T...> const &t) { os << "("; __output_tuple<0>(os, t); os << ")"; return os; } template <class T, class U> std::ostream &operator<<(std::ostream &os, std::pair<T, U> const &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <class T> std::ostream &operator<<(std::ostream &os, const std::stack<T> &a) { os << "{"; for (auto tmp = a; tmp.size(); tmp.pop()) os << (a.size() == tmp.size() ? "" : ", ") << tmp.top(); os << "}"; return os; } template <class T, class Container, class Compare> std::ostream &operator<<(std::ostream &os, std::priority_queue<T, Container, Compare> a) { os << "{ (top) "; while (a.size()) os << a.top() << (a.size() == 1 ? "" : ", "), a.pop(); os << " }"; return os; } template <class T, class Container> std::ostream &operator<<(std::ostream &os, std::queue<T, Container> a) { os << "{ "; while (a.size()) os << a.front() << (a.size() == 1 ? "" : ", "), a.pop(); os << " }"; return os; } #ifdef DEBUG #if !defined(DEBUG_OUT) // #define DEBUG_OUT std::cerr #endif #define dump(...) \ [&]() { \ auto __debug_tap = std::make_tuple(__VA_ARGS__); \ DEBUG_OUT << "[" << __LINE__ << "] " << #__VA_ARGS__ << " = " \ << __debug_tap << std::endl; \ }() template <class T> inline void dump2D(T &d, size_t sizey, size_t sizex) { for (size_t i = 0; i < sizey; i++) { DEBUG_OUT << "\t"; for (size_t j = 0; j < sizex; j++) DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t"); DEBUG_OUT << std::endl; } } template <class T> inline void dump1D(T &d, size_t sizey) { for (size_t i = 0; i < sizey; i++) { DEBUG_OUT << d[i] << (i + 1 == sizey ? "" : " "); } DEBUG_OUT << std::endl; } template < class T, class = typename std::iterator_traits<decltype(begin(T()))>::value_type, class = typename std::enable_if<!std::is_same<T, std::string>::value>::type> std::ostream &operator<<(std::ostream &os, const T &a) { os << "{"; for (auto ite = begin(a); ite != end(a); ++ite) os << (ite == begin(a) ? "" : ", ") << *ite; os << "}"; return os; } #else #define dump(...) ((void)42) #define dump2D(...) ((void)42) #define dump1D(...) ((void)42) template < class T, class = typename std::iterator_traits<decltype(begin(T()))>::value_type, class = typename std::enable_if<!std::is_same<T, std::string>::value>::type> std::ostream &operator<<(std::ostream &os, const T &a) { for (auto ite = begin(a); ite != end(a); ++ite) os << (ite == begin(a) ? "" : " ") << *ite; return os; } #endif // }}} int main() { std::ios::sync_with_stdio(false), std::cin.tie(0); int n; cin >> n; vector<int> neg, pos; int zero = 0; for (int i = 0; i < n; i++) { int a; cin >> a; if (a < 0) neg.push_back(a); else if (a > 0) pos.push_back(a); else zero++; } if (neg.size()) { for (int i = 0; i < zero; i++) neg.push_back(0); } else { for (int i = 0; i < zero; i++) pos.push_back(0); } assert(pos.size() + neg.size() == n); vector<pair<int, int>> ans; int M = 0; if (pos.size() == 0) { dump(1); sort(begin(neg), end(neg)); int m = neg.size(); int x = neg.back(); for (int i = 0; i < m - 1; i++) { ans.emplace_back(x, neg[i]); x = x - neg[i]; } M = x; } else if (neg.size() == 0) { dump(2); sort(begin(pos), end(pos)); int m = pos.size(); if (m == 2) { ans.emplace_back(pos[1], pos[0]); M = pos[1] - pos[0]; } else { int x = pos.front(); for (int i = 1; i < m - 1; i++) { ans.emplace_back(x, pos[i]); x -= pos[i]; } ans.emplace_back(pos.back(), x); M = pos.back() - x; } } else { dump(3); int x = neg[0]; for (int i = 1; i < pos.size(); i++) { ans.emplace_back(x, pos[i]); x -= pos[i]; } int y = pos[0]; for (int i = 1; i < neg.size(); i++) { ans.emplace_back(y, neg[i]); y -= neg[i]; } ans.emplace_back(y, x); M = y - x; } dump(pos, neg); dump(ans); assert(ans.size() == n - 1); cout << M << endl; for (int i = 0; i < n - 1; i++) cout << ans[i].first << " " << ans[i].second << "\n"; return 0; }
replace
148
157
148
156
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } sort(A.begin(), A.end()); int c = lower_bound(A.begin(), A.end(), 0) - A.begin(); // vector<int> vec1(c); vector<int> vec2(N - c); vector<pair<int, int>> B; for (int i = 0; i < c; i++) { vec1[i] = A[i]; } for (int j = c; j < N; j++) { vec2[j - c] = A[j]; } if ((int)vec2.size() >= 2) { for (int i = 0; i <= N - 2 - c; i++) { B.push_back(make_pair(vec1[0], vec2[i])); vec1[0] -= vec2[i]; } } if ((int)vec1.size() >= 2) { for (int j = 1; j <= c - 1; j++) { B.push_back(make_pair(vec2[N - 1 - c], vec1[j])); vec2[N - 1 - c] -= vec1[j]; } } B.push_back(make_pair(vec2[N - 1 - c], vec1[0])); int M = vec2[N - 1 - c] - vec1[0]; cout << M << endl; for (int i = 0; i < (int)B.size(); i++) { cout << B[i].first << " " << B[i].second << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } sort(A.begin(), A.end()); int c = lower_bound(A.begin(), A.end(), 0) - A.begin(); // if (c <= 0) { // all of components are positive c = 1; } else if (c >= N) { // all of components are negative c = N - 1; } vector<int> vec1(c); vector<int> vec2(N - c); vector<pair<int, int>> B; for (int i = 0; i < c; i++) { vec1[i] = A[i]; } for (int j = c; j < N; j++) { vec2[j - c] = A[j]; } if ((int)vec2.size() >= 2) { for (int i = 0; i <= N - 2 - c; i++) { B.push_back(make_pair(vec1[0], vec2[i])); vec1[0] -= vec2[i]; } } if ((int)vec1.size() >= 2) { for (int j = 1; j <= c - 1; j++) { B.push_back(make_pair(vec2[N - 1 - c], vec1[j])); vec2[N - 1 - c] -= vec1[j]; } } B.push_back(make_pair(vec2[N - 1 - c], vec1[0])); int M = vec2[N - 1 - c] - vec1[0]; cout << M << endl; for (int i = 0; i < (int)B.size(); i++) { cout << B[i].first << " " << B[i].second << endl; } return 0; }
insert
14
14
14
23
0
p03007
C++
Runtime Error
#include <bits/stdc++.h> #define f(i, n) for (int i = 0; i < (n); i++) #define inf (int)(3e18) #define int long long #define mod (int)(1000000007) #define intt long long #define P pair<int, int> using namespace std; // Library // モッドパウ int modpow(int x, int y, int m = mod) { int res = 1; while (y) { if (y % 2) { res *= x; res %= m; } x = x * x % mod; y /= 2; } return res; } int mypow(int x, int y) { int res = 1; while (y) { if (y % 2) { res *= x; } x = x * x; y /= 2; } return res; } // is the number (x) a prime number? bool prime(int x) { for (int i = 2; i * i <= x; i++) { if (!(x % i)) { return false; } } return true; } double kyori(pair<int, int> f, pair<int, int> s) { double ans = 0; double t = fabs(f.first - s.first); double y = fabs(f.second - s.second); ans = sqrt(t * t + y * y); return ans; } // saidai-kouyakusuu inline int gcd(int x, int y) { if (!y) { return x; } return gcd(y, x % y); } // Union-Find Tree class Union_Find { vector<int> par; vector<int> rankmy; public: Union_Find(int size) { par = vector<int>(size); rankmy = vector<int>(size); for (int i = 0; i < size; i++) { par[i] = i; } } int find(int x) { if (par[x] == x) { return x; } return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } if (rankmy[x] < rankmy[y]) { par[x] = y; } else { par[y] = x; if (rankmy[x] == rankmy[y]) { rankmy[x]++; } } } bool same(int x, int y) { return find(x) == find(y); } }; // Union-Find-End // SegTree template <class T> class SegTree { int n; // 葉の数 vector<T> data; // データを格納するvector T def; // 初期値かつ単位元 function<T(T, T)> operation; // 区間クエリで使う処理 function<T(T, T)> update; // 点更新で使う処理 // 区間[a,b)の総和。ノードk=[l,r)に着目している。 T _query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return def; // 交差しない if (a <= l && r <= b) return data[k]; // a,l,r,bの順で完全に含まれる else { T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return operation(c1, c2); } } public: // _n:必要サイズ, _def:初期値かつ単位元, _operation:クエリ関数, // _update:更新関数 SegTree(size_t _n, T _def, function<T(T, T)> _operation, function<T(T, T)> _update) : def(_def), operation(_operation), update(_update) { n = 1; while (n < _n) { n *= 2; } data = vector<T>(2 * n - 1, def); } // 場所i(0-indexed)の値をxで更新 void change(int i, T x) { i += n - 1; data[i] = update(data[i], x); while (i > 0) { i = (i - 1) / 2; data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]); } } // [a, b)の区間クエリを実行 T query(int a, int b) { return _query(a, b, 0, 0, n); } // 添字でアクセス T operator[](int i) { return data[i + n - 1]; } }; #define R_MIN ([](long long a, long long b) { return min(a, b); }) #define R_MAX ([](long long a, long long b) { return max(a, b); }) #define R_SUM ([](long long a, long long b) { return a + b; }) #define NORMAL_UPDATE ([](long long a, long long b) { return b; }) #define ADD_UPDATE ([](long long a, long long b) { return a + b; }) #define MINUS_UPDATE ([](long long a, long long b) { return a - b; } // Seg-Tree-End // dfs // vector<int> v[100004]; // bool went[100004]; // void dfs(int x) { // went[x] = true; // for (int i = 0; i < (int)v[x].size(); i++) { // if (!went[v[x][i]]) { // dfs(v[x][i]); // } // } // } // number of keta int keta(int x) { int ans = 0; while (x) { x /= 10; ans++; } return ans; } // sum of keta int ketasum(int x) { int ans = 0; while (x) { ans += x % 10; x /= 10; } return ans; } inline int lcm(int x, int y) { int ans = x / gcd(x, y) * y; return ans; } int twobeki(int x) { int ans = 0; while (1) { if (!(x & 1)) { ans++; x /= 2; } else { break; } } return ans; } int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= i; res %= mod; } return res; } int comb(int x, int y) { if (y > x) return 0; // cout<<kai(x, y)<<' '<<modpow(kai(y, y), mod - 2)<<endl; return kai(x, y) * modpow(kai(y, y), mod - 2) % mod; } int fukugen(vector<int> l) { int ans = 0; for (int i = 0; i < (int)l.size(); i++) { ans *= 10; ans += l[i]; } return ans; } int nanjyo(int n) { int ans = 0; while (n > 1) { ans++; n /= 2; } return ans; } bool intersect(pair<pair<int, int>, pair<int, int>> p1, pair<pair<int, int>, pair<int, int>> p2) { int men11 = (p2.first.first - p1.first.first) * (p2.second.second - p1.first.second) - (p2.second.first - p1.first.first) * (p2.first.second - p1.first.second); int men12 = (p2.first.first - p1.second.first) * (p2.second.second - p1.second.second) - (p2.second.first - p1.second.first) * (p2.first.second - p1.second.second); int men21 = (p1.first.first - p2.first.first) * (p1.second.second - p2.first.second) - (p1.second.first - p2.first.first) * (p1.first.second - p2.first.second); int men22 = (p1.first.first - p2.second.first) * (p1.second.second - p2.second.second) - (p1.second.first - p2.second.first) * (p1.first.second - p2.second.second); return ((signbit(men11)) ^ (signbit(men12))) && ((signbit(men21)) ^ (signbit(men22))); } template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } void Yes() { cout << "Yes" << endl; } void No() { cout << "No" << endl; } void YES() { cout << "YES" << endl; } void NO() { cout << "NO" << endl; } int yakusuu(int x) { int ans = 0; for (int i = 1; i * i <= x; i++) { if (!(x % i)) { if (i * i == x) { ans++; } else ans += 2; } } return ans; } #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i <= n; i++) // Library-End int n, a[100004], ans; vector<int> mi, po; vector<pair<int, int>> v; signed main() { cin >> n; rep(i, n) { scanf("%lld", &a[i]); if (a[i] > 0) { po.push_back(a[i]); } if (a[i] < 0) { mi.push_back(a[i]); } } sort(po.begin(), po.end(), greater<int>()); sort(mi.begin(), mi.end()); if (!mi.size()) { mi.push_back(po[(int)po.size() - 1]); po.pop_back(); } if (!po.size()) { po.push_back(mi[(int)mi.size()] - 1); mi.pop_back(); } rep(i, (int)po.size() - 1) { v.push_back({mi[0], po[i + 1]}); mi[0] -= po[i + 1]; } rep(i, (int)mi.size() - 1) { v.push_back({po[0], mi[i + 1]}); po[0] -= mi[i + 1]; } v.push_back({po[0], mi[0]}); cout << po[0] - mi[0] << endl; rep(i, v.size()) { printf("%lld %lld\n", v[i].first, v[i].second); } }
#include <bits/stdc++.h> #define f(i, n) for (int i = 0; i < (n); i++) #define inf (int)(3e18) #define int long long #define mod (int)(1000000007) #define intt long long #define P pair<int, int> using namespace std; // Library // モッドパウ int modpow(int x, int y, int m = mod) { int res = 1; while (y) { if (y % 2) { res *= x; res %= m; } x = x * x % mod; y /= 2; } return res; } int mypow(int x, int y) { int res = 1; while (y) { if (y % 2) { res *= x; } x = x * x; y /= 2; } return res; } // is the number (x) a prime number? bool prime(int x) { for (int i = 2; i * i <= x; i++) { if (!(x % i)) { return false; } } return true; } double kyori(pair<int, int> f, pair<int, int> s) { double ans = 0; double t = fabs(f.first - s.first); double y = fabs(f.second - s.second); ans = sqrt(t * t + y * y); return ans; } // saidai-kouyakusuu inline int gcd(int x, int y) { if (!y) { return x; } return gcd(y, x % y); } // Union-Find Tree class Union_Find { vector<int> par; vector<int> rankmy; public: Union_Find(int size) { par = vector<int>(size); rankmy = vector<int>(size); for (int i = 0; i < size; i++) { par[i] = i; } } int find(int x) { if (par[x] == x) { return x; } return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } if (rankmy[x] < rankmy[y]) { par[x] = y; } else { par[y] = x; if (rankmy[x] == rankmy[y]) { rankmy[x]++; } } } bool same(int x, int y) { return find(x) == find(y); } }; // Union-Find-End // SegTree template <class T> class SegTree { int n; // 葉の数 vector<T> data; // データを格納するvector T def; // 初期値かつ単位元 function<T(T, T)> operation; // 区間クエリで使う処理 function<T(T, T)> update; // 点更新で使う処理 // 区間[a,b)の総和。ノードk=[l,r)に着目している。 T _query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return def; // 交差しない if (a <= l && r <= b) return data[k]; // a,l,r,bの順で完全に含まれる else { T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return operation(c1, c2); } } public: // _n:必要サイズ, _def:初期値かつ単位元, _operation:クエリ関数, // _update:更新関数 SegTree(size_t _n, T _def, function<T(T, T)> _operation, function<T(T, T)> _update) : def(_def), operation(_operation), update(_update) { n = 1; while (n < _n) { n *= 2; } data = vector<T>(2 * n - 1, def); } // 場所i(0-indexed)の値をxで更新 void change(int i, T x) { i += n - 1; data[i] = update(data[i], x); while (i > 0) { i = (i - 1) / 2; data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]); } } // [a, b)の区間クエリを実行 T query(int a, int b) { return _query(a, b, 0, 0, n); } // 添字でアクセス T operator[](int i) { return data[i + n - 1]; } }; #define R_MIN ([](long long a, long long b) { return min(a, b); }) #define R_MAX ([](long long a, long long b) { return max(a, b); }) #define R_SUM ([](long long a, long long b) { return a + b; }) #define NORMAL_UPDATE ([](long long a, long long b) { return b; }) #define ADD_UPDATE ([](long long a, long long b) { return a + b; }) #define MINUS_UPDATE ([](long long a, long long b) { return a - b; } // Seg-Tree-End // dfs // vector<int> v[100004]; // bool went[100004]; // void dfs(int x) { // went[x] = true; // for (int i = 0; i < (int)v[x].size(); i++) { // if (!went[v[x][i]]) { // dfs(v[x][i]); // } // } // } // number of keta int keta(int x) { int ans = 0; while (x) { x /= 10; ans++; } return ans; } // sum of keta int ketasum(int x) { int ans = 0; while (x) { ans += x % 10; x /= 10; } return ans; } inline int lcm(int x, int y) { int ans = x / gcd(x, y) * y; return ans; } int twobeki(int x) { int ans = 0; while (1) { if (!(x & 1)) { ans++; x /= 2; } else { break; } } return ans; } int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= i; res %= mod; } return res; } int comb(int x, int y) { if (y > x) return 0; // cout<<kai(x, y)<<' '<<modpow(kai(y, y), mod - 2)<<endl; return kai(x, y) * modpow(kai(y, y), mod - 2) % mod; } int fukugen(vector<int> l) { int ans = 0; for (int i = 0; i < (int)l.size(); i++) { ans *= 10; ans += l[i]; } return ans; } int nanjyo(int n) { int ans = 0; while (n > 1) { ans++; n /= 2; } return ans; } bool intersect(pair<pair<int, int>, pair<int, int>> p1, pair<pair<int, int>, pair<int, int>> p2) { int men11 = (p2.first.first - p1.first.first) * (p2.second.second - p1.first.second) - (p2.second.first - p1.first.first) * (p2.first.second - p1.first.second); int men12 = (p2.first.first - p1.second.first) * (p2.second.second - p1.second.second) - (p2.second.first - p1.second.first) * (p2.first.second - p1.second.second); int men21 = (p1.first.first - p2.first.first) * (p1.second.second - p2.first.second) - (p1.second.first - p2.first.first) * (p1.first.second - p2.first.second); int men22 = (p1.first.first - p2.second.first) * (p1.second.second - p2.second.second) - (p1.second.first - p2.second.first) * (p1.first.second - p2.second.second); return ((signbit(men11)) ^ (signbit(men12))) && ((signbit(men21)) ^ (signbit(men22))); } template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } void Yes() { cout << "Yes" << endl; } void No() { cout << "No" << endl; } void YES() { cout << "YES" << endl; } void NO() { cout << "NO" << endl; } int yakusuu(int x) { int ans = 0; for (int i = 1; i * i <= x; i++) { if (!(x % i)) { if (i * i == x) { ans++; } else ans += 2; } } return ans; } #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i <= n; i++) // Library-End int n, a[100004], ans; vector<int> mi, po; vector<pair<int, int>> v; signed main() { cin >> n; rep(i, n) { scanf("%lld", &a[i]); if (a[i] >= 0) { po.push_back(a[i]); } if (a[i] < 0) { mi.push_back(a[i]); } } sort(po.begin(), po.end(), greater<int>()); sort(mi.begin(), mi.end()); if (!mi.size()) { mi.push_back(po[(int)po.size() - 1]); po.pop_back(); } if (!po.size()) { po.push_back(mi[(int)mi.size()] - 1); mi.pop_back(); } rep(i, (int)po.size() - 1) { v.push_back({mi[0], po[i + 1]}); mi[0] -= po[i + 1]; } rep(i, (int)mi.size() - 1) { v.push_back({po[0], mi[i + 1]}); po[0] -= mi[i + 1]; } v.push_back({po[0], mi[0]}); cout << po[0] - mi[0] << endl; rep(i, v.size()) { printf("%lld %lld\n", v[i].first, v[i].second); } }
replace
310
311
310
311
0
p03007
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define mp make_pair #define fst first #define snd second #define forn(i, n) for (int i = 0; i < int(n); i++) #define forn1(i, n) for (int i = 1; i <= int(n); i++) #define popcnt __builtin_popcount using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, int> pli; typedef pair<int, ll> pil; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; template <typename T> T id(T b) { return b; }; template <class It> bool all(It f, It l) { return std::all_of(f, l, id<bool>); } template <class It> bool any(It f, It l) { return std::any_of(f, l, id<bool>); } template <typename T> void chmax(T &x, T y) { if (x < y) x = y; } template <typename T> void chmin(T &x, T y) { if (x > y) x = y; } const int MAX_N = 100000; int n; int a[MAX_N]; int main() { #ifdef FASTIO ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif cin >> n; forn(i, n) cin >> a[i]; sort(a, a + n); int res = -a[0] + a[n - 1]; for (int i = 1; i < n - 1; i++) res += abs(a[i]); cout << res << endl; int i = n - 2; int prev = a[0]; while (i > 0 && a[i] >= 0) { cout << prev << " " << a[i] << "\n"; prev -= a[i]; i--; } cout << a[n - 1] << " " << prev << "\n"; prev = a[n - 1] - prev; while (i > 0) { cout << prev << " " << a[i] << "\n"; prev -= a[i]; } return 0; }
#include <bits/stdc++.h> #define mp make_pair #define fst first #define snd second #define forn(i, n) for (int i = 0; i < int(n); i++) #define forn1(i, n) for (int i = 1; i <= int(n); i++) #define popcnt __builtin_popcount using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, int> pli; typedef pair<int, ll> pil; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; template <typename T> T id(T b) { return b; }; template <class It> bool all(It f, It l) { return std::all_of(f, l, id<bool>); } template <class It> bool any(It f, It l) { return std::any_of(f, l, id<bool>); } template <typename T> void chmax(T &x, T y) { if (x < y) x = y; } template <typename T> void chmin(T &x, T y) { if (x > y) x = y; } const int MAX_N = 100000; int n; int a[MAX_N]; int main() { #ifdef FASTIO ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif cin >> n; forn(i, n) cin >> a[i]; sort(a, a + n); int res = -a[0] + a[n - 1]; for (int i = 1; i < n - 1; i++) res += abs(a[i]); cout << res << endl; int i = n - 2; int prev = a[0]; while (i > 0 && a[i] >= 0) { cout << prev << " " << a[i] << "\n"; prev -= a[i]; i--; } cout << a[n - 1] << " " << prev << "\n"; prev = a[n - 1] - prev; while (i > 0) { cout << prev << " " << a[i] << "\n"; prev -= a[i]; i--; } return 0; }
insert
68
68
68
69
TLE
p03008
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; #define REP(NAME, NUM) for (int NAME = 0; NAME < (NUM); ++NAME) #define BREP(NAME, NUM) for (int NAME = (NUM)-1; NAME >= 0; --NAME) #define ALL(NAME) (NAME).begin(), (NAME).end() #define cMOD 1000000007ULL #define cINF ((1ll << 62) - 1) #define cINFINT ((1 << 30) - 1) ll knapsackNonLimit(const vector<ll> &v, const vector<ll> &w, ll maxW) { ll n = min(v.size(), w.size()); vector<vector<ll>> dp(n + 1, vector<ll>(maxW + 1, 0)); REP(i, n) REP(j, maxW + 1) { if (j < w[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - w[i]] + v[i]); } return dp[n][maxW]; } int main() { ull n = 0; cin >> n; vector<ll> a(3, 0), b(3, 0); cin >> a[0] >> a[1] >> a[2] >> b[0] >> b[1] >> b[2]; auto v0 = b; v0.push_back(1); auto w0 = a; w0.push_back(1); auto v1 = a; v1.push_back(1); auto w1 = b; w1.push_back(1); ll n1 = knapsackNonLimit(v0, w0, n); ll n2 = knapsackNonLimit(v1, w1, n1); cout << n2 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; #define REP(NAME, NUM) for (int NAME = 0; NAME < (NUM); ++NAME) #define BREP(NAME, NUM) for (int NAME = (NUM)-1; NAME >= 0; --NAME) #define ALL(NAME) (NAME).begin(), (NAME).end() #define cMOD 1000000007ULL #define cINF ((1ll << 62) - 1) #define cINFINT ((1 << 30) - 1) ll knapsackNonLimit(const vector<ll> &v, const vector<ll> &w, ll maxW) { ll n = min(v.size(), w.size()); vector<ll> dp(maxW + 1, 0); REP(i, n) REP(j, maxW + 1) if (j >= w[i]) dp[j] = max(dp[j], dp[j - w[i]] + v[i]); return dp[maxW]; } int main() { ull n = 0; cin >> n; vector<ll> a(3, 0), b(3, 0); cin >> a[0] >> a[1] >> a[2] >> b[0] >> b[1] >> b[2]; auto v0 = b; v0.push_back(1); auto w0 = a; w0.push_back(1); auto v1 = a; v1.push_back(1); auto w1 = b; w1.push_back(1); ll n1 = knapsackNonLimit(v0, w0, n); ll n2 = knapsackNonLimit(v1, w1, n1); cout << n2 << endl; return 0; }
replace
16
24
16
20
MLE
p03008
C++
Runtime Error
#include <bits/stdc++.h> #define loop(n, i) for (int i = 0; i < n; i++) #define loop1(n, i) for (int i = 1; i <= n; i++) #define rloop(n, i) for (int i = n; i; i++) #define HERE cout << "HERE: " << __LINE__ << endl; #define INSP(v) cout << v << " at " << __LINE__ << endl; using namespace std; using ll = long long; template <class T> using V = vector<T>; const int MAX = 5000 * 5000 + 10; ll G[2][3]; ll dp[MAX]; int main() { int n; cin >> n; loop(2, i) loop(3, j) cin >> G[i][j]; fill(dp, dp + MAX, 0); loop(n, i) { dp[i + 1] = max(dp[i + 1], dp[i] + 1); loop(3, j) { int k = i + G[0][j]; dp[k] = max(dp[k], dp[i] + G[1][j]); } } int m = dp[n]; fill(dp, dp + MAX, 0); loop(m, i) { dp[i + 1] = max(dp[i + 1], dp[i] + 1); loop(3, j) { int k = i + G[1][j]; dp[k] = max(dp[k], dp[i] + G[0][j]); } } cout << dp[m] << endl; return 0; }
#include <bits/stdc++.h> #define loop(n, i) for (int i = 0; i < n; i++) #define loop1(n, i) for (int i = 1; i <= n; i++) #define rloop(n, i) for (int i = n; i; i++) #define HERE cout << "HERE: " << __LINE__ << endl; #define INSP(v) cout << v << " at " << __LINE__ << endl; using namespace std; using ll = long long; template <class T> using V = vector<T>; const int MAX = 5000 * 5100; ll G[2][3]; ll dp[MAX]; int main() { int n; cin >> n; loop(2, i) loop(3, j) cin >> G[i][j]; fill(dp, dp + MAX, 0); loop(n, i) { dp[i + 1] = max(dp[i + 1], dp[i] + 1); loop(3, j) { int k = i + G[0][j]; dp[k] = max(dp[k], dp[i] + G[1][j]); } } int m = dp[n]; fill(dp, dp + MAX, 0); loop(m, i) { dp[i + 1] = max(dp[i + 1], dp[i] + 1); loop(3, j) { int k = i + G[1][j]; dp[k] = max(dp[k], dp[i] + G[0][j]); } } cout << dp[m] << endl; return 0; }
replace
10
11
10
11
-11
p03008
C++
Runtime Error
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> // namespace mp = boost::multiprecision; using namespace std; const double PI = 3.14159265358979323846; typedef long long ll; const double EPS = 1e-9; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef pair<ll, ll> P; const ll INF = 10e17; #define cmin(x, y) x = min(x, y) #define cmax(x, y) x = max(x, y) #define ret() return 0; double equal(double a, double b) { return fabs(a - b) < DBL_EPSILON; } std::istream &operator>>(std::istream &in, set<int> &o) { int a; in >> a; o.insert(a); return in; } std::istream &operator>>(std::istream &in, queue<int> &o) { ll a; in >> a; o.push(a); return in; } bool contain(set<int> &s, int a) { return s.find(a) != s.end(); } typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK; // a < b < c < || a > b > c > ll solve_dp(ll n, ll ga, ll sa, ll ba, ll gb, ll sb, ll bb) { vector<vector<ll>> dp_b(n + 1, vector<ll>(n + 1, -1)); vector<vector<ll>> dp_d(n + 1, vector<ll>(n + 1, -1)); for (ll g = 0; g <= n; g++) { for (ll s = 0; s <= n; s++) { ll use = g * ga + s * sa; ll rem = n - use; if (use > n) continue; if (ba > bb) { dp_b[g][s] = 0; dp_d[g][s] = rem; } else { ll b = rem / ba; dp_b[g][s] = b; dp_d[g][s] = rem - (rem / ba * ba); } } } ll ans = 0; for (ll g = 0; g <= n; g++) { for (ll s = 0; s <= n; s++) { if (dp_b[g][s] == -1) continue; ll now = g * gb + s * sb + dp_b[g][s] * bb + dp_d[g][s]; cmax(ans, now); } } return ans; } void all(ll n, ll ga, ll sa, ll ba, ll gb, ll sb, ll bb) { ll ans = solve_dp(n, ga, sa, ba, gb, sb, bb); cout << ans << endl; } void one(ll n, ll ga, ll sa, ll gb, ll sb, ll ba, ll bb) { ll ab = solve_dp(n, ga, sa, ba, gb, sb, bb); ll p = ab / bb; ll q = p * bb; ll t = ab - q; ll ans = p * ba + t; cout << ans << endl; } void tow(ll n, ll ba, ll bb, ll ga, ll sa, ll gb, ll sb) { ll p = n / ba; ll r = p * ba; ll q = p * bb; ll nn = n - r + q; // sa >= sb && ga >= gb ll ans = 0; for (ll g = 0; g * gb <= nn; g++) { ll gp = g * gb; ll rem = nn - gp; ll s = rem / sb; ll sp = s * sb; ll t = nn - gp - sp; ll now = g * ga + s * sa + t; cmax(ans, now); } cout << ans << endl; } int main() { ll n; ll ga, sa, ba; ll gb, sb, bb; cin >> n; cin >> ga >> sa >> ba; cin >> gb >> sb >> bb; if (ga <= gb && sa <= sb && ba <= bb) { all(n, ga, sa, ba, gb, sb, bb); ret(); } if (ga >= gb && sa >= sb && ba >= bb) { all(n, gb, sb, bb, ga, sa, ba); ret(); } if (ga <= gb && sa <= sb && ba >= bb) { one(n, ga, sa, gb, sb, ba, bb); ret(); } if (ga <= gb && sa >= sb && ba <= bb) { one(n, ga, ba, gb, bb, sa, sb); ret(); } if (ga >= gb && sa <= sb && ba <= bb) { one(n, sa, ba, sb, bb, ga, gb); ret(); } if (ga <= gb && sa >= sb && ba >= bb) { tow(n, ga, gb, sa, ba, sb, bb); ret(); } __throw_runtime_error("mada"); }
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> // namespace mp = boost::multiprecision; using namespace std; const double PI = 3.14159265358979323846; typedef long long ll; const double EPS = 1e-9; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef pair<ll, ll> P; const ll INF = 10e17; #define cmin(x, y) x = min(x, y) #define cmax(x, y) x = max(x, y) #define ret() return 0; double equal(double a, double b) { return fabs(a - b) < DBL_EPSILON; } std::istream &operator>>(std::istream &in, set<int> &o) { int a; in >> a; o.insert(a); return in; } std::istream &operator>>(std::istream &in, queue<int> &o) { ll a; in >> a; o.push(a); return in; } bool contain(set<int> &s, int a) { return s.find(a) != s.end(); } typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK; // a < b < c < || a > b > c > ll solve_dp(ll n, ll ga, ll sa, ll ba, ll gb, ll sb, ll bb) { vector<vector<ll>> dp_b(n + 1, vector<ll>(n + 1, -1)); vector<vector<ll>> dp_d(n + 1, vector<ll>(n + 1, -1)); for (ll g = 0; g <= n; g++) { for (ll s = 0; s <= n; s++) { ll use = g * ga + s * sa; ll rem = n - use; if (use > n) continue; if (ba > bb) { dp_b[g][s] = 0; dp_d[g][s] = rem; } else { ll b = rem / ba; dp_b[g][s] = b; dp_d[g][s] = rem - (rem / ba * ba); } } } ll ans = 0; for (ll g = 0; g <= n; g++) { for (ll s = 0; s <= n; s++) { if (dp_b[g][s] == -1) continue; ll now = g * gb + s * sb + dp_b[g][s] * bb + dp_d[g][s]; cmax(ans, now); } } return ans; } void all(ll n, ll ga, ll sa, ll ba, ll gb, ll sb, ll bb) { ll ans = solve_dp(n, ga, sa, ba, gb, sb, bb); cout << ans << endl; } void one(ll n, ll ga, ll sa, ll gb, ll sb, ll ba, ll bb) { ll ab = solve_dp(n, ga, sa, ba, gb, sb, bb); ll p = ab / bb; ll q = p * bb; ll t = ab - q; ll ans = p * ba + t; cout << ans << endl; } void tow(ll n, ll ba, ll bb, ll ga, ll sa, ll gb, ll sb) { ll p = n / ba; ll r = p * ba; ll q = p * bb; ll nn = n - r + q; // sa >= sb && ga >= gb ll ans = 0; for (ll g = 0; g * gb <= nn; g++) { ll gp = g * gb; ll rem = nn - gp; ll s = rem / sb; ll sp = s * sb; ll t = nn - gp - sp; ll now = g * ga + s * sa + t; cmax(ans, now); } cout << ans << endl; } int main() { ll n; ll ga, sa, ba; ll gb, sb, bb; cin >> n; cin >> ga >> sa >> ba; cin >> gb >> sb >> bb; if (ga <= gb && sa <= sb && ba <= bb) { all(n, ga, sa, ba, gb, sb, bb); ret(); } if (ga >= gb && sa >= sb && ba >= bb) { all(n, gb, sb, bb, ga, sa, ba); ret(); } if (ga <= gb && sa <= sb && ba >= bb) { one(n, ga, sa, gb, sb, ba, bb); ret(); } if (ga <= gb && sa >= sb && ba <= bb) { one(n, ga, ba, gb, bb, sa, sb); ret(); } if (ga >= gb && sa <= sb && ba <= bb) { one(n, sa, ba, sb, bb, ga, gb); ret(); } if (ga <= gb && sa >= sb && ba >= bb) { tow(n, ga, gb, sa, ba, sb, bb); ret(); } if (ga >= gb && sa <= sb && ba >= bb) { tow(n, sa, sb, ga, ba, gb, bb); ret(); } if (ga >= gb && sa >= sb && ba <= bb) { tow(n, ba, bb, ga, sa, gb, sb); ret(); } __throw_runtime_error("mada"); }
insert
143
143
143
153
0
p03008
C++
Runtime Error
#include "bits/stdc++.h" #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; using ll = long long; int main() { int N, a[2][3], b[3]; cin >> N; rep(i, 2) rep(j, 3) cin >> a[i][j]; rep(i, 3) b[i] = a[1][i] - a[0][i]; vector<vector<ll>> dp1(4, vector<ll>(N + 1, 0)); rep(i, 3) { rep(j, N + 1) { dp1[i + 1][j] = dp1[i][j]; if (j >= a[0][i]) { ll x = dp1[i + 1][j - a[0][i]] + b[i]; if (x > dp1[i + 1][j]) dp1[i + 1][j] = x; } } } N += dp1[3][N]; vector<vector<ll>> dp2(4, vector<ll>(N + 1, 0)); rep(i, 3) { rep(j, N + 1) { dp2[i + 1][j] = dp2[i][j]; if (j >= a[1][i]) { ll x = dp2[i + 1][j - a[0][i]] - b[i]; if (x > dp2[i + 1][j]) dp2[i + 1][j] = x; } } } cout << N + dp2[3][N] << endl; }
#include "bits/stdc++.h" #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; using ll = long long; int main() { int N, a[2][3], b[3]; cin >> N; rep(i, 2) rep(j, 3) cin >> a[i][j]; rep(i, 3) b[i] = a[1][i] - a[0][i]; vector<vector<ll>> dp1(4, vector<ll>(N + 1, 0)); rep(i, 3) { rep(j, N + 1) { dp1[i + 1][j] = dp1[i][j]; if (j >= a[0][i]) { ll x = dp1[i + 1][j - a[0][i]] + b[i]; if (x > dp1[i + 1][j]) dp1[i + 1][j] = x; } } } N += dp1[3][N]; vector<vector<ll>> dp2(4, vector<ll>(N + 1, 0)); rep(i, 3) { rep(j, N + 1) { dp2[i + 1][j] = dp2[i][j]; if (j >= a[1][i]) { ll x = dp2[i + 1][j - a[1][i]] - b[i]; if (x > dp2[i + 1][j]) dp2[i + 1][j] = x; } } } cout << N + dp2[3][N] << endl; }
replace
28
29
28
29
0
p03008
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define FOR(i, n, m) for (int i = n; i < (int)m; i++) #define REP(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define pb push_back using namespace std; using ll = std::int_fast64_t; using P = pair<ll, ll>; constexpr ll inf = 1000000000; constexpr ll mod = 1000000007; constexpr long double eps = 1e-15; template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << to_string(p.first) << " " << to_string(p.second); return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { REP(i, v.size()) { if (i) os << " "; os << to_string(v[i]); } return os; } // number// ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } struct modint { ll n; public: modint(const ll n = 0) : n((n % mod + mod) % mod) {} static modint pow(modint a, ll m) { // static vector<modint> v(1000010, -1); // if(m == mod - 2 && v[a.n] != -1) return v[a.n]; // int tmp = a.n; modint r = 1; while (m > 0) { if (m & 1) { r *= a; } a = (a * a); m /= 2; } // if(m == mod - 2) return v[tmp] = r; return r; } modint &operator++() { *this += 1; return *this; } modint &operator--() { *this -= 1; return *this; } modint operator++(int) { modint ret = *this; *this += 1; return ret; } modint operator--(int) { modint ret = *this; *this -= 1; return ret; } modint operator~() const { return (this->pow(n, mod - 2)); } // inverse friend bool operator==(const modint &lhs, const modint &rhs) { return lhs.n == rhs.n; } friend bool operator<(const modint &lhs, const modint &rhs) { return lhs.n < rhs.n; } friend bool operator>(const modint &lhs, const modint &rhs) { return lhs.n > rhs.n; } friend modint &operator+=(modint &lhs, const modint &rhs) { lhs.n += rhs.n; if (lhs.n >= mod) lhs.n -= mod; return lhs; } friend modint &operator-=(modint &lhs, const modint &rhs) { lhs.n -= rhs.n; if (lhs.n < 0) lhs.n += mod; return lhs; } friend modint &operator*=(modint &lhs, const modint &rhs) { lhs.n = (lhs.n * rhs.n) % mod; return lhs; } friend modint &operator/=(modint &lhs, const modint &rhs) { lhs.n = (lhs.n * (~rhs).n) % mod; return lhs; } friend modint operator+(const modint &lhs, const modint &rhs) { return modint(lhs.n + rhs.n); } friend modint operator-(const modint &lhs, const modint &rhs) { return modint(lhs.n - rhs.n); } friend modint operator*(const modint &lhs, const modint &rhs) { return modint(lhs.n * rhs.n); } friend modint operator/(const modint &lhs, const modint &rhs) { return modint(lhs.n * (~rhs).n); } }; istream &operator>>(istream &is, modint m) { is >> m.n; return is; } ostream &operator<<(ostream &os, modint m) { os << m.n; return os; } struct binomial_coefficient { private: int m; vector<modint> fact; public: binomial_coefficient(int m) : m(m) { fact.resize(m); fact[0] = 1; for (int i = 1; i < m; i++) fact[i] = fact[i - 1] * i; } modint combination(int n, int k) { if (n < 0 || k < 0 || n < k) return 0; return fact[n] / fact[n - k] / fact[k]; } }; // graph // using weight = long long; struct edge { int to; weight w; }; template <typename T> struct vedge : edge { T v; }; struct graph { private: int n; bool weighted; bool tree; vector<vector<edge>> g; public: graph(int n, bool tree = false) : n(n), weighted(false), tree(tree), g(vector<vector<edge>>(n)) {} vector<edge> &operator[](int i) { return g[i]; } int size() { return n; } bool isweighted() { return weighted; } bool istree() { return tree; } void add_edge(int s, int t, weight w = 1) { g[s].push_back({t, w}); g[t].push_back({s, w}); if (w != 1) weighted = true; } void add_dedge(int s, int t, weight w = 1) { g[s].push_back({t, w}); if (w != 1) weighted = true; } }; template <typename T> struct vgraph { public: private: int n; bool weighted; bool tree; vector<vector<vedge<T>>> g; vgraph(int n, bool tree = false) : n(n), weighted(false), tree(tree), g(vector<vector<vedge<T>>>(n)) {} vector<edge> &operator[](int i) { return g[i]; } int size() { return n; } bool isweighted() { return weighted; } bool istree() { return tree; } void add_edge(int s, int t, T v, weight w = 1) { g[s].push_back({t, w, v}); g[t].push_back({s, w, v}); if (w != 1) weighted = true; } void add_dedge(int s, int t, T v, weight w = 1) { g[s].push_back({t, w, v}); if (w != 1) weighted = true; } graph build_graph() { graph ret(n); for (auto v : g) for (auto e : g[v]) { ret.add_dedge(v, e.to, e.w); } return ret; } }; vector<weight> shortest_path(graph &g, int s) { int n = g.size(); vector<weight> path(n, -1); if (g.isweighted() && !g.istree()) { // dijkstra priority_queue<pair<weight, int>, vector<pair<weight, int>>, greater<pair<weight, int>>> q; path[s] = 0; q.push({0, s}); while (!q.empty()) { weight cost = q.top().first; int p = q.top().second; q.pop(); if (path[p] != cost) continue; for (auto e : g[p]) { if (path[e.to] == -1 || path[e.to] > cost + e.w) { path[e.to] = cost + e.w; q.push({path[e.to], e.to}); } } } } else { // bfs queue<int> q; path[s] = 0; q.push(s); while (!q.empty()) { int p = q.front(); q.pop(); for (auto e : g[p]) { if (path[e.to] == -1) { path[e.to] = path[p] + 1; q.push(e.to); } } } } return path; } using mi = modint; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n; ll ga, sa, ba, gb, sb, bb; cin >> n; cin >> ga >> sa >> ba >> gb >> sb >> bb; ll dong2 = n; ll ans = n; REP(i, n + 1) REP(j, n + 1) { if (i * ga + j * sa > n) continue; ll k = 0; if (ba < bb) k = ((n - i * ga - j * sa) / ba); dong2 = max(dong2, n + i * (gb - ga) + j * (sb - sa) + k * (bb - ba)); } REP(i, dong2 + 1) REP(j, dong2 + 1) { if (i * gb + j * sb > dong2) continue; ll k = 0; if (bb < ba) k = ((dong2 - i * gb - j * sb) / bb); ans = max(ans, dong2 + i * (ga - gb) + j * (sa - sb) + k * (ba - bb)); } cout << ans << endl; return 0; } // ---------------------------------------
#include <bits/stdc++.h> #define FOR(i, n, m) for (int i = n; i < (int)m; i++) #define REP(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define pb push_back using namespace std; using ll = std::int_fast64_t; using P = pair<ll, ll>; constexpr ll inf = 1000000000; constexpr ll mod = 1000000007; constexpr long double eps = 1e-15; template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << to_string(p.first) << " " << to_string(p.second); return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { REP(i, v.size()) { if (i) os << " "; os << to_string(v[i]); } return os; } // number// ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } struct modint { ll n; public: modint(const ll n = 0) : n((n % mod + mod) % mod) {} static modint pow(modint a, ll m) { // static vector<modint> v(1000010, -1); // if(m == mod - 2 && v[a.n] != -1) return v[a.n]; // int tmp = a.n; modint r = 1; while (m > 0) { if (m & 1) { r *= a; } a = (a * a); m /= 2; } // if(m == mod - 2) return v[tmp] = r; return r; } modint &operator++() { *this += 1; return *this; } modint &operator--() { *this -= 1; return *this; } modint operator++(int) { modint ret = *this; *this += 1; return ret; } modint operator--(int) { modint ret = *this; *this -= 1; return ret; } modint operator~() const { return (this->pow(n, mod - 2)); } // inverse friend bool operator==(const modint &lhs, const modint &rhs) { return lhs.n == rhs.n; } friend bool operator<(const modint &lhs, const modint &rhs) { return lhs.n < rhs.n; } friend bool operator>(const modint &lhs, const modint &rhs) { return lhs.n > rhs.n; } friend modint &operator+=(modint &lhs, const modint &rhs) { lhs.n += rhs.n; if (lhs.n >= mod) lhs.n -= mod; return lhs; } friend modint &operator-=(modint &lhs, const modint &rhs) { lhs.n -= rhs.n; if (lhs.n < 0) lhs.n += mod; return lhs; } friend modint &operator*=(modint &lhs, const modint &rhs) { lhs.n = (lhs.n * rhs.n) % mod; return lhs; } friend modint &operator/=(modint &lhs, const modint &rhs) { lhs.n = (lhs.n * (~rhs).n) % mod; return lhs; } friend modint operator+(const modint &lhs, const modint &rhs) { return modint(lhs.n + rhs.n); } friend modint operator-(const modint &lhs, const modint &rhs) { return modint(lhs.n - rhs.n); } friend modint operator*(const modint &lhs, const modint &rhs) { return modint(lhs.n * rhs.n); } friend modint operator/(const modint &lhs, const modint &rhs) { return modint(lhs.n * (~rhs).n); } }; istream &operator>>(istream &is, modint m) { is >> m.n; return is; } ostream &operator<<(ostream &os, modint m) { os << m.n; return os; } struct binomial_coefficient { private: int m; vector<modint> fact; public: binomial_coefficient(int m) : m(m) { fact.resize(m); fact[0] = 1; for (int i = 1; i < m; i++) fact[i] = fact[i - 1] * i; } modint combination(int n, int k) { if (n < 0 || k < 0 || n < k) return 0; return fact[n] / fact[n - k] / fact[k]; } }; // graph // using weight = long long; struct edge { int to; weight w; }; template <typename T> struct vedge : edge { T v; }; struct graph { private: int n; bool weighted; bool tree; vector<vector<edge>> g; public: graph(int n, bool tree = false) : n(n), weighted(false), tree(tree), g(vector<vector<edge>>(n)) {} vector<edge> &operator[](int i) { return g[i]; } int size() { return n; } bool isweighted() { return weighted; } bool istree() { return tree; } void add_edge(int s, int t, weight w = 1) { g[s].push_back({t, w}); g[t].push_back({s, w}); if (w != 1) weighted = true; } void add_dedge(int s, int t, weight w = 1) { g[s].push_back({t, w}); if (w != 1) weighted = true; } }; template <typename T> struct vgraph { public: private: int n; bool weighted; bool tree; vector<vector<vedge<T>>> g; vgraph(int n, bool tree = false) : n(n), weighted(false), tree(tree), g(vector<vector<vedge<T>>>(n)) {} vector<edge> &operator[](int i) { return g[i]; } int size() { return n; } bool isweighted() { return weighted; } bool istree() { return tree; } void add_edge(int s, int t, T v, weight w = 1) { g[s].push_back({t, w, v}); g[t].push_back({s, w, v}); if (w != 1) weighted = true; } void add_dedge(int s, int t, T v, weight w = 1) { g[s].push_back({t, w, v}); if (w != 1) weighted = true; } graph build_graph() { graph ret(n); for (auto v : g) for (auto e : g[v]) { ret.add_dedge(v, e.to, e.w); } return ret; } }; vector<weight> shortest_path(graph &g, int s) { int n = g.size(); vector<weight> path(n, -1); if (g.isweighted() && !g.istree()) { // dijkstra priority_queue<pair<weight, int>, vector<pair<weight, int>>, greater<pair<weight, int>>> q; path[s] = 0; q.push({0, s}); while (!q.empty()) { weight cost = q.top().first; int p = q.top().second; q.pop(); if (path[p] != cost) continue; for (auto e : g[p]) { if (path[e.to] == -1 || path[e.to] > cost + e.w) { path[e.to] = cost + e.w; q.push({path[e.to], e.to}); } } } } else { // bfs queue<int> q; path[s] = 0; q.push(s); while (!q.empty()) { int p = q.front(); q.pop(); for (auto e : g[p]) { if (path[e.to] == -1) { path[e.to] = path[p] + 1; q.push(e.to); } } } } return path; } using mi = modint; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n; ll ga, sa, ba, gb, sb, bb; cin >> n; cin >> ga >> sa >> ba >> gb >> sb >> bb; ll dong2 = n; ll ans = n; REP(i, n + 1) REP(j, n + 1) { if (i * ga + j * sa > n) continue; ll k = 0; if (ba < bb) k = ((n - i * ga - j * sa) / ba); dong2 = max(dong2, n + i * (gb - ga) + j * (sb - sa) + k * (bb - ba)); } ans = dong2; if (ga > gb && sa > sb && ba > bb) { REP(i, dong2 + 1) REP(j, dong2 + 1) { if (i * gb + j * sb > dong2) continue; ll k = 0; if (bb < ba) k = ((dong2 - i * gb - j * sb) / bb); ans = max(ans, dong2 + i * (ga - gb) + j * (sa - sb) + k * (ba - bb)); } } else if (ga > gb && sa > sb) { REP(i, dong2 + 1) { if (i * gb > dong2) continue; ll j = ((dong2 - i * gb) / sb); ans = max(ans, dong2 + i * (ga - gb) + j * (sa - sb)); } } else if (sa > sb && ba > bb) { REP(i, dong2 + 1) { if (i * sb > dong2) continue; ll j = ((dong2 - i * sb) / bb); ans = max(ans, dong2 + i * (sa - sb) + j * (ba - bb)); } } else if (ba > bb && ga > gb) { REP(i, dong2 + 1) { if (i * bb > dong2) continue; ll j = ((dong2 - i * bb) / gb); ans = max(ans, dong2 + i * (ba - bb) + j * (ga - gb)); } } else if (ga > gb) { ll i = (dong2 / gb); ans = max(ans, dong2 + i * (ga - gb)); } else if (sa > sb) { ll i = (dong2 / sb); ans = max(ans, dong2 + i * (sa - sb)); } else if (ba > bb) { ll i = (dong2 / bb); ans = max(ans, dong2 + i * (ba - bb)); } cout << ans << endl; return 0; } // ---------------------------------------
replace
269
276
269
309
TLE
p03008
C++
Runtime Error
#define _USE_MATH_DEFINES #include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <tuple> #include <vector> using ll = long long; // LLONG_MAX #define int long long #define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end()) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define ARY_SORT(a, size) sort((a), (a) + (size)) #define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end())) #define REVERSE(v) (reverse((v).begin(), (v).end())) #define LOWER_BOUND(v, a) (lower_bound((v).begin(), (v).end(), (a))) #define UPPER_BOUND(v, a) (upper_bound((v).begin(), (v).end(), (a))) #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) #define CONTAINS_MAP(m, a) (m).find((a)) != m.end() using namespace std; int N, GA, SA, BA, GB, SB, BB; int dp0[4][5001]; int dp1[4][25000001]; signed main() { cin >> N; cin >> GA >> SA >> BA; cin >> GB >> SB >> BB; int m0[4] = {0, GA, SA, BA}; int v0[4] = {0, GB, SB, BB}; for (int i = 0; i <= N; i++) { dp0[0][i] = i; } for (int j = 1; j <= 3; j++) { for (int i = 0; i <= N; i++) { int a = (i >= m0[j]) ? dp0[j][i - m0[j]] + v0[j] : 0; int b = dp0[j - 1][i]; dp0[j][i] = MAX(a, b); } } int M = dp0[3][N]; int m1[4] = {0, GB, SB, BB}; int v1[4] = {0, GA, SA, BA}; for (int i = 0; i <= M; i++) { dp1[0][i] = i; } for (int j = 1; j <= 3; j++) { for (int i = 0; i <= M; i++) { int a = (i >= m1[j]) ? dp0[1][i - m1[j]] + v1[j] : 0; int b = dp1[j - 1][i]; dp1[j][i] = MAX(a, b); } } cout << dp1[3][M] << endl; }
#define _USE_MATH_DEFINES #include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <tuple> #include <vector> using ll = long long; // LLONG_MAX #define int long long #define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end()) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define ARY_SORT(a, size) sort((a), (a) + (size)) #define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end())) #define REVERSE(v) (reverse((v).begin(), (v).end())) #define LOWER_BOUND(v, a) (lower_bound((v).begin(), (v).end(), (a))) #define UPPER_BOUND(v, a) (upper_bound((v).begin(), (v).end(), (a))) #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) #define CONTAINS_MAP(m, a) (m).find((a)) != m.end() using namespace std; int N, GA, SA, BA, GB, SB, BB; int dp0[4][5001]; int dp1[4][25000001]; signed main() { cin >> N; cin >> GA >> SA >> BA; cin >> GB >> SB >> BB; int m0[4] = {0, GA, SA, BA}; int v0[4] = {0, GB, SB, BB}; for (int i = 0; i <= N; i++) { dp0[0][i] = i; } for (int j = 1; j <= 3; j++) { for (int i = 0; i <= N; i++) { int a = (i >= m0[j]) ? dp0[j][i - m0[j]] + v0[j] : 0; int b = dp0[j - 1][i]; dp0[j][i] = MAX(a, b); } } int M = dp0[3][N]; int m1[4] = {0, GB, SB, BB}; int v1[4] = {0, GA, SA, BA}; for (int i = 0; i <= M; i++) { dp1[0][i] = i; } for (int j = 1; j <= 3; j++) { for (int i = 0; i <= M; i++) { int a = (i >= m1[j]) ? dp1[j][i - m1[j]] + v1[j] : 0; int b = dp1[j - 1][i]; dp1[j][i] = MAX(a, b); } } cout << dp1[3][M] << endl; }
replace
70
71
70
71
-11
p03008
C++
Runtime Error
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author alireza_kaviani */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef pair<ld, ld> pld; typedef pair<string, string> pss; #define all(x) (x).begin(), (x).end() #define Sort(x) sort(all((x))) #define X first #define Y second #define Mp make_pair #define sep ' ' #define endl '\n' #define debug(x) cerr << #x << " = " << x << endl #define SZ(x) ll(x.size()) #define fast_io \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define file_io \ freopen("in.txt", "r+", stdin); \ freopen("out.txt", "w+", stdout); #define set_random \ mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); inline ll input() { ll n; cin >> n; return n; } ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } set_random; const ll MAXN = 2.5e7 + 10; const ll INF = 1e15; const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9; ll n, ans, ga[4], gb[4], dp[MAXN], dp2[MAXN]; int main() { fast_io; for (ll j = 0; j < MAXN; j++) { dp[j] = -INF; dp2[j] = -INF; } cin >> n >> ga[1] >> ga[2] >> ga[3] >> gb[1] >> gb[2] >> gb[3]; dp[n] = 0; for (ll i = 1; i < 4; i++) { for (ll j = MAXN - 1; j >= ga[i]; j--) { dp[j - ga[i]] = max(dp[j - ga[i]], dp[j] + gb[i]); } } n = 0; for (ll i = 0; i < MAXN; i++) n = max(n, i + dp[i]); dp2[n] = 0; for (ll i = 1; i < 4; i++) { for (ll j = MAXN - 1; j >= ga[i]; j--) { dp2[j - gb[i]] = max(dp2[j - gb[i]], dp2[j] + ga[i]); } } n = 0; for (ll i = 0; i < MAXN; i++) n = max(n, i + dp2[i]); cout << n << endl; return 0; } /* todo : 1- set constants 2- check TimeLimit and MemoryLimit 3- check special test cases 4- don't forget BS can help you that is good idea(use for loop for floats) 5- don't forget printf and scanf can help you in your code speed */
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author alireza_kaviani */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef pair<ld, ld> pld; typedef pair<string, string> pss; #define all(x) (x).begin(), (x).end() #define Sort(x) sort(all((x))) #define X first #define Y second #define Mp make_pair #define sep ' ' #define endl '\n' #define debug(x) cerr << #x << " = " << x << endl #define SZ(x) ll(x.size()) #define fast_io \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define file_io \ freopen("in.txt", "r+", stdin); \ freopen("out.txt", "w+", stdout); #define set_random \ mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); inline ll input() { ll n; cin >> n; return n; } ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } set_random; const ll MAXN = 2.5e7 + 10; const ll INF = 1e15; const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9; ll n, ans, ga[4], gb[4], dp[MAXN], dp2[MAXN]; int main() { fast_io; for (ll j = 0; j < MAXN; j++) { dp[j] = -INF; dp2[j] = -INF; } cin >> n >> ga[1] >> ga[2] >> ga[3] >> gb[1] >> gb[2] >> gb[3]; dp[n] = 0; for (ll i = 1; i < 4; i++) { for (ll j = MAXN - 1; j >= ga[i]; j--) { dp[j - ga[i]] = max(dp[j - ga[i]], dp[j] + gb[i]); } } n = 0; for (ll i = 0; i < MAXN; i++) n = max(n, i + dp[i]); dp2[n] = 0; for (ll i = 1; i < 4; i++) { for (ll j = MAXN - 1; j >= gb[i]; j--) { dp2[j - gb[i]] = max(dp2[j - gb[i]], dp2[j] + ga[i]); } } n = 0; for (ll i = 0; i < MAXN; i++) n = max(n, i + dp2[i]); cout << n << endl; return 0; } /* todo : 1- set constants 2- check TimeLimit and MemoryLimit 3- check special test cases 4- don't forget BS can help you that is good idea(use for loop for floats) 5- don't forget printf and scanf can help you in your code speed */
replace
85
86
85
86
-11
p03008
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <chrono> #include <iostream> #include <random> #include <string> #include <unordered_map> #include <vector> std::vector<int64_t> dp; int main(void) { int64_t n; std::vector<int64_t> g(2), s(2), b(2); std::cin >> n; for (int i = 0; i < 2; i++) { std::cin >> g[i] >> s[i] >> b[i]; } dp.resize(5002); int64_t gain[3]; gain[0] = g[1] - g[0]; gain[1] = s[1] - s[0]; gain[2] = b[1] - b[0]; for (int i = 0; i <= dp.size(); i++) dp[i] = n; for (int i = 0; i <= n; i++) { dp[(i + g[0]) % 5001] = std::max(dp[(i + g[0]) % 5001], dp[i % 5001] + gain[0]); dp[(i + s[0]) % 5001] = std::max(dp[(i + s[0]) % 5001], dp[i % 5001] + gain[1]); dp[(i + b[0]) % 5001] = std::max(dp[(i + b[0]) % 5001], dp[i % 5001] + gain[2]); if (i != n) dp[i % 5001] = n; } n = dp[n % 5001]; for (int i = 0; i < 3; i++) gain[i] *= -1; for (int i = 0; i <= dp.size(); i++) dp[i] = n; for (int i = 0; i <= n; i++) { dp[(i + g[1]) % 5001] = std::max(dp[(i + g[1]) % 5001], dp[i % 5001] + gain[0]); dp[(i + s[1]) % 5001] = std::max(dp[(i + s[1]) % 5001], dp[i % 5001] + gain[1]); dp[(i + b[1]) % 5001] = std::max(dp[(i + b[1]) % 5001], dp[i % 5001] + gain[2]); if (i != n) dp[i % 5001] = n; } std::cout << dp[n] << std::endl; }
#include <algorithm> #include <array> #include <bitset> #include <chrono> #include <iostream> #include <random> #include <string> #include <unordered_map> #include <vector> std::vector<int64_t> dp; int main(void) { int64_t n; std::vector<int64_t> g(2), s(2), b(2); std::cin >> n; for (int i = 0; i < 2; i++) { std::cin >> g[i] >> s[i] >> b[i]; } dp.resize(5002); int64_t gain[3]; gain[0] = g[1] - g[0]; gain[1] = s[1] - s[0]; gain[2] = b[1] - b[0]; for (int i = 0; i <= dp.size(); i++) dp[i] = n; for (int i = 0; i <= n; i++) { dp[(i + g[0]) % 5001] = std::max(dp[(i + g[0]) % 5001], dp[i % 5001] + gain[0]); dp[(i + s[0]) % 5001] = std::max(dp[(i + s[0]) % 5001], dp[i % 5001] + gain[1]); dp[(i + b[0]) % 5001] = std::max(dp[(i + b[0]) % 5001], dp[i % 5001] + gain[2]); if (i != n) dp[i % 5001] = n; } n = dp[n % 5001]; for (int i = 0; i < 3; i++) gain[i] *= -1; for (int i = 0; i <= dp.size(); i++) dp[i] = n; for (int i = 0; i <= n; i++) { dp[(i + g[1]) % 5001] = std::max(dp[(i + g[1]) % 5001], dp[i % 5001] + gain[0]); dp[(i + s[1]) % 5001] = std::max(dp[(i + s[1]) % 5001], dp[i % 5001] + gain[1]); dp[(i + b[1]) % 5001] = std::max(dp[(i + b[1]) % 5001], dp[i % 5001] + gain[2]); if (i != n) dp[i % 5001] = n; } std::cout << dp[n % 5001] << std::endl; }
replace
51
52
51
52
0
p03008
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define ii pair<int, int> #define app push_back #define all(a) a.begin(), a.end() #define bp __builtin_popcountll #define ll long long #define mp make_pair #define f first #define s second #define Time (double)clock() / CLOCKS_PER_SEC int solve(int n, vector<int> a, vector<int> b) { vector<int> r(3); for (int i = 0; i < 3; ++i) { if (a[i] >= b[i]) { r[i] = 0; } else { r[i] = n / a[i]; } } int ans = n; for (int i = 0; i <= r[0]; ++i) { for (int j = 0; i * a[0] + j * a[1] <= n && j <= r[1]; ++j) { int h = n - i * a[0] - j * a[1]; int k = h / a[2]; ans = max(ans, n + i * (b[0] - a[0]) + j * (b[1] - a[1]) + k * (b[2] - a[2])); ans = max(ans, n + i * (b[0] - a[0]) + j * (b[1] - a[1])); } } return ans; } signed main() { #ifdef HOME freopen("input.txt", "r", stdin); #else #define endl '\n' ios_base::sync_with_stdio(0); cin.tie(0); #endif int n; cin >> n; vector<int> a(3), b(3); for (int i = 0; i < 3; ++i) cin >> a[i]; for (int i = 0; i < 3; ++i) cin >> b[i]; n = solve(n, a, b); n = solve(n, b, a); cout << n << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long #define ii pair<int, int> #define app push_back #define all(a) a.begin(), a.end() #define bp __builtin_popcountll #define ll long long #define mp make_pair #define f first #define s second #define Time (double)clock() / CLOCKS_PER_SEC int solve(int n, vector<int> a, vector<int> b) { auto comp = [&](int i, int j) { return b[i] - a[i] < b[j] - a[j]; }; vector<int> per; for (int i = 0; i < 3; ++i) per.app(i); sort(all(per), comp); vector<int> a1(3), b1(3); for (int i = 0; i < 3; ++i) { a1[i] = a[per[i]]; b1[i] = b[per[i]]; } a = a1; b = b1; vector<int> r(3); for (int i = 0; i < 3; ++i) { if (a[i] >= b[i]) { r[i] = 0; } else { r[i] = n / a[i]; } } int ans = n; for (int i = 0; i <= r[0]; ++i) { for (int j = 0; i * a[0] + j * a[1] <= n && j <= r[1]; ++j) { int h = n - i * a[0] - j * a[1]; int k = h / a[2]; ans = max(ans, n + i * (b[0] - a[0]) + j * (b[1] - a[1]) + k * (b[2] - a[2])); ans = max(ans, n + i * (b[0] - a[0]) + j * (b[1] - a[1])); } } return ans; } signed main() { #ifdef HOME freopen("input.txt", "r", stdin); #else #define endl '\n' ios_base::sync_with_stdio(0); cin.tie(0); #endif int n; cin >> n; vector<int> a(3), b(3); for (int i = 0; i < 3; ++i) cin >> a[i]; for (int i = 0; i < 3; ++i) cin >> b[i]; n = solve(n, a, b); n = solve(n, b, a); cout << n << endl; }
insert
14
14
14
29
TLE
p03008
Python
Time Limit Exceeded
N = int(input()) A = [int(_) for _ in input().split()] B = [int(_) for _ in input().split()] def count(n, a, b): a0, a1, a2 = a b0, b1, b2 = b dp = [0] * (n + 1) for i in range(n + 1): c0 = dp[i - a0] + b0 if i - a0 >= 0 else 0 c1 = dp[i - a1] + b1 if i - a1 >= 0 else 0 c2 = dp[i - a2] + b2 if i - a2 >= 0 else 0 dp[i] = max([i, c0, c1, c2]) return dp[n] print(count(count(N, A, B), B, A))
N = int(input()) A = [int(_) for _ in input().split()] B = [int(_) for _ in input().split()] def count(n, a, b): a0, a1, a2 = a b0, b1, b2 = b dp = [0] * (n + 1) for i in range(n + 1): c0 = dp[i - a0] + b0 if i - a0 >= 0 else 0 c1 = dp[i - a1] + b1 if i - a1 >= 0 else 0 c2 = dp[i - a2] + b2 if i - a2 >= 0 else 0 dp[i] = max(i, c0, c1, c2) return dp[n] print(count(count(N, A, B), B, A))
replace
13
14
13
14
TLE
p03008
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { long n, n1 = 0, n2 = 0, g1, s1, b1, g2, s2, b2; cin >> n >> g1 >> s1 >> b1 >> g2 >> s2 >> b2; for (long i = 0; i * g1 <= n; i++) { for (long j = 0; i * g1 + j * s1 <= n; j++) { n1 = max(n1, i * g2 + j * s2 + (n - i * g1 - j * s1)); n1 = max(n1, i * g2 + j * s2 + (n - i * g1 - j * s1) / b1 * b2 + (n - i * g1 - j * s1) % b1); } } for (long i = 0; i * g2 <= n1; i++) { for (long j = 0; i * g2 + j * s2 <= n1; j++) { n2 = max(n2, i * g1 + j * s1 + (n1 - i * g2 - j * s2)); n2 = max(n2, i * g1 + j * s1 + (n1 - i * g2 - j * s2) / b2 * b1 + (n1 - i * g2 - j * s2) % b2); } } cout << n2 << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long n, n1 = 0, n2 = 0, g1, s1, b1, g2, s2, b2; cin >> n >> g1 >> s1 >> b1 >> g2 >> s2 >> b2; for (long i = 0; i * g1 <= n; i++) { for (long j = 0; i * g1 + j * s1 <= n; j++) { n1 = max(n1, i * g2 + j * s2 + (n - i * g1 - j * s1)); n1 = max(n1, i * g2 + j * s2 + (n - i * g1 - j * s1) / b1 * b2 + (n - i * g1 - j * s1) % b1); } } vector<long> v2, v1; if (g2 < g1) { v2.push_back(g2); v1.push_back(g1); } if (s2 < s1) { v2.push_back(s2); v1.push_back(s1); } if (b2 < b1) { v2.push_back(b2); v1.push_back(b1); } if (v2.size() == 0) n2 = n1; if (v2.size() == 1) n2 = n1 / v2[0] * v1[0] + n1 % v2[0]; if (v2.size() == 2) { for (long i = 0; i * v2[0] <= n1; i++) { n2 = max(n2, i * v1[0] + (n1 - i * v1[0])); n2 = max(n2, i * v1[0] + (n1 - i * v2[0]) / v2[1] * v1[1] + (n1 - i * v2[0]) % v2[1]); } } if (v2.size() == 3) { for (long i = 0; i * g2 <= n1; i++) { for (long j = 0; i * g2 + j * s2 <= n1; j++) { n2 = max(n2, i * g1 + j * s1 + (n1 - i * g2 - j * s2)); n2 = max(n2, i * g1 + j * s1 + (n1 - i * g2 - j * s2) / b2 * b1 + (n1 - i * g2 - j * s2) % b2); } } } cout << n2 << endl; }
replace
14
19
14
45
TLE
p03008
C++
Runtime Error
#include <bits/stdc++.h> #include <stdlib.h> using namespace std; typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; typedef vector<double> Vec; typedef vector<Vec> Mat; typedef pair<ll, ll> P; typedef pair<double, ll> Pd; typedef pair<double, double> PD; typedef priority_queue<P, vector<P>, greater<P>> P_queue; typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue; const ll MOD = 998244353; const ll mod = 1000000007; const ll INF = 1e15; const double DEL = 1e-6; #define REP(i, a, b) for (int i = (int)a; i < (int)b; i++) #define rep(i, n) REP(i, 0, n) #define pb push_back #define mp make_pair #define ALL(a) a.begin(), a.end() #define SORT(a) sort(ALL(a)) #define U_ERASE(V) V.erase(unique(ALL(V)), V.end()); #define ADD(a, b) a = (a + b) % mod #define MUL(a, b) a = (a * b) % mod ll N; ll n[2][3]; ll dp[2500001]; int main() { cin >> N; rep(i, 2) rep(j, 3) cin >> n[i][j]; dp[N] = N; ll ans = 0; rep(i, 3) if (n[0][i] < n[1][i]) { for (int j = N; j >= n[0][i]; j--) dp[j - n[0][i]] = max(dp[j - n[0][i]], dp[j] + n[1][i] - n[0][i]); } rep(i, N + 1) ans = max(ans, dp[i]); rep(i, N + 1) dp[i] = 0; dp[ans] = ans; rep(i, 3) if (n[0][i] > n[1][i]) { for (int j = ans; j >= n[1][i]; j--) dp[j - n[1][i]] = max(dp[j - n[1][i]], dp[j] + n[0][i] - n[1][i]); } ll X = ans; rep(i, X + 1) ans = max(ans, dp[i]); cout << ans << endl; }
#include <bits/stdc++.h> #include <stdlib.h> using namespace std; typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; typedef vector<double> Vec; typedef vector<Vec> Mat; typedef pair<ll, ll> P; typedef pair<double, ll> Pd; typedef pair<double, double> PD; typedef priority_queue<P, vector<P>, greater<P>> P_queue; typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue; const ll MOD = 998244353; const ll mod = 1000000007; const ll INF = 1e15; const double DEL = 1e-6; #define REP(i, a, b) for (int i = (int)a; i < (int)b; i++) #define rep(i, n) REP(i, 0, n) #define pb push_back #define mp make_pair #define ALL(a) a.begin(), a.end() #define SORT(a) sort(ALL(a)) #define U_ERASE(V) V.erase(unique(ALL(V)), V.end()); #define ADD(a, b) a = (a + b) % mod #define MUL(a, b) a = (a * b) % mod ll N; ll n[2][3]; ll dp[25000001]; int main() { cin >> N; rep(i, 2) rep(j, 3) cin >> n[i][j]; dp[N] = N; ll ans = 0; rep(i, 3) if (n[0][i] < n[1][i]) { for (int j = N; j >= n[0][i]; j--) dp[j - n[0][i]] = max(dp[j - n[0][i]], dp[j] + n[1][i] - n[0][i]); } rep(i, N + 1) ans = max(ans, dp[i]); rep(i, N + 1) dp[i] = 0; dp[ans] = ans; rep(i, 3) if (n[0][i] > n[1][i]) { for (int j = ans; j >= n[1][i]; j--) dp[j - n[1][i]] = max(dp[j - n[1][i]], dp[j] + n[0][i] - n[1][i]); } ll X = ans; rep(i, X + 1) ans = max(ans, dp[i]); cout << ans << endl; }
replace
31
32
31
32
0
p03008
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < int(n); i++) #define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define reps(i, n) for (int i = 1; i <= int(n); i++) #define rreps(i, n) for (int i = int(n); i >= 1; i--) #define repc(i, n) for (int i = 0; i <= int(n); i++) #define rrepc(i, n) for (int i = int(n); i >= 0; i--) #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define repic(i, a, b) for (int i = int(a); i <= int(b); i++) #define each(x, y) for (auto &x : y) #define all(a) (a).begin(), (a).end() #define bit32(x) (1 << (x)) #define bit64(x) (1ll << (x)) using namespace std; using i32 = int; using i64 = long long; using u64 = unsigned long long; using f80 = long double; using vi32 = vector<i32>; using vi64 = vector<i64>; using vu64 = vector<u64>; using vf80 = vector<f80>; using vstr = vector<string>; inline void yes() { cout << "Yes" << '\n'; exit(0); } inline void no() { cout << "No" << '\n'; exit(0); } inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); } inline i64 lcm(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); return a / gcd(a, b) * b; } inline u64 xorshift() { static u64 x = 88172645463325252ull; x = x ^ (x << 7); return x = x ^ (x >> 9); } template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {}; template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {}; template <typename T> inline void amax(T &x, T y) { if (x < y) x = y; } template <typename T> inline void amin(T &x, T y) { if (x > y) x = y; } template <typename T> inline T exp(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } template <typename T> inline T bis(T ok, T ng, function<bool(T)> f, T eps = 1) { while (abs(ok - ng) > eps) { T mi = (ok + ng) / 2; (f(mi) ? ok : ng) = mi; } return ok; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { each(x, v) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; } void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; } void solve() { int N; cin >> N; int ga, sa, ba; int gb, sb, bb; cin >> ga >> sa >> ba >> gb >> sb >> bb; vector<i64> dp(10001); repc(i, N) { amax(dp[i], (i64)i); amax(dp[i + ga], dp[i] + gb); amax(dp[i + sa], dp[i] + sb); amax(dp[i + ba], dp[i] + bb); } i64 ma = 0; repc(i, N) amax(ma, dp[i]); dp.assign(ma + 1, 0); repc(i, ma) { amax(dp[i], (i64)i); amax(dp[i + gb], dp[i] + ga); amax(dp[i + sb], dp[i] + sa); amax(dp[i + bb], dp[i] + ba); } i64 ans = 0; repc(i, ma) amax(ans, dp[i]); cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < int(n); i++) #define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define reps(i, n) for (int i = 1; i <= int(n); i++) #define rreps(i, n) for (int i = int(n); i >= 1; i--) #define repc(i, n) for (int i = 0; i <= int(n); i++) #define rrepc(i, n) for (int i = int(n); i >= 0; i--) #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define repic(i, a, b) for (int i = int(a); i <= int(b); i++) #define each(x, y) for (auto &x : y) #define all(a) (a).begin(), (a).end() #define bit32(x) (1 << (x)) #define bit64(x) (1ll << (x)) using namespace std; using i32 = int; using i64 = long long; using u64 = unsigned long long; using f80 = long double; using vi32 = vector<i32>; using vi64 = vector<i64>; using vu64 = vector<u64>; using vf80 = vector<f80>; using vstr = vector<string>; inline void yes() { cout << "Yes" << '\n'; exit(0); } inline void no() { cout << "No" << '\n'; exit(0); } inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); } inline i64 lcm(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); return a / gcd(a, b) * b; } inline u64 xorshift() { static u64 x = 88172645463325252ull; x = x ^ (x << 7); return x = x ^ (x >> 9); } template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {}; template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {}; template <typename T> inline void amax(T &x, T y) { if (x < y) x = y; } template <typename T> inline void amin(T &x, T y) { if (x > y) x = y; } template <typename T> inline T exp(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } template <typename T> inline T bis(T ok, T ng, function<bool(T)> f, T eps = 1) { while (abs(ok - ng) > eps) { T mi = (ok + ng) / 2; (f(mi) ? ok : ng) = mi; } return ok; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { each(x, v) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; } void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; } void solve() { int N; cin >> N; int ga, sa, ba; int gb, sb, bb; cin >> ga >> sa >> ba >> gb >> sb >> bb; vector<i64> dp(10001); repc(i, N) { amax(dp[i], (i64)i); amax(dp[i + ga], dp[i] + gb); amax(dp[i + sa], dp[i] + sb); amax(dp[i + ba], dp[i] + bb); } i64 ma = 0; repc(i, N) amax(ma, dp[i]); dp.assign(ma + 1, 0); repc(i, ma) { amax(dp[i], (i64)i); if (i + gb <= ma) amax(dp[i + gb], dp[i] + ga); if (i + sb <= ma) amax(dp[i + sb], dp[i] + sa); if (i + bb <= ma) amax(dp[i + bb], dp[i] + ba); } i64 ans = 0; repc(i, ma) amax(ans, dp[i]); cout << ans << endl; }
replace
120
123
120
126
0
p03008
C++
Runtime Error
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i) #define rep(i, n) REP(i, 0, n) typedef long long ll; typedef pair<int, int> pint; typedef pair<ll, int> pli; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1000003; ll dp[5][5050], dp2[5][100010]; ll w[5], w2[5]; ll v[5], v2[5]; int main() { ll n; cin >> n; ll a[2][3]; rep(i, 2) { rep(j, 3) cin >> a[i][j]; } rep(i, 3) { w[i] = a[0][i]; v[i] = a[1][i]; w2[i] = a[1][i]; v2[i] = a[0][i]; } v[3] = 1; w[3] = 1; v2[3] = 1; w2[3] = 1; for (int i = 0; i < 4; i++) { for (int j = 0; j <= n; j++) { if (j < w[i]) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - w[i]] + v[i]); } } } ll m = dp[4][n]; for (int i = 0; i < 4; i++) { for (int j = 0; j <= m; j++) { if (j < w2[i]) { dp2[i + 1][j] = dp2[i][j]; } else { dp2[i + 1][j] = max(dp2[i][j], dp2[i + 1][j - w2[i]] + v2[i]); } } } cout << dp2[4][m] << endl; return 0; }
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i) #define rep(i, n) REP(i, 0, n) typedef long long ll; typedef pair<int, int> pint; typedef pair<ll, int> pli; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1000003; ll dp[5][5050], dp2[5][25000001]; ll w[5], w2[5]; ll v[5], v2[5]; int main() { ll n; cin >> n; ll a[2][3]; rep(i, 2) { rep(j, 3) cin >> a[i][j]; } rep(i, 3) { w[i] = a[0][i]; v[i] = a[1][i]; w2[i] = a[1][i]; v2[i] = a[0][i]; } v[3] = 1; w[3] = 1; v2[3] = 1; w2[3] = 1; for (int i = 0; i < 4; i++) { for (int j = 0; j <= n; j++) { if (j < w[i]) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - w[i]] + v[i]); } } } ll m = dp[4][n]; for (int i = 0; i < 4; i++) { for (int j = 0; j <= m; j++) { if (j < w2[i]) { dp2[i + 1][j] = dp2[i][j]; } else { dp2[i + 1][j] = max(dp2[i][j], dp2[i + 1][j - w2[i]] + v2[i]); } } } cout << dp2[4][m] << endl; return 0; }
replace
23
24
23
24
0
p03008
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "../../dump.hpp" #else #define dump(...) #endif #define int ll #define rep(i, n) for (int i = 0, i##_cond = (n); i < i##_cond; ++i) #define FOR(i, a, b) for (int i = (a), i##_cond = (b); i < i##_cond; ++i) #define ROF(i, a, b) for (int i = (a)-1, i##_cond = (b); i >= i##_cond; --i) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() // sortで降順 #define pb push_back #define fst first #define snd second using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>; using pii = pair<int, int>; constexpr ll inf = 1ll << 61; constexpr ll mod = 1e9 + 7; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> T add(const T &a, const T &b) { return (a + b) % mod; } signed main() { int n; cin >> n; vi a(3), b(3); rep(i, 3) cin >> a[i]; rep(i, 3) cin >> b[i]; rep(_, 2) { int m = n; rep(i, n + 1) rep(j, n - i + 1) { int k = n - i - j; // 金にi,銀にj,銅にk個使う int x = i / a[0], y = j / a[1], z = k / a[2]; int tmpn = n - x * a[0] - y * a[1] - z * a[2]; tmpn += x * b[0] + y * b[1] + z * b[2]; m = max(m, tmpn); } swap(a, b); n = m; } cout << n << endl; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "../../dump.hpp" #else #define dump(...) #endif #define int ll #define rep(i, n) for (int i = 0, i##_cond = (n); i < i##_cond; ++i) #define FOR(i, a, b) for (int i = (a), i##_cond = (b); i < i##_cond; ++i) #define ROF(i, a, b) for (int i = (a)-1, i##_cond = (b); i >= i##_cond; --i) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() // sortで降順 #define pb push_back #define fst first #define snd second using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>; using pii = pair<int, int>; constexpr ll inf = 1ll << 61; constexpr ll mod = 1e9 + 7; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> T add(const T &a, const T &b) { return (a + b) % mod; } signed main() { int n; cin >> n; vi a(3), b(3); rep(i, 3) cin >> a[i]; rep(i, 3) cin >> b[i]; rep(_, 2) { int m = n; vi c, d; rep(i, 3) if (a[i] < b[i]) { c.push_back(a[i]); d.push_back(b[i]); } if (c.size() == 3) { rep(i, n + 1) rep(j, n - i + 1) { int k = n - i - j; // 金にi,銀にj,銅にk個使う int x = i / c[0], y = j / c[1], z = k / c[2]; int tmpn = n - x * c[0] - y * c[1] - z * c[2]; tmpn += x * d[0] + y * d[1] + z * d[2]; m = max(m, tmpn); } } else if (c.size() == 2) { rep(i, n + 1) { int j = n - i; // 金にi,銀にj個使う int x = i / c[0], y = j / c[1]; int tmpn = n - x * c[0] - y * c[1]; tmpn += x * d[0] + y * d[1]; m = max(m, tmpn); } } else if (c.size() == 1) { int i = n; // 金にi個使う int x = i / c[0]; int tmpn = n - x * c[0]; tmpn += x * d[0]; m = max(m, tmpn); } swap(a, b); n = m; } cout << n << endl; }
replace
42
48
42
71
TLE
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; const ull mod = 1e9 + 7; #define REP(i, n) for (int i = 0; i < (int)n; ++i) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> v) { os << "(" << v.first << ", " << v.second << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> v) { for (int i = 0; i < v.size(); i++) { if (i > 0) { os << " "; } os << v[i]; } return os; } template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) { for (int i = 0; i < v.size(); i++) { if (i > 0) { os << endl; } os << v[i]; } return os; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll N; vector<ll> AS(4, 0), BS(4, 0); cin >> N >> AS[0] >> AS[1] >> AS[2] >> BS[0] >> BS[1] >> BS[2]; AS[3] = 1; BS[3] = 1; // const ll ma = 5000*5000 + 10; const ll ma = 10000000; ll dp[5][ma]; REP(i, 5) REP(j, ma) dp[i][j] = 0; // former ll limit = N; REP(i, 4) { REP(j, limit + 1) { if (j < AS[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - AS[i]] + BS[i]); } } ll M = dp[4][limit]; // latter REP(i, 5) REP(j, ma) dp[i][j] = 0; limit = M; REP(i, 4) { REP(j, limit + 1) { if (j < BS[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - BS[i]] + AS[i]); } } cout << dp[4][limit] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; const ull mod = 1e9 + 7; #define REP(i, n) for (int i = 0; i < (int)n; ++i) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> v) { os << "(" << v.first << ", " << v.second << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> v) { for (int i = 0; i < v.size(); i++) { if (i > 0) { os << " "; } os << v[i]; } return os; } template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) { for (int i = 0; i < v.size(); i++) { if (i > 0) { os << endl; } os << v[i]; } return os; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll N; vector<ll> AS(4, 0), BS(4, 0); cin >> N >> AS[0] >> AS[1] >> AS[2] >> BS[0] >> BS[1] >> BS[2]; AS[3] = 1; BS[3] = 1; const ll ma = 5000 * 5000 + 10; // const ll ma = 10000; static ll dp[5][ma]; REP(i, 5) REP(j, ma) dp[i][j] = 0; // former ll limit = N; REP(i, 4) { REP(j, limit + 1) { if (j < AS[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - AS[i]] + BS[i]); } } ll M = dp[4][limit]; // latter REP(i, 5) REP(j, ma) dp[i][j] = 0; limit = M; REP(i, 4) { REP(j, limit + 1) { if (j < BS[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - BS[i]] + AS[i]); } } cout << dp[4][limit] << endl; return 0; }
replace
51
54
51
54
-11
p03008
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <time.h> #define cl_has_dh(x...) \ cl_get_th_100(x, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) #define cl_get_th_100( \ _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, \ _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, \ _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, \ _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, \ _62, _63, _64, _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, \ _77, _78, _79, _80, _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91, \ _92, _93, _94, _95, _96, _97, _98, _99, _100, x...) \ _100 #if 0 #define cl_blzk cl_blzk0 #else #define cl_blzk cl_blzk3 #endif #define cl_blzk0(x...) cl_blzk1(cl_blzk1(cl_blzk1(x))) #define cl_blzk1(x...) cl_blzk2(cl_blzk2(cl_blzk2(x))) #define cl_blzk2(x...) cl_blzk3(cl_blzk3(cl_blzk3(x))) #define cl_blzk3(x...) cl_blzk4(cl_blzk4(cl_blzk4(x))) #define cl_blzk4(x...) cl_blzk5(cl_blzk5(cl_blzk5(x))) #define cl_blzk5(x...) cl_blzk6(cl_blzk6(cl_blzk6(x))) #define cl_blzk6(x...) cl_blzk7(cl_blzk7(cl_blzk7(x))) #define cl_blzk7(x...) x #define cl_lj_(a, b) a##b #define cl_lj(a, b) cl_lj_(a, b) #define cl_not(x) cl_lj(cl_not_, x) #define cl_not_0 1 #define cl_not_1 0 #define cl_and(x, y) cl_lj(cl_and_, cl_lj(x, y)) #define cl_and_00 0 #define cl_and_01 0 #define cl_and_10 0 #define cl_and_11 1 #define cl_bif(x) cl_lj(cl_bif_, x) #define cl_bif_0(x...) cl_bif_0_ #define cl_bif_0_(x...) x #define cl_bif_1(x...) x cl_bif_1_ #define cl_bif_1_(x...) #define cl_nn(x...) #define cl_zzk(x...) x #define cl_zkk(x...) x #define cl_after(x...) cl_zzk cl_nn()(x) #define cl_txt(x...) #x #define cl_show(x...) puts(cl_txt(x)) #define cl_is_nn(x...) cl_is_nnmm(x) #define cl_x_to_dh(x...) , #define cl_is_nnmm(x...) \ cl_and(cl_and(cl_not(cl_has_dh(cl_x_to_dh x)), cl_has_dh(cl_x_to_dh x())), \ cl_not(cl_has_dh(x()))) typedef long long ll; typedef long double ld; #define gs1(x, y, z) \ (quick_IO::very_important_cs_scw = y, quick_IO::very_important_cs_xsd = z, x) #define gs0(x, y) (quick_IO::very_important_cs_scw = y, x) #define gs(x, y...) cl_lj(gs, cl_has_dh(y))(x, y) #define read_cz(cs, _1, _01) \ cl_bif(_01)((+)quick_IO::whreadm(_1) cl_zkk)(() cl_nn) #define write_cz(cs, _1, _01) \ cl_bif(_01)((, )quick_IO::whwritem(_1) cl_zkk)(() cl_nn) #define read(x...) (cl_blzk(cl_nn cl_d(read_cz, , x)())) #define write(x...) cl_blzk(cl_nn cl_d(write_cz, , x)()) #define cl_pp(cs, x...) cl_blzk(cl_d(cl_pp_cz, cs, x)) #define cl_pp_cz(cs, _1, _01) \ cl_bif(_01)(template <> struct cs<_1> { typedef _1 Type; };)() #define cl_d(cz, cs, x...) cl_ds(cz, cs, x) #define cl_ds(cz, cs, x...) cl_lj(cl_ds_, cl_is_nn(x))(cz, cs, x) #define cl_ds_1(cz, cs, _1, x...) cz(cs, _1, 0) #define cl_ds_0(cz, cs, _1, x...) cz(cs, _1, 1) cl_after(cl_ds_cpy)()(cz, cs, x) #define cl_ds_cpy() cl_ds #define isdigit(x) (x >= '0' && x <= '9') #define pp(a, b) \ template <> struct a<b> { \ typedef b Type; \ }; namespace quick_IO { inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; } ll very_important_cs_scw = 0, very_important_cs_xsd = 6, cs = 0; inline char gc() { return getchar(); } inline void pc(char c) { cs ? fprintf(stderr, "%c", c) : putchar(c); } template <typename T> struct Checker1; cl_pp(Checker1, bool, int, short, long, long long, unsigned int, unsigned long, unsigned short, unsigned long long, double, long double, float) template <typename T> struct Checker2; cl_pp(Checker2, char) char lastc = -2; template <typename T> inline ll readm(typename Checker1<T>::Type &x) { register double tmp = 1; register bool sign = 0; x = 0; register char ch = lastc == -2 ? gc() : lastc; for (; !isdigit(ch); ch = gc()) if (ch == '-') sign = 1; else if (ch == EOF) return 0; for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - '0'); if (ch == '.') for (ch = gc(); isdigit(ch); ch = gc()) tmp /= 10.0, x += tmp * (ch - '0'); if (sign) x = -x; lastc = ch; return 1; } template <typename T> inline ll readm(typename Checker2<T>::Type &c) { for (c = lastc == -2 ? gc() : lastc; blank(c); c = gc()) ; if (c == EOF) return 0; lastc = -2; return 1; } inline ll readm(char *s) { register char ch = lastc == -2 ? gc() : lastc; for (; blank(ch) && ~ch; ch = gc()) if (ch == EOF) return 0; for (; ~ch && !blank(ch); ch = gc()) *s++ = ch; *s = 0; lastc = -2; return 1; } void writellm(ll c, ll x) { if (c < 0) pc('-'), c = -c, x--; if (c < 10) return ({ for (ll i = 1; i <= x - 1; i++) pc(' '); pc(c ^ 48); void(0); }); writellm(c / 10, x - 1); pc((c % 10) ^ 48); } template <typename T> ll whreadm(T *x) { return readm(x); }; template <typename T> ll whreadm(T &x) { return readm<T>(x); }; #define x very_important_cs_xsd #define y very_important_cs_scw template <typename T> void writem(typename Checker2<T>::Type c) { for (ll i = 1; i <= y - 1; i++) pc(' '); pc(c); } template <typename T> struct Checkers1; cl_pp(Checkers1, bool, int, short, long, long long, unsigned int, unsigned long, unsigned long long) template <typename T> inline void ycl(typename Checkers1<T>::Type c) { x = 0; } template <typename T> void ycl(T &c) { long double ys = 0.5; for (ll i = 1; i <= x; i++) ys /= 10; c += ys; } template <typename T> void writem(typename Checker1<T>::Type c) { if (c < 0) putchar('-'), c = -c; ycl<T>(c); writellm(ll(c), y - (x ? x + 1 : 0)); c -= ll(c); if (!x) return; pc('.'); for (ll i = 1; i <= x; i++) c *= 10, pc(int(c) ^ 48), c -= char(c); } void writem(const char *c) { ll len = std::strlen(c); for (ll i = 1; i <= y - len; i++) pc(' '); while (*c) pc(*c++); } #undef x #undef y template <typename T> void whwritem(T *x) { writem(x); very_important_cs_scw = 0, very_important_cs_xsd = 6; }; template <typename T> void whwritem(T x) { writem<T>(x); very_important_cs_scw = 0, very_important_cs_xsd = 6; }; } // namespace quick_IO #define IO_debug 0 #define IO(x) \ freopen(#x ".in", "r", stdin) \ cl_bif(IO_debug)()(; freopen(#x ".out", "w", stdout)) #define bla , (((zjhyyds))), #define cl_del_3_kh(x...) cl_bif(cl_has_3_kh(x))(cl_del_2_kh x)(x) #define cl_del_2_kh(x...) cl_bif(cl_has_2_kh(x))(cl_del_1_kh x)(x) #define cl_del_1_kh(x...) cl_bif(cl_has_1_kh(x))(cl_zkk x)(x) #define cl_has_3_kh(x...) cl_bif(cl_has_1_kh(x))(cl_has_2_kh x)(0) #define cl_has_2_kh(x...) cl_bif(cl_has_1_kh(x))(cl_has_1_kh x)(0) #define cl_has_1_kh(x...) cl_is_nn(cl_nn x) #define is_zjhyyds(x) cl_bif(cl_has_3_kh(x))(cl_is_nn(is_zjhyyds_(x)))(0) #define is_zjhyyds_(x) cl_lj(is_zjhyyds_s, cl_del_3_kh(x)) #define is_zjhyyds_szjhyyds #define cl_rir_dh(cs, x...) cl_zzk(cl_nn cl_rir(cs, (, )x cl_zzk)()) #define cl_rir(cs, x...) cl_blzk(cl_d(cl_rir_cz, (x), cl_zkk cs)) #define cl_rir_cz(cs, _1, _01) \ cl_bif(_01)(cl_after(cl_hg_cpy)()(_1, cl_zkk cs))() #define cl_hg_cpy() cl_hg #define cl_hg(cs, x...) cl_nn cl_after(cl_ds_cpy)()(cl_hg_cz, cs, x)() #define cl_hg_cz(cs, _1, _01) \ cl_bif(_01)(cl_bif(is_zjhyyds(_1))(() cs cl_nn)((, )_1 cl_zkk))(() cl_nn) ll readll(void) { ll x = 0, w = 1; char c = getchar(); for (; c < '0' || c > '9'; (c - '-') || (w = -w), c = getchar()) ; for (; c >= '0' && c <= '9'; x = (x << 1) + (x << 3) + (c ^ 48), c = getchar()) ; return x * w; } #define cl_rill(x...) cl_blzk(cl_nn cl_d(cl_rill_cz, , x)()) #define rill(x...) cl_rill(x) #define cl_rill_cz(cs, _1, _01) cl_bif(_01)((, )_1 = readll() cl_zkk)(() cl_nn) #define writeln(x...) cl_bif(cl_is_nn(x))(write('\n'))(write(x, '\n')) #define rep(cs, low, up, x...) \ rfor(ll cs = low; cs <= up; cl_bif(cl_is_nn(x))(++cs)(x)) #define frh(cs, cz, low, up, x...) \ rfor(ll cs, cl_lj(WCR_yyds_, cs) = low; \ cs = cz[cl_lj(WCR_yyds_, cs)], cl_lj(WCR_yyds_, cs) <= up; \ cl_lj(WCR_yyds_, cs)++) cl_bif(cl_is_nn(x))()(if (x)) #define per(cs, low, up, x...) \ rfor(ll cs = low; cs >= up; cl_bif(cl_is_nn(x))(--cs)(x)) #define cl_hsdd(hs, x...) cl_blzk(cl_bif(cl_is_nn(x))()(cl_hsdd_(hs, x))) #define cl_hsdd_(hs, _1, x...) cl_lj(cl_hsdd_, cl_is_nn(x))(hs, _1, x) #define cl_hsdd_1(hs, _1, x...) _1 #define cl_hsdd_0(hs, _1, x...) hs(_1, cl_after(cl_hsdd_cpy)()(hs, x)) #define cl_hsdd_cpy() cl_hsdd_ #define max(x...) cl_hsdd(std::max, x) #define min(x...) cl_hsdd(std::min, x) #define rfor(x...) for (register x) #define ssscc 1 #if 1 #define debug(x...) quick_IO::cs = ssscc, write(x), quick_IO::cs = 0 #define debugln(x...) quick_IO::cs = ssscc, writeln(x), quick_IO::cs = 0 #else #define debug(x...) quick_IO::cs = 0 #define debugln(x...) quick_IO::cs = 0 #endif #define fin(a, b, c) \ rfor(ll a = cl_bif(cl_is_nn(c))(b)(c); a >= 0; a && (--a &= b) || (a = -1)) ll n, f[27334421]; void calc(ll a, ll b) { rep(i, a, n) f[i] = max(f[i], f[i - a] + b); } int main() { IO(D); rill(n); ll rill(ga, sa, ba); ll rill(gb, sb, bb); calc(1, 1); calc(ga, gb); calc(sa, sb); calc(ba, bb); n = f[n]; rep(i, 1, n) f[i] = i; calc(gb, ga); calc(sb, sa); calc(bb, ba); writeln(f[n]); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <time.h> #define cl_has_dh(x...) \ cl_get_th_100(x, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) #define cl_get_th_100( \ _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, \ _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, \ _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, \ _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, \ _62, _63, _64, _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, \ _77, _78, _79, _80, _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91, \ _92, _93, _94, _95, _96, _97, _98, _99, _100, x...) \ _100 #if 0 #define cl_blzk cl_blzk0 #else #define cl_blzk cl_blzk3 #endif #define cl_blzk0(x...) cl_blzk1(cl_blzk1(cl_blzk1(x))) #define cl_blzk1(x...) cl_blzk2(cl_blzk2(cl_blzk2(x))) #define cl_blzk2(x...) cl_blzk3(cl_blzk3(cl_blzk3(x))) #define cl_blzk3(x...) cl_blzk4(cl_blzk4(cl_blzk4(x))) #define cl_blzk4(x...) cl_blzk5(cl_blzk5(cl_blzk5(x))) #define cl_blzk5(x...) cl_blzk6(cl_blzk6(cl_blzk6(x))) #define cl_blzk6(x...) cl_blzk7(cl_blzk7(cl_blzk7(x))) #define cl_blzk7(x...) x #define cl_lj_(a, b) a##b #define cl_lj(a, b) cl_lj_(a, b) #define cl_not(x) cl_lj(cl_not_, x) #define cl_not_0 1 #define cl_not_1 0 #define cl_and(x, y) cl_lj(cl_and_, cl_lj(x, y)) #define cl_and_00 0 #define cl_and_01 0 #define cl_and_10 0 #define cl_and_11 1 #define cl_bif(x) cl_lj(cl_bif_, x) #define cl_bif_0(x...) cl_bif_0_ #define cl_bif_0_(x...) x #define cl_bif_1(x...) x cl_bif_1_ #define cl_bif_1_(x...) #define cl_nn(x...) #define cl_zzk(x...) x #define cl_zkk(x...) x #define cl_after(x...) cl_zzk cl_nn()(x) #define cl_txt(x...) #x #define cl_show(x...) puts(cl_txt(x)) #define cl_is_nn(x...) cl_is_nnmm(x) #define cl_x_to_dh(x...) , #define cl_is_nnmm(x...) \ cl_and(cl_and(cl_not(cl_has_dh(cl_x_to_dh x)), cl_has_dh(cl_x_to_dh x())), \ cl_not(cl_has_dh(x()))) typedef long long ll; typedef long double ld; #define gs1(x, y, z) \ (quick_IO::very_important_cs_scw = y, quick_IO::very_important_cs_xsd = z, x) #define gs0(x, y) (quick_IO::very_important_cs_scw = y, x) #define gs(x, y...) cl_lj(gs, cl_has_dh(y))(x, y) #define read_cz(cs, _1, _01) \ cl_bif(_01)((+)quick_IO::whreadm(_1) cl_zkk)(() cl_nn) #define write_cz(cs, _1, _01) \ cl_bif(_01)((, )quick_IO::whwritem(_1) cl_zkk)(() cl_nn) #define read(x...) (cl_blzk(cl_nn cl_d(read_cz, , x)())) #define write(x...) cl_blzk(cl_nn cl_d(write_cz, , x)()) #define cl_pp(cs, x...) cl_blzk(cl_d(cl_pp_cz, cs, x)) #define cl_pp_cz(cs, _1, _01) \ cl_bif(_01)(template <> struct cs<_1> { typedef _1 Type; };)() #define cl_d(cz, cs, x...) cl_ds(cz, cs, x) #define cl_ds(cz, cs, x...) cl_lj(cl_ds_, cl_is_nn(x))(cz, cs, x) #define cl_ds_1(cz, cs, _1, x...) cz(cs, _1, 0) #define cl_ds_0(cz, cs, _1, x...) cz(cs, _1, 1) cl_after(cl_ds_cpy)()(cz, cs, x) #define cl_ds_cpy() cl_ds #define isdigit(x) (x >= '0' && x <= '9') #define pp(a, b) \ template <> struct a<b> { \ typedef b Type; \ }; namespace quick_IO { inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; } ll very_important_cs_scw = 0, very_important_cs_xsd = 6, cs = 0; inline char gc() { return getchar(); } inline void pc(char c) { cs ? fprintf(stderr, "%c", c) : putchar(c); } template <typename T> struct Checker1; cl_pp(Checker1, bool, int, short, long, long long, unsigned int, unsigned long, unsigned short, unsigned long long, double, long double, float) template <typename T> struct Checker2; cl_pp(Checker2, char) char lastc = -2; template <typename T> inline ll readm(typename Checker1<T>::Type &x) { register double tmp = 1; register bool sign = 0; x = 0; register char ch = lastc == -2 ? gc() : lastc; for (; !isdigit(ch); ch = gc()) if (ch == '-') sign = 1; else if (ch == EOF) return 0; for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - '0'); if (ch == '.') for (ch = gc(); isdigit(ch); ch = gc()) tmp /= 10.0, x += tmp * (ch - '0'); if (sign) x = -x; lastc = ch; return 1; } template <typename T> inline ll readm(typename Checker2<T>::Type &c) { for (c = lastc == -2 ? gc() : lastc; blank(c); c = gc()) ; if (c == EOF) return 0; lastc = -2; return 1; } inline ll readm(char *s) { register char ch = lastc == -2 ? gc() : lastc; for (; blank(ch) && ~ch; ch = gc()) if (ch == EOF) return 0; for (; ~ch && !blank(ch); ch = gc()) *s++ = ch; *s = 0; lastc = -2; return 1; } void writellm(ll c, ll x) { if (c < 0) pc('-'), c = -c, x--; if (c < 10) return ({ for (ll i = 1; i <= x - 1; i++) pc(' '); pc(c ^ 48); void(0); }); writellm(c / 10, x - 1); pc((c % 10) ^ 48); } template <typename T> ll whreadm(T *x) { return readm(x); }; template <typename T> ll whreadm(T &x) { return readm<T>(x); }; #define x very_important_cs_xsd #define y very_important_cs_scw template <typename T> void writem(typename Checker2<T>::Type c) { for (ll i = 1; i <= y - 1; i++) pc(' '); pc(c); } template <typename T> struct Checkers1; cl_pp(Checkers1, bool, int, short, long, long long, unsigned int, unsigned long, unsigned long long) template <typename T> inline void ycl(typename Checkers1<T>::Type c) { x = 0; } template <typename T> void ycl(T &c) { long double ys = 0.5; for (ll i = 1; i <= x; i++) ys /= 10; c += ys; } template <typename T> void writem(typename Checker1<T>::Type c) { if (c < 0) putchar('-'), c = -c; ycl<T>(c); writellm(ll(c), y - (x ? x + 1 : 0)); c -= ll(c); if (!x) return; pc('.'); for (ll i = 1; i <= x; i++) c *= 10, pc(int(c) ^ 48), c -= char(c); } void writem(const char *c) { ll len = std::strlen(c); for (ll i = 1; i <= y - len; i++) pc(' '); while (*c) pc(*c++); } #undef x #undef y template <typename T> void whwritem(T *x) { writem(x); very_important_cs_scw = 0, very_important_cs_xsd = 6; }; template <typename T> void whwritem(T x) { writem<T>(x); very_important_cs_scw = 0, very_important_cs_xsd = 6; }; } // namespace quick_IO #define IO_debug 0 #define IO(x) \ freopen(#x ".in", "r", stdin) \ cl_bif(IO_debug)()(; freopen(#x ".out", "w", stdout)) #define bla , (((zjhyyds))), #define cl_del_3_kh(x...) cl_bif(cl_has_3_kh(x))(cl_del_2_kh x)(x) #define cl_del_2_kh(x...) cl_bif(cl_has_2_kh(x))(cl_del_1_kh x)(x) #define cl_del_1_kh(x...) cl_bif(cl_has_1_kh(x))(cl_zkk x)(x) #define cl_has_3_kh(x...) cl_bif(cl_has_1_kh(x))(cl_has_2_kh x)(0) #define cl_has_2_kh(x...) cl_bif(cl_has_1_kh(x))(cl_has_1_kh x)(0) #define cl_has_1_kh(x...) cl_is_nn(cl_nn x) #define is_zjhyyds(x) cl_bif(cl_has_3_kh(x))(cl_is_nn(is_zjhyyds_(x)))(0) #define is_zjhyyds_(x) cl_lj(is_zjhyyds_s, cl_del_3_kh(x)) #define is_zjhyyds_szjhyyds #define cl_rir_dh(cs, x...) cl_zzk(cl_nn cl_rir(cs, (, )x cl_zzk)()) #define cl_rir(cs, x...) cl_blzk(cl_d(cl_rir_cz, (x), cl_zkk cs)) #define cl_rir_cz(cs, _1, _01) \ cl_bif(_01)(cl_after(cl_hg_cpy)()(_1, cl_zkk cs))() #define cl_hg_cpy() cl_hg #define cl_hg(cs, x...) cl_nn cl_after(cl_ds_cpy)()(cl_hg_cz, cs, x)() #define cl_hg_cz(cs, _1, _01) \ cl_bif(_01)(cl_bif(is_zjhyyds(_1))(() cs cl_nn)((, )_1 cl_zkk))(() cl_nn) ll readll(void) { ll x = 0, w = 1; char c = getchar(); for (; c < '0' || c > '9'; (c - '-') || (w = -w), c = getchar()) ; for (; c >= '0' && c <= '9'; x = (x << 1) + (x << 3) + (c ^ 48), c = getchar()) ; return x * w; } #define cl_rill(x...) cl_blzk(cl_nn cl_d(cl_rill_cz, , x)()) #define rill(x...) cl_rill(x) #define cl_rill_cz(cs, _1, _01) cl_bif(_01)((, )_1 = readll() cl_zkk)(() cl_nn) #define writeln(x...) cl_bif(cl_is_nn(x))(write('\n'))(write(x, '\n')) #define rep(cs, low, up, x...) \ rfor(ll cs = low; cs <= up; cl_bif(cl_is_nn(x))(++cs)(x)) #define frh(cs, cz, low, up, x...) \ rfor(ll cs, cl_lj(WCR_yyds_, cs) = low; \ cs = cz[cl_lj(WCR_yyds_, cs)], cl_lj(WCR_yyds_, cs) <= up; \ cl_lj(WCR_yyds_, cs)++) cl_bif(cl_is_nn(x))()(if (x)) #define per(cs, low, up, x...) \ rfor(ll cs = low; cs >= up; cl_bif(cl_is_nn(x))(--cs)(x)) #define cl_hsdd(hs, x...) cl_blzk(cl_bif(cl_is_nn(x))()(cl_hsdd_(hs, x))) #define cl_hsdd_(hs, _1, x...) cl_lj(cl_hsdd_, cl_is_nn(x))(hs, _1, x) #define cl_hsdd_1(hs, _1, x...) _1 #define cl_hsdd_0(hs, _1, x...) hs(_1, cl_after(cl_hsdd_cpy)()(hs, x)) #define cl_hsdd_cpy() cl_hsdd_ #define max(x...) cl_hsdd(std::max, x) #define min(x...) cl_hsdd(std::min, x) #define rfor(x...) for (register x) #define ssscc 1 #if 1 #define debug(x...) quick_IO::cs = ssscc, write(x), quick_IO::cs = 0 #define debugln(x...) quick_IO::cs = ssscc, writeln(x), quick_IO::cs = 0 #else #define debug(x...) quick_IO::cs = 0 #define debugln(x...) quick_IO::cs = 0 #endif #define fin(a, b, c) \ rfor(ll a = cl_bif(cl_is_nn(c))(b)(c); a >= 0; a && (--a &= b) || (a = -1)) ll n, f[27334421]; void calc(ll a, ll b) { rep(i, a, n) f[i] = max(f[i], f[i - a] + b); } int main() { // IO(D); rill(n); ll rill(ga, sa, ba); ll rill(gb, sb, bb); calc(1, 1); calc(ga, gb); calc(sa, sb); calc(ba, bb); n = f[n]; rep(i, 1, n) f[i] = i; calc(gb, ga); calc(sb, sa); calc(bb, ba); writeln(f[n]); return 0; }
replace
269
270
269
270
TLE
p03008
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; #define int long long int MOD = 1000000007; signed main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; int A[2][3]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cin >> A[i][j]; } } int mx = 0; int s; for (int z = 0; z < 2; z++) { mx = 0; int tt = 1 + (N / A[z][0]); for (int i = 0; i <= tt; i++) { for (int j = 0; j <= N; j++) { int r = N - i * A[z][0] - j * A[z][1]; if (r < 0) { break; } else { s = A[1 - z][0] * i + A[1 - z][1] * j; mx = max(mx, r + s); int k = r / A[z][2]; r -= k * A[z][2]; mx = max(mx, r + s + A[1 - z][2] * k); } } } N = mx; } cout << N << endl; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; #define int long long int MOD = 1000000007; signed main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; int A[2][3]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cin >> A[i][j]; } } int mx = 0; int s; for (int z = 0; z < 2; z++) { mx = 0; int tt = 1 + (N / A[z][0]); for (int i = 0; i <= tt; i++) { if (i == 5001) i = max(i, tt - 5001); int zz = 1 + ((N - i * A[z][0]) / A[z][1]); for (int j = 0; j <= zz; j++) { if (j == 5001) j = max(j, zz - 5001); int r = N - i * A[z][0] - j * A[z][1]; if (r < 0) { break; } else { s = A[1 - z][0] * i + A[1 - z][1] * j; mx = max(mx, r + s); int k = r / A[z][2]; r -= k * A[z][2]; mx = max(mx, r + s + A[1 - z][2] * k); } } } N = mx; } cout << N << endl; }
replace
31
32
31
39
TLE
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for (int i = 0; i < int(n); i++) #define FOR(i, m, n) for (int i = int(m); i < int(n); i++) #define ALL(obj) (obj).begin(), (obj).end() #define VI vector<int> #define VLL vector<long long> #define VVI vector<vector<int>> #define VVLL vector<vector<long long>> #define VC vector<char> #define VS vector<string> #define VVC vector<vector<char>> #define VB vector<bool> #define VVB vector<vector<bool>> #define fore(i, a) for (auto &i : a) typedef pair<int, int> P; template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } const int INF = 1 << 30; const ll INFL = 1LL << 60; const ll mod = 1000000007; int main() { int n; vector<pair<double, pair<ll, ll>>> p; cin >> n; VLL v(6); vector<double> vv(6); REP(i, 6) { cin >> v[i]; vv[i] = v[i]; } if (vv[0] != vv[3]) p.push_back({vv[0] / vv[3], {vv[0], vv[3]}}); if (vv[1] != vv[4]) p.push_back({vv[1] / vv[4], {vv[1], vv[4]}}); if (vv[2] != vv[5]) p.push_back({vv[2] / vv[5], {vv[2], vv[5]}}); if (p.size() == 0) { cout << n << endl; return 0; } if (p.size() == 1) { ll a = p[0].second.second; ll b = p[0].second.first; if (a < b) swap(a, b); ll k = n / b; n %= b; n += k * a; cout << n << endl; return 0; } sort(ALL(p)); if (p.size() == 2) { if (p[0].first < 1 && p[1].first > 1) { ll a = p[0].second.second; ll b = p[0].second.first; ll k = n / b; n %= b; n += k * a; a = p[1].second.first; b = p[1].second.second; k = n / b; n %= b; n += k * a; cout << n << endl; return 0; } assert(n == 0); ll a = p[0].second.second; ll b = p[0].second.first; if (a < b) swap(a, b); ll c = p[1].second.second; ll d = p[1].second.first; if (c < d) swap(c, d); ll ans = 0; REP(i, n + 1) { REP(j, n + 1) { if (b * i + d * j > n) break; ll cur = n + (a - b) * i + (c - d) * j; ans = max(ans, cur); } } cout << ans << endl; return 0; } if (p[0].first > 1 || p[2].first < 1) { ll a = p[0].second.second; ll b = p[0].second.first; if (a < b) swap(a, b); ll c = p[1].second.second; ll d = p[1].second.first; if (c < d) swap(c, d); ll e = p[2].second.second; ll f = p[2].second.first; if (e < f) swap(e, f); ll ans = 0; REP(i, n + 1) { REP(j, n + 1) { if (b * i + d * j > n) break; ll k = (n - b * i - d * j) / f; ll cur = n + (a - b) * i + (c - d) * j + (e - f) * k; ans = max(ans, cur); } } cout << ans << endl; return 0; } //========================================================= if (p[1].first < 1) { ll a = p[0].second.second; ll b = p[0].second.first; if (a < b) swap(a, b); ll c = p[1].second.second; ll d = p[1].second.first; if (c < d) swap(c, d); ll ans = 0; REP(i, n + 50) { REP(j, n + 50) { if (((b * i) + (d * j)) > n) break; ll cur = n + (a - b) * i + (c - d) * j; ans = max(ans, cur); } } ll ansa = 0; ll e = p[2].second.second; ll f = p[2].second.first; if (e < f) swap(e, f); ll k = ans / f; ansa = ans + (e - f) * k; cout << ansa << endl; return 0; } //===================================================== if (p[1].first > 1) { ll a = p[0].second.second; ll b = p[0].second.first; if (a < b) swap(a, b); ll k = n / b; n %= b; n += k * a; ll c = p[1].second.second; ll d = p[1].second.first; if (c < d) swap(c, d); ll e = p[2].second.second; ll f = p[2].second.first; if (e < f) swap(e, f); ll ans = 0; REP(i, n + 1) { if (d * i > n) break; ll j = (n - d * i) / f; ll cur = n + (c - d) * i + (e - f) * j; ans = max(ans, cur); } cout << ans << endl; return 0; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for (int i = 0; i < int(n); i++) #define FOR(i, m, n) for (int i = int(m); i < int(n); i++) #define ALL(obj) (obj).begin(), (obj).end() #define VI vector<int> #define VLL vector<long long> #define VVI vector<vector<int>> #define VVLL vector<vector<long long>> #define VC vector<char> #define VS vector<string> #define VVC vector<vector<char>> #define VB vector<bool> #define VVB vector<vector<bool>> #define fore(i, a) for (auto &i : a) typedef pair<int, int> P; template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } const int INF = 1 << 30; const ll INFL = 1LL << 60; const ll mod = 1000000007; int main() { int n; vector<pair<double, pair<ll, ll>>> p; cin >> n; VLL v(6); vector<double> vv(6); REP(i, 6) { cin >> v[i]; vv[i] = v[i]; } if (vv[0] != vv[3]) p.push_back({vv[0] / vv[3], {vv[0], vv[3]}}); if (vv[1] != vv[4]) p.push_back({vv[1] / vv[4], {vv[1], vv[4]}}); if (vv[2] != vv[5]) p.push_back({vv[2] / vv[5], {vv[2], vv[5]}}); if (p.size() == 0) { cout << n << endl; return 0; } if (p.size() == 1) { ll a = p[0].second.second; ll b = p[0].second.first; if (a < b) swap(a, b); ll k = n / b; n %= b; n += k * a; cout << n << endl; return 0; } sort(ALL(p)); if (p.size() == 2) { if (p[0].first < 1 && p[1].first > 1) { ll a = p[0].second.second; ll b = p[0].second.first; ll k = n / b; n %= b; n += k * a; a = p[1].second.first; b = p[1].second.second; k = n / b; n %= b; n += k * a; cout << n << endl; return 0; } ll a = p[0].second.second; ll b = p[0].second.first; if (a < b) swap(a, b); ll c = p[1].second.second; ll d = p[1].second.first; if (c < d) swap(c, d); ll ans = 0; REP(i, n + 1) { REP(j, n + 1) { if (b * i + d * j > n) break; ll cur = n + (a - b) * i + (c - d) * j; ans = max(ans, cur); } } cout << ans << endl; return 0; } if (p[0].first > 1 || p[2].first < 1) { ll a = p[0].second.second; ll b = p[0].second.first; if (a < b) swap(a, b); ll c = p[1].second.second; ll d = p[1].second.first; if (c < d) swap(c, d); ll e = p[2].second.second; ll f = p[2].second.first; if (e < f) swap(e, f); ll ans = 0; REP(i, n + 1) { REP(j, n + 1) { if (b * i + d * j > n) break; ll k = (n - b * i - d * j) / f; ll cur = n + (a - b) * i + (c - d) * j + (e - f) * k; ans = max(ans, cur); } } cout << ans << endl; return 0; } //========================================================= if (p[1].first < 1) { ll a = p[0].second.second; ll b = p[0].second.first; if (a < b) swap(a, b); ll c = p[1].second.second; ll d = p[1].second.first; if (c < d) swap(c, d); ll ans = 0; REP(i, n + 50) { REP(j, n + 50) { if (((b * i) + (d * j)) > n) break; ll cur = n + (a - b) * i + (c - d) * j; ans = max(ans, cur); } } ll ansa = 0; ll e = p[2].second.second; ll f = p[2].second.first; if (e < f) swap(e, f); ll k = ans / f; ansa = ans + (e - f) * k; cout << ansa << endl; return 0; } //===================================================== if (p[1].first > 1) { ll a = p[0].second.second; ll b = p[0].second.first; if (a < b) swap(a, b); ll k = n / b; n %= b; n += k * a; ll c = p[1].second.second; ll d = p[1].second.first; if (c < d) swap(c, d); ll e = p[2].second.second; ll f = p[2].second.first; if (e < f) swap(e, f); ll ans = 0; REP(i, n + 1) { if (d * i > n) break; ll j = (n - d * i) / f; ll cur = n + (c - d) * i + (e - f) * j; ans = max(ans, cur); } cout << ans << endl; return 0; } }
delete
93
94
93
93
0
p03008
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define REP(i, n) for (long long i = 0, max_i = (n); i < max_i; i++) #define REPI(i, a, b) for (long long i = (a), max_i = (b); i < max_i; i++) #define ALL(obj) begin(obj), end(obj) #define RALL(obj) rbegin(obj), rend(obj) #define fi first #define se second using ii = pair<int, int>; vector<ii> dirs = { {1, 0}, {0, 1}, {-1, 0}, {0, -1}, // 4方向 {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // 斜め {0, 0}, // 自身 }; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T, class S> vector<T> make_vec(size_t n, S x) { return vector<T>(n, x); } template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...)); } // debug template <class T> ostream &operator<<(ostream &s, vector<T> &d) { REP(i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : " "); return s; } template <class T> ostream &operator<<(ostream &s, vector<vector<T>> &d) { REP(i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : "\n"); return s; } template <class T, class S> ostream &operator<<(ostream &s, pair<T, S> &p) { s << "{" << p.first << ", " << p.second << "}"; return s; } template <class T, class S> ostream &operator<<(ostream &s, map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; } template <class T, class S> ostream &operator<<(ostream &s, unordered_map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; } #ifdef _MY_DEBUG #define dump(...) \ cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ \ << "]" << endl, \ dump_func(__VA_ARGS__), cerr << "*/\n\n"; #else #define dump(...) #define endl "\n" #endif void dump_func() { cerr << endl; } template <class Head, class... Tail> void dump_func(Head &&h, Tail &&...t) { cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...); } struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); constexpr int MOD = 1000000007; // *************** TEMPLATE END *************** signed main() { int n; cin >> n; int xa, ya, za; cin >> xa >> ya >> za; int xb, yb, zb; cin >> xb >> yb >> zb; int ans1 = n; REP(n1, (xb > xa ? n + 1 : 1)) { REP(n2, (yb > ya ? n + 1 : 1)) { int n3 = n - n1 - n2; if (n3 < 0) break; chmax(ans1, n + n1 / xa * (xb - xa) + n2 / ya * (yb - ya) + n3 / za * (zb - za)); } } dump(ans1); swap(xa, xb); swap(ya, yb); swap(za, zb); int ans2 = ans1; REP(n1, (xb > xa ? ans1 + 1 : 1)) { REP(n2, (yb > ya ? ans1 + 1 : 1)) { int n3 = ans1 - n1 - n2; if (n3 < 0) break; chmax(ans2, ans1 + n1 / xa * (xb - xa) + n2 / ya * (yb - ya) + n3 / za * (zb - za)); } } cout << ans2 << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long #define REP(i, n) for (long long i = 0, max_i = (n); i < max_i; i++) #define REPI(i, a, b) for (long long i = (a), max_i = (b); i < max_i; i++) #define ALL(obj) begin(obj), end(obj) #define RALL(obj) rbegin(obj), rend(obj) #define fi first #define se second using ii = pair<int, int>; vector<ii> dirs = { {1, 0}, {0, 1}, {-1, 0}, {0, -1}, // 4方向 {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // 斜め {0, 0}, // 自身 }; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T, class S> vector<T> make_vec(size_t n, S x) { return vector<T>(n, x); } template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...)); } // debug template <class T> ostream &operator<<(ostream &s, vector<T> &d) { REP(i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : " "); return s; } template <class T> ostream &operator<<(ostream &s, vector<vector<T>> &d) { REP(i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : "\n"); return s; } template <class T, class S> ostream &operator<<(ostream &s, pair<T, S> &p) { s << "{" << p.first << ", " << p.second << "}"; return s; } template <class T, class S> ostream &operator<<(ostream &s, map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; } template <class T, class S> ostream &operator<<(ostream &s, unordered_map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; } #ifdef _MY_DEBUG #define dump(...) \ cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ \ << "]" << endl, \ dump_func(__VA_ARGS__), cerr << "*/\n\n"; #else #define dump(...) #define endl "\n" #endif void dump_func() { cerr << endl; } template <class Head, class... Tail> void dump_func(Head &&h, Tail &&...t) { cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...); } struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); constexpr int MOD = 1000000007; // *************** TEMPLATE END *************** signed main() { int n; cin >> n; int xa, ya, za; cin >> xa >> ya >> za; int xb, yb, zb; cin >> xb >> yb >> zb; int ans1 = n; REP(n1, (xb > xa ? n + 1 : 1)) { REP(n2, (yb > ya ? n + 1 : 1)) { int n3 = n - n1 - n2; if (n3 < 0) break; chmax(ans1, n + n1 / xa * (xb - xa) + n2 / ya * (yb - ya) + n3 / za * (zb - za)); } } dump(ans1); swap(xa, xb); swap(ya, yb); swap(za, zb); int ans2 = ans1; if (xb > xa && yb > ya && zb < za) { REP(n1, ans1 + 1) { int n2 = ans1 - n1; chmax(ans2, ans1 + n1 / xa * (xb - xa) + n2 / ya * (yb - ya)); } } else { REP(n1, (xb > xa ? ans1 + 1 : 1)) { REP(n2, (yb > ya ? ans1 + 1 : 1)) { int n3 = ans1 - n1 - n2; if (n3 < 0) break; chmax(ans2, ans1 + n1 / xa * (xb - xa) + n2 / ya * (yb - ya) + n3 / za * (zb - za)); } } } cout << ans2 << endl; }
replace
112
119
112
126
TLE
p03008
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; int MAX_N = (1 << 25); long long N, SA, SB, SC, TA, TB, TC; long long dp[1 << 25], dp2[1 << 25]; int main() { cin >> N >> SA >> SB >> SC >> TA >> TB >> TC; for (int i = 1; i < MAX_N; i++) dp[i] = -(1 << 30); dp[0] = N; for (int i = 0; i < MAX_N - TA; i++) dp[i + TA] = max(dp[i + TA], dp[i] - SA); for (int i = 0; i < MAX_N - TB; i++) dp[i + TB] = max(dp[i + TB], dp[i] - SB); for (int i = 0; i < MAX_N - TC; i++) dp[i + TC] = max(dp[i + TC], dp[i] - SC); for (int i = 1; i < MAX_N; i++) dp2[i] = -(1 << 30); dp2[0] = 0; for (int i = 0; i < MAX_N - TA; i++) dp2[i + TA] = max(dp2[i + TA], dp2[i] + SA); for (int i = 0; i < MAX_N - TB; i++) dp2[i + TB] = max(dp2[i + TB], dp2[i] + SB); for (int i = 0; i < MAX_N - TC; i++) dp2[i + TC] = max(dp2[i + TC], dp2[i] + SC); for (int i = 0; i < MAX_N; i++) dp2[i + 1] = max(dp2[i + 1], dp2[i] + 1LL); long long maxn = 0; for (int i = 0; i < MAX_N; i++) { if (dp[i] < 0) continue; maxn = max(maxn, dp2[i + dp[i]]); } cout << maxn << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int MAX_N = (1 << 25); long long N, SA, SB, SC, TA, TB, TC; long long dp[1 << 25], dp2[1 << 25]; int main() { cin >> N >> SA >> SB >> SC >> TA >> TB >> TC; for (int i = 1; i < MAX_N; i++) dp[i] = -(1 << 30); dp[0] = N; for (int i = 0; i < MAX_N - TA; i++) dp[i + TA] = max(dp[i + TA], dp[i] - SA); for (int i = 0; i < MAX_N - TB; i++) dp[i + TB] = max(dp[i + TB], dp[i] - SB); for (int i = 0; i < MAX_N - TC; i++) dp[i + TC] = max(dp[i + TC], dp[i] - SC); for (int i = 1; i < MAX_N; i++) dp2[i] = -(1 << 30); dp2[0] = 0; for (int i = 0; i < MAX_N - TA; i++) dp2[i + TA] = max(dp2[i + TA], dp2[i] + SA); for (int i = 0; i < MAX_N - TB; i++) dp2[i + TB] = max(dp2[i + TB], dp2[i] + SB); for (int i = 0; i < MAX_N - TC; i++) dp2[i + TC] = max(dp2[i + TC], dp2[i] + SC); for (int i = 0; i < MAX_N - 1; i++) dp2[i + 1] = max(dp2[i + 1], dp2[i] + 1LL); long long maxn = 0; for (int i = 0; i < MAX_N; i++) { if (dp[i] < 0) continue; maxn = max(maxn, dp2[i + dp[i]]); } cout << maxn << endl; return 0; }
replace
30
31
30
31
-11
p03008
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cfloat> #include <cmath> #include <cstdint> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using lint = int64_t; using uint = uint32_t; using ulint = uint64_t; template <class T> using vector2d = vector<vector<T>>; template <class T> bool UpdateMax(T &a, const T &b) { if (a < b) { a = b; return true; } else { return false; } } template <class T> bool UpdateMin(T &a, const T &b) { if (a > b) { a = b; return true; } else { return false; } } template <class T> void OutVec(const vector<T> &vec) { for (int i = 0; i < vec.size() - 1; ++i) { cout << vec[i] << " "; } cout << vec.back() << endl; } template <class T> void OutVec2d(const vector2d<T> &vec) { for (auto v : vec) { OutVec(v); } } lint solve(lint n, lint ga, lint gb, lint sa, lint sb, lint ba, lint bb) { lint tmp_max_nuts = n; for (lint gi = 0; gi <= 5000; gi++) { for (lint si = 0; si <= 5000; si++) { lint nuts = n - gi * ga - si * sa; if (nuts < 0) continue; if (ba < bb) { lint bi = nuts / ba; nuts -= bi * ba; nuts += gi * gb + si * sb + bi * bb; UpdateMax(tmp_max_nuts, nuts); } else { nuts += gi * gb + si * sb; UpdateMax(tmp_max_nuts, nuts); } } } lint result = tmp_max_nuts; for (lint gi = 0; gi <= 5000; gi++) { for (lint si = 0; si <= 5000; si++) { lint nuts = tmp_max_nuts - gi * gb - si * sb; if (nuts < 0) continue; if (ba > bb) { lint bi = nuts / bb; nuts -= bi * bb; nuts += gi * ga + si * sa + bi * ba; UpdateMax(result, nuts); } else { nuts += gi * ga + si * sa; UpdateMax(result, nuts); } } } return result; } int main() { cout << std::fixed << std::setprecision(16); cin.tie(nullptr); std::ios::sync_with_stdio(false); lint n, ga, gb, sa, sb, ba, bb; cin >> n >> ga >> sa >> ba >> gb >> sb >> bb; lint result = n; UpdateMax(result, solve(n, ga, gb, sa, sb, ba, bb)); UpdateMax(result, solve(n, sa, sb, ba, bb, ga, gb)); UpdateMax(result, solve(n, ba, bb, ga, gb, sa, sb)); return result; return 0; }
#include <algorithm> #include <cassert> #include <cfloat> #include <cmath> #include <cstdint> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using lint = int64_t; using uint = uint32_t; using ulint = uint64_t; template <class T> using vector2d = vector<vector<T>>; template <class T> bool UpdateMax(T &a, const T &b) { if (a < b) { a = b; return true; } else { return false; } } template <class T> bool UpdateMin(T &a, const T &b) { if (a > b) { a = b; return true; } else { return false; } } template <class T> void OutVec(const vector<T> &vec) { for (int i = 0; i < vec.size() - 1; ++i) { cout << vec[i] << " "; } cout << vec.back() << endl; } template <class T> void OutVec2d(const vector2d<T> &vec) { for (auto v : vec) { OutVec(v); } } lint solve(lint n, lint ga, lint gb, lint sa, lint sb, lint ba, lint bb) { lint tmp_max_nuts = n; for (lint gi = 0; gi <= 5000; gi++) { for (lint si = 0; si <= 5000; si++) { lint nuts = n - gi * ga - si * sa; if (nuts < 0) continue; if (ba < bb) { lint bi = nuts / ba; nuts -= bi * ba; nuts += gi * gb + si * sb + bi * bb; UpdateMax(tmp_max_nuts, nuts); } else { nuts += gi * gb + si * sb; UpdateMax(tmp_max_nuts, nuts); } } } lint result = tmp_max_nuts; for (lint gi = 0; gi <= 5000; gi++) { for (lint si = 0; si <= 5000; si++) { lint nuts = tmp_max_nuts - gi * gb - si * sb; if (nuts < 0) continue; if (ba > bb) { lint bi = nuts / bb; nuts -= bi * bb; nuts += gi * ga + si * sa + bi * ba; UpdateMax(result, nuts); } else { nuts += gi * ga + si * sa; UpdateMax(result, nuts); } } } return result; } int main() { cout << std::fixed << std::setprecision(16); cin.tie(nullptr); std::ios::sync_with_stdio(false); lint n, ga, gb, sa, sb, ba, bb; cin >> n >> ga >> sa >> ba >> gb >> sb >> bb; lint result = n; UpdateMax(result, solve(n, ga, gb, sa, sb, ba, bb)); UpdateMax(result, solve(n, sa, sb, ba, bb, ga, gb)); UpdateMax(result, solve(n, ba, bb, ga, gb, sa, sb)); cout << result << endl; return 0; }
replace
109
110
109
110
46
p03008
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; long long int N, g[3], s[3], b[3]; long long int dp[5555]; long long int dp2[3000000]; int main(void) { cin >> N; for (int i = 1; i <= 2; i++) { cin >> g[i] >> s[i] >> b[i]; } long long int M = 0; for (int i = 1; i <= N; i++) { dp[i] = i; if (i - g[1] >= 0) { dp[i] = max(dp[i], dp[i - g[1]] + g[2]); } if (i - s[1] >= 0) { dp[i] = max(dp[i], dp[i - s[1]] + s[2]); } if (i - b[1] >= 0) { dp[i] = max(dp[i], dp[i - b[1]] + b[2]); } M = max(M, dp[i]); } //============================= long long int ans = 0; for (long long int i = 1; i <= M; i++) { dp2[i] = i; if (i - g[2] >= 0) { dp2[i] = max(dp2[i], dp2[i - g[2]] + g[1]); } if (i - s[2] >= 0) { dp2[i] = max(dp2[i], dp2[i - s[2]] + s[1]); } if (i - b[2] >= 0) { dp2[i] = max(dp2[i], dp2[i - b[2]] + b[1]); } ans = max(ans, dp2[i]); } cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; long long int N, g[3], s[3], b[3]; long long int dp[5555]; long long int dp2[30000000]; int main(void) { cin >> N; for (int i = 1; i <= 2; i++) { cin >> g[i] >> s[i] >> b[i]; } long long int M = 0; for (int i = 1; i <= N; i++) { dp[i] = i; if (i - g[1] >= 0) { dp[i] = max(dp[i], dp[i - g[1]] + g[2]); } if (i - s[1] >= 0) { dp[i] = max(dp[i], dp[i - s[1]] + s[2]); } if (i - b[1] >= 0) { dp[i] = max(dp[i], dp[i - b[1]] + b[2]); } M = max(M, dp[i]); } //============================= long long int ans = 0; for (long long int i = 1; i <= M; i++) { dp2[i] = i; if (i - g[2] >= 0) { dp2[i] = max(dp2[i], dp2[i - g[2]] + g[1]); } if (i - s[2] >= 0) { dp2[i] = max(dp2[i], dp2[i - s[2]] + s[1]); } if (i - b[2] >= 0) { dp2[i] = max(dp2[i], dp2[i - b[2]] + b[1]); } ans = max(ans, dp2[i]); } cout << ans << endl; return 0; }
replace
9
10
9
10
0
p03008
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; /**** Type Define ****/ typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> Q; typedef complex<double> C; /**** Macro Define ****/ #define cx real() #define cy imag() /**** Const List ****/ const ll INF = 1LL << 60; const double DINF = 1e30; const ll mod = 1000000007; const ll MAX_FLOW_MAX_V = 10000; const ll MIN_COST_FLOW_MAX_V = 10000; const ll BIPARTITE_MATCHING_MAX_V = 10000; const ll dx[4] = {1, 0, -1, 0}; const ll dy[4] = {0, -1, 0, 1}; const C I = C(0, 1); const double EPS = 1e-10; /**** General Functions ****/ template <typename T> T tmin(T a, T b) { return a > b ? b : a; }; template <typename T> T tmax(T a, T b) { return a > b ? a : b; }; template <typename T> T tadd(T a, T b) { return a + b; }; template <typename T> T tmul(T a, T b) { return a * b; }; template <typename T> T tpow(T a, T b) { return a * b; }; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1, y = 0; return a; } ll q = a / b, g = extgcd(b, a - q * b, x, y); ll z = x - q * y; x = y; y = z; return g; } ll invmod(ll a, ll m) { // a^-1 mod m ll x, y; extgcd(a, m, x, y); x %= m; if (x < 0) x += m; return x; } ll nCk(ll n, ll k, ll mod) { ll ans = 1; for (ll i = n, j = 1; j <= k; i--, j++) ans = (((ans * i) % mod) * invmod(j, mod)) % mod; return ans; } ll lmin(ll a, ll b) { return a > b ? b : a; }; ll lmax(ll a, ll b) { return a > b ? a : b; }; ll lsum(ll a, ll b) { return a + b; }; /**** Matrix ****/ template <typename T> struct Matrix { typedef vector<T> vec; typedef vector<vec> mat; ll x, y; // x: horizon y: vertical mat d; Matrix(ll _y, ll _x = -1) { if (_x == -1) _x = _y; x = _x, y = _y; for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) d[i][j] = 0; } void unit() { for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) d[i][j] = i == j ? 1 : 0; } Matrix copy() { Matrix m(y, x); for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) m.d[i][j] = d[i][j]; return m; } Matrix<T> operator+( Matrix<T> &t) { // No error check! Don't forget to check Matrix size!! Matrix<T> m(y, x); for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) m.d[i][j] = d[i][j] + t.d[i][j]; return m; } Matrix<T> operator-(Matrix<T> &t) { Matrix<T> m(y, x); for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) m.d[i][j] = d[i][j] - t.d[i][j]; return m; } Matrix<T> operator*(T t) { Matrix<T> m(y, x); for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) m.d[i][j] = d[i][j] * t; return m; } Matrix<T> det(Matrix<T> &t) { // x need to correspond to t.y Matrix<T> m(y, x); for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) for (int k = 0; k < t.x; k++) m.d[i][j] += d[i][k] * t.d[k][j]; ////////////// mod??? return m; } }; /**** Zip ****/ template <typename T> class Zip { vector<T> d; bool flag; public: Zip() { flag = false; } void add(T x) { d.push_back(x); flag = true; } ll getNum(T x) { // T need to have operator < !! if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return lower_bound(d.begin(), d.end(), x) - d.begin(); } ll size() { if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return (ll)d.size(); } }; /**** Union Find ****/ class UnionFind { vector<ll> par, rank; // par > 0: number, par < 0: -par public: void init(ll n) { par.resize(n, 1); rank.resize(n, 0); } ll getSize(ll x) { return par[find(x)]; } ll find(ll x) { if (par[x] > 0) return x; return -(par[x] = -find(-par[x])); } void merge(ll x, ll y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = -y; } else { par[x] += par[y]; par[y] = -x; if (rank[x] == rank[y]) rank[x]++; } } bool isSame(ll x, ll y) { return find(x) == find(y); } }; template <typename T> struct UnionFindT { vector<ll> par; vector<ll> rank; vector<T> diff_weight; UnionFindT(ll n = 1, T SUM_UNITY = 0) { init(n, SUM_UNITY); } void init(ll n = 1, T SUM_UNITY = 0) { par.resize(n); rank.resize(n); diff_weight.resize(n); for (ll i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY; } ll find(ll x) { if (par[x] == x) { return x; } else { ll r = find(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } } T weight(ll x) { find(x); return diff_weight[x]; } bool isSame(ll x, ll y) { return find(x) == find(y); } bool merge(ll x, ll y, T w) { w += weight(x); w -= weight(y); x = find(x); y = find(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y), w = -w; if (rank[x] == rank[y]) ++rank[x]; par[y] = x; diff_weight[y] = w; return true; } T diff(ll x, ll y) { return weight(y) - weight(x); } }; class PersistentUnionFind { vector<ll> rank, fin, par; ll index; public: void init(ll n) { index = 0; par.resize(n); rank.resize(n, 1); fin.resize(n, 0); for (ll i = 0; i < n; i++) par[i] = i; } ll find(ll x, ll t) { if (t >= fin[x] && par[x] != x) return find(par[x], t); return x; } void merge(ll x, ll y) { x = find(x, index); y = find(y, index); index++; if (x == y) return; if (rank[x] < rank[y]) par[x] = y, fin[x] = index; else { par[y] = x, fin[y] = index; if (rank[x] == rank[y]) rank[x]++; } } bool isSame(ll x, ll y, ll t) { return find(x, t) == find(y, t); } }; /**** Segment Tree ****/ template <typename T> class SegmentTree { ll n; vector<T> node; function<T(T, T)> fun, fun2; bool customChange; T outValue, initValue; public: void init(ll num, function<T(T, T)> resultFunction, T init, T out, function<T(T, T)> changeFunction = NULL) { // changeFunction: (input, beforevalue) => newvalue fun = resultFunction; fun2 = changeFunction; customChange = changeFunction != NULL; n = 1; while (n < num) n *= 2; node.resize(2 * n - 1, init); outValue = out; initValue = init; } void valueChange(ll num, T value) { num += n - 1; if (customChange) node[num] = fun2(value, node[num]); else node[num] = value; while (num > 0) num = (num - 1) / 2, node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]); } T rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b) if (r == -1) r = n; if (a <= l && r <= b) return node[k]; if (b <= l || r <= a) return outValue; ll mid = (l + r) / 2; return fun(rangeQuery(a, b, l, mid, 2 * k + 1), rangeQuery(a, b, mid, r, 2 * k + 2)); } }; template <typename T> class LazySegmentTree { ll n; vector<T> node; vector<T> lazyNode; function<T(T, T)> fun, fun2; function<T(T, ll)> fun3; T outValue, initValue; T substitution(T a, T b) { return a; } void eval(ll k, ll l, ll r) { if (lazyNode[k] == 0) return; node[k] = fun2(fun3(lazyNode[k], r - l), node[k]); if (r - l > 1) { lazyNode[2 * k + 1] = fun2(lazyNode[k], lazyNode[2 * k + 1]); lazyNode[2 * k + 2] = fun2(lazyNode[k], lazyNode[2 * k + 2]); } lazyNode[k] = initValue; } public: void init(ll num, function<T(T, T)> resultFunction, function<T(T, T)> changeFunction, function<T(T, ll)> lazyFunction, T init, T out) { // changeFunction: (input, beforevalue) => newvalue // lazyFunction: (lazyNode, diff) => newvalue fun = resultFunction; fun2 = changeFunction; fun3 = lazyFunction; n = 1; while (n < num) n *= 2; node.resize(2 * n - 1, init); lazyNode.resize(2 * n - 1, init); outValue = out; initValue = init; } void rangeChange(ll a, ll b, T value, ll l = 0, ll r = -1, ll k = 0) { if (r == -1) r = n; eval(k, l, r); if (b <= l || r <= a) return; if (a <= l && r <= b) { lazyNode[k] = fun2(value, lazyNode[k]); eval(k, l, r); } else { ll mid = (l + r) / 2; rangeChange(a, b, value, l, mid, 2 * k + 1); rangeChange(a, b, value, mid, r, 2 * k + 2); node[k] = fun(node[2 * k + 1], node[2 * k + 2]); } } T rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b) if (r == -1) r = n; if (b <= l || r <= a) return outValue; eval(k, l, r); if (a <= l && r <= b) return node[k]; ll mid = (l + r) / 2; return fun(rangeQuery(a, b, l, mid, 2 * k + 1), rangeQuery(a, b, mid, r, 2 * k + 2)); } }; /**** Network Flow ****/ class MaxFlow { public: struct edge { ll to, cap, rev; }; vector<edge> G[MAX_FLOW_MAX_V]; bool used[MAX_FLOW_MAX_V]; ll level[MAX_FLOW_MAX_V]; ll iter[MAX_FLOW_MAX_V]; void init() { for (ll i = 0; i < MAX_FLOW_MAX_V; i++) { G[i].clear(); } } void add_edge(ll from, ll to, ll cap) { G[from].push_back((edge){to, cap, (ll)G[to].size()}); G[to].push_back((edge){from, 0, (ll)G[from].size() - 1}); } void add_undirected_edge(ll e1, ll e2, ll cap) { G[e1].push_back((edge){e2, cap, (ll)G[e2].size()}); G[e2].push_back((edge){e1, cap, (ll)G[e1].size() - 1}); } ll dfs(ll v, ll t, ll f) { if (v == t) return f; used[v] = true; for (ll i = 0; i < (ll)G[v].size(); i++) { edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } ll max_flow(ll s, ll t) { ll flow = 0; while (1) { memset(used, 0, sizeof(used)); ll f = dfs(s, t, INF); if (f == 0) return flow; flow += f; } } void bfs(ll s) { memset(level, -1, sizeof(level)); queue<ll> que; level[s] = 0; que.push(s); while (!que.empty()) { ll v = que.front(); que.pop(); for (ll i = 0; i < (ll)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } ll dinic_dfs(ll v, ll t, ll f) { if (v == t) return f; for (ll &i = iter[v]; i < (ll)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { ll d = dinic_dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } ll dinic(ll s, ll t) { ll flow = 0; while (1) { bfs(s); if (level[t] < 0) return flow; memset(iter, 0, sizeof(iter)); ll f; while ((f = dinic_dfs(s, t, INF)) > 0) { flow += f; } } } }; /**** bipartite matching ****/ class BipartiteMatching { vector<ll> pre, root; vector<vector<ll>> to; vector<ll> p, q; ll n, m; public: BipartiteMatching(ll n, ll m) : pre(n, -1), root(n, -1), to(n), p(n, -1), q(m, -1), n(n), m(m) {} void add(ll a, ll b) { to[a].push_back(b); } ll solve() { ll res = 0; bool upd = true; while (upd) { upd = false; queue<ll> s; for (ll i = 0; i < n; ++i) { if (!~p[i]) { root[i] = i; s.push(i); } } while (!s.empty()) { ll v = s.front(); s.pop(); if (~p[root[v]]) continue; for (ll i = 0; i < (ll)to[v].size(); ++i) { ll u = to[v][i]; if (!~q[u]) { while (~u) { q[u] = v; swap(p[v], u); v = pre[v]; } upd = true; ++res; break; } u = q[u]; if (~pre[u]) continue; pre[u] = v; root[u] = root[v]; s.push(u); } } if (upd) fill(pre.begin(), pre.end(), -1), fill(root.begin(), root.end(), -1); } return res; } }; class MinCostFlow { public: struct edge { ll to, cap, cost, rev; }; ll V; vector<edge> G[MIN_COST_FLOW_MAX_V]; ll dist[MIN_COST_FLOW_MAX_V]; ll prevv[MIN_COST_FLOW_MAX_V]; ll preve[MIN_COST_FLOW_MAX_V]; ll h[MIN_COST_FLOW_MAX_V]; MinCostFlow(ll v) { V = v; } void init() { for (ll i = 0; i < MAX_FLOW_MAX_V; i++) { G[i].clear(); } } void add_edge(ll from, ll to, ll cap, ll cost) { G[from].push_back((edge){to, cap, cost, (ll)G[to].size()}); G[to].push_back((edge){from, 0, -cost, (ll)G[from].size() - 1}); } void add_undirected_edge(ll e1, ll e2, ll cap, ll cost) { add_edge(e1, e2, cap, cost); add_edge(e2, e1, cap, cost); } ll min_cost_flow(ll s, ll t, ll f) { // minas ll res = 0; while (f > 0) { fill(dist, dist + V, INF); dist[s] = 0; bool update = true; while (update) { update = false; for (ll v = 0; v < V; v++) { if (dist[v] == INF) continue; for (ll i = 0; i < (ll)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) { dist[e.to] = dist[v] + e.cost; prevv[e.to] = v; preve[e.to] = i; update = true; } } } } if (dist[t] == INF) { return -1; } ll d = f; for (ll v = t; v != s; v = prevv[v]) { d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * dist[t]; for (ll v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } ll min_cost_flow_dijkstra(ll s, ll t, ll f) { ll res = 0; fill(h, h + V, 0); while (f > 0) { priority_queue<P, vector<P>, greater<P>> que; fill(dist, dist + V, 0); dist[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first) continue; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.push(P(dist[e.to], e.to)); } } } if (dist[t] == INF) { return -1; } for (int v = 0; v < V; v++) h[v] += dist[v]; int d = f; for (int v = t; v != s; v = prevv[v]) { d = tmin<ll>(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * h[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } return res; } return 0; } }; /**** LIS ****/ ll lis(ll *a, ll n, ll *dp) { fill(dp, dp + n, INF); for (ll i = 0; i < n; i++) *lower_bound(dp, dp + n, a[i]) = a[i]; return (ll)(lower_bound(dp, dp + n, INF) - dp); } /**** Binary Search ****/ ll binarySearch(function<bool(ll)> check, ll ok, ll ng) { while ((ok - ng > 1) || (ng - ok > 1)) { ll mid = (ok + ng) / 2; if (check(mid)) ok = mid; else ng = mid; } return ok; } double binarySearchDouble(function<bool(double)> check, double ok, double ng) { while ((ok - ng > EPS) || (ng - ok > EPS)) { double mid = (ok + ng) / 2; if (check(mid)) ok = mid; else ng = mid; } return ok; } /**** Geometry ****/ bool isEqual(double a, double b) { return abs(a - b) < EPS; } bool isCEqual(C a, C b) { return isEqual(a.cx, a.cy) && isEqual(a.cy, a.cy); } bool isZero(double a) { return abs(a) < EPS; } // a == 0 bool isUZero(double a) { return a > EPS; } // a > 0 bool isUEZero(double a) { return a > -EPS; } // a >= 0 bool isLZero(double a) { return a < -EPS; } // a < 0 bool isLEZero(double a) { return a < EPS; } // a <= 0 C getUnitVector(C a) { double len = abs(a); return isZero(len) ? C(0.0, 0.0) : a / len; } double dot(C a, C b) { return a.cx * b.cx + a.cy * b.cy; } // |a||b|cosθ double det(C a, C b) { return a.cx * b.cy - a.cy * b.cx; } // |a||b|sinθ bool isLineOrthogonal(C a1, C a2, C b1, C b2) { return isZero(dot(a1 - a2, b1 - b2)); } // a1-a2, b1-b2 bool isLineParallel(C a1, C a2, C b1, C b2) { return isZero(det(a1 - a2, b1 - b2)); } // a1-a2, b1-b2 bool isPointOnLine(C a, C b, C c) { return isZero(det(b - a, c - a)); } // a-b <- c /* bool isPointOnLineSegment(C a, C b, C c) { // a-b <- c return isZero(det(b - a, c - a)) && isUEZero(dot(b - a, c - a)) && isUEZero(dot(a - b, c - b)); } */ bool isPointOnLineSegment(C a, C b, C c) { return isZero(abs(a - c) + abs(c - b) - abs(a - b)); } double distanceLineAndPoint(C a, C b, C c) { return abs(det(b - a, c - a)) / abs(b - a); } // a-b <- c double distanceLineSegmentAndPoint(C a, C b, C c) { // a-b <- c if (isLEZero(dot(b - a, c - a))) return abs(c - a); if (isLEZero(dot(a - b, c - b))) return abs(c - b); return abs(det(b - a, c - a)) / abs(b - a); } bool isIntersectedLine(C a1, C a2, C b1, C b2) { // a1-a2, b1-b2 return !isLineParallel(a1, a2, b1, b2); } C intersectionLine(C a1, C a2, C b1, C b2) { // isIntersectedLine-> true C a = a2 - a1, b = b2 - b1; return a1 + a * det(b, b1 - a1) / det(b, a); } /**** NG Words ****/ // cx cy P Q C // Warning: EPS /**** main function ****/ ll n, ga, gb, sa, sb, ba, bb; ll search(ll n, ll g, ll s, ll b, ll dg, ll ds, ll db) { ll ans = 0; for (ll i = 0; i <= ((dg > 0) ? n / g : 1); i++) for (ll j = 0; j <= ((ds > 0) ? n / s : 1); j++) { ll sum = g * i + s * j; if (sum > n) continue; ll k = (db > 0) ? (n - sum) / b : 0; ans = max(ans, dg * i + ds * j + db * k); } return ans; } int main() { scanf("%lld", &n); scanf("%lld%lld%lld", &ga, &sa, &ba); scanf("%lld%lld%lld", &gb, &sb, &bb); n += search(n, ga, sa, ba, gb - ga, sb - sa, bb - ba); n += search(n, gb, sb, bb, ga - gb, sa - sb, ba - bb); printf("%lld\n", n); }
#include <algorithm> #include <bitset> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; /**** Type Define ****/ typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> Q; typedef complex<double> C; /**** Macro Define ****/ #define cx real() #define cy imag() /**** Const List ****/ const ll INF = 1LL << 60; const double DINF = 1e30; const ll mod = 1000000007; const ll MAX_FLOW_MAX_V = 10000; const ll MIN_COST_FLOW_MAX_V = 10000; const ll BIPARTITE_MATCHING_MAX_V = 10000; const ll dx[4] = {1, 0, -1, 0}; const ll dy[4] = {0, -1, 0, 1}; const C I = C(0, 1); const double EPS = 1e-10; /**** General Functions ****/ template <typename T> T tmin(T a, T b) { return a > b ? b : a; }; template <typename T> T tmax(T a, T b) { return a > b ? a : b; }; template <typename T> T tadd(T a, T b) { return a + b; }; template <typename T> T tmul(T a, T b) { return a * b; }; template <typename T> T tpow(T a, T b) { return a * b; }; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1, y = 0; return a; } ll q = a / b, g = extgcd(b, a - q * b, x, y); ll z = x - q * y; x = y; y = z; return g; } ll invmod(ll a, ll m) { // a^-1 mod m ll x, y; extgcd(a, m, x, y); x %= m; if (x < 0) x += m; return x; } ll nCk(ll n, ll k, ll mod) { ll ans = 1; for (ll i = n, j = 1; j <= k; i--, j++) ans = (((ans * i) % mod) * invmod(j, mod)) % mod; return ans; } ll lmin(ll a, ll b) { return a > b ? b : a; }; ll lmax(ll a, ll b) { return a > b ? a : b; }; ll lsum(ll a, ll b) { return a + b; }; /**** Matrix ****/ template <typename T> struct Matrix { typedef vector<T> vec; typedef vector<vec> mat; ll x, y; // x: horizon y: vertical mat d; Matrix(ll _y, ll _x = -1) { if (_x == -1) _x = _y; x = _x, y = _y; for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) d[i][j] = 0; } void unit() { for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) d[i][j] = i == j ? 1 : 0; } Matrix copy() { Matrix m(y, x); for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) m.d[i][j] = d[i][j]; return m; } Matrix<T> operator+( Matrix<T> &t) { // No error check! Don't forget to check Matrix size!! Matrix<T> m(y, x); for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) m.d[i][j] = d[i][j] + t.d[i][j]; return m; } Matrix<T> operator-(Matrix<T> &t) { Matrix<T> m(y, x); for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) m.d[i][j] = d[i][j] - t.d[i][j]; return m; } Matrix<T> operator*(T t) { Matrix<T> m(y, x); for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) m.d[i][j] = d[i][j] * t; return m; } Matrix<T> det(Matrix<T> &t) { // x need to correspond to t.y Matrix<T> m(y, x); for (int i = 0; i < y; i++) for (int j = 0; j < x; j++) for (int k = 0; k < t.x; k++) m.d[i][j] += d[i][k] * t.d[k][j]; ////////////// mod??? return m; } }; /**** Zip ****/ template <typename T> class Zip { vector<T> d; bool flag; public: Zip() { flag = false; } void add(T x) { d.push_back(x); flag = true; } ll getNum(T x) { // T need to have operator < !! if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return lower_bound(d.begin(), d.end(), x) - d.begin(); } ll size() { if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return (ll)d.size(); } }; /**** Union Find ****/ class UnionFind { vector<ll> par, rank; // par > 0: number, par < 0: -par public: void init(ll n) { par.resize(n, 1); rank.resize(n, 0); } ll getSize(ll x) { return par[find(x)]; } ll find(ll x) { if (par[x] > 0) return x; return -(par[x] = -find(-par[x])); } void merge(ll x, ll y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = -y; } else { par[x] += par[y]; par[y] = -x; if (rank[x] == rank[y]) rank[x]++; } } bool isSame(ll x, ll y) { return find(x) == find(y); } }; template <typename T> struct UnionFindT { vector<ll> par; vector<ll> rank; vector<T> diff_weight; UnionFindT(ll n = 1, T SUM_UNITY = 0) { init(n, SUM_UNITY); } void init(ll n = 1, T SUM_UNITY = 0) { par.resize(n); rank.resize(n); diff_weight.resize(n); for (ll i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY; } ll find(ll x) { if (par[x] == x) { return x; } else { ll r = find(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } } T weight(ll x) { find(x); return diff_weight[x]; } bool isSame(ll x, ll y) { return find(x) == find(y); } bool merge(ll x, ll y, T w) { w += weight(x); w -= weight(y); x = find(x); y = find(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y), w = -w; if (rank[x] == rank[y]) ++rank[x]; par[y] = x; diff_weight[y] = w; return true; } T diff(ll x, ll y) { return weight(y) - weight(x); } }; class PersistentUnionFind { vector<ll> rank, fin, par; ll index; public: void init(ll n) { index = 0; par.resize(n); rank.resize(n, 1); fin.resize(n, 0); for (ll i = 0; i < n; i++) par[i] = i; } ll find(ll x, ll t) { if (t >= fin[x] && par[x] != x) return find(par[x], t); return x; } void merge(ll x, ll y) { x = find(x, index); y = find(y, index); index++; if (x == y) return; if (rank[x] < rank[y]) par[x] = y, fin[x] = index; else { par[y] = x, fin[y] = index; if (rank[x] == rank[y]) rank[x]++; } } bool isSame(ll x, ll y, ll t) { return find(x, t) == find(y, t); } }; /**** Segment Tree ****/ template <typename T> class SegmentTree { ll n; vector<T> node; function<T(T, T)> fun, fun2; bool customChange; T outValue, initValue; public: void init(ll num, function<T(T, T)> resultFunction, T init, T out, function<T(T, T)> changeFunction = NULL) { // changeFunction: (input, beforevalue) => newvalue fun = resultFunction; fun2 = changeFunction; customChange = changeFunction != NULL; n = 1; while (n < num) n *= 2; node.resize(2 * n - 1, init); outValue = out; initValue = init; } void valueChange(ll num, T value) { num += n - 1; if (customChange) node[num] = fun2(value, node[num]); else node[num] = value; while (num > 0) num = (num - 1) / 2, node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]); } T rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b) if (r == -1) r = n; if (a <= l && r <= b) return node[k]; if (b <= l || r <= a) return outValue; ll mid = (l + r) / 2; return fun(rangeQuery(a, b, l, mid, 2 * k + 1), rangeQuery(a, b, mid, r, 2 * k + 2)); } }; template <typename T> class LazySegmentTree { ll n; vector<T> node; vector<T> lazyNode; function<T(T, T)> fun, fun2; function<T(T, ll)> fun3; T outValue, initValue; T substitution(T a, T b) { return a; } void eval(ll k, ll l, ll r) { if (lazyNode[k] == 0) return; node[k] = fun2(fun3(lazyNode[k], r - l), node[k]); if (r - l > 1) { lazyNode[2 * k + 1] = fun2(lazyNode[k], lazyNode[2 * k + 1]); lazyNode[2 * k + 2] = fun2(lazyNode[k], lazyNode[2 * k + 2]); } lazyNode[k] = initValue; } public: void init(ll num, function<T(T, T)> resultFunction, function<T(T, T)> changeFunction, function<T(T, ll)> lazyFunction, T init, T out) { // changeFunction: (input, beforevalue) => newvalue // lazyFunction: (lazyNode, diff) => newvalue fun = resultFunction; fun2 = changeFunction; fun3 = lazyFunction; n = 1; while (n < num) n *= 2; node.resize(2 * n - 1, init); lazyNode.resize(2 * n - 1, init); outValue = out; initValue = init; } void rangeChange(ll a, ll b, T value, ll l = 0, ll r = -1, ll k = 0) { if (r == -1) r = n; eval(k, l, r); if (b <= l || r <= a) return; if (a <= l && r <= b) { lazyNode[k] = fun2(value, lazyNode[k]); eval(k, l, r); } else { ll mid = (l + r) / 2; rangeChange(a, b, value, l, mid, 2 * k + 1); rangeChange(a, b, value, mid, r, 2 * k + 2); node[k] = fun(node[2 * k + 1], node[2 * k + 2]); } } T rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b) if (r == -1) r = n; if (b <= l || r <= a) return outValue; eval(k, l, r); if (a <= l && r <= b) return node[k]; ll mid = (l + r) / 2; return fun(rangeQuery(a, b, l, mid, 2 * k + 1), rangeQuery(a, b, mid, r, 2 * k + 2)); } }; /**** Network Flow ****/ class MaxFlow { public: struct edge { ll to, cap, rev; }; vector<edge> G[MAX_FLOW_MAX_V]; bool used[MAX_FLOW_MAX_V]; ll level[MAX_FLOW_MAX_V]; ll iter[MAX_FLOW_MAX_V]; void init() { for (ll i = 0; i < MAX_FLOW_MAX_V; i++) { G[i].clear(); } } void add_edge(ll from, ll to, ll cap) { G[from].push_back((edge){to, cap, (ll)G[to].size()}); G[to].push_back((edge){from, 0, (ll)G[from].size() - 1}); } void add_undirected_edge(ll e1, ll e2, ll cap) { G[e1].push_back((edge){e2, cap, (ll)G[e2].size()}); G[e2].push_back((edge){e1, cap, (ll)G[e1].size() - 1}); } ll dfs(ll v, ll t, ll f) { if (v == t) return f; used[v] = true; for (ll i = 0; i < (ll)G[v].size(); i++) { edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } ll max_flow(ll s, ll t) { ll flow = 0; while (1) { memset(used, 0, sizeof(used)); ll f = dfs(s, t, INF); if (f == 0) return flow; flow += f; } } void bfs(ll s) { memset(level, -1, sizeof(level)); queue<ll> que; level[s] = 0; que.push(s); while (!que.empty()) { ll v = que.front(); que.pop(); for (ll i = 0; i < (ll)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } ll dinic_dfs(ll v, ll t, ll f) { if (v == t) return f; for (ll &i = iter[v]; i < (ll)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { ll d = dinic_dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } ll dinic(ll s, ll t) { ll flow = 0; while (1) { bfs(s); if (level[t] < 0) return flow; memset(iter, 0, sizeof(iter)); ll f; while ((f = dinic_dfs(s, t, INF)) > 0) { flow += f; } } } }; /**** bipartite matching ****/ class BipartiteMatching { vector<ll> pre, root; vector<vector<ll>> to; vector<ll> p, q; ll n, m; public: BipartiteMatching(ll n, ll m) : pre(n, -1), root(n, -1), to(n), p(n, -1), q(m, -1), n(n), m(m) {} void add(ll a, ll b) { to[a].push_back(b); } ll solve() { ll res = 0; bool upd = true; while (upd) { upd = false; queue<ll> s; for (ll i = 0; i < n; ++i) { if (!~p[i]) { root[i] = i; s.push(i); } } while (!s.empty()) { ll v = s.front(); s.pop(); if (~p[root[v]]) continue; for (ll i = 0; i < (ll)to[v].size(); ++i) { ll u = to[v][i]; if (!~q[u]) { while (~u) { q[u] = v; swap(p[v], u); v = pre[v]; } upd = true; ++res; break; } u = q[u]; if (~pre[u]) continue; pre[u] = v; root[u] = root[v]; s.push(u); } } if (upd) fill(pre.begin(), pre.end(), -1), fill(root.begin(), root.end(), -1); } return res; } }; class MinCostFlow { public: struct edge { ll to, cap, cost, rev; }; ll V; vector<edge> G[MIN_COST_FLOW_MAX_V]; ll dist[MIN_COST_FLOW_MAX_V]; ll prevv[MIN_COST_FLOW_MAX_V]; ll preve[MIN_COST_FLOW_MAX_V]; ll h[MIN_COST_FLOW_MAX_V]; MinCostFlow(ll v) { V = v; } void init() { for (ll i = 0; i < MAX_FLOW_MAX_V; i++) { G[i].clear(); } } void add_edge(ll from, ll to, ll cap, ll cost) { G[from].push_back((edge){to, cap, cost, (ll)G[to].size()}); G[to].push_back((edge){from, 0, -cost, (ll)G[from].size() - 1}); } void add_undirected_edge(ll e1, ll e2, ll cap, ll cost) { add_edge(e1, e2, cap, cost); add_edge(e2, e1, cap, cost); } ll min_cost_flow(ll s, ll t, ll f) { // minas ll res = 0; while (f > 0) { fill(dist, dist + V, INF); dist[s] = 0; bool update = true; while (update) { update = false; for (ll v = 0; v < V; v++) { if (dist[v] == INF) continue; for (ll i = 0; i < (ll)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) { dist[e.to] = dist[v] + e.cost; prevv[e.to] = v; preve[e.to] = i; update = true; } } } } if (dist[t] == INF) { return -1; } ll d = f; for (ll v = t; v != s; v = prevv[v]) { d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * dist[t]; for (ll v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } ll min_cost_flow_dijkstra(ll s, ll t, ll f) { ll res = 0; fill(h, h + V, 0); while (f > 0) { priority_queue<P, vector<P>, greater<P>> que; fill(dist, dist + V, 0); dist[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first) continue; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.push(P(dist[e.to], e.to)); } } } if (dist[t] == INF) { return -1; } for (int v = 0; v < V; v++) h[v] += dist[v]; int d = f; for (int v = t; v != s; v = prevv[v]) { d = tmin<ll>(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * h[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } return res; } return 0; } }; /**** LIS ****/ ll lis(ll *a, ll n, ll *dp) { fill(dp, dp + n, INF); for (ll i = 0; i < n; i++) *lower_bound(dp, dp + n, a[i]) = a[i]; return (ll)(lower_bound(dp, dp + n, INF) - dp); } /**** Binary Search ****/ ll binarySearch(function<bool(ll)> check, ll ok, ll ng) { while ((ok - ng > 1) || (ng - ok > 1)) { ll mid = (ok + ng) / 2; if (check(mid)) ok = mid; else ng = mid; } return ok; } double binarySearchDouble(function<bool(double)> check, double ok, double ng) { while ((ok - ng > EPS) || (ng - ok > EPS)) { double mid = (ok + ng) / 2; if (check(mid)) ok = mid; else ng = mid; } return ok; } /**** Geometry ****/ bool isEqual(double a, double b) { return abs(a - b) < EPS; } bool isCEqual(C a, C b) { return isEqual(a.cx, a.cy) && isEqual(a.cy, a.cy); } bool isZero(double a) { return abs(a) < EPS; } // a == 0 bool isUZero(double a) { return a > EPS; } // a > 0 bool isUEZero(double a) { return a > -EPS; } // a >= 0 bool isLZero(double a) { return a < -EPS; } // a < 0 bool isLEZero(double a) { return a < EPS; } // a <= 0 C getUnitVector(C a) { double len = abs(a); return isZero(len) ? C(0.0, 0.0) : a / len; } double dot(C a, C b) { return a.cx * b.cx + a.cy * b.cy; } // |a||b|cosθ double det(C a, C b) { return a.cx * b.cy - a.cy * b.cx; } // |a||b|sinθ bool isLineOrthogonal(C a1, C a2, C b1, C b2) { return isZero(dot(a1 - a2, b1 - b2)); } // a1-a2, b1-b2 bool isLineParallel(C a1, C a2, C b1, C b2) { return isZero(det(a1 - a2, b1 - b2)); } // a1-a2, b1-b2 bool isPointOnLine(C a, C b, C c) { return isZero(det(b - a, c - a)); } // a-b <- c /* bool isPointOnLineSegment(C a, C b, C c) { // a-b <- c return isZero(det(b - a, c - a)) && isUEZero(dot(b - a, c - a)) && isUEZero(dot(a - b, c - b)); } */ bool isPointOnLineSegment(C a, C b, C c) { return isZero(abs(a - c) + abs(c - b) - abs(a - b)); } double distanceLineAndPoint(C a, C b, C c) { return abs(det(b - a, c - a)) / abs(b - a); } // a-b <- c double distanceLineSegmentAndPoint(C a, C b, C c) { // a-b <- c if (isLEZero(dot(b - a, c - a))) return abs(c - a); if (isLEZero(dot(a - b, c - b))) return abs(c - b); return abs(det(b - a, c - a)) / abs(b - a); } bool isIntersectedLine(C a1, C a2, C b1, C b2) { // a1-a2, b1-b2 return !isLineParallel(a1, a2, b1, b2); } C intersectionLine(C a1, C a2, C b1, C b2) { // isIntersectedLine-> true C a = a2 - a1, b = b2 - b1; return a1 + a * det(b, b1 - a1) / det(b, a); } /**** NG Words ****/ // cx cy P Q C // Warning: EPS /**** main function ****/ ll n, ga, gb, sa, sb, ba, bb; ll search(ll n, ll g, ll s, ll b, ll dg, ll ds, ll db) { ll ans = 0; ll mg = ((dg > 0) ? n / g : 1); ll ms = ((ds > 0) ? n / s : 1); ll mb = ((db > 0) ? n / b : 1); if (mb < mg) swap(b, g), swap(mb, mg), swap(db, dg); if (mb < ms) swap(b, s), swap(mb, ms), swap(db, ds); for (ll i = 0; i <= mg; i++) for (ll j = 0; j <= ((ds > 0) ? n / s : 1); j++) { ll sum = g * i + s * j; if (sum > n) continue; ll k = (db > 0) ? (n - sum) / b : 0; ans = max(ans, dg * i + ds * j + db * k); } return ans; } int main() { scanf("%lld", &n); scanf("%lld%lld%lld", &ga, &sa, &ba); scanf("%lld%lld%lld", &gb, &sb, &bb); n += search(n, ga, sa, ba, gb - ga, sb - sa, bb - ba); n += search(n, gb, sb, bb, ga - gb, sa - sb, ba - bb); printf("%lld\n", n); }
replace
776
777
776
784
TLE
p03008
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string.h> #include <string> #include <vector> // output #define SPBR(w, n) std::cout << (w + 1 == n ? '\n' : ' '); #define YES cout << "YES" << endl #define Yes cout << "Yes" << endl #define NO cout << "NO" << endl #define No cout << "No" << endl // utility #define ALL(i) (i).begin(), (i).end() #define FOR(i, a, n) for (int i = (a); i < (n); ++i) #define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i) #define REP(i, n) for (int i = 0; i < int(n); ++i) #define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i) #define IN(a, x, b) (a <= x && x < b) #define OUT(a, x, b) (x < a || b <= x) template <class T> inline T chmax(T &a, const T b) { return a = (a < b) ? b : a; } template <class T> inline T chmin(T &a, const T b) { return a = (a > b) ? b : a; } // type/const #define int ll using ll = long long; using ull = unsigned long long; using ld = long double; const int MOD = 1000000007; /* const int MOD = 998244353; */ const int INF = 1e18; const double PI = acos(-1); using namespace std; struct INIT { INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } INIT; int f(int N, int a[], int b[]) { vector<int> dp(N + 1, -INF); dp[0] = 0; REP(i, N) REP(j, 3) { if (i != 0) chmax(dp[i], dp[i - 1]); if (i + a[j] <= N) chmax(dp[i + a[j]], dp[i] + b[j] - a[j]); } return dp[N] + N; } signed main() { int N; cin >> N; int a[3], b[3]; REP(i, 3) cin >> a[i]; REP(i, 3) cin >> b[i]; int ret = f(N, a, b); cout << f(ret, b, a) << "\n"; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string.h> #include <string> #include <vector> // output #define SPBR(w, n) std::cout << (w + 1 == n ? '\n' : ' '); #define YES cout << "YES" << endl #define Yes cout << "Yes" << endl #define NO cout << "NO" << endl #define No cout << "No" << endl // utility #define ALL(i) (i).begin(), (i).end() #define FOR(i, a, n) for (int i = (a); i < (n); ++i) #define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i) #define REP(i, n) for (int i = 0; i < int(n); ++i) #define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i) #define IN(a, x, b) (a <= x && x < b) #define OUT(a, x, b) (x < a || b <= x) template <class T> inline T chmax(T &a, const T b) { return a = (a < b) ? b : a; } template <class T> inline T chmin(T &a, const T b) { return a = (a > b) ? b : a; } // type/const #define int ll using ll = long long; using ull = unsigned long long; using ld = long double; const int MOD = 1000000007; /* const int MOD = 998244353; */ const int INF = 1e18; const double PI = acos(-1); using namespace std; struct INIT { INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } INIT; int f(int N, int a[], int b[]) { vector<int> dp(N + 1, -INF); dp[0] = 0; REP(i, N + 1) REP(j, 3) { if (i != 0) chmax(dp[i], dp[i - 1]); if (i + a[j] <= N) chmax(dp[i + a[j]], dp[i] + b[j] - a[j]); } return dp[N] + N; } signed main() { int N; cin >> N; int a[3], b[3]; REP(i, 3) cin >> a[i]; REP(i, 3) cin >> b[i]; int ret = f(N, a, b); cout << f(ret, b, a) << "\n"; return 0; }
replace
60
61
60
61
0
p03008
C++
Runtime Error
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; ///////////////////library zone!!!!!!!!!!!!!!!!!!!!!!!!!!!! typedef long long ll; typedef long double ld; #define all(a) (a).begin(), (a).end() #define EPS (1e-5) #define bit(n, k) ((n >> k) & 1) const ll Mod = 1000000007; const ll mod = 998244353; struct H { ll x, y; bool operator<(const H &b) const { if (x != b.x) return x < b.x; return y < b.y; } bool operator>(const H &b) const { if (x != b.x) return x > b.x; return y > b.y; } bool operator==(const H &b) const { return x == b.x && y == b.y; } bool operator!=(const H &b) const { return (*this) != b; } }; struct P { ll pos, cost; bool operator<(const P &b) const { return cost < b.cost; } bool operator>(const P &b) const { return cost > b.cost; } }; struct B { ll to, cost; }; struct E { ll from, to, cost; bool operator<(const E &b) const { return cost < b.cost; } bool operator>(const E &b) const { return cost > b.cost; } }; template <typename T, typename U> void chmin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> void chmax(T &a, U b) { if (a < b) a = b; } template <typename T> T max_0(T a) { if (a < 0) return 0; return a; } template <typename T> T min_0(T a) { if (a > 0) return 0; return a; } ll read() { ll u; ll k = scanf("%lld", &u); return u; } ll gcd(ll i, ll j) { if (i > j) swap(i, j); if (i == 0) return j; return gcd(j % i, i); } ll mod_pow(ll x, ll n, ll p) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % p; x = x * x % p; n >>= 1; } return res; } // x^n%p const ll Inf = 3023372036854775807; const int inf = 1500000000; #define int long long //---------------------------------------------------- int n; int ga, sa, ba, gb, sb, bb; int dp[300000]; signed main() { cin >> n >> ga >> sa >> ba >> gb >> sb >> bb; int g = 0, s = 0, b = 0; int ans = n; // AとBの換どんパートが終わった時点での最大値 for (int i = 0; i <= n; i += ga) { s = 0; for (int j = 0; i + j <= n; j += sa) { if (bb > ba) { int b = (n - i - j) / ba; ans = max(ans, n - i - j - b * ba + g * gb + s * sb + b * bb); } else { ans = max(ans, n - i - j + s * sb + g * gb); } s++; } g++; } int res = ans; // 現在ドングリ250000個 // dp[i]=使っていないドングリが残りiこの時の換金した後の増える値の最大値 // これと決めたやつしか使う必要はない if (ga > gb) { for (int i = ans; i >= 0; i--) { if (i - gb >= 0) { dp[i - gb] = max(dp[i - gb], dp[i] + ga - gb); } } } if (sa > sb) { for (int i = ans; i >= 0; i--) { if (i - sb >= 0) { dp[i - sb] = max(dp[i - sb], dp[i] + sa - sb); } } } if (ba > bb) { for (int i = ans; i >= 0; i--) { if (i - bb >= 0) { dp[i - bb] = max(dp[i - bb], dp[i] + ba - bb); } } } int rer = 0; for (int i = 0; i <= ans; i++) { rer = max(rer, ans + dp[i]); } cout << rer << endl; }
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; ///////////////////library zone!!!!!!!!!!!!!!!!!!!!!!!!!!!! typedef long long ll; typedef long double ld; #define all(a) (a).begin(), (a).end() #define EPS (1e-5) #define bit(n, k) ((n >> k) & 1) const ll Mod = 1000000007; const ll mod = 998244353; struct H { ll x, y; bool operator<(const H &b) const { if (x != b.x) return x < b.x; return y < b.y; } bool operator>(const H &b) const { if (x != b.x) return x > b.x; return y > b.y; } bool operator==(const H &b) const { return x == b.x && y == b.y; } bool operator!=(const H &b) const { return (*this) != b; } }; struct P { ll pos, cost; bool operator<(const P &b) const { return cost < b.cost; } bool operator>(const P &b) const { return cost > b.cost; } }; struct B { ll to, cost; }; struct E { ll from, to, cost; bool operator<(const E &b) const { return cost < b.cost; } bool operator>(const E &b) const { return cost > b.cost; } }; template <typename T, typename U> void chmin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> void chmax(T &a, U b) { if (a < b) a = b; } template <typename T> T max_0(T a) { if (a < 0) return 0; return a; } template <typename T> T min_0(T a) { if (a > 0) return 0; return a; } ll read() { ll u; ll k = scanf("%lld", &u); return u; } ll gcd(ll i, ll j) { if (i > j) swap(i, j); if (i == 0) return j; return gcd(j % i, i); } ll mod_pow(ll x, ll n, ll p) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % p; x = x * x % p; n >>= 1; } return res; } // x^n%p const ll Inf = 3023372036854775807; const int inf = 1500000000; #define int long long //---------------------------------------------------- int n; int ga, sa, ba, gb, sb, bb; int dp[26000000]; signed main() { cin >> n >> ga >> sa >> ba >> gb >> sb >> bb; int g = 0, s = 0, b = 0; int ans = n; // AとBの換どんパートが終わった時点での最大値 for (int i = 0; i <= n; i += ga) { s = 0; for (int j = 0; i + j <= n; j += sa) { if (bb > ba) { int b = (n - i - j) / ba; ans = max(ans, n - i - j - b * ba + g * gb + s * sb + b * bb); } else { ans = max(ans, n - i - j + s * sb + g * gb); } s++; } g++; } int res = ans; // 現在ドングリ250000個 // dp[i]=使っていないドングリが残りiこの時の換金した後の増える値の最大値 // これと決めたやつしか使う必要はない if (ga > gb) { for (int i = ans; i >= 0; i--) { if (i - gb >= 0) { dp[i - gb] = max(dp[i - gb], dp[i] + ga - gb); } } } if (sa > sb) { for (int i = ans; i >= 0; i--) { if (i - sb >= 0) { dp[i - sb] = max(dp[i - sb], dp[i] + sa - sb); } } } if (ba > bb) { for (int i = ans; i >= 0; i--) { if (i - bb >= 0) { dp[i - bb] = max(dp[i - bb], dp[i] + ba - bb); } } } int rer = 0; for (int i = 0; i <= ans; i++) { rer = max(rer, ans + dp[i]); } cout << rer << endl; }
replace
102
103
102
103
0
p03008
C++
Time Limit Exceeded
#pragma GCC optimize("Ofast") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,tune=native") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #include <functional> // for less #include <iostream> using namespace __gnu_pbds; using namespace std; #define ll long long typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; using namespace std; #define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++) #define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--) #define rep(i, n) for (long long i = 0, _n = (n); i < _n; i++) #define repr(i, n) for (long long i = (n)-1; i >= 0; i--) #define foreach(it, ar) \ for (typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++) #define fill(ar, val) memset(ar, val, sizeof(ar)) #define fill0(ar) fill((ar), 0) #define fillinf(ar, n) fr(i, 0, (n)) ar[i] = INF #define debug(x) cout << #x << ": " << x << endl #define arr1d(a, n) \ cout << #a << " : "; \ fr(_, 1, n) cout << a[_] << ' '; \ cout << endl; #define arr1d0(a, n) \ cout << #a << " : "; \ rep(_, n) cout << a[_] << ' '; \ cout << endl; #define arr2d(a, n, m) \ cout << #a << " :" << endl; \ fr(_, 1, n) { \ fr(__, 1, m) cout << a[_][__] << ' '; \ cout << endl; \ } #define arr2d0(a, n, m) \ cout << #a << " :" << endl; \ rep(_, n) { \ rep(__, m) cout << a[_][__] << ' '; \ cout << endl; \ } /*Author Ritick Goenka || ritick(codechef) ||ritick(codeforces) */ /*IIT Roorkee = <3 */ #define ull unsigned long long #define ld long double #define ui unsigned int #define all(ar) ar.begin(), ar.end() #define pb push_back #define mp make_pair #define ff first #define ss second #define y0 yyyyyy0 auto clk = clock(); #define y1 yyyyyy1 #define setb __builtin_popcount #define BIT(n) (1 << (n)) #define SQR(x) ((x) * (x)) #define CUBE(x) ((x) * (x) * (x)) #define LSOne(S) (S) & (-S) inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } typedef pair<int, int> ii; typedef pair<int, ii> iii; typedef vector<ii> vii; typedef vector<int> vi; typedef vector<string> vs; template <typename T> inline T gcd(T a, T b) { if (b == 0) return a; else return gcd(b, a % b); } // template<typename T>inline T lcm(T a, T b){return (a * b) / gcd(a, b);} template <typename T> string toStr(T x) { stringstream st; st << x; string s; st >> s; return s; } template <class T> void splitStr(const string &s, vector<T> &out) { istringstream in(s); out.clear(); copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out)); } inline int two(int n) { return 1 << n; } inline int isOnBit(int n, int b) { return (n >> b) & 1; } inline void onBit(int &n, int b) { n |= two(b); } inline void offBit(int &n, int b) { n &= ~two(b); } inline int lastBit(int n) { return n & (-n); } inline int cntBit(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } const int dx4[] = {-1, 0, 1, 0}; const int dy4[] = {0, 1, 0, -1}; const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1}; const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1}; #define INP "test.inp" #define OUT "test.out" #define PI 3.1415926535897932385 #define INF 10000000000000000ll #define EPS 1e-7 #define MAXN 100000 #define MOD 1000000009 #define dec decr #define endl '\n' mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } ll modexp(ll x, ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } pair<ll, ll> find(ll x, ll g1, ll g2) { ll x1 = x; ll f = x / g1; x -= f * g1; ll y = f * g2; ll pro = y + x - x1; if (pro >= 0) { return mp(x, x1 + pro - x); } else { return mp(x, -INF); } } ll mat[2][3]; ll lel(ll n) { ll ans = n; for (ll i = 0; i <= n / mat[0][0] + 1; i++) { for (ll j = 0; j <= n / mat[0][1] + 1; j++) { ll x1 = 0; if (i * mat[0][0] + j * mat[0][1] <= n) { x1 += (find(i * mat[0][0], mat[0][0], mat[1][0])).ss; x1 += (find(j * mat[0][1], mat[0][1], mat[1][1])).ss; auto x = (find(n - i * mat[0][0] - j * mat[0][1], mat[0][2], mat[1][2])); x1 += x.ff; x1 += x.ss; ans = max(ans, x1); } } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed; cout << setprecision(9); #ifdef rg freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll n; cin >> n; rep(i, 3) { cin >> mat[0][i]; } rep(i, 3) { cin >> mat[1][i]; } n = lel(n); // cout<<n; rep(i, 3) { swap(mat[0][i], mat[1][i]); } n = lel(n); cout << n; #ifdef rg cout << endl << endl << "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl; #endif return 0; }
#pragma GCC optimize("Ofast") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,tune=native") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #include <functional> // for less #include <iostream> using namespace __gnu_pbds; using namespace std; #define ll long long typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; using namespace std; #define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++) #define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--) #define rep(i, n) for (long long i = 0, _n = (n); i < _n; i++) #define repr(i, n) for (long long i = (n)-1; i >= 0; i--) #define foreach(it, ar) \ for (typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++) #define fill(ar, val) memset(ar, val, sizeof(ar)) #define fill0(ar) fill((ar), 0) #define fillinf(ar, n) fr(i, 0, (n)) ar[i] = INF #define debug(x) cout << #x << ": " << x << endl #define arr1d(a, n) \ cout << #a << " : "; \ fr(_, 1, n) cout << a[_] << ' '; \ cout << endl; #define arr1d0(a, n) \ cout << #a << " : "; \ rep(_, n) cout << a[_] << ' '; \ cout << endl; #define arr2d(a, n, m) \ cout << #a << " :" << endl; \ fr(_, 1, n) { \ fr(__, 1, m) cout << a[_][__] << ' '; \ cout << endl; \ } #define arr2d0(a, n, m) \ cout << #a << " :" << endl; \ rep(_, n) { \ rep(__, m) cout << a[_][__] << ' '; \ cout << endl; \ } /*Author Ritick Goenka || ritick(codechef) ||ritick(codeforces) */ /*IIT Roorkee = <3 */ #define ull unsigned long long #define ld long double #define ui unsigned int #define all(ar) ar.begin(), ar.end() #define pb push_back #define mp make_pair #define ff first #define ss second #define y0 yyyyyy0 auto clk = clock(); #define y1 yyyyyy1 #define setb __builtin_popcount #define BIT(n) (1 << (n)) #define SQR(x) ((x) * (x)) #define CUBE(x) ((x) * (x) * (x)) #define LSOne(S) (S) & (-S) inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } typedef pair<int, int> ii; typedef pair<int, ii> iii; typedef vector<ii> vii; typedef vector<int> vi; typedef vector<string> vs; template <typename T> inline T gcd(T a, T b) { if (b == 0) return a; else return gcd(b, a % b); } // template<typename T>inline T lcm(T a, T b){return (a * b) / gcd(a, b);} template <typename T> string toStr(T x) { stringstream st; st << x; string s; st >> s; return s; } template <class T> void splitStr(const string &s, vector<T> &out) { istringstream in(s); out.clear(); copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out)); } inline int two(int n) { return 1 << n; } inline int isOnBit(int n, int b) { return (n >> b) & 1; } inline void onBit(int &n, int b) { n |= two(b); } inline void offBit(int &n, int b) { n &= ~two(b); } inline int lastBit(int n) { return n & (-n); } inline int cntBit(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } const int dx4[] = {-1, 0, 1, 0}; const int dy4[] = {0, 1, 0, -1}; const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1}; const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1}; #define INP "test.inp" #define OUT "test.out" #define PI 3.1415926535897932385 #define INF 10000000000000000ll #define EPS 1e-7 #define MAXN 100000 #define MOD 1000000009 #define dec decr #define endl '\n' mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } ll modexp(ll x, ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } pair<ll, ll> find(ll x, ll g1, ll g2) { ll x1 = x; ll f = x / g1; x -= f * g1; ll y = f * g2; ll pro = y + x - x1; if (pro >= 0) { return mp(x, x1 + pro - x); } else { return mp(x, -INF); } } ll mat[2][3]; ll lel(ll n) { ll ans = n; ll ax[3] = {0}; ll s = 0; rep(i, 3) { if (mat[0][i] < mat[1][i]) { ax[i] = 1; s++; } } if (s == 3) { for (ll i = 0; i <= n / mat[0][0] + 1; i++) { for (ll j = 0; j <= n / mat[0][1] + 1; j++) { ll x1 = 0; if (i * mat[0][0] + j * mat[0][1] <= n) { x1 += (find(i * mat[0][0], mat[0][0], mat[1][0])).ss; x1 += (find(j * mat[0][1], mat[0][1], mat[1][1])).ss; auto x = (find(n - i * mat[0][0] - j * mat[0][1], mat[0][2], mat[1][2])); x1 += x.ff; x1 += x.ss; ans = max(ans, x1); } } } } if (s == 1) { if (ax[0] == 1) { auto x = (find(n, mat[0][0], mat[1][0])); ans = max(ans, x.ff + x.ss); } else if (ax[1] == 1) { auto x = (find(n, mat[0][1], mat[1][1])); ans = max(ans, x.ff + x.ss); } else { auto x = (find(n, mat[0][2], mat[1][2])); ans = max(ans, x.ff + x.ss); } } else if (s == 2) { if (ax[0] == 0) { for (ll i = 0; i <= n / mat[0][1] + 1; i++) { ll x1 = 0; if (i * mat[0][1] <= n) { x1 += (find(i * mat[0][1], mat[0][1], mat[1][1])).ss; auto x = (find(n - i * mat[0][1], mat[0][2], mat[1][2])); x1 += x.ff; x1 += x.ss; ans = max(ans, x1); } } } else if (ax[1] == 0) { for (ll i = 0; i <= n / mat[0][2] + 1; i++) { ll x1 = 0; if (i * mat[0][2] <= n) { x1 += (find(i * mat[0][2], mat[0][2], mat[1][2])).ss; auto x = (find(n - i * mat[0][2], mat[0][0], mat[1][0])); x1 += x.ff; x1 += x.ss; ans = max(ans, x1); } } } else { for (ll i = 0; i <= n / mat[0][1] + 1; i++) { ll x1 = 0; if (i * mat[0][1] <= n) { x1 += (find(i * mat[0][1], mat[0][1], mat[1][1])).ss; auto x = (find(n - i * mat[0][1], mat[0][0], mat[1][0])); x1 += x.ff; x1 += x.ss; ans = max(ans, x1); } } } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed; cout << setprecision(9); #ifdef rg freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll n; cin >> n; rep(i, 3) { cin >> mat[0][i]; } rep(i, 3) { cin >> mat[1][i]; } n = lel(n); // cout<<n; rep(i, 3) { swap(mat[0][i], mat[1][i]); } n = lel(n); cout << n; #ifdef rg cout << endl << endl << "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl; #endif return 0; }
replace
152
163
152
220
TLE
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; #define fi first #define se second #define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) #define rep(i, n) repl(i, 0, n) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x "=" << x << endl #define mmax(x, y) (x > y ? x : y) #define mmin(x, y) (x < y ? x : y) #define maxch(x, y) x = mmax(x, y) #define minch(x, y) x = mmin(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt __builtin_popcountll #define INF 1e16 #define mod 1000000007 ll n; ll g[2], s[2], b[2]; ll dp[30000000]; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; rep(i, 2) cin >> g[i] >> s[i] >> b[i]; rep(i, 2) { ll j = 1 - i; rep(k, n + 1) dp[k] = -INF; dp[0] = 0; rep(k, n + 1) { if (k - g[i] >= 0) maxch(dp[k], dp[k - g[i]] + max(g[i], g[j])); if (k - s[i] >= 0) maxch(dp[k], dp[k - s[i]] + max(s[i], s[j])); if (k - b[i] >= 0) maxch(dp[k], dp[k - b[i]] + max(b[i], b[j])); } ll nxt = dp[n]; n = nxt; } cout << n << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; #define fi first #define se second #define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) #define rep(i, n) repl(i, 0, n) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x "=" << x << endl #define mmax(x, y) (x > y ? x : y) #define mmin(x, y) (x < y ? x : y) #define maxch(x, y) x = mmax(x, y) #define minch(x, y) x = mmin(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt __builtin_popcountll #define INF 1e16 #define mod 1000000007 ll n; ll g[2], s[2], b[2]; ll dp[30000000]; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; rep(i, 2) cin >> g[i] >> s[i] >> b[i]; rep(i, 2) { ll j = 1 - i; rep(k, n + 1) dp[k] = k; dp[0] = 0; rep(k, n + 1) { if (k - g[i] >= 0) maxch(dp[k], dp[k - g[i]] + max(g[i], g[j])); if (k - s[i] >= 0) maxch(dp[k], dp[k - s[i]] + max(s[i], s[j])); if (k - b[i] >= 0) maxch(dp[k], dp[k - b[i]] + max(b[i], b[j])); } ll nxt = dp[n]; n = nxt; } cout << n << endl; return 0; }
replace
37
38
37
38
-11
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define fi first #define se second #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rep1(i, n) for (int i = 1; i <= (int)(n); ++i) #define rep11(i, n) for (int i = 1; i < (int)(n); ++i) #define repo(i, o, n) for (int i = o; i < (int)(n); ++i) #define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define sperase(v, n) (v).erase(remove(all(v), n), (v).end()) #define vdelete(v) (v).erase(unique(all(v)), (v).end()) #define pb(n) push_back(n) #define mp make_pair #define MOD 1000000007 #define INF 9223372036854775807 int n, memo[10][10], dp[10000]; signed main() { cin >> n; rep(i, 2) rep(j, 3) cin >> memo[j][i]; rep(i, 3) { rep(j, n - memo[i][0] + 1) { dp[j + memo[i][0]] = max(dp[j + memo[i][0]], dp[j] + memo[i][1]); } } rep1(i, n) dp[i] = max(dp[i], dp[i - 1] + 1); n = dp[n]; rep(i, 6000) dp[i] = 0; rep(i, 3) swap(memo[i][0], memo[i][1]); rep(i, 3) { rep(j, n - memo[i][0] + 1) { dp[j + memo[i][0]] = max(dp[j + memo[i][0]], dp[j] + memo[i][1]); } } rep1(i, n) dp[i] = max(dp[i], dp[i - 1] + 1); cout << dp[n] << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long #define fi first #define se second #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rep1(i, n) for (int i = 1; i <= (int)(n); ++i) #define rep11(i, n) for (int i = 1; i < (int)(n); ++i) #define repo(i, o, n) for (int i = o; i < (int)(n); ++i) #define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define sperase(v, n) (v).erase(remove(all(v), n), (v).end()) #define vdelete(v) (v).erase(unique(all(v)), (v).end()) #define pb(n) push_back(n) #define mp make_pair #define MOD 1000000007 #define INF 9223372036854775807 int n, memo[10][10], dp[25000000]; signed main() { cin >> n; rep(i, 2) rep(j, 3) cin >> memo[j][i]; rep(i, 3) { rep(j, n - memo[i][0] + 1) { dp[j + memo[i][0]] = max(dp[j + memo[i][0]], dp[j] + memo[i][1]); } } rep1(i, n) dp[i] = max(dp[i], dp[i - 1] + 1); n = dp[n]; rep(i, 6000) dp[i] = 0; rep(i, 3) swap(memo[i][0], memo[i][1]); rep(i, 3) { rep(j, n - memo[i][0] + 1) { dp[j + memo[i][0]] = max(dp[j + memo[i][0]], dp[j] + memo[i][1]); } } rep1(i, n) dp[i] = max(dp[i], dp[i - 1] + 1); cout << dp[n] << endl; }
replace
20
21
20
21
0
p03008
C++
Runtime Error
// by xxj #include <bits/stdc++.h> using namespace std; #define fst first #define snd second #define mp make_pair #define ll long long #define pii pair<int, int> #define lowbit(x) x & -x const int inf = 1e9 + 7; const double eps = 1e-10; const ll linf = 1e18 + 7; const ll hh = 523; // const int mod=; ll dp[5007]; int a[3], b[3]; int main() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); int n; scanf("%d", &n); for (int i = 0; i < 3; i++) { scanf("%d", a + i); } for (int i = 0; i < 3; i++) { scanf("%d", b + i); } memset(dp, -1, sizeof(dp)); dp[0] = 0; ll mx = n; for (int i = 0; i < 3; i++) { for (int j = a[i]; j <= n; j++) { if (dp[j - a[i]] != -1) { dp[j] = max(dp[j], dp[j - a[i]] + b[i]); mx = max(mx, n - j + dp[j]); } } } memset(dp, -1, sizeof(dp)); dp[0] = 0; ll ans = mx; for (int i = 0; i < 3; i++) { for (int j = b[i]; j <= mx; j++) { if (dp[j - b[i]] != -1) { dp[j] = max(dp[j], dp[j - b[i]] + a[i]); ans = max(ans, dp[j] + mx - j); } } } printf("%lld\n", ans); return 0; } /* input: */
// by xxj #include <bits/stdc++.h> using namespace std; #define fst first #define snd second #define mp make_pair #define ll long long #define pii pair<int, int> #define lowbit(x) x & -x const int inf = 1e9 + 7; const double eps = 1e-10; const ll linf = 1e18 + 7; const ll hh = 523; // const int mod=; ll dp[25000007]; int a[3], b[3]; int main() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); int n; scanf("%d", &n); for (int i = 0; i < 3; i++) { scanf("%d", a + i); } for (int i = 0; i < 3; i++) { scanf("%d", b + i); } memset(dp, -1, sizeof(dp)); dp[0] = 0; ll mx = n; for (int i = 0; i < 3; i++) { for (int j = a[i]; j <= n; j++) { if (dp[j - a[i]] != -1) { dp[j] = max(dp[j], dp[j - a[i]] + b[i]); mx = max(mx, n - j + dp[j]); } } } memset(dp, -1, sizeof(dp)); dp[0] = 0; ll ans = mx; for (int i = 0; i < 3; i++) { for (int j = b[i]; j <= mx; j++) { if (dp[j - b[i]] != -1) { dp[j] = max(dp[j], dp[j - b[i]] + a[i]); ans = max(ans, dp[j] + mx - j); } } } printf("%lld\n", ans); return 0; } /* input: */
replace
14
15
14
15
0
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define LL long long #define mp make_pair #define pb push_back #define pii pair<int, int> #define pll pair<LL, LL> #define x first #define y second #define pi acos(-1) #define sqr(x) ((x) * (x)) #define pdd pair<double, double> #define MEMS(x) memset(x, -1, sizeof(x)) #define MEM(x) memset(x, 0, sizeof(x)) #define N 200005 #define rank Rank #define index Index LL dp[2500005]; int main() { LL n; scanf("%lld", &n); pll p[4]; scanf("%lld %lld %lld %lld %lld %lld", &p[0].x, &p[1].x, &p[2].x, &p[0].y, &p[1].y, &p[2].y); p[3] = mp(1, 1); MEM(dp); for (int i = 0; i < 4; i++) { for (int j = 0; j <= n - p[i].x; j++) { dp[j + p[i].x] = max(dp[j + p[i].x], dp[j] + p[i].y); } swap(p[i].x, p[i].y); } LL temp = 0; for (int i = 0; i <= n; i++) temp = max(temp, dp[i]); // printf("%lld\n",temp); n = temp; MEM(dp); for (int i = 0; i < 4; i++) { for (int j = 0; j <= n - p[i].x; j++) { dp[j + p[i].x] = max(dp[j + p[i].x], dp[j] + p[i].y); } // printf("%d %d\n",p[i].x,p[i].y); } printf("%lld\n", dp[n]); return 0; }
#include <bits/stdc++.h> using namespace std; #define LL long long #define mp make_pair #define pb push_back #define pii pair<int, int> #define pll pair<LL, LL> #define x first #define y second #define pi acos(-1) #define sqr(x) ((x) * (x)) #define pdd pair<double, double> #define MEMS(x) memset(x, -1, sizeof(x)) #define MEM(x) memset(x, 0, sizeof(x)) #define N 200005 #define rank Rank #define index Index LL dp[25000005]; int main() { LL n; scanf("%lld", &n); pll p[4]; scanf("%lld %lld %lld %lld %lld %lld", &p[0].x, &p[1].x, &p[2].x, &p[0].y, &p[1].y, &p[2].y); p[3] = mp(1, 1); MEM(dp); for (int i = 0; i < 4; i++) { for (int j = 0; j <= n - p[i].x; j++) { dp[j + p[i].x] = max(dp[j + p[i].x], dp[j] + p[i].y); } swap(p[i].x, p[i].y); } LL temp = 0; for (int i = 0; i <= n; i++) temp = max(temp, dp[i]); // printf("%lld\n",temp); n = temp; MEM(dp); for (int i = 0; i < 4; i++) { for (int j = 0; j <= n - p[i].x; j++) { dp[j + p[i].x] = max(dp[j + p[i].x], dp[j] + p[i].y); } // printf("%d %d\n",p[i].x,p[i].y); } printf("%lld\n", dp[n]); return 0; }
replace
17
18
17
18
0
p03008
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; typedef long long ll; int main() { int n; ll a[3], b[3]; cin >> n; for (int i = 0; i < 3; i++) { cin >> a[i]; } for (int i = 0; i < 3; i++) { cin >> b[i]; } ll maxa2b = n; for (int x = 0; x <= n; x++) { if (a[0] * x > n) break; for (int y = 0; y <= n; y++) { if (a[1] * y > n - a[0] * x) break; int z = (n - a[0] * x - a[1] * y) / a[2]; ll change = b[0] * x + b[1] * y + b[2] * z + (n - a[0] * x - a[1] * y) % a[2]; maxa2b = max(maxa2b, change); } } ll maxb2a = maxa2b; for (int x = 0; x <= maxa2b; x++) { if (b[0] * x > maxa2b) break; for (int y = 0; y <= maxa2b; y++) { if (b[1] * y > maxa2b - b[0] * x) break; int z = (maxa2b - b[0] * x - b[1] * y) / b[2]; ll change = a[0] * x + a[1] * y + a[2] * z + (maxa2b - b[0] * x - b[1] * y) % b[2]; maxb2a = max(maxb2a, change); } } cout << maxb2a << endl; }
#include <algorithm> #include <iostream> using namespace std; typedef long long ll; int main() { int n; ll a[3], b[3]; cin >> n; for (int i = 0; i < 3; i++) { cin >> a[i]; } for (int i = 0; i < 3; i++) { cin >> b[i]; } ll maxa2b = n; for (int x = 0; x <= n; x++) { if (a[0] * x > n) break; for (int y = 0; y <= n; y++) { if (a[1] * y > n - a[0] * x) break; int z = (n - a[0] * x - a[1] * y) / a[2]; ll change = b[0] * x + b[1] * y + b[2] * z + (n - a[0] * x - a[1] * y) % a[2]; maxa2b = max(maxa2b, change); } } ll maxb2a = maxa2b; if (a[0] < b[0] && a[1] < b[1] && a[2] < b[2]) { cout << maxb2a << endl; return 0; } if (a[0] < b[0]) { for (int y = 0; y <= maxa2b; y++) { if (b[1] * y > maxa2b) break; int z = (maxa2b - b[1] * y) / b[2]; ll change = a[1] * y + a[2] * z + (maxa2b - b[1] * y) % b[2]; maxb2a = max(maxb2a, change); } cout << maxb2a << endl; return 0; } if (a[1] < b[1]) { for (int x = 0; x <= maxa2b; x++) { if (b[0] * x > maxa2b) break; int z = (maxa2b - b[0] * x) / b[2]; ll change = a[0] * x + a[2] * z + (maxa2b - b[0] * x) % b[2]; maxb2a = max(maxb2a, change); } cout << maxb2a << endl; return 0; } if (a[2] < b[2]) { for (int y = 0; y <= maxa2b; y++) { if (b[1] * y > maxa2b) break; int x = (maxa2b - b[1] * y) / b[0]; ll change = a[1] * y + a[0] * x + (maxa2b - b[1] * y) % b[0]; maxb2a = max(maxb2a, change); } cout << maxb2a << endl; return 0; } for (int x = 0; x <= maxa2b; x++) { if (b[0] * x > maxa2b) break; for (int y = 0; y <= maxa2b; y++) { if (b[1] * y > maxa2b - b[0] * x) break; int z = (maxa2b - b[0] * x - b[1] * y) / b[2]; ll change = a[0] * x + a[1] * y + a[2] * z + (maxa2b - b[0] * x - b[1] * y) % b[2]; maxb2a = max(maxb2a, change); } } cout << maxb2a << endl; }
insert
29
29
29
66
TLE
p03008
C++
Time Limit Exceeded
#include <iostream> long long trade(long long n, int g_A, int s_A, int b_A, int g_B, int s_B, int b_B) { long long n_Max = n; for (int g = 0; g * g_A <= n; g++) { if ((g_B - g_A <= 0) && (g > 0)) { break; } for (int s = 0; g * g_A + s * s_A <= n; s++) { if ((s_B - s_A <= 0) && (s > 0)) { break; } int b = 0; if (b_B - b_A > 0) { b = (n - g * g_A - s * s_A) / b_A; } long long n_New = n + (long long)g * (g_B - g_A) + (long long)s * (s_B - s_A) + (long long)b * (b_B - b_A); if (n_New > n_Max) { n_Max = n_New; } } } return n_Max; } int main(int argc, char *argv[]) { long long n; int g_A, g_B; int s_A, s_B; int b_A, b_B; int ans; std::cin >> n; std::cin >> g_A >> s_A >> b_A; std::cin >> g_B >> s_B >> b_B; // 取引所 A -> B n = trade(n, g_A, s_A, b_A, g_B, s_B, b_B); ; // 取引所 B -> A n = trade(n, g_B, s_B, b_B, g_A, s_A, b_A); std::cout << n << std::endl; return 0; }
#include <iostream> long long trade(long long n, int g_A, int s_A, int b_A, int g_B, int s_B, int b_B) { long long n_Max = n; for (int g = 0; g * g_A <= n; g++) { if ((g_B - g_A <= 0) && (g > 0)) { break; } for (int s = 0; g * g_A + s * s_A <= n; s++) { if ((s_B - s_A <= 0) && (s > 0)) { break; } int b = 0; if (b_B - b_A > 0) { b = (n - g * g_A - s * s_A) / b_A; } else { s = (n - g * g_A) / s_A; } long long n_New = n + (long long)g * (g_B - g_A) + (long long)s * (s_B - s_A) + (long long)b * (b_B - b_A); if (n_New > n_Max) { n_Max = n_New; } } } return n_Max; } int main(int argc, char *argv[]) { long long n; int g_A, g_B; int s_A, s_B; int b_A, b_B; int ans; std::cin >> n; std::cin >> g_A >> s_A >> b_A; std::cin >> g_B >> s_B >> b_B; // 取引所 A -> B n = trade(n, g_A, s_A, b_A, g_B, s_B, b_B); ; // 取引所 B -> A n = trade(n, g_B, s_B, b_B, g_A, s_A, b_A); std::cout << n << std::endl; return 0; }
insert
19
19
19
21
TLE
p03008
C++
Time Limit Exceeded
#include <algorithm> #include <cstring> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <math.h> #include <queue> #include <string> #include <tuple> #include <vector> using namespace std; #define MOD (long long int)(1e9 + 7) #define ll long long int #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, n) for (int i = 1; i <= (int)(n); i++) #define REP(i, n) for (int i = n - 1; i >= 0; i--) #define REPS(i, n) for (int i = n; i > 0; i--) #define INF (int)(1123456789) #define LINF (long long int)(112345678901234567) #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) #define all(v) v.begin(), v.end() int main(void) { ll n; cin >> n; ll g1, s1, b1, g2, s2, b2; cin >> g1 >> s1 >> b1; cin >> g2 >> s2 >> b2; ll don = n; ll max_value = 0; rep(gi, don / g1 + 1) { rep(si, don / s1 + 1) { ll sum = g1 * gi + s1 * si; ll remain = don - sum; if (remain < 0) break; chmax(max_value, (g2 - g1) * gi + (s2 - s1) * si + max((ll)0, (b2 - b1) * (remain / b1))); } } don += max_value; max_value = 0; rep(gi, don / g2 + 1) { rep(si, don / s2 + 1) { ll sum = g2 * gi + s2 * si; ll remain = don - sum; if (remain < 0) break; chmax(max_value, (g1 - g2) * gi + (s1 - s2) * si + max((ll)0, (b1 - b2) * (remain / b2))); } } don += max_value; cout << don << endl; /*ll n; cin>>n; ll in1,in2,in3; cin>>in1>>in2>>in3; ll in4,in5,in6; cin>>in4>>in5>>in6; ll sa1,sa2,sa3; sa1 = in4-in1; sa2 = in5-in2; sa3 = in6-in3; ll g1,s1,b1,g2,s2,b2; if(sa1 >= sa2 && sa1 >= sa3){ g1 = in1; g2 = in4; if(sa2 >= sa3){ s1 = in2; s2 = in5; b1 = in3; b2 = in6; }else{ s1 = in3; s2 = in6; b1 = in2; b2 = in5; } }else if(sa2 >= sa3){ g1 = in2; g2 = in5; if(sa1 >= sa3){ s1 = in1; s2 = in4; b1 = in3; b2 = in6; }else{ s1 = in3; s2 = in6; b1 = in1; b2 = in4; } }else{ g1 = in3; g2 = in6; if(sa1 >= sa2){ s1 = in1; s2 = in4; b1 = in2; b2 = in5; }else{ s1 = in2; s2 = in5; b1 = in1; b2 = in4; } } //利益はg>s>b //第一世代のどんぐり数 ll don = n; if(g2 - g1 <= 0){ }else if(s2 - s1 <= 0){ don = don + (g2 - g1) * (don / g1); }else if(b2 - b1 <= 0){ ll max = 0; rep(gi, don/g1){ rep(si, don/s1){ //gi*g1個とsi*s1個それぞれ買う if(gi*g1 + si*s1 > don) break; chmax(max, (g2-g1) * gi + (s2-s1) * si); } } don = don + max; }else{ //得られる利益、残りの個数 vector<pair<ll, ll>> GS; rep(gi, don/g1){ rep(si, don/s1){ if(gi*g1 + si*s1 > don) break; GS.push_back(pair<ll,ll>{(g2-g1) * gi + (s2-s1) * si, don - gi*g1 + si*s1}); } } sort(all(GS)); reverse(all(GS)); //利益が高く、残り個数が大きいものを高評価としてsort ll max = 0; rep(i, GS.size()){ ll remain = GS[i].second; chmax(max, GS[i].first + (b2-b1) * (remain / b1)); } don = don + max; } cout<<don<<endl; //第二世代を計算 ll tmp; tmp = g1;g1 = g2;g2 = tmp; tmp = s1;s1 = s2;s2 = tmp; tmp = b1;b1 = b2;b2 = tmp; tmp = g1;g1 = b1;b1 = tmp; tmp = g2;g2 = b2;b2 = tmp; if(g2 - g1 <= 0){ }else if(s2 - s1 <= 0){ don = don + (g2 - g1) * (don / g1); }else if(b2 - b1 <= 0){ ll max = 0; rep(gi, don/g1){ rep(si, don/s1){ //gi*g1個とsi*s1個それぞれ買う if(gi*g1 + si*s1 > don) break; chmax(max, (g2-g1) * gi + (s2-s1) * si); } } don = don + max; }else{ //得られる利益、残りの個数 vector<pair<ll, ll>> GS; rep(gi, don/g1){ rep(si, don/s1){ if(gi*g1 + si*s1 > don) break; GS.push_back(pair<ll,ll>{(g2-g1) * gi + (s2-s1) * si, don - gi*g1 + si*s1}); } } sort(all(GS)); reverse(all(GS)); //利益が高く、残り個数が大きいものを高評価としてsort ll max = 0; rep(i, GS.size()){ ll remain = GS[i].second; chmax(max, GS[i].first + (b2-b1) * (remain / b1)); } don = don + max; } cout<<don<<endl; return 0;*/ }
#include <algorithm> #include <cstring> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <math.h> #include <queue> #include <string> #include <tuple> #include <vector> using namespace std; #define MOD (long long int)(1e9 + 7) #define ll long long int #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, n) for (int i = 1; i <= (int)(n); i++) #define REP(i, n) for (int i = n - 1; i >= 0; i--) #define REPS(i, n) for (int i = n; i > 0; i--) #define INF (int)(1123456789) #define LINF (long long int)(112345678901234567) #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) #define all(v) v.begin(), v.end() int main(void) { ll n; cin >> n; ll g1, s1, b1, g2, s2, b2; cin >> g1 >> s1 >> b1; cin >> g2 >> s2 >> b2; ll don = n; ll max_value = 0; rep(gi, don / g1 + 1) { rep(si, don / s1 + 1) { ll sum = g1 * gi + s1 * si; ll remain = don - sum; if (remain < 0) break; chmax(max_value, (g2 - g1) * gi + (s2 - s1) * si + max((ll)0, (b2 - b1) * (remain / b1))); } } don += max_value; if (max_value > 0) { ll x1, x2, y1, y2; // a2Toa1, b2Tob1 if (g2 - g1 > 0) { x1 = s1; x2 = s2; y1 = b1; y2 = b2; } else if (s2 - s1 > 0) { x1 = g1; x2 = g2; y1 = b1; y2 = b2; } else { x1 = g1; x2 = g2; y1 = s1; y2 = s2; } ll max_value = 0; rep(xi, don / x2 + 1) { ll sum = x2 * xi; ll remain = don - sum; if (remain < 0) break; chmax(max_value, (x1 - x2) * xi + max((ll)0, (y1 - y2) * (remain / y2))); } don += max_value; cout << don << endl; return 0; } max_value = 0; rep(gi, don / g2 + 1) { rep(si, don / s2 + 1) { ll sum = g2 * gi + s2 * si; ll remain = don - sum; if (remain < 0) break; chmax(max_value, (g1 - g2) * gi + (s1 - s2) * si + max((ll)0, (b1 - b2) * (remain / b2))); } } don += max_value; cout << don << endl; /*ll n; cin>>n; ll in1,in2,in3; cin>>in1>>in2>>in3; ll in4,in5,in6; cin>>in4>>in5>>in6; ll sa1,sa2,sa3; sa1 = in4-in1; sa2 = in5-in2; sa3 = in6-in3; ll g1,s1,b1,g2,s2,b2; if(sa1 >= sa2 && sa1 >= sa3){ g1 = in1; g2 = in4; if(sa2 >= sa3){ s1 = in2; s2 = in5; b1 = in3; b2 = in6; }else{ s1 = in3; s2 = in6; b1 = in2; b2 = in5; } }else if(sa2 >= sa3){ g1 = in2; g2 = in5; if(sa1 >= sa3){ s1 = in1; s2 = in4; b1 = in3; b2 = in6; }else{ s1 = in3; s2 = in6; b1 = in1; b2 = in4; } }else{ g1 = in3; g2 = in6; if(sa1 >= sa2){ s1 = in1; s2 = in4; b1 = in2; b2 = in5; }else{ s1 = in2; s2 = in5; b1 = in1; b2 = in4; } } //利益はg>s>b //第一世代のどんぐり数 ll don = n; if(g2 - g1 <= 0){ }else if(s2 - s1 <= 0){ don = don + (g2 - g1) * (don / g1); }else if(b2 - b1 <= 0){ ll max = 0; rep(gi, don/g1){ rep(si, don/s1){ //gi*g1個とsi*s1個それぞれ買う if(gi*g1 + si*s1 > don) break; chmax(max, (g2-g1) * gi + (s2-s1) * si); } } don = don + max; }else{ //得られる利益、残りの個数 vector<pair<ll, ll>> GS; rep(gi, don/g1){ rep(si, don/s1){ if(gi*g1 + si*s1 > don) break; GS.push_back(pair<ll,ll>{(g2-g1) * gi + (s2-s1) * si, don - gi*g1 + si*s1}); } } sort(all(GS)); reverse(all(GS)); //利益が高く、残り個数が大きいものを高評価としてsort ll max = 0; rep(i, GS.size()){ ll remain = GS[i].second; chmax(max, GS[i].first + (b2-b1) * (remain / b1)); } don = don + max; } cout<<don<<endl; //第二世代を計算 ll tmp; tmp = g1;g1 = g2;g2 = tmp; tmp = s1;s1 = s2;s2 = tmp; tmp = b1;b1 = b2;b2 = tmp; tmp = g1;g1 = b1;b1 = tmp; tmp = g2;g2 = b2;b2 = tmp; if(g2 - g1 <= 0){ }else if(s2 - s1 <= 0){ don = don + (g2 - g1) * (don / g1); }else if(b2 - b1 <= 0){ ll max = 0; rep(gi, don/g1){ rep(si, don/s1){ //gi*g1個とsi*s1個それぞれ買う if(gi*g1 + si*s1 > don) break; chmax(max, (g2-g1) * gi + (s2-s1) * si); } } don = don + max; }else{ //得られる利益、残りの個数 vector<pair<ll, ll>> GS; rep(gi, don/g1){ rep(si, don/s1){ if(gi*g1 + si*s1 > don) break; GS.push_back(pair<ll,ll>{(g2-g1) * gi + (s2-s1) * si, don - gi*g1 + si*s1}); } } sort(all(GS)); reverse(all(GS)); //利益が高く、残り個数が大きいものを高評価としてsort ll max = 0; rep(i, GS.size()){ ll remain = GS[i].second; chmax(max, GS[i].first + (b2-b1) * (remain / b1)); } don = don + max; } cout<<don<<endl; return 0;*/ }
insert
45
45
45
77
TLE
p03008
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N; cin >> N; long ga, sa, ba, gb, sb, bb; cin >> ga >> sa >> ba >> gb >> sb >> bb; struct Stat { long f, t; }; Stat stats[3] = { Stat{min(ga, gb), max(ga, gb)}, Stat{min(sa, sb), max(sa, sb)}, Stat{min(ba, bb), max(ba, bb)}, }; bool st[3] = {ga <= gb, sa <= sb, ba <= bb}; long NN = 0; for (int i = N / stats[0].f; i >= 0; i--) { if (!st[0]) i = 0; auto nn = N - stats[0].f * i; long ss = stats[0].t * i; for (int j = nn / stats[1].f; j >= 0; j--) { if (!st[1]) j = 0; auto nnn = nn - stats[1].f * j; auto k = nnn / stats[2].f; if (!st[2]) k = 0; long score = ss + stats[1].t * j + stats[2].t * k + nnn - stats[2].f * k; NN = max(NN, score); } } long ans = 0; for (int i = NN / stats[0].f; i >= 0; i--) { if (st[0]) i = 0; auto nn = NN - stats[0].f * i; long ss = stats[0].t * i; for (int j = nn / stats[1].f; j >= 0; j--) { if (st[1]) j = 0; auto nnn = nn - stats[1].f * j; auto k = nnn / stats[2].f; if (st[2]) k = 0; long score = ss + stats[1].t * j + stats[2].t * k + nnn - stats[2].f * k; ans = max(ans, score); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N; cin >> N; long ga, sa, ba, gb, sb, bb; cin >> ga >> sa >> ba >> gb >> sb >> bb; struct Stat { long f, t; }; Stat stats[3] = { Stat{min(ga, gb), max(ga, gb)}, Stat{min(sa, sb), max(sa, sb)}, Stat{min(ba, bb), max(ba, bb)}, }; bool st[3] = {ga <= gb, sa <= sb, ba <= bb}; long NN = 0; for (int i = N / stats[0].f; i >= 0; i--) { if (!st[0]) i = 0; auto nn = N - stats[0].f * i; long ss = stats[0].t * i; for (int j = nn / stats[1].f; j >= 0; j--) { if (!st[1]) j = 0; auto nnn = nn - stats[1].f * j; auto k = nnn / stats[2].f; if (!st[2]) k = 0; long score = ss + stats[1].t * j + stats[2].t * k + nnn - stats[2].f * k; NN = max(NN, score); } } long ans = 0; for (int i = NN / stats[0].f; i >= 0; i--) { if (st[0]) i = 0; auto nn = NN - stats[0].f * i; long ss = stats[0].t * i; for (int j = nn / stats[1].f; j >= 0; j--) { if (st[1]) j = 0; auto nnn = nn - stats[1].f * j; auto k = nnn / stats[2].f; if (st[2]) k = 0; long score = ss + stats[1].t * j + stats[2].t * k + nnn - stats[2].f * k; ans = max(ans, score); if (st[2]) j = 0; } } cout << ans << endl; return 0; }
insert
54
54
54
56
TLE
p03008
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <vector> using namespace std; typedef long long ll; ll get(ll n, vector<ll> buy, vector<ll> sell) { ll ret = n; for (int i = 0; i * buy[0] <= n; i++) { for (int j = 0; i * buy[0] + j * buy[1] <= n; j++) { ret = max(ret, n + i * (sell[0] - buy[0]) + j * (sell[1] - buy[1])); int k = (n - i * buy[0] - j * buy[1]) / buy[2]; ret = max(ret, n + i * (sell[0] - buy[0]) + j * (sell[1] - buy[1]) + k * (sell[2] - buy[2])); } } return ret; } void solve() { ll n; vector<ll> a, b; a.resize(3); b.resize(3); cin >> n; for (int i = 0; i < 3; i++) cin >> a[i]; for (int i = 0; i < 3; i++) cin >> b[i]; n = get(n, a, b); n = get(n, b, a); cout << n << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); /* ll t; cin >> t; for(ll i = 1; i <= t; i++) { cout << "Case #" << i << ": "; solve(); } */ }
#include <algorithm> #include <bitset> #include <cassert> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <vector> using namespace std; typedef long long ll; ll get(ll n, vector<ll> buy, vector<ll> sell) { ll ret = n; vector<int> good; for (int i = 0; i < buy.size(); i++) { if (buy[i] < sell[i]) good.push_back(i); } if (good.size() == 3) { for (int i = 0; i * buy[0] <= n; i++) { if (i > 0 && buy[0] >= sell[0]) continue; for (int j = 0; i * buy[0] + j * buy[1] <= n; j++) { if (j > 0 && buy[1] >= sell[1]) continue; ret = max(ret, n + i * (sell[0] - buy[0]) + j * (sell[1] - buy[1])); int k = (n - i * buy[0] - j * buy[1]) / buy[2]; ret = max(ret, n + i * (sell[0] - buy[0]) + j * (sell[1] - buy[1]) + k * (sell[2] - buy[2])); } } } if (good.size() == 1) { ll amt = ret / buy[good[0]]; ret = max(ret, n + amt * (sell[good[0]] - buy[good[0]])); } if (good.size() == 2) { for (int i = 0; i * buy[good[0]] <= n; i++) { int j = n - i * (buy[good[0]]); j /= buy[good[1]]; ret = max(ret, n + i * (sell[good[0]] - buy[good[0]]) + j * (sell[good[1]] - buy[good[1]])); } } return ret; } void solve() { ll n; vector<ll> a, b; a.resize(3); b.resize(3); cin >> n; for (int i = 0; i < 3; i++) cin >> a[i]; for (int i = 0; i < 3; i++) cin >> b[i]; n = get(n, a, b); n = get(n, b, a); cout << n << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); /* ll t; cin >> t; for(ll i = 1; i <= t; i++) { cout << "Case #" << i << ": "; solve(); } */ }
replace
19
25
19
48
TLE
p03008
C++
Time Limit Exceeded
/*{{{*/ #include <algorithm> #include <assert.h> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <limits.h> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define FOR(I, A, B) for (int I = (A); I <= (B); ++I) #define FORS(I, S) for (int I = 0; S[I]; ++I) #define RS(X) scanf("%s", (X)) #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin()) #define CASET \ int ___T; \ scanf("%d", &___T); \ for (int cs = 1; cs <= ___T; cs++) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define F first #define S second using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PII> VPII; typedef pair<LL, LL> PLL; typedef vector<PLL> VPLL; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(LL &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <class T, class... U> void R(T &head, U &...tail) { _R(head); R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const LL &x) { printf("%lld", x); } void _W(const double &x) { printf("%.16f", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template <class T, class U> void _W(const pair<T, U> &x) { _W(x.F); putchar(' '); _W(x.S); } template <class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template <class T, class... U> void W(const T &head, const U &...tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } #ifdef HOME #define DEBUG(...) \ { \ printf("# "); \ printf(__VA_ARGS__); \ puts(""); \ } #else #define DEBUG(...) #endif int MOD = 1e9 + 7; void ADD(LL &x, LL v) { x = (x + v) % MOD; if (x < 0) x += MOD; } /*}}}*/ const int SIZE = 1e6 + 10; int main() { LL N; R(N); LL AA[3][2]; REP(i, 2) REP(j, 3) R(AA[j][i]); LL res = 0; for (int i = 0; i * AA[0][0] <= N; i++) for (int j = 0; i * AA[0][0] + j * AA[1][0] <= N; j++) { LL k = (N - (i * AA[0][0] + j * AA[1][0])) / AA[2][0]; res = max(res, N + i * (AA[0][1] - AA[0][0]) + j * (AA[1][1] - AA[1][0]) + k * (AA[2][1] - AA[2][0])); k = 0; res = max(res, N + i * (AA[0][1] - AA[0][0]) + j * (AA[1][1] - AA[1][0]) + k * (AA[2][1] - AA[2][0])); } N = res; res = 0; REP(i, 3) swap(AA[i][0], AA[i][1]); for (int i = 0; i * AA[0][0] <= N; i++) for (int j = 0; i * AA[0][0] + j * AA[1][0] <= N; j++) { if (j > 0 && AA[1][1] <= AA[1][0]) break; if (i > 0 && AA[0][1] <= AA[0][0]) break; LL k = (N - (i * AA[0][0] + j * AA[1][0])) / AA[2][0]; res = max(res, N + i * (AA[0][1] - AA[0][0]) + j * (AA[1][1] - AA[1][0]) + k * (AA[2][1] - AA[2][0])); k = 0; res = max(res, N + i * (AA[0][1] - AA[0][0]) + j * (AA[1][1] - AA[1][0]) + k * (AA[2][1] - AA[2][0])); } N = res; W(N); return 0; }
/*{{{*/ #include <algorithm> #include <assert.h> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <limits.h> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define FOR(I, A, B) for (int I = (A); I <= (B); ++I) #define FORS(I, S) for (int I = 0; S[I]; ++I) #define RS(X) scanf("%s", (X)) #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin()) #define CASET \ int ___T; \ scanf("%d", &___T); \ for (int cs = 1; cs <= ___T; cs++) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define F first #define S second using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PII> VPII; typedef pair<LL, LL> PLL; typedef vector<PLL> VPLL; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(LL &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <class T, class... U> void R(T &head, U &...tail) { _R(head); R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const LL &x) { printf("%lld", x); } void _W(const double &x) { printf("%.16f", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template <class T, class U> void _W(const pair<T, U> &x) { _W(x.F); putchar(' '); _W(x.S); } template <class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template <class T, class... U> void W(const T &head, const U &...tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } #ifdef HOME #define DEBUG(...) \ { \ printf("# "); \ printf(__VA_ARGS__); \ puts(""); \ } #else #define DEBUG(...) #endif int MOD = 1e9 + 7; void ADD(LL &x, LL v) { x = (x + v) % MOD; if (x < 0) x += MOD; } /*}}}*/ const int SIZE = 1e6 + 10; int main() { LL N; R(N); LL AA[3][2]; REP(i, 2) REP(j, 3) R(AA[j][i]); LL res = 0; for (int i = 0; i * AA[0][0] <= N; i++) for (int j = 0; i * AA[0][0] + j * AA[1][0] <= N; j++) { LL k = (N - (i * AA[0][0] + j * AA[1][0])) / AA[2][0]; res = max(res, N + i * (AA[0][1] - AA[0][0]) + j * (AA[1][1] - AA[1][0]) + k * (AA[2][1] - AA[2][0])); k = 0; res = max(res, N + i * (AA[0][1] - AA[0][0]) + j * (AA[1][1] - AA[1][0]) + k * (AA[2][1] - AA[2][0])); } N = res; res = 0; REP(i, 3) swap(AA[i][0], AA[i][1]); if (AA[0][1] <= AA[0][0]) { swap(AA[0][1], AA[2][1]); swap(AA[0][0], AA[2][0]); } if (AA[0][1] <= AA[0][0]) { swap(AA[0][1], AA[1][1]); swap(AA[0][0], AA[1][0]); } if (AA[1][1] <= AA[1][0]) { swap(AA[1][1], AA[2][1]); swap(AA[1][0], AA[2][0]); } if (AA[2][1] <= AA[2][0]) { for (int i = 0; i * AA[0][0] <= N; i++) { LL k = (N - i * AA[0][0]) / AA[1][0]; res = max(res, N + i * (AA[0][1] - AA[0][0]) + k * (AA[1][1] - AA[1][0])); k = 0; res = max(res, N + i * (AA[0][1] - AA[0][0]) + k * (AA[1][1] - AA[1][0])); } W(res); return 0; } for (int i = 0; i * AA[0][0] <= N; i++) for (int j = 0; i * AA[0][0] + j * AA[1][0] <= N; j++) { if (j > 0 && AA[1][1] <= AA[1][0]) break; if (i > 0 && AA[0][1] <= AA[0][0]) break; LL k = (N - (i * AA[0][0] + j * AA[1][0])) / AA[2][0]; res = max(res, N + i * (AA[0][1] - AA[0][0]) + j * (AA[1][1] - AA[1][0]) + k * (AA[2][1] - AA[2][0])); k = 0; res = max(res, N + i * (AA[0][1] - AA[0][0]) + j * (AA[1][1] - AA[1][0]) + k * (AA[2][1] - AA[2][0])); } N = res; W(N); return 0; }
insert
117
117
117
139
TLE
p03008
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME #define pr(...) \ cerr << GET_MACRO(__VA_ARGS__, pr8, pr7, pr6, pr5, pr4, pr3, pr2, \ pr1)(__VA_ARGS__) \ << endl #define pr1(a) (#a) << "=" << (a) << " " #define pr2(a, b) pr1(a) << pr1(b) #define pr3(a, b, c) pr1(a) << pr2(b, c) #define pr4(a, b, c, d) pr1(a) << pr3(b, c, d) #define pr5(a, b, c, d, e) pr1(a) << pr4(b, c, d, e) #define pr6(a, b, c, d, e, f) pr1(a) << pr5(b, c, d, e, f) #define pr7(a, b, c, d, e, f, g) pr1(a) << pr6(b, c, d, e, f, g) #define pr8(a, b, c, d, e, f, g, h) pr1(a) << pr7(b, c, d, e, f, g, h) #define prArr(a) \ { \ cerr << (#a) << "={"; \ int i = 0; \ for (auto t : (a)) \ cerr << (i++ ? ", " : "") << t; \ cerr << "}" << endl; \ } using namespace std; using Int = long long; using _int = int; using ll = long long; using Double = long double; const Int INF = (1LL << 60) + 1e9; // ~ 1.15 * 1e18 const Int mod = (1e9) + 7; const Double EPS = 1e-8; const Double PI = 6.0 * asin((Double)0.5); using P = pair<Int, Int>; template <class T> T Max(T &a, T b) { return a = max(a, b); } template <class T> T Min(T &a, T b) { return a = min(a, b); } template <class T1, class T2> ostream &operator<<(ostream &o, pair<T1, T2> p) { return o << "(" << p.first << "," << p.second << ")"; } template <class T1, class T2, class T3> ostream &operator<<(ostream &o, tuple<T1, T2, T3> t) { return o << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << ")"; } template <class T1, class T2> istream &operator>>(istream &i, pair<T1, T2> &p) { return i >> p.first >> p.second; } template <class T> ostream &operator<<(ostream &o, vector<T> a) { Int i = 0; for (T t : a) o << (i++ ? " " : "") << t; return o; } template <class T> istream &operator>>(istream &i, vector<T> &a) { for (T &t : a) i >> t; return i; } // INSERT ABOVE HERE vector<Int> A(3), B(3); Int secondB(Int N) { Int res = N; for (int k = 0; k < 3; k++) { swap(A[0], A[2]), swap(A[0], A[1]); swap(B[0], B[2]), swap(B[0], B[1]); for (Int i = 0; i <= 10001; i++) { for (Int j = 0; j <= 10001; j++) { Int a = i; Int b = j; Int r = (N - (a * B[0] + b * B[1])); if (r < 0) break; Int c = r / B[2]; Int t = r % B[2]; Int M = a * A[0] + b * A[1] + max(c * A[2] + t, r); Max(res, M); } } } return res; } Int firstA(Int N) { for (Int i = 0; i < 3; i++) for (Int j = 2; j > 0; j--) if (A[j - 1] < A[j]) swap(B[j], B[j - 1]), swap(A[j], A[j - 1]); Int res = N; for (Int i = 0; i <= N; i++) { for (Int j = 0; j <= N; j++) { Int a = i; Int b = j; Int r = (N - (a * A[0] + b * A[1])); Int c = r / A[2]; Int t = r % A[2]; if (r < 0) break; // assert(r >= 0); Int M = a * B[0] + b * B[1] + max(c * B[2] + t, r); Max(res, M); } } return secondB(res); } signed main() { srand((unsigned)time(NULL)); cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(12); Int N; cin >> N; cin >> A >> B; Int ans = firstA(N); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME #define pr(...) \ cerr << GET_MACRO(__VA_ARGS__, pr8, pr7, pr6, pr5, pr4, pr3, pr2, \ pr1)(__VA_ARGS__) \ << endl #define pr1(a) (#a) << "=" << (a) << " " #define pr2(a, b) pr1(a) << pr1(b) #define pr3(a, b, c) pr1(a) << pr2(b, c) #define pr4(a, b, c, d) pr1(a) << pr3(b, c, d) #define pr5(a, b, c, d, e) pr1(a) << pr4(b, c, d, e) #define pr6(a, b, c, d, e, f) pr1(a) << pr5(b, c, d, e, f) #define pr7(a, b, c, d, e, f, g) pr1(a) << pr6(b, c, d, e, f, g) #define pr8(a, b, c, d, e, f, g, h) pr1(a) << pr7(b, c, d, e, f, g, h) #define prArr(a) \ { \ cerr << (#a) << "={"; \ int i = 0; \ for (auto t : (a)) \ cerr << (i++ ? ", " : "") << t; \ cerr << "}" << endl; \ } using namespace std; using Int = long long; using _int = int; using ll = long long; using Double = long double; const Int INF = (1LL << 60) + 1e9; // ~ 1.15 * 1e18 const Int mod = (1e9) + 7; const Double EPS = 1e-8; const Double PI = 6.0 * asin((Double)0.5); using P = pair<Int, Int>; template <class T> T Max(T &a, T b) { return a = max(a, b); } template <class T> T Min(T &a, T b) { return a = min(a, b); } template <class T1, class T2> ostream &operator<<(ostream &o, pair<T1, T2> p) { return o << "(" << p.first << "," << p.second << ")"; } template <class T1, class T2, class T3> ostream &operator<<(ostream &o, tuple<T1, T2, T3> t) { return o << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << ")"; } template <class T1, class T2> istream &operator>>(istream &i, pair<T1, T2> &p) { return i >> p.first >> p.second; } template <class T> ostream &operator<<(ostream &o, vector<T> a) { Int i = 0; for (T t : a) o << (i++ ? " " : "") << t; return o; } template <class T> istream &operator>>(istream &i, vector<T> &a) { for (T &t : a) i >> t; return i; } // INSERT ABOVE HERE vector<Int> A(3), B(3); Int secondB(Int N) { Int res = N; for (int k = 0; k < 3; k++) { swap(A[0], A[2]), swap(A[0], A[1]); swap(B[0], B[2]), swap(B[0], B[1]); for (Int i = 0; i <= 8001; i++) { for (Int j = 0; j <= 8001; j++) { Int a = i; Int b = j; Int r = (N - (a * B[0] + b * B[1])); if (r < 0) break; Int c = r / B[2]; Int t = r % B[2]; Int M = a * A[0] + b * A[1] + max(c * A[2] + t, r); Max(res, M); } } } return res; } Int firstA(Int N) { for (Int i = 0; i < 3; i++) for (Int j = 2; j > 0; j--) if (A[j - 1] < A[j]) swap(B[j], B[j - 1]), swap(A[j], A[j - 1]); Int res = N; for (Int i = 0; i <= N; i++) { for (Int j = 0; j <= N; j++) { Int a = i; Int b = j; Int r = (N - (a * A[0] + b * A[1])); Int c = r / A[2]; Int t = r % A[2]; if (r < 0) break; // assert(r >= 0); Int M = a * B[0] + b * B[1] + max(c * B[2] + t, r); Max(res, M); } } return secondB(res); } signed main() { srand((unsigned)time(NULL)); cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(12); Int N; cin >> N; cin >> A >> B; Int ans = firstA(N); cout << ans << endl; return 0; }
replace
66
68
66
68
TLE
p03008
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define repi(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, a) repi(i, 0, a) #define all(a) (a).begin(), (a).end() template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } using ll = long long; ll N; ll g[2], s[2], b[2]; ll dp[5][100010]; ll knap(const std::vector<ll> &w, const std::vector<ll> &v, ll W) { rep(i, 5) rep(j, W + 1) dp[i][j] = 0; rep(i, 3) rep(j, W + 1) { if (j - w[i] >= 0) chmax(dp[i + 1][j], std::max({dp[i + 1][j - w[i]] + v[i], dp[i][j - w[i]] + v[i]})); chmax(dp[i + 1][j], dp[i][j]); } ll ret = 0; rep(j, W + 1) chmax(ret, dp[3][j]); return W + ret; } int main() { std::cin >> N; rep(i, 2) std::cin >> g[i] >> s[i] >> b[i]; ll M = knap({g[0], s[0], b[0]}, {g[1] - g[0], s[1] - s[0], b[1] - b[0]}, N); ll ans = knap({g[1], s[1], b[1]}, {g[0] - g[1], s[0] - s[1], b[0] - b[1]}, M); std::cout << ans << std::endl; return 0; }
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define repi(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, a) repi(i, 0, a) #define all(a) (a).begin(), (a).end() template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } using ll = long long; ll N; ll g[2], s[2], b[2]; ll dp[5][25000010]; ll knap(const std::vector<ll> &w, const std::vector<ll> &v, ll W) { rep(i, 5) rep(j, W + 1) dp[i][j] = 0; rep(i, 3) rep(j, W + 1) { if (j - w[i] >= 0) chmax(dp[i + 1][j], std::max({dp[i + 1][j - w[i]] + v[i], dp[i][j - w[i]] + v[i]})); chmax(dp[i + 1][j], dp[i][j]); } ll ret = 0; rep(j, W + 1) chmax(ret, dp[3][j]); return W + ret; } int main() { std::cin >> N; rep(i, 2) std::cin >> g[i] >> s[i] >> b[i]; ll M = knap({g[0], s[0], b[0]}, {g[1] - g[0], s[1] - s[0], b[1] - b[0]}, N); ll ans = knap({g[1], s[1], b[1]}, {g[0] - g[1], s[0] - s[1], b[0] - b[1]}, M); std::cout << ans << std::endl; return 0; }
replace
36
37
36
37
0
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back #define re return typedef vector<int> vi; typedef long long ll; typedef pair<int, int> pii; ll dp[5010]; ll n; ll a[10][10]; ll ans; int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); cin >> n; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cin >> a[i][j]; } } for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; if (i - a[0][0] >= 0) { dp[i] = max(dp[i], dp[i - a[0][0]] + a[1][0] - a[0][0]); } if (i - a[0][1] >= 0) { dp[i] = max(dp[i], dp[i - a[0][1]] + a[1][1] - a[0][1]); } if (i - a[0][2] >= 0) { dp[i] = max(dp[i], dp[i - a[0][2]] + a[1][2] - a[0][2]); } } ans = n + dp[n]; n = n + dp[n]; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; if (i - a[1][0] >= 0) { dp[i] = max(dp[i], dp[i - a[1][0]] + a[0][0] - a[1][0]); } if (i - a[1][1] >= 0) { dp[i] = max(dp[i], dp[i - a[1][1]] + a[0][1] - a[1][1]); } if (i - a[1][2] >= 0) { dp[i] = max(dp[i], dp[i - a[1][2]] + a[0][2] - a[1][2]); } } ans += dp[n]; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back #define re return typedef vector<int> vi; typedef long long ll; typedef pair<int, int> pii; ll dp[5001 * 5001]; ll n; ll a[10][10]; ll ans; int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); cin >> n; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cin >> a[i][j]; } } for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; if (i - a[0][0] >= 0) { dp[i] = max(dp[i], dp[i - a[0][0]] + a[1][0] - a[0][0]); } if (i - a[0][1] >= 0) { dp[i] = max(dp[i], dp[i - a[0][1]] + a[1][1] - a[0][1]); } if (i - a[0][2] >= 0) { dp[i] = max(dp[i], dp[i - a[0][2]] + a[1][2] - a[0][2]); } } ans = n + dp[n]; n = n + dp[n]; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; if (i - a[1][0] >= 0) { dp[i] = max(dp[i], dp[i - a[1][0]] + a[0][0] - a[1][0]); } if (i - a[1][1] >= 0) { dp[i] = max(dp[i], dp[i - a[1][1]] + a[0][1] - a[1][1]); } if (i - a[1][2] >= 0) { dp[i] = max(dp[i], dp[i - a[1][2]] + a[0][2] - a[1][2]); } } ans += dp[n]; cout << ans << endl; return 0; }
replace
8
9
8
9
0
p03008
C++
Runtime Error
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define pii pair<int, int> #define pp pair<pair<ll, ll>, pair<ll, ll>> #define pll pair<ll, ll> #define pdd pair<double, double> #define vii vector<int> #define vll vector<ll> #define mat vector<vector<ll>> #define lb lower_bound #define pb push_back #define fi first #define sc second #define rep(i, n) for (ll i = 0; i < n; i++) #define rep2(i, a, b) for (ll i = a; i < b; i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define all(x) x.begin(), x.end() #define pq priority_queue<ll> #define pqg priority_queue<ll, vector<ll>, greater<ll>> #define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin()) #define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin()) #define ERASE(v) \ sort(v.begin(), v.end()); \ v.erase(unique(v.begin(), v.end()), v.end()) // #define int ll using namespace std; const ll INF = (1 << 30) - 1; const ll LLINF = (1LL << 60LL); const ll MOD = 1000000007; const ll mod = 998244353; const ll MAX = 1100000; const double pi = acos(-1); const double eps = 1e-10; ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } struct Timer { chrono::system_clock::time_point start, end; Timer() { start = chrono::system_clock::now(); } ~Timer() { end = chrono::system_clock::now(); auto msec = chrono::duration_cast<chrono::milliseconds>(end - start).count(); cerr << "time : " << msec << " ms" << endl; } }; vll a(3), b(3); ll dp[300010]; ll calc(vll a, vll b, ll M) { memset(dp, 0, sizeof(dp)); rep(i, a.size()) { for (ll v = 0; v + a[i] <= M; v++) { chmax(dp[v + a[i]], dp[v] + b[i]); } } ll res = M; rep(v, M + 1) { chmax(res, dp[v] + M - v); } return res; } signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); Timer TM; ll n; cin >> n; rep(i, 3) cin >> a[i]; rep(i, 3) cin >> b[i]; n = calc(a, b, n); n = calc(b, a, n); cout << n << endl; return 0; }
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define pii pair<int, int> #define pp pair<pair<ll, ll>, pair<ll, ll>> #define pll pair<ll, ll> #define pdd pair<double, double> #define vii vector<int> #define vll vector<ll> #define mat vector<vector<ll>> #define lb lower_bound #define pb push_back #define fi first #define sc second #define rep(i, n) for (ll i = 0; i < n; i++) #define rep2(i, a, b) for (ll i = a; i < b; i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define all(x) x.begin(), x.end() #define pq priority_queue<ll> #define pqg priority_queue<ll, vector<ll>, greater<ll>> #define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin()) #define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin()) #define ERASE(v) \ sort(v.begin(), v.end()); \ v.erase(unique(v.begin(), v.end()), v.end()) // #define int ll using namespace std; const ll INF = (1 << 30) - 1; const ll LLINF = (1LL << 60LL); const ll MOD = 1000000007; const ll mod = 998244353; const ll MAX = 1100000; const double pi = acos(-1); const double eps = 1e-10; ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } struct Timer { chrono::system_clock::time_point start, end; Timer() { start = chrono::system_clock::now(); } ~Timer() { end = chrono::system_clock::now(); auto msec = chrono::duration_cast<chrono::milliseconds>(end - start).count(); cerr << "time : " << msec << " ms" << endl; } }; vll a(3), b(3); ll dp[30000010]; ll calc(vll a, vll b, ll M) { memset(dp, 0, sizeof(dp)); rep(i, a.size()) { for (ll v = 0; v + a[i] <= M; v++) { chmax(dp[v + a[i]], dp[v] + b[i]); } } ll res = M; rep(v, M + 1) { chmax(res, dp[v] + M - v); } return res; } signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); Timer TM; ll n; cin >> n; rep(i, 3) cin >> a[i]; rep(i, 3) cin >> b[i]; n = calc(a, b, n); n = calc(b, a, n); cout << n << endl; return 0; }
replace
65
66
65
66
0
time : 2 ms
p03008
C++
Time Limit Exceeded
#include <cstdio> #include <iostream> #include <vector> using namespace std; typedef long long ll; ll n, ta[3], tb[3]; vector<ll> a[2], b[2]; ll f(ll p, ll q, ll r) { ll i, re = 0; if (r == a[p].size()) return q; for (i = 0; i * a[p][r] <= q; i++) { re = max(re, f(p, q - i * a[p][r], r + 1) + i * b[p][r]); } return re; } int main() { ll i; cin >> n; for (i = 0; i < 3; i++) scanf("%lld", &ta[i]); for (i = 0; i < 3; i++) scanf("%lld", &tb[i]); for (i = 0; i < 3; i++) { if (ta[i] < tb[i]) a[0].push_back(ta[i]), b[0].push_back(tb[i]); else if (ta[i] > tb[i]) a[1].push_back(tb[i]), b[1].push_back(ta[i]); } cout << f(1, f(0, n, 0), 0) << endl; return 0; }
#include <cstdio> #include <iostream> #include <vector> using namespace std; typedef long long ll; ll n, ta[3], tb[3]; vector<ll> a[2], b[2]; ll f(ll p, ll q, ll r) { ll i, re = 0; if (r == a[p].size()) return q; if (r == a[p].size() - 1) return q / a[p][r] * b[p][r] + q % a[p][r]; for (i = 0; i * a[p][r] <= q; i++) { re = max(re, f(p, q - i * a[p][r], r + 1) + i * b[p][r]); } return re; } int main() { ll i; cin >> n; for (i = 0; i < 3; i++) scanf("%lld", &ta[i]); for (i = 0; i < 3; i++) scanf("%lld", &tb[i]); for (i = 0; i < 3; i++) { if (ta[i] < tb[i]) a[0].push_back(ta[i]), b[0].push_back(tb[i]); else if (ta[i] > tb[i]) a[1].push_back(tb[i]), b[1].push_back(ta[i]); } cout << f(1, f(0, n, 0), 0) << endl; return 0; }
insert
13
13
13
15
TLE
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int ll #define ll long long #define I32_MAX 2147483647 #define I64_MAX 9223372036854775807LL #define I64_MAX2 1223372036854775807LL #define INF I64_MAX2 #define MOD 1000000007 // #define MOD 998244353 #define MEM_SIZE 100010 #define DEBUG_OUT true #define ALL(x) (x).begin(), (x).end() template <typename T> void DEBUG(T e) { if (DEBUG_OUT == false) return; std::cout << e << " "; } template <typename T> void DEBUG(const std::vector<T> &v) { if (DEBUG_OUT == false) return; for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) { if (DEBUG_OUT == false) return; for (const auto &v : vv) { DEBUG(v); } } template <class T, class... Ts> void DEBUG(T d, Ts... e) { if (DEBUG_OUT == false) return; DEBUG(d); DEBUG(e...); } template <class T> void corner(bool flg, T hoge) { if (flg) { cout << hoge << endl; abort(); } } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } int calc(vector<int> &a, vector<int> &b, int m) { vector<int> dp(MEM_SIZE * 10, 0); for (int i = 0; i < a.size(); i++) { // v + a[i]個をv個 + b[i]個に換金する。 // max: ax + by + cz // s.t. px + qy + rz <= Nが解ける。すごい。 for (int v = 0; v + a[i] <= m; v++) { chmax(dp[v + a[i]], dp[v] + b[i]); } } int res = m; for (int i = 0; i < m + 1; i++) { chmax(res, dp[i] + m - i); } return res; } void solve(void) { vector<int> a(3, 0); vector<int> b(3, 0); int n; cin >> n; for (size_t i = 0; i < a.size(); i++) { cin >> a[i]; } for (size_t i = 0; i < b.size(); i++) { cin >> b[i]; } n = calc(a, b, n); n = calc(b, a, n); cout << n << endl; return; } int32_t main(int32_t argc, const char *argv[]) { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout << std::fixed; std::cout << std::setprecision(11); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define int ll #define ll long long #define I32_MAX 2147483647 #define I64_MAX 9223372036854775807LL #define I64_MAX2 1223372036854775807LL #define INF I64_MAX2 #define MOD 1000000007 // #define MOD 998244353 #define MEM_SIZE 100010 #define DEBUG_OUT true #define ALL(x) (x).begin(), (x).end() template <typename T> void DEBUG(T e) { if (DEBUG_OUT == false) return; std::cout << e << " "; } template <typename T> void DEBUG(const std::vector<T> &v) { if (DEBUG_OUT == false) return; for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) { if (DEBUG_OUT == false) return; for (const auto &v : vv) { DEBUG(v); } } template <class T, class... Ts> void DEBUG(T d, Ts... e) { if (DEBUG_OUT == false) return; DEBUG(d); DEBUG(e...); } template <class T> void corner(bool flg, T hoge) { if (flg) { cout << hoge << endl; abort(); } } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } int calc(vector<int> &a, vector<int> &b, int m) { vector<int> dp(m + 1, 0); for (int i = 0; i < a.size(); i++) { // v + a[i]個をv個 + b[i]個に換金する。 // max: ax + by + cz // s.t. px + qy + rz <= Nが解ける。すごい。 for (int v = 0; v + a[i] <= m; v++) { chmax(dp[v + a[i]], dp[v] + b[i]); } } int res = m; for (int i = 0; i < m + 1; i++) { chmax(res, dp[i] + m - i); } return res; } void solve(void) { vector<int> a(3, 0); vector<int> b(3, 0); int n; cin >> n; for (size_t i = 0; i < a.size(); i++) { cin >> a[i]; } for (size_t i = 0; i < b.size(); i++) { cin >> b[i]; } n = calc(a, b, n); n = calc(b, a, n); cout << n << endl; return; } int32_t main(int32_t argc, const char *argv[]) { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout << std::fixed; std::cout << std::setprecision(11); solve(); return 0; }
replace
55
56
55
56
0
p03008
C++
Runtime Error
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i) #define rep(i, n) REP(i, 0, n) typedef long long ll; typedef pair<int, int> pint; typedef pair<ll, int> pli; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1000003; ll dp[5050], dp2[200010]; ll w[5], w2[5]; ll v[5], v2[5]; int main() { ll n; cin >> n; ll a[2][3]; rep(i, 2) { rep(j, 3) cin >> a[i][j]; } rep(i, 3) { w[i] = a[0][i]; v[i] = a[1][i]; w2[i] = a[1][i]; v2[i] = a[0][i]; } v[3] = 1; w[3] = 1; v2[3] = 1; w2[3] = 1; for (int i = 0; i < 4; i++) { for (int j = 0; j <= n; j++) { if (j < w[i]) { dp[j] = dp[j]; } else { dp[j] = max(dp[j], dp[j - w[i]] + v[i]); } } } ll m = dp[n]; for (int i = 0; i < 4; i++) { for (int j = 0; j <= m; j++) { if (j < w2[i]) { dp2[j] = dp2[j]; } else { dp2[j] = max(dp2[j], dp2[j - w2[i]] + v2[i]); } } } cout << dp2[m] << endl; return 0; }
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i) #define rep(i, n) REP(i, 0, n) typedef long long ll; typedef pair<int, int> pint; typedef pair<ll, int> pli; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1000003; ll dp[5050], dp2[25000001]; ll w[5], w2[5]; ll v[5], v2[5]; int main() { ll n; cin >> n; ll a[2][3]; rep(i, 2) { rep(j, 3) cin >> a[i][j]; } rep(i, 3) { w[i] = a[0][i]; v[i] = a[1][i]; w2[i] = a[1][i]; v2[i] = a[0][i]; } v[3] = 1; w[3] = 1; v2[3] = 1; w2[3] = 1; for (int i = 0; i < 4; i++) { for (int j = 0; j <= n; j++) { if (j < w[i]) { dp[j] = dp[j]; } else { dp[j] = max(dp[j], dp[j - w[i]] + v[i]); } } } ll m = dp[n]; for (int i = 0; i < 4; i++) { for (int j = 0; j <= m; j++) { if (j < w2[i]) { dp2[j] = dp2[j]; } else { dp2[j] = max(dp2[j], dp2[j - w2[i]] + v2[i]); } } } cout << dp2[m] << endl; return 0; }
replace
23
24
23
24
0
p03008
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using ll = long long; using P = pair<ll, ll>; #define rep(i, x) for (ll i = 0; i < (ll)(x); i++) #define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--) #define all(x) (x).begin(), (x).end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define chmin(x, y) (x = min(x, y)) #define chmax(x, y) (x = max(x, y)) // ll gcd(ll a, ll b){return b?gcd(b,a%b):a;} int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; const ll mod = 1e9 + 7; const ll IMF = 1LL << 29; const double PI = 3.14159265358979323846; const int MAX = 2600000; int n; ll dp[MAX]; ll sub(ll k, vector<P> v) { memset(dp, 0, sizeof(dp)); for (P p : v) { int cost = p.first; int val = p.second; if (val == 0) continue; for (ll i = 0; i < MAX; i++) { if (i - cost < 0) continue; dp[i] = max(dp[i], dp[i - cost] + val); } } ll mx = 0; for (ll i = 0; i < k + 1; i++) { chmax(mx, dp[i]); } return mx + k; } int main() { int ax, bx, cx; int ay, by, cy; cin >> n >> ax >> bx >> cx >> ay >> by >> cy; vector<P> A; vector<P> B; if (ax < ay) { A.push_back(P(ax, ay - ax)); } else { B.push_back(P(ay, ax - ay)); } if (bx < by) { A.push_back(P(bx, by - bx)); } else { B.push_back(P(by, bx - by)); } if (cx < cy) { A.push_back(P(cx, cy - cx)); } else { B.push_back(P(cy, cx - cy)); } ll sum = n; sum = sub(sum, A); sum = sub(sum, B); cout << sum << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using ll = long long; using P = pair<ll, ll>; #define rep(i, x) for (ll i = 0; i < (ll)(x); i++) #define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--) #define all(x) (x).begin(), (x).end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define chmin(x, y) (x = min(x, y)) #define chmax(x, y) (x = max(x, y)) // ll gcd(ll a, ll b){return b?gcd(b,a%b):a;} int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; const ll mod = 1e9 + 7; const ll IMF = 1LL << 29; const double PI = 3.14159265358979323846; const int MAX = 26000000; int n; ll dp[MAX]; ll sub(ll k, vector<P> v) { memset(dp, 0, sizeof(dp)); for (P p : v) { int cost = p.first; int val = p.second; if (val == 0) continue; for (ll i = 0; i < MAX; i++) { if (i - cost < 0) continue; dp[i] = max(dp[i], dp[i - cost] + val); } } ll mx = 0; for (ll i = 0; i < k + 1; i++) { chmax(mx, dp[i]); } return mx + k; } int main() { int ax, bx, cx; int ay, by, cy; cin >> n >> ax >> bx >> cx >> ay >> by >> cy; vector<P> A; vector<P> B; if (ax < ay) { A.push_back(P(ax, ay - ax)); } else { B.push_back(P(ay, ax - ay)); } if (bx < by) { A.push_back(P(bx, by - bx)); } else { B.push_back(P(by, bx - by)); } if (cx < cy) { A.push_back(P(cx, cy - cx)); } else { B.push_back(P(cy, cx - cy)); } ll sum = n; sum = sub(sum, A); sum = sub(sum, B); cout << sum << endl; return 0; }
replace
36
37
36
37
0
p03008
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pi 3.141592653589793238 #define int long long using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int dp[5005]; vector<int> dp2; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); #ifndef ONLINE_JUDGE if (fopen("INPUT.txt", "r")) { freopen("INPUT.txt", "r", stdin); freopen("OUTPUT.txt", "w", stdout); } #endif int n; cin >> n; int a[3]; int b[3]; for (int i = 0; i < 3; i++) cin >> a[i]; for (int i = 0; i < 3; i++) cin >> b[i]; for (int i = n; i >= 0; i--) { for (int j = 0; j < 3; j++) { if (i - a[j] >= 0) dp[i - a[j]] = max(dp[i - a[j]], dp[i] + (b[j] - a[j])); } } int mx = 0; for (int i = 0; i <= n; i++) { mx = max(mx, n + dp[i]); } n = mx; dp2.assign(n + 5, 0); for (int i = n; i >= 0; i--) { for (int j = 0; j < 3; j++) { if (i - b[j] >= 0) dp[i - b[j]] = max(dp[i - b[j]], dp[i] + (a[j] - b[j])); } } mx = 0; for (int i = 0; i <= n; i++) { mx = max(mx, n + dp2[i]); } cout << mx; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pi 3.141592653589793238 #define int long long using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int dp[5005]; vector<int> dp2; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); #ifndef ONLINE_JUDGE if (fopen("INPUT.txt", "r")) { freopen("INPUT.txt", "r", stdin); freopen("OUTPUT.txt", "w", stdout); } #endif int n; cin >> n; int a[3]; int b[3]; for (int i = 0; i < 3; i++) cin >> a[i]; for (int i = 0; i < 3; i++) cin >> b[i]; for (int i = n; i >= 0; i--) { for (int j = 0; j < 3; j++) { if (i - a[j] >= 0) dp[i - a[j]] = max(dp[i - a[j]], dp[i] + (b[j] - a[j])); } } int mx = 0; for (int i = 0; i <= n; i++) { mx = max(mx, n + dp[i]); } n = mx; dp2.assign(n + 5, 0); for (int i = n; i >= 0; i--) { for (int j = 0; j < 3; j++) { if (i - b[j] >= 0) dp2[i - b[j]] = max(dp2[i - b[j]], dp2[i] + (a[j] - b[j])); } } mx = 0; for (int i = 0; i <= n; i++) { mx = max(mx, n + dp2[i]); } cout << mx; }
replace
49
50
49
50
0
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define eb emplace_back #define fi first #define se second #define fr(i, j, k) for (i = j; i < (k); i++) #define all(x) x.begin(), x.end() #define el '\n' #define remax(a, b) a = max(a, b) #define remin(a, b) a = min(a, b) typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<vi> vvi; typedef vector<pii> vpi; // --------------------------- TEMPLATE ENDS // ------------------------------------- const pii dxy[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; const int mod = 1e9 + 7; const int inf = 2e18; const ld eps = 1e-9; const int NN = 2.5e7; vvi adj; int dp[NN][3]; int a[4], b[4]; void solve() { int i = 0, j = 0, k = 0, n = 0, m = 0; cin >> n; fr(i, 0, 3) { cin >> a[i]; } fr(i, 0, 3) { cin >> b[i]; } for (j = 0; j < 3; j++) { for (i = 0; i <= n; i++) { int &v = dp[i][j]; if (j > 0) { v = dp[i][j - 1]; } else { v = i; } if (b[j] - a[j] > 0 and i >= a[j]) { remax(v, dp[i - a[j]][j] + b[j]); } } } int now = dp[n][2]; memset(dp, 0, sizeof dp); for (j = 0; j < 3; j++) { for (i = 0; i <= now; i++) { int &v = dp[i][j]; if (j > 0) { v = dp[i][j - 1]; } else { v = i; } if (a[j] - b[j] > 0 and i >= b[j]) { remax(v, dp[i - b[j]][j] + a[j]); } } } cout << dp[now][2] << endl; } int32_t main() { #ifndef TRACE ios::sync_with_stdio(false); cin.tie(0); #endif int T = 1, tc; for (tc = 1; tc <= T; tc++) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define eb emplace_back #define fi first #define se second #define fr(i, j, k) for (i = j; i < (k); i++) #define all(x) x.begin(), x.end() #define el '\n' #define remax(a, b) a = max(a, b) #define remin(a, b) a = min(a, b) typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<vi> vvi; typedef vector<pii> vpi; // --------------------------- TEMPLATE ENDS // ------------------------------------- const pii dxy[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; const int mod = 1e9 + 7; const int inf = 2e18; const ld eps = 1e-9; const int NN = 3e7; int dp[NN][3]; int a[4], b[4]; void solve() { int i = 0, j = 0, k = 0, n = 0, m = 0; cin >> n; fr(i, 0, 3) { cin >> a[i]; } fr(i, 0, 3) { cin >> b[i]; } for (j = 0; j < 3; j++) { for (i = 0; i <= n; i++) { int &v = dp[i][j]; if (j > 0) { v = dp[i][j - 1]; } else { v = i; } if (b[j] - a[j] > 0 and i >= a[j]) { remax(v, dp[i - a[j]][j] + b[j]); } } } int now = dp[n][2]; memset(dp, 0, sizeof dp); for (j = 0; j < 3; j++) { for (i = 0; i <= now; i++) { int &v = dp[i][j]; if (j > 0) { v = dp[i][j - 1]; } else { v = i; } if (a[j] - b[j] > 0 and i >= b[j]) { remax(v, dp[i - b[j]][j] + a[j]); } } } cout << dp[now][2] << endl; } int32_t main() { #ifndef TRACE ios::sync_with_stdio(false); cin.tie(0); #endif int T = 1, tc; for (tc = 1; tc <= T; tc++) { solve(); } return 0; }
replace
28
31
28
29
-11
p03008
C++
Runtime Error
#include <algorithm> #include <bitset> #include <climits> #include <cmath> // abs() for float, and fabs() #include <cstdlib> // abs() for integer #include <functional> #include <iomanip> #include <iostream> #include <map> // pair #include <math.h> #include <numeric> #include <queue> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <utility> #include <vector> typedef std::stack<int> IntStack; typedef std::stack<char> charStack; #define START 0 #define FORWARD 1 #define BACK 2 using namespace std; using ll = long long; typedef pair<ll, ll> pll; bool comp(const pll &a, const pll &b) { return a.first < b.first; } //********************************************************* // 最大公約数(Greatest Common Divisor)を返す。 // 引数に0がある場合は0を返す。 //********************************************************* ll gcd(ll a, ll b) { ll tmp, r; if (a < b) { tmp = a; a = b; b = tmp; } /* ユークリッドの互除法 */ r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; //// 引数に0がある場合は0を返す // if ((0 == m) || (0 == n)) // return 0; //// ユークリッドの方法 // while (m != n) //{ // if (m > n) m = m - n; // else n = n - m; // } // return m; } // gcd int main() { ll a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, ans = 0; ll x[100004] = {}; ll y[100004] = {}; ll z[100004] = {}; vector<tuple<ll, ll>> vec; string s; string t; // IntStack yStack; // charStack Stack; // vector<pair<int, int> > pairs(a); // cout << LLONG_MAX<< endl; cin >> a; for (int i = 0; i < 3; i++) { cin >> x[i]; } for (int i = 0; i < 3; i++) { cin >> y[i]; } z[0] = a; for (int k = 0; k < 3; k++) { for (int i = 0; i <= a; i++) { if (i >= x[k] && x[k] < y[k]) { z[i] = max(z[i], z[i - x[k]] + (y[k] - x[k])); } } } for (int i = 0; i <= a; i++) { ans = max(ans, z[i]); } z[0] = ans; for (int k = 0; k < 3; k++) { for (int i = 0; i <= ans; i++) { if (i >= y[k] && x[k] > y[k]) { z[i] = max(z[i], z[i - y[k]] + (x[k] - y[k])); } } } // for (int i = 0; i <= a; i++) //{ // cout << z[i] << endl; //} // cout << endl; b = ans; for (int i = 0; i <= b; i++) { ans = max(ans, z[i]); } cout << ans << endl; ////std::cout << std::fixed; // cout << std::setprecision(20) << << endl; // cout << d+1 << endl; // cout << "" << endl; return 0; } // sort(x, x + a, std::greater<ll>()); // char型の文字数字は、-'0'で数値に変換できる // accumulate(x, x + a, 0);//int型までの配列の合計 // memcpy(x, visited, sizeof(ll)*n); // void kansuu(ll num, ll visited[10]) {} // kansuu(1, zz); // cout << setprecision(15) << v << endl; // cout << char(z - 'a'); // log10(i) + 1 //10進数の桁数を取得 // // int temp; // for (int i = 0; i < n - 1; i++) { // for (int j = n - 1; j > i; j--) { // if (w[j - 1] < w[j]) { /* 前の要素の方が大きかったら */ // temp = w[j]; /* 交換する */ // w[j] = w[j - 1]; // w[j - 1] = temp; // } // } // } // // 最小公倍数 // ll x = a * b; // ll tmp; ///* 自然数 a > b を確認・入替 */ // if (a<b) { // tmp = a; // a = b; // b = tmp; // } ///* ユークリッドの互除法 */ // ll r = a % b; // while (r != 0) { // a = b; // b = r; // r = a % b; // } // a = x / b;//答え /*vector<pair<ll, ll> > hoges(a); for (int i = 0; i < a; i++) { cin >> x[i]; cin >> y[i]; hoges[i] = make_pair(x[i], y[i]); } sort(hoges.begin(),hoges.end(),comp); for (int i = 0; i < a; i++) { if (hoges[i].second>=b) { ans += hoges[i].first*b; break; } else { ans += hoges[i].first*hoges[i].second; b -= hoges[i].second; } }*/
#include <algorithm> #include <bitset> #include <climits> #include <cmath> // abs() for float, and fabs() #include <cstdlib> // abs() for integer #include <functional> #include <iomanip> #include <iostream> #include <map> // pair #include <math.h> #include <numeric> #include <queue> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <utility> #include <vector> typedef std::stack<int> IntStack; typedef std::stack<char> charStack; #define START 0 #define FORWARD 1 #define BACK 2 using namespace std; using ll = long long; typedef pair<ll, ll> pll; bool comp(const pll &a, const pll &b) { return a.first < b.first; } //********************************************************* // 最大公約数(Greatest Common Divisor)を返す。 // 引数に0がある場合は0を返す。 //********************************************************* ll gcd(ll a, ll b) { ll tmp, r; if (a < b) { tmp = a; a = b; b = tmp; } /* ユークリッドの互除法 */ r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; //// 引数に0がある場合は0を返す // if ((0 == m) || (0 == n)) // return 0; //// ユークリッドの方法 // while (m != n) //{ // if (m > n) m = m - n; // else n = n - m; // } // return m; } // gcd int main() { ll a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, ans = 0; ll x[100004] = {}; ll y[100004] = {}; ll z[25000001] = {}; vector<tuple<ll, ll>> vec; string s; string t; // IntStack yStack; // charStack Stack; // vector<pair<int, int> > pairs(a); // cout << LLONG_MAX<< endl; cin >> a; for (int i = 0; i < 3; i++) { cin >> x[i]; } for (int i = 0; i < 3; i++) { cin >> y[i]; } z[0] = a; for (int k = 0; k < 3; k++) { for (int i = 0; i <= a; i++) { if (i >= x[k] && x[k] < y[k]) { z[i] = max(z[i], z[i - x[k]] + (y[k] - x[k])); } } } for (int i = 0; i <= a; i++) { ans = max(ans, z[i]); } z[0] = ans; for (int k = 0; k < 3; k++) { for (int i = 0; i <= ans; i++) { if (i >= y[k] && x[k] > y[k]) { z[i] = max(z[i], z[i - y[k]] + (x[k] - y[k])); } } } // for (int i = 0; i <= a; i++) //{ // cout << z[i] << endl; //} // cout << endl; b = ans; for (int i = 0; i <= b; i++) { ans = max(ans, z[i]); } cout << ans << endl; ////std::cout << std::fixed; // cout << std::setprecision(20) << << endl; // cout << d+1 << endl; // cout << "" << endl; return 0; } // sort(x, x + a, std::greater<ll>()); // char型の文字数字は、-'0'で数値に変換できる // accumulate(x, x + a, 0);//int型までの配列の合計 // memcpy(x, visited, sizeof(ll)*n); // void kansuu(ll num, ll visited[10]) {} // kansuu(1, zz); // cout << setprecision(15) << v << endl; // cout << char(z - 'a'); // log10(i) + 1 //10進数の桁数を取得 // // int temp; // for (int i = 0; i < n - 1; i++) { // for (int j = n - 1; j > i; j--) { // if (w[j - 1] < w[j]) { /* 前の要素の方が大きかったら */ // temp = w[j]; /* 交換する */ // w[j] = w[j - 1]; // w[j - 1] = temp; // } // } // } // // 最小公倍数 // ll x = a * b; // ll tmp; ///* 自然数 a > b を確認・入替 */ // if (a<b) { // tmp = a; // a = b; // b = tmp; // } ///* ユークリッドの互除法 */ // ll r = a % b; // while (r != 0) { // a = b; // b = r; // r = a % b; // } // a = x / b;//答え /*vector<pair<ll, ll> > hoges(a); for (int i = 0; i < a; i++) { cin >> x[i]; cin >> y[i]; hoges[i] = make_pair(x[i], y[i]); } sort(hoges.begin(),hoges.end(),comp); for (int i = 0; i < a; i++) { if (hoges[i].second>=b) { ans += hoges[i].first*b; break; } else { ans += hoges[i].first*hoges[i].second; b -= hoges[i].second; } }*/
replace
65
66
65
66
0
p03008
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define lint long long #define pii pair<int, int> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(c) (c).begin(), (c).end() #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MINF(a) memset(a, 0x3f, sizeof(a)) #define POW(n) (1LL << (n)) #define IN(i, a, b) (a <= i && i <= b) using namespace std; template <typename T> inline bool CHMIN(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <typename T> inline bool CHMAX(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline void SORT(T &a) { sort(ALL(a)); } template <typename T> inline void REV(T &a) { reverse(ALL(a)); } template <typename T> inline void UNI(T &a) { sort(ALL(a)); a.erase(unique(ALL(a)), a.end()); } template <typename T> inline T LB(vector<T> &v, T a) { return *lower_bound(ALL(v), a); } template <typename T> inline int LBP(vector<T> &v, T a) { return lower_bound(ALL(v), a) - v.begin(); } template <typename T> inline T UB(vector<T> &v, T a) { return *upper_bound(ALL(v), a); } template <typename T> inline int UBP(vector<T> &v, T a) { return upper_bound(ALL(v), a) - v.begin(); } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { REP(i, v.size()) { if (i) os << " "; os << v[i]; } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <typename T = int> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } const lint MOD = 1000000007; const lint INF = 0x3f3f3f3f3f3f3f3f; const double EPS = 1e-10; #define _DEBUG int N; int p[2][3]; int dp[25000010]; void _main() { cin >> N; REP(i, 2) REP(j, 3) cin >> p[i][j]; for (int i = 0; i < 3; i++) { for (int j = p[0][i]; j <= N; j++) { dp[j] = max(dp[j], dp[j - p[0][i]] + p[1][i] - p[0][i]); } } N += dp[N]; ZERO(dp); for (int i = 0; i < 3; i++) { for (int j = p[1][i]; j <= N; j++) { dp[j] = max(dp[j], dp[j - p[1][i]] + p[0][i] - p[1][i]); } } cout << N + dp[N] << endl; } signed main() { #ifdef _DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); _main(); return 0; }
#include <bits/stdc++.h> #define int long long #define lint long long #define pii pair<int, int> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(c) (c).begin(), (c).end() #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MINF(a) memset(a, 0x3f, sizeof(a)) #define POW(n) (1LL << (n)) #define IN(i, a, b) (a <= i && i <= b) using namespace std; template <typename T> inline bool CHMIN(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <typename T> inline bool CHMAX(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline void SORT(T &a) { sort(ALL(a)); } template <typename T> inline void REV(T &a) { reverse(ALL(a)); } template <typename T> inline void UNI(T &a) { sort(ALL(a)); a.erase(unique(ALL(a)), a.end()); } template <typename T> inline T LB(vector<T> &v, T a) { return *lower_bound(ALL(v), a); } template <typename T> inline int LBP(vector<T> &v, T a) { return lower_bound(ALL(v), a) - v.begin(); } template <typename T> inline T UB(vector<T> &v, T a) { return *upper_bound(ALL(v), a); } template <typename T> inline int UBP(vector<T> &v, T a) { return upper_bound(ALL(v), a) - v.begin(); } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { REP(i, v.size()) { if (i) os << " "; os << v[i]; } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <typename T = int> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } const lint MOD = 1000000007; const lint INF = 0x3f3f3f3f3f3f3f3f; const double EPS = 1e-10; // #define _DEBUG int N; int p[2][3]; int dp[25000010]; void _main() { cin >> N; REP(i, 2) REP(j, 3) cin >> p[i][j]; for (int i = 0; i < 3; i++) { for (int j = p[0][i]; j <= N; j++) { dp[j] = max(dp[j], dp[j - p[0][i]] + p[1][i] - p[0][i]); } } N += dp[N]; ZERO(dp); for (int i = 0; i < 3; i++) { for (int j = p[1][i]; j <= N; j++) { dp[j] = max(dp[j], dp[j - p[1][i]] + p[0][i] - p[1][i]); } } cout << N + dp[N] << endl; } signed main() { #ifdef _DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); _main(); return 0; }
replace
84
85
84
85
-11
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long // #define double long double #define FOR(i, a, b) for (ll i = (a); i < (b); ++i) #define FORR(i, a, b) for (ll i = (a); i > (b); --i) #define REP(i, n) for (ll i = 0; i < (n); ++i) #define REPR(i, n) for (ll i = n; i >= 0; i--) #define FOREACH(x, a) for (auto &(x) : (a)) #define VECCIN(x) \ for (auto &youso_ : (x)) \ cin >> youso_ #define bitcnt __builtin_popcount #define SZ(x) ((ll)(x).size()) #define fi first #define se second #define All(a) (a).begin(), (a).end() template <typename T = long long> inline T IN() { T x; cin >> x; return (x); } inline void CIN() {} template <class Head, class... Tail> inline void CIN(Head &&head, Tail &&...tail) { cin >> head; CIN(move(tail)...); } #define CCIN(...) \ char __VA_ARGS__; \ CIN(__VA_ARGS__) #define DCIN(...) \ double __VA_ARGS__; \ CIN(__VA_ARGS__) #define LCIN(...) \ ll __VA_ARGS__; \ CIN(__VA_ARGS__) #define SCIN(...) \ string __VA_ARGS__; \ CIN(__VA_ARGS__) #define Printv(v) \ { \ REP(x, v.size()) { cout << v[x] << (x == v.size() - 1 ? "\n" : " "); } \ } template <typename T = string> inline void eputs(T s) { cout << s << "\n"; exit(0); } template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>; template <typename T> using PQ = priority_queue<T>; typedef long long ll; typedef vector<ll> VL; typedef vector<VL> VVL; typedef pair<ll, ll> PL; const int INF = 1e9; const int MOD = 1e9 + 7; const double PI = atan(1.0) * 4.0; const ll LINF = 1e18; const ll dx[] = {1, 0, -1, 0}; const ll dy[] = {0, 1, 0, -1}; void cinfast() { cin.tie(0); ios::sync_with_stdio(false); } VVL gsb(2, VL(3, 0)); ll dp[2][25000000]; signed main() { LCIN(N); REP(i, 2) REP(j, 3) cin >> gsb[i][j]; REP(i, 2) REP(j, 25000000) dp[i][j] = j; REP(i, 2) { REP(j, 3) { ll v = gsb[(i + 1) % 2][j], w = gsb[i][j]; REP(k, N - w + 1) { dp[i][k + w] = max(dp[i][k + w], dp[i][k] + v); } } N = dp[i][N]; } cout << N << "\n"; }
#include <bits/stdc++.h> using namespace std; #define int long long // #define double long double #define FOR(i, a, b) for (ll i = (a); i < (b); ++i) #define FORR(i, a, b) for (ll i = (a); i > (b); --i) #define REP(i, n) for (ll i = 0; i < (n); ++i) #define REPR(i, n) for (ll i = n; i >= 0; i--) #define FOREACH(x, a) for (auto &(x) : (a)) #define VECCIN(x) \ for (auto &youso_ : (x)) \ cin >> youso_ #define bitcnt __builtin_popcount #define SZ(x) ((ll)(x).size()) #define fi first #define se second #define All(a) (a).begin(), (a).end() template <typename T = long long> inline T IN() { T x; cin >> x; return (x); } inline void CIN() {} template <class Head, class... Tail> inline void CIN(Head &&head, Tail &&...tail) { cin >> head; CIN(move(tail)...); } #define CCIN(...) \ char __VA_ARGS__; \ CIN(__VA_ARGS__) #define DCIN(...) \ double __VA_ARGS__; \ CIN(__VA_ARGS__) #define LCIN(...) \ ll __VA_ARGS__; \ CIN(__VA_ARGS__) #define SCIN(...) \ string __VA_ARGS__; \ CIN(__VA_ARGS__) #define Printv(v) \ { \ REP(x, v.size()) { cout << v[x] << (x == v.size() - 1 ? "\n" : " "); } \ } template <typename T = string> inline void eputs(T s) { cout << s << "\n"; exit(0); } template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>; template <typename T> using PQ = priority_queue<T>; typedef long long ll; typedef vector<ll> VL; typedef vector<VL> VVL; typedef pair<ll, ll> PL; const int INF = 1e9; const int MOD = 1e9 + 7; const double PI = atan(1.0) * 4.0; const ll LINF = 1e18; const ll dx[] = {1, 0, -1, 0}; const ll dy[] = {0, 1, 0, -1}; void cinfast() { cin.tie(0); ios::sync_with_stdio(false); } VVL gsb(2, VL(3, 0)); ll dp[2][25000100]; signed main() { LCIN(N); REP(i, 2) REP(j, 3) cin >> gsb[i][j]; REP(i, 2) REP(j, 25000000) dp[i][j] = j; REP(i, 2) { REP(j, 3) { ll v = gsb[(i + 1) % 2][j], w = gsb[i][j]; REP(k, N - w + 1) { dp[i][k + w] = max(dp[i][k + w], dp[i][k] + v); } } N = dp[i][N]; } cout << N << "\n"; }
replace
75
76
75
76
-11
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long n; long long a[3], b[3], go[5005], back[5005]; int main() { scanf("%lld", &n); for (int i = 0; i < 3; i++) { scanf("%lld", &a[i]); } for (int i = 0; i < 3; i++) { scanf("%lld", &b[i]); } for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { if (i - a[j] >= 0) go[i] = max(go[i], go[i - a[j]] + b[j] - a[j]); } } n += go[n]; for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { if (i - b[j] >= 0) back[i] = max(back[i], back[i - b[j]] + a[j] - b[j]); } } n += back[n]; printf("%lld", n); }
#include <bits/stdc++.h> using namespace std; long long n; long long a[3], b[3], go[5005], back[5005 * 5005]; int main() { scanf("%lld", &n); for (int i = 0; i < 3; i++) { scanf("%lld", &a[i]); } for (int i = 0; i < 3; i++) { scanf("%lld", &b[i]); } for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { if (i - a[j] >= 0) go[i] = max(go[i], go[i - a[j]] + b[j] - a[j]); } } n += go[n]; for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { if (i - b[j] >= 0) back[i] = max(back[i], back[i - b[j]] + a[j] - b[j]); } } n += back[n]; printf("%lld", n); }
replace
3
4
3
4
0
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long f[5000005]; int main() { int n; cin >> n; int a[3], b[3]; for (int i = 0; i < 3; i++) cin >> a[i]; for (int i = 0; i < 3; i++) cin >> b[i]; for (int i = 0; i < 3; i++) if (a[i] < b[i]) { for (int j = a[i]; j <= n; j++) f[j] = max(f[j], f[j - a[i]] + b[i]); } long long s = 0; for (int i = 0; i <= n; i++) s = max(s, f[i] - i); n += s; memset(f, 0, sizeof(f)); for (int i = 0; i < 3; i++) if (a[i] > b[i]) { for (int j = b[i]; j <= n; j++) f[j] = max(f[j], f[j - b[i]] + a[i]); } s = 0; for (int i = 0; i <= n; i++) s = max(s, f[i] - i); printf("%lld\n", s + n); return 0; }
#include <bits/stdc++.h> using namespace std; long long f[25000005]; int main() { int n; cin >> n; int a[3], b[3]; for (int i = 0; i < 3; i++) cin >> a[i]; for (int i = 0; i < 3; i++) cin >> b[i]; for (int i = 0; i < 3; i++) if (a[i] < b[i]) { for (int j = a[i]; j <= n; j++) f[j] = max(f[j], f[j - a[i]] + b[i]); } long long s = 0; for (int i = 0; i <= n; i++) s = max(s, f[i] - i); n += s; memset(f, 0, sizeof(f)); for (int i = 0; i < 3; i++) if (a[i] > b[i]) { for (int j = b[i]; j <= n; j++) f[j] = max(f[j], f[j - b[i]] + a[i]); } s = 0; for (int i = 0; i <= n; i++) s = max(s, f[i] - i); printf("%lld\n", s + n); return 0; }
replace
3
4
3
4
0
p03008
C++
Runtime Error
#include <bits/stdc++.h> #define fr(i, n) for (int i = 0; i < (n); ++i) #define foor(i, a, b) for (int i = (a); i <= (b); ++i) #define rf(i, n) for (int i = (n); i--;) #define roof(i, b, a) for (int i = (b); i >= (a); --i) #define elsif else if #define all(x) x.begin(), x.end() #define Sort(x) sort(all(x)) #define Reverse(x) reverse(all(x)) #define PQ priority_queue #define NP(x) next_permutation(all(x)) #define M_PI 3.14159265358979323846 #define popcount __builtin_popcount using namespace std; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<int> vi; typedef vector<vi> vvi; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef unsigned long long ull; typedef vector<ull> vu; typedef vector<vu> vvu; typedef double dbl; typedef vector<dbl> vd; typedef vector<vd> vvd; typedef string str; typedef vector<str> vs; typedef vector<vs> vvs; typedef pair<int, int> pii; typedef vector<pii> vpii; typedef map<int, int> mii; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef map<ll, ll> mll; typedef pair<dbl, dbl> pdd; typedef vector<pdd> vpdd; typedef map<dbl, dbl> mdd; typedef pair<str, str> pss; typedef vector<pss> vpss; typedef map<str, str> mss; typedef pair<int, ll> pil; typedef vector<pil> vpil; typedef map<int, ll> mil; typedef pair<ll, int> pli; typedef vector<pli> vpli; typedef map<ll, int> mli; typedef pair<dbl, int> pdi; typedef vector<pdi> vpdi; typedef map<dbl, int> mdi; template <typename T> vector<T> &operator<<(vector<T> &v, const T t) { v.push_back(t); return v; } template <typename T> multiset<T> &operator<<(multiset<T> &m, const T t) { m.insert(t); return m; } template <typename T> set<T> &operator<<(set<T> &s, const T t) { s.insert(t); return s; } template <typename T> stack<T> &operator<<(stack<T> &s, const T t) { s.push(t); return s; } template <typename T> stack<T> &operator>>(stack<T> &s, T &t) { t = s.top(); s.pop(); return s; } template <typename T> queue<T> &operator<<(queue<T> &q, const T t) { q.push(t); return q; } template <typename T> queue<T> &operator>>(queue<T> &q, T &t) { t = q.front(); q.pop(); return q; } template <typename T, typename U> PQ<T, vector<T>, U> &operator<<(PQ<T, vector<T>, U> &q, const T t) { q.push(t); return q; } template <typename T, typename U> PQ<T, vector<T>, U> &operator>>(PQ<T, vector<T>, U> &q, T &t) { t = q.top(); q.pop(); return q; } template <typename T, typename U> istream &operator>>(istream &s, pair<T, U> &p) { return s >> p.first >> p.second; } istream &operator>>(istream &s, _Bit_reference b) { int a; s >> a; assert(a == 0 || a == 1); b = a; return s; } template <typename T> istream &operator>>(istream &s, vector<T> &v) { fr(i, v.size()) { s >> v[i]; } return s; } template <typename T, typename U> ostream &operator<<(ostream &s, const pair<T, U> p) { return s << p.first << " " << p.second; } // template<typename T>ostream&operator<<(ostream&s,const vector<T>v){for(auto // a:v){s<<a<<"\n";}return s;} template <typename T> ostream &operator<<(ostream &s, const vector<T> v) { fr(i, v.size()) { i ? s << " " << v[i] : s << v[i]; } return s; } template <typename T> ostream &operator<<(ostream &s, const deque<T> d) { fr(i, d.size()) { i ? s << " " << d[i] : s << d[i]; } return s; } template <typename T> _Bit_reference operator&=(_Bit_reference b, T t) { return b = b & t; } template <typename T> _Bit_reference operator^=(_Bit_reference b, T t) { return b = b ^ t; } template <typename T> _Bit_reference operator|=(_Bit_reference b, T t) { return b = b | t; } template <typename T, typename U> pair<T, U> operator+(pair<T, U> a, pair<T, U> b) { return {a.first + b.first, a.second + b.second}; } template <typename T, typename U> pair<T, U> operator-(pair<T, U> a, pair<T, U> b) { return {a.first - b.first, a.second - b.second}; } template <typename T, typename U> pair<T, U> &operator+=(pair<T, U> &a, pair<T, U> b) { return a = a + b; } template <typename T, typename U> pair<T, U> &operator-=(pair<T, U> &a, pair<T, U> b) { return a = a - b; } void print(void) { cout << "\n"; } void Print(void) { cout << endl; } template <typename T> void print(T t) { cout << t << "\n"; } template <typename T> void Print(T t) { cout << t << endl; } template <typename T, typename... U> void print(T &&t, U &&...u) { cout << t << " "; print(forward<U>(u)...); } template <typename T, typename... U> void Print(T &&t, U &&...u) { cout << t << " "; Print(forward<U>(u)...); } bool YN(bool b) { print(b ? "YES" : "NO"); return b; } bool PI(bool b) { print(b ? "POSSIBLE" : "IMPOSSIBLE"); return b; } bool Yn(bool b) { print(b ? "Yes" : "No"); return b; } bool Pi(bool b) { print(b ? "Possible" : "Impossible"); return b; } bool yn(bool b) { print(b ? "yes" : "no"); return b; } bool pi(bool b) { print(b ? "possible" : "impossible"); return b; } const int e5 = 1e5; const int e9 = 1e9; const int MD = 1e9 + 7; const ll e18 = 1e18; template <typename T> str to_string(const T &n) { ostringstream s; s << n; return s.str(); } template <typename T> T &chmax(T &a, T b) { return a = max(a, b); } template <typename T> T &chmin(T &a, T b) { return a = min(a, b); } template <typename T, typename U> vector<pair<T, U>> dijkstra(const vector<vector<pair<T, U>>> &E, const U s, const T inf) { using P = pair<T, U>; vector<P> d; fr(i, E.size()) { d << P{inf, i}; } PQ<P, vector<P>, greater<P>> pq; pq << (d[s] = P{0, s}); while (pq.size()) { P a = pq.top(); pq.pop(); U v = a.second; if (d[v].first >= a.first) { for (P e : E[v]) { if (d[v].first + e.first < d[e.second].first) { d[e.second] = P{d[v].first + e.first, v}; pq << P{d[v].first + e.first, e.second}; } } } } return d; } template <typename T, typename U> map<U, pair<T, U>> dijkstra(map<U, vector<pair<T, U>>> E, const U s, const T inf) { using P = pair<T, U>; map<U, P> d; for (pair<U, vector<P>> e : E) { d[e.first] = P{inf, e.first}; } PQ<P, vector<P>, greater<P>> pq; pq << (d[s] = P{0, s}); while (pq.size()) { P a = pq.top(); pq.pop(); U v = a.second; if (d[v].first >= a.first) { for (P e : E[v]) { if (d[v].first + e.first < d[e.second].first) { d[e.second] = P{d[v].first + e.first, v}; pq << P{d[v].first + e.first, e.second}; } } } } return d; } ll maxflow(vector<mil> &E, int s, int t) { ll z = 0; vi b(E.size(), -1); for (int i = 0;; ++i) { static auto dfs = [&](int v, ll f, auto &dfs) -> ll { if (v == t) return f; b[v] = i; for (auto &p : E[v]) { if (b[p.first] < i && p.second) { if (ll r = dfs(p.first, min(f, p.second), dfs)) { p.second -= r; E[p.first][v] += r; return r; } } } return 0; }; ll x = dfs(s, ll(1e18), dfs); z += x; if (x == 0) return z; } } template <typename T> T distsq(pair<T, T> a, pair<T, T> b) { return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); } template <typename T> T max(const vector<T> a) { assert(a.size()); T m = a[0]; for (T e : a) { m = max(m, e); } return m; } template <typename T> T min(const vector<T> a) { assert(a.size()); T m = a[0]; for (T e : a) { m = min(m, e); } return m; } template <typename T> T gcd(const T a, const T b) { return a ? gcd(b % a, a) : b; } template <typename T> T gcd(const vector<T> a) { T g = a[0]; for (T e : a) { g = gcd(g, e); } return g; } template <typename T> vector<T> LIS(const vector<T> A) { vector<T> B; for (T a : A) { auto it = lower_bound(all(B), a); if (it == B.end()) { B << a; } else { *it = a; } } return B; } template <typename T> vector<T> LCS(vector<T> A, vector<T> B) { int N = A.size(), M = B.size(); vector<vector<pair<int, pii>>> d(N + 1, vector<pair<int, pii>>(M + 1)); fr(i, N) { fr(j, M) { if (A[i] == B[j]) { d[i + 1][j + 1] = {d[i][j].first + 1, {i, j}}; } else { d[i + 1][j + 1] = max(d[i][j + 1], d[i + 1][j]); } } } vector<T> r; for (pii p = {N, M}; d[p.first][p.second].first; p = d[p.first][p.second].second) { r << A[d[p.first][p.second].second.first]; } Reverse(r); return r; } str LCS(str S, str T) { vector<char> s = LCS(vector<char>(S.begin(), S.end()), vector<char>(T.begin(), T.end())); return str(s.begin(), s.end()); } template <typename T> vector<pair<T, T>> ConvexHull(vector<pair<T, T>> V) { if (V.size() <= 3) { return V; } Sort(V); rf(i, V.size() - 1) V << V[i]; vector<pair<T, T>> r; for (pair<T, T> p : V) { int s = r.size(); while (s >= 2 && (p.second - r[s - 1].second) * (p.first - r[s - 2].first) < (p.second - r[s - 2].second) * (p.first - r[s - 1].first)) { r.pop_back(); --s; } r << p; } r.pop_back(); return r; } class UnionFind { vi p, s; void extend(int N) { foor(i, p.size(), N) { p << i; s << 1; } } public: UnionFind(void) {} UnionFind(int N) { extend(N - 1); } int find(int i) { extend(i); return p[i] = p[i] == i ? i : find(p[i]); } void unite(int a, int b) { extend(a); extend(b); if ((a = find(a)) != (b = find(b))) { if (s[a] > s[b]) { swap(a, b); } s[b] += s[a]; p[a] = b; } } void unite(pii p) { return unite(p.first, p.second); } bool same(int a, int b) { extend(a); extend(b); return find(a) == find(b); } bool same(pii p) { return same(p.first, p.second); } int size(int x) { extend(x); return s[find(x)]; } }; ll MST(vector<pair<ll, pii>> &E) { Sort(E); UnionFind uf; ll z = 0; for (auto &e : E) { if (!uf.same(e.second)) { z += e.first; uf.unite(e.second); } } return z; } ll strmod(const str &s, const int m) { ll x = 0; fr(i, s.size()) { x = (x * 10 + s[i] - 48) % m; } return x; } vvl mul(const vvl &A, const vvl &B, const int m) { vvl C; fr(y, A.size()) { C << vl(B[y].size()); } fr(y, C.size()) { fr(x, C[y].size()) { fr(i, A[0].size()) { (C[y][x] += A[y][i] * B[i][x]) %= m; } } } return C; } vvl pow(const vvl &A, const ll n, const int m) { vvl B; fr(y, A.size()) { B << vl(A.size()); } if (n == 0) { fr(i, B.size()) { B[i][i] = 1; } } elsif(n % 2) { B = mul(A, pow(A, n - 1, m), m); } else { vvl C = pow(A, n / 2, m); B = mul(C, C, m); } return B; } ll pow(const ll a, const ll n, const int m) { ll t; return n ? (n & 1 ? a >= 0 ? a % m : (m - (-a % m)) % m : 1) * (t = pow(a, n >> 1, m), t * t % m) % m : !!a; } ll inv(const ll x, const int p) { assert(x != 0); return pow(x, p - 2, p); } ll inv(const ll x) { return inv(x, MD); } vpll fact(const int n, const int p) { assert(n < p); vpll v(n + 1); v[0].first = 1; foor(i, 1, n) { v[i].first = v[i - 1].first * i % p; } v[n].second = inv(v[n].first, p); roof(i, n, 1) { v[i - 1].second = v[i].second * i % p; } return v; } class Combination { const vpll f; const int M; public: Combination(int n, int m) : f(fact(n, m)), M(m) {} Combination(int n) : Combination(n, MD) {} ll P(int n, int k) { return n < 0 || k < 0 || n < k ? 0ll : f[n].first * f[n - k].second % M; } ll C(int n, int k) { return k < 0 ? 0 : P(n, k) * f[k].second % M; } ll H(int n, int k) { return n == 0 && k == 0 ? 1ll : C(n + k - 1, k); } ll F(int n) { return n < 0 ? 0 : f[n].first; } }; ll C2(const int n) { return (ll)n * ~-n / 2; } ll sum(const vi a) { ll s = 0; for (int e : a) { s += e; } return s; } ll sum(const vl a) { ll s = 0; for (ll e : a) { s += e; } return s; } template <typename T> int MSB(T N) { int r = -1; for (; N > 0; N /= 2) { ++r; } return r; } template <typename T> class SegmentTree { vector<T> S; T (*const op)(T a, T b); const T zero; const int B; public: SegmentTree(int N, T (*f)(T a, T b), const T zero) : S(1 << MSB(N - 1) + 2, zero), op(f), zero(zero), B(1 << MSB(N - 1) + 1) {} SegmentTree(vector<T> v, T (*f)(T a, T b), const T zero) : SegmentTree(v.size(), f, zero) { fr(i, v.size()) { S[S.size() / 2 + i] = v[i]; } roof(i, S.size() / 2 - 1, 1) { S[i] = op(S[i * 2], S[i * 2 + 1]); } } T calc(int l, int r) { l += B; r += B; if (l > r) { return zero; } if (l == r) { return S[l]; } T L = S[l], R = S[r]; for (; l / 2 < r / 2; l /= 2, r /= 2) { if (l % 2 == 0) { L = op(L, S[l + 1]); } if (r % 2 == 1) { R = op(S[r - 1], R); } } return op(L, R); } void replace(int i, T x) { for (S[i += B] = x; i != 1; i /= 2) { if (i % 2) { S[i / 2] = op(S[i - 1], S[i]); } else { S[i / 2] = op(S[i], S[i + 1]); } } } void add(int i, T x) { replace(i, op(S[B + i], x)); } T top() { return S[1]; } }; ll BITsum(vl &B, int i) { ll z = 0; while (i > 0) { z += B[i]; i -= i & -i; } return z; } void BITadd(vl &B, int i, ll x) { while (i < B.size()) { B[i] += x; i += i & -i; } } ll fib(const ll n, const int m) { ll a, b, c, d, A, B, C, D; a = 1; b = 0; c = 0; d = 1; rf(i, 63) { A = a * a + b * c; B = a * b + b * d; C = c * a + d * c; D = c * b + d * d; if (n >> i & 1) { a = A; b = B; c = C; d = D; A = a + b; B = a; C = c + d; D = c; } a = A % m; b = B % m; c = C % m; d = D % m; } return b; } vi primes(int n) { vb b(n + 1); vi p; foor(i, 2, n) { if (!b[i]) { p << i; for (int j = 2 * i; j <= n; j += i) { b[j] = true; } } } return p; } vb isprime(const int n) { vb v(n + 1, true); v[0] = v[1] = false; foor(i, 2, n) { if (v[i]) { for (int j = 2 * i; j <= n; j += i) { v[j] = false; } } } return v; } class LCA { vvi par; vi dep; public: LCA(vvi &E, int root) : par(MSB(E.size()) + 1, vi(E.size())), dep(E.size()) { function<void(int, int)> dfs = [&](int i, int p) { for (int j : E[i]) if (j != p) { par[0][j] = i; dep[j] = dep[i] + 1; dfs(j, i); } }; par[0][root] = root; dfs(root, root); fr(i, par.size() - 1) { fr(j, par[0].size()) { par[i + 1][j] = par[i][par[i][j]]; } } } int operator()(int a, int b) { if (dep[a] > dep[b]) swap(a, b); for (int t = dep[b] - dep[a], i = 0; t; t >>= 1, ++i) { if (t & 1) { b = par[i][b]; } } if (a == b) return a; rf(i, par.size()) { if (par[i][a] != par[i][b]) { a = par[i][a]; b = par[i][b]; } } return par[0][a]; } }; vpii factor(int N) { vpii r; for (int i = 2; i * i <= N; ++i) { if (N % i == 0) { r << pii{i, 0}; while (N % i == 0) { N /= i; ++r.back().second; } } } if (N > 1) { r << pii{N, 1}; } return r; } vi SuffixArray(str S) { int N = S.size(); vi rank(N + 1), tmp(N + 1), sa(N + 1); fr(i, N) { sa[i] = i; rank[i] = S[i]; } sa[N] = N; rank[N] = -1; int k; auto cmp = [&](int &a, int &b) -> bool { if (rank[a] != rank[b]) return rank[a] < rank[b]; return (a + k <= N ? rank[a + k] : -1) < (b + k <= N ? rank[b + k] : -1); }; for (k = 1; k <= N; k *= 2) { sort(all(sa), cmp); tmp[sa[0]] = 0; foor(i, 1, N) { tmp[sa[i]] = tmp[sa[i - 1]] + cmp(sa[i - 1], sa[i]); } rank = tmp; } return sa; }; int main() { cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; vpll A(3); cin >> A[0].first >> A[1].first >> A[2].first; cin >> A[0].second >> A[1].second >> A[2].second; sort(all(A), [&](pll &a, pll &b) { return a.first - a.second < b.first - b.second; }); ll z = 0; if (A[0].first > A[0].second) { for (ll g = 0; g * A[0].second <= N; ++g) { for (ll s = 0; g * A[0].second + s * A[1].second <= N; ++s) { ll b = (N - g * A[0].second - s * A[1].second) / A[2].second; ll n = N - g * A[0].second - s * A[1].second - b * A[2].second; chmax(z, n + g * A[0].first + s * A[1].first + b * A[2].first); } } } elsif(A[1].first > A[1].second) { N = N % A[0].first + N / A[0].first * A[0].second; for (ll s = 0; s * A[1].second <= N; ++s) { ll b = (N - s * A[1].second) / A[2].second; ll n = N - s * A[1].second - b * A[2].second; chmax(z, n + s * A[1].first + b * A[2].first); } } elsif(A[2].first > A[2].second) { assert(false); } else { for (ll g = 0; g * A[0].first <= N; ++g) { for (ll s = 0; g * A[0].first + s * A[1].first <= N; ++s) { ll b = (N - g * A[0].first - s * A[1].first) / A[2].first; ll n = N - g * A[0].first - s * A[1].first - b * A[2].first; chmax(z, n + g * A[0].second + s * A[1].second + b * A[2].second); } } } print(z); return 0; }
#include <bits/stdc++.h> #define fr(i, n) for (int i = 0; i < (n); ++i) #define foor(i, a, b) for (int i = (a); i <= (b); ++i) #define rf(i, n) for (int i = (n); i--;) #define roof(i, b, a) for (int i = (b); i >= (a); --i) #define elsif else if #define all(x) x.begin(), x.end() #define Sort(x) sort(all(x)) #define Reverse(x) reverse(all(x)) #define PQ priority_queue #define NP(x) next_permutation(all(x)) #define M_PI 3.14159265358979323846 #define popcount __builtin_popcount using namespace std; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<int> vi; typedef vector<vi> vvi; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef unsigned long long ull; typedef vector<ull> vu; typedef vector<vu> vvu; typedef double dbl; typedef vector<dbl> vd; typedef vector<vd> vvd; typedef string str; typedef vector<str> vs; typedef vector<vs> vvs; typedef pair<int, int> pii; typedef vector<pii> vpii; typedef map<int, int> mii; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef map<ll, ll> mll; typedef pair<dbl, dbl> pdd; typedef vector<pdd> vpdd; typedef map<dbl, dbl> mdd; typedef pair<str, str> pss; typedef vector<pss> vpss; typedef map<str, str> mss; typedef pair<int, ll> pil; typedef vector<pil> vpil; typedef map<int, ll> mil; typedef pair<ll, int> pli; typedef vector<pli> vpli; typedef map<ll, int> mli; typedef pair<dbl, int> pdi; typedef vector<pdi> vpdi; typedef map<dbl, int> mdi; template <typename T> vector<T> &operator<<(vector<T> &v, const T t) { v.push_back(t); return v; } template <typename T> multiset<T> &operator<<(multiset<T> &m, const T t) { m.insert(t); return m; } template <typename T> set<T> &operator<<(set<T> &s, const T t) { s.insert(t); return s; } template <typename T> stack<T> &operator<<(stack<T> &s, const T t) { s.push(t); return s; } template <typename T> stack<T> &operator>>(stack<T> &s, T &t) { t = s.top(); s.pop(); return s; } template <typename T> queue<T> &operator<<(queue<T> &q, const T t) { q.push(t); return q; } template <typename T> queue<T> &operator>>(queue<T> &q, T &t) { t = q.front(); q.pop(); return q; } template <typename T, typename U> PQ<T, vector<T>, U> &operator<<(PQ<T, vector<T>, U> &q, const T t) { q.push(t); return q; } template <typename T, typename U> PQ<T, vector<T>, U> &operator>>(PQ<T, vector<T>, U> &q, T &t) { t = q.top(); q.pop(); return q; } template <typename T, typename U> istream &operator>>(istream &s, pair<T, U> &p) { return s >> p.first >> p.second; } istream &operator>>(istream &s, _Bit_reference b) { int a; s >> a; assert(a == 0 || a == 1); b = a; return s; } template <typename T> istream &operator>>(istream &s, vector<T> &v) { fr(i, v.size()) { s >> v[i]; } return s; } template <typename T, typename U> ostream &operator<<(ostream &s, const pair<T, U> p) { return s << p.first << " " << p.second; } // template<typename T>ostream&operator<<(ostream&s,const vector<T>v){for(auto // a:v){s<<a<<"\n";}return s;} template <typename T> ostream &operator<<(ostream &s, const vector<T> v) { fr(i, v.size()) { i ? s << " " << v[i] : s << v[i]; } return s; } template <typename T> ostream &operator<<(ostream &s, const deque<T> d) { fr(i, d.size()) { i ? s << " " << d[i] : s << d[i]; } return s; } template <typename T> _Bit_reference operator&=(_Bit_reference b, T t) { return b = b & t; } template <typename T> _Bit_reference operator^=(_Bit_reference b, T t) { return b = b ^ t; } template <typename T> _Bit_reference operator|=(_Bit_reference b, T t) { return b = b | t; } template <typename T, typename U> pair<T, U> operator+(pair<T, U> a, pair<T, U> b) { return {a.first + b.first, a.second + b.second}; } template <typename T, typename U> pair<T, U> operator-(pair<T, U> a, pair<T, U> b) { return {a.first - b.first, a.second - b.second}; } template <typename T, typename U> pair<T, U> &operator+=(pair<T, U> &a, pair<T, U> b) { return a = a + b; } template <typename T, typename U> pair<T, U> &operator-=(pair<T, U> &a, pair<T, U> b) { return a = a - b; } void print(void) { cout << "\n"; } void Print(void) { cout << endl; } template <typename T> void print(T t) { cout << t << "\n"; } template <typename T> void Print(T t) { cout << t << endl; } template <typename T, typename... U> void print(T &&t, U &&...u) { cout << t << " "; print(forward<U>(u)...); } template <typename T, typename... U> void Print(T &&t, U &&...u) { cout << t << " "; Print(forward<U>(u)...); } bool YN(bool b) { print(b ? "YES" : "NO"); return b; } bool PI(bool b) { print(b ? "POSSIBLE" : "IMPOSSIBLE"); return b; } bool Yn(bool b) { print(b ? "Yes" : "No"); return b; } bool Pi(bool b) { print(b ? "Possible" : "Impossible"); return b; } bool yn(bool b) { print(b ? "yes" : "no"); return b; } bool pi(bool b) { print(b ? "possible" : "impossible"); return b; } const int e5 = 1e5; const int e9 = 1e9; const int MD = 1e9 + 7; const ll e18 = 1e18; template <typename T> str to_string(const T &n) { ostringstream s; s << n; return s.str(); } template <typename T> T &chmax(T &a, T b) { return a = max(a, b); } template <typename T> T &chmin(T &a, T b) { return a = min(a, b); } template <typename T, typename U> vector<pair<T, U>> dijkstra(const vector<vector<pair<T, U>>> &E, const U s, const T inf) { using P = pair<T, U>; vector<P> d; fr(i, E.size()) { d << P{inf, i}; } PQ<P, vector<P>, greater<P>> pq; pq << (d[s] = P{0, s}); while (pq.size()) { P a = pq.top(); pq.pop(); U v = a.second; if (d[v].first >= a.first) { for (P e : E[v]) { if (d[v].first + e.first < d[e.second].first) { d[e.second] = P{d[v].first + e.first, v}; pq << P{d[v].first + e.first, e.second}; } } } } return d; } template <typename T, typename U> map<U, pair<T, U>> dijkstra(map<U, vector<pair<T, U>>> E, const U s, const T inf) { using P = pair<T, U>; map<U, P> d; for (pair<U, vector<P>> e : E) { d[e.first] = P{inf, e.first}; } PQ<P, vector<P>, greater<P>> pq; pq << (d[s] = P{0, s}); while (pq.size()) { P a = pq.top(); pq.pop(); U v = a.second; if (d[v].first >= a.first) { for (P e : E[v]) { if (d[v].first + e.first < d[e.second].first) { d[e.second] = P{d[v].first + e.first, v}; pq << P{d[v].first + e.first, e.second}; } } } } return d; } ll maxflow(vector<mil> &E, int s, int t) { ll z = 0; vi b(E.size(), -1); for (int i = 0;; ++i) { static auto dfs = [&](int v, ll f, auto &dfs) -> ll { if (v == t) return f; b[v] = i; for (auto &p : E[v]) { if (b[p.first] < i && p.second) { if (ll r = dfs(p.first, min(f, p.second), dfs)) { p.second -= r; E[p.first][v] += r; return r; } } } return 0; }; ll x = dfs(s, ll(1e18), dfs); z += x; if (x == 0) return z; } } template <typename T> T distsq(pair<T, T> a, pair<T, T> b) { return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); } template <typename T> T max(const vector<T> a) { assert(a.size()); T m = a[0]; for (T e : a) { m = max(m, e); } return m; } template <typename T> T min(const vector<T> a) { assert(a.size()); T m = a[0]; for (T e : a) { m = min(m, e); } return m; } template <typename T> T gcd(const T a, const T b) { return a ? gcd(b % a, a) : b; } template <typename T> T gcd(const vector<T> a) { T g = a[0]; for (T e : a) { g = gcd(g, e); } return g; } template <typename T> vector<T> LIS(const vector<T> A) { vector<T> B; for (T a : A) { auto it = lower_bound(all(B), a); if (it == B.end()) { B << a; } else { *it = a; } } return B; } template <typename T> vector<T> LCS(vector<T> A, vector<T> B) { int N = A.size(), M = B.size(); vector<vector<pair<int, pii>>> d(N + 1, vector<pair<int, pii>>(M + 1)); fr(i, N) { fr(j, M) { if (A[i] == B[j]) { d[i + 1][j + 1] = {d[i][j].first + 1, {i, j}}; } else { d[i + 1][j + 1] = max(d[i][j + 1], d[i + 1][j]); } } } vector<T> r; for (pii p = {N, M}; d[p.first][p.second].first; p = d[p.first][p.second].second) { r << A[d[p.first][p.second].second.first]; } Reverse(r); return r; } str LCS(str S, str T) { vector<char> s = LCS(vector<char>(S.begin(), S.end()), vector<char>(T.begin(), T.end())); return str(s.begin(), s.end()); } template <typename T> vector<pair<T, T>> ConvexHull(vector<pair<T, T>> V) { if (V.size() <= 3) { return V; } Sort(V); rf(i, V.size() - 1) V << V[i]; vector<pair<T, T>> r; for (pair<T, T> p : V) { int s = r.size(); while (s >= 2 && (p.second - r[s - 1].second) * (p.first - r[s - 2].first) < (p.second - r[s - 2].second) * (p.first - r[s - 1].first)) { r.pop_back(); --s; } r << p; } r.pop_back(); return r; } class UnionFind { vi p, s; void extend(int N) { foor(i, p.size(), N) { p << i; s << 1; } } public: UnionFind(void) {} UnionFind(int N) { extend(N - 1); } int find(int i) { extend(i); return p[i] = p[i] == i ? i : find(p[i]); } void unite(int a, int b) { extend(a); extend(b); if ((a = find(a)) != (b = find(b))) { if (s[a] > s[b]) { swap(a, b); } s[b] += s[a]; p[a] = b; } } void unite(pii p) { return unite(p.first, p.second); } bool same(int a, int b) { extend(a); extend(b); return find(a) == find(b); } bool same(pii p) { return same(p.first, p.second); } int size(int x) { extend(x); return s[find(x)]; } }; ll MST(vector<pair<ll, pii>> &E) { Sort(E); UnionFind uf; ll z = 0; for (auto &e : E) { if (!uf.same(e.second)) { z += e.first; uf.unite(e.second); } } return z; } ll strmod(const str &s, const int m) { ll x = 0; fr(i, s.size()) { x = (x * 10 + s[i] - 48) % m; } return x; } vvl mul(const vvl &A, const vvl &B, const int m) { vvl C; fr(y, A.size()) { C << vl(B[y].size()); } fr(y, C.size()) { fr(x, C[y].size()) { fr(i, A[0].size()) { (C[y][x] += A[y][i] * B[i][x]) %= m; } } } return C; } vvl pow(const vvl &A, const ll n, const int m) { vvl B; fr(y, A.size()) { B << vl(A.size()); } if (n == 0) { fr(i, B.size()) { B[i][i] = 1; } } elsif(n % 2) { B = mul(A, pow(A, n - 1, m), m); } else { vvl C = pow(A, n / 2, m); B = mul(C, C, m); } return B; } ll pow(const ll a, const ll n, const int m) { ll t; return n ? (n & 1 ? a >= 0 ? a % m : (m - (-a % m)) % m : 1) * (t = pow(a, n >> 1, m), t * t % m) % m : !!a; } ll inv(const ll x, const int p) { assert(x != 0); return pow(x, p - 2, p); } ll inv(const ll x) { return inv(x, MD); } vpll fact(const int n, const int p) { assert(n < p); vpll v(n + 1); v[0].first = 1; foor(i, 1, n) { v[i].first = v[i - 1].first * i % p; } v[n].second = inv(v[n].first, p); roof(i, n, 1) { v[i - 1].second = v[i].second * i % p; } return v; } class Combination { const vpll f; const int M; public: Combination(int n, int m) : f(fact(n, m)), M(m) {} Combination(int n) : Combination(n, MD) {} ll P(int n, int k) { return n < 0 || k < 0 || n < k ? 0ll : f[n].first * f[n - k].second % M; } ll C(int n, int k) { return k < 0 ? 0 : P(n, k) * f[k].second % M; } ll H(int n, int k) { return n == 0 && k == 0 ? 1ll : C(n + k - 1, k); } ll F(int n) { return n < 0 ? 0 : f[n].first; } }; ll C2(const int n) { return (ll)n * ~-n / 2; } ll sum(const vi a) { ll s = 0; for (int e : a) { s += e; } return s; } ll sum(const vl a) { ll s = 0; for (ll e : a) { s += e; } return s; } template <typename T> int MSB(T N) { int r = -1; for (; N > 0; N /= 2) { ++r; } return r; } template <typename T> class SegmentTree { vector<T> S; T (*const op)(T a, T b); const T zero; const int B; public: SegmentTree(int N, T (*f)(T a, T b), const T zero) : S(1 << MSB(N - 1) + 2, zero), op(f), zero(zero), B(1 << MSB(N - 1) + 1) {} SegmentTree(vector<T> v, T (*f)(T a, T b), const T zero) : SegmentTree(v.size(), f, zero) { fr(i, v.size()) { S[S.size() / 2 + i] = v[i]; } roof(i, S.size() / 2 - 1, 1) { S[i] = op(S[i * 2], S[i * 2 + 1]); } } T calc(int l, int r) { l += B; r += B; if (l > r) { return zero; } if (l == r) { return S[l]; } T L = S[l], R = S[r]; for (; l / 2 < r / 2; l /= 2, r /= 2) { if (l % 2 == 0) { L = op(L, S[l + 1]); } if (r % 2 == 1) { R = op(S[r - 1], R); } } return op(L, R); } void replace(int i, T x) { for (S[i += B] = x; i != 1; i /= 2) { if (i % 2) { S[i / 2] = op(S[i - 1], S[i]); } else { S[i / 2] = op(S[i], S[i + 1]); } } } void add(int i, T x) { replace(i, op(S[B + i], x)); } T top() { return S[1]; } }; ll BITsum(vl &B, int i) { ll z = 0; while (i > 0) { z += B[i]; i -= i & -i; } return z; } void BITadd(vl &B, int i, ll x) { while (i < B.size()) { B[i] += x; i += i & -i; } } ll fib(const ll n, const int m) { ll a, b, c, d, A, B, C, D; a = 1; b = 0; c = 0; d = 1; rf(i, 63) { A = a * a + b * c; B = a * b + b * d; C = c * a + d * c; D = c * b + d * d; if (n >> i & 1) { a = A; b = B; c = C; d = D; A = a + b; B = a; C = c + d; D = c; } a = A % m; b = B % m; c = C % m; d = D % m; } return b; } vi primes(int n) { vb b(n + 1); vi p; foor(i, 2, n) { if (!b[i]) { p << i; for (int j = 2 * i; j <= n; j += i) { b[j] = true; } } } return p; } vb isprime(const int n) { vb v(n + 1, true); v[0] = v[1] = false; foor(i, 2, n) { if (v[i]) { for (int j = 2 * i; j <= n; j += i) { v[j] = false; } } } return v; } class LCA { vvi par; vi dep; public: LCA(vvi &E, int root) : par(MSB(E.size()) + 1, vi(E.size())), dep(E.size()) { function<void(int, int)> dfs = [&](int i, int p) { for (int j : E[i]) if (j != p) { par[0][j] = i; dep[j] = dep[i] + 1; dfs(j, i); } }; par[0][root] = root; dfs(root, root); fr(i, par.size() - 1) { fr(j, par[0].size()) { par[i + 1][j] = par[i][par[i][j]]; } } } int operator()(int a, int b) { if (dep[a] > dep[b]) swap(a, b); for (int t = dep[b] - dep[a], i = 0; t; t >>= 1, ++i) { if (t & 1) { b = par[i][b]; } } if (a == b) return a; rf(i, par.size()) { if (par[i][a] != par[i][b]) { a = par[i][a]; b = par[i][b]; } } return par[0][a]; } }; vpii factor(int N) { vpii r; for (int i = 2; i * i <= N; ++i) { if (N % i == 0) { r << pii{i, 0}; while (N % i == 0) { N /= i; ++r.back().second; } } } if (N > 1) { r << pii{N, 1}; } return r; } vi SuffixArray(str S) { int N = S.size(); vi rank(N + 1), tmp(N + 1), sa(N + 1); fr(i, N) { sa[i] = i; rank[i] = S[i]; } sa[N] = N; rank[N] = -1; int k; auto cmp = [&](int &a, int &b) -> bool { if (rank[a] != rank[b]) return rank[a] < rank[b]; return (a + k <= N ? rank[a + k] : -1) < (b + k <= N ? rank[b + k] : -1); }; for (k = 1; k <= N; k *= 2) { sort(all(sa), cmp); tmp[sa[0]] = 0; foor(i, 1, N) { tmp[sa[i]] = tmp[sa[i - 1]] + cmp(sa[i - 1], sa[i]); } rank = tmp; } return sa; }; int main() { cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; vpll A(3); cin >> A[0].first >> A[1].first >> A[2].first; cin >> A[0].second >> A[1].second >> A[2].second; sort(all(A), [&](pll &a, pll &b) { return a.first - a.second < b.first - b.second; }); ll z = 0; if (A[0].first > A[0].second) { for (ll g = 0; g * A[0].second <= N; ++g) { for (ll s = 0; g * A[0].second + s * A[1].second <= N; ++s) { ll b = (N - g * A[0].second - s * A[1].second) / A[2].second; ll n = N - g * A[0].second - s * A[1].second - b * A[2].second; chmax(z, n + g * A[0].first + s * A[1].first + b * A[2].first); } } } elsif(A[1].first > A[1].second) { N = N % A[0].first + N / A[0].first * A[0].second; for (ll s = 0; s * A[1].second <= N; ++s) { ll b = (N - s * A[1].second) / A[2].second; ll n = N - s * A[1].second - b * A[2].second; chmax(z, n + s * A[1].first + b * A[2].first); } } elsif(A[2].first > A[2].second) { for (ll s = 0; s * A[0].first <= N; ++s) { for (ll g = 0; s * A[0].first + g * A[1].first <= N; ++g) { ll n = N - s * A[0].first - g * A[1].first + s * A[0].second + g * A[1].second; ll b = n / A[2].second; chmax(z, n % A[2].second + b * A[2].first); } } } else { for (ll g = 0; g * A[0].first <= N; ++g) { for (ll s = 0; g * A[0].first + s * A[1].first <= N; ++s) { ll b = (N - g * A[0].first - s * A[1].first) / A[2].first; ll n = N - g * A[0].first - s * A[1].first - b * A[2].first; chmax(z, n + g * A[0].second + s * A[1].second + b * A[2].second); } } } print(z); return 0; }
replace
709
710
709
719
0
p03008
C++
Runtime Error
#include <iostream> using namespace std; const long long MAXN = 5e6 + 5; long long a[5][5]; long long dp[MAXN]; long long m, ans; int main() { long long n; cin >> n; for (long long i = 0; i < 2; i++) { for (long long j = 1; j <= 3; j++) { cin >> a[i][j]; } } for (long long i = 0; i <= n; i++) { dp[i] = n; } m = n; for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= 3; j++) { if (a[1][j] > a[0][j] && i - a[0][j] >= 0) { dp[i] = max(dp[i], dp[i - a[0][j]] + a[1][j] - a[0][j]); } m = max(m, dp[i]); } } for (long long i = 0; i <= m; i++) { dp[i] = m; } for (long long i = 1; i <= m; i++) { for (long long j = 1; j <= 3; j++) { if (a[0][j] > a[1][j] && i - a[1][j] >= 0) { dp[i] = max(dp[i], dp[i - a[1][j]] + a[0][j] - a[1][j]); } ans = max(ans, dp[i]); } } cout << ans << endl; }
#include <iostream> using namespace std; const long long MAXN = 5e7 + 5; long long a[5][5]; long long dp[MAXN]; long long m, ans; int main() { long long n; cin >> n; for (long long i = 0; i < 2; i++) { for (long long j = 1; j <= 3; j++) { cin >> a[i][j]; } } for (long long i = 0; i <= n; i++) { dp[i] = n; } m = n; for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= 3; j++) { if (a[1][j] > a[0][j] && i - a[0][j] >= 0) { dp[i] = max(dp[i], dp[i - a[0][j]] + a[1][j] - a[0][j]); } m = max(m, dp[i]); } } for (long long i = 0; i <= m; i++) { dp[i] = m; } for (long long i = 1; i <= m; i++) { for (long long j = 1; j <= 3; j++) { if (a[0][j] > a[1][j] && i - a[1][j] >= 0) { dp[i] = max(dp[i], dp[i - a[1][j]] + a[0][j] - a[1][j]); } ans = max(ans, dp[i]); } } cout << ans << endl; }
replace
3
4
3
4
0
p03008
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define REP(i, N) for (int i = 0; i < N; ++i) #define MAX_I INT_MAX // 1e9 #define MIN_I INT_MIN //-1e9 #define MAX_UI UINT_MAX // 1e9 #define MAX_LL LLONG_MAX // 1e18 #define MIN_LL LLONG_MIN //-1e18 #define MAX_ULL ULLONG_MAX // 1e19 typedef long long ll; typedef pair<int, int> PII; typedef pair<char, char> PCC; typedef pair<ll, ll> PLL; typedef pair<char, int> PCI; typedef pair<int, char> PIC; typedef pair<ll, int> PLI; typedef pair<int, ll> PIL; typedef pair<ll, char> PLC; typedef pair<char, ll> PCL; bool comp(const PII &l, const PII &r) { return (double)l.second / l.first > (double)r.second / r.first; } int main(void) { ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(15); ll N; cin >> N; int N_MAX = 3e7; vector<PLL> x(3); cin >> x[0].first >> x[1].first >> x[2].first; cin >> x[0].second >> x[1].second >> x[2].second; int M = x.size(); // dp[i+1][j]:i番目まで見て,j個ある時の変換後の最大数 vector<vector<ll>> dp(M + 1, vector<ll>(N_MAX + 1)); REP(j, N + 1) dp[0][j] = N; REP(i, M) { REP(j, N + 1) { if (j < x[i].first) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - x[i].first] + x[i].second - x[i].first); } } } N = dp[M][N]; REP(j, N + 1) dp[0][j] = N; REP(i, M) { REP(j, N + 1) { if (j < x[i].second) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - x[i].second] + x[i].first - x[i].second); } } } N = dp[M][N]; cout << N << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, N) for (int i = 0; i < N; ++i) #define MAX_I INT_MAX // 1e9 #define MIN_I INT_MIN //-1e9 #define MAX_UI UINT_MAX // 1e9 #define MAX_LL LLONG_MAX // 1e18 #define MIN_LL LLONG_MIN //-1e18 #define MAX_ULL ULLONG_MAX // 1e19 typedef long long ll; typedef pair<int, int> PII; typedef pair<char, char> PCC; typedef pair<ll, ll> PLL; typedef pair<char, int> PCI; typedef pair<int, char> PIC; typedef pair<ll, int> PLI; typedef pair<int, ll> PIL; typedef pair<ll, char> PLC; typedef pair<char, ll> PCL; bool comp(const PII &l, const PII &r) { return (double)l.second / l.first > (double)r.second / r.first; } int main(void) { ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(15); ll N; cin >> N; int N_MAX = 2.5e7; vector<PLL> x(3); cin >> x[0].first >> x[1].first >> x[2].first; cin >> x[0].second >> x[1].second >> x[2].second; int M = x.size(); // dp[i+1][j]:i番目まで見て,j個ある時の変換後の最大数 vector<vector<ll>> dp(M + 1, vector<ll>(N_MAX + 1)); REP(j, N + 1) dp[0][j] = N; REP(i, M) { REP(j, N + 1) { if (j < x[i].first) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - x[i].first] + x[i].second - x[i].first); } } } N = dp[M][N]; REP(j, N + 1) dp[0][j] = N; REP(i, M) { REP(j, N + 1) { if (j < x[i].second) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - x[i].second] + x[i].first - x[i].second); } } } N = dp[M][N]; cout << N << endl; return 0; }
replace
31
32
31
32
MLE
p03008
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <random> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; #define int ll typedef pair<int, int> pii; typedef pair<pii, pii> piii; typedef vector<piii> vpii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpi; typedef vector<short> vs; typedef vector<vs> vvs; typedef vector<vvs> vvvs; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef vector<ld> vld; typedef vector<vld> vvld; typedef vector<vvld> vvvld; typedef vector<string> vst; typedef vector<vst> vvst; typedef pair<ld, ld> pld; typedef complex<double> base; #define inmin(a, b) a = min(a, (b)) #define inmax(a, b) a = max(a, (b)) #define mp(a, b) make_pair(a, (b)) #define ALL(a) a.begin(), a.end() #define RALL(a) a.rbegin(), a.rend() #define sqr(x) ((x) * (x)) #define fori(i, n) for (int i = 0; i < int(n); ++i) #define SZ(a) ((int)((a).size())) #define triple(T) tuple<T, T, T> #define quad(T) tuple<T, T, T, T> #define watch(x) cout << (#x) << " = " << x << endl; #ifdef MAX_HOME #define cerr cout #else #define cerr \ if (false) \ cerr #endif 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...); } const double PI = 2 * acos(0.0); const string DIGITS = "0123456789"; const string ALPH = "abcdefghijklmnopqrstuvwxyz"; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count()); istream &operator>>(istream &in, pii &a) { in >> a.first >> a.second; return in; } ostream &operator<<(ostream &out, pii &a) { out << a.first << ' ' << a.second; return out; } template <class T0, class T1> inline ostream &operator<<(ostream &out, pair<T0, T1> &a) { return out << "{" << a.first << ", " << a.second << "}"; } template <class T0, class T1, class T2> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2> &a) { return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << "}"; } template <class T0, class T1, class T2, class T3> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2, T3> &a) { return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ", " << get<3>(a) << "}"; } template <class T> inline ostream &operator<<(ostream &out, vector<T> &a) { out << "["; fori(i, a.size()) out << a[i] << vector<string>{", ", "] "}[i + 1 == a.size()]; return out; } void smain(); signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef ART_HOME freopen("input.txt", "r", stdin); clock_t start = clock(); #endif cout << setprecision(0) << fixed; smain(); #ifdef ART_HOME cout << "\n\n\n\nTOTAL EXECUTION TIME: " << float(clock() - start) / CLOCKS_PER_SEC << endl; #endif } const int N = 1 << 17, psze = 20, oo = 1e9 + 100, B = 100, M = 1e9 + 6; const ld pi = 2 * atan2(1, 0), EPS = 1e-9; int stupid(vi a, vi b, int n) { int ans = 0; for (int i = 0; i * a[0] <= n; ++i) for (int j = 0; i * a[0] + j * a[1] <= n; ++j) { int v = (n - (i * a[0] + j * a[1])); ans = max(ans, b[0] * i + j * b[1] + (v / a[2]) * b[2] + v % a[2]); } return ans; } int stupid2(vi a, vi b, int n) { int ans = 0; for (int i = 0; i * a[0] <= n; ++i) for (int j = 0; i * a[0] + j * a[1] <= n; ++j) { int v = (n - (i * a[0] + j * a[1])); ans = max(ans, b[0] * i + j * b[1] + v); } return ans; } int stupid3(vi a, vi b, int n) { int ans = 0; for (int i = 0; i * a[0] <= n; ++i) for (int j = 0; i * a[0] + j * a[1] <= n; ++j) { int v = (n - (i * a[0] + j * a[1])); int cnt = b[0] * i + j * b[1] + v; ans = max(ans, (cnt / b[2]) * a[2] + cnt % b[2]); } return ans; } void smain() { int n; cin >> n; int m = 3; vi a(m), b(m), c(m); for (int i = 0; i < m; ++i) cin >> a[i]; for (int i = 0; i < m; ++i) cin >> b[i]; if (a[0] == b[0]) a[0] = 3e7, b[0] = 3e7 + 1; if (a[1] == b[1]) a[1] = 3e7, b[1] = 3e7 + 1; if (a[2] == b[2]) a[2] = 3e7, b[2] = 3e7 + 1; if (a[0] > b[0] && a[1] > b[1] && a[2] > b[2]) cout << stupid(b, a, n); else if (a[0] < b[0] && a[1] < b[1] && a[2] < b[2]) cout << stupid(a, b, n); else if (a[0] > b[0] && a[1] < b[1] && a[2] < b[2]) { swap(a[0], a[2]); swap(b[0], b[2]); cout << stupid3(a, b, n); } else if (a[0] < b[0] && a[1] > b[1] && a[2] < b[2]) { swap(a[1], a[2]); swap(b[1], b[2]); cout << stupid3(a, b, n); } else if (a[0] < b[0] && a[1] < b[1] && a[2] > b[2]) { cout << stupid3(a, b, n); } else if (a[0] > b[0] && a[1] > b[1] && a[2] < b[2]) { int v = (n / a[2]) * b[2] + n % a[2]; cout << stupid2(b, a, v); } else if (a[0] > b[0] && a[1] < b[1] && a[2] > b[2]) { int v = (n / a[1]) * b[1] + n % a[1]; swap(a[1], a[2]); swap(b[1], b[2]); cout << stupid2(b, a, v); } else if (a[0] < b[0] && a[1] > b[1] && a[2] > b[2]) { int v = (n / a[0]) * b[0] + n % a[0]; swap(a[0], a[2]); swap(b[0], b[2]); cout << stupid2(b, a, v); } }
#include <bits/stdc++.h> #include <random> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; #define int ll typedef pair<int, int> pii; typedef pair<pii, pii> piii; typedef vector<piii> vpii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpi; typedef vector<short> vs; typedef vector<vs> vvs; typedef vector<vvs> vvvs; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef vector<ld> vld; typedef vector<vld> vvld; typedef vector<vvld> vvvld; typedef vector<string> vst; typedef vector<vst> vvst; typedef pair<ld, ld> pld; typedef complex<double> base; #define inmin(a, b) a = min(a, (b)) #define inmax(a, b) a = max(a, (b)) #define mp(a, b) make_pair(a, (b)) #define ALL(a) a.begin(), a.end() #define RALL(a) a.rbegin(), a.rend() #define sqr(x) ((x) * (x)) #define fori(i, n) for (int i = 0; i < int(n); ++i) #define SZ(a) ((int)((a).size())) #define triple(T) tuple<T, T, T> #define quad(T) tuple<T, T, T, T> #define watch(x) cout << (#x) << " = " << x << endl; #ifdef MAX_HOME #define cerr cout #else #define cerr \ if (false) \ cerr #endif 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...); } const double PI = 2 * acos(0.0); const string DIGITS = "0123456789"; const string ALPH = "abcdefghijklmnopqrstuvwxyz"; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count()); istream &operator>>(istream &in, pii &a) { in >> a.first >> a.second; return in; } ostream &operator<<(ostream &out, pii &a) { out << a.first << ' ' << a.second; return out; } template <class T0, class T1> inline ostream &operator<<(ostream &out, pair<T0, T1> &a) { return out << "{" << a.first << ", " << a.second << "}"; } template <class T0, class T1, class T2> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2> &a) { return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << "}"; } template <class T0, class T1, class T2, class T3> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2, T3> &a) { return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ", " << get<3>(a) << "}"; } template <class T> inline ostream &operator<<(ostream &out, vector<T> &a) { out << "["; fori(i, a.size()) out << a[i] << vector<string>{", ", "] "}[i + 1 == a.size()]; return out; } void smain(); signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef ART_HOME freopen("input.txt", "r", stdin); clock_t start = clock(); #endif cout << setprecision(0) << fixed; smain(); #ifdef ART_HOME cout << "\n\n\n\nTOTAL EXECUTION TIME: " << float(clock() - start) / CLOCKS_PER_SEC << endl; #endif } const int N = 1 << 17, psze = 20, oo = 1e9 + 100, B = 100, M = 1e9 + 6; const ld pi = 2 * atan2(1, 0), EPS = 1e-9; int stupid(vi a, vi b, int n) { int ans = 0; for (int i = 0; i * a[0] <= n; ++i) for (int j = 0; i * a[0] + j * a[1] <= n; ++j) { int v = (n - (i * a[0] + j * a[1])); ans = max(ans, b[0] * i + j * b[1] + (v / a[2]) * b[2] + v % a[2]); } return ans; } int stupid2(vi a, vi b, int n) { int ans = 0; for (int i = 0; i * a[0] <= n; ++i) { int v = (n - (i * a[0])); ans = max(ans, b[0] * i + (v / a[1]) * b[1] + v % a[1]); } return ans; } int stupid3(vi a, vi b, int n) { int ans = 0; for (int i = 0; i * a[0] <= n; ++i) for (int j = 0; i * a[0] + j * a[1] <= n; ++j) { int v = (n - (i * a[0] + j * a[1])); int cnt = b[0] * i + j * b[1] + v; ans = max(ans, (cnt / b[2]) * a[2] + cnt % b[2]); } return ans; } void smain() { int n; cin >> n; int m = 3; vi a(m), b(m), c(m); for (int i = 0; i < m; ++i) cin >> a[i]; for (int i = 0; i < m; ++i) cin >> b[i]; if (a[0] == b[0]) a[0] = 3e7, b[0] = 3e7 + 1; if (a[1] == b[1]) a[1] = 3e7, b[1] = 3e7 + 1; if (a[2] == b[2]) a[2] = 3e7, b[2] = 3e7 + 1; if (a[0] > b[0] && a[1] > b[1] && a[2] > b[2]) cout << stupid(b, a, n); else if (a[0] < b[0] && a[1] < b[1] && a[2] < b[2]) cout << stupid(a, b, n); else if (a[0] > b[0] && a[1] < b[1] && a[2] < b[2]) { swap(a[0], a[2]); swap(b[0], b[2]); cout << stupid3(a, b, n); } else if (a[0] < b[0] && a[1] > b[1] && a[2] < b[2]) { swap(a[1], a[2]); swap(b[1], b[2]); cout << stupid3(a, b, n); } else if (a[0] < b[0] && a[1] < b[1] && a[2] > b[2]) { cout << stupid3(a, b, n); } else if (a[0] > b[0] && a[1] > b[1] && a[2] < b[2]) { int v = (n / a[2]) * b[2] + n % a[2]; cout << stupid2(b, a, v); } else if (a[0] > b[0] && a[1] < b[1] && a[2] > b[2]) { int v = (n / a[1]) * b[1] + n % a[1]; swap(a[1], a[2]); swap(b[1], b[2]); cout << stupid2(b, a, v); } else if (a[0] < b[0] && a[1] > b[1] && a[2] > b[2]) { int v = (n / a[0]) * b[0] + n % a[0]; swap(a[0], a[2]); swap(b[0], b[2]); cout << stupid2(b, a, v); } }
replace
137
142
137
141
TLE
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef long double ld; const ll MOD = 1e9 + 7; const ll N = 2e5 + 5; const ld pi = 3.14159265359; #define REP(i, n) for (ll i = 0; i < n; i++) #define REP1(i, n) for (ll i = 1; i <= n; i++) #define pb push_back #define mp make_pair #define X first #define Y second #define setp setprecision #define lwb lower_bound #define SZ(a) (ll) a.size() ll n, a[3], b[3], dp[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; REP(i, 3) cin >> a[i]; REP(i, 3) cin >> b[i]; dp[0] = 0; REP1(i, n) { dp[i] = i; REP(j, 3) { if (i >= a[j]) dp[i] = max(dp[i], dp[i - a[j]] + b[j]); } } n = dp[n]; REP1(i, n) { dp[i] = i; REP(j, 3) { if (i >= b[j]) dp[i] = max(dp[i], dp[i - b[j]] + a[j]); } } cout << dp[n] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef long double ld; const ll MOD = 1e9 + 7; const ll N = 5000 * 5000 + 5; const ld pi = 3.14159265359; #define REP(i, n) for (ll i = 0; i < n; i++) #define REP1(i, n) for (ll i = 1; i <= n; i++) #define pb push_back #define mp make_pair #define X first #define Y second #define setp setprecision #define lwb lower_bound #define SZ(a) (ll) a.size() ll n, a[3], b[3], dp[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; REP(i, 3) cin >> a[i]; REP(i, 3) cin >> b[i]; dp[0] = 0; REP1(i, n) { dp[i] = i; REP(j, 3) { if (i >= a[j]) dp[i] = max(dp[i], dp[i - a[j]] + b[j]); } } n = dp[n]; REP1(i, n) { dp[i] = i; REP(j, 3) { if (i >= b[j]) dp[i] = max(dp[i], dp[i - b[j]] + a[j]); } } cout << dp[n] << "\n"; return 0; }
replace
6
7
6
7
0
p03008
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long LL; typedef pair<int, int> pii; typedef pair<LL, LL> pll; const int MAXN = (int)1e7; int ex[2][87]; LL dp[MAXN]; int main() { int N; scanf("%d", &N); for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { scanf("%d", &ex[i][j]); } } for (int i = 0; i <= N; i++) { for (int j = 0; j < 3; j++) { int sub = ex[0][j]; int add = ex[1][j]; dp[i + sub] = max(dp[i + sub], dp[i] + add); } } LL nN = 0; for (int i = 0; i <= N; i++) { int tmp = N - i; nN = max(nN, tmp + dp[i]); } memset(dp, 0, sizeof(dp)); for (int i = 0; i <= nN; i++) { for (int j = 0; j < 3; j++) { int sub = ex[1][j]; int add = ex[0][j]; dp[i + sub] = max(dp[i + sub], dp[i] + add); } } LL ans = 0; for (int i = 0; i <= nN; i++) { int tmp = nN - i; ans = max(ans, tmp + dp[i]); } printf("%lld\n", ans); return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long LL; typedef pair<int, int> pii; typedef pair<LL, LL> pll; const int MAXN = (int)3e7; int ex[2][87]; LL dp[MAXN]; int main() { int N; scanf("%d", &N); for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { scanf("%d", &ex[i][j]); } } for (int i = 0; i <= N; i++) { for (int j = 0; j < 3; j++) { int sub = ex[0][j]; int add = ex[1][j]; dp[i + sub] = max(dp[i + sub], dp[i] + add); } } LL nN = 0; for (int i = 0; i <= N; i++) { int tmp = N - i; nN = max(nN, tmp + dp[i]); } memset(dp, 0, sizeof(dp)); for (int i = 0; i <= nN; i++) { for (int j = 0; j < 3; j++) { int sub = ex[1][j]; int add = ex[0][j]; dp[i + sub] = max(dp[i + sub], dp[i] + add); } } LL ans = 0; for (int i = 0; i <= nN; i++) { int tmp = nN - i; ans = max(ans, tmp + dp[i]); } printf("%lld\n", ans); return 0; }
replace
18
19
18
19
0
p03008
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> #define ll long long #define pb push_back #define sz(x) (int)(x).size() #define mp make_pair #define f first #define s second #define all(x) x.begin(), x.end() #define D(x) cerr << #x << " is " << (x) << "\n"; using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; /// find_by_order(),order_of_key() template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { os << '{' << a.f << ", " << a.s << '}'; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &a) { os << '{'; for (int i = 0; i < sz(a); i++) { if (i > 0 && i < sz(a)) os << ", "; os << a[i]; } os << '}'; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &a) { os << '{'; int i = 0; for (auto p : a) { if (i > 0 && i < sz(a)) os << ", "; os << p; i++; } os << '}'; return os; } template <class T> ostream &operator<<(ostream &os, const multiset<T> &a) { os << '{'; int i = 0; for (auto p : a) { if (i > 0 && i < sz(a)) os << ", "; os << p; i++; } os << '}'; return os; } template <class T1, class T2> ostream &operator<<(ostream &os, const map<T1, T2> &a) { os << '{'; int i = 0; for (auto p : a) { if (i > 0 && i < sz(a)) os << ", "; os << p; i++; } os << '}'; return os; } vector<pair<int, int>> AB, BA; int dp1[3][5001]; vector<vector<ll>> dp2; int calc1(int tr, int money) { if (tr == AB.size()) return money; if (dp1[tr][money] != -1) return dp1[tr][money]; int m = money; int profit = 0; dp1[tr][money] = calc1(tr + 1, m); while (m >= AB[tr].s) { m -= AB[tr].s; profit += AB[tr].f; dp1[tr][money] = max(dp1[tr][money], profit + calc1(tr + 1, m)); } return dp1[tr][money]; } ll calc2(int tr, int money) { if (tr == BA.size()) return money; if (dp2[tr][money] != -1) return dp2[tr][money]; int m = money; ll profit = 0; dp2[tr][money] = calc2(tr + 1, m); while (m >= BA[tr].s) { m -= BA[tr].s; profit += BA[tr].f; dp2[tr][money] = max(dp2[tr][money], profit + calc2(tr + 1, m)); } return dp2[tr][money]; } int main() { memset(dp1, -1, sizeof(dp1)); int n; scanf("%i", &n); int ga, sa, ba, gb, sb, bb; scanf("%i %i %i %i %i %i", &ga, &sa, &ba, &gb, &sb, &bb); if (ga > gb) BA.pb({ga, gb}); if (ga < gb) AB.pb({gb, ga}); if (sa > sb) BA.pb({sa, sb}); if (sa < sb) AB.pb({sb, sa}); if (ba > bb) BA.pb({ba, bb}); if (ba < bb) AB.pb({bb, ba}); int novo = calc1(0, n); dp2.resize((int)BA.size(), vector<ll>(novo + 1, -1)); printf("%lld\n", calc2(0, novo)); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> #define ll long long #define pb push_back #define sz(x) (int)(x).size() #define mp make_pair #define f first #define s second #define all(x) x.begin(), x.end() #define D(x) cerr << #x << " is " << (x) << "\n"; using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; /// find_by_order(),order_of_key() template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { os << '{' << a.f << ", " << a.s << '}'; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &a) { os << '{'; for (int i = 0; i < sz(a); i++) { if (i > 0 && i < sz(a)) os << ", "; os << a[i]; } os << '}'; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &a) { os << '{'; int i = 0; for (auto p : a) { if (i > 0 && i < sz(a)) os << ", "; os << p; i++; } os << '}'; return os; } template <class T> ostream &operator<<(ostream &os, const multiset<T> &a) { os << '{'; int i = 0; for (auto p : a) { if (i > 0 && i < sz(a)) os << ", "; os << p; i++; } os << '}'; return os; } template <class T1, class T2> ostream &operator<<(ostream &os, const map<T1, T2> &a) { os << '{'; int i = 0; for (auto p : a) { if (i > 0 && i < sz(a)) os << ", "; os << p; i++; } os << '}'; return os; } vector<pair<int, int>> AB, BA; int dp1[3][5001]; vector<vector<ll>> dp2; int calc1(int tr, int money) { if (tr == AB.size()) return money; if (dp1[tr][money] != -1) return dp1[tr][money]; int m = money; int profit = 0; dp1[tr][money] = calc1(tr + 1, m); while (m >= AB[tr].s) { m -= AB[tr].s; profit += AB[tr].f; dp1[tr][money] = max(dp1[tr][money], profit + calc1(tr + 1, m)); } return dp1[tr][money]; } ll calc2(int tr, int money) { if (tr == BA.size()) return money; if (dp2[tr][money] != -1) return dp2[tr][money]; int m = money; ll profit = 0; if (tr == BA.size() - 1) { int puta = m / BA[tr].s; dp2[tr][money] = (ll)puta * BA[tr].f + m - BA[tr].s * puta; return dp2[tr][money]; } dp2[tr][money] = calc2(tr + 1, m); while (m >= BA[tr].s) { m -= BA[tr].s; profit += BA[tr].f; dp2[tr][money] = max(dp2[tr][money], profit + calc2(tr + 1, m)); } return dp2[tr][money]; } int main() { memset(dp1, -1, sizeof(dp1)); int n; scanf("%i", &n); int ga, sa, ba, gb, sb, bb; scanf("%i %i %i %i %i %i", &ga, &sa, &ba, &gb, &sb, &bb); if (ga > gb) BA.pb({ga, gb}); if (ga < gb) AB.pb({gb, ga}); if (sa > sb) BA.pb({sa, sb}); if (sa < sb) AB.pb({sb, sa}); if (ba > bb) BA.pb({ba, bb}); if (ba < bb) AB.pb({bb, ba}); int novo = calc1(0, n); dp2.resize((int)BA.size(), vector<ll>(novo + 1, -1)); printf("%lld\n", calc2(0, novo)); return 0; }
insert
101
101
101
106
TLE
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x, to) for (x = 0; x < (to); x++) #define FORR(x, arr) for (auto &x : arr) #define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++) #define ALL(a) (a.begin()), (a.end()) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) //------------------------------------------------------- int N; ll G[2], A[2], B[2]; void solve() { int i, j, k, l, r, x, y; string s; cin >> N; FOR(i, 2) cin >> G[i] >> A[i] >> B[i]; if (G[0] >= G[1] && A[0] >= A[1] && B[0] >= B[1]) { swap(G[0], G[1]); swap(A[0], A[1]); swap(B[0], B[1]); } ll ret = N; if (G[0] <= G[1] && A[0] <= A[1] && B[0] <= B[1]) { for (x = 0; x <= N; x += G[0]) { for (y = 0; x + y <= N; y += A[0]) { int z = (N - x - y) / B[0] * B[0]; ll cur = (N - x - y - z) + x / G[0] * G[1] + y / A[0] * A[1] + z / B[0] * B[1]; ret = max(ret, cur); } } } else { if (G[0] >= G[1] && A[0] < A[1]) swap(G[0], A[0]), swap(G[1], A[1]); if (A[0] >= A[1] && B[0] < B[1]) swap(B[0], A[0]), swap(B[1], A[1]); if (G[0] >= G[1] && A[0] < A[1]) swap(G[0], A[0]), swap(G[1], A[1]); if (A[0] < A[1]) { for (x = 0; x <= N; x += G[0]) { for (y = 0; x + y <= N; y += A[0]) { ll cur = (N - x - y) + x / G[0] * G[1] + y / A[0] * A[1]; ll v = cur / B[1]; cur = cur % B[1] + v * B[0]; ret = max(ret, cur); } } } else { ll v = N / G[0]; ll cur = N % v + v * G[1]; for (x = 0; x <= cur; x += A[1]) { y = (cur - x) / B[1] * B[1]; ll ncur = (cur - x - y) + x / A[1] * A[0] + y / B[1] * B[0]; ret = max(ret, ncur); } } } cout << ret << endl; } int main(int argc, char **argv) { string s; int i; if (argc == 1) ios::sync_with_stdio(false), cin.tie(0); FOR(i, argc - 1) s += argv[i + 1], s += '\n'; FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x, to) for (x = 0; x < (to); x++) #define FORR(x, arr) for (auto &x : arr) #define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++) #define ALL(a) (a.begin()), (a.end()) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) //------------------------------------------------------- int N; ll G[2], A[2], B[2]; void solve() { int i, j, k, l, r, x, y; string s; cin >> N; FOR(i, 2) cin >> G[i] >> A[i] >> B[i]; if (G[0] >= G[1] && A[0] >= A[1] && B[0] >= B[1]) { swap(G[0], G[1]); swap(A[0], A[1]); swap(B[0], B[1]); } ll ret = N; if (G[0] <= G[1] && A[0] <= A[1] && B[0] <= B[1]) { for (x = 0; x <= N; x += G[0]) { for (y = 0; x + y <= N; y += A[0]) { int z = (N - x - y) / B[0] * B[0]; ll cur = (N - x - y - z) + x / G[0] * G[1] + y / A[0] * A[1] + z / B[0] * B[1]; ret = max(ret, cur); } } } else { if (G[0] >= G[1] && A[0] < A[1]) swap(G[0], A[0]), swap(G[1], A[1]); if (A[0] >= A[1] && B[0] < B[1]) swap(B[0], A[0]), swap(B[1], A[1]); if (G[0] >= G[1] && A[0] < A[1]) swap(G[0], A[0]), swap(G[1], A[1]); if (A[0] < A[1]) { for (x = 0; x <= N; x += G[0]) { for (y = 0; x + y <= N; y += A[0]) { ll cur = (N - x - y) + x / G[0] * G[1] + y / A[0] * A[1]; ll v = cur / B[1]; cur = cur % B[1] + v * B[0]; ret = max(ret, cur); } } } else { ll v = N / G[0]; ll cur = N % G[0] + v * G[1]; for (x = 0; x <= cur; x += A[1]) { y = (cur - x) / B[1] * B[1]; ll ncur = (cur - x - y) + x / A[1] * A[0] + y / B[1] * B[0]; ret = max(ret, ncur); } } } cout << ret << endl; } int main(int argc, char **argv) { string s; int i; if (argc == 1) ios::sync_with_stdio(false), cin.tie(0); FOR(i, argc - 1) s += argv[i + 1], s += '\n'; FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin); cout.tie(0); solve(); return 0; }
replace
59
60
59
60
0
p03008
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define int long long int solve(int N, vector<int> a, vector<int> b) { int ans = N; for (int cf = 0; cf * a[0] <= N; cf++) { if (cf > 0 && a[0] > b[0]) break; for (int cs = 0; cf * a[0] + cs * a[1] <= N; cs++) { if (cs > 0 && a[1] > b[1]) break; int ct = (N - cf * a[0] - cs * a[1]) / a[2]; ans = max(ans, cf * b[0] + cs * b[1] + ct * b[2] + (N - cf * a[0] - cs * a[1] - ct * a[2])); ct = 0; ans = max(ans, cf * b[0] + cs * b[1] + ct * b[2] + (N - cf * a[0] - cs * a[1] - ct * a[2])); } } return ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector<int> A(3); vector<int> B(3); for (auto &t : A) { cin >> t; } for (auto &t : B) { cin >> t; } N = solve(N, A, B); N = solve(N, B, A); cout << N << '\n'; }
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define int long long int solve(int N, vector<int> a, vector<int> b) { int ans = N; vector<int> ind(3); iota(ind.begin(), ind.end(), 0); sort(ind.begin(), ind.end(), [&](int i, int j) { return b[i] - a[i] < b[j] - a[j]; }); { vector<int> na(3); vector<int> nb(3); for (int i = 0; i < 3; i++) { na[i] = a[ind[i]]; nb[i] = b[ind[i]]; } a = na; b = nb; } for (int cf = 0; cf * a[0] <= N; cf++) { if (cf > 0 && a[0] > b[0]) break; for (int cs = 0; cf * a[0] + cs * a[1] <= N; cs++) { if (cs > 0 && a[1] > b[1]) break; int ct = (N - cf * a[0] - cs * a[1]) / a[2]; ans = max(ans, cf * b[0] + cs * b[1] + ct * b[2] + (N - cf * a[0] - cs * a[1] - ct * a[2])); ct = 0; ans = max(ans, cf * b[0] + cs * b[1] + ct * b[2] + (N - cf * a[0] - cs * a[1] - ct * a[2])); } } return ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector<int> A(3); vector<int> B(3); for (auto &t : A) { cin >> t; } for (auto &t : B) { cin >> t; } N = solve(N, A, B); N = solve(N, B, A); cout << N << '\n'; }
insert
29
29
29
43
TLE
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 1e7 + 5; int n, a, b, c, d, e, f, ma, dp[N]; signed main() { cin >> n; cin >> a >> b >> c >> d >> e >> f; memset(dp, -63, sizeof(dp)); dp[0] = 0; for (int i = a; i <= n; i++) dp[i] = max(dp[i], dp[i - a] + d); for (int i = b; i <= n; i++) dp[i] = max(dp[i], dp[i - b] + e); for (int i = c; i <= n; i++) dp[i] = max(dp[i], dp[i - c] + f); ma = n; for (int i = 0; i <= n; i++) ma = max(ma, dp[i] + n - i); for (int i = d; i <= n; i++) dp[i] = max(dp[i], dp[i - d] + a); for (int i = e; i <= n; i++) dp[i] = max(dp[i], dp[i - e] + b); for (int i = f; i <= n; i++) dp[i] = max(dp[i], dp[i - f] + c); int ans = n; for (int i = 0; i <= n; i++) ans = max(ans, dp[i] + n - i); memset(dp, -63, sizeof(dp)); dp[0] = 0; n = ma; for (int i = d; i <= n; i++) dp[i] = max(dp[i], dp[i - d] + a); for (int i = e; i <= n; i++) dp[i] = max(dp[i], dp[i - e] + b); for (int i = f; i <= n; i++) dp[i] = max(dp[i], dp[i - f] + c); ans = max(ans, n); for (int i = 0; i <= n; i++) ans = max(ans, dp[i] + n - i); cout << ans; }
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 5000 * 5000 + 5; int n, a, b, c, d, e, f, ma, dp[N]; signed main() { cin >> n; cin >> a >> b >> c >> d >> e >> f; memset(dp, -63, sizeof(dp)); dp[0] = 0; for (int i = a; i <= n; i++) dp[i] = max(dp[i], dp[i - a] + d); for (int i = b; i <= n; i++) dp[i] = max(dp[i], dp[i - b] + e); for (int i = c; i <= n; i++) dp[i] = max(dp[i], dp[i - c] + f); ma = n; for (int i = 0; i <= n; i++) ma = max(ma, dp[i] + n - i); for (int i = d; i <= n; i++) dp[i] = max(dp[i], dp[i - d] + a); for (int i = e; i <= n; i++) dp[i] = max(dp[i], dp[i - e] + b); for (int i = f; i <= n; i++) dp[i] = max(dp[i], dp[i - f] + c); int ans = n; for (int i = 0; i <= n; i++) ans = max(ans, dp[i] + n - i); memset(dp, -63, sizeof(dp)); dp[0] = 0; n = ma; for (int i = d; i <= n; i++) dp[i] = max(dp[i], dp[i - d] + a); for (int i = e; i <= n; i++) dp[i] = max(dp[i], dp[i - e] + b); for (int i = f; i <= n; i++) dp[i] = max(dp[i], dp[i - f] + c); ans = max(ans, n); for (int i = 0; i <= n; i++) ans = max(ans, dp[i] + n - i); cout << ans; }
replace
3
4
3
4
0
p03008
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pi; typedef pair<pi, pi> pp; typedef pair<ll, ll> pl; const double EPS = 1e-9; const ll MOD = 1000000007; const int inf = 1 << 30; const ll linf = 1LL << 60; const double PI = 3.14159265358979323846; int n; int ga, sa, ba; int gb, sb, bb; ll dp[10000000]; int main() { cin >> n; cin >> ga >> sa >> ba; cin >> gb >> sb >> bb; dp[0] = 0; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; if (i - ga >= 0) dp[i] = max(dp[i], dp[i - ga] + gb - ga); if (i - sa >= 0) dp[i] = max(dp[i], dp[i - sa] + sb - sa); if (i - ba >= 0) dp[i] = max(dp[i], dp[i - ba] + bb - ba); } n += dp[n]; dp[0] = 0; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; if (i - gb >= 0) dp[i] = max(dp[i], dp[i - gb] + ga - gb); if (i - sb >= 0) dp[i] = max(dp[i], dp[i - sb] + sa - sb); if (i - bb >= 0) dp[i] = max(dp[i], dp[i - bb] + ba - bb); } cout << n + dp[n] << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pi; typedef pair<pi, pi> pp; typedef pair<ll, ll> pl; const double EPS = 1e-9; const ll MOD = 1000000007; const int inf = 1 << 30; const ll linf = 1LL << 60; const double PI = 3.14159265358979323846; int n; int ga, sa, ba; int gb, sb, bb; ll dp[30000000]; int main() { cin >> n; cin >> ga >> sa >> ba; cin >> gb >> sb >> bb; dp[0] = 0; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; if (i - ga >= 0) dp[i] = max(dp[i], dp[i - ga] + gb - ga); if (i - sa >= 0) dp[i] = max(dp[i], dp[i - sa] + sb - sa); if (i - ba >= 0) dp[i] = max(dp[i], dp[i - ba] + bb - ba); } n += dp[n]; dp[0] = 0; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; if (i - gb >= 0) dp[i] = max(dp[i], dp[i - gb] + ga - gb); if (i - sb >= 0) dp[i] = max(dp[i], dp[i - sb] + sa - sb); if (i - bb >= 0) dp[i] = max(dp[i], dp[i - bb] + ba - bb); } cout << n + dp[n] << endl; }
replace
17
18
17
18
0
p03008
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; inline void prep() { cin.tie(0); cin.sync_with_stdio(0); }; const double pi = acos(-1.); // printf(%.12lf\n,mid); // __builtin_popcount(int) count 1's in binary // get decimal part of number /* double intpart; double thiss = modf((double)(n*m)/(double)k, &intpart); */ // round = (int)(num+.5) long long mod = 1000000007; // 32 mil = ~1 sec long long mymod(long long num) { if (num < 0 && !(num % mod == 0)) { if (mod == 1) { return 0; } return mod - abs(num) % mod; } else { return num % mod; } } /*string sentence = And I feel fine...; istringstream iss(sentence); */ double eps = .000000001; int a[3]; int b[3]; int n; int main() { prep(); cin >> n; for (int i = 0; i < 3; i++) { cin >> a[i]; } for (int i = 0; i < 3; i++) { cin >> b[i]; } bool which[3] = {false}; int stata = 0; for (int i = 0; i < 3; i++) { if (b[i] / a[i] >= 1) { stata++; which[i] = true; } } long long maxest = n; if (stata == 0 || stata == 3) { // n choose 2 for other side bool sidea = false; if (stata == 3) { sidea = true; } for (int i = 0; i <= n; i++) { for (int j = i; j <= n; j++) { int one = i; int two = j - one; int three = n - two - one; int places[3] = {one, two, three}; long long sum = 0; if (sidea) { for (int k = 0; k < 3; k++) { sum += (places[k] / a[k]) * b[k]; sum += places[k] % a[k]; } } else { for (int k = 0; k < 3; k++) { sum += (places[k] / b[k]) * a[k]; sum += places[k] % b[k]; } } maxest = max(maxest, sum); } } } else if (stata == 1) { // put all into that one, (new acorn amount) choose 1 for other side long long neww = 0; for (int i = 0; i < 3; i++) { if (which[i]) { neww += (n / a[i]) * b[i]; neww += n % a[i]; } } for (int j = 0; j <= neww; j++) { int one = j; int two = neww - one; long long res = 0; int ind = 0; long long places[2] = {one, two}; for (int k = 0; k < 3; k++) { if (!which[k]) { res += (places[ind] / b[k]) * a[j]; res += places[ind] % b[k]; ind++; } } maxest = max(maxest, res); } } else if (stata == 2) { // n choose 1 for this side, put all into other station's long long curmax = 0; for (int j = 0; j <= n; j++) { int one = j; int two = n - one; long long res = 0; int ind = 0; long long places[2] = {one, two}; for (int k = 0; k < 3; k++) { if (which[k]) { res += (places[ind] / a[k]) * b[k]; res += places[ind] % a[k]; ind++; } } curmax = max(curmax, res); } for (int i = 0; i < 3; i++) { if (!which[i]) { curmax = curmax % b[i] + (curmax / b[i]) * a[i]; break; } } maxest = max(maxest, curmax); } cout << maxest; return 0; }
#include <bits/stdc++.h> using namespace std; inline void prep() { cin.tie(0); cin.sync_with_stdio(0); }; const double pi = acos(-1.); // printf(%.12lf\n,mid); // __builtin_popcount(int) count 1's in binary // get decimal part of number /* double intpart; double thiss = modf((double)(n*m)/(double)k, &intpart); */ // round = (int)(num+.5) long long mod = 1000000007; // 32 mil = ~1 sec long long mymod(long long num) { if (num < 0 && !(num % mod == 0)) { if (mod == 1) { return 0; } return mod - abs(num) % mod; } else { return num % mod; } } /*string sentence = And I feel fine...; istringstream iss(sentence); */ double eps = .000000001; int a[3]; int b[3]; int n; int main() { prep(); cin >> n; for (int i = 0; i < 3; i++) { cin >> a[i]; } for (int i = 0; i < 3; i++) { cin >> b[i]; } bool which[3] = {false}; int stata = 0; for (int i = 0; i < 3; i++) { if (b[i] / a[i] >= 1) { stata++; which[i] = true; } } long long maxest = n; if (stata == 0 || stata == 3) { // n choose 2 for other side bool sidea = false; if (stata == 3) { sidea = true; } for (int i = 0; i <= n; i++) { for (int j = i; j <= n; j++) { int one = i; int two = j - one; int three = n - two - one; int places[3] = {one, two, three}; long long sum = 0; if (sidea) { for (int k = 0; k < 3; k++) { sum += (places[k] / a[k]) * b[k]; sum += places[k] % a[k]; } } else { for (int k = 0; k < 3; k++) { sum += (places[k] / b[k]) * a[k]; sum += places[k] % b[k]; } } maxest = max(maxest, sum); } } } else if (stata == 1) { // put all into that one, (new acorn amount) choose 1 for other side long long neww = 0; for (int i = 0; i < 3; i++) { if (which[i]) { neww += (n / a[i]) * b[i]; neww += n % a[i]; } } for (int j = 0; j <= neww; j++) { int one = j; int two = neww - one; long long res = 0; int ind = 0; long long places[2] = {one, two}; for (int k = 0; k < 3; k++) { if (!which[k]) { res += (places[ind] / b[k]) * a[k]; res += places[ind] % b[k]; ind++; } } maxest = max(maxest, res); } } else if (stata == 2) { // n choose 1 for this side, put all into other station's long long curmax = 0; for (int j = 0; j <= n; j++) { int one = j; int two = n - one; long long res = 0; int ind = 0; long long places[2] = {one, two}; for (int k = 0; k < 3; k++) { if (which[k]) { res += (places[ind] / a[k]) * b[k]; res += places[ind] % a[k]; ind++; } } curmax = max(curmax, res); } for (int i = 0; i < 3; i++) { if (!which[i]) { curmax = curmax % b[i] + (curmax / b[i]) * a[i]; break; } } maxest = max(maxest, curmax); } cout << maxest; return 0; }
replace
104
105
104
105
0
p03008
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long int64; #define MAX_N 200005 struct data { double ratio; int64 cost, profit; }; void DFS(int depth, vector<data> &vs, int64 value, int64 now, int64 &res) { if (depth == 3) { res = max(res, value + now); return; } int64 upper = now / vs[depth].cost; if (vs[depth].ratio <= 1) upper = 0LL; for (int64 i = upper; i >= 0; i--) { int64 remain = now - i * vs[depth].cost; DFS(depth + 1, vs, value + i * vs[depth].profit, remain, res); } } int64 solve(int64 N, int64 a, int64 b, int64 c, int64 A, int64 B, int64 C) { vector<data> vs; vs.push_back(data{1.0 * A / a, a, A}); vs.push_back(data{1.0 * B / b, b, B}); vs.push_back(data{1.0 * C / c, c, C}); sort(vs.begin(), vs.end(), [](auto &lhs, auto &rhs) { return lhs.ratio > rhs.ratio; }); int64 res = 0LL; DFS(0, vs, 0LL, N, res); return res; } int main() { // freopen("input.txt", "r", stdin); int64 N, gA, sA, bA, gB, sB, bB; scanf("%lld", &N); scanf("%lld %lld %lld", &gA, &sA, &bA); scanf("%lld %lld %lld", &gB, &sB, &bB); int64 M = solve(N, gA, sA, bA, gB, sB, bB); int64 res = solve(M, gB, sB, bB, gA, sA, bA); printf("%lld\n", res); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int64; #define MAX_N 200005 struct data { double ratio; int64 cost, profit; }; void DFS(int depth, vector<data> &vs, int64 value, int64 now, int64 &res) { if (depth == 3) { res = max(res, value + now); return; } if (value + now * max(1.0, vs[depth].ratio) < res) return; int64 upper = now / vs[depth].cost; if (vs[depth].ratio <= 1) upper = 0LL; for (int64 i = upper; i >= 0; i--) { int64 remain = now - i * vs[depth].cost; DFS(depth + 1, vs, value + i * vs[depth].profit, remain, res); } } int64 solve(int64 N, int64 a, int64 b, int64 c, int64 A, int64 B, int64 C) { vector<data> vs; vs.push_back(data{1.0 * A / a, a, A}); vs.push_back(data{1.0 * B / b, b, B}); vs.push_back(data{1.0 * C / c, c, C}); sort(vs.begin(), vs.end(), [](auto &lhs, auto &rhs) { return lhs.ratio > rhs.ratio; }); int64 res = 0LL; DFS(0, vs, 0LL, N, res); return res; } int main() { // freopen("input.txt", "r", stdin); int64 N, gA, sA, bA, gB, sB, bB; scanf("%lld", &N); scanf("%lld %lld %lld", &gA, &sA, &bA); scanf("%lld %lld %lld", &gB, &sB, &bB); int64 M = solve(N, gA, sA, bA, gB, sB, bB); int64 res = solve(M, gB, sB, bB, gA, sA, bA); printf("%lld\n", res); return 0; }
insert
17
17
17
20
TLE
p03008
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; // Template Headers {{{ using pii = pair<int, int>; using vpii = vector<pii>; using vi = vector<int>; using vvi = vector<vi>; using ll = long long; using vll = vector<long long>; template <class T> istream &operator>>(istream &, vector<T> &); template <class T> ostream &operator<<(ostream &, const vector<T> &); template <class T, class U> istream &operator>>(istream &, pair<T, U> &); template <class T, class U> ostream &operator<<(ostream &, const pair<T, U> &); constexpr int INF = 0x3f3f3f3f; constexpr ll BINF = 0x3f3f3f3f3f3f3f3fLL; // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // }}} #define int ll int solve() { int n; cin >> n; vi a(3), b(3); cin >> a >> b; vpii c(3); for (int i = 0; i < 3; i++) c[i] = {a[i], b[i]}; sort(c.begin(), c.end(), [&](auto l, auto r) { return l.first - l.second < r.first - r.second; }); if (c[0].first - c[0].second >= 0) { cerr << "case 1" << endl; // all buy B, sale A int ans = n; for (int x = 0; x <= n; x++) for (int y = 0; y <= n; y++) { int rem = n - c[0].second * x - c[1].second * y; if (rem < 0) break; int z = rem / c[2].second; rem -= c[2].second * z; ans = max(ans, rem + x * c[0].first + y * c[1].first + z * c[2].first); } cout << ans << endl; } else if (c[2].first - c[2].second <= 0) { cerr << "case 2" << endl; // all buy A, sale B int ans = n; for (int x = 0; x <= n; x++) for (int y = 0; y <= n; y++) { int rem = n - c[0].first * x - c[1].first * y; if (rem < 0) break; int z = rem / c[2].first; rem -= c[2].first * z; ans = max(ans, rem + x * c[0].second + y * c[1].second + z * c[2].second); } cout << ans << endl; } else if (c[1].first - c[1].second >= 0) { cerr << "case 3" << endl; // -ve, +ve, +ve // A<B, A>B, A>B // first buy A sale B // second, third buy B sale A int now = n; int canbuy = n / c[0].first; now -= canbuy * c[0].first; now += canbuy * c[0].second; int ans = now; for (int x = 0; x <= now; x++) { int rem = now - 1LL * x * c[1].second; if (rem < 0) break; int y = rem / c[2].second; rem -= y * c[2].second; ans = max(ans, rem + x * c[1].first + y * c[2].first); } cout << ans << endl; } else { cerr << "case 4" << endl; // -ve, -ve, +ve // A<B, A<B, B>A int ans = n; for (int x = 0; x <= n; x++) for (int y = 0; y <= n; y++) { int rem = n - x * c[0].first - y * c[1].first; if (rem < 0) break; int now = rem + x * c[0].second + y * c[1].second; // cerr << x << ' ' << y << ' ' << now; int z = now / c[2].second; now -= z * c[2].second; ans = max(ans, now + z * c[2].first); cerr << ' ' << ans << endl; } cout << ans << endl; } return 0; } // Template Main {{{ signed main() { ios::sync_with_stdio(0); // precompute(); // int t; cin >> t; for (int i=1;i<=t;i++) solve(); // cout << (solve() ? "YES" : "NO") << endl; return 0; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (auto it = v.begin(); it != v.end(); ++it) is >> *it; return is; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto it = v.begin(); it != v.end();) os << *it, os << " \n"[++it == v.end()]; return os; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { return is >> p.first >> p.second; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << p.first << ' ' << p.second; } // }}} // vim: fdm=marker
#include <bits/stdc++.h> using namespace std; // Template Headers {{{ using pii = pair<int, int>; using vpii = vector<pii>; using vi = vector<int>; using vvi = vector<vi>; using ll = long long; using vll = vector<long long>; template <class T> istream &operator>>(istream &, vector<T> &); template <class T> ostream &operator<<(ostream &, const vector<T> &); template <class T, class U> istream &operator>>(istream &, pair<T, U> &); template <class T, class U> ostream &operator<<(ostream &, const pair<T, U> &); constexpr int INF = 0x3f3f3f3f; constexpr ll BINF = 0x3f3f3f3f3f3f3f3fLL; // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // }}} #define int ll int solve() { int n; cin >> n; vi a(3), b(3); cin >> a >> b; vpii c(3); for (int i = 0; i < 3; i++) c[i] = {a[i], b[i]}; sort(c.begin(), c.end(), [&](auto l, auto r) { return l.first - l.second < r.first - r.second; }); if (c[0].first - c[0].second >= 0) { cerr << "case 1" << endl; // all buy B, sale A int ans = n; for (int x = 0; x <= n; x++) for (int y = 0; y <= n; y++) { int rem = n - c[0].second * x - c[1].second * y; if (rem < 0) break; int z = rem / c[2].second; rem -= c[2].second * z; ans = max(ans, rem + x * c[0].first + y * c[1].first + z * c[2].first); } cout << ans << endl; } else if (c[2].first - c[2].second <= 0) { cerr << "case 2" << endl; // all buy A, sale B int ans = n; for (int x = 0; x <= n; x++) for (int y = 0; y <= n; y++) { int rem = n - c[0].first * x - c[1].first * y; if (rem < 0) break; int z = rem / c[2].first; rem -= c[2].first * z; ans = max(ans, rem + x * c[0].second + y * c[1].second + z * c[2].second); } cout << ans << endl; } else if (c[1].first - c[1].second >= 0) { cerr << "case 3" << endl; // -ve, +ve, +ve // A<B, A>B, A>B // first buy A sale B // second, third buy B sale A int now = n; int canbuy = n / c[0].first; now -= canbuy * c[0].first; now += canbuy * c[0].second; int ans = now; for (int x = 0; x <= now; x++) { int rem = now - 1LL * x * c[1].second; if (rem < 0) break; int y = rem / c[2].second; rem -= y * c[2].second; ans = max(ans, rem + x * c[1].first + y * c[2].first); } cout << ans << endl; } else { cerr << "case 4" << endl; // -ve, -ve, +ve // A<B, A<B, B>A int ans = n; for (int x = 0; x <= n; x++) for (int y = 0; y <= n; y++) { int rem = n - x * c[0].first - y * c[1].first; if (rem < 0) break; int now = rem + x * c[0].second + y * c[1].second; // cerr << x << ' ' << y << ' ' << now; int z = now / c[2].second; now -= z * c[2].second; ans = max(ans, now + z * c[2].first); // cerr << ' ' << ans << endl; } cout << ans << endl; } return 0; } // Template Main {{{ signed main() { ios::sync_with_stdio(0); // precompute(); // int t; cin >> t; for (int i=1;i<=t;i++) solve(); // cout << (solve() ? "YES" : "NO") << endl; return 0; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (auto it = v.begin(); it != v.end(); ++it) is >> *it; return is; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto it = v.begin(); it != v.end();) os << *it, os << " \n"[++it == v.end()]; return os; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { return is >> p.first >> p.second; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << p.first << ' ' << p.second; } // }}} // vim: fdm=marker
replace
94
95
94
95
TLE
p03008
C++
Time Limit Exceeded
// #pragma GCC optimize("O3") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define trav(x, a) for (const auto &x : a) #define sz(x) (int)(x).size() #define mem(a, v) memset((a), (v), sizeof(a)) #define enl printf("\n") #define case(t) printf("Case #%d: ", (t)) #define ni(n) scanf("%d", &(n)) #define nl(n) scanf("%lld", &(n)) #define nai(a, n) \ for (int _i = 0; _i < (n); _i++) \ ni(a[_i]) #define nal(a, n) \ for (int _i = 0; _i < (n); _i++) \ nl(a[_i]) #define pri(n) printf("%d\n", (n)) #define prl(n) printf("%lld\n", (n)) #define pii pair<int, int> #define pll pair<long long, long long> #define vii vector<pii> #define vll vector<pll> #define vi vector<int> #define vl vector<long long> #define pb push_back #define mp make_pair #define st first #define nd second using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef cc_hash_table<int, int, hash<int>> ht; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset; const double pi = acos(-1); const int mod = 1e9 + 7; const int inf = 1e9 + 7; const int N = 1e6 + 5; const double eps = 1e-9; ll a[3][3], b[3], it[3]; int main() { int n; scanf("%d", &n); for (int i = 0; i < 2; i++) for (int j = 0; j < 3; j++) scanf("%lld", &a[i][j]); ll ans = n; int c = 0; for (int i = 0; i < 3; i++) c += a[0][i] < a[1][i]; for (int i = 0; i < 3; i++) it[i] = i; sort(it, it + 3, [](int x, int y) { return a[1][x] - a[0][x] > a[1][y] - a[0][y]; }); if (c == 3 || c == 0) { for (int i = 0; i < 3; i++) { b[i] = abs(a[0][i] - a[1][i]); a[0][i] = max(a[0][i], a[1][i]); a[1][i] = a[0][i] - b[i]; } for (int i = 0; i <= n; i += a[1][0]) for (int j = 0; j + i <= n; j += a[1][1]) ans = max(ans, i / a[1][0] * a[0][0] + j / a[1][1] * a[0][1] + ((n - i - j) / a[1][2]) * a[0][2] + ((n - i - j) % a[1][2])); } else if (c == 2) { for (int i = 0; i <= n; i += a[0][it[0]]) for (int j = 0; j + i <= n; j += a[0][it[1]]) { ll nx = i / a[0][it[0]] * a[1][it[0]] + j / a[0][it[1]] * a[1][it[1]] + (n - i - j); ans = max(ans, (nx / a[1][it[2]]) * a[0][it[2]] + (nx % a[1][it[2]])); } } else { for (int i = 0; i <= n; i += a[0][it[0]]) { ll nx = i / a[0][it[0]] * a[1][it[0]] + n - i; for (int j = 0; j <= nx; j += a[1][it[1]]) ans = max(ans, j / a[1][it[1]] * a[0][it[1]] + ((nx - j) / a[1][it[2]]) * a[0][it[2]] + ((nx - j) % a[1][it[2]])); } } printf("%lld\n", ans); return 0; }
// #pragma GCC optimize("O3") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define trav(x, a) for (const auto &x : a) #define sz(x) (int)(x).size() #define mem(a, v) memset((a), (v), sizeof(a)) #define enl printf("\n") #define case(t) printf("Case #%d: ", (t)) #define ni(n) scanf("%d", &(n)) #define nl(n) scanf("%lld", &(n)) #define nai(a, n) \ for (int _i = 0; _i < (n); _i++) \ ni(a[_i]) #define nal(a, n) \ for (int _i = 0; _i < (n); _i++) \ nl(a[_i]) #define pri(n) printf("%d\n", (n)) #define prl(n) printf("%lld\n", (n)) #define pii pair<int, int> #define pll pair<long long, long long> #define vii vector<pii> #define vll vector<pll> #define vi vector<int> #define vl vector<long long> #define pb push_back #define mp make_pair #define st first #define nd second using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef cc_hash_table<int, int, hash<int>> ht; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset; const double pi = acos(-1); const int mod = 1e9 + 7; const int inf = 1e9 + 7; const int N = 1e6 + 5; const double eps = 1e-9; ll a[3][3], b[3], it[3]; int main() { int n; scanf("%d", &n); for (int i = 0; i < 2; i++) for (int j = 0; j < 3; j++) scanf("%lld", &a[i][j]); ll ans = n; int c = 0; for (int i = 0; i < 3; i++) c += a[0][i] < a[1][i]; for (int i = 0; i < 3; i++) it[i] = i; sort(it, it + 3, [](int x, int y) { return a[1][x] - a[0][x] > a[1][y] - a[0][y]; }); if (c == 3 || c == 0) { for (int i = 0; i < 3; i++) { b[i] = abs(a[0][i] - a[1][i]); a[0][i] = max(a[0][i], a[1][i]); a[1][i] = a[0][i] - b[i]; } for (int i = 0; i <= n; i += a[1][0]) for (int j = 0; j + i <= n; j += a[1][1]) ans = max(ans, i / a[1][0] * a[0][0] + j / a[1][1] * a[0][1] + ((n - i - j) / a[1][2]) * a[0][2] + ((n - i - j) % a[1][2])); } else if (c == 2) { for (int i = 0; i <= n; i += a[0][it[0]]) for (int j = 0; j + i <= n; j += a[0][it[1]]) { ll nx = i / a[0][it[0]] * a[1][it[0]] + j / a[0][it[1]] * a[1][it[1]] + (n - i - j); ans = max(ans, (nx / a[1][it[2]]) * a[0][it[2]] + (nx % a[1][it[2]])); } } else { ll nx = n / a[0][it[0]] * a[1][it[0]] + (n % a[0][it[0]]); if (a[0][it[1]] * a[1][it[2]] > a[0][it[2]] * a[1][it[1]]) swap(it[1], it[2]); for (int j = 0; j <= nx; j += a[1][it[1]]) ans = max(ans, j / a[1][it[1]] * a[0][it[1]] + ((nx - j) / a[1][it[2]]) * a[0][it[2]] + ((nx - j) % a[1][it[2]])); } printf("%lld\n", ans); return 0; }
replace
76
83
76
83
TLE
p03008
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define ll long long #define ull unsigned long long #define ld long double #define pii pair<int, int> #define pll pair<ll, ll> #define pci pair<char, int> #define ppll pair<pll, pll> #define vll vector<ll> #define vvll vector<vll> #define vpll vector<pll> #define pb push_back #define mp make_pair #define fi first #define se second #define mll map<ll, ll> #define fastmap gp_hash_table #define cd complex<double> #define vcd vector<cd> #define PI 3.14159265358979 #pragma 03 using namespace std; using namespace __gnu_pbds; // yesterday was a series of blunders. // first failed to properly implement E (which was just really basic dp/brute // force), then failed to realize that F is just basic centroid decomposition + // dsu. (if I could just decently implement that in contest time... 2100 is // within reach...) such mistakes cannot repeat again. ll a[3], b[3]; ll dp[2500005]; int main() { ll n; cin >> n; for (ll i = 0; i < 3; i++) cin >> a[i]; for (ll i = 0; i < 3; i++) cin >> b[i]; for (ll i = 0; i <= n; i++) { dp[i] = max(dp[i], i); for (ll j = 0; j < 3; j++) { if (i - a[j] >= 0) dp[i] = max(dp[i], dp[i - a[j]] + b[j]); } } n = dp[n]; for (ll i = 0; i <= n; i++) { dp[i] = i; for (ll j = 0; j < 3; j++) { if (i - b[j] >= 0) dp[i] = max(dp[i], dp[i - b[j]] + a[j]); } } n = dp[n]; cout << n << endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define ll long long #define ull unsigned long long #define ld long double #define pii pair<int, int> #define pll pair<ll, ll> #define pci pair<char, int> #define ppll pair<pll, pll> #define vll vector<ll> #define vvll vector<vll> #define vpll vector<pll> #define pb push_back #define mp make_pair #define fi first #define se second #define mll map<ll, ll> #define fastmap gp_hash_table #define cd complex<double> #define vcd vector<cd> #define PI 3.14159265358979 #pragma 03 using namespace std; using namespace __gnu_pbds; // yesterday was a series of blunders. // first failed to properly implement E (which was just really basic dp/brute // force), then failed to realize that F is just basic centroid decomposition + // dsu. (if I could just decently implement that in contest time... 2100 is // within reach...) such mistakes cannot repeat again. ll a[3], b[3]; ll dp[36000000]; int main() { ll n; cin >> n; for (ll i = 0; i < 3; i++) cin >> a[i]; for (ll i = 0; i < 3; i++) cin >> b[i]; for (ll i = 0; i <= n; i++) { dp[i] = max(dp[i], i); for (ll j = 0; j < 3; j++) { if (i - a[j] >= 0) dp[i] = max(dp[i], dp[i - a[j]] + b[j]); } } n = dp[n]; for (ll i = 0; i <= n; i++) { dp[i] = i; for (ll j = 0; j < 3; j++) { if (i - b[j] >= 0) dp[i] = max(dp[i], dp[i - b[j]] + a[j]); } } n = dp[n]; cout << n << endl; }
replace
34
35
34
35
0
p03008
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> gsb_A(3), gsb_B(3); cin >> gsb_A[0] >> gsb_A[1] >> gsb_A[2]; cin >> gsb_B[0] >> gsb_B[1] >> gsb_B[2]; // A -> B // dp[n] := with n acrons, chokudai can gain dp[n] profit // dp[n] = max(dp[n], dp[n - g_A] + (g_B - g_A)); vector<long long> dp1(N + 1); vector<int> profits(3); for (int i = 0; i < 3; i++) { profits[i] = max(0, gsb_B[i] - gsb_A[i]); } for (int n = 0; n <= N; n++) { for (int i = 0; i < 3; i++) { if (n >= gsb_A[i]) { dp1[n] = max(dp1[n], dp1[n - gsb_A[i]] + profits[i]); } } } long long N2 = dp1[N] + N; for (int i = 0; i < 3; i++) { profits[i] = max(0, gsb_A[i] - gsb_B[i]); } vector<long long> dp2(N2); for (int n = 0; n <= N2; n++) { for (int i = 0; i < 3; i++) { if (n >= gsb_B[i]) { dp2[n] = max(dp2[n], dp2[n - gsb_B[i]] + profits[i]); } } } cout << dp2[N2] + N2 << endl; }
#include <iostream> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> gsb_A(3), gsb_B(3); cin >> gsb_A[0] >> gsb_A[1] >> gsb_A[2]; cin >> gsb_B[0] >> gsb_B[1] >> gsb_B[2]; // A -> B // dp[n] := with n acrons, chokudai can gain dp[n] profit // dp[n] = max(dp[n], dp[n - g_A] + (g_B - g_A)); vector<long long> dp1(N + 1); vector<int> profits(3); for (int i = 0; i < 3; i++) { profits[i] = max(0, gsb_B[i] - gsb_A[i]); } for (int n = 0; n <= N; n++) { for (int i = 0; i < 3; i++) { if (n >= gsb_A[i]) { dp1[n] = max(dp1[n], dp1[n - gsb_A[i]] + profits[i]); } } } long long N2 = dp1[N] + N; for (int i = 0; i < 3; i++) { profits[i] = max(0, gsb_A[i] - gsb_B[i]); } vector<long long> dp2(N2 + 1); for (int n = 0; n <= N2; n++) { for (int i = 0; i < 3; i++) { if (n >= gsb_B[i]) { dp2[n] = max(dp2[n], dp2[n - gsb_B[i]] + profits[i]); } } } cout << dp2[N2] + N2 << endl; }
replace
32
33
32
33
0
p03009
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; // #define int long long #define REP(i, n) for (int i = 0, max_i = (n); i < max_i; i++) #define REPI(i, a, b) for (int i = (a), max_i = (b); i < max_i; i++) #define ALL(obj) begin(obj), end(obj) #define RALL(obj) rbegin(obj), rend(obj) #define fi first #define se second using ii = pair<int, int>; vector<ii> dirs = { {1, 0}, {0, 1}, {-1, 0}, {0, -1}, // 4方向 {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // 斜め {0, 0}, // 自身 }; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T, class S> vector<T> make_vec(size_t n, S x) { return vector<T>(n, x); } template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...)); } // debug template <class T> ostream &operator<<(ostream &s, vector<T> &d) { REP(i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : " "); return s; } template <class T> ostream &operator<<(ostream &s, vector<vector<T>> &d) { REP(i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : "\n"); return s; } template <class T, class S> ostream &operator<<(ostream &s, pair<T, S> &p) { s << "{" << p.first << ", " << p.second << "}"; return s; } template <class T, class S> ostream &operator<<(ostream &s, map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; } template <class T, class S> ostream &operator<<(ostream &s, unordered_map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; } #ifdef _MY_DEBUG #define dump(...) \ cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ \ << "]" << endl, \ dump_func(__VA_ARGS__), cerr << "*/\n\n"; #else #define dump(...) #define endl "\n" #endif void dump_func() { cerr << endl; } template <class Head, class... Tail> void dump_func(Head &&h, Tail &&...t) { cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...); } struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); constexpr int MOD = 1000000007; // *************** TEMPLATE END *************** template <class T> T pow(T x, int n, const T UNION = 1) { T ret = UNION; while (n) { if (n & 1) ret *= x; x *= x; n >>= 1; } return ret; } template <int MD> struct ModInt { int x; static unordered_map<int, int> to_inv; ModInt() : x(0) {} ModInt(int x_) { if ((x = x_ % MD + MD) >= MD) x -= MD; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MD) x -= MD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MD; return *this; } ModInt &operator-=(ModInt that) { if ((x -= that.x) < 0) x += MD; return *this; } ModInt &operator/=(ModInt that) { x = (unsigned long long)x * that.inv().x % MD; return *this; } ModInt operator-() const { return -x < 0 ? MD - x : -x; } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } bool operator==(ModInt that) const { return x == that.x; } bool operator!=(ModInt that) const { return x != that.x; } ModInt inv() const { return to_inv.count(this->x) ? to_inv[this->x] : (to_inv[this->x] = pow(*this, MD - 2).x); } friend ostream &operator<<(ostream &s, ModInt<MD> a) { s << a.x; return s; } friend istream &operator>>(istream &s, ModInt<MD> &a) { s >> a.x; return s; } }; template <int MD> unordered_map<int, int> ModInt<MD>::to_inv; using mint = ModInt<MOD>; vector<mint> fact, fact_inv; void init_factorial(int n) { fact = vector<mint>(n + 1, 1); fact_inv = vector<mint>(n + 1); for (int i = 0; i < n; i++) fact[i + 1] = fact[i] * (i + 1); fact_inv[n] = mint(1) / fact[n]; for (int i = n - 1; i >= 0; i--) fact_inv[i] = fact_inv[i + 1] * (i + 1); // for (int i = 0; i < n + 1; i++) assert(fact[i] * fact_inv[i] == 1); } mint comb(int n, int r) { return fact[n] * fact_inv[r] * fact_inv[n - r]; } // T0: 元の配列のモノイド // T1: T0に対する作用素モノイド template <class T0, class T1> class SegmentTree { // k番目のノードにのlazyを伝搬 void eval(int k, int len) { // u1が正確に単位元ならいらない // if (lazy[k] == u1) return; // len個分のlazy[k]を評価 node[k] = g(node[k], p(lazy[k], len)); if (k < N - 1) { // 最下段でなければ下のlazyに伝搬 lazy[2 * k + 1] = f1(lazy[2 * k + 1], lazy[k]); lazy[2 * k + 2] = f1(lazy[2 * k + 2], lazy[k]); } lazy[k] = u1; } // k番目のノード[l, r)について、[a, b)の範囲内にxを作用 void update(int a, int b, T1 x, int k, int l, int r) { eval(k, r - l); if (b <= l || r <= a) return; if (a <= l && r <= b) { lazy[k] = f1(lazy[k], x); eval(k, r - l); } else { update(a, b, x, 2 * k + 1, l, (l + r) / 2); update(a, b, x, 2 * k + 2, (l + r) / 2, r); node[k] = f0(node[2 * k + 1], node[2 * k + 2]); } } // k番目のノード[l, r)について、[a, b)のクエリを求める T0 query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return u0; eval(k, r - l); if (a <= l && r <= b) return node[k]; T0 vl = query(a, b, 2 * k + 1, l, (l + r) / 2); T0 vr = query(a, b, 2 * k + 2, (l + r) / 2, r); return f0(vl, vr); } public: int sz; // 元の配列のサイズ int N; vector<T0> node; vector<T1> lazy; // T0上の演算、単位元 using F0 = function<T0(T0, T0)>; F0 f0; T0 u0; // T1上の演算、単位元 using F1 = function<T1(T1, T1)>; F1 f1; T1 u1; // 作用 using G = function<T0(T0, T1)>; G g; // 多数のt1(T1)に対するf1の合成 using P = function<T1(T1, int)>; P p; SegmentTree(const vector<T0> &a, F0 f0, T0 u0, F1 f1, T1 u1, G g, P p) : sz(a.size()), f0(f0), u0(u0), f1(f1), u1(u1), g(g), p(p) { for (N = 1; N < sz; N *= 2) ; node.resize(2 * N - 1); lazy.resize(2 * N - 1, u1); REP(i, sz) node[N - 1 + i] = a[i]; for (int i = N - 2; i >= 0; i--) node[i] = f0(node[2 * i + 1], node[2 * i + 2]); } // [a, b)にxを作用 void update(int a, int b, T1 x) { assert(0 <= a && a < b && b <= sz); update(a, b, x, 0, 0, N); } void update(int a, T1 x) { update(a, a + 1, x); } // [a, b) T0 query(int a, int b) { return query(a, b, 0, 0, N); } T0 query(int a) { return query(a, a + 1); } }; signed main() { int n, H, D; cin >> n >> H >> D; init_factorial(n + 10); mint mul = 0; REPI(i, 1, n + 1) { mul += fact[i]; } // Sum & Add SegmentTree<mint, mint> seg(vector<mint>(H + 1, 0), plus<mint>(), 0, plus<mint>(), 0, plus<mint>(), multiplies<mint>()); seg.update(0, fact[n] / mul); REP(i, H) { mint now = seg.query(i); seg.update(i + 1, min(H + 1, i + D + 1), now * mul); } cout << seg.query(H) << endl; }
#include <bits/stdc++.h> using namespace std; // #define int long long #define REP(i, n) for (int i = 0, max_i = (n); i < max_i; i++) #define REPI(i, a, b) for (int i = (a), max_i = (b); i < max_i; i++) #define ALL(obj) begin(obj), end(obj) #define RALL(obj) rbegin(obj), rend(obj) #define fi first #define se second using ii = pair<int, int>; vector<ii> dirs = { {1, 0}, {0, 1}, {-1, 0}, {0, -1}, // 4方向 {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // 斜め {0, 0}, // 自身 }; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T, class S> vector<T> make_vec(size_t n, S x) { return vector<T>(n, x); } template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...)); } // debug template <class T> ostream &operator<<(ostream &s, vector<T> &d) { REP(i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : " "); return s; } template <class T> ostream &operator<<(ostream &s, vector<vector<T>> &d) { REP(i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : "\n"); return s; } template <class T, class S> ostream &operator<<(ostream &s, pair<T, S> &p) { s << "{" << p.first << ", " << p.second << "}"; return s; } template <class T, class S> ostream &operator<<(ostream &s, map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; } template <class T, class S> ostream &operator<<(ostream &s, unordered_map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; } #ifdef _MY_DEBUG #define dump(...) \ cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ \ << "]" << endl, \ dump_func(__VA_ARGS__), cerr << "*/\n\n"; #else #define dump(...) #define endl "\n" #endif void dump_func() { cerr << endl; } template <class Head, class... Tail> void dump_func(Head &&h, Tail &&...t) { cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...); } struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); constexpr int MOD = 1000000007; // *************** TEMPLATE END *************** template <class T> T pow(T x, int n, const T UNION = 1) { T ret = UNION; while (n) { if (n & 1) ret *= x; x *= x; n >>= 1; } return ret; } template <int MD> struct ModInt { int x; static unordered_map<int, int> to_inv; ModInt() : x(0) {} ModInt(int x_) { if ((x = x_ % MD + MD) >= MD) x -= MD; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MD) x -= MD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MD; return *this; } ModInt &operator-=(ModInt that) { if ((x -= that.x) < 0) x += MD; return *this; } ModInt &operator/=(ModInt that) { x = (unsigned long long)x * that.inv().x % MD; return *this; } ModInt operator-() const { return -x < 0 ? MD - x : -x; } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } bool operator==(ModInt that) const { return x == that.x; } bool operator!=(ModInt that) const { return x != that.x; } ModInt inv() const { return to_inv.count(this->x) ? to_inv[this->x] : (to_inv[this->x] = pow(*this, MD - 2).x); } friend ostream &operator<<(ostream &s, ModInt<MD> a) { s << a.x; return s; } friend istream &operator>>(istream &s, ModInt<MD> &a) { s >> a.x; return s; } }; template <int MD> unordered_map<int, int> ModInt<MD>::to_inv; using mint = ModInt<MOD>; vector<mint> fact, fact_inv; void init_factorial(int n) { fact = vector<mint>(n + 1, 1); fact_inv = vector<mint>(n + 1); for (int i = 0; i < n; i++) fact[i + 1] = fact[i] * (i + 1); fact_inv[n] = mint(1) / fact[n]; for (int i = n - 1; i >= 0; i--) fact_inv[i] = fact_inv[i + 1] * (i + 1); // for (int i = 0; i < n + 1; i++) assert(fact[i] * fact_inv[i] == 1); } mint comb(int n, int r) { return fact[n] * fact_inv[r] * fact_inv[n - r]; } // T0: 元の配列のモノイド // T1: T0に対する作用素モノイド template <class T0, class T1> class SegmentTree { // k番目のノードにのlazyを伝搬 void eval(int k, int len) { // u1が正確に単位元ならいらない // if (lazy[k] == u1) return; // len個分のlazy[k]を評価 node[k] = g(node[k], p(lazy[k], len)); if (k < N - 1) { // 最下段でなければ下のlazyに伝搬 lazy[2 * k + 1] = f1(lazy[2 * k + 1], lazy[k]); lazy[2 * k + 2] = f1(lazy[2 * k + 2], lazy[k]); } lazy[k] = u1; } // k番目のノード[l, r)について、[a, b)の範囲内にxを作用 void update(int a, int b, T1 x, int k, int l, int r) { eval(k, r - l); if (b <= l || r <= a) return; if (a <= l && r <= b) { lazy[k] = f1(lazy[k], x); eval(k, r - l); } else { update(a, b, x, 2 * k + 1, l, (l + r) / 2); update(a, b, x, 2 * k + 2, (l + r) / 2, r); node[k] = f0(node[2 * k + 1], node[2 * k + 2]); } } // k番目のノード[l, r)について、[a, b)のクエリを求める T0 query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return u0; eval(k, r - l); if (a <= l && r <= b) return node[k]; T0 vl = query(a, b, 2 * k + 1, l, (l + r) / 2); T0 vr = query(a, b, 2 * k + 2, (l + r) / 2, r); return f0(vl, vr); } public: int sz; // 元の配列のサイズ int N; vector<T0> node; vector<T1> lazy; // T0上の演算、単位元 using F0 = function<T0(T0, T0)>; F0 f0; T0 u0; // T1上の演算、単位元 using F1 = function<T1(T1, T1)>; F1 f1; T1 u1; // 作用 using G = function<T0(T0, T1)>; G g; // 多数のt1(T1)に対するf1の合成 using P = function<T1(T1, int)>; P p; SegmentTree(const vector<T0> &a, F0 f0, T0 u0, F1 f1, T1 u1, G g, P p) : sz(a.size()), f0(f0), u0(u0), f1(f1), u1(u1), g(g), p(p) { for (N = 1; N < sz; N *= 2) ; node.resize(2 * N - 1); lazy.resize(2 * N - 1, u1); REP(i, sz) node[N - 1 + i] = a[i]; for (int i = N - 2; i >= 0; i--) node[i] = f0(node[2 * i + 1], node[2 * i + 2]); } // [a, b)にxを作用 void update(int a, int b, T1 x) { assert(0 <= a && a < b && b <= sz); update(a, b, x, 0, 0, N); } void update(int a, T1 x) { update(a, a + 1, x); } // [a, b) T0 query(int a, int b) { return query(a, b, 0, 0, N); } T0 query(int a) { return query(a, a + 1); } }; signed main() { int n, H, D; cin >> n >> H >> D; init_factorial(n + 10); mint mul = 0; REPI(i, 1, n + 1) { mul += fact[i]; } vector<mint> dp(H + 1); dp[0] = fact[n] / mul; mint acc = dp[0]; REPI(i, 1, H + 1) { dp[i] = acc * mul; acc += dp[i]; if (i - D >= 0) acc -= dp[i - D]; } cout << dp[H] << endl; ; }
replace
251
262
251
263
TLE
p03009
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using int64 = long long; const int mod = 1e9 + 7; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template <typename T = int64> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } }; using modint = ModInt<mod>; template <class T> struct BinaryIndexedTree { vector<T> data; BinaryIndexedTree(int sz) { data.assign(++sz, 0); } T sum(int k) { T ret = 0; for (++k; k > 0; k -= k & -k) ret += data[k]; return (ret); } void add(int k, T x) { for (++k; k < data.size(); k += k & -k) data[k] += x; } }; int main() { int N, H, D; cin >> N >> H >> D; modint uku = 0, p = 1; for (int i = 1; i <= N; i++) { p *= i; uku += p; } BinaryIndexedTree<modint> tap(N + 1); modint beet = uku + 1; modint mul = p; BinaryIndexedTree<modint> bit(N + 1); bit.add(1, mul); for (int i = 2; i <= D; i++) { // D のところまでいく mul *= beet; bit.add(i, mul); } modint cur = mul; if (H == D) { cout << cur << endl; exit(0); } for (int i = D + 1; i <= H; i++) { modint sum; sum = bit.sum(i); sum -= bit.sum(i - D - 1); sum *= uku; bit.add(i, sum); if (i == H) { cout << sum << endl; } } }
#include <bits/stdc++.h> using namespace std; using int64 = long long; const int mod = 1e9 + 7; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template <typename T = int64> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } }; using modint = ModInt<mod>; template <class T> struct BinaryIndexedTree { vector<T> data; BinaryIndexedTree(int sz) { data.assign(++sz, 0); } T sum(int k) { T ret = 0; for (++k; k > 0; k -= k & -k) ret += data[k]; return (ret); } void add(int k, T x) { for (++k; k < data.size(); k += k & -k) data[k] += x; } }; int main() { int N, H, D; cin >> N >> H >> D; modint uku = 0, p = 1; for (int i = 1; i <= N; i++) { p *= i; uku += p; } BinaryIndexedTree<modint> tap(N + 1); modint beet = uku + 1; modint mul = p; BinaryIndexedTree<modint> bit(H + 1); bit.add(1, mul); for (int i = 2; i <= D; i++) { // D のところまでいく mul *= beet; bit.add(i, mul); } modint cur = mul; if (H == D) { cout << cur << endl; exit(0); } for (int i = D + 1; i <= H; i++) { modint sum; sum = bit.sum(i); sum -= bit.sum(i - D - 1); sum *= uku; bit.add(i, sum); if (i == H) { cout << sum << endl; } } }
replace
180
181
180
181
0
p03009
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; const int maxn = 100010; const ll P = 1000000007; int n, m, D; ll ans, n1; ll f[maxn], s[maxn]; int main() { scanf("%d%d%d", &n, &m, &D); int i; ll tmp = 1; f[0] = s[0] = 1; for (i = 1; i <= n; i++) tmp = tmp * i % P, n1 = (n1 + tmp) % P; for (i = 1; i < m; i++) { f[i] = s[i - 1] - ((i <= D) ? 0 : s[i - D - 1]) + P; f[i] = f[i] * n1 % P; s[i] = (s[i - 1] + f[i]) % P; } printf("%lld\n", (s[m - 1] - s[m - D - 1] + P) * tmp % P); return 0; }
#include <algorithm> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; const int maxn = 1000010; const ll P = 1000000007; int n, m, D; ll ans, n1; ll f[maxn], s[maxn]; int main() { scanf("%d%d%d", &n, &m, &D); int i; ll tmp = 1; f[0] = s[0] = 1; for (i = 1; i <= n; i++) tmp = tmp * i % P, n1 = (n1 + tmp) % P; for (i = 1; i < m; i++) { f[i] = s[i - 1] - ((i <= D) ? 0 : s[i - D - 1]) + P; f[i] = f[i] * n1 % P; s[i] = (s[i - 1] + f[i]) % P; } printf("%lld\n", (s[m - 1] - s[m - D - 1] + P) * tmp % P); return 0; }
replace
5
6
5
6
0
p03009
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long fact[1000005]; long long dp[1000005]; const long long MOD = 1e9 + 7; int main() { int N, H, D; scanf("%d%d%d", &N, &H, &D); long long retainWays = 0; fact[0] = 1; for (int i = 1; i <= N; i++) { fact[i] = fact[i - 1] * i % MOD; retainWays += fact[i] % MOD; } retainWays %= MOD; // printf("retainWays=%lld\n", retainWays); dp[0] = fact[N]; long long slidingWindowSum = dp[0]; for (int i = 1; i <= H; i++) { dp[i] = slidingWindowSum; /*for(int j = 1; j <= min(i, D); j ++){ dp[i] += dp[i-j]; }*/ dp[i] %= MOD; if (i == H) { break; } dp[i] = dp[i] * retainWays % MOD; slidingWindowSum += dp[i]; if (slidingWindowSum >= 0) { slidingWindowSum -= dp[i - D]; } slidingWindowSum = (slidingWindowSum + 2 * MOD) % MOD; // printf("dp[%d]=%lld\n", i, dp[i]); } printf("%lld", dp[H]); return 0; }
#include <bits/stdc++.h> using namespace std; long long fact[1000005]; long long dp[1000005]; const long long MOD = 1e9 + 7; int main() { int N, H, D; scanf("%d%d%d", &N, &H, &D); long long retainWays = 0; fact[0] = 1; for (int i = 1; i <= N; i++) { fact[i] = fact[i - 1] * i % MOD; retainWays += fact[i] % MOD; } retainWays %= MOD; // printf("retainWays=%lld\n", retainWays); dp[0] = fact[N]; long long slidingWindowSum = dp[0]; for (int i = 1; i <= H; i++) { dp[i] = slidingWindowSum; /*for(int j = 1; j <= min(i, D); j ++){ dp[i] += dp[i-j]; }*/ dp[i] %= MOD; if (i == H) { break; } dp[i] = dp[i] * retainWays % MOD; slidingWindowSum += dp[i]; if (i - D >= 0) { slidingWindowSum -= dp[i - D]; } slidingWindowSum = (slidingWindowSum + 2 * MOD) % MOD; // printf("dp[%d]=%lld\n", i, dp[i]); } printf("%lld", dp[H]); return 0; }
replace
39
40
39
40
0
p03009
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, x, y) for (register int i = x; i <= y; ++i) #define repd(i, x, y) for (register int i = x; i >= y; --i) using namespace std; typedef long long ll; template <typename T> inline void read(T &x) { char c; int sign = 1; x = 0; do { c = getchar(); if (c == '-') sign = -1; } while (!isdigit(c)); do { x = x * 10 + c - '0'; c = getchar(); } while (isdigit(c)); x *= sign; } const int mod = 1e9 + 7, N = 2e5 + 50; int f[N], fac[N], m, n, h, d; inline int ksm(int x, int y) { int ans = 1; while (y) { if (y & 1) ans = 1ll * ans * x % mod; y >>= 1; x = 1ll * x * x % mod; } return ans; } int main() { read(n); read(h); read(d); f[0] = 1; fac[0] = 1; rep(i, 1, n) fac[i] = 1ll * fac[i - 1] * i % mod; rep(i, 1, n) m = (m + fac[i]) % mod; rep(i, 1, h) { f[i] = 1ll * (f[i - 1] - (i - d - 1 >= 0 ? f[i - d - 1] : 0)) * m % mod; if (f[i] < 0) f[i] = f[i] + mod; f[i] = (f[i - 1] + f[i]) % mod; } cout << 1ll * (f[h] - f[h - 1] + mod) % mod * fac[n] % mod * ksm(m, mod - 2) % mod << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, x, y) for (register int i = x; i <= y; ++i) #define repd(i, x, y) for (register int i = x; i >= y; --i) using namespace std; typedef long long ll; template <typename T> inline void read(T &x) { char c; int sign = 1; x = 0; do { c = getchar(); if (c == '-') sign = -1; } while (!isdigit(c)); do { x = x * 10 + c - '0'; c = getchar(); } while (isdigit(c)); x *= sign; } const int mod = 1e9 + 7, N = 1e6 + 50; int f[N], fac[N], m, n, h, d; inline int ksm(int x, int y) { int ans = 1; while (y) { if (y & 1) ans = 1ll * ans * x % mod; y >>= 1; x = 1ll * x * x % mod; } return ans; } int main() { read(n); read(h); read(d); f[0] = 1; fac[0] = 1; rep(i, 1, n) fac[i] = 1ll * fac[i - 1] * i % mod; rep(i, 1, n) m = (m + fac[i]) % mod; rep(i, 1, h) { f[i] = 1ll * (f[i - 1] - (i - d - 1 >= 0 ? f[i - d - 1] : 0)) * m % mod; if (f[i] < 0) f[i] = f[i] + mod; f[i] = (f[i - 1] + f[i]) % mod; } cout << 1ll * (f[h] - f[h - 1] + mod) % mod * fac[n] % mod * ksm(m, mod - 2) % mod << endl; return 0; }
replace
21
22
21
22
0
p03009
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <bitset> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iosfwd> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; const int N = 3e5 + 500; const int LN = 21; const long long mod = 1e9 + 7; const long long INF = 1LL << 57; long long n, m, u, v, k, t, q, a, h, x, d; long long fact[N]; long long dp[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> h >> d; fact[0] = 1; long long sum = 0; for (int i = 1; i <= n; ++i) { fact[i] = 1LL * i * fact[i - 1]; fact[i] %= mod; sum += fact[i]; if (sum >= mod) sum -= mod; } dp[0] = fact[n]; for (int i = 1; i <= h - 1; ++i) { long long cur = dp[i - 1]; if (i - d - 1 >= 0) cur -= dp[i - d - 1]; if (cur < 0) cur += mod; dp[i] = (sum * cur) % mod; dp[i] += dp[i - 1]; if (dp[i] >= mod) dp[i] -= mod; } long long cur = dp[h - 1]; if (h - d - 1 >= 0) cur -= dp[h - d - 1]; if (cur < 0) cur += mod; cout << cur << endl; }
#include <algorithm> #include <assert.h> #include <bitset> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iosfwd> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; const int N = 1e6 + 500; const int LN = 21; const long long mod = 1e9 + 7; const long long INF = 1LL << 57; long long n, m, u, v, k, t, q, a, h, x, d; long long fact[N]; long long dp[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> h >> d; fact[0] = 1; long long sum = 0; for (int i = 1; i <= n; ++i) { fact[i] = 1LL * i * fact[i - 1]; fact[i] %= mod; sum += fact[i]; if (sum >= mod) sum -= mod; } dp[0] = fact[n]; for (int i = 1; i <= h - 1; ++i) { long long cur = dp[i - 1]; if (i - d - 1 >= 0) cur -= dp[i - d - 1]; if (cur < 0) cur += mod; dp[i] = (sum * cur) % mod; dp[i] += dp[i - 1]; if (dp[i] >= mod) dp[i] -= mod; } long long cur = dp[h - 1]; if (h - d - 1 >= 0) cur -= dp[h - d - 1]; if (cur < 0) cur += mod; cout << cur << endl; }
replace
32
33
32
33
0
p03011
Python
Runtime Error
s = map(int, input().split()) s.sort() print(s[0] + s[1])
s = list(map(int, input().split())) s.sort() print(s[0] + s[1])
replace
0
1
0
1
AttributeError: 'map' object has no attribute 'sort'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03011/Python/s230336688.py", line 2, in <module> s.sort() AttributeError: 'map' object has no attribute 'sort'