#include #include #include using namespace std; const int MAX_H = 500005; // 最大身長 int main() { int N; cin >> N; vector A(N), B(N); for (int i = 0; i < N; ++i) cin >> A[i]; for (int i = 0; i < N; ++i) cin >> B[i]; vector dp(MAX_H, false); dp[1] = true; // 初期身長は1cm for (int i = 0; i < N; ++i) { vector next_dp = dp; for (int h = 0; h < MAX_H; ++h) { if (dp[h] && h >= A[i]) { int new_h = h + B[i]; if (new_h < MAX_H) { next_dp[new_h] = true; } } } dp = next_dp; } // dp配列から最大の身長を探す for (int h = MAX_H - 1; h >= 0; --h) { if (dp[h]) { cout << h << endl; return 0; } } }