Not understanding simple mathematical (no fancy stuff) solution to a task The problem sounds like that:

Kevin has N friends and one building. He wants to organize a
  party in that building and he invites exacly 1 friend / day. Kevin
  unfortunately has grumpy neighbors which aren't too happy with the
  noise that Kevin's party does. For that, Kevin wants to minimize the
  noise level of his party. (the noise level is equal to the number of
  friends in the building) In order to do that, after some day he can
  clear the building by asking his friends to leave (he can do that K times, for K different days). (ex: he clears the
  building after day 2 so at day 3 when he invites another friend, for
  day 3 the noise level will be 1 because the past day he cleared the
  building; if he didn't clear the building at day 2, the noise level
  for day 3 will have been 3 (1 from day 2, 1 from day 1 and the new invited friend) and the total noise level for day 1, 2 and 3 would have been 1+2+3=6).

For better understanding the task:
Input:
5 2 (N, K)

Output:
7

Explanation:
In the input example, N friends will be invited at the party, in the following order:
1 (day 1)                                                       1 (day 1)
1 (day 2)    so in the building for each day will be present    2 (day 2)
1 (day 3)   ------------------------------------------------->  3 (day 3)
1 (day 4)          (without clearing the building)              4 (day 4)
1 (day 5)                                                       5 (day 5)
                                                             -----(+)
                                                               15

So, clearing the building 0 times (K=0), the noise level will be 15.
Clearing the building 1 time (K=1), the sum will be:
(0 times)
    1                                              1
 __ 2 __     clearing the building after day 2     2
    3       ----------------------------------->   1
    4                                              2
    5                                              3
                                                 ----(+)
                                                   9

At this case (K=1), another solution could be to clear after day 3, same sum.
Clearing 2 times (K=2):
(0 times)
    1                                                  1
 __ 2 __     clearing the building after day 2 & 3     2
 __ 3 __    -------------------------------------->    1
    4                                                  1
    5                                                  2
                                                     ----(+)
                                                       7


I have the solution but I don't understand it!
I tried, took some other cases but still nothing, maybe you guys can explain me why the solution is the way it is.
This is how sum(N, K) is calculated:

sum(N, K) = minimum noise level clearing the building K times (K>=0)

C++ code:
int sum(int n)
 {
    return n * (n + 1) / 2;
 }

int solve(int n, int k)
{
    int p = k + 1;
    int mp = n/p;
    int bp = (n+p-1)/p;
    int nmaj = n%p;
    int nmic = p-nmaj;

    return nmic * sum(mp) + nmaj*sum(bp);
}

What is the purpose of each variable? 
What does nmic * sum(mp) and nmaj*sum(bp) computes?
!!!!
For the example above (at the beginning - N=5, K=2), I debugged the code and wrote for N=5, K=0..2 the value for each variable, hoping to get the idea but no succes. I am going to post them for you, maybe you'll get the idea and then explain to me (I am a novice in algorithmic problems).
Here is it:
variable values for each case
I repeat, I tried for some hours to understand but no succes. And it's not a homework. Thank you!
 A: The assumption behind this program for finding a solution is that the optimal thing is to divide the $N$ days into $K+1$ periods as evenly as possible.
I.e., if you have $N=12$ days and you can clear $K=2$ times, the best thing is to have $K+1=3$ periods of $12/3 = 4$ days each.
If the days where $N=13$ then the best thing was to have one period of $5$ days and the other two periods of $4$ days each.
This is what the functions does. It takes $n$ days, it divides that in $p=k+1$ periods. Among these, $p$ periods there will be a certain number of periods of length $c$ and other of length $c+1$ (if $n$ is not divisible by $p$).
Example, $n=17$ and $k=4$. You have $k+1=5$ periods. The division will be $3$ periods of length $3$ and the remaining two periods of length $4$. Indeed $3\cdot3 +2\cdot4=17$.
The function sum(n) simply computes the cumulate noise over $n$ days. It is the $n$-th triangular number $1+2+\cdots+n$ which is equal to $n(n+1)/2$.
A main ingredient of this program is that in C language if you compute $a/b$ between positive integers, the result is $\lfloor a/b\rfloor$, i.e., the integer part of the division.
Let's analyze function solve.
int p=k+1;
int  mp = n/p;

Here $p$ is simply $k+1$, i.e., the number of periods in which we want to divide $n$. The variable $mp$, which is equal to $\lfloor n/p\rfloor$, it is simply the number of consecutive days you will get if $n$ is divisible by $p$, i.e., if there is not rest in the division $n/p$.
For example, if $n=17$ and you want to divide that in $p=5$ periods you get $mp=3$, because in a solution with $5$ periods you will have the least periods equal to $3$.
int bp = (n+p-1)/p;

This  computes the length of the longer periods. If $n$ is a multiple of $p$, then $\lfloor(n+p-1)/p$ will be equal to $n/p$, because adding $p-1$ to $n$ is not enough to reach the next multiple of $p$. 
Instead, if $n$ is not a precise multiple of $p$, we will have $\lfloor(n+p-1)/p = \lfloor n/p\rfloor + 1$.
So, if $n=17$ and $p=5$, we have $mp=3$ and $bp=4$ (because $17$ is not a multiple of $5$).
If $n=20$ and $p=5$, we have $mp=bp=4$ because $20$ is a multiple of $5$.
So now we have computed how many days are in the short periods ($mp$) and how many days in the long periods ($bp$) (and $mp=bp$ if $n$ is a multiple of $p$).
Now we have just to compute how many short and how many long periods are there.
int nmaj=n%p;

This simply compute the remainder in the division $n/p$. This will be the number of long periods. It could be $0$.
The number of short periods will be equal to the number of periods minus $nmaj$, i.e.,
int nmic=p-nmaj;

Now we can compute the total of noise. We have $nmic$ periods of $mp$ days each, and $nmaj$ periods of $bp$ days each, hence the total is 
return nmic * sum(mp) + nmaj*sum(bp);

