Skip to content

Instantly share code, notes, and snippets.

@IvanIsCoding
Last active July 11, 2017 15:25
Show Gist options
  • Select an option

  • Save IvanIsCoding/40fea52e46daee6f445eaa87d05f634c to your computer and use it in GitHub Desktop.

Select an option

Save IvanIsCoding/40fea52e46daee6f445eaa87d05f634c to your computer and use it in GitHub Desktop.
Seletiva IOI 2014
// Ivan Carvalho
// Inversor - Seletiva IOI - OBI 2014
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN = 100010;
const ll NEG = -1*(1e13);
ll soma[MAXN],best[MAXN],resp;
int main(){
ll N,K;
resp = NEG;
scanf("%lld %lld",&N,&K);
for(ll i=1;i<=N;i++){
scanf("%lld",&soma[i]);
soma[i] = soma[i-1] - soma[i];
}
for(ll i=1;i<=N+1;i++) best[i] = NEG;
ll fim = N;
ll ini = N - K + 1;
while(ini >= 1){
ll davez = soma[fim] - soma[ini-1];
best[ini] = max(best[ini+1],davez);
ini--;
fim--;
}
ini = 1;
fim = K;
while(fim <= N){
ll davez = soma[fim] - soma[ini-1];
resp = max(resp,davez + best[fim+1]);
ini++;
fim++;
}
printf("%lld\n",resp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment