graph

First, perform bucket sorting on $$$c_i$$$ to transform it into a strictly monotonically increasing sequence of positive integers. Now all the following $$$c_i$$$ satisfy this assumption.

For each vertex $$$i$$$, we split it into two points $$$i^+$$$ and $$$i^-$$$, with one representing the incoming edge and the other representing the outgoing edge. For example, if there is an edge from $$$1$$$ to $$$2$$$, it becomes an edge between $$$1^-$$$ and $$$2^+$$$. It is not difficult to see that the resulting graph is an undirected bipartite graph.

Let's first ignore the conditions of self-loops, double edges, and bidirectional edges. Then the problem becomes: for an undirected bipartite graph $$$G=X+Y$$$, find $$$|E(G)|_{\min}$$$ such that there exist $$$m$$$ points with degrees of $$$c_1, c_2, \dots, and c_m$$$ respectively.

In fact, we consider the points with degrees $$$c_{m-i+1}, \dots, c_{m-1}, c_m$$$. Suppose there are $$$a$$$ points inside $$$X$$$ and $$$b$$$ points inside $$$Y$$$; then $$$a + b = i$$$. The set $$$E(G)$$$ contains all the edges formed by these points, and the number of these edges is $$$\ge \sum\limits_{k=m-i+1}^m c_k - ab$$$ (total minus the repeated counts) $$$\ge \sum\limits_{k=m-i+1}^m c_k - \left\lfloor\dfrac{i^2}{4}\right\rfloor$$$. Hence, we obtain a lower bound for $$$|E(G)|$$$:

$$$$$$\max_{1 \le i \le m}\{\sum_{k=m-i+1}^m c_k - \left\lfloor\dfrac{i^2}{4}\right\rfloor\}$$$$$$

Next, we construct an example that can obtain this many edges. Let $$$X = \{u_1, \dots, u_n\}$$$ and $$$Y = \{v_1, \dots, v_n\}$$$. Assume that the expression reaches its maximum value at $$$i = t$$$. If the maximum is achieved at multiple points, choose the one with the smallest $$$t$$$. Let $$$a = \left\lceil\dfrac{t}{2}\right\rceil$$$ and $$$b = \left\lfloor\dfrac{t}{2}\right\rfloor$$$.

We select $$$a$$$ points from set $$$X$$$, with the degrees of these points being $$$c_m, c_{m-2}, \dots, c_{m-2(a-1)}$$$, and we select $$$b$$$ points from set $$$Y$$$, with the degrees of these points being $$$c_{m-1}, c_{m-3}, \dots, c_{m-(2b-1)}$$$. Let these points be $$$u_1, \dots, u_a, v_1, \dots, v_b$$$.

Our construction method is as follows: For all $$$1 \le j \le a$$$, $$$u_j$$$ is connected to $$$v_1, v_2, \dots, v_{c_{m-2j+2}}$$$; for all $$$1 \le k \le b$$$, $$$v_k$$$ is connected to $$$u_1, u_2, \dots, u_{c_{m-2k+1}}$$$.

Since the expression reaches its maximum value when $$$i=t$$$, we have $$$c_{m-t} \le \left\lceil\dfrac{t}{2}\right\rceil$$$ (comparing the two expressions when $$$i=t$$$ and $$$i=t+1$$$), and $$$c_{m-t+1} \ge \left\lceil\dfrac{t+1}{2}\right\rceil$$$ (comparing when $$$i=t$$$ with $$$i=t-1$$$ and considering the minimality of $$$t$$$). Hence, in this construction, points with degrees of $$$1, 2, \dots, \left\lceil\dfrac{t}{2}\right\rceil$$$ are necessarily present. Therefore, this construction meets the requirements. Additionally, we observe that when $$$(u_1, u_2, \dots, u_n)=(1, 2, \dots, n)$$$ and $$$(v_1, v_2, \dots, v_n)=(n, n-1, \dots, 1)$$$, this construction can directly handle the "no self-loops, multiple edges, and bidirectional edges" condition in the problem. In fact, it can be noticed that in this construction, there do not exist $$$1 \le p, q \le n$$$ such that $$$p^{-}$$$ is connected to $$$q^{+}$$$ and $$$p^{+}$$$ is also connected to $$$q^{-}$$$.

Over. When $$$opt = 0$$$, the time complexity is $$$\mathcal O(n^2)$$$; when $$$opt = 1$$$, the time complexity is $$$\mathcal O(n + m)$$$.