#include <stdio.h> #include <iostream> using namespace std; int bead[15]; int N; int firstN; int Max; void shift(int idx) { for(int i = idx; i <= N-1; i++) { bead[i] = bead[i+1]; } N--; } void back_up(int idx, int value) { for(int i = N+1; i > idx; i--) { bead[i] = bead[i-1]; } bead[idx] = value; N++; } void permutation(int energy, int cnt) { if(cnt == firstN-2 + 1) { if(energy > Max) { Max = energy; } return; } for(int i = 2; i <= N-1; i++) { int choiceIdx = i; int choiceValue = bead[i]; int tempEnergy = bead[choiceIdx-1] * bead[choiceIdx+1]; shift(choiceIdx); permutation(energy + tempEnergy, cnt+1); back_up(choiceIdx, choiceValue); } } int main(void) { // freopen("B16198_input.txt", "r", stdin); scanf("%d", &N); firstN = N; for(int i = 1; i <= N; i++) { scanf("%d", &bead[i]); } permutation(0, 1); cout << Max; return 0; } | cs |
'Baekjoon > BruteForce' 카테고리의 다른 글
[백준 15684] 사다리 조작 (조합) (C/C++) (★★★) (2) | 2019.11.20 |
---|---|
[백준 15686] 연산자 끼워넣기(2) (순열) (C/C++) (0) | 2019.11.20 |
[백준 14502] 연구소 (조합) (C/C++) (★★) (0) | 2019.11.20 |
[백준 14888] 연산자 끼워넣기 (순열) (C/C++) (0) | 2019.11.20 |
[백준 14501] 퇴사 (조합) (C/C++) (★) (0) | 2019.11.20 |