Algorithm


Problem Name: 2 AD-HOC - beecrowd | 2567

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2567

Virus

 

By Flávio Zavan, UFPR BR Brazil

Timelimit: 1

The Department of Public Health of Nlogonia has just issued an alert. A virus is infecting the entire population.

After a lot of studies, the Nlogonia’s researchers found out that after infiltrating a host body, the viruses match in pairs to become lethal. The level of lethality of an infection is determined by the sum of the age difference in days of the paired viruses. Virues not paired don’t influence the level.

Thus, if there are 4 viruses in the host body with ages (in days), equals to:

4, 10, 9, 43

And they are paired as follows:

4 with 9, 43 with 10

Then the level of lethality would be (9 - 4) + (43 - 10) = 38.

The Department of Public Health of Nlognia asked you to write a program that, given the virus count in a host and the age of each of them, calculate the maximum level of lethality that the infection can take.

 

Input

 

The input contains several test cases. The first line of each test case consists of an integer N (1 ≤ N ≤ 1000), the number of viruses in the host body. The following line contains N integers ai (0 ≤ ai ≤ 1000) separated by blank spaces, the ages (in days) of all the viruses in the host body.

The input ends with end-of-file (EOF).

 

Output

 

For each test case, print a single line with the maximum lethality level that the infection can take.

 

 

 

Input Sample Output Sample

4
4 9 43 10
3
0 100 50

40
100

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <stdlib.h>

int compara(int *, int *);

void main ()
{

	int soma;
	int n, i, j;

	while (scanf("%u", &n) != EOF)
	{

		int vetor[n];

		for (i = 0; i  <  n; ++i)
			scanf("%d", &vetor[i]);

		qsort(vetor, n, sizeof(int), compara);

		j = n - 1;
		soma = i = 0;

		while (j > i)
			soma += abs(vetor[i++] - vetor[j--]);

		printf("%d\n", soma);

	}

}

int compara(int *a, int *b)
{

	if (*a == *b)
		return 0;
	else if (*a > *b)
		return 1;
	else
		return -1;

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4
4 9 43 10
3
0 100 50

Output

x
+
cmd
40
100

#2 Code Example with C++ Programming

Code - C++ Programming


#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        vector<int> entrada;
        for (int i = 0; i  <  n; i++) {
            int davez;
            scanf("%d", &davez);
            entrada.push_back(davez);
        }
        sort(entrada.begin(), entrada.end());
        int resp = 0;
        int ini = 0, fim = n - 1;
        while (ini  < = fim) {
            resp += entrada[fim] - entrada[ini];
            ini++;
            fim--;
        }
        printf("%d\n", resp);
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4
4 9 43 10
3
0 100 50

Output

x
+
cmd
40
100

#3 Code Example with Python Programming

Code - Python Programming


while True:
    try:
        input()
        a=list(map(int,input().split()))
        aux=0
        while len(a) >= 2:
            maior,menor=max(a),min(a)
            a.remove(maior)
            a.remove(menor)
            aux+=(maior-menor)
        print(aux)
    except EOFError:break 
Copy The Code & Try With Live Editor

Input

x
+
cmd
4
4 9 43 10
3
0 100 50

Output

x
+
cmd
40
100
Advertisements

Demonstration


Previous
#2554 Beecrowd Online Judge Solution 2554 Pizza Before BH Solution in C, C++, Java, Js and Python
Next
#2568 Beecrowd Online Judge Solution 2568 Actions Solution in C, C++, Java, Js and Python