Maximum bipartite matching

Given bipartite part sizes n1, n2, matching size ans, and minimum degree d, find the maximum possible number of edges, or -1 if impossible.

Medium6GraphGreedyMathCombinatoricsNo attempts yetTime limit2sMemory limit512 MB

Problem

In an undirected graph, a matching is a subset of the edges in which no two edges share a vertex. A maximum matching is a matching of the largest possible size.

Decide whether a graph GG with all of the following properties exists. If it does, find the largest number of edges such a GG can have.

  • GG is a simple undirected graph.
  • GG is bipartite, and its two parts have sizes n1n_1 and n2n_2.
  • The size of a maximum matching of GG is ansans.
  • Every vertex of GG has degree at least dd.

Input

The first line contains n1n_1, n2n_2, ansans, and dd, separated by spaces. (1n1,n21091 \le n_1, n_2 \le 10^9, 1ans,dmin(n1,n2)1 \le ans, d \le \min(n_1, n_2))

Output

If no graph satisfies the conditions, print -1. Otherwise print the largest number of edges such a graph can have.