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
p03031
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <set> #include <vector> using namespace std; #define pb push_back #define fi first #define se second typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<string> vs; typedef vector<pii> vpii; typedef vector<pll> vpll; static const ll maxLL = (ll)1 << 62; const ll MOD = 1000000007, INF = 1e18; int main(void) { int n, m; cin >> n >> m; vector<vi> s(m, vi(m, 0)); vi p(m, 0); for (int i = 0; i < m; ++i) { int k; cin >> k; for (int j = 0; j < k; ++j) { cin >> s[i][j]; } } for (int i = 0; i < m; ++i) { cin >> p[i]; } int ans = 0; if (n == 1) { cout << p[0] << endl; return 0; } for (int bit = 0; bit < (1 << n); ++bit) { bool is_cnt_up = true; for (int j = 0; j < m; ++j) { int on_num = 0; for (auto sk : s[j]) { if ((bit >> (sk - 1)) & 1) on_num++; } if (on_num % 2 != p[j]) is_cnt_up = false; } if (is_cnt_up) ans++; } cout << ans << endl; }
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <set> #include <vector> using namespace std; #define pb push_back #define fi first #define se second typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<string> vs; typedef vector<pii> vpii; typedef vector<pll> vpll; static const ll maxLL = (ll)1 << 62; const ll MOD = 1000000007, INF = 1e18; int main(void) { int n, m; cin >> n >> m; vector<vi> s(m, vi(11, 0)); vi p(m, 0); for (int i = 0; i < m; ++i) { int k; cin >> k; for (int j = 0; j < k; ++j) { cin >> s[i][j]; } } for (int i = 0; i < m; ++i) { cin >> p[i]; } int ans = 0; if (n == 1) { cout << p[0] << endl; return 0; } for (int bit = 0; bit < (1 << n); ++bit) { bool is_cnt_up = true; for (int j = 0; j < m; ++j) { int on_num = 0; for (auto sk : s[j]) { if ((bit >> (sk - 1)) & 1) on_num++; } if (on_num % 2 != p[j]) is_cnt_up = false; } if (is_cnt_up) ans++; } cout << ans << endl; }
replace
27
28
27
28
0
p03031
C++
Runtime Error
#include <stdio.h> #include <stdlib.h> #define MAX_N (10) #define MAX_M (10) int main(int argc, char *argv[]) { // read inputs int N, M, ks[MAX_M], xss[MAX_M][MAX_N], ps[MAX_M]; scanf("%d %d", &N, &M); for (int i = 0; i < M; i++) { scanf("%d", &ks[i]); for (int j = 0; j < N; j++) { scanf("%d", &xss[i][j]); xss[i][j]--; // NOTE : modified input } } for (int i = 0; i < M; i++) { scanf("%d", &ps[i]); } // search all int ans = 0; for (int S = 0; S < (1 << N); S++) { int is_all_on = 1; for (int i = 0; i < M; i++) { // check if light bulb i will light up const int k = ks[i], p = ps[i]; int num = 0; for (int l = 0; l < k; l++) { const int x = xss[i][l]; if ((S & (1 << x)) != 0) { num++; } } if (num % 2 != p) { is_all_on = 0; break; } } if (is_all_on) { ans++; } } printf("%d\n", ans); return 0; }
#include <stdio.h> #include <stdlib.h> #define MAX_N (10) #define MAX_M (10) int main(int argc, char *argv[]) { // read inputs int N, M, ks[MAX_M], xss[MAX_M][MAX_N], ps[MAX_M]; scanf("%d %d", &N, &M); for (int i = 0; i < M; i++) { scanf("%d", &ks[i]); for (int l = 0; l < ks[i]; l++) { scanf("%d", &xss[i][l]); xss[i][l]--; // NOTE : modified input } } for (int i = 0; i < M; i++) { scanf("%d", &ps[i]); } // search all int ans = 0; for (int S = 0; S < (1 << N); S++) { int is_all_on = 1; for (int i = 0; i < M; i++) { // check if light bulb i will light up const int k = ks[i], p = ps[i]; int num = 0; for (int l = 0; l < k; l++) { const int x = xss[i][l]; if ((S & (1 << x)) != 0) { num++; } } if (num % 2 != p) { is_all_on = 0; break; } } if (is_all_on) { ans++; } } printf("%d\n", ans); return 0; }
replace
12
15
12
15
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) int main() { int n, m, k; cin >> n >> m; vector<bitset<10>> a(m); vector<int> c(m); rep(i, m) { cin >> k; int b; bitset<10> bs; rep(j, k) { cin >> b; bs.set(b); } a[i] = bs; } rep(i, m) { cin >> c[i]; } int sum = 0; for (int bit = 0; bit < (1 << n); ++bit) { rep(i, m) { if (c[i] != (int)(a[i] & (bitset<10>)bit).count() % 2) { sum--; break; } } sum++; } cout << sum; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) int main() { int n, m, k; cin >> n >> m; vector<bitset<10>> a(m); vector<int> c(m); rep(i, m) { cin >> k; int b; bitset<10> bs; rep(j, k) { cin >> b; bs.set(b - 1); } a[i] = bs; } rep(i, m) { cin >> c[i]; } int sum = 0; for (int bit = 0; bit < (1 << n); ++bit) { rep(i, m) { if (c[i] != (int)(a[i] & (bitset<10>)bit).count() % 2) { sum--; break; } } sum++; } cout << sum; }
replace
14
15
14
15
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> #include <cmath> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <string> #define pb push_back #define ll long long #define ull unsigned long long #define deb(x) cerr << #x << " " << x << "\n" #define debi cerr << "hey sparky\n" #define x first #define y second #define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define eps (double)1e-9 #define all(x) x.begin(), x.end() #define int long long using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pdbs; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } // void __print(long long x) {cerr << x;} void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifndef ONLINE_JUDGE #define debug(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define debug(x...) #endif // look for all edge cases // search for a pattern int n, bulbs; map<int, set<int>> m; vector<int> decision; int calc(int idx, vector<int> state) { if (idx == n) { int cnt = 0; for (int i = 0; i < (int)state.size(); i++) { if (state[i] % 2 == decision[i]) { cnt += 1; } } if (cnt == bulbs) { return 1; } else { return 0; } } int ans = 0; vector<int> new_state = state; for (auto it : m[idx]) { new_state[it] += 1; } ans += calc(idx + 1, state); ans += calc(idx + 1, new_state); return ans; } signed main() { ios::sync_with_stdio(0); cout.tie(0); cin.tie(0); cin >> n >> bulbs; for (int i = 0; i < bulbs; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int temp; cin >> temp; m[temp - 1].insert(j); } } for (int i = 0; i < bulbs; i++) { decision.pb(-1); cin >> decision[i]; } vector<int> state(bulbs, 0); int ans = calc(0, state); cout << ans; return 0; }
#include <bits/stdc++.h> #include <cmath> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <string> #define pb push_back #define ll long long #define ull unsigned long long #define deb(x) cerr << #x << " " << x << "\n" #define debi cerr << "hey sparky\n" #define x first #define y second #define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define eps (double)1e-9 #define all(x) x.begin(), x.end() #define int long long using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pdbs; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } // void __print(long long x) {cerr << x;} void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifndef ONLINE_JUDGE #define debug(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define debug(x...) #endif // look for all edge cases // search for a pattern int n, bulbs; map<int, set<int>> m; vector<int> decision; int calc(int idx, vector<int> state) { if (idx == n) { int cnt = 0; for (int i = 0; i < (int)state.size(); i++) { if (state[i] % 2 == decision[i]) { cnt += 1; } } if (cnt == bulbs) { return 1; } else { return 0; } } int ans = 0; vector<int> new_state = state; for (auto it : m[idx]) { new_state[it] += 1; } ans += calc(idx + 1, state); ans += calc(idx + 1, new_state); return ans; } signed main() { ios::sync_with_stdio(0); cout.tie(0); cin.tie(0); cin >> n >> bulbs; for (int i = 0; i < bulbs; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int temp; cin >> temp; m[temp - 1].insert(i); } } for (int i = 0; i < bulbs; i++) { decision.pb(-1); cin >> decision[i]; } vector<int> state(bulbs, 0); int ans = calc(0, state); cout << ans; return 0; }
replace
113
114
113
114
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; using namespace std; ll MOD = 1000000007; #define vec vector<int> #define vecll vector<ll> #define vecst vector<string> #define vecb vector<bool> #define vec2(var, n, m) vector<vector<int>> var(n, vector<int>(m, 0)) #define vecll2(var, n, m) vector<vector<ll>> var(n, vector<ll>(m, 0)) #define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++) #define REP(i, m, n) for (ll i = (ll)m; i < (ll)n; i++) #define arr(var, n) \ vec var(n); \ rep(i, n) { cin >> var[i]; } #define arrll(var, n) \ vecll var(n); \ rep(i, n) { cin >> var[i]; } #define arrst(var, n) \ vecst var(n); \ rep(i, n) { cin >> var[i]; } #define all(var) (var).begin(), (var).end() #define sortall(var) sort(all(var)) #define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end()); #define f_sum(var) accumulate(all(var), 0) #define f_sumll(var) accumulate(all(var), 0LL) #define chmin(v1, v2) v1 = min(v1, v2) #define chmax(v1, v2) v1 = max(v1, v2) #define pb(var) push_back(var) #define prt(var) cout << (var) << "\n" #define prtd(n, var) cout << fixed << setprecision(n) << var << "\n" #define prtfill(n, var) cout << setw(n) << setfill('0') << var; #define prt2(v1, v2) cout << v1 << " " << v2 << "\n" #define prt3(v1, v2, v3) cout << v1 << " " << v2 << " " << v3 << "\n" #define prtall(v) \ rep(i, v.size()) { cout << v[i] << (i != v.size() - 1 ? " " : "\n"); } void prtok(bool ok) { prt(ok ? "yes" : "no"); } //---------------------------------------------------------------- #define bitrep(i, n) for (ll i = 0; i < (ll)(1 << n); i++) #define bis(i, j) i & (1 << j) int main(void) { int n, m; cin >> n >> m; bool light[m][n]; rep(i, n) { rep(j, n) { light[i][j] = false; } } rep(i, m) { int num; cin >> num; rep(j, num) { int tmp; cin >> tmp; tmp--; light[i][tmp] = true; } } arr(p, m); int ans = 0; bitrep(i, n) { int lightnum = 0; rep(j, m) { int on = 0; rep(k, n) { if (bis(i, k)) { if (light[j][k]) { on++; } } } if (on % 2 == p[j]) { lightnum++; } } if (lightnum == m) { ans++; } } prt(ans); }
#include <bits/stdc++.h> typedef long long ll; using namespace std; ll MOD = 1000000007; #define vec vector<int> #define vecll vector<ll> #define vecst vector<string> #define vecb vector<bool> #define vec2(var, n, m) vector<vector<int>> var(n, vector<int>(m, 0)) #define vecll2(var, n, m) vector<vector<ll>> var(n, vector<ll>(m, 0)) #define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++) #define REP(i, m, n) for (ll i = (ll)m; i < (ll)n; i++) #define arr(var, n) \ vec var(n); \ rep(i, n) { cin >> var[i]; } #define arrll(var, n) \ vecll var(n); \ rep(i, n) { cin >> var[i]; } #define arrst(var, n) \ vecst var(n); \ rep(i, n) { cin >> var[i]; } #define all(var) (var).begin(), (var).end() #define sortall(var) sort(all(var)) #define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end()); #define f_sum(var) accumulate(all(var), 0) #define f_sumll(var) accumulate(all(var), 0LL) #define chmin(v1, v2) v1 = min(v1, v2) #define chmax(v1, v2) v1 = max(v1, v2) #define pb(var) push_back(var) #define prt(var) cout << (var) << "\n" #define prtd(n, var) cout << fixed << setprecision(n) << var << "\n" #define prtfill(n, var) cout << setw(n) << setfill('0') << var; #define prt2(v1, v2) cout << v1 << " " << v2 << "\n" #define prt3(v1, v2, v3) cout << v1 << " " << v2 << " " << v3 << "\n" #define prtall(v) \ rep(i, v.size()) { cout << v[i] << (i != v.size() - 1 ? " " : "\n"); } void prtok(bool ok) { prt(ok ? "yes" : "no"); } //---------------------------------------------------------------- #define bitrep(i, n) for (ll i = 0; i < (ll)(1 << n); i++) #define bis(i, j) i & (1 << j) int main(void) { int n, m; cin >> n >> m; bool light[m][n]; rep(i, m) { rep(j, n) { light[i][j] = false; } } rep(i, m) { int num; cin >> num; rep(j, num) { int tmp; cin >> tmp; tmp--; light[i][tmp] = true; } } arr(p, m); int ans = 0; bitrep(i, n) { int lightnum = 0; rep(j, m) { int on = 0; rep(k, n) { if (bis(i, k)) { if (light[j][k]) { on++; } } } if (on % 2 == p[j]) { lightnum++; } } if (lightnum == m) { ans++; } } prt(ans); }
replace
47
48
47
48
0
p03031
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; int main() { int n, m; cin >> n >> m; vector<int> k(m); vector<vector<int>> s(m, vector<int>(m)); vector<int> p(m); rep(i, m) { cin >> k[i]; rep(j, k[i]) { cin >> s[i][j]; } } rep(i, m) cin >> p[i]; int res = 0; for (int bit = 0; bit < (1 << n); bit++) { bool flag = 0; rep(i, m) { bool parity = 0; rep(j, k[i]) { if ((1 << (s[i][j] - 1)) & bit) parity ^= 1; } if (parity == p[i]) continue; else { flag = 1; break; } } if (flag == 0) res++; } cout << res << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; int main() { int n, m; cin >> n >> m; vector<int> k(m); vector<vector<int>> s(m, vector<int>(n)); vector<int> p(m); rep(i, m) { cin >> k[i]; rep(j, k[i]) { cin >> s[i][j]; } } rep(i, m) cin >> p[i]; int res = 0; for (int bit = 0; bit < (1 << n); bit++) { bool flag = 0; rep(i, m) { bool parity = 0; rep(j, k[i]) { if ((1 << (s[i][j] - 1)) & bit) parity ^= 1; } if (parity == p[i]) continue; else { flag = 1; break; } } if (flag == 0) res++; } cout << res << endl; return 0; }
replace
10
11
10
11
0
p03031
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <string> #include <vector> using namespace std; int main() { int n, m; cin >> n >> m; auto k = vector<int>(m); auto s = vector<vector<int>>(m); auto p = vector<int>(m); for (int i = 0; i < m; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { int tmp; cin >> tmp; s[i].push_back(tmp); } } for (int i = 0; i < m; i++) { cin >> p[i]; } int ans = 0; int ok; for (int i = 0; i < (int)pow(2, n); i++) { ok = 1; for (int j = 0; j < m; j++) { int sum = 0; for (int l = 0; l < k[i]; l++) { sum = (sum + ((i / (int)pow(2, s[j][l])) % 2)) % 2; } if (sum != p[j]) { ok = 0; break; } } if (ok == 1) ans++; } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <string> #include <vector> using namespace std; int main() { int n, m; cin >> n >> m; auto k = vector<int>(m); auto s = vector<vector<int>>(m); auto p = vector<int>(m); for (int i = 0; i < m; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { int tmp; cin >> tmp; s[i].push_back(tmp); } } for (int i = 0; i < m; i++) { cin >> p[i]; } int ans = 0; int ok; for (int i = 0; i < (int)pow(2, n); i++) { ok = 1; for (int j = 0; j < m; j++) { int sum = 0; for (int l = 0; l < k[j]; l++) { sum = (sum + ((i / (int)pow(2, s[j][l] - 1)) % 2)) % 2; } if (sum != p[j]) { ok = 0; break; } } if (ok == 1) ans++; } cout << ans << endl; return 0; }
replace
33
35
33
35
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using Graph = vector<vector<int>>; #define ll long long #define _GLIBCXX_DEBUG const ll MOD = 1000000007; const int MAX = 510000; int main() { int N, M; cin >> N >> M; vector<int> k(M), p(M); vector<vector<int>> s(M); for (int i = 0; i < M; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { int num; cin >> num; s[i].push_back(num); } } for (int i = 0; i < M; i++) cin >> p[i]; // スイッチがonになっているとき1,そうでなければ0 int res = 0; for (int tmp = 0; tmp < (1 << 10); tmp++) { bitset<10> b(tmp); bool OK = true; for (int i = 0; i < M; i++) { int num2 = 0; for (int j = 0; j < k[i]; j++) { if (b.test(s[i][j])) num2++; } if (num2 % 2 != p[i]) { OK = false; break; } } if (OK) res++; } res /= pow(2, 10 - N); cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using Graph = vector<vector<int>>; #define ll long long #define _GLIBCXX_DEBUG const ll MOD = 1000000007; const int MAX = 510000; int main() { int N, M; cin >> N >> M; vector<int> k(M), p(M); vector<vector<int>> s(M); for (int i = 0; i < M; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { int num; cin >> num; num--; s[i].push_back(num); } } for (int i = 0; i < M; i++) cin >> p[i]; // スイッチがonになっているとき1,そうでなければ0 int res = 0; for (int tmp = 0; tmp < (1 << 10); tmp++) { bitset<10> b(tmp); bool OK = true; for (int i = 0; i < M; i++) { int num2 = 0; for (int j = 0; j < k[i]; j++) { if (b.test(s[i][j])) num2++; } if (num2 % 2 != p[i]) { OK = false; break; } } if (OK) res++; } res /= pow(2, 10 - N); cout << res << endl; return 0; }
insert
18
18
18
19
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; vector<vector<int>> S(M); for (int i = 0; i < M; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { cin >> S.at(i).at(j); } } vector<int> P(M); for (int i = 0; i < M; i++) cin >> P.at(i); int count = 0; for (int bit; bit < (1 << N); bit++) { bool result = true; for (int i = 0; i < M; i++) { int c = 0; for (int x : S.at(i)) { if (bit & (1 << (x - 1))) c++; } if (!(c % 2) == P.at(i)) { result = false; break; } } if (result) count++; } cout << count << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; vector<vector<int>> S(M); for (int i = 0; i < M; i++) { int k; cin >> k; S.at(i).resize(k); for (int j = 0; j < k; j++) { cin >> S.at(i).at(j); } } vector<int> P(M); for (int i = 0; i < M; i++) cin >> P.at(i); int count = 0; for (int bit; bit < (1 << N); bit++) { bool result = true; for (int i = 0; i < M; i++) { int c = 0; for (int x : S.at(i)) { if (bit & (1 << (x - 1))) c++; } if (!(c % 2) == P.at(i)) { result = false; break; } } if (result) count++; } cout << count << endl; }
insert
10
10
10
11
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
p03031
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, start, end) for (long long i = start; i < end; ++i) #define srt(x) sort((x).begin(), (x).end()); #define rsrt(x) sort((x).rbegin(), (x).rend()); #define deb(x) cout << #x << " = " << (x) << " (L" << LINE << ")" << endl; #define vdeb(x) \ { \ cout << #x << " = { "; \ rep(i, x.size()) cout << x[i] << ' '; \ cout << '}' << " (L" << LINE << ")" << endl; \ } using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vivi = vector<vi>; using vll = vector<long long>; using vllvll = vector<vll>; using vs = vector<string>; using um = unordered_map<long long, long long>; const long long INF = 1LL << 60; const long long MOD = 1e9 + 7; int main() { ll N, M; cin >> N >> M; vllvll s(M, vll(0)); ll tmp; ll k; rep(i, 0, M) { cin >> k; rep(j, 0, k) { cin >> tmp; --tmp; s[i].push_back(tmp); } } vll p(M); rep(i, 0, M) { cin >> p[i]; } ll ans = 0; rep(bit, 0, 1 << N) { // each pattern vll switches(M, 0); rep(i, 0, N) { // filter if (bit & 1 << i) { switches[i] = 1; } } bool flg = true; rep(i, 0, M) { // each bulb ll sm = 0; rep(j, 0, s[i].size()) { // each connected switch sm += switches[s[i][j]]; } if (sm % 2 != p[i]) { flg = false; break; } } if (flg) { ++ans; } } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, start, end) for (long long i = start; i < end; ++i) #define srt(x) sort((x).begin(), (x).end()); #define rsrt(x) sort((x).rbegin(), (x).rend()); #define deb(x) cout << #x << " = " << (x) << " (L" << LINE << ")" << endl; #define vdeb(x) \ { \ cout << #x << " = { "; \ rep(i, x.size()) cout << x[i] << ' '; \ cout << '}' << " (L" << LINE << ")" << endl; \ } using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vivi = vector<vi>; using vll = vector<long long>; using vllvll = vector<vll>; using vs = vector<string>; using um = unordered_map<long long, long long>; const long long INF = 1LL << 60; const long long MOD = 1e9 + 7; int main() { ll N, M; cin >> N >> M; vllvll s(M, vll(0)); ll tmp; ll k; rep(i, 0, M) { cin >> k; rep(j, 0, k) { cin >> tmp; --tmp; s[i].push_back(tmp); } } vll p(M); rep(i, 0, M) { cin >> p[i]; } ll ans = 0; rep(bit, 0, 1 << N) { // each pattern vll switches(N, 0); rep(i, 0, N) { // filter if (bit & 1 << i) { switches[i] = 1; } } bool flg = true; rep(i, 0, M) { // each bulb ll sm = 0; rep(j, 0, s[i].size()) { // each connected switch sm += switches[s[i][j]]; } if (sm % 2 != p[i]) { flg = false; break; } } if (flg) { ++ans; } } cout << ans << endl; }
replace
42
43
42
43
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> #define ff first #define ss second #define ll long long #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { #ifndef ONLINE_JUDGE freopen("inp.txt", "r", stdin); freopen("outp.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, ans = 0; cin >> n >> m; vector<vector<int>> light(m); vector<int> num(m), prt(m); for (int i = 0; i < m; i++) { cin >> num[i]; for (int j = 0; j < num[i]; j++) { int temp; cin >> temp; light[i].push_back(temp); } } for (int i = 0; i < m; i++) { cin >> prt[i]; } for (int x = 0; x < (1 << n); x++) { bool jury = true; for (int i = 0; i < m; i++) { int cnt = 0; for (int j = 0; j < num[i]; j++) { if (x & (1 << (light[i][j] - 1))) { cnt++; } } if (cnt % 2 != prt[i]) { jury = false; break; } } if (jury) { ans++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ff first #define ss second #define ll long long #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, ans = 0; cin >> n >> m; vector<vector<int>> light(m); vector<int> num(m), prt(m); for (int i = 0; i < m; i++) { cin >> num[i]; for (int j = 0; j < num[i]; j++) { int temp; cin >> temp; light[i].push_back(temp); } } for (int i = 0; i < m; i++) { cin >> prt[i]; } for (int x = 0; x < (1 << n); x++) { bool jury = true; for (int i = 0; i < m; i++) { int cnt = 0; for (int j = 0; j < num[i]; j++) { if (x & (1 << (light[i][j] - 1))) { cnt++; } } if (cnt % 2 != prt[i]) { jury = false; break; } } if (jury) { ans++; } } cout << ans << endl; return 0; }
replace
8
12
8
9
-6
terminate called after throwing an instance of 'std::length_error' what(): cannot create std::vector larger than max_size()
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> cont(n); for (int i = 0; i < m; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s; cin >> s; s--; cont.at(i) |= 1 << s; } } int p = 0; for (int i = 0; i < m; i++) { int s; cin >> s; p |= s << i; } int ans = 0; for (int i = 0; i < (1 << n); i++) { int t = 0; for (int j = 0; j < n; j++) { if (i >> j & 1) { t ^= cont.at(j); } } if (t == p) { ans++; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> cont(n); for (int i = 0; i < m; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s; cin >> s; s--; cont.at(s) |= 1 << i; } } int p = 0; for (int i = 0; i < m; i++) { int s; cin >> s; p |= s << i; } int ans = 0; for (int i = 0; i < (1 << n); i++) { int t = 0; for (int j = 0; j < n; j++) { if (i >> j & 1) { t ^= cont.at(j); } } if (t == p) { ans++; } } cout << ans << endl; }
replace
16
17
16
17
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; const ll INF = 1LL << 60; const ll MOD = 1e9 + 7; int main() { int N, M; cin >> N >> M; vector<int> k(M); vector<vector<int>> s(N); for (int i = 0; i < M; i++) { cin >> k[i]; s[i].resize(k[i]); for (int j = 0; j < k[i]; j++) { cin >> s[i][j]; } } vector<int> p(M); for (int i = 0; i < M; i++) cin >> p[i]; int ans = 0; for (int bits = 0; bits < (1 << N); bits++) { vector<bool> s_on(N, false); bool ok = true; for (int i = 0; i < N; i++) { if ((bits >> i) & 1) { s_on[i] = true; } } for (int i = 0; i < M; i++) { int sum = 0; for (int j = 0; j < k[i]; j++) { if (s_on[s[i][j] - 1]) sum++; } if (sum % 2 != p[i]) ok = false; } if (ok) ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; const ll INF = 1LL << 60; const ll MOD = 1e9 + 7; int main() { int N, M; cin >> N >> M; vector<int> k(M); vector<vector<int>> s(M); for (int i = 0; i < M; i++) { cin >> k[i]; s[i].resize(k[i]); for (int j = 0; j < k[i]; j++) { cin >> s[i][j]; } } vector<int> p(M); for (int i = 0; i < M; i++) cin >> p[i]; int ans = 0; for (int bits = 0; bits < (1 << N); bits++) { vector<bool> s_on(N, false); bool ok = true; for (int i = 0; i < N; i++) { if ((bits >> i) & 1) { s_on[i] = true; } } for (int i = 0; i < M; i++) { int sum = 0; for (int j = 0; j < k[i]; j++) { if (s_on[s[i][j] - 1]) sum++; } if (sum % 2 != p[i]) ok = false; } if (ok) ans++; } cout << ans << endl; return 0; }
replace
12
13
12
13
0
p03031
C++
Runtime Error
// wawando's template #include <algorithm> #include <cctype> #include <climits> // LLONG_MAX , LLONG_MIN , INT_MAX , INT_MIN #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> //clock_t , clock() , CLOCKS_PER_SEC #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> // MACROS #define pb push_back #define mp make_pair #define INF 1000000000 // 1 billion safer for floyd warshall, avoid overflow using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<ii> vii; int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); ios::sync_with_stdio(false); cin.tie(0); int N, M; vector<vi> bulbs; vi p; cin >> N >> M; bulbs.assign(N, vi()); for (int i = 0; i < M; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s; cin >> s; bulbs[i].pb(s - 1); } } for (int i = 0; i < M; i++) { int pi; cin >> pi; p.pb(pi); } int ans = 0; for (int mask = 0; mask < (1 << N); mask++) { bool isOn = true; for (int i = 0; i < M; i++) { int onCount = 0; for (int j = 0; j < (int)bulbs[i].size(); j++) { if (mask & (1 << bulbs[i][j])) onCount++; } isOn &= (onCount % 2 == p[i]); } ans += (isOn ? 1 : 0); } cout << ans << endl; return 0; }
// wawando's template #include <algorithm> #include <cctype> #include <climits> // LLONG_MAX , LLONG_MIN , INT_MAX , INT_MIN #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> //clock_t , clock() , CLOCKS_PER_SEC #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> // MACROS #define pb push_back #define mp make_pair #define INF 1000000000 // 1 billion safer for floyd warshall, avoid overflow using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<ii> vii; int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); ios::sync_with_stdio(false); cin.tie(0); int N, M; vector<vi> bulbs; vi p; cin >> N >> M; bulbs.assign(M, vi()); for (int i = 0; i < M; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s; cin >> s; bulbs[i].pb(s - 1); } } for (int i = 0; i < M; i++) { int pi; cin >> pi; p.pb(pi); } int ans = 0; for (int mask = 0; mask < (1 << N); mask++) { bool isOn = true; for (int i = 0; i < M; i++) { int onCount = 0; for (int j = 0; j < (int)bulbs[i].size(); j++) { if (mask & (1 << bulbs[i][j])) onCount++; } isOn &= (onCount % 2 == p[i]); } ans += (isOn ? 1 : 0); } cout << ans << endl; return 0; }
replace
47
48
47
48
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int N, M; cin >> N >> M; vector<int> k(M); vector<vector<int>> s(M); vector<int> p(M); for (int i = 0; i < M; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { cin >> s[i][j]; s[i][j]--; } } for (int i = 0; i < M; i++) cin >> p[i]; ll ans = 0; for (int bit = 0; bit < (1 << N); bit++) { bool ok = true; for (int i = 0; i < M; i++) { int sum = 0; for (auto v : s[i]) { if ((1 << v) & bit) sum++; } if (sum % 2 != p[i]) ok = false; } if (ok) ans += 1; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int N, M; cin >> N >> M; vector<int> k(M); vector<vector<int>> s(M); vector<int> p(M); for (int i = 0; i < M; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { int a; cin >> a; a--; s[i].push_back(a); } } for (int i = 0; i < M; i++) cin >> p[i]; ll ans = 0; for (int bit = 0; bit < (1 << N); bit++) { bool ok = true; for (int i = 0; i < M; i++) { int sum = 0; for (auto v : s[i]) { if ((1 << v) & bit) sum++; } if (sum % 2 != p[i]) ok = false; } if (ok) ans += 1; } cout << ans; return 0; }
replace
15
17
15
19
-11
p03031
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define pii_ pair<int, int> #define mp_ make_pair #define pb push_back #define fi first #define se second #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (a); i >= (b); i--) #define show1(a) cout << #a << " = " << a << endl #define show2(a, b) cout << #a << " = " << a << "; " << #b << " = " << b << endl using namespace std; const ll INF = 1LL << 60; const int inf = 1 << 30; const int maxn = 2e5 + 5; inline void fastio() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } int n, m, g[15][15], p[15], cnt[15]; int main() { fastio(); cin >> n >> m; rep(i, 0, m - 1) { int k, x; cin >> k; rep(j, 1, k) { cin >> x; g[--x][i] = 1; } } rep(i, 0, m - 1) cin >> p[i]; int ans = 0; rep(i, 0, (1 << n) - 1) { memset(cnt, 0, sizeof(cnt)); rep(j, 0, i - 1) { if ((i >> j) & 1) { rep(k, 0, m - 1) { if (g[j][k]) cnt[k]++; } } } int flag = 1; rep(i, 0, m - 1) if (cnt[i] % 2 != p[i]) flag = 0; ans += flag; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define pii_ pair<int, int> #define mp_ make_pair #define pb push_back #define fi first #define se second #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (a); i >= (b); i--) #define show1(a) cout << #a << " = " << a << endl #define show2(a, b) cout << #a << " = " << a << "; " << #b << " = " << b << endl using namespace std; const ll INF = 1LL << 60; const int inf = 1 << 30; const int maxn = 2e5 + 5; inline void fastio() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } int n, m, g[15][15], p[15], cnt[15]; int main() { fastio(); cin >> n >> m; rep(i, 0, m - 1) { int k, x; cin >> k; rep(j, 1, k) { cin >> x; g[--x][i] = 1; } } rep(i, 0, m - 1) cin >> p[i]; int ans = 0; rep(i, 0, (1 << n) - 1) { memset(cnt, 0, sizeof(cnt)); rep(j, 0, n - 1) { if ((i >> j) & 1) { rep(k, 0, m - 1) { if (g[j][k]) cnt[k]++; } } } int flag = 1; rep(i, 0, m - 1) if (cnt[i] % 2 != p[i]) flag = 0; ans += flag; } cout << ans << endl; return 0; }
replace
38
39
38
39
0
p03031
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main() { int N, M; cin >> N >> M; vector<vector<int>> s(M); for (int i = 0; i < M; ++i) { int k; cin >> k; s[i].resize(k); for (int j = 0; j < k; ++j) { cin >> s[i][j]; --s[i][j]; } } vector<int> p(M); for (int i = 1; i <= M; ++i) { cin >> p[i]; } int ans = 0; for (int i = 0; i < (1 << N); ++i) { bool flg = true; for (int m = 0; m < M; ++m) { int cnt = 0; for (auto sw : s[m]) { if ((i >> sw) & 1) ++cnt; } if (cnt % 2 != p[m]) flg = false; } if (flg) ++ans; } cout << ans << endl; return 0; }
#include <iostream> #include <vector> using namespace std; int main() { int N, M; cin >> N >> M; vector<vector<int>> s(M); for (int i = 0; i < M; ++i) { int k; cin >> k; s[i].resize(k); for (int j = 0; j < k; ++j) { cin >> s[i][j]; --s[i][j]; } } vector<int> p(M); for (int i = 0; i < M; ++i) { cin >> p[i]; } int ans = 0; for (int i = 0; i < (1 << N); ++i) { bool flg = true; for (int m = 0; m < M; ++m) { int cnt = 0; for (auto sw : s[m]) { if ((i >> sw) & 1) ++cnt; } if (cnt % 2 != p[m]) flg = false; } if (flg) ++ans; } cout << ans << endl; return 0; }
replace
22
23
22
23
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int>> s(n); for (int i = 0; i < m; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s_in; cin >> s_in; s[s_in - 1].push_back(j); } } vector<int> p(m); for (int i = 0; i < m; i++) { cin >> p[i]; } int ans = 0; for (int k = 0; k < (1 << n); k++) { vector<int> o(m, 0); for (int i = 0; i < n; i++) { if (k & (1 << i)) { for (auto itr = s[i].begin(); itr != s[i].end(); itr++) { o[*itr]++; } } } bool flg = true; for (int i = 0; i < m; i++) { if (o[i] % 2 != p[i] % 2) { flg = false; break; } } if (flg) { ans++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int>> s(n); for (int i = 0; i < m; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s_in; cin >> s_in; s[s_in - 1].push_back(i); } } vector<int> p(m); for (int i = 0; i < m; i++) { cin >> p[i]; } int ans = 0; for (int k = 0; k < (1 << n); k++) { vector<int> o(m, 0); for (int i = 0; i < n; i++) { if (k & (1 << i)) { for (auto itr = s[i].begin(); itr != s[i].end(); itr++) { o[*itr]++; } } } bool flg = true; for (int i = 0; i < m; i++) { if (o[i] % 2 != p[i] % 2) { flg = false; break; } } if (flg) { ans++; } } cout << ans << endl; return 0; }
replace
12
13
12
13
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main(void) { int n, m, ans; cin >> n >> m; vector<vector<int>> ks(m); for (int i = 0; i < m; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { cin >> ks[i][j]; ks[i][j]--; } } vector<int> p(m); for (int i = 0; i < m; i++) cin >> p[i]; for (int i = 0; i < (1 << n); i++) { bool ok = true; for (int j = 0; j < m; j++) { int count = 0; for (int num : ks[j]) { if ((i >> num) & 1) count++; } count %= 2; if (count != p[j]) ok = false; } if (ok) ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int n, m, ans; cin >> n >> m; vector<vector<int>> ks(m); for (int i = 0; i < m; i++) { int k; cin >> k; ks[i].resize(k); for (int j = 0; j < k; j++) { cin >> ks[i][j]; ks[i][j]--; } } vector<int> p(m); for (int i = 0; i < m; i++) cin >> p[i]; for (int i = 0; i < (1 << n); i++) { bool ok = true; for (int j = 0; j < m; j++) { int count = 0; for (int num : ks[j]) { if ((i >> num) & 1) count++; } count %= 2; if (count != p[j]) ok = false; } if (ok) ans++; } cout << ans << endl; return 0; }
insert
9
9
9
10
-11
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int k[20], s[20][20], p[20]; for (int i = 0; i < m; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { cin >> s[i][j]; } } for (int i = 0; i < m; i++) cin >> p[i]; int ans = 0; for (int bit = 0; bit < (1 << n); bit++) { int light = 0; for (int i = 0; i < n; i++) { int cnt = 0; for (int j = 0; j < k[i]; j++) { if (bit & (1 << s[i][j] - 1)) cnt++; } if (p[i] == 0 && (cnt % 2 == 0)) light++; if (p[i] == 1 && (cnt % 2 != 0)) light++; } if (light == m) ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int k[20], s[20][20], p[20]; for (int i = 0; i < m; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { cin >> s[i][j]; } } for (int i = 0; i < m; i++) cin >> p[i]; int ans = 0; for (int bit = 0; bit < (1 << n); bit++) { int light = 0; for (int i = 0; i < m; i++) { int cnt = 0; for (int j = 0; j < k[i]; j++) { if (bit & (1 << s[i][j] - 1)) cnt++; } if (p[i] == 0 && (cnt % 2 == 0)) light++; if (p[i] == 1 && (cnt % 2 != 0)) light++; } if (light == m) ans++; } cout << ans << endl; return 0; }
replace
18
19
18
19
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); ++i) #define all(a) (a).begin(), (a).end() using namespace std; using Graph = vector<vector<int>>; typedef pair<int, int> P; typedef long long ll; int main() { int n, m; cin >> n >> m; vector<vector<bool>> g(n, vector<bool>(m, false)); rep(i, m) { int k; cin >> k; rep(j, k) { int s; cin >> s; s--; g[i][s] = true; } } vector<int> p(m); rep(i, m) cin >> p[i]; ll ans = 0; for (int bit = 0; bit < (1 << n); bit++) { vector<int> a(m, 0); rep(i, n) { if (bit & (1 << i)) { rep(j, m) { if (g[i][j]) a[j] ^= 1; } } } bool jud = true; rep(i, m) { if (p[i] != a[i]) jud = false; } if (jud) ans++; } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); ++i) #define all(a) (a).begin(), (a).end() using namespace std; using Graph = vector<vector<int>>; typedef pair<int, int> P; typedef long long ll; int main() { int n, m; cin >> n >> m; vector<vector<bool>> g(n, vector<bool>(m, false)); rep(i, m) { int k; cin >> k; rep(j, k) { int s; cin >> s; s--; g[s][i] = true; } } vector<int> p(m); rep(i, m) cin >> p[i]; ll ans = 0; for (int bit = 0; bit < (1 << n); bit++) { vector<int> a(m, 0); rep(i, n) { if (bit & (1 << i)) { rep(j, m) { if (g[i][j]) a[j] ^= 1; } } } bool jud = true; rep(i, m) { if (p[i] != a[i]) jud = false; } if (jud) ans++; } cout << ans << endl; }
replace
20
21
20
21
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> #include <cmath> #include <map> #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define s(x) (x).begin(), (x).end() using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<long long> vl; int main() { int n, m; cin >> n >> m; vector<vl> a(m); rep(i, m) { int k; cin >> k; rep(j, k) { int s; cin >> s; a[n].push_back(s); } } vl p(m); rep(i, m) cin >> p[i]; int ans = 0; for (int bit = 0; bit < (1 << n); bit++) { bool f = true; rep(i, m) { int count = 0; for (auto v : a[i]) { if (bit & (1 << v)) count++; } if (count % 2 != p[i]) f = false; } if (f) ans++; } cout << ans << endl; }
#include <bits/stdc++.h> #include <cmath> #include <map> #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define s(x) (x).begin(), (x).end() using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<long long> vl; int main() { int n, m; cin >> n >> m; vector<vl> a(m); rep(i, m) { int k; cin >> k; rep(j, k) { int s; cin >> s; s--; a[i].push_back(s); } } vl p(m); rep(i, m) cin >> p[i]; int ans = 0; for (int bit = 0; bit < (1 << n); bit++) { bool f = true; rep(i, m) { int count = 0; for (auto v : a[i]) { if (bit & (1 << v)) count++; } if (count % 2 != p[i]) f = false; } if (f) ans++; } cout << ans << endl; }
replace
19
20
19
21
-11
p03031
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <iostream> #include <string> using namespace std; int MOD = 1000000007; int main() { int N, M, k, ans = 0, c = 0; cin >> N >> M; vector<vector<int>> s(M + 10); for (int i = 0; i < M; i++) { cin >> k; for (int j = 0; j < k; j++) { cin >> s[i][j]; s[i][j]--; } } vector<int> p(M + 10); for (int i = 0; i < M; ++i) cin >> p[i]; for (int i = 0; i < (1 << N); i++) { bool ok = true; for (int j = 0; j < M; j++) { c = 0; for (int id : s[j]) { if ((i >> id) & 1) { c++; } } c %= 2; if (c != p[j]) { ok = false; } } if (ok) { ans++; } } cout << ans << endl; }
#include <algorithm> #include <bits/stdc++.h> #include <iostream> #include <string> using namespace std; int MOD = 1000000007; int main() { int N, M, k, ans = 0, c = 0; cin >> N >> M; vector<vector<int>> s(M + 10); for (int i = 0; i < M; i++) { cin >> k; s[i].resize(k); for (int j = 0; j < k; j++) { cin >> s[i][j]; s[i][j]--; } } vector<int> p(M + 10); for (int i = 0; i < M; ++i) cin >> p[i]; for (int i = 0; i < (1 << N); i++) { bool ok = true; for (int j = 0; j < M; j++) { c = 0; for (int id : s[j]) { if ((i >> id) & 1) { c++; } } c %= 2; if (c != p[j]) { ok = false; } } if (ok) { ans++; } } cout << ans << endl; }
insert
15
15
15
16
-11
p03031
C++
Runtime Error
#include <bits/stdc++.h> #include <bitset> using namespace std; int main() { int N, M; cin >> N >> M; vector<long long> Bit(M, 0); vector<long long> mod(M, 0); for (int i = 0; i < N; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s; cin >> s; Bit[i] ^= 1 << (s - 1); } } for (int i = 0; i < M; i++) cin >> mod[i]; long long ans = 0; for (long long i = 0; i < (1 << N); i++) { bool flg = false; // cout << bitset<3>(i) << "\n"; for (int j = 0; j < M; j++) { // cout << j << ' ' << __builtin_popcount(Bit[j]&i)%2 << ' ' << // mod[j] << "\n"; if (__builtin_popcountll(Bit[j] & i) % 2 != mod[j]) { flg = true; break; } } if (!flg) ans++; } cout << ans << "\n"; // for (int i=0; i<M; i++) { // cout << bitset<3>(Bit[i]) << "\n"; // } }
#include <bits/stdc++.h> #include <bitset> using namespace std; int main() { int N, M; cin >> N >> M; vector<long long> Bit(M, 0); vector<long long> mod(M, 0); for (int i = 0; i < M; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s; cin >> s; Bit[i] ^= 1 << (s - 1); } } for (int i = 0; i < M; i++) cin >> mod[i]; long long ans = 0; for (long long i = 0; i < (1 << N); i++) { bool flg = false; // cout << bitset<3>(i) << "\n"; for (int j = 0; j < M; j++) { // cout << j << ' ' << __builtin_popcount(Bit[j]&i)%2 << ' ' << // mod[j] << "\n"; if (__builtin_popcountll(Bit[j] & i) % 2 != mod[j]) { flg = true; break; } } if (!flg) ans++; } cout << ans << "\n"; // for (int i=0; i<M; i++) { // cout << bitset<3>(Bit[i]) << "\n"; // } }
replace
9
10
9
10
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { unsigned int n, m; cin >> n >> m; vector<vector<int>> s(m); for (int i = 0; i < m; ++i) { int k; cin >> k; for (int j = 0; j < k; ++j) { int a; cin >> a; a--; s[i].emplace_back(a); } } vector<int> p(m); for (int i = 0; i < m; ++i) { cin >> p[i]; } auto ans = 0L; for (unsigned int bit = 0; bit < (1U << n); ++bit) { bool isOK = true; // 電球iがついているかどうか for (int i = 0; i < n; ++i) { int cnt = 0; for (unsigned int v : s[i]) { if (bit & (1U << v)) ++cnt; } if (cnt % 2 != p[i]) isOK = false; } if (isOK) ++ans; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { unsigned int n, m; cin >> n >> m; vector<vector<int>> s(m); for (int i = 0; i < m; ++i) { int k; cin >> k; for (int j = 0; j < k; ++j) { int a; cin >> a; a--; s[i].emplace_back(a); } } vector<int> p(m); for (int i = 0; i < m; ++i) { cin >> p[i]; } auto ans = 0L; for (unsigned int bit = 0; bit < (1U << n); ++bit) { bool isOK = true; // 電球iがついているかどうか for (int i = 0; i < m; ++i) { int cnt = 0; for (unsigned int v : s[i]) { if (bit & (1U << v)) ++cnt; } if (cnt % 2 != p[i]) isOK = false; } if (isOK) ++ans; } cout << ans << endl; return 0; }
replace
29
30
29
30
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define MODV 1000000007 #define INF INT_MAX // 2147483647 #define INFLL LLONG_MAX // 9223372036854775807 #define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++) #define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++) #define per(i, n) for (ll i = ((ll)(n)) - 1; i >= 0; i--) #define perf(i, n) for (ll i = ((ll)(n)); i > 0; i--) #define all(v) v.begin(), v.end() #define endl "\n" #define vi vector<ll> #define vvi vector<vector<ll>> #define Yes() cout << "Yes" << endl #define YES() cout << "YES" << endl #define No() cout << "No" << endl #define NO() cout << "NO" << endl 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; } using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(10); ll n, m, ans = 0; cin >> n >> m; vi k(n), p(m); vvi s(n, vi()); // 入力処理 rep(i, m) { cin >> k[i]; s[i].resize(k[i]); rep(j, k[i]) { cin >> s[i][j]; s[i][j]--; } } rep(i, m) cin >> p[i]; // ビット全探索 for (ll bit = 0; bit < (1LL << (n)); bit++) { vector<bool> sw(n, false); vector<bool> light(m, false); rep(j, n) { ll mask = (1LL << j); if (bit & mask) sw[j] = true; } rep(i, m) { ll cnt = 0; rep(j, k[i]) if (sw[s[i][j]]) cnt++; if (cnt % 2 == p[i]) light[i] = true; } bool tmp = 1; rep(i, m) tmp = tmp & light[i]; ans += tmp; } // 答えの出力 cout << ans << endl; }
#include <bits/stdc++.h> #define ll long long #define MODV 1000000007 #define INF INT_MAX // 2147483647 #define INFLL LLONG_MAX // 9223372036854775807 #define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++) #define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++) #define per(i, n) for (ll i = ((ll)(n)) - 1; i >= 0; i--) #define perf(i, n) for (ll i = ((ll)(n)); i > 0; i--) #define all(v) v.begin(), v.end() #define endl "\n" #define vi vector<ll> #define vvi vector<vector<ll>> #define Yes() cout << "Yes" << endl #define YES() cout << "YES" << endl #define No() cout << "No" << endl #define NO() cout << "NO" << endl 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; } using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(10); ll n, m, ans = 0; cin >> n >> m; vi k(n), p(m); vvi s(m, vi()); // 入力処理 rep(i, m) { cin >> k[i]; s[i].resize(k[i]); rep(j, k[i]) { cin >> s[i][j]; s[i][j]--; } } rep(i, m) cin >> p[i]; // ビット全探索 for (ll bit = 0; bit < (1LL << (n)); bit++) { vector<bool> sw(n, false); vector<bool> light(m, false); rep(j, n) { ll mask = (1LL << j); if (bit & mask) sw[j] = true; } rep(i, m) { ll cnt = 0; rep(j, k[i]) if (sw[s[i][j]]) cnt++; if (cnt % 2 == p[i]) light[i] = true; } bool tmp = 1; rep(i, m) tmp = tmp & light[i]; ans += tmp; } // 答えの出力 cout << ans << endl; }
replace
40
41
40
41
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define uint unsigned int #define ld long double #define showoff \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define pii pair<int, int> #define FOR(i, a, b) for (int i = a; i < b; ++i) #define RFOR(i, a, b) for (int i = a; i > b; --i) #define f first #define se second #define maxn 200005 #define all(v) v.begin(), v.end() #define sz(x) (int)x.size() #define mod 1000000007 #define pqueue priority_queue<int> #define pdqueue priority_queue<int, vector<int>, greater<int>> using namespace std; // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int power(int a, int n) { if (n == 1) return a; if (n == 0) return 1; if (n % 2) return a * power(a * a, n / 2); return power(a * a, n / 2); } const int md = 998244353; const int inf = (int)1e18; /*int inverse(int x){ return power(x,mod-2)%mod;//little fermat.... }*/ signed main() { showoff; /*int n,ans[maxn]; cin >> n; vector<pair<pair<string,int>,int> >v; FOR(i,1,n+1){ int sc; string s; cin >> s >> sc; v.pb({{s,-sc},i}); } sort(v.begin(),v.end()); FOR(i,0,n)ans[v[i].se] = i+1; FOR(i,1,n+1)cout << ans[i] << "\n";*/ int n, m, p[100]; cin >> n >> m; vector<int> adj[m + 5]; FOR(i, 1, m + 1) { int k; cin >> k; FOR(i, 1, k + 1) { int x; cin >> x; adj[i].pb(x - 1); } } int ans = 0; FOR(i, 1, m + 1) cin >> p[i]; FOR(i, 0, (1 << n)) { bool fo = 1; FOR(j, 1, m + 1) { int c = 0; for (auto &x : adj[j]) { if (i & (1 << x)) ++c; } if (c % 2 != p[j] % 2) fo = 0; } if (fo) ++ans; } cout << ans; return 0; } //*->for large size of matrix take int not long long if possible...... //*->always take maximum as inf for safer side ...
#include <bits/stdc++.h> #define int long long #define uint unsigned int #define ld long double #define showoff \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define pii pair<int, int> #define FOR(i, a, b) for (int i = a; i < b; ++i) #define RFOR(i, a, b) for (int i = a; i > b; --i) #define f first #define se second #define maxn 200005 #define all(v) v.begin(), v.end() #define sz(x) (int)x.size() #define mod 1000000007 #define pqueue priority_queue<int> #define pdqueue priority_queue<int, vector<int>, greater<int>> using namespace std; // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int power(int a, int n) { if (n == 1) return a; if (n == 0) return 1; if (n % 2) return a * power(a * a, n / 2); return power(a * a, n / 2); } const int md = 998244353; const int inf = (int)1e18; /*int inverse(int x){ return power(x,mod-2)%mod;//little fermat.... }*/ signed main() { showoff; /*int n,ans[maxn]; cin >> n; vector<pair<pair<string,int>,int> >v; FOR(i,1,n+1){ int sc; string s; cin >> s >> sc; v.pb({{s,-sc},i}); } sort(v.begin(),v.end()); FOR(i,0,n)ans[v[i].se] = i+1; FOR(i,1,n+1)cout << ans[i] << "\n";*/ int n, m, p[100]; cin >> n >> m; vector<int> adj[m + 5]; FOR(i, 1, m + 1) { int k; cin >> k; FOR(j, 1, k + 1) { int x; cin >> x; adj[i].pb(x - 1); } } int ans = 0; FOR(i, 1, m + 1) cin >> p[i]; FOR(i, 0, (1 << n)) { bool fo = 1; FOR(j, 1, m + 1) { int c = 0; for (auto &x : adj[j]) { if (i & (1 << x)) ++c; } if (c % 2 != p[j] % 2) fo = 0; } if (fo) ++ans; } cout << ans; return 0; } //*->for large size of matrix take int not long long if possible...... //*->always take maximum as inf for safer side ...
replace
63
64
63
64
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double D; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef long double ld; #define rd(x) scanf("%d", &x) #define rd2(x, y) scanf("%d %d", &x, &y) #define rd3(x, y, z) scanf("%d %d %d", &x, &y, &z) #define rl(x) scanf("%lld", &x) #define rl2(x, y) scanf("%lld %lld", &x, &y) #define rl3(x, y, z) scanf("%lld %lld %lld", &x, &y, &z) #define wd(x) printf("%d", x) #define wd2(x, y) printf("%d %d", x, y) #define wl(x) printf("%lld", x) #define wl2(x, y) printf("%lld %lld", x, y) #define PC(x) putchar(x) #define GC() getchar() #define NL printf("\n") #define SP printf(" ") #define SZ(a) (int)a.size() #define ALL(a) a.begin(), a.end() #define ITER(it, a) \ for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++) #define FILL(a, value) memset(a, value, sizeof(a)) #define F first #define S second #define MP make_pair #define PB push_back #define SPEED \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define fast_cin() \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) #define fr(i, s, e) for (int i = s; i < e; i++) #define frl(i, s, e) for (ll i = s; i < e; i++) #define frr(i, s, e) for (int i = s - 1; i >= e; i--) #define frv(i, a) for (int i = 0; i < (int)a.size(); i++) #define frvr(i, a) for (int i = a.size() - 1; i >= 0; i--) #define tr(i, a) for (typeof(a.begin()) i = a.begin(); i != a.end(); i++) #define inf 10000000000000 #define mod 1000000007 #define maxn 100009 #define maxr 100009 map<ll, VL> mp; void solve() { ll n, m; rl2(n, m); fr(i, 0, m) { ll k; rl(k); fr(j, 0, k) { ll s; rl(s); mp[s - 1].PB(j); } } VL p(m); fr(i, 0, m) rl(p[i]); ll pset = pow(2, n); ll ct = 0; fr(i, 0, pset) { VL b(m, 0); fr(j, 0, n) { if (i & (1 << j)) { VL tmp = mp[j]; fr(x, 0, SZ(tmp)) { b[tmp[x]]++; } } } ll flag = 1; fr(j, 0, m) { if (p[j] != (b[j] % 2)) flag = 0; } if (flag) ct++; } wl(ct); NL; return; } int main() { // sstd::ios::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double D; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef long double ld; #define rd(x) scanf("%d", &x) #define rd2(x, y) scanf("%d %d", &x, &y) #define rd3(x, y, z) scanf("%d %d %d", &x, &y, &z) #define rl(x) scanf("%lld", &x) #define rl2(x, y) scanf("%lld %lld", &x, &y) #define rl3(x, y, z) scanf("%lld %lld %lld", &x, &y, &z) #define wd(x) printf("%d", x) #define wd2(x, y) printf("%d %d", x, y) #define wl(x) printf("%lld", x) #define wl2(x, y) printf("%lld %lld", x, y) #define PC(x) putchar(x) #define GC() getchar() #define NL printf("\n") #define SP printf(" ") #define SZ(a) (int)a.size() #define ALL(a) a.begin(), a.end() #define ITER(it, a) \ for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++) #define FILL(a, value) memset(a, value, sizeof(a)) #define F first #define S second #define MP make_pair #define PB push_back #define SPEED \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define fast_cin() \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) #define fr(i, s, e) for (int i = s; i < e; i++) #define frl(i, s, e) for (ll i = s; i < e; i++) #define frr(i, s, e) for (int i = s - 1; i >= e; i--) #define frv(i, a) for (int i = 0; i < (int)a.size(); i++) #define frvr(i, a) for (int i = a.size() - 1; i >= 0; i--) #define tr(i, a) for (typeof(a.begin()) i = a.begin(); i != a.end(); i++) #define inf 10000000000000 #define mod 1000000007 #define maxn 100009 #define maxr 100009 map<ll, VL> mp; void solve() { ll n, m; rl2(n, m); fr(i, 0, m) { ll k; rl(k); fr(j, 0, k) { ll s; rl(s); mp[s - 1].PB(i); } } VL p(m); fr(i, 0, m) rl(p[i]); ll pset = pow(2, n); ll ct = 0; fr(i, 0, pset) { VL b(m, 0); fr(j, 0, n) { if (i & (1 << j)) { VL tmp = mp[j]; fr(x, 0, SZ(tmp)) { b[tmp[x]]++; } } } ll flag = 1; fr(j, 0, m) { if (p[j] != (b[j] % 2)) flag = 0; } if (flag) ct++; } wl(ct); NL; return; } int main() { // sstd::ios::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
replace
69
70
69
70
0
p03031
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, n) for (int i = 1; i < (int)(n); ++i) using namespace std; int main() { int N, M; cin >> N >> M; vector<vector<int>> S(N); rep(i, M) { int k; cin >> k; rep(i, k) { int s; cin >> s; S[s - 1].push_back(i); } } vector<int> p(M); rep(i, M) cin >> p[i]; int cnt = 0; for (int bit = 0; bit < (1 << N); ++bit) { vector<int> X; rep(i, N) { if (bit & (1 << i)) X.push_back(i); } vector<int> P(M, 0); for (int x : X) { for (int y : S[x]) ++P[y]; } rep(i, M) P[i] %= 2; bool judge = true; rep(i, M) { if (p[i] != P[i]) judge = false; } if (judge) ++cnt; } cout << cnt << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, n) for (int i = 1; i < (int)(n); ++i) using namespace std; int main() { int N, M; cin >> N >> M; vector<vector<int>> S(N); rep(i, M) { int k; cin >> k; rep(j, k) { int s; cin >> s; S[s - 1].push_back(i); } } vector<int> p(M); rep(i, M) cin >> p[i]; int cnt = 0; for (int bit = 0; bit < (1 << N); ++bit) { vector<int> X; rep(i, N) { if (bit & (1 << i)) X.push_back(i); } vector<int> P(M, 0); for (int x : X) { for (int y : S[x]) ++P[y]; } rep(i, M) P[i] %= 2; bool judge = true; rep(i, M) { if (p[i] != P[i]) judge = false; } if (judge) ++cnt; } cout << cnt << endl; return 0; }
replace
14
15
14
15
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, M, A = 0; cin >> N >> M; vector<int> k(M), p(M); vector<vector<int>> s(M, vector<int>(N)); for (int i = 0; i < M; i++) { cin >> k.at(i); for (int j = 0; j < k.at(i); j++) cin >> s.at(i).at(j); } for (int i = 0; i < M; i++) cin >> p.at(i); for (int i = 0; i < (1 << 10); i++) { bitset<10> x(i); if (x.test(N)) break; int T = 0; for (int j = 0; j < M; j++) { int S = 0; for (int a = 0; a < k.at(j); a++) if (x.test(s.at(j).at(a) - 1)) S++; if (S % 2 == p.at(j)) T++; } if (T == M) A++; } cout << A << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, M, A = 0; cin >> N >> M; vector<int> k(M), p(M); vector<vector<int>> s(M, vector<int>(N)); for (int i = 0; i < M; i++) { cin >> k.at(i); for (int j = 0; j < k.at(i); j++) cin >> s.at(i).at(j); } for (int i = 0; i < M; i++) cin >> p.at(i); for (int i = 0; i < (1 << 10); i++) { bitset<10> x(i); if (N < 10 && x.test(N)) break; int T = 0; for (int j = 0; j < M; j++) { int S = 0; for (int a = 0; a < k.at(j); a++) if (x.test(s.at(j).at(a) - 1)) S++; if (S % 2 == p.at(j)) T++; } if (T == M) A++; } cout << A << endl; }
replace
16
17
16
17
0
p03031
Python
Runtime Error
import itertools N, M = map(int, input().split()) connected_switches = [[int(x) - 1 for x in input().split()[1:]] for x in range(M)] conditions = list(map(int, input().split())) ok = 0 for patterns in itertools.product((0, 1), repeat=M): patterns = list(patterns) is_lighting = True for i, lights in enumerate(connected_switches): turned_sw = sum([patterns[x] for x in lights]) if turned_sw % 2 != conditions[i]: is_lighting = False break if is_lighting: ok += 1 print(ok)
import itertools N, M = map(int, input().split()) connected_switches = [[int(x) - 1 for x in input().split()[1:]] for x in range(M)] conditions = list(map(int, input().split())) ok = 0 for patterns in itertools.product((0, 1), repeat=N): patterns = list(patterns) is_lighting = True for i, lights in enumerate(connected_switches): turned_sw = sum([patterns[x] for x in lights]) if turned_sw % 2 != conditions[i]: is_lighting = False break if is_lighting: ok += 1 print(ok)
replace
8
9
8
9
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++) using namespace std; int N = 0, M = 0, k = 0; long long ans = 0; int main() { cin >> N >> M; // 繋がっているスイッチを格納するベクター vector<vector<int>> S(M, vector<int>(M)); // 各電球に繋がっているスイッチの個数を格納しておく配列 int K[M]; // 2で割ったあまりの条件を格納しておく配列 int P[M]; // 入力をそれぞれに格納していく rep(m, M) { cin >> K[m]; rep(i, K[m]) { cin >> S[m][i]; } } rep(m, M) { cin >> P[m]; } // ここからソルバー // bit全探索やる // まずは電球のONOFFを表すフラグを2^Nだけ考える rep(mask, 1 << N) { bool flag[N]; rep(n, N) { if (mask & (1 << n)) flag[n] = true; else flag[n] = false; } // 全ての電球について2で割ったあまりがPの条件に等しいかどうかを保存するcheckフラグ bool check = true; rep(m, M) { // ONの場所はtrueつまり1が格納されているのでインクリメントしていく int sum_flag = 0; rep(idx, K[m]) { sum_flag += flag[S[m][idx] - 1]; } // Pの条件にあっているかどうかを判断 if (sum_flag % 2 != P[m]) check = false; } // checkがいまだにtrueであれば全ての電球についてtureであったということ if (check) ans++; } // 解答出力 cout << ans << endl; }
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++) using namespace std; int N = 0, M = 0, k = 0; long long ans = 0; int main() { cin >> N >> M; // 繋がっているスイッチを格納するベクター vector<vector<int>> S(M, vector<int>(N)); // 各電球に繋がっているスイッチの個数を格納しておく配列 int K[M]; // 2で割ったあまりの条件を格納しておく配列 int P[M]; // 入力をそれぞれに格納していく rep(m, M) { cin >> K[m]; rep(i, K[m]) { cin >> S[m][i]; } } rep(m, M) { cin >> P[m]; } // ここからソルバー // bit全探索やる // まずは電球のONOFFを表すフラグを2^Nだけ考える rep(mask, 1 << N) { bool flag[N]; rep(n, N) { if (mask & (1 << n)) flag[n] = true; else flag[n] = false; } // 全ての電球について2で割ったあまりがPの条件に等しいかどうかを保存するcheckフラグ bool check = true; rep(m, M) { // ONの場所はtrueつまり1が格納されているのでインクリメントしていく int sum_flag = 0; rep(idx, K[m]) { sum_flag += flag[S[m][idx] - 1]; } // Pの条件にあっているかどうかを判断 if (sum_flag % 2 != P[m]) check = false; } // checkがいまだにtrueであれば全ての電球についてtureであったということ if (check) ans++; } // 解答出力 cout << ans << endl; }
replace
12
13
12
13
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i, s, n) for (int i = (s); i < (n); ++i) #define ll long long #define ld long double #define P pair<ll, ll> #define all(v) v.begin(), v.end() const ll mod = 1e9 + 7; const ll INF = 1e18; const double pi = acos(-1.0); int main(void) { ll n, m; cin >> n >> m; vector<vector<ll>> s; rep(i, m) { ll k; cin >> k; rep(j, k) { ll a; cin >> a; a--; s[i].push_back(a); } } vector<ll> p(m); rep(i, m) cin >> p[i]; ll ans = 0; for (int bit = 0; bit < (1 << n); bit++) { bool ok = true; rep(i, m) { ll cnt = 0; for (auto on : s[i]) { if (bit & (1 << on)) cnt++; } if (cnt % 2 != p[i]) ok = false; } if (ok) ++ans; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i, s, n) for (int i = (s); i < (n); ++i) #define ll long long #define ld long double #define P pair<ll, ll> #define all(v) v.begin(), v.end() const ll mod = 1e9 + 7; const ll INF = 1e18; const double pi = acos(-1.0); int main(void) { ll n, m; cin >> n >> m; vector<vector<ll>> s(m); rep(i, m) { ll k; cin >> k; rep(j, k) { ll a; cin >> a; a--; s[i].push_back(a); } } vector<ll> p(m); rep(i, m) cin >> p[i]; ll ans = 0; for (int bit = 0; bit < (1 << n); bit++) { bool ok = true; rep(i, m) { ll cnt = 0; for (auto on : s[i]) { if (bit & (1 << on)) cnt++; } if (cnt % 2 != p[i]) ok = false; } if (ok) ++ans; } cout << ans << endl; return 0; }
replace
17
18
17
18
-11
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; vector<vector<int>> vec(M); for (int i = 0; i < M; ++i) { int k; cin >> k; vec[i].resize(k); for (int j = 0; j < k; ++j) { cin >> vec[i][j]; --vec[i][j]; } } vector<int> p(M); for (int i = 0; i < M; ++i) cin >> p[i]; int ans = 0; for (int i = 0; i < (1 << N); ++i) { bool ok = true; for (int j = 0; j < M; ++j) { int c = 0; for (int id = 0; id < vec[j].size(); id++) { if ((i >> vec[id][j]) & 1) { ++c; } } c %= 2; if (c != p[j]) { ok = false; } } if (ok) { ++ans; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; vector<vector<int>> vec(M); for (int i = 0; i < M; ++i) { int k; cin >> k; vec[i].resize(k); for (int j = 0; j < k; ++j) { cin >> vec[i][j]; --vec[i][j]; } } vector<int> p(M); for (int i = 0; i < M; ++i) cin >> p[i]; int ans = 0; for (int i = 0; i < (1 << N); ++i) { bool ok = true; for (int j = 0; j < M; ++j) { int c = 0; for (int id = 0; id < vec[j].size(); id++) { if ((i >> vec[j][id]) & 1) { ++c; } } c %= 2; if (c != p[j]) { ok = false; } } if (ok) { ++ans; } } cout << ans << endl; return 0; }
replace
24
25
24
25
0
p03031
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; int cnt = 0; vector<int> a(N); for (int i = 0; i < M; i++) { int k; cin >> k; for (int j = 0; j < k; k++) { int s = 0; cin >> s; s--; a[s] |= 1 << i; } } int p; for (int i = 0; i < M; i++) { int x; cin >> x; p |= x << i; } for (int s = 0; s < (1 << N); s++) { int t = 0; for (int i = 0; i < N; i++) { if ((s >> i) & 1) { t ^= a[i]; } } if (t == p) { cnt++; } } cout << cnt << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; int cnt = 0; vector<int> a(N); for (int i = 0; i < M; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s = 0; cin >> s; s--; a[s] |= 1 << i; } } int p; for (int i = 0; i < M; i++) { int x; cin >> x; p |= x << i; } for (int s = 0; s < (1 << N); s++) { int t = 0; for (int i = 0; i < N; i++) { if ((s >> i) & 1) { t ^= a[i]; } } if (t == p) { cnt++; } } cout << cnt << endl; }
replace
12
13
12
13
TLE
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; vector<int> S[N], T(M); for (int i = 0; i < M; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s; cin >> s; S[i].push_back(s - 1); } } for (int i = 0; i < M; i++) { cin >> T[i]; } int sum = 0; for (int bit = 0; bit < (1 << N); bit++) { int ans = 0; for (int j = 0; j < M; j++) { int cnt = 0; for (int i = 0; i < S[j].size(); i++) { if (bit & (1 << S[j][i])) cnt++; } if (cnt % 2 == T[j]) ans++; } if (ans == M) sum++; } cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; vector<int> S[M], T(M); for (int i = 0; i < M; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s; cin >> s; S[i].push_back(s - 1); } } for (int i = 0; i < M; i++) { cin >> T[i]; } int sum = 0; for (int bit = 0; bit < (1 << N); bit++) { int ans = 0; for (int j = 0; j < M; j++) { int cnt = 0; for (int i = 0; i < S[j].size(); i++) { if (bit & (1 << S[j][i])) cnt++; } if (cnt % 2 == T[j]) ans++; } if (ans == M) sum++; } cout << sum << endl; }
replace
7
8
7
8
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int>> c(m, vector<int>(n, 0)); for (int i = 0; i < m; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s; cin >> s; c.at(i).at(s - 1) = 1; } } vector<int> p(n); for (int i = 0; i < m; i++) cin >> p.at(i); int a = 0; for (int i = 0; i < (1 << n); i++) { vector<int> b(n); int x = i; for (int d = 0; d < n; d++) { b.at(d) = x % 2; x /= 2; } bool f = 1; for (int j = 0; j < m; j++) { int s = 0; for (int k = 0; k < n; k++) s += b.at(k) * c.at(j).at(k); f = (s % 2 == p.at(j) ? f : 0); } a += (f ? 1 : 0); } cout << a << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int>> c(m, vector<int>(n, 0)); for (int i = 0; i < m; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s; cin >> s; c.at(i).at(s - 1) = 1; } } vector<int> p(m); for (int i = 0; i < m; i++) cin >> p.at(i); int a = 0; for (int i = 0; i < (1 << n); i++) { vector<int> b(n); int x = i; for (int d = 0; d < n; d++) { b.at(d) = x % 2; x /= 2; } bool f = 1; for (int j = 0; j < m; j++) { int s = 0; for (int k = 0; k < n; k++) s += b.at(k) * c.at(j).at(k); f = (s % 2 == p.at(j) ? f : 0); } a += (f ? 1 : 0); } cout << a << endl; }
replace
15
16
15
16
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { ifstream in("data1.txt"); cin.rdbuf(in.rdbuf()); int N, M; cin >> N >> M; vector<vector<int>> vec(M); for (int i = 0; i < M; ++i) { int k; cin >> k; vec[i].resize(k); for (int j = 0; j < k; ++j) { cin >> vec[i][j]; --vec[i][j]; } } vector<int> p(M); for (int i = 0; i < M; ++i) cin >> p[i]; int ans = 0; for (int i = 0; i < (1 << N); ++i) { bool ok = true; for (int j = 0; j < M; ++j) { int c = 0; for (int id : vec[j]) { if ((i >> id) & 1) { ++c; } } c %= 2; if (c != p[j]) { ok = false; } } if (ok) { ++ans; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { // ifstream in("data1.txt"); // cin.rdbuf(in.rdbuf()); int N, M; cin >> N >> M; vector<vector<int>> vec(M); for (int i = 0; i < M; ++i) { int k; cin >> k; vec[i].resize(k); for (int j = 0; j < k; ++j) { cin >> vec[i][j]; --vec[i][j]; } } vector<int> p(M); for (int i = 0; i < M; ++i) cin >> p[i]; int ans = 0; for (int i = 0; i < (1 << N); ++i) { bool ok = true; for (int j = 0; j < M; ++j) { int c = 0; for (int id : vec[j]) { if ((i >> id) & 1) { ++c; } } c %= 2; if (c != p[j]) { ok = false; } } if (ok) { ++ans; } } cout << ans << endl; return 0; }
replace
4
6
4
6
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main(void) { int n, m; int *k, **s, *p; int pattern = 1; int count = 0; cin >> n >> m; k = new int[m]; s = new int *[m]; p = new int[m]; for (int i = 0; i < n; i++) { pattern *= 2; } count = pattern; for (int i = 0; i < m; i++) { cin >> k[i]; s[i] = new int(k[i]); for (int j = 0; j < k[i]; j++) { cin >> s[i][j]; s[i][j] -= 1; } } for (int i = 0; i < m; i++) { cin >> p[i]; } for (int bits = 0; bits < pattern; bits++) { for (int i = 0; i < m; i++) { int l = 0; for (int j = 0; j < k[i]; j++) { l += (bits >> s[i][j]) & 1; } if (l % 2 != p[i]) { count--; break; } } } cout << count << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int n, m; int *k, **s, *p; int pattern = 1; int count = 0; cin >> n >> m; k = new int[m]; s = new int *[m]; p = new int[m]; for (int i = 0; i < n; i++) { pattern *= 2; } count = pattern; for (int i = 0; i < m; i++) { cin >> k[i]; s[i] = new int[k[i]]; for (int j = 0; j < k[i]; j++) { cin >> s[i][j]; s[i][j] -= 1; } } for (int i = 0; i < m; i++) { cin >> p[i]; } for (int bits = 0; bits < pattern; bits++) { for (int i = 0; i < m; i++) { int l = 0; for (int j = 0; j < k[i]; j++) { l += (bits >> s[i][j]) & 1; } if (l % 2 != p[i]) { count--; break; } } } cout << count << endl; return 0; }
replace
18
19
18
19
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main(void) { int n, m; cin >> n >> m; vector<vector<int>> b(m); for (int i = 0; i < m; i++) { int k; cin >> k; for (int i = 0; i < k; i++) { int s; cin >> s; b[i].push_back(s); } } vector<int> p(m); for (int i = 0; i < m; i++) cin >> p[i]; int ans = 0; for (int sw = 0; sw < (int)pow(2, n); sw++) { bool all_on = true; for (int i = 0; i < m; i++) { int on = 0; for (int s : b[i]) { on += (sw >> (s - 1)) % 2; } all_on &= (on % 2 == p[i]); } if (all_on) ans += 1; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int n, m; cin >> n >> m; vector<vector<int>> b(m); for (int i = 0; i < m; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s; cin >> s; b[i].push_back(s); } } vector<int> p(m); for (int i = 0; i < m; i++) cin >> p[i]; int ans = 0; for (int sw = 0; sw < (int)pow(2, n); sw++) { bool all_on = true; for (int i = 0; i < m; i++) { int on = 0; for (int s : b[i]) { on += (sw >> (s - 1)) % 2; } all_on &= (on % 2 == p[i]); } if (all_on) ans += 1; } cout << ans << endl; return 0; }
replace
11
12
11
12
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(x) (x).begin(), (x).end() using namespace std; template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } using ll = long long; using ld = long double; const int INF = 1e9; const ld eps = 1e-9, pi = acos(-1.0); bitset<10> connect[10]; int flag[10]; int main() { int n, m, tmp; cin >> n >> m; REP(i, m) { int k; cin >> k; REP(j, k) { cin >> tmp; connect[i].set(tmp); } } int res = 0; REP(i, m) cin >> flag[i]; REP(i, 1 << n) { bitset<10> target(i); bool isok = true; REP(j, m) if ((target & connect[j]).count() % 2 != flag[j]) { isok = false; break; } if (isok) res++; } cout << res << endl; return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(x) (x).begin(), (x).end() using namespace std; template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } using ll = long long; using ld = long double; const int INF = 1e9; const ld eps = 1e-9, pi = acos(-1.0); bitset<10> connect[10]; int flag[10]; int main() { int n, m, tmp; cin >> n >> m; REP(i, m) { int k; cin >> k; REP(j, k) { cin >> tmp; connect[i].set(tmp - 1); } } int res = 0; REP(i, m) cin >> flag[i]; REP(i, 1 << n) { bitset<10> target(i); bool isok = true; REP(j, m) if ((target & connect[j]).count() % 2 != flag[j]) { isok = false; break; } if (isok) res++; } cout << res << endl; return 0; }
replace
26
27
26
27
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } using namespace std; #define min_3(a, b, c) min(a, min(b, c)) typedef long long ll; const int mod = 1e9 + 7; int main() { int n, m; cin >> n >> m; vector<vector<int>> connect(n); vector<int> p(m); for (int i = 0; i < m; i++) { int k; cin >> k; connect[i].resize(k); for (int j = 0; j < k; j++) { cin >> connect[i][j]; connect[i][j]--; } } for (int i = 0; i < m; i++) cin >> p[i]; int ans = 0; for (int i = 0; i < (1 << n); i++) // 2^n通り試す { bool ok = true; // 光る? for (int j = 0; j < m; j++) { int count = 0; for (int id : connect[j]) { if ((i >> id) & 1) count++; // 繋がっているものの中でonの個数 } count %= 2; if (count != p[j]) ok = false; } if (ok == 1) ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } using namespace std; #define min_3(a, b, c) min(a, min(b, c)) typedef long long ll; const int mod = 1e9 + 7; int main() { int n, m; cin >> n >> m; vector<vector<int>> connect(m); vector<int> p(m); for (int i = 0; i < m; i++) { int k; cin >> k; connect[i].resize(k); for (int j = 0; j < k; j++) { cin >> connect[i][j]; connect[i][j]--; } } for (int i = 0; i < m; i++) cin >> p[i]; int ans = 0; for (int i = 0; i < (1 << n); i++) // 2^n通り試す { bool ok = true; // 光る? for (int j = 0; j < m; j++) { int count = 0; for (int id : connect[j]) { if ((i >> id) & 1) count++; // 繋がっているものの中でonの個数 } count %= 2; if (count != p[j]) ok = false; } if (ok == 1) ans++; } cout << ans << endl; return 0; }
replace
25
26
25
26
0
p03031
C++
Runtime Error
#include <algorithm> #include <cmath> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; typedef pair<int, int> P; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; int main() { ll n, m; cin >> n >> m; vector<vector<ll>> s(m); vector<ll> k(m); vector<ll> p(m); for (int i = 0; i < m; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { ll a; cin >> a; a--; s[i].push_back(a); } } rep(i, m) cin >> p[i]; ll ans = 0; for (ll bit = 0; bit < (1 << n); bit++) { vector<ll> cnt(n, 0); for (int j = 0; j < n; j++) { if (bit & (1 << j)) { cnt[j]++; } } vector<ll> count(m, 0); for (int a = 0; a < n; a++) { for (int b = 0; b < k[a]; b++) { count[a] += cnt[s[a][b]]; } } bool flag = true; for (int x = 0; x < m; x++) { if (count[x] % 2 == p[x]) { flag = true; } else { flag = false; break; } } if (flag) ans++; } cout << ans << endl; }
#include <algorithm> #include <cmath> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; typedef pair<int, int> P; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; int main() { ll n, m; cin >> n >> m; vector<vector<ll>> s(m); vector<ll> k(m); vector<ll> p(m); for (int i = 0; i < m; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { ll a; cin >> a; a--; s[i].push_back(a); } } rep(i, m) cin >> p[i]; ll ans = 0; for (ll bit = 0; bit < (1 << n); bit++) { vector<ll> cnt(n, 0); for (int j = 0; j < n; j++) { if (bit & (1 << j)) { cnt[j]++; } } vector<ll> count(m, 0); for (int a = 0; a < m; a++) { for (int b = 0; b < k[a]; b++) { count[a] += cnt[s[a][b]]; } } bool flag = true; for (int x = 0; x < m; x++) { if (count[x] % 2 == p[x]) { flag = true; } else { flag = false; break; } } if (flag) ans++; } cout << ans << endl; }
replace
45
46
45
46
0
p03031
C++
Runtime Error
#include <iostream> #include <vector> typedef long long ll; using namespace std; int main() { ll N, M; cin >> N >> M; vector<vector<ll>> switches(M); vector<ll> p(M); for (ll i = 0; i < M; i++) { ll k; cin >> k; for (ll i = 0; i < k; i++) { ll s; cin >> s; switches[i].push_back(s - 1); } } for (ll i = 0; i < M; i++) { cin >> p[i]; } ll ans = 0; // スイッチの点灯状態を全探索 // 0ビット目が1 -> 1個目のスイッチがON for (ll bit = 0; bit < (1 << N); bit++) { bool ok = true; // 全ての電球に対して、点灯するどうかチェックする for (ll i = 0; i < M; i++) { // onになっているスイッチの数 ll ons = 0; for (auto s : switches[i]) { if (bit & (1 << s)) { ons++; } } // onになっているスイッチの数の余りを計算 if (ons % 2 != p[i]) { ok = false; break; } } if (ok) ans++; } cout << ans << endl; }
#include <iostream> #include <vector> typedef long long ll; using namespace std; int main() { ll N, M; cin >> N >> M; vector<vector<ll>> switches(M); vector<ll> p(M); for (ll i = 0; i < M; i++) { ll k; cin >> k; for (ll j = 0; j < k; j++) { ll s; cin >> s; switches[i].push_back(s - 1); } } for (ll i = 0; i < M; i++) { cin >> p[i]; } ll ans = 0; // スイッチの点灯状態を全探索 // 0ビット目が1 -> 1個目のスイッチがON for (ll bit = 0; bit < (1 << N); bit++) { bool ok = true; // 全ての電球に対して、点灯するどうかチェックする for (ll i = 0; i < M; i++) { // onになっているスイッチの数 ll ons = 0; for (auto s : switches[i]) { if (bit & (1 << s)) { ons++; } } // onになっているスイッチの数の余りを計算 if (ons % 2 != p[i]) { ok = false; break; } } if (ok) ans++; } cout << ans << endl; }
replace
15
16
15
16
0
p03031
C++
Runtime Error
#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 ((1ull << 62) - 1) #define cINFINT ((1 << 30) - 1) #define BITREP(BIT, NUM) REP(BIT, 1 << (NUM)) #define ISBIT(BIT, INDEX) ((BIT) & (1 << (INDEX))) int main() { ull n, m; cin >> n >> m; vector<ll> k(m, 0); vector<vector<ll>> s(m, vector<ll>(m, 0)); vector<ll> p(m, 0); REP(k_, m) { cin >> k[k_]; REP(s_, k[k_]) { cin >> s[k_][s_]; s[k_][s_]--; } } REP(p_, m) cin >> p[p_]; ll ans = 0; BITREP(i, n) { bool ok = true; REP(k_, m) { int b = 0; REP(s_, k[k_]) { if (ISBIT(i, s[k_][s_])) b = b == 0 ? 1 : 0; } if (b != p[k_]) { ok = false; break; } } if (ok) ++ans; } cout << ans << 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 ((1ull << 62) - 1) #define cINFINT ((1 << 30) - 1) #define BITREP(BIT, NUM) REP(BIT, 1 << (NUM)) #define ISBIT(BIT, INDEX) ((BIT) & (1 << (INDEX))) int main() { ull n, m; cin >> n >> m; vector<ll> k(m, 0); vector<vector<ll>> s(m, vector<ll>(n, 0)); vector<ll> p(m, 0); REP(k_, m) { cin >> k[k_]; REP(s_, k[k_]) { cin >> s[k_][s_]; s[k_][s_]--; } } REP(p_, m) cin >> p[p_]; ll ans = 0; BITREP(i, n) { bool ok = true; REP(k_, m) { int b = 0; REP(s_, k[k_]) { if (ISBIT(i, s[k_][s_])) b = b == 0 ? 1 : 0; } if (b != p[k_]) { ok = false; break; } } if (ok) ++ans; } cout << ans << endl; return 0; }
replace
21
22
21
22
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long bool isprime(int n) { double rootn = sqrt(n); if (n < 2) { return false; } else if (n == 2) { return true; } else if (n % 2 == 0) { return false; } else { for (int i = 3; i <= rootn; i += 2) { if (n % i == 0) { return false; } } return true; } } // 空のベクトルを二つ渡す。N (>=2) 以下の素数を primes に順番に入れる。 void prime_vectors(int N, vector<int> &primes, vector<bool> &is_prime) { primes = {}; is_prime = {}; for (int i = 0; i <= N; i++) { is_prime.push_back(true); } primes.push_back(2); for (int j = 4; j <= N; j += 2) { is_prime[j] = false; } int i; for (i = 3; i * i <= N; i += 2) { if (!is_prime[i]) continue; primes.push_back(i); for (int j = 2 * i; j <= N; j += i) { is_prime[j] = false; } } for (; i <= N; i += 2) { if (is_prime[i]) { primes.push_back(i); } } } signed main() { // cout << fixed << setprecision(10) << flush; int n, m; cin >> n >> m; vector<vector<int>> s(m + 1, vector<int>()); vector<int> k(m + 1); for (int i = 1; i <= m; i++) { cin >> k[i]; for (int j = 1; j <= k[i]; j++) { int sij; cin >> sij; s[i].push_back(sij); } } vector<int> p(m); for (int i = 1; i <= m; i++) { cin >> p[i]; } vector<int> sw(n + 1, 0); int n_on = 0; for (int i = 0; i < (1 << n); i++) { int tempi = i; for (int j = 1; j <= n; j++) { sw[j] = tempi % 2; tempi /= 2; } int l; for (l = 1; l <= m; l++) { // 電球 l について int cond = 0; for (int u = 0; u < k[l]; u++) { cond += (sw[s[l][u]] == 1); } if (cond % 2 != p[l]) { break; } } if (l == m + 1) { // 全部 on だった n_on++; } } cout << n_on << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long bool isprime(int n) { double rootn = sqrt(n); if (n < 2) { return false; } else if (n == 2) { return true; } else if (n % 2 == 0) { return false; } else { for (int i = 3; i <= rootn; i += 2) { if (n % i == 0) { return false; } } return true; } } // 空のベクトルを二つ渡す。N (>=2) 以下の素数を primes に順番に入れる。 void prime_vectors(int N, vector<int> &primes, vector<bool> &is_prime) { primes = {}; is_prime = {}; for (int i = 0; i <= N; i++) { is_prime.push_back(true); } primes.push_back(2); for (int j = 4; j <= N; j += 2) { is_prime[j] = false; } int i; for (i = 3; i * i <= N; i += 2) { if (!is_prime[i]) continue; primes.push_back(i); for (int j = 2 * i; j <= N; j += i) { is_prime[j] = false; } } for (; i <= N; i += 2) { if (is_prime[i]) { primes.push_back(i); } } } signed main() { // cout << fixed << setprecision(10) << flush; int n, m; cin >> n >> m; vector<vector<int>> s(m + 1, vector<int>()); vector<int> k(m + 1); for (int i = 1; i <= m; i++) { cin >> k[i]; for (int j = 1; j <= k[i]; j++) { int sij; cin >> sij; s[i].push_back(sij); } } vector<int> p(m + 1); for (int i = 1; i <= m; i++) { cin >> p[i]; } vector<int> sw(n + 1, 0); int n_on = 0; for (int i = 0; i < (1 << n); i++) { int tempi = i; for (int j = 1; j <= n; j++) { sw[j] = tempi % 2; tempi /= 2; } int l; for (l = 1; l <= m; l++) { // 電球 l について int cond = 0; for (int u = 0; u < k[l]; u++) { cond += (sw[s[l][u]] == 1); } if (cond % 2 != p[l]) { break; } } if (l == m + 1) { // 全部 on だった n_on++; } } cout << n_on << endl; return 0; }
replace
68
69
68
69
0
p03031
C++
Runtime Error
#include <iostream> using namespace std; int ans, n, m, p[10], k[10], d[10][10]; void dfs(int a[], int l) { if (l == n) { bool f = true; for (int i = 0; i < m; i++) { if (a[i] != p[i]) f = false; } if (f) ans++; return; } int aa[10]; for (int i = 0; i < n; i++) aa[i] = a[i]; for (int i = 0; i < n; i++) aa[i] = (aa[i] + d[l][i]) % 2; dfs(aa, l + 1); dfs(a, l + 1); return; } int main() { int s[10][10]; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { cin >> s[i][j]; s[i][j]--; d[s[i][j]][i] = 1; } } for (int i = 0; i < m; i++) { cin >> p[i]; } int a[10]{0}; dfs(a, 0); cout << ans << endl; }
#include <iostream> using namespace std; int ans, n, m, p[10], k[10], d[10][10]; void dfs(int a[], int l) { if (l == n) { bool f = true; for (int i = 0; i < m; i++) { if (a[i] != p[i]) f = false; } if (f) ans++; return; } int aa[10]; for (int i = 0; i < n; i++) aa[i] = a[i]; for (int i = 0; i < n; i++) aa[i] = (aa[i] + d[l][i]) % 2; dfs(aa, l + 1); dfs(a, l + 1); return; } int main() { int s[10][10]; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { cin >> s[i][j]; s[i][j]--; d[s[i][j]][i] = 1; } } for (int i = 0; i < m; i++) { cin >> p[i]; } int a[10]{0}; dfs(a, 0); cout << ans << endl; }
replace
29
30
29
30
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, a, b) for (int(i) = (a); (i) < (b); ++(i)) #define RREP(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i)) #define ALL(x) (x).begin(), (x).end() #define chmin(x, v) x = min(x, v) #define chmax(x, v) x = max(x, v) using namespace std; typedef long long ll; int main() { int N, M; cin >> N >> M; vector<int> k(M); vector<vector<int>> s(M, vector<int>(M)); vector<int> p(M); REP(i, 0, M) { cin >> k[i]; REP(j, 0, k[i]) { cin >> s[i][j]; s[i][j]--; } } REP(i, 0, M) cin >> p[i]; int ans = 0; // 組み合わせ数 REP(mask, 0, 1 << N) { // N個のスイッチの状態 bool flag = true; // 全ての電球が付く REP(i, 0, M) { // 電球iが点灯するか? // 電球iに,繋がっているスイッチのうち何個がonか int x = 0; REP(j, 0, k[i]) { // スイッチ s[i][j]がonか? if (mask & (1 << s[i][j])) ++x; } // 電球iが点灯する if (x % 2 == p[i]) continue; else { flag = false; break; } } if (flag) ++ans; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define REP(i, a, b) for (int(i) = (a); (i) < (b); ++(i)) #define RREP(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i)) #define ALL(x) (x).begin(), (x).end() #define chmin(x, v) x = min(x, v) #define chmax(x, v) x = max(x, v) using namespace std; typedef long long ll; int main() { int N, M; cin >> N >> M; vector<int> k(M); // vector<vector<int>> s(M, vector<int>(M)); int s[20][20]; vector<int> p(M); REP(i, 0, M) { cin >> k[i]; REP(j, 0, k[i]) { cin >> s[i][j]; s[i][j]--; } } REP(i, 0, M) cin >> p[i]; int ans = 0; // 組み合わせ数 REP(mask, 0, 1 << N) { // N個のスイッチの状態 bool flag = true; // 全ての電球が付く REP(i, 0, M) { // 電球iが点灯するか? // 電球iに,繋がっているスイッチのうち何個がonか int x = 0; REP(j, 0, k[i]) { // スイッチ s[i][j]がonか? if (mask & (1 << s[i][j])) ++x; } // 電球iが点灯する if (x % 2 == p[i]) continue; else { flag = false; break; } } if (flag) ++ans; } cout << ans << endl; return 0; }
replace
13
14
13
15
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, a, n) for (int i = a; i < n; i++) #define repr(i, a, n) for (int i = n - 1; i >= a; i--) using namespace std; using ll = long long; using P = pair<int, int>; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<vector<int>> connect(m); rep(i, 0, m) { int k; cin >> k; rep(i, 0, k) { int s; cin >> s; s--; connect[i].push_back(s); } } vector<int> p(m); rep(i, 0, m) cin >> p[i]; int ans = 0; rep(i, 0, 1 << n) { bool isok = true; rep(j, 0, m) { int c = 0; for (int id : connect[j]) { if (i >> id & 1) c++; } c %= 2; if (c != p[j]) isok = false; } if (isok) ans++; } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, a, n) for (int i = a; i < n; i++) #define repr(i, a, n) for (int i = n - 1; i >= a; i--) using namespace std; using ll = long long; using P = pair<int, int>; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<vector<int>> connect(m); rep(i, 0, m) { int k; cin >> k; rep(j, 0, k) { int s; cin >> s; s--; connect[i].push_back(s); } } vector<int> p(m); rep(i, 0, m) cin >> p[i]; int ans = 0; rep(i, 0, 1 << n) { bool isok = true; rep(j, 0, m) { int c = 0; for (int id : connect[j]) { if (i >> id & 1) c++; } c %= 2; if (c != p[j]) isok = false; } if (isok) ans++; } cout << ans << endl; }
replace
18
19
18
19
0
p03031
C++
Runtime Error
#include <algorithm> // sort #include <cmath> #include <iostream> #include <map> // pair #include <numeric> #include <stdio.h> #include <string> #include <vector> using namespace std; void bin_vecadd(vector<int> &input) { int N = input.size(); input[0]++; for (int i = 0; i < N; i++) { if (input[i] == 2) { input[i] = 0; input[i + 1]++; } } } int main(int argc, char const *argv[]) { int N, M; cin >> N >> M; vector<vector<int>> mat; vector<int> switch_param(M, 0); int k = 0; for (int i = 0; i < M; i++) { cin >> k; vector<int> vec(k, 0); for (int j = 0; j < k; j++) { cin >> vec[j]; } mat.push_back(vec); } for (int i = 0; i < M; i++) { cin >> switch_param[i]; } int count = 0; int loop = int(pow(2, N)); vector<int> switch_vec(N, 0); int ans_num = 0; int check_signal = 1; int light_sum = 0; for (int i = 0; i < loop; i++) // パターン { for (int j = 0; j < M; j++) // 電球 { for (int k = 0; k < mat[j].size(); k++) // 電球k番目 { light_sum += switch_vec[mat[j][k] - 1]; } if (light_sum % 2 != switch_param[j]) { check_signal = 0; } // init light_sum = 0; } // for(int j = 0; j < switch_vec.size(); j ++) // { // cout<<switch_vec[j]; // } if (check_signal == 1) { ans_num++; } else { check_signal = 1; } bin_vecadd(switch_vec); } cout << ans_num; return 0; }
#include <algorithm> // sort #include <cmath> #include <iostream> #include <map> // pair #include <numeric> #include <stdio.h> #include <string> #include <vector> using namespace std; void bin_vecadd(vector<int> &input) { int N = input.size(); input[0]++; for (int i = 0; i < N; i++) { if (input[i] == 2) { input[i] = 0; if (i + 1 != N) input[i + 1]++; } } } int main(int argc, char const *argv[]) { int N, M; cin >> N >> M; vector<vector<int>> mat; vector<int> switch_param(M, 0); int k = 0; for (int i = 0; i < M; i++) { cin >> k; vector<int> vec(k, 0); for (int j = 0; j < k; j++) { cin >> vec[j]; } mat.push_back(vec); } for (int i = 0; i < M; i++) { cin >> switch_param[i]; } int count = 0; int loop = int(pow(2, N)); vector<int> switch_vec(N, 0); int ans_num = 0; int check_signal = 1; int light_sum = 0; for (int i = 0; i < loop; i++) // パターン { for (int j = 0; j < M; j++) // 電球 { for (int k = 0; k < mat[j].size(); k++) // 電球k番目 { light_sum += switch_vec[mat[j][k] - 1]; } if (light_sum % 2 != switch_param[j]) { check_signal = 0; } // init light_sum = 0; } // for(int j = 0; j < switch_vec.size(); j ++) // { // cout<<switch_vec[j]; // } if (check_signal == 1) { ans_num++; } else { check_signal = 1; } bin_vecadd(switch_vec); } cout << ans_num; return 0; }
replace
16
17
16
18
0
p03031
C++
Runtime Error
#include <iostream> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int nijou(int a) { int b = 1; rep(i, a - 1) b *= 2; return b; } int main() { int n, m, ans = 0; cin >> n >> m; vector<vector<int>> a(m, vector<int>(n)); vector<int> p(m); rep(i, m) { cin >> a[i][0]; rep(j, a[i][0]) cin >> a[i][j + 1]; } rep(i, m) cin >> p[i]; rep(i, 1 << n) { vector<int> s(m); rep(j, n) { int mask = 1 << j; if (i & mask) { rep(k, m) { rep(l, a[k][0]) { if (nijou(a[k][l + 1]) == mask) { s[k]++; } } } } } bool ok = true; rep(j, m) { if (s[j] % 2 != p[j]) ok = false; } if (ok) ans++; } cout << ans << endl; return 0; }
#include <iostream> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int nijou(int a) { int b = 1; rep(i, a - 1) b *= 2; return b; } int main() { int n, m, ans = 0; cin >> n >> m; vector<vector<int>> a(m, vector<int>(n + 1)); vector<int> p(m); rep(i, m) { cin >> a[i][0]; rep(j, a[i][0]) cin >> a[i][j + 1]; } rep(i, m) cin >> p[i]; rep(i, 1 << n) { vector<int> s(m); rep(j, n) { int mask = 1 << j; if (i & mask) { rep(k, m) { rep(l, a[k][0]) { if (nijou(a[k][l + 1]) == mask) { s[k]++; } } } } } bool ok = true; rep(j, m) { if (s[j] % 2 != p[j]) ok = false; } if (ok) ans++; } cout << ans << endl; return 0; }
replace
14
15
14
15
0
p03031
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define N 110 int n, m; vector<vector<int>> vec; int p[N], a[20]; int main() { while (scanf("%d%d", &n, &m) != EOF) { vec.clear(); vec.resize(m + 1); for (int i = 1, k; i <= m; ++i) { scanf("%d", &k); for (int j = 1, x; j <= k; ++j) { scanf("%d", &x); vec[x].push_back(i); } } for (int i = 1; i <= m; ++i) { scanf("%d", p + i); } int res = 0; for (int i = 0; i < (1 << n); ++i) { memset(a, 0, sizeof a); for (int j = 0; j < n; ++j) { if ((i >> j) & 1) { for (auto it : vec[j + 1]) { a[it] ^= 1; } } } bool F = 1; for (int i = 1; i <= m; ++i) { if ((p[i] ^ a[i]) == 1) { F = 0; break; } } res += F; } printf("%d\n", res); } return 0; }
#include <bits/stdc++.h> using namespace std; #define N 110 int n, m; vector<vector<int>> vec; int p[N], a[20]; int main() { while (scanf("%d%d", &n, &m) != EOF) { vec.clear(); vec.resize(n + 1); for (int i = 1, k; i <= m; ++i) { scanf("%d", &k); for (int j = 1, x; j <= k; ++j) { scanf("%d", &x); vec[x].push_back(i); } } for (int i = 1; i <= m; ++i) { scanf("%d", p + i); } int res = 0; for (int i = 0; i < (1 << n); ++i) { memset(a, 0, sizeof a); for (int j = 0; j < n; ++j) { if ((i >> j) & 1) { for (auto it : vec[j + 1]) { a[it] ^= 1; } } } bool F = 1; for (int i = 1; i <= m; ++i) { if ((p[i] ^ a[i]) == 1) { F = 0; break; } } res += F; } printf("%d\n", res); } return 0; }
replace
11
12
11
12
0
p03032
C++
Runtime Error
#pragma region include #include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define ALL(obj) (obj).begin(), (obj).end() #define RALL(obj) (obj).rbegin(), (obj).rend() #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 MOD 1000000007 #define INF 1000000000 #define LLINF 4000000000000000000 using namespace std; typedef long long ll; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; #pragma endregion // #define __DEBUG__ #ifdef __DEBUG__ #define dump(x) \ cerr << #x << " = " << (x) << " [" << __LINE__ << ":" << __FUNCTION__ \ << "] " << endl; // vector出力 template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { os << "{"; REP(i, (int)v.size()) { os << v[i] << (i < v.size() - 1 ? ", " : ""); } os << "}"; return os; } // pair出力 template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &p) { return os << "(" << p.first << ", " << p.second << ")"; } // map出力 template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set 出力 template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #endif int main() { cin.tie(0); ios::sync_with_stdio(false); int N, K; cin >> N >> K; VI v1(N), v2(N); vector<ll> sum(N + 1); REP(i, N) { cin >> v1[i]; v2[N - 1 - i] = v1[i]; } sum[0] = 0; REP(i, N) { sum[i + 1] = sum[i] + v1[i]; } ll val = 0; ll ans = -LLINF; for (int a = 0; a <= N; a++) { for (int b = 0; b <= K - a; b++) { val = sum[a] + sum[N] - sum[N - b]; vector<int> vec(N, INF); REP(i, a) { vec[i] = v1[i]; } for (int i = 0; i < b; i++) { vec[i + a] = v2[i]; } sort(ALL(vec)); REP(i, K - a - b) { if (vec[i] < 0) { val -= vec[i]; } else { break; } } ans = max(ans, val); } } cout << ans << endl; getchar(); getchar(); }
#pragma region include #include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define ALL(obj) (obj).begin(), (obj).end() #define RALL(obj) (obj).rbegin(), (obj).rend() #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 MOD 1000000007 #define INF 1000000000 #define LLINF 4000000000000000000 using namespace std; typedef long long ll; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; #pragma endregion // #define __DEBUG__ #ifdef __DEBUG__ #define dump(x) \ cerr << #x << " = " << (x) << " [" << __LINE__ << ":" << __FUNCTION__ \ << "] " << endl; // vector出力 template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { os << "{"; REP(i, (int)v.size()) { os << v[i] << (i < v.size() - 1 ? ", " : ""); } os << "}"; return os; } // pair出力 template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &p) { return os << "(" << p.first << ", " << p.second << ")"; } // map出力 template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set 出力 template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #endif int main() { cin.tie(0); ios::sync_with_stdio(false); int N, K; cin >> N >> K; VI v1(N), v2(N); vector<ll> sum(N + 1); REP(i, N) { cin >> v1[i]; v2[N - 1 - i] = v1[i]; } sum[0] = 0; REP(i, N) { sum[i + 1] = sum[i] + v1[i]; } ll val = 0; ll ans = 0; int aaa = max(N, K); int bbb = min(N, K); for (int a = 0; a <= bbb; a++) { for (int b = 0; b <= bbb - a; b++) { val = sum[a] - sum[N - b] + sum[N]; vector<int> vec(aaa, INF); REP(i, a) { vec[i] = v1[i]; } for (int i = 0; i < b; i++) { vec[i + a] = v2[i]; } sort(ALL(vec)); REP(i, K - a - b) { if (vec[i] < 0) { val -= vec[i]; } else { break; } } ans = max(ans, val); } } cout << ans << endl; getchar(); getchar(); }
replace
86
91
86
93
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> #define all(x) begin(x), end(x) #define dbg(x) cerr << #x << " = " << x << endl #define _ << ' ' << using namespace std; using ll = long long; using vi = vector<int>; int n, k, v[50], dp[50][50][100]; int g(int p, int q, int r) { vector<int> x; for (int i = 0; i < p; ++i) x.push_back(v[i]); for (int i = n - 1; i > q; --i) x.push_back(v[i]); sort(all(x)); int sum = accumulate(all(x), 0); for (int i = 0; i < r && i < x.size() && x[i] < 0; ++i) sum -= x[i]; return sum; } int f(int p, int q, int r) { if (r == k) return g(p, q, 0); if (dp[p][q][r] != -1) return dp[p][q][r]; int sol = g(p, q, k - r); if (p <= q) { sol = max(sol, f(p + 1, q, r + 1)); sol = max(sol, f(p, q - 1, r + 1)); } return dp[p][q][r] = sol; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; ++i) cin >> v[i]; memset(dp, -1, sizeof dp); cout << f(0, n - 1, 0); }
#include <bits/stdc++.h> #define all(x) begin(x), end(x) #define dbg(x) cerr << #x << " = " << x << endl #define _ << ' ' << using namespace std; using ll = long long; using vi = vector<int>; int n, k, v[50], dp[50][50][100]; int g(int p, int q, int r) { vector<int> x; for (int i = 0; i < p; ++i) x.push_back(v[i]); for (int i = n - 1; i > q; --i) x.push_back(v[i]); sort(all(x)); int sum = accumulate(all(x), 0); for (int i = 0; i < r && i < x.size() && x[i] < 0; ++i) sum -= x[i]; return sum; } int f(int p, int q, int r) { if (r == k || p > q) return g(p, q, k - r); if (dp[p][q][r] != -1) return dp[p][q][r]; int sol = g(p, q, k - r); if (p <= q) { sol = max(sol, f(p + 1, q, r + 1)); sol = max(sol, f(p, q - 1, r + 1)); } return dp[p][q][r] = sol; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; ++i) cin >> v[i]; memset(dp, -1, sizeof dp); cout << f(0, n - 1, 0); }
replace
24
26
24
26
0
p03032
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS /* include ***********************/ #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* define *************************/ // for #define REP(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++) #define REPS(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++) #define RREP(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define RREPS(i, n) for (int i = (int)(n); i > 0; i--) #define FOR(i, s, n) \ for (int i = (int)(s), i##_len = (int)(n); i < i##_len; i++) #define RFOR(i, s, n) \ for (int i = (int)(n)-1, i##_len = (int)(s); i >= i##_len; i--) // printf #define PRINTD(d) printf("%d\n", (d)) #define PRINTL(d) printf("%lld\n", (d)) // memset #define m0(s) memset(s, 0, sizeof(s)) #define ml(s) memset(s, 63, sizeof(s)) #define fill(s, c) memset(s, c, sizeof(s)) #define INF 1e9 #define MOD 1000000007 typedef long long ll; typedef unsigned long long ull; int diff[4][2] = { {0, -1}, {-1, 0}, {1, 0}, {0, 1}, }; // 今回の変数 int Min(int a, int b) { return (a) < (b) ? (a) : (b); } int Max(int a, int b) { return (a) > (b) ? (a) : (b); } ll Minl(ll a, ll b) { return (a) < (b) ? (a) : (b); } ll Maxl(ll a, ll b) { return (a) > (b) ? (a) : (b); } void hSwap(int x[], int i, int j) { int temp; temp = x[i]; x[i] = x[j]; x[j] = temp; } void ShowData(int x[], int left, int right) { int i; for (i = left; i <= right; i++) printf("%d ", x[i]); printf("\n"); } void QSort(int x[], int left, int right, int n) { int i, j; // 左端,右端 int pivot; // 軸 i = left; j = right; pivot = x[(left + right) / 2]; while (1) { if (n > 0) { // n>0なら昇順、n<=0なら降順 while ((x[i] < pivot) && (i <= right)) i++; // 軸値より大きい要素 while ((pivot < x[j]) && (i <= right)) j--; // 軸値より小さい要素 } else { while ((x[i] > pivot) && (i <= right)) i++; // 軸値より小さい要素 while ((pivot > x[j]) && (i <= right)) j--; // 軸値より大きい要素 } if (i >= j) break; hSwap(x, i, j); i++; j--; } // ShowData(x, left, right); if (left < i - 1) QSort(x, left, i - 1, n); if (j + 1 < right) QSort(x, j + 1, right, n); } int main() { int ans = -1 * 1e9; int n, k; scanf("%d%d", &n, &k); int v[50]; REP(i, n) scanf("%d", &v[i]); REP(i, n + 1) { if (i > k) break; REP(j, n + 1) { if (i + j > k) break; int left = i; int right = j; int back = k - left - right; int min[50]; int num = 0; int cnt = 0; REP(m, 50) min[m] = 1e9; REP(m, left) { num += v[m]; if (v[m] < 0) { min[cnt++] = v[m]; } } REP(m, right) { num += v[n - 1 - m]; if (v[n - 1 - m] < 0) { min[cnt++] = v[n - 1 - m]; } } if (cnt > back) { QSort(min, 0, cnt - 1, 1); } REP(m, Min(cnt, back)) { num -= min[m]; } // printf("left:%d,right:%d,back:%d,:cnt:%d\n", left, right, back, cnt); ans = Max(ans, num); // PRINTD(ans); // puts(""); } } PRINTD(ans); }
#define _CRT_SECURE_NO_WARNINGS /* include ***********************/ #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* define *************************/ // for #define REP(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++) #define REPS(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++) #define RREP(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define RREPS(i, n) for (int i = (int)(n); i > 0; i--) #define FOR(i, s, n) \ for (int i = (int)(s), i##_len = (int)(n); i < i##_len; i++) #define RFOR(i, s, n) \ for (int i = (int)(n)-1, i##_len = (int)(s); i >= i##_len; i--) // printf #define PRINTD(d) printf("%d\n", (d)) #define PRINTL(d) printf("%lld\n", (d)) // memset #define m0(s) memset(s, 0, sizeof(s)) #define ml(s) memset(s, 63, sizeof(s)) #define fill(s, c) memset(s, c, sizeof(s)) #define INF 1e9 #define MOD 1000000007 typedef long long ll; typedef unsigned long long ull; int diff[4][2] = { {0, -1}, {-1, 0}, {1, 0}, {0, 1}, }; // 今回の変数 int Min(int a, int b) { return (a) < (b) ? (a) : (b); } int Max(int a, int b) { return (a) > (b) ? (a) : (b); } ll Minl(ll a, ll b) { return (a) < (b) ? (a) : (b); } ll Maxl(ll a, ll b) { return (a) > (b) ? (a) : (b); } void hSwap(int x[], int i, int j) { int temp; temp = x[i]; x[i] = x[j]; x[j] = temp; } void ShowData(int x[], int left, int right) { int i; for (i = left; i <= right; i++) printf("%d ", x[i]); printf("\n"); } void QSort(int x[], int left, int right, int n) { int i, j; // 左端,右端 int pivot; // 軸 i = left; j = right; pivot = x[(left + right) / 2]; while (1) { if (n > 0) { // n>0なら昇順、n<=0なら降順 while ((x[i] < pivot) && (i <= right)) i++; // 軸値より大きい要素 while ((pivot < x[j]) && (i <= right)) j--; // 軸値より小さい要素 } else { while ((x[i] > pivot) && (i <= right)) i++; // 軸値より小さい要素 while ((pivot > x[j]) && (i <= right)) j--; // 軸値より大きい要素 } if (i >= j) break; hSwap(x, i, j); i++; j--; } // ShowData(x, left, right); if (left < i - 1) QSort(x, left, i - 1, n); if (j + 1 < right) QSort(x, j + 1, right, n); } int main() { int ans = -1 * 1e9; int n, k; scanf("%d%d", &n, &k); int v[50]; REP(i, n) scanf("%d", &v[i]); REP(i, n + 1) { if (i > k) break; REP(j, n + 1 - i) { if (i + j > k) break; int left = i; int right = j; int back = k - left - right; int min[50]; int num = 0; int cnt = 0; REP(m, 50) min[m] = 1e9; REP(m, left) { num += v[m]; if (v[m] < 0) { min[cnt++] = v[m]; } } REP(m, right) { num += v[n - 1 - m]; if (v[n - 1 - m] < 0) { min[cnt++] = v[n - 1 - m]; } } if (cnt > back) { QSort(min, 0, cnt - 1, 1); } REP(m, Min(cnt, back)) { num -= min[m]; } // printf("left:%d,right:%d,back:%d,:cnt:%d\n", left, right, back, cnt); ans = Max(ans, num); // PRINTD(ans); // puts(""); } } PRINTD(ans); }
replace
105
106
105
106
0
p03032
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; typedef long long ll; const int INF = (1 << 30); const ll INFLL = (1ll << 60); const ll MOD = (ll)(1e9 + 7); #define l_ength size void mul_mod(ll &a, ll b) { a *= b; a %= MOD; } void add_mod(ll &a, ll b) { a = (a < MOD) ? a : (a - MOD); b = (b < MOD) ? b : (b - MOD); a += b; a = (a < MOD) ? a : (a - MOD); } bool done[123][55][55]; ll memo[123][55][55], v[55]; ll solve(int t, int l, int r) { if (done[t][l][r]) { return memo[t][l][r]; } done[t][l][r] = 0ll; if (!t) { return memo[t][l][r]; } if (l > r) { return memo[t][l][r]; } memo[t][l][r] = max(memo[t][l][r], solve(t - 1, l + 1, r) + v[l]); memo[t][l][r] = max(memo[t][l][r], solve(t - 1, l, r - 1) + v[r]); if (t > 1) { memo[t][l][r] = max(memo[t][l][r], solve(t - 2, l + 1, r)); memo[t][l][r] = max(memo[t][l][r], solve(t - 2, l, r - 1)); } return memo[t][l][r]; } int main(void) { int n, k, i; cin >> n >> k; for (i = 0; i < n; ++i) { cin >> v[i]; } cout << solve(k, 0, n - 1) << endl; return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; const int INF = (1 << 30); const ll INFLL = (1ll << 60); const ll MOD = (ll)(1e9 + 7); #define l_ength size void mul_mod(ll &a, ll b) { a *= b; a %= MOD; } void add_mod(ll &a, ll b) { a = (a < MOD) ? a : (a - MOD); b = (b < MOD) ? b : (b - MOD); a += b; a = (a < MOD) ? a : (a - MOD); } bool done[123][55][55]; ll memo[123][55][55], v[55]; ll solve(int t, int l, int r) { if (done[t][l][r]) { return memo[t][l][r]; } done[t][l][r] = true; if (!t) { return memo[t][l][r]; } if (l > r) { return memo[t][l][r]; } memo[t][l][r] = max(memo[t][l][r], solve(t - 1, l + 1, r) + v[l]); memo[t][l][r] = max(memo[t][l][r], solve(t - 1, l, r - 1) + v[r]); if (t > 1) { memo[t][l][r] = max(memo[t][l][r], solve(t - 2, l + 1, r)); memo[t][l][r] = max(memo[t][l][r], solve(t - 2, l, r - 1)); } return memo[t][l][r]; } int main(void) { int n, k, i; cin >> n >> k; for (i = 0; i < n; ++i) { cin >> v[i]; } cout << solve(k, 0, n - 1) << endl; return 0; }
replace
28
29
28
29
TLE
p03032
C++
Time Limit Exceeded
// ↓template↓ #include "bits/stdc++.h" using namespace std; #define Would #define you #define all(n) n.begin(), n.end() #define rall(n) n.rbegin(), n.rend() typedef long long ll; const long long INF = 1e18; const long long MOD = 1e9 + 7; const double pi = acos(-1); const long long SIZE = 1 << 17; int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}, alp[30]; long long fac[200005], finv[200005], inv[200005]; vector<long long> dij; struct edge { long long to, cost; }; vector<vector<edge>> G; long long mod_pow(long long a, long long b) { long long res = 1, mul = a; for (int i = 0; i < 31; ++i) { if (b >> i & 1) { res *= mul; res %= MOD; } mul = (mul * mul) % MOD; } return res; } void addedge(int from, int to, int cost) { G[from].push_back({to, cost}); G[to].push_back({from, cost}); } template <typename T> 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 a, b; vector<ll> v(a); ll dp(ll a, ll b, ll c); vector<vector<vector<ll>>> m; int main() { cin >> a >> b; v.resize(a); for (int i = 0; i < a; ++i) { cin >> v[i]; } m.resize(a, vector<std::vector<ll>>(a, std::vector<ll>(b + 1, -1))); cout << dp(0, a - 1, b) << endl; } ll dp(ll a, ll b, ll c) { if (b < a || c < 0) { return -INF; } if (c == 0) { return 0; } if (a == b) { return max(0ll, v[a]); } ll aa = dp(a + 1, b, c - 1) + v[a], bb = dp(a + 1, b, c - 2), cc = dp(a, b - 1, c - 1) + v[b], dd = dp(a, b - 1, c - 2); return m[a][b][c] = max({aa, bb, cc, dd, 0ll}); }
// ↓template↓ #include "bits/stdc++.h" using namespace std; #define Would #define you #define all(n) n.begin(), n.end() #define rall(n) n.rbegin(), n.rend() typedef long long ll; const long long INF = 1e18; const long long MOD = 1e9 + 7; const double pi = acos(-1); const long long SIZE = 1 << 17; int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}, alp[30]; long long fac[200005], finv[200005], inv[200005]; vector<long long> dij; struct edge { long long to, cost; }; vector<vector<edge>> G; long long mod_pow(long long a, long long b) { long long res = 1, mul = a; for (int i = 0; i < 31; ++i) { if (b >> i & 1) { res *= mul; res %= MOD; } mul = (mul * mul) % MOD; } return res; } void addedge(int from, int to, int cost) { G[from].push_back({to, cost}); G[to].push_back({from, cost}); } template <typename T> 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 a, b; vector<ll> v(a); ll dp(ll a, ll b, ll c); vector<vector<vector<ll>>> m; int main() { cin >> a >> b; v.resize(a); for (int i = 0; i < a; ++i) { cin >> v[i]; } m.resize(a, vector<std::vector<ll>>(a, std::vector<ll>(b + 1, -1))); cout << dp(0, a - 1, b) << endl; } ll dp(ll a, ll b, ll c) { if (b < a || c < 0) { return -INF; } if (c == 0) { return 0; } if (a == b) { return max(0ll, v[a]); } if (m[a][b][c] != -1) { return m[a][b][c]; } ll aa = dp(a + 1, b, c - 1) + v[a], bb = dp(a + 1, b, c - 2), cc = dp(a, b - 1, c - 1) + v[b], dd = dp(a, b - 1, c - 2); return m[a][b][c] = max({aa, bb, cc, dd, 0ll}); }
insert
83
83
83
86
TLE
p03032
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <vector> typedef long long ll; using namespace std; int main(void) { int n, k, i, j, s, t, v[50]; cin >> n >> k; for (i = 0; i < n; i++) { cin >> v[i]; } int r = min(n, k); int suma[51], sumb[51], sum, max = 0; suma[0] = sumb[0] = 0; for (i = 0; i < n; i++) { suma[i + 1] = suma[i] + v[i]; sumb[i + 1] = sumb[i] + v[n - 1 - i]; } int m[51], mn; for (i = 0; i <= r; i++) { for (j = 0; j <= r; j++) { t = k - (i + j); if (t < 0) { break; } mn = 0; sum = suma[i] + sumb[j]; for (s = 0; s < i; s++) { if (v[s] < 0) { m[mn] = v[s]; mn++; } } for (s = 0; s < j; s++) { if (v[n - 1 - s] < 0) { m[mn] = v[n - 1 - s]; mn++; } } sort(m, m + mn); for (s = 0; s < t && s < mn; s++) { sum -= m[s]; } if (sum > max) max = sum; } } cout << max << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <vector> typedef long long ll; using namespace std; int main(void) { int n, k, i, j, s, t, v[50]; cin >> n >> k; for (i = 0; i < n; i++) { cin >> v[i]; } int r = min(n, k); int suma[51], sumb[51], sum, max = 0; suma[0] = sumb[0] = 0; for (i = 0; i < n; i++) { suma[i + 1] = suma[i] + v[i]; sumb[i + 1] = sumb[i] + v[n - 1 - i]; } int m[51], mn; for (i = 0; i <= r; i++) { for (j = 0; j <= r; j++) { t = k - (i + j); if (t < 0 || (i + j) > n) { break; } mn = 0; sum = suma[i] + sumb[j]; for (s = 0; s < i; s++) { if (v[s] < 0) { m[mn] = v[s]; mn++; } } for (s = 0; s < j; s++) { if (v[n - 1 - s] < 0) { m[mn] = v[n - 1 - s]; mn++; } } sort(m, m + mn); for (s = 0; s < t && s < mn; s++) { sum -= m[s]; } if (sum > max) max = sum; } } cout << max << endl; return 0; }
replace
30
31
30
31
0
p03032
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <regex> #include <set> #include <vector> using namespace std; using ll = long long; using Lf = long double; using plong = pair<ll, ll>; const int mod = 1000000007; int main() { ll N, K; cin >> N >> K; deque<ll> _V; for (ll i = 0; i < N; i++) { ll v; cin >> v; _V.push_front(v); } const ll INF = 99999999999; ll res = -INF; for (ll k = 0; k <= K; k++) { for (ll a = 0; a <= k; a++) { for (ll b = 0; a + b <= k; b++) { ll tmp = 0; vector<ll> have; deque<ll> V = _V; for (ll i = 0; i < a; i++) { have.push_back(V.front()); V.pop_front(); } for (ll i = 0; i < b; i++) { have.push_back(V.back()); V.pop_back(); } sort(have.begin(), have.end()); ll ind = 0; if (have.size() != 0) { for (ll i = 0; i < K - k; i++) { if (i == have.size() - 1) break; if (have[i] < 0) { ind++; } } } for (ll i = ind; i < have.size(); i++) { tmp += have[i]; } // cout<<"a:"<<a<<", b:"<<b<<":"<<tmp<<endl; res = max(res, tmp); } } } cout << res << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <regex> #include <set> #include <vector> using namespace std; using ll = long long; using Lf = long double; using plong = pair<ll, ll>; const int mod = 1000000007; int main() { ll N, K; cin >> N >> K; deque<ll> _V; for (ll i = 0; i < N; i++) { ll v; cin >> v; _V.push_front(v); } const ll INF = 99999999999; ll res = -INF; for (ll k = 0; k <= K; k++) { for (ll a = 0; a <= min(N, k); a++) { for (ll b = 0; a + b <= min(N, k); b++) { ll tmp = 0; vector<ll> have; deque<ll> V = _V; for (ll i = 0; i < a; i++) { have.push_back(V.front()); V.pop_front(); } for (ll i = 0; i < b; i++) { have.push_back(V.back()); V.pop_back(); } sort(have.begin(), have.end()); ll ind = 0; if (have.size() != 0) { for (ll i = 0; i < K - k; i++) { if (i == have.size() - 1) break; if (have[i] < 0) { ind++; } } } for (ll i = ind; i < have.size(); i++) { tmp += have[i]; } // cout<<"a:"<<a<<", b:"<<b<<":"<<tmp<<endl; res = max(res, tmp); } } } cout << res << endl; }
replace
28
30
28
30
0
p03032
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <limits> #include <map> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; #define LL long long #define MOD (1000000007) // 129 /* int main() { LL H, W; scanf("%lld %lld", &H, &W); static int hor_label[2020][2020]; static int var_label[2020][2020]; static int hor_label_to_count[2020 * 2020]; static int var_label_to_count[2020 * 2020]; memset(hor_label, 0, sizeof(hor_label)); memset(var_label, 0, sizeof(var_label)); memset(hor_label_to_count, 0, sizeof(hor_label_to_count)); memset(var_label_to_count, 0, sizeof(var_label_to_count)); char s[2020]; int hor_curr_label = 1; int var_curr_label = 1; for (int j = 0; j < H; j++) { scanf("%s", &s); for (int i = 0; i < W; i++) { if (s[i] == '.') { hor_label[j][i] = hor_curr_label; hor_label_to_count[hor_curr_label]++; if (j == 0) { var_label[j][i] = var_curr_label; var_label_to_count[var_curr_label]++; var_curr_label++; } else { int abobe_label = var_label[j - 1][i]; if (abobe_label > 0) { var_label[j][i] = abobe_label; var_label_to_count[abobe_label]++; } else { var_label[j][i] = var_curr_label; var_label_to_count[var_curr_label]++; var_curr_label++; } } } else { hor_curr_label++; } } hor_curr_label++; } LL result = 0; for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { if (hor_label[j][i] == 0) { continue; } LL count = hor_label_to_count[hor_label[j][i]] + var_label_to_count[var_label[j][i]] - 1; result = max(count, result); } } printf("%lld\n", result); return 0; } */ // 128 int main() { LL N, K; scanf("%lld %lld", &N, &K); vector<LL> V(N); vector<LL> Lval(N + 1); Lval[0] = 0; for (int i = 0; i < N; i++) { scanf("%lld", &V[i]); Lval[i + 1] = Lval[i] + V[i]; } vector<LL> Rval(N + 1); Rval[0] = 0; for (int i = 0; i < N; i++) { Rval[i + 1] = Rval[i] + V[N - i - 1]; } vector<vector<vector<LL>>> Dq(N + 1, vector<vector<LL>>(N + 1, vector<LL>(0))); for (int i = 0; i <= N; i++) { // L for (int j = 0; j <= N - i; j++) { // R for (int l = 0; l < i; l++) { if (V[l] < 0) { Dq[i][j].push_back(V[l]); } } for (int r = 0; r < j; r++) { if (V[N - r - 1] < 0) { Dq[i][j].push_back(V[N - r - 1]); } } if (Dq[i][j].size() > 0) { std::sort(Dq[i][j].begin(), Dq[i][j].end()); } } } LL result = 0; for (int l = 0; l <= K; l++) { for (int r = 0; r <= K - l; r++) { for (int d = 0; d <= K - l - r; d++) { LL value = Lval[l] + Rval[r]; int dis = min(d, (int)Dq[l][r].size()); for (int i = 0; i < dis; i++) { value -= Dq[l][r][i]; } result = max(result, value); } } } printf("%lld\n", result); return 0; } // 127 /* struct IntPair { LL B, C; }; int main() { LL N, M; scanf("%lld %lld", &N, &M); vector<LL> A; for (int i = 0; i < N; i++) { LL val; scanf("%lld", &val); A.push_back(val); } vector<IntPair> P; for (int i = 0; i < M; i++) { LL val1, val2; scanf("%lld %lld", &val1, &val2); IntPair pair; pair.B = val1; pair.C = val2; P.push_back(pair); } std::sort(A.begin(), A.end()); std::sort(P.begin(), P.end(), [](const IntPair &a, const IntPair &b) { return a.C > b.C; }); LL indexA = 0; bool finish = false; for (auto item : P) { for (int i = 0; i < item.B; i++) { if (item.C > A[indexA]) { A[indexA] = item.C; } else { finish = true; break; } indexA++; if (indexA >= N) { finish = true; break; } } if (finish) { break; } } LL result = 0; for (auto item : A) { result += item; } printf("%lld\n", result); return 0; } */ // 126 /* struct Node { unsigned int connect; unsigned int weight; }; int main() { LL N; scanf("%lld", &N); static vector<Node> node[100010]; static char color[100010]; for (int i = 0; i < N - 1; i++) { unsigned int u, v, w; scanf("%d %d %d", &u, &v, &w); Node n1; n1.connect = v; n1.weight = w; node[u].push_back(n1); Node n2; n2.connect = u; n2.weight = w; node[v].push_back(n2); } for (int i = 0; i <= N; i++) { color[i] = -1; } std::queue<unsigned int> q; color[1] = 0; q.push(1); //���D��T�� while (q.size() > 0) { unsigned int index = q.front(); q.pop(); char c = color[index]; for (auto item : node[index]) { if (color[item.connect] < 0) { //�܂��ʂ��Ă��Ȃ��m�[�h color[item.connect] = item.weight % 2 == 0 ? c : 1 - c; q.push(item.connect); } } } for (int i = 1; i <= N; i++) { printf("%d\n", color[i]); } return 0; } */
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <limits> #include <map> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; #define LL long long #define MOD (1000000007) // 129 /* int main() { LL H, W; scanf("%lld %lld", &H, &W); static int hor_label[2020][2020]; static int var_label[2020][2020]; static int hor_label_to_count[2020 * 2020]; static int var_label_to_count[2020 * 2020]; memset(hor_label, 0, sizeof(hor_label)); memset(var_label, 0, sizeof(var_label)); memset(hor_label_to_count, 0, sizeof(hor_label_to_count)); memset(var_label_to_count, 0, sizeof(var_label_to_count)); char s[2020]; int hor_curr_label = 1; int var_curr_label = 1; for (int j = 0; j < H; j++) { scanf("%s", &s); for (int i = 0; i < W; i++) { if (s[i] == '.') { hor_label[j][i] = hor_curr_label; hor_label_to_count[hor_curr_label]++; if (j == 0) { var_label[j][i] = var_curr_label; var_label_to_count[var_curr_label]++; var_curr_label++; } else { int abobe_label = var_label[j - 1][i]; if (abobe_label > 0) { var_label[j][i] = abobe_label; var_label_to_count[abobe_label]++; } else { var_label[j][i] = var_curr_label; var_label_to_count[var_curr_label]++; var_curr_label++; } } } else { hor_curr_label++; } } hor_curr_label++; } LL result = 0; for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { if (hor_label[j][i] == 0) { continue; } LL count = hor_label_to_count[hor_label[j][i]] + var_label_to_count[var_label[j][i]] - 1; result = max(count, result); } } printf("%lld\n", result); return 0; } */ // 128 int main() { LL N, K; scanf("%lld %lld", &N, &K); vector<LL> V(N); vector<LL> Lval(N + 1); Lval[0] = 0; for (int i = 0; i < N; i++) { scanf("%lld", &V[i]); Lval[i + 1] = Lval[i] + V[i]; } vector<LL> Rval(N + 1); Rval[0] = 0; for (int i = 0; i < N; i++) { Rval[i + 1] = Rval[i] + V[N - i - 1]; } vector<vector<vector<LL>>> Dq(N + 1, vector<vector<LL>>(N + 1, vector<LL>(0))); for (int i = 0; i <= N; i++) { // L for (int j = 0; j <= N - i; j++) { // R for (int l = 0; l < i; l++) { if (V[l] < 0) { Dq[i][j].push_back(V[l]); } } for (int r = 0; r < j; r++) { if (V[N - r - 1] < 0) { Dq[i][j].push_back(V[N - r - 1]); } } if (Dq[i][j].size() > 0) { std::sort(Dq[i][j].begin(), Dq[i][j].end()); } } } LL result = 0; LL max_get = min(N, K); for (int l = 0; l <= max_get; l++) { for (int r = 0; r <= max_get - l; r++) { for (int d = 0; d <= K - l - r; d++) { LL value = Lval[l] + Rval[r]; int dis = min(d, (int)Dq[l][r].size()); for (int i = 0; i < dis; i++) { value -= Dq[l][r][i]; } result = max(result, value); } } } printf("%lld\n", result); return 0; } // 127 /* struct IntPair { LL B, C; }; int main() { LL N, M; scanf("%lld %lld", &N, &M); vector<LL> A; for (int i = 0; i < N; i++) { LL val; scanf("%lld", &val); A.push_back(val); } vector<IntPair> P; for (int i = 0; i < M; i++) { LL val1, val2; scanf("%lld %lld", &val1, &val2); IntPair pair; pair.B = val1; pair.C = val2; P.push_back(pair); } std::sort(A.begin(), A.end()); std::sort(P.begin(), P.end(), [](const IntPair &a, const IntPair &b) { return a.C > b.C; }); LL indexA = 0; bool finish = false; for (auto item : P) { for (int i = 0; i < item.B; i++) { if (item.C > A[indexA]) { A[indexA] = item.C; } else { finish = true; break; } indexA++; if (indexA >= N) { finish = true; break; } } if (finish) { break; } } LL result = 0; for (auto item : A) { result += item; } printf("%lld\n", result); return 0; } */ // 126 /* struct Node { unsigned int connect; unsigned int weight; }; int main() { LL N; scanf("%lld", &N); static vector<Node> node[100010]; static char color[100010]; for (int i = 0; i < N - 1; i++) { unsigned int u, v, w; scanf("%d %d %d", &u, &v, &w); Node n1; n1.connect = v; n1.weight = w; node[u].push_back(n1); Node n2; n2.connect = u; n2.weight = w; node[v].push_back(n2); } for (int i = 0; i <= N; i++) { color[i] = -1; } std::queue<unsigned int> q; color[1] = 0; q.push(1); //���D��T�� while (q.size() > 0) { unsigned int index = q.front(); q.pop(); char c = color[index]; for (auto item : node[index]) { if (color[item.connect] < 0) { //�܂��ʂ��Ă��Ȃ��m�[�h color[item.connect] = item.weight % 2 == 0 ? c : 1 - c; q.push(item.connect); } } } for (int i = 1; i <= N; i++) { printf("%d\n", color[i]); } return 0; } */
replace
134
136
134
138
0
p03032
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; using ld = long double; using P = pair<ll, ll>; using Graph = vector<vector<int>>; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; #define rep(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++) #define INF 1000000000000000000 #define MAX 200001 #define MOD 1000000007 template <typename T> inline string toString(const T &a) { ostringstream oss; oss << a; return oss.str(); }; int main() { int N, K; ll res = 0; cin >> N >> K; deque<int> dq; for (int i = 0; i < N; i++) { int v; cin >> v; dq.push_back(v); } for (int i = 0; i <= K; i++) { for (int j = 0; j <= K; j++) { if (i + j > K) continue; auto dq_copy = dq; int re = K - i - j; vector<int> hou; int idx = 0; while (i != idx) { hou.push_back(dq_copy.front()); dq_copy.pop_front(); idx++; } idx = 0; while (j != idx) { hou.push_back(dq_copy.back()); dq_copy.pop_back(); idx++; } ll ans = 0; sort(hou.begin(), hou.end()); int s = hou.size(); for (int k = 0; k < s; k++) { if (hou[k] >= 0 || re == 0) ans += hou[k]; else re--; } res = max(res, ans); } } cout << res << endl; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; using ld = long double; using P = pair<ll, ll>; using Graph = vector<vector<int>>; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; #define rep(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++) #define INF 1000000000000000000 #define MAX 200001 #define MOD 1000000007 template <typename T> inline string toString(const T &a) { ostringstream oss; oss << a; return oss.str(); }; int main() { int N, K; ll res = 0; cin >> N >> K; deque<int> dq; for (int i = 0; i < N; i++) { int v; cin >> v; dq.push_back(v); } for (int i = 0; i <= K; i++) { for (int j = 0; j <= K; j++) { if (i + j > K || i + j > N) continue; auto dq_copy = dq; int re = K - i - j; vector<int> hou; int idx = 0; while (i != idx) { hou.push_back(dq_copy.front()); dq_copy.pop_front(); idx++; } idx = 0; while (j != idx) { hou.push_back(dq_copy.back()); dq_copy.pop_back(); idx++; } ll ans = 0; sort(hou.begin(), hou.end()); int s = hou.size(); for (int k = 0; k < s; k++) { if (hou[k] >= 0 || re == 0) ans += hou[k]; else re--; } res = max(res, ans); } } cout << res << endl; }
replace
41
42
41
42
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v.at(i); } int uu = 0; for (int i = 0; i < k + 1; i++) { for (int j = 0; j < k - i + 1; j++) { vector<int> a; for (int l = 0; l < i; l++) { a.push_back(v.at(l)); } for (int l = 0; l < j; l++) { a.push_back(v.at(n - l - 1)); } sort(a.begin(), a.end()); int yyyy = a.size(); int f = min(k - i - j, yyyy); for (int l = 0; l < f; l++) { if (a.at(l) < 0) { a.at(l) = 0; } else { break; } } int r = 0; for (int l = 0; l < a.size(); l++) { r += a.at(l); } uu = max(uu, r); } } cout << uu << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v.at(i); } int uu = 0; for (int i = 0; i < min(k + 1, n + 1); i++) { for (int j = 0; j < min(k - i + 1, n + 1 - i); j++) { vector<int> a; for (int l = 0; l < i; l++) { a.push_back(v.at(l)); } for (int l = 0; l < j; l++) { a.push_back(v.at(n - l - 1)); } sort(a.begin(), a.end()); int yyyy = a.size(); int f = min(k - i - j, yyyy); for (int l = 0; l < f; l++) { if (a.at(l) < 0) { a.at(l) = 0; } else { break; } } int r = 0; for (int l = 0; l < a.size(); l++) { r += a.at(l); } uu = max(uu, r); } } cout << uu << endl; }
replace
11
13
11
13
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; using namespace std; int main() { int n, k; cin >> n >> k; int v[55], sort_lr[55]; for (int i = 0; i < n; i++) { cin >> v[i]; } int ans = 0; for (int left = 0; left <= k; left++) { for (int right = 0; right <= k - left; right++) { for (int i = 0; i <= left - 1; i++) { sort_lr[i] = v[i]; } for (int i = 0; i <= right - 1; i++) { sort_lr[i + left] = v[n - 1 - i]; } sort(sort_lr, sort_lr + (left + right)); int have = 0; for (int i = 0; i < min(left + right, k - (left + right)); i++) { if (sort_lr[i] > 0) have += sort_lr[i]; } for (int i = k - (left + right); i <= left + right - 1; i++) { have += sort_lr[i]; } ans = max(ans, have); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; int main() { int n, k; cin >> n >> k; int v[55], sort_lr[55]; for (int i = 0; i < n; i++) { cin >> v[i]; } int ans = 0; for (int left = 0; left <= k; left++) { for (int right = 0; right <= min(n - left, k - left); right++) { for (int i = 0; i <= left - 1; i++) { sort_lr[i] = v[i]; } for (int i = 0; i <= right - 1; i++) { sort_lr[i + left] = v[n - 1 - i]; } sort(sort_lr, sort_lr + (left + right)); int have = 0; for (int i = 0; i < min(left + right, k - (left + right)); i++) { if (sort_lr[i] > 0) have += sort_lr[i]; } for (int i = k - (left + right); i <= left + right - 1; i++) { have += sort_lr[i]; } ans = max(ans, have); } } cout << ans << endl; return 0; }
replace
13
14
13
14
0
p03032
C++
Runtime Error
#pragma GCC optimize("O3") #pragma GCC target("sse4") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef pair<int, int> pi; #define debug(x) cout << #x << ": " << x << endl #define repn(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, a) for (int i = 0; i < (int)(a); i++) #define all(v) v.begin(), v.end() #define mp make_pair #define pb push_back #define fi first #define se second #define endl '\n' int gcd(int a, int b) { return ((b == 0) ? a : gcd(b, a % b)); } int n, k; ll v[55]; ll dp[55][55][105]; ll solve(int l, int r, int x) { if (x < 0) return -1e9; if (x == 0 || l > r) return 0; if (dp[l][r][x] != -1) return dp[l][r][x]; ll op1 = solve(l + 1, r, x - 1) + v[l]; ll op2 = solve(l, r - 1, x - 1) + v[r]; ll op3 = solve(l + 1, r, x - 2); ll op4 = solve(l, r + 1, x - 2); ll op5 = 0; ll ret = max({op1, op2, op3, op4, op5}); return dp[l][r][x] = ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); // freopen("input.in", "r", stdin); // freopen("output.out", "w", stdout); memset(dp, -1, sizeof(dp)); cin >> n >> k; rep(i, n) cin >> v[i]; cout << solve(0, n - 1, k) << endl; return 0; } /* Things to look out for: - Integer overflows - Array bounds - Special cases Be careful! */
#pragma GCC optimize("O3") #pragma GCC target("sse4") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef pair<int, int> pi; #define debug(x) cout << #x << ": " << x << endl #define repn(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, a) for (int i = 0; i < (int)(a); i++) #define all(v) v.begin(), v.end() #define mp make_pair #define pb push_back #define fi first #define se second #define endl '\n' int gcd(int a, int b) { return ((b == 0) ? a : gcd(b, a % b)); } int n, k; ll v[55]; ll dp[55][55][105]; ll solve(int l, int r, int x) { if (x < 0) return -1e9; if (x == 0 || l > r) return 0; if (dp[l][r][x] != -1) return dp[l][r][x]; ll op1 = solve(l + 1, r, x - 1) + v[l]; ll op2 = solve(l, r - 1, x - 1) + v[r]; ll op3 = solve(l + 1, r, x - 2); ll op4 = solve(l, r - 1, x - 2); ll op5 = 0; ll ret = max({op1, op2, op3, op4, op5}); return dp[l][r][x] = ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); // freopen("input.in", "r", stdin); // freopen("output.out", "w", stdout); memset(dp, -1, sizeof(dp)); cin >> n >> k; rep(i, n) cin >> v[i]; cout << solve(0, n - 1, k) << endl; return 0; } /* Things to look out for: - Integer overflows - Array bounds - Special cases Be careful! */
replace
36
37
36
37
0
p03032
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; int n; vector<int> shift(int x, vector<int> v) { vector<int> ans(n); ans = v; int tmp = ans[n - 1]; for (int i = x; i <= n - 1; i++) ans[i] = v[i - 1]; ans[x - 1] = tmp; return ans; } void print(vector<int> a) { rep(i, n) printf("%d ", a[i]); puts(""); } int main() { int k; cin >> n >> k; vector<int> v(n); rep(i, n) cin >> v[i]; int ans = 0; rep(x, k) { if (x != 0) v = shift(x, v); // print(v); for (int i = 1; i <= k && i <= n; ++i) { int tmp = 0; priority_queue<int, vector<int>, greater<int>> q; for (int j = i; j > 0; j--) { q.push(v[j - 1]); tmp += v[j - 1]; } for (int j = k - i; j > 0; j--) { int bk = q.top(); if (!q.empty() && bk < 0) { q.pop(); tmp -= bk; } else break; } ans = max(ans, tmp); } } cout << ans; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; int n; vector<int> shift(int x, vector<int> v) { vector<int> ans(n); ans = v; int tmp = ans[n - 1]; for (int i = x; i <= n - 1; i++) ans[i] = v[i - 1]; ans[x - 1] = tmp; return ans; } void print(vector<int> a) { rep(i, n) printf("%d ", a[i]); puts(""); } int main() { int k; cin >> n >> k; vector<int> v(n); rep(i, n) cin >> v[i]; int ans = 0; rep(x, min(k, n)) { if (x != 0) v = shift(x, v); // print(v); for (int i = 1; i <= k && i <= n; ++i) { int tmp = 0; priority_queue<int, vector<int>, greater<int>> q; for (int j = i; j > 0; j--) { q.push(v[j - 1]); tmp += v[j - 1]; } for (int j = k - i; j > 0; j--) { int bk = q.top(); if (!q.empty() && bk < 0) { q.pop(); tmp -= bk; } else break; } ans = max(ans, tmp); } } cout << ans; }
replace
24
25
24
25
0
p03032
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n, k; cin >> n >> k; vector<ll> D; for (int i = 0; i < n; i++) { ll tmp; cin >> tmp; D.push_back(tmp); } ll max; max = -INT_MAX; for (int a = 0; a <= k; a++) { for (int b = 0; b <= k - a; b++) { vector<ll> V; vector<ll> D_tmp; D_tmp = D; for (int i = 0; i < a; i++) { V.push_back(D_tmp.front()); D_tmp.erase(D_tmp.begin()); } for (int j = 0; j < b; j++) { V.push_back(D_tmp.back()); D_tmp.pop_back(); } if (V.empty() == 1) { continue; } sort(V.begin(), V.end()); for (int i = 0; i < k - a - b; i++) { if (V.empty() == 1) { break; } if (V.front() < 0) { V.erase(V.begin()); } } ll tmp; tmp = accumulate(V.begin(), V.end(), 0); if (max < tmp) { max = tmp; } } } cout << max; return 0; }
#include <algorithm> #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n, k; cin >> n >> k; vector<ll> D; for (int i = 0; i < n; i++) { ll tmp; cin >> tmp; D.push_back(tmp); } ll max; max = -INT_MAX; int m; m = min(n, k); for (int a = 0; a <= m; a++) { for (int b = 0; b <= m - a; b++) { vector<ll> V; vector<ll> D_tmp; D_tmp = D; for (int i = 0; i < a; i++) { V.push_back(D_tmp.front()); D_tmp.erase(D_tmp.begin()); } for (int j = 0; j < b; j++) { V.push_back(D_tmp.back()); D_tmp.pop_back(); } if (V.empty() == 1) { continue; } sort(V.begin(), V.end()); for (int i = 0; i < k - a - b; i++) { if (V.empty() == 1) { break; } if (V.front() < 0) { V.erase(V.begin()); } } ll tmp; tmp = accumulate(V.begin(), V.end(), 0); if (max < tmp) { max = tmp; } } } cout << max; return 0; }
replace
20
22
20
25
0
p03032
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iomanip> #include <iostream> #include <limits.h> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <stdlib.h> #include <string> #include <vector> #define ll long long #define rep2(i, a, b) for (ll i = a; i <= b; i++) #define rep(i, n) for (ll i = 0; i < n; i++) #define rep3(i, a, b) for (int i = a; i >= b; i--) #define REP(e, v) for (auto e : v) #define queint queue<int> #define pii pair<int, int> #define pll pair<ll, ll> #define pq priority_queue<int> // 大きい順 #define pqg priority_queue<int, vec, greater<int>> // 小さい順 #define pb push_back #define vec vector<int> #define vecvec vector<vector<int>> #define vecll vector<ll> #define vecvecll vector<vector<ll>> #define bs binary_search #define All(c) (c).begin(), (c).end() #define mp make_pair using namespace std; int in() { int x; scanf("%d", &x); return x; } string stin() { string s; cin >> s; return s; } ll lin() { ll x; scanf("%lld", &x); return x; } int main() { int n = in(); int k = in(); deque<int> q; rep(i, n) { q.push_back(in()); } ll ans = 0; for (int x = 0; x <= max((ll)k, (ll)q.size()); x++) { for (int y = 0; x + y <= max((ll)k, (ll)q.size()); y++) { deque<int> p = q; vec a(x + y); rep(i, x) { a[i] = p.front(); p.pop_front(); } rep(i, y) { a[i + x] = p.back(); p.pop_back(); } sort(All(a)); ll tmp = 0; ll sum = 0; rep(i, x + y) sum += a[i]; tmp = sum; for (int i = 0; i < k - x - y & i < a.size(); i++) { sum -= a[i]; tmp = max(tmp, sum); } ans = max(ans, tmp); } } cout << ans << endl; }
#include <algorithm> #include <cstdio> #include <iomanip> #include <iostream> #include <limits.h> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <stdlib.h> #include <string> #include <vector> #define ll long long #define rep2(i, a, b) for (ll i = a; i <= b; i++) #define rep(i, n) for (ll i = 0; i < n; i++) #define rep3(i, a, b) for (int i = a; i >= b; i--) #define REP(e, v) for (auto e : v) #define queint queue<int> #define pii pair<int, int> #define pll pair<ll, ll> #define pq priority_queue<int> // 大きい順 #define pqg priority_queue<int, vec, greater<int>> // 小さい順 #define pb push_back #define vec vector<int> #define vecvec vector<vector<int>> #define vecll vector<ll> #define vecvecll vector<vector<ll>> #define bs binary_search #define All(c) (c).begin(), (c).end() #define mp make_pair using namespace std; int in() { int x; scanf("%d", &x); return x; } string stin() { string s; cin >> s; return s; } ll lin() { ll x; scanf("%lld", &x); return x; } int main() { int n = in(); int k = in(); deque<int> q; rep(i, n) { q.push_back(in()); } ll ans = 0; for (int x = 0; x <= k; x++) { for (int y = 0; x + y <= min((ll)k, (ll)q.size()); y++) { deque<int> p = q; vec a(x + y); rep(i, x) { a[i] = p.front(); p.pop_front(); } rep(i, y) { a[i + x] = p.back(); p.pop_back(); } sort(All(a)); ll tmp = 0; ll sum = 0; rep(i, x + y) sum += a[i]; tmp = sum; for (int i = 0; i < k - x - y & i < a.size(); i++) { sum -= a[i]; tmp = max(tmp, sum); } ans = max(ans, tmp); } } cout << ans << endl; }
replace
52
54
52
54
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(a) (a).begin(), (a).end() using namespace std; using ll = long long int; using vec = vector<int>; const int INF = 1e9 + 7; int main() { int n, k; cin >> n >> k; vec v(n); rep(i, n) { cin >> v[i]; } int m = 0; vec o; int sum_a = 0; rep(a, min(n, k) + 1) { int sum_b = 0; vec o1 = o; rep(b, min(n, k) - a + 1) { sort(all(o1)); int sum_c = 0; rep(i, min(k - a - b, a + b) + 1) { m = max(m, sum_a + sum_b - sum_c); sum_c += o1[i]; } if (n - b - 1 >= 0) { o1.push_back(v[n - b - 1]); sum_b += v[n - b - 1]; } } if (a < n) { o.push_back(v[a]); sum_a += v[a]; } } cout << m << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(a) (a).begin(), (a).end() using namespace std; using ll = long long int; using vec = vector<int>; const int INF = 1e9 + 7; int main() { int n, k; cin >> n >> k; vec v(n); rep(i, n) { cin >> v[i]; } int m = 0; vec o; int sum_a = 0; rep(a, min(n, k) + 1) { int sum_b = 0; vec o1 = o; rep(b, min(n, k) - a + 1) { sort(all(o1)); int sum_c = 0; rep(i, min(k - a - b, a + b) + 1) { m = max(m, sum_a + sum_b - sum_c); if (i < a + b) sum_c += o1[i]; } if (n - b - 1 >= 0) { o1.push_back(v[n - b - 1]); sum_b += v[n - b - 1]; } } if (a < n) { o.push_back(v[a]); sum_a += v[a]; } } cout << m << endl; }
replace
27
28
27
29
-11
p03032
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <chrono> #include <iostream> #include <random> #include <string> #include <vector> int main(void) { int n, k; std::cin >> n >> k; std::vector<int> v(n); for (int i = 0; i < n; i++) std::cin >> v[i]; std::vector<int> hand(n); int64_t ans = 0; for (int popleft = 0; popleft <= k; popleft++) { for (int popright = 0; popright <= k - popleft; popright++) { for (int i = 0; i < popleft; i++) { hand[i] = v[i]; } for (int i = 0; i < popright; i++) { hand[i + popleft] = v[n - 1 - i]; } std::sort(hand.begin(), hand.begin() + popleft + popright); for (int push = 0; push <= k - popleft - popright; push++) { int64_t sum = 0; for (int i = push; i < popleft + popright; i++) { sum += hand[i]; } ans = std::max(sum, ans); } } } std::cout << ans << std::endl; }
#include <algorithm> #include <array> #include <bitset> #include <chrono> #include <iostream> #include <random> #include <string> #include <vector> int main(void) { int n, k; std::cin >> n >> k; std::vector<int> v(n); for (int i = 0; i < n; i++) std::cin >> v[i]; std::vector<int> hand(n); int64_t ans = 0; for (int popleft = 0; popleft <= k && popleft <= n; popleft++) { for (int popright = 0; popright <= k - popleft && popleft + popright <= n; popright++) { for (int i = 0; i < popleft; i++) { hand[i] = v[i]; } for (int i = 0; i < popright; i++) { hand[i + popleft] = v[n - 1 - i]; } std::sort(hand.begin(), hand.begin() + popleft + popright); for (int push = 0; push <= k - popleft - popright; push++) { int64_t sum = 0; for (int i = push; i < popleft + popright; i++) { sum += hand[i]; } ans = std::max(sum, ans); } } } std::cout << ans << std::endl; }
replace
17
19
17
20
0
p03032
C++
Runtime Error
#ifdef _WIN32 #define DEBUG 1 #endif #define _CRT_SECURE_NO_WARNINGS #define MATH_PI 3.14159265358979323846264338327950288419716939 #include <fstream> #include <iostream> #include <math.h> #include <string.h> using namespace std; #ifdef DEBUG #include "include.h" #else inline signed wait() { return 0; } inline void dout(const char *arg, ...) {} #endif template <typename T> inline void SWAP(T &a, T &b) { T t = a; a = b; b = t; } inline void CSWAP(char *&a, char *&b) { char *t = a; a = b; b = t; } #define CLIP(ptr, min, max) \ { \ if ((min) <= (max)) { \ if (ptr < (min)) { \ ptr = (min); \ } \ if (ptr > (max)) { \ ptr = (max); \ } \ } \ } // #define max(a, b) a > b ? a : b; // #define min(a, b) a > b ? b : a; #define Sin(deg) sin((deg)*MATH_PI / 180) #define Cos(deg) cos((deg)*MATH_PI / 180) #define Tan(deg) tan((deg)*MATH_PI / 180) #define Rad(deg) ((deg)*MATH_PI / 180) #define ff(param, num) for (int param = 0; param < num; ++param) #define fi(num) for (int i = 0; i < num; ++i) #define fj(num) for (int j = 0; j < num; ++j) #define fk(num) for (int k = 0; k < num; ++k) #define fl(num) for (int l = 0; l < num; ++l) #define fn(num) for (int n = 0; n < num; ++n) #define ffr(param, num) for (int param = num - 1; param >= 0; --param) #define fir(num) for (int i = num - 1; i >= 0; --i) #define fjr(num) for (int j = num - 1; j >= 0; --j) #define fkr(num) for (int k = num - 1; k >= 0; --k) #define flr(num) for (int l = num - 1; l >= 0; --l) #define fnr(num) for (int n = num - 1; n >= 0; --n) #define gi(p) Scanf("%d", p) #define gi2(p1, p2) Scanf2("%d %d", p1, p2) #define gi3(p1, p2, p3) Scanf3("%d %d %d", p1, p2, p3) #define gi4(p1, p2, p3, p4) Scanf4("%d %d %d %d", p1, p2, p3, p4) #define gll(p) Scanf("%lld", p) #define gll2(p1, p2) Scanf2("%lld %lld", p1, p2) #define gll3(p1, p2, p3) Scanf3("%lld %lld %lld", p1, p2, p3) #define gll4(p1, p2, p3, p4) Scanf4("%lld %lld %lld %lld", p1, p2, p3, p4) #define gf(p) Scanf("%f", p) #define gf2(p1, p2) Scanf2("%f %f", p1, p2) #define gf3(p1, p2, p3) Scanf3("%f %f %f", p1, p2, p3) #define gf4(p1, p2, p3, p4) Scanf4("%f %f %f %f", p1, p2, p3, p4) #ifdef DEBUG #define gc(buf) fscanf(local_in.fp, "%s", buf) #define Scanf(expr, p) fscanf(local_in.fp, expr, &p) #define Scanf2(expr, p1, p2) fscanf(local_in.fp, expr, &p1, &p2) #define Scanf3(expr, p1, p2, p3) fscanf(local_in.fp, expr, &p1, &p2, &p3) #define Scanf4(expr, p1, p2, p3, p4) \ fscanf(local_in.fp, expr, &p1, &p2, &p3, &p4) #else #define gc(buf) Scanf("%s", buf) #define Scanf(expr, p) scanf(expr, &p) #define Scanf2(expr, p1, p2) scanf(expr, &p1, &p2) #define Scanf3(expr, p1, p2, p3) scanf(expr, &p1, &p2, &p3) #define Scanf4(expr, p1, p2, p3, p4) scanf(expr, &p1, &p2, &p3, &p4) #endif #define ans(p) cout << p << endl; void CombSort(int N, int *ar, int order_ascending = 1) { int h = int(N / 1.3); int flag; int i; while (true) { flag = 0; for (i = 0; i + h < N; ++i) { if ((order_ascending && ar[i] > ar[i + h]) || (!order_ascending && ar[i] < ar[i + h])) { swap<int>(ar[i], ar[i + h]); flag = 1; } } if (h == 1 && !flag) break; if (h == 9 || h == 10) h = 11; if (h > 1) h = int(h / 1.3); } } // int EuclideanAlgorithm(int N, int *ar) { fn(N - 1) { while (true) { if (ar[n] // % ar[n + 1] == 0 || ar[n + 1] % ar[n] == 0) { ar[n + 1] = min(ar[n], ar[n + // 1]); break; }if (ar[n] > ar[n + 1]) { ar[n] %= ar[n + 1]; } else { ar[n + 1] // %= ar[n]; } } }return ar[N - 1]; } template <typename T> void CombSort(int N, T *ar, int order_ascending = 1) { int i, flag; int h = N / 1.3; while (true) { flag = 0; for (i = 0; i + h < N; ++i) { if (order_ascending && ar[i].SortValue > ar[i + h].SortValue || !order_ascending && ar[i].SortValue < ar[i + h].SortValue) { swap<T>(ar[i], ar[i + h]); flag = 1; } } if (h > 1) { h /= 1.3; if (h == 9 || h == 10) h = 11; } else { if (!flag) break; } } } #include <algorithm> #include <vector> int table[40] = {0}; long long int dat[100010] = {0}; using namespace std; struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化 for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); // xの根をrx int ry = root(y); // yの根をry if (rx == ry) return; // xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; char cbuf[128][16]; int mx; void Calc(int *arr1, int narr1, int *arr2, int narr2, int nPush) { int cnt = 0; int nList = 0; int List[50]; fi(narr1) { cnt += arr1[i]; if (arr1[i] < 0) List[nList++] = -arr1[i]; } fi(narr2) { cnt += arr2[i]; if (arr2[i] < 0) List[nList++] = -arr2[i]; } if (nList >= 2) CombSort(nList, List, 0); fi(nPush) { if (i >= nList) break; cnt += List[i]; } mx = max(cnt, mx); // cout << cnt << endl; } void Pop2(int *arr1, int narr1, int *arr2, int narr2, int nPush); void Pop1(int *arr1, int narr1, int *arr2, int narr2, int nPush) { Calc(arr1, narr1, arr2, narr2, nPush); if (narr1 > 0) { --narr1; ++nPush; Pop1(arr1, narr1, arr2, narr2, nPush); } if (narr2 > 0) { --narr2; ++nPush; Pop2(arr1, narr1, arr2, narr2, nPush); } } void Pop2(int *arr1, int narr1, int *arr2, int narr2, int nPush) { Calc(arr1, narr1, arr2, narr2, nPush); if (narr2 > 0) { --narr2; ++nPush; Pop2(arr1, narr1, arr2, narr2, nPush); } } signed main() { int V[50]; int N, K; gi2(N, K); fi(N) gi(V[i]); mx = 0; int Selected[2][50]; int nSelected[2]; int lt, rt; int nPush = 0; for (lt = K; lt >= 0; --lt) { if (lt > N) lt = N; rt = K - lt; nSelected[0] = nSelected[1] = 0; for (int i = 0; i < lt; ++i) { Selected[0][nSelected[0]++] = V[i]; } for (int i = N - 1; i > N - 1 - rt; --i) { Selected[1][nSelected[1]++] = V[i]; } nPush = K - (nSelected[0] + nSelected[1]); Pop1(Selected[0], nSelected[0], Selected[1], nSelected[1], nPush); } ans(mx); return wait(); }
#ifdef _WIN32 #define DEBUG 1 #endif #define _CRT_SECURE_NO_WARNINGS #define MATH_PI 3.14159265358979323846264338327950288419716939 #include <fstream> #include <iostream> #include <math.h> #include <string.h> using namespace std; #ifdef DEBUG #include "include.h" #else inline signed wait() { return 0; } inline void dout(const char *arg, ...) {} #endif template <typename T> inline void SWAP(T &a, T &b) { T t = a; a = b; b = t; } inline void CSWAP(char *&a, char *&b) { char *t = a; a = b; b = t; } #define CLIP(ptr, min, max) \ { \ if ((min) <= (max)) { \ if (ptr < (min)) { \ ptr = (min); \ } \ if (ptr > (max)) { \ ptr = (max); \ } \ } \ } // #define max(a, b) a > b ? a : b; // #define min(a, b) a > b ? b : a; #define Sin(deg) sin((deg)*MATH_PI / 180) #define Cos(deg) cos((deg)*MATH_PI / 180) #define Tan(deg) tan((deg)*MATH_PI / 180) #define Rad(deg) ((deg)*MATH_PI / 180) #define ff(param, num) for (int param = 0; param < num; ++param) #define fi(num) for (int i = 0; i < num; ++i) #define fj(num) for (int j = 0; j < num; ++j) #define fk(num) for (int k = 0; k < num; ++k) #define fl(num) for (int l = 0; l < num; ++l) #define fn(num) for (int n = 0; n < num; ++n) #define ffr(param, num) for (int param = num - 1; param >= 0; --param) #define fir(num) for (int i = num - 1; i >= 0; --i) #define fjr(num) for (int j = num - 1; j >= 0; --j) #define fkr(num) for (int k = num - 1; k >= 0; --k) #define flr(num) for (int l = num - 1; l >= 0; --l) #define fnr(num) for (int n = num - 1; n >= 0; --n) #define gi(p) Scanf("%d", p) #define gi2(p1, p2) Scanf2("%d %d", p1, p2) #define gi3(p1, p2, p3) Scanf3("%d %d %d", p1, p2, p3) #define gi4(p1, p2, p3, p4) Scanf4("%d %d %d %d", p1, p2, p3, p4) #define gll(p) Scanf("%lld", p) #define gll2(p1, p2) Scanf2("%lld %lld", p1, p2) #define gll3(p1, p2, p3) Scanf3("%lld %lld %lld", p1, p2, p3) #define gll4(p1, p2, p3, p4) Scanf4("%lld %lld %lld %lld", p1, p2, p3, p4) #define gf(p) Scanf("%f", p) #define gf2(p1, p2) Scanf2("%f %f", p1, p2) #define gf3(p1, p2, p3) Scanf3("%f %f %f", p1, p2, p3) #define gf4(p1, p2, p3, p4) Scanf4("%f %f %f %f", p1, p2, p3, p4) #ifdef DEBUG #define gc(buf) fscanf(local_in.fp, "%s", buf) #define Scanf(expr, p) fscanf(local_in.fp, expr, &p) #define Scanf2(expr, p1, p2) fscanf(local_in.fp, expr, &p1, &p2) #define Scanf3(expr, p1, p2, p3) fscanf(local_in.fp, expr, &p1, &p2, &p3) #define Scanf4(expr, p1, p2, p3, p4) \ fscanf(local_in.fp, expr, &p1, &p2, &p3, &p4) #else #define gc(buf) Scanf("%s", buf) #define Scanf(expr, p) scanf(expr, &p) #define Scanf2(expr, p1, p2) scanf(expr, &p1, &p2) #define Scanf3(expr, p1, p2, p3) scanf(expr, &p1, &p2, &p3) #define Scanf4(expr, p1, p2, p3, p4) scanf(expr, &p1, &p2, &p3, &p4) #endif #define ans(p) cout << p << endl; void CombSort(int N, int *ar, int order_ascending = 1) { int h = int(N / 1.3); int flag; int i; while (true) { flag = 0; for (i = 0; i + h < N; ++i) { if ((order_ascending && ar[i] > ar[i + h]) || (!order_ascending && ar[i] < ar[i + h])) { swap<int>(ar[i], ar[i + h]); flag = 1; } } if (h == 1 && !flag) break; if (h == 9 || h == 10) h = 11; if (h > 1) h = int(h / 1.3); } } // int EuclideanAlgorithm(int N, int *ar) { fn(N - 1) { while (true) { if (ar[n] // % ar[n + 1] == 0 || ar[n + 1] % ar[n] == 0) { ar[n + 1] = min(ar[n], ar[n + // 1]); break; }if (ar[n] > ar[n + 1]) { ar[n] %= ar[n + 1]; } else { ar[n + 1] // %= ar[n]; } } }return ar[N - 1]; } template <typename T> void CombSort(int N, T *ar, int order_ascending = 1) { int i, flag; int h = N / 1.3; while (true) { flag = 0; for (i = 0; i + h < N; ++i) { if (order_ascending && ar[i].SortValue > ar[i + h].SortValue || !order_ascending && ar[i].SortValue < ar[i + h].SortValue) { swap<T>(ar[i], ar[i + h]); flag = 1; } } if (h > 1) { h /= 1.3; if (h == 9 || h == 10) h = 11; } else { if (!flag) break; } } } #include <algorithm> #include <vector> int table[40] = {0}; long long int dat[100010] = {0}; using namespace std; struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化 for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); // xの根をrx int ry = root(y); // yの根をry if (rx == ry) return; // xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; char cbuf[128][16]; int mx; void Calc(int *arr1, int narr1, int *arr2, int narr2, int nPush) { int cnt = 0; int nList = 0; int List[50]; fi(narr1) { cnt += arr1[i]; if (arr1[i] < 0) List[nList++] = -arr1[i]; } fi(narr2) { cnt += arr2[i]; if (arr2[i] < 0) List[nList++] = -arr2[i]; } if (nList >= 2) CombSort(nList, List, 0); fi(nPush) { if (i >= nList) break; cnt += List[i]; } mx = max(cnt, mx); // cout << cnt << endl; } void Pop2(int *arr1, int narr1, int *arr2, int narr2, int nPush); void Pop1(int *arr1, int narr1, int *arr2, int narr2, int nPush) { Calc(arr1, narr1, arr2, narr2, nPush); if (narr1 > 0) { --narr1; ++nPush; Pop1(arr1, narr1, arr2, narr2, nPush); } if (narr2 > 0) { --narr2; ++nPush; Pop2(arr1, narr1, arr2, narr2, nPush); } } void Pop2(int *arr1, int narr1, int *arr2, int narr2, int nPush) { Calc(arr1, narr1, arr2, narr2, nPush); if (narr2 > 0) { --narr2; ++nPush; Pop2(arr1, narr1, arr2, narr2, nPush); } } signed main() { int V[50]; int N, K; gi2(N, K); fi(N) gi(V[i]); mx = 0; int Selected[2][50]; int nSelected[2]; int lt, rt; int nPush = 0; int rK = min(K, N); for (lt = rK; lt >= 0; --lt) { rt = rK - lt; nSelected[0] = nSelected[1] = 0; for (int i = 0; i < lt; ++i) { Selected[0][nSelected[0]++] = V[i]; } for (int i = N - 1; i > N - 1 - rt; --i) { Selected[1][nSelected[1]++] = V[i]; } nPush = K - (nSelected[0] + nSelected[1]); Pop1(Selected[0], nSelected[0], Selected[1], nSelected[1], nPush); } ans(mx); return wait(); }
replace
238
242
238
241
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double db; typedef pair<ll, ll> P; #define pb push_back #define ft first #define sd second #define mp make_pair #define fr(i, n) for (int i = 0; i < (n); i++) #define Fr(i, n) for (int i = 0; i++ < (n);) #define ifr(i, n) for (int i = (n)-1; i >= 0; i--) #define iFr(i, n) for (int i = (n); i > 0; i--) int main() { ll n, k, ans = 0; cin >> n >> k; deque<ll> v(n); fr(i, n) cin >> v[i]; fr(i, k + 1) { fr(j, i + 1) { ll kari = 0; priority_queue<ll> pq; fr(l, j) { kari += v[l]; if (v[l] < 0) pq.push(-v[l]); } fr(l, i - j) { kari += v[n - 1 - l]; if (v[n - 1 - l] < 0) pq.push(-v[n - 1 - l]); } fr(l, k - i) { if (pq.empty()) break; kari += pq.top(); pq.pop(); } ans = max(ans, kari); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double db; typedef pair<ll, ll> P; #define pb push_back #define ft first #define sd second #define mp make_pair #define fr(i, n) for (int i = 0; i < (n); i++) #define Fr(i, n) for (int i = 0; i++ < (n);) #define ifr(i, n) for (int i = (n)-1; i >= 0; i--) #define iFr(i, n) for (int i = (n); i > 0; i--) int main() { ll n, k, ans = 0; cin >> n >> k; deque<ll> v(n); fr(i, n) cin >> v[i]; fr(i, min(k + 1, n + 1)) { fr(j, i + 1) { ll kari = 0; priority_queue<ll> pq; fr(l, j) { kari += v[l]; if (v[l] < 0) pq.push(-v[l]); } fr(l, i - j) { kari += v[n - 1 - l]; if (v[n - 1 - l] < 0) pq.push(-v[n - 1 - l]); } fr(l, k - i) { if (pq.empty()) break; kari += pq.top(); pq.pop(); } ans = max(ans, kari); } } cout << ans << endl; }
replace
19
20
19
20
0
p03032
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <utility> #include <vector> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> v(n), range(k * 2, 0), vals(0); for (int i = 0; i < n; i++) { cin >> v.at(i); } for (int i = 0; i < k; i++) { if (k - 1 - i < n) { range.at(i) = v.at(k - 1 - i); } if (n - 1 - i >= 0) { range.at(k + i) = v.at(n - 1 - i); } } for (int cd = 0; cd <= k / 2; cd++) { int x = min(n, k - cd); for (int offset = k - x; offset <= k; offset++) { vector<int> hand(x, 0); int val = 0; for (int pos = 0; pos < x; pos++) { hand.at(pos) = range.at(pos + offset); val += hand.at(pos); } vals.push_back(val); sort(hand.begin(), hand.end()); for (int cnt = 0; cnt < cd; cnt++) { val -= hand.at(cnt); vals.push_back(val); } } } int val_max = 0; for (int i = 0; i < vals.size(); i++) { if (vals.at(i) > val_max) { val_max = vals.at(i); } } cout << val_max << endl; }
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <utility> #include <vector> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> v(n), range(k * 2, 0), vals(0); for (int i = 0; i < n; i++) { cin >> v.at(i); } for (int i = 0; i < k; i++) { if (k - 1 - i < n) { range.at(i) = v.at(k - 1 - i); } if (n - 1 - i >= 0) { range.at(k + i) = v.at(n - 1 - i); } } for (int cd = 0; cd <= k / 2; cd++) { int x = min(n, k - cd); for (int offset = k - x; offset <= k; offset++) { vector<int> hand(x, 0); int val = 0; for (int pos = 0; pos < x; pos++) { hand.at(pos) = range.at(pos + offset); val += hand.at(pos); } vals.push_back(val); sort(hand.begin(), hand.end()); int y = min(x, cd); for (int cnt = 0; cnt < y; cnt++) { val -= hand.at(cnt); vals.push_back(val); } } } int val_max = 0; for (int i = 0; i < vals.size(); i++) { if (vals.at(i) > val_max) { val_max = vals.at(i); } } cout << val_max << endl; }
replace
37
38
37
39
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using Graph = vector<vector<ll>>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++) #define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll MOD = 1000000007; const ll INF = 1000000000000000000L; #ifdef __DEBUG /** * For DEBUG * https://github.com/ta7uw/cpp-pyprint */ #include "cpp-pyprint/pyprint.h" #endif void solve() { ll N, K; cin >> N >> K; deque<ll> V(N); rep(i, N) cin >> V[i]; ll ans = 0; rep(i, K + 1) { ll remain = K - i; rep(j, remain + 1) { auto X = V; vector<ll> S; rep(x, i) { S.push_back(X.front()); X.pop_front(); } rep(x, j) { S.push_back(X.back()); X.pop_back(); } ll s = accumulate(S.begin(), S.end(), 0LL); sort(S.begin(), S.end()); ll r = K - i - j; rep(k, min(r, (ll)S.size())) { if (S[k] < 0) s -= S[k]; } ans = max(ans, s); } } cout << ans << '\n'; } int main() { cin.tie(nullptr); // ios::sync_with_stdio(false); // cout << fixed << setprecision(15); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using Graph = vector<vector<ll>>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++) #define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll MOD = 1000000007; const ll INF = 1000000000000000000L; #ifdef __DEBUG /** * For DEBUG * https://github.com/ta7uw/cpp-pyprint */ #include "cpp-pyprint/pyprint.h" #endif void solve() { ll N, K; cin >> N >> K; deque<ll> V(N); rep(i, N) cin >> V[i]; ll ans = 0; rep(i, K + 1) { ll remain = K - i; rep(j, remain + 1) { auto X = V; vector<ll> S; if (i + j > N) continue; rep(x, i) { S.push_back(X.front()); X.pop_front(); } rep(x, j) { S.push_back(X.back()); X.pop_back(); } ll s = accumulate(S.begin(), S.end(), 0LL); sort(S.begin(), S.end()); ll r = K - i - j; rep(k, min(r, (ll)S.size())) { if (S[k] < 0) s -= S[k]; } ans = max(ans, s); } } cout << ans << '\n'; } int main() { cin.tie(nullptr); // ios::sync_with_stdio(false); // cout << fixed << setprecision(15); solve(); return 0; }
insert
37
37
37
39
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define fi first #define se second #define pb push_back #define rep(i, s, n) for (int i = s; i < n; i++) #define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--) #define all(a) a.begin(), a.end() typedef pair<int, int> pint; typedef vector<int> vint; typedef vector<pint> vpint; const long long MOD = 1000000007, INF = 1e17; #define endl '\n' #define IOS() \ ios_base::sync_with_stdio(0); \ cin.tie(0) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <typename T> 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); } int A, B, C, D, E, F, G, H, I, J, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, K; string s, t; signed main() { IOS(); cin >> N >> K; vector<int> a(N); for (int i = 0; i < N; i++) { cin >> a[i]; } auto dp = make_v<int>(N + 100, N + 100, K + 100); fill_v(dp, -INF); dp[0][0][0] = 0; rep(k, 0, K + 1) { rep(i, 0, N + 1) { rep(j, 0, N + 1) { if (dp[i][j][k] == -1) continue; if (i + j >= N) continue; if (i <= N - 1) chmax(dp[k + 1][i + 1][j], dp[k][i][j] + a[i]); if (N - j - 1 >= 0) chmax(dp[k + 1][i][j + 1], dp[k][i][j] + a[N - j - 1]); chmax(dp[k + 2][i + 1][j], dp[k][i][j]); chmax(dp[k + 2][i][j + 1], dp[k][i][j]); } } } int ans = -INF; rep(i, 0, N + 1) rep(j, 0, N + 1) rep(k, 0, K + 1) chmax(ans, dp[k][i][j]); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define fi first #define se second #define pb push_back #define rep(i, s, n) for (int i = s; i < n; i++) #define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--) #define all(a) a.begin(), a.end() typedef pair<int, int> pint; typedef vector<int> vint; typedef vector<pint> vpint; const long long MOD = 1000000007, INF = 1e17; #define endl '\n' #define IOS() \ ios_base::sync_with_stdio(0); \ cin.tie(0) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <typename T> 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); } int A, B, C, D, E, F, G, H, I, J, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, K; string s, t; signed main() { IOS(); cin >> N >> K; vector<int> a(N); for (int i = 0; i < N; i++) { cin >> a[i]; } auto dp = make_v<int>(K + 100, N + 100, N + 100); fill_v(dp, -INF); dp[0][0][0] = 0; rep(k, 0, K + 1) { rep(i, 0, N + 1) { rep(j, 0, N + 1) { if (dp[i][j][k] == -1) continue; if (i + j >= N) continue; if (i <= N - 1) chmax(dp[k + 1][i + 1][j], dp[k][i][j] + a[i]); if (N - j - 1 >= 0) chmax(dp[k + 1][i][j + 1], dp[k][i][j] + a[N - j - 1]); chmax(dp[k + 2][i + 1][j], dp[k][i][j]); chmax(dp[k + 2][i][j + 1], dp[k][i][j]); } } } int ans = -INF; rep(i, 0, N + 1) rep(j, 0, N + 1) rep(k, 0, K + 1) chmax(ans, dp[k][i][j]); cout << ans << endl; }
replace
60
61
60
61
0
p03032
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long int #define speed_up \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define mod 1000000007 #define F first #define S second #define ld long double #define rep(i, a, b) for (ll i = a; i < b; i++) const int N = 55, M = 110; ll n, t, a[N], dp[N][N][M]; ll recur(ll l, ll r, ll k) { if (k <= 0) return 0; if (l == r) return a[l]; ll &ans = dp[l][r][k]; if (ans != -1) return ans; /*ll op1 = recur(l+1, r, k-1) + a[l]; ll op2 = recur(l, r-1, k-1) + a[r]; ll op3 = recur(l+1, r, k-2); ll op4 = recur(l, r-1, k-2); ll op5 = 0; ans = max(op1, op2); ans = max(ans, op3); ans = max(ans, op4); ans = max(ans, op5);*/ ans = max(ans, a[l] + recur(l + 1, r, k - 1)); ans = max(ans, a[r] + recur(l, r - 1, k - 1)); ans = max(ans, recur(l + 1, r, k - 2)); ans = max(ans, recur(l, r - 1, k - 2)); // return ans; return ans; } void solve() { cin >> n >> t; memset(dp, -1, sizeof dp); rep(i, 1, n + 1) cin >> a[i]; ll ans = recur(1, n, t); cout << ans << endl; } int main() { ll T = 1; speed_up // cin>>T; while (T--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define speed_up \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define mod 1000000007 #define F first #define S second #define ld long double #define rep(i, a, b) for (ll i = a; i < b; i++) const int N = 55, M = 110; ll n, t, a[N], dp[N][N][M]; ll recur(ll l, ll r, ll k) { if (k <= 0) return 0; if (l == r) return a[l]; ll &ans = dp[l][r][k]; if (ans != -1) return ans; /*ll op1 = recur(l+1, r, k-1) + a[l]; ll op2 = recur(l, r-1, k-1) + a[r]; ll op3 = recur(l+1, r, k-2); ll op4 = recur(l, r-1, k-2); ll op5 = 0; ans = max(op1, op2); ans = max(ans, op3); ans = max(ans, op4); ans = max(ans, op5);*/ ans = max(ans, a[l] + recur(l + 1, r, k - 1)); ans = max(ans, a[r] + recur(l, r - 1, k - 1)); ans = max(ans, recur(l + 1, r, k - 2)); ans = max(ans, recur(l, r - 1, k - 2)); ans = max(ans, 0LL); // return ans; return ans; } void solve() { cin >> n >> t; memset(dp, -1, sizeof dp); rep(i, 1, n + 1) cin >> a[i]; ll ans = recur(1, n, t); cout << ans << endl; } int main() { ll T = 1; speed_up // cin>>T; while (T--) solve(); return 0; }
replace
37
38
37
38
TLE
p03032
C++
Time Limit Exceeded
#include <bits/stdc++.h> typedef long long ll; using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector<int> q(n); for (int i = 0; i < n; i++) cin >> q[i]; int ans = 0; for (int l = 0; l <= k; l++) { for (int r = 0; r <= k - l; r--) { if (l + r > n) continue; int d = k - l - r; int cur = 0; vector<int> s; for (int i = 0; i < l; i++) { cur += q[i]; s.push_back(q[i]); } for (int i = n - r; i < n; i++) { cur += q[i]; s.push_back(q[i]); } sort(s.begin(), s.end()); for (int i = 0; i < d; i++) { if (i >= s.size()) break; if (s[i] > 0) break; cur -= s[i]; } ans = max(ans, cur); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector<int> q(n); for (int i = 0; i < n; i++) cin >> q[i]; int ans = 0; for (int l = 0; l <= k; l++) { for (int r = 0; r <= k - l; r++) { if (l + r > n) continue; int d = k - l - r; int cur = 0; vector<int> s; for (int i = 0; i < l; i++) { cur += q[i]; s.push_back(q[i]); } for (int i = n - r; i < n; i++) { cur += q[i]; s.push_back(q[i]); } sort(s.begin(), s.end()); for (int i = 0; i < d; i++) { if (i >= s.size()) break; if (s[i] > 0) break; cur -= s[i]; } ans = max(ans, cur); } } cout << ans << endl; return 0; }
replace
15
16
15
16
TLE
p03032
C++
Runtime Error
#include <algorithm> #include <cmath> //abs()かfabs()で少数絶対値 #include <cstdlib> //abs()で整数絶対値 #include <deque> #include <functional> //sort第三引数greater<型名>()で降順 #include <iomanip> //cout<<setw(数字) で空白による桁揃え #include <iostream> //cout<<right で右揃え #include <map> #include <queue> #include <string> #include <vector> using namespace std; using ll = long long int; int main() { int N, K; cin >> N >> K; vector<int> V(N); for (int i = 0; i < N; i++) { cin >> V[i]; } int max = 0; int sum = 0; vector<int> minus; for (int i = 0; i <= min(N, K); i++) { for (int j = 0; j <= min(N - i, K - i); j++) { sum = 0; minus.clear(); for (int k = 0; k < i; k++) { sum += V[k]; if (V[k] < 0) { minus.push_back(V[k]); } } for (int k = 0; k < j; k++) { sum += V[(N - 1) - k]; if (V[(N - 1) - k] < 0) { minus.push_back(V[(N - 1) - k]); } } sort(minus.begin(), minus.end(), greater<int>()); if (minus.size() > 0) { for (int k = 0; k < K - (i + j) || minus.size() != 0; k++) { sum -= minus.back(); minus.pop_back(); } } if (max < sum) max = sum; } } cout << max << endl; }
#include <algorithm> #include <cmath> //abs()かfabs()で少数絶対値 #include <cstdlib> //abs()で整数絶対値 #include <deque> #include <functional> //sort第三引数greater<型名>()で降順 #include <iomanip> //cout<<setw(数字) で空白による桁揃え #include <iostream> //cout<<right で右揃え #include <map> #include <queue> #include <string> #include <vector> using namespace std; using ll = long long int; int main() { int N, K; cin >> N >> K; vector<int> V(N); for (int i = 0; i < N; i++) { cin >> V[i]; } int max = 0; int sum = 0; vector<int> minus; for (int i = 0; i <= min(N, K); i++) { for (int j = 0; j <= min(N - i, K - i); j++) { sum = 0; minus.clear(); for (int k = 0; k < i; k++) { sum += V[k]; if (V[k] < 0) { minus.push_back(V[k]); } } for (int k = 0; k < j; k++) { sum += V[(N - 1) - k]; if (V[(N - 1) - k] < 0) { minus.push_back(V[(N - 1) - k]); } } sort(minus.begin(), minus.end(), greater<int>()); if (minus.size() > 0) { for (int k = 0; k < K - (i + j) && minus.size() != 0; k++) { sum -= minus.back(); minus.pop_back(); } } if (max < sum) max = sum; } } cout << max << endl; }
replace
43
44
43
44
-11
p03032
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K; vector<int> V(N); for (int i = 0; i < N; i++) cin >> V.at(i); int ans = 0; for (int i = 0; i <= K; i++) { for (int j = 0; i + j <= K; j++) { int L = min(i, N); int R = min(j, N - i); vector<int> tmp; while (L) tmp.push_back(V.at(--L)); while (R) tmp.push_back(V.at(N - 1 - --R)); int sum = accumulate(tmp.begin(), tmp.end(), 0); ans = max(ans, sum); sort(tmp.begin(), tmp.end()); for (int k = 0; k < min(K - i - j, (int)tmp.size()); k++) { sum -= tmp.at(k); ans = max(ans, sum); } } } cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K; vector<int> V(N); for (int i = 0; i < N; i++) cin >> V.at(i); int ans = 0; for (int i = 0; i <= K; i++) { for (int j = 0; i + j <= K; j++) { int L = min(i, N); int R = min(j, max(0, N - i)); vector<int> tmp; while (L) tmp.push_back(V.at(--L)); while (R) tmp.push_back(V.at(N - 1 - --R)); int sum = accumulate(tmp.begin(), tmp.end(), 0); ans = max(ans, sum); sort(tmp.begin(), tmp.end()); for (int k = 0; k < min(K - i - j, (int)tmp.size()); k++) { sum -= tmp.at(k); ans = max(ans, sum); } } } cout << ans << "\n"; }
replace
14
15
14
15
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long const int inf = 1e18; #include "string" int n, k; int dp[55][55][101], a[55]; int solve(int l, int r, int cnt) { if (cnt > k) return -inf; if (cnt == k or l > r) return 0; int &tmp = dp[l][r][cnt]; if (tmp != -1) return tmp; tmp = 0; tmp = max(tmp, solve(l + 1, r, cnt + 1) + a[l]); tmp = max(tmp, solve(l, r - 1, cnt + 1) + a[r]); if (a[l] < 0) tmp = max(tmp, solve(l + 1, r, cnt + 2)); if (a[r] < 0) tmp = max(tmp, solve(l, r - 1, cnt + 2)); return tmp; } int32_t main() { int t = 1; cin >> t; while (t--) { cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; memset(dp, -1, sizeof dp); int ans = solve(0, n - 1, 0); cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; #define int long long const int inf = 1e18; #include "string" int n, k; int dp[55][55][101], a[55]; int solve(int l, int r, int cnt) { if (cnt > k) return -inf; if (cnt == k or l > r) return 0; int &tmp = dp[l][r][cnt]; if (tmp != -1) return tmp; tmp = 0; tmp = max(tmp, solve(l + 1, r, cnt + 1) + a[l]); tmp = max(tmp, solve(l, r - 1, cnt + 1) + a[r]); if (a[l] < 0) tmp = max(tmp, solve(l + 1, r, cnt + 2)); if (a[r] < 0) tmp = max(tmp, solve(l, r - 1, cnt + 2)); return tmp; } int32_t main() { int t = 1; // cin>>t; while (t--) { cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; memset(dp, -1, sizeof dp); int ans = solve(0, n - 1, 0); cout << ans << endl; } }
replace
29
30
29
30
0
p03032
C++
Time Limit Exceeded
// #pragma GCC optimize ('O3') #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef long double ld; #define mp make_pair #define f first #define s second #define pb push_back const int N = 50 + 5; const int M = 100 + 5; const ll OO = 1e18; const int mod = 1e9 + 7; int n, k, v[N], dp[N][N][M]; int solve(int s, int e, int g) { if (s > e) return 0; int &ret = dp[s][e][g]; ret = 0; if (g >= 1) { ret = max( {ret, solve(s, e - 1, g - 1) + v[e], solve(s + 1, e, g - 1) + v[s]}); } if (g > 1) { ret = max({ret, solve(s, e - 1, g - 2), solve(s + 1, e, g - 2)}); } return ret; } void init() {} int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> v[i]; } memset(dp, -1, sizeof dp); cout << solve(0, n - 1, k) << '\n'; return 0; }
// #pragma GCC optimize ('O3') #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef long double ld; #define mp make_pair #define f first #define s second #define pb push_back const int N = 50 + 5; const int M = 100 + 5; const ll OO = 1e18; const int mod = 1e9 + 7; int n, k, v[N], dp[N][N][M]; int solve(int s, int e, int g) { if (s > e) return 0; int &ret = dp[s][e][g]; if (~ret) return ret; ret = 0; if (g >= 1) { ret = max( {ret, solve(s, e - 1, g - 1) + v[e], solve(s + 1, e, g - 1) + v[s]}); } if (g > 1) { ret = max({ret, solve(s, e - 1, g - 2), solve(s + 1, e, g - 2)}); } return ret; } void init() {} int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> v[i]; } memset(dp, -1, sizeof dp); cout << solve(0, n - 1, k) << '\n'; return 0; }
insert
25
25
25
28
TLE
p03032
C++
Runtime Error
#include "bits/stdc++.h" // #include <intrin.h> //AtCoder (gcc) // 上ではこれがあると動かない。__popcnt用のincludeファイル。 using namespace std; typedef long long ll; typedef long double ld; #define int long long #define rep(i, n) for (long long i = 0; i < (n); i++) #define sqrt(d) pow((long double)(d), 0.50) #define PII pair<int, int> #define MP make_pair const int INF = 2000000000; // 2e9 const long long INF2 = 1000000000000000000; // 1e18 const long double pi = acos(-1); const int large_P = 1000000007; // 1e9 + 7 // const int large_P = 1000000009; //1e9 + 9 // const int large_P = 998244353; // 繰り返し2乗法 // N^aの、Mで割った余りを求める。 ll my_pow(ll N, ll a, ll M) { ll tempo; if (a == 0) { return 1; } else { if (a % 2 == 0) { tempo = my_pow(N, a / 2, M); return (tempo * tempo) % M; } else { tempo = my_pow(N, a - 1, M); return (tempo * N) % M; } } } // N_C_a を M で割った余り ll my_combination(ll N, ll a, ll M) { ll answer = 1; rep(i, a) { answer *= (N - i); answer %= M; } rep(i, a) { answer *= my_pow(i + 1, M - 2, M); answer %= M; } return answer; } // N_C_i を M で割った余りを、v.at(i) に代入する。 void my_combination_table(ll N, ll M, vector<ll> &v) { v.assign(N + 1, 1); for (int i = 1; i <= N; i++) { v.at(i) = v.at(i - 1) * (N - (i - 1)); v.at(i) %= M; v.at(i) *= my_pow(i, M - 2, M); v.at(i) %= M; } } ll factorial(ll x, vector<ll> &dp) { if (dp.size() <= x) { int n = dp.size(); rep(i, x + 1 - n) { dp.push_back(0); } } if (x == 0) return dp.at(x) = 1; if (dp.at(x) != -1 && dp.at(x) != 0) return dp.at(x); return dp.at(x) = x * factorial(x - 1, dp); } ll my_gcd(ll &a, ll &b) { if (b == 0) return a; ll temp = a % b; return my_gcd(b, temp); } // base を底としたときの、n の i桁目を、v.at(i) に入れる。(桁数は n // に応じて自動で設定する。) void ll_to_vector(signed base, long long n, vector<signed> &v) { long long tempo = n; long long tempo2 = n; signed n_digit = 1; while (tempo2 >= 10LL) { tempo2 /= 10LL; n_digit++; } v.assign(n_digit, 0); // ll n_digit = v.size(); // v のサイズをそのままにする場合。 for (signed i = 0; i < n_digit; i++) { v.at(i) = tempo / (ll)pow(base, n_digit - 1 - i); tempo -= v.at(i) * (ll)pow(base, n_digit - 1 - i); } } int char_to_int(char c) { switch (c) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; default: return 0; } } // エラトステネスの篩で、prime で ないところに false を入れる。 // true で初期化された vector を代入する必要がある。 void prime_judge(vector<bool> &prime_or_not) { prime_or_not.assign(prime_or_not.size(), true); prime_or_not.at(0) = false; prime_or_not.at(1) = false; ll n = prime_or_not.size() - 1; ll region = (ll)sqrt(n) + 1; for (ll i = 2; i <= n / 2; i++) { prime_or_not.at(2 * i) = false; } for (ll i2 = 1; i2 < n / 2; i2++) { ll i = 2 * i2 + 1; // ここからは奇数のみ探索 if (i < region && prime_or_not.at(i)) { ll j = i * i; while (j < n + 1) { prime_or_not.at(j) = false; j += 2 * i; } } } }; map<ll, ll> divide_to_prime(int target) { map<ll, ll> res; // sqrt(target) まで調べる。 ll upper_lim = ceil(sqrt(target)); vector<bool> prime_or_not(upper_lim + 1, true); prime_or_not.at(0) = false; prime_or_not.at(1) = false; if (upper_lim < 20) prime_or_not.assign(25, true); prime_judge(prime_or_not); ll tempo = target; rep(i, (upper_lim + 1)) { if (prime_or_not.at(i)) { while (tempo % i == 0) { tempo /= i; res[i]++; } } } if (tempo != 1) res[tempo]++; // sqrt(target) より大きな素因数は高々1つしかない。 return res; } vector<long long> count_dividers(long long target) { vector<long long> dividers, tempo; int i = 1; while (i < sqrt(target) + 1) { if (target % i == 0) { dividers.push_back(i); if (i < target / i) tempo.push_back(target / i); // if節がないと、平方数の時、sqrt(target) // がダブルカウントされる。 } i++; } for (int j = 0; j < tempo.size(); j++) { dividers.push_back(tempo.at(tempo.size() - 1 - j)); } return dividers; } void BFS_labyrinth(queue<pair<int, int>> &que, vector<vector<int>> &dist, int &area) { int n = dist.size(); int m = dist.at(0).size(); while (!que.empty()) { int h, w; pair<int, int> tempo = que.front(); que.pop(); h = tempo.first; w = tempo.second; // cout << temp_i << " " << temp_j << endl; for (int dh = -1; dh <= 1; dh++) { for (int dw = -1; dw <= 1; dw++) { if (h + dh < 0 || n <= h + dh) continue; // 範囲外 if (w + dw < 0 || m <= w + dw) continue; // 範囲外 if (dh == 0 && dw == 0) continue; // 動いていない if (dh * dw != 0) continue; // 右上など。八近傍の場合は消す。 if (dist.at(h + dh).at(w + dw) != -1) continue; // 行けない領域に、既に INF // などが代入されている場合はこの条件だけで ok dist.at(h + dh).at(w + dw) = dist.at(h).at(w) + 1; que.push(make_pair(h + dh, w + dw)); } } // 何か所も領域がある場合だけ必要 if (que.empty()) { rep(i, n) { rep(j, m) { if (dist.at(i).at(j) == -1) { que.push(make_pair(i, j)); dist.at(i).at(j) = 0; area++; break; } } if (!que.empty()) break; } } } } void BFS01_labyrinth(deque<pair<int, int>> &que, vector<vector<int>> &dist, vector<vector<int>> &cost) { int n = dist.size(); int m = dist.at(0).size(); while (!que.empty()) { int h, w; pair<int, int> tempo = que.front(); que.pop_front(); h = tempo.first; w = tempo.second; // cout << temp_i << " " << temp_j << endl; for (int dh = -1; dh <= 1; dh++) { for (int dw = -1; dw <= 1; dw++) { if (h + dh < 0 || n <= h + dh) continue; // 範囲外 if (w + dw < 0 || m <= w + dw) continue; // 範囲外 if (dh == 0 && dw == 0) continue; // 動いていない if (dh * dw != 0) continue; // 右上など。八近傍の場合は消す。 if (dist.at(h + dh).at(w + dw) != -1) continue; // 行けない領域に、既に INF // などが代入されている場合はこの条件だけで ok dist.at(h + dh).at(w + dw) = dist.at(h).at(w) + cost.at(h + dh).at(w + dw); if (cost.at(h + dh).at(w + dw) == 0) { // コストが低い場合 que.push_front(make_pair(h + dh, w + dw)); } else { // コストが高い場合 que.push_back(make_pair(h + dh, w + dw)); } } } } } void dfs(const vector<vector<int>> &G, vector<bool> &seen, int v) { seen.at(v) = true; for (int next_v : G.at(v)) { if (seen.at(next_v)) continue; dfs(G, seen, next_v); } } class edge { public: int to; int cost; }; void dijkstra(int s, const vector<vector<edge>> G, vector<int> &dist) { int V = dist.size(); // 頂点数 dist.assign(V, INF); // first が最短距離、second が頂点番号。 priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que; dist.at(s) = 0; que.push(make_pair(0, s)); while (!que.empty()) { pair<int, int> p = que.top(); que.pop(); int v = p.second; if (dist.at(v) < p.first) continue; // 最短距離がすでに更新されているので無視。 for (int i = 0; i < G.at(v).size(); i++) { edge e = G.at(v).at(i); // for (auto&& e : G.at(v)) { // ← なぜか、やや遅いので。 if (dist.at(e.to) > dist.at(v) + e.cost) { dist.at(e.to) = dist.at(v) + e.cost; que.push(make_pair(dist.at(e.to), e.to)); } } } } const int Vmax2 = 1; int dp_warshall[Vmax2][Vmax2]; // G.at(i).at(j) は i から j への移動コスト。隣接行列。 void warshall_floyd(int V, const vector<vector<int>> G) { rep(i, V) { rep(j, V) { dp_warshall[i][j] = G.at(i).at(j); // 初期化 } } rep(k, V) { rep(i, V) { rep(j, V) { dp_warshall[i][j] = min(dp_warshall[i][j], dp_warshall[i][k] + dp_warshall[k][j]); } } } } class UnionFind { public: vector<int> parent; vector<int> rank; UnionFind(int N) : parent(N), rank(N, 0) { rep(i, N) { parent.at(i) = i; } } int root(int x) { if (parent.at(x) == x) return x; return parent.at(x) = root(parent.at(x)); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; // xの根とyの根が同じなので、何もしない。 if (rank.at(rx) < rank.at(ry)) { parent.at(rx) = ry; } else { parent.at(ry) = rx; if (rank.at(rx) == rank.at(ry)) rank.at(rx)++; } } bool same(int x, int y) { return (root(x) == root(y)); } }; class wUnionFind { public: vector<int> parent; vector<int> diff_weight; // 親との差分。 vector<int> rank; wUnionFind(int N) : parent(N), diff_weight(N, 0), rank(N, 0) { rep(i, N) { parent.at(i) = i; } } int root(int x) { if (parent.at(x) == x) return x; int r = root(parent.at(x)); diff_weight.at(x) += diff_weight.at(parent.at(x)); // 累積和 return parent.at(x) = r; } // x の重みを出力する関数。 int weight(int x) { root(x); return diff_weight.at(x); } // weight.at(y) - weight.at(x) == w となるようにする。 bool unite(int x, int y, int w) { int rx = root(x); int ry = root(y); int diff_weight_to_ry_from_rx = w + weight(x) - weight(y); if (rx == ry) return false; // xの根とyの根が同じなので、何もしない。 if (rank.at(rx) < rank.at(ry)) { parent.at(rx) = ry; diff_weight.at(rx) = -diff_weight_to_ry_from_rx; } else { parent.at(ry) = rx; diff_weight.at(ry) = diff_weight_to_ry_from_rx; if (rank.at(rx) == rank.at(ry)) rank.at(rx)++; } return true; } bool same(int x, int y) { return (root(x) == root(y)); } }; class SegmentTree { private: int ini = INF; public: int Size; vector<int> node; SegmentTree(int N) : Size(N), node(N) { int new_N = 1; while (new_N < N) new_N *= 2; node.assign(2 * new_N - 1, ini); Size = new_N; } SegmentTree(vector<int> v) : Size(v.size()), node(v.size()) { int new_N = 1; while (new_N < v.size()) new_N *= 2; node.assign(2 * new_N - 1, ini); Size = new_N; // 葉の初期化 for (int i = 0; i < v.size(); i++) node.at(Size - 1 + i) = v.at(i); // 上りながら初期化 for (int i = Size - 2; i >= 0; i--) node.at(i) = min(node.at(2 * i + 1), node.at(2 * i + 2)); } // k 番目の値 (0-indexed) を x に変更する。 void update(int k, int x) { // 葉のノードの番号 (等比数列の和の公式) k += Size - 1; node.at(k) = x; // 上りながら更新 (0-indexedの場合、例えば、3 の子は、7, 8) while (k > 0) { k = (k - 1) / 2; // 子の最小値を親の最小値とする。 node.at(k) = min(node.at(k * 2 + 1), node.at(k * 2 + 2)); } } //[a, b) の最小値を計算する。[l, r) は、ノード k に対応する区間を与える。 // query(a, b, 0, 0, size) で呼べばよい。 int query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return ini; // 交差しない。 if (a <= l && r <= b) return node.at(k); // 完全に含む。 else { int vl = query(a, b, k * 2 + 1, l, (l + r) / 2); int vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return min(vl, vr); } } }; // 転倒数を返す (comp = less<int>() の場合)。 // comp = greater<int>() の場合は、N_C_2 - 転倒数 を返すことになる。 int my_merge(vector<int> &A, vector<int> &B, int left, int mid, int right, function<bool(int, int)> comp) { int i = left; // Aを分割したときの、左側の配列を差す配列。 int j = mid; // Bを分割したときの、左側の配列を差す配列。 int k = 0; // 分割した後の配列 (一時的に) B に保存。 int res = 0; while (i < mid && j < right) { if (comp(A.at(i), A.at(j))) B.at(k++) = A.at(i++); else { B.at(k++) = A.at(j++); res += mid - i; } } // 左側をBに移動し尽くしたので、右側を順に入れていく。 if (i == mid) { while (j < right) { B.at(k++) = A.at(j++); } } else { // 右側をBに移動し尽くしたので、左側を順に入れていく。 while (i < mid) { B.at(k++) = A.at(i++); } } // Aに値を戻す。 rep(l, k) { A.at(left + l) = B.at(l); } return res; } int my_merge_sort(vector<int> &target, vector<int> &tempo, int left, int right, function<bool(int, int)> comp) { int mid, res = 0; if (left == right || left == right - 1) return res; mid = (left + right) / 2; res += my_merge_sort(target, tempo, left, mid, comp); res += my_merge_sort(target, tempo, mid, right, comp); res += my_merge(target, tempo, left, mid, right, comp); return res; } const int max_K = 100; int dp[max_K + 1][max_K + 1]; // 左から i 番目まで見る // 右から j 番目まで見る // K - i - j 個は捨てる。 signed main() { int N, K; cin >> N >> K; deque<int> V(N); rep(i, N) cin >> V.at(i); int res = 0; rep(i, K + 1) { rep(j, K + 1) { if (K - i - j < 0) continue; priority_queue<int> que; rep(k, i) que.push(V.at(k)); rep(k, j) que.push(V.at(N - 1 - k)); int tempo1 = 0; rep(k, 2 * (i + j) - K) { int tempo2 = que.top(); que.pop(); tempo1 += tempo2; } if (tempo1 > res) res = tempo1; } } cout << res << endl; }
#include "bits/stdc++.h" // #include <intrin.h> //AtCoder (gcc) // 上ではこれがあると動かない。__popcnt用のincludeファイル。 using namespace std; typedef long long ll; typedef long double ld; #define int long long #define rep(i, n) for (long long i = 0; i < (n); i++) #define sqrt(d) pow((long double)(d), 0.50) #define PII pair<int, int> #define MP make_pair const int INF = 2000000000; // 2e9 const long long INF2 = 1000000000000000000; // 1e18 const long double pi = acos(-1); const int large_P = 1000000007; // 1e9 + 7 // const int large_P = 1000000009; //1e9 + 9 // const int large_P = 998244353; // 繰り返し2乗法 // N^aの、Mで割った余りを求める。 ll my_pow(ll N, ll a, ll M) { ll tempo; if (a == 0) { return 1; } else { if (a % 2 == 0) { tempo = my_pow(N, a / 2, M); return (tempo * tempo) % M; } else { tempo = my_pow(N, a - 1, M); return (tempo * N) % M; } } } // N_C_a を M で割った余り ll my_combination(ll N, ll a, ll M) { ll answer = 1; rep(i, a) { answer *= (N - i); answer %= M; } rep(i, a) { answer *= my_pow(i + 1, M - 2, M); answer %= M; } return answer; } // N_C_i を M で割った余りを、v.at(i) に代入する。 void my_combination_table(ll N, ll M, vector<ll> &v) { v.assign(N + 1, 1); for (int i = 1; i <= N; i++) { v.at(i) = v.at(i - 1) * (N - (i - 1)); v.at(i) %= M; v.at(i) *= my_pow(i, M - 2, M); v.at(i) %= M; } } ll factorial(ll x, vector<ll> &dp) { if (dp.size() <= x) { int n = dp.size(); rep(i, x + 1 - n) { dp.push_back(0); } } if (x == 0) return dp.at(x) = 1; if (dp.at(x) != -1 && dp.at(x) != 0) return dp.at(x); return dp.at(x) = x * factorial(x - 1, dp); } ll my_gcd(ll &a, ll &b) { if (b == 0) return a; ll temp = a % b; return my_gcd(b, temp); } // base を底としたときの、n の i桁目を、v.at(i) に入れる。(桁数は n // に応じて自動で設定する。) void ll_to_vector(signed base, long long n, vector<signed> &v) { long long tempo = n; long long tempo2 = n; signed n_digit = 1; while (tempo2 >= 10LL) { tempo2 /= 10LL; n_digit++; } v.assign(n_digit, 0); // ll n_digit = v.size(); // v のサイズをそのままにする場合。 for (signed i = 0; i < n_digit; i++) { v.at(i) = tempo / (ll)pow(base, n_digit - 1 - i); tempo -= v.at(i) * (ll)pow(base, n_digit - 1 - i); } } int char_to_int(char c) { switch (c) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; default: return 0; } } // エラトステネスの篩で、prime で ないところに false を入れる。 // true で初期化された vector を代入する必要がある。 void prime_judge(vector<bool> &prime_or_not) { prime_or_not.assign(prime_or_not.size(), true); prime_or_not.at(0) = false; prime_or_not.at(1) = false; ll n = prime_or_not.size() - 1; ll region = (ll)sqrt(n) + 1; for (ll i = 2; i <= n / 2; i++) { prime_or_not.at(2 * i) = false; } for (ll i2 = 1; i2 < n / 2; i2++) { ll i = 2 * i2 + 1; // ここからは奇数のみ探索 if (i < region && prime_or_not.at(i)) { ll j = i * i; while (j < n + 1) { prime_or_not.at(j) = false; j += 2 * i; } } } }; map<ll, ll> divide_to_prime(int target) { map<ll, ll> res; // sqrt(target) まで調べる。 ll upper_lim = ceil(sqrt(target)); vector<bool> prime_or_not(upper_lim + 1, true); prime_or_not.at(0) = false; prime_or_not.at(1) = false; if (upper_lim < 20) prime_or_not.assign(25, true); prime_judge(prime_or_not); ll tempo = target; rep(i, (upper_lim + 1)) { if (prime_or_not.at(i)) { while (tempo % i == 0) { tempo /= i; res[i]++; } } } if (tempo != 1) res[tempo]++; // sqrt(target) より大きな素因数は高々1つしかない。 return res; } vector<long long> count_dividers(long long target) { vector<long long> dividers, tempo; int i = 1; while (i < sqrt(target) + 1) { if (target % i == 0) { dividers.push_back(i); if (i < target / i) tempo.push_back(target / i); // if節がないと、平方数の時、sqrt(target) // がダブルカウントされる。 } i++; } for (int j = 0; j < tempo.size(); j++) { dividers.push_back(tempo.at(tempo.size() - 1 - j)); } return dividers; } void BFS_labyrinth(queue<pair<int, int>> &que, vector<vector<int>> &dist, int &area) { int n = dist.size(); int m = dist.at(0).size(); while (!que.empty()) { int h, w; pair<int, int> tempo = que.front(); que.pop(); h = tempo.first; w = tempo.second; // cout << temp_i << " " << temp_j << endl; for (int dh = -1; dh <= 1; dh++) { for (int dw = -1; dw <= 1; dw++) { if (h + dh < 0 || n <= h + dh) continue; // 範囲外 if (w + dw < 0 || m <= w + dw) continue; // 範囲外 if (dh == 0 && dw == 0) continue; // 動いていない if (dh * dw != 0) continue; // 右上など。八近傍の場合は消す。 if (dist.at(h + dh).at(w + dw) != -1) continue; // 行けない領域に、既に INF // などが代入されている場合はこの条件だけで ok dist.at(h + dh).at(w + dw) = dist.at(h).at(w) + 1; que.push(make_pair(h + dh, w + dw)); } } // 何か所も領域がある場合だけ必要 if (que.empty()) { rep(i, n) { rep(j, m) { if (dist.at(i).at(j) == -1) { que.push(make_pair(i, j)); dist.at(i).at(j) = 0; area++; break; } } if (!que.empty()) break; } } } } void BFS01_labyrinth(deque<pair<int, int>> &que, vector<vector<int>> &dist, vector<vector<int>> &cost) { int n = dist.size(); int m = dist.at(0).size(); while (!que.empty()) { int h, w; pair<int, int> tempo = que.front(); que.pop_front(); h = tempo.first; w = tempo.second; // cout << temp_i << " " << temp_j << endl; for (int dh = -1; dh <= 1; dh++) { for (int dw = -1; dw <= 1; dw++) { if (h + dh < 0 || n <= h + dh) continue; // 範囲外 if (w + dw < 0 || m <= w + dw) continue; // 範囲外 if (dh == 0 && dw == 0) continue; // 動いていない if (dh * dw != 0) continue; // 右上など。八近傍の場合は消す。 if (dist.at(h + dh).at(w + dw) != -1) continue; // 行けない領域に、既に INF // などが代入されている場合はこの条件だけで ok dist.at(h + dh).at(w + dw) = dist.at(h).at(w) + cost.at(h + dh).at(w + dw); if (cost.at(h + dh).at(w + dw) == 0) { // コストが低い場合 que.push_front(make_pair(h + dh, w + dw)); } else { // コストが高い場合 que.push_back(make_pair(h + dh, w + dw)); } } } } } void dfs(const vector<vector<int>> &G, vector<bool> &seen, int v) { seen.at(v) = true; for (int next_v : G.at(v)) { if (seen.at(next_v)) continue; dfs(G, seen, next_v); } } class edge { public: int to; int cost; }; void dijkstra(int s, const vector<vector<edge>> G, vector<int> &dist) { int V = dist.size(); // 頂点数 dist.assign(V, INF); // first が最短距離、second が頂点番号。 priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que; dist.at(s) = 0; que.push(make_pair(0, s)); while (!que.empty()) { pair<int, int> p = que.top(); que.pop(); int v = p.second; if (dist.at(v) < p.first) continue; // 最短距離がすでに更新されているので無視。 for (int i = 0; i < G.at(v).size(); i++) { edge e = G.at(v).at(i); // for (auto&& e : G.at(v)) { // ← なぜか、やや遅いので。 if (dist.at(e.to) > dist.at(v) + e.cost) { dist.at(e.to) = dist.at(v) + e.cost; que.push(make_pair(dist.at(e.to), e.to)); } } } } const int Vmax2 = 1; int dp_warshall[Vmax2][Vmax2]; // G.at(i).at(j) は i から j への移動コスト。隣接行列。 void warshall_floyd(int V, const vector<vector<int>> G) { rep(i, V) { rep(j, V) { dp_warshall[i][j] = G.at(i).at(j); // 初期化 } } rep(k, V) { rep(i, V) { rep(j, V) { dp_warshall[i][j] = min(dp_warshall[i][j], dp_warshall[i][k] + dp_warshall[k][j]); } } } } class UnionFind { public: vector<int> parent; vector<int> rank; UnionFind(int N) : parent(N), rank(N, 0) { rep(i, N) { parent.at(i) = i; } } int root(int x) { if (parent.at(x) == x) return x; return parent.at(x) = root(parent.at(x)); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; // xの根とyの根が同じなので、何もしない。 if (rank.at(rx) < rank.at(ry)) { parent.at(rx) = ry; } else { parent.at(ry) = rx; if (rank.at(rx) == rank.at(ry)) rank.at(rx)++; } } bool same(int x, int y) { return (root(x) == root(y)); } }; class wUnionFind { public: vector<int> parent; vector<int> diff_weight; // 親との差分。 vector<int> rank; wUnionFind(int N) : parent(N), diff_weight(N, 0), rank(N, 0) { rep(i, N) { parent.at(i) = i; } } int root(int x) { if (parent.at(x) == x) return x; int r = root(parent.at(x)); diff_weight.at(x) += diff_weight.at(parent.at(x)); // 累積和 return parent.at(x) = r; } // x の重みを出力する関数。 int weight(int x) { root(x); return diff_weight.at(x); } // weight.at(y) - weight.at(x) == w となるようにする。 bool unite(int x, int y, int w) { int rx = root(x); int ry = root(y); int diff_weight_to_ry_from_rx = w + weight(x) - weight(y); if (rx == ry) return false; // xの根とyの根が同じなので、何もしない。 if (rank.at(rx) < rank.at(ry)) { parent.at(rx) = ry; diff_weight.at(rx) = -diff_weight_to_ry_from_rx; } else { parent.at(ry) = rx; diff_weight.at(ry) = diff_weight_to_ry_from_rx; if (rank.at(rx) == rank.at(ry)) rank.at(rx)++; } return true; } bool same(int x, int y) { return (root(x) == root(y)); } }; class SegmentTree { private: int ini = INF; public: int Size; vector<int> node; SegmentTree(int N) : Size(N), node(N) { int new_N = 1; while (new_N < N) new_N *= 2; node.assign(2 * new_N - 1, ini); Size = new_N; } SegmentTree(vector<int> v) : Size(v.size()), node(v.size()) { int new_N = 1; while (new_N < v.size()) new_N *= 2; node.assign(2 * new_N - 1, ini); Size = new_N; // 葉の初期化 for (int i = 0; i < v.size(); i++) node.at(Size - 1 + i) = v.at(i); // 上りながら初期化 for (int i = Size - 2; i >= 0; i--) node.at(i) = min(node.at(2 * i + 1), node.at(2 * i + 2)); } // k 番目の値 (0-indexed) を x に変更する。 void update(int k, int x) { // 葉のノードの番号 (等比数列の和の公式) k += Size - 1; node.at(k) = x; // 上りながら更新 (0-indexedの場合、例えば、3 の子は、7, 8) while (k > 0) { k = (k - 1) / 2; // 子の最小値を親の最小値とする。 node.at(k) = min(node.at(k * 2 + 1), node.at(k * 2 + 2)); } } //[a, b) の最小値を計算する。[l, r) は、ノード k に対応する区間を与える。 // query(a, b, 0, 0, size) で呼べばよい。 int query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return ini; // 交差しない。 if (a <= l && r <= b) return node.at(k); // 完全に含む。 else { int vl = query(a, b, k * 2 + 1, l, (l + r) / 2); int vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return min(vl, vr); } } }; // 転倒数を返す (comp = less<int>() の場合)。 // comp = greater<int>() の場合は、N_C_2 - 転倒数 を返すことになる。 int my_merge(vector<int> &A, vector<int> &B, int left, int mid, int right, function<bool(int, int)> comp) { int i = left; // Aを分割したときの、左側の配列を差す配列。 int j = mid; // Bを分割したときの、左側の配列を差す配列。 int k = 0; // 分割した後の配列 (一時的に) B に保存。 int res = 0; while (i < mid && j < right) { if (comp(A.at(i), A.at(j))) B.at(k++) = A.at(i++); else { B.at(k++) = A.at(j++); res += mid - i; } } // 左側をBに移動し尽くしたので、右側を順に入れていく。 if (i == mid) { while (j < right) { B.at(k++) = A.at(j++); } } else { // 右側をBに移動し尽くしたので、左側を順に入れていく。 while (i < mid) { B.at(k++) = A.at(i++); } } // Aに値を戻す。 rep(l, k) { A.at(left + l) = B.at(l); } return res; } int my_merge_sort(vector<int> &target, vector<int> &tempo, int left, int right, function<bool(int, int)> comp) { int mid, res = 0; if (left == right || left == right - 1) return res; mid = (left + right) / 2; res += my_merge_sort(target, tempo, left, mid, comp); res += my_merge_sort(target, tempo, mid, right, comp); res += my_merge(target, tempo, left, mid, right, comp); return res; } const int max_K = 100; int dp[max_K + 1][max_K + 1]; // 左から i 番目まで見る // 右から j 番目まで見る // K - i - j 個は捨てる。 signed main() { int N, K; cin >> N >> K; deque<int> V(N); rep(i, N) cin >> V.at(i); int res = 0; if (K >= N) { priority_queue<int> que; rep(i, N) { que.push(V.at(i)); } while (!que.empty() && que.top() > 0) { res += que.top(); que.pop(); } cout << res << endl; return 0; } else { rep(i, K + 1) { rep(j, K + 1) { if (K - i - j < 0) continue; priority_queue<int> que; rep(k, i) que.push(V.at(k)); rep(k, j) { que.push(V.at(N - 1 - k)); } int tempo1 = 0; int M = 2 * (i + j) - K; rep(k, M) { int tempo2 = que.top(); que.pop(); tempo1 += tempo2; } if (i + j - M > 0) { rep(k, i + j - M) { int tempo2; if (!que.empty()) { tempo2 = que.top(); que.pop(); } else break; if (tempo2 > 0) tempo1 += tempo2; else break; } } if (tempo1 > res) res = tempo1; } } cout << res << endl; return 0; } }
replace
583
605
583
634
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> //Carefully Crafted by hetp111 using namespace std; #define int long long #define double long double #define all(v) (v).begin(), (v).end() #define vi vector<int> #define vvi vector<vi> #define pii pair<int, int> #define vii vector<pii> #define MOD 1000000007 #define MOD2 998244353 #define MOD3 1000000009 #define PI acos(-1) #define eps (1e-8) #define INF (1e18) #define FASTER \ ios_base::sync_with_stdio(0); \ cin.tie(0) template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a) { return out << "(" << a.first << "," << a.second << ")"; } template <class A> ostream &operator<<(ostream &out, const vector<A> &a) { for (const A &it : a) out << it << " "; return out; } template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) { return in >> a.first >> a.second; } template <class A> istream &operator>>(istream &in, vector<A> &a) { for (A &i : a) in >> i; return in; } // ifstream cinn("in.in");ofstream coutt("out.out"); int poww(const int &a, int b, const int &m = MOD) { if (b == 0) return 1; int x = poww(a, b / 2, m); x = x * x % m; if (b & 1) x = x * a % m; return x; } int ceil(const int &a, const int &b) { return (a + b - 1) / b; } ////Read: // Check corner cases, n==1; // ////Some function: //__builtin_popcountll(), int n, c, dp[55][55][55][55]; vi v; int f(int i, int cnt, int mxgap, int lst) { if (i == n) { mxgap = max(mxgap, i - lst - 1); if (cnt + (n - cnt - mxgap) * 2 <= c) return 0; return -INF; } int &ans = dp[i][cnt][mxgap][lst + 1]; if (ans != -INF) return ans; ans = v[i] + f(i + 1, cnt + 1, max(mxgap, i - lst - 1), i); ans = max(ans, f(i + 1, cnt, mxgap, lst)); } signed main() { FASTER; cin >> n >> c; v = vi(n); cin >> v; for (int i = 0; i < 55; i++) { for (int j = 0; j < 55; j++) { for (int k = 0; k < 55; k++) { for (int l = 0; l < 55; l++) { dp[i][j][k][l] = -INF; } } } } cout << f(0, 0, 0, -1); }
#include <bits/stdc++.h> //Carefully Crafted by hetp111 using namespace std; #define int long long #define double long double #define all(v) (v).begin(), (v).end() #define vi vector<int> #define vvi vector<vi> #define pii pair<int, int> #define vii vector<pii> #define MOD 1000000007 #define MOD2 998244353 #define MOD3 1000000009 #define PI acos(-1) #define eps (1e-8) #define INF (1e18) #define FASTER \ ios_base::sync_with_stdio(0); \ cin.tie(0) template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a) { return out << "(" << a.first << "," << a.second << ")"; } template <class A> ostream &operator<<(ostream &out, const vector<A> &a) { for (const A &it : a) out << it << " "; return out; } template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) { return in >> a.first >> a.second; } template <class A> istream &operator>>(istream &in, vector<A> &a) { for (A &i : a) in >> i; return in; } // ifstream cinn("in.in");ofstream coutt("out.out"); int poww(const int &a, int b, const int &m = MOD) { if (b == 0) return 1; int x = poww(a, b / 2, m); x = x * x % m; if (b & 1) x = x * a % m; return x; } int ceil(const int &a, const int &b) { return (a + b - 1) / b; } ////Read: // Check corner cases, n==1; // ////Some function: //__builtin_popcountll(), int n, c, dp[55][55][55][55]; vi v; int f(int i, int cnt, int mxgap, int lst) { if (i == n) { mxgap = max(mxgap, i - lst - 1); if (cnt + (n - cnt - mxgap) * 2 <= c) return 0; return -INF; } int &ans = dp[i][cnt][mxgap][lst + 1]; if (ans != -INF) return ans; ans = v[i] + f(i + 1, cnt + 1, max(mxgap, i - lst - 1), i); ans = max(ans, f(i + 1, cnt, mxgap, lst)); return ans; } signed main() { FASTER; cin >> n >> c; v = vi(n); cin >> v; for (int i = 0; i < 55; i++) { for (int j = 0; j < 55; j++) { for (int k = 0; k < 55; k++) { for (int l = 0; l < 55; l++) { dp[i][j][k][l] = -INF; } } } } cout << f(0, 0, 0, -1); }
insert
67
67
67
68
0
p03032
C++
Runtime Error
#include <algorithm> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <istream> #include <queue> #include <random> #include <sstream> #include <stack> #include <string> #include <typeinfo> #include <vector> using namespace std; typedef long long ll; int main() { ll N, K; cin >> N >> K; vector<ll> V(N, 0); for (ll i = 0; i < N; i++) { cin >> V[i]; } ll ans = 0; for (ll l = 0; l <= min(K, N); l++) { for (ll r = 0; l + r <= min(K, N); r++) { // ぷらいおりてぃきゅー // 昇順 priority_queue<ll, vector<ll>, greater<ll>> que; ll sum = 0; for (ll i = 0; i < l; i++) { que.push(V[i]); sum += V[i]; } for (ll i = 0; i < r; i++) { que.push(V[N - i - 1]); sum += V[N - i - 1]; } // マイナスの価値の宝石を消す for (ll i = 0; i <= min(K - l - r, (ll)que.size()); i++) { if (que.top() < 0) { sum -= que.top(); que.pop(); } } ans = max(ans, sum); } } cout << ans << endl; }
#include <algorithm> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <istream> #include <queue> #include <random> #include <sstream> #include <stack> #include <string> #include <typeinfo> #include <vector> using namespace std; typedef long long ll; int main() { ll N, K; cin >> N >> K; vector<ll> V(N, 0); for (ll i = 0; i < N; i++) { cin >> V[i]; } ll ans = 0; for (ll l = 0; l <= min(K, N); l++) { for (ll r = 0; l + r <= min(K, N); r++) { // ぷらいおりてぃきゅー // 昇順 priority_queue<ll, vector<ll>, greater<ll>> que; ll sum = 0; for (ll i = 0; i < l; i++) { que.push(V[i]); sum += V[i]; } for (ll i = 0; i < r; i++) { que.push(V[N - i - 1]); sum += V[N - i - 1]; } // マイナスの価値の宝石を消す for (ll i = 0; i < K - l - r; i++) { if (que.empty()) break; if (que.top() > 0) break; if (que.top() < 0) { sum -= que.top(); que.pop(); } } ans = max(ans, sum); } } cout << ans << endl; }
replace
43
44
43
48
-11
p03032
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define FAST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define dofloat cout << fixed << setprecision(8) #define pb push_back #define mp make_pair #define fi first #define se second #define bitcount _builtin_popcount #define all(vec) vec.begin(), vec.end() #define rall(vec) vec.rbegin(), vec.rend() using namespace __gnu_pbds; using namespace std; // typedef typedef long long ll; typedef long double ld; typedef vector<long long> vl; typedef pair<long long, long long> pll; typedef vector<pair<long long, long long>> vll; typedef vector<pair<int, int>> vii; typedef vector<int> vi; typedef pair<int, int> ii; // constants const long long MOD = 1000000007; const long long MAX = 100005; const long double PI = 3.14159265359; const long double G = 9.807; const long long INF = 1e18; const long double EPS = 1e-6; // #define forn(i, n) for (ll i = 0; i < n; i++) // gcd of a and b long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } // distance between (a,b) and (c,d) long double dis(long long a, long long b, long long c, long long d) { return sqrt(((a) - (c)) * ((a) - (c)) + ((b) - (d)) * ((b) - (d))); } // simple prime check bool isprime(long long a) { if (a == 2) return 1; if (!(a & 1)) return 0; for (ll i = 3; i * i <= a; i += 2) { if (a % i == 0) return 0; } return 1; } // fast expo long long mpow(long long base, long long exponent, long long modulus) { if (modulus == 1) return 0; long long result = 1; base = base % modulus; while (exponent) { if (exponent % 2 == 1) result = (result * base) % modulus; exponent = exponent >> 1; base = (base * base) % modulus; } return result; } // modular inverse ll minv(ll a, ll mod) { ll _gcd = gcd(a, mod); assert(_gcd == 1); return mpow(a, mod - 2, mod); } /* template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; */ /* ll dx[]={1,0,-1,0,1,1,-1,-1}; ll dy[]={0,-1,0,1,1,-1,-1,1}; */ /* void sieve(int N){ for(int i=0;i<=N;i++){ spf[i]=i; } for(int i=2;i*i<=N;i++){ if(spf[i]==i){ for(int j=i*i;j<=N;j+=i){ spf[j]=min(spf[j],i); } } } return; } */ /* void nCr(ll n,ll k){ ll i,j; for(i=0;i<=n;i++){ for(j=0;j<=min(i,k);j++){ if(j==0 or j==i)C[i][j]=1LL; else C[i][j]=(C[i-1][j-1]+C[i-1][j])%MOD; } } return; } */ /* ll ncr(ll N,ll K){ if(N<K)return 0; if(K==0)return 1; if(N==0)return 0; ll den=1; for(ll i=1;i<=K;i++)den*=i; ll num=1; while(K--){ num*=N; if(num%den==0){ num/=den; den=1; } N--; } return num; } */ ll n, k; ll dp[105][55][55], done[105][55][55]; ll a[55]; ll solve(ll left, ll lo, ll hi) { if (hi < lo) { return 0; } if (left == 0) { return 0; } ll ans = 0; // either reserve a card for take and keep or take the card or stop here if (left >= 2) { ans += max({solve(left - 2, lo + 1, hi), solve(left - 2, lo, hi - 1), a[lo] + solve(left - 1, lo + 1, hi), a[hi] + solve(left - 1, lo, hi - 1), solve(0, lo, hi)}); } else { ans += max({a[lo] + solve(left - 1, lo + 1, hi), a[hi] + solve(left - 1, lo, hi - 1), solve(0, lo, hi)}); } done[left][lo][hi] = 1; return dp[left][lo][hi] = ans; } int main() { FAST; cin >> n >> k; for (ll i = 0; i < n; i++) cin >> a[i]; cout << solve(k, 0, n - 1); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define FAST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define dofloat cout << fixed << setprecision(8) #define pb push_back #define mp make_pair #define fi first #define se second #define bitcount _builtin_popcount #define all(vec) vec.begin(), vec.end() #define rall(vec) vec.rbegin(), vec.rend() using namespace __gnu_pbds; using namespace std; // typedef typedef long long ll; typedef long double ld; typedef vector<long long> vl; typedef pair<long long, long long> pll; typedef vector<pair<long long, long long>> vll; typedef vector<pair<int, int>> vii; typedef vector<int> vi; typedef pair<int, int> ii; // constants const long long MOD = 1000000007; const long long MAX = 100005; const long double PI = 3.14159265359; const long double G = 9.807; const long long INF = 1e18; const long double EPS = 1e-6; // #define forn(i, n) for (ll i = 0; i < n; i++) // gcd of a and b long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } // distance between (a,b) and (c,d) long double dis(long long a, long long b, long long c, long long d) { return sqrt(((a) - (c)) * ((a) - (c)) + ((b) - (d)) * ((b) - (d))); } // simple prime check bool isprime(long long a) { if (a == 2) return 1; if (!(a & 1)) return 0; for (ll i = 3; i * i <= a; i += 2) { if (a % i == 0) return 0; } return 1; } // fast expo long long mpow(long long base, long long exponent, long long modulus) { if (modulus == 1) return 0; long long result = 1; base = base % modulus; while (exponent) { if (exponent % 2 == 1) result = (result * base) % modulus; exponent = exponent >> 1; base = (base * base) % modulus; } return result; } // modular inverse ll minv(ll a, ll mod) { ll _gcd = gcd(a, mod); assert(_gcd == 1); return mpow(a, mod - 2, mod); } /* template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; */ /* ll dx[]={1,0,-1,0,1,1,-1,-1}; ll dy[]={0,-1,0,1,1,-1,-1,1}; */ /* void sieve(int N){ for(int i=0;i<=N;i++){ spf[i]=i; } for(int i=2;i*i<=N;i++){ if(spf[i]==i){ for(int j=i*i;j<=N;j+=i){ spf[j]=min(spf[j],i); } } } return; } */ /* void nCr(ll n,ll k){ ll i,j; for(i=0;i<=n;i++){ for(j=0;j<=min(i,k);j++){ if(j==0 or j==i)C[i][j]=1LL; else C[i][j]=(C[i-1][j-1]+C[i-1][j])%MOD; } } return; } */ /* ll ncr(ll N,ll K){ if(N<K)return 0; if(K==0)return 1; if(N==0)return 0; ll den=1; for(ll i=1;i<=K;i++)den*=i; ll num=1; while(K--){ num*=N; if(num%den==0){ num/=den; den=1; } N--; } return num; } */ ll n, k; ll dp[105][55][55], done[105][55][55]; ll a[55]; ll solve(ll left, ll lo, ll hi) { if (hi < lo) { return 0; } if (left == 0) { return 0; } if (done[left][lo][hi]) return dp[left][lo][hi]; ll ans = 0; // either reserve a card for take and keep or take the card or stop here if (left >= 2) { ans += max({solve(left - 2, lo + 1, hi), solve(left - 2, lo, hi - 1), a[lo] + solve(left - 1, lo + 1, hi), a[hi] + solve(left - 1, lo, hi - 1), solve(0, lo, hi)}); } else { ans += max({a[lo] + solve(left - 1, lo + 1, hi), a[hi] + solve(left - 1, lo, hi - 1), solve(0, lo, hi)}); } done[left][lo][hi] = 1; return dp[left][lo][hi] = ans; } int main() { FAST; cin >> n >> k; for (ll i = 0; i < n; i++) cin >> a[i]; cout << solve(k, 0, n - 1); return 0; }
insert
144
144
144
146
TLE
p03032
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdlib> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <vector> using namespace std; int main() { int N, K; cin >> N >> K; vector<long long int> D(N); vector<long long int> Juwel(N); for (int i = 0; i < N; i++) { cin >> D.at(i); } int answer = 0, sum = 0; for (int action = 0; action <= K; action++) { for (int back = 0; back <= action; back++) { for (int left = 0; left <= action - back; left++) { Juwel.clear(); sum = 0; if (left < (int)D.size()) { for (int i = 0; i < left; i++) Juwel.push_back(D.at(i)); for (int i = 0; i < action - back - left; i++) Juwel.push_back(D.at(N - i - 1)); } else { for (int i = 0; i < (int)D.size(); i++) Juwel.push_back(D.at(i)); } sort(Juwel.begin(), Juwel.end()); for (int i = 0; i < min(back, (int)Juwel.size()); i++) { if (Juwel.at(i) < 0) Juwel.at(i) = 0; } for (int i = 0; i < (int)Juwel.size(); i++) sum += Juwel.at(i); if (sum >= answer) answer = sum; } } } cout << answer << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdlib> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <vector> using namespace std; int main() { int N, K; cin >> N >> K; vector<long long int> D(N); vector<long long int> Juwel(N); for (int i = 0; i < N; i++) { cin >> D.at(i); } int answer = 0, sum = 0; for (int action = 0; action <= K; action++) { for (int back = 0; back <= action; back++) { for (int left = 0; left <= action - back; left++) { Juwel.clear(); sum = 0; if (left < (int)D.size()) { for (int i = 0; i < left; i++) Juwel.push_back(D.at(i)); for (int i = 0; i < min(action - back - left, N - back - left); i++) Juwel.push_back(D.at(N - i - 1)); } else { for (int i = 0; i < (int)D.size(); i++) Juwel.push_back(D.at(i)); } sort(Juwel.begin(), Juwel.end()); for (int i = 0; i < min(back, (int)Juwel.size()); i++) { if (Juwel.at(i) < 0) Juwel.at(i) = 0; } for (int i = 0; i < (int)Juwel.size(); i++) sum += Juwel.at(i); if (sum >= answer) answer = sum; } } } cout << answer << endl; return 0; }
replace
34
35
34
35
0
p03032
C++
Runtime Error
// 2020-07-15 23:13:36 #include <bits/stdc++.h> #ifdef LOCAL #include "lib/debug.hpp" #else #define debug(...) 1 #endif #define ALL(a) (a).begin(), (a).end() #define rep(i, n) REP(i, 0, (n)) #define repc(i, n) REPC(i, 0, (n)) #define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++) #define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++) #define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--) using namespace std; using ll = long long; using ld = long double; using pr = pair<ll, ll>; using vll = vector<ll>; using vpr = vector<pr>; template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } else return false; } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } else return false; } vector<int> vs; int dp[55][55][105]; int rec(int l, int r, int k) { if (dp[l][r][k] >= 0) return dp[l][r][k]; chmax(dp[l][r][k], 0); if (k >= 1) { chmax(dp[l][r][k], rec(l + 1, r, k - 1) + vs[l]); chmax(dp[l][r][k], rec(l, r - 1, k - 1) + vs[r - 1]); } if (k >= 2) { chmax(dp[l][r][k], rec(l + 1, r, k - 2)); chmax(dp[l][r][k], rec(l, r - 1, k - 2)); } return dp[l][r][k]; } void answer() { int n, k; cin >> n >> k; vs.resize(n); rep(i, n) cin >> vs[i]; repc(i, n) repc(j, n) repc(ki, k) dp[i][j][ki] = -1; cout << rec(0, n, k) << '\n'; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); answer(); return 0; }
// 2020-07-15 23:13:36 #include <bits/stdc++.h> #ifdef LOCAL #include "lib/debug.hpp" #else #define debug(...) 1 #endif #define ALL(a) (a).begin(), (a).end() #define rep(i, n) REP(i, 0, (n)) #define repc(i, n) REPC(i, 0, (n)) #define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++) #define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++) #define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--) using namespace std; using ll = long long; using ld = long double; using pr = pair<ll, ll>; using vll = vector<ll>; using vpr = vector<pr>; template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } else return false; } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } else return false; } vector<int> vs; int dp[55][55][105]; int rec(int l, int r, int k) { if (dp[l][r][k] >= 0) return dp[l][r][k]; chmax(dp[l][r][k], 0); if (l == r) return dp[l][r][k]; if (k >= 1) { chmax(dp[l][r][k], rec(l + 1, r, k - 1) + vs[l]); chmax(dp[l][r][k], rec(l, r - 1, k - 1) + vs[r - 1]); } if (k >= 2) { chmax(dp[l][r][k], rec(l + 1, r, k - 2)); chmax(dp[l][r][k], rec(l, r - 1, k - 2)); } return dp[l][r][k]; } void answer() { int n, k; cin >> n >> k; vs.resize(n); rep(i, n) cin >> vs[i]; repc(i, n) repc(j, n) repc(ki, k) dp[i][j][ki] = -1; cout << rec(0, n, k) << '\n'; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); answer(); return 0; }
insert
40
40
40
42
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using VVV = V<VV<T>>; template <class S, class T> using P = pair<S, T>; template <class S, class T, class U> using TP = tuple<S, T, U>; using ll = long long; using ull = unsigned long long; using dbl = double; using str = string; using vll = V<ll>; using vvll = V<vll>; using vvvll = V<vvll>; using pl = P<ll, ll>; using tl = TP<ll, ll, ll>; using vpl = V<pl>; using vvpl = V<vpl>; using vtl = V<tl>; using vvtl = V<vtl>; using vs = V<str>; using vvs = V<vs>; using vd = V<dbl>; using vvd = V<vd>; using qll = queue<ll>; using qpl = queue<pl>; using stll = stack<ll>; using stpl = stack<pl>; using mapll = map<ll, ll>; using setll = set<ll>; using pqll = priority_queue<ll>; #define int ll #define fi first #define se second #define mp make_pair #define mt make_tuple #define pb push_back #define pob pop_back() #define pf push_front #define pof pop_front() #define sz size() #define bgn begin() #define en end() #define asn assign #define emp empty() #define fr front() #define bk back() #define clr clear() #define ins insert #define ers erase #define res resize #define tp top() #define p_q priority_queue #define inv inverse() #define FOR(i, a, b) for (ll i = (a); i <= (ll)(b); i++) #define rFOR(i, a, b) for (ll i = (b); i >= (ll)(a); i--) #define REP(i, a) FOR((i), 0, (ll)(a)-1) #define REP0(i, a) FOR((i), 0, (ll)(a)) #define REP1(i, a) FOR((i), 1, (ll)(a)) #define rREP(i, a) rFOR((i), 0, (ll)(a)-1) #define rREP0(i, a) rFOR((i), 0, (ll)(a)) #define rREP1(i, a) rFOR((i), 1, (ll)(a)) #define ROR(v, i) for (auto &(i) : (v)) #define IOTA(a, n) iota((a).bgn, (a).en, (n)) #define SORT(a) sort((a).bgn, (a).en) #define rSORT(a) sort((a).rbegin(), (a).rend()) #define UNIQUE(a) (a).erase(unique((a).bgn, (a).en), (a).en) #define PREVP(a) prev_permutation((a).bgn, (a).en) #define NEXTP(a) next_permutation((a).bgn, (a).en) #define BINS(a, b) binary_search((a).bgn, (a).en, (b)) #define LOWB(a, b) (lower_bound((a).bgn, (a).en, (b)) - (a).bgn) #define UPB(a, b) (upper_bound((a).bgn, (a).en, (b)) - (a).bgn) #define CNT(a, b) count((a).bgn, (a).en, b) #define SUM(a) accumulate((a).bgn, (a).en, 0) #define REV(a) reverse((a).bgn, (a).en) #define REGS(a, b) regex_search((a), regex(b)) #define REGM(a, b) regex_match((a), regex(b)) #define yn(a) cout << ((a) ? "yes" : "no") << "\n"; #define Yn(a) cout << ((a) ? "Yes" : "No") << "\n"; #define YN(a) cout << ((a) ? "YES" : "NO") << "\n"; #define imp(a) cout << ((a) ? "possible" : "impossible") << "\n"; #define Imp(a) cout << ((a) ? "Possible" : "Impossible") << "\n"; #define IMP(a) cout << ((a) ? "POSSIBLE" : "IMPOSSIBLE") << "\n"; #define fs(a) cout << ((a) ? "second" : "first") << "\n"; #define Fs(a) cout << ((a) ? "Second" : "First") << "\n"; #define FS(a) cout << ((a) ? "SECOND" : "FIRST") << "\n"; #define sak cout << "\n"; #define sas cout << " "; #define sat cout << "\t"; #define dbg(a) cerr << (#a) << ": " << (a) << "\n"; #define dbgs(...) \ dal(#__VA_ARGS__); \ dal(__VA_ARGS__); #define c2l(a) ((ll)(a - 48)) #define a2l(a) ((ll)(a - 97)) #define A2l(a) ((ll)(a - 65)) #define l2c(a) ((char)(a + 48)) #define l2a(a) ((char)(a + 97)) #define l2A(a) ((char)(a + 65)) #define DigN2(a) ((llabs(a) == 0) ? (1) : ((ll)(log2(double(llabs(a)))) + 1)) #define DigN10(a) ((llabs(a) == 0) ? (1) : ((ll)(log10(double(llabs(a)))) + 1)) #define Dig2(a, b) (((a) >> (b)) & 1) #define Dig10(a, b) (ll)(((a) / ((ll)(pow(10.0, (double)(b))))) % 10) #define Pow2(a) ((ll)(1) << (a)) #define Pow10(a) ((ll)(pow(10.0, double(a)))) #define LSB(a) ((a) & (-(a))) #define vin(v) \ ROR((v), (i)) { cin >> (i); }; #define vllin(v, N) \ vll(v)((N)); \ vin(v); #define vllin2(a, b, N) \ vll(a)(N), (b)(N); \ REP(i, N) { cin >> (a)[i] >> (b)[i]; }; #define vsin(v, N) \ vs(v)((N)); \ vin(v); #define rdn(a, b) ((a) / (b)) #define rou(a, b) \ ((((double(a) / double(b)) - ((a) / (b))) < 0.5) ? ((a) / (b)) \ : (((a) / (b)) + 1)) #define rup(a, b) ((((a) % (b)) == 0) ? ((a) / (b)) : (((a) / (b)) + 1)) #define min(...) Min(__VA_ARGS__) #define max(...) Max(__VA_ARGS__) #define emin(a, ...) ((a) = Min((a), __VA_ARGS__)) #define emax(a, ...) ((a) = Max((a), __VA_ARGS__)) #define egcd(a, ...) ((a) = gcd((a), __VA_ARGS__)) #define elcm(a, ...) ((a) = lcm((a), __VA_ARGS__)) #define powll(a, b) (ll)(pow((double)(a), (double)(b))) #define Triangle(x1, y1, x2, y2, x3, y3) \ (((x1) - (x2)) * ((y1) - (y3)) - ((x1) - (x3)) * ((y1) - (y2))) #define svll SumV<ll> #define svvll SumV2<ll> #define li(...) \ ll __VA_ARGS__; \ Input(__VA_ARGS__); #define si(...) \ str __VA_ARGS__; \ Input(__VA_ARGS__); #define vli(size, ...) \ vll __VA_ARGS__; \ vInput(size, __VA_ARGS__); #define vsi(size, ...) \ vs __VA_ARGS__; \ vInput(size, __VA_ARGS__); // const ll MOD = 1e9 + 7; const ll MOD = 998244353; // const ll MOD = 924844033; // const ll MOD = 9007199254740881; const ll INF = 1LL << 60; // 1.15e18 const double PI = acos(-1.0); const vll DX = {0, -1, 0, 1, 0, -1, 1, 1, -1}; const vll DY = {0, 0, -1, 0, 1, -1, -1, 1, 1}; const str alp = "abcdefghijklmnopqrstuvwxyz"; const str ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; void Input() {} template <class Var, class... Args> void Input(Var &var, Args &...args) { cin >> var; Input(args...); } void vInit(ll size) {} template <class T, class... Args> void vInit(ll size, V<T> &v, Args &...args) { v.res(size); vInit(size, args...); } void vInputNum(ll num) {} template <class T, class... Args> void vInputNum(ll num, V<T> &v, Args &...args) { cin >> v[num]; vInputNum(num, args...); } void vInput(ll size) {} template <class... Args> void vInput(ll size, Args &...args) { vInit(size, args...); REP(i, size) vInputNum(i, args...); } template <class S, class T> ostream &operator<<(ostream &out, const P<S, T> &p) { return out << "[" << p.fi << ", " << p.se << "]"; } template <class T> ostream &operator<<(ostream &out, V<T> &v) { if (v.emp) return out << "{}"; else { auto itr = v.bgn; out << "{" << *itr; itr++; while (itr != v.en) { out << ", " << *itr; itr++; } out << "}"; return out; } } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> &m) { if (m.emp) return out << "<[]>"; else { auto itr = m.bgn; out << "< [" << (itr->fi) << ": " << (itr->se); itr++; while (itr != m.en) { out << "], [" << (itr->fi) << ": " << (itr->se); itr++; } out << "] >"; return out; } } template <class T> ostream &operator<<(ostream &out, const set<T> &s) { if (s.emp) return out << "<>"; else { auto itr = s.bgn; out << "<" << *itr; itr++; while (itr != s.en) { out << ", " << *itr; itr++; } out << ">"; return out; } } void say() {} template <class T> void say(T t) { cout << t; } template <class Head, class... Body> void say(Head head, Body... body) { cout << head << " "; say(body...); } void sal() { cout << "\n"; } template <class... Args> void sal(Args... args) { say(args...); cout << "\n"; } void day() {} template <class T> void day(T t) { cerr << t; } template <class Head, class... Body> void day(Head head, Body... body) { cerr << head << " "; day(body...); } void dal() { cerr << "\n"; } template <class... Args> void dal(Args... args) { day(args...); cerr << "\n"; } void salv() {} template <class T> void salv(V<T> v) { if (v.emp) sal(); else { auto itr = v.bgn; say(*itr); itr++; while (itr != v.en) { sas; say(*itr); itr++; } sak; } } template <class T, class... Args> void salv(V<T> v, Args... args) { salv(v); salv(args...); } void salv2() {} template <class T> void salv2(V<V<T>> v) { if (v.emp) sal(); else { ROR(v, i) salv(i); } } template <class T, class... Args> void salv2(V<V<T>> v, Args... args) { salv2(v); salv2(args...); } template <class Monoid> struct Action { public: Monoid I; function<Monoid(Monoid, Monoid)> A; Action(Monoid I, function<Monoid(Monoid, Monoid)> A) : I(I), A(A) {} Monoid operator()() { return I; } Monoid operator()(Monoid x) { return x; } Monoid operator()(Monoid l, Monoid r) { return A(l, r); } template <class... Args> Monoid operator()(Monoid x, Args... args) { Monoid tmp = operator()(args...); return A(x, tmp); } }; template <class T> Action<T> ADD = Action<T>(0, [](T l, T r) { return l + r; }); template <> Action<str> ADD<str> = Action<str>("", [](str l, str r) { return l + r; }); template <class T1, class T2> Action<P<T1, T2>> ADD<P<T1, T2>> = Action<P<T1, T2>>(mp(ADD<T1>.I, ADD<T2>.I), [](P<T1, T2> l, P<T1, T2> r) { return mp(l.fi + r.fi, l.se + r.se); }); template <class T> Action<T> MUL = Action<T>(1, [](T l, T r) { return l * r; }); template <class T> Action<T> OR = Action<T>(0, [](T l, T r) { return l | r; }); template <class T> Action<T> XOR = Action<T>(0, [](T l, T r) { return l ^ r; }); template <class T> Action<T> AND = Action<T>(((ll)(1) << 63) - 1, [](T l, T r) { return l & r; }); template <> Action<bool> AND<bool> = Action<bool>(true, [](bool l, bool r) { return l & r; }); template <> Action<ull> AND<ull> = Action<ull>(((ull)(1) << 63) - 1, [](ull l, ull r) { return l & r; }); template <class T> Action<T> MIN = Action<T>(INF, [](T l, T r) { return (l < r) ? l : r; }); template <class T> Action<T> MAX = Action<T>(-INF, [](T l, T r) { return (l > r) ? l : r; }); template <class T> Action<T> GCD = Action<T>(0, [](T l, T r) { if (l < r) { l ^= r; r ^= l; l ^= r; } return (r ? GCD<T>(r, l % r) : l); }); template <class T> Action<T> LCM = Action<T>(1, [](T l, T r) { return (l * r) / GCD<T>(l, r); }); template <class Head> Head Min(Head head) { return head; } template <class Head, class... Body> Head Min(Head head, Body... body) { auto tmp = Min(body...); return (head < tmp) ? head : tmp; } template <class Head> Head Max(Head head) { return head; } template <class Head, class... Body> auto Max(Head head, Body... body) { auto tmp = Max(body...); return (head > tmp) ? head : tmp; } ll gcd(ll a, ll b) { if (a < b) { a ^= b; b ^= a; a ^= b; } return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll gcd(ll head) { return head; } template <class... Body> ll gcd(ll head, Body... body) { return gcd(head, gcd(body...)); } ll lcm(ll head) { return head; } template <class... Body> ll lcm(ll head, Body... body) { return lcm(head, lcm(body...)); } ll Bset(ll a, ll b, ll c) { if (c) a |= b; else a &= ~b; return a; } struct UFT { public: ll tsize; ll mode; vll par; vll rank; UFT() {} UFT(const UFT &uft) {} UFT(ll tsizeget, ll modeget = 0) { tsize = tsizeget; mode = modeget; par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); } ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return; if (mode) { par[x] += par[y]; par[y] = x; } else { 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]++; } } } ll size(ll x) { return -par[root(x)]; } }; struct pUFT { public: ll tsize; ll now; vll par; vll rank; vll mtime; vvll sizepi; vvll sizepv; pUFT() {} pUFT(const pUFT &puft) {} pUFT(ll tsizeget) { tsize = tsizeget; now = 0; par.asn(tsize, -1); rank.asn(tsize, 0); mtime.asn(tsize, INF); sizepi.asn(tsize, {0}); sizepv.asn(tsize, {1}); } ll root(ll x, ll t) { return (mtime[x] > t) ? x : root(par[x], t); } bool same(ll x, ll y, ll t) { return root(x, t) == root(y, t); } ll merge(ll x, ll y) { now++; x = root(x, now); y = root(y, now); if (x != y) { if (rank[x] < rank[y]) { par[y] += par[x]; sizepi[y].pb(now); sizepv[y].pb(-par[y]); par[x] = y; mtime[x] = now; } else { par[x] += par[y]; sizepi[x].pb(now); sizepv[x].pb(-par[x]); par[y] = x; mtime[y] = now; if (rank[x] == rank[y]) rank[x]++; } } return now; } ll size(ll x, ll t) { x = root(x, t); return sizepv[x][UPB(sizepi[x], t) - 1]; } }; struct wUFT { public: ll tsize; ll mode; vll par; vll rank; vll dweight; wUFT() {} wUFT(const wUFT &wuft) {} wUFT(ll tsizeget, ll modeget = 0) { tsize = tsizeget; mode = modeget; par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); dweight.asn(tsize, 0); } ll root(ll x) { if (par[x] < 0) return x; else { ll r = root(par[x]); dweight[x] += dweight[par[x]]; return par[x] = r; } } ll weight(ll x) { root(x); return dweight[x]; } ll diff(ll x, ll y) { return weight(y) - weight(x); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y, ll w) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return; if (mode) { par[x] += par[y]; par[y] = x; } else { if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = y; dweight[x] = -w; } else { par[x] += par[y]; par[y] = x; if (rank[x] == rank[y]) rank[x]++; dweight[y] = w; } } } ll size(ll x) { return -par[root(x)]; } }; template <typename valtype> class SegT { public: ll size; vector<valtype> v; valtype initv; function<valtype(valtype x, valtype y)> calc; SegT() {} SegT(const SegT &segt) {} SegT(ll sizeget, ll modeget = 0) { sizeset(sizeget); modeset(modeget); init(); } SegT(vector<valtype> cpyvec, ll modeget = 0) { sizeset(cpyvec.sz); modeset(modeget); init(); copy(cpyvec); } SegT(ll sizeget, valtype initvget, function<valtype(valtype x, valtype y)> calcget) { sizeset(sizeget); initv = initvget; calc = calcget; init(); } SegT(vector<valtype> cpyvec, valtype initvget, function<valtype(valtype x, valtype y)> calcget) { sizeset(cpyvec.sz); initv = initvget; calc = calcget; init(); copy(cpyvec); } void sizeset(ll rsize) { size = DigN2(rsize); if (rsize == Pow2(size - 1)) size--; return; } void modeset(ll mode) { switch (mode) { case 0: initv = 0; calc = [](valtype x, valtype y) { return x + y; }; break; case 1: initv = INF; calc = [](valtype x, valtype y) { return min(x, y); }; break; case 2: initv = -INF; calc = [](valtype x, valtype y) { return max(x, y); }; break; } return; } void init() { v.asn(Pow2(size + 1) - 1, initv); } void copy(vector<valtype> cpyvec) { REP(i, min(cpyvec.sz, Pow2(size))) set(i, cpyvec[i]); } ll i2v(ll i) const { if (i < 0 || i >= Pow2(size)) return -1; return Pow2(size) + i - 1; } ll top(ll i) const { if (i == 0) return -1; return (i - 1) / 2; } pl bot(ll i) const { if (i + 1 >= Pow2(size)) return mp(-1, -1); return mp(2 * i + 1, 2 * i + 2); } void set(ll i, valtype x) { i = i2v(i); v[i] = x; while (i > 0) { i = top(i); v[i] = calc(v[bot(i).fi], v[bot(i).se]); } return; } void add(ll i, valtype x) { set(i, v[i2v(i)] + x); return; } valtype operator[](const ll &i) const { return v[i2v(i)]; } // valtype que(ll a = 0, ll b = Pow2(size) - 1) { valtype que(ll a, ll b) { if (a == b) return v[i2v(a)]; if (a > b) return initv; // swap(a, b); valtype ans = initv; ll ai = i2v(a); ll bi = i2v(b); FOR(i, 1, size + 1) { if (a > b) break; if (a % Pow2(i)) { ans = calc(ans, v[ai]); a += Pow2(i - 1); ai = top(ai) + 1; } else { ai = top(ai); } if (a > b) break; if ((b + 1) % Pow2(i)) { ans = calc(ans, v[bi]); b -= Pow2(i - 1); bi = top(bi) - 1; } else { bi = top(bi); } if (a > b) break; } return ans; } valtype que(ll b) { return que(0, b); } valtype que() { return que(0, Pow2(size) - 1); } }; template <typename lentype> class Graph { public: ll size; ll mode; ll mapmode; lentype zlen; lentype ilen; vector<map<ll, ll>> v2n; vvll n2v; vector<vector<lentype>> Edge; vector<pair<lentype, ll>> Primresult; Graph() {} Graph(const Graph &graph) {} Graph(ll sizeget = 0, ll mapmodeget = 0, ll modeget = 0, lentype zlenget = 0, lentype ilenget = INF) { size = sizeget; mapmode = mapmodeget; mode = modeget; zlen = zlenget; ilen = ilenget; init(); } void init() { Edge.res(size); v2n.res(size); n2v.res(size); Primresult.asn(size, mp(ilen, -1)); } lentype lenplus(lentype l, lentype r) { return l + r; } lentype lenequal(lentype l, lentype r) { return l == r; } lentype lenlcr(lentype l, lentype r) { return l < r; } ll addV(ll vs) { size += vs; init(); return size; } void caddE(ll x, ll y, lentype c) { if (mapmode) v2n[x][y] = Edge[x].sz; Edge[x].pb(c); n2v[x].pb(y); } void csetE(ll x, ll y, lentype c) { if (mapmode) Edge[x][v2n[x][y]] = c; } void cersE(ll x, ll y) { if (mapmode) { ll n = v2n[x][y]; Edge[x][n] = ilen; n2v[x][n] = -1; v2n[x].ers(y); } } void addE(ll x, ll y, lentype c) { caddE(x, y, c); if (!mode) caddE(y, x, c); } void setE(ll x, ll y, lentype c) { csetE(x, y, c); if (!mode) csetE(y, x, c); } void ersE(ll x, ll y, lentype c) { cersE(x, y, c); if (!mode) cersE(y, x, c); } lentype getE(ll x, ll y) { if (mapmode) { if (v2n[x].count(y)) { return Edge[x][v2n[x][y]]; } } return ilen; } ll getVsz(ll x) { return Edge[x].sz; } pair<lentype, ll> getV(ll x, ll n) { if (n >= getVsz(x)) return mp(ilen, -1); return mp(Edge[x][n], n2v[x][n]); } vector<pair<lentype, vll>> Dijk(ll x) { vector<pair<lentype, vll>> result(size); REP(i, size) { result[i].fi = ilen; result[i].se = {-1}; } vll stat(size, 0); pair<lentype, ll> now; pair<lentype, ll> nowlv; SegT<pair<lentype, ll>> Q(size, mp(ilen, -1), [](pair<lentype, ll> l, pair<lentype, ll> r) { if (l.se == -1) return r; if (r.se == -1) return l; return l.fi < r.fi ? l : r; }); Q.set(x, mp(zlen, x)); result[x].fi = zlen; result[x].se = {-1}; while (Q.que(0, size - 1).se != -1) { now = Q.que(0, size - 1); Q.set(now.se, mp(ilen, -1)); stat[now.se] = 1; REP(i, getVsz(now.se)) { nowlv = getV(now.se, i); if (stat[nowlv.se]) continue; if (Q[nowlv.se].se == -1) { result[nowlv.se].fi = lenplus(result[now.se].fi, nowlv.fi); result[nowlv.se].se = {now.se}; Q.set(nowlv.se, mp(result[nowlv.se].fi, nowlv.se)); } else { if (lenlcr(lenplus(result[now.se].fi, nowlv.fi), result[nowlv.se].fi)) { result[nowlv.se].fi = lenplus(result[now.se].fi, nowlv.fi); result[nowlv.se].se = {now.se}; Q.set(nowlv.se, mp(result[nowlv.se].fi, nowlv.se)); } else if (lenequal(lenplus(result[now.se].fi, nowlv.fi), result[nowlv.se].fi)) { result[nowlv.se].se.pb(now.se); } } } } return result; } lentype Prim(ll x = 0) { lentype ans = zlen; vll stat(size, 0); pair<lentype, ll> now; pair<lentype, ll> nowlv; SegT<pair<lentype, ll>> Q(size, mp(ilen, -1), [](pair<lentype, ll> l, pair<lentype, ll> r) { if (l.se == -1) return r; if (r.se == -1) return l; return l.fi < r.fi ? l : r; }); Q.set(x, mp(zlen, x)); Primresult[x] = mp(zlen, -1); while (Q.que(0, size - 1).se != -1) { now = Q.que(0, size - 1); Q.set(now.se, mp(ilen, -1)); stat[now.se] = 1; ans = lenplus(ans, Primresult[now.se].fi); REP(i, getVsz(now.se)) { nowlv = getV(now.se, i); if (stat[nowlv.se]) continue; if (Q[nowlv.se].se == -1) { Primresult[nowlv.se] = mp(nowlv.fi, now.se); Q.set(nowlv.se, mp(Primresult[nowlv.se].fi, nowlv.se)); } else { if (lenlcr(nowlv.fi, Primresult[nowlv.se].fi)) { Primresult[nowlv.se] = mp(nowlv.fi, now.se); Q.set(nowlv.se, mp(Primresult[nowlv.se].fi, nowlv.se)); } } } } return ans; } }; /*template <class Type> class DP { public: vector<Type> v; Type initv; vll size, block; DP() {} DP(const DP &dp) {} template<class... Args> DP(Args... args) { block.asn(1, 1); Initialize(args...); } void Initialize(Type initv_) { initv = initv_; v.asn(block.bk, initv); } template<class... Args> void Initialize(ll val, Args... args) { size.pb(val); block.pb(block.bk*val); Initialize(args...); } };*/ pl Bezout(ll a, ll b) { if (b != 0) { pl xy; xy = Bezout(b, a % b); return mp(xy.se, xy.fi - ((a / b) * xy.se)); } else { return mp(1, 0); } } pl Bez(ll a, ll b, ll c) { pl xy; ll x, y, z, gc; xy = Bezout(a, b); gc = gcd(a, b); if (c % gc != 0) return mp(-1, -1); x = xy.fi * (c / gc); y = xy.se * (c / gc); if (x < 0) z = rup(-x, (b / gc)); if (x >= 0) z = -x / (b / gc); x += z * (b / gc); y -= z * (a / gc); return mp(x, y); } ll DigS10(ll n) { ll ans = 0; while (1) { ans += n % 10; n /= 10; if (!n) break; } return ans; } ll isP(ll n) { if (n <= 1) return 0; FOR(i, 2, (ll)sqrt(n) + 1) { if (n % i == 0) return 0; } return 1; } ll Tot(ll n) { if (n <= 0) return 0; ll ans = n, x = 2; while (x * x <= n) { if (n % x == 0) { ans -= ans / x; while (n % x == 0) n /= x; } x++; } if (n > 1) ans -= ans / n; return ans; } template <class T> struct Sum { public: V<T> v, s; ll size; Action<T> Add; Sum(V<T> v, Action<T> Add = ADD<T>) : v(v), size(v.sz), Add(Add) { Init(); Calc(); } void Init() { s.asn(size + 1, Add.I); } void Calc() { REP(i, size) s[i + 1] = Add(s[i], v[i]); } T operator()(ll x) { return operator()(0, x); } T operator()(ll l, ll r) { if (l < 0) l = 0; if (r >= size) r = size - 1; if (l > r) return Add.I; return s[r + 1] - s[l]; // for ADD } }; using sumll = Sum<ll>; template <class T> struct Sum2 { public: V<V<T>> v, s; ll RowSize, ColumnSize; Action<T> Add; Sum2(V<V<T>> v, Action<T> Add = ADD<T>) : v(v), RowSize(v.sz), ColumnSize(v.sz ? v[0].sz : 0), Add(Add) { Init(); Calc(); } void Init() { s.asn(RowSize + 1, V<T>(ColumnSize, Add.I)); } void Calc() { REP1(r, RowSize) { REP1(c, ColumnSize) { s[r][c] = v[r - 1][c - 1] + operator()(r, c - 1) + operator()(r - 1, c) - operator()(r - 1, c - 1); } } } T operator()(ll r, ll c) { return operator()(0, 0, r, c); } T operator()(ll r1, ll c1, ll r2, ll c2) { if (r1 < 0) r1 = 0; if (c1 < 0) c1 = 0; if (r2 >= RowSize) r2 = RowSize - 1; if (c2 >= ColumnSize) c2 = ColumnSize - 1; if (r1 > r2) return Add.I; if (c1 > c2) return Add.I; return s[r2 + 1][c2 + 1] - s[r2 + 1][c1] - s[r1][c2 + 1] + s[r1][c1]; } }; using sum2ll = Sum2<ll>; template <class T> struct Point2 { public: V<V<T>> v; ll h, w; Point2() : h(0), w(0) {} Point2(ll h, ll w) : h(h), w(w) { asn(h, w); } Point2(ll h, ll w, T val) : h(h), w(w) { asn(h, w, val); } void assign(ll h, ll w) { v.asn(h, V<T>(w)); } void assign(ll h, ll w, ll val) { v.asn(h, V<T>(w, val)); } T &operator()(ll h, ll w) { return v[h][w]; } T &operator()(pl p) { return v[p.fi][p.se]; } }; template <class T> using P2 = Point2<T>; template <ll Mod> struct Modll { public: ll v; Modll() : v(0) {} Modll(ll _v) { set(_v % Mod + Mod); } Modll &set(ll _v) { v = (_v < Mod) ? _v : (_v - Mod); return *this; } Modll pow(ll n) const { Modll x = *this, ans = 1; while (n) { if (n & 1) ans *= x; x *= x; n >>= 1; } return ans; } Modll inverse() const { return (*this).pow(Mod - 2); } Modll operator+(const Modll &m) const { return Modll().set(v + m.v); } Modll operator-(const Modll &m) const { return Modll().set(Mod + v - m.v); } Modll operator*(const Modll &m) const { return Modll().set((ull(v) * m.v) % Mod); } Modll operator/(const Modll &m) const { return *this * m.inv; } Modll &operator+=(const Modll &m) { return *this = *this + m; } Modll &operator-=(const Modll &m) { return *this = *this - m; } Modll &operator*=(const Modll &m) { return *this = *this * m; } Modll &operator/=(const Modll &m) { return *this = *this / m; } Modll operator-() const { return Modll(0) - *this; } explicit operator bool() const { return v != 0; } friend ostream &operator<<(ostream &out, const Modll &m) { return out << m.v; } }; using mll = Modll<MOD>; using vmll = V<mll>; using vvmll = V<vmll>; using vvvmll = V<vvmll>; template <class T> T vmin(V<T> v) { T tmp = MIN<T>.I; ROR(v, i) emin(tmp, i); return tmp; } template <class T, class... Args> T vmin(V<T> v, Args... args) { T tmp = vmin(args...); return min(vmin(v), tmp); } template <class T> T vmax(V<T> v) { T tmp = MAX<T>.I; ROR(v, i) emax(tmp, i); return tmp; } template <class T, class... Args> T vmax(V<T> v, Args... args) { T tmp = vmax(args...); return max(vmax(v), tmp); } template <class T> T vgcd(V<T> v) { T tmp = GCD<T>.I; ROR(v, i) egcd(tmp, i); return tmp; } template <class T, class... Args> T vgcd(V<T> v, Args... args) { T tmp = vgcd(args...); return gcd(vgcd(v), tmp); } template <class T> T vlcm(V<T> v) { T tmp = LCM<T>.I; ROR(v, i) elcm(tmp, i); return tmp; } template <class T, class... Args> T vlcm(V<T> v, Args... args) { T tmp = vlcm(args...); return lcm(vlcm(v), tmp); } vmll MFactMemo(2, 1); vmll MIFactMemo(2, 1); mll mFact(ll n) { if (MFactMemo.sz <= n) { ll oldsize = MFactMemo.sz; MFactMemo.res(n + 1, 1); FOR(i, oldsize, n) MFactMemo[i] = MFactMemo[i - 1] * i; } return MFactMemo[n]; } mll miFact(ll n) { if (MIFactMemo.sz <= n) { ll oldsize = MIFactMemo.sz; MIFactMemo.res(n + 1, 1); MIFactMemo.bk = mFact(n).inv; rFOR(i, oldsize + 1, n) MIFactMemo[i - 1] = MIFactMemo[i] * i; } return MIFactMemo[n]; } mll mComb(ll n, ll k) { if (n < 0 || k < 0 || n < k) return 0; return mFact(n) * miFact(k) * miFact(n - k); } ll LIS(vll v, ll m = 0) { if (v.sz > 0) { ll ans = 0; vll dp(v.sz, INF); FOR(i, 0, v.sz - 1) { dp[m ? UPB(dp, v[i]) : LOWB(dp, v[i])] = v[i]; } FOR(i, 0, v.sz - 1) { if (dp[i] == INF) break; ans++; } return ans; } else { return 0; } } void PCmprs(vll &v) { if (v.sz == 0) return; vll vv(v); IOTA(vv, 0); sort(vv.bgn, vv.en, [&](ll v1, ll v2) { return v[v1] < v[v2]; }); IOTA(v, 0); sort(v.bgn, v.en, [&](ll v1, ll v2) { return vv[v1] < vv[v2]; }); return; } ll BblCnt(vll v) { PCmprs(v); SegT<ll> b(v.sz, 0); ll ans = 0; REP(i, v.sz) { ans += (i - b.que(0, v[i])); b.add(v[i], 1); } return ans; } ll Bsrch(function<bool(ll x)> f, ll mi, ll ma) { ll ng = mi - 1, ok = ma, mid; while (ok - ng > 1) { mid = (ng + ok) / 2; (f(mid) ? ok : ng) = mid; } return ok; } template <class T> V<V<T>> MultiMatrix(V<V<T>> A, V<V<T>> B, Action<T> Mul = MUL<T>, Action<T> Add = ADD<T>) { V<V<T>> ans; ll ii = A.sz; if (!ii) return ans; ll jj = A[0].sz; if (!jj) return ans; ll jj2 = B.sz; if (!jj2) return ans; if (jj != jj2) return ans; ll kk = B[0].sz; if (!kk) return ans; ans.asn(ii, V<T>(kk, 0)); REP(i, ii) { REP(k, kk) { REP(j, jj) { ans[i][k] = Add(ans[i][k], Mul(A[i][j], B[j][k])); } } } return ans; } vvll CombMemo(1000, vll(1000, -1)); ll Comb(ll n, ll k) { if ((n < 0) || (k < 0)) return 0; if (CombMemo[n][k] == -1) { if (n < k) CombMemo[n][k] = 0; else { if (n == 0) CombMemo[n][k] = 1; else if (k == 0) CombMemo[n][k] = 1; else if (n == k) CombMemo[n][k] = 1; else CombMemo[n][k] = Comb(n - 1, k - 1) + Comb(n - 1, k); } } return CombMemo[n][k]; } template <class T> map<T, ll> Dict(V<T> v) { map<T, ll> m; if (!v.sz) return m; SORT(v); UNIQUE(v); REP(i, v.sz) { m[v[i]] = i; } return m; } template <class T> vll Cmprs(V<T> v) { auto m = Dict(v); vll ans(v.sz); REP(i, v.sz) { ans[i] = m[v[i]]; } return ans; } template <class T> auto vecn(T val) { return val; } template <class... Args> auto vecn(ll val, Args... args) { auto tmp = vecn(args...); return V<decltype(tmp)>(val, tmp); } void Solve(); signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20) << fixed; Solve(); } void Solve() { li(n, k); vli(n, v); ll mx = 0; vll l(n + 10), r(n + 10); l[0] = 0; r[0] = 0; REP1(i, n) { l[i] = l[i - 1] + v[i - 1]; r[i] = r[i - 1] + v[n - i]; } vvll hs(n + 10, vll(n + 10, 0)), ms(n + 10, vll(n + 10, 0)); vector<ll> hhs, mms; REP1(a, n) { hhs.pb(v[a - 1]); mms.pb(v[n - a]); SORT(hhs); SORT(mms); REP(b, a) { if (b) { hs[a][b] = hs[a][b - 1]; if (hhs[b - 1] < 0) { hs[a][b] += hhs[b - 1]; } ms[a][b] = ms[a][b - 1]; if (mms[b - 1] < 0) { ms[a][b] += mms[b - 1]; } } } } REP(i, k + 1) { REP(j, k - i + 1) { REP(h, k - i - j + 1) { REP(m, k - i - j - h + 1) { mx = max(mx, l[i] + r[j] - hs[i][h] - ms[j][m]); } } } } cout << mx; }
#include <bits/stdc++.h> using namespace std; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using VVV = V<VV<T>>; template <class S, class T> using P = pair<S, T>; template <class S, class T, class U> using TP = tuple<S, T, U>; using ll = long long; using ull = unsigned long long; using dbl = double; using str = string; using vll = V<ll>; using vvll = V<vll>; using vvvll = V<vvll>; using pl = P<ll, ll>; using tl = TP<ll, ll, ll>; using vpl = V<pl>; using vvpl = V<vpl>; using vtl = V<tl>; using vvtl = V<vtl>; using vs = V<str>; using vvs = V<vs>; using vd = V<dbl>; using vvd = V<vd>; using qll = queue<ll>; using qpl = queue<pl>; using stll = stack<ll>; using stpl = stack<pl>; using mapll = map<ll, ll>; using setll = set<ll>; using pqll = priority_queue<ll>; #define int ll #define fi first #define se second #define mp make_pair #define mt make_tuple #define pb push_back #define pob pop_back() #define pf push_front #define pof pop_front() #define sz size() #define bgn begin() #define en end() #define asn assign #define emp empty() #define fr front() #define bk back() #define clr clear() #define ins insert #define ers erase #define res resize #define tp top() #define p_q priority_queue #define inv inverse() #define FOR(i, a, b) for (ll i = (a); i <= (ll)(b); i++) #define rFOR(i, a, b) for (ll i = (b); i >= (ll)(a); i--) #define REP(i, a) FOR((i), 0, (ll)(a)-1) #define REP0(i, a) FOR((i), 0, (ll)(a)) #define REP1(i, a) FOR((i), 1, (ll)(a)) #define rREP(i, a) rFOR((i), 0, (ll)(a)-1) #define rREP0(i, a) rFOR((i), 0, (ll)(a)) #define rREP1(i, a) rFOR((i), 1, (ll)(a)) #define ROR(v, i) for (auto &(i) : (v)) #define IOTA(a, n) iota((a).bgn, (a).en, (n)) #define SORT(a) sort((a).bgn, (a).en) #define rSORT(a) sort((a).rbegin(), (a).rend()) #define UNIQUE(a) (a).erase(unique((a).bgn, (a).en), (a).en) #define PREVP(a) prev_permutation((a).bgn, (a).en) #define NEXTP(a) next_permutation((a).bgn, (a).en) #define BINS(a, b) binary_search((a).bgn, (a).en, (b)) #define LOWB(a, b) (lower_bound((a).bgn, (a).en, (b)) - (a).bgn) #define UPB(a, b) (upper_bound((a).bgn, (a).en, (b)) - (a).bgn) #define CNT(a, b) count((a).bgn, (a).en, b) #define SUM(a) accumulate((a).bgn, (a).en, 0) #define REV(a) reverse((a).bgn, (a).en) #define REGS(a, b) regex_search((a), regex(b)) #define REGM(a, b) regex_match((a), regex(b)) #define yn(a) cout << ((a) ? "yes" : "no") << "\n"; #define Yn(a) cout << ((a) ? "Yes" : "No") << "\n"; #define YN(a) cout << ((a) ? "YES" : "NO") << "\n"; #define imp(a) cout << ((a) ? "possible" : "impossible") << "\n"; #define Imp(a) cout << ((a) ? "Possible" : "Impossible") << "\n"; #define IMP(a) cout << ((a) ? "POSSIBLE" : "IMPOSSIBLE") << "\n"; #define fs(a) cout << ((a) ? "second" : "first") << "\n"; #define Fs(a) cout << ((a) ? "Second" : "First") << "\n"; #define FS(a) cout << ((a) ? "SECOND" : "FIRST") << "\n"; #define sak cout << "\n"; #define sas cout << " "; #define sat cout << "\t"; #define dbg(a) cerr << (#a) << ": " << (a) << "\n"; #define dbgs(...) \ dal(#__VA_ARGS__); \ dal(__VA_ARGS__); #define c2l(a) ((ll)(a - 48)) #define a2l(a) ((ll)(a - 97)) #define A2l(a) ((ll)(a - 65)) #define l2c(a) ((char)(a + 48)) #define l2a(a) ((char)(a + 97)) #define l2A(a) ((char)(a + 65)) #define DigN2(a) ((llabs(a) == 0) ? (1) : ((ll)(log2(double(llabs(a)))) + 1)) #define DigN10(a) ((llabs(a) == 0) ? (1) : ((ll)(log10(double(llabs(a)))) + 1)) #define Dig2(a, b) (((a) >> (b)) & 1) #define Dig10(a, b) (ll)(((a) / ((ll)(pow(10.0, (double)(b))))) % 10) #define Pow2(a) ((ll)(1) << (a)) #define Pow10(a) ((ll)(pow(10.0, double(a)))) #define LSB(a) ((a) & (-(a))) #define vin(v) \ ROR((v), (i)) { cin >> (i); }; #define vllin(v, N) \ vll(v)((N)); \ vin(v); #define vllin2(a, b, N) \ vll(a)(N), (b)(N); \ REP(i, N) { cin >> (a)[i] >> (b)[i]; }; #define vsin(v, N) \ vs(v)((N)); \ vin(v); #define rdn(a, b) ((a) / (b)) #define rou(a, b) \ ((((double(a) / double(b)) - ((a) / (b))) < 0.5) ? ((a) / (b)) \ : (((a) / (b)) + 1)) #define rup(a, b) ((((a) % (b)) == 0) ? ((a) / (b)) : (((a) / (b)) + 1)) #define min(...) Min(__VA_ARGS__) #define max(...) Max(__VA_ARGS__) #define emin(a, ...) ((a) = Min((a), __VA_ARGS__)) #define emax(a, ...) ((a) = Max((a), __VA_ARGS__)) #define egcd(a, ...) ((a) = gcd((a), __VA_ARGS__)) #define elcm(a, ...) ((a) = lcm((a), __VA_ARGS__)) #define powll(a, b) (ll)(pow((double)(a), (double)(b))) #define Triangle(x1, y1, x2, y2, x3, y3) \ (((x1) - (x2)) * ((y1) - (y3)) - ((x1) - (x3)) * ((y1) - (y2))) #define svll SumV<ll> #define svvll SumV2<ll> #define li(...) \ ll __VA_ARGS__; \ Input(__VA_ARGS__); #define si(...) \ str __VA_ARGS__; \ Input(__VA_ARGS__); #define vli(size, ...) \ vll __VA_ARGS__; \ vInput(size, __VA_ARGS__); #define vsi(size, ...) \ vs __VA_ARGS__; \ vInput(size, __VA_ARGS__); // const ll MOD = 1e9 + 7; const ll MOD = 998244353; // const ll MOD = 924844033; // const ll MOD = 9007199254740881; const ll INF = 1LL << 60; // 1.15e18 const double PI = acos(-1.0); const vll DX = {0, -1, 0, 1, 0, -1, 1, 1, -1}; const vll DY = {0, 0, -1, 0, 1, -1, -1, 1, 1}; const str alp = "abcdefghijklmnopqrstuvwxyz"; const str ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; void Input() {} template <class Var, class... Args> void Input(Var &var, Args &...args) { cin >> var; Input(args...); } void vInit(ll size) {} template <class T, class... Args> void vInit(ll size, V<T> &v, Args &...args) { v.res(size); vInit(size, args...); } void vInputNum(ll num) {} template <class T, class... Args> void vInputNum(ll num, V<T> &v, Args &...args) { cin >> v[num]; vInputNum(num, args...); } void vInput(ll size) {} template <class... Args> void vInput(ll size, Args &...args) { vInit(size, args...); REP(i, size) vInputNum(i, args...); } template <class S, class T> ostream &operator<<(ostream &out, const P<S, T> &p) { return out << "[" << p.fi << ", " << p.se << "]"; } template <class T> ostream &operator<<(ostream &out, V<T> &v) { if (v.emp) return out << "{}"; else { auto itr = v.bgn; out << "{" << *itr; itr++; while (itr != v.en) { out << ", " << *itr; itr++; } out << "}"; return out; } } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> &m) { if (m.emp) return out << "<[]>"; else { auto itr = m.bgn; out << "< [" << (itr->fi) << ": " << (itr->se); itr++; while (itr != m.en) { out << "], [" << (itr->fi) << ": " << (itr->se); itr++; } out << "] >"; return out; } } template <class T> ostream &operator<<(ostream &out, const set<T> &s) { if (s.emp) return out << "<>"; else { auto itr = s.bgn; out << "<" << *itr; itr++; while (itr != s.en) { out << ", " << *itr; itr++; } out << ">"; return out; } } void say() {} template <class T> void say(T t) { cout << t; } template <class Head, class... Body> void say(Head head, Body... body) { cout << head << " "; say(body...); } void sal() { cout << "\n"; } template <class... Args> void sal(Args... args) { say(args...); cout << "\n"; } void day() {} template <class T> void day(T t) { cerr << t; } template <class Head, class... Body> void day(Head head, Body... body) { cerr << head << " "; day(body...); } void dal() { cerr << "\n"; } template <class... Args> void dal(Args... args) { day(args...); cerr << "\n"; } void salv() {} template <class T> void salv(V<T> v) { if (v.emp) sal(); else { auto itr = v.bgn; say(*itr); itr++; while (itr != v.en) { sas; say(*itr); itr++; } sak; } } template <class T, class... Args> void salv(V<T> v, Args... args) { salv(v); salv(args...); } void salv2() {} template <class T> void salv2(V<V<T>> v) { if (v.emp) sal(); else { ROR(v, i) salv(i); } } template <class T, class... Args> void salv2(V<V<T>> v, Args... args) { salv2(v); salv2(args...); } template <class Monoid> struct Action { public: Monoid I; function<Monoid(Monoid, Monoid)> A; Action(Monoid I, function<Monoid(Monoid, Monoid)> A) : I(I), A(A) {} Monoid operator()() { return I; } Monoid operator()(Monoid x) { return x; } Monoid operator()(Monoid l, Monoid r) { return A(l, r); } template <class... Args> Monoid operator()(Monoid x, Args... args) { Monoid tmp = operator()(args...); return A(x, tmp); } }; template <class T> Action<T> ADD = Action<T>(0, [](T l, T r) { return l + r; }); template <> Action<str> ADD<str> = Action<str>("", [](str l, str r) { return l + r; }); template <class T1, class T2> Action<P<T1, T2>> ADD<P<T1, T2>> = Action<P<T1, T2>>(mp(ADD<T1>.I, ADD<T2>.I), [](P<T1, T2> l, P<T1, T2> r) { return mp(l.fi + r.fi, l.se + r.se); }); template <class T> Action<T> MUL = Action<T>(1, [](T l, T r) { return l * r; }); template <class T> Action<T> OR = Action<T>(0, [](T l, T r) { return l | r; }); template <class T> Action<T> XOR = Action<T>(0, [](T l, T r) { return l ^ r; }); template <class T> Action<T> AND = Action<T>(((ll)(1) << 63) - 1, [](T l, T r) { return l & r; }); template <> Action<bool> AND<bool> = Action<bool>(true, [](bool l, bool r) { return l & r; }); template <> Action<ull> AND<ull> = Action<ull>(((ull)(1) << 63) - 1, [](ull l, ull r) { return l & r; }); template <class T> Action<T> MIN = Action<T>(INF, [](T l, T r) { return (l < r) ? l : r; }); template <class T> Action<T> MAX = Action<T>(-INF, [](T l, T r) { return (l > r) ? l : r; }); template <class T> Action<T> GCD = Action<T>(0, [](T l, T r) { if (l < r) { l ^= r; r ^= l; l ^= r; } return (r ? GCD<T>(r, l % r) : l); }); template <class T> Action<T> LCM = Action<T>(1, [](T l, T r) { return (l * r) / GCD<T>(l, r); }); template <class Head> Head Min(Head head) { return head; } template <class Head, class... Body> Head Min(Head head, Body... body) { auto tmp = Min(body...); return (head < tmp) ? head : tmp; } template <class Head> Head Max(Head head) { return head; } template <class Head, class... Body> auto Max(Head head, Body... body) { auto tmp = Max(body...); return (head > tmp) ? head : tmp; } ll gcd(ll a, ll b) { if (a < b) { a ^= b; b ^= a; a ^= b; } return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll gcd(ll head) { return head; } template <class... Body> ll gcd(ll head, Body... body) { return gcd(head, gcd(body...)); } ll lcm(ll head) { return head; } template <class... Body> ll lcm(ll head, Body... body) { return lcm(head, lcm(body...)); } ll Bset(ll a, ll b, ll c) { if (c) a |= b; else a &= ~b; return a; } struct UFT { public: ll tsize; ll mode; vll par; vll rank; UFT() {} UFT(const UFT &uft) {} UFT(ll tsizeget, ll modeget = 0) { tsize = tsizeget; mode = modeget; par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); } ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return; if (mode) { par[x] += par[y]; par[y] = x; } else { 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]++; } } } ll size(ll x) { return -par[root(x)]; } }; struct pUFT { public: ll tsize; ll now; vll par; vll rank; vll mtime; vvll sizepi; vvll sizepv; pUFT() {} pUFT(const pUFT &puft) {} pUFT(ll tsizeget) { tsize = tsizeget; now = 0; par.asn(tsize, -1); rank.asn(tsize, 0); mtime.asn(tsize, INF); sizepi.asn(tsize, {0}); sizepv.asn(tsize, {1}); } ll root(ll x, ll t) { return (mtime[x] > t) ? x : root(par[x], t); } bool same(ll x, ll y, ll t) { return root(x, t) == root(y, t); } ll merge(ll x, ll y) { now++; x = root(x, now); y = root(y, now); if (x != y) { if (rank[x] < rank[y]) { par[y] += par[x]; sizepi[y].pb(now); sizepv[y].pb(-par[y]); par[x] = y; mtime[x] = now; } else { par[x] += par[y]; sizepi[x].pb(now); sizepv[x].pb(-par[x]); par[y] = x; mtime[y] = now; if (rank[x] == rank[y]) rank[x]++; } } return now; } ll size(ll x, ll t) { x = root(x, t); return sizepv[x][UPB(sizepi[x], t) - 1]; } }; struct wUFT { public: ll tsize; ll mode; vll par; vll rank; vll dweight; wUFT() {} wUFT(const wUFT &wuft) {} wUFT(ll tsizeget, ll modeget = 0) { tsize = tsizeget; mode = modeget; par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); dweight.asn(tsize, 0); } ll root(ll x) { if (par[x] < 0) return x; else { ll r = root(par[x]); dweight[x] += dweight[par[x]]; return par[x] = r; } } ll weight(ll x) { root(x); return dweight[x]; } ll diff(ll x, ll y) { return weight(y) - weight(x); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y, ll w) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return; if (mode) { par[x] += par[y]; par[y] = x; } else { if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = y; dweight[x] = -w; } else { par[x] += par[y]; par[y] = x; if (rank[x] == rank[y]) rank[x]++; dweight[y] = w; } } } ll size(ll x) { return -par[root(x)]; } }; template <typename valtype> class SegT { public: ll size; vector<valtype> v; valtype initv; function<valtype(valtype x, valtype y)> calc; SegT() {} SegT(const SegT &segt) {} SegT(ll sizeget, ll modeget = 0) { sizeset(sizeget); modeset(modeget); init(); } SegT(vector<valtype> cpyvec, ll modeget = 0) { sizeset(cpyvec.sz); modeset(modeget); init(); copy(cpyvec); } SegT(ll sizeget, valtype initvget, function<valtype(valtype x, valtype y)> calcget) { sizeset(sizeget); initv = initvget; calc = calcget; init(); } SegT(vector<valtype> cpyvec, valtype initvget, function<valtype(valtype x, valtype y)> calcget) { sizeset(cpyvec.sz); initv = initvget; calc = calcget; init(); copy(cpyvec); } void sizeset(ll rsize) { size = DigN2(rsize); if (rsize == Pow2(size - 1)) size--; return; } void modeset(ll mode) { switch (mode) { case 0: initv = 0; calc = [](valtype x, valtype y) { return x + y; }; break; case 1: initv = INF; calc = [](valtype x, valtype y) { return min(x, y); }; break; case 2: initv = -INF; calc = [](valtype x, valtype y) { return max(x, y); }; break; } return; } void init() { v.asn(Pow2(size + 1) - 1, initv); } void copy(vector<valtype> cpyvec) { REP(i, min(cpyvec.sz, Pow2(size))) set(i, cpyvec[i]); } ll i2v(ll i) const { if (i < 0 || i >= Pow2(size)) return -1; return Pow2(size) + i - 1; } ll top(ll i) const { if (i == 0) return -1; return (i - 1) / 2; } pl bot(ll i) const { if (i + 1 >= Pow2(size)) return mp(-1, -1); return mp(2 * i + 1, 2 * i + 2); } void set(ll i, valtype x) { i = i2v(i); v[i] = x; while (i > 0) { i = top(i); v[i] = calc(v[bot(i).fi], v[bot(i).se]); } return; } void add(ll i, valtype x) { set(i, v[i2v(i)] + x); return; } valtype operator[](const ll &i) const { return v[i2v(i)]; } // valtype que(ll a = 0, ll b = Pow2(size) - 1) { valtype que(ll a, ll b) { if (a == b) return v[i2v(a)]; if (a > b) return initv; // swap(a, b); valtype ans = initv; ll ai = i2v(a); ll bi = i2v(b); FOR(i, 1, size + 1) { if (a > b) break; if (a % Pow2(i)) { ans = calc(ans, v[ai]); a += Pow2(i - 1); ai = top(ai) + 1; } else { ai = top(ai); } if (a > b) break; if ((b + 1) % Pow2(i)) { ans = calc(ans, v[bi]); b -= Pow2(i - 1); bi = top(bi) - 1; } else { bi = top(bi); } if (a > b) break; } return ans; } valtype que(ll b) { return que(0, b); } valtype que() { return que(0, Pow2(size) - 1); } }; template <typename lentype> class Graph { public: ll size; ll mode; ll mapmode; lentype zlen; lentype ilen; vector<map<ll, ll>> v2n; vvll n2v; vector<vector<lentype>> Edge; vector<pair<lentype, ll>> Primresult; Graph() {} Graph(const Graph &graph) {} Graph(ll sizeget = 0, ll mapmodeget = 0, ll modeget = 0, lentype zlenget = 0, lentype ilenget = INF) { size = sizeget; mapmode = mapmodeget; mode = modeget; zlen = zlenget; ilen = ilenget; init(); } void init() { Edge.res(size); v2n.res(size); n2v.res(size); Primresult.asn(size, mp(ilen, -1)); } lentype lenplus(lentype l, lentype r) { return l + r; } lentype lenequal(lentype l, lentype r) { return l == r; } lentype lenlcr(lentype l, lentype r) { return l < r; } ll addV(ll vs) { size += vs; init(); return size; } void caddE(ll x, ll y, lentype c) { if (mapmode) v2n[x][y] = Edge[x].sz; Edge[x].pb(c); n2v[x].pb(y); } void csetE(ll x, ll y, lentype c) { if (mapmode) Edge[x][v2n[x][y]] = c; } void cersE(ll x, ll y) { if (mapmode) { ll n = v2n[x][y]; Edge[x][n] = ilen; n2v[x][n] = -1; v2n[x].ers(y); } } void addE(ll x, ll y, lentype c) { caddE(x, y, c); if (!mode) caddE(y, x, c); } void setE(ll x, ll y, lentype c) { csetE(x, y, c); if (!mode) csetE(y, x, c); } void ersE(ll x, ll y, lentype c) { cersE(x, y, c); if (!mode) cersE(y, x, c); } lentype getE(ll x, ll y) { if (mapmode) { if (v2n[x].count(y)) { return Edge[x][v2n[x][y]]; } } return ilen; } ll getVsz(ll x) { return Edge[x].sz; } pair<lentype, ll> getV(ll x, ll n) { if (n >= getVsz(x)) return mp(ilen, -1); return mp(Edge[x][n], n2v[x][n]); } vector<pair<lentype, vll>> Dijk(ll x) { vector<pair<lentype, vll>> result(size); REP(i, size) { result[i].fi = ilen; result[i].se = {-1}; } vll stat(size, 0); pair<lentype, ll> now; pair<lentype, ll> nowlv; SegT<pair<lentype, ll>> Q(size, mp(ilen, -1), [](pair<lentype, ll> l, pair<lentype, ll> r) { if (l.se == -1) return r; if (r.se == -1) return l; return l.fi < r.fi ? l : r; }); Q.set(x, mp(zlen, x)); result[x].fi = zlen; result[x].se = {-1}; while (Q.que(0, size - 1).se != -1) { now = Q.que(0, size - 1); Q.set(now.se, mp(ilen, -1)); stat[now.se] = 1; REP(i, getVsz(now.se)) { nowlv = getV(now.se, i); if (stat[nowlv.se]) continue; if (Q[nowlv.se].se == -1) { result[nowlv.se].fi = lenplus(result[now.se].fi, nowlv.fi); result[nowlv.se].se = {now.se}; Q.set(nowlv.se, mp(result[nowlv.se].fi, nowlv.se)); } else { if (lenlcr(lenplus(result[now.se].fi, nowlv.fi), result[nowlv.se].fi)) { result[nowlv.se].fi = lenplus(result[now.se].fi, nowlv.fi); result[nowlv.se].se = {now.se}; Q.set(nowlv.se, mp(result[nowlv.se].fi, nowlv.se)); } else if (lenequal(lenplus(result[now.se].fi, nowlv.fi), result[nowlv.se].fi)) { result[nowlv.se].se.pb(now.se); } } } } return result; } lentype Prim(ll x = 0) { lentype ans = zlen; vll stat(size, 0); pair<lentype, ll> now; pair<lentype, ll> nowlv; SegT<pair<lentype, ll>> Q(size, mp(ilen, -1), [](pair<lentype, ll> l, pair<lentype, ll> r) { if (l.se == -1) return r; if (r.se == -1) return l; return l.fi < r.fi ? l : r; }); Q.set(x, mp(zlen, x)); Primresult[x] = mp(zlen, -1); while (Q.que(0, size - 1).se != -1) { now = Q.que(0, size - 1); Q.set(now.se, mp(ilen, -1)); stat[now.se] = 1; ans = lenplus(ans, Primresult[now.se].fi); REP(i, getVsz(now.se)) { nowlv = getV(now.se, i); if (stat[nowlv.se]) continue; if (Q[nowlv.se].se == -1) { Primresult[nowlv.se] = mp(nowlv.fi, now.se); Q.set(nowlv.se, mp(Primresult[nowlv.se].fi, nowlv.se)); } else { if (lenlcr(nowlv.fi, Primresult[nowlv.se].fi)) { Primresult[nowlv.se] = mp(nowlv.fi, now.se); Q.set(nowlv.se, mp(Primresult[nowlv.se].fi, nowlv.se)); } } } } return ans; } }; /*template <class Type> class DP { public: vector<Type> v; Type initv; vll size, block; DP() {} DP(const DP &dp) {} template<class... Args> DP(Args... args) { block.asn(1, 1); Initialize(args...); } void Initialize(Type initv_) { initv = initv_; v.asn(block.bk, initv); } template<class... Args> void Initialize(ll val, Args... args) { size.pb(val); block.pb(block.bk*val); Initialize(args...); } };*/ pl Bezout(ll a, ll b) { if (b != 0) { pl xy; xy = Bezout(b, a % b); return mp(xy.se, xy.fi - ((a / b) * xy.se)); } else { return mp(1, 0); } } pl Bez(ll a, ll b, ll c) { pl xy; ll x, y, z, gc; xy = Bezout(a, b); gc = gcd(a, b); if (c % gc != 0) return mp(-1, -1); x = xy.fi * (c / gc); y = xy.se * (c / gc); if (x < 0) z = rup(-x, (b / gc)); if (x >= 0) z = -x / (b / gc); x += z * (b / gc); y -= z * (a / gc); return mp(x, y); } ll DigS10(ll n) { ll ans = 0; while (1) { ans += n % 10; n /= 10; if (!n) break; } return ans; } ll isP(ll n) { if (n <= 1) return 0; FOR(i, 2, (ll)sqrt(n) + 1) { if (n % i == 0) return 0; } return 1; } ll Tot(ll n) { if (n <= 0) return 0; ll ans = n, x = 2; while (x * x <= n) { if (n % x == 0) { ans -= ans / x; while (n % x == 0) n /= x; } x++; } if (n > 1) ans -= ans / n; return ans; } template <class T> struct Sum { public: V<T> v, s; ll size; Action<T> Add; Sum(V<T> v, Action<T> Add = ADD<T>) : v(v), size(v.sz), Add(Add) { Init(); Calc(); } void Init() { s.asn(size + 1, Add.I); } void Calc() { REP(i, size) s[i + 1] = Add(s[i], v[i]); } T operator()(ll x) { return operator()(0, x); } T operator()(ll l, ll r) { if (l < 0) l = 0; if (r >= size) r = size - 1; if (l > r) return Add.I; return s[r + 1] - s[l]; // for ADD } }; using sumll = Sum<ll>; template <class T> struct Sum2 { public: V<V<T>> v, s; ll RowSize, ColumnSize; Action<T> Add; Sum2(V<V<T>> v, Action<T> Add = ADD<T>) : v(v), RowSize(v.sz), ColumnSize(v.sz ? v[0].sz : 0), Add(Add) { Init(); Calc(); } void Init() { s.asn(RowSize + 1, V<T>(ColumnSize, Add.I)); } void Calc() { REP1(r, RowSize) { REP1(c, ColumnSize) { s[r][c] = v[r - 1][c - 1] + operator()(r, c - 1) + operator()(r - 1, c) - operator()(r - 1, c - 1); } } } T operator()(ll r, ll c) { return operator()(0, 0, r, c); } T operator()(ll r1, ll c1, ll r2, ll c2) { if (r1 < 0) r1 = 0; if (c1 < 0) c1 = 0; if (r2 >= RowSize) r2 = RowSize - 1; if (c2 >= ColumnSize) c2 = ColumnSize - 1; if (r1 > r2) return Add.I; if (c1 > c2) return Add.I; return s[r2 + 1][c2 + 1] - s[r2 + 1][c1] - s[r1][c2 + 1] + s[r1][c1]; } }; using sum2ll = Sum2<ll>; template <class T> struct Point2 { public: V<V<T>> v; ll h, w; Point2() : h(0), w(0) {} Point2(ll h, ll w) : h(h), w(w) { asn(h, w); } Point2(ll h, ll w, T val) : h(h), w(w) { asn(h, w, val); } void assign(ll h, ll w) { v.asn(h, V<T>(w)); } void assign(ll h, ll w, ll val) { v.asn(h, V<T>(w, val)); } T &operator()(ll h, ll w) { return v[h][w]; } T &operator()(pl p) { return v[p.fi][p.se]; } }; template <class T> using P2 = Point2<T>; template <ll Mod> struct Modll { public: ll v; Modll() : v(0) {} Modll(ll _v) { set(_v % Mod + Mod); } Modll &set(ll _v) { v = (_v < Mod) ? _v : (_v - Mod); return *this; } Modll pow(ll n) const { Modll x = *this, ans = 1; while (n) { if (n & 1) ans *= x; x *= x; n >>= 1; } return ans; } Modll inverse() const { return (*this).pow(Mod - 2); } Modll operator+(const Modll &m) const { return Modll().set(v + m.v); } Modll operator-(const Modll &m) const { return Modll().set(Mod + v - m.v); } Modll operator*(const Modll &m) const { return Modll().set((ull(v) * m.v) % Mod); } Modll operator/(const Modll &m) const { return *this * m.inv; } Modll &operator+=(const Modll &m) { return *this = *this + m; } Modll &operator-=(const Modll &m) { return *this = *this - m; } Modll &operator*=(const Modll &m) { return *this = *this * m; } Modll &operator/=(const Modll &m) { return *this = *this / m; } Modll operator-() const { return Modll(0) - *this; } explicit operator bool() const { return v != 0; } friend ostream &operator<<(ostream &out, const Modll &m) { return out << m.v; } }; using mll = Modll<MOD>; using vmll = V<mll>; using vvmll = V<vmll>; using vvvmll = V<vvmll>; template <class T> T vmin(V<T> v) { T tmp = MIN<T>.I; ROR(v, i) emin(tmp, i); return tmp; } template <class T, class... Args> T vmin(V<T> v, Args... args) { T tmp = vmin(args...); return min(vmin(v), tmp); } template <class T> T vmax(V<T> v) { T tmp = MAX<T>.I; ROR(v, i) emax(tmp, i); return tmp; } template <class T, class... Args> T vmax(V<T> v, Args... args) { T tmp = vmax(args...); return max(vmax(v), tmp); } template <class T> T vgcd(V<T> v) { T tmp = GCD<T>.I; ROR(v, i) egcd(tmp, i); return tmp; } template <class T, class... Args> T vgcd(V<T> v, Args... args) { T tmp = vgcd(args...); return gcd(vgcd(v), tmp); } template <class T> T vlcm(V<T> v) { T tmp = LCM<T>.I; ROR(v, i) elcm(tmp, i); return tmp; } template <class T, class... Args> T vlcm(V<T> v, Args... args) { T tmp = vlcm(args...); return lcm(vlcm(v), tmp); } vmll MFactMemo(2, 1); vmll MIFactMemo(2, 1); mll mFact(ll n) { if (MFactMemo.sz <= n) { ll oldsize = MFactMemo.sz; MFactMemo.res(n + 1, 1); FOR(i, oldsize, n) MFactMemo[i] = MFactMemo[i - 1] * i; } return MFactMemo[n]; } mll miFact(ll n) { if (MIFactMemo.sz <= n) { ll oldsize = MIFactMemo.sz; MIFactMemo.res(n + 1, 1); MIFactMemo.bk = mFact(n).inv; rFOR(i, oldsize + 1, n) MIFactMemo[i - 1] = MIFactMemo[i] * i; } return MIFactMemo[n]; } mll mComb(ll n, ll k) { if (n < 0 || k < 0 || n < k) return 0; return mFact(n) * miFact(k) * miFact(n - k); } ll LIS(vll v, ll m = 0) { if (v.sz > 0) { ll ans = 0; vll dp(v.sz, INF); FOR(i, 0, v.sz - 1) { dp[m ? UPB(dp, v[i]) : LOWB(dp, v[i])] = v[i]; } FOR(i, 0, v.sz - 1) { if (dp[i] == INF) break; ans++; } return ans; } else { return 0; } } void PCmprs(vll &v) { if (v.sz == 0) return; vll vv(v); IOTA(vv, 0); sort(vv.bgn, vv.en, [&](ll v1, ll v2) { return v[v1] < v[v2]; }); IOTA(v, 0); sort(v.bgn, v.en, [&](ll v1, ll v2) { return vv[v1] < vv[v2]; }); return; } ll BblCnt(vll v) { PCmprs(v); SegT<ll> b(v.sz, 0); ll ans = 0; REP(i, v.sz) { ans += (i - b.que(0, v[i])); b.add(v[i], 1); } return ans; } ll Bsrch(function<bool(ll x)> f, ll mi, ll ma) { ll ng = mi - 1, ok = ma, mid; while (ok - ng > 1) { mid = (ng + ok) / 2; (f(mid) ? ok : ng) = mid; } return ok; } template <class T> V<V<T>> MultiMatrix(V<V<T>> A, V<V<T>> B, Action<T> Mul = MUL<T>, Action<T> Add = ADD<T>) { V<V<T>> ans; ll ii = A.sz; if (!ii) return ans; ll jj = A[0].sz; if (!jj) return ans; ll jj2 = B.sz; if (!jj2) return ans; if (jj != jj2) return ans; ll kk = B[0].sz; if (!kk) return ans; ans.asn(ii, V<T>(kk, 0)); REP(i, ii) { REP(k, kk) { REP(j, jj) { ans[i][k] = Add(ans[i][k], Mul(A[i][j], B[j][k])); } } } return ans; } vvll CombMemo(1000, vll(1000, -1)); ll Comb(ll n, ll k) { if ((n < 0) || (k < 0)) return 0; if (CombMemo[n][k] == -1) { if (n < k) CombMemo[n][k] = 0; else { if (n == 0) CombMemo[n][k] = 1; else if (k == 0) CombMemo[n][k] = 1; else if (n == k) CombMemo[n][k] = 1; else CombMemo[n][k] = Comb(n - 1, k - 1) + Comb(n - 1, k); } } return CombMemo[n][k]; } template <class T> map<T, ll> Dict(V<T> v) { map<T, ll> m; if (!v.sz) return m; SORT(v); UNIQUE(v); REP(i, v.sz) { m[v[i]] = i; } return m; } template <class T> vll Cmprs(V<T> v) { auto m = Dict(v); vll ans(v.sz); REP(i, v.sz) { ans[i] = m[v[i]]; } return ans; } template <class T> auto vecn(T val) { return val; } template <class... Args> auto vecn(ll val, Args... args) { auto tmp = vecn(args...); return V<decltype(tmp)>(val, tmp); } void Solve(); signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20) << fixed; Solve(); } void Solve() { li(n, k); vli(n, v); ll mx = 0; vll l(n + 10), r(n + 10); l[0] = 0; r[0] = 0; REP1(i, n) { l[i] = l[i - 1] + v[i - 1]; r[i] = r[i - 1] + v[n - i]; } vvll hs(n + 10, vll(n + 10, 0)), ms(n + 10, vll(n + 10, 0)); vector<ll> hhs, mms; REP1(a, n) { hhs.pb(v[a - 1]); mms.pb(v[n - a]); SORT(hhs); SORT(mms); REP(b, a) { if (b) { hs[a][b] = hs[a][b - 1]; if (hhs[b - 1] < 0) { hs[a][b] += hhs[b - 1]; } ms[a][b] = ms[a][b - 1]; if (mms[b - 1] < 0) { ms[a][b] += mms[b - 1]; } } } } REP(i, min(k, n) + 1) { REP(j, min(k, n) - i + 1) { REP(h, i + 1) { REP(m, j + 1) { if (i + j + h + m <= k) mx = max(mx, l[i] + r[j] - hs[i][h] - ms[j][m]); } } } } cout << mx; }
replace
1,294
1,299
1,294
1,300
0
p03032
C++
Time Limit Exceeded
#include <algorithm> #include <chrono> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <random> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <time.h> #include <unordered_map> #include <utility> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (int i = a; i < b; i++) #define ll long long #define INF 1000000001 #define mod 1000000007 #define SORT(s) sort(s.begin(), s.end()); #define reverse(s) reverse(s.begin(), s.end()); #define P pair<int, int> #define mp make_pair #define valid(x, y, h, w) (0 <= x && x < h && 0 <= y && y < w) int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; using namespace std; int POW(int x, int y) { return int(pow(double(x), double(y))); } double log(double a, double b) { return log(b) / log(a); } int main() { int n, k; cin >> n >> k; vector<int> v(n); rep(i, n) { cin >> v[i]; } int ans = 0; // i 取り出す回数 k-i回戻す rep(i, k + 1) { if (n < i) { i = n; } rep(j, i + 1) { int score = 0; vector<int> boo; rep(k, j) { score += v[k]; if (v[k] < 0) { boo.push_back(v[k]); } } rep(k, i - j) { score += v[n - 1 - k]; if (v[n - 1 - k] < 0) { boo.push_back(v[n - 1 - k]); } } int back = k - i; SORT(boo); rep(z, back) { if (z == boo.size()) { break; } score -= boo[z]; } ans = max(ans, score); } } cout << ans << endl; }
#include <algorithm> #include <chrono> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <random> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <time.h> #include <unordered_map> #include <utility> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (int i = a; i < b; i++) #define ll long long #define INF 1000000001 #define mod 1000000007 #define SORT(s) sort(s.begin(), s.end()); #define reverse(s) reverse(s.begin(), s.end()); #define P pair<int, int> #define mp make_pair #define valid(x, y, h, w) (0 <= x && x < h && 0 <= y && y < w) int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; using namespace std; int POW(int x, int y) { return int(pow(double(x), double(y))); } double log(double a, double b) { return log(b) / log(a); } int main() { int n, k; cin >> n >> k; vector<int> v(n); rep(i, n) { cin >> v[i]; } int ans = 0; // i 取り出す回数 k-i回戻す rep(i0, k + 1) { int i = i0; if (n < i) { i = n; } rep(j, i + 1) { int score = 0; vector<int> boo; rep(k, j) { score += v[k]; if (v[k] < 0) { boo.push_back(v[k]); } } rep(k, i - j) { score += v[n - 1 - k]; if (v[n - 1 - k] < 0) { boo.push_back(v[n - 1 - k]); } } int back = k - i; SORT(boo); rep(z, back) { if (z == boo.size()) { break; } score -= boo[z]; } ans = max(ans, score); } } cout << ans << endl; }
replace
47
48
47
49
TLE
p03032
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (ll(i) = (0); (i) < (n); ++i) #define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i) #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second #define SHOW1d(v, n) \ { \ REP(WW, n) cerr << v[WW] << ' '; \ cerr << endl << endl; \ } #define SHOW2d(v, WW, HH) \ { \ REP(W_, WW) { \ REP(H_, HH) cerr << v[W_][H_] << ' '; \ cerr << endl; \ } \ cerr << endl; \ } #define ALL(v) v.begin(), v.end() #define Decimal fixed << setprecision(20) #define INF 1000000000 #define LLINF 1000000000000000000LL #define MOD 998244353 typedef long long ll; typedef pair<ll, ll> P; vector<ll> v; ll dp[55][55][111]; ll n, k; ll dfs(ll l, ll r, ll cou) { if (l > l) return 0; if (cou > k) return -LLINF; if (dp[l][r][cou] != -LLINF) return dp[l][r][cou]; ll ret = 0; ret = max(ret, dfs(l + 1, r, cou + 1) + v[l]); ret = max(ret, dfs(l + 1, r, cou + 2)); ret = max(ret, dfs(l, r - 1, cou + 1) + v[r]); ret = max(ret, dfs(l, r - 1, cou + 2)); return dp[l][r][cou] = ret; } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); REP(i, 55) REP(j, 55) REP(k, 111) dp[i][j][k] = -LLINF; cin >> n >> k; REP(i, n) { int tmp; cin >> tmp; v.PB(tmp); } cout << dfs(0, n - 1, 0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (ll(i) = (0); (i) < (n); ++i) #define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i) #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second #define SHOW1d(v, n) \ { \ REP(WW, n) cerr << v[WW] << ' '; \ cerr << endl << endl; \ } #define SHOW2d(v, WW, HH) \ { \ REP(W_, WW) { \ REP(H_, HH) cerr << v[W_][H_] << ' '; \ cerr << endl; \ } \ cerr << endl; \ } #define ALL(v) v.begin(), v.end() #define Decimal fixed << setprecision(20) #define INF 1000000000 #define LLINF 1000000000000000000LL #define MOD 998244353 typedef long long ll; typedef pair<ll, ll> P; vector<ll> v; ll dp[55][55][111]; ll n, k; ll dfs(ll l, ll r, ll cou) { if (l > r || cou == k) return 0; if (cou > k) return -LLINF; if (dp[l][r][cou] != -LLINF) return dp[l][r][cou]; ll ret = 0; ret = max(ret, dfs(l + 1, r, cou + 1) + v[l]); ret = max(ret, dfs(l + 1, r, cou + 2)); ret = max(ret, dfs(l, r - 1, cou + 1) + v[r]); ret = max(ret, dfs(l, r - 1, cou + 2)); return dp[l][r][cou] = ret; } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); REP(i, 55) REP(j, 55) REP(k, 111) dp[i][j][k] = -LLINF; cin >> n >> k; REP(i, n) { int tmp; cin >> tmp; v.PB(tmp); } cout << dfs(0, n - 1, 0) << endl; return 0; }
replace
39
40
39
40
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0}; const int INF = 1e9 + 101; const long long LINF = 1e18; const double EPS = 1e-8; #define pb push_back #define mk make_pair #define fr first #define sc second #define reps(i, j, k) for (ll i = (j); i < (k); ++i) #define rep(i, j) reps(i, 0, j) #define all(a) (a).begin(), (a).end() #define MOD 1000000007 typedef long long ll; typedef pair<ll, ll> Pii; typedef pair<Pii, int> P; typedef vector<int> vi; typedef vector<ll> vll; template <class T> ostream &operator<<(ostream &out, const vector<T> &v) { out << "{"; rep(i, v.size()) { out << v[i] << ", "; } return out << "}" << endl; } int N, K; vector<int> v, rv; int memo[52][52][52]; int solve(int depth, int r_depth, int cnt) { if (cnt == K) { return 0; } if (cnt > K) { return -INF; } if (memo[depth][r_depth][cnt] != -INF) { return memo[depth][r_depth][cnt]; } int ret = -INF; ret = solve(depth, r_depth, K); if (v[depth] < 0) { ret = max(ret, max(solve(depth + 1, r_depth, cnt + 2), solve(depth + 1, r_depth, cnt + 1) + v[depth])); if (cnt + 2 <= K) ret = max(ret, solve(depth + 1, r_depth, K)); } else { ret = max(ret, solve(depth + 1, r_depth, cnt + 1) + v[depth]); ret = max(ret, solve(depth + 1, r_depth, K) + v[depth]); } if (rv[r_depth] < 0) { ret = max(ret, max(solve(depth, r_depth + 1, cnt + 2), solve(depth, r_depth + 1, cnt + 1) + rv[r_depth])); if (cnt + 2 <= K) ret = max(ret, solve(depth, r_depth + 1, K)); } else { ret = max(ret, solve(depth, r_depth + 1, cnt + 1) + rv[r_depth]); ret = max(ret, solve(depth, r_depth + 1, K) + rv[r_depth]); } return memo[depth][r_depth][cnt] = ret; } int main() { for (int i = 0; i < 52; ++i) { for (int j = 0; j < 52; ++j) { for (int k = 0; k < 52; ++k) { memo[i][j][k] = -INF; } } } cin >> N >> K; rep(i, N) { int a; cin >> a; v.pb(a); } rep(i, N) { rv.pb(v[N - i - 1]); } int ans = solve(0, 0, 0); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0}; const int INF = 1e9 + 101; const long long LINF = 1e18; const double EPS = 1e-8; #define pb push_back #define mk make_pair #define fr first #define sc second #define reps(i, j, k) for (ll i = (j); i < (k); ++i) #define rep(i, j) reps(i, 0, j) #define all(a) (a).begin(), (a).end() #define MOD 1000000007 typedef long long ll; typedef pair<ll, ll> Pii; typedef pair<Pii, int> P; typedef vector<int> vi; typedef vector<ll> vll; template <class T> ostream &operator<<(ostream &out, const vector<T> &v) { out << "{"; rep(i, v.size()) { out << v[i] << ", "; } return out << "}" << endl; } int N, K; vector<int> v, rv; int memo[52][52][52]; int solve(int depth, int r_depth, int cnt) { if (cnt == K || depth == N || r_depth == N || depth + r_depth == N) { return 0; } if (cnt > K) { return -INF; } if (memo[depth][r_depth][cnt] != -INF) { return memo[depth][r_depth][cnt]; } int ret = -INF; ret = solve(depth, r_depth, K); if (v[depth] < 0) { ret = max(ret, max(solve(depth + 1, r_depth, cnt + 2), solve(depth + 1, r_depth, cnt + 1) + v[depth])); if (cnt + 2 <= K) ret = max(ret, solve(depth + 1, r_depth, K)); } else { ret = max(ret, solve(depth + 1, r_depth, cnt + 1) + v[depth]); ret = max(ret, solve(depth + 1, r_depth, K) + v[depth]); } if (rv[r_depth] < 0) { ret = max(ret, max(solve(depth, r_depth + 1, cnt + 2), solve(depth, r_depth + 1, cnt + 1) + rv[r_depth])); if (cnt + 2 <= K) ret = max(ret, solve(depth, r_depth + 1, K)); } else { ret = max(ret, solve(depth, r_depth + 1, cnt + 1) + rv[r_depth]); ret = max(ret, solve(depth, r_depth + 1, K) + rv[r_depth]); } return memo[depth][r_depth][cnt] = ret; } int main() { for (int i = 0; i < 52; ++i) { for (int j = 0; j < 52; ++j) { for (int k = 0; k < 52; ++k) { memo[i][j][k] = -INF; } } } cin >> N >> K; rep(i, N) { int a; cin >> a; v.pb(a); } rep(i, N) { rv.pb(v[N - i - 1]); } int ans = solve(0, 0, 0); cout << ans << endl; return 0; }
replace
30
31
30
31
0
p03032
C++
Runtime Error
#ifdef LOCAL #define _GLIBCXX_DEBUG #define __clock__ #else #pragma GCC optimize("Ofast") #endif #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using VI = vector<ll>; using VV = vector<VI>; using VS = vector<string>; using PII = pair<ll, ll>; // tourist set template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << '\n'; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif // tourist set end 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; } #define FOR(i, a, b) for (ll i = (a); i < (b); ++i) #define rep(i, b) FOR(i, 0, b) #define ALL(v) (v).begin(), (v).end() #define p(s) cout << (s) << '\n' #define p2(s, t) cout << (s) << " " << (t) << '\n' #define br() p("") #define pn(s) cout << (#s) << " " << (s) << '\n' #define SZ(x) ((int)(x).size()) #define SORT(A) sort(ALL(A)) #define RSORT(A) sort(ALL(A), greater<ll>()) #define MP make_pair #define p_yes() p("Yes") #define p_no() p("No") ll SUM(VI &V) { return accumulate(ALL(V), 0LL); } ll MIN(VI &V) { return *min_element(ALL(V)); } ll MAX(VI &V) { return *max_element(ALL(V)); } void print_vector(VI &V) { ll n = V.size(); rep(i, n) { if (i) cout << ' '; cout << V[i]; } cout << endl; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } // long double using ld = long double; #define EPS (1e-14) #define equals(a, b) (fabs((a) - (b)) < EPS) void no() { p_no(); exit(0); } void yes() { p_yes(); exit(0); } const ll mod = 1e9 + 7; const ll inf = 1e18; const double PI = acos(-1); int main() { cin.tie(0); ios::sync_with_stdio(false); // input ll N, K; cin >> N >> K; deque<ll> q; rep(i, N) { ll a; cin >> a; q.push_back(a); } ll ma = 0; // 操作回数 rep(i, K + 1) { // 左から取る回数 rep(l, K + 1) { // 右から取る回数 rep(r, i - l + 1) { // 捨てる回数 ll rest = i - l - r; if (rest > l + r) continue; auto q2 = q; VI A; rep(j, l) { A.push_back(q2.front()); q2.pop_front(); } rep(j, r) { A.push_back(q2.back()); q2.pop_back(); } SORT(A); ll sum = 0; FOR(j, rest, A.size()) { sum += A[j]; } chmax(ma, sum); } } } p(ma); return 0; }
#ifdef LOCAL #define _GLIBCXX_DEBUG #define __clock__ #else #pragma GCC optimize("Ofast") #endif #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using VI = vector<ll>; using VV = vector<VI>; using VS = vector<string>; using PII = pair<ll, ll>; // tourist set template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << '\n'; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif // tourist set end 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; } #define FOR(i, a, b) for (ll i = (a); i < (b); ++i) #define rep(i, b) FOR(i, 0, b) #define ALL(v) (v).begin(), (v).end() #define p(s) cout << (s) << '\n' #define p2(s, t) cout << (s) << " " << (t) << '\n' #define br() p("") #define pn(s) cout << (#s) << " " << (s) << '\n' #define SZ(x) ((int)(x).size()) #define SORT(A) sort(ALL(A)) #define RSORT(A) sort(ALL(A), greater<ll>()) #define MP make_pair #define p_yes() p("Yes") #define p_no() p("No") ll SUM(VI &V) { return accumulate(ALL(V), 0LL); } ll MIN(VI &V) { return *min_element(ALL(V)); } ll MAX(VI &V) { return *max_element(ALL(V)); } void print_vector(VI &V) { ll n = V.size(); rep(i, n) { if (i) cout << ' '; cout << V[i]; } cout << endl; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } // long double using ld = long double; #define EPS (1e-14) #define equals(a, b) (fabs((a) - (b)) < EPS) void no() { p_no(); exit(0); } void yes() { p_yes(); exit(0); } const ll mod = 1e9 + 7; const ll inf = 1e18; const double PI = acos(-1); int main() { cin.tie(0); ios::sync_with_stdio(false); // input ll N, K; cin >> N >> K; deque<ll> q; rep(i, N) { ll a; cin >> a; q.push_back(a); } ll ma = 0; // 操作回数 rep(i, K + 1) { // 左から取る回数 rep(l, K + 1) { // 右から取る回数 rep(r, i - l + 1) { if (l + r > N) continue; // 捨てる回数 ll rest = i - l - r; if (rest > l + r) continue; auto q2 = q; VI A; rep(j, l) { A.push_back(q2.front()); q2.pop_front(); } rep(j, r) { A.push_back(q2.back()); q2.pop_back(); } SORT(A); ll sum = 0; FOR(j, rest, A.size()) { sum += A[j]; } chmax(ma, sum); } } } p(ma); return 0; }
insert
193
193
193
195
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v.at(i); } int maxvalue = 0; for (int take = k / 2; take <= k; take++) { for (int left = 0; left <= take; left++) { vector<int> jewels(take); for (int i = 0; i < left; i++) { jewels.at(i) = v.at(i); } for (int i = left; i < take; i++) { jewels.at(i) = v.at(i + n - take); } sort(jewels.begin(), jewels.end()); for (int i = 0; i < min(take, k - take); i++) { jewels.at(i) = max(0, jewels.at(i)); } int value = 0; for (int i = 0; i < take; i++) { value += jewels.at(i); } maxvalue = max(value, maxvalue); } } cout << maxvalue << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v.at(i); } int maxvalue = 0; for (int take = min(n, k / 2); take <= min(n, k); take++) { for (int left = 0; left <= take; left++) { vector<int> jewels(take); for (int i = 0; i < left; i++) { jewels.at(i) = v.at(i); } for (int i = left; i < take; i++) { jewels.at(i) = v.at(i + n - take); } sort(jewels.begin(), jewels.end()); for (int i = 0; i < min(take, k - take); i++) { jewels.at(i) = max(0, jewels.at(i)); } int value = 0; for (int i = 0; i < take; i++) { value += jewels.at(i); } maxvalue = max(value, maxvalue); } } cout << maxvalue << endl; }
replace
11
12
11
12
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) #define rrep(i, n) for (int i = n - 1; i >= 0; --i) #define fi first #define se second using namespace std; using lint = long long; using uint = unsigned int; using ulint = unsigned long long; using ldouble = long double; using pii = pair<int, int>; using pli = pair<lint, lint>; using pdd = pair<double, double>; using pld = pair<ldouble, ldouble>; using v1i = vector<int>; using v1li = vector<lint>; using v2i = vector<vector<int>>; using v2li = vector<vector<lint>>; using v3i = vector<vector<vector<int>>>; using v3li = vector<vector<vector<lint>>>; using v1b = vector<bool>; using v2b = vector<vector<bool>>; using v3b = vector<vector<vector<bool>>>; using v1c = vector<char>; using v2c = vector<vector<char>>; using v3c = vector<vector<vector<char>>>; constexpr lint mod1 = 1e9 + 7; constexpr lint mod2 = 998244353; int main() { int n, k, m = 0; cin >> n >> k; deque<int> d; rep(i, n) { int e; cin >> e; d.push_back(e); } rep(i, k + 1) { rep(j, k - i + 1) { int t = 0, a = 0, b = 0; deque<int> c = d; priority_queue<int, vector<int>, greater<int>> pq; while (a < i && !c.empty()) { pq.push(c.front()); c.pop_front(); ++a; } while (b < j && !c.empty()) { pq.push(c.back()); c.pop_back(); ++b; } rep(l, k - a - b) { if (!pq.empty()) break; if (pq.top() < 0) pq.pop(); else break; } while (!pq.empty()) { t += pq.top(); pq.pop(); } m = max(m, t); } } cout << m << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) #define rrep(i, n) for (int i = n - 1; i >= 0; --i) #define fi first #define se second using namespace std; using lint = long long; using uint = unsigned int; using ulint = unsigned long long; using ldouble = long double; using pii = pair<int, int>; using pli = pair<lint, lint>; using pdd = pair<double, double>; using pld = pair<ldouble, ldouble>; using v1i = vector<int>; using v1li = vector<lint>; using v2i = vector<vector<int>>; using v2li = vector<vector<lint>>; using v3i = vector<vector<vector<int>>>; using v3li = vector<vector<vector<lint>>>; using v1b = vector<bool>; using v2b = vector<vector<bool>>; using v3b = vector<vector<vector<bool>>>; using v1c = vector<char>; using v2c = vector<vector<char>>; using v3c = vector<vector<vector<char>>>; constexpr lint mod1 = 1e9 + 7; constexpr lint mod2 = 998244353; int main() { int n, k, m = 0; cin >> n >> k; deque<int> d; rep(i, n) { int e; cin >> e; d.push_back(e); } rep(i, k + 1) { rep(j, k - i + 1) { int t = 0, a = 0, b = 0; deque<int> c = d; priority_queue<int, vector<int>, greater<int>> pq; while (a < i && !c.empty()) { pq.push(c.front()); c.pop_front(); ++a; } while (b < j && !c.empty()) { pq.push(c.back()); c.pop_back(); ++b; } rep(l, k - a - b) { if (pq.empty()) break; if (pq.top() < 0) pq.pop(); else break; } while (!pq.empty()) { t += pq.top(); pq.pop(); } m = max(m, t); } } cout << m << endl; return 0; }
replace
54
55
54
55
-11
p03032
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define sorted(vector) sort(vector.begin(), vector.end()) #define vmax(vector) *max_element(vector.begin(), vector.end()) #define vsum(vector) accumulate(vector.begin(), vector.end(), 0) #define append(a) push_back(a) #define int(a) stoi(a) #define str(a) to_string(a) int main(void) { int n, k, tmp; vector<int> v, motimono, ans; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> tmp; v.push_back(tmp); } if (k >= n) { k -= n; ans.append(vsum(v)); sorted(v); for (int i = 0; i < k; i++) { for (int j = 0; j < min<unsigned long>(k, v.size()); i++) { v[j] = 0; ans.append(vsum(v)); } } } else { for (int i = 0; i < n; i++) { v.append(v[i]); } for (int j = 1; j < k + 1; j++) { // 長さ for (int i = n - j; i < n + 1; i++) { // 開始index vector<int> motimono; for (int r = i; r < i + j; r++) { motimono.append(v[r]); } ans.append(vsum(motimono)); sorted(motimono); for (int i = 0; i < min<unsigned long>(k - j, motimono.size()); i++) { motimono[i] = 0; ans.append(vsum(motimono)); } } } } cout << vmax(ans) << endl; }
#include <bits/stdc++.h> using namespace std; #define sorted(vector) sort(vector.begin(), vector.end()) #define vmax(vector) *max_element(vector.begin(), vector.end()) #define vsum(vector) accumulate(vector.begin(), vector.end(), 0) #define append(a) push_back(a) #define int(a) stoi(a) #define str(a) to_string(a) int main(void) { int n, k, tmp; vector<int> v, motimono, ans; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> tmp; v.push_back(tmp); } if (k >= n) { k -= n; ans.append(vsum(v)); sorted(v); for (int i = 0; i < min<unsigned long>(k, n); i++) { v[i] = 0; ans.append(vsum(v)); } } else { for (int i = 0; i < n; i++) { v.append(v[i]); } for (int j = 1; j < k + 1; j++) { // 長さ for (int i = n - j; i < n + 1; i++) { // 開始index vector<int> motimono; for (int r = i; r < i + j; r++) { motimono.append(v[r]); } ans.append(vsum(motimono)); sorted(motimono); for (int i = 0; i < min<unsigned long>(k - j, motimono.size()); i++) { motimono[i] = 0; ans.append(vsum(motimono)); } } } } cout << vmax(ans) << endl; }
replace
22
27
22
25
TLE
p03032
Python
Time Limit Exceeded
import heapq N, K = [int(_) for _ in input().split()] V = [int(_) for _ in input().split()] ans = 0 for il in range(min([N + 1, K + 1])): for ir in range(min([N + 1 - il, K + 1 - il])): for dl in range(il + 1): for dr in range(ir + 1): if il + ir + dl + dr > K: continue HL = V[:il] HR = V[N - ir : N] heapq.heapify(HL) heapq.heapify(HR) value = 0 for _ in range(dl): v = heapq.heappop(HL) if v >= 0: value += v break value += sum(HL) for _ in range(dr): v = heapq.heappop(HR) if v >= 0: value += v break value += sum(HR) ans = max([ans, value]) print(ans)
import heapq N, K = [int(_) for _ in input().split()] V = [int(_) for _ in input().split()] ans = 0 for il in range(K + 1): for ir in range(K + 1 - il): if il + ir > min([N, K]): continue H = V[:il] + V[N - ir : N] heapq.heapify(H) now = 0 for d in range(1, il + ir): if il + ir + d > K: break v = heapq.heappop(H) if v >= 0: now += v break now += sum(H) ans = max([ans, now]) print(ans)
replace
5
29
5
21
TLE
p03032
Python
Runtime Error
N, K = map(int, input().split()) d = list(map(int, input().split())) left = [0] left_m = [] right = [0] right_m = [] for i, l, r in zip(range(K), d, d[::-1]): left.append(left[i] + l) left_m.append(l if l < 0 else 0) right.append(right[i] + r) right_m.append(r if r < 0 else 0) point = 0 for i in range(K): for j in range(K - i): p = K - i - j minus = list(sorted(left_m[:i] + right_m[:j])) point = max(point, left[i] + right[j] - sum(minus[:p])) print(point)
N, K = map(int, input().split()) d = list(map(int, input().split())) left = [0] left_m = [] right = [0] right_m = [] for i, l, r in zip(range(K), d, d[::-1]): left.append(left[i] + l) left_m.append(l if l < 0 else 0) right.append(right[i] + r) right_m.append(r if r < 0 else 0) point = 0 m = min(N, K) for i in range(m + 1): for j in range(m - i + 1): p = K - i - j minus = list(sorted(left_m[:i] + right_m[:j])) point = max(point, left[i] + right[j] - sum(minus[:p])) print(point)
replace
12
14
12
15
0
p03032
Python
Runtime Error
def solve(string): n, k, *v = map(int, string.split()) def dfs(deq, get, count, minus): if count == k: return sum(get) op1 = dfs(deq[1:], get + [deq[0]], count + 1, minus + (deq[0] < 0)) op2 = dfs(deq[:-1], get + [deq[-1]], count + 1, minus + (deq[-1] < 0)) m = min(get) if len(get) > 0 else 0 op3 = 0 if m >= 0 else dfs(deq[:], sorted(get)[1:], count + 1, minus - 1) return max(op1, op2, op3) return str(dfs(v[:], [], 0, 0)) if __name__ == "__main__": print(solve("\n".join([input(), input()])))
def solve(string): n, k, *v = map(int, string.split()) ans = 0 for i in range(k + 1): for j in range(k - i + 1): base = sorted(v[:i] + v[max(n - k + i + j, i) :]) ans = max(ans, sum([b for l, b in enumerate(base) if b >= 0 or j <= l])) return str(ans) if __name__ == "__main__": print(solve("\n".join([input(), input()])))
replace
2
13
2
8
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // 関数 // main関数 int main() { // 入力 long N, K; cin >> N >> K; vector<long> V(N); for (int i = 0; i < N; i++) { cin >> V.at(i); } // 計算 long ans = -1000000000000, Ls = 0; multiset<long> Lm; for (int L = 0; L <= N; L++) { if (L > 0) { Ls += V.at(L - 1); if (V.at(L - 1) < 0) Lm.insert(V.at(L - 1)); } long Rs = 0; multiset<long> M = Lm; for (int R = 0; L + R <= K; R++) { if (R > 0) { Rs += V.at(N - R); if (V.at(N - R) < 0) M.insert(V.at(N - R)); } long Pans = Ls + Rs; long count = 0; for (auto ite = M.begin(); (ite != M.end() && count < K - L - R); ite++) { Pans -= *ite; count++; } // cout<<"L"<<L<<"R"<<R<<"Pans"<<Pans<<endl; ans = max(Pans, ans); } } // 出力 cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; // 関数 // main関数 int main() { // 入力 long N, K; cin >> N >> K; vector<long> V(N); for (int i = 0; i < N; i++) { cin >> V.at(i); } // 計算 long ans = -1000000000000, Ls = 0; multiset<long> Lm; for (int L = 0; L <= N; L++) { if (L > 0) { Ls += V.at(L - 1); if (V.at(L - 1) < 0) Lm.insert(V.at(L - 1)); } long Rs = 0; multiset<long> M = Lm; for (int R = 0; L + R <= min(K, N); R++) { if (R > 0) { Rs += V.at(N - R); if (V.at(N - R) < 0) M.insert(V.at(N - R)); } long Pans = Ls + Rs; long count = 0; for (auto ite = M.begin(); (ite != M.end() && count < K - L - R); ite++) { Pans -= *ite; count++; } // cout<<"L"<<L<<"R"<<R<<"Pans"<<Pans<<endl; ans = max(Pans, ans); } } // 出力 cout << ans << endl; }
replace
26
27
26
27
0
p03032
C++
Runtime Error
#include <bits/stdc++.h> #define err(args...) \ {} #ifdef DEBUG #include "_debug.cpp" #endif using namespace std; using ll = long long; using ld = long double; template <typename T> using lim = numeric_limits<T>; template <typename T> istream &operator>>(istream &is, vector<T> &a) { for (T &x : a) { is >> x; } return is; } template <typename X, typename Y> istream &operator>>(istream &is, pair<X, Y> &p) { return is >> p.first >> p.second; } const int N = 50, K = 100; ll mem[N + 1][N + 1][K + 1]; ll opt(int i, int j, int k, vector<int> &v) { ll &ans = mem[i + 1][j + 1][k]; if (ans == -1) { if (i <= j) { ans = max({k >= 1 ? v[i] + opt(i + 1, j, k - 1, v) : 0, k >= 1 ? v[j] + opt(i, j - 1, k - 1, v) : 0, k >= 2 ? opt(i + 1, j, k - 2, v) : 0, k >= 2 ? opt(i, j - 1, k - 2, v) : 0}); } else { ans = 0; } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> v(n); cin >> v; memset(mem, -1, sizeof mem); ll ans = 0; for (int kk = 0; kk <= k; kk++) { ans = max(ans, opt(0, n - 1, kk, v)); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define err(args...) \ {} #ifdef DEBUG #include "_debug.cpp" #endif using namespace std; using ll = long long; using ld = long double; template <typename T> using lim = numeric_limits<T>; template <typename T> istream &operator>>(istream &is, vector<T> &a) { for (T &x : a) { is >> x; } return is; } template <typename X, typename Y> istream &operator>>(istream &is, pair<X, Y> &p) { return is >> p.first >> p.second; } const int N = 50, K = 100; ll mem[N + 2][N + 1][K + 1]; ll opt(int i, int j, int k, vector<int> &v) { ll &ans = mem[i + 1][j + 1][k]; if (ans == -1) { if (i <= j) { ans = max({k >= 1 ? v[i] + opt(i + 1, j, k - 1, v) : 0, k >= 1 ? v[j] + opt(i, j - 1, k - 1, v) : 0, k >= 2 ? opt(i + 1, j, k - 2, v) : 0, k >= 2 ? opt(i, j - 1, k - 2, v) : 0}); } else { ans = 0; } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> v(n); cin >> v; memset(mem, -1, sizeof mem); ll ans = 0; for (int kk = 0; kk <= k; kk++) { ans = max(ans, opt(0, n - 1, kk, v)); } cout << ans << endl; return 0; }
replace
21
22
21
22
0
p03032
C++
Runtime Error
/*------------------------------------ ........Bismillahir Rahmanir Rahim.... ..........created by Abdul Aziz....... ------------------------------------*/ #include <bits/stdc++.h> #define int long long #define pb push_back #define co(x) cout << x << '\n' #define sz(x) (int)x.size() #define all(a) a.begin(), a.end() using namespace std; bool cmp(int a, int b) { return a >= b; } inline void solve() { int n, k, ans = 0; cin >> n >> k; // max(n,k) <= 100 vector<int> a(n + 5); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i <= min(n, k); i++) { for (int j = 0; j <= i; j++) { int l = j, r = i - j, cur = 0; vector<int> hand; for (int t = 0; t < l; t++) { hand.pb(a[t]); } for (int t = 0; t < r; t++) { hand.pb(a[n - t - 1]); } sort(all(hand), cmp); for (int t = 1; t <= k - i and sz(hand) > 0; t++) { if (hand.back() < 0) hand.pop_back(); else break; } for (auto &it : hand) cur += it; ans = max(ans, cur); } } co(ans); } // problem link: https://atcoder.jp/contests/abc128/tasks/abc128_d signed main() { int n = 1; // cin>>n; while (n--) solve(); return 0; }
/*------------------------------------ ........Bismillahir Rahmanir Rahim.... ..........created by Abdul Aziz....... ------------------------------------*/ #include <bits/stdc++.h> #define int long long #define pb push_back #define co(x) cout << x << '\n' #define sz(x) (int)x.size() #define all(a) a.begin(), a.end() using namespace std; bool cmp(int a, int b) { return (a > b); } inline void solve() { int n, k, ans = 0; cin >> n >> k; // max(n,k) <= 100 vector<int> a(n + 5); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i <= min(n, k); i++) { for (int j = 0; j <= i; j++) { int l = j, r = i - j, cur = 0; vector<int> hand; for (int t = 0; t < l; t++) { hand.pb(a[t]); } for (int t = 0; t < r; t++) { hand.pb(a[n - t - 1]); } sort(all(hand), cmp); for (int t = 1; t <= k - i and sz(hand) > 0; t++) { if (hand.back() < 0) hand.pop_back(); else break; } for (auto &it : hand) cur += it; ans = max(ans, cur); } } co(ans); } // problem link: https://atcoder.jp/contests/abc128/tasks/abc128_d signed main() { int n = 1; // cin>>n; while (n--) solve(); return 0; }
replace
12
13
12
13
0