Submission #5818007


Source Code Expand

import std.functional,
       std.algorithm,
       std.typecons,
       std.bigint,
       std.string,
       std.traits,
       std.array,
       std.range,
       std.stdio,
       std.conv,
       std.math;

T read_num(T)() { return readln.chomp.to!T; }
T[] read_nums(T)() { return readln.chomp.split.to!(T[]); }

enum INF = 1L << 30;

T max(T)(T a, T b) { return a < b ? b : a; }
T abs(T)(T x) { return x < 0 ? -x : x; }
bool chmax(T)(ref T a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
bool chmin(T)(ref T a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
void main() {
  long N = read_num!long;
  long[] A = read_nums!long;
  long[] negs, poss;
  long ans;

  A.sort!"a<b";
  foreach (a; A) {
    if (a < 0) {
      negs ~= a;
    } else {
      poss ~= a;
    }
  }

  if (negs.length & 1) {
    long l = -INF;
    long r = INF;
    foreach (x; negs) {
      chmax(l, x);
    }
    foreach (x; poss) {
      chmin(r, x);
    }
    ans -= min(abs(l), abs(r)) * 2;
  }
    foreach (a; A) {
      ans += abs(a);
    }

  writeln(ans);
}

Submission Info

Submission Time
Task D - Flipping Signs
User alphaKAI
Language D (DMD64 v2.070.1)
Score 400
Code Size 1187 Byte
Status AC
Exec Time 40 ms
Memory 4520 KiB

Judge Result

Set Name All Sample
Score / Max Score 400 / 400 0 / 0
Status
AC × 23
AC × 3
Set Name Test Cases
All sample_01, sample_02, sample_03, testcase_01, testcase_02, testcase_03, testcase_04, testcase_05, testcase_06, testcase_07, testcase_08, testcase_09, testcase_10, testcase_11, testcase_12, testcase_13, testcase_14, testcase_15, testcase_16, testcase_17, testcase_18, testcase_19, testcase_20
Sample sample_01, sample_02, sample_03
Case Name Status Exec Time Memory
sample_01 AC 1 ms 256 KiB
sample_02 AC 1 ms 256 KiB
sample_03 AC 1 ms 256 KiB
testcase_01 AC 25 ms 3708 KiB
testcase_02 AC 10 ms 1532 KiB
testcase_03 AC 39 ms 4356 KiB
testcase_04 AC 13 ms 2812 KiB
testcase_05 AC 3 ms 2556 KiB
testcase_06 AC 38 ms 4520 KiB
testcase_07 AC 23 ms 3324 KiB
testcase_08 AC 28 ms 3608 KiB
testcase_09 AC 26 ms 3324 KiB
testcase_10 AC 37 ms 4404 KiB
testcase_11 AC 21 ms 3672 KiB
testcase_12 AC 15 ms 2944 KiB
testcase_13 AC 40 ms 4456 KiB
testcase_14 AC 40 ms 4456 KiB
testcase_15 AC 38 ms 4404 KiB
testcase_16 AC 40 ms 4436 KiB
testcase_17 AC 40 ms 4436 KiB
testcase_18 AC 16 ms 3964 KiB
testcase_19 AC 1 ms 256 KiB
testcase_20 AC 34 ms 4376 KiB