// Problem: B - Plus and AND // Contest: AtCoder - AtCoder Regular Contest 146 // URL: https://atcoder.jp/contests/arc146/tasks/arc146_b // Memory Limit: 1024 MB // Time Limit: 5000 ms // // Powered by CP Editor (https://cpeditor.org)
/* Name: Author: xiaruize Date: */ #include<bits/stdc++.h> usingnamespace std; #define int long long #define ull unsigned long long #define ALL(a) (a).begin(), (a).end() #define pb push_back #define mk make_pair #define pii pair<int, int> #define pis pair<int, string> #define sec second #define fir first #define sz(a) int((a).size()) #define rep(i, x, y) for (int i = x; i <= y; i++) #define repp(i, x, y) for (int i = x; i >= y; i--) #define Yes cout << "Yes" << endl #define YES cout << "YES" << endl #define No cout << "No" << endl #define NO cout << "NO" << endl #define debug(x) cerr << #x << ": " << x << endl #define double long double constint INF = 0x3f3f3f3f; constint MOD = 1000000007; constint N = 2e5 + 10;
// bool st; int n, m, k; pii a[N]; bool vis[N]; // bool en;
boolcheck(int x) { int cnt = 0; for (int i = 1; i <= n; i++) a[i].fir &= (1 << (x + 1)) - 1; sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) { if (vis[a[i].sec]) continue; if (a[i].fir & (1 << x)) { cnt++; } } int sum = 0; for (int i = n; i >= 1; i--) { if (vis[a[i].sec]) continue; if (cnt < k && (!(a[i].fir & (1 << x)))) { sum += (1 << x) - (a[i].fir & ((1 << x) - 1)); if (sum > m) returnfalse; cnt++; } elseif ((!(a[i].fir & (1 << x)))) { vis[a[i].sec] = true; } } if (sum > m) returnfalse; m -= sum; returntrue; }
signedmain() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); // cerr<<(&en-&st)/1024.0/1024.0<<endl; // auto t_1=chrono::high_resolution_clock::now(); cin >> n >> m >> k; for (int i = 1; i <= n; i++) cin >> a[i].fir, a[i].sec = i; int res = 0; for (int i = 30; i >= 0; i--) { if (check(i)) { for (int j = 1; j <= n; j++) { if (!vis[a[j].sec] && (((a[j].fir >> i) ^ 1) & 1)) a[j].fir = 0; } res += (1 << i); } } cout << res << endl; // auto t_2=chrono::high_resolution_clock::now(); // cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl; return0; }
/* Name: Author: xiaruize Date: */ #include<bits/stdc++.h> usingnamespace std; #define int long long #define ull unsigned long long #define ALL(a) (a).begin(), (a).end() #define pb push_back #define mk make_pair #define pii pair<int, int> #define pis pair<int, string> #define sec second #define fir first #define sz(a) int((a).size()) #define rep(i, x, y) for (int i = x; i <= y; i++) #define repp(i, x, y) for (int i = x; i >= y; i--) #define Yes cout << "Yes" << endl #define YES cout << "YES" << endl #define No cout << "No" << endl #define NO cout << "NO" << endl #define debug(x) cerr << #x << ": " << x << endl #define double long double constint INF = 0x3f3f3f3f; constint MOD = 1000000007; constint N = 2e5 + 10;
// bool st; int n, m, k; structnode { int x, q, y; }; vector<node> g[N]; int a[N]; int st[N]; queue<int> que; // bool en;
boolcmp(node a, node b) { return a.x < b.x; }
signedmain() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); // cerr<<(&en-&st)/1024.0/1024.0<<endl; // auto t_1=chrono::high_resolution_clock::now(); cin >> n >> m >> k; for (int i = 1; i <= k; i++) { int p, x, q, y; cin >> p >> x >> q >> y; g[p].pb({x, q, y}); g[q].pb({y, p, x}); g[p].pb({x + 1, q, y + 1}); g[q].pb({y + 1, p, x + 1}); } for (int i = 1; i <= n; i++) { a[i] = 1; que.push(i); sort(ALL(g[i]), cmp); } while (!que.empty()) { int x = que.front(); que.pop(); for (int &i = st[x]; i < g[x].size(); i++) { node v = g[x][i]; if (a[x] < v.x) break; if (a[v.q] < v.y) que.push(v.q); a[v.q] = max(a[v.q], v.y); if (a[v.q] > m) { cout << "-1" << endl; return0; } } } int res = 0; for (int i = 1; i <= n; i++) res += a[i]; cout << res << endl; // auto t_2=chrono::high_resolution_clock::now(); // cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl; return0; }