Problem:
Pipe Fitter and the Fierce Dogs
You, a proud pipe fitter of ICPC (International Community for Pipe Connection), undertake a new task. The area in which you will take charge of piping work is a rectangular shape with $W$ blocks from west to east and $H$ blocks from north to south. We refer to the block at the $i$-th from west and the $j$-th from north as $(i, j)$. The westernmost and northernmost block is $(1, 1)$, and the easternmost and southernmost block is $(W,H)$. To make the area good scenery, the block $(i, j)$ has exactly one house if and only if both of $i$ and $j$ are odd numbers.
Your task is to construct a water pipe network in the area such that every house in the area is supplied water through the network. A water pipe network consists of pipelines. A pipeline is made by connecting one or more pipes, and a pipeline with l pipes is constructed as follows:
choose a first house, and connect the house to an underground water source with a
special pipe
.
choose an $i$-th house ($2 \leq i \leq l$), and connect the $i$-th house to the ($i - 1$)-th house with a
common pipe
. In this case, there is a condition to choose a next $i$-th house because the area is slope land. Let $(x, y)$ be the block of the ($i - 1$)-th house. An $i$-th house must be located at either $(x - 2, y + 2)$, $(x, y + 2)$, or $(x + 2, y + 2)$. A common pipe connecting two houses must be located at $(x - 1, y + 1)$, $(x, y + 1)$, or $(x + 1, y + 1)$, respectively.
In addition, you should notice the followings when you construct several pipelines:
For each house, exactly one pipeline is through the house.
Multiple pipes can be located at one block.
In your task, common pipes are common, so you can use any number of common pipes. On the other hand, special pipes are special, so the number of available special pipes in this task is restricted under ICPC regulation.
Besides the restriction of available special pipes, there is another factor obstructing your pipe work: fierce dogs. Some of the blocks which do not contain a house seem to be home of fierce dogs. Each dog always stays at his/her home block. Since several dogs must not live at the same block as their home, you can assume each block is home of only one dog, or not home of any dogs.
The figure below is an example of a water pipe network in a 5 $\times$ 5 area with 4 special pipes. This corresponds to the first sample.
Locating a common pipe at a no-dog block costs 1 unit time, but locating a common pipe at a dog-living block costs 2 unit time because you have to fight against the fierce dog. Note that when you locate multiple pipes at the same block, each pipe-locating costs 1 unit time for no-dog blocks and 2 for dog-living blocks, respectively. By the way, special pipes are very special, so locating a special pipe costs 0 unit time.
You, a proud pipe fitter, want to accomplish this task as soon as possible. Fortunately, you get a list of blocks which are home of dogs. You have frequently participated in programmi
...(truncated)

Submitted solutions:

code_snippet: #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// head

const int N=10100;
//map<int,int> dp,v,c;
VI cand,d[N];
int w,h,k,n,x,y,v[N],c[N],dp[N];
//int query(int x) { return (--dp.upper_bound(x))->se;}
int solve(VI x) {
/*	v.clear(); c.clear();
	cand.clear();
	for (auto p:x) {
		if (p%2==0) c[p/2+1]=1;
		else v[p/2+1]=1;
		cand.pb(p/2+1);
		if (p/2+2<=(w+1)/2) cand.pb(p/2+2);
		if (p/2+3<=(w+1)/2) cand.pb(p/2+3);
	}
	cand.pb((w+1)/2);
	cand.pb(0);
	sort(all(cand));
	cand.erase(unique(all(cand)),cand.end());
	dp.clear();
	dp[0]=0;
	rep(x,1,SZ(cand)) {
		int i=cand[x];
		dp[i]=query(i-1)+v[i];
		if (i-2>=0) dp[i]=min(dp[i],query(i-2)+c[i]);
	}
	return dp[w/2+1];*/
	rep(i,1,w/2+2) {
		v[i]=c[i]=0;
	}
	for (auto p:x) {
		if (p%2==0) c[p/2+1]=1; else v[p/2+1]=1;
	}
	dp[0]=0;
	rep(i,1,w/2+2) {
		dp[i]=dp[i-1]+v[i];
		if (i>=2) dp[i]=min(dp[i],dp[i-2]+2*c[i]);
	}
	return dp[w/2+1];
}
int main() {
	scanf("%d%d%d",&w,&h,&k);
	k-=(w+1)/2;
	if (k<0) {
		puts("-1");
		return 0;
	}
	scanf("%d",&n);
	rep(i,0,n) {
		scanf("%d%d",&x,&y);
		if (y%2==0) d[y/2].pb(x);
	}
	int r=0;
	rep(i,1,h/2+1) r+=solve(d[i]);
	int cur=(h/2)*(w/2+1)-r;
	int dd=min(r,k); r-=dd; k-=dd;
	dd=min(cur,k); cur-=dd; k-=dd;
	printf("%d\n",cur+r*2);
}
accuracy: 1
time_ms: 60
memory_kb: 3692
final_rank: 5

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}


// https://ei1333.github.io/luzhiled/snippets/graph/dinic.html
template<typename flow_t>
struct Dinic{
  /*
    復元をするときはfromの頂点から出る辺からのcapを出力．
    is_e == trueの時その辺は元のグラフにあった"本当の"辺．
    "本当の"辺eについてfr -> toにv[e.to][e.ref].capだけフローが流れている．
    二部マッチングの復元の場合，仮想の始点（終点）から（へ）の辺に注意．
  */
  struct edge{
    int to;
    flow_t cap;
    int ref;//逆辺の番号
    bool is_e;//本物の辺かどうか
    int idx;//入力で与えられる辺としてのindex
  };
  vector<vector<edge>> v;
  vector<int> mn_cost,iter;
  const flow_t inf;
  Dinic(int n):inf(numeric_limits<flow_t>::max()),v(n){}
  void add_edge(int fr,int to,flow_t cap,int idx = -1){
    v[fr].emplace_back((edge){to,cap,(int)v[to].size(),true,idx});
    v[to].emplace_back((edge){fr,0,(int)v[fr].size()-1,false,idx});
  }
  bool bfs(int s,int t){
    mn_cost.assign(v.size(),-1);
    queue<int> que;
    mn_cost[s] = 0;
    que.push(s);
    while(!que.empty() && mn_cost[t] == -1){
      int ov = que.front();que.pop();
      for(auto &e : v[ov]){
        if(e.cap > 0 && mn_cost[e.to] == -1){
          mn_cost[e.to] = mn_cost[ov] + 1;
          que.push(e.to);
        }
      }
    }
    return mn_cost[t] != -1;
  }
  flow_t dfs(int ov,const int t,flow_t flow){
    if(ov == t)return flow;
    for(int &i = iter[ov];i < v[ov].size();i++){
      edge &e = v[ov][i];
      if(e.cap > 0 && mn_cost[ov] < mn_cost[e.to]){
        flow_t d = dfs(e.to,t,min(flow,e.cap));
        if(d > 0){
          e.cap -= d;
          v[e.to][e.ref].cap += d;
          return d;
        }
      }
    }
    return 0;
  }
  // O(m n^2)(n:頂点数 m:辺数)
  flow_t max_flow(int s,int t){
    flow_t flow = 0;
    while(bfs(s,t)){
      iter.assign(v.size(),0);
      flow_t f = 0;
      while((f = dfs(s,t,inf)) > 0)flow += f;
    }
    return flow;
  }
  void output(){
    for(int i = 0;i < v.size();i++){
      for(auto &e : v[i]) {
        if(!e.is_e) continue;
        auto &rev_e = v[e.to][e.ref];
        cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << "\n";
      }
    }
  }
};

int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  ll h,w,K;cin >> w >> h >> K;
  int n;cin >> n;
  vector<set<int>> st(h+1);
  rep(i,n){
    ll x,y;cin >> x >> y;
    st[y].insert(x);
  }
  K -= (w+1)/2;
  if(K < 0){
    cout << "-1\n";
    return 0;
  }
  ll res = 0;
  for(int i = 2;i <= h;i+=2){
    set<int> cand;
    for(auto au : st[i]){
      for(int j = -2;j <= 2;j++){
        if((au+j+100)%2 == 1 && au+j >= 1 && au+j <= w)cand.insert(au+j);
      }
    }
    int SZ = sz(cand);
    Dinic<int> dn(SZ*2 + 2);
    int S = SZ*2,T = S+1;
    int cnt = 0;
    for(auto au : cand){
      for(int j = -2;j <= 2;j+=2){
        if(cand.count(au+j) && !st[i].count(au+(j/2))){
          dn.add_edge(cnt,SZ+cnt+(j/2),1);
        }
      }
      cnt++;
    }
    rep(i,SZ){
      dn.add_edge(S,i,1);
      dn.add_edge(SZ+i,T,1);
    }
    ll flw = dn.max_flow(S,T);
    res += (w+1)/2 - cnt + flw;
    flw = cnt-flw;
    int k = min(K,flw);
    K -= k;
    flw -= k;
    res += flw*2;
  }
  if(K)res -= min(res,K);
  cout << res << "\n";
  

  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 9144
final_rank: 12

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

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    int N, M, K;
    cin >> M >> N >> K;
    if ((M+1)/2 > K) {
        cout << -1 << endl;
        return 0;
    }
    K -= (M+1)/2;
    int Q;
    cin >> Q;
    vector<vector<int>> X(N);
    while(Q--) {
        int A, B;
        cin >> B >> A;
        A--, B--;
        X[A].push_back(B);
    }
    int ANS = 0;
    rep(i,0,(N-1)/2) {
        vector<bool> B(M,false);
        for (int j : X[i*2+1]) B[j] = true;
        vector<int> DP((M+1)/2+1,inf);
        DP[0] = 0;
        rep(j,0,(M+1)/2) {
            chmin(DP[j+1], DP[j] + (B[j*2] ? 1 : 0));
            if (j+2 < (M+1)/2+1) chmin(DP[j+2], DP[j] + (B[j*2+1] ? 2 : 0));
        }
        ANS += DP[(M+1)/2];
    }
    int ANS2 = ((N-1)/2)*((M+1)/2)-ANS;
    int MIN = min(ANS, K);
    K -= MIN;
    ANS -= MIN;
    MIN = min(ANS2, K);
    ANS2 -= MIN;
    cout << ANS2 + ANS * 2 << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 4208
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> pi;
typedef pair<ll,pi> ppi;
#define FOR(i,l,r) for(ll i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
#define sz(A) ((ll)A.size())
#define ALL(A) A.begin(),A.end()
#define LB(A,x) (lower_bound(ALL(A),x)-A.begin())
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
struct segtree{
    vi tree;
    int n;
    ll op(ll a,ll b){return max(a,b);}
    ll e(){return -1e18;}
    segtree(int _N){
        n=1;
        while(n<_N)n<<=1;
        tree.assign(2*n,e());
    }
    void set(int i,ll x){
        i+=n;
        tree[i]=x;
        while(i>1){
            i>>=1;
            tree[i]=op(tree[2*i],tree[2*i+1]);
        }
    }
    ll prod(int l,int r){
        l+=n;r+=n;
        ll L=e(),R=e();
        while(l<r){
            if(l%2)L=op(L,tree[l++]);
            if(r%2)R=op(tree[--r],R);
            l>>=1;r>>=1;
        }
        return op(L,R);
    }
};
int main(){
    ll W,H,K,N;cin>>W>>H>>K>>N;
    vvi D(H);
    REP(i,N){
        ll x,y;cin>>x>>y;x--;y--;
        if(y%2)D[y].emplace_back(x);
    }
    if(K<(W+1)/2){cout<<-1<<endl;return 0;}
    K-=(W+1)/2;
    ll ans=0;
    vi DP(W/2+2,1e18);
    REP(i,H/2){
        auto A=D[2*i+1];
        if(W==1){
            if(sz(A))ans++;
            continue;
        }
        vi B,C;
        for(auto i:A){
            if(i%2)C.emplace_back(i/2);
            else B.emplace_back(i/2);
        }
        sort(ALL(B));
        sort(ALL(C));
        vi X;
        FOR(j,-5,6){
            for(auto k:B){
                if(0<=k+j&&k+j<=(W+1)/2)X.emplace_back(k+j);
            }
            for(auto k:C){
                if(0<=k+j&&k+j<=(W+1)/2)X.emplace_back(k+j);
            }
        }
        sort(ALL(X));
        X.erase(unique(ALL(X)),X.end());
        REP(i,sz(X)){
            ll j=i;
            while(i<sz(X)&&X[i]==X[j]+i-j)i++;
            FOR(k,j,i)DP[X[k]]=1e18;
            DP[X[j]]=0;
            FOR(k,j,i-1){
                if(binary_search(ALL(C),X[k])==false&&k+2<i)DP[X[k+2]]=min(DP[X[k+2]],DP[X[k]]);
                DP[X[k+1]]=min(DP[X[k+1]],DP[X[k]]+binary_search(ALL(B),X[k]));
            }
            ans+=DP[X[i-1]];
            i--;
        }
        // DP[0]=0;
        // REP(i,W/2+1){
        //     if(i&&!binary_search(ALL(C),i-1))DP[i+1]=DP[i-1];
        //     DP[i+1]=min(DP[i+1],DP[i]+binary_search(ALL(B),i));
        // }
        // ans+=DP.back();
        //i->i+1 にコスト　B
        //i->i+2　はNG
        //Aのうち最小で何個踏むことになるのか 
    }
    ll total=(H/2)*((W+1)/2);
    if(K>=ans)cout<<max(0LL,total-K)<<endl;
    else cout<<total+ans-2*K<<endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 6268
final_rank: 6

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

using namespace std;

int dog[10001][10001];
int dp[10010];

int main() {
    int W, H, K;
    scanf("%d%d%d", &W, &H, &K);
    int N;
    scanf("%d", &N);
    int x, y;
    for (int i = 1; i <= N; i++) {
        scanf("%d%d", &y, &x);
        dog[x][y] = 1;
    }
    if (K < W / 2 + 1) { // Если не хватило для каждой верхней трубы
        printf("-1\n");
        return 0;
    }
    int ans = 0;
    for (int i = 2; i < H; i += 2) {
        memset(dp, 0x3f, sizeof(dp));
        dp[0] = 0;
        // Считается только доп время за трубы.
        for (int j = 1; j <= W; j += 2) {
            // Напрямую сверху {
            dp[j] = dog[i][j];
            if (j > 1) {
                dp[j] += dp[j-2];
            }
            // }
            // Крест накрест {
            if (j > 3) {
                dp[j] = min(dp[j], dp[j-4] + 2 * dog[i][j-1]);
            } else if (j > 1) {
                dp[j] = min(dp[j], 2 * dog[i][j-1]);
            }
            // }
        }
        ans += dp[W];
    }
    K -= W / 2 + 1;
    int num = (H / 2) * (W / 2 + 1) - ans;
    if (K <= ans) {
        ans -= K;
    } else {
        K -= ans;
        ans = 0;
        if (K <= num) {
            num -= K;
        } else {
            K -= num;
            num = 0;
        }
    }
    printf("%d\n", ans * 2 + num);
}
accuracy: 1
time_ms: 130
memory_kb: 268172
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

// ===================================================================== Solve Function =====================================================================
pair<int, int> SubSolve(int N, vector<bool> L1, vector<bool> L2) {
    // Step 1. First Division
    vector<int> vec;
    vec.push_back(0);
    for (int i = 0; i < N; i++) {
        if (L2[i] == true) vec.push_back(i + 1);
    }
    vec.push_back(N);

    // Step 2. Second Greedy
    vector<int> cons(N + 1, 0);
    for (int i = 0; i < (int)vec.size() - 1; i++) {
        if ((vec[i + 1] - vec[i]) % 2 == 0) continue;
        int cnt = 0;
        for (int j = vec[i]; j < vec[i + 1]; j++) {
            if (L1[j] == false && (j - vec[i]) % 2 == 0) cnt += 1;
        }
        if (cnt >= 1) continue;

        // Special Case
        int num = 0; if (vec[i] >= 1) num = cons[vec[i] - 1];
        int idx = -1;
        if (num % 2 == 0) idx = vec[i + 1] - 1;
        if (num % 2 == 1) idx = vec[i];
        cons[idx] = num + 1;
    }

    // Step 3. Calculate
    int ret1 = 0;
    int ret2 = 0;
    for (int i = 0; i < N + 1; i++) {
        if (cons[i] >= 1 && cons[i + 1] == 0) {
            ret1 += (cons[i] / 2);
            ret2 += (cons[i] % 2);
        }
    }
    return make_pair(ret1, ret2);
}

// ===================================================================== Main Function =====================================================================
int W;
int H;
int K;
int N, X[1 << 19], Y[1 << 19];
bool Dog[10009][10009];

int main() {
    // Step 1. Input
    cin >> W >> H >> K;
    cin >> N;
    for (int i = 1; i <= N; i++) cin >> Y[i] >> X[i];
    for (int i = 1; i <= N; i++) Dog[X[i]][Y[i]] = true;

    // Step 2. Get Answer
    int Answer1 = 0;
    int Answer2 = 0;
    for (int i = 2; i <= H; i += 2) {
        vector<bool> L1((W + 1) / 2, false);
        vector<bool> L2((W - 1) / 2, false);
        for (int j = 1; j <= W; j++) {
            if (Dog[i][j] == false) continue;
            if (j % 2 == 1) L1[(j - 1) / 2] = true;
            if (j % 2 == 0) L2[(j - 1) / 2] = true;
        }
        pair<int, int> ret = SubSolve(L1.size(), L1, L2);
        Answer1 += ret.first;
        Answer2 += ret.second;
    }
    
    // Step 3. Output
    if (K < ((W + 1) / 2)) {
        cout << "-1" << endl;
    }
    else {
        int cur = 2 * Answer1 + 1 * Answer2;
        int rem = K - ((W + 1) / 2);
        int FinalAns = 0;
        if (rem < Answer1) FinalAns = cur - 2 * rem;
        else if (rem < Answer1 + Answer2) FinalAns = (Answer1 + Answer2 - rem);
        else FinalAns = 0;
        cout << max(0, ((H + 1) / 2) * ((W + 1) / 2) - K) + FinalAns << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 103696
final_rank: 17

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <cstdint>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <fstream>
#include <cmath>
#include <limits>
#include <chrono>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
#include <cassert>
#include <random>
//#include <bit>
#include <cstdint>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll = long long;




ll ll_gcd(ll a, ll b) {
	if (a < b) return ll_gcd(b, a);
	ll r;
	while ((r = a % b)) {
		a = b;
		b = r;
	}
	return b;
}

long long modpow(long long a, long long n, long long mod) {
	if (n < 0)return 0;
	long long res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

long long modinv(long long a, long long mod) {
	return modpow(a, mod - 2, mod);
}

ll merge_cnt(vector<ll>& a) {
	int n = a.size();
	if (n <= 1) { return 0; }

	ll cnt = 0;
	vector<ll> b(a.begin(), a.begin() + n / 2);
	vector<ll> c(a.begin() + n / 2, a.end());

	cnt += merge_cnt(b);
	cnt += merge_cnt(c);

	int ai = 0, bi = 0, ci = 0;
	while (ai < n) {
		if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) {
			a[ai++] = b[bi++];
		}
		else {
			cnt += n / 2 - bi;
			a[ai++] = c[ci++];
		}
	}
	return cnt;
}

template< typename T >
size_t longest_increasing_subsequence(const vector< T >& a, bool strict) {
	vector< T > lis;
	for (auto& p : a) {
		typename vector< T >::iterator it;
		if (strict) it = lower_bound(begin(lis), end(lis), p);
		else it = upper_bound(begin(lis), end(lis), p);
		if (end(lis) == it) lis.emplace_back(p);
		else *it = p;
	}
	return lis.size();
}


constexpr uint32_t PrimitiveRoot(uint32_t mod) {
	using u64 = uint64_t;
	if (mod == 2) return 1;
	u64 ds[32] = {};
	int idx = 0;
	u64 m = mod - 1;
	for (u64 i = 2; i * i <= m; ++i) {
		if (m % i == 0) {
			ds[idx++] = i;
			while (m % i == 0) m /= i;
		}
	}
	if (m != 1) ds[idx++] = m;

	uint32_t pr = 2;
	while (1) {
		int flg = 1;
		for (int i = 0; i < idx; ++i) {
			u64 a = pr, b = (mod - 1) / ds[i], r = 1;
			while (b) {
				if (b & 1) r = r * a % mod;
				a = a * a % mod;
				b >>= 1;
			}
			if (r == 1) {
				flg = 0;
				break;
			}
		}
		if (flg == 1) break;
		++pr;
	}
	return pr;
}


struct edge { ll to; ll cost; };
typedef pair<ll, ll> P;
struct graph {
	ll V;
	vector<vector<edge> > G;
	vector<ll> d;

	graph(ll n) {
		init(n);
	}

	void init(ll n) {
		V = n;
		G.resize(V);
		d.resize(V);
		for (int i = 0; i < V; i++) {
			d[i] = 2000000000000000000;
		}
	}

	void add_edge(ll s, ll t, ll cost) {
		edge e;
		e.to = t, e.cost = cost;
		G[s].push_back(e);
	}

	void dijkstra(ll s) {
		for (int i = 0; i < V; i++) {
			d[i] = 2000000000000000000;
		}
		d[s] = 0;
		priority_queue<P, vector<P>, greater<P> > que;
		que.push(P(0, s));
		while (!que.empty()) {
			P p = que.top(); que.pop();
			ll v = p.second;
			if (d[v] < p.first) continue;
			for (auto e : G[v]) {
				if (d[e.to] > d[v] + e.cost) {
					d[e.to] = d[v] + e.cost;
					que.push(P(d[e.to], e.to));
				}
			}
		}
	}
};

struct UnionFind {
	vector <ll> par;
	vector <ll> siz;
	UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
		for (ll i = 0; i < sz_; ++i) par[i] = i;
	}
	void init(ll sz_) {
		par.resize(sz_);
		siz.assign(sz_, 1LL);
		for (ll i = 0; i < sz_; ++i) par[i] = i;
	}
	ll root(ll x) {
		while (par[x] != x) {
			x = par[x] = par[par[x]];
		}
		return x;
	}
	bool merge(ll x, ll y) {
		x = root(x);
		y = root(y);
		if (x == y) return false;
		if (siz[x] < siz[y]) swap(x, y);
		siz[x] += siz[y];
		par[y] = x;
		return true;
	}

	bool issame(ll x, ll y) {
		return root(x) == root(y);
	}

	ll size(ll x) {
		return siz[root(x)];
	}
};

//using mint = modint998244353;


long long extGCD(long long a, long long b, long long& x, long long& y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	long long d = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return d;
}

int main() {
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	ll w, h, k;
	cin >> h >> w >> k;
	ll n;
	cin >> n;
	vector<vector<ll>> z(w,{-1,h});
	for (int i = 0; i < n; i++) {
		ll x, y;
		cin >> x >> y;
		y--;
		x--;
		z[y].push_back(x);
	}
	ll ans = 0;
	for (int i = 0; i < w; i++) {
		if (i % 2 == 0)continue;
		sort(z[i].begin(), z[i].end());
		vector<vector<ll>> a(1);
		ll now = 0;
		for (int j = 0; j < z[i].size(); j++) {
			a[now].push_back(z[i][j]);
			if (z[i][j] != h && z[i][j] != -1 && z[i][j] % 2 == 1) {
				now++;
				a.push_back({});
				a[now].push_back(z[i][j]);
			}
		}
			for (int ij = 0; ij < a.size(); ij++) {
				ll t = 1;
				if ((a[ij][a[ij].size() - 1] - a[ij][0]) % 4 == 0)t = 0;
				set<ll> p;
				for (int k = 1; k < a[ij].size(); k++) {
					p.insert(a[ij][k]);
				}
				for (int ks = a[ij][0] +1; ks < a[ij][a[ij].size() - 1]; ks+=4) {
					if (!p.count(ks)) {
						t = 0;
						break;
					}
				}
				ans += t;
			}
		
	}
	if (h / 2 + 1 > k)cout << -1 << endl;
	else if (ans + h / 2 + 1 < k) {
		cout << max((ll)(h / 2+1) * (w / 2+1) - k, (ll)0) << endl;
	}
	else {
		cout << (h / 2 + 1) * (w / 2 + 1) + (ans)-2 * (k)+h/2+1 << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 4872
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
using ll = long long;
template<class T> bool chmin(T &a,const T b){if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a,const T b){if(a<b){a=b;return 1;}return 0;}
const int INF = (1<<30)-1;
#define all(p) p.begin(),p.end()
const int mod=998244353;

int main(){
	int W,H,K;
	cin>>W>>H>>K;
	int N;
	cin>>N;
	vector<vector<int>> p(H/2);
	rep(i,0,N){
		int x,y;
		cin>>x>>y;
		if(y%2) continue;
		p[y/2-1].push_back(x-1);
	}
	int ans=0;
	rep(i,0,H/2){
		vector<int> dp(W/2+3,INF);
		p[i].push_back(INF);
		sort(all(p[i]));
		int ind=0;
		dp[0]=0;
		rep(j,0,W/2+1){
			if(p[i][ind]==j*2){
				chmin(dp[j+1],dp[j]+1);
				ind++;
			}else{
				chmin(dp[j+1],dp[j]);
			}
			if(p[i][ind]==j*2+1){
				chmin(dp[j+2],dp[j]+2);
				ind++;
			}else{
				chmin(dp[j+2],dp[j]);
			}
		}
		ans+=dp[W/2+1];
	}
	if(K<W/2+1){
		cout<<"-1\n";
		return 0;
	}
	ans=max(0,ans-(K-(W/2+1)));
	ans+=max(0,(W/2+1)*(H/2+1)-K);
	cout<<ans<<"\n";
}
accuracy: 1
time_ms: 70
memory_kb: 3956
final_rank: 10

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int w,h,k; cin >> w >> h >> k;
    int n; cin >> n;
    h = (h+1)/2;
    w = (w+1)/2;
    if(w > k){
        cout << -1 << endl;
        return 0;
    }
    vector<vector<int>> v(h-1);
    for(int i=0;i<n;i++){
        int x,y; cin >> x >> y;
        if(y%2 == 0){
            v[y/2-1].push_back(x-1);
        }
    }
    constexpr int mx = 1e9;
    int cost = 0; // 2
    vector<int> dp(w+1,mx);
    for(int i=0;i+1<h;i++){
        if(!v[i].size()) continue;
        fill(dp.begin(), dp.end(), mx);
        dp[0] = 0;
        vector<bool> down(w),cross(w);
        for(int j:v[i]){
            if(j%2 == 0) down[j/2] = true;
            else cross[j/2] = true;
        }
        for(int j=0;j<w;j++){
            dp[j+1] = min(dp[j+1], dp[j]+down[j]);
            if(j+1<w){
                dp[j+2] = min(dp[j+2], dp[j]+cross[j]+cross[j]);
            }
        }
        cost += dp.back();
    }
    int res = (h-1)*w;
    k -= w;
    if(k >= cost){
        res -= k;
    }
    else{
        res += (cost-k);
        res -= k;
    }
    cout << max(0,res) << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3988
final_rank: 8

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int w,h,k; cin >> w >> h >> k;
    int n; cin >> n;
    h = (h+1)/2;
    w = (w+1)/2;
    if(w > k){
        cout << -1 << endl;
        return 0;
    }
    vector<vector<int>> v(h-1);
    for(int i=0;i<n;i++){
        int x,y; cin >> x >> y;
        if(y%2 == 0){
            v[y/2-1].push_back(x-1);
        }
    }
    constexpr int mx = 1e9;
    int cost = 0; // 2
    for(int i=0;i+1<h;i++){
        if(!v[i].size()) continue;
        vector<int> dp(w+1,mx);
        dp[0] = 0;
        vector<bool> down(w),cross(w);
        for(int j:v[i]){
            if(j%2 == 0) down[j/2] = true;
            else cross[j/2] = true;
        }
        for(int j=0;j<w;j++){
            dp[j+1] = min(dp[j+1], dp[j]+down[j]);
            if(j+1<w){
                dp[j+2] = min(dp[j+2], dp[j]+cross[j]+cross[j]);
            }
        }
        cost += dp.back();
    }
    int res = (h-1)*w;
    k -= w;
    if(k >= cost){
        res -= k;
    }
    else{
        res += (cost-k);
        res -= k;
    }
    cout << max(0,res) << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3960
final_rank: 7

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

using namespace std;

#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)

#define popcount __builtin_popcount
#define next __next
#define prev __prev

#ifndef LOCAL
#define dmp(...) ;
#else
#define dmp(...)                                                               \
  cerr << "[ " << #__VA_ARGS__ << " ] : " << dump_str(__VA_ARGS__) << endl
#endif

// ---------------- Utility ------------------

template <class T>
bool chmin(T &a, const T &b) {
  if (a <= b) return false;
  a = b;
  return true;
}

template <class T>
bool chmax(T &a, const T &b) {
  if (a >= b) return false;
  a = b;
  return true;
}

template <class T>
using MaxHeap = priority_queue<T>;

template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

// ----------------- Input -------------------

template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}

// ----------------- Output ------------------

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << ',' << p.second;
}

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &e : v) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const deque<T> &d) {
  for (const T &e : d) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{ ";
  for (const T &e : s) os << e << " ";
  return os << "}";
}

template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  os << "{ ";
  for (const auto &[key, val] : m) os << "( " << key << " -> " << val << " ) ";
  return os << "}";
}

template <class TupleTy, size_t... I>
void dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {
  (..., (os << (I == 0 ? "" : ",") << std::get<I>(t)));
}

template <class... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  os << "(";
  dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());
  return os << ")";
}

void dump_str_rec(ostringstream &) {}

template <class Head, class... Tail>
void dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << ", " << head;
  dump_str_rec(oss, forward<Tail>(tail)...);
}

template <class T, class... U>
string dump_str(T &&arg, U &&... args) {
  ostringstream oss;
  oss << arg;
  dump_str_rec(oss, forward<U>(args)...);
  return oss.str();
}

// --------------- Fast I/O ------------------

void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

// ------------------ ACL --------------------

// #include <atcoder/modint>
// constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
// using mint = atcoder::static_modint<MOD>;
// ostream &operator<<(ostream &os, const mint &v) {
//   return os << v.val();
// }

// ------------ End of template --------------

#define endl "\n"
using ll = long long;
using pii = pair<int, int>;

const bool multitest = false;

void solve() {
  ll W, H, K;
  cin >> W >> H >> K;
  int N;
  cin >> N;

  set<int> us;
  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    us.insert(y * (int)W + x);
  }

  ll w = (W + 1) / 2;
  ll h = (H + 1) / 2;
  if (K < w) {
    cout << -1 << endl;
    return;
  }

  if (K >= w * h) {
    cout << 0 << endl;
    return;
  }

  K -= w;

  ll need_dog = 0ll;

  auto check = [&](int X, int Y) {
    if (us.count(X * (int)W + Y))
      return true;
    else
      return false;
  };

  for (int i = 3; i <= H; i += 2) {
    vector<ll> dp(w + 1, INF);
    dp[0] = 0;
    for (int j = 0; j < w; j++) {
      if (check(i - 1, 2 * j + 1))
        chmin(dp[j + 1], dp[j] + 1);
      else
        chmin(dp[j + 1], dp[j]);
      if (j + 2 <= w) {
        if (check(i - 1, 2 * j + 2))
          chmin(dp[j + 2], dp[j] + 2);
        else
          chmin(dp[j + 2], dp[j]);
      }
    }
    dmp(dp[w]);
    need_dog += dp[w];
  }

  ll need_common = (h - 1) * w - need_dog;

  {
    ll val = min(need_dog, K);
    need_dog -= val;
    K -= val;
  }

  {
    ll val = min(need_common, K);
    need_common -= val;
    K -= val;
  }

  cout << need_common + need_dog * 2ll << endl;
  return;
}

int main() {
  fastio();
  if (!multitest) {
    solve();
  } else {
    cerr << "[Warning] Multi testcase mode on" << endl;
    int t;
    cin >> t;
    while (t--) solve();
  }
  return 0;
}
accuracy: 1
time_ms: 1050
memory_kb: 8100
final_rank: 18

code_snippet: /*	author:  Kite_kuma
	created: 2021.11.03 12:07:40 */

// #ifdef LOCAL
// #define _GLIBCXX_DEBUG
// #endif
#include <bits/stdc++.h>
using namespace std;

#pragma region macros

#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcnt(n) __builtin_popcountll((long long)n)

#define REPname(a, b, c, d, e, ...) e
#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))

#define DREPname(a, b, c, d, e, ...) e
#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)
#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))

#pragma endregion

#pragma region aliases

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vvvi = std::vector<std::vector<std::vector<int>>>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
template <class T = ll>
using V = std::vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
template <class T = ll>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T = ll>
using pqdn = std::priority_queue<T>;

#pragma endregion

#pragma region constants

const int inf = 1e9;
const long long INF = 1e18;
const long double pi = acos(-1);
const char dl = '\n';
const char sp = ' ';
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int mod_1000000007 = 1000000007;
const int mod_998244353 = 998244353;

#pragma endregion

#pragma region basic_operation

template <class T0, class T1, class T2>
inline bool in_range(T0 x, T1 lef, T2 rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool f = 1) { std::cout << (f ? "Yes" : "No") << '\n'; }
void No() { std::cout << "No\n"; }
void YES(bool f = 1) { std::cout << (f ? "YES" : "NO") << '\n'; }
void NO() { std::cout << "NO\n"; }

template <class T>
void drop(T answer) {
	std::cout << answer << '\n';
	exit(0);
}

void err(bool flag = true) {
	if(!flag) return;
	std::cout << -1 << '\n';
	exit(0);
}

template <class T>
void vout(std::vector<T> const &v, bool vertically = 0) {
	if(vertically) {
		for(auto const &a : v) {
			std::cout << a << '\n';
		}
		return;
	}
	for(auto it = v.begin(); it != v.end(); it++) {
		std::cout << (*it);
		if(it != v.end() - 1) {
			std::cout << ' ';
		}
	}
	std::cout << '\n';
	return;
}

inline void print() { std::cout << '\n'; }
template <class T>
inline void print(T x) {
	std::cout << x << '\n';
	return;
}
template <typename Head, typename... Tail>
void print(Head H, Tail... T) {
	std::cout << H << " ";
	print(T...);
}

template <class T>
void add(std::vector<T> &v, T val) {
	for(auto &a : v) a += val;
	return;
}

template <class T>
T dup(T a, T b) {
	assert(b != 0);
	return (a + b - 1) / b;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

long long POW(long long a, long long n) {
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	assert(n >= 0 && mod);
	if(mod == 1) return 0LL;
	long long res = 1;
	a %= mod;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res < 0 ? res + mod : res;
}

// return x which satisfies a * x % mod == gcd(a, mod)
// not (mod | a)
long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		std::swap(a, b);
		u -= t * v;
		std::swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

template <class T>
int lb(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));
}
template <class T>
int ub(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));
}
template <class T>
void unq_sort(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<int> press(std::vector<T> &a) {
	auto vec = a;
	unq_sort(vec);
	std::vector<int> ret;
	for(auto &v : a) ret.push_back(lb(vec, v));
	return ret;
}

#pragma endregion

#pragma region input
#define VEC(type, name, size) \
	vector<type> name(size);  \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHR(...)      \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DBL(...)        \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define TPL3(type0, type1, type2, name)   \
	std::tuple<type0, type1, type2> name; \
	scanner::INPUT(name);
#define TPL4(type0, type1, type2, type3, name)   \
	std::tuple<type0, type1, type2, type3> name; \
	scanner::INPUT(name);

namespace scanner {
template <class T>
void scan(T &a) {
	std::cin >> a;
}

template <class T, class L>
void scan(std::pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}

template <class T0, class T1, class T2>
void scan(std::tuple<T0, T1, T2> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	scan(t0);
	scan(t1);
	scan(t2);
	p = std::make_tuple(t0, t1, t2);
}

template <class T0, class T1, class T2, class T3>
void scan(std::tuple<T0, T1, T2, T3> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	T3 t3;
	scan(t0);
	scan(t1);
	scan(t2);
	scan(t3);
	p = std::make_tuple(t0, t1, t2, t3);
}

template <class T>
void scan(std::vector<T> &a) {
	for(auto &i : a) scan(i);
}

void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner

template <typename T1, typename T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
#pragma endregion

#pragma region debug

#pragma region output
template <typename T1, typename T2>
std::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {
	for(int i = 0; i < (int)v.size(); i++) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
#pragma endregion

#pragma region view

namespace viewer {
void view(const long long e) {
	if(e == INF)
		std::cerr << "INF";
	else if(e == -INF)
		std::cerr << "-INF";
	else
		std::cerr << e;
}

void view(const int e) {
	if(e == inf)
		std::cerr << "inf";
	else if(e == -inf)
		std::cerr << "-inf";
	else
		std::cerr << e;
}

template <typename T>
void view(const T e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ")";
}

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ")";
}

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ", ";
	view(std::get<3>(p));
	std::cerr << ")";
}

template <typename T>
void view(const std::set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		std::cerr << ", ";
		view(c.second);
		std::cerr << ")\n";
	}
	std::cerr << "}";
}

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}
}  // namespace viewer

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                      \
	do {                                             \
		std::cerr << __LINE__ << " " << #x << " : "; \
		viewer::view(x);                             \
		std::cerr << '\n';                           \
	} while(0)

#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

ll solve(int w) {
	int h;
	ll k;
	cin >> h >> k;
	int n;
	cin >> n;
	V<pair<int, int>> dogs;
	rep(n) {
		int x, y;
		cin >> y >> x;
		dogs.emplace_back(x, y);
	}
	sort(all(dogs));

	ll houses = ll(dup(h, 2)) * ll(dup(w, 2));
	ll cost = dup(w, 2);
	if(cost > k) return -1;
	houses -= cost;
	k -= cost;
	cost = 0;

	for(auto it = dogs.begin(); it != dogs.end();) {
		int x = it->first;
		vi ys;
		while(it != dogs.end() && it->first == x) {
			ys.push_back((it++)->second);
		}
		debug(x, ys);
		if(x & 1) continue;

		set<int> yset(ys.begin(), ys.end());

		int pre_notvalid = -1;
		foa(y, ys) {
			if(y & 1) {
				if(pre_notvalid == y) {
					continue;
				} else if(!yset.count(y - 1) && pre_notvalid != y - 2) {
					// ok
				} else if(!yset.count(y + 1) && y + 2 <= w) {
					pre_notvalid = y + 2;
				} else {
					cost++;
				}
			} else {
				// pass
			}
		}
	}

	debug(houses, cost, k);

	ll ret = 0;
	ll special_two = min(cost, k);
	k -= special_two;
	houses -= cost;
	cost -= special_two;
	ret += cost * 2 + max(0LL, houses - k);
	return ret;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout << std::fixed << std::setprecision(15);
	srand((unsigned)time(NULL));

	int w;
	while(cin >> w && w) {
		cout << solve(w) << dl;
	}

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4252
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=100005,INF=1<<30;

vector<int> wh[MAX];

int dp[MAX];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int H,W,K;cin>>W>>H>>K;
    int N;cin>>N;
    set<pair<int,int>> SE;
    
    for(int i=0;i<N;i++){
        int h,w;cin>>w>>h;
        wh[h].push_back(w+4);
        SE.insert(mp(h,w+4));
    }
    
    int need=0;
    
    for(int i=1;i<=H;i++){
        sort(all(wh[i]));
        int j=0;
        while(j<si(wh[i])){
            int k=j+1;
            while(k<si(wh[i])&&wh[i][k]-wh[i][k-1]<=6) k++;
            int s=wh[i][j]-1,t=wh[i][k-1]+1;
            if(s%2==0) s--;
            if(s<5) s+=2;
            if(t%2==0) t++;
            if(t>W+4) t-=2;
            for(int w=s;w<=t;w++) dp[w]=INF;
            dp[s-2]=0;
            for(int w=s-2;w<t;w+=2){
                if(SE.count(mp(i,w+2))) chmin(dp[w+2],dp[w]+1);
                else chmin(dp[w+2],dp[w]);
                
                if(w+4<=t){
                    if(SE.count(mp(i,w+3))) chmin(dp[w+4],dp[w]+2);
                    else chmin(dp[w+4],dp[w]);
                }
            }
            
            //cout<<j<<" "<<k<<" "<<s<<" "<<t<<" "<<dp[t]<<endl;
            
            need+=dp[t];
            
            j=k;
        }
    }
    
    if(K<(W+1)/2){
        cout<<-1<<endl;
    }else{
        K-=(W+1)/2;
        int al=(H-1)/2*(W+1)/2;
        chmin(K,al);
        int ans=al+need;
        if(need>=K) ans-=2*K;
        else ans-=2*need+(K-need);
        
        cout<<ans<<endl;
        
    }
}
accuracy: 1
time_ms: 40
memory_kb: 11304
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = 10000000;
int main(){
  int W, H, K;
  cin >> W >> H >> K;
  int w = (W + 1) / 2, h = (H + 1) / 2;
  if (K < w){
    cout << -1 << endl;
  } else {
    int N;
    cin >> N;
    vector<vector<int>> p(h);
    for (int i = 0; i < N; i++){
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      if (y % 2 == 1){
        p[(y - 1) / 2].push_back(x);
      }
    }
    int mn = 0;
    for (int i = 0; i < h; i++){
      if (!p[i].empty()){
      	int cnt = p[i].size();
        set<int> st;
        for (int j = 0; j < cnt; j++){;
          st.insert(p[i][j]);
        }
        vector<int> dp(w + 1, INF);
        dp[0] = 0;
        for (int j = 0; j < w; j++){
          if (st.count(j * 2) == 0){
            dp[j + 1] = min(dp[j + 1], dp[j]);
          } else {
            dp[j + 1] = min(dp[j + 1], dp[j] + 1);
          }
          if (j < w - 1){
            if (st.count(j * 2 + 1) == 0){
              dp[j + 2] = min(dp[j + 2], dp[j]);
            } else {
              dp[j + 2] = min(dp[j + 2], dp[j] + 2);
            }
          }
        }
        mn += dp[w];
      }
    }
    int ans = (h - 1) * w + mn;
    K -= w;
    if (K < mn){
      ans -= K * 2;
    } else {
      ans -= mn * 2;
      K -= mn;
      ans = max(ans - K, 0);
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 250
memory_kb: 3884
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = 10000000;
int main(){
  int W, H, K;
  cin >> W >> H >> K;
  int w = (W + 1) / 2, h = (H + 1) / 2;
  if (K < w){
    cout << -1 << endl;
  } else {
    int N;
    cin >> N;
    vector<vector<int>> p(h);
    for (int i = 0; i < N; i++){
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      if (y % 2 == 1){
        p[(y - 1) / 2].push_back(x);
      }
    }
    int mn = 0;
    for (int i = 0; i < h; i++){
      if (!p[i].empty()){
        p[i].insert(p[i].begin(), -1);
        p[i].push_back(W);
        int cnt = p[i].size();
        /*
        vector<int> d(cnt - 1);
        for (int j = 0; j < cnt - 1; j++){
          d[j] = p[i][j + 1] - p[i][j];
          if (d[j] > 8){
            d[j] -= (d[j] - 8) / 2 * 2;
          }
        }
        for (int j = 0; j < cnt - 1; j++){
          p[i][j + 1] = p[i][j] + d[j];
        }
        */
        int W2 = (p[i][cnt - 1] + 1) / 2;
        set<int> st;
        for (int j = 1; j < cnt - 1; j++){;
          st.insert(p[i][j]);
        }
        vector<int> dp(W2 + 1, INF);
        dp[0] = 0;
        for (int j = 0; j < W2; j++){
          if (st.count(j * 2) == 0){
            dp[j + 1] = min(dp[j + 1], dp[j]);
          } else {
            dp[j + 1] = min(dp[j + 1], dp[j] + 1);
          }
          if (j < W2 - 1){
            if (st.count(j * 2 + 1) == 0){
              dp[j + 2] = min(dp[j + 2], dp[j]);
            } else {
              dp[j + 2] = min(dp[j + 2], dp[j] + 2);
            }
          }
        }
        mn += dp[W2];
      }
    }
    int ans = (h - 1) * w + mn;
    K -= w;
    if (K < mn){
      ans -= K * 2;
    } else {
      ans -= mn * 2;
      K -= mn;
      ans = max(ans - K, 0);
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 180
memory_kb: 3724
final_rank: 13

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 22;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

bool exi[10000];
int dp[10005];
void solve() {
	int w, h, k; cin >> w >> h >> k;
	int n; cin >> n;
	vector<vector<int>> vs(h);
	rep(i, n) {
		int x, y; cin >> x >> y; x--; y--;
		vs[y].push_back(x);
	}
	k -= (w/2+1);
	if (k < 0) {
		cout << -1 << "\n"; return;
	}
	int c1 = 0, c2 = 0;
	for (int i = 1; i < h; i += 2) {
		for (int x : vs[i])exi[x] = true;
		for (int j = 0; j < w+2; j += 2) {
			dp[j] = mod;
		}
		dp[0] = 0;
		for (int j = 0; j < w; j += 2) {
			//single
			if (exi[j]) {
				dp[j + 2] = min(dp[j + 2], dp[j] + 1);
			}
			else {
				dp[j + 2] = min(dp[j + 2], dp[j]);
			}
			//double
			if (j + 2 < w) {
				int cost = 0;
				if (exi[j + 1]) {
					cost = 2;
				}
				dp[j + 4] = min(dp[j + 4], dp[j] + cost);
			}
		}
		c2 += dp[w + 1];
		c1 += (w / 2 + 1) - dp[w + 1];
		for (int x : vs[i])exi[x] = false;
	}
	//cout << c1 << " " << c2 << "\n";
	int ans = c1 + 2 * c2;
	int m = min(c2, k);
	ans -= 2 * m; k -= m;
	m = min(c1, k);
	ans -= m;
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//init();
	//expr();
	//int t; cin >> t; rep(i, t)
		//expr();
	solve();
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 69764
final_rank: 16

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int inf = 1e9;

int prev(int a, vector<int> &v){
    auto itr = lower_bound(v.begin(), v.end(), a);
    if(itr == v.begin()) return -inf;
    return *(itr-1);
}
int next(int a, vector<int> &v){
    auto itr = lower_bound(v.begin(), v.end(), a);
    if(itr == v.end()) return inf;
    return *itr;
}
bool isin(int a, vector<int> &v){
    auto itr = lower_bound(v.begin(), v.end(), a);
    return itr!=v.end() and *itr==a;
}

int main(){
    int w,h,k;
    cin >> w >> h >> k;
    w=(w+1)/2; h=h/2;
    if(w > k){
        cout << -1 << endl;
        return 0;
    }
    k -= w;
    int n;
    cin >> n;
    vector<vector<int>> a(h);
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        if(y%2 == 0){
            a[y/2-1].push_back(x-1);
        }
    }
    for(auto& v: a){
        sort(v.begin(), v.end());
    }

    int ans = 0;
    vector<int> dp(w+1, inf);
    for(int i=0; i<h; i++){
        int base = 2*w*(h-i);
        dp[0] = base;
        for(int j=0; j<w; j++){
            int x = 2*j;
            // skip
            int nj = min(next(x, a[i])/2 -1, w);
            int pj = prev(x, a[i])/2;
            if(pj+2<j and j+2<nj){
                dp[nj] = dp[j]+(nj-j);
                j = nj-1;
                continue;
            }
            // normal
            if(j < w-1){
                int cost = (isin(x+1, a[i]))? 4: 2;
                dp[j+2] = min(dp[j+2], dp[j]+cost);
            }
            int cost = (isin(x, a[i]))? 2: 1;
            dp[j+1] = min(dp[j+1], dp[j]+cost);
        }
        ans += dp[w]-base;
    }
    int two = ans -h*w;
    int one = ans -2*two;
    if(k > two){
        ans = max(two +one -k, 0);
    }else{
        ans = 2*(two-k) +one;
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3992
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int inf = 1e9;

int next(int a, vector<int> &v){
    auto itr = lower_bound(v.begin(), v.end(), a);
    if(itr == v.end()) return inf;
    return *itr;
}
bool isin(int a, vector<int> &v){
    auto itr = lower_bound(v.begin(), v.end(), a);
    return itr!=v.end() and *itr==a;
}

int main(){
    int w,h,k;
    cin >> w >> h >> k;
    w=(w+1)/2; h=h/2;
    if(w > k){
        cout << -1 << endl;
        return 0;
    }
    k -= w;
    int n;
    cin >> n;
    vector<vector<int>> a(h);
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        if(y%2 == 0){
            a[y/2-1].push_back(x-1);
        }
    }
    for(auto& v: a){
        sort(v.begin(), v.end());
    }

    int ans = 0;
    vector<int> dp(w+1, inf);
    for(int i=0; i<h; i++){
        int base = 2*w*(h-i);
        dp[0] = base;
        for(int j=0; j<w; j++){
            int x = 2*j;
            // skip
            int nj = min(next(x, a[i])/2 -1, w);
            if(nj > j+2){
                dp[nj] = dp[j]+(nj-j);
                j = nj-1;
                continue;
            }
            // normal
            if(j < w-1){
                int cost = (isin(x+1, a[i]))? 4: 2;
                dp[j+2] = min(dp[j+2], dp[j]+cost);
            }
            int cost = (isin(x, a[i]))? 2: 1;
            dp[j+1] = min(dp[j+1], dp[j]+cost);
        }
        ans += dp[w]-base;
    }
    int two = ans -h*w;
    int one = ans -2*two;
    if(k > two){
        ans = max(two +one -k, 0);
    }else{
        ans = 2*(two-k) +one;
    }
    cout << ans << endl;
    return 0;
}
accuracy: 0.5925925925925926
time_ms: 20
memory_kb: 3832
final_rank: 20

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
using namespace std;
typedef long long int LL;
typedef pair<LL,LL> P;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}

int main(){
    LL n,m;
    int i,j,k;
    LL a,b,c;
    LL w,h;
    LL x,y;
    set<P> s1,s2;
    cin>>w>>h>>m;
    vector<vector<LL>> v1(h);
    cin>>n;
    for(i=0;i<n;i++){
        cin>>a>>b;
        a--,b--;
        s1.insert(make_pair(a,b));
        if(b%2==0)continue;
        a/=2,b=(b+1)/2;
        if(s2.find(make_pair(a,b))==s2.end()){
            v1[b].push_back(a);
            s2.insert(make_pair(a,b));
        }
        a--;
        if(a<0)continue;
        if(s2.find(make_pair(a,b))==s2.end()){
            v1[b].push_back(a);
            s2.insert(make_pair(a,b));
        }
    }
    if(m<=w/2){
        cout<<-1<<endl;
        return 0;
    }
    m-=w/2+1;
    b=0;
    for(i=1;i<h/2+1;i++){
        int t[5]={0};
        a=-10;
        sort(v1[i].begin(),v1[i].end());
        if(v1[i].empty() || v1[i].back()!=w/2)v1[i].push_back(w/2);
        for(auto num:v1[i]){
            if(a+1==num){
                t[0]=t[1],t[1]=t[2],t[2]=INF;
            }else{
                t[0]=min({t[1],t[2]});
                t[1]=t[2]=INF;
            }
            a=num;
            x=num*2,y=i*2;
            if(s1.find(make_pair(x,y-1))!=s1.end())t[1]=min(t[0]+1,t[1]);
            else t[1]=min(t[0],t[1]);
            if(s1.find(make_pair(x+1,y-1))!=s1.end())t[2]=min(t[0]+2,t[2]);
            else t[2]=min(t[0],t[2]);
        }
        b+=t[1];
    }
    a=(w/2+1)*(h/2)-b;
    c=min(b,m);
    b-=c,m-=c;
    a-=min(a,m);
    cout<<a+2*b<<endl;
}
accuracy: 1
time_ms: 140
memory_kb: 18336
final_rank: 14

code_snippet: #include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<list>
#include<bitset>
#include<stack>
#include<memory>
#include<numeric>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const llint mod=1000000007;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-12;
template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}

int main(void){
	int w,h,K,n,i;cin>>w>>h>>K>>n;
	int H=(h+1)/2,W=(w+1)/2;
	vector<pair<int,int>>dog(n+1);
	dog[n]=mp(mod,mod);
	for(i=0;i<n;i++){cin>>dog[i].sec>>dog[i].fir;}
	SO(dog);
	K-=W;
	if(K<0){cout<<-1<<endl;return 0;}
	pair<int,int>mae=mp(-1,-1);
	int ans=0,dp=0,ep=0,fp=0;
	for(auto it:dog){
		if(mae.fir!=it.fir||mae.sec+6<=it.sec){//清算します
			ans+=min({dp,ep,fp});
			dp=0;ep=0;fp=0;
			if(it.sec==1){dp=99;}
			
		}else{
			for(i=mae.sec+1;i<=it.sec;i++){
				if(i%2==1){int sp=min(dp,ep);dp=fp;ep=sp;fp=sp;}
			}
		}
		if(it.sec%2==0){fp+=2;}//中間
		else{ep++;}
		if(it.sec==w){fp=mod;}
		mae=it;
	}
	//cerr<<"ans="<<ans<<endl;
	cout<<max(0,(H-1)*W-K)+max(0,ans-K)<<endl;
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3576
final_rank: 3