Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pii pair<int, int>
- #define pb push_back
- #define mp make_pair
- #define f first
- #define s second
- using namespace std;
- typedef long long ll;
- const int MAXN = (int) 3e6 + 7;
- const int INF = (int) 1e9 + 7;
- int n;
- int a[MAXN];
- ll moves[MAXN];
- int used[MAXN];
- int timer;
- int cnt[MAXN];
- int mn[MAXN];
- int main() {
- #ifdef LOCAL
- freopen("in", "r", stdin);
- #endif
- scanf("%d", &n);
- for (int i = 1; i <= n; i++) {
- ++timer;
- scanf("%d", &a[i]);
- vector<pii> all;
- for (int it = 0, x = a[i]; x > 0; it++, x >>= 1) {
- all.pb(mp(x, it));
- for (int it2 = it + 1, y = x * 2; y <= 300000; y <<= 1, it2++) {
- all.pb(mp(y, it2));
- }
- }
- timer++;
- for (auto it : all) mn[it.f] = INF;
- for (auto it : all) {
- mn[it.f] = min(mn[it.f], it.s);
- }
- for (auto it : all) {
- if (used[it.f] != timer)
- cnt[it.f]++,
- moves[it.f] += mn[it.f];
- used[it.f] = timer;
- }
- }
- ll ans = 1e11;
- for (int i = 1; i <= 300000; i++) {
- if (cnt[i] == n) {
- ans = min(ans, moves[i]);
- }
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement