summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-03-23 20:27:48 +0800
committercrupest <crupest@outlook.com>2021-03-23 20:27:48 +0800
commit78a73830213b61ff42564432a23daf83ee9dc915 (patch)
treee62aa7c0b20bd3d7eea97fc7d234a2a2919b14ca
parentd0d850a9cbf0b2792c8ed1e01adbe5eb26db8cea (diff)
downloadsolutions-78a73830213b61ff42564432a23daf83ee9dc915.tar.gz
solutions-78a73830213b61ff42564432a23daf83ee9dc915.tar.bz2
solutions-78a73830213b61ff42564432a23daf83ee9dc915.zip
Add acwing 1247.
-rw-r--r--acwing/1247.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/acwing/1247.cpp b/acwing/1247.cpp
new file mode 100644
index 0000000..132c63c
--- /dev/null
+++ b/acwing/1247.cpp
@@ -0,0 +1,39 @@
+#include <cmath>
+#include <iostream>
+
+int main() {
+ std::ios_base::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int N, M;
+ std::cin >> N >> M;
+
+ long long result = 0;
+
+ long long min = 1e9 + 1;
+ long long max = -1e9 - 1;
+
+ for (int i = 0; i < N + M + 1; i++) {
+ long long a;
+ std::cin >> a;
+ max = std::max(a, max);
+ min = std::min(a, min);
+ if (M == 0) {
+ result += a;
+ } else {
+ result += std::abs(a);
+ }
+ }
+
+ if (M != 0 && max < 0) {
+ result += 2 * max;
+ }
+
+ if (M == 1 && min > 0) {
+ result -= 2 * min;
+ }
+
+ std::cout << result;
+
+ return 0;
+}