-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25633(BruteForce).java
More file actions
60 lines (51 loc) · 1.65 KB
/
Copy path25633(BruteForce).java
File metadata and controls
60 lines (51 loc) · 1.65 KB
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
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int arr[] = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
arr[0] = Integer.parseInt(st.nextToken());
for(int i = 1; i < N; i++){
int temp = Integer.parseInt(st.nextToken());
arr[i] = temp;
}
int dp[] = new int[N];
dp[0] = 1;
for(int i = 1; i < N; i++){
if(arr[i] <= arr[i - 1]){
arr[i] += arr[i - 1];
dp[i] = dp[i - 1] + 1;
}else{
int count = dp[i - 1];
int temp = arr[i - 1];
for(int j = i + 1; j < N; j++){
if(temp >= arr[j]){
temp += arr[j];
count++;
}else{
break;
}
}
int secCount = 1;
temp = arr[i];
for(int j = i + 1; j < N; j++) {
if(temp >= arr[j]){
temp += arr[j];
secCount++;
}else{
break;
}
}
if(count > secCount){
arr[i] = arr[i - 1];
dp[i] = dp[i - 1];
}else{
dp[i] = 1;
}
}
}
System.out.println(dp[N - 1]);
}
}