Problem Link
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100000;
long long dpWithDelete[N + 1], dpWithoutDelete[N + 1];
int main()
{
int t, n;
long long arr[N + 1];
long long maxVal;
cin >> t;
while(t--)
{
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
memset(dpWithDelete, 0, sizeof dpWithDelete);
memset(dpWithoutDelete, 0, sizeof dpWithoutDelete);
dpWithDelete[0] = dpWithoutDelete[0] = arr[0];
maxVal = arr[0];
for(int i = 1; i < n; i++)
{
dpWithoutDelete[i] = max(arr[i], dpWithoutDelete[i - 1] + arr[i]);
}
dpWithDelete[1] = max(arr[0], arr[1]);
for(int i = 2; i < n; i++)
{
dpWithDelete[i] = max(dpWithDelete[i - 1] + arr[i], dpWithoutDelete[i - 1]);
}
for(int i = 1; i < n; i++)
{
maxVal = max(maxVal, max(dpWithDelete[i], dpWithoutDelete[i]));
}
cout << maxVal << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100000;
long long dpWithDelete[N + 1], dpWithoutDelete[N + 1];
int main()
{
int t, n;
long long arr[N + 1];
long long maxVal;
cin >> t;
while(t--)
{
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
memset(dpWithDelete, 0, sizeof dpWithDelete);
memset(dpWithoutDelete, 0, sizeof dpWithoutDelete);
dpWithDelete[0] = dpWithoutDelete[0] = arr[0];
maxVal = arr[0];
for(int i = 1; i < n; i++)
{
dpWithoutDelete[i] = max(arr[i], dpWithoutDelete[i - 1] + arr[i]);
}
dpWithDelete[1] = max(arr[0], arr[1]);
for(int i = 2; i < n; i++)
{
dpWithDelete[i] = max(dpWithDelete[i - 1] + arr[i], dpWithoutDelete[i - 1]);
}
for(int i = 1; i < n; i++)
{
maxVal = max(maxVal, max(dpWithDelete[i], dpWithoutDelete[i]));
}
cout << maxVal << endl;
}
return 0;
}
Comments
Post a Comment