// yukicoder: No.9 モンスターのレベル上げ // bal4u 2019.8.18 #include #include //// 入出力関係 #if 1 #define gc() getchar_unlocked() #else #define gc() getchar() #endif int in() { // 非負整数の入力 int n = 0, c = gc(); do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0'); return n; } //// 優先度付き(最小)キュー #define MAX 10000 typedef struct { int a, n; } QUE; QUE que[MAX]; int qsize; QUE qcp[MAX]; int qsz; #define PARENT(i) ((i)>>1) #define LEFT(i) ((i)<<1) #define RIGHT(i) (((i)<<1)+1) void min_heapify(int i) { int l, r, mi; l = LEFT(i), r = RIGHT(i); if (l < qsize && (que[l].a < que[i].a || que[l].a == que[i].a && que[l].n < que[i].n)) mi = l; else mi = i; if (r < qsize && (que[r].a < que[mi].a || que[r].a == que[mi].a && que[r].n < que[mi].n)) mi = r; if (mi != i) { QUE qt = que[i]; que[i] = que[mi]; que[mi] = qt; min_heapify(mi); } } void deq() { que[0] = que[--qsize]; min_heapify(0); } void enq(int a, int n) { int i, mi; i = qsize++; que[i].a = a, que[i].n = n; while (i > 0 && (que[i].a < que[mi = PARENT(i)].a || que[i].a == que[mi].a && que[i].n < que[i].n)) { QUE qt = que[i]; que[i] = que[mi]; que[mi] = qt; i = mi; } } //// 本問題関連 int N; int a[1503], b[1503]; int main() { int i, j, k, t, ans; N = in(); for (i = 0; i < N; i++) a[i] = in(); for (i = 0; i < N; i++) b[i] = in(); if (N == 1) { puts("1"); return 0; } ans = N; qsize = 0; for (i = 0; i < N; i++) enq(a[i], 0); memcpy(qcp, que, sizeof(que)), qsz = qsize; for (i = 0; i < N; i++) { if (i) qsize = qsz, memcpy(que, qcp, sizeof(que)); t = 0, k = i; for (j = 0; j < N; j++) { int x = que[0].a, y = que[0].n+1; deq(); enq(x + (b[k]>>1), y); if (y > t) t = y; if (++k == N) k = 0; } if (t < ans) { ans = t; if (ans == 1) break; } } printf("%d\n", ans); return 0; }