text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int maxn = 300009; const int maxm = 300009; const double pi = acos(-1.0); const double eps = 1e-6; long long myrand(long long mod) { return ((long long)rand() << 32 ^ (long long)rand() << 16 ^ rand()) % mod; } template <class T> inline bool scan_d(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return false; while (c != '-' && (c < '0' || c > '9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return true; } inline bool scan_ch(char &ch) { if (ch = getchar(), ch == EOF) return false; while (ch == ' ' || ch == '\n') ch = getchar(); return true; } inline void out_number(long long x) { if (x < 0) { putchar('-'); out_number(-x); return; } if (x > 9) out_number(x / 10); putchar(x % 10 + '0'); } int n; long long data[maxn], k; int main() { ios ::sync_with_stdio(false); cin.tie(0); cout.tie(0); while (cin >> n >> k) { for (int i = 1; i <= n; i++) cin >> data[i]; sort(data + 1, data + 1 + n); if (n == 1) { cout << abs(k - data[1]) << endl; continue; } long long ans = 0; int mid = (n + 1) >> 1; if (data[mid] < k) { for (int i = mid; i <= n; ++i) { if (data[i] < k) ans += k - data[i]; else break; } } if (data[mid] > k) { for (int i = mid; i >= 1; --i) { if (data[i] > k) ans += data[i] - k; else break; } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; bool prime[1000001]; const double gr = 1.6180339887; void SieveOfErat(long long int mn) { memset(prime, true, sizeof(prime)); for (long long i = 2; i * i <= mn; i++) { if (prime[i] == true) { for (long long j = i * 2; j <= 1000000; j += i) prime[j] = false; } } } long long int n, s; vector<long long int> a; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s; for (long long int i = 0; i < n; i++) { long long int x; cin >> x; a.push_back(x); } long long int cnt = 0; long long int poss = n / 2; sort(a.begin(), a.end()); long long int pos = lower_bound(a.begin(), a.end(), s) - a.begin(); if (s < a[0] || s > a[n - 1]) pos = -1; if (pos == -1) { if (a[0] > s) { for (long long int i = 0; i <= poss; i++) cnt += abs(a[i] - s); } else if (a[n - 1] < s) { for (long long int i = poss; i < n; i++) cnt += abs(a[i] - s); } cout << cnt << endl; return 0; } long long int med = a[n / 2]; if (pos == poss) { cout << abs(a[poss] - s) << endl; return 0; } if (a[pos] >= med) { for (long long int i = poss; i < pos; i++) { cnt += abs(a[i] - s); } } else { for (long long int i = pos; i <= poss; i++) { cnt += abs(a[i] - s); } } cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long a[200010]; int main() { long long n, s, i, ans = 0; cin >> n >> s; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (i = 0; i < n / 2; i++) if (a[i] > s) ans += a[i] - s; ans += abs(a[n / 2] - s); for (i = n / 2 + 1; i < n; i++) if (a[i] < s) ans += s - a[i]; cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, s; scanf("%lld", &n); scanf("%lld", &s); vector<long long int> v(n); for (long long int i = 0; i < n; ++i) { scanf("%lld", &v[i]); } if (n == 1) { cout << abs(v[0] - s); return 0; } sort(v.begin(), v.end()); long long int x = n / 2; long long int m = v[x]; if (s == m) { cout << 0; return 0; } long long int ans = 0; if (s < m) { long long int index = upper_bound(v.begin(), v.end(), s) - v.begin(); for (long long int i = index; i <= x; ++i) { ans = ans + abs(v[i] - s); } } else { long long int index = upper_bound(v.begin(), v.end(), s) - v.begin() - 1; for (long long int i = x; i <= index; ++i) { ans += abs(v[i] - s); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, i, j, l, c = 0, s; cin >> n >> s; long long a[n], b[n]; vector<long long> v; for (i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } sort(b, b + n); bool bb = true; if (s == b[n / 2]) return cout << "0", 0; else if (s < b[n / 2]) { for (i = n / 2; i >= 0; i--) { if (b[i] > s) { c += abs(s - b[i]); } } cout << c << endl; } else if (s > b[n / 2]) { for (i = n / 2; i < n; i++) { if (s > b[i]) { c += abs(s - b[i]); } } cout << c << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5; int n, s; int a[N + 87]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> s; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); if (a[n / 2] == s) { cout << 0 << endl; } else if (a[n / 2] < s) { long long int ans = 0; for (int i = n / 2; i < n && a[i] < s; i++) ans += s - a[i]; cout << ans << endl; } else { long long int ans = 0; for (int i = n / 2; i >= 0 && a[i] > s; i--) ans += a[i] - s; cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; template <typename Q> void inin(Q &x) { x = 0; int f = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x = f ? -x : x; } long long n, a[200020], s; int main() { inin(n), inin(s); for (int i = (1); i <= (n); i++) inin(a[i]); sort(a + 1, a + n + 1); long long sum = 0; sum += abs(a[n / 2 + 1] - s); a[n / 2 + 1] = s; for (int i = (1); i <= (n / 2); i++) if (a[i] > s) sum += abs(a[i] - s); for (int i = (n / 2 + 2); i <= (n); i++) if (a[i] < s) sum += abs(a[i] - s); cout << sum; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n = 0, m = 0, x = 0, i = 0; long long int c = 0; cin >> n >> m; vector<int> a; for (int i = 0; i < n; i++) { cin >> x; a.push_back(x); } sort(a.begin(), a.end()); if (a[(n - 1) / 2] > m) { c = a[(n - 1) / 2] - m; i = (n - 1) / 2 - 1; while (a[i] > m && i >= 0) { c += a[i] - m; i--; } } else { c = m - a[(n - 1) / 2]; i = (n - 1) / 2 + 1; while (a[i] < m && i < n) { c += m - a[i]; i++; } } cout << c; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); int n, s; cin >> n >> s; long long ar[n]; for (int i = 0; i < n; i++) cin >> ar[i]; sort(ar, ar + n); long long ans = 0; if (ar[n / 2] > s) { ans = ar[n / 2] - s; int in = n / 2; in--; while (ar[in] > s) { if (in < 0) break; ans += ar[in] - s; in--; } } if (ar[n / 2] < s) { ans = s - ar[n / 2]; int in = n / 2; in++; while (ar[in] < s) { if (in == n) break; ans += s - ar[in]; in++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long choose(long long n, long long k); bool isprime(long long n); long long power(long long x, long long y, long long p); int main() { long long n, s; cin >> n >> s; long long ans = 0; long long arr[n]; long long arr1[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; arr1[i] = arr[i]; } sort(arr1, arr1 + n); long long mid = arr1[n / 2]; if (s >= mid) { for (int i = n / 2; i < n; i++) { if (arr1[i] > s) break; ans += s - arr1[i]; } cout << ans; } else { for (int i = n / 2; i >= 0; i--) { if (arr1[i] < s) break; ans += arr1[i] - s; } cout << ans; } return 0; } long long choose(long long n, long long k) { if (k == 0) return 1; return (n * choose(n - 1, k - 1)) / k; } bool isprime(long long n) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, s, i, j; cin >> n >> s; vector<long long> a(n); for (i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); long long mid = n / 2; long long tot = 0; if (a[mid] == s) { cout << tot << endl; return 0; } else if (a[mid] < s) { for (i = mid; i < n; i++) tot += max(0LL, s - a[i]); cout << tot << endl; return 0; } else { for (i = mid; i >= 0; i--) tot += max(0LL, a[i] - s); cout << tot << endl; return 0; } }
#include <bits/stdc++.h> using namespace std; int n, s, a[210000]; int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + n + 1); int t = (n + 1) / 2; long long ans = 0; if (a[t] >= s) { for (int i = 1; i <= t; i++) if (a[i] >= s) ans += a[i] - s; } else { for (int i = t; i <= n; i++) if (a[i] < s) ans += s - a[i]; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; vector<long long> ar(n); for (long long i = 0; i <= n - 1; ++i) { cin >> ar[i]; } sort(ar.begin(), ar.end()); long long idx = n / 2; long long ans = 0; for (long long i = 0; i <= idx - 1; ++i) { if (ar[i] > k) { ans += (ar[i] - k); } } ans += abs(ar[idx] - k); for (long long i = idx + 1; i <= n - 1; ++i) { if (ar[i] < k) ans += k - ar[i]; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k, m, n; long long r = 0; scanf("%d %d\n", &n, &m); int a[n]; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); j = n / 2; if (a[j] < m) { for (i = j; i < n; i++) { if (a[i] >= m) break; r += m - a[i]; } } else if (a[j] > m) { for (i = j; i >= 0; i--) { if (a[i] <= m) break; r += a[i] - m; } } printf("%I64d", r); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; long long s; cin >> n >> s; vector<long long> nums; int cur; for (int i = 0; i < n; i++) { cin >> cur; nums.push_back(cur); } sort(nums.begin(), nums.end()); int bel = 0; int abo = 0; for (int i = 0; i < nums.size(); i++) { if (nums[i] < s) bel++; if (nums[i] > s) abo++; } long long ans = 0LL; if (bel > n / 2) { for (int i = bel - 1; i >= n / 2; i--) { ans += (s - nums[i]); } } if (abo > n / 2) { for (int i = n - abo; i < n - n / 2; i++) { ans += (nums[i] - s); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, s; cin >> n >> s; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); int med = n / 2; long long ans = abs(arr[med] - s); if (s < arr[med]) { int target = s; for (int i = med - 1; i >= 0; i--) { if (arr[i] > target) { ans += (long long)(arr[i] - target); } } } else { int target = s; for (int i = med + 1; i < n; i++) { if (arr[i] < target) { ans += (long long)(target - arr[i]); } } } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, s; cin >> n >> s; ll ans = 0; vi a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort((a).begin(), (a).end()); for (int i = 0; i < n / 2; i++) ans += max(0, a[i] - s); ans += abs(a[n / 2] - s); for (int i = n / 2 + 1; i < n; i++) ans += max(0, s - a[i]); cout << ans; }
#include <bits/stdc++.h> using namespace std; long long int MOD(long long int a, long long int b) { if (a > b) return a - b; else return b - a; } int max3(int a, int b, int c) { return max(c, max(a, b)); } long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long int logg(long long int a) { long long int x = 0; while (a > 1) { x++; a /= 2; } return x; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n, second; cin >> n >> second; vector<int> A(n); for (int i = 0; i < n; i++) cin >> A[i]; sort(A.begin(), A.end()); int mid = n / 2; long long int sum = 0; if (second < A[mid]) { int i = mid; while (A[i] > second && i >= 0) { sum += A[i] - second; i--; } } else { int i = mid; while (A[i] < second && i < n) { sum += second - A[i]; i++; } } cout << sum; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("O3") int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, med, ans; ans = 0; cin >> n >> med; long long arr[n]; for (long long i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); int lb = lower_bound(arr, arr + n, med) - arr; if (lb == n / 2) cout << abs(med - arr[n / 2]); else { ans = abs(med - arr[n / 2]); int temp = n / 2 + 1; while (arr[temp] < med and temp < n) { ans += med - arr[temp]; temp++; } temp = n / 2 - 1; while (arr[temp] > med and temp >= 0) { ans += arr[temp] - med; temp--; } cout << ans << "\n"; } }
#include <bits/stdc++.h> using namespace std; long long int a[200010]; int main() { int n, s; cin >> n >> s; for (int i = 0; i < n; i++) scanf("%lld", &a[i]); sort(a, a + n); long long int amid = 0; if (a[n / 2] <= s) { for (int i = n / 2; i < n; i++) { if (a[i] >= s) break; amid += s - a[i]; } cout << amid << '\n'; return 0; } for (int i = n / 2; i >= 0; i--) { if (a[i] <= s) break; amid += abs(a[i] - s); } cout << amid << '\n'; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; int intcmp(const void *v1, const void *v2) { return *(int *)v1 - *(int *)v2; } inline int read() { int x = 0, f = 1; char cc = getchar(); while (cc < '0' || cc > '9') { if (cc == '-') f = -1; cc = getchar(); } while (cc >= '0' && cc <= '9') { x = x * 10 + cc - '0'; cc = getchar(); } return x * f; } const int maxn = 2e5 + 100; int arr[maxn]; int n; int s; int main() { n = read(); s = read(); for (int i = 0; i < n; i++) scanf("%d", arr + i); sort(arr, arr + n); long long res = 0; for (int i = n / 2; i < n; i++) { if (arr[i] < s) { res += (s - arr[i]); } } for (int i = 0; i < n / 2 + 1; i++) { if (arr[i] > s) res += (arr[i] - s); } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void guan() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } const long long mod2 = 1009050431; const long long mod3 = 784514251; const long long hash_mod[] = {998244353, 1009050431, 784514251}; const int INF = 0x7FFFFFFF; const long long LINF = 0x7FFFFFFFFFFFFFFF; const double pi = acos(-1.0); const long long maxx = 1LL << 61; const double eps = 1e-8; const long long mod = 998244353; const int maxn = 1010101; int a[maxn]; int main() { guan(); int n, s; while (cin >> n >> s) { for (int i = 1; i <= n; ++i) cin >> a[i]; sort(a + 1, a + 1 + n); int mid = n + 1 >> 1; long long ans = 0; if (a[mid] < s) { for (int i = mid; i <= n; i++) { if (a[i] < s) ans += s - a[i]; else break; } } else { for (int i = mid; i >= 1; i--) { if (a[i] > s) ans += a[i] - s; else break; } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long int abs1(long long int a) { if (a > 0) return a; return -1 * a; } long long int max1(long long int a, long long int b) { if (a > b) return a; return b; } long long int min1(long long int a, long long int b) { if (a < b) return a; return b; } int main() { long long int n, k; cin >> n >> k; vector<long long int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); long long int ans = 0; if (a[n / 2] != k) ans = abs1(k - a[n / 2]); for (int i = n / 2 + 1; i < n; i++) { if (a[i] >= k) break; ans += k - a[i]; } for (int i = n / 2 - 1; i >= 0; i--) { if (a[i] <= k) break; ans += a[i] - k; } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> int main() { int64_t n, s, res = 0; std::cin >> n >> s; std::vector<int64_t> inp(n); for (auto &k : inp) { std::cin >> k; } std::sort(inp.begin(), inp.end()); int64_t mid = n / 2; if (inp[mid] < s) { for (int64_t i = mid; i < n && inp[i] < s; res += std::abs(inp[i] - s), i++) ; } else { for (int64_t i = mid; i >= 0 && inp[i] > s; res += std::abs(inp[i] - s), i--) ; } std::cout << res; }
#include <bits/stdc++.h> using namespace std; vector<long long int> v; int main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); cin.tie(0); long long int n, s; cin >> n >> s; for (int i = 0; i < n; i++) { long long int a; cin >> a; v.push_back(a); } sort(v.begin(), v.end()); int m = n / 2; if (v[m] == s) { cout << 0 << endl; return 0; } long long int ans = 0; if (v[m] > s) { int start = 0; while (start <= m && s > v[start]) { start++; }; for (int i = start; i <= m; i++) { ans += v[i] - s; } } else { int end = n - 1; while (end >= m && s < v[end]) { end--; }; for (int i = end; i >= m; i--) { ans += s - v[i]; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; long long arr[200010]; int main() { long long n, second; scanf("%lld %lld", &n, &second); for (int i = 0; i < n; i++) { scanf("%lld", &arr[i]); } sort(arr, arr + n); long long res = 0; if (second >= arr[n / 2]) { for (int i = n / 2; i < n; i++) { if (arr[i] >= second) break; res += second - arr[i]; } } else { for (int i = n / 2; i >= 0; i--) { if (arr[i] <= second) break; res += arr[i] - second; } } printf("%lld\n", res); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, s; cin >> n >> s; int a[n + 1]; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1); long long ans = 0; for (int i = 1; i < (n + 1) / 2; i++) { if (a[i] > s) { ans += a[i] - s; } } for (int i = (n + 1) / 2 + 1; i <= n; i++) { if (a[i] < s) { ans += s - a[i]; } } cout << ans + abs(a[(n + 1) / 2] - s); }
#include <bits/stdc++.h> using namespace std; const int N = 100001; vector<int> cum, counter; int main() { ios_base::sync_with_stdio(false); int n, s; cin >> n >> s; long long int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); long long int sum = 0; int k = n / 2; if (arr[k] < s) { for (int i = k; i < n; i++) { if (arr[i] < s) { sum += s - arr[i]; } } } else if (arr[k] > s) { for (int i = k; i >= 0; i--) { if (arr[i] > s) { sum += arr[i] - s; } } } cout << sum; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ifstream myFile("task.in"); if (!myFile.fail()) { assert(freopen("task.in", "r", stdin)); } long long n, s; cin >> n >> s; long long a[n + 10]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int mid = (n / 2); if (s == a[mid]) { cout << 0; return 0; } long long res = abs(s - a[mid]); if (a[mid] < s) { int i = mid + 1; while (a[i] < s && i < n) { res += abs(a[i] - s); i++; } } else { int i = mid - 1; while (a[i] > s && i >= 0) { res += abs(a[i] - s); i--; } } cout << res; printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') f = -f; for (; isdigit(ch); ch = getchar()) x = 10 * x + ch - '0'; return x * f; } const int maxn = 2e5 + 10; int n, sa; int s[maxn]; int main() { n = read(), sa = read(); for (int i = 1; i <= n; i++) s[i] = read(); sort(s + 1, s + n + 1); if (s[n / 2 + 1] > sa) { long long ans = 0; for (int i = 1; i <= n / 2 + 1; i++) if (s[i] > sa) ans += (s[i] - sa); cout << ans; return 0; } else { long long ans = 0; for (int i = n / 2 + 1; i <= n; i++) if (s[i] <= sa) ans += (sa - s[i]); cout << ans; return 0; }; ; ; }
#include <bits/stdc++.h> using namespace std; long long n, m, ans = 0; vector<long long> v; int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); cin >> n >> m; v.resize(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } sort(v.begin(), v.end()); for (int i = 0; i < n; ++i) { if (i <= n / 2) { if (v[i] > m) ans += v[i] - m; } if (i >= n / 2) { if (m > v[i]) ans += m - v[i]; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; long long n, s, d, dd, a[1000010]; int main() { cin >> n >> s; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + 1 + n); if (a[(n + 1) / 2] == s) { cout << 0; } if (a[(n + 1) / 2] > s) { for (int i = 1; i <= (n + 1) / 2; i++) if (a[i] > s) d += abs(s - a[i]); cout << d; } if (a[(n + 1) / 2] < s) { for (int i = (n + 1) / 2; i <= n; i++) if (a[i] < s) dd += abs(s - a[i]); cout << dd; } return 0; }
#include <bits/stdc++.h> using namespace std; long long n, ans, c1, c2, s; vector<long long> v1, v2; int32_t main() { cin >> n >> s; for (long long i = 0; i < n; i++) { long long a; cin >> a; if (a < s) c1++, v1.push_back(a); if (a > s) c2++, v2.push_back(a); } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); reverse(v1.begin(), v1.end()); long long c3 = n - c1 - c2; for (long long i = 0; i < c1 && c3 + c2 < (n + 1) / 2; i++) { ans += s - v1[i]; c3++; } for (long long i = 0; i < c2 && c3 + c1 < (n + 1) / 2; i++) { ans += v2[i] - s; c3++; } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, s, i, a[200010], sum = 0, j; cin >> n >> s; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); if (a[n / 2] == s) cout << 0 << endl; else if (a[n / 2] > s) { sum += a[n / 2] - s; j = n / 2; j--; for (j = j; j >= 0; j--) { if (a[j] > s) sum += a[j] - s; else break; } cout << sum << endl; } else if (a[n / 2] < s) { sum += s - a[n / 2]; j = n / 2; j++; for (j = j; j < n; j++) { if (a[j] < s) sum += s - a[j]; else break; } cout << sum << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = int64_t; using ull = uint64_t; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vi>; using vii = vector<pii>; void solve() { int n, s; cin >> n >> s; vi a(n); for (int &x : a) cin >> x; sort(a.begin(), a.end()); int median = a[n / 2]; ll ans = 0; if (median <= s) { for (int i = n / 2; i < n && a[i] < s; i++) ans += (s - a[i]); } else { for (int i = n / 2; i >= 0 && a[i] > s; i--) ans += (a[i] - s); } cout << ans << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); int T = 1; while (T--) solve(); return 0; }
#include <bits/stdc++.h> const int MAX = 1e6 + 10; const int MOD = 1e9 + 7; using namespace std; int a[MAX]; int main() { int n, s; cin >> n >> s; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + n + 1); long long ans = 0; for (int i = 1; i <= n / 2; i++) ans += max(a[i] - s, 0); for (int i = n / 2 + 2; i <= n; i++) ans += max(s - a[i], 0); cout << ans + abs(s - a[(n + 1) / 2]) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); int k; cin >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); long long ans = 0; for (int i = 0; i <= n / 2; i++) { if (a[i] > k) { ans += a[i] - k; } } for (int i = n / 2; i < n; i++) { if (a[i] < k) { ans += k - a[i]; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int n, a[200005], x; long long ans; int main() { scanf("%d%d", &n, &x); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + n + 1); ans += abs(a[(n + 1) / 2] - x); for (int i = 1; i < (n + 1) / 2; i++) if (a[i] > x) ans += a[i] - x; for (int i = (n + 1) / 2 + 1; i <= n; i++) if (a[i] < x) ans += x - a[i]; printf("%lld\n", ans); }
#include <bits/stdc++.h> using namespace std; const int iINF = 0x3f3f3f3f; const long long lINF = 0x3f3f3f3f3f3f3f3f; const int MAXN = 2e5 + 5; int n, a[MAXN], s; long long ans = 0; int main() { scanf("%d%d", &n, &s); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); int mid = (n + 1) / 2 - 1; if (a[mid] > s) { for (int i = mid; i >= 0 && a[i] > s; i--) ans += a[i] - s; } else if (a[mid] < s) { for (int i = mid; i < n && a[i] < s; i++) ans += s - a[i]; } printf("%I64d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; const double PI = acos(-1); const long long MOD = 1e9 + 7; const int INF = 0x7FFFFFFF; const long long LINF = 0x7FFFFFFFFFFFFFFFLL; const unsigned long long UINF = 0xFFFFFFFFFFFFFFFFLL; const int dCol[] = {0, +1, 0, -1, +1, +1, -1, -1}; const int dRow[] = {-1, 0, +1, 0, -1, +1, +1, -1}; template <typename T> inline void VIN(vector<T> &i) { T x; cin >> x; i.push_back(x); } inline void FIO(string i) { freopen((i + ".in").c_str(), "r", stdin); freopen((i + ".out").c_str(), "w", stdout); } int N, S, data[200005], median; long long ans = 0; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N >> S; for (int i = (0); i < (N); i++) cin >> data[i]; sort(data, data + N); median = data[N / 2]; if (S > median) { for (int i = (N / 2); i < (N); i++) if (data[i] < S) ans += abs(data[i] - S); } else if (S < median) { for (int i = (N / 2); i >= (0); i--) if (data[i] > S) ans += abs(data[i] - S); } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long gcd(long long a, long long b) { if (a == 0) return b; else return gcd(b % a, a); } int main() { long long n, second; cin >> n >> second; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); long long p = n / 2; long long ans = 0; if (second > a[p]) { while (second > a[p] && p < n) { ans += (second - a[p]); p++; } } else { while (second < a[p] && p >= 0) { ans += (a[p] - second); p--; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; void solve() { long long n, k; cin >> n >> k; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); long long ans = 0; long long i = 0; while (i < n / 2) { if (a[i] >= k) ans += a[i] - k; i++; } ans += abs(a[i] - k); i++; while (i < n) { if (a[i] < k) ans += k - a[i]; i++; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int i = 1; solve(); }
#include <bits/stdc++.h> using namespace std; long long const MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n, k, i; cin >> n >> k; int a[n]; long long ans = 0; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int val = a[n / 2]; if (k > val) { i = n / 2; while (i < n) { if (a[i] < k) { ans += k - a[i]; i++; } else break; } cout << ans; } else { i = n / 2; while (i >= 0) { if (a[i] > k) { ans += a[i] - k; i--; } else break; } cout << ans; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, s; cin >> n >> s; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); int j = n / 2; long long int sum = 0; if (s > a[j]) { sum += s - a[j]; j++; while (j < n && s > a[j]) sum += s - a[j++]; cout << sum; } else if (s < a[j]) { sum += a[j] - s; j--; while (j >= 0 && s < a[j]) sum += a[j--] - s; cout << sum; } else cout << 0; return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (!a) return b; return gcd(b % a, a); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, s; cin >> n >> s; long long a[n], ans = 0; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (long long i = 0; i <= (n / 2); i++) { if (a[i] > s) ans += a[i] - s; } for (long long i = (n / 2); i < n; i++) { if (a[i] < s) ans += s - a[i]; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; bool isV(char ch) { return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'); } void tc() { int n, m, x; cin >> n >> x; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); unsigned long long int ans = 0; if (v[n / 2] == x) { cout << 0; return; } if (v[n / 2] < x) { ans += 1LL * (x - v[n / 2]); v[n / 2] = x; for (int i = (n / 2) + 1; i < n; i++) { if (v[i] < v[i - 1]) { ans += 1LL * (v[i - 1] - v[i]); v[i] = v[i - 1]; } else { break; } } cout << ans; } else if (v[n / 2] > x) { ans += 1LL * (v[n / 2] - x); v[n / 2] = x; for (int i = (n / 2) - 1; i >= 0; i--) { if (v[i] > v[i + 1]) { ans += 1LL * (v[i] - v[i + 1]); v[i] = v[i + 1]; } else { break; } } cout << ans; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); tc(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, s; cin >> n >> s; long long int arr[n]; for (long long int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); long long int mid = arr[n / 2]; long long int cst = 0; cst = cst + abs(s - mid); mid = s; for (long long int i = 0; i < n; i++) { if (i != n / 2) { if (i < n / 2) { if (arr[i] > mid) { cst = cst + arr[i] - mid; } } else { if (arr[i] < mid) { cst = cst + mid - arr[i]; } } } } cout << cst; }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v; long long int n, s, x, countt = 0; cin >> n >> s; for (int i = 0; i < n; i++) { cin >> x; v.push_back(x); } sort(v.begin(), v.end()); int mid = n / 2; int m = v[mid]; if (m <= s) { for (int i = mid; i < v.size(); i++) { if (v[i] <= s) { countt += abs(v[i] - s); } else { break; } } } else if (s <= m) { for (int i = mid; i >= 0; i--) { if (v[i] >= s) { countt += abs(v[i] - s); } else { break; } } } cout << countt << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, s; scanf("%d %d", &n, &s); vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } sort(v.begin(), v.end()); long long res = 0; for (int i = 0; i < n; i++) { if (i < n / 2) { if (v[i] > s) res += abs(v[i] - s); } else if (i > n / 2) { if (v[i] < s) res += abs(v[i] - s); } else { res += abs(v[i] - s); } } cout << res << "\n"; }
#include <bits/stdc++.h> using namespace std; int n, s, a[300005]; int main() { cin >> n >> s; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); long long sum = 0; for (int i = n / 2; i >= 0 && a[i] > s; i--) sum += a[i] - s; for (int i = n / 2; i < n && a[i] < s; i++) sum += s - a[i]; cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; long long arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); long long res = 0; int pos = n / 2; if (arr[pos] < k) { for (int i = pos; i < n; i++) { res += max((long long)0, k - arr[i]); } } else if (arr[pos] > k) { for (int i = pos; i >= 0; i--) { res += max((long long)0, arr[i] - k); } } cout << res; }
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, k, n, m, x = 0, y, z; cin >> n >> m; long long a[n + 5]; for (i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + 1 + n); k = (n + 1) / 2; if (a[k] == m) { cout << 0 << endl; return 0; } if (a[k] > m) { x = abs(a[k] - m); a[k] = a[k] - x; for (i = k; i >= 1; i--) { if (a[i - 1] > a[i] && i - 1 >= 1) { z = abs(a[i] - a[i - 1]); x = x + z; a[i - 1] = a[i - 1] - z; } } cout << x << endl; return 0; } x = abs(a[k] - m); a[k] = m; for (i = k + 1; i <= n; i++) { if (a[i] < a[i - 1]) { z = abs(a[i] - a[i - 1]); x = x + z; a[i] = a[i] + z; } } cout << x << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, s; cin >> n >> s; vector<long long> v(n); for (long long &x : v) cin >> x; sort(begin(v), end(v)); int m = n / 2; long long ans = 0; if (v[m] > s) { for (int i = 0; i <= m; i++) { if (v[i] > s) ans += v[i] - s; } } else if (v[m] < s) { for (int i = m; i < n; i++) { if (v[i] < s) ans += s - v[i]; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int find_dist(double x, double y, double x1, double y1) { double dist; x = abs(x1 - x); y = abs(y1 - y); dist = hypot(x, y); return dist; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { long long int n, s, j = 0, i, sum = 0; cin >> n >> s; long long a[n]; for (i = 0; i < n; i++) { cin >> a[i]; if (a[i] == s) { j = 1; } } sort(a, a + i); long long int x = (n / 2); if (a[x] == s) { cout << "0"; return 0; } if (!j) { i = x; if (s > a[x]) { while (a[i] < s && i <= n - 1) { sum += (s - a[i]); i++; } } else if (s < a[x]) { while (a[i] > s && i >= 0) { sum += (a[i] - s); i--; } } cout << sum; return 0; } if (a[x] < s) { i = x; while (a[i] != s) { sum += (s - a[i]); i++; } } else if (a[x] > s) { i = x; while (a[i] != s) { sum += (a[i] - s); i--; } } cout << sum; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, s; cin >> n >> s; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); int temp = n / 2; long long int ans = 0; for (int i = 0; i < temp; i++) { if (arr[i] > s) { ans += arr[i] - s; } } for (int i = n - 1; i > temp; i--) { if (arr[i] < s) { ans += s - arr[i]; } } ans += abs(arr[n / 2] - s); cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i, s; vector<long long int> v; cin >> n; cin >> s; long long int p; for (i = 0; i < n; i++) { cin >> p; v.push_back(p); } sort(v.begin(), v.end()); long long int med = v[(v.size() - 1) / 2]; long long int count = 0; if (s > med) { i = (v.size() - 1) / 2; while (v[i] <= s && i < n) { count = count + (s - v[i]); if (v[i] == s) { break; } i++; } cout << count; } else if (s < med) { i = (v.size() - 1) / 2; while (v[i] >= s && i >= 0) { count = count + (v[i] - s); if (v[i] == s) { break; } i--; } cout << count; } if (s == med) { cout << "0"; } }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100; int a[N]; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); int n, s; cin >> n >> s; for (int i = 1; i <= n; ++i) { cin >> a[i]; } sort(a + 1, a + n + 1); long long ans = 0; for (int i = 1; i <= n / 2; ++i) { ans += max(0, a[i] - s); } ans += abs(a[n / 2 + 1] - s); for (int i = n / 2 + 2; i <= n; ++i) { ans += max(0, s - a[i]); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const double EPS = 1e-9; const long long INF = 1e18 + 2007; const double PI = 3.1415926535897932384626433832795; const long long N = 200005; vector<long long> v; signed main() { ios_base::sync_with_stdio(0); long long n, s; cin >> n >> s; v.resize(n); for (long long i = 0; i < n; i++) { cin >> v[i]; } sort((v).begin(), (v).end()); long long ans = 0; for (long long i = 0; i < n / 2; i++) { if (v[i] > s) { ans += abs(s - v[i]); } } for (long long i = n / 2 + 1; i < n; i++) { if (v[i] < s) { ans += abs(s - v[i]); } } cout << ans + abs(v[n / 2] - s); return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int main() { long long n, second, count = 0; cin >> n >> second; vector<long long> v(n); for (long long i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); auto lower = lower_bound(v.begin(), v.end(), second); if (*lower == second) { long long index = lower - v.begin(); while (index != n / 2) { count += abs(second - v[index]); index < n / 2 ? index++ : index--; } count += abs(second - v[index]); } else { long long mid = n / 2; if (v[mid] > second) { while (mid >= 0 and v[mid] > second) { count += abs(second - v[mid]); mid--; } } else { while (mid < n and v[mid] < second) { count += abs(second - v[mid]); mid++; } } } cout << count << "\n"; }
#include <bits/stdc++.h> using namespace std; string itosm(long long x) { if (x == 0) return "0"; string res = ""; while (x > 0) { res += ((x % 10) + '0'); x /= 10; } reverse(res.begin(), res.end()); return res; } long long stoim(string str) { long long res = 0; int p = 0; if (str[0] == '-') p++; for (int i = p; i < str.length(); i++) { res *= 10; res += (str[i] - '0'); } return res; } const long long infll = 1e18 + 3; const int inf = 1009000999; const long double eps = 1e-7; const int maxn = 1e6 + 1146; const int baseint = 1000200013; const long long basell = 1e18 + 3; const long double PI = acos(-1.0); const long long mod = 1e9 + 7; long long binpow(long long x, long long st) { if (st == 0) return 1; if (st & 1) return binpow(x, st - 1) * x % mod; long long res = binpow(x, st >> 1); return res * res % mod; } int a[maxn]; void solve() { int n, s; cin >> n >> s; for (int i = 00; i < n; i++) cin >> a[i]; sort(a, a + n); long long ans = 0; if (a[n / 2] > s) { for (int i = 0; i <= n / 2; i++) if (a[i] > s) ans += a[i] - s; } else { for (int i = n / 2; i < n; i++) if (a[i] < s) ans += s - a[i]; } cout << ans; } int main() { srand(time(0)); ios_base::sync_with_stdio(0); ; cin.tie(0); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, s; cin >> n >> s; vector<long long> v(n); for (long long i = 0; i < n; i++) { cin >> v[i]; } sort(v.begin(), v.end()); if (s > v[n / 2]) { long long u = lower_bound(v.begin(), v.end(), s) - v.begin(); long long ans = 0; for (long long i = u - 1; i >= n / 2; i--) { ans += s - v[i]; } cout << ans; } else if (s < v[n / 2]) { long long u = upper_bound(v.begin(), v.end(), s) - v.begin(); long long ans = 0; u--; for (long i = u + 1; i <= n / 2; i++) { ans += v[i] - s; } cout << ans; } else cout << 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, s, c = 0, i; cin >> n >> s; long long int a[n]; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); if (a[n / 2] < s) { c += s - a[n / 2]; for (i = n / 2 + 1; i < n; i++) if (a[i] < s) c += s - a[i]; } if (a[n / 2] > s) { c += a[n / 2] - s; for (i = 0; i < n / 2; i++) if (a[i] > s) c += a[i] - s; } cout << c; return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e6; long long arr[N + 2], s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, m; cin >> n >> m; for (long long i = 1; i <= n; i++) cin >> arr[i]; sort(arr + 1, arr + n + 1); s = abs(arr[n / 2 + 1] - m); arr[n / 2 + 1] = m; for (long long i = 1; i <= n / 2 + 1; i++) if (arr[i] > m) s += abs(arr[i] - m); for (long long i = n / 2 + 1; i <= n; i++) if (arr[i] < m) s += abs(arr[i] - m); cout << s << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <class _T> inline void ina(_T a[], int n) { for (int i = 0; i < n; i++) cin >> (a[i]); } int TEST_CASE = 0; template <class _T> inline _T pow(_T a, _T b, _T m) { a %= m; _T ans = 1 % m; while (b) { if (b & 1) ans *= a, ans %= m; a *= a; a %= m; b >>= 1; } return ans; } template <class _T> inline _T pow(_T a, _T b) { _T ans = 1; while (b) { if (b & 1) ans *= a; a *= a; b >>= 1; } return ans; } template <class _T> inline _T add(_T a, _T b, _T m) { return a >= m - b ? a - (m - b) : a + b; } template <class _T> inline _T multiply(_T a, _T b, _T m) { _T ans = 0; if (b > a) swap(a, b); while (b) { if (b & 1) ans = add(ans, a, m); b >>= 1; a = add(a, a, m); } return ans; } template <class _T> inline _T bigpow(_T a, _T b, _T m) { a %= m; _T ans = 1 % m; while (b) { if (b & 1) ans = multiply(ans, a, m); a = multiply(a, a, m); b >>= 1; } return ans; } template <class _T> inline _T modinvers(_T a, _T m) { return m > 2000000000LL ? bigpow(a, m - 2, m) : pow(a, m - 2, m); } template <class _T> _T _egcd(_T a, _T b, _T &x, _T &y) { if (!b) { x = 1, y = 0; return a; } _T _g = _egcd(b, a % b, x, y); _T xt = x; x = y, y = xt - (a / b) * y; return _g; } template <class _T> inline _T fmodinvers(_T a, _T m) { _T x, y; _egcd(a, m, x, y); x %= m; if (x < 0) x += m; return x; } template <class _T> inline _T _lcm(_T a, _T b) { return (a * b) / __gcd(a, b); } template <class T> inline T SQ(T a) { return a * a; } long long SQRT(long long n) { long long e = sqrt(n * 1.0); long long l = max(0LL, e - 2), r = min(n, e + 2); long long ans = 0; while (l <= r) { long long m = ((l + r) >> 1); if (m * m <= n) ans = m, l = m + 1; else r = m - 1; } return ans; } long long CBRT(long long n) { long long e = cbrt(n * 1.0); long long l = max(0LL, e - 2), r = min(n, e + 2); long long ans = 0; while (l <= r) { long long m = ((l + r) >> 1); if (m * m * m <= n) ans = m, l = m + 1; else r = m - 1; } return ans; } const long double EPS = 1e-9; const long double PI = acos(-1.0); const int SIZE = 1e6; long long mod = 1e9 + 7; long long a[1000006]; int main() { long long n, s; cin >> n >> s; ina(a, n); sort(a, a + n); if (a[n / 2] == s) cout << 0 << '\n'; else if (a[n / 2] > s) { long long ans = 0; for (int i = 0; i <= n / 2; i++) ans += max(0LL, a[i] - s); cout << ans << '\n'; } else { long long ans = 0; for (int i = n / 2; i < n; i++) ans += max(0LL, s - a[i]); cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, s; scanf("%d %d", &n, &s); vector<int> a(n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a.begin(), a.end()); if (a[n / 2] == s) { printf("0\n"); return 0; } if (a[n / 2] > s) { long long sum = 0; for (int j = 0; j <= n / 2; j++) { if (s < a[j]) { sum += abs(s - a[j]); } } printf("%lld\n", sum); return 0; } if (a[n / 2] < s) { long long sum = 0; for (int j = n / 2; j < n; j++) { if (s > a[j]) { sum += abs(s - a[j]); } } printf("%lld\n", sum); return 0; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 200008; long long n, m, nn, a[N]; long long absx(long long x) { if (x < 0) x = -x; return x; } int main(void) { std::ios::sync_with_stdio(false); long long i, s1; cin >> n >> m; nn = (n + 1) / 2; for (i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + 1 + n); if (a[nn] == m) { cout << 0; return 0; } if (a[nn] < m) { s1 = m - a[nn]; for (i = nn + 1; i <= n; i++) if (a[i] < m) s1 += m - a[i]; cout << s1; return 0; } if (a[nn] > m) { s1 = a[nn] - m; for (i = 1; i < nn; i++) if (a[i] > m) s1 += a[i] - m; cout << s1; return 0; } return 0; }
#include <bits/stdc++.h> using namespace std; long long ans; int n, s, a[200005], i; int main() { cin >> n >> s; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); ans = 0; if (a[n / 2] == s) { cout << 0 << endl; return 0; } else if (a[n / 2] < s) { for (i = n / 2; i < n; i++) ans += (long long)max(0, s - a[i]); } else { for (i = 0; i <= n / 2; i++) ans += (long long)max(0, a[i] - s); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, s; cin >> n >> s; long long arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; if (n == 1) return cout << abs(arr[0] - s), 0; long long mid = n / 2; sort(arr, arr + n); long long ans = abs(arr[mid] - s); for (int i = mid - 1;; i--) { if (arr[i] <= s) break; ans += abs(arr[i] - s); } for (int i = mid + 1;; i++) { if (arr[i] >= s) break; ans += abs(arr[i] - s); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int a[200005], n, k; long long ans; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + n + 1); for (int i = 1; i <= n / 2; i++) if (a[i] > k) ans += a[i] - k; for (int i = n / 2 + 2; i <= n; i++) if (a[i] < k) ans += k - a[i]; printf("%lld\n", ans + abs(k - a[n / 2 + 1])); }
#include <bits/stdc++.h> using namespace std; long long int reach(long long int ar[], long long int n, long long int s) { long long int x = n / 2, sf = 0, ff = 0; for (long long int i = 0; i < x; i++) { if (ar[i] > s) ff += ar[i] - s; if (ar[n - 1 - i] < s) sf += s - ar[n - 1 - i]; } return ff + sf + abs(ar[x] - s); } int main() { long long int n, s; cin >> n >> s; long long int ar[n]; for (long long int i = 0; i < n; i++) { cin >> ar[i]; } sort(ar, ar + n); cout << reach(ar, n, s) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; int a[maxn]; int n, s, mid; long long ans = 0; int main() { scanf("%d %d", &n, &s); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + n + 1); int mid = a[n / 2 + 1]; if (mid == s) { printf("0\n"); return 0; } int x = lower_bound(a + 1, a + n + 1, s) - a; int y = upper_bound(a + 1, a + n + 1, s) - a; if (s > mid) { for (int i = n / 2 + 1; i < x; i++) { ans += (s - a[i]); } } else if (s < mid) { for (int i = y; i <= n / 2 + 1; i++) { ans += (a[i] - s); } } printf("%I64d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, x, counter = 0; cin >> n >> x; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); if (a[n / 2] == x) counter = 0; else if (a[n / 2] < x) { for (int i = (n / 2); i < n; i++) { if (x > a[i]) counter += abs(x - a[i]); else break; } } else if (a[n / 2] > x) { for (int i = n / 2; i >= 0; i--) { if (x < a[i]) counter += abs(-x + a[i]); else break; } } cout << counter << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long a[200010]; int main() { long long n, s, i, ps = 1, zw, ans = 0; cin >> n >> s; for (i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); while (a[ps++] < s && ps <= n + 1) ; ps--; zw = (n + 1) / 2; if (ps > zw) ps--, swap(ps, zw); for (i = ps; i <= zw; i++) ans += abs(s - a[i]); cout << ans; }
#include <bits/stdc++.h> using namespace std; int n, s, a[10001000]; long long ans; int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + 1 + n); int mid = (n / 2 + 1); for (int i = 1; i <= mid; i++) { if (a[i] > s) ans += a[i] - s, a[i] = s; } for (int i = mid; i <= n; i++) { if (a[i] < s) ans += s - a[i], a[i] = s; } printf("%lld", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, s; scanf("%d%d", &n, &s); int a[n]; for (int i = 0; i < n; ++i) { scanf("%d", a + i); } sort(a, a + n); long long ans = 0; if (a[n / 2] < s) { for (int i = n / 2; i < n; ++i) { if (a[i] < s) { ans += s - a[i]; } } } else if (a[n / 2] > s) { for (int i = n / 2; i >= 0; --i) { if (a[i] > s) { ans += a[i] - s; } } } printf("%I64d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline bool smin(T &a, const T &b) { return a > b ? a = b : a; } template <typename T> inline bool smax(T &a, const T &b) { return a < b ? a = b : a; } const int N = (int)1e6 + 6, mod = (int)0; int a[N]; int main() { int n, s; cin >> n >> s; for (int j = 0; j < n; ++j) cin >> a[j]; sort(a, a + n); long long res = abs(a[n / 2] - s); for (int j = 0; j < n / 2; ++j) res += max(0, a[j] - s); for (int j = n / 2 + 1; j < n; ++j) res += max(0, s - a[j]); cout << res << endl; }
#include <bits/stdc++.h> using namespace std; int n, m; struct lut { string name; int cl; int val; int size; }; struct an { string name; string whr; int cl; int val; int num; }; vector<lut> l; vector<an> anim; vector<an> d[3]; vector<bool> used; int all_size; void Inputdata() { cin >> n; l.resize(n); for (int i = 0; i < n; i++) { string name; string cls; int a, b, c, s; cin >> name >> cls >> a >> b >> c >> s; l[i].name = name; l[i].size = s; all_size += s; if (cls == "weapon") { l[i].cl = 0; l[i].val = a; } if (cls == "armor") { l[i].cl = 1; l[i].val = b; } if (cls == "orb") { l[i].cl = 2; l[i].val = c; } } cin >> m; anim.resize(m); used.resize(m); for (int i = 0; i < m; i++) { string name, type, whr; int val; cin >> name >> type >> val >> whr; anim[i].name = name; anim[i].whr = whr; anim[i].val = val; anim[i].num = i; if (type == "gladiator") anim[i].cl = 0; if (type == "sentry") anim[i].cl = 1; if (type == "physician") anim[i].cl = 2; d[anim[i].cl].push_back(anim[i]); } } bool Comp(const an &a, const an &b) { return a.val > b.val; } void Solve() { sort(d[0].begin(), d[0].end(), Comp); sort(d[1].begin(), d[1].end(), Comp); sort(d[2].begin(), d[2].end(), Comp); int ans[3]; int max_val[3]; for (int i = 0; i < 3; i++) max_val[i] = -10; if (all_size > m) { for (int i = 0; i < n; i++) { int type = l[i].cl; int now_val = l[i].val; for (int j = 0; j < min(l[i].size, int(d[type].size())); j++) now_val += d[type][j].val; if (max_val[type] < now_val) { max_val[type] = now_val; ans[type] = i; } } vector<string> ans_out[3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < min(l[ans[i]].size, int(d[i].size())); j++) { ans_out[i].push_back(d[i][j].name); used[d[i][j].num] = true; } } for (int i = 0; i < 3; i++) { int cnt = int(ans_out[i].size()); for (int j = 0; j < m; j++) { if (cnt >= l[ans[i]].size) break; if (!used[j]) { ans_out[i].push_back(anim[j].name); used[j] = true; cnt++; } } } for (int i = 0; i < 3; i++) { cout << l[ans[i]].name << ' '; cout << ans_out[i].size() << ' '; for (int j = 0; j < ans_out[i].size(); j++) cout << ans_out[i][j] << ' '; cout << endl; } } else { int max_val[3]; int ans[3]; for (int i = 0; i < n; i++) { int type = l[i].cl; int now_val = l[i].val; for (int j = 0; j < m; j++) if (anim[j].whr == l[i].name && l[i].cl == anim[j].cl) now_val += anim[j].val; if (max_val[type] < now_val) { max_val[type] = now_val; ans[type] = i; } } vector<string> ans_out[3]; for (int i = 0; i < 3; i++) { int type = l[ans[i]].cl; for (int j = 0; j < m; j++) if (anim[j].whr == l[ans[i]].name) ans_out[i].push_back(anim[j].name); } for (int i = 0; i < 3; i++) { cout << l[ans[i]].name << ' '; cout << ans_out[i].size() << ' '; for (int j = 0; j < ans_out[i].size(); j++) cout << ans_out[i][j] << ' '; cout << endl; } } } int main() { Inputdata(); Solve(); return 0; }
#include <bits/stdc++.h> using namespace std; struct Item { string nazwa, typ; int pkt, wlk; }; struct Resident { string nazwa, typ; int pkt; string item; }; string it[] = {"weapon", "armor", "orb"}; string re[] = {"gladiator", "sentry", "physician"}; bool por(Resident a, Resident b) { if (a.pkt > b.pkt) return true; if (a.pkt < b.pkt) return false; return (a.nazwa < b.nazwa); } void Usun(vector<Resident>& a, string x) { vector<Resident> b; for (int i = 0; i < a.size(); ++i) if (a[i].nazwa != x) b.push_back(a[i]); a = b; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<Item> I(n); int sWlk = 0; for (int i = 0; i < n; ++i) { int x, y, z; cin >> I[i].nazwa >> I[i].typ >> x >> y >> z >> I[i].wlk; if (I[i].typ == it[0]) I[i].pkt = x; if (I[i].typ == it[1]) I[i].pkt = y; if (I[i].typ == it[2]) I[i].pkt = z; sWlk += I[i].wlk; } int m; cin >> m; vector<Resident> R(m); for (int i = 0; i < m; ++i) cin >> R[i].nazwa >> R[i].typ >> R[i].pkt >> R[i].item; vector<string> nazwyItOdp(3); vector<int> wynOdp(3, -1); vector<vector<string> > nazwyReOdp(3); if (m == sWlk) { for (int i = 0; i < n; ++i) { int wynT = 0; vector<string> nazwyT; for (int j = 0; j < 3; ++j) { if (I[i].typ != it[j]) continue; for (int k = 0; k < m; ++k) { if (R[k].item == I[i].nazwa) { if (R[k].typ == re[j]) wynT += R[k].pkt; nazwyT.push_back(R[k].nazwa); } } wynT += I[i].pkt; if (wynT > wynOdp[j]) { nazwyItOdp[j] = I[i].nazwa; wynOdp[j] = wynT; nazwyReOdp[j] = nazwyT; } } } } else { vector<int> wlkOdp(3); for (int i = 0; i < n; ++i) { vector<Resident> T; int wynT = 0; vector<string> nazwyT; for (int j = 0; j < 3; ++j) { if (I[i].typ != it[j]) continue; for (int k = 0; k < m; ++k) if (R[k].typ == re[j]) T.push_back(R[k]); sort(T.begin(), T.end(), por); for (int k = 0; k < min((int)I[i].wlk, (int)T.size()); ++k) { wynT += T[k].pkt; nazwyT.push_back(T[k].nazwa); } wynT += I[i].pkt; if (wynT > wynOdp[j]) { nazwyItOdp[j] = I[i].nazwa; wynOdp[j] = wynT; wlkOdp[j] = I[i].wlk; nazwyReOdp[j] = nazwyT; } } } for (int i = 0; i < 3; ++i) for (int j = 0; j < nazwyReOdp[i].size(); ++j) Usun(R, nazwyReOdp[i][j]); for (int i = 0; i < R.size(); ++i) { if (nazwyReOdp[0].size() < wlkOdp[0]) { nazwyReOdp[0].push_back(R[i].nazwa); continue; } if (nazwyReOdp[1].size() < wlkOdp[1]) { nazwyReOdp[1].push_back(R[i].nazwa); continue; } if (nazwyReOdp[2].size() < wlkOdp[2]) { nazwyReOdp[2].push_back(R[i].nazwa); continue; } } } for (int i = 0; i < 3; ++i) { cout << nazwyItOdp[i] << " " << nazwyReOdp[i].size() << " "; for (int j = 0; j < nazwyReOdp[i].size(); ++j) cout << nazwyReOdp[i][j] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Item { string name; int size; int pt; }; vector<Item> items[3]; struct Res { string name; string home; int bonus; }; struct G { bool operator()(const Res& a, const Res& b) { return a.bonus > b.bonus; } }; vector<Res> res[3]; set<string> names; int main() { int n; cin >> n; int sum = 0; for (int i = 0; i < n; i++) { string name, cl; int size; int p[3]; cin >> name >> cl >> p[0] >> p[1] >> p[2] >> size; int index = 0; if (cl == "weapon") { index = 0; } else if (cl == "armor") { index = 1; } else if (cl == "orb") { index = 2; } else { assert(false); } items[index].push_back(Item{name, size, p[index]}); sum += size; } int k; cin >> k; for (int i = 0; i < k; i++) { string name, tp, home; int bounus; cin >> name >> tp >> bounus >> home; int index = 0; if (tp == "gladiator") { index = 0; } else if (tp == "sentry") { index = 1; } else if (tp == "physician") { index = 2; } res[index].push_back(Res{name, home, bounus}); names.insert(name); } if (sum == k) { for (int i = 0; i < 3; i++) { map<string, int> total; int m = -1; string mname; for (auto it : items[i]) { total[it.name] = it.pt; if (it.pt > m) { m = it.pt; mname = it.name; } } for (auto r : res[i]) { if (total.count(r.home)) { total[r.home] += r.bonus; if (total[r.home] > m) { m = total[r.home]; mname = r.home; } } } cout << mname; vector<string> mem; for (int j = 0; j < 3; j++) { for (auto r : res[j]) { if (r.home == mname) { mem.push_back(r.name); } } } cout << " " << mem.size(); for (auto r : mem) { cout << " " << r; } cout << endl; } return 0; } Item ret0[3]; vector<string> ret[3]; for (int i = 0; i < 3; i++) { sort(res[i].begin(), res[i].end(), G()); int m = -1; Item mit; for (auto it : items[i]) { for (int j = 0; j < min(it.size, (int)res[i].size()); j++) { it.pt += res[i][j].bonus; } if (it.pt > m) { m = it.pt; mit = it; } } ret0[i] = mit; for (int j = 0; j < min(mit.size, (int)res[i].size()); j++) { names.erase(res[i][j].name); ret[i].push_back(res[i][j].name); } } for (int i = 0; i < 3; i++) { while (ret0[i].size > ret[i].size() && names.size()) { ret[i].push_back(*names.begin()); names.erase(names.begin()); } cout << ret0[i].name << " " << ret[i].size(); for (auto r : ret[i]) { cout << " " << r; } cout << endl; } return 0; }
#include <bits/stdc++.h> template <typename T> inline void SWAP(T &a, T &b) { T tmp = a; a = b; b = tmp; } template <typename T> inline T ABS(const T &val) { return val < 0 ? -val : val; } template <typename T> inline T MAX(const T &a, const T &b) { return a > b ? a : b; } template <typename T> inline T MIN(const T &a, const T &b) { return a < b ? a : b; } const int INTinf = 2147483647; const int nINTinf = 0 - 2147483648; using namespace std; int n, k, m; int places = 0; struct item; int bonus[3][10]; struct resid { string name; int type, param; item *parent; resid(string name, int type, int param, item *parent) { this->name.assign(name.begin(), name.end()); this->type = type; this->param = param; this->parent = parent; } bool operator<(const resid &q) const { return param > q.param; } }; int cmp(const resid *a, const resid *b) { return *a < *b; } struct item { string name; int atk, def, rez, size, type; item(string name, int type, int atk, int def, int rez, int size) { this->name.assign(name.begin(), name.end()); this->type = type; this->atk = atk; this->def = def; this->rez = rez; this->size = size; } int getParam() { int ret = 0; for (int i = 0; i != resids.size(); i++) { if (resids[i]->type == type) { ret += resids[i]->param; } } switch (type) { case 0: return ret + atk; case 1: return ret + def; default: return ret + rez; } } int getParam2() { switch (type) { case 0: return bonus[0][size - 1] + atk; case 1: return bonus[1][size - 1] + def; default: return bonus[2][size - 1] + rez; } } void swap(resid *own, resid *aim) { if (own) { for (int i = 0; i != aim->parent->resids.size(); i++) { if (aim->parent->resids[i] == aim) { aim->parent->resids[i] = own; own->parent = aim->parent; aim->parent = this; for (int j = 0; j != resids.size(); j++) { if (own == resids[j]) { resids[j] = aim; return; } } } } } else { for (int i = 0; i != aim->parent->resids.size(); i++) { if (aim->parent->resids[i] == aim) { for (int j = i + 1; j != aim->parent->resids.size(); j++) { aim->parent->resids[j - 1] = aim->parent->resids[j]; } aim->parent->resids.pop_back(); resids.push_back(aim); aim->parent = this; return; } } } } vector<resid *> resids; }; map<string, item *> items; bool can = true; struct vend { vector<item *> items[3]; vector<resid *> resids[3]; void add(item *it) { places += it->size; ::items[it->name] = it; items[it->type].push_back(it); } void addR(resid *res) { resids[res->type].push_back(res); res->parent->resids.push_back(res); } }; vend V; string str, str2, str3; int q, w, e, s; int main() { cin >> n; for (int i = 0; i != n; i++) { cin >> str >> str2 >> q >> w >> e >> s; int tt; switch (str2[0]) { case 'w': tt = 0; break; case 'a': tt = 1; break; case 'o': tt = 2; break; } V.add(new item(str, tt, q, w, e, s)); } cin >> m; can = (m != places); for (int i = 0; i != m; i++) { cin >> str >> str2 >> q >> str3; int tt; switch (str2[0]) { case 'g': tt = 0; break; case 's': tt = 1; break; case 'p': tt = 2; break; } V.addR(new resid(str, tt, q, items[str3])); } if (can) { sort(V.resids[0].begin(), V.resids[0].end(), cmp); sort(V.resids[1].begin(), V.resids[1].end(), cmp); sort(V.resids[2].begin(), V.resids[2].end(), cmp); memset(bonus, 0, sizeof(bonus)); for (int i = 0; i != 3; i++) { int to = MIN(10, (int)V.resids[i].size()); if (to) bonus[i][0] = V.resids[i][0]->param; if (to) for (int j = 1; j != 10; j++) { bonus[i][j] = bonus[i][j - 1] + (j >= to ? 0 : V.resids[i][j]->param); } } } int MX[] = {-1, -1, -1}; item *best[3]; for (int i = 0; i != 3; i++) { for (int j = 0; j != V.items[i].size(); j++) { int param = (can ? V.items[i][j]->getParam2() : V.items[i][j]->getParam()); if (param > MX[i]) { MX[i] = param; best[i] = V.items[i][j]; } } } if (can) { for (int i = 0; i != 3; i++) { int to = MIN(best[i]->size, (int)V.resids[i].size()); for (int j = 0; j != to; j++) { resid *own; if (j >= best[i]->resids.size()) { own = 0; } else { own = best[i]->resids[j]; } best[i]->swap(own, V.resids[i][j]); } } } for (int i = 0; i != 3; i++) { cout << best[i]->name << ' ' << best[i]->resids.size(); for (int j = 0; j != best[i]->resids.size(); j++) { cout << ' ' << best[i]->resids[j]->name; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int max_n = 1111, inf = 1111111111; int get_type_orj(string s) { if (s == "weapon") return 0; if (s == "armor") return 1; if (s == "orb") return 2; } int get_type_res(string s) { if (s == "gladiator") return 0; if (s == "sentry") return 1; if (s == "physician") return 2; } int get_x(int tp, int a, int b, int c) { if (tp == 0) return a; if (tp == 1) return b; if (tp == 2) return c; } map<string, int> pl, TP; map<string, vector<string> > A; struct orj { string name; int tp, x, sz; void read() { string cl; int a, b, c; cin >> name >> cl >> a >> b >> c >> sz; tp = get_type_orj(cl); x = get_x(tp, a, b, c); TP[name] = tp; } }; struct res { string name, home; int tp, x; void read() { string cl; cin >> name >> cl >> x >> home; tp = get_type_res(cl); if (tp == TP[home]) { pl[home] += x; } A[home].push_back(name); } bool operator<(const res &r) const { return x > r.x; } }; int n, k, sumsz, sum[3][max_n], ans[3]; set<string> all; vector<string> Q; vector<orj> a[3]; vector<res> b[3]; int main() { cin >> n; for (int i = 0; i < n; ++i) { orj o; o.read(); a[o.tp].push_back(o); sumsz += o.sz; } cin >> k; for (int i = 0; i < k; ++i) { res r; r.read(); b[r.tp].push_back(r); } if (k == sumsz) { string ans[3]; for (int tp = 0; tp < 3; ++tp) { int mx = -1; for (int i = 0; i < a[tp].size(); ++i) { if (mx < a[tp][i].x + pl[a[tp][i].name]) { mx = a[tp][i].x + pl[a[tp][i].name]; ans[tp] = a[tp][i].name; } } vector<string> v = A[ans[tp]]; cout << ans[tp] << " " << v.size() << " "; for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } cout << endl; } return 0; } sort(b[0].begin(), b[0].end()); sort(b[1].begin(), b[1].end()); sort(b[2].begin(), b[2].end()); for (int tp = 0; tp < 3; ++tp) { if (b[tp].size() == 0) { continue; } sum[tp][0] = b[tp][0].x; for (int i = 1; i < b[tp].size(); ++i) { sum[tp][i] = b[tp][i].x + sum[tp][i - 1]; } for (int i = b[tp].size(); i < max_n; ++i) { sum[tp][i] = sum[tp][i - 1]; } } for (int tp = 0; tp < 3; ++tp) { int mx = -1; for (int i = 0; i < a[tp].size(); ++i) { int Res = a[tp][i].x + sum[tp][a[tp][i].sz - 1]; if (mx < Res) { mx = Res; ans[tp] = i; } } for (int i = 0; i < a[tp][ans[tp]].sz && i < b[tp].size(); ++i) { all.insert(b[tp][i].name); } } for (int tp = 0; tp < 3; ++tp) { vector<string> v, q; for (int i = 0; i < a[tp][ans[tp]].sz && i < b[tp].size(); ++i) { v.push_back(b[tp][i].name); } q = A[a[tp][ans[tp]].name]; for (int i = 0; i < q.size(); ++i) { if (!all.count(q[i])) { if (a[tp][ans[tp]].sz == v.size()) { Q.push_back(q[i]); } else { v.push_back(q[i]); } } } while (Q.size() && v.size() < a[tp][ans[tp]].sz) { v.push_back(Q.back()); Q.pop_back(); } cout << a[tp][ans[tp]].name << " " << v.size() << " "; for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Item { int num[3], size, now; queue<pair<int, int> > que; char name[100]; void init(char s[]) { scanf("%s%s%d%d%d%d", name, s, &num[0], &num[1], &num[2], &size); now = 0; } }; vector<Item> items[3]; struct Resident { int add, x, y; char name[105]; void init(char s[]) { char belong[105]; scanf("%s%s%d%s", name, s, &add, belong); for (x = 0; x < 3; x++) for (y = 0; y < items[x].size(); y++) if (strcmp(items[x][y].name, belong) == 0) { items[x][y].now++; return; } } }; bool cmp(const Resident& a, const Resident& b) { return a.add > b.add; } vector<Resident> data[3]; int sum[10005]; int used[3], out[3]; int main() { int n, m; scanf("%d", &n); for (int i = 0; i < n; i++) { char s[105]; Item tmp; tmp.init(s); if (s[0] == 'w') items[0].push_back(tmp); else if (s[0] == 'a') items[1].push_back(tmp); else items[2].push_back(tmp); } scanf("%d", &m); for (int i = 0; i < m; i++) { char s[105]; Resident tmp; tmp.init(s); int k = 0; if (s[0] == 's') k = 1; else if (s[0] == 'p') k = 2; data[k].push_back(tmp); } bool flag = false; for (int i = 0; i < 3; i++) for (int j = 0; j < items[i].size(); j++) if (items[i][j].now < items[i][j].size) flag = true; if (flag) { for (int i = 0; i < 3; i++) for (int j = 0; j < items[i].size(); j++) { items[i][j].now = 0; while (!items[i][j].que.empty()) items[i][j].que.pop(); } for (int i = 0; i < 3; i++) { sort(data[i].begin(), data[i].end(), cmp); sum[0] = 0; for (int j = 0; j < data[i].size(); j++) sum[j + 1] = sum[j] + data[i][j].add; int ans = 0, cnt = data[i].size(); for (int k = 1; k < items[i].size(); k++) if (items[i][k].num[i] + sum[min(items[i][k].size, cnt)] > items[i][ans].num[i] + sum[min(items[i][ans].size, cnt)]) ans = k; for (int k = 0; k < items[i].size(); k++) items[i][k].now = 0; used[i] = items[i][ans].now = min(items[i][ans].size, cnt); for (int k = 0; k < used[i]; k++) { items[i][ans].que.push(make_pair(i, k)); } out[i] = ans; } for (int i = 0, g = 0, h = 0; i < 3; i++) { while (used[i] < data[i].size()) { while (items[g][h].now == items[g][h].size) { h++; if (items[g].size() == h) g++, h = 0; } items[g][h].now++; items[g][h].que.push(make_pair(i, used[i]++)); } } } else { for (int i = 0; i < 3; i++) for (int j = 0; j < data[i].size(); j++) { if (data[i][j].x == i) items[i][data[i][j].y].num[i] += data[i][j].add; items[data[i][j].x][data[i][j].y].que.push(make_pair(i, j)); } for (int i = 0; i < 3; i++) { out[i] = 0; for (int k = 1; k < items[i].size(); k++) if (items[i][k].num[i] > items[i][out[i]].num[i]) out[i] = k; } } for (int i = 0; i < 3; i++) { printf("%s %d", items[i][out[i]].name, items[i][out[i]].que.size()); while (!items[i][out[i]].que.empty()) { printf(" %s", data[items[i][out[i]].que.front().first] [items[i][out[i]].que.front().second] .name); items[i][out[i]].que.pop(); } puts(""); } }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; struct hz { string name, cl; int a, d, r, s; hz() : name(""), cl(""), a(0), d(0), r(0), s(0) {} hz(string _name, string _cl, int _a, int _d, int _r, int _s) : name(_name), cl(_cl), a(_a), d(_d), r(_r), s(_s) {} }; int a, b, c, d, i, j, n, m, k; vector<hz> w; map<string, int> cnt; map<string, vector<string> > hren; map<string, int> bonus, msize; map<string, string> cret; vector<string> fr; vector<pair<int, string> > weap, arm, orb; set<string> bil; char s1[10001], s2[10001], s3[10001]; inline bool match(const string& s1, const string& s2) { if (s1[0] == 'w' && s2[0] == 'g') return 1; if (s1[0] == 'a' && s2[0] == 's') return 1; if (s1[0] == 'o' && s2[0] == 'p') return 1; return 0; } inline string findex(string tp) { string ans; int best = -1; for (int _n(((int)((w).size())) - 1), i(0); i <= _n; i++) { if (w[i].cl != tp) continue; int cur = 0; if (tp[0] == 'w') cur += w[i].a; else if (tp[0] == 'a') cur += w[i].d; else cur += w[i].r; const vector<string>& ins = hren[w[i].name]; for (int _n(((int)((ins).size())) - 1), j(0); j <= _n; j++) { if (!match(tp, cret[ins[j]])) continue; cur += bonus[ins[j]]; } if (cur > best) { best = cur; ans = w[i].name; } } return ans; } inline string findex(const string& tp, vector<pair<int, string> > itm) { string ans; int best = -1; for (int _n(((int)((w).size())) - 1), i(0); i <= _n; i++) { if (w[i].cl != tp) continue; int cur = 0; if (tp[0] == 'w') cur += w[i].a; else if (tp[0] == 'a') cur += w[i].d; else cur += w[i].r; int mn = min((int)((itm).size()), w[i].s); hren[w[i].name].clear(); for (int _n((mn)-1), j(0); j <= _n; j++) { cur += itm[j].first; hren[w[i].name].push_back(itm[j].second); } if (cur > best) { best = cur; ans = w[i].name; } } return ans; } inline void add(const string& s) { const vector<string>& ins = hren[s]; for (int _n(((int)((ins).size())) - 1), i(0); i <= _n; i++) bil.insert(ins[i]); } inline void md(const string& s) { vector<string>& ins = hren[s]; int ts = msize[s]; for (int _n(((int)((fr).size())) - 1), i(0); i <= _n; i++) { if ((int)((ins).size()) == ts) break; if (bil.count(fr[i])) continue; ins.push_back(fr[i]); bil.insert(fr[i]); } } inline void output(const string& s) { printf("%s %d", s.c_str(), hren[s].size()); const vector<string>& ins = hren[s]; for (int _n(((int)((ins).size())) - 1), i(0); i <= _n; i++) { printf(" %s", ins[i].c_str()); } printf("\n"); } int main() { scanf("%d", &n); for (int _n((n)-1), i(0); i <= _n; i++) { scanf("%s%s%d%d%d%d", s1, s2, &a, &b, &c, &d); w.push_back(hz(s1, s2, a, b, c, d)); msize[s1] = d; } scanf("%d", &m); for (int _n((m)-1), i(0); i <= _n; i++) { scanf("%s%s%d%s", s1, s2, &a, s3); bonus[s1] = a; fr.push_back(s1); hren[s3].push_back(s1); cret[s1] = s2; if (s2[0] == 'g') weap.push_back(make_pair(a, s1)); else if (s2[0] == 's') arm.push_back(make_pair(a, s1)); else orb.push_back(make_pair(a, s1)); } sort((weap).begin(), (weap).end(), greater<pair<int, string> >()); sort((arm).begin(), (arm).end(), greater<pair<int, string> >()); sort((orb).begin(), (orb).end(), greater<pair<int, string> >()); bool ok = 0; for (int _n(((int)((w).size())) - 1), i(0); i <= _n; i++) { if (hren[w[i].name].size() < w[i].s) { ok = 1; break; } } if (!ok) { string wp = findex("weapon"); string ar = findex("armor"); string ob = findex("orb"); output(wp); output(ar); output(ob); exit(0); } string wp = findex("weapon", weap); string ar = findex("armor", arm); string ob = findex("orb", orb); add(wp); add(ar); add(ob); md(wp); md(ar); md(ob); output(wp); output(ar); output(ob); }
#include <bits/stdc++.h> using namespace std; struct item { string n, c; int a, s; }; item make(string n, string c, int a, int s) { item z; z.a = a; z.c = c; z.n = n; z.s = s; return z; } struct rez { string n, t, h; int b; }; bool f2(rez a, rez b) { return a.b > b.b; } struct equip { string n; vector<string> r; int s; }; int n, k, s, ya[3], yb[3]; item a[3][100]; rez b[3][1000]; equip z[3]; int sum(int h, int l) { int sss = 0; for (int i = 0; i < l; i++) sss += b[h][i].b; return sss; } int main() { string name, clas; int atk, def, rz, siz; cin >> n; for (int i = 0; i < n; i++) { cin >> name >> clas >> atk >> def >> rz >> siz; if (clas == "weapon") { a[0][ya[0]] = make(name, clas, atk, siz); ya[0]++; } if (clas == "armor") { a[1][ya[1]] = make(name, clas, def, siz); ya[1]++; } if (clas == "orb") { a[2][ya[2]] = make(name, clas, rz, siz); ya[2]++; } s += siz; } cin >> k; rez y; for (int i = 0; i < k; i++) { cin >> y.n >> y.t >> y.b >> y.h; if (y.t == "gladiator") { b[0][yb[0]] = y; yb[0]++; } if (y.t == "sentry") { b[1][yb[1]] = y; yb[1]++; } if (y.t == "physician") { b[2][yb[2]] = y; yb[2]++; } } for (int h = 0; h < 3; h++) sort(b[h], b[h] + yb[h], f2); if (s == k) { for (int h = 0; h < 3; h++) { int mx = -1, m; for (int i = 0; i < ya[h]; i++) { int ss = 0; for (int j = 0; j < yb[h]; j++) if (b[h][j].h == a[h][i].n) ss += b[h][j].b; if (ss + a[h][i].a > mx) { mx = ss + a[h][i].a; m = i; } } z[h].n = a[h][m].n; for (int i = 0; i < 3; i++) for (int j = 0; j < yb[i]; j++) if (b[i][j].h == z[h].n) z[h].r.push_back(b[i][j].n); } } else { int m[3]; for (int h = 0; h < 3; h++) { int mx = -1, mm, l; for (int i = 0; i < ya[h]; i++) { int sm = sum(h, min(a[h][i].s, yb[h])); if (a[h][i].a + sm > mx) { mx = a[h][i].a + sm; mm = i; l = min(a[h][i].s, yb[h]); } } z[h].n = a[h][mm].n; z[h].s = a[h][mm].s; for (int i = 0; i < l; i++) z[h].r.push_back(b[h][i].n); m[h] = l; } for (int h = 0; h < 3; h++) for (int i = 0; i < 3; i++) while (m[i] < yb[i] && z[h].r.size() < z[h].s) { z[h].r.push_back(b[i][m[i]].n); m[i]++; } } for (int i = 0; i < 3; i++) { cout << z[i].n << ' ' << z[i].r.size(); for (int j = 0; j < z[i].r.size(); j++) cout << ' ' << z[i].r[j]; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void prepare() {} struct P { string name, cl; int type, atk, def, res, size, num; void read() { cin >> name >> cl; cin >> atk >> def >> res >> size; if (cl == "weapon") type = 0; else if (cl == "armor") type = 1; else if (cl == "orb") type = 2; else assert(false); } }; struct R { string name, cl, home; int type, bonus, num; bool operator<(const R &v) const { return bonus > v.bonus; } void read() { cin >> name >> cl >> bonus >> home; if (cl == "gladiator") type = 0; else if (cl == "sentry") type = 1; else if (cl == "physician") type = 2; else assert(false); } }; int n, m; vector<P> p[3]; vector<R> r[3], v[3]; vector<int> sum[3]; vector<bool> used[3]; int all_size, all_res; map<string, vector<R> > ma; map<string, int> sma[3]; map<string, int> tt; bool ms(const P &a, const P &b) { return sma[a.type][a.name] > sma[b.type][b.name]; } void solve() { cin >> n; P e; for (int i = (0); i < (((n))); i++) { e.read(); p[e.type].push_back(e); all_size += e.size; tt[e.name] = e.type; if (e.type == 0) sma[e.type][e.name] = e.atk; else if (e.type == 1) sma[e.type][e.name] = e.def; else sma[e.type][e.name] = e.res; } cin >> m; R f; for (int i = (0); i < (((m))); i++) { f.read(); r[f.type].push_back(f); all_res++; ma[f.home].push_back(f); if (tt[f.home] == f.type) sma[f.type][f.home] = sma[f.type][f.home] + f.bonus; } if (all_size == all_res) { for (int i = (0); i < (((3))); i++) sort((p[i]).begin(), (p[i]).end(), ms); for (int i = (0); i < (((3))); i++) { string name = p[i][0].name; printf("%s %d", name.c_str(), (int)(ma[name]).size()); for (vector<R>::iterator it = ma[name].begin(); it != ma[name].end(); ++it) printf(" %s", it->name.c_str()); printf("\n"); } return; } for (int i = (0); i < (((3))); i++) { if ((int)(r[i]).size()) { sort((r[i]).begin(), (r[i]).end()); sum[i].push_back(r[i][0].bonus); for (int j = (1); j < ((int)(r[i]).size()); j++) sum[i].push_back(sum[i].back() + r[i][j].bonus); for (int j = (0); j < ((((int)(r[i]).size()))); j++) used[i].push_back(false); } } int res_cnt[3], res_p[3], cur_cnt[3]; res_cnt[0] = -1; res_cnt[1] = -1; res_cnt[2] = -1; for (int i = (0); i < ((((int)(p[0]).size()))); i++) { if ((int)(sum[0]).size()) cur_cnt[0] = p[0][i].atk + sum[0][min((int)(sum[0]).size() - 1, p[0][i].size - 1)]; else cur_cnt[0] = p[0][i].atk; if (cur_cnt[0] < res_cnt[0]) continue; for (int j = (0); j < ((((int)(p[1]).size()))); j++) { if ((int)(sum[1]).size()) cur_cnt[1] = p[1][j].def + sum[1][min((int)(sum[1]).size() - 1, p[1][j].size - 1)]; else cur_cnt[1] = p[1][j].def; if (cur_cnt[0] == res_cnt[0] && cur_cnt[1] < res_cnt[1]) continue; for (int k = (0); k < ((int)(p[2]).size()); k++) { if ((int)(sum[2]).size()) cur_cnt[2] = p[2][k].res + sum[2][min((int)(sum[2]).size() - 1, p[2][k].size - 1)]; else cur_cnt[2] = p[2][k].res; if (cur_cnt[0] == res_cnt[0] && cur_cnt[1] == res_cnt[1] && cur_cnt[2] < res_cnt[2]) continue; res_cnt[0] = cur_cnt[0]; res_cnt[1] = cur_cnt[1]; res_cnt[2] = cur_cnt[2]; res_p[0] = i; res_p[1] = j; res_p[2] = k; } } } for (int i = (0); i < (((3))); i++) { for (int j = (0); j < (((min((int)(r[i]).size(), p[i][res_p[i]].size)))); j++) { v[i].push_back(r[i][j]); used[i][j] = true; } } for (int k = (0); k < (3); k++) { for (int i = (0); i < (((3))); i++) { if ((int)(v[k]).size() >= p[k][res_p[k]].size) break; for (int j = (0); j < ((((int)(r[i]).size()))); j++) { if ((int)(v[k]).size() >= p[k][res_p[k]].size) break; if (!used[i][j]) { used[i][j] = true; v[k].push_back(r[i][j]); } } } } for (int i = (0); i < (((3))); i++) { printf("%s %d", p[i][res_p[i]].name.c_str(), (int)(v[i]).size()); for (int j = (0); j < ((((int)(v[i]).size()))); j++) printf(" %s", v[i][j].name.c_str()); printf("\n"); } } int main() { prepare(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100; const int MAX_K = 1000; enum { WPN, AMR, ORB }; struct Item { string nm; int cl, p, sz, v; vector<int> rs; Item() {} Item(string _nm, int _cl, int _p, int _sz) : nm(_nm), cl(_cl), p(_p), sz(_sz), v(_p), rs() {} void print() { printf("%s[%d] %d(%d) %d\n", nm.c_str(), cl, p, v, sz); } bool operator<(const Item &it) const { return v < it.v; } }; enum { GLD, SNT, PHY }; struct Res { string nm; int tp, b; Res() {} Res(string _nm, int _tp, int _b) : nm(_nm), tp(_tp), b(_b) {} void print() { printf("%s[%d] %d\n", nm.c_str(), tp, b); } bool operator<(const Res &r) const { return b < r.b; } }; Item its[MAX_N]; Res rss[MAX_K]; map<string, int> itmap; vector<int> ics[3], rts[3]; bool cmpit(int &a, int &b) { return its[b] < its[a]; } bool cmprs(int &a, int &b) { return rss[b] < rss[a]; } void showitem(Item &it) { printf("%s %d", it.nm.c_str(), (int)it.rs.size()); for (int i = 0; i < it.rs.size(); i++) printf(" %s", rss[it.rs[i]].nm.c_str()); putchar('\n'); } int main() { int n; scanf("%d", &n); int szsum = 0; for (int i = 0; i < n; i++) { char s[16], t[16]; int a, d, r, sz; scanf("%s%s%d%d%d%d", s, t, &a, &d, &r, &sz); string nm(s); int cl, p; if (t[0] == 'w') cl = WPN, p = a; else if (t[0] == 'a') cl = AMR, p = d; else cl = ORB, p = r; its[i] = Item(nm, cl, p, sz); itmap[nm] = i; ics[cl].push_back(i); szsum += sz; } int k; scanf("%d", &k); for (int i = 0; i < k; i++) { char s[16], t[16], u[16]; int b; scanf("%s%s%d%s", s, t, &b, u); string nm(s), inm(u); int tp = (t[0] == 'g') ? GLD : (t[0] == 's') ? SNT : PHY; rss[i] = Res(nm, tp, b); Item &it = its[itmap[inm]]; it.rs.push_back(i); if (it.cl == tp) it.v += b; rts[tp].push_back(i); } if (k == szsum) { for (int c = 0; c < 3; c++) { vector<int> &icc = ics[c]; sort(icc.begin(), icc.end(), cmpit); showitem(its[icc[0]]); } return 0; } for (int c = 0; c < 3; c++) { vector<int> &icc = ics[c], &rtc = rts[c]; sort(rtc.begin(), rtc.end(), cmprs); for (vector<int>::iterator vit = icc.begin(); vit != icc.end(); vit++) { Item &it = its[*vit]; it.v = it.p; it.rs.clear(); int maxi = min((int)rtc.size(), it.sz); for (int i = 0; i < maxi; i++) it.v += rss[rtc[i]].b, it.rs.push_back(rtc[i]); } sort(icc.begin(), icc.end(), cmpit); } queue<int> qrs; for (int c = 0; c < 3; c++) { vector<int> &icc = ics[c], &rtc = rts[c]; Item &it = its[icc[0]]; for (int i = it.rs.size(); i < rtc.size(); i++) qrs.push(rtc[i]); } for (int c = 0; c < 3; c++) { vector<int> &icc = ics[c], &rtc = rts[c]; Item &it = its[icc[0]]; while (!qrs.empty() && it.rs.size() < it.sz) it.rs.push_back(qrs.front()), qrs.pop(); } for (int c = 0; c < 3; c++) showitem(its[ics[c][0]]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); string name[100], kind[100]; int a[100], b[100], c[100], s[100]; int S = 0; for (int i = 0; i < n; i++) { cin >> name[i] >> kind[i] >> a[i] >> b[i] >> c[i] >> s[i]; S += s[i]; } int k; scanf("%d", &k); if (S == k) { int p[100] = {0}; static string Name[1000], bl[1000]; for (int i = 0; i < k; i++) { string s; int K; cin >> Name[i] >> s >> K >> bl[i]; for (int j = 0; j < n; j++) { if (name[j] == bl[i]) { p[j]++; if (s[0] == 'g') { a[j] += K; } if (s[0] == 's') { b[j] += K; } if (s[0] == 'p') { c[j] += K; } break; } } } int A = -1, B = -1, C = -1, AA, BB, CC; for (int i = 0; i < n; i++) { if (kind[i][0] == 'w' && A < a[i]) { A = a[i]; AA = i; } if (kind[i][0] == 'a' && B < b[i]) { B = b[i]; BB = i; } if (kind[i][0] == 'o' && C < c[i]) { C = c[i]; CC = i; } } cout << name[AA] << ' ' << p[AA]; for (int j = 0; j < k; j++) { if (bl[j] == name[AA]) { cout << ' ' << Name[j]; } } cout << '\n'; cout << name[BB] << ' ' << p[BB]; for (int j = 0; j < k; j++) { if (bl[j] == name[BB]) { cout << ' ' << Name[j]; } } cout << '\n'; cout << name[CC] << ' ' << p[CC]; for (int j = 0; j < k; j++) { if (bl[j] == name[CC]) { cout << ' ' << Name[j]; } } cout << '\n'; } else { vector<pair<int, string> > gl, se, ph; for (int i = 0; i < k; i++) { string Name, Kind, bl; int K; cin >> Name >> Kind >> K >> bl; if (Kind[0] == 'g') { gl.push_back(make_pair(K, Name)); } if (Kind[0] == 's') { se.push_back(make_pair(K, Name)); } if (Kind[0] == 'p') { ph.push_back(make_pair(K, Name)); } } sort(gl.begin(), gl.end()); sort(se.begin(), se.end()); sort(ph.begin(), ph.end()); for (int i = 0; i < n; i++) { if (kind[i][0] == 'w') { for (int j = 0; j < s[i] && j < gl.size(); j++) { a[i] += gl[gl.size() - j - 1].first; } } if (kind[i][0] == 'a') { for (int j = 0; j < s[i] && j < se.size(); j++) { b[i] += se[se.size() - j - 1].first; } } if (kind[i][0] == 'o') { for (int j = 0; j < s[i] && j < ph.size(); j++) { c[i] += ph[ph.size() - j - 1].first; } } } int A = -1, B = -1, C = -1, AA, BB, CC; for (int i = 0; i < n; i++) { if (kind[i][0] == 'w' && A < a[i]) { A = a[i]; AA = i; } if (kind[i][0] == 'a' && B < b[i]) { B = b[i]; BB = i; } if (kind[i][0] == 'o' && C < c[i]) { C = c[i]; CC = i; } } vector<string> As, Bs, Cs; for (int j = 0; j < s[AA] && j < gl.size(); j++) { As.push_back(gl[gl.size() - j - 1].second); } for (int j = 0; j < s[BB] && j < se.size(); j++) { Bs.push_back(se[se.size() - j - 1].second); } for (int j = 0; j < s[CC] && j < ph.size(); j++) { Cs.push_back(ph[ph.size() - j - 1].second); } for (int j = 0; j < (int)gl.size() - s[AA]; j++) { if (Bs.size() < s[BB]) { Bs.push_back(gl[j].second); } else if (Cs.size() < s[CC]) { Cs.push_back(gl[j].second); } } for (int j = 0; j < (int)se.size() - s[BB]; j++) { if (As.size() < s[AA]) { As.push_back(se[j].second); } else if (Cs.size() < s[CC]) { Cs.push_back(se[j].second); } } for (int j = 0; j < (int)ph.size() - s[CC]; j++) { if (Bs.size() < s[BB]) { Bs.push_back(ph[j].second); } else if (As.size() < s[AA]) { As.push_back(ph[j].second); } } cout << name[AA] << ' ' << As.size(); for (int i = 0; i < As.size(); i++) { cout << ' ' << As[i]; } cout << '\n'; cout << name[BB] << ' ' << Bs.size(); for (int i = 0; i < Bs.size(); i++) { cout << ' ' << Bs[i]; } cout << '\n'; cout << name[CC] << ' ' << Cs.size(); for (int i = 0; i < Cs.size(); i++) { cout << ' ' << Cs[i]; } cout << '\n'; } }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int sum = 0; vector<pair<string, pair<int, int> > > A, B, C; for (int i = 0; i < N; i++) { string name, type; cin >> name >> type; int atk, def, orb, sz; cin >> atk >> def >> orb >> sz; if (type == "weapon") { A.push_back(make_pair(name, make_pair(atk, sz))); } else if (type == "armor") { B.push_back(make_pair(name, make_pair(def, sz))); } else { C.push_back(make_pair(name, make_pair(orb, sz))); } sum += sz; } int M; cin >> M; map<string, vector<string> > mp; set<string> names; vector<pair<int, string> > EA, EB, EC; for (int i = 0; i < M; i++) { string name, type, home; int bonus; cin >> name >> type >> bonus >> home; mp[home].push_back(name); names.insert(name); if (type == "gladiator") { if (M == sum) { for (int i = 0; i < A.size(); i++) if (A[i].first == home) { A[i].second.first += bonus; } } else { EA.push_back(make_pair(bonus, name)); } } else if (type == "sentry") { if (M == sum) { for (int i = 0; i < B.size(); i++) if (B[i].first == home) { B[i].second.first += bonus; } } else { EB.push_back(make_pair(bonus, name)); } } else { if (M == sum) { for (int i = 0; i < C.size(); i++) if (C[i].first == home) { C[i].second.first += bonus; } } else { EC.push_back(make_pair(bonus, name)); } } } if (M < sum) { mp.clear(); sort(EA.begin(), EA.end()); sort(EB.begin(), EB.end()); sort(EC.begin(), EC.end()); for (int i = 0; i < A.size(); i++) { for (int j = max(0, (int)EA.size() - A[i].second.second); j < EA.size(); j++) { A[i].second.first += EA[j].first; mp[A[i].first].push_back(EA[j].second); } } for (int i = 0; i < B.size(); i++) { for (int j = max(0, (int)EB.size() - B[i].second.second); j < EB.size(); j++) { B[i].second.first += EB[j].first; mp[B[i].first].push_back(EB[j].second); } } for (int i = 0; i < C.size(); i++) { for (int j = max(0, (int)EC.size() - C[i].second.second); j < EC.size(); j++) { C[i].second.first += EC[j].first; mp[C[i].first].push_back(EC[j].second); } } } int ai = 0, bi = 0, ci = 0; for (int i = 1; i < A.size(); i++) { if (A[i].second.first > A[ai].second.first) ai = i; } for (int i = 1; i < B.size(); i++) { if (B[i].second.first > B[bi].second.first) bi = i; } for (int i = 1; i < C.size(); i++) { if (C[i].second.first > C[ci].second.first) ci = i; } if (M < sum) { vector<string> vv = mp[A[ai].first]; for (int i = 0; i < vv.size(); i++) names.erase(vv[i]); vv = mp[B[bi].first]; for (int i = 0; i < vv.size(); i++) names.erase(vv[i]); vv = mp[C[ci].first]; for (int i = 0; i < vv.size(); i++) names.erase(vv[i]); while (mp[A[ai].first].size() < A[ai].second.second && !names.empty()) { mp[A[ai].first].push_back(*names.begin()); names.erase(names.begin()); } while (mp[B[bi].first].size() < B[bi].second.second && !names.empty()) { mp[B[bi].first].push_back(*names.begin()); names.erase(names.begin()); } while (mp[C[ci].first].size() < C[ci].second.second && !names.empty()) { mp[C[ci].first].push_back(*names.begin()); names.erase(names.begin()); } } vector<string> vv = mp[A[ai].first]; cout << A[ai].first << ' ' << vv.size(); sort(vv.begin(), vv.end()); for (int i = 0; i < vv.size(); i++) cout << ' ' << vv[i]; cout << endl; vv = mp[B[bi].first]; cout << B[bi].first << ' ' << vv.size(); sort(vv.begin(), vv.end()); for (int i = 0; i < vv.size(); i++) cout << ' ' << vv[i]; cout << endl; vv = mp[C[ci].first]; cout << C[ci].first << ' ' << vv.size(); sort(vv.begin(), vv.end()); for (int i = 0; i < vv.size(); i++) cout << ' ' << vv[i]; cout << endl; }
#include <bits/stdc++.h> using namespace std; int main() { vector<pair<string, pair<int, int> > > weapon, armor, orb; int n, k; cin >> n; int sumsize = 0; for (int i = 0; i < n; ++i) { string name, clas; int atk, def, res, size; cin >> name >> clas >> atk >> def >> res >> size; if (clas == "weapon") { weapon.push_back(make_pair(name, make_pair(atk, size))); } else if (clas == "armor") { armor.push_back(make_pair(name, make_pair(def, size))); } else { orb.push_back(make_pair(name, make_pair(res, size))); } sumsize += size; } cin >> k; int sw = -1, sa = -1, so = -1; vector<string> a, b, c; if (k == sumsize) { map<string, int> gladiator, sentry, physician; map<string, vector<string> > residents; for (int i = 0; i < k; ++i) { string name, type, home; int bonus; cin >> name >> type >> bonus >> home; residents[home].push_back(name); if (type == "gladiator") { gladiator[home] += bonus; } else if (type == "sentry") { sentry[home] += bonus; } else { physician[home] += bonus; } } { int m = -1, q = -1; for (int i = 0; i < weapon.size(); ++i) { int p = weapon[i].second.first; p += gladiator[weapon[i].first]; if (p > m) { m = p; q = i; } } sw = q; } { int m = -1, q = -1; for (int i = 0; i < armor.size(); ++i) { int p = armor[i].second.first; p += sentry[armor[i].first]; if (p > m) { m = p; q = i; } } sa = q; } { int m = -1, q = -1; for (int i = 0; i < orb.size(); ++i) { int p = orb[i].second.first; p += physician[orb[i].first]; if (p > m) { m = p; q = i; } } so = q; } a = residents[weapon[sw].first]; b = residents[armor[sa].first]; c = residents[orb[so].first]; } else { vector<pair<int, string> > gladiator, sentry, physician; for (int i = 0; i < k; ++i) { string name, type, home; int bonus; cin >> name >> type >> bonus >> home; if (type == "gladiator") { gladiator.push_back(make_pair(bonus, name)); } else if (type == "sentry") { sentry.push_back(make_pair(bonus, name)); } else { physician.push_back(make_pair(bonus, name)); } } sort(gladiator.rbegin(), gladiator.rend()); sort(sentry.rbegin(), sentry.rend()); sort(physician.rbegin(), physician.rend()); { int m = -1, q = -1; for (int i = 0; i < weapon.size(); ++i) { int p = weapon[i].second.first; for (int j = 0; j < gladiator.size() && j < weapon[i].second.second; ++j) { p += gladiator[j].first; } if (p > m) { m = p; q = i; } } sw = q; } { int m = -1, q = -1; for (int i = 0; i < armor.size(); ++i) { int p = armor[i].second.first; for (int j = 0; j < sentry.size() && j < armor[i].second.second; ++j) { p += sentry[j].first; } if (p > m) { m = p; q = i; } } sa = q; } { int m = -1, q = -1; for (int i = 0; i < orb.size(); ++i) { int p = orb[i].second.first; for (int j = 0; j < physician.size() && j < orb[i].second.second; ++j) { p += physician[j].first; } if (p > m) { m = p; q = i; } } so = q; } vector<string> d; for (int j = 0; j < gladiator.size(); ++j) { if (j < weapon[sw].second.second) { a.push_back(gladiator[j].second); } else { d.push_back(gladiator[j].second); } } for (int j = 0; j < sentry.size(); ++j) { if (j < armor[sa].second.second) { b.push_back(sentry[j].second); } else { d.push_back(sentry[j].second); } } for (int j = 0; j < physician.size(); ++j) { if (j < orb[so].second.second) { c.push_back(physician[j].second); } else { d.push_back(physician[j].second); } } while (a.size() < weapon[sw].second.second && d.size()) { a.push_back(d.back()); d.pop_back(); } while (b.size() < armor[sa].second.second && d.size()) { b.push_back(d.back()); d.pop_back(); } while (c.size() < orb[so].second.second && d.size()) { c.push_back(d.back()); d.pop_back(); } } cout << weapon[sw].first << " " << a.size(); for (int i = 0; i < a.size(); ++i) { cout << " " << a[i]; } cout << endl; cout << armor[sa].first << " " << b.size(); for (int i = 0; i < b.size(); ++i) { cout << " " << b[i]; } cout << endl; cout << orb[so].first << " " << c.size(); for (int i = 0; i < c.size(); ++i) { cout << " " << c[i]; } cout << endl; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; struct obj { string name; int cls; int p[3], sz; obj() { p[0] = p[1] = p[2] = 0; } } o[100]; struct res { string name; int type, bonus; int home; bool operator<(const res& x) const { return bonus > x.bonus; } } r[1000]; void output(int index, int n, int m) { int best = -1, ind = -1, i; vector<string> residents; for (i = 0; i < n; i++) if (o[i].cls == index && best < o[i].p[index]) { best = o[i].p[index]; ind = i; } cout << o[ind].name << " "; for (i = 0; i < m; i++) if (r[i].home == ind) residents.push_back(r[i].name); cout << residents.size(); for (i = 0; i < residents.size(); i++) cout << " " << residents[i]; cout << endl; } vector<int> taken[100]; bool u[1000]; void make(int index, int n, int m) { int i, j, k, best = -1, ind = -1; vector<int> ob; for (i = 0; i < n; i++) { if (o[i].cls == index) ob.push_back(i); } vector<int> rs; for (i = 0; i < m && rs.size() < 10; i++) if (r[i].type == index) rs.push_back(i); for (i = 0; i < ob.size(); i++) { int cur = o[ob[i]].p[index]; for (j = 0; j < o[ob[i]].sz && j < rs.size(); j++) cur += r[rs[j]].bonus; if (cur > best) { best = cur; ind = ob[i]; } } for (j = 0; j < o[ind].sz && j < rs.size(); j++) { taken[ind].push_back(rs[j]); o[ind].p[index] += r[rs[j]].bonus; r[rs[j]].home = ind; u[rs[j]] = true; } } int main() { int n, m, i, j, k, totalSize = 0; string s; cin >> n; for (i = 0; i < n; i++) { cin >> o[i].name; cin >> s; if (s == "weapon") o[i].cls = 0; if (s == "armor") o[i].cls = 1; if (s == "orb") o[i].cls = 2; cin >> o[i].p[0] >> o[i].p[1] >> o[i].p[2] >> o[i].sz; totalSize += o[i].sz; } cin >> m; for (i = 0; i < m; i++) { cin >> r[i].name; cin >> s; if (s == "gladiator") r[i].type = 0; if (s == "sentry") r[i].type = 1; if (s == "physician") r[i].type = 2; cin >> r[i].bonus; cin >> s; for (j = 0; j < n; j++) if (o[j].name == s) { r[i].home = j; break; } } if (totalSize == m) { for (i = 0; i < m; i++) o[r[i].home].p[r[i].type] += r[i].bonus; } else { sort(r, r + m); make(0, n, m); make(1, n, m); make(2, n, m); for (i = 0; i < m; i++) if (!u[i]) for (j = 0; j < n; j++) if (o[j].sz > taken[j].size()) { taken[j].push_back(i); r[i].home = j; break; } } output(0, n, m); output(1, n, m); output(2, n, m); return 0; }
#include <bits/stdc++.h> using namespace std; struct item { string name; int param; int size; item(const string& n, int p, int s) : name(n), param(p), size(s) {} bool operator<(const item& that) const { if (param == that.param) { return size < that.size; } else { return param < that.param; } } }; struct resident { string name; int bonus; string home; resident(const string& n, int b, const string& h) : name(n), bonus(b), home(h) {} bool operator>(const resident& that) const { return bonus > that.bonus; } }; int main() { int n; scanf("%d", &n); vector<item> items[3]; int total_size = 0; for (int i = 0; i < n; i++) { char name[20], type_[20]; int atk, def, res, size; scanf("%s %s %d %d %d %d", name, type_, &atk, &def, &res, &size); const string type(type_); if (type == "weapon") { items[0].push_back(item(name, atk, size)); } else if (type == "armor") { items[1].push_back(item(name, def, size)); } else { items[2].push_back(item(name, res, size)); } total_size += size; } int k; scanf("%d", &k); if (k == total_size) { map<string, pair<int, vector<string> > > ms[3]; for (int i = 0; i < 3; i++) { for (vector<item>::const_iterator it = items[i].begin(); it != items[i].end(); ++it) { ms[i].insert( make_pair(it->name, make_pair(it->param, vector<string>()))); } } for (int i = 0; i < k; i++) { char name[20], type_[20], home[20]; int bonus; scanf("%s %s %d %s", name, type_, &bonus, home); const string type(type_); if (ms[0].count(home)) { if (type == "gladiator") { ms[0][home].first += bonus; } ms[0][home].second.push_back(name); } else if (ms[1].count(home)) { if (type == "sentry") { ms[1][home].first += bonus; } ms[1][home].second.push_back(name); } else { if (type == "physician") { ms[2][home].first += bonus; } ms[2][home].second.push_back(name); } } int params[3] = {0, 0, 0}; string keys[3]; vector<string> ans[3]; for (int i = 0; i < 3; i++) { for (map<string, pair<int, vector<string> > >::const_iterator it = ms[i].begin(); it != ms[i].end(); ++it) { if (it->second.first > params[i]) { params[i] = it->second.first; keys[i] = it->first; ans[i] = it->second.second; } } } for (int i = 0; i < 3; i++) { printf("%s %lu", keys[i].c_str(), ans[i].size()); for (vector<string>::const_iterator it = ans[i].begin(); it != ans[i].end(); ++it) { printf(" %s", it->c_str()); } puts(""); } } else { vector<resident> rs[3]; for (int i = 0; i < k; i++) { char name[20], type_[20], home[20]; int bonus; scanf("%s %s %d %s", name, type_, &bonus, home); const string type(type_); if (type == "gladiator") { rs[0].push_back(resident(name, bonus, home)); } else if (type == "sentry") { rs[1].push_back(resident(name, bonus, home)); } else { rs[2].push_back(resident(name, bonus, home)); } } for (int i = 0; i < 3; i++) { sort(rs[i].begin(), rs[i].end(), greater<resident>()); } int max_params[3] = {-1, -1, -1}; vector<item>::const_iterator ans_iter[3]; vector<string> ans[3]; vector<resident>::const_iterator jts[3]; for (int i = 0; i < 3; i++) { for (vector<item>::const_iterator it = items[i].begin(); it != items[i].end(); ++it) { vector<resident>::const_iterator jt = rs[i].begin(); int param = it->param; vector<string> tmp; while (tmp.size() < it->size && jt != rs[i].end()) { param += jt->bonus; tmp.push_back(jt->name); ++jt; } if (param > max_params[i]) { max_params[i] = param; ans_iter[i] = it; ans[i] = tmp; jts[i] = jt; } } } for (int i = 0; i < 3; i++) { while (ans[i].size() < ans_iter[i]->size && (jts[0] != rs[0].end() || jts[1] != rs[1].end() || jts[2] != rs[2].end())) { if (jts[0] != rs[0].end()) { ans[i].push_back(jts[0]->name); ++jts[0]; } else if (jts[1] != rs[1].end()) { ans[i].push_back(jts[1]->name); ++jts[1]; } else if (jts[2] != rs[2].end()) { ans[i].push_back(jts[2]->name); ++jts[2]; } } printf("%s %lu", ans_iter[i]->name.c_str(), ans[i].size()); for (vector<string>::const_iterator it = ans[i].begin(); it != ans[i].end(); ++it) { printf(" %s", it->c_str()); } puts(""); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 2147483647; const long long LLINF = 9223372036854775807LL; struct item { string name, cl; int atk, def, res, size; int cr; }; struct res { string name, type, home; int bonus; bool operator<(const res &r2) const { if (bonus != r2.bonus) return bonus < r2.bonus; return name < r2.name; } }; const int maxn = 1010; item a[maxn]; res r[maxn]; int vatk[maxn], vdef[maxn], vres[maxn]; int catk = 0, cdef = 0, cres = 0; int main() { int n; scanf("%d", &n); int totalsz = 0; map<string, int> ind; for (int i = 0; i < n; ++i) { cin >> a[i].name >> a[i].cl; ind[a[i].name] = i; scanf("%d%d%d%d", &a[i].atk, &a[i].def, &a[i].res, &a[i].size); a[i].cr = 0; totalsz += a[i].size; } int k; scanf("%d", &k); for (int i = 0; i < k; ++i) { cin >> r[i].name >> r[i].type >> r[i].bonus >> r[i].home; a[ind[r[i].home]].cr++; } if (totalsz == k) { for (int i = 0; i < k; ++i) { if (r[i].type[0] == 'g') a[ind[r[i].home]].atk += r[i].bonus; if (r[i].type[0] == 's') a[ind[r[i].home]].def += r[i].bonus; if (r[i].type[0] == 'p') a[ind[r[i].home]].res += r[i].bonus; } int bi = -1; int bj = -1; int bk = -1; int vi = -1; int vj = -1; int vk = -1; for (int ww = 0; ww < n; ++ww) for (int aa = 0; aa < n; ++aa) for (int oo = 0; oo < n; ++oo) { if (a[ww].cl[0] != 'w') continue; if (a[aa].cl[0] != 'a') continue; if (a[oo].cl[0] != 'o') continue; int ci = a[ww].atk; int cj = a[aa].def; int ck = a[oo].res; if (make_pair(make_pair(ci, cj), ck) > make_pair(make_pair(vi, vj), vk)) { bi = ww; bj = aa; bk = oo; vi = ci; vj = cj; vk = ck; } } cout << a[bi].name << " " << a[bi].cr; for (int i = 0; i < k; ++i) if (r[i].home == a[bi].name) cout << " " << r[i].name; cout << endl; cout << a[bj].name << " " << a[bj].cr; for (int i = 0; i < k; ++i) if (r[i].home == a[bj].name) cout << " " << r[i].name; cout << endl; cout << a[bk].name << " " << a[bk].cr; for (int i = 0; i < k; ++i) if (r[i].home == a[bk].name) cout << " " << r[i].name; cout << endl; } else { set<res> s1, s2, s3; for (int i = 0; i < k; ++i) if (r[i].type[0] == 'g') s1.insert(r[i]), vatk[catk++] = r[i].bonus; else if (r[i].type[0] == 's') s2.insert(r[i]), vdef[cdef++] = r[i].bonus; else if (r[i].type[0] == 'p') s3.insert(r[i]), vres[cres++] = r[i].bonus; sort(vatk, vatk + catk); reverse(vatk, vatk + catk); sort(vdef, vdef + cdef); reverse(vdef, vdef + cdef); sort(vres, vres + cres); reverse(vres, vres + cres); for (int i = 1; i < catk; ++i) vatk[i] += vatk[i - 1]; for (int i = 1; i < cdef; ++i) vdef[i] += vdef[i - 1]; for (int i = 1; i < cres; ++i) vres[i] += vres[i - 1]; int bi = -1; int bj = -1; int bk = -1; int vi = -1; int vj = -1; int vk = -1; for (int ww = 0; ww < n; ++ww) for (int aa = 0; aa < n; ++aa) for (int oo = 0; oo < n; ++oo) { if (a[ww].cl[0] != 'w') continue; if (a[aa].cl[0] != 'a') continue; if (a[oo].cl[0] != 'o') continue; int ci = a[ww].atk + (catk == 0 ? 0 : vatk[min(catk, a[ww].size) - 1]); int cj = a[aa].def + (cdef == 0 ? 0 : vdef[min(cdef, a[aa].size) - 1]); int ck = a[oo].res + (cres == 0 ? 0 : vres[min(cres, a[oo].size) - 1]); if (make_pair(make_pair(ci, cj), ck) > make_pair(make_pair(vi, vj), vk)) { bi = ww; bj = aa; bk = oo; vi = ci; vj = cj; vk = ck; } } vector<string> a1, a2, a3; for (int i = 0; i < a[bi].size && int((s1).size()); ++i) a1.push_back(s1.rbegin()->name), s1.erase(*s1.rbegin()); for (int i = 0; i < a[bj].size && int((s2).size()); ++i) a2.push_back(s2.rbegin()->name), s2.erase(*s2.rbegin()); for (int i = 0; i < a[bk].size && int((s3).size()); ++i) a3.push_back(s3.rbegin()->name), s3.erase(*s3.rbegin()); queue<string> q; while (int((s1).size())) q.push(s1.begin()->name), s1.erase(s1.begin()); while (int((s2).size())) q.push(s2.begin()->name), s2.erase(s2.begin()); while (int((s3).size())) q.push(s3.begin()->name), s3.erase(s3.begin()); while (!q.empty() && int((a1).size()) < a[bi].size) a1.push_back(q.front()), q.pop(); while (!q.empty() && int((a2).size()) < a[bj].size) a2.push_back(q.front()), q.pop(); while (!q.empty() && int((a3).size()) < a[bk].size) a3.push_back(q.front()), q.pop(); cout << a[bi].name << " " << int((a1).size()); for (int i = 0; i < int((a1).size()); ++i) cout << " " << a1[i]; cout << endl; cout << a[bj].name << " " << int((a2).size()); for (int i = 0; i < int((a2).size()); ++i) cout << " " << a2[i]; cout << endl; cout << a[bk].name << " " << int((a3).size()); for (int i = 0; i < int((a3).size()); ++i) cout << " " << a3[i]; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void require(bool cond, const string& message = "Runtime error") { if (!cond) { cerr << message << endl; assert(false); } } void readData() {} struct Item { string name; int val, size; }; struct Resident { string name; int bonus; }; bool operator<(const Resident& a, const Resident& b) { return a.bonus > b.bonus; } int pack(const Item& item, const vector<Resident>& rss) { int res = item.val; for (int i = 0; i < int(min(item.size, (int)rss.size())); ++i) { res += rss[i].bonus; } return res; } void solve() { int n; cin >> n; map<string, vector<Item> > items; map<string, int> cnt; map<string, int> add; map<string, string> clz; map<string, vector<Resident> > homes; for (int i = 0; i < int(n); ++i) { string name, clazz; int atk, def, res, size; cin >> name >> clazz >> atk >> def >> res >> size; Item item = {name, atk, size}; if (clazz == "armor") { item.val = def; } else if (clazz == "orb") { item.val = res; } else { assert(clazz == "weapon"); } items[clazz].push_back(item); clz[name] = clazz; } set<string> allres; int k; cin >> k; map<string, vector<Resident> > ress; for (int i = 0; i < int(k); ++i) { string name, clazz, home; int bonus; cin >> name >> clazz >> bonus >> home; ++cnt[home]; string cl = clazz; if (cl == "gladiator") clazz = "weapon"; else if (cl == "sentry") clazz = "armor"; else if (cl == "physician") clazz = "orb"; else { assert(false); } allres.insert(name); Resident res = {name, bonus}; ress[clazz].push_back(res); homes[home].push_back(res); if (clz[home] == clazz) { add[home] += bonus; } } bool full = true; for (map<string, vector<Item> >::iterator it = items.begin(); it != items.end(); ++it) { const vector<Item>& its = it->second; for (int i = 0; i < int(its.size()); ++i) { const Item& item = its[i]; if (cnt[item.name] < item.size) { full = false; break; } } if (!full) { break; } } vector<string> cnames; cnames.push_back("weapon"); cnames.push_back("armor"); cnames.push_back("orb"); if (full) { for (int it = 0; it < int(cnames.size()); ++it) { string clazz = cnames[it]; const vector<Item>& its = items[clazz]; Item nb = its[0]; for (int i = 0; i < int(its.size()); ++i) { const Item& item = its[i]; if (item.val + add[item.name] > nb.val + add[nb.name]) nb = item; } cout << nb.name << " " << homes[nb.name].size(); const vector<Resident>& rss = homes[nb.name]; for (int i = 0; i < int(rss.size()); ++i) cout << " " << rss[i].name; cout << endl; } return; } for (int it = 0; it < int(cnames.size()); ++it) { sort(ress[cnames[it]].begin(), ress[cnames[it]].end()); } map<string, Item> best; map<string, vector<string> > bres; for (int it = 0; it < int(cnames.size()); ++it) { string clazz = cnames[it]; vector<Item>& its = items[clazz]; int maxi = 0; int maxv = -1; const vector<Resident>& rs = ress[clazz]; for (int i = 0; i < int(its.size()); ++i) { const Item& item = its[i]; int pk = pack(item, rs); if (pk > maxv) { maxv = pk; maxi = i; } } Item item = its[maxi]; best[clazz] = item; vector<string> ans; for (int i = 0; i < int(min(item.size, (int)rs.size())); ++i) { item.val += rs[i].bonus; ans.push_back(rs[i].name); allres.erase(rs[i].name); } bres[clazz] = ans; } queue<string> left; for (set<string>::iterator it = allres.begin(); it != allres.end(); ++it) { left.push(*it); } for (int it = 0; it < int(cnames.size()); ++it) { string clazz = cnames[it]; while (!left.empty() && bres[clazz].size() < best[clazz].size) { bres[clazz].push_back(left.front()); left.pop(); } } for (int it = 0; it < int(cnames.size()); ++it) { string clazz = cnames[it]; const vector<string>& rss = bres[clazz]; cout << best[clazz].name << " " << rss.size(); for (int i = 0; i < int(rss.size()); ++i) cout << " " << rss[i]; cout << endl; } } int main() { ios_base::sync_with_stdio(false); readData(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; struct Item { string s; int a[3]; int k, sz; }; struct Resi { string s, fa; int k, v; }; int n, m; Item a[1005]; Resi b[1005]; map<string, int> gg1, gg2, f; vector<int> v[1005]; bool cmp(const int& l, const int& r) { return b[l].v > b[r].v; } void gao0() { for (int j = 0; j < m; ++j) { int k = f[b[j].fa]; a[k].a[b[j].k] += b[j].v; v[k].push_back(j); } for (int c = 0; c < 3; ++c) { int mx = -1, mi; for (int i = 0; i < n; ++i) if (a[i].k == c) { if (a[i].a[c] > mx) { mx = a[i].a[c]; mi = i; } } printf("%s %d", a[mi].s.c_str(), v[mi].size()); for (vector<int>::iterator it = v[mi].begin(); it != v[mi].end(); ++it) printf(" %s", b[*it].s.c_str()); puts(""); } } int pi[3], ci[3]; vector<int> pr[3]; bool vis[1005]; void gao1(int sumsz) { int gao = 0; memset(vis, 0, sizeof(vis)); for (int c = 0; c < 3; ++c) { for (int i = 0; i < n; ++i) v[i].clear(); for (int j = 0; j < m; ++j) if (b[j].k == c) for (int i = 0; i < n; ++i) if (a[i].k == c) v[i].push_back(j); int mx = -1, mi; for (int i = 0; i < n; ++i) if (a[i].k == c) { sort(v[i].begin(), v[i].end(), cmp); for (int j = 0; j < a[i].sz && j < v[i].size(); ++j) a[i].a[c] += b[v[i][j]].v; if (a[i].a[c] > mx) { mx = a[i].a[c]; mi = i; } } int cc = a[mi].sz; if (cc > v[mi].size()) cc = v[mi].size(); pi[c] = mi; ci[c] = cc; for (int j = 0; j < a[mi].sz && j < v[mi].size(); ++j) { vis[v[mi][j]] = 1; pr[c].push_back(v[mi][j]); } sumsz -= a[mi].sz; } sumsz = max(0, m - sumsz); for (int c = 0; c < 3; ++c) for (int j = 0; j < m && pr[c].size() < a[pi[c]].sz && sumsz; ++j) if (!vis[j]) { pr[c].push_back(j); vis[j] = 1; --sumsz; ++ci[c]; } for (int c = 0; c < 3; ++c) { printf("%s %d", a[pi[c]].s.c_str(), ci[c]); for (vector<int>::iterator it = pr[c].begin(); it != pr[c].end(); ++it) printf(" %s", b[*it].s.c_str()); puts(""); } } int main() { gg1["weapon"] = 0; gg1["armor"] = 1; gg1["orb"] = 2; gg2["gladiator"] = 0; gg2["sentry"] = 1; gg2["physician"] = 2; char str[100]; int sumsz = 0; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%s", str); f[str] = i; a[i].s = str; scanf("%s", str); a[i].k = gg1[str]; for (int j = 0; j < 3; ++j) scanf("%d", &a[i].a[j]); scanf("%d", &a[i].sz); sumsz += a[i].sz; } scanf("%d", &m); for (int i = 0; i < m; ++i) { scanf("%s", str); b[i].s = str; scanf("%s", str); b[i].k = gg2[str]; scanf("%d", &b[i].v); scanf("%s", str); b[i].fa = str; } if (sumsz == m) gao0(); else gao1(sumsz); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1999999999; const double pi = acos(-1.0); const double eps = 1e-9; char gc() { char c; while (isspace(c = getchar())) ; return c; } int gs(char* s) { fgets(s, 2000000, stdin); int l = strlen(s); while (l && isspace(s[l - 1])) s[--l] = 0; return l; } string weps[100]; int type[100]; int power[100], cap[100]; vector<int> resid[100], tresid[100]; string pers[1000]; int ptype[1000]; int ppower[1000]; bool resu[1000]; map<string, int> wepm; int gettype(string t) { char c = t[0]; if (c == 'w') return 0; if (c == 'a') return 1; if (c == 'o') return 2; if (c == 'g') return 0; if (c == 's') return 1; if (c == 'p') return 2; return -1; } int main() { ios::sync_with_stdio(false); cout << fixed << setprecision(15); int n, m; cin >> n; string s, t; int a[3]; int totcap = 0; for (int i = 0; i < n; i++) { cin >> s >> t >> a[0] >> a[1] >> a[2] >> cap[i]; totcap += cap[i]; wepm[s] = i; weps[i] = s; type[i] = gettype(t); power[i] = a[type[i]]; } cin >> m; for (int i = 0; i < m; i++) { cin >> pers[i] >> t >> ppower[i] >> s; ptype[i] = gettype(t); int a = wepm[s]; resid[a].push_back(i); } if (totcap == m) { for (int t = 0; t < 3; t++) { vector<pair<int, int> > v; for (int i = 0; i < n; i++) if (type[i] == t) { int s = power[i]; for (int j = 0; j < ((int)(resid[i]).size()); j++) if (ptype[resid[i][j]] == t) s += ppower[resid[i][j]]; v.push_back(make_pair(s, i)); } int i = max_element((v).begin(), (v).end())->second; cout << weps[i] << ' ' << resid[i].size(); for (int j = 0; j < ((int)(resid[i]).size()); j++) cout << ' ' << pers[resid[i][j]]; cout << ('\n'); } return 0; } vector<int> ans; for (int t = 0; t < 3; t++) { vector<pair<int, int> > pows; for (int i = 0; i < m; i++) if (ptype[i] == t) pows.push_back(make_pair(ppower[i], i)); sort((pows).begin(), (pows).end(), greater<pair<int, int> >()); vector<pair<int, int> > v; for (int i = 0; i < n; i++) if (type[i] == t) { int s = power[i]; for (int j = 0; j < cap[i] && j < ((int)(pows).size()); j++) { s += pows[j].first; tresid[i].push_back(pows[j].second); } v.push_back(make_pair(s, i)); } int i = max_element((v).begin(), (v).end())->second; ans.push_back(i); for (int j = 0; j < ((int)(tresid[i]).size()); j++) resu[tresid[i][j]] = true; } int pos = 0; for (int i = 0; i < m && pos < 3; i++) { if (resu[i] == true) continue; while (pos < 3 && ((int)(tresid[ans[pos]]).size()) == cap[ans[pos]]) pos++; if (pos == 3) break; tresid[ans[pos]].push_back(i); } for (int t = 0; t < 3; t++) { int i = ans[t]; cout << weps[i] << ' ' << tresid[i].size(); for (int j = 0; j < ((int)(tresid[i]).size()); j++) cout << ' ' << pers[tresid[i][j]]; cout << ('\n'); } return 0; }
#include <bits/stdc++.h> using namespace std; struct resident { string name; int type, bonus, home, id; void read(int i); }; resident a[1100]; struct sub { string name; int type; int atk, def, res, size, id; vector<int> a; void read(int i); void print() { cout << name << " " << a.size() << " "; for (int i = 0; i < a.size(); i++) cout << ::a[a[i]].name << " "; cout << endl; } }; map<string, int> R; map<string, int> S; sub b[110]; int n, m; int get(int u, int mode) { int add = 0; for (int i = 0; i < b[u].a.size(); i++) { int v = b[u].a[i]; if (mode == a[v].type) add += a[v].bonus; } return add; } bool comp(int x, int y) { return a[x].bonus > a[y].bonus; } vector<int> order; void resident::read(int i) { cin >> name; string s; cin >> s; if (s == "gladiator") type = 1; else if (s == "sentry") type = 2; else type = 3; cin >> bonus; cin >> s; home = S[s]; R[name] = i; id = i; b[home].a.push_back(i); } void sub::read(int i) { cin >> name; string s; cin >> s; if (s == "weapon") type = 1; else if (s == "armor") type = 2; else type = 3; cin >> atk >> def >> res >> size; S[name] = i; id = i; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) b[i].read(i); scanf("%d", &m); for (int i = 1; i <= m; i++) a[i].read(i); bool good = true; for (int i = 1; i <= n; i++) if (b[i].size != b[i].a.size()) good = false; if (good) { int x = -1, y = -1, z = -1, u = -1, v = -1, w = -1; for (int i = 1; i <= n; i++) if (b[i].type == 1) for (int j = 1; j <= n; j++) if (b[j].type == 2) for (int k = 1; k <= n; k++) if (b[k].type == 3) { int s1 = b[i].atk + get(i, 1), s2 = b[j].def + get(j, 2), s3 = b[k].res + get(k, 3); if (make_pair(s1, make_pair(s2, s3)) > make_pair(x, make_pair(y, z))) x = s1, y = s2, z = s3, u = i, v = j, w = k; } b[u].print(); b[v].print(); b[w].print(); return 0; } for (int i = 1; i <= n; i++) { order.clear(); for (int j = 1; j <= m; j++) if (a[j].type == b[i].type) order.push_back(j); sort(order.begin(), order.end(), comp); b[i].a.clear(); for (int j = 0; j < min((int)order.size(), b[i].size); j++) b[i].a.push_back(order[j]); } int x = -1, y = -1, z = -1, u = -1, v = -1, w = -1; for (int i = 1; i <= n; i++) if (b[i].type == 1) for (int j = 1; j <= n; j++) if (b[j].type == 2) for (int k = 1; k <= n; k++) if (b[k].type == 3) { int s1 = b[i].atk + get(i, 1), s2 = b[j].def + get(j, 2), s3 = b[k].res + get(k, 3); if (make_pair(s1, make_pair(s2, s3)) > make_pair(x, make_pair(y, z))) x = s1, y = s2, z = s3, u = i, v = j, w = k; } for (int i = 0; i < b[u].a.size(); i++) { int t = b[u].a[i]; R.erase(a[t].name); } for (int i = 0; i < b[v].a.size(); i++) { int t = b[v].a[i]; R.erase(a[t].name); } for (int i = 0; i < b[w].a.size(); i++) { int t = b[w].a[i]; R.erase(a[t].name); } for (map<string, int>::iterator it = R.begin(); it != R.end(); it++) { int t = it->second; if (b[u].size > b[u].a.size()) b[u].a.push_back(t); else if (b[v].size > b[v].a.size()) b[v].a.push_back(t); else if (b[w].size > b[w].a.size()) b[w].a.push_back(t); } b[u].print(); b[v].print(); b[w].print(); return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int f = 1, x = 0; char ch; do { ch = getchar(); if (ch == '-') f = -1; } while (ch < '0' || ch > '9'); do { x = x * 10 + ch - '0'; ch = getchar(); } while (ch >= '0' && ch <= '9'); return f * x; } const int N = 1e3; int n, m; struct Eqp { string name; int atk, def, spd, size; vector<string> in; }; vector<Eqp> atk, def, spd; struct Props { string name; int up; }; vector<Props> patk, pdef, pspd; int tot; int sum[N + 1]; vector<string> ans[4]; int nowatk, nowdef, nowspd; inline bool compare1(Props x, Props y) { return x.up > y.up; } inline bool compare2(Eqp x, Eqp y) { return x.atk > y.atk; } inline bool compare3(Eqp x, Eqp y) { return x.def > y.def; } inline bool compare4(Eqp x, Eqp y) { return x.spd > y.spd; } int main() { n = read(); for (int i = 1; i <= n; i++) { string name, type; cin >> name >> type; if (type == "weapon") { atk.push_back(Eqp{name, read(), read(), read(), read()}); tot += atk[atk.size() - 1].size; } if (type == "armor") { def.push_back(Eqp{name, read(), read(), read(), read()}); tot += def[def.size() - 1].size; } if (type == "orb") { spd.push_back(Eqp{name, read(), read(), read(), read()}); tot += spd[spd.size() - 1].size; } } m = read(); for (int i = 1; i <= m; i++) { string name, type, in; cin >> name >> type; int up = read(); if (type == "gladiator") patk.push_back(Props{name, up}); if (type == "sentry") pdef.push_back(Props{name, up}); if (type == "physician") pspd.push_back(Props{name, up}); cin >> in; if (tot == m) { for (int j = 0; j < atk.size(); j++) { if (atk[j].name == in) { atk[j].in.push_back(name); if (type == "gladiator") atk[j].atk += up; } } for (int j = 0; j < def.size(); j++) { if (def[j].name == in) { def[j].in.push_back(name); if (type == "sentry") def[j].def += up; } } for (int j = 0; j < spd.size(); j++) { if (spd[j].name == in) { spd[j].in.push_back(name); if (type == "physician") spd[j].spd += up; } } } } if (tot == m) { sort(atk.begin(), atk.end(), compare2); sort(def.begin(), def.end(), compare3); sort(spd.begin(), spd.end(), compare4); cout << atk[0].name; printf(" %d ", atk[0].in.size()); for (int i = 0; i < atk[0].in.size(); i++) cout << atk[0].in[i] << " "; printf("\n"); cout << def[0].name; printf(" %d ", def[0].in.size()); for (int i = 0; i < def[0].in.size(); i++) cout << def[0].in[i] << " "; printf("\n"); cout << spd[0].name; printf(" %d ", spd[0].in.size()); for (int i = 0; i < spd[0].in.size(); i++) cout << spd[0].in[i] << " "; printf("\n"); return 0; } sort(patk.begin(), patk.end(), compare1); for (int i = 1; i <= patk.size(); i++) sum[i] = sum[i - 1] + patk[i - 1].up; for (int i = 0; i < atk.size(); i++) atk[i].atk += sum[min(atk[i].size, (int)patk.size())]; sort(atk.begin(), atk.end(), compare2); sort(pdef.begin(), pdef.end(), compare1); for (int i = 1; i <= pdef.size(); i++) sum[i] = sum[i - 1] + pdef[i - 1].up; for (int i = 0; i < def.size(); i++) def[i].def += sum[min(def[i].size, (int)pdef.size())]; sort(def.begin(), def.end(), compare3); sort(pspd.begin(), pspd.end(), compare1); for (int i = 1; i <= pspd.size(); i++) sum[i] = sum[i - 1] + pspd[i - 1].up; for (int i = 0; i < spd.size(); i++) spd[i].spd += sum[min(spd[i].size, (int)pspd.size())]; sort(spd.begin(), spd.end(), compare4); nowatk = min(atk[0].size, (int)patk.size()); nowdef = min(def[0].size, (int)pdef.size()); nowspd = min(spd[0].size, (int)pspd.size()); for (int i = 0; i < nowatk; i++) ans[1].push_back(patk[i].name); for (int i = 0; i < nowdef; i++) ans[2].push_back(pdef[i].name); for (int i = 0; i < nowspd; i++) ans[3].push_back(pspd[i].name); if (ans[1].size() < atk[0].size) { for (; nowatk < patk.size() && ans[1].size() < atk[0].size; nowatk++) ans[1].push_back(patk[nowatk].name); if (ans[1].size() < atk[0].size) { for (; nowdef < pdef.size() && ans[1].size() < atk[0].size; nowdef++) ans[1].push_back(pdef[nowdef].name); if (ans[1].size() < atk[0].size) for (; nowspd < pspd.size() && ans[1].size() < atk[0].size; nowspd++) ans[1].push_back(pspd[nowspd].name); } } if (ans[2].size() < def[0].size) { for (; nowatk < patk.size() && ans[2].size() < def[0].size; nowatk++) ans[2].push_back(patk[nowatk].name); if (ans[2].size() < def[0].size) { for (; nowdef < pdef.size() && ans[2].size() < def[0].size; nowdef++) ans[2].push_back(pdef[nowdef].name); if (ans[2].size() < def[0].size) for (; nowspd < pspd.size() && ans[2].size() < def[0].size; nowspd++) ans[2].push_back(pspd[nowspd].name); } } if (ans[3].size() < spd[0].size) { for (; nowatk < patk.size() && ans[3].size() < spd[0].size; nowatk++) ans[3].push_back(patk[nowatk].name); if (ans[3].size() < spd[0].size) { for (; nowdef < pdef.size() && ans[3].size() < spd[0].size; nowdef++) ans[3].push_back(pdef[nowdef].name); if (ans[3].size() < spd[0].size) for (; nowspd < pspd.size() && ans[3].size() < spd[0].size; nowspd++) ans[3].push_back(pspd[nowspd].name); } } cout << atk[0].name; printf(" %d ", ans[1].size()); for (int i = 0; i < ans[1].size(); i++) cout << ans[1][i] << " "; printf("\n"); cout << def[0].name; printf(" %d ", ans[2].size()); for (int i = 0; i < ans[2].size(); i++) cout << ans[2][i] << " "; printf("\n"); cout << spd[0].name; printf(" %d ", ans[3].size()); for (int i = 0; i < ans[3].size(); i++) cout << ans[3][i] << " "; printf("\n"); }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1E-15; const int inf = int(1e9) + 7; const int maxN = 1000 + 5; map<string, int> mp1, mp2, mp3; struct node { string name; int kind; int value, belong; }; bool operator<(const node &a1, const node &a2) { return (a1.value > a2.value); } node rs[maxN]; string name[maxN]; int kind[maxN], value[maxN], add_value[maxN], size[maxN]; int n, m; void init() { mp1["weapon"] = 0; mp1["armor"] = 1; mp1["orb"] = 2; mp2["gladiator"] = 0; mp2["sentry"] = 1; mp2["physician"] = 2; } void work() { cin >> n; string s1, s2; int p[3], totSize = 0; for (int(i) = 0; (i) < (n); ++(i)) { cin >> s1 >> s2; name[i] = s1; mp3[s1] = i; kind[i] = mp1[s2]; for (int(j) = 0; (j) < (3); ++(j)) cin >> p[j]; value[i] = p[kind[i]]; cin >> size[i]; totSize += size[i]; } cin >> m; for (int(i) = 0; (i) < (m); ++(i)) { cin >> s1 >> s2; rs[i].name = s1; rs[i].kind = mp2[s2]; cin >> rs[i].value; cin >> s1; rs[i].belong = mp3[s1]; if (rs[i].kind == kind[rs[i].belong]) { add_value[rs[i].belong] += rs[i].value; } } if (totSize == m) { for (int(k) = 0; (k) < (3); ++(k)) { int mx = -1, j; for (int(i) = 0; (i) < (n); ++(i)) if (kind[i] == k) { if (value[i] + add_value[i] > mx) { mx = value[i] + add_value[i]; j = i; } } cout << name[j]; int nr = 0; for (int(i) = 0; (i) < (m); ++(i)) if (rs[i].belong == j) nr++; cout << " " << nr; for (int(i) = 0; (i) < (m); ++(i)) if (rs[i].belong == j) cout << " " << rs[i].name; cout << endl; } } else { int ans[3]; sort(rs, rs + m); for (int(k) = 0; (k) < (3); ++(k)) { int mx = -1, j; for (int(i) = 0; (i) < (n); ++(i)) if (kind[i] == k) { int tmp = value[i]; int i1 = -1; for (int(z) = 0; (z) < (size[i]); ++(z)) { i1++; while (i1 < m) { if (rs[i1].kind == k) break; i1++; } if (i1 >= m) break; rs[i1].belong = i; tmp += rs[i1].value; } if (tmp > mx) { mx = tmp; ans[k] = i; } } } for (int(i) = 0; (i) < (m); ++(i)) rs[i].belong = -1; for (int(k) = 0; (k) < (3); ++(k)) { int nr = 0; for (int(i) = 0; (i) < (m); ++(i)) if (rs[i].kind == k && nr < size[ans[k]]) { nr++; rs[i].belong = ans[k]; } } for (int(k) = 0; (k) < (3); ++(k)) { int j = ans[k]; cout << name[j]; int nr = 0; for (int(i) = 0; (i) < (m); ++(i)) if (rs[i].belong == j) nr++; for (int(i) = 0; (i) < (m); ++(i)) if (rs[i].belong == -1 && nr < size[j]) { nr++; rs[i].belong = j; } cout << " " << nr; for (int(i) = 0; (i) < (m); ++(i)) if (rs[i].belong == j) { cout << " " << rs[i].name; } cout << endl; } } } int main() { init(); work(); return 0; }
#include <bits/stdc++.h> using namespace std; struct resident { string name, type, home; int idx, bonus; resident(int n1) { idx = n1; cin >> name >> type >> bonus >> home; } }; struct item { string name, type; int atk, def, res; int size; item() { cin >> name >> type >> atk >> def >> res >> size; } int getScore(vector<resident> v) { int score = 0; if (type == "weapon") score = atk; else if (type == "armor") score = def; else score = res; for (int i = 0; i < v.size(); i++) { if (type == "weapon" && v[i].type == "gladiator") score += v[i].bonus; else if (type == "armor" && v[i].type == "sentry") score += v[i].bonus; else if (type == "orb" && v[i].type == "physician") score += v[i].bonus; } return score; } void show(vector<resident> v) { cout << name << " " << v.size(); for (int i = 0; i < v.size(); i++) cout << " " << v[i].name; cout << endl; } }; vector<item> items; vector<resident> residents; bool orden(int a, int b) { return residents[a].bonus > residents[b].bonus; } int main() { int n, k; while (cin >> n) { items.clear(); residents.clear(); vector<int> W, A, O; int total = 0; map<string, int> itemIndex; for (int i = 0; i < n; i++) { items.push_back(item()); if (items[i].type == "weapon") W.push_back(i); else if (items[i].type == "armor") A.push_back(i); else O.push_back(i); itemIndex[items[i].name] = i; total += items[i].size; } vector<vector<resident> > adj(n); cin >> k; vector<int> G, S, P; for (int i = 0; i < k; i++) { residents.push_back(resident(i)); if (residents[i].type == "gladiator") G.push_back(i); else if (residents[i].type == "sentry") S.push_back(i); else P.push_back(i); adj[itemIndex[residents[i].home]].push_back(residents[i]); } if (total == k) { int wIndex = -1, aIndex = -1, oIndex = -1; int maxAtk = -1, maxDef = -1, maxRes = -1; for (int i = 0; i < W.size(); i++) { int atk = items[W[i]].getScore(adj[W[i]]); for (int j = 0; j < A.size(); j++) { int def = items[A[j]].getScore(adj[A[j]]); for (int k = 0; k < O.size(); k++) { int res = items[O[k]].getScore(adj[O[k]]); if (atk > maxAtk || (atk == maxAtk && def > maxDef) || (atk == maxAtk && def == maxDef && res > maxRes)) { wIndex = W[i]; aIndex = A[j]; oIndex = O[k]; maxAtk = atk; maxDef = def; maxRes = res; } } } } items[wIndex].show(adj[wIndex]); items[aIndex].show(adj[aIndex]); items[oIndex].show(adj[oIndex]); } else { sort(G.begin(), G.end(), orden); sort(S.begin(), S.end(), orden); sort(P.begin(), P.end(), orden); int wIndex = -1, aIndex = -1, oIndex = -1; int maxAtk = -1, maxDef = -1, maxRes = -1; vector<resident> g, s, p; for (int i = 0; i < W.size(); i++) { vector<resident> tmp; for (int j = 0; j < min((int)G.size(), items[W[i]].size); j++) tmp.push_back(residents[G[j]]); int score = items[W[i]].getScore(tmp); if (score > maxAtk) { wIndex = W[i]; maxAtk = score; g = tmp; } } for (int i = 0; i < A.size(); i++) { vector<resident> tmp; for (int j = 0; j < min((int)S.size(), items[A[i]].size); j++) tmp.push_back(residents[S[j]]); int score = items[A[i]].getScore(tmp); if (score > maxDef) { aIndex = A[i]; maxDef = score; s = tmp; } } for (int i = 0; i < O.size(); i++) { vector<resident> tmp; for (int j = 0; j < min((int)P.size(), items[O[i]].size); j++) tmp.push_back(residents[P[j]]); int score = items[O[i]].getScore(tmp); if (score > maxRes) { oIndex = O[i]; maxRes = score; p = tmp; } } vector<bool> used(k, 0); for (int i = 0; i < g.size(); i++) used[g[i].idx] = 1; for (int i = 0; i < s.size(); i++) used[s[i].idx] = 1; for (int i = 0; i < p.size(); i++) used[p[i].idx] = 1; for (int i = 0; i < k; i++) { if (!used[i]) { if (items[wIndex].size > g.size()) g.push_back(residents[i]); else if (items[aIndex].size > s.size()) s.push_back(residents[i]); else if (items[oIndex].size > p.size()) p.push_back(residents[i]); } } items[wIndex].show(g); items[aIndex].show(s); items[oIndex].show(p); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct B { int k; string s, s2; } ee; int pt1(B a, B b) { return a.k > b.k; } vector<B> f[3]; struct A { int s, n; vector<string> da; }; int pt2(A a, A b) { return a.s > b.s; } map<string, A> mp[3]; string nam[3] = {"weapon", "armor", "orb"}; map<string, int> id; struct C { string nm; A a; } ans[3]; map<string, int> fk; int main() { id["gladiator"] = 0; id["sentry"] = 1; id["physician"] = 2; int n; while (cin >> n) { for (int i = 0; i < 3; i++) mp[i].clear(); int ind = 0; for (int i = 0; i < n; i++) { string nm, cs; int a[3]; int nn; cin >> nm >> cs >> a[0] >> a[1] >> a[2] >> nn; ind += nn; A e; for (int i = 0; i < 3; i++) { if (cs == nam[i]) { e.n = nn; e.s = a[i]; mp[i][nm] = e; } } } int m; cin >> m; map<string, A>::iterator it; if (ind == m) { for (int i = 0; i < m; i++) { int k; string a, b, c; cin >> a >> b >> k >> c; for (int z = 0; z < 3; z++) { it = mp[z].find(c); if (it == mp[z].end()) continue; else { it->second.da.push_back(a); if (id[b] == z) it->second.s += k; } } } } else { while (m--) { int k; string nm1, nm2, nm3; cin >> nm1 >> nm2 >> k >> nm3; ee.s = nm1; ee.k = k; f[id[nm2]].push_back(ee); fk[nm1] = 1; } for (int i = 0; i < 3; i++) sort(f[i].begin(), f[i].end(), pt1); for (int i = 0; i < 3; i++) { string danm; for (it = mp[i].begin(); it != mp[i].end(); it++) for (int q = 0; q < min(it->second.n, (int)f[i].size()); q++) it->second.s += f[i][q].k, it->second.da.push_back(f[i][q].s); } } for (int i = 0; i < 3; i++) { int da = -1e9; for (it = mp[i].begin(); it != mp[i].end(); it++) { if (it->second.s > da) { da = it->second.s; ans[i].nm = it->first; ans[i].a = it->second; } } } for (int i = 0; i < 3; i++) { for (int q = 0; q < ans[i].a.da.size(); q++) fk.erase(ans[i].a.da[q]); } for (int i = 0; i < 3; i++) { while (fk.size() && ans[i].a.da.size() != ans[i].a.n) ans[i].a.da.push_back(fk.begin()->first), fk.erase(fk.begin()->first); cout << ans[i].nm << ' ' << ans[i].a.da.size(); for (int q = 0; q < ans[i].a.da.size(); q++) cout << ' ' << ans[i].a.da[q]; cout << endl; } } }