You always want your best programmers on your team, so start by sorting the number of problems each programmer can solve, largest first. Then go through each possible team size k=1..n, and compute: ([sum of the first k programmers] + [kth bonus])/k Find the largest of all of those. Here is some python code: n = int(input()) a = list(map(int, input().split())) s = list(map(int, input().split())) s.sort() ans = 0 tot = 0 for i in range(n): tot += s[n-i-1] tans = (tot + a[i]) / (i + 1) ans = max(ans, tans) print(ans)