signedmain(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, m; std::cin >> n >> m;
int t = n - (m - 1); int a = ((n % m == 0 ? 0 : 1) + n / m), b = n / m; i64 max = 1LL * t * (t - 1) / 2; i64 min = 1LL * a * (a - 1) / 2 * (n % m) + 1LL * (m - n % m) * (b - 1) * b / 2; std::cout << min << " " << max << "\n"; return0; }
std::vector<int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; }
int min = *min_element(a.begin(), a.end());
bool ok = true; std::vector<int> b = a; std::sort(b.begin(), b.end()); for (int i = 0; i < n; i++) { if (b[i] != a[i] && a[i] % min != 0) { ok = false; } }
std::vector<std::pair<int, int>> a; for (int i = 0; i < n; i++) { int x; std::cin >> x; a.emplace_back(x, 1); } for (int i = 0; i < n; i++) { int x; std::cin >> x; a.emplace_back(x, -1); }
std::sort(a.begin(), a.end());
std::vector<int> c(n); for (int i = 0; i < n; i++) { std::cin >> c[i]; } std::sort(c.rbegin(), c.rend());
std::vector<int> stk, len; for (auto [x, t] : a) { if (t == 1) { stk.emplace_back(x); } else { assert(!stk.empty()); len.emplace_back(x - stk.back()); stk.pop_back(); } } std::sort(len.begin(), len.end());
i64 ans = 0; for (int i = 0; i < n; i++) { ans += 1LL * c[i] * len[i]; } std::cout << ans << "\n"; }
bool ok = s[0] == 'A'; int ans = std::count(s.begin(), s.end(), 'A'); int A = 0, n = s.size(); std::vector<int> a; for (int i = 0; i < n; i++) { if (s[i] == 'B') { if (A) { a.push_back(A); A = 0; } else { ok = false; } } A += s[i] == 'A'; }
if (A) a.push_back(A); else { ok = false; } if (ok) { ans -= *min_element(a.begin(), a.end()); } std::cout << ans << "\n"; }
intmain(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n;
std::vector<int> a(n + 1), b(n + 1); for (int i = 1; i <= n; i++) { std::cin >> a[i]; } for (int i = 1; i <= n; i++) { std::cin >> b[i]; }
std::set<int> set; std::vector<int> p(n + 1, -1); for (int i = 1; i <= n; i++) { if (a[i] == b[i]) { p[i] = a[i]; set.insert(p[i]); } } std::set<int> cur; for (int i = 1; i <= n; i++) { if (!set.count(i)) { cur.insert(i); } } std::vector<int> pos; for (int i = 1; i <= n; i++) { if (p[i] == -1) { pos.push_back(i); } }
auto check = [&](auto a, auto b, auto p) { if (a == b) returnfalse; int cnt = 0; for (int i = 1; i <= n; i++) { if (a[i] != p[i]) { cnt++; } } if (cnt > 1) returnfalse; cnt = 0; for (int i = 1; i <= n; i++) { if (b[i] != p[i]) { cnt++; } } if (cnt > 1) returnfalse; returntrue; };
if (pos.size() == 1) { p[pos[0]] = *cur.begin(); } else { p[pos[0]] = *cur.begin(); p[pos[1]] = *--cur.end(); if (check(a, b, p)) { for (int i = 1; i <= n; i++) { std::cout << p[i] << " \n"[i == n]; } return0; } p[pos[1]] = *cur.begin(); p[pos[0]] = *--cur.end(); }
for (int i = 1; i <= n; i++) { std::cout << p[i] << " \n"[i == n]; }