728x90
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
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
int n = stoi(br.readLine());
String[] str = br.readLine().split(" ");
int[] DP = new int[n+1];
int max = Integer.MIN_VALUE;
for(int i=1; i<=n; i++) {
int num = stoi(str[i-1]);
DP[i] = (DP[i-1] + num > num)? DP[i-1] + num : num;
max = Math.max(max,DP[i]);
}
System.out.println(max);
// bw.write(sBuilder.toString());
// bw.flush();
// bw.close();
}
private static int stoi(String input) {
return Integer.parseInt(input);
}
}
|
cs |
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
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
int n = stoi(br.readLine());
String[] str = br.readLine().split(" ");
int[] DP = new int[n+1];
int max = Integer.MIN_VALUE;
for(int i=1; i<=n; i++) {
int num = stoi(str[i-1]);
// DP[i] = (DP[i-1] + num > 0)? DP[i-1] + num : 0;
if(DP[i-1]<0 && num>=0)
DP[i] = num;
else if(DP[i-1]<0 && num<0) {
DP[i]=(DP[i-1]>num)? DP[i-1] : num;
}
else
DP[i] = DP[i-1] + num;
max = Math.max(max,DP[i]);
}
System.out.println(max);
// bw.write(sBuilder.toString());
// bw.flush();
// bw.close();
}
private static int stoi(String input) {
return Integer.parseInt(input);
}
}
|
cs |
'알고리즘 풀이 > 백준' 카테고리의 다른 글
[백준][Java] 10610번 30 (문자열) (0) | 2021.09.20 |
---|---|
[백준][Java] 2015번 수들의 합 4 (누적합) (0) | 2021.09.19 |
[백준][Java] 21318번 피아노 체조 (누적합) (0) | 2021.09.19 |
[백준][Java] 11660번 구간 합 구하기 5 (누적합) (0) | 2021.09.19 |
[백준][Java] 19951번 태상이의 훈련소 생활 (누적합) (0) | 2021.09.19 |