티스토리 뷰

https://www.acmicpc.net/problem/13398

 

13398번: 연속합 2

첫째 줄에 정수 n(1 ≤ n ≤ 100,000)이 주어지고 둘째 줄에는 n개의 정수로 이루어진 수열이 주어진다. 수는 -1,000보다 크거나 같고, 1,000보다 작거나 같은 정수이다.

www.acmicpc.net

const fs = require('fs');
const [n, ...num] = fs.readFileSync("./dev/stdin").toString().trim().split('\n');
const N = +n;
const nums1 = num[0].split(' ').map(Number);
const nums2 = num[0].split(' ').map(Number).reverse();
const dp1 = new Array(N).fill(0);
const dp2 = new Array(N).fill(0);

dp1[0] = nums1[0]
let max = nums1[0]
for(let i = 1; i<N; i++){
  dp1[i] = Math.max(dp1[i-1]+nums1[i],nums1[i]);
  max = Math.max(max,dp1[i]);
}

dp2[0] = nums2[0];
for(let i =1 ; i<N; i++){
  dp2[i] = Math.max(dp2[i-1]+nums2[i], nums2[i])
}

for(let i = 1; i<N-1; i++){
  const temp = dp1[i-1]+dp2[N-2-i]
  max = Math.max(temp,max)  
}

console.log(max)
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
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
글 보관함