Problem: Example

Input

5 5 4
6
3 2
4 2
5 2
1 4
3 4
5 4


Output

6

Solution (in CPP):

Solution:
#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int>P;

int h,w,k,n,x,y,c,ans;
int dp[10099];
set<P>s;

int A(int y,int x){
  return s.count(P(y-1,x))?2:1;
}

int B(int y,int x){
  if(s.count(P(y-1,x-1)))return 4;
  return 2;
}

int main(){
  cin>>w>>h>>k>>n;
  while(n--){
    cin>>x>>y;
    s.insert(P(y,x));
  }
  int t=(w/2+1);
  for(int i=3;i<=h;i+=2){
    memset(dp,0,sizeof(dp));
    dp[1]=A(i,1);
    dp[3]=min(dp[1]+A(i,3),B(i,3));
    for(int j=5;j<=w;j+=2){
      dp[j]=min(dp[j-2]+A(i,j),dp[j-4]+B(i,j));
    }
    ans+=dp[w];
    c+=dp[w]-(w/2+1);
  }
  if(t>k)cout<<-1<<endl;
  else{
    ans-=min(c,k-t)*2;
    if((k-t)>c)ans-=(k-t)-c;
    cout<<max(0,ans)<<endl;
  }
}