[BOJ] 2104 부분배열 고르기

Time Lapse :NONE

2104.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstdio>

using namespace std;

#define MAXSIZE 100001

int N;
int arr[MAXSIZE];
int start_index;
int end_index;

long long compare(int start, int end)
{
if (start == end)
return (long long)arr[start] * arr[start];

long long ret;
int mid = (start + end) / 2;
int left = mid;
int right = mid + 1;

ret = max(compare(start, left), compare(right, end));

long long sum = arr[left] + arr[right];
long long minvalue = min(arr[left], arr[right]);

ret = max(ret, sum*minvalue);

while (left>start || right<end)
{
if (right < end && (left == start || arr[left-1] < arr[right+1]))
{
sum += arr[++right];
minvalue = min(minvalue, (long long)arr[right]);
}
else
{
sum += arr[--left];
minvalue = min(minvalue, (long long)arr[left]);
}
long long comparewith = sum * minvalue;
ret = max(ret, comparewith);
}

return ret;
}

int main(void)
{
cin >> N;
for (int i = 0; i < N; i++)
{
cin >> arr[i];
}
cout << compare(0, N-1);

}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/2104/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.