Colorful Intervals

시간 제한1초메모리 제한2048 MB

요약
색 배열이 주어질 때, 모든 색을 한 번 이상 포함하도록 두 구간을 골라 보게 되는 그림 수의 합을 최소화한다.
난이도

보통10점 중 6점

유형
배열, 투 포인터, 완전 탐색, 해시맵
정답자
아직 제출이 없습니다

문제

The Museum of Contemporary Art is holding a painting gallery focused on modern art, especially Monochromatic style paintings, which use only a single color. The gallery displays nn paintings arranged in a line.

The ICPC wants to bring students on an excursion to the gallery to spark their interest in art. However, the students are programmers, and everyone knows programmers only care about the colors of these modern paintings. They are also somewhat impatient. To keep their attention and to ensure they see every color without overwhelming them, the organizer decided to show them exactly two intervals of painting. This approach balances their short attention span and ensures all colors are represented. The task is to find two intervals of paintings such that each color appears at least once in at least one of the intervals, and the total number of paintings the students need to see is minimized.

입력

The input consists of a single line containing a non-negative integer nn (2≤n≤20002 \le n \le 2000), indicating the number of paintings. This is followed by nn lines, each containing a string representing the color of a painting. Each color is represented by a non-empty lowercase string with a length of less than 2020. It is guaranteed that there are at least 22 and at most 5050 different colors in the input.

출력

In the output, print the minimum number of paintings the ICPC students need to see, which is the sum of the lengths of the two intervals.

예제2

  1. 예제 1

    입력
    5
    blue
    red
    blue
    black
    red
    
    예상 출력
    3
    
  2. 예제 2

    입력
    8
    peachfuzz
    livingcoral
    livingcoral
    teal
    teal
    livingcoral
    livingcoral
    coral
    
    예상 출력
    5