#include #include #include #include int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ //Find out what total overlap is required at the edges //Then compare the costs of using each overlap int arraySize=0; int range=0; int scale=0; int i=0; int overlap=0; int found=0; int first=0; int more=1; long long int count=1; int startPoint=0; unsigned long long int* costArray; unsigned long long int totalCost=0; unsigned long long int totalCost2=0; unsigned long long int lowestCost=0; scanf("%d %d", &arraySize, &range); scale=1+(2*range); costArray=malloc(arraySize*sizeof(unsigned long long int)); for(i=0;iscale) { while(found==0) { if((arraySize-overlap)%scale==0) { found=1; } else { overlap++; } } //Test overlap at one end, then the other startPoint=range+1-overlap; if(startPoint<0) { startPoint=0; } totalCost=costArray[startPoint]; count=startPoint+scale; while(more==1) { totalCost+=costArray[count]; count+=scale; if(count>arraySize) { more=0; } } more=1; totalCost2=costArray[arraySize-startPoint-1]; count=arraySize-startPoint-1-scale; while(more==1) { totalCost2+=costArray[count]; count-=scale; if(count<0) { more=0; } } if(totalCost>totalCost2) { lowestCost=totalCost2; } else { lowestCost=totalCost; } } else { //find minimum cost lowestCost=999999999999; for(i=0;i