#include<bits/stdc++.h>
using namespace std;
int n,a[100001],k, dp[100001];
void nhap()
{
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
cin >> k;
}
void solve()
{
dp[0] = 0, dp[1] = a[1];
if(k == 1)
{
dp[2] = max(a[1], a[2]);
for(int i = 3; i <= n; i++)
{
dp[i] = max(dp[i-2] + a[i], dp[i-1]);
}
if(dp[n] < 0) cout <<0;
else cout << dp[n];
}
else
{
dp[2] = max(a[2], dp[1] + a[2]);
for(int i = 3; i <= n; i++)
{
dp[i] = max(a[i] + a[i-1] + dp[i-3], max(dp[i-2] + a[i], dp[i-1]));
}
if(dp[n] < 0) cout<< 0;
else cout << dp[n];
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
nhap();
solve();
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IG4sYVsxMDAwMDFdLGssIGRwWzEwMDAwMV07CnZvaWQgbmhhcCgpCnsKICAgIGNpbiA+PiBuOwogICAgZm9yKGludCBpID0gMTsgaSA8PSBuOyBpKyspIGNpbiA+PiBhW2ldOwogICAgY2luID4+IGs7Cn0Kdm9pZCBzb2x2ZSgpCnsKICAgIGRwWzBdID0gMCwgZHBbMV0gPSBhWzFdOwogICAgaWYoayA9PSAxKQogICAgewogICAgICAgIGRwWzJdID0gbWF4KGFbMV0sIGFbMl0pOwogICAgICAgIGZvcihpbnQgaSA9IDM7IGkgPD0gbjsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgZHBbaV0gPSBtYXgoZHBbaS0yXSArIGFbaV0sIGRwW2ktMV0pOwogICAgICAgIH0KICAgICAgICBpZihkcFtuXSA8IDApIGNvdXQgPDwwOwogICAgICAgIGVsc2UgY291dCA8PCBkcFtuXTsKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgICBkcFsyXSA9IG1heChhWzJdLCBkcFsxXSArIGFbMl0pOwogICAgICAgIGZvcihpbnQgaSA9IDM7IGkgPD0gbjsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgZHBbaV0gPSBtYXgoYVtpXSArIGFbaS0xXSArIGRwW2ktM10sIG1heChkcFtpLTJdICsgYVtpXSwgZHBbaS0xXSkpOwogICAgICAgIH0KICAgICAgICBpZihkcFtuXSA8IDApIGNvdXQ8PCAwOwogICAgICAgIGVsc2UgY291dCA8PCBkcFtuXTsKICAgIH0KfQppbnQgbWFpbigpCnsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKICAgIG5oYXAoKTsKICAgIHNvbHZlKCk7CiAgICByZXR1cm4gMDsKfQo=