比赛 EYOI与SBOI开学欢乐赛4th 评测结果 AAWAAWWWWW
题目名称 白色谎言 最终得分 40
用户昵称 HeSn 运行时间 0.720 s
代码语言 C++ 内存使用 2.87 MiB
提交时间 2022-09-12 20:47:28
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n, m, a[1010];
bool vis[1010];
queue<int> q;
map<int, int> mp;
signed main() {
	freopen("white_lies.in", "r", stdin);
	freopen("white_lies.out", "w", stdout);
	while(cin >> m >> n) {
		for(int i = 1; i <= n; i ++) {
			cin >> a[i];
		}
		if(m == 1) {
			int maxn = 0, x = 0;
			for(int i = 1; i <= n; i ++) {
				x += a[i];
				if(x > maxn) {
					maxn = x;
				}
				if(x < 0) {
					x = 0;
				}
			}
			cout << maxn << endl;
		}
		else {
			int maxn = 0, maxn1 = 0, minn = 100, st, to, pos, x = 0;
			memset(vis, 0, sizeof(vis));
			for(int i = 1; i <= n; i ++) {
				x += a[i];
				if(x > maxn) {
					maxn = x;
					to = i;
					st = pos;
				}
				if(x < 0) {
					pos = i + 1;
					x = 0;
				}
			}
			x = 0;
			for(int i = 1; i <= n; i ++) {
				x += a[i];
				if(i >= st && i <= to) {
					x = 0;
					continue;
				}
				if(x < 0) {
					x = 0;
				}
				maxn1 = max(maxn1, x);
			}
			x = 0;
			for(int i = st; i <= to; i ++) {
				x += a[i];
				if(x > 0) {
					x = 0;
				}
				minn = min(minn, x);
			}
			minn = - minn;
			cout << maxn + max(minn, maxn1) << endl;
		}
	}
    return 0;
}