Cod sursa(job #2436392)

Utilizator CiobaCatalinCioba Catalin Adrian CiobaCatalin Data 5 iulie 2019 17:10:06
Problema Subsecventa de suma maxima Scor 65
Compilator java Status done
Runda Arhiva educationala Marime 2.38 kb
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;

class Main {
    public static void main(String[] args) {
        try (FileWriter fout = new FileWriter(new File("ssm.out"))) {
            FastReader in = new FastReader(new FileReader(new File("ssm.in")));

            int n = in.nextInt();

            int arr[] = new int[n];

            for (int i = 0; i < n; ++i) {
                arr[i] = in.nextInt();
            }

            int sumSoFar = arr[0];
            int minSoFar = arr[0];
            int minSoFarIdx = 0;
            
            int maxSubsequence = arr[0];
            int maxSubEnd = 0;
            int maxSubStart = 0;

            for (int i = 1; i < n; ++i) {
                sumSoFar += arr[i];

                if (sumSoFar < minSoFar) {
                    minSoFar = sumSoFar;
                    minSoFarIdx = i;
                }
                if (sumSoFar - minSoFar > maxSubsequence) {
                    maxSubsequence = sumSoFar - minSoFar;
                    maxSubEnd = i;
                    maxSubStart = minSoFarIdx + 1;
                }
            }
            fout.write(maxSubsequence + " " + 
            (maxSubStart + 1) + " " + (maxSubEnd + 1));

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }


    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        // public FastReader() {
        // br = new BufferedReader(new InputStreamReader(System.in));
        // }

        public FastReader(FileReader in) {
            br = new BufferedReader(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;
        }
    }
}