Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define _CRT_SECURE_NO_WARNINGS
- int main()
- {
- int iStonesValue = 0;
- scanf_s("%d", &iStonesValue);
- int iTmp,iSum = 0;
- int *heap = (int*) malloc (iStonesValue * sizeof(int));
- for (int i = 0; i < iStonesValue; i++)
- {
- scanf_s("%d", &heap[i]);
- iSum += heap[i];
- }
- for (int i = 0; i < iStonesValue-1; i++)
- {
- if(heap[i] > heap[i+1])
- {
- iTmp = heap[i];
- heap[i] = heap[i + 1];
- heap[i + 1] = iTmp;
- }
- }
- int iFirstHeapWeight = abs(iSum - heap[iStonesValue - 1]);
- int iSecondHeapWeight = heap[iStonesValue - 1];
- int iHeapWeightDifference = abs(iFirstHeapWeight - iSecondHeapWeight);
- for (int i = 0; i < iStonesValue-1; i++)
- {
- if (iHeapWeightDifference >= abs(iFirstHeapWeight - iSecondHeapWeight))
- {
- iHeapWeightDifference = abs(iFirstHeapWeight - iSecondHeapWeight);
- }
- iSecondHeapWeight += heap[i];
- iFirstHeapWeight = abs(iSum - iSecondHeapWeight);
- }
- printf("%d", iHeapWeightDifference);
- free(heap);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement