SOLUTION INFO
C++ · main.cpp
- 작성자
- ccocco0609
- 공동 작성자
- 없음
#include <bits/stdc++.h>
using namespace std;
// 전역에 배열 또는 변수를 선언한 경우 0으로 초기화 됩니다.
int wine[10001];
int dp[10001];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N; cin >> N;
for (int i = 1; i <= N; i++) cin >> wine[i];
dp[1] = wine[1];
dp[2] = wine[1] + wine[2];
for (int i = 3; i <= N; i++) {
dp[i] = max(wine[i] + dp[i - 2], max(wine[i] + wine[i - 1] + dp[i - 3], dp[i - 1]));
}
cout << dp[N];
return 0;
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.
SOLUTION INFO
Java · Main.java
- 작성자
- vswngjs
- 공동 작성자
- 없음
import java.util.*;
import java.io.*;
public class Main {
public static void main(String [] args) {
FastReader rd = new FastReader();
int N = rd.nextInt();
int[] grapes = new int[N + 1];
int[] dp = new int[N + 1];
for (int i = 1; i <= N; i++) {
grapes[i] = rd.nextInt();
}
if(N == 1) {
System.out.println(grapes[1]);
}
else {
dp[1] = grapes[1];
dp[2] = grapes[1] + grapes[2];
for (int i = 3; i <= N; i++) {
dp[i] = Math.max(dp[i - 2] + grapes[i], dp[i - 3] + grapes[i - 1] + grapes[i]);
dp[i] = Math.max(dp[i], dp[i - 1]);
}
System.out.println(dp[N]);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.
SOLUTION INFO
Python · main.py
- 작성자
- wassup37
- 공동 작성자
- tony9402
import sys
def input():
return sys.stdin.readline().rstrip()
N = int(input())
wine = [0] + [int(input()) for i in range(N)] # 인덱스가 1부터 시작하도록 만듭니다.
if N == 1: # n이 1일 경우 맨 처음 포도주를 마시는게 최대입니다.
print(wine[1])
else:
dp = [0, wine[1], wine[1] + wine[2]] # n 인덱스까지의 최댓값을 저장하는 리스트
for i in range(3, N+1): # 3부터 n까지 dp 테이블을 채웁니다.
dp.append(max(dp[i - 1], dp[i - 2] + wine[i], dp[i - 3] + wine[i - 1] + wine[i]))
print(dp[N]) # 포도주 잔의 개수가 n개일 때, 최대로 마실 수 있는 포도주의 양 출력
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.