Consider a graph, where nodes represent pupils of this school and two nodes are connected by an edge if corresponding pupils are friends. Among all possible partitions of nodes on two sets A and B we choose a partition where S=b⋅SA+a⋅SB is the smallest, where SA and SB denote the amount of edges inside groups A and B. We show that this partition satisfies the required condition.
Indeed, let's assume that there exists a node X with a degree at least a+1 in the group A, then all edges coming from X add at least b(a+1) to the sum S. Also, there are no more than b nodes in the group B that are adjacent to X. Thus if we shift X to the group B, the sum S will decrease by at least b(a+1) and increase by ba. Therefore, S decreases and we have a contradiction with our assumption of choosing the partition. Similarly, there is no node in the group B with a degree ≥b.