|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Main { |
| 7 | + |
| 8 | + // IO field |
| 9 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + static StringTokenizer st; |
| 12 | + |
| 13 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 14 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 15 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 16 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 17 | + |
| 18 | + // Additional field |
| 19 | + static int[][] dp; |
| 20 | + static int[] A, B; |
| 21 | + static int N; |
| 22 | + static final int INF = (int)1e9 + 1; |
| 23 | + |
| 24 | + public static void main(String[] args) throws Exception { |
| 25 | + |
| 26 | + ready(); |
| 27 | + solve(); |
| 28 | + |
| 29 | + bwEnd(); |
| 30 | + } |
| 31 | + |
| 32 | + static void ready() throws Exception{ |
| 33 | + |
| 34 | + N = Integer.parseInt(br.readLine()); |
| 35 | + A = new int[N+1]; |
| 36 | + B = new int[N+1]; |
| 37 | + dp = new int[N+2][N+2]; |
| 38 | + for(int i=0;i<N+2;i++) Arrays.fill(dp[i], INF); |
| 39 | + |
| 40 | + nextLine(); |
| 41 | + for(int i=1;i<=N;i++) A[i] = nextInt(); |
| 42 | + nextLine(); |
| 43 | + for(int i=1;i<=N;i++) B[i] = nextInt(); |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + static void solve() throws Exception{ |
| 48 | + |
| 49 | + dp[0][0] = 0; |
| 50 | + for(int i=1;i<=N+1;i++) for(int j=1;j<=N+1;j++) { |
| 51 | + if(dp[i-1][j] != INF) { |
| 52 | + if(dp[i][j] == INF) dp[i][j] = dp[i-1][j]; |
| 53 | + else dp[i][j] = Math.max(dp[i][j], dp[i-1][j]); |
| 54 | + } |
| 55 | + if(dp[i-1][j-1] != INF) { |
| 56 | + if(dp[i][j] == INF) dp[i][j] = dp[i-1][j-1]; |
| 57 | + else dp[i][j] = Math.max(dp[i][j], dp[i-1][j-1]); |
| 58 | + } |
| 59 | + if(i<=N && j>1 && A[i] > B[j-1] && dp[i][j-1] != INF) { |
| 60 | + if(dp[i][j] == INF) dp[i][j] = dp[i][j-1] + B[j-1]; |
| 61 | + else dp[i][j] = Math.max(dp[i][j], dp[i][j-1] + B[j-1]); |
| 62 | + } |
| 63 | + } |
| 64 | + int ans = 0; |
| 65 | + for(int i=1;i<=N+1;i++) { |
| 66 | + if(dp[i][N+1] != INF) ans = Math.max(ans, dp[i][N+1]); |
| 67 | + if(dp[N+1][i] != INF) ans = Math.max(ans, dp[N+1][i]); |
| 68 | + } |
| 69 | + bw.write(ans+"\n"); |
| 70 | + |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +``` |
0 commit comments