알고리즘

[알고리즘] 백준 5557 - 1학년 (C++)

blueberrysoda 2025. 1. 13. 14:30

[문제 링크] : https://www.acmicpc.net/problem/5557

#include <iostream>
using namespace std;

int N;
long long Arr[101];
long long DP[101][21];

int main(){
    cin >> N;
    for(int i=0; i<N; i++){
        cin >> Arr[i];
    }

    DP[0][Arr[0]] = 1;

    for(int i=1; i<N-1; i++){
        for(int j=0; j<21; j++){
            if(DP[i-1][j] == 0){
                continue;
            }
            
            long long a = j + Arr[i];
            long long b = j - Arr[i];

            if(a >= 0 && a <= 20){
                DP[i][a] += DP[i-1][j];
            }
            if(b >= 0 && b <= 20){
                DP[i][b] += DP[i-1][j];
            }
        }        
    }

    cout << DP[N-2][Arr[N-1]] << "\n";
    return 0;
}