题意:
给你一个序列,可以让一个区间 * k , 也可以不乘 ,让序列的区间和最大
思路:
动态规划
dp[1] 表示当前节点当前节点更新区间之前 ,
dp[2] 表示当前节点在更新区间中,
dp[3]表示当前节点已经在更新区间之后
1 #include<cstdio> 2 #include<string.h> 3 #include<algorithm> 4 #include<cmath> 5 #include<iostream> 6 #include<vector> 7 #include<queue> 8 #include<set> 9 #include<map> 10 #include<cctype> 11 #define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) 12 #define mem(a,x) memset(a,x,sizeof(a)) 13 #define lson rt<<1,l,mid 14 #define rson rt<<1|1,mid + 1,r 15 #define P pair<int,int> 16 #define ull unsigned long long 17 using namespace std; 18 typedef long long ll; 19 const int maxn = 1e6 + 10; 20 const ll mod = 998244353; 21 const int inf = 0x3f3f3f3f; 22 const long long INF = 0x3f3f3f3f3f3f3f3f; 23 const double eps = 1e-7; 24 inline ll read() 25 { 26 ll X = 0, w = 0; char ch = 0; 27 while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } 28 while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); 29 return w ? -X : X; 30 } 31 ll n, m; 32 ll dp[10]; 33 int main() 34 { 35 n = read(), m = read(); 36 ll ans = 0; 37 for (int i = 1; i <= n; ++i) 38 { 39 ll num = read(); 40 dp[1] = max(0 * 1ll, dp[1] + num); 41 dp[2] = max(dp[1], dp[2] + num * m); 42 dp[3] = max(dp[2], dp[3] + num); 43 ans = max(ans, dp[3]); 44 } 45 cout << ans << endl; 46 return 0; 47 }