// ロジック検討中. #include using namespace std; using LL = long long; const LL MAX = 1e9; int main() { // 1. 入力情報取得. LL N; scanf("%llu", &N); LL Y[N]; for(int i = 0; i < N; i++){ scanf("%llu", &Y[i]); Y[i] += MAX; } // 2. 昇順sort. sort(Y, Y + N); // 3. Noelちゃんが動かす必要のある距離の総和の最小値 を 計算する. // -> 二分探索で, opt を 探す LL hi = Y[N - 1]; LL lo = Y[0]; LL opt = 1LL + (hi + lo) / 2LL; LL dist = 1e14, cur = 0LL; int counter = 0; // 無限ループ防止のため, カウンター入れる. while(counter < 100){ // 3-1. 移動距離を保存. cur = 0LL; for(int i = 0; i < N; i++) cur += abs(opt - Y[i]); // 3-2. hi, lo, opt 更新. // cout << " hi=" << hi << " lo=" << lo << " opt=" << opt << endl; if(cur < dist) hi = (lo + hi) / 2LL; else lo = (lo + hi) / 2LL; opt = 1LL + (hi + lo) / 2LL; // 3-3. カウンター を インクリメント. counter++; // 3-4. 最小移動を距離更新. dist = min(dist, cur); // 3-5. 終了. if(hi - lo <= 0LL) break; } // 4. 出力. printf("%llu\n", dist); return 0; }