题解 | #最大序列和#
最大序列和
https://www.nowcoder.com/practice/df219d60a7af4171a981ef56bd597f7b
#include <iostream> #define INF 1000000 using namespace std; const int MAXN = 1e6; long long dp[MAXN]; long long arr[MAXN]; long long MaxSequence (int n){ long long maxium = -INF; for (int i = 0; i < n; i++){ if (i == 0){ dp[i] = arr[i]; }else{ dp[i] = max(arr[i], dp[i - 1] + arr[i]); } maxium = max(dp[i], maxium); } return maxium; } int main () { int n; while(cin >> n){ for (int i = 0; i < n; i++){ cin >> arr[i]; } cout << MaxSequence(n) << endl; } return 0; }
王道那学的