fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int n;
  9. cin >> n;
  10.  
  11. vector<int> a(n+1), b(n+1);
  12. for(int i = 1; i <= n; i++) cin >> a[i];
  13. for(int i = 1; i <= n; i++) cin >> b[i];
  14.  
  15. vector<int> prev(n+1, 0), curr(n+1, 0);
  16.  
  17. for(int i = 1; i <= n; i++) {
  18. for(int j = 1; j <= n; j++) {
  19. curr[j] = max(prev[j], curr[j-1]);
  20. if(a[i] == b[j]) {
  21. curr[j] = max(curr[j], prev[j-1] + a[i]);
  22. }
  23. }
  24. prev = curr;
  25. }
  26.  
  27. cout << prev[n] << "\n";
  28. }
Success #stdin #stdout 0s 5308KB
stdin
8
2 2 1 2 3 2 2 1
1 2 3 2 3 2 3 2
stdout
11