Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int atualizaVendendo (int array[], int size) {
- for (int i = 0; i < size; i++) {
- if (array[i] < 0) {
- return i;
- }
- }
- return -1;
- }
- int atualizaComprando (int array[], int size) {
- for (int i = 0; i < size; i++) {
- if (array[i] > 0) {
- return i;
- }
- }
- return -1;
- }
- int main() {
- while (1) {
- int vizinhos;
- cin >> vizinhos;
- long long trabalho = 0;
- if (vizinhos == 0) {
- break;
- }
- int * array = new int[vizinhos];
- for (int i=0; i<vizinhos; i++)
- cin >> array[i];
- int vendendo = 0;
- int comprando = 0;
- while (vendendo != -1 && comprando != -1) {
- vendendo = atualizaVendendo(array, vizinhos);
- comprando = atualizaComprando(array, vizinhos);
- if (abs(array[vendendo]) >= array[comprando]) {
- trabalho += array[comprando] * abs(comprando - vendendo);
- array[vendendo] = (abs(array[vendendo]) - array[comprando]) * -1;
- array[comprando] = 0;
- } else {
- trabalho += abs(array[vendendo]) * abs(comprando - vendendo);
- array[comprando] = array[comprando] - abs(array[vendendo]);
- array[vendendo] = 0;
- }
- }
- std::cout << trabalho << std::endl;
- delete[]array;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement