upper bound $\sum_{i=1}^n a_i (a_i-1)/2$ using a function of $\sum_{i=1}^n a_i$ Given $a_i \in \mathbb{N}\cup\{0\}$ and 
define 
$$
A(n) = \sum_{i=1}^n a_i (a_i-1)/2
$$
and 
$$
B(n) = \sum_{i=1}^n a_i
$$
Any ideas how to upper bound $A(n)$ as a "function" of $B(n)$? (the tighter, the better; form of this "function" does not matter). 

Let $f(x)=\dfrac{x(x-1)}{2}$. We
note first that
$$\forall\,x,y\ge0,\quad f(x+y)-f(x)-f(y)=xy\ge0$$
Thus, by induction, for nonnegative numbers $a_1,\ldots,a_n$ we have
$$\sum_{i=1}^n f(a_i)\le f\left(\sum_{i=1}^na_i\right)$$
 Thus,
$$A(n)\le \frac{B(n)(B(n)-1)}{2}$$
With equality if $a_1=\cdots=a_{n-1}=0$ for example. So, this is an optimal inequality.
