Iterate over all substrings, starting with the longest ones, and for each one count the number of appearances. The complexity is O(L4) with a small multiplicative constant.
It's clear that at least one person (the one with the least number of friends) will have to leave. We claim that at least two persons will leave. Indeed, suppose that only one person left, and he had d friends. Then all other people had more than d friends before he left, and after that they had less than d + 1 friends, i.e. not more than d. So, his leaving influenced the number of friends for every other person, which means that he was friends with everyone: d = N - 1. But he has fewer friends than everyone — a contradiction.
So, the answer is not more than N - 2. We'll prove that it's possible for N - 2 people to stay (of course, if N > 1). The graph of friendship will be the following: take a complete graph on N vertices and delete one edge. Then the degrees of two vertices equal N - 2, and other degrees equal N - 1. After the first two vertices are removed, we have a complete graph on N - 2 vertices, and all degrees equal N - 3, which means that no one else will leave.
Sort the boxes in increasing number of oranges. Count the total number of apples in boxes with odd and even numbers. If the boxes with odd numbers contain at least half of all apples, choose them (there are exactly N boxes with odd numbers). If the boxes with even numbers contain at least half of all apples, take them and the last box (which contains the largest number of oranges). It's easy to see that in both cases the conditions of the task are fulfilled.
Let ABCD be the quadrangle that we're looking for, and K, L, and M be the middle points of equal sides AB, BC and CD, correspondingly. Let M' be the point symmetric to M with respect to L. Triangles BLM' and CLM are equal by two sides and angle, so BM' = CM = BL = BK, i. e. B is the circumcenter of the triangle KLM'.
Knowing B, we can reconstruct the whole quadrangle, using the symmetries with respect to the points K, L, and M, and then check whether it satisfies all conditions.
Note that we don't know which of the given points is L, so we need to check all 3 cases.
Lemma. In one of optimal solutions there are no simple paths of length 3.
Proof. We can remove the middle edge from such a path. The connected component will split into two components of sizes a and b, where a ≥ 2, b ≥ 2, and therefore ab ≥ a + b.
We'll root the tree and calculate recursively the numbers hv = the solution of the problem for the subtree with the root v, and fv = the product of hu for all children of v. If v is a leaf, then hv = fv = 1.
We show how to calculate hv, given the solution for all subtrees of v. Consider the connected component of v in an optimal solution. It follows from the lemma that the component has one of the following types:
1. The single vertex v.
2. The vertex v and several children of v.
3. The vertex v, one child of v — w, and several children of w.
In the first case the result of the game is fv.
In the second case it equals Πfi · Πhj · k = Π(fi / hi) · fv · k, where i iterates over children belonging to the connected component, j iterates over the rest children, and k is the size of the component. Since we want to maximize the result, we're interested in children with the largest value of fi / hi. Therefore, the largest possible result in this case equals the maximum value of Πi ≤ s (fi / hi) · fv · (s + 1), where it's supposed that the children are sorted in descending order of fi / hi.
In the third case we can use a similar reasoning for every child w. The best result will be the maximum of the expression fv · (fw / hw) · Πi ≤ s (fi / hi) · (s + 2) as w iterates over children of v, and s iterates from 1 to the number of children of w; note that the children of w have already been sorted in the previous step.
Therefore, the number of operations necessary to calculate fv is proportional to the total number of children and grandchildren of v, which is less than n. The complexity of the algorithm, then, is O(n2) (ignoring operations with long numbers).