시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB41216913043.333%

문제

수열의 값이란 수열에서 가장 큰 원소와 가장 작은 원소의 차이이다.

예를 들어, (3, 1, 7, 2)는 수열의 값이 6이고, (42, 42)는 0이다.

수열이 주어졌을 때, 모든 부분 수열의 값의 합을 출력하는 프로그램을 작성하시오 .

(3, 1, 7, 2)의 부분 수열은 총 10개 (3), (1), (7), (2), (3,1), (1,7), (7,2), (3,1,7), (1,7,2), (3,1,7,2) 가 있고, 각각의 수열의 값을 구한뒤 이를 합하면 31이 된다.

입력

첫째 줄에 수열의 크기 N(2 ≤ N ≤ 300,000)이 주어진다. 둘째 줄부터 N개의 줄에는 수열의 원소가 한 줄에 하나씩 주어진다. 원소는 100,000,000보다 크지 않은 양의 정수이다.

출력

첫째 줄에 입력으로 주어진 수열의 부분 수열의 값의 합을 출력한다.

예제 입력 1

3
1
2
3

예제 출력 1

4

예제 입력 2

4
7
5
7
5

예제 출력 2

12

예제 입력 3

4
3
1
7
2

예제 출력 3

31
W3sicHJvYmxlbV9pZCI6IjI4NjciLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJcdWMyMThcdWM1ZjRcdWM3NTggXHVhYzEyIiwiZGVzY3JpcHRpb24iOiI8cD5cdWMyMThcdWM1ZjRcdWM3NTggXHVhYzEyXHVjNzc0XHViNzgwIFx1YzIxOFx1YzVmNFx1YzVkMFx1YzExYyBcdWFjMDBcdWM3YTUgXHVkMDcwIFx1YzZkMFx1YzE4Y1x1YzY0MCBcdWFjMDBcdWM3YTUgXHVjNzkxXHVjNzQwIFx1YzZkMFx1YzE4Y1x1Yzc1OCBcdWNjMjhcdWM3NzRcdWM3NzRcdWIyZTQuPFwvcD5cclxuXHJcbjxwPlx1YzYwOFx1Yjk3YyBcdWI0ZTRcdWM1YjQsICgzLCAxLCA3LCAyKVx1YjI5NCBcdWMyMThcdWM1ZjRcdWM3NTggXHVhYzEyXHVjNzc0IDZcdWM3NzRcdWFjZTAs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VlbiB0aGUgbGFyZ2VzdCBhbmQgdGhlIHNtYWxsZXN0IG51bWJlciB3aXRoaW4gdGhhdCBzZXF1ZW5jZS4gRm9yIGV4YW1wbGUsIHZhbHVlIG9mIHNlcXVlbmNlICgzLCAxLCA3LCAyKSBpcyA2LCBhbmQgdmFsdWUgb2YgKDQyLCA0MikgaXMgMC4mbmJzcDs8XC9wPlxyXG5cclxuPHA+RmluZCB0aGUgc3VtIG9mIHZhbHVlcyBvZiBhbGwgc3Vic2VxdWVuY2VzIG9mIGNvbnNlY3V0aXZlIGVsZW1lbnRzIG9mIGEgZ2l2ZW4gc2VxdWVuY2UuJm5ic3A7PFwvcD5cclxuIiwiaW5wdXQiOiI8cD5UaGUgZmlyc3QgbGluZSBvZiBpbnB1dCBjb250YWlucyBhIHNpbmdsZSBpbnRlZ2VyIE4gKDIgJmxlOyBOICZsZTsgMzAwIDAwMCksIG51bWJlciBvZiBlbGVtZW50cyBvZiB0aGUgc2VxdWVuY2UuJm5ic3A7PFwvcD5cclxuXHJcbjxwPk5leHQgTiBsaW5lcyBjb250YWluIGVsZW1lbnRzIG9mIHRoZSBzZXF1ZW5jZS4gRWFjaCBlbGVtZW50IGlzIGEgcG9zaXRpdmUgaW50ZWdlciBub3QgZ3JlYXRlciB0aGFuIDEwMCAwMDAgMDAwLiZuYnNwOzxcL3A+XHJcbiIsIm91dHB1dCI6IjxwPlRoZSBmaXJzdCBhbmQgb25seSBsaW5lIG9mIG91dHB1dCBtdXN0IGNvbnRhaW4gdGhlIHJlcXVlc3RlZCBzdW0uJm5ic3A7PFwvcD5cclxuXHJcbjxwPiZuYnNwOzxcL3A+XHJcbiIsImhpbnQiOiIiLCJvcmlnaW5hbCI6IjEiLCJodG1sX3RpdGxlIjoiMCIsInByb2JsZW1fbGFuZ190Y29kZSI6IkVuZ2xpc2gifV0=

출처

Contest > Croatian Open Competition in Informatics > COCI 2010/2011 > Contest #3 5번

  • 잘못된 조건을 찾은 사람: august14
  • 문제를 번역한 사람: baekjoon
  • 데이터를 추가한 사람: cgiosy