2493번 탑 문제를 풀면 비교적 쉽게 접근할 수 있는 문제.
최댓값을 두 번 이상 가질 때 이를 체크해야겠다고 생각했는데, 큰 값을 저장하면서 풀면 따로 인덱스를 기억할 필요가 없었다.
#include <iostream>
using namespace std;
int N;
int arr[1001];
int startIdx = 1001;
int endIdx = -1;
int maxValue = -1;
int maxIdx1 = -1;
int maxIdx2 = -1;
int main()
{
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> N;
for (int i = 0; i < N; i++)
{
int idx, value;
cin >> idx >> value;
arr[idx] = value;
if (idx < startIdx) startIdx = idx;
if (idx > endIdx) endIdx = idx;
if (value > maxValue)
{
maxValue = value;
}
}
int firstIdx = -1;
int secondIdx = 1001;
int res = 0;
int val = 0;
for (firstIdx = startIdx; firstIdx < endIdx; firstIdx++)
{
if (arr[firstIdx] == maxValue)
{
break;
}
if (val < arr[firstIdx])
{
val = arr[firstIdx];
}
res += val;
}
val = 0;
for (secondIdx = endIdx; secondIdx >= startIdx; secondIdx--)
{
if (arr[secondIdx] == maxValue)
{
break;
}
if (val < arr[secondIdx])
{
val = arr[secondIdx];
}
res += val;
}
res += (secondIdx - firstIdx + 1) * maxValue;
cout << res;
}