You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
#define int long long
int n, p; // n个小朋友对p取模
int num[N]; // 记录每个小朋友的数字
int tz[N]; // 记录每个小朋友的特征值
int score[N]; // 记录每个小朋友得分
int calc(int x) {
// 求1-x区间最大连续子序列的和
// 每个小朋友的特征值等于排在他前面(包括他本人)的小朋友中
// 连续若干个(最少有一个)小朋友手上的数字之和的最大值。
int ans = num[1];
for (int i = 1; i <= x; i++) // 开始
for (int j = i; j <= x; j++) { // 结束
int sum = 0; // 枚举区间,求和并打擂台
for (int k = i; k <= j; k++) sum += num[k];
ans = max(ans, sum);
}
return ans;
}
signed main() {
// 加快读入
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> p;
// 读入每个小朋友的数字
for (int i = 1; i <= n; i++) cin >> num[i];
// 计算每个小朋友的特征值
for (int i = 1; i <= n; i++) tz[i] = calc(i);
// 计算每个小朋友的分数
score[1] = tz[1]; // 第一个小朋友的分数是他的特征值
int mx = tz[1] + score[1]; // mx用来记录当前最大特征值与分数和
for (int i = 2; i <= n; i++) {
score[i] = mx;
mx = max(mx, tz[i] + score[i]);
}
int ans = score[1];
for (int i = 2; i <= n; i++) ans = max(ans, score[i]);
cout << ans % p << endl;
}