using System.Collections.Generic; using System; public class Deque { T[] buf; int offset, count, capacity; public int Count { get { return count; } } public Deque(int cap) { buf = new T[capacity = cap]; } public Deque() { buf = new T[capacity = 16]; } public void Init() { count = 0; } public T this[int index] { get { return buf[getIndex(index)]; } set { buf[getIndex(index)] = value; } } private int getIndex(int index) { if (index >= capacity) throw new IndexOutOfRangeException("out of range"); var ret = index + offset; if (ret >= capacity) ret -= capacity; return ret; } public void PushFront(T item) { if (count == capacity) Extend(); if (--offset < 0) offset += buf.Length; buf[offset] = item; ++count; } public T PopFront() { if (count == 0) throw new InvalidOperationException("collection is empty"); --count; var ret = buf[offset++]; if (offset >= capacity) offset -= capacity; return ret; } public void PushBack(T item) { if (count == capacity) Extend(); var id = count++ + offset; if (id >= capacity) id -= capacity; buf[id] = item; } public T PopBack() { if (count == 0) throw new InvalidOperationException("collection is empty"); return buf[getIndex(--count)]; } public void Insert(int index, T item) { if (index > count) throw new IndexOutOfRangeException(); this.PushFront(item); for (int i = 0; i < index; i++) this[i] = this[i + 1]; this[index] = item; } public T RemoveAt(int index) { if (index < 0 || index >= count) throw new IndexOutOfRangeException(); var ret = this[index]; for (int i = index; i > 0; i--) this[i] = this[i - 1]; this.PopFront(); return ret; } private void Extend() { T[] newBuffer = new T[capacity << 1]; if (offset > capacity - count) { var len = buf.Length - offset; Array.Copy(buf, offset, newBuffer, 0, len); Array.Copy(buf, 0, newBuffer, len, count - len); } else Array.Copy(buf, offset, newBuffer, 0, count); buf = newBuffer; offset = 0; capacity <<= 1; } } public class Hello { public static Deque d; static void Main() { var n = int.Parse(Console.ReadLine().Trim()); string[] line = Console.ReadLine().Trim().Split(' '); var a = new List(); var p = 0; var m = 0; for (int i = 0; i < n; i++) { if (line[i] == "+") p++; else if (line[i] == "-") m++; else a.Add(int.Parse(line[i])); } d = new Deque(n); a.Sort(); foreach (var x in a) d.PushBack(x); getAns(p, m, n - p - m, a); } static long getMin2(int n) { var dc = d.Count; var k = 1; var res = 0L; var pc = 0; while (pc < dc) { for (int i = 0; i < n; i++) { var w = d.PopBack(); res += w * k; pc++; if (pc == dc) break; } k *= 10; } return res; } static long getMax() => d.PopBack(); static long getMax(int t) { var a = ""; for (int i = 0; i < t; i++) a += d.PopBack().ToString(); return long.Parse(a); } static long getMin() => d.PopFront(); static void getAns(int p, int m, int c, List a) { var fc = p + m; var tmax = getMax(c - fc); for (int i = 0; i < p; i++) tmax += getMax(); for (int i = 0; i < m; i++) tmax -= getMin(); foreach (var x in a) d.PushBack(x); long tmin; if (m == 0) tmin = getMin2(fc + 1); else { if (p == 0) { tmin = getMin(); tmin -= getMax(c - fc); m--; for (int i = 0; i < m; i++) tmin -= getMax(); } else { tmin = -getMax(c - fc); m--; for (int i = 0; i < p+1; i++) tmin += getMin(); for (int i = 0; i < m; i++) tmin -= getMax(); } } Console.WriteLine("{0} {1}", tmax, tmin); } }