Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	seq := make([]int, 360)
	N := io.NextInt()
	for i := 0; i < N; i++ {
		M, D, V, S := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
		M = (M - 1) * 30
		D--
		for j := M + D; j < M+D+V; j++ {
			seq[j%360] = intMax(S, seq[j%360])
		}
		for j := 0; j < S; j++ {
			seq[(M+D-j+360)%360] = intMax(S-j, seq[(M+D-j+360)%360])
			seq[(M+D+V+j-1)%360] = intMax(S-j, seq[(M+D+V+j-1)%360])
		}
	}
	ans := 360
	for _, val := range seq {
		ans = intMin(val, ans)
	}
	io.Println(ans)
}

func intMin(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func intMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n;i++)
#define INF 1e9
int main(){
    int n; cin>>n;
    using pii = pair<int,int>;
    using piii = pair<pii,int>;
    vector<pii>range;
    vector<int>nd(361,0);
    vector<int>s;
    rep(i,n){
        int m,d,v,_s; cin>>m>>d>>v>>_s;
        pii temp;
        temp.first=(m-1)*30+d;
        temp.second=temp.first+v-1;  if(temp.second>360)temp.second-=360;
        range.push_back(temp);
        if(temp.first<=temp.second)
            for(int i=temp.first;i<=temp.second;i++)
                nd[i]=max(nd[i],_s);
        else{
            for(int i=temp.first; i<=360;i++)nd[i]=max(nd[i],_s);
            for(int i=1; i<=temp.second;i++)nd[i]=max(nd[i],_s);
        }
        
        s.push_back(_s);
    }
    
    for(int day=1; day<=360;day++){
        for(int i=0; i<n;i++){
            pii t=range[i];
            pii value;
            if(t.first>=day)value.first=t.first-day;
            else value.first=360-day+t.first;
            if(t.second<=day)value.second=day-t.second;
            else value.second=360-day+t.second;
            nd[day]=max(nd[day],max(0,s[i]-min(value.first,value.second)));
        }
    }
    int ans=INF;
    for(int i=1; i<=360;i++)ans=min(ans,nd[i]);
    cout<<ans<<endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n;i++)
#define INF 1e9
int main(){
    vector<int>array(600,0);
    int n; cin>>n;
    using pii = pair<int,int>;
    using piii=pair<pii,int>;
    vector<piii>holiday;
    for(int i=0;i<n;i++){
        int m,d,v,s; cin>>m>>d>>v>>s;
        holiday.push_back(piii(pii((m-1)*30+d,v),s));
        int x=(m-1)*30+d;
        int cnt=0;
        for(int i=0;i<v;i++,cnt++){
            array[x+cnt]=max(array[x+cnt],s);
            if(x+cnt==360){x=0,cnt=0;}
        }
    }
    for(int j=1;j<=360;j++){
        for(int i=0;i<holiday.size();i++){
            int fi=(holiday[i].first.first-j);
            int sec=(j-(holiday[i].first.first+holiday[i].first.second-1));
            if(fi<0)fi=360-j+holiday[i].first.first;
            if(sec<0)sec=j+360-(holiday[i].first.first+holiday[i].first.second-1);
            array[j]=max(array[j],max(0,holiday[i].second-min(fi,sec)));
        }
    }
    int ans=INF;
    for(int i=1; i<=360;i++)ans=min(ans,array[i]);
    cout<<ans<<endl;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
using namespace std;
int inf = 1000000000;


int main(void) {
  int n;
  cin >> n;
  vector<int> m(n);
  vector<int> d(n);
  vector<int> v(n);
  vector<int> s(n);
  vector<int> c(12 * 30, 0);
  for(int i=0; i<n; i++){
    cin >> m[i] >> d[i] >> v[i] >> s[i];
    m[i]--; d[i]--;
    int tmp = m[i] * 30 + d[i];
    for(int j=tmp; j<tmp+v[i]; j++){
      if( j < 0 ) c[(720+j)%360] = max(c[(720+j)%360], s[i]);
      else if( j >= 360 ) c[j%360] = max(c[(j)%360], s[i]);
      else c[j] = max(c[j], s[i]);
    }
    for(int j=1; j<=s[i]; j++){
      if( tmp - j < 0 ) c[(720+tmp-j)%360] = max({0, c[(720+tmp-j)%360], s[i]-j});
      else if( tmp - j >= 360 ) c[(tmp-j)%360] = max({0, c[(tmp-j)%360], s[i]-j});
      else c[tmp-j] = max({0, c[tmp-j], s[i]-j});
    }
    for(int j=0; j<s[i]; j++){
      if( tmp+v[i]+j < 0 ) c[(720+tmp+v[i]+j)%360] = max({0, c[(720+tmp+v[i]+j)%360], s[i]-j-1});
      else if( tmp+v[i]+j >= 360 ) c[(tmp+v[i]+j)%360] = max({0, c[(tmp+v[i]+j)%360], s[i]-j-1});
      else c[tmp+v[i]+j] = max({0, c[tmp+v[i]+j], s[i]-j-1});
    }
  }

  cout << *min_element(c.begin(), c.end()) << endl;


  return 0;
}

// EOF

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define inRange(x,a,b) ((a <= x && x <= b) || (a <= x+360 && x+360 <= b))

struct holiday{
    int from, to, s;
};

int diff(int from, int to){
    if(from > to)   to += 360;
    return to-from;
}

int main(){
    int n;
    cin >> n;
    vector<holiday> v;
    int m, d, V, s;
    for(int i = 0; i < n; i++){
        cin >> m >> d >> V >> s;
        v.push_back(holiday({30*(m-1)+d, 30*(m-1)+d+V-1, s}));
    }
    int ans = 1<<30;
    for(int i = 1; i <= 360; i++){
        int tmp = 0;
        for(int j = 0; j < n; j++){
            if(inRange(i,v[j].from,v[j].to)){
                tmp = max(tmp, v[j].s);
            }else{
                tmp = max(tmp, max(0, v[j].s-min(diff(v[j].to, i), diff(i, v[j].from))));
            }
        }
        ans = min(ans, tmp);
    }
    cout << ans << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int N;
int M[100],D[100],V[100],S[100];
int fi[100],se[100];



int main(){
  cin>>N;
  for(int i=0;i<N;i++){
    cin>>M[i]>>D[i]>>V[i]>>S[i];
    fi[i]=(M[i]-1)*30+(D[i]-1);
    se[i]=(fi[i]+V[i])%360;
  }
  int ans=1e9;
  for(int x=0;x<360;x++){
    int val=0;
    for(int j=0;j<N;j++){
      int tmp = -1;
      if( (fi[j]<=x&&x<se[j]) || (fi[j]>=se[j]&&(fi[j]<=x || x<se[j])) ){
        tmp=S[j];
      }else{
        int a=(x-se[j]+1+360)%360;
        int b=(fi[j]-x+360)%360;
        tmp=S[j]-min(a,b);
      }

      val=max(val,tmp);
    }
    ans=min(ans,val);
  }
  cout<<ans<<endl;
  return 0;
}