시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
5 초 256 MB 118 44 32 32.990%

문제

GSHS 앞에 있는 모 편의점에서 허니버터칩을 무료로 나눠주는 행사를 진행하고 있다! 재현이는 이 기회를 놓칠 수 없어서 무단외출을 감수하고 편의점으로 달려갔다.

편의점에는 N봉지의 허니버터칩이 좌에서 우로 나열되어 있으며, 이 중 i번째 봉지는 Ai개의 과자를 가지고 있다. 추가적으로 M개의 봉지가 더 제공되며, 이 중 i번째 봉지는 Bi개의 과자를 가지고 있다. (봉지가 다 비슷해보이지만 실제 안에 들어 있는 과자의 개수는 천차만별이다)

재현이는 먼저 좌에서 우로 나열되어 있는 N봉지의 허니버터칩 사이에 M개의 봉지를 아무 곳에나 (시작점, 끝점, 봉지 사이) 끼워 넣을 수 있다. 이렇게 되면 N+M 개의 봉지가 좌에서 우로 나열되며 초기의 N 봉지는 상대적 순서를 유지하는 형태가 될 것이다.

재현이는 이렇게 만들어 놓은 리스트를 좌에서 우로 순서대로 걸어가면서 뽑아간다. 재현이는 리스트에 있는 과자를 고를 수도 있고, 안 고를 수도 있지만, 재현이의 걸음이 빠른 편이기 때문에 만약에 과자 한봉지를 가져갔다면 그 다음 과자는 절대 가져갈 수 없다. 다른 말로 하면, 리스트에서 연속된 과자를 고를 수 없다.

재현이가 허니버터칩을 가장 많이 가져갈 수 있도록, 재현이를 도와주자!

입력

첫 번째 줄에는 N이 주어진다. 이후 N개의 줄에 Ai가 주어진다. (1 ≤ N ≤ 3,000, 1 ≤ Ai ≤ 100,000)

N+2번째 줄에는 M이 주어진다. 이후 M개의 줄에 Bi가 주어진다. (0 ≤ M ≤ 100, 1 ≤ Bi ≤ 100,000)

출력

재현이가 최대로 가져갈 수 있는 허니버터칩의 개수를 출력하라.

예제 입력 1

5
10
12
6
14
7
3
1
8
2

예제 출력 1

44

힌트

10, 1, 12, 2, 8, 6, 14, 7의 순서가 최적이다.

W3sicHJvYmxlbV9pZCI6IjEwNzc3IiwicHJvYmxlbV9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wiOiIxIiwicHJvYmxlbV9sYW5nX2NvZGUiOiJcdWM2MDFcdWM1YjQifV0=

출처

Olympiad > Canadian Computing Competition & Olympiad > 2015 > CCC 2015 Senior Division 5번

  • 문제를 번역한 사람: koosaga
  • 잘못된 조건을 찾은 사람: topology