Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- #############################################################################################%####################################%###############
- #--%###*-=+-####--+::::=##%-:*#=:%#*-*#%-::+########%=::+##:=%#*-=#=-##:::::+%-:###-=####-:::=%##-::::*##=:=#####-::*%::::::+#-+###-::*##%--#%%--#
- #::%###*:-*:-##=:#=..::-###=.*#-.*#=:**:.:..:#####*#:.:::##.-##+.+#=:##...::+%:.=#%:-####:.::.:%#..:::*#%::.####-.::-%:....:+*:=#*..:.:=##:.=##:-#
- #::####*:-%::%#-:%=.#######*.+#:.:%:.%-:*##+:+#####-:#%##*#.-##+:=#=:*#.:%###%::.*%:-###%::##=:*#.-#####+:#.+##::####*##-:%##*.=#-:##%-:#%::.##::#
- #::####*:-#+:*#:=#=.::-*####:=*.=:#:-#::%##*.=#####::######.::::.=#=:##..:--#%::--#:-####:.##::##.::-=##-:#:=#+:=#######-:%##*.=%:-###+.=%::-:#::#
- #::####+:-#*:+*.*#=:::-#####:-=:+.*.+%::##%*.=*--=#::#####*..::..=#=:*#..::-#%:.+.+:-####:..:.+##:::-=##:=#-:*+:=#######::###*.=#:-###+:-#::*.*.-#
- #::%###+:-##:-::%#=:########:::-#:-:%#::*##*.+#--=#::*###*#:-##+:=#=:*#::%###%::%=.:-####:.#::*##:-####*.....#*:-#######-:###*:=%-:###=:+%::#-.::%
- #::*****:-##-..+##=.+***####=..##..:##*::%+.:####*#+::#*:##.-##*:=#=:##.:*#**%::#*:.-###%:.##:-##.:***#::###.-%-:-#+-%##-:###*:=#+..#+.-%#::#%:.:#
- #:..::+*:=##%::*##=:...:%##%#::##=:-###*:..:#########:::-#*:-##*:+%=:##:..::=%::##+:-###%-:#%=:=%:....+:-###--*#=:.:=###-:###*:+*#*.::-###:-##+:-#
- ################################################################################################################################################%#
- ####################################################%#################################################################%#########%#################
- %#%#%@%@@%%%%%*%@%#%%%%@@%@@@@@@#%%#***+++@@@@@@@@@@*@@%@@@@@@%#--@@@@@@@@@@@@@@@@@@@@@@@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@@@@%@@@@@@@@@@@%%%%@**@@@%*%%#:+@@%%#%#*++##%#+++++*%%%*++++++*@%@@@##=.-%@%%#%++++@@@@%%%%%%%%%%%%
- %@%#%#@%%@%%%%%%%%%%#%%@%%%#=*@@@@@@@@@@@@@@@%@@@@%@@@@@@@@@@@%#%%%#@@@%@%@@##:-%%#*+*#%%%*+++++++++++*++++++++%%##=:.*@@@#%%#++++@@@%%%######%%%%
- #%%%%@%%%%#%%%@@@@@%%%%%%%%%@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#%@@%@@%#%%#%%*%@##*-:=+*%%%#*++++++++++++*+++++++++#+:+#@@@@@@@@%*+++@@@%%%##########
- %%%%@@@@%%%%%#%#%##%%%%%%#%%%#+==***%@@@@@@@@@@@@@@@@@@@@@@@@@@@@***##*#%%%%%%@%%%#*#@%*+++++++++++++++%++++++++=#*++%@@%@@@@%*+++@@%%%%##########
- @@@%%%#%%###%%%%%#######%%%%%##+==#=====*+#%@@@@@@@@@@@@@@%@@@@+*#*+=#%@%#%%%%%%#%%%%%*+++++++**+++++++**+++**++++@+++%%@@@@@@#+=%%%%%%%##########
- ####%%%%%#%%%%%#%@%#####%%%%%%%*=+=#=====#+==*@@@@@@@@@@@**@@@###%@%%@@#%%%%%%%%##%%@*++++++*%++++++++++*++++###*+#*++*@@@@@%%%*@%%%%%%###########
- %##%%%%%%##%%%%##%%######%%#%%%#++=+%=====#===+%@@%%@@@@@%#@@@%%%%%@@%#%%%%%%%%%%%%%%++=++#%#*++++++++++*+++++#==##++++@%@@%%%%####%%##%%######*##
- %####%@%#%#%%%%###@%####%###%%%%#====*=====#==++#@@@@@@@@@@@@@@#%%%@%#%%%%%%%%%####%%#%%*===*#++++++++++*+++++#===+%+++%%%%%%%%%%#####%%%%%%%%%%%%
- %####%#%#%#%%%%###%@######%%%%%%%+===#+====+%+=+*%@@@@@@@@%%#*%%@@@%%%%#%#%%%%%#++*#%=---==-=+%*++++++++++++++#=====%++*%%%%%%%%%#%###%%%%%%%%%%%@
- %######%%%%%%%#%##%%%%%####%%%%%%#++==#======#=++*%@@@@@@@@@@%%@@@%%##%#%%%%##*++++#*-===---=-==##++++++++++**==-===%++#%%%%%%%%%%%%%#%%%%%%%@@@@@
- %####%%#%%%%###%%%#%%%#%###%@%%%%%++=++#=====**=+++%@@@@@@@@@@@@@##%#%%%%%%#%*+++++%+==-===--===-=*%*+++++#*==-=--==%++#%%%%%%%%%%%%%%%#*%@@@@@@@@
- %#######%@%%@@@%%#%%%%#%%#%%%%%%%##+*==**==+==*==+*+@@@@@@@@@@@@%%%%#%%%%%%#+++++++%+=========--====+%%**-=--====-==%++#@@%@%%%%#*%%%%%#%**+*@%@@%
- %%#%%#%%#%@#****#%@@@@@%%@@@@@@@@@@@@@@@@%%#***+*++++%@@@@@@@@@%%%%%%%#%%%%*+++++++%+===-==========--====---=====-=+%++#@@@%%+++###%#%-=+#=-#%@@@#
- %%##%%%#%##@##*******##%%%%%%#####**#**#*****##%@@%%%#@@@@@@@@%%%%%%%%%#%#*++++++++##=-====---=---==--==--=======-=*#++@@%#=*+++--:-=-+-==#*=++*=-
- #%##%%%####%@%#%%%%%%%%%%%#***********************#%@@@@@@@@@@%%%%%%##%##*++++++++++%=---=--===--===========-==-==-%#+*@#**%#%%%@#*%%%%%#%@@%-*==*
- %@#********##%%%%%%%%%%%##****************************%@@@@@@%#%%%%%#%%%*+++++++++++*%*-=-=-=-+%=-===========-*#==-%*=%+==%%%%%@@@##@%%@@@@@%@%%%%
- %%%%%%%%%%%%%%%%%%%%%%%#********************************#%@@@%##%#%%#%%*+++++++++++*++#%==--=%*%============--%%=-*#+%%%%%%%@@@@%@%@@@@@@@@@%@@@%@
- %%%%%%%%%%%%%%%%%%%%%%%#*******************************##%%%%#%%%%%%%%*++++++++=+#@#+++%%=-+#+*%====-=----===-##*#++++%%%%%@%%%@@@@@@@@@@@@@@@@@@@
- %%%%%%%%%%%#@%%%%%%%%%%###****************************#%%%#%#%%%%%%%%#++++++++#@%###++**###++++##-====--=====#**%*++++*@@@%##%@@@@@@@@@@@@@@@@@@@@
- %%%%@%*++++++%%%%%%%%%%%%%####*************####%%#%#**#%###%%%#%%%%%#+++*#%@%#*+++#%+%++++++++++#%==-===--=+%+++++++#==#%%%%@@@@@@@@@@@@@@@@@@@@@@
- %%@#+=+++++++*%@@@%%*+++*@%%%%%%%%%##%%%@@%%%#++==**+%#########%%@%%%#%%%%#%*+++++***=+++++++++++*%#===-==@*++++++++*%++%%@@@@@@@@@@@@@@@@@@@@@@@@
- @#+=+++++++++++++++=++++++%%%%%%%%%%%%%%###%#%*====##%##########%%%%%#%###%#+++=*++++++++++++++++++#@*-+%*+=+++++++++@++#@@@@@@@@@@@@@@@@@@@@@@@@@
- #+++++++++++++++++++++++++%%%%%@@@%##%%@#%%%%##+=*%##########%#@%%%%%%%%%%%++++#++++++++++++++++++++++#++++++++++++++*#++@@@@@@@@@@@@@@@@@@@@@@@@@
- #+++++*++++%++++++++++++++#@%@%%%#%##%%%%%%#%#%#%##############@%%%%%%%%%#++++*=++=+++++++++++++++++++*+++++++++++++++%++%@@@@@@@@@@@@@@@%%@%@@@@@
- ##%%%#++++#=++++++#+++++++*%#%%@%#######%#%%#%%%###%##########%#%%%%%@%#%*+++**=+++++++++++++++++++=++#+++++++++++++++%++#@%@@@@@@@@@@@@@@@@@%@%%#
- %%%%%++++%+++++++*@%#+=+++*%%%#%@#%%###%%@%#%%##############%%%%%%#%@%#%*++++*++++++++++++++++++++++++#+++++++++++++++#*+#@@@@@@@@@@%%#########%%%
- %%%@%*++*++++++++#@%@%####@%%%#%%@#%####%#%%%#%##############%%%%%%%@%%%++++*+++++++++++++++++++++++++#++++++++=++++++#%%@###*###%%%%%@@@@@@@@@@@@
- %%%@##++*+++++++*%@%%@@%%%@%%##%%%%##%%%#%%%%################%%#%%#@%%%#+++++=++++++++++++++++++++++++#+++++++*+++++++#%%%@%%%%#%%@%#%@@@@@@@@@@@@
- %%%@%##+*++++++#%@%%##@%%%%##%%%#%@%#%#%#%%#################%@%%#+=*##%%*++++++++++++++=+++++++++++=+=#+++++++*+++++++#@%%%%##%@@@@@@@@@@@@@@@@@@@
- %%%%@%##**++##%%%##%##%@%%#%#%%##%#@%#%%%#############%######+--====%%#%#+++++++++++++*+++++++++++++++#++++++**+++++++##***#%#%@@@@@@@@@@@@@@@@@@@
- %%%%%@%#%##%##%%##%#%%%%%#%%#%%%%%##@%#@%########%###%@#####==-======@%%#+++++++++++++#=++++++++++++#=*%+++++#++++++++%#*#%@@@%@@@@@%@@%%@@%%@@@@@
- %%%%%%@%#%%%%@%##%%#%%%%%@%##%%%%%%%%%%%###########@%#%####=--=-=-=*%#@#%*++++++++++++@*++++++++++*+==-=##+++@*+++++++%%@%%@@@@@@@%%@%%%%%%@@@@@@@
- %%%%%%%@%#%@%#%%%#%%##%%##@%#%%%%%#%#%%#%###%###%@###%%###=======+@*+=#@%%+++++++++++%*%+++++++=*#-=--====@+*+%+++++++%@@@@%%%%%%%%%%@@%@@@@@@@@@@
- %%%%@%%#%%%%@%%%%%%%######%%%##%%#%%#@%######@@######%%%#=-=--=%%#+-===*@%#++++++++%+==#*++++=*%=-======-====-+%*+++++@%%%@@@%%%%%@%%@@@@@@@@@@@@@
- @@#####%###%#%#%%%%%####%##%@%#%%%@%#%%%%%%##########%%*=-==#@#%==-=-==-+%@+++++##===-==@+++#*==-=======-==--===%#+++##@@@@%%%%%%%%%@@@@@@@@@@@@@@
- %###%%%%##%%%#@%%%%%%%%##%#%%@%@%#%##%###############%#%%##%#*---=-==--===*%###==-====-=+%%=----=-=====--===-==-=%%++@=%@%%%%%%%%%@@@@@%@@@@@@@@@@
- %%%############@#%#%%%%#%#%#%@%#%###########################==-======--======-===--===--===--==========-----======#%%-=+%%%@@%%%@@@@@@@@@@@@@@@@@@
- %%%%%#########%#%#%%%%%%%#@@###############################*=====-------=--==--==---==-=====-============-=-=======---==%@@%%%%%%@@@@@@@@@@@@@@@@@
- %%%%%##########%#@#%%#%%@%##%##########################%###==-=====------======-=-=----=-#=====-===-=--=====--=--======-@@@@@@@@@@@@@@@@@@@@@@@@@@
- %%%%%#########%%##%%%#%%##################################*========------=======+===---=#%==---=-==@==-===-*==-=-======-%@@@@@@@@@@@@@@@@@@@@@@@@@
- #%%%%#########%##%%%#@################################%###=-==========---=-====*%===-==*%%==-==-==@#%-=--==%+-===-====--#@@@@@@@@@@@@@@@@@@@@@@@@@
- #%%%%%%########%#%#@%################%##################%*=====-===-===--=-==-#%%--=-=*##%+-=====@*#%@---==%%-==--=====-*@@@@@@@@@@@@@@@@@@@@@@@@@
- #%%%%%%%########%#@###################################%##=-======-%====--=-==%#%+=-=-@#*%%*-=--+%*++##%*-==%%%---=----=-*@@@@@@@@@@@@@@@@@@@@@@@@@
- #%%%%%%%########%###############%@%%##%#################*-========@%+==-==-*@+%@=-=%%*++%%#==*%#++=++%%%%*@#%%#--======-*@@@@@@@@@@@@@@@@@@@@@@@@@
- #%%%%%%%######%%%############%@%#%#@####################=-=======+##@*-==@@**=%%###*+=++##%%%#*+++++++*#%%*+###@+=====-=%@@@@@@@@@@@@@@@@@@@@@@@@@
- */
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- const ll MOD = 1E9 + 7;
- const int INF = 1E9; const ll INFLL = 1E18;
- int N;
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- int T; cin >> T;
- for(int test = 1; test <= T; test++) {
- cin >> N;
- vector<int> b(N);
- vector<int> w(N);
- for(int i = 0; i < N; i++) {
- cin >> b[i];
- }
- for(int i = 0; i < N; i++) {
- cin >> w[i];
- }
- ll needSum = 0; ll needTop = 0; ll needBottom = 0;
- for(int i = 0; i < N; i++) {
- needSum = (needSum + b[i] + w[i]);
- needBottom += w[i];
- needTop += b[i];
- }
- vector<int> bw(N);
- for(int i = 0; i < N; i++) {
- bw[i] = b[i] - w[i];
- }
- vector<int> diffPrefix(N);
- for(int i = 1; i < N; i++) {
- diffPrefix[i] = bw[i] - bw[0];
- }
- sort(diffPrefix.begin(), diffPrefix.end());
- int center = diffPrefix[N / 2];
- int radius = 1;
- int countBottom = 0;
- ll sumBottom = 0; ll sumTop = 0;
- for(int i : diffPrefix) {
- if(i >= center - radius) {
- sumTop += i - (center - radius);
- } else {
- sumBottom += (center - radius) - i;
- countBottom++;
- }
- }
- ll wochien = INFLL;
- ll vo = INFLL; int va = 0;
- for(int i = center - radius; i <= center + radius; i++) {
- while(countBottom < N && diffPrefix[countBottom] == i) {
- countBottom++;
- }
- int sum = (sumBottom + sumTop) % (N - 2);
- if(N % 2) {
- int diff = ((needSum % (N - 2)) - sum + (N - 2)) % (N - 2);
- if(diff % 2) {
- diff += N - 2;
- }
- wochien = min(wochien, sumBottom + sumTop + diff);
- } else {
- int diff = ((needSum % (N - 2)) - sum + (N - 2)) % (N - 2);
- ll change = needTop - (sumTop + diff / 2);
- ll mod = 1LL * N * (N - 2);
- ll add = (change > 0 ? mod - (N - 1) : N - 1);
- change = abs(change) % mod;
- if((sumBottom + diff / 2 + (long long)(((__int128)change * (__int128)add) % mod)) % mod == needBottom % mod) {
- wochien = min(wochien, sumBottom + sumTop + diff);
- if(sumBottom + sumTop == vo) {
- assert(va);
- }
- va = 0;
- } else {
- wochien = min(wochien, sumBottom + sumTop + diff + (N - 2));
- if(sumBottom + sumTop == vo) {
- assert(!va);
- }
- va = 1;
- }
- }
- vo = sumBottom + sumTop;
- sumBottom += countBottom;
- sumTop -= (N - countBottom);
- }
- if(!wochien && needSum) {
- wochien = 2LL * (N - 2);
- }
- cout << wochien << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment