QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#355260 | #2299. Heating Up | ckiseki# | AC ✓ | 259ms | 245684kb | C++20 | 2.5kb | 2024-03-16 15:02:48 | 2024-03-16 15:02:49 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(auto s, auto ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int f = 0;
(..., (cerr << (f++ ? ", " : "") << a));
cerr << ")\e[0m\n";
}
#include <experimental/iterator>
void orange_(auto s, auto L, auto R) {
cerr << "\e[1;33m[ " << s << " ] = [ ";
using namespace experimental;
copy(L, R, make_ostream_joiner(cerr, ", "));
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
using lld = int64_t;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<lld> a(n), pre(n + 1);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++)
pre[i + 1] = pre[i] + a[i];
auto query = [&](int l, int r) -> lld {
if (l == r) return 0;
if (l < r)
return pre[r] - pre[l];
return pre[r] - pre[l] + pre[n];
};
const int LG = 60;
vector<int> prv[LG], nxt[LG];
for (int b = 0; b < LG; b++) {
prv[b].assign(n, -1);
int last = -1;
for (int i = 0; i < n; i++) {
if (a[i] >= (1LL << b)) last = i;
prv[b][i] = last;
}
for (int i = 0; i < n; i++)
if (prv[b][i] == -1)
prv[b][i] = last;
else
break;
}
for (int b = 0; b < LG; b++) {
nxt[b].assign(n, -1);
int last = -1;
for (int i = n - 1; i >= 0; i--) {
if (a[i] >= (1LL << b)) last = i;
nxt[b][i] = last;
}
for (int i = n - 1; i >= 0; i--)
if (nxt[b][i] == -1)
nxt[b][i] = last;
else
break;
}
lld ans = 1e13;
for (int start = 0; start < n; start++) {
int l = (start + 1) % n;
int r = start;
lld x = a[start] * 2;
lld cur = a[start];
while (l != r) {
int lev = x==0 ? 0 : __lg(x);
debug(x, l, r);
if (nxt[lev][l] == -1) {
break;
}
assert(prv[lev][(r + n - 1) % n] != -1);
int L = nxt[lev][l];
int R = prv[lev][(r + n - 1) % n];
x += query(l, L);
x += query((R + 1) % n, r);
l = L;
r = (R + 1) % n;
if (x >= a[L]) {
x += a[L];
l = (L + 1) % n;
} else if (x >= a[R]) {
x += a[R];
r = R;
} else {
lld add = min(a[L], a[R]) - x;
x += add;
cur += add;
}
}
debug(start, cur);
ans = min(ans, cur);
}
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3632kb
input:
4 10 20 15 1
output:
9
result:
ok single line: '9'
Test #2:
score: 0
Accepted
time: 96ms
memory: 245596kb
input:
500000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1...
output:
1
result:
ok single line: '1'
Test #3:
score: 0
Accepted
time: 67ms
memory: 245528kb
input:
500000 500000 499999 499998 499997 499996 499995 499994 499993 499992 499991 499990 499989 499988 499987 499986 499985 499984 499983 499982 499981 499980 499979 499978 499977 499976 499975 499974 499973 499972 499971 499970 499969 499968 499967 499966 499965 499964 499963 499962 499961 499960 499959...
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 110ms
memory: 245604kb
input:
500000 269655 357411 31288 467020 110496 411556 112354 389593 171879 31947 4478 451939 305813 353339 49648 499863 157385 370552 9830 451015 205703 127891 152977 102706 178312 99678 251482 407026 65794 348294 45973 39969 169990 115902 287834 225236 292268 427507 131002 392853 312830 353489 390159 370...
output:
13561
result:
ok single line: '13561'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3596kb
input:
3 0 0 0
output:
0
result:
ok single line: '0'
Test #6:
score: 0
Accepted
time: 67ms
memory: 245616kb
input:
500000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
3 10000000000000 10000000000000 10000000000000
output:
10000000000000
result:
ok single line: '10000000000000'
Test #8:
score: 0
Accepted
time: 118ms
memory: 245640kb
input:
500000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000000000 10000000...
output:
10000000000000
result:
ok single line: '10000000000000'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3508kb
input:
3 9 5 2
output:
3
result:
ok single line: '3'
Test #10:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
3 443 555 705
output:
443
result:
ok single line: '443'
Test #11:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
3 4716156205617 1471795665076 6443609220689
output:
3244360540541
result:
ok single line: '3244360540541'
Test #12:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
10 9 4 10 4 2 2 2 4 1 10
output:
2
result:
ok single line: '2'
Test #13:
score: 0
Accepted
time: 0ms
memory: 3788kb
input:
10 437 194 316 8 760 5 240 658 951 36
output:
194
result:
ok single line: '194'
Test #14:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
10 7766206761324 4390096407416 5007750532908 6105856289679 8058953691162 7878324499767 198372065990 2561194123806 5270309573574 108952625948
output:
2510743383778
result:
ok single line: '2510743383778'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
1000 5 3 1 2 5 3 2 0 5 9 1 2 9 0 6 5 9 5 1 6 4 8 9 8 4 6 1 10 10 3 4 8 10 0 1 4 2 4 9 10 2 7 0 5 0 5 7 5 4 7 5 5 6 8 8 10 2 9 0 3 5 8 9 5 6 1 10 5 2 2 1 3 4 10 9 3 10 4 1 0 9 1 1 10 6 4 1 10 3 7 8 5 10 2 3 9 3 0 8 4 0 1 1 8 8 3 8 9 5 5 3 2 4 10 9 2 10 9 8 8 8 7 7 2 7 1 0 7 10 5 9 0 9 6 5 10 9 1 0 5 ...
output:
1
result:
ok single line: '1'
Test #16:
score: 0
Accepted
time: 1ms
memory: 3776kb
input:
1000 876 688 340 910 327 791 194 228 468 91 276 245 236 492 613 986 131 16 224 816 567 919 207 114 181 264 311 795 632 568 601 96 87 458 139 930 235 486 298 995 80 254 108 522 174 553 953 604 880 165 574 982 332 156 884 205 125 712 146 1000 644 595 388 325 341 96 378 750 0 733 976 572 630 581 252 26...
output:
81
result:
ok single line: '81'
Test #17:
score: 0
Accepted
time: 1ms
memory: 3776kb
input:
1000 4244476445981 4188008041459 1343004383823 4430976398873 2707281889836 9972518046105 3771690011425 9278910842226 9458266244956 6404668117946 1394635778532 7077743790553 8082228561075 6755386194521 7394605523559 1358052292721 7208404326898 2369146891376 6397957436808 8922154200087 4039707770071 3...
output:
941569522270
result:
ok single line: '941569522270'
Test #18:
score: 0
Accepted
time: 4ms
memory: 8128kb
input:
10000 3 3 3 7 6 2 8 0 7 4 3 9 7 10 8 4 2 0 0 10 3 5 0 6 4 8 0 0 4 10 1 2 7 0 0 4 1 5 10 4 2 0 7 3 9 5 2 4 10 3 8 1 7 2 0 8 3 5 7 4 4 10 0 6 8 4 8 10 3 10 3 3 10 4 8 2 10 7 10 9 4 9 8 9 0 6 9 5 1 7 2 0 2 3 0 10 8 7 3 0 5 4 6 2 0 4 2 6 7 0 9 2 2 2 8 2 10 2 10 3 10 8 3 8 10 5 6 1 0 7 7 7 9 4 6 1 0 7 2 ...
output:
1
result:
ok single line: '1'
Test #19:
score: 0
Accepted
time: 0ms
memory: 8168kb
input:
10000 503 224 821 855 343 35 399 822 512 417 644 624 149 462 32 590 663 563 166 996 566 215 811 33 650 788 934 83 697 927 763 817 74 322 75 772 793 906 151 242 718 460 299 921 498 862 323 441 439 708 322 864 965 194 126 374 578 793 4 791 782 212 143 920 308 415 663 739 103 925 562 873 402 777 262 13...
output:
43
result:
ok single line: '43'
Test #20:
score: 0
Accepted
time: 0ms
memory: 8204kb
input:
10000 7803527661501 2395353435002 4570545035827 9189376124397 4613753129310 337134819369 1771517168097 6923910338059 1805047473968 5919987687999 9377832980782 3088260997173 1648061138881 1769001301496 7474212099137 9697794622249 3014578785761 9277959749147 5392922696370 1678858571990 9998623397131 2...
output:
431788281471
result:
ok single line: '431788281471'
Test #21:
score: 0
Accepted
time: 100ms
memory: 245608kb
input:
500000 8 2 6 5 10 4 0 8 0 10 7 1 10 4 2 9 3 1 0 7 9 3 7 2 6 1 9 1 7 5 3 5 10 5 2 7 6 2 5 6 7 1 6 8 3 5 7 10 4 0 4 2 2 9 2 4 9 6 5 10 5 4 10 5 10 6 7 5 5 8 5 5 8 0 4 9 1 1 0 2 5 4 4 7 2 5 8 6 6 4 0 8 8 1 4 4 10 0 5 9 4 7 4 6 9 1 4 0 8 9 0 5 10 10 3 3 4 6 3 7 8 1 7 7 4 1 3 3 5 8 9 6 3 6 7 4 2 0 4 7 10...
output:
1
result:
ok single line: '1'
Test #22:
score: 0
Accepted
time: 91ms
memory: 245680kb
input:
500000 945 441 317 935 298 231 897 357 108 180 104 812 189 558 258 262 448 672 51 557 764 336 106 910 833 461 599 372 656 345 132 997 53 150 748 806 728 284 628 738 839 238 521 920 917 165 909 262 90 805 570 774 108 250 25 234 27 589 785 202 613 102 336 575 872 469 697 697 800 364 33 787 342 82 5 28...
output:
24
result:
ok single line: '24'
Test #23:
score: 0
Accepted
time: 129ms
memory: 245528kb
input:
500000 8915837115681 1854924308350 4983171063409 9326350649871 3777458241922 4011735663721 6194627458492 968523372193 9719827134355 9233303438062 5481508725626 3743774964152 9665944214496 70071673654 9122981525604 5317000871929 8175936807290 296801525625 3488614064184 7847932215143 4209793972634 412...
output:
263506680769
result:
ok single line: '263506680769'
Test #24:
score: 0
Accepted
time: 0ms
memory: 3808kb
input:
1000 692 162 2646232467 1661392687526 2434434 1113 211 1027044183775 22 838664463100 331 413541 6 7354866040419 78512 15716 1243876261117 2371446131 18277 162582936964 1 104 4143659288 3863 3618 986710438140 949203668452 8672248522792 1060827857035 355 128 8354978 335285 5311273591 617856 1672862 70...
output:
114695390678
result:
ok single line: '114695390678'
Test #25:
score: 0
Accepted
time: 259ms
memory: 245536kb
input:
500000 437933005416 785 14320 1397084437204 1791441286 1511814 1 22795 46344 62155 467420034 2972 40991 19947739744 2728130 24 17271937929 14950076324 12 98781715531 1557656287604 20142452764 15555981223 242 542336379 1360227458 8 44531139100 1480999054246 3494809 588 47251 209574 23836173 300185110...
output:
7990458392
result:
ok single line: '7990458392'
Test #26:
score: 0
Accepted
time: 172ms
memory: 245684kb
input:
500000 0 1 3 7 15 31 63 127 255 511 1023 2047 4095 8191 16383 32767 65535 131071 262143 524287 1048575 2097151 4194303 8388607 16777215 33554431 67108863 134217727 268435455 536870911 1073741823 2147483647 4294967295 8589934591 17179869183 34359738367 68719476735 137438953471 274877906943 5497558138...
output:
1
result:
ok single line: '1'
Test #27:
score: 0
Accepted
time: 90ms
memory: 245540kb
input:
500000 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1...
output:
4749859
result:
ok single line: '4749859'
Test #28:
score: 0
Accepted
time: 107ms
memory: 245556kb
input:
500000 100002 100001 100000 99999 99998 99997 99996 99995 99994 99993 99992 99991 99990 99989 99988 99987 99986 99985 99984 99983 99982 99981 99980 99979 99978 99977 99976 99975 99974 99973 99972 99971 99970 99969 99968 99967 99966 99965 99964 99963 99962 99961 99960 99959 99958 99957 99956 99955 99...
output:
1240000135296
result:
ok single line: '1240000135296'
Test #29:
score: 0
Accepted
time: 91ms
memory: 245616kb
input:
500000 266444 266447 266446 266449 266448 266451 266450 266453 266452 266455 266454 266457 266456 266459 266458 266461 266460 266463 266462 266465 266464 266467 266466 266469 266468 266471 266470 266473 266472 266475 266474 266477 266476 266479 266478 266481 266480 266483 266482 266485 266484 266487...
output:
2
result:
ok single line: '2'
Test #30:
score: 0
Accepted
time: 87ms
memory: 245592kb
input:
500000 233555 233552 233553 233550 233551 233548 233549 233546 233547 233544 233545 233542 233543 233540 233541 233538 233539 233536 233537 233534 233535 233532 233533 233530 233531 233528 233529 233526 233527 233524 233525 233522 233523 233520 233521 233518 233519 233516 233517 233514 233515 233512...
output:
2
result:
ok single line: '2'
Test #31:
score: 0
Accepted
time: 82ms
memory: 245612kb
input:
500000 32890 32892 32894 32896 32898 32900 32902 32904 32906 32908 32910 32912 32914 32916 32918 32920 32922 32924 32926 32928 32930 32932 32934 32936 32938 32940 32942 32944 32946 32948 32950 32952 32954 32956 32958 32960 32962 32964 32966 32968 32970 32972 32974 32976 32978 32980 32982 32984 32986...
output:
1
result:
ok single line: '1'