#include void make_table(); void add(int, int); int n, a[1500], b[1500], table[15001], buttle[1501], next[1501]; int main(void) { scanf("%d", &n); int i, j; for(i = 0; i < n; i++) { scanf("%d", &a[i]); } for(i = 0; i < n; i++) { scanf("%d", &b[i]); b[i] /= 2; } int min = 1501; for(i = 0; i < n; i++) { // i: 開始位置 int max = 0; make_table(); int min_p = 0; for(j = 0; j < n; j++) { int k = (i + j) % n; int l = min_p; while(table[l] == 0) { l++; } min_p = l; int p = table[l]; table[l] = next[p]; buttle[p]++; max = max < buttle[p] ? buttle[p] : max; add(l + b[k], p); } min = max < min ? max : min; } printf("%d\n", min); return 0; } void make_table() { int i; for(i = 0; i < 15001; i++) { table[i] = 0; } for(i = 0; i < 1501; i++) { buttle[i] = 0; next[i] = 0; } for(i = 0; i < n; i++) { next[i + 1] = table[ a[i] ]; // 先頭につないでいく table[ a[i] ] = i + 1; } buttle[0] = 1501; // 番兵 return; } void add(int l, int p) { if(buttle[p] <= buttle[ table[l] ]) { next[p] = next[ table[l] ]; table[l] = p; } else { int q = table[l]; while( buttle[ next[q] ] < buttle[p] ) { q = next[q]; } next[p] = next[q]; next[q] = p; } return; }