14256: 【原4256】Candies
题目
题目描述
author: Guo Linsong 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4256
Description
We have a \(2\times N\) grid. We will denote the square at the i-th row and j-th column (\(1≤i≤2\),\(1≤j≤N\)) as \((i,j)\).You are initially in the top-left square, \((1,1)\). You will travel to the bottom-right square, \((2,N)\), by repeatedly moving right or down.The square \((i,j)\) contains \(A_{i,j}\) candies. You will collect all the candies you visit during the travel. The top-left and bottom-right squares also contain candies, and you will also collect them.At most how many candies can you collect when you choose the best way to travel?
Input Format
Input is given from Standard Input in the following format:
\(N\)
\(A_{1,1}~A_{1,2}\ldots A_{1,N}\)
\(A_{2,1}~A_{2,2}\ldots A_{2,N}\)
其中\(1 \leq N \leq 100,1 \leq A_{i,j} \leq 100\)
Output Format
Print the maximum number of candies that can be collected.
Sample Input 1
5
5 4 3 2 1
1 2 3 4 5
Sample Output 1
24
Sample Input 2
5
2 8 3 1 7
8 9 2 3 4
Sample Output 2
28
ligongzzz's solution
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> up(n + 1), down(n + 1), ups(n + 1, 0), downs(n + 1, 0);
for (int i = 1; i <= n; ++i) {
cin >> up[i];
ups[i] = ups[i - 1] + up[i];
}
for (int i = 1; i <= n; ++i)
cin >> down[i];
downs[n] = down[n];
for (int i = n - 1; i >= 1; --i)
downs[i] = downs[i + 1] + down[i];
int ans = 0;
for (int i = 1; i <= n; ++i) {
int cur_ans = ups[i] + downs[i];
ans = cur_ans > ans ? cur_ans : ans;
}
cout << ans;
return 0;
}