cses

CSES Solutions (0x6A73)
git clone https://git.pngu.org/cses
Log | Files | Refs

commit 1bd17a77301438997ccc4e3eaf7ebe04474a5767
parent dcef6fef864949bcde09907c120180733e5d37bc
Author: Jack Sangdahl <jack@pngu.org>
Date:   Wed, 19 Feb 2025 11:48:40 -0700

1623. apple division

Diffstat:
Aintroductory_problems/apple_division.cpp | 23+++++++++++++++++++++++
Mintroductory_problems/readme.md | 7+++++++
2 files changed, 30 insertions(+), 0 deletions(-)

diff --git a/introductory_problems/apple_division.cpp b/introductory_problems/apple_division.cpp @@ -0,0 +1,23 @@ +// 1623. Apple Division +#include <bits/stdc++.h> +using namespace std; + +int main() { + const long long max = INT_MAX; + static int n, p[20]; + long long a, b, best = max; + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%d", &p[i]); + for (int i = 0; i < (1 << n); i++) { + a = b = 0; + for (int j = 0; j < n; j++) { + if (i & (1 << j)) + a += p[j]; + else + b += p[j]; + } + best = min(best, abs(a - b)); + } + printf("%lld\n", best); +} diff --git a/introductory_problems/readme.md b/introductory_problems/readme.md @@ -112,3 +112,10 @@ The goal is to move all the disks to the right stack using the middle stack. On **Input**: The only input line has an integer `n`: the number of disks. (Example: 2) **Output**: First print an integer `k`: the minimum number of moves. After this, print `k` lines that describe the moves. Each line has two integers `a` and `b`: you move a distk from stack `a` to stack `b`. (Example: 3 12 13 23) + +### Apple Division +There are `n` apples with known weights. Your task is to divide the apples into two groups so that the difference between the weights of the groups is minimal. + +**Input**: The first input line has an integer `n`: the number of apples. The next input line has `n` integers `p1, p2, ..., pn`: the weight of each apple. (Example: 5`\n`3 2 7 4 1) + +**Output**: Print one integer: the minimum difference between the weights of the groups. (Example: 1)